LineEditForm.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * LineEditForm.cpp
  3. *
  4. * Created on: Oct 6, 2011
  5. * Author: Gapchich Vlad
  6. */
  7. #include "LineEditForm.h"
  8. #include <QLineEdit>
  9. #include <QPushButton>
  10. #include <QBoxLayout>
  11. #include <QMessageBox>
  12. #include <QApplication>
  13. #include <QDesktopWidget>
  14. #include <QKeyEvent>
  15. //! A constructor
  16. /*!
  17. * Constructor is responsible for allocating memory for all the GUI,
  18. * arranging and connecting them in the right order.
  19. */
  20. LineEditForm::LineEditForm(QWidget *aParent)
  21. : QWidget(aParent)
  22. {
  23. //setWindowTitle(tr("Image Description"));
  24. purpose_ = NoPurpose;
  25. layout_v_ = new QVBoxLayout(this);
  26. layout_h_ = new QHBoxLayout;
  27. data_ = new QLineEdit(this);
  28. button_ok_ = new QPushButton(this);
  29. button_ok_->setText(tr("OK"));
  30. button_cancel_ = new QPushButton(this);
  31. button_cancel_->setText(tr("Cancel"));
  32. layout_v_->addWidget(data_);
  33. layout_v_->addLayout(layout_h_);
  34. layout_h_->addWidget(button_ok_);
  35. layout_h_->addWidget(button_cancel_);
  36. connect(
  37. button_ok_,
  38. SIGNAL(clicked()),
  39. this,
  40. SLOT(setData())
  41. );
  42. connect(
  43. button_cancel_,
  44. SIGNAL(clicked()),
  45. this,
  46. SLOT(hide())
  47. );
  48. /* moving form to the center of the screen */
  49. adjustSize();
  50. move(QApplication::desktop()->screen()->rect().center() - rect().center());
  51. }
  52. //! A destructor
  53. LineEditForm::~LineEditForm()
  54. {
  55. delete data_;
  56. delete button_ok_;
  57. delete button_cancel_;
  58. delete layout_v_;
  59. }
  60. //! Transmits data to any reciever by dataSet(QString aData)
  61. void
  62. LineEditForm::setData()
  63. {
  64. if (!data_->text().isEmpty()) {
  65. emit dataSet(data_->text());
  66. hide();
  67. }
  68. else
  69. QMessageBox::warning(
  70. this,
  71. tr("Warning!"),
  72. tr("Image description is empty"),
  73. QMessageBox::Ok,
  74. QMessageBox::Cancel
  75. );
  76. }
  77. //! Showing form and setting proper purpose(ImageDescriptionPurpose)
  78. void
  79. LineEditForm::setDescription()
  80. {
  81. data_->clear();
  82. purpose_ = ImageDescriptionPurpose;
  83. setWindowTitle(tr("Image Description"));
  84. show();
  85. }
  86. //! Showing form and setting proper purpose(TaggingPurpose)
  87. void
  88. LineEditForm::setTags()
  89. {
  90. data_->clear();
  91. purpose_ = TaggingPurpose;
  92. setWindowTitle(tr("Tagging"));
  93. show();
  94. }
  95. //! Returns current purpose of the form
  96. FormPurpose
  97. LineEditForm::purpose()
  98. {
  99. return purpose_;
  100. }
  101. //! An event which is automatically called on every key press
  102. void
  103. LineEditForm::keyPressEvent(QKeyEvent *anEvent)
  104. {
  105. if ((Qt::Key_Enter == anEvent->key() ||
  106. Qt::Key_Return == anEvent->key())) {
  107. setData();
  108. }
  109. }
  110. /*
  111. *
  112. */