newflight.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 "newflight.h"
  19. #include "ui_newflight.h"
  20. #include "newacft.h"
  21. #include "calc.h"
  22. #include "dbman.cpp"
  23. #include <QMessageBox>
  24. #include <QDebug>
  25. #include <QCompleter>
  26. #include <QStringList>
  27. #include <QStringListModel>
  28. #include <QSortFilterProxyModel>
  29. #include <QButtonGroup>
  30. void NewFlight::on_verifyButton_clicked()
  31. {
  32. /*on_newDoft_editingFinished();// - activate slots in case user has been active in last input before clicking submit
  33. on_newTonb_editingFinished();
  34. on_newTofb_editingFinished();
  35. on_newDept_editingFinished();
  36. on_newDest_editingFinished();
  37. on_newAcft_editingFinished();
  38. on_newPic_editingFinished();
  39. verifyInput();*/
  40. fillExtrasLineEdits();
  41. }
  42. /*
  43. * Initialise variables
  44. */
  45. QDate date(QDate::currentDate());
  46. QString doft(QDate::currentDate().toString(Qt::ISODate));
  47. QString dept;
  48. QString dest;
  49. QTime tofb;
  50. QTime tonb;
  51. QTime tblk;
  52. QString pic;
  53. QString acft;
  54. QVector<QString> flight;
  55. // extras
  56. QString PilotFunction;
  57. QString PilotTask;
  58. QString TakeOff;
  59. QString Landing;
  60. QString Autoland;
  61. QString ApproachType;
  62. /*
  63. * Window
  64. */
  65. NewFlight::NewFlight(QWidget *parent) :
  66. QDialog(parent),
  67. ui(new Ui::NewFlight)
  68. {
  69. ui->setupUi(this);
  70. ui->newDoft->setDate(QDate::currentDate());
  71. bool hasoldinput = db::CheckScratchpad();
  72. qDebug() << "Hasoldinput? = " << hasoldinput;
  73. if(hasoldinput) // Re-populate the Form
  74. {
  75. flight = db::RetreiveScratchpad();
  76. qDebug() << "Re-Filling Form from Scratchpad";
  77. returnInput(flight);
  78. }
  79. // Validators for Line Edits
  80. QRegExp icao_rx("[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]"); // allow only letters (upper and lower)
  81. QValidator *ICAOvalidator = new QRegExpValidator(icao_rx, this);
  82. ui->newDept->setValidator(ICAOvalidator);
  83. ui->newDest->setValidator(ICAOvalidator);
  84. 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
  85. QValidator *timeInputValidator = new QRegExpValidator(timehhmm, this);
  86. ui->newTofb->setValidator(timeInputValidator);
  87. ui->newTonb->setValidator(timeInputValidator);
  88. ui->spseTimeLineEdit->setValidator(timeInputValidator);
  89. ui->spmeTimeLineEdit->setValidator(timeInputValidator);
  90. ui->mpTimeLineEdit->setValidator(timeInputValidator);
  91. ui->totalTimeLineEdit->setValidator(timeInputValidator);
  92. ui->ifrTimeLineEdit->setValidator(timeInputValidator);
  93. ui->vfrTimeLineEdit->setValidator(timeInputValidator);
  94. ui->nightTimeLineEdit->setValidator(timeInputValidator);
  95. ui->xcTimeLineEdit->setValidator(timeInputValidator);
  96. ui->picTimeLineEdit->setValidator(timeInputValidator);
  97. ui->copTimeLineEdit->setValidator(timeInputValidator);
  98. ui->dualTimeLineEdit->setValidator(timeInputValidator);
  99. ui->fiTimeLineEdit->setValidator(timeInputValidator);
  100. ui->simTimeLineEdit->setValidator(timeInputValidator);
  101. QRegExp picname("^[a-zA-Z_]+,?( [a-zA-Z_]+)*$");// allow only lastname, firstname or lastname firstname with one whitespace
  102. QValidator *picNameValidator = new QRegExpValidator(picname, this);
  103. ui->newPic->setValidator(picNameValidator);
  104. // Groups for CheckBoxes
  105. QButtonGroup *FlightRulesGroup = new QButtonGroup(this);
  106. FlightRulesGroup->addButton(ui->IfrCheckBox);
  107. FlightRulesGroup->addButton(ui->VfrCheckBox);
  108. QButtonGroup *PilotTaskGroup = new QButtonGroup(this);
  109. PilotTaskGroup->addButton(ui->PilotFlyingCheckBox);
  110. PilotTaskGroup->addButton(ui->PilotMonitoringCheckBox);
  111. restoreSettings();
  112. ui->deptTZ->setFocusPolicy(Qt::NoFocus);
  113. ui->destTZ->setFocusPolicy(Qt::NoFocus);
  114. ui->newDept->setFocus();
  115. }
  116. NewFlight::~NewFlight()
  117. {
  118. delete ui;
  119. }
  120. /*
  121. * Functions
  122. */
  123. void NewFlight::nope()
  124. {
  125. QMessageBox nope(this); //error box
  126. nope.setText("This feature is not yet available!");
  127. nope.exec();
  128. }
  129. /*!
  130. * \brief NewFlight::validateTimeInput verifies user input and formats to hh:mm
  131. * \param userinput from a QLineEdit
  132. * \return formatted QString "hh:mm"
  133. */
  134. QString NewFlight::validateTimeInput(QString userinput)
  135. {
  136. QString output;
  137. QTime temptime;
  138. if(userinput.length() <3)
  139. {
  140. QMessageBox timeformat(this);
  141. timeformat.setText("Please enter a valid time. Any of these formats is valid:\n845 0845 8:45 08:45");
  142. timeformat.exec();
  143. }
  144. bool containsSeperator = userinput.contains(":");
  145. if(userinput.length() == 4 && !containsSeperator)
  146. {
  147. temptime = QTime::fromString(userinput,"hhmm");
  148. }else if(userinput.length() == 3 && !containsSeperator)
  149. {
  150. if(userinput.toInt() < 240) //Qtime is invalid if time is between 000 and 240 for this case
  151. {
  152. QString tempstring = userinput.prepend("0");
  153. temptime = QTime::fromString(tempstring,"hhmm");
  154. }else
  155. {
  156. temptime = QTime::fromString(userinput,"Hmm");
  157. }
  158. }else if(userinput.length() == 4 && containsSeperator)
  159. {
  160. temptime = QTime::fromString(userinput,"h:mm");
  161. }else if(userinput.length() == 5 && containsSeperator)
  162. {
  163. temptime = QTime::fromString(userinput,"hh:mm");
  164. }
  165. return temptime.toString("hh:mm");
  166. }
  167. /*!
  168. * \brief NewFlight::fillExtrasLineEdits Fills the flight time line edits according to ui selections
  169. */
  170. void NewFlight::fillExtrasLineEdits()
  171. {
  172. // SP SE
  173. // SP ME
  174. // MP
  175. // TOTAL
  176. if(tofb.isValid() && tonb.isValid()) {
  177. ui->totalTimeLineEdit->setText(calc::blocktime(tofb,tonb).toString("hh:mm"));
  178. }
  179. // IFR
  180. // VFR
  181. // Night
  182. if(ui->autoNightCheckBox->isChecked() && tofb.isValid() && tonb.isValid() && dept.length() == 4 && dest.length() == 4) {
  183. QString deptDate = date.toString(Qt::ISODate) + 'T' + tofb.toString("hh:mm");
  184. qDebug() << "Departure Date: " << deptDate;
  185. QDateTime deptDateTime = QDateTime::fromString(deptDate,"yyyy-MM-ddThh:mm");
  186. qDebug() << "Departure DateTime " << deptDateTime;
  187. int blocktime = calc::time_to_minutes(calc::blocktime(tofb,tonb));
  188. qDebug() << "Blocktime: " << blocktime;
  189. //qDebug() << calc::calculateNightTime(dept, dest, deptDateTime, blocktime);
  190. //qDebug() << calc::minutes_to_string(QString::number(calc::calculateNightTime(dept, dest, deptDateTime, blocktime)));
  191. ui->nightTimeLineEdit->setText(calc::minutes_to_string(QString::number(calc::calculateNightTime(dept, dest, deptDateTime, blocktime))));
  192. }else if(!ui->autoNightCheckBox->isChecked()) {
  193. ui->nightTimeLineEdit->setText("");
  194. }
  195. // XC
  196. // PIC
  197. // Co-Pilot
  198. // Dual
  199. // FI
  200. // SIM
  201. }
  202. QVector<QString> NewFlight::collectInput()
  203. {
  204. // Collect input from the user fields and return a vector containing the flight information
  205. QString doft = date.toString(Qt::ISODate); // Format Date
  206. QTime tblk = calc::blocktime(tofb,tonb); // Calculate Blocktime
  207. // Prepare Vector for commit to database
  208. flight = db::CreateFlightVectorFromInput(doft, dept, tofb, dest, tonb, tblk, pic, acft);
  209. qDebug() << "Created flight vector:" << flight;
  210. return flight;
  211. }
  212. bool NewFlight::verifyInput()
  213. //check if the input is correctly formatted and all required fields are filled
  214. {
  215. bool deptValid = false;
  216. bool tofbValid = false;
  217. bool destValid = false;
  218. bool tonbValid = false;
  219. bool tblkValid = false;
  220. bool picValid = false;
  221. bool acftValid = false;
  222. QTime tblk = calc::blocktime(tofb,tonb);
  223. int checktblk = calc::time_to_minutes(tblk);
  224. bool doftValid = true; //doft assumed to be always valid due to QDateTimeEdit constraints
  225. qDebug() << "NewFlight::verifyInput() says: Date:" << doft << " - Valid?\t" << doftValid;
  226. deptValid = db::CheckICAOValid(dept);
  227. qDebug() << "NewFlight::verifyInput() says: Departure is:\t" << dept << " - Valid?\t" << deptValid;
  228. destValid = db::CheckICAOValid(dest);
  229. qDebug() << "NewFlight::verifyInput() says: Destination is:\t" << dest << " - Valid?\t" << destValid;
  230. tofbValid = (unsigned)(calc::time_to_minutes(tofb)-0) <= (1440-0) && tofb.toString("hh:mm") != ""; // Make sure time is within range, DB 1 day = 1440 minutes. 0 is allowed (midnight) & that it is not empty.
  231. qDebug() << "NewFlight::verifyInput() says: tofb is:\t\t" << tofb.toString("hh:mm") << " - Valid?\t" << tofbValid;
  232. tonbValid = (unsigned)(calc::time_to_minutes(tonb)-0) <= (1440-0) && tonb.toString("hh:mm") != ""; // Make sure time is within range, DB 1 day = 1440 minutes
  233. qDebug() << "NewFlight::verifyInput() says: tonb is:\t\t" << tonb.toString("hh:mm")<< " - Valid?\t" << tonbValid;
  234. picValid = (pic.toInt() > 0); // pic should be a pilot_id retreived from the database
  235. qDebug() << "NewFlight::verifyInput() says: pic is pilotd_id:\t" << pic << " - Valid?\t" << picValid;
  236. if(!picValid)
  237. {
  238. QMessageBox msgBox(this);
  239. msgBox.setText("You cannot log a flight without a valid pilot");
  240. msgBox.exec();
  241. }
  242. acftValid = (acft.toInt() > 0);
  243. qDebug() << "NewFlight::verifyInput() says: acft is tail_id:\t" << acft << " - Valid?\t" << acftValid;
  244. if(!acftValid)
  245. {
  246. QMessageBox msgBox(this);
  247. msgBox.setText("You cannot log a flight without a valid aircraft");
  248. msgBox.exec();
  249. }
  250. tblkValid = checktblk != 0;
  251. qDebug() << "NewFlight::verifyInput() says: tblk is:\t\t" << tblk.toString("hh:mm") << " - Valid?\t" << tblkValid;
  252. if(deptValid && tofbValid && destValid && tonbValid
  253. && tblkValid && picValid && acftValid)
  254. {
  255. qDebug() << "====================================================";
  256. qDebug() << "NewFlight::verifyInput() says: All checks passed! Very impressive. ";
  257. return 1;
  258. }else
  259. {
  260. qDebug() << "====================================================";
  261. qDebug() << "NewFlight::verifyInput() says: Flight is invalid.";
  262. qDebug() << "NewFlight::verifyInput() says: I will call the cops.";
  263. return 0;
  264. }
  265. return 0;
  266. }
  267. void NewFlight::returnInput(QVector<QString> flight)
  268. {
  269. // Re-populates the input masks with the selected fields if input was erroneous to allow for corrections to be made
  270. ui->newDoft->setDate(QDate::fromString(flight[1],Qt::ISODate));
  271. ui->newDept->setText(flight[2]);
  272. ui->newTofb->setText(flight[3]);
  273. ui->newDest->setText(flight[4]);
  274. ui->newTonb->setText(flight[5]);
  275. // flight[6] is blocktime
  276. ui->newPic->setText(db::RetreivePilotNameFromID(flight[7]));
  277. ui->newAcft->setText(db::RetreiveRegistration(flight[8]));
  278. }
  279. void NewFlight::storeSettings()
  280. {
  281. qDebug() << "Storing Settings...";
  282. db::storesetting(100, ui->FunctionComboBox->currentText());
  283. db::storesetting(101, ui->ApproachComboBox->currentText());
  284. db::storesetting(102, QString::number(ui->PilotFlyingCheckBox->isChecked()));
  285. db::storesetting(103, QString::number(ui->PilotMonitoringCheckBox->isChecked()));
  286. db::storesetting(104, QString::number(ui->TakeoffSpinBox->value()));
  287. db::storesetting(105, QString::number(ui->TakeoffCheckBox->isChecked()));
  288. db::storesetting(106, QString::number(ui->LandingSpinBox->value()));
  289. db::storesetting(107, QString::number(ui->LandingCheckBox->isChecked()));
  290. db::storesetting(108, QString::number(ui->AutolandSpinBox->value()));
  291. db::storesetting(109, QString::number(ui->AutolandCheckBox->isChecked()));
  292. db::storesetting(110, QString::number(ui->IfrCheckBox->isChecked()));
  293. db::storesetting(111, QString::number(ui->VfrCheckBox->isChecked()));
  294. db::storesetting(112, QString::number(ui->autoNightCheckBox->isChecked()));
  295. }
  296. void NewFlight::restoreSettings()
  297. {
  298. qDebug() << "Restoring Settings...";
  299. ui->FunctionComboBox->setCurrentText(db::retreiveSetting("100")[1]);
  300. ui->ApproachComboBox->setCurrentText(db::retreiveSetting("101")[1]);
  301. ui->PilotFlyingCheckBox->setChecked(db::retreiveSetting("102")[1].toInt());
  302. ui->PilotMonitoringCheckBox->setChecked(db::retreiveSetting("103")[1].toInt());
  303. ui->TakeoffSpinBox->setValue(db::retreiveSetting("104")[1].toInt());
  304. ui->TakeoffCheckBox->setChecked(db::retreiveSetting("105")[1].toInt());
  305. ui->LandingSpinBox->setValue(db::retreiveSetting("106")[1].toInt());
  306. ui->LandingCheckBox->setChecked(db::retreiveSetting("107")[1].toInt());
  307. ui->AutolandSpinBox->setValue(db::retreiveSetting("108")[1].toInt());
  308. ui->AutolandCheckBox->setChecked(db::retreiveSetting("109")[1].toInt());
  309. ui->IfrCheckBox->setChecked(db::retreiveSetting("110")[1].toInt());
  310. ui->VfrCheckBox->setChecked(db::retreiveSetting("111")[1].toInt());
  311. ui->autoNightCheckBox->setChecked(db::retreiveSetting("112")[1].toInt());
  312. //qDebug() << "restore Settings ifr to int: " << db::retreiveSetting("110")[1].toInt();
  313. /*
  314. *
  315. * QString PilotFunction;
  316. QString PilotTask;
  317. QString TakeOff;
  318. QString Landing;
  319. QString Autoland;
  320. QString ApproachType;
  321. 100 PIC NewFlight::FunctionComboBox
  322. 101 ILS CAT I NewFlight::ApproachComboBox
  323. 102 Qt::Checked NewFlight::PilotFlyingCheckBox
  324. 103 Qt::Unchecked NewFlight::PilotMonitoringCheckBox
  325. 104 1 NewFlight::TakeoffSpinBox
  326. 105 Qt::Checked NewFlight::TakeoffCheckBox
  327. 106 1 NewFlight::LandingSpinBox
  328. 107 Qt::Checked NewFlight::LandingCheckBox
  329. 108 0 NewFlight::AutolandSpinBox
  330. 109 Qt::Unchecked NewFlight::AutolandCheckBox
  331. 110 Qt::Checked NewFlight::IfrCheckBox
  332. 111 Qt::Unchecked NewFlight::VfrCheckBox
  333. */
  334. }
  335. /*
  336. * Slots
  337. */
  338. void NewFlight::on_deptTZ_currentTextChanged(const QString &arg1)
  339. {
  340. if(arg1 == "Local"){nope();}
  341. ui->deptTZ->setCurrentIndex(0);
  342. }
  343. void NewFlight::on_destTZ_currentIndexChanged(const QString &arg1)
  344. {
  345. if(arg1 == "Local"){nope();}
  346. ui->destTZ->setCurrentIndex(0);
  347. }
  348. void NewFlight::on_newDept_textEdited(const QString &arg1)
  349. {
  350. ui->newDept->setText(arg1.toUpper());
  351. if(arg1.length()>2)
  352. {
  353. QStringList deptList = db::CompleteIcaoOrIata(arg1);
  354. qDebug() << "deptList = " << deptList;
  355. QCompleter *deptCompleter = new QCompleter(deptList, this);
  356. deptCompleter->setCaseSensitivity(Qt::CaseInsensitive);
  357. deptCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
  358. ui->newDept->setCompleter(deptCompleter);
  359. }
  360. }
  361. void NewFlight::on_newDept_editingFinished()
  362. {
  363. if(ui->newDept->text().length()<3)
  364. {
  365. QMessageBox msgBox(this);
  366. msgBox.setText("Please enter a 3- or 4-letter code.");
  367. msgBox.exec();
  368. ui->newDept->setText("");
  369. ui->newDept->setFocus();
  370. }
  371. QStringList deptList;
  372. if(ui->newDept->text().length()>1)
  373. {
  374. QStringList deptList = db::CompleteIcaoOrIata(ui->newDept->text());
  375. if(deptList.length() != 0)
  376. {
  377. dept = deptList.first();
  378. ui->newDept->setText(dept);
  379. }else
  380. {
  381. QMessageBox msgBox(this);
  382. msgBox.setText("No Airport with that name found.");
  383. msgBox.exec();
  384. ui->newDept->setText("");
  385. ui->newDept->setFocus();
  386. }
  387. }
  388. }
  389. void NewFlight::on_newDest_textEdited(const QString &arg1)
  390. {
  391. ui->newDest->setText(arg1.toUpper());
  392. if(arg1.length()>2)
  393. {
  394. QStringList destList = db::CompleteIcaoOrIata(arg1);
  395. QCompleter *destCompleter = new QCompleter(destList, this);
  396. destCompleter->setCaseSensitivity(Qt::CaseInsensitive);
  397. destCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
  398. ui->newDest->setCompleter(destCompleter);
  399. }
  400. }
  401. void NewFlight::on_newDest_editingFinished()
  402. {
  403. if(ui->newDest->text().length()<3)
  404. {
  405. QMessageBox msgBox(this);
  406. msgBox.setText("Please enter a 3- or 4-letter code.");
  407. msgBox.exec();
  408. ui->newDest->setText("");
  409. ui->newDest->setFocus();
  410. }
  411. QStringList destList;
  412. if(ui->newDest->text().length()>1)
  413. {
  414. QStringList destList = db::CompleteIcaoOrIata(ui->newDest->text());
  415. if(destList.length() != 0)
  416. {
  417. dest = destList.first();
  418. ui->newDest->setText(dest);
  419. }else
  420. {
  421. QMessageBox msgBox(this);
  422. msgBox.setText("No Airport with that name found.");
  423. msgBox.exec();
  424. ui->newDest->setText("");
  425. ui->newDest->setFocus();
  426. }
  427. }
  428. }
  429. void NewFlight::on_newDoft_editingFinished()
  430. {
  431. date = ui->newDoft->date();
  432. doft = date.toString(Qt::ISODate);
  433. }
  434. void NewFlight::on_newTofb_editingFinished()
  435. {
  436. ui->newTofb->setText(validateTimeInput(ui->newTofb->text()));
  437. tofb = QTime::fromString(ui->newTofb->text(),"hh:mm");
  438. qDebug() << "New Time Off Blocks: " << tofb;
  439. }
  440. void NewFlight::on_newTonb_editingFinished()
  441. {
  442. ui->newTonb->setText(validateTimeInput(ui->newTonb->text()));
  443. tonb = QTime::fromString(ui->newTonb->text(),"hh:mm");
  444. qDebug() << "New Time On Blocks: " << tonb;
  445. }
  446. void NewFlight::on_newAcft_textEdited(const QString &arg1)
  447. {
  448. if(arg1.length()>1)
  449. {
  450. QStringList acftList = db::newAcftGetString(arg1);
  451. QCompleter *acftCompleter = new QCompleter(acftList, this);
  452. acftCompleter->setCaseSensitivity(Qt::CaseInsensitive);
  453. acftCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
  454. ui->newAcft->setCompleter(acftCompleter);
  455. }
  456. }
  457. void NewFlight::on_newAcft_editingFinished()
  458. {
  459. acft = "-1";// set invalid
  460. if(ui->newAcft->text().contains(" "))// is input from QCompleter
  461. {
  462. QStringList input = ui->newAcft->text().split(" ");
  463. QString registration = input[0].trimmed();
  464. QStringList result = db::newAcftGetString(registration);
  465. if(result.length() > 0)
  466. {
  467. ui->newAcft->setText(result[0]);
  468. acft = db::newAcftGetId(registration);
  469. }else
  470. {
  471. acft = "-1";
  472. }
  473. }else // is input from user
  474. {
  475. QString input = ui->newAcft->text();
  476. QStringList result = db::newAcftGetString(input);
  477. if(result.length() > 0)
  478. {
  479. ui->newAcft->setText(result[0]);
  480. QStringList input = ui->newAcft->text().split(" ");
  481. QString registration = input[0].trimmed();
  482. acft = db::newAcftGetId(registration);
  483. }else
  484. {
  485. acft = "-1";
  486. }
  487. }
  488. if(acft == "-1")
  489. {
  490. QMessageBox::StandardButton reply;
  491. reply = QMessageBox::question(this, "No aircraft found", "No aircraft found.\n Would you like to add a new aircraft to the database?",
  492. QMessageBox::Yes|QMessageBox::No);
  493. if (reply == QMessageBox::Yes) {
  494. NewAcft na(this);
  495. na.exec();
  496. ui->newAcft->setText("");
  497. ui->newAcft->setFocus();
  498. }
  499. }
  500. qDebug() << "Editing finished. Acft: " << acft;
  501. }
  502. void NewFlight::on_newPic_textEdited(const QString &arg1)
  503. {
  504. if(arg1.length()>2)
  505. {
  506. QStringList picList = db::newPicGetString(arg1);
  507. QCompleter *picCompleter = new QCompleter(picList, this);
  508. picCompleter->setCaseSensitivity(Qt::CaseInsensitive);
  509. picCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
  510. ui->newPic->setCompleter(picCompleter);
  511. }
  512. }
  513. void NewFlight::on_newPic_editingFinished()
  514. {
  515. pic = "-1"; // set invalid
  516. if(ui->newPic->text() == "self")
  517. {
  518. pic = "1";
  519. }else
  520. {
  521. QString picname;
  522. QStringList picList = db::newPicGetString(ui->newPic->text());
  523. qDebug() << picList;
  524. if(picList.length()!= 0)
  525. {
  526. picname = picList[0];
  527. ui->newPic->setText(picname);
  528. pic = db::newPicGetId(picname);
  529. }else
  530. {
  531. QMessageBox::StandardButton reply;
  532. reply = QMessageBox::question(this, "No Pilot found", "No pilot found.\n Would you like to add a new pilot to the database?",
  533. QMessageBox::Yes|QMessageBox::No);
  534. if (reply == QMessageBox::Yes)
  535. {
  536. qDebug() << "Add new pilot selected";
  537. nope();
  538. }
  539. }
  540. }
  541. }
  542. /*
  543. * Extras
  544. */
  545. void NewFlight::on_PilotFlyingCheckBox_stateChanged(int)//0, unchecked, 2 checked
  546. {
  547. if(ui->PilotFlyingCheckBox->isChecked()){
  548. ui->TakeoffSpinBox->setValue(1);
  549. ui->TakeoffCheckBox->setCheckState(Qt::Checked);
  550. ui->LandingSpinBox->setValue(1);
  551. ui->LandingCheckBox->setCheckState(Qt::Checked);
  552. }else if(!ui->PilotFlyingCheckBox->isChecked()){
  553. ui->TakeoffSpinBox->setValue(0);
  554. ui->TakeoffCheckBox->setCheckState(Qt::Unchecked);
  555. ui->LandingSpinBox->setValue(0);
  556. ui->LandingCheckBox->setCheckState(Qt::Unchecked);
  557. }
  558. }
  559. void NewFlight::on_setAsDefaultButton_clicked()
  560. {
  561. storeSettings();
  562. }
  563. void NewFlight::on_restoreDefaultButton_clicked()
  564. {
  565. restoreSettings();
  566. }
  567. void NewFlight::on_buttonBox_accepted()
  568. {
  569. on_newDoft_editingFinished();// - activate slots in case user has been active in last input before clicking submit
  570. on_newTonb_editingFinished();
  571. on_newTofb_editingFinished();
  572. on_newDept_editingFinished();
  573. on_newDest_editingFinished();
  574. on_newAcft_editingFinished();
  575. on_newPic_editingFinished();
  576. QVector<QString> flight;
  577. flight = collectInput();
  578. if(verifyInput())
  579. {
  580. db::CommitFlight(flight);
  581. qDebug() << flight << "Has been commited.";
  582. QMessageBox msgBox(this);
  583. msgBox.setText("Flight has been commited.");
  584. msgBox.exec();
  585. }else
  586. {
  587. qDebug() << "Invalid Input. No entry has been made in the database.";
  588. db::CommitToScratchpad(flight);
  589. QMessageBox msgBox(this);
  590. msgBox.setText("Invalid entries detected. Please check your input.");
  591. msgBox.exec();
  592. NewFlight nf(this);
  593. nf.exec();
  594. }
  595. }
  596. void NewFlight::on_buttonBox_rejected()
  597. {
  598. qDebug() << "NewFlight: Rejected\n";
  599. }
  600. void NewFlight::on_ApproachComboBox_currentTextChanged(const QString &arg1)
  601. {
  602. if(arg1 == "ILS CAT III"){
  603. ui->AutolandCheckBox->setCheckState(Qt::Checked);
  604. ui->AutolandSpinBox->setValue(1);
  605. }else{
  606. ui->AutolandCheckBox->setCheckState(Qt::Unchecked);
  607. ui->AutolandSpinBox->setValue(0);
  608. }
  609. ApproachType = arg1;
  610. qDebug() << "Approach Type: " << ApproachType;
  611. }
  612. /*
  613. * Times
  614. */
  615. void NewFlight::on_nightTimeLineEdit_editingFinished()
  616. {
  617. ui->nightTimeLineEdit->setText(validateTimeInput(ui->nightTimeLineEdit->text()));
  618. }