OptionsForm.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * ImageDescriptionForm.cpp
  3. *
  4. * Created on: Oct 11, 2011
  5. * Author: Gapchich Vlad
  6. */
  7. #include "OptionsForm.h"
  8. #include <QCheckBox>
  9. #include <QPushButton>
  10. #include <QLabel>
  11. #include <QLineEdit>
  12. #include <QBoxLayout>
  13. #include <QMessageBox>
  14. #include <QApplication>
  15. #include <QDesktopWidget>
  16. #include <QFileDialog>
  17. #include <QKeyEvent>
  18. //! A constructor
  19. /*!
  20. * Constructor is responsible for allocating memory for all the GUI,
  21. * arranging and connecting them in the right order.
  22. */
  23. OptionsForm::OptionsForm(QWidget *aParent)
  24. : QWidget(aParent)
  25. {
  26. setWindowTitle(tr("Options"));
  27. PASCALpath_ = 0;
  28. layout_v_ = new QVBoxLayout(this);
  29. layout_PASCAL_root_ = new QHBoxLayout;
  30. layout_h_ = new QHBoxLayout;
  31. auto_color_generation_box_ = new QCheckBox(this);
  32. auto_color_generation_box_->setText(tr("Automatic label color generation"));
  33. button_set_PASCAL_root_ = new QPushButton(this);
  34. button_set_PASCAL_root_->setText(tr("set PASCAL root path"));
  35. edit_PASCAL_root_ = new QLineEdit("", this);
  36. edit_PASCAL_root_->setMinimumWidth(180);
  37. button_ok_ = new QPushButton(this);
  38. button_ok_->setText(tr("OK"));
  39. button_cancel_ = new QPushButton(this);
  40. button_cancel_->setText(tr("Cancel"));
  41. layout_v_->addWidget(auto_color_generation_box_);
  42. layout_v_->addLayout(layout_PASCAL_root_);
  43. layout_v_->addLayout(layout_h_);
  44. layout_PASCAL_root_->addWidget(button_set_PASCAL_root_);
  45. layout_PASCAL_root_->addWidget(edit_PASCAL_root_);
  46. layout_h_->addWidget(button_ok_);
  47. layout_h_->addWidget(button_cancel_);
  48. connect(
  49. button_set_PASCAL_root_,
  50. SIGNAL(clicked()),
  51. this,
  52. SLOT(newPascalPath())
  53. );
  54. connect(
  55. button_ok_,
  56. SIGNAL(clicked()),
  57. this,
  58. SLOT(setOptions())
  59. );
  60. connect(
  61. button_cancel_,
  62. SIGNAL(clicked()),
  63. this,
  64. SLOT(hide())
  65. );
  66. connect(
  67. edit_PASCAL_root_,
  68. SIGNAL(editingFinished()),
  69. this,
  70. SLOT(onPathEditing())
  71. );
  72. /* moving it to the center of the screen */
  73. move(QApplication::desktop()->screen()->rect().center() - rect().center());
  74. }
  75. //! A destructor
  76. OptionsForm::~OptionsForm()
  77. {
  78. delete auto_color_generation_box_;
  79. delete button_set_PASCAL_root_;
  80. delete edit_PASCAL_root_;
  81. delete button_ok_;
  82. delete button_cancel_;
  83. delete layout_v_;
  84. }
  85. //! A slot member confirming all the changes in options
  86. void
  87. OptionsForm::setOptions()
  88. {
  89. *auto_color_generation_ = auto_color_generation_box_->isChecked();
  90. hide();
  91. }
  92. //! Sets a pointer on the ImageLabeler::PASCALpath_
  93. void
  94. OptionsForm::setPASCALpath(QString *aPath)
  95. {
  96. if (!aPath) {
  97. return;
  98. /* NOTREACHED */
  99. }
  100. PASCALpath_ = aPath;
  101. }
  102. //! Asks user for the new path to the PASCAL "root" folder
  103. void
  104. OptionsForm::newPascalPath()
  105. {
  106. QString newPath;
  107. QFileDialog fileDialog(0, tr("root directory for the PASCAL files"));
  108. fileDialog.setFileMode(QFileDialog::Directory);
  109. if (fileDialog.exec())
  110. newPath = fileDialog.selectedFiles().last();
  111. else {
  112. return;
  113. /* NOTREACHED */
  114. }
  115. if (newPath.isEmpty()) {
  116. return;
  117. /* NOTREACHED */
  118. }
  119. *PASCALpath_ = newPath;
  120. edit_PASCAL_root_->setText(newPath);
  121. }
  122. //! Returns auto_color_generation_box_ status
  123. bool
  124. OptionsForm::autoColorGeneration()
  125. {
  126. return auto_color_generation_box_->isChecked();
  127. }
  128. //! Sets auto_color_generation_box_ status
  129. void
  130. OptionsForm::setAutoColorGeneration(bool *flag)
  131. {
  132. auto_color_generation_box_->setChecked(*flag);
  133. auto_color_generation_ = flag;
  134. }
  135. //! A slot member showing the form and initializing widgets
  136. void
  137. OptionsForm::showOptions()
  138. {
  139. if (!PASCALpath_) {
  140. return;
  141. /* NOTREACHED */
  142. }
  143. if (PASCALpath_->isEmpty())
  144. edit_PASCAL_root_->setText(tr("root path is not set yet"));
  145. else
  146. edit_PASCAL_root_->setText(*PASCALpath_);
  147. show();
  148. }
  149. //! A slot member validating path to the PASCAL "root" folder typed by user
  150. void
  151. OptionsForm::onPathEditing()
  152. {
  153. QString text = edit_PASCAL_root_->text();
  154. QDir dir(text);
  155. if (dir.exists())
  156. *PASCALpath_ = text;
  157. else
  158. edit_PASCAL_root_->setText(*PASCALpath_);
  159. }
  160. //! An event which is automatically called on every key press
  161. void
  162. OptionsForm::keyPressEvent(QKeyEvent *anEvent)
  163. {
  164. if ((Qt::Key_Enter == anEvent->key() ||
  165. Qt::Key_Return == anEvent->key()) &&
  166. !edit_PASCAL_root_->hasFocus()) {
  167. setOptions();
  168. }
  169. }
  170. /*
  171. *
  172. */