newflightdialog.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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 "newflightdialog.h"
  19. #include "ui_newflight.h"
  20. #include "src/gui/dialogues/newtaildialog.h"
  21. #include "src/gui/dialogues/newpilotdialog.h"
  22. #include "src/functions/acalc.h"
  23. #include "src/testing/atimer.h"
  24. #include "src/database/adatabase.h"
  25. #include "src/oplconstants.h"
  26. #include "src/testing/adebug.h"
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. /// constants ///
  29. ///////////////////////////////////////////////////////////////////////////////////////////////////
  30. static const auto EMPTY_STRING=QStringLiteral("");
  31. static const auto NAME_RX = QLatin1String("((\\p{L}+)?('|\\-|,)?(\\p{L}+)?)");
  32. static const auto ADD_NAME_RX = QLatin1String("(\\s?(\\p{L}+('|\\-|,)?\\p{L}+?))?");
  33. static const auto TIME_VALID_RGX = QRegularExpression(
  34. "([01]?[0-9]|2[0-3]):?[0-5][0-9]?");
  35. static const auto LOC_VALID_RGX = QRegularExpression(
  36. "[a-zA-Z0-9]{1,4}");
  37. static const auto AIRCRAFT_VALID_RGX = QRegularExpression(
  38. "\\w+\\-?(\\w+)?");
  39. static const auto NAME_VALID_RGX = QRegularExpression(
  40. NAME_RX + ADD_NAME_RX + ADD_NAME_RX + ADD_NAME_RX + ",?\\s?" // up to 4 first names
  41. + NAME_RX + ADD_NAME_RX + ADD_NAME_RX + ADD_NAME_RX ); // up to 4 last names
  42. static const auto DATE_VALID_RGX = QRegularExpression(
  43. "^([1-9][0-9]{3}).?(1[0-2]|0[1-9]).?(3[01]|0[1-9]|[12][0-9])?$");
  44. static const auto SELF_RX = QRegularExpression(
  45. "self", QRegularExpression::CaseInsensitiveOption);
  46. static const auto MANDATORY_LINE_EDITS_DISPLAY_NAMES = QMap<int, QLatin1String> {
  47. {0, QLatin1String("Date of Flight")}, {1, QLatin1String("Departure Airport")},
  48. {2, QLatin1String("Destination Airport")}, {3, QLatin1String("Time Off Blocks")},
  49. {4, QLatin1String("Time on Blocks")}, {5, QLatin1String("PIC Name")},
  50. {6, QLatin1String("Aircraft Registration")}
  51. };
  52. ///////////////////////////////////////////////////////////////////////////////////////////////////
  53. /// Construction ///
  54. ///////////////////////////////////////////////////////////////////////////////////////////////////
  55. /// [F] The general idea for this dialog is this:
  56. /// - Most line edits have validators and completers.
  57. /// - Validators are based on regular expressions, serving as raw input validation
  58. /// - The Completers are based off the database and provide auto-completion
  59. /// - mandatory line edits only emit editing finished if their content has passed
  60. /// raw input validation or focus is lost.
  61. /// - Editing finished triggers validating inputs by mapping them to Database values
  62. /// where required and results in either pass or fail.
  63. /// - A QBitArray is mainained containing the state of validity of the mandatory line edits
  64. /// - The deducted entries are automatically filled if all mandatory entries
  65. /// are valid.
  66. /// - Comitting an entry to the database is only allowed if all mandatory inputs are valid.
  67. ///
  68. /// if the user presses "OK", check if all mandatory inputs are valid,
  69. /// check if optional user inputs are valid and commit.
  70. ///
  71. /// For the completion and mapping, I have settled on a more low-level approach using
  72. /// Completers based on QStringLists and mapping with QMaps.
  73. ///
  74. /// I implemented the Completers and mapping based on a QSqlTableModel which would
  75. /// have been quite nice, since it would keep all data in one place, providing both completion
  76. /// and mapping in one model.
  77. /// But as we have seen before with the more high-level qt classes, they are quite slow on execution
  78. /// when used for tasks they were probably not designed to do.
  79. /// Mapping a registration to an ID for example took around 300ms, which is very
  80. /// noticeable in the UI and not an acceptable user experience. Using QStringLists and QMaps
  81. /// this goes down to around 5ms.
  82. NewFlightDialog::NewFlightDialog(QWidget *parent) :
  83. QDialog(parent),
  84. ui(new Ui::NewFlight)
  85. {
  86. ui->setupUi(this);
  87. flightEntry = AFlightEntry();
  88. setup();
  89. if (ASettings::read(ASettings::NewFlight::FunctionComboBox).toString() == "SIC") {
  90. ui->picNameLineEdit->setText(QStringLiteral(""));
  91. ui->secondPilotNameLineEdit->setText("self");
  92. }
  93. if(ASettings::read(ASettings::NewFlight::FunctionComboBox).toString() == "PIC"){
  94. ui->picNameLineEdit->setText("self");
  95. emit ui->picNameLineEdit->editingFinished();
  96. }
  97. }
  98. NewFlightDialog::NewFlightDialog(int row_id, QWidget *parent) :
  99. QDialog(parent),
  100. ui(new Ui::NewFlight)
  101. {
  102. ui->setupUi(this);
  103. flightEntry = aDB->getFlightEntry(row_id);
  104. setup();
  105. formFiller();
  106. }
  107. NewFlightDialog::~NewFlightDialog()
  108. {
  109. delete ui;
  110. }
  111. ///////////////////////////////////////////////////////////////////////////////////////////////////
  112. /// Methods - setup and maintenance of dialog ///
  113. ///////////////////////////////////////////////////////////////////////////////////////////////////
  114. void NewFlightDialog::setup()
  115. {
  116. updateEnabled = true;
  117. setupButtonGroups();
  118. setupRawInputValidation();
  119. setupSignalsAndSlots();
  120. readSettings();
  121. // Visually mark mandatory fields
  122. ui->deptLocLineEdit->setStyleSheet("border: 0.1ex solid #3daee9");
  123. ui->destLocLineEdit->setStyleSheet("border: 0.1ex solid #3daee9");
  124. ui->tofbTimeLineEdit->setStyleSheet("border: 0.1ex solid #3daee9");
  125. ui->tonbTimeLineEdit->setStyleSheet("border: 0.1ex solid #3daee9");
  126. ui->picNameLineEdit->setStyleSheet("border: 0.1ex solid #3daee9");
  127. ui->acftLineEdit->setStyleSheet("border: 0.1ex solid #3daee9");
  128. ui->doftLineEdit->setText(QDate::currentDate().toString(Qt::ISODate));
  129. emit ui->doftLineEdit->editingFinished();
  130. ui->deptLocLineEdit->setFocus();
  131. }
  132. void NewFlightDialog::readSettings()
  133. {
  134. DEB << "Reading Settings...";
  135. QSettings settings;
  136. ui->FunctionComboBox->setCurrentText(ASettings::read(ASettings::FlightLogging::Function).toString());
  137. ui->ApproachComboBox->setCurrentText(ASettings::read(ASettings::FlightLogging::Approach).toString());
  138. ASettings::read(ASettings::FlightLogging::PilotFlying).toBool() ? ui->PilotFlyingCheckBox->setChecked(true)
  139. : ui->PilotMonitoringCheckBox->setChecked(true);
  140. ui->TakeoffSpinBox->setValue(ASettings::read(ASettings::FlightLogging::NumberTakeoffs).toInt());
  141. ui->TakeoffSpinBox->value() > 0 ? ui->TakeoffCheckBox->setChecked(true)
  142. : ui->TakeoffCheckBox->setChecked(false);
  143. ui->LandingSpinBox->setValue(ASettings::read(ASettings::FlightLogging::NumberLandings).toInt());
  144. ui->LandingSpinBox->value() > 0 ? ui->LandingCheckBox->setChecked(true)
  145. : ui->LandingCheckBox->setChecked(false);
  146. if (ASettings::read(ASettings::FlightLogging::LogIFR).toBool()) {
  147. ui->IfrCheckBox->setChecked(true);
  148. } else {
  149. ui->VfrCheckBox->setChecked(true);
  150. }
  151. ui->FlightNumberLineEdit->setText(ASettings::read(ASettings::FlightLogging::FlightNumberPrefix).toString());
  152. ui->calendarCheckBox->setChecked(ASettings::read(ASettings::FlightLogging::PopupCalendar).toBool());
  153. // Debug
  154. ASettings::write(ASettings::FlightLogging::FlightTimeFormat, opl::time::Default);
  155. //[F]: Support for Decimal Logging is not implemented yet.
  156. flightTimeFormat = static_cast<opl::time::FlightTimeFormat>(
  157. ASettings::read(ASettings::FlightLogging::FlightTimeFormat).toInt());
  158. }
  159. void NewFlightDialog::writeSettings()
  160. {
  161. DEB << "Writing Settings...";
  162. ASettings::write(ASettings::FlightLogging::Function, ui->FunctionComboBox->currentText());
  163. ASettings::write(ASettings::FlightLogging::Approach, ui->ApproachComboBox->currentText());
  164. ASettings::write(ASettings::FlightLogging::PilotFlying, ui->PilotFlyingCheckBox->isChecked());
  165. ASettings::write(ASettings::FlightLogging::NumberTakeoffs, ui->TakeoffSpinBox->value());
  166. ASettings::write(ASettings::FlightLogging::NumberLandings, ui->LandingSpinBox->value());
  167. ASettings::write(ASettings::FlightLogging::LogIFR, ui->IfrCheckBox->isChecked());
  168. ASettings::write(ASettings::FlightLogging::PopupCalendar, ui->calendarCheckBox->isChecked());
  169. }
  170. void NewFlightDialog::setupButtonGroups()
  171. {
  172. QButtonGroup *FlightRulesGroup = new QButtonGroup(this);
  173. FlightRulesGroup->addButton(ui->IfrCheckBox);
  174. FlightRulesGroup->addButton(ui->VfrCheckBox);
  175. QButtonGroup *PilotTaskGroup = new QButtonGroup(this);
  176. PilotTaskGroup->addButton(ui->PilotFlyingCheckBox);
  177. PilotTaskGroup->addButton(ui->PilotMonitoringCheckBox);
  178. }
  179. void NewFlightDialog::setupRawInputValidation()
  180. {
  181. // get Maps
  182. pilotsIdMap = aDB->getIdMap(ADatabaseTarget::pilots);
  183. tailsIdMap = aDB->getIdMap(ADatabaseTarget::tails);
  184. airportIcaoIdMap = aDB->getIdMap(ADatabaseTarget::airport_identifier_icao);
  185. airportIataIdMap = aDB->getIdMap(ADatabaseTarget::airport_identifier_iata);
  186. airportNameIdMap = aDB->getIdMap(ADatabaseTarget::airport_names);
  187. //get Completer Lists
  188. pilotList = aDB->getCompletionList(ADatabaseTarget::pilots);
  189. tailsList = aDB->getCompletionList(ADatabaseTarget::registrations);
  190. airportList = aDB->getCompletionList(ADatabaseTarget::airport_identifier_all);
  191. auto tempList = QStringList();
  192. // define tuples
  193. const std::tuple<QString, QStringList*, QRegularExpression>
  194. location_line_edit_settings = {QStringLiteral("Loc"), &airportList, LOC_VALID_RGX};
  195. const std::tuple<QString, QStringList*, QRegularExpression>
  196. name_line_edit_settings = {QStringLiteral("Name"), &pilotList, NAME_VALID_RGX};
  197. const std::tuple<QString, QStringList*, QRegularExpression>
  198. acft_line_edit_settings = {QStringLiteral("acft"), &tailsList, AIRCRAFT_VALID_RGX};
  199. const std::tuple<QString, QStringList*, QRegularExpression>
  200. time_line_edit_settings = {QStringLiteral("Time"), &tempList, TIME_VALID_RGX};
  201. const QList<std::tuple<QString, QStringList*, QRegularExpression>> line_edit_settings = {
  202. location_line_edit_settings,
  203. name_line_edit_settings,
  204. acft_line_edit_settings,
  205. time_line_edit_settings
  206. };
  207. //get line edits, set up completers and validators
  208. auto line_edits = ui->flightDataTab->findChildren<QLineEdit*>();
  209. for (const auto &item : line_edit_settings) {
  210. for (const auto &line_edit : line_edits) {
  211. if(line_edit->objectName().contains(std::get<0>(item))) {
  212. DEB << "Setting up: " << line_edit->objectName();
  213. // Set Validator
  214. auto validator = new QRegularExpressionValidator(std::get<2>(item), line_edit);
  215. line_edit->setValidator(validator);
  216. // Set Completer
  217. auto completer = new QCompleter(*std::get<1>(item), line_edit);
  218. completer->setCaseSensitivity(Qt::CaseInsensitive);
  219. completer->setCompletionMode(QCompleter::PopupCompletion);
  220. completer->setFilterMode(Qt::MatchContains);
  221. line_edit->setCompleter(completer);
  222. }
  223. }
  224. }
  225. // populate Mandatory Line Edits list and prepare QBitArray
  226. mandatoryLineEdits = {
  227. ui->doftLineEdit,
  228. ui->deptLocLineEdit,
  229. ui->destLocLineEdit,
  230. ui->tofbTimeLineEdit,
  231. ui->tonbTimeLineEdit,
  232. ui->picNameLineEdit,
  233. ui->acftLineEdit,
  234. };
  235. primaryTimeLineEdits = {
  236. ui->tofbTimeLineEdit,
  237. ui->tonbTimeLineEdit
  238. };
  239. pilotsLineEdits = {
  240. ui->picNameLineEdit,
  241. ui->secondPilotNameLineEdit,
  242. ui->thirdPilotNameLineEdit
  243. };
  244. mandatoryLineEditsGood.resize(mandatoryLineEdits.size());
  245. }
  246. void NewFlightDialog::setupSignalsAndSlots()
  247. {
  248. auto line_edits = this->findChildren<QLineEdit*>();
  249. for (const auto &line_edit : line_edits){
  250. line_edit->installEventFilter(this);
  251. if(line_edit->objectName().contains(QStringLiteral("Loc"))){
  252. QObject::connect(line_edit, &QLineEdit::textChanged,
  253. this, &NewFlightDialog::onToUpperTriggered_textChanged);
  254. }
  255. if(line_edit->objectName().contains(QStringLiteral("acft"))){
  256. QObject::connect(line_edit, &QLineEdit::textChanged,
  257. this, &NewFlightDialog::onToUpperTriggered_textChanged);
  258. }
  259. if(line_edit->objectName().contains(QStringLiteral("Name"))){
  260. QObject::connect(line_edit, &QLineEdit::editingFinished,
  261. this, &NewFlightDialog::onPilotNameLineEdit_editingFinished);
  262. }
  263. if(line_edit->objectName().contains(QStringLiteral("Time"))){
  264. QObject::connect(line_edit, &QLineEdit::editingFinished,
  265. this, &NewFlightDialog::onTimeLineEdit_editingFinished);
  266. }
  267. }
  268. #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
  269. for (const auto &line_edit : qAsConst(mandatoryLineEdits)) {
  270. if(line_edit->objectName().contains(QStringLiteral("doft")))
  271. break;
  272. QObject::connect(line_edit->completer(), QOverload<const QString &>::of(&QCompleter::highlighted),
  273. this, &NewFlightDialog::onCompleter_highlighted);
  274. QObject::connect(line_edit->completer(), QOverload<const QString &>::of(&QCompleter::activated),
  275. this, &NewFlightDialog::onCompleter_activated);
  276. }
  277. #endif
  278. }
  279. void NewFlightDialog::setPopUpCalendarEnabled(bool state)
  280. {
  281. ui->flightDataTabWidget->setCurrentIndex(0);
  282. ui->flightDataTabWidget->removeTab(2); // hide calendar widget
  283. if (state) {
  284. DEB << "Enabling pop-up calendar widget...";
  285. ui->calendarWidget->installEventFilter(this);
  286. ui->placeLabel1->installEventFilter(this);
  287. ui->doftLineEdit->installEventFilter(this);
  288. QObject::connect(ui->calendarWidget, &QCalendarWidget::clicked,
  289. this, &NewFlightDialog::onCalendarWidget_clicked);
  290. QObject::connect(ui->calendarWidget, &QCalendarWidget::activated,
  291. this, &NewFlightDialog::onCalendarWidget_selected);
  292. } else {
  293. DEB << "Disabling pop-up calendar widget...";
  294. ui->calendarWidget->removeEventFilter(this);
  295. ui->placeLabel1->removeEventFilter(this);
  296. ui->doftLineEdit->removeEventFilter(this);
  297. QObject::disconnect(ui->calendarWidget, &QCalendarWidget::clicked,
  298. this, &NewFlightDialog::onCalendarWidget_clicked);
  299. QObject::disconnect(ui->calendarWidget, &QCalendarWidget::activated,
  300. this, &NewFlightDialog::onCalendarWidget_selected);
  301. }
  302. }
  303. bool NewFlightDialog::eventFilter(QObject* object, QEvent* event)
  304. {
  305. if (object == ui->doftLineEdit && event->type() == QEvent::MouseButtonPress) {
  306. onDoftLineEdit_entered();
  307. return false; // let the event continue to the edit
  308. }
  309. auto line_edit = qobject_cast<QLineEdit*>(object);
  310. if (line_edit != nullptr) {
  311. if (mandatoryLineEdits.contains(line_edit) && event->type() == QEvent::FocusIn) {
  312. mandatoryLineEditsGood.setBit(mandatoryLineEdits.indexOf(line_edit), false);
  313. DEB << "Editing " << line_edit->objectName();
  314. // set verification bit to false when entering a mandatory line edit
  315. return false;
  316. }
  317. if (mandatoryLineEdits.contains(line_edit) && event->type() == QEvent::KeyPress) {
  318. // show completion menu when pressing down arrow
  319. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
  320. if (keyEvent->key() == Qt::Key_Down) {
  321. DEB << "Key down event.";
  322. line_edit->completer()->complete();
  323. }
  324. return false;
  325. }
  326. if (line_edit->objectName().contains("Name") && event->type() == QEvent::KeyPress) {
  327. // show completion menu when pressing down arrow
  328. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
  329. if (keyEvent->key() == Qt::Key_Down) {
  330. DEB << "Key down event.";
  331. line_edit->completer()->complete();
  332. }
  333. return false;
  334. }
  335. }
  336. return false;
  337. }
  338. ///////////////////////////////////////////////////////////////////////////////////////////////////
  339. /// Methods - Input Processing ///
  340. ///////////////////////////////////////////////////////////////////////////////////////////////////
  341. /*!
  342. * \brief Fills the deductable items in the Dialog based on mandatory data ui selections.
  343. */
  344. void NewFlightDialog::fillDeductibleData()
  345. {
  346. // check if mandatory line edits are valid
  347. if (mandatoryLineEditsGood.count(true) != 7) {
  348. return;
  349. }
  350. //zero out labels and line edits to delete previous calculations
  351. QList<QLineEdit*> LE = {ui->tSPSETimeLineEdit, ui->tSPMETimeLineEdit, ui->tMPTimeLineEdit, ui->tIFRTimeLineEdit,
  352. ui->tNIGHTTimeLineEdit,ui->tPICTimeLineEdit, ui->tPICUSTimeLineEdit, ui->tSICTimeLineEdit,
  353. ui->tDUALTimeLineEdit, ui->tFITimeLineEdit,};
  354. QList<QLabel*> LB = {ui->tSPSELabel, ui->tSPMELabel, ui->tMPLabel, ui->tIFRLabel, ui->tNIGHTLabel,
  355. ui->tPICLabel, ui->tPICUSLabel, ui->tSICLabel, ui->tDUALLabel, ui->tFILabel};
  356. for(const auto& widget : LE) {widget->setText(EMPTY_STRING);}
  357. for(const auto& widget : LB) {widget->setText(opl::db::NULL_TIME_hhmm);}
  358. //Calculate block time
  359. const auto tofb = ATime::fromString(ui->tofbTimeLineEdit->text());
  360. const auto tonb = ATime::fromString(ui->tonbTimeLineEdit->text());
  361. const auto tblk = ATime::blocktime(tofb, tonb);
  362. const auto block_time_string = ATime::toString(tblk, flightTimeFormat);
  363. const auto block_minutes = ATime::toMinutes(tblk);
  364. ui->tblkTimeLineEdit->setText(block_time_string);
  365. // get acft data and fill deductible entries
  366. auto acft = aDB->getTailEntry(tailsIdMap.value(ui->acftLineEdit->text()));
  367. if (acft.getData().isEmpty())
  368. DEB << "Error: No valid aircraft object available, unable to deterime auto times.";
  369. // SP SE
  370. if(acft.getData().value(opl::db::TAILS_MULTIPILOT).toInt() == 0
  371. && acft.getData().value(opl::db::TAILS_MULTIENGINE).toInt() == 0){
  372. ui->tSPSETimeLineEdit->setText(block_time_string);
  373. ui->tSPSELabel->setText(block_time_string);
  374. }
  375. // SP ME
  376. if(acft.getData().value(opl::db::TAILS_MULTIPILOT).toInt() == 0
  377. && acft.getData().value(opl::db::TAILS_MULTIENGINE).toInt() == 1){
  378. ui->tSPMETimeLineEdit->setText(block_time_string);
  379. ui->tSPMELabel->setText(block_time_string);
  380. }
  381. // MP
  382. if(acft.getData().value(opl::db::TAILS_MULTIPILOT).toInt() == 1){
  383. ui->tMPTimeLineEdit->setText(block_time_string);
  384. ui->tMPLabel->setText(block_time_string);
  385. }
  386. // TOTAL
  387. ui->tblkLabel->setText("<b>" + block_time_string + "</b>");
  388. // IFR
  389. if(ui->IfrCheckBox->isChecked()){
  390. ui->tIFRTimeLineEdit->setText(block_time_string);
  391. ui->tIFRLabel->setText(block_time_string);
  392. }
  393. // Night
  394. auto dept_date = ui->doftLineEdit->text() + 'T'
  395. + ATime::toString(tofb);
  396. auto dept_date_time = QDateTime::fromString(dept_date, QStringLiteral("yyyy-MM-ddThh:mm"));
  397. const int night_angle = ASettings::read(ASettings::FlightLogging::NightAngle).toInt();
  398. auto night_time = ATime::fromMinutes(ACalc::calculateNightTime(
  399. ui->deptLocLineEdit->text(),
  400. ui->destLocLineEdit->text(),
  401. dept_date_time,
  402. block_minutes,
  403. night_angle));
  404. ui->tNIGHTTimeLineEdit->setText(ATime::toString(night_time, flightTimeFormat));
  405. ui->tNIGHTLabel->setText(ATime::toString(night_time, flightTimeFormat));
  406. // Function times
  407. switch (ui->FunctionComboBox->currentIndex()) {
  408. case 0://PIC
  409. ui->tPICTimeLineEdit->setText(block_time_string);
  410. ui->tPICLabel->setText(block_time_string);
  411. break;
  412. case 1://PICus
  413. ui->tPICUSTimeLineEdit->setText(block_time_string);
  414. ui->tPICUSLabel->setText(block_time_string);
  415. break;
  416. case 2://Co-Pilot
  417. ui->tSICTimeLineEdit->setText(block_time_string);
  418. ui->tSICLabel->setText(block_time_string);
  419. break;
  420. case 3://Dual
  421. ui->tDUALTimeLineEdit->setText(block_time_string);
  422. ui->tDUALLabel->setText(block_time_string);
  423. break;
  424. case 4://Instructor
  425. ui->tFITimeLineEdit->setText(block_time_string);
  426. ui->tFILabel->setText(block_time_string);
  427. ui->tPICTimeLineEdit->setText(block_time_string);
  428. ui->tPICLabel->setText(block_time_string);
  429. }
  430. }
  431. /*!
  432. * \brief Collect input and create a Data map for the entry object.
  433. *
  434. * This function should only be called if input validation has been passed, since
  435. * no input validation is done in this step and input data is assumed to be valid.
  436. * \return
  437. */
  438. RowData NewFlightDialog::collectInput()
  439. {
  440. RowData newData;
  441. DEB << "Collecting Input...";
  442. //Block Time
  443. const auto tofb = ATime::fromString(ui->tofbTimeLineEdit->text());
  444. const auto tonb = ATime::fromString(ui->tonbTimeLineEdit->text());
  445. const auto tblk = ATime::blocktime(tofb, tonb);
  446. const auto block_minutes = ATime::toMinutes(tblk);
  447. // Mandatory data
  448. newData.insert(opl::db::FLIGHTS_DOFT, ui->doftLineEdit->text());
  449. newData.insert(opl::db::FLIGHTS_DEPT, ui->deptLocLineEdit->text());
  450. newData.insert(opl::db::FLIGHTS_TOFB, ATime::toMinutes(tofb));
  451. newData.insert(opl::db::FLIGHTS_DEST, ui->destLocLineEdit->text());
  452. newData.insert(opl::db::FLIGHTS_TONB, ATime::toMinutes(tonb));
  453. newData.insert(opl::db::FLIGHTS_TBLK, block_minutes);
  454. // Aircraft
  455. newData.insert(opl::db::FLIGHTS_ACFT, tailsIdMap.value(ui->acftLineEdit->text()));
  456. // Pilots
  457. newData.insert(opl::db::FLIGHTS_PIC, pilotsIdMap.value(ui->picNameLineEdit->text()));
  458. newData.insert(opl::db::FLIGHTS_SECONDPILOT, pilotsIdMap.value(ui->secondPilotNameLineEdit->text()));
  459. newData.insert(opl::db::FLIGHTS_THIRDPILOT, pilotsIdMap.value(ui->thirdPilotNameLineEdit->text()));
  460. // Extra Times
  461. ui->tSPSETimeLineEdit->text().isEmpty() ?
  462. newData.insert(opl::db::FLIGHTS_TSPSE, EMPTY_STRING)
  463. : newData.insert(opl::db::FLIGHTS_TSPSE, stringToMinutes(
  464. ui->tSPSETimeLineEdit->text(), flightTimeFormat));
  465. ui->tSPMETimeLineEdit->text().isEmpty() ?
  466. newData.insert(opl::db::FLIGHTS_TSPME, EMPTY_STRING)
  467. : newData.insert(opl::db::FLIGHTS_TSPME, stringToMinutes(
  468. ui->tSPMETimeLineEdit->text(), flightTimeFormat));
  469. ui->tMPTimeLineEdit->text().isEmpty() ?
  470. newData.insert(opl::db::FLIGHTS_TMP, EMPTY_STRING)
  471. : newData.insert(opl::db::FLIGHTS_TMP, stringToMinutes(
  472. ui->tMPTimeLineEdit->text(), flightTimeFormat));
  473. if (ui->IfrCheckBox->isChecked()) {
  474. newData.insert(opl::db::FLIGHTS_TIFR, block_minutes);
  475. } else {
  476. newData.insert(opl::db::FLIGHTS_TIFR, EMPTY_STRING);
  477. }
  478. // Night
  479. const auto dept_date = ui->doftLineEdit->text() + 'T'
  480. + ATime::toString(tofb);
  481. const auto dept_date_time = QDateTime::fromString(dept_date, QStringLiteral("yyyy-MM-ddThh:mm"));
  482. const int night_angle = ASettings::read(ASettings::FlightLogging::NightAngle).toInt();
  483. const auto night_time = ATime::fromMinutes(ACalc::calculateNightTime(
  484. ui->deptLocLineEdit->text(),
  485. ui->destLocLineEdit->text(),
  486. dept_date_time,
  487. block_minutes,
  488. night_angle));
  489. const auto night_minutes = ATime::toMinutes(night_time);
  490. newData.insert(opl::db::FLIGHTS_TNIGHT, night_minutes);
  491. // Function times - This is a little explicit but these are mutually exclusive so its better to be safe than sorry here.
  492. switch (ui->FunctionComboBox->currentIndex()) {
  493. case 0://PIC
  494. newData.insert(opl::db::FLIGHTS_TPIC, block_minutes);
  495. newData.insert(opl::db::FLIGHTS_TPICUS, EMPTY_STRING);
  496. newData.insert(opl::db::FLIGHTS_TSIC, EMPTY_STRING);
  497. newData.insert(opl::db::FLIGHTS_TDUAL, EMPTY_STRING);
  498. newData.insert(opl::db::FLIGHTS_TFI, EMPTY_STRING);
  499. break;
  500. case 1://PICUS
  501. newData.insert(opl::db::FLIGHTS_TPIC, EMPTY_STRING);
  502. newData.insert(opl::db::FLIGHTS_TPICUS, block_minutes);
  503. newData.insert(opl::db::FLIGHTS_TSIC, EMPTY_STRING);
  504. newData.insert(opl::db::FLIGHTS_TDUAL, EMPTY_STRING);
  505. newData.insert(opl::db::FLIGHTS_TFI, EMPTY_STRING);
  506. break;
  507. case 2://Co-Pilot
  508. newData.insert(opl::db::FLIGHTS_TPIC, EMPTY_STRING);
  509. newData.insert(opl::db::FLIGHTS_TPICUS, EMPTY_STRING);
  510. newData.insert(opl::db::FLIGHTS_TSIC, block_minutes);
  511. newData.insert(opl::db::FLIGHTS_TDUAL, EMPTY_STRING);
  512. newData.insert(opl::db::FLIGHTS_TFI, EMPTY_STRING);
  513. break;
  514. case 3://Dual
  515. newData.insert(opl::db::FLIGHTS_TPIC, EMPTY_STRING);
  516. newData.insert(opl::db::FLIGHTS_TPICUS, EMPTY_STRING);
  517. newData.insert(opl::db::FLIGHTS_TSIC, EMPTY_STRING);
  518. newData.insert(opl::db::FLIGHTS_TDUAL, block_minutes);
  519. newData.insert(opl::db::FLIGHTS_TFI, EMPTY_STRING);
  520. break;
  521. case 4://Instructor
  522. newData.insert(opl::db::FLIGHTS_TPIC, block_minutes);
  523. newData.insert(opl::db::FLIGHTS_TPICUS, EMPTY_STRING);
  524. newData.insert(opl::db::FLIGHTS_TSIC, EMPTY_STRING);
  525. newData.insert(opl::db::FLIGHTS_TDUAL, EMPTY_STRING);
  526. newData.insert(opl::db::FLIGHTS_TFI, block_minutes);
  527. }
  528. // Pilot Flying
  529. newData.insert(opl::db::FLIGHTS_PILOTFLYING, ui->PilotFlyingCheckBox->isChecked());
  530. // TO and LDG - again a bit explicit, but we need to check for both night to day as well as day to night transitions.
  531. if (ui->TakeoffCheckBox->isChecked()) {
  532. if (night_minutes == 0) { // all day
  533. newData.insert(opl::db::FLIGHTS_TODAY, ui->TakeoffSpinBox->value());
  534. newData.insert(opl::db::FLIGHTS_TONIGHT, 0);
  535. } else if (night_minutes == block_minutes) { // all night
  536. newData.insert(opl::db::FLIGHTS_TODAY, 0);
  537. newData.insert(opl::db::FLIGHTS_TONIGHT, ui->TakeoffSpinBox->value());
  538. } else {
  539. if(ACalc::isNight(ui->deptLocLineEdit->text(), dept_date_time, night_angle)) {
  540. newData.insert(opl::db::FLIGHTS_TODAY, 0);
  541. newData.insert(opl::db::FLIGHTS_TONIGHT, ui->TakeoffSpinBox->value());
  542. } else {
  543. newData.insert(opl::db::FLIGHTS_TODAY, ui->TakeoffSpinBox->value());
  544. newData.insert(opl::db::FLIGHTS_TONIGHT, 0);
  545. }
  546. }
  547. } else {
  548. newData.insert(opl::db::FLIGHTS_TODAY, 0);
  549. newData.insert(opl::db::FLIGHTS_TONIGHT, 0);
  550. }
  551. if (ui->LandingCheckBox->isChecked()) {
  552. if (night_minutes == 0) { // all day
  553. newData.insert(opl::db::FLIGHTS_LDGDAY, ui->LandingSpinBox->value());
  554. newData.insert(opl::db::FLIGHTS_LDGNIGHT, 0);
  555. } else if (night_minutes == block_minutes) { // all night
  556. newData.insert(opl::db::FLIGHTS_LDGDAY, 0);
  557. newData.insert(opl::db::FLIGHTS_LDGNIGHT, ui->LandingSpinBox->value());
  558. } else { //check
  559. const auto dest_date = ui->doftLineEdit->text() + 'T'
  560. + ATime::toString(tonb);
  561. const auto dest_date_time = QDateTime::fromString(dest_date, QStringLiteral("yyyy-MM-ddThh:mm"));
  562. if (ACalc::isNight(ui->destLocLineEdit->text(), dest_date_time, night_angle)) {
  563. newData.insert(opl::db::FLIGHTS_LDGDAY, 0);
  564. newData.insert(opl::db::FLIGHTS_LDGNIGHT, ui->LandingSpinBox->value());
  565. } else {
  566. newData.insert(opl::db::FLIGHTS_LDGDAY, ui->LandingSpinBox->value());
  567. newData.insert(opl::db::FLIGHTS_LDGNIGHT, 0);
  568. }
  569. }
  570. } else {
  571. newData.insert(opl::db::FLIGHTS_LDGDAY, 0);
  572. newData.insert(opl::db::FLIGHTS_LDGNIGHT, 0);
  573. }
  574. newData.insert(opl::db::FLIGHTS_AUTOLAND, ui->AutolandSpinBox->value());
  575. newData.insert(opl::db::FLIGHTS_APPROACHTYPE, ui->ApproachComboBox->currentText());
  576. newData.insert(opl::db::FLIGHTS_FLIGHTNUMBER, ui->FlightNumberLineEdit->text());
  577. newData.insert(opl::db::FLIGHTS_REMARKS, ui->RemarksLineEdit->text());
  578. DEB << "New Flight Data: " << newData;
  579. return newData;
  580. }
  581. void NewFlightDialog::formFiller()
  582. {
  583. DEB << "Filling Line Edits...";
  584. // get Line Edits
  585. auto line_edits = this->findChildren<QLineEdit *>();
  586. QStringList line_edits_names;
  587. for (const auto& le : line_edits) {
  588. line_edits_names << le->objectName();
  589. }
  590. ui->acftLineEdit->setText(flightEntry.getRegistration());
  591. line_edits_names.removeOne("acftLineEdit");
  592. for (const auto& data_key : flightEntry.getData().keys()) {
  593. auto rx = QRegularExpression(data_key + "LineEdit");//acftLineEdit
  594. for(const auto& leName : qAsConst(line_edits_names)){
  595. if(rx.match(leName).hasMatch()) {
  596. //DEB << "Loc Match found: " << key << " - " << leName);
  597. auto line_edit = this->findChild<QLineEdit *>(leName);
  598. if(line_edit != nullptr){
  599. line_edit->setText(flightEntry.getData().value(data_key).toString());
  600. line_edits_names.removeOne(leName);
  601. }
  602. break;
  603. }
  604. }
  605. rx = QRegularExpression(data_key + "Loc\\w+?");
  606. for(const auto& leName : qAsConst(line_edits_names)){
  607. if(rx.match(leName).hasMatch()) {
  608. //DEB << "Loc Match found: " << key << " - " << leName);
  609. auto line_edit = this->findChild<QLineEdit *>(leName);
  610. if(line_edit != nullptr){
  611. line_edit->setText(flightEntry.getData().value(data_key).toString());
  612. line_edits_names.removeOne(leName);
  613. }
  614. break;
  615. }
  616. }
  617. rx = QRegularExpression(data_key + "Time\\w+?");
  618. for(const auto& leName : qAsConst(line_edits_names)){
  619. if(rx.match(leName).hasMatch()) {
  620. //DEB << "Time Match found: " << key << " - " << leName);
  621. auto line_edits = this->findChild<QLineEdit *>(leName);
  622. if(line_edits != nullptr){
  623. DEB << "Setting " << line_edits->objectName() << " to " << ATime::toString(flightEntry.getData().value(data_key).toInt(), flightTimeFormat);
  624. line_edits->setText(ATime::toString(flightEntry.getData().value(data_key).toInt(),
  625. flightTimeFormat));
  626. line_edits_names.removeOne(leName);
  627. }
  628. break;
  629. }
  630. }
  631. rx = QRegularExpression(data_key + "Name\\w+?");
  632. for(const auto& leName : qAsConst(line_edits_names)){
  633. if(rx.match(leName).hasMatch()) {
  634. auto line_edits = this->findChild<QLineEdit *>(leName);
  635. if(line_edits != nullptr){
  636. DEB << pilotsIdMap.key(1);
  637. line_edits->setText(pilotsIdMap.key(flightEntry.getData().value(data_key).toInt()));
  638. line_edits_names.removeOne(leName);
  639. }
  640. break;
  641. }
  642. }
  643. }
  644. //FunctionComboBox
  645. QList<QLineEdit*> function_combo_boxes = {ui->tPICTimeLineEdit, ui->tPICUSTimeLineEdit,
  646. ui->tSICTimeLineEdit, ui->tDUALTimeLineEdit,
  647. ui->tFITimeLineEdit};
  648. for(const auto& line_edit : function_combo_boxes){
  649. if(line_edit->text() != "00:00"){
  650. QString name = line_edit->objectName();
  651. name.chop(12);
  652. name.remove(0,1);
  653. ui->FunctionComboBox->setCurrentText(name);
  654. }
  655. }
  656. // Approach Combo Box
  657. const QString& app = flightEntry.getData().value(opl::db::FLIGHTS_APPROACHTYPE).toString();
  658. if(app != EMPTY_STRING){
  659. ui->ApproachComboBox->setCurrentText(app);
  660. }
  661. // Task and Rules
  662. qint8 PF = flightEntry.getData().value(opl::db::FLIGHTS_PILOTFLYING).toInt();
  663. if (PF > 0) {
  664. ui->PilotFlyingCheckBox->setChecked(true);
  665. } else {
  666. ui->PilotMonitoringCheckBox->setChecked(true);
  667. }
  668. qint8 FR = flightEntry.getData().value(opl::db::FLIGHTS_TIFR).toInt();
  669. if (FR > 0) {
  670. ui->IfrCheckBox->setChecked(true);
  671. } else {
  672. ui->tIFRTimeLineEdit->setText(EMPTY_STRING);
  673. ui->VfrCheckBox->setChecked(true);
  674. }
  675. // Take Off and Landing
  676. qint8 TO = flightEntry.getData().value(opl::db::FLIGHTS_TODAY).toInt()
  677. + flightEntry.getData().value(opl::db::FLIGHTS_TONIGHT).toInt();
  678. qint8 LDG = flightEntry.getData().value(opl::db::FLIGHTS_LDGDAY).toInt()
  679. + flightEntry.getData().value(opl::db::FLIGHTS_LDGNIGHT).toInt();
  680. if(TO > 0) {
  681. ui->TakeoffCheckBox->setChecked(true);
  682. ui->TakeoffSpinBox->setValue(TO);
  683. } else {
  684. ui->TakeoffCheckBox->setChecked(false);
  685. ui->TakeoffSpinBox->setValue(0);
  686. }
  687. if(LDG > 0) {
  688. ui->LandingCheckBox->setChecked(true);
  689. ui->LandingSpinBox->setValue(LDG);
  690. } else {
  691. ui->LandingCheckBox->setChecked(false);
  692. ui->LandingSpinBox->setValue(0);
  693. }
  694. qint8 AL = flightEntry.getData().value(opl::db::FLIGHTS_AUTOLAND).toInt();
  695. if(AL > 0) {
  696. ui->AutolandCheckBox->setChecked(true);
  697. ui->AutolandSpinBox->setValue(AL);
  698. }
  699. for(const auto& le : qAsConst(mandatoryLineEdits)){
  700. emit le->editingFinished();
  701. }
  702. }
  703. bool NewFlightDialog::isLessOrEqualThanBlockTime(const QString time_string)
  704. {
  705. if (mandatoryLineEditsGood.count(true) != 7){
  706. QMessageBox message_box(this);
  707. message_box.setText("Unable to determine total block time.<br>"
  708. "Please fill out all Mandatory Fields<br>"
  709. "before manually editing these times.");
  710. message_box.exec();
  711. return false;
  712. }
  713. auto extra_time = ATime::fromString(time_string, flightTimeFormat);
  714. auto block_time = ATime::blocktime(ATime::fromString(
  715. ui->tofbTimeLineEdit->text(), flightTimeFormat),
  716. ATime::fromString(
  717. ui->tonbTimeLineEdit->text(), flightTimeFormat));
  718. if (extra_time <= block_time) {
  719. return true;
  720. } else {
  721. QMessageBox message_box(this);
  722. message_box.setWindowTitle("Error");
  723. message_box.setText("The flight time you have entered is longer than the total blocktime:<br><center><b>"
  724. + ATime::toString(block_time, flightTimeFormat)
  725. + "</b></center>");
  726. message_box.exec();
  727. return false;
  728. }
  729. }
  730. /*!
  731. * \brief NewFlight::addNewTailMessageBox If the user input is not in the aircraftList, the user
  732. * is prompted if he wants to add a new entry to the database
  733. */
  734. void NewFlightDialog::addNewTail(QLineEdit *parent_line_edit)
  735. {
  736. QMessageBox::StandardButton reply;
  737. reply = QMessageBox::question(this, "No Aircraft found",
  738. "No aircraft with this registration found.<br>"
  739. "If this is the first time you log a flight with this aircraft, you have to "
  740. "add the registration to the database first.<br><br>Would you like to add a new aircraft to the database?",
  741. QMessageBox::Yes|QMessageBox::No);
  742. if (reply == QMessageBox::Yes) {
  743. DEB << "Add new aircraft selected";
  744. // create and open new aircraft dialog
  745. NewTailDialog na(ui->acftLineEdit->text(), this);
  746. na.exec();
  747. // update map and list, set line edit
  748. tailsIdMap = aDB->getIdMap(ADatabaseTarget::tails);
  749. tailsList = aDB->getCompletionList(ADatabaseTarget::registrations);
  750. DEB << "New Entry added. Id:" << aDB->getLastEntry(ADatabaseTarget::tails);
  751. DEB << "AC Map: " << tailsIdMap;
  752. parent_line_edit->setText(tailsIdMap.key(aDB->getLastEntry(ADatabaseTarget::tails)));
  753. emit parent_line_edit->editingFinished();
  754. } else {
  755. parent_line_edit->setText(EMPTY_STRING);
  756. }
  757. }
  758. /*!
  759. * \brief NewFlight::addNewPilot If the user input is not in the pilotNameList, the user
  760. * is prompted if he wants to add a new entry to the database
  761. */
  762. void NewFlightDialog::addNewPilot(QLineEdit *parent_line_edit)
  763. {
  764. QMessageBox::StandardButton reply;
  765. reply = QMessageBox::question(this, "No Pilot found",
  766. "No pilot found.<br>Please enter the Name as"
  767. "<br><br><center><b>Lastname, Firstname</b></center><br><br>"
  768. "If this is the first time you log a flight with this pilot, you have to "
  769. "add the name to the database first.<br><br>Would you like to add a new pilot to the database?",
  770. QMessageBox::Yes|QMessageBox::No);
  771. if (reply == QMessageBox::Yes) {
  772. DEB << "Add new pilot selected";
  773. // create and open new pilot dialog
  774. NewPilotDialog np(this);
  775. np.exec();
  776. // update map and list, set line edit
  777. pilotsIdMap = aDB->getIdMap(ADatabaseTarget::pilots);
  778. pilotList = aDB->getCompletionList(ADatabaseTarget::pilots);
  779. DEB << "Setting new entry: " << pilotsIdMap.key(aDB->getLastEntry(ADatabaseTarget::pilots));
  780. parent_line_edit->setText(pilotsIdMap.key(aDB->getLastEntry(ADatabaseTarget::pilots)));
  781. emit parent_line_edit->editingFinished();
  782. } else {
  783. parent_line_edit->setText(EMPTY_STRING);
  784. }
  785. }
  786. ///////////////////////////////////////////////////////////////////////////////////////////////////
  787. /// Flight Data Tab Slots ///
  788. ///////////////////////////////////////////////////////////////////////////////////////////////////
  789. void NewFlightDialog::on_cancelButton_clicked()
  790. {
  791. DEB << "Cancel Button clicked.";
  792. reject();
  793. }
  794. void NewFlightDialog::on_submitButton_clicked()
  795. {
  796. for (const auto &line_edit : qAsConst(mandatoryLineEdits)) {
  797. emit line_edit->editingFinished();
  798. }
  799. DEB << "editing finished emitted. good count: " << mandatoryLineEditsGood.count(true);
  800. if (mandatoryLineEditsGood.count(true) != 7) {
  801. QString error_message = "Not all mandatory entries are valid.<br>The following"
  802. " item(s) are empty or missing:<br><br><center><b>";
  803. for (int i=0; i < mandatoryLineEditsGood.size(); i++) {
  804. if (!mandatoryLineEditsGood[i]){
  805. error_message.append(MANDATORY_LINE_EDITS_DISPLAY_NAMES.value(i) + "<br>");
  806. mandatoryLineEdits[i]->setStyleSheet("border: 1px solid red");
  807. }
  808. }
  809. error_message.append("</b></center><br>Please go back and fill in the required data.");
  810. QMessageBox message_box(this);
  811. message_box.setText(error_message);
  812. message_box.exec();
  813. return;
  814. }
  815. DEB << "Submit Button clicked. Mandatory good (out of 7): " << mandatoryLineEditsGood.count(true);
  816. auto newData = collectInput();
  817. DEB << "Setting Data for flightEntry...";
  818. flightEntry.setData(newData);
  819. DEB << "Committing...";
  820. if (!aDB->commit(flightEntry)) {
  821. QMessageBox message_box(this);
  822. message_box.setText("The following error has ocurred:\n\n"
  823. + aDB->lastError.text()
  824. + "\n\nYour entry has not been saved.");
  825. message_box.setIcon(QMessageBox::Warning);
  826. message_box.exec();
  827. return;
  828. } else {
  829. QDialog::accept();
  830. }
  831. }
  832. /*
  833. * Shared Slots
  834. */
  835. void NewFlightDialog::onGoodInputReceived(QLineEdit *line_edit)
  836. {
  837. DEB << line_edit->objectName() << " - Good input received - " << line_edit->text();
  838. line_edit->setStyleSheet("");
  839. if (mandatoryLineEdits.contains(line_edit))
  840. mandatoryLineEditsGood.setBit(mandatoryLineEdits.indexOf(line_edit), true);
  841. if (mandatoryLineEditsGood.count(true) == 7)
  842. onMandatoryLineEditsFilled();
  843. DEB << "Mandatory good: " << mandatoryLineEditsGood.count(true)
  844. << " (out of 7) " << mandatoryLineEditsGood;
  845. }
  846. void NewFlightDialog::onBadInputReceived(QLineEdit *line_edit)
  847. {
  848. DEB << line_edit->objectName() << " - Bad input received - " << line_edit->text();
  849. line_edit->setStyleSheet("border: 1px solid red");
  850. DEB << "Mandatory Good: " << mandatoryLineEditsGood.count(true) << " out of "
  851. << mandatoryLineEditsGood.size() << ". Array: " << mandatoryLineEditsGood;
  852. }
  853. // capitalize input for dept, dest and registration input
  854. void NewFlightDialog::onToUpperTriggered_textChanged(const QString &text)
  855. {
  856. auto sender_object = sender();
  857. auto line_edit = this->findChild<QLineEdit*>(sender_object->objectName());
  858. DEB << "Text changed - " << line_edit->objectName() << line_edit->text();
  859. {
  860. const QSignalBlocker blocker(line_edit);
  861. line_edit->setText(text.toUpper());
  862. }
  863. }
  864. // update is disabled if the user chose to manually edit extra times
  865. void NewFlightDialog::onMandatoryLineEditsFilled()
  866. {
  867. if (!(mandatoryLineEditsGood.count(true) == 7)) {
  868. DEB << "erroneously called.";
  869. return;
  870. };
  871. if (updateEnabled)
  872. fillDeductibleData();
  873. DEB << mandatoryLineEditsGood;
  874. }
  875. // make sure that when using keyboard to scroll through completer sugggestions, line edit is up to date
  876. void NewFlightDialog::onCompleter_highlighted(const QString &completion)
  877. {
  878. auto sender_object = sender();
  879. auto line_edit = this->findChild<QLineEdit*>(sender_object->objectName());
  880. DEB << "Completer highlighted - " << line_edit->objectName() << completion;
  881. line_edit->setText(completion);
  882. }
  883. void NewFlightDialog::onCompleter_activated(const QString &)
  884. {
  885. auto sender_object = sender();
  886. auto line_edit = this->findChild<QLineEdit*>(sender_object->objectName());
  887. DEB << "Line edit " << line_edit->objectName() << "completer activated.";
  888. emit line_edit->editingFinished();
  889. }
  890. /*
  891. * Date of Flight
  892. */
  893. void NewFlightDialog::on_doftLineEdit_editingFinished()
  894. {
  895. auto line_edit = ui->doftLineEdit;
  896. auto text = ui->doftLineEdit->text();
  897. auto label = ui->doftDisplayLabel;
  898. DEB << line_edit->objectName() << "Editing finished - " << text;
  899. auto date = QDate::fromString(text, Qt::ISODate);
  900. if (date.isValid()) {
  901. label->setText(date.toString(Qt::TextDate));
  902. onGoodInputReceived(line_edit);
  903. return;
  904. }
  905. //try to correct input if only numbers are entered, eg 20200101
  906. if(text.length() == 8) {
  907. DEB << "Trying to fix input...";
  908. text.insert(4,'-');
  909. text.insert(7,'-');
  910. date = QDate::fromString(text, Qt::ISODate);
  911. if (date.isValid()) {
  912. line_edit->setText(date.toString(Qt::ISODate));
  913. label->setText(date.toString(Qt::TextDate));
  914. onGoodInputReceived(line_edit);
  915. return;
  916. }
  917. }
  918. label->setText("Invalid Date.");
  919. onBadInputReceived(line_edit);
  920. }
  921. void NewFlightDialog::onCalendarWidget_clicked(const QDate &date)
  922. {
  923. const auto& le = ui->doftLineEdit;
  924. le->blockSignals(false);
  925. ui->calendarWidget->hide();
  926. ui->placeLabel1->resize(ui->placeLabel2->size());
  927. le->setText(date.toString(Qt::ISODate));
  928. le->setFocus();
  929. }
  930. void NewFlightDialog::onCalendarWidget_selected(const QDate &date)
  931. {
  932. ui->calendarWidget->hide();
  933. ui->placeLabel1->resize(ui->placeLabel2->size());
  934. ui->doftDisplayLabel->setText(date.toString(Qt::TextDate));
  935. const auto& le = ui->doftLineEdit;
  936. le->setText(date.toString(Qt::ISODate));
  937. le->setFocus();
  938. le->blockSignals(false);
  939. }
  940. void NewFlightDialog::onDoftLineEdit_entered()
  941. {
  942. const auto& cw = ui->calendarWidget;
  943. const auto& le = ui->doftLineEdit;
  944. const auto& anchor = ui->placeLabel1;
  945. if(cw->isVisible()){
  946. le->blockSignals(false);
  947. cw->hide();
  948. anchor->resize(ui->placeLabel2->size());
  949. le->setFocus();
  950. } else {
  951. le->blockSignals(true);
  952. // Determine size based on layout coordinates
  953. int c1 = anchor->pos().rx();
  954. int c2 = le->pos().rx();
  955. int z = le->size().rwidth();
  956. int x = (c2 - c1)+ z;
  957. c1 = anchor->pos().ry();
  958. c2 = ui->acftLineEdit->pos().ry();
  959. z = ui->acftLineEdit->size().height();
  960. int y = (c2 - c1) + z;
  961. //Re-size calendar and parent label accordingly
  962. anchor->resize(x, y);
  963. cw->setParent(ui->placeLabel1);
  964. cw->setGeometry(QRect(0, 0, x, y));
  965. cw->show();
  966. cw->setFocus();
  967. }
  968. }
  969. void NewFlightDialog::on_calendarCheckBox_stateChanged(int arg1)
  970. {
  971. ASettings::write(ASettings::NewFlight::CalendarCheckBox, ui->calendarCheckBox->isChecked());
  972. DEB << "Calendar check box state changed.";
  973. switch (arg1) {
  974. case 0: // unchecked
  975. setPopUpCalendarEnabled(false);
  976. break;
  977. case 2: // checked
  978. setPopUpCalendarEnabled(true);
  979. break;
  980. default:
  981. break;
  982. }
  983. }
  984. /*
  985. * Location Line Edits
  986. */
  987. void NewFlightDialog::on_deptLocLineEdit_editingFinished()
  988. {
  989. onLocationEditingFinished(ui->deptLocLineEdit, ui->deptNameLabel);
  990. }
  991. void NewFlightDialog::on_destLocLineEdit_editingFinished()
  992. {
  993. onLocationEditingFinished(ui->destLocLineEdit, ui->destNameLabel);
  994. }
  995. void NewFlightDialog::onLocationEditingFinished(QLineEdit *line_edit, QLabel *name_label)
  996. {
  997. const auto &text = line_edit->text();
  998. //DEB << line_edit->objectName() << " Editing finished. " << text);
  999. int airport_id = 0;
  1000. // try to map iata or icao code to airport id;
  1001. if (text.length() == 3) {
  1002. airport_id = airportIataIdMap.value(text);
  1003. } else {
  1004. airport_id = airportIcaoIdMap.value(text);
  1005. }
  1006. // check result
  1007. if (airport_id == 0) {
  1008. // to do: prompt user how to handle unknown airport
  1009. name_label->setText("Unknown airport identifier");
  1010. onBadInputReceived(line_edit);
  1011. return;
  1012. }
  1013. line_edit->setText(airportIcaoIdMap.key(airport_id));
  1014. name_label->setText(airportNameIdMap.key(airport_id));
  1015. onGoodInputReceived(line_edit);
  1016. }
  1017. /*
  1018. * Time Line Edits
  1019. */
  1020. void NewFlightDialog::onTimeLineEdit_editingFinished()
  1021. {
  1022. auto sender_object = sender();
  1023. auto line_edit = this->findChild<QLineEdit*>(sender_object->objectName());
  1024. DEB << line_edit->objectName() << "Editing Finished -" << line_edit->text();
  1025. line_edit->setText(ATime::formatTimeInput(line_edit->text()));
  1026. const auto time = ATime::fromString(line_edit->text());
  1027. if(time.isValid()){
  1028. if(primaryTimeLineEdits.contains(line_edit)) {
  1029. onGoodInputReceived(line_edit);
  1030. } else { // is extra time line edit
  1031. if (!isLessOrEqualThanBlockTime(line_edit->text())) {
  1032. line_edit->setText(EMPTY_STRING);
  1033. line_edit->setFocus();
  1034. return;
  1035. }
  1036. }
  1037. } else {
  1038. if (!line_edit->text().isEmpty())
  1039. onBadInputReceived(line_edit);
  1040. }
  1041. }
  1042. /*
  1043. * Aircraft Line Edit
  1044. */
  1045. void NewFlightDialog::on_acftLineEdit_editingFinished()
  1046. {
  1047. auto line_edit = ui->acftLineEdit;
  1048. //DEB << line_edit->objectName() << "Editing Finished!" << line_edit->text());
  1049. if (tailsIdMap.value(line_edit->text()) != 0) {
  1050. DEB << "Mapped: " << line_edit->text() << tailsIdMap.value(line_edit->text());
  1051. auto acft = aDB->getTailEntry(tailsIdMap.value(line_edit->text()));
  1052. ui->acftTypeLabel->setText(acft.type());
  1053. onGoodInputReceived(line_edit);
  1054. return;
  1055. }
  1056. // try to fix input
  1057. if (!line_edit->completer()->currentCompletion().isEmpty()
  1058. && !line_edit->text().isEmpty()) {
  1059. DEB << "Trying to fix input...";
  1060. line_edit->setText(line_edit->completer()->currentCompletion());
  1061. emit line_edit->editingFinished();
  1062. return;
  1063. }
  1064. // to do: promp user to add new
  1065. onBadInputReceived(line_edit);
  1066. ui->acftTypeLabel->setText("Unknown Registration.");
  1067. addNewTail(line_edit);
  1068. }
  1069. /*
  1070. * Pilot Line Edits
  1071. */
  1072. void NewFlightDialog::onPilotNameLineEdit_editingFinished()
  1073. {
  1074. auto sender_object = sender();
  1075. auto line_edit = this->findChild<QLineEdit*>(sender_object->objectName());
  1076. //DEB << line_edit->objectName() << "Editing Finished -" << line_edit->text());
  1077. if(line_edit->text().contains(SELF_RX)) {
  1078. DEB << "self recognized.";
  1079. line_edit->setText(pilotsIdMap.key(1));
  1080. auto pilot = aDB->getPilotEntry(1);
  1081. ui->picCompanyLabel->setText(pilot.getData().value(opl::db::TAILS_COMPANY).toString());
  1082. onGoodInputReceived(line_edit);
  1083. return;
  1084. }
  1085. if(pilotsIdMap.value(line_edit->text()) != 0) {
  1086. DEB << "Mapped: " << line_edit->text() << pilotsIdMap.value(line_edit->text());
  1087. auto pilot = aDB->getPilotEntry(pilotsIdMap.value(line_edit->text()));
  1088. ui->picCompanyLabel->setText(pilot.getData().value(opl::db::TAILS_COMPANY).toString());
  1089. onGoodInputReceived(line_edit);
  1090. return;
  1091. }
  1092. if (line_edit->text().isEmpty()) {
  1093. return;
  1094. }
  1095. if (!line_edit->completer()->currentCompletion().isEmpty()) {
  1096. DEB << "Trying to fix input...";
  1097. line_edit->setText(line_edit->completer()->currentCompletion());
  1098. emit line_edit->editingFinished();
  1099. return;
  1100. }
  1101. // to do: prompt user to add new
  1102. onBadInputReceived(line_edit);
  1103. addNewPilot(line_edit);
  1104. }
  1105. ///////////////////////////////////////////////////////////////////////////////////////////////////
  1106. /// Auto Logging Tab Slots ///
  1107. ///////////////////////////////////////////////////////////////////////////////////////////////////
  1108. void NewFlightDialog::on_setAsDefaultButton_clicked()
  1109. {
  1110. writeSettings();
  1111. }
  1112. void NewFlightDialog::on_restoreDefaultButton_clicked()
  1113. {
  1114. readSettings();
  1115. }
  1116. void NewFlightDialog::on_PilotFlyingCheckBox_stateChanged(int)
  1117. {
  1118. DEB << "PF checkbox state changed.";
  1119. if(ui->PilotFlyingCheckBox->isChecked()){
  1120. ui->TakeoffSpinBox->setValue(1);
  1121. ui->TakeoffCheckBox->setCheckState(Qt::Checked);
  1122. ui->LandingSpinBox->setValue(1);
  1123. ui->LandingCheckBox->setCheckState(Qt::Checked);
  1124. }else if(!ui->PilotFlyingCheckBox->isChecked()){
  1125. ui->TakeoffSpinBox->setValue(0);
  1126. ui->TakeoffCheckBox->setCheckState(Qt::Unchecked);
  1127. ui->LandingSpinBox->setValue(0);
  1128. ui->LandingCheckBox->setCheckState(Qt::Unchecked);
  1129. }
  1130. }
  1131. void NewFlightDialog::on_IfrCheckBox_stateChanged(int)
  1132. {
  1133. if (mandatoryLineEditsGood.count(true) == 7 && updateEnabled)
  1134. onMandatoryLineEditsFilled();
  1135. }
  1136. void NewFlightDialog::on_manualEditingCheckBox_stateChanged(int arg1)
  1137. {
  1138. if (!(mandatoryLineEditsGood.count(true) == 7) && ui->manualEditingCheckBox->isChecked()) {
  1139. QMessageBox message_box(this);
  1140. message_box.setText("Before editing times manually, please fill out the required fields in the flight data tab,"
  1141. " so that total time can be calculated.");
  1142. message_box.exec();
  1143. ui->manualEditingCheckBox->setChecked(false);
  1144. return;
  1145. }
  1146. QList<QLineEdit*> LE = {ui->tSPSETimeLineEdit, ui->tSPMETimeLineEdit, ui->tMPTimeLineEdit, ui->tIFRTimeLineEdit,
  1147. ui->tNIGHTTimeLineEdit,ui->tPICTimeLineEdit, ui->tPICUSTimeLineEdit, ui->tSICTimeLineEdit,
  1148. ui->tDUALTimeLineEdit, ui->tFITimeLineEdit};
  1149. switch (arg1) {
  1150. case 0:
  1151. for(const auto& le : LE){
  1152. le->setFocusPolicy(Qt::NoFocus);
  1153. le->setStyleSheet("");
  1154. }
  1155. updateEnabled = true;
  1156. if (mandatoryLineEditsGood.count(true) == 7 && updateEnabled)
  1157. onMandatoryLineEditsFilled();
  1158. break;
  1159. case 2:
  1160. for(const auto& le : LE){
  1161. le->setFocusPolicy(Qt::StrongFocus);
  1162. }
  1163. updateEnabled = false;
  1164. break;
  1165. default:
  1166. break;
  1167. }
  1168. }
  1169. void NewFlightDialog::on_ApproachComboBox_currentTextChanged(const QString &arg1)
  1170. {
  1171. if(arg1 == "ILS CAT III"){ //for a CAT III approach an Autoland is mandatory, so we can preselect it.
  1172. ui->AutolandCheckBox->setCheckState(Qt::Checked);
  1173. ui->AutolandSpinBox->setValue(1);
  1174. }else{
  1175. ui->AutolandCheckBox->setCheckState(Qt::Unchecked);
  1176. ui->AutolandSpinBox->setValue(0);
  1177. }
  1178. }
  1179. void NewFlightDialog::on_FunctionComboBox_currentIndexChanged(int)
  1180. {
  1181. if (updateEnabled)
  1182. fillDeductibleData();
  1183. }