2
0

newsimdialog.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "newsimdialog.h"
  2. #include "ui_newsimdialog.h"
  3. #include "src/opl.h"
  4. #include "src/functions/atime.h"
  5. #include "src/functions/adate.h"
  6. #include <QCompleter>
  7. /*!
  8. * \brief create a NewSimDialog to add a new Simulator Entry to the database
  9. */
  10. NewSimDialog::NewSimDialog(QWidget *parent) :
  11. QDialog(parent),
  12. ui(new Ui::NewSimDialog)
  13. {
  14. //entry = ASimulatorEntry();
  15. ui->setupUi(this);
  16. ui->dateLineEdit->setText(ADate::currentDate());
  17. init();
  18. }
  19. /*!
  20. * \brief create a NewSimDialog to edit an existing Simulator Entry
  21. * \param row_id of the entry to be edited
  22. */
  23. NewSimDialog::NewSimDialog(int row_id, QWidget *parent) :
  24. QDialog(parent),
  25. ui(new Ui::NewSimDialog)
  26. {
  27. ui->setupUi(this);
  28. entry = DB->getSimEntry(row_id);
  29. init();
  30. fillEntryData();
  31. }
  32. /*!
  33. * \brief set up the UI with Combo Box entries and QCompleter
  34. */
  35. void NewSimDialog::init()
  36. {
  37. OPL::GLOBALS->loadSimulatorTypes(ui->deviceTypeComboBox);
  38. const QStringList aircraft_list = OPL::DbCompletionData::getCompletionList(OPL::CompleterTarget::AircraftTypes);
  39. auto completer = new QCompleter(aircraft_list, ui->aircraftTypeLineEdit);
  40. completer->setCaseSensitivity(Qt::CaseInsensitive);
  41. completer->setCompletionMode(QCompleter::PopupCompletion);
  42. completer->setFilterMode(Qt::MatchContains);
  43. ui->aircraftTypeLineEdit->setCompleter(completer);
  44. }
  45. /*!
  46. * \brief fills the UI with data retreived from an existing entry.
  47. */
  48. void NewSimDialog::fillEntryData()
  49. {
  50. const auto& data = entry.getData();
  51. ui->dateLineEdit->setText(data.value(OPL::Db::SIMULATORS_DATE).toString());
  52. ui->totalTimeLineEdit->setText(ATime::toString(data.value(OPL::Db::SIMULATORS_TIME).toInt()));
  53. ui->deviceTypeComboBox->setCurrentIndex(data.value(OPL::Db::SIMULATORS_TYPE).toInt());
  54. ui->aircraftTypeLineEdit->setText(data.value(OPL::Db::SIMULATORS_ACFT).toString());
  55. ui->registrationLineEdit->setText(data.value(OPL::Db::SIMULATORS_REG).toString());
  56. ui->remarksLineEdit->setText(data.value(OPL::Db::SIMULATORS_REMARKS).toString());
  57. }
  58. NewSimDialog::~NewSimDialog()
  59. {
  60. delete ui;
  61. }
  62. void NewSimDialog::on_dateLineEdit_editingFinished()
  63. {
  64. auto text = ui->dateLineEdit->text();
  65. OPL::DateFormat date_format = OPL::DateFormat::ISODate;
  66. auto date = ADate::parseInput(text, date_format);
  67. if (date.isValid()) {
  68. ui->dateLineEdit->setText(ADate::toString(date, date_format));
  69. ui->dateLineEdit->setStyleSheet(QString());
  70. return;
  71. } else {
  72. ui->dateLineEdit->setText(QString());
  73. ui->dateLineEdit->setStyleSheet(OPL::Styles::RED_BORDER);
  74. }
  75. }
  76. void NewSimDialog::on_totalTimeLineEdit_editingFinished()
  77. {
  78. const QString time_string = ATime::formatTimeInput(ui->totalTimeLineEdit->text());
  79. const QTime time = ATime::fromString(time_string);
  80. if (time.isValid()) {
  81. ui->totalTimeLineEdit->setText(time_string);
  82. ui->totalTimeLineEdit->setStyleSheet(QString());
  83. } else {
  84. ui->totalTimeLineEdit->setText(QString());
  85. ui->totalTimeLineEdit->setStyleSheet(OPL::Styles::RED_BORDER);
  86. }
  87. }
  88. void NewSimDialog::on_registrationLineEdit_textChanged(const QString &arg1)
  89. {
  90. ui->registrationLineEdit->setText(arg1.toUpper());
  91. }
  92. void NewSimDialog::on_helpPushButton_clicked()
  93. {
  94. INFO(tr("<br>"
  95. "For any FSTD enter the type of aircraft and qualification "
  96. "number of the device. For other flight training devices enter "
  97. "either FNPT I or FNPT II as appropriate<br><br>"
  98. "Total time of session includes all exercises carried out in the "
  99. "device, including pre- and after-flight checks<br><br>"
  100. "Enter the type of exercise performed in the ‘remarks’ field "
  101. "for example operator proficiency check, revalidation."));
  102. }
  103. bool NewSimDialog::verifyInput(QString& error_msg)
  104. {
  105. // Date
  106. auto text = ui->dateLineEdit->text();
  107. OPL::DateFormat date_format = OPL::DateFormat::ISODate;
  108. const auto date = ADate::parseInput(text, date_format);
  109. if (!date.isValid()) {
  110. ui->dateLineEdit->setStyleSheet(OPL::Styles::RED_BORDER);
  111. ui->dateLineEdit->setText(QString());
  112. error_msg = tr("Invalid Date");
  113. return false;
  114. }
  115. // Time
  116. const QString time_string = ATime::formatTimeInput(ui->totalTimeLineEdit->text());
  117. const QTime time = ATime::fromString(time_string);
  118. if (!time.isValid()) {
  119. ui->totalTimeLineEdit->setStyleSheet(OPL::Styles::RED_BORDER);
  120. ui->totalTimeLineEdit->setText(QString());
  121. error_msg = tr("Invalid time");
  122. return false;
  123. }
  124. // Device Type - for FSTD, aircraft info is required
  125. if (ui->deviceTypeComboBox->currentIndex() == static_cast<int>(OPL::SimulatorType::FSTD)
  126. && ui->aircraftTypeLineEdit->text() == QString()) {
  127. error_msg = tr("For FSTD, please enter the aircraft type.");
  128. return false;
  129. }
  130. return true;
  131. }
  132. OPL::RowData_T NewSimDialog::collectInput()
  133. {
  134. OPL::RowData_T new_entry;
  135. // Date
  136. new_entry.insert(OPL::Db::SIMULATORS_DATE, ui->dateLineEdit->text());
  137. // Time
  138. new_entry.insert(OPL::Db::SIMULATORS_TIME, ATime::toMinutes(ui->totalTimeLineEdit->text()));
  139. // Device Type
  140. new_entry.insert(OPL::Db::SIMULATORS_TYPE, ui->deviceTypeComboBox->currentText());
  141. // Aircraft Type
  142. new_entry.insert(OPL::Db::SIMULATORS_ACFT, ui->aircraftTypeLineEdit->text());
  143. // Registration
  144. if (!ui->registrationLineEdit->text().isEmpty())
  145. new_entry.insert(OPL::Db::SIMULATORS_REG, ui->registrationLineEdit->text());
  146. // Remarks
  147. if (!ui->remarksLineEdit->text().isEmpty())
  148. new_entry.insert(OPL::Db::FLIGHTS_REMARKS, ui->remarksLineEdit->text());
  149. return new_entry;
  150. }
  151. void NewSimDialog::on_buttonBox_accepted()
  152. {
  153. QString error_msg;
  154. if (!verifyInput(error_msg)) {
  155. INFO(error_msg);
  156. return;
  157. }
  158. entry.setData(collectInput());
  159. DEB << entry;
  160. if(DB->commit(entry))
  161. QDialog::accept();
  162. else
  163. WARN(tr("Unable to commit entry to database. The following error has ocurred <br><br>%1").arg(DB->lastError.text()));
  164. }