OptionsForm.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. OptionsForm::OptionsForm(QWidget *aParent)
  19. : QWidget(aParent)
  20. {
  21. setWindowTitle(tr("Options"));
  22. PASCALpath_ = 0;
  23. layout_v_ = new QVBoxLayout(this);
  24. layout_PASCAL_root_ = new QHBoxLayout;
  25. layout_h_ = new QHBoxLayout;
  26. auto_color_generation_box_ = new QCheckBox(this);
  27. auto_color_generation_box_->setText(tr("Automatic label color generation"));
  28. button_set_PASCAL_root_ = new QPushButton(this);
  29. button_set_PASCAL_root_->setText(tr("set PASCAL root path"));
  30. edit_PASCAL_root_ = new QLineEdit("", this);
  31. edit_PASCAL_root_->setMinimumWidth(180);
  32. button_ok_ = new QPushButton(this);
  33. button_ok_->setText(tr("OK"));
  34. button_cancel_ = new QPushButton(this);
  35. button_cancel_->setText(tr("Cancel"));
  36. layout_v_->addWidget(auto_color_generation_box_);
  37. layout_v_->addLayout(layout_PASCAL_root_);
  38. layout_v_->addLayout(layout_h_);
  39. layout_PASCAL_root_->addWidget(button_set_PASCAL_root_);
  40. layout_PASCAL_root_->addWidget(edit_PASCAL_root_);
  41. layout_h_->addWidget(button_ok_);
  42. layout_h_->addWidget(button_cancel_);
  43. connect(
  44. button_set_PASCAL_root_,
  45. SIGNAL(clicked()),
  46. this,
  47. SLOT(newPascalPath())
  48. );
  49. connect(
  50. button_ok_,
  51. SIGNAL(clicked()),
  52. this,
  53. SLOT(setOptions())
  54. );
  55. connect(
  56. button_cancel_,
  57. SIGNAL(clicked()),
  58. this,
  59. SLOT(hide())
  60. );
  61. connect(
  62. edit_PASCAL_root_,
  63. SIGNAL(editingFinished()),
  64. this,
  65. SLOT(onPathEditing())
  66. );
  67. move(QApplication::desktop()->screen()->rect().center() - rect().center());
  68. }
  69. OptionsForm::~OptionsForm()
  70. {
  71. delete auto_color_generation_;
  72. delete button_set_PASCAL_root_;
  73. delete edit_PASCAL_root_;
  74. delete button_ok_;
  75. delete button_cancel_;
  76. delete layout_v_;
  77. }
  78. void
  79. OptionsForm::setOptions()
  80. {
  81. *auto_color_generation_ = auto_color_generation_box_->isChecked();
  82. hide();
  83. }
  84. void
  85. OptionsForm::setPASCALpath(QString *aPath)
  86. {
  87. if (!aPath) {
  88. return;
  89. /* NOTREACHED */
  90. }
  91. PASCALpath_ = aPath;
  92. }
  93. void
  94. OptionsForm::newPascalPath()
  95. {
  96. QString newPath;
  97. QFileDialog fileDialog(0, tr("root directory for the PASCAL files"));
  98. fileDialog.setFileMode(QFileDialog::Directory);
  99. if (fileDialog.exec())
  100. newPath = fileDialog.selectedFiles().last();
  101. else {
  102. return;
  103. /* NOTREACHED */
  104. }
  105. if (newPath.isEmpty()) {
  106. return;
  107. /* NOTREACHED */
  108. }
  109. *PASCALpath_ = newPath;
  110. edit_PASCAL_root_->setText(newPath);
  111. }
  112. bool
  113. OptionsForm::autoColorGeneration()
  114. {
  115. return auto_color_generation_box_->isChecked();
  116. }
  117. void
  118. OptionsForm::setAutoColorGeneration(bool *flag)
  119. {
  120. auto_color_generation_box_->setChecked(*flag);
  121. auto_color_generation_ = flag;
  122. }
  123. void
  124. OptionsForm::showOptions()
  125. {
  126. if (!PASCALpath_) {
  127. return;
  128. /* NOTREACHED */
  129. }
  130. if (PASCALpath_->isEmpty())
  131. edit_PASCAL_root_->setText(tr("root path is not set yet"));
  132. else
  133. edit_PASCAL_root_->setText(*PASCALpath_);
  134. show();
  135. }
  136. void
  137. OptionsForm::onPathEditing()
  138. {
  139. QString text = edit_PASCAL_root_->text();
  140. QDir dir(text);
  141. if (dir.exists())
  142. *PASCALpath_ = text;
  143. else
  144. edit_PASCAL_root_->setText(*PASCALpath_);
  145. }
  146. void
  147. OptionsForm::keyPressEvent(QKeyEvent *anEvent)
  148. {
  149. if ((Qt::Key_Enter == anEvent->key() ||
  150. Qt::Key_Return == anEvent->key()) &&
  151. !edit_PASCAL_root_->hasFocus()) {
  152. setOptions();
  153. }
  154. }
  155. /*
  156. *
  157. */