newtaildialog.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2022 Felix Turowsky
  4. *
  5. *This program is free software: you can redistribute it and/or modify
  6. *it under the terms of the GNU General Public License as published by
  7. *the Free Software Foundation, either version 3 of the License, or
  8. *(at your option) any later version.
  9. *
  10. *This program is distributed in the hope that it will be useful,
  11. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. *GNU General Public License for more details.
  14. *
  15. *You should have received a copy of the GNU General Public License
  16. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include "newtaildialog.h"
  19. #include "ui_newtail.h"
  20. #include "src/functions/alog.h"
  21. #include "src/opl.h"
  22. #include "src/database/dbcompletiondata.h"
  23. NewTailDialog::NewTailDialog(const QString &new_registration, QWidget *parent) :
  24. QDialog(parent),
  25. ui(new Ui::NewTail)
  26. {
  27. ui->setupUi(this);
  28. setupCompleter();
  29. setupValidators();
  30. ui->registrationLineEdit->setText(new_registration);
  31. ui->searchLineEdit->setStyleSheet(QStringLiteral("border: 1px solid blue"));
  32. ui->searchLineEdit->setFocus();
  33. //entry = OPL::TailEntry();
  34. }
  35. NewTailDialog::NewTailDialog(int row_id, QWidget *parent) :
  36. QDialog(parent),
  37. ui(new Ui::NewTail)
  38. {
  39. ui->setupUi(this);
  40. ui->searchLabel->hide();
  41. ui->searchLineEdit->hide();
  42. ui->line->hide();
  43. setupValidators();
  44. entry = DB->getTailEntry(row_id);
  45. LOG << "Editing: " << entry;
  46. fillForm(entry, false);
  47. }
  48. NewTailDialog::~NewTailDialog()
  49. {
  50. delete ui;
  51. }
  52. /*!
  53. * \brief NewTail::setupCompleter obtains a QHash<QString searchstring, int aircaft_id> for auto completion
  54. * and obtains a QStringList for QCompleter. This function then sets up the search line edit where
  55. * the user can select a template from the aircraft database to pre-fill the form with the details
  56. * for the selected type.
  57. */
  58. void NewTailDialog::setupCompleter()
  59. {
  60. idMap = OPL::DbCompletionData::getIdMap(OPL::CompleterTarget::AircraftTypes);
  61. aircraftList = OPL::DbCompletionData::getCompletionList(OPL::CompleterTarget::AircraftTypes);
  62. QCompleter *completer = new QCompleter(aircraftList, ui->searchLineEdit);
  63. completer->setCaseSensitivity(Qt::CaseInsensitive);
  64. completer->setCompletionMode(QCompleter::PopupCompletion);
  65. completer->setFilterMode(Qt::MatchContains);
  66. ui->searchLineEdit->setCompleter(completer);
  67. QObject::connect(completer, static_cast<void(QCompleter::*)(const QString &)>(&QCompleter::activated),
  68. this, &NewTailDialog::onSearchCompleterActivated);
  69. QObject::connect(completer, static_cast<void(QCompleter::*)(const QString &)>(&QCompleter::highlighted),
  70. this, &NewTailDialog::onSearchCompleterActivated);
  71. }
  72. void NewTailDialog::setupValidators()
  73. {
  74. const QHash<QLatin1String, QRegularExpression> line_edit_validators = {
  75. {QLatin1String("registrationLineEdit"), QRegularExpression(QLatin1String("\\w+-\\w+"))},
  76. {QLatin1String("makeLineEdit"), QRegularExpression(QLatin1String("[-a-zA-Z\\s]+"))},
  77. {QLatin1String("modelLineEdit"), QRegularExpression(QLatin1String("[\\s\\w-]+"))},
  78. {QLatin1String("variantLineEdit"), QRegularExpression(QLatin1String("[\\s\\w-]+"))},
  79. };
  80. QHash<QLatin1String, QRegularExpression>::const_iterator i;
  81. for (i = line_edit_validators.constBegin(); i != line_edit_validators.constEnd(); ++i) {
  82. const auto line_edit = this->findChild<QLineEdit*>(i.key());
  83. auto validator = new QRegularExpressionValidator(i.value(), line_edit);
  84. line_edit->setValidator(validator);
  85. }
  86. }
  87. /*!
  88. * \brief NewTailDialog::fillForm populates the Dialog with the
  89. * information contained in an entry object. This can be either
  90. * a template (AircraftEntry, used when creating a new entry) or
  91. * a tail (TailEntry, used when editing an existing entry)
  92. * \param is_template - determines whether we are adding a new entry
  93. * or editing an existing one.
  94. */
  95. void NewTailDialog::fillForm(OPL::Row entry, bool is_template)
  96. {
  97. DEB << "Filling Form for a/c" << entry;
  98. //fill Line Edits
  99. auto line_edits = this->findChildren<QLineEdit *>();
  100. if (is_template)
  101. line_edits.removeOne(ui->registrationLineEdit);
  102. auto data = entry.getData();
  103. for (const auto &le : qAsConst(line_edits)) {
  104. auto key = le->objectName().remove(QStringLiteral("LineEdit"));
  105. le->setText(data.value(key).toString());
  106. }
  107. ui->operationComboBox->setCurrentIndex(data.value(OPL::Db::TAILS_MULTIPILOT).toInt() + 1);
  108. ui->ppNumberComboBox ->setCurrentIndex(data.value(OPL::Db::TAILS_MULTIENGINE).toInt() + 1);
  109. ui->ppTypeComboBox->setCurrentIndex(data.value(OPL::Db::TAILS_ENGINETYPE).toInt() + 1);
  110. ui->weightComboBox->setCurrentIndex(data.value(OPL::Db::TAILS_WEIGHTCLASS).toInt() + 1);
  111. }
  112. /*!
  113. * \brief NewTail::verify A simple check for empty recommended fields in the form
  114. * \return true if all reconmmended fields are populated
  115. */
  116. bool NewTailDialog::verify()
  117. {
  118. auto recommended_line_edits = this->findChildren<QLineEdit *>(QStringLiteral("registrationLineEdit"));
  119. recommended_line_edits.append(this->findChild<QLineEdit *>(QStringLiteral("makeLineEdit")));
  120. recommended_line_edits.append(this->findChild<QLineEdit *>(QStringLiteral("modelLineEdit")));
  121. auto recommended_combo_boxes = this->findChildren<QComboBox *>(QStringLiteral("operationComboBox"));
  122. recommended_combo_boxes.append(this->findChild<QComboBox *>(QStringLiteral("ppNumberComboBox")));
  123. recommended_combo_boxes.append(this->findChild<QComboBox *>(QStringLiteral("ppTypeComboBox")));
  124. for (const auto &le : qAsConst(recommended_line_edits)) {
  125. if (le->text() != "") {
  126. DEB << "Good: " << le;
  127. recommended_line_edits.removeOne(le);
  128. le->setStyleSheet("");
  129. } else {
  130. le->setStyleSheet(QStringLiteral("border: 1px solid red"));
  131. DEB << "Not Good: " << le;
  132. }
  133. }
  134. for (const auto &cb : qAsConst(recommended_combo_boxes)) {
  135. if (cb->currentIndex() != 0) {
  136. recommended_combo_boxes.removeOne(cb);
  137. cb->setStyleSheet(QString());
  138. } else {
  139. cb->setStyleSheet(QStringLiteral("background: orange"));
  140. DEB << "Not Good: " << cb;
  141. }
  142. }
  143. if (recommended_line_edits.isEmpty() && recommended_combo_boxes.isEmpty()) {
  144. return true;
  145. } else {
  146. return false;
  147. }
  148. }
  149. /*!
  150. * \brief NewTail::submitForm collects input from Line Edits and creates
  151. * or updates a database entry and commits or updates the database
  152. * \param edRole editExisting or createNew
  153. */
  154. void NewTailDialog::submitForm()
  155. {
  156. OPL::RowData_T new_data;
  157. //retreive Line Edits
  158. auto line_edits = this->findChildren<QLineEdit *>();
  159. line_edits.removeOne(this->findChild<QLineEdit *>(QStringLiteral("searchLineEdit")));
  160. for (const auto &le : qAsConst(line_edits)) {
  161. auto key = le->objectName().remove(QStringLiteral("LineEdit"));
  162. new_data.insert(key, le->text());
  163. }
  164. if (ui->operationComboBox->currentIndex() != 0) { // bool Multipilot
  165. new_data.insert(OPL::Db::TAILS_MULTIPILOT, ui->operationComboBox->currentIndex() - 1);
  166. }
  167. if (ui->ppNumberComboBox->currentIndex() != 0) { // bool MultiEngine
  168. new_data.insert(OPL::Db::TAILS_MULTIENGINE, ui->ppNumberComboBox->currentIndex() - 1);
  169. }
  170. if (ui->ppTypeComboBox->currentIndex() != 0) { // int 0=unpowered,....4=jet
  171. new_data.insert(OPL::Db::TAILS_ENGINETYPE, ui->ppTypeComboBox->currentIndex() - 1);
  172. }
  173. if (ui->weightComboBox->currentIndex() != 0) { // int 0=light...3=super
  174. new_data.insert(OPL::Db::TAILS_WEIGHTCLASS, ui->weightComboBox->currentIndex() - 1);
  175. }
  176. //create db object
  177. entry.setData(new_data);
  178. LOG << "Commiting: " << entry;
  179. if (!DB->commit(entry)) {
  180. QMessageBox message_box(this);
  181. message_box.setText(tr("The following error has ocurred:"
  182. "<br><br>%1<br><br>"
  183. "The entry has not been saved."
  184. ).arg(DB->lastError.text()));
  185. message_box.exec();
  186. return;
  187. } else {
  188. emit tailDataChanged();
  189. QDialog::accept();
  190. }
  191. }
  192. /// Slots
  193. void NewTailDialog::on_operationComboBox_currentIndexChanged(int index)
  194. {
  195. if (index != 0)
  196. ui->operationComboBox->setStyleSheet(QString());
  197. }
  198. void NewTailDialog::on_ppTypeComboBox_currentIndexChanged(int index)
  199. {
  200. if (index != 0)
  201. ui->ppTypeComboBox->setStyleSheet(QString());
  202. }
  203. void NewTailDialog::on_ppNumberComboBox_currentIndexChanged(int index)
  204. {
  205. if (index != 0)
  206. ui->ppNumberComboBox->setStyleSheet(QString());
  207. }
  208. void NewTailDialog::on_weightComboBox_currentIndexChanged(int index)
  209. {
  210. if (index != 0)
  211. ui->weightComboBox->setStyleSheet(QString());
  212. }
  213. void NewTailDialog::on_buttonBox_accepted()
  214. {
  215. DEB << "Button Box Accepted.";
  216. if (ui->registrationLineEdit->text().isEmpty()) {
  217. QMessageBox message_box(this);
  218. message_box.setText(tr("Registration cannot be empty."));
  219. message_box.exec();
  220. return;
  221. }
  222. if (!verify()) {
  223. QMessageBox message_box(this);
  224. message_box.setIcon(QMessageBox::Warning);
  225. message_box.setText(tr("Some or all recommended fields are empty.<br>"
  226. "Please fill out the mandatory fields. You can use "
  227. "the search function to automatically fill out all "
  228. "the required fields for a known aircraft type."));
  229. message_box.exec();
  230. return;
  231. }
  232. submitForm();
  233. }
  234. void NewTailDialog::onSearchCompleterActivated()
  235. {
  236. const auto &text = ui->searchLineEdit->text();
  237. if (aircraftList.contains(text)) {
  238. DEB << "Template Selected. aircraft_id is: " << idMap.key(text);
  239. //call autofiller for dialog
  240. fillForm(DB->getAircraftEntry(idMap.key(text)), true);
  241. ui->searchLineEdit->setStyleSheet(QStringLiteral("border: 1px solid green"));
  242. ui->searchLabel->setText(text);
  243. } else {
  244. //for example, editing finished without selecting a result from Qcompleter
  245. ui->searchLineEdit->setStyleSheet(QStringLiteral("border: 1px solid orange"));
  246. }
  247. }
  248. void NewTailDialog::on_registrationLineEdit_textChanged(const QString &arg1)
  249. {
  250. ui->registrationLineEdit->setText(arg1.toUpper());
  251. }