editflight.cpp 15 KB

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