newflight.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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()//debug
  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. qDebug() << "return Input: " << flight;
  271. ui->newDoft->setDate(QDate::fromString(flight[1],Qt::ISODate));
  272. ui->newDept->setText(flight[2]);
  273. ui->newTofb->setText(flight[3]);
  274. ui->newDest->setText(flight[4]);
  275. ui->newTonb->setText(flight[5]);
  276. // flight[6] is blocktime
  277. ui->newPic->setText(db::RetreivePilotNameFromID(flight[7]));
  278. ui->newAcft->setText(db::RetreiveRegistration(flight[8]));
  279. }
  280. void NewFlight::storeSettings()
  281. {
  282. qDebug() << "Storing Settings...";
  283. db::storesetting(100, ui->FunctionComboBox->currentText());
  284. db::storesetting(101, ui->ApproachComboBox->currentText());
  285. db::storesetting(102, QString::number(ui->PilotFlyingCheckBox->isChecked()));
  286. db::storesetting(103, QString::number(ui->PilotMonitoringCheckBox->isChecked()));
  287. db::storesetting(104, QString::number(ui->TakeoffSpinBox->value()));
  288. db::storesetting(105, QString::number(ui->TakeoffCheckBox->isChecked()));
  289. db::storesetting(106, QString::number(ui->LandingSpinBox->value()));
  290. db::storesetting(107, QString::number(ui->LandingCheckBox->isChecked()));
  291. db::storesetting(108, QString::number(ui->AutolandSpinBox->value()));
  292. db::storesetting(109, QString::number(ui->AutolandCheckBox->isChecked()));
  293. db::storesetting(110, QString::number(ui->IfrCheckBox->isChecked()));
  294. db::storesetting(111, QString::number(ui->VfrCheckBox->isChecked()));
  295. db::storesetting(112, QString::number(ui->autoNightCheckBox->isChecked()));
  296. }
  297. void NewFlight::restoreSettings()
  298. {
  299. qDebug() << "Restoring Settings...";//crashes if db is empty due to QVector index out of range.
  300. ui->FunctionComboBox->setCurrentText(db::retreiveSetting("100")[1]);
  301. ui->ApproachComboBox->setCurrentText(db::retreiveSetting("101")[1]);
  302. ui->PilotFlyingCheckBox->setChecked(db::retreiveSetting("102")[1].toInt());
  303. ui->PilotMonitoringCheckBox->setChecked(db::retreiveSetting("103")[1].toInt());
  304. ui->TakeoffSpinBox->setValue(db::retreiveSetting("104")[1].toInt());
  305. ui->TakeoffCheckBox->setChecked(db::retreiveSetting("105")[1].toInt());
  306. ui->LandingSpinBox->setValue(db::retreiveSetting("106")[1].toInt());
  307. ui->LandingCheckBox->setChecked(db::retreiveSetting("107")[1].toInt());
  308. ui->AutolandSpinBox->setValue(db::retreiveSetting("108")[1].toInt());
  309. ui->AutolandCheckBox->setChecked(db::retreiveSetting("109")[1].toInt());
  310. ui->IfrCheckBox->setChecked(db::retreiveSetting("110")[1].toInt());
  311. ui->VfrCheckBox->setChecked(db::retreiveSetting("111")[1].toInt());
  312. ui->autoNightCheckBox->setChecked(db::retreiveSetting("112")[1].toInt());
  313. //qDebug() << "restore Settings ifr to int: " << db::retreiveSetting("110")[1].toInt();
  314. /*
  315. *
  316. * QString PilotFunction;
  317. QString PilotTask;
  318. QString TakeOff;
  319. QString Landing;
  320. QString Autoland;
  321. QString ApproachType;
  322. 100 PIC NewFlight::FunctionComboBox
  323. 101 ILS CAT I NewFlight::ApproachComboBox
  324. 102 Qt::Checked NewFlight::PilotFlyingCheckBox
  325. 103 Qt::Unchecked NewFlight::PilotMonitoringCheckBox
  326. 104 1 NewFlight::TakeoffSpinBox
  327. 105 Qt::Checked NewFlight::TakeoffCheckBox
  328. 106 1 NewFlight::LandingSpinBox
  329. 107 Qt::Checked NewFlight::LandingCheckBox
  330. 108 0 NewFlight::AutolandSpinBox
  331. 109 Qt::Unchecked NewFlight::AutolandCheckBox
  332. 110 Qt::Checked NewFlight::IfrCheckBox
  333. 111 Qt::Unchecked NewFlight::VfrCheckBox
  334. */
  335. }
  336. /*
  337. * Slots
  338. */
  339. void NewFlight::on_deptTZ_currentTextChanged(const QString &arg1)
  340. {
  341. if(arg1 == "Local"){nope();}
  342. ui->deptTZ->setCurrentIndex(0);
  343. }
  344. void NewFlight::on_destTZ_currentIndexChanged(const QString &arg1)
  345. {
  346. if(arg1 == "Local"){nope();}
  347. ui->destTZ->setCurrentIndex(0);
  348. }
  349. void NewFlight::on_newDept_textEdited(const QString &arg1)
  350. {
  351. ui->newDept->setText(arg1.toUpper());
  352. if(arg1.length()>2)
  353. {
  354. QStringList deptList = db::CompleteIcaoOrIata(arg1);
  355. qDebug() << "deptList = " << deptList;
  356. QCompleter *deptCompleter = new QCompleter(deptList, this);
  357. deptCompleter->setCaseSensitivity(Qt::CaseInsensitive);
  358. deptCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
  359. ui->newDept->setCompleter(deptCompleter);
  360. }
  361. }
  362. void NewFlight::on_newDept_editingFinished()
  363. {
  364. if(ui->newDept->text().length()<3)
  365. {
  366. QMessageBox msgBox(this);
  367. msgBox.setText("Please enter a 3- or 4-letter code.");
  368. msgBox.exec();
  369. ui->newDept->setText("");
  370. ui->newDept->setFocus();
  371. }
  372. QStringList deptList;
  373. if(ui->newDept->text().length()>1)
  374. {
  375. QStringList deptList = db::CompleteIcaoOrIata(ui->newDept->text());
  376. if(deptList.length() != 0)
  377. {
  378. dept = deptList.first();
  379. ui->newDept->setText(dept);
  380. }else
  381. {
  382. QMessageBox msgBox(this);
  383. msgBox.setText("No Airport with that name found.");
  384. msgBox.exec();
  385. ui->newDept->setText("");
  386. ui->newDept->setFocus();
  387. }
  388. }
  389. }
  390. void NewFlight::on_newDest_textEdited(const QString &arg1)
  391. {
  392. ui->newDest->setText(arg1.toUpper());
  393. if(arg1.length()>2)
  394. {
  395. QStringList destList = db::CompleteIcaoOrIata(arg1);
  396. QCompleter *destCompleter = new QCompleter(destList, this);
  397. destCompleter->setCaseSensitivity(Qt::CaseInsensitive);
  398. destCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
  399. ui->newDest->setCompleter(destCompleter);
  400. }
  401. }
  402. void NewFlight::on_newDest_editingFinished()
  403. {
  404. if(ui->newDest->text().length()<3)
  405. {
  406. QMessageBox msgBox(this);
  407. msgBox.setText("Please enter a 3- or 4-letter code.");
  408. msgBox.exec();
  409. ui->newDest->setText("");
  410. ui->newDest->setFocus();
  411. }
  412. QStringList destList;
  413. if(ui->newDest->text().length()>1)
  414. {
  415. QStringList destList = db::CompleteIcaoOrIata(ui->newDest->text());
  416. if(destList.length() != 0)
  417. {
  418. dest = destList.first();
  419. ui->newDest->setText(dest);
  420. }else
  421. {
  422. QMessageBox msgBox(this);
  423. msgBox.setText("No Airport with that name found.");
  424. msgBox.exec();
  425. ui->newDest->setText("");
  426. ui->newDest->setFocus();
  427. }
  428. }
  429. }
  430. void NewFlight::on_newDoft_editingFinished()
  431. {
  432. date = ui->newDoft->date();
  433. doft = date.toString(Qt::ISODate);
  434. }
  435. void NewFlight::on_newTofb_editingFinished()
  436. {
  437. ui->newTofb->setText(validateTimeInput(ui->newTofb->text()));
  438. tofb = QTime::fromString(ui->newTofb->text(),"hh:mm");
  439. qDebug() << "New Time Off Blocks: " << tofb;
  440. }
  441. void NewFlight::on_newTonb_editingFinished()
  442. {
  443. ui->newTonb->setText(validateTimeInput(ui->newTonb->text()));
  444. tonb = QTime::fromString(ui->newTonb->text(),"hh:mm");
  445. qDebug() << "New Time On Blocks: " << tonb;
  446. }
  447. void NewFlight::on_newAcft_textEdited(const QString &arg1)
  448. {
  449. if(arg1.length()>1)
  450. {
  451. QStringList acftList = db::newAcftGetString(arg1);
  452. QCompleter *acftCompleter = new QCompleter(acftList, this);
  453. acftCompleter->setCaseSensitivity(Qt::CaseInsensitive);
  454. acftCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
  455. ui->newAcft->setCompleter(acftCompleter);
  456. }
  457. }
  458. void NewFlight::on_newAcft_editingFinished()
  459. {
  460. acft = "-1";// set invalid
  461. if(ui->newAcft->text().contains(" "))// is input from QCompleter
  462. {
  463. QStringList input = ui->newAcft->text().split(" ");
  464. QString registration = input[0].trimmed();
  465. QStringList result = db::newAcftGetString(registration);
  466. if(result.length() > 0)
  467. {
  468. ui->newAcft->setText(result[0]);
  469. acft = db::newAcftGetId(registration);
  470. }else
  471. {
  472. acft = "-1";
  473. }
  474. }else // is input from user
  475. {
  476. QString input = ui->newAcft->text();
  477. QStringList result = db::newAcftGetString(input);
  478. if(result.length() > 0)
  479. {
  480. ui->newAcft->setText(result[0]);
  481. QStringList input = ui->newAcft->text().split(" ");
  482. QString registration = input[0].trimmed();
  483. acft = db::newAcftGetId(registration);
  484. }else
  485. {
  486. acft = "-1";
  487. }
  488. }
  489. if(acft == "-1")
  490. {
  491. QMessageBox::StandardButton reply;
  492. reply = QMessageBox::question(this, "No aircraft found", "No aircraft found.\n Would you like to add a new aircraft to the database?",
  493. QMessageBox::Yes|QMessageBox::No);
  494. if (reply == QMessageBox::Yes) {
  495. NewAcft na(this);
  496. na.exec();
  497. ui->newAcft->setText("");
  498. ui->newAcft->setFocus();
  499. }
  500. }
  501. qDebug() << "Editing finished. Acft: " << acft;
  502. }
  503. void NewFlight::on_newPic_textEdited(const QString &arg1)
  504. {
  505. if(arg1.length()>2)
  506. {
  507. QStringList picList = db::newPicGetString(arg1);
  508. QCompleter *picCompleter = new QCompleter(picList, this);
  509. picCompleter->setCaseSensitivity(Qt::CaseInsensitive);
  510. picCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
  511. ui->newPic->setCompleter(picCompleter);
  512. }
  513. }
  514. void NewFlight::on_newPic_editingFinished()
  515. {
  516. pic = "-1"; // set invalid
  517. if(ui->newPic->text() == "self")
  518. {
  519. pic = "1";
  520. }else
  521. {
  522. QString picname;
  523. QStringList picList = db::newPicGetString(ui->newPic->text());
  524. qDebug() << picList;
  525. if(picList.length()!= 0)
  526. {
  527. picname = picList[0];
  528. ui->newPic->setText(picname);
  529. pic = db::newPicGetId(picname);
  530. }else
  531. {
  532. QMessageBox::StandardButton reply;
  533. reply = QMessageBox::question(this, "No Pilot found", "No pilot found.\n Would you like to add a new pilot to the database?",
  534. QMessageBox::Yes|QMessageBox::No);
  535. if (reply == QMessageBox::Yes)
  536. {
  537. qDebug() << "Add new pilot selected";
  538. nope();
  539. }
  540. }
  541. }
  542. }
  543. /*
  544. * Extras
  545. */
  546. void NewFlight::on_PilotFlyingCheckBox_stateChanged(int)//0, unchecked, 2 checked
  547. {
  548. if(ui->PilotFlyingCheckBox->isChecked()){
  549. ui->TakeoffSpinBox->setValue(1);
  550. ui->TakeoffCheckBox->setCheckState(Qt::Checked);
  551. ui->LandingSpinBox->setValue(1);
  552. ui->LandingCheckBox->setCheckState(Qt::Checked);
  553. }else if(!ui->PilotFlyingCheckBox->isChecked()){
  554. ui->TakeoffSpinBox->setValue(0);
  555. ui->TakeoffCheckBox->setCheckState(Qt::Unchecked);
  556. ui->LandingSpinBox->setValue(0);
  557. ui->LandingCheckBox->setCheckState(Qt::Unchecked);
  558. }
  559. }
  560. void NewFlight::on_setAsDefaultButton_clicked()
  561. {
  562. storeSettings();
  563. }
  564. void NewFlight::on_restoreDefaultButton_clicked()
  565. {
  566. restoreSettings();
  567. }
  568. void NewFlight::on_buttonBox_accepted()
  569. {
  570. on_newDoft_editingFinished();// - activate slots in case user has been active in last input before clicking submit
  571. on_newTonb_editingFinished();
  572. on_newTofb_editingFinished();
  573. on_newDept_editingFinished();
  574. on_newDest_editingFinished();
  575. on_newAcft_editingFinished();
  576. on_newPic_editingFinished();
  577. QVector<QString> flight;
  578. flight = collectInput();
  579. if(verifyInput())
  580. {
  581. db::CommitFlight(flight);
  582. qDebug() << flight << "Has been commited.";
  583. QMessageBox msgBox(this);
  584. msgBox.setText("Flight has been commited.");
  585. msgBox.exec();
  586. }else
  587. {
  588. qDebug() << "Invalid Input. No entry has been made in the database.";
  589. db::CommitToScratchpad(flight);
  590. QMessageBox msgBox(this);
  591. msgBox.setText("Invalid entries detected. Please check your input.");
  592. msgBox.exec();
  593. NewFlight nf(this);
  594. nf.exec();
  595. }
  596. }
  597. void NewFlight::on_buttonBox_rejected()
  598. {
  599. qDebug() << "NewFlight: Rejected\n";
  600. }
  601. void NewFlight::on_ApproachComboBox_currentTextChanged(const QString &arg1)
  602. {
  603. if(arg1 == "ILS CAT III"){
  604. ui->AutolandCheckBox->setCheckState(Qt::Checked);
  605. ui->AutolandSpinBox->setValue(1);
  606. }else{
  607. ui->AutolandCheckBox->setCheckState(Qt::Unchecked);
  608. ui->AutolandSpinBox->setValue(0);
  609. }
  610. ApproachType = arg1;
  611. qDebug() << "Approach Type: " << ApproachType;
  612. }
  613. /*
  614. * Times
  615. */
  616. void NewFlight::on_nightTimeLineEdit_editingFinished()
  617. {
  618. ui->nightTimeLineEdit->setText(validateTimeInput(ui->nightTimeLineEdit->text()));
  619. }