editflight.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 "editflight.h"
  19. #include "ui_editflight.h"
  20. #include "calc.h"
  21. #include "dbman.cpp"
  22. #include "dbflight.h"
  23. #include <QMessageBox>
  24. #include <QDebug>
  25. /*
  26. * Initialise variables
  27. */
  28. QDate editdate(QDate::currentDate());
  29. QString editdoft(QDate::currentDate().toString(Qt::ISODate));
  30. QString editdept;
  31. QString editdest;
  32. QTime edittofb;
  33. QTime edittonb;
  34. QTime edittblk;
  35. QString editpic;
  36. QString editacft;
  37. QVector<QString> editflight;
  38. /*
  39. * Window
  40. */
  41. EditFlight::EditFlight(QWidget *parent) :
  42. QDialog(parent),
  43. ui(new Ui::EditFlight)
  44. {
  45. ui->setupUi(this);
  46. editflight = dbFlight::retreiveScratchpad();
  47. qDebug() << "EditFlight: Re-assigning variables from vector " << editflight;
  48. editdate = QDate::fromString(editflight[1],Qt::ISODate);
  49. editdoft = editflight[1];
  50. editdept = editflight[2];
  51. edittofb = QTime::fromString(editflight[3], "hh:mm");
  52. editdest = editflight[4];
  53. edittonb = QTime::fromString(editflight[5], "hh:mm");
  54. // flight[6] is blocktime
  55. editpic = editflight[7];
  56. editacft = editflight[8];
  57. qDebug() << "Reassigned:" << editdate << editdoft << editdept << edittofb << editdest << edittonb << editpic << editacft;
  58. qDebug() << "Re-Filling Form from Scratchpad";
  59. returnInput(editflight);
  60. // Validators for Line Edits
  61. QRegExp icao_rx("[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]"); // allow only letters (upper and lower)
  62. QValidator *ICAOvalidator = new QRegExpValidator(icao_rx, this);
  63. ui->newDept->setValidator(ICAOvalidator);
  64. ui->newDest->setValidator(ICAOvalidator);
  65. QRegExp timehhmm("([01]?[0-9]|2[0-3]):?[0-5][0-9]"); //allows time in 24h format with optional leading 0 and with or without seperator
  66. QValidator *timeInputValidator = new QRegExpValidator(timehhmm, this);
  67. ui->newTofb->setValidator(timeInputValidator);
  68. ui->newTonb->setValidator(timeInputValidator);
  69. ui->deptHintlineEdit->setFocusPolicy(Qt::NoFocus);
  70. ui->destHintlineEdit->setFocusPolicy(Qt::NoFocus);
  71. ui->acftHintLineEdit->setFocusPolicy(Qt::NoFocus);
  72. ui->picHintLineEdit->setFocusPolicy(Qt::NoFocus);
  73. ui->deptTZ->setFocusPolicy(Qt::NoFocus);
  74. ui->desTZ->setFocusPolicy(Qt::NoFocus);
  75. ui->newDept->setFocus();
  76. }
  77. EditFlight::~EditFlight()
  78. {
  79. delete ui;
  80. }
  81. /*
  82. * Functions
  83. */
  84. void EditFlight::nope()
  85. {
  86. QMessageBox nope; //error box
  87. nope.setText("This feature is not yet available!");
  88. nope.exec();
  89. }
  90. QVector<QString> EditFlight::collectInput()
  91. {
  92. // Collect input from the user fields and return a vector containing the flight information
  93. QString editdoft = editdate.toString(Qt::ISODate); // Format Date
  94. QTime tblk = calc::blocktime(edittofb,edittonb); // Calculate Blocktime
  95. // Prepare Vector for commit to database
  96. editflight = dbFlight::createFlightVectorFromInput(editdoft, editdept, edittofb, editdest, edittonb, tblk, editpic, editacft);
  97. qDebug() << "Created flight vector:" << editflight;
  98. return editflight;
  99. }
  100. bool EditFlight::verifyInput()
  101. //check if the input is correctly formatted and all required fields are filled
  102. {
  103. bool deptValid = false;
  104. bool tofbValid = false;
  105. bool destValid = false;
  106. bool tonbValid = false;
  107. bool tblkValid = false;
  108. bool picValid = false;
  109. bool acftValid = false;
  110. QTime tblk = calc::blocktime(edittofb,edittonb);
  111. int checktblk = calc::time_to_minutes(tblk);
  112. bool doftValid = true; //doft assumed to be always valid due to QDateTimeEdit constraints
  113. qDebug() << "EditFlight::verifyInput() says: Date:" << editdoft << " - Valid?\t" << doftValid;
  114. deptValid = db::CheckICAOValid(editdept);
  115. qDebug() << "EditFlight::verifyInput() says: Departure is:\t" << editdept << " - Valid?\t" << deptValid;
  116. destValid = db::CheckICAOValid(editdest);
  117. qDebug() << "EditFlight::verifyInput() says: Destination is:\t" << editdest << " - Valid?\t" << destValid;
  118. tofbValid = (unsigned)(calc::time_to_minutes(edittofb)-0) <= (1440-0) && edittofb.toString("hh:mm") != ""; // Make sure time is within range, DB 1 day = 1440 minutes. 0 is allowed (midnight) & that it is not empty.
  119. qDebug() << "EditFlight::verifyInput() says: tofb is:\t\t" << edittofb.toString("hh:mm") << " - Valid?\t" << tofbValid;
  120. tonbValid = (unsigned)(calc::time_to_minutes(edittonb)-0) <= (1440-0) && edittofb.toString("hh:mm") != ""; // Make sure time is within range, DB 1 day = 1440 minutes
  121. qDebug() << "EditFlight::verifyInput() says: tonb is:\t\t" << edittonb.toString("hh:mm")<< " - Valid?\t" << tonbValid;
  122. picValid = (editpic.toInt() != 0); // pic should be a pilot_id retreived from the database
  123. qDebug() << "EditFlight::verifyInput() says: pic is pilotd_id:\t" << editpic << " - Valid?\t" << picValid;
  124. acftValid = (editacft.toInt() != 0);
  125. qDebug() << "EditFlight::verifyInput() says: acft is tail_id:\t" << editacft << " - Valid?\t" << acftValid;
  126. tblkValid = checktblk != 0;
  127. qDebug() << "EditFlight::verifyInput() says: tblk is:\t\t" << tblk.toString("hh:mm") << " - Valid?\t" << tblkValid;
  128. if(deptValid && tofbValid && destValid && tonbValid
  129. && tblkValid && picValid && acftValid)
  130. {
  131. qDebug() << "====================================================";
  132. qDebug() << "EditFlight::verifyInput() says: All checks passed! Very impressive. ";
  133. return 1;
  134. }else
  135. {
  136. qDebug() << "====================================================";
  137. qDebug() << "EditFlight::verifyInput() says: Flight is invalid.";
  138. qDebug() << "EditFlight::verifyInput() says: I will call the cops.";
  139. return 0;
  140. }
  141. return 0;
  142. }
  143. void EditFlight::returnInput(QVector<QString> flight)
  144. {
  145. // Re-populates the input masks with the selected fields if input was erroneous to allow for corrections to be made
  146. ui->newDoft->setDate(QDate::fromString(flight[1],Qt::ISODate));
  147. ui->newDept->setText(flight[2]);
  148. ui->newTofb->setText(flight[3]);
  149. ui->newDest->setText(flight[4]);
  150. ui->newTonb->setText(flight[5]);
  151. // flight[6] is blocktime
  152. ui->newPic->setText(dbPilots::retreivePilotNameFromID(flight[7]));
  153. ui->newAcft->setText(db::RetreiveRegistration(flight[8]));
  154. }
  155. /*
  156. * Slots
  157. */
  158. void EditFlight::on_newDoft_editingFinished()
  159. {
  160. editdate = ui->newDoft->date();
  161. editdoft = editdate.toString(Qt::ISODate);
  162. //ui->dateHintLineEdit->setText(doft);
  163. }
  164. void EditFlight::on_newDept_textChanged(const QString &arg1)
  165. {
  166. if(arg1.length() > 2)
  167. {
  168. QString result;
  169. result = db::RetreiveAirportNameFromIcaoOrIata(arg1);
  170. ui->deptHintlineEdit->setPlaceholderText(result);
  171. }
  172. }
  173. void EditFlight::on_newDest_textChanged(const QString &arg1)
  174. {
  175. if(arg1.length() > 2)
  176. {
  177. QString result;
  178. result = db::RetreiveAirportNameFromIcaoOrIata(arg1);
  179. ui->destHintlineEdit->setPlaceholderText(result);
  180. }
  181. }
  182. void EditFlight::on_newDept_editingFinished()
  183. {
  184. editdept = ui->newDept->text().toUpper();
  185. ui->newDept->setText(editdept);
  186. if (!ui->deptHintlineEdit->placeholderText().compare("No matching airport found.", Qt::CaseInsensitive))
  187. {
  188. QMessageBox msgBox;
  189. msgBox.setText("Airport not found.\nCreate new entry in Database?");
  190. msgBox.exec();
  191. }else if (editdept.length() < 4)
  192. {
  193. qDebug() << "on_new_Dept_editingFinished() Incomplete entry. Completing.";
  194. //editdept = db::CompleteIcaoOrIata(editdept);
  195. ui->newDept->setText(editdept);
  196. }
  197. }
  198. void EditFlight::on_newTofb_editingFinished()
  199. {
  200. bool containsSeperator = ui->newTofb->text().contains(":");
  201. if(ui->newTofb->text().length() == 4 && !containsSeperator)
  202. {
  203. qDebug() << "1) Contains seperator: " << containsSeperator << "Length = " << ui->newTofb->text().length();
  204. edittofb = QTime::fromString(ui->newTofb->text(),"hhmm");
  205. qDebug() << edittofb;
  206. }else if(ui->newTofb->text().length() == 3 && !containsSeperator)
  207. {
  208. if(ui->newTofb->text().toInt() < 240) //Qtime is invalid if time is between 000 and 240 for this case
  209. {
  210. QString tempstring = ui->newTofb->text().prepend("0");
  211. edittofb = QTime::fromString(tempstring,"hhmm");
  212. qDebug() << tempstring << "is the tempstring (Special Case) " << edittofb;
  213. }else
  214. {
  215. qDebug() << "2) Contains seperator: " << containsSeperator << "Length = " << ui->newTofb->text().length();
  216. qDebug() << "Tofb = " << ui->newTofb->text();
  217. edittofb = QTime::fromString(ui->newTofb->text(),"Hmm");
  218. qDebug() << edittofb;
  219. }
  220. }else if(ui->newTofb->text().length() == 4 && containsSeperator)
  221. {
  222. qDebug() << "3) Contains seperator: " << containsSeperator << "Length = " << ui->newTofb->text().length();
  223. edittofb = QTime::fromString(ui->newTofb->text(),"h:mm");
  224. qDebug() << edittofb;
  225. }else if(ui->newTofb->text().length() == 5 && containsSeperator)
  226. {
  227. qDebug() << "4) Contains seperator: " << containsSeperator << "Length = " << ui->newTofb->text().length();
  228. edittofb = QTime::fromString(ui->newTofb->text(),"hh:mm");
  229. qDebug() << edittofb;
  230. }
  231. ui->newTofb->setText(edittofb.toString("hh:mm"));
  232. }
  233. void EditFlight::on_newDest_editingFinished()
  234. {
  235. editdest = ui->newDest->text().toUpper();
  236. ui->newDest->setText(editdest);
  237. if (!ui->destHintlineEdit->placeholderText().compare("No matching airport found.", Qt::CaseInsensitive))
  238. {
  239. QMessageBox msgBox;
  240. msgBox.setText("Airport not found.\nCreate new entry in Database?");
  241. msgBox.exec();
  242. }else if (editdest.length() < 4)
  243. {
  244. qDebug() << "on_new_Dest_editingFinished() Incomplete entry. Completing.";
  245. //editdest = db::CompleteIcaoOrIata(editdest);
  246. ui->newDest->setText(editdest);
  247. //ui->newDest->setText(db::CompleteIcaoOrIata(dest));
  248. }
  249. }
  250. void EditFlight::on_newTonb_editingFinished()
  251. {
  252. bool containsSeperator = ui->newTonb->text().contains(":");
  253. if(ui->newTonb->text().length() == 4 && !containsSeperator)
  254. {
  255. edittonb = QTime::fromString(ui->newTonb->text(),"hhmm");
  256. qDebug() << edittonb;
  257. }else if(ui->newTonb->text().length() == 3 && !containsSeperator)
  258. {
  259. if(ui->newTonb->text().toInt() < 240) //Qtime is invalid if time is between 000 and 240 for this case
  260. {
  261. QString tempstring = ui->newTonb->text().prepend("0");
  262. edittonb = QTime::fromString(tempstring,"hhmm");
  263. qDebug() << tempstring << "is the tempstring (Special Case) " << edittonb;
  264. }else
  265. {
  266. qDebug() << "Tofb = " << ui->newTonb->text();
  267. edittonb = QTime::fromString(ui->newTonb->text(),"Hmm");
  268. qDebug() << edittonb;
  269. }
  270. }else if(ui->newTonb->text().length() == 4 && containsSeperator)
  271. {
  272. edittonb = QTime::fromString(ui->newTonb->text(),"h:mm");
  273. qDebug() << edittonb;
  274. }else if(ui->newTonb->text().length() == 5 && containsSeperator)
  275. {
  276. edittonb = QTime::fromString(ui->newTonb->text(),"hh:mm");
  277. qDebug() << edittonb;
  278. }
  279. ui->newTonb->setText(edittonb.toString("hh:mm"));
  280. }
  281. void EditFlight::on_newPic_textChanged(const QString &arg1)
  282. {
  283. qDebug() << arg1;
  284. /*{
  285. if(arg1.length() > 3)
  286. {
  287. QVector<QString> hint = db::RetreivePilotNameFromString(arg1);
  288. if(hint.size()!= 0)
  289. {
  290. QString combinedname = hint[0] + ", " + hint[1] + " [ " + hint[2] + " ]";
  291. ui->picHintLineEdit->setPlaceholderText(combinedname);
  292. }
  293. }
  294. }*/
  295. }
  296. void EditFlight::on_newPic_editingFinished()
  297. {
  298. /*QString input = ui->newPic->text();
  299. if(input.length()>2)
  300. {
  301. QVector<QString> result;
  302. result = db::RetreivePilotNameFromString(input);
  303. qDebug() << result;
  304. if(result.size()!=0)
  305. {
  306. QString lastname = result[0];
  307. ui->newPic->setText(lastname);// to do: pop up to navigate result set, here first result is selected
  308. editpic = db::RetreivePilotIdFromString(lastname);// to do, adjust before commit
  309. //qDebug() << "on_newPic_editingFinished() says: Pilot ID = " << pic;
  310. }else
  311. {
  312. QMessageBox msgBox;
  313. msgBox.setText("No Pilot found!");// to do: add new pilot
  314. msgBox.exec();
  315. ui->newPic->setText("");
  316. ui->newPic->setFocus();
  317. }
  318. }else if (input.length() == 0)
  319. {
  320. }else
  321. {
  322. QMessageBox msgBox;
  323. msgBox.setText("For Auto-Completion, please enter 3 or more characters!");
  324. msgBox.exec();
  325. ui->newPic->setFocus();
  326. }*/
  327. }
  328. void EditFlight::on_newAcft_textChanged(const QString &arg1)
  329. {
  330. if(arg1.length() > 2)
  331. {
  332. QVector<QString> hint = db::RetreiveAircraftTypeFromReg(arg1);
  333. if(hint.size() != 0)
  334. {
  335. ui->acftHintLineEdit->setPlaceholderText(hint[1] + " [ " + hint[0] + " | " + hint[2] + " ]");
  336. }
  337. }
  338. }
  339. void EditFlight::on_newAcft_editingFinished()
  340. {
  341. editacft = ui->newAcft->text();
  342. if(ui->newAcft->text().length()>2)
  343. {
  344. QVector<QString> result;
  345. result = db::RetreiveAircraftTypeFromReg(editacft);
  346. if(result.size()!=0)
  347. {
  348. ui->newAcft->setText(result[0]);// to do: pop up to navigate result set, here first result is selected
  349. editacft = result[3];
  350. }else
  351. {
  352. QMessageBox msgBox;
  353. msgBox.setText("No Aircraft found!");
  354. msgBox.exec();
  355. ui->newAcft->setText("");
  356. ui->newAcft->setFocus();
  357. }
  358. }
  359. }
  360. void EditFlight::on_buttonBox_accepted()
  361. {
  362. QVector<QString> flight;
  363. flight = collectInput();
  364. if(verifyInput())
  365. {
  366. //!x db::CommitFlight(flight);
  367. qDebug() << flight << "Has been commited.";
  368. QMessageBox msgBox;
  369. msgBox.setText("Flight successfully edited. (At least once I wrote that function. Now nothing has happened.)\nlol\nWhat a troll.");
  370. msgBox.exec();
  371. }else
  372. {
  373. qDebug() << "Invalid Input. No entry has been made in the database.";
  374. dbFlight::commitToScratchpad(flight);
  375. QMessageBox msgBox;
  376. msgBox.setText("La Base de datos se niega a ser violada!");
  377. msgBox.exec();
  378. EditFlight nf(this);
  379. nf.exec();
  380. }
  381. }
  382. void EditFlight::on_buttonBox_rejected()
  383. {
  384. dbFlight::clearScratchpad();
  385. }
  386. void EditFlight::on_verifyButton_clicked()
  387. {
  388. editflight = collectInput();
  389. if (verifyInput())
  390. {
  391. ui->verifyEdit->setPlaceholderText("No Errors detected.");
  392. }else
  393. {
  394. ui->verifyEdit->setPlaceholderText("Invalid Input.");
  395. }
  396. }