newtaildialog.cpp 12 KB

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