newtail.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 "newtail.h"
  19. #include "ui_newtail.h"
  20. // Debug Makro
  21. #define DEB(expr) \
  22. qDebug() << __PRETTY_FUNCTION__<< "\t" << expr
  23. const auto MAKE_VALID = QPair<QString, QRegularExpression> {
  24. "makeLineEdit", QRegularExpression("[a-zA-Z]+")};
  25. const auto MODEL_VALID = QPair<QString, QRegularExpression> {
  26. "modelLineEdit", QRegularExpression("\\w+")};
  27. const auto VARIANT_VALID = QPair<QString, QRegularExpression> {
  28. "variantLineEdit", QRegularExpression("\\w+")};
  29. const auto LINE_EDIT_VALIDATORS = QVector({MAKE_VALID, MODEL_VALID, VARIANT_VALID});
  30. //Dialog to be used to create a new tail
  31. NewTail::NewTail(QString newreg, Db::editRole edRole, QWidget *parent) :
  32. QDialog(parent),
  33. ui(new Ui::NewTail)
  34. {
  35. ui->setupUi(this);
  36. role = edRole;
  37. setupCompleter();
  38. setupValidators();
  39. ui->editLabel->hide();
  40. ui->registrationLineEdit->setText(newreg);
  41. ui->searchLineEdit->setStyleSheet("border: 1px solid blue");
  42. ui->searchLineEdit->setFocus();
  43. }
  44. //Dialog to be used to edit an existing tail
  45. NewTail::NewTail(Aircraft dbentry, Db::editRole edRole, QWidget *parent) :
  46. QDialog(parent),
  47. ui(new Ui::NewTail)
  48. {
  49. oldEntry = dbentry;
  50. role = edRole;
  51. ui->setupUi(this);
  52. ui->searchLabel->hide();
  53. ui->searchLineEdit->hide();
  54. ui->line->hide();
  55. setupValidators();
  56. formFiller(oldEntry);
  57. }
  58. NewTail::~NewTail()
  59. {
  60. delete ui;
  61. }
  62. /// Functions
  63. /*!
  64. * \brief NewTail::setupCompleter creates a QMap<aircaft_id,searchstring> for auto completion,
  65. * obtains a QStringList for QCompleter and sets up search line edit accordingly
  66. */
  67. void NewTail::setupCompleter()
  68. {
  69. auto query = QLatin1String("SELECT make||' '||model||'-'||variant, aircraft_id FROM aircraft");
  70. auto vector = Db::customQuery(query, 2);
  71. QMap<QString, int> map;
  72. for (int i = 0; i < vector.length() - 2 ; i += 2) {
  73. if (vector[i] != QLatin1String("")) {
  74. map.insert(vector[i], vector[i + 1].toInt());
  75. }
  76. }
  77. //creating QStringlist for QCompleter. This list is identical to a list of map<key>,
  78. //but creating it like this is faster.
  79. auto cl = new CompletionList(CompleterTarget::aircraft);
  80. aircraftlist = cl->list;
  81. idMap = map;
  82. QCompleter *completer = new QCompleter(aircraftlist, ui->searchLineEdit);
  83. completer->setCaseSensitivity(Qt::CaseInsensitive);
  84. completer->setCompletionMode(QCompleter::PopupCompletion);
  85. completer->setFilterMode(Qt::MatchContains);
  86. ui->searchLineEdit->setCompleter(completer);
  87. }
  88. void NewTail::setupValidators()
  89. {
  90. for(const auto& pair : LINE_EDIT_VALIDATORS){
  91. auto line_edit = parent()->findChild<QLineEdit*>(pair.first);
  92. auto validator = new QRegularExpressionValidator(pair.second,line_edit);
  93. line_edit->setValidator(validator);
  94. }
  95. }
  96. /*!
  97. * \brief NewTail::formFiller populates the Dialog with the
  98. * information contained in an aircraft object.
  99. * \param db - entry retreived from database
  100. */
  101. void NewTail::formFiller(Aircraft entry)
  102. {
  103. DEB("Filling Form for a/c" << entry);
  104. //fill Line Edits
  105. auto line_edits = parent()->findChildren<QLineEdit *>();
  106. for (const auto &le : line_edits) {
  107. QString name = le->objectName();
  108. name.chop(8);//remove "LineEdit"
  109. QString value = entry.data.value(name);
  110. if (!value.isEmpty()) {
  111. le->setText(value);
  112. };
  113. }
  114. //select comboboxes
  115. QVector<QString> operation = {entry.data.value("singleengine"), entry.data.value("multiengine")};
  116. QVector<QString> ppNumber = {entry.data.value("singlepilot"), entry.data.value("multipilot")};
  117. QVector<QString> ppType = {entry.data.value("unpowered"), entry.data.value("piston"),
  118. entry.data.value("turboprop"), entry.data.value("jet")
  119. };
  120. QVector<QString> weight = {entry.data.value("light"), entry.data.value("medium"),
  121. entry.data.value("heavy"), entry.data.value("super")
  122. };
  123. ui->operationComboBox->setCurrentIndex(operation.indexOf("1") + 1);
  124. ui->ppNumberComboBox->setCurrentIndex(ppNumber.indexOf("1") + 1);
  125. ui->ppTypeComboBox->setCurrentIndex(ppType.indexOf("1") + 1);
  126. ui->weightComboBox->setCurrentIndex(weight.indexOf("1") + 1);
  127. }
  128. /*!
  129. * \brief NewTail::verify A simple check for empty recommended fields in the form
  130. * \return true if all reconmmended fields are populated
  131. */
  132. bool NewTail::verify()
  133. {
  134. auto recommended_line_edits = parent()->findChildren<QLineEdit *>("registrationLineEdit");
  135. recommended_line_edits.append(parent()->findChild<QLineEdit *>("makeLineEdit"));
  136. recommended_line_edits.append(parent()->findChild<QLineEdit *>("modelLineEdit"));
  137. auto recommended_combo_boxes = parent()->findChildren<QComboBox *>("operationComboBox");
  138. recommended_combo_boxes.append(parent()->findChild<QComboBox *>("ppNumberComboBox"));
  139. recommended_combo_boxes.append(parent()->findChild<QComboBox *>("ppTypeComboBox"));
  140. for (const auto le : recommended_line_edits) {
  141. if (le->text() != "") {
  142. DEB("Good: " << le);
  143. recommended_line_edits.removeOne(le);
  144. le->setStyleSheet("");
  145. } else {
  146. le->setStyleSheet("border: 1px solid red");
  147. DEB("Not Good: " << le);
  148. }
  149. }
  150. for (const auto cb : recommended_combo_boxes) {
  151. if (cb->currentIndex() != 0) {
  152. recommended_combo_boxes.removeOne(cb);
  153. cb->setStyleSheet("");
  154. } else {
  155. cb->setStyleSheet("background: orange");
  156. DEB("Not Good: " << cb);
  157. }
  158. }
  159. if (recommended_line_edits.isEmpty() && recommended_combo_boxes.isEmpty()) {
  160. return true;
  161. } else {
  162. return false;
  163. }
  164. }
  165. /*!
  166. * \brief NewTail::submitForm collects input from Line Edits and creates
  167. * or updates a database entry and commits or updates the database
  168. * \param edRole editExisting or createNew
  169. */
  170. void NewTail::submitForm(Db::editRole edRole)
  171. {
  172. DEB("Creating Database Object...");
  173. QMap<QString, QString> newData;
  174. //retreive Line Edits
  175. auto line_edits = parent()->findChildren<QLineEdit *>();
  176. for (const auto &le : line_edits) {
  177. QString name = le->objectName();
  178. name.chop(8);//remove "LineEdit"
  179. if (!le->text().isEmpty()) {
  180. newData.insert(name, le->text());
  181. }
  182. }
  183. //prepare comboboxes
  184. QVector<QString> operation = {"singlepilot", "multipilot"};
  185. QVector<QString> ppNumber = {"singleengine", "multiengine"};
  186. QVector<QString> ppType = {"unpowered", "piston",
  187. "turboprop", "jet"
  188. };
  189. QVector<QString> weight = {"light", "medium",
  190. "heavy", "super"
  191. };
  192. if (ui->operationComboBox->currentIndex() != 0) {
  193. newData.insert(operation[ui->operationComboBox->currentIndex() - 1], QLatin1String("1"));
  194. }
  195. if (ui->ppNumberComboBox->currentIndex() != 0) {
  196. newData.insert(ppNumber[ui->ppNumberComboBox->currentIndex() - 1], QLatin1String("1"));
  197. }
  198. if (ui->ppTypeComboBox->currentIndex() != 0) {
  199. newData.insert(ppType[ui->ppTypeComboBox->currentIndex() - 1], QLatin1String("1"));
  200. }
  201. if (ui->weightComboBox->currentIndex() != 0) {
  202. newData.insert(weight[ui->weightComboBox->currentIndex() - 1], QLatin1String("1"));
  203. }
  204. //create db object
  205. switch (edRole) {
  206. case Db::createNew: {
  207. auto newEntry = Aircraft("tails", newData);;
  208. newEntry.commit();
  209. break;
  210. }
  211. case Db::editExisting:
  212. oldEntry.setData(newData);
  213. oldEntry.commit();
  214. Calc::updateAutoTimes(oldEntry.position.second);
  215. break;
  216. }
  217. }
  218. /// Slots
  219. void NewTail::on_searchLineEdit_textChanged(const QString &arg1)
  220. {
  221. if (aircraftlist.contains(
  222. arg1)) { //equivalent to editing finished for this purpose. todo: consider connecing qcompleter activated signal with editing finished slot.
  223. DEB("Template Selected. aircraft_id is: " << idMap.value(arg1));
  224. //call autofiller for dialog
  225. formFiller(Aircraft("aircraft", idMap.value(arg1)));
  226. ui->searchLineEdit->setStyleSheet("border: 1px solid green");
  227. } else {
  228. //for example, editing finished without selecting a result from Qcompleter
  229. ui->searchLineEdit->setStyleSheet("border: 1px solid orange");
  230. }
  231. }
  232. void NewTail::on_operationComboBox_currentIndexChanged(int index)
  233. {
  234. if (index != 0) {
  235. ui->operationComboBox->setStyleSheet("");
  236. }
  237. }
  238. void NewTail::on_ppTypeComboBox_currentIndexChanged(int index)
  239. {
  240. if (index != 0) {
  241. ui->ppTypeComboBox->setStyleSheet("");
  242. }
  243. }
  244. void NewTail::on_ppNumberComboBox_currentIndexChanged(int index)
  245. {
  246. if (index != 0) {
  247. ui->ppNumberComboBox->setStyleSheet("");
  248. }
  249. }
  250. void NewTail::on_weightComboBox_currentIndexChanged(int index)
  251. {
  252. if (index != 0) {
  253. ui->weightComboBox->setStyleSheet("");
  254. }
  255. }
  256. void NewTail::on_buttonBox_accepted()
  257. {
  258. DEB("Button Box Accepted.");
  259. if (ui->registrationLineEdit->text().isEmpty()) {
  260. auto nope = new QMessageBox(this);
  261. nope->setText("Registration cannot be empty.");
  262. nope->show();
  263. } else {
  264. if (verify()) {
  265. DEB("Form verified");
  266. submitForm(role);
  267. accept();
  268. } else {
  269. QSettings setting;
  270. if (!setting.value("userdata/acAllowIncomplete").toInt()) {
  271. auto nope = new QMessageBox(this);
  272. nope->setText("Some or all fields are empty.\nPlease go back and "
  273. "complete the form.\n\nYou can allow logging incomplete entries on the settings page.");
  274. nope->show();
  275. } else {
  276. QMessageBox::StandardButton reply;
  277. reply = QMessageBox::question(this, "Warning",
  278. "Some recommended fields are empty.\n\n"
  279. "If you do not fill out the aircraft details, "
  280. "it will be impossible to automatically determine Single/Multi Pilot Times or Single/Multi Engine Time."
  281. "This will also impact statistics and auto-logging capabilites.\n\n"
  282. "It is highly recommended to fill in all the details.\n\n"
  283. "Are you sure you want to proceed?",
  284. QMessageBox::Yes | QMessageBox::No);
  285. if (reply == QMessageBox::Yes) {
  286. submitForm(role);
  287. accept();
  288. }
  289. }
  290. }
  291. }
  292. }