editflight.cpp 15 KB

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