newtaildialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. *openPilot Log - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020 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/testing/adebug.h"
  21. #include "src/database/tablecolumnliterals.h"
  22. static const auto REG_VALID = QPair<QString, QRegularExpression> {
  23. "registrationLineEdit", QRegularExpression("\\w+-\\w+")};
  24. static const auto MAKE_VALID = QPair<QString, QRegularExpression> {
  25. "makeLineEdit", QRegularExpression("[-a-zA-Z\\s]+")};
  26. static const auto MODEL_VALID = QPair<QString, QRegularExpression> {
  27. "modelLineEdit", QRegularExpression("[\\s\\w-]+")};
  28. static const auto VARIANT_VALID = QPair<QString, QRegularExpression> {
  29. "variantLineEdit", QRegularExpression("[\\s\\w-]+")};
  30. static const auto LINE_EDIT_VALIDATORS = QVector({REG_VALID, MAKE_VALID, MODEL_VALID, VARIANT_VALID});
  31. NewTailDialog::NewTailDialog(QString new_registration, QWidget *parent) :
  32. QDialog(parent),
  33. ui(new Ui::NewTail)
  34. {
  35. DEB("new NewTailDialog (experimental)");
  36. ui->setupUi(this);
  37. setupCompleter();
  38. setupValidators();
  39. ui->registrationLineEdit->setText(new_registration);
  40. ui->searchLineEdit->setStyleSheet("border: 1px solid blue");
  41. ui->searchLineEdit->setFocus();
  42. entry = experimental::ATailEntry();
  43. }
  44. NewTailDialog::NewTailDialog(int row_id, QWidget *parent) :
  45. QDialog(parent),
  46. ui(new Ui::NewTail)
  47. {
  48. using namespace experimental;
  49. DEB("New experimental New Pilot Dialog (edit existing)");
  50. ui->setupUi(this);
  51. ui->searchLabel->hide();
  52. ui->searchLineEdit->hide();
  53. ui->line->hide();
  54. setupValidators();
  55. entry = aDB()->getTailEntry(row_id);
  56. fillForm(entry, false);
  57. }
  58. NewTailDialog::~NewTailDialog()
  59. {
  60. DEB("Deleting NewTailDialog\n");
  61. delete ui;
  62. }
  63. /// Functions
  64. /*!
  65. * \brief NewTail::setupCompleter obtains a QMap<QString searchstring, int aircaft_id> for auto completion
  66. * and obtains a QStringList for QCompleter. This function then sets up the search line edit where
  67. * the user can select a template from the aircraft database to pre-fill the form with the details
  68. * for the selected type.
  69. */
  70. void NewTailDialog::setupCompleter()
  71. {
  72. using namespace experimental;
  73. idMap = aDB()->getIdMap(ADatabaseTarget::aircraft);
  74. aircraftList = aDB()->getCompletionList(ADatabaseTarget::aircraft);
  75. QCompleter *completer = new QCompleter(aircraftList, ui->searchLineEdit);
  76. completer->setCaseSensitivity(Qt::CaseInsensitive);
  77. completer->setCompletionMode(QCompleter::PopupCompletion);
  78. completer->setFilterMode(Qt::MatchContains);
  79. ui->searchLineEdit->setCompleter(completer);
  80. QObject::connect(completer, static_cast<void(QCompleter::*)(const QString &)>(&QCompleter::activated),
  81. this, &NewTailDialog::onSearchCompleterActivated);
  82. QObject::connect(completer, static_cast<void(QCompleter::*)(const QString &)>(&QCompleter::highlighted),
  83. this, &NewTailDialog::onSearchCompleterActivated);
  84. }
  85. void NewTailDialog::setupValidators()
  86. {
  87. for(const auto& pair : LINE_EDIT_VALIDATORS){
  88. auto line_edit = this->findChild<QLineEdit*>(pair.first);
  89. auto validator = new QRegularExpressionValidator(pair.second, line_edit);
  90. line_edit->setValidator(validator);
  91. }
  92. }
  93. /*!
  94. * \brief NewTailDialog::fillForm populates the Dialog with the
  95. * information contained in an entry object. This can be either
  96. * a template (AAircraft, used when creating a new entry) or
  97. * a tail (ATail, used when editing an existing entry)
  98. * \param entry
  99. */
  100. void NewTailDialog::fillForm(experimental::AEntry entry, bool is_template)
  101. {
  102. DEB("Filling Form for (experimental) a/c" << entry.getPosition());
  103. //fill Line Edits
  104. auto line_edits = this->findChildren<QLineEdit *>();
  105. if (is_template)
  106. line_edits.removeOne(ui->registrationLineEdit);
  107. auto data = entry.getData();
  108. for (const auto &le : line_edits) {
  109. auto key = le->objectName().remove("LineEdit");
  110. le->setText(data.value(key).toString());
  111. }
  112. ui->operationComboBox->setCurrentIndex(data.value(DB_TAILS_MULTIPILOT).toInt() + 1);
  113. ui->ppNumberComboBox ->setCurrentIndex(data.value(DB_TAILS_MULTIENGINE).toInt() + 1);
  114. ui->ppTypeComboBox->setCurrentIndex(data.value(DB_TAILS_ENGINETYPE).toInt() + 1);
  115. ui->weightComboBox->setCurrentIndex(data.value(DB_TAILS_WEIGHTCLASS).toInt() + 1);
  116. }
  117. /*!
  118. * \brief NewTail::verify A simple check for empty recommended fields in the form
  119. * \return true if all reconmmended fields are populated
  120. */
  121. bool NewTailDialog::verify()
  122. {
  123. auto recommended_line_edits = this->findChildren<QLineEdit *>("registrationLineEdit");
  124. recommended_line_edits.append(this->findChild<QLineEdit *>("makeLineEdit"));
  125. recommended_line_edits.append(this->findChild<QLineEdit *>("modelLineEdit"));
  126. auto recommended_combo_boxes = this->findChildren<QComboBox *>("operationComboBox");
  127. recommended_combo_boxes.append(this->findChild<QComboBox *>("ppNumberComboBox"));
  128. recommended_combo_boxes.append(this->findChild<QComboBox *>("ppTypeComboBox"));
  129. for (const auto &le : recommended_line_edits) {
  130. if (le->text() != "") {
  131. DEB("Good: " << le);
  132. recommended_line_edits.removeOne(le);
  133. le->setStyleSheet("");
  134. } else {
  135. le->setStyleSheet("border: 1px solid red");
  136. DEB("Not Good: " << le);
  137. }
  138. }
  139. for (const auto &cb : recommended_combo_boxes) {
  140. if (cb->currentIndex() != 0) {
  141. recommended_combo_boxes.removeOne(cb);
  142. cb->setStyleSheet("");
  143. } else {
  144. cb->setStyleSheet("background: orange");
  145. DEB("Not Good: " << cb);
  146. }
  147. }
  148. if (recommended_line_edits.isEmpty() && recommended_combo_boxes.isEmpty()) {
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. }
  154. /*!
  155. * \brief NewTail::submitForm collects input from Line Edits and creates
  156. * or updates a database entry and commits or updates the database
  157. * \param edRole editExisting or createNew
  158. */
  159. void NewTailDialog::submitForm()
  160. {
  161. DEB("Creating Database Object...");
  162. using namespace experimental;
  163. TableData new_data;
  164. //retreive Line Edits
  165. auto line_edits = this->findChildren<QLineEdit *>();
  166. line_edits.removeOne(this->findChild<QLineEdit *>("searchLineEdit"));
  167. for (const auto &le : line_edits) {
  168. auto key = le->objectName().remove("LineEdit");
  169. new_data.insert(key, le->text());
  170. }
  171. if (ui->operationComboBox->currentIndex() != 0) { // bool Multipilot
  172. new_data.insert(DB_TAILS_MULTIPILOT, ui->operationComboBox->currentIndex() - 1);
  173. }
  174. if (ui->ppNumberComboBox->currentIndex() != 0) { // bool MultiEngine
  175. new_data.insert(DB_TAILS_MULTIENGINE, ui->ppNumberComboBox->currentIndex() - 1);
  176. }
  177. if (ui->ppTypeComboBox->currentIndex() != 0) { // int 0=unpowered,....4=jet
  178. new_data.insert(DB_TAILS_ENGINETYPE, ui->ppTypeComboBox->currentIndex() - 1);
  179. }
  180. if (ui->weightComboBox->currentIndex() != 0) { // int 0=light...3=super
  181. new_data.insert(DB_TAILS_WEIGHTCLASS, ui->weightComboBox->currentIndex() - 1);
  182. }
  183. //create db object
  184. entry.setData(new_data);
  185. if (!aDB()->commit(entry)) {
  186. auto message_box = QMessageBox(this);
  187. message_box.setText("The following error has ocurred:\n\n"
  188. + aDB()->lastError.text()
  189. + "\n\nThe entry has not been saved.");
  190. message_box.exec();
  191. return;
  192. } else {
  193. if (entry.getPosition().second != 0)
  194. ACalc::updateAutoTimes(entry.getPosition().second);
  195. QDialog::accept();
  196. }
  197. }
  198. /// Slots
  199. void NewTailDialog::on_operationComboBox_currentIndexChanged(int index)
  200. {
  201. if (index != 0) {
  202. ui->operationComboBox->setStyleSheet("");
  203. }
  204. }
  205. void NewTailDialog::on_ppTypeComboBox_currentIndexChanged(int index)
  206. {
  207. if (index != 0) {
  208. ui->ppTypeComboBox->setStyleSheet("");
  209. }
  210. }
  211. void NewTailDialog::on_ppNumberComboBox_currentIndexChanged(int index)
  212. {
  213. if (index != 0) {
  214. ui->ppNumberComboBox->setStyleSheet("");
  215. }
  216. }
  217. void NewTailDialog::on_weightComboBox_currentIndexChanged(int index)
  218. {
  219. if (index != 0) {
  220. ui->weightComboBox->setStyleSheet("");
  221. }
  222. }
  223. void NewTailDialog::on_buttonBox_accepted()
  224. {
  225. DEB("Button Box Accepted.");
  226. if (ui->registrationLineEdit->text().isEmpty()) {
  227. auto nope = QMessageBox(this);
  228. nope.setText("Registration cannot be empty.");
  229. nope.exec();
  230. return;
  231. }
  232. if (!verify()) {
  233. if (!ASettings::read("userdata/acAllowIncomplete").toInt()) {
  234. auto nope = QMessageBox(this);
  235. nope.setIcon(QMessageBox::Warning);
  236. nope.setText("Some or all recommended fields are empty.\nPlease go back and "
  237. "complete the form.\n\nYou can allow logging incomplete tail entries on the settings page.");
  238. nope.exec();
  239. return;
  240. } else {
  241. QMessageBox::StandardButton reply;
  242. reply = QMessageBox::question(this, "Warning",
  243. "Some recommended fields are empty.\n\n"
  244. "If you do not fill out the aircraft details, "
  245. "it will be impossible to automatically determine Single/Multi Pilot Times or Single/Multi Engine Time."
  246. "This will also impact statistics and auto-logging capabilites.\n\n"
  247. "It is highly recommended to fill in all the details.\n\n"
  248. "Are you sure you want to proceed?",
  249. QMessageBox::Yes | QMessageBox::No);
  250. if (reply == QMessageBox::Yes) {
  251. submitForm();
  252. }
  253. }
  254. }
  255. DEB("Form verified");
  256. submitForm();
  257. }
  258. void NewTailDialog::onSearchCompleterActivated()
  259. {
  260. DEB("Search completer activated!");
  261. const auto &text = ui->searchLineEdit->text();
  262. if (aircraftList.contains(text)) {
  263. DEB("Template Selected. aircraft_id is: " << idMap.value(text));
  264. //call autofiller for dialog
  265. using namespace experimental;
  266. fillForm(aDB()->getAircraftEntry(idMap.value(text)), true);
  267. ui->searchLineEdit->setStyleSheet("border: 1px solid green");
  268. ui->searchLabel->setText(text);
  269. } else {
  270. //for example, editing finished without selecting a result from Qcompleter
  271. ui->searchLineEdit->setStyleSheet("border: 1px solid orange");
  272. }
  273. }
  274. void NewTailDialog::on_registrationLineEdit_textChanged(const QString &arg1)
  275. {
  276. ui->registrationLineEdit->setText(arg1.toUpper());
  277. }