newflight.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  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 <QSqlRelationalTableModel>
  21. /// =======================================================
  22. /// Debug / WIP section
  23. /// =======================================================
  24. #define DEBUG(expr) \
  25. qDebug() << "~DEBUG" << __func__ << expr
  26. void NewFlight::on_verifyButton_clicked()//debug button
  27. {
  28. //fillExtrasLineEdits();
  29. collectBasicData();
  30. collectAdditionalData();
  31. }
  32. static const auto IATA_RX = QLatin1String("[a-zA-Z0-9]{3}");
  33. static const auto ICAO_RX = QLatin1String("[a-zA-Z0-9]{4}");
  34. static const auto NAME_RX = QLatin1String("(\\p{L}+('|\\-)?)");//(\\p{L}+(\\s|'|\\-)?\\s?(\\p{L}+)?\\s?)
  35. static const auto ADD_NAME_RX = QLatin1String("(\\s?(\\p{L}+('|\\-)?))?");
  36. static const auto SELF_RX = QLatin1String("(self|SELF)");
  37. /// Raw Input validation
  38. static const auto TIME_VALID_RGX = QRegularExpression("([01]?[0-9]|2[0-3]):?[0-5][0-9]?");// We only want to allow inputs that make sense as a time, e.g. 99:99 is not a valid time
  39. static const auto LOC_VALID_RGX = QRegularExpression(IATA_RX + "|" + ICAO_RX);
  40. static const auto AIRCRAFT_VALID_RGX = QRegularExpression("[A-Z0-9]+\\-?[A-Z0-9]+");
  41. static const auto PILOT_NAME_VALID_RGX = QRegularExpression(SELF_RX + QLatin1Char('|')
  42. + NAME_RX + ADD_NAME_RX + ADD_NAME_RX + ADD_NAME_RX + ",?\\s?" // up to 4 first names
  43. + NAME_RX + ADD_NAME_RX + ADD_NAME_RX + ADD_NAME_RX );// up to 4 last names
  44. /// Invalid characters (validators keep text even if it returns Invalid, see `onInputRejected` below)
  45. static const auto TIME_INVALID_RGX = QRegularExpression("[^0-9:]");
  46. static const auto LOC_INVALID_RGX = QRegularExpression("[^A-Z0-9]");
  47. static const auto AIRCRAFT_INVALID_RGX = QRegularExpression("[^a-zA-Z0-9\\-]");
  48. static const auto PILOT_NAME_INVALID_RGX = QRegularExpression("[^\\p{L}|\\s|,]");
  49. static const auto INVALID_CHARS_RGX = QRegularExpression("[^\\p{L}|\\s|,|\\-|'|0-9|:]");
  50. /// Sql columns
  51. static const auto LOC_SQL_COL = SqlColumnNum(1); // TODO: locations are iata/icao so 1,2 merge columns in sql?
  52. static const auto AIRCRAFT_SQL_COL = SqlColumnNum(4);
  53. static const auto PILOT_NAME_SQL_COL = SqlColumnNum(6);
  54. /*
  55. * Window Construction
  56. */
  57. //For adding a new Flight to the logbook
  58. NewFlight::NewFlight(QWidget *parent, Db::editRole edRole) :
  59. QDialog(parent),
  60. ui(new Ui::NewFlight)
  61. {
  62. ui->setupUi(this);
  63. role = edRole;
  64. doUpdate = true;
  65. setup();
  66. //set date for new object
  67. auto date = QDate::currentDate();
  68. ui->doftDateEdit->setDate(date);
  69. // Visually mark mandatory fields
  70. ui->deptLocLineEdit->setStyleSheet("border: 1px solid orange");
  71. ui->destLocLineEdit->setStyleSheet("border: 1px solid orange");
  72. ui->tofbTimeLineEdit->setStyleSheet("border: 1px solid orange");
  73. ui->tonbTimeLineEdit->setStyleSheet("border: 1px solid orange");
  74. ui->picNameLineEdit->setStyleSheet("border: 1px solid orange");
  75. ui->acftLineEdit->setStyleSheet("border: 1px solid orange");
  76. readSettings();
  77. }
  78. //For editing an existing flight
  79. NewFlight::NewFlight(QWidget *parent, Flight oldFlight, Db::editRole edRole) :
  80. QDialog(parent),
  81. ui(new Ui::NewFlight)
  82. {
  83. ui->setupUi(this);
  84. role=edRole;
  85. entry = oldFlight;
  86. doUpdate = true;
  87. setup();
  88. formFiller(oldFlight);
  89. }
  90. NewFlight::~NewFlight()
  91. {
  92. delete ui;
  93. }
  94. void NewFlight::setup(){
  95. auto db = Db::Database();
  96. const auto location_settings = \
  97. LineEditSettings(LOC_VALID_RGX, LOC_INVALID_RGX, LOC_SQL_COL);
  98. const auto aircraft_settings = \
  99. LineEditSettings(AIRCRAFT_VALID_RGX, AIRCRAFT_INVALID_RGX, AIRCRAFT_SQL_COL);
  100. const auto pilot_name_settings = \
  101. LineEditSettings(PILOT_NAME_VALID_RGX, PILOT_NAME_INVALID_RGX, PILOT_NAME_SQL_COL);
  102. const auto time_settings = \
  103. LineEditSettings(TIME_VALID_RGX, TIME_INVALID_RGX, SqlColumnNum());
  104. // Set up Line Edits with QValidators, QCompleters
  105. auto line_edits = ui->flightDataTab->findChildren<QLineEdit*>();
  106. auto le_size = line_edits.size();
  107. for(auto i = 0; i < le_size; ++i){
  108. // auto bit_array = new QBitArray(le_size);
  109. // bit_array->setBit(i, true);
  110. this->lineEditBitMap[line_edits[i]] = i;
  111. }
  112. this->allOkBits.resize(line_edits.size());
  113. this->mandatoryLineEdits = {
  114. ui->deptLocLineEdit,
  115. ui->destLocLineEdit,
  116. ui->tofbTimeLineEdit,
  117. ui->tonbTimeLineEdit,
  118. ui->picNameLineEdit,
  119. ui->acftLineEdit,
  120. };
  121. for(auto line_edit : line_edits)
  122. {
  123. auto le_name = line_edit->objectName();
  124. if(QRegularExpression("Loc").match(le_name).hasMatch()){
  125. setupLineEdit(line_edit, location_settings);
  126. }
  127. else if (QRegularExpression("Time").match(le_name).hasMatch())
  128. {
  129. setupLineEdit(line_edit, time_settings);
  130. }
  131. else if (QRegularExpression("acft").match(le_name).hasMatch())
  132. {
  133. setupLineEdit(line_edit, aircraft_settings);
  134. }
  135. else if (QRegularExpression("Name").match(le_name).hasMatch())
  136. {
  137. setupLineEdit(line_edit, pilot_name_settings);
  138. }
  139. }
  140. //fill Lists
  141. pilots = CompletionList(CompleterTarget::pilots).list;
  142. tails = CompletionList(CompleterTarget::registrations).list;
  143. airports = CompletionList(CompleterTarget::airports).list;
  144. QString statement = "SELECT iata, icao FROM airports";
  145. auto result = Db::customQuery(statement,2);
  146. for(int i=0; i<result.length()-2; i += 2){
  147. airportMap.insert(result[i],result[i+1]);
  148. }
  149. // Groups for CheckBoxes
  150. QButtonGroup *FlightRulesGroup = new QButtonGroup(this);
  151. FlightRulesGroup->addButton(ui->IfrCheckBox);
  152. FlightRulesGroup->addButton(ui->VfrCheckBox);
  153. QButtonGroup *PilotTaskGroup = new QButtonGroup(this);
  154. PilotTaskGroup->addButton(ui->PilotFlyingCheckBox);
  155. PilotTaskGroup->addButton(ui->PilotMonitoringCheckBox);
  156. ui->flightDataTabWidget->setCurrentIndex(0);
  157. ui->deptLocLineEdit->setFocus();
  158. }
  159. void NewFlight::formFiller(Flight oldFlight)
  160. {
  161. DEBUG("Filling Line Edits...");
  162. DEBUG("With Data: " << oldFlight.data);
  163. // Date
  164. ui->doftDateEdit->setDate(QDate::fromString(oldFlight.data.value("doft"), Qt::ISODate));
  165. QStringList filled;
  166. // Line Edits
  167. auto line_edits = parent()->findChildren<QLineEdit *>();
  168. QStringList line_edits_names;
  169. for(const auto& le : line_edits){
  170. line_edits_names << le->objectName();
  171. }
  172. const QString& acft = Db::singleSelect("registration", "tails", "tail_id",
  173. oldFlight.data.value("acft"),
  174. Db::exactMatch);
  175. ui->acftLineEdit->setText(acft);
  176. line_edits_names.removeOne("acftLineEdit");
  177. for(const auto& key : oldFlight.data.keys()){
  178. auto rx = QRegularExpression(key + "LineEdit");//acftLineEdit
  179. for(const auto& leName : line_edits_names){
  180. if(rx.match(leName).hasMatch()) {
  181. //DEBUG("Loc Match found: " << key << " - " << leName);
  182. auto le = parent()->findChild<QLineEdit *>(leName);
  183. if(le != nullptr){
  184. le->setText(oldFlight.data.value(key));
  185. filled << leName;
  186. line_edits_names.removeOne(leName);
  187. }
  188. break;
  189. }
  190. }
  191. rx = QRegularExpression(key + "Loc\\w+?");
  192. for(const auto& leName : line_edits_names){
  193. if(rx.match(leName).hasMatch()) {
  194. //DEBUG("Loc Match found: " << key << " - " << leName);
  195. auto le = parent()->findChild<QLineEdit *>(leName);
  196. if(le != nullptr){
  197. le->setText(oldFlight.data.value(key));
  198. filled << leName;
  199. line_edits_names.removeOne(leName);
  200. }
  201. break;
  202. }
  203. }
  204. rx = QRegularExpression(key + "Time\\w+?");
  205. for(const auto& leName : line_edits_names){
  206. if(rx.match(leName).hasMatch()) {
  207. //DEBUG("Time Match found: " << key << " - " << leName);
  208. auto le = parent()->findChild<QLineEdit *>(leName);
  209. if(le != nullptr){
  210. DEBUG("Setting " << le->objectName() << " to " << Calc::minutesToString(oldFlight.data.value(key)));
  211. le->setText(Calc::minutesToString(
  212. oldFlight.data.value(key)));
  213. filled << leName;
  214. line_edits_names.removeOne(leName);
  215. }
  216. break;
  217. }
  218. }
  219. rx = QRegularExpression(key + "Name\\w+?");
  220. for(const auto& leName : line_edits_names){
  221. if(rx.match(leName).hasMatch()) {
  222. //DEBUG("Time Match found: " << key << " - " << leName);
  223. auto le = parent()->findChild<QLineEdit *>(leName);
  224. if(le != nullptr){
  225. const QString& column = "piclastname||', '||picfirstname";
  226. const QString& name = Db::singleSelect(column, "pilots", "pilot_id",
  227. oldFlight.data.value(key),
  228. Db::exactMatch);
  229. le->setText(name);
  230. filled << leName;
  231. line_edits_names.removeOne(leName);
  232. }
  233. break;
  234. }
  235. }
  236. }
  237. //FunctionComboBox
  238. QList<QLineEdit*> FCB = {ui->tPICTimeLineEdit, ui->tPICUSTimeLineEdit, ui->tSICTimeLineEdit,
  239. ui->tDualTimeLineEdit, ui->tFITimeLineEdit};
  240. for(const auto& le : FCB){
  241. if(le->text() != "00:00"){
  242. QString name = le->objectName();
  243. name.chop(12);
  244. name.remove(0,1);
  245. ui->FunctionComboBox->setCurrentText(name);
  246. }
  247. }
  248. // Approach Combo Box
  249. const QString& app = oldFlight.data.value("ApproachType");
  250. if(app != " "){
  251. ui->ApproachComboBox->setCurrentText(app);
  252. }
  253. // Task and Rules
  254. qint8 PF = oldFlight.data.value("pilotFlying").toInt();
  255. if (PF > 0) {
  256. ui->PilotFlyingCheckBox->setChecked(true);
  257. } else {
  258. ui->PilotMonitoringCheckBox->setChecked(true);
  259. }
  260. qint8 FR = oldFlight.data.value("tIFR").toInt();
  261. if (FR > 0) {
  262. ui->IfrCheckBox->setChecked(true);
  263. } else {
  264. ui->tIFRTimeLineEdit->setText("00:00");
  265. ui->VfrCheckBox->setChecked(true);
  266. }
  267. // Take Off and Landing
  268. qint8 TO = oldFlight.data.value("toDay").toInt() + oldFlight.data.value("toNight").toInt();
  269. qint8 LDG = oldFlight.data.value("ldgDay").toInt() + oldFlight.data.value("ldgNight").toInt();
  270. DEBUG("TO and LDG:" << TO << LDG);
  271. if(TO > 0) {
  272. ui->TakeoffCheckBox->setChecked(true);
  273. ui->TakeoffSpinBox->setValue(TO);
  274. } else {
  275. ui->TakeoffCheckBox->setChecked(false);
  276. ui->TakeoffSpinBox->setValue(0);
  277. }
  278. if(LDG > 0) {
  279. ui->LandingCheckBox->setChecked(true);
  280. ui->LandingSpinBox->setValue(LDG);
  281. } else {
  282. ui->LandingCheckBox->setChecked(false);
  283. ui->LandingSpinBox->setValue(0);
  284. }
  285. qint8 AL = oldFlight.data.value("autoland").toInt();
  286. if(AL > 0) {
  287. ui->AutolandCheckBox->setChecked(true);
  288. ui->AutolandSpinBox->setValue(AL);
  289. }
  290. for(const auto& le : mandatoryLineEdits){
  291. emit le->editingFinished();
  292. }
  293. //DEBUG("Filled: ");
  294. //DEBUG(filled);
  295. //DEBUG("Unfilled: ");
  296. //DEBUG(line_edits_names);
  297. }
  298. /*
  299. * ============================================================================
  300. * Functions
  301. * ============================================================================
  302. */
  303. /*!
  304. * \brief setLineEditValidator set Validators for QLineEdits that end with Time, Loc,
  305. * Aircraft or Name
  306. */
  307. inline void NewFlight::setupLineEdit(QLineEdit* line_edit, LineEditSettings settings)
  308. {
  309. auto db = QSqlDatabase::database("qt_sql_default_connection");
  310. auto line_edit_objectName = line_edit->objectName();
  311. DEBUG("Setting validators for " << line_edit_objectName);
  312. auto [valid_rgx, invalid_rgx, sql_col] = settings.getAll();
  313. auto validator = new StrictRxValidator(valid_rgx, line_edit);
  314. auto comp_model = new QSqlRelationalTableModel(line_edit, db);
  315. comp_model->database().open();
  316. comp_model->setTable("QCompleterView");
  317. comp_model->select();
  318. auto completer = new QCompleter(comp_model, line_edit);
  319. completer->setCaseSensitivity(Qt::CaseInsensitive);
  320. completer->setCompletionMode(QCompleter::PopupCompletion);
  321. completer->setCompletionColumn(sql_col.column());
  322. completer->setFilterMode(Qt::MatchContains);
  323. /*if(QRegularExpression("\\w+Loc").match(line_edit_objectName).hasMatch()) { completer->setFilterMode(Qt::MatchContains); }
  324. if(QRegularExpression("\\w+Acft").match(line_edit_objectName).hasMatch()) { completer->setFilterMode(Qt::MatchContains); }
  325. if(QRegularExpression("\\w+Name").match(line_edit_objectName).hasMatch()) { completer->setFilterMode(Qt::MatchContains); }*/
  326. line_edit->setValidator(validator);
  327. line_edit->setCompleter(completer);
  328. }
  329. /*!
  330. * \brief NewFlight::writeSettings Writes current selection for auto-logging
  331. * to settings file.
  332. */
  333. void NewFlight::writeSettings()
  334. {
  335. DEBUG("Writing Settings...");
  336. Settings::write("NewFlight/FunctionComboBox",ui->FunctionComboBox->currentText());
  337. Settings::write("NewFlight/ApproachComboBox",ui->ApproachComboBox->currentText());
  338. Settings::write("NewFlight/PilotFlyingCheckBox",ui->PilotFlyingCheckBox->isChecked());
  339. Settings::write("NewFlight/PilotMonitoringCheckBox",ui->PilotMonitoringCheckBox->isChecked());
  340. Settings::write("NewFlight/TakeoffSpinBox",ui->TakeoffSpinBox->value());
  341. Settings::write("NewFlight/TakeoffCheckBox",ui->TakeoffCheckBox->isChecked());
  342. Settings::write("NewFlight/LandingSpinBox",ui->LandingSpinBox->value());
  343. Settings::write("NewFlight/LandingCheckBox",ui->LandingCheckBox->isChecked());
  344. Settings::write("NewFlight/AutolandSpinBox",ui->AutolandSpinBox->value());
  345. Settings::write("NewFlight/AutolandCheckBox",ui->AutolandCheckBox->isChecked());
  346. Settings::write("NewFlight/IfrCheckBox",ui->IfrCheckBox->isChecked());
  347. Settings::write("NewFlight/VfrCheckBox",ui->VfrCheckBox->isChecked());
  348. }
  349. /*!
  350. * \brief NewFlight::readSettings Retreives auto-logging settings
  351. * and sets up ui accordingly
  352. */
  353. void NewFlight::readSettings()
  354. {
  355. DEBUG("Reading Settings...");
  356. QSettings settings;
  357. ui->FunctionComboBox->setCurrentText(Settings::read("NewFlight/FunctionComboBox").toString());
  358. ui->ApproachComboBox->setCurrentText(Settings::read("NewFlight/ApproachComboBox").toString());
  359. ui->PilotFlyingCheckBox->setChecked(Settings::read("NewFlight/PilotFlyingCheckBox").toInt());
  360. ui->PilotMonitoringCheckBox->setChecked(Settings::read("NewFlight/PilotMonitoringCheckBox").toInt());
  361. ui->TakeoffSpinBox->setValue(Settings::read("NewFlight/TakeoffSpinBox").toInt());
  362. ui->TakeoffCheckBox->setChecked(Settings::read("NewFlight/TakeoffCheckBox").toInt());
  363. ui->LandingSpinBox->setValue(Settings::read("NewFlight/LandingSpinBox").toInt());
  364. ui->LandingCheckBox->setChecked(Settings::read("NewFlight/LandingCheckBox").toInt());
  365. ui->AutolandSpinBox->setValue(Settings::read("NewFlight/AutolandSpinBox").toInt());
  366. ui->AutolandCheckBox->setChecked(Settings::read("NewFlight/AutolandCheckBox").toInt());
  367. ui->IfrCheckBox->setChecked(Settings::read("NewFlight/IfrCheckBox").toInt());
  368. ui->VfrCheckBox->setChecked(Settings::read("NewFlight/VfrCheckBox").toInt());
  369. ui->FlightNumberLineEdit->setText(Settings::read("flightlogging/flightnumberPrefix").toString());
  370. if(Settings::read("NewFlight/FunctionComboBox").toString() == "PIC"){
  371. ui->picNameLineEdit->setText("self");
  372. ui->secondPilotNameLineEdit->setText("");
  373. }else if (Settings::read("NewFlight/FunctionComboBox").toString() == "Co-Pilot") {
  374. ui->picNameLineEdit->setText("");
  375. ui->secondPilotNameLineEdit->setText("self");
  376. }
  377. }
  378. /*!
  379. * \brief NewFlight::addNewPilotMessageBox If the user input is not in the pilotNameList, the user
  380. * is prompted if he wants to add a new entry to the database
  381. */
  382. void NewFlight::addNewPilotMessageBox(QLineEdit *parent)
  383. {
  384. QMessageBox::StandardButton reply;
  385. reply = QMessageBox::question(this, "No Pilot found",
  386. "No pilot found.<br>Please enter the Name as"
  387. "<br><br><center><b>Lastname, Firstname</b></center><br><br>"
  388. "If this is the first time you log a flight with this pilot, you have to "
  389. "add the name to the database first.<br><br>Would you like to add a new pilot to the database?",
  390. QMessageBox::Yes|QMessageBox::No);
  391. if (reply == QMessageBox::Yes)
  392. {
  393. qDebug() << "Add new pilot selected";
  394. // create and open new pilot dialog
  395. auto np = NewPilot(Db::createNew, this);
  396. np.exec();
  397. QString statement = "SELECT MAX(pilot_id) FROM pilots";
  398. QString id = Db::customQuery(statement,1).first();
  399. Pilot newPilot = Pilot(id.toInt());
  400. parent->setText(newPilot.data.value("displayname"));
  401. emit parent->editingFinished();
  402. }
  403. }
  404. /*!
  405. * \brief NewFlight::addNewAircraftMessageBox If the user input is not in the aircraftList, the user
  406. * is prompted if he wants to add a new entry to the database
  407. */
  408. void NewFlight::addNewAircraftMessageBox(QLineEdit *parent)
  409. {
  410. QMessageBox::StandardButton reply;
  411. reply = QMessageBox::question(this, "No Aircraft found",
  412. "No aircraft with this registration found.<br>"
  413. "If this is the first time you log a flight with this aircraft, you have to "
  414. "add the registration to the database first.<br><br>Would you like to add a new aircraft to the database?",
  415. QMessageBox::Yes|QMessageBox::No);
  416. if (reply == QMessageBox::Yes)
  417. {
  418. DEBUG("Add new aircraft selected");
  419. // create and open new aircraft dialog
  420. auto na = NewTail(ui->acftLineEdit->text(), Db::createNew, this);
  421. na.exec();
  422. QString statement = "SELECT MAX(tail_id) FROM tails";
  423. QString id = Db::customQuery(statement,1).first();
  424. auto newAcft = Aircraft(id.toInt());
  425. parent->setText(newAcft.data.value("registration"));
  426. emit parent->editingFinished();
  427. }
  428. }
  429. /// Input Verification and Collection
  430. /*!
  431. * \brief NewFlight::update check if needed inputs are available and update form
  432. */
  433. void NewFlight::update()
  434. {
  435. if(doUpdate){
  436. QVector<QLineEdit*> notFilled;
  437. for(auto lineEdit : mandatoryLineEdits)
  438. {
  439. if(!allOkBits.testBit(lineEditBitMap[lineEdit]))
  440. {
  441. notFilled.push_back(lineEdit);
  442. }
  443. }
  444. if(notFilled.empty()){
  445. collectBasicData();
  446. fillExtras();
  447. } else {
  448. DEBUG("No update - not enough variables known.");
  449. }
  450. }
  451. }
  452. /*!
  453. * \brief NewFlight::collectBasicInput After mandatory inputs have passed
  454. * validation, collect values from line edits and combo boxes to create new Data
  455. * for flight object.
  456. */
  457. void NewFlight::collectBasicData()
  458. {
  459. // purge old data to ensure database integrity
  460. newData.clear();
  461. DEBUG("Collecting Basic Input...");
  462. // Date of Flight
  463. auto date = ui->doftDateEdit->date();
  464. auto doft = date.toString(Qt::ISODate);
  465. newData.insert("doft",doft);
  466. // Departure Loc
  467. newData.insert("dept",ui->deptLocLineEdit->text());
  468. // Time Off Blocks
  469. auto timeOff = QTime::fromString(ui->tofbTimeLineEdit->text(),"hh:mm");
  470. if(timeOff.isValid()){
  471. int tofb = timeOff.hour() * 60 + timeOff.minute();
  472. newData.insert("tofb",QString::number(tofb));
  473. }
  474. // Destination Loc
  475. newData.insert("dest",ui->destLocLineEdit->text());
  476. // Time On Blocks
  477. auto timeOn = QTime::fromString(ui->tonbTimeLineEdit->text(),"hh:mm");
  478. if(timeOn.isValid()){
  479. int tonb = timeOn.hour() * 60 + timeOn.minute();
  480. newData.insert("tonb",QString::number(tonb));
  481. }
  482. //Block Time
  483. auto tofb = QTime::fromString(ui->tofbTimeLineEdit->text(),"hh:mm");
  484. auto tonb = QTime::fromString(ui->tonbTimeLineEdit->text(),"hh:mm");
  485. QString blockTime = Calc::blocktime(tofb, tonb).toString("hh:mm");
  486. QString blockMinutes = QString::number(Calc::stringToMinutes(blockTime));
  487. newData.insert("tblk",blockMinutes);
  488. // Aircraft
  489. QString reg = ui->acftLineEdit->text();
  490. QString acft = Db::singleSelect("tail_id","tails","registration",reg,Db::exactMatch);
  491. if(!acft.isEmpty()){
  492. newData.insert("acft",acft);
  493. } else {
  494. emit ui->acftLineEdit->inputRejected();
  495. }
  496. // Pilot
  497. if(ui->picNameLineEdit->text() == "self" || ui->picNameLineEdit->text() == "self"){
  498. newData.insert("pic","1");
  499. } else {
  500. QString name = ui->picNameLineEdit->text();
  501. QStringList names = name.split(',');
  502. if(names.length()==2){
  503. QString firstNames = names[1].simplified();
  504. QString lastNames = names[0].simplified();
  505. QString query = "SELECT pilot_id FROM pilots WHERE piclastname = \"" + lastNames
  506. + "\" AND picfirstname = \"" + firstNames + "\"";
  507. QVector<QString> pic = Db::customQuery(query,1);
  508. if(!pic.isEmpty()){
  509. newData.insert("pic",pic.first());
  510. }else {
  511. emit ui->picNameLineEdit->inputRejected();
  512. }
  513. }
  514. }
  515. }
  516. void NewFlight::collectAdditionalData()
  517. {
  518. // Pilot 2
  519. if(!ui->secondPilotNameLineEdit->text().isEmpty()){
  520. if(ui->secondPilotNameLineEdit->text() == "self" || ui->secondPilotNameLineEdit->text() == "self"){
  521. newData.insert("secondPilot","1");
  522. } else {
  523. QString name = ui->secondPilotNameLineEdit->text();
  524. QStringList names = name.split(',');
  525. if(names.length()==2){
  526. QString firstNames = names[1].simplified();
  527. QString lastNames = names[0].simplified();
  528. QString query = "SELECT pilot_id FROM pilots WHERE piclastname = \"" + lastNames
  529. + "\" AND picfirstname = \"" + firstNames + "\"";
  530. QVector<QString> pic = Db::customQuery(query,1);
  531. if(!pic.isEmpty()){
  532. newData.insert("secondPilot",pic.first());
  533. }else {
  534. emit ui->secondPilotNameLineEdit->inputRejected();
  535. }
  536. }
  537. }
  538. }
  539. // Pilot 3
  540. if(!ui->thirdPilotNameLineEdit->text().isEmpty()){
  541. if(ui->thirdPilotNameLineEdit->text() == "self" || ui->thirdPilotNameLineEdit->text() == "self"){
  542. newData.insert("thirdPilot","1");
  543. } else {
  544. QString name = ui->thirdPilotNameLineEdit->text();
  545. QStringList names = name.split(',');
  546. if(names.length()==2){
  547. QString firstNames = names[1].simplified();
  548. QString lastNames = names[0].simplified();
  549. QString query = "SELECT pilot_id FROM pilots WHERE piclastname = \"" + lastNames
  550. + "\" AND picfirstname = \"" + firstNames + "\"";
  551. QVector<QString> pic = Db::customQuery(query,1);
  552. if(!pic.isEmpty()){
  553. newData.insert("thirdPilot",pic.first());
  554. }else {
  555. emit ui->thirdPilotNameLineEdit->inputRejected();
  556. }
  557. }
  558. }
  559. }
  560. // Extra Times
  561. auto tofb = QTime::fromString(ui->tofbTimeLineEdit->text(),"hh:mm");
  562. auto tonb = QTime::fromString(ui->tonbTimeLineEdit->text(),"hh:mm");
  563. QString blockTime = Calc::blocktime(tofb, tonb).toString("hh:mm");
  564. QString blockMinutes = QString::number(Calc::stringToMinutes(blockTime));
  565. auto acft = Aircraft(newData.value("acft").toInt());
  566. if(!acft.data.isEmpty()){// valid aircraft
  567. // SP SE
  568. if(acft.data.value("singlepilot") == "1" && acft.data.value("singleengine") == "1"){
  569. newData.insert("tSPSE", blockMinutes);
  570. newData.insert("tSPME", "");
  571. newData.insert("tMP", "");
  572. }
  573. // SP ME
  574. if(acft.data.value("singlepilot") == "1" && acft.data.value("multiengine") == "1"){
  575. newData.insert("tSPSE", "");
  576. newData.insert("tSPME", blockMinutes);
  577. newData.insert("tMP", "");
  578. }
  579. // MP
  580. if(acft.data.value("multipilot") == "1"){
  581. newData.insert("tSPSE", "");
  582. newData.insert("tSPME", "");
  583. newData.insert("tMP", blockMinutes);
  584. }
  585. }else{DEBUG("Aircraft Details Empty");}//invalid aircraft
  586. // IFR
  587. if(ui->IfrCheckBox->isChecked()){
  588. newData.insert("tIFR",blockMinutes);
  589. } else {
  590. newData.insert("tIFR","");
  591. }
  592. // Night
  593. QString deptDate = ui->doftDateEdit->date().toString(Qt::ISODate) + 'T' + tofb.toString("hh:mm");
  594. QDateTime deptDateTime = QDateTime::fromString(deptDate,"yyyy-MM-ddThh:mm");
  595. int tblk = blockMinutes.toInt();
  596. const int nightAngle = Settings::read("flightlogging/nightangle").toInt();
  597. QString nightTime = QString::number(
  598. Calc::calculateNightTime(
  599. newData.value("dept"), newData.value("dest"),
  600. deptDateTime, tblk, nightAngle));
  601. newData.insert("tNIGHT", nightTime);
  602. // Function times
  603. switch (ui->FunctionComboBox->currentIndex()) {
  604. case 0://PIC
  605. newData.insert("tPIC", blockMinutes);
  606. newData.insert("tPICUS", "");
  607. newData.insert("tSIC", "");
  608. newData.insert("tDual", "");
  609. newData.insert("tFI", "");
  610. break;
  611. case 1://PICUS
  612. newData.insert("tPIC", "");
  613. newData.insert("tPICUS", blockMinutes);
  614. newData.insert("tSIC", "");
  615. newData.insert("tDual", "");
  616. newData.insert("tFI", "");
  617. break;
  618. case 2://Co-Pilot
  619. newData.insert("tPIC", "");
  620. newData.insert("tPICUS", "");
  621. newData.insert("tSIC", blockMinutes);
  622. newData.insert("tDual", "");
  623. newData.insert("tFI", "");
  624. break;
  625. case 3://Dual
  626. newData.insert("tPIC", "");
  627. newData.insert("tPICUS", "");
  628. newData.insert("tSIC", "");
  629. newData.insert("tDual", blockMinutes);
  630. newData.insert("tFI", "");
  631. break;
  632. case 4://Instructor
  633. newData.insert("tPIC", "");
  634. newData.insert("tPICUS", "");
  635. newData.insert("tSIC", "");
  636. newData.insert("tDual", "");
  637. newData.insert("tFI", blockMinutes);
  638. }
  639. // Pilot Flying
  640. newData.insert("pilotFlying", QString::number(ui->PilotFlyingCheckBox->isChecked()));
  641. // TO and LDG
  642. if(ui->TakeoffCheckBox->isChecked()) {
  643. if(nightTime == "0"){ // all day
  644. newData.insert("toDay", QString::number(ui->TakeoffSpinBox->value()));
  645. newData.insert("toNight", "0");
  646. }else if (nightTime == blockTime) { // all night
  647. newData.insert("toDay", "0");
  648. newData.insert("toNight", QString::number(ui->TakeoffSpinBox->value()));
  649. } else { //check
  650. if(Calc::isNight(ui->deptLocLineEdit->text(), deptDateTime, nightAngle)){
  651. newData.insert("toDay", "0");
  652. newData.insert("toNight", QString::number(ui->TakeoffSpinBox->value()));
  653. }else{
  654. newData.insert("toDay", QString::number(ui->TakeoffSpinBox->value()));
  655. newData.insert("toNight", "0");
  656. }
  657. }
  658. } else {
  659. newData.insert("toDay", "0");
  660. newData.insert("toNight", "0");
  661. }
  662. if(ui->LandingCheckBox->isChecked()) {
  663. if(nightTime == "0"){ // all day
  664. newData.insert("ldgDay", QString::number(ui->LandingSpinBox->value()));
  665. newData.insert("ldgNight", "0");
  666. }else if (nightTime == blockTime) { // all night
  667. newData.insert("ldgDay", "0");
  668. newData.insert("ldgNight", QString::number(ui->LandingSpinBox->value()));
  669. } else { //check
  670. QString destDate = ui->doftDateEdit->date().toString(Qt::ISODate) + 'T' + tonb.toString("hh:mm");
  671. QDateTime destDateTime = QDateTime::fromString(destDate,"yyyy-MM-ddThh:mm");
  672. if(Calc::isNight(ui->destLocLineEdit->text(), destDateTime, nightAngle)){
  673. newData.insert("ldgDay", "0");
  674. newData.insert("ldgNight", QString::number(ui->LandingSpinBox->value()));
  675. }else{
  676. newData.insert("ldgDay", QString::number(ui->LandingSpinBox->value()));
  677. newData.insert("ldgNight", "0");
  678. }
  679. }
  680. } else {
  681. newData.insert("ldgDay", "0");
  682. newData.insert("ldgNight", "0");
  683. }
  684. newData.insert("autoland", QString::number(ui->AutolandSpinBox->value()));
  685. newData.insert("ApproachType", ui->ApproachComboBox->currentText());
  686. newData.insert("FlightNumber", ui->FlightNumberLineEdit->text());
  687. newData.insert("Remarks", ui->RemarksLineEdit->text());
  688. }
  689. /*!
  690. * \brief NewFlight::fillExtrasLineEdits Fills the deductable items in the newData map and
  691. * additional flight time line edits according to ui selections.
  692. * Neccessary prerequisites are valid Date, Departure Time and Place, Destination Time and Place,
  693. * PIC name (pilot_id) and Aircraft (tail_id)
  694. */
  695. void NewFlight::fillExtras()
  696. {
  697. //zero labels and line edits
  698. QList<QLineEdit*> LE = {ui->tSPSETimeLineEdit, ui->tSPMETimeLineEdit, ui->tMPTimeLineEdit, ui->tIFRTimeLineEdit,
  699. ui->tNIGHTTimeLineEdit,ui->tPICTimeLineEdit, ui->tPICUSTimeLineEdit, ui->tSICTimeLineEdit,
  700. ui->tDualTimeLineEdit, ui->tFITimeLineEdit,};
  701. QList<QLabel*> LB = {ui->tSPSELabel, ui->tSPMELabel, ui->tMPLabel, ui->tIFRLabel, ui->tNIGHTLabel,
  702. ui->tPICLabel, ui->tPICUSLabel, ui->tSICLabel, ui->tDualLabel, ui->tFILabel};
  703. for(const auto& widget : LE) {widget->setText("");}
  704. for(const auto& widget : LB) {widget->setText("00:00");}
  705. //Times
  706. auto tofb = QTime::fromString(ui->tofbTimeLineEdit->text(),"hh:mm");
  707. auto tonb = QTime::fromString(ui->tonbTimeLineEdit->text(),"hh:mm");
  708. QString blockTime = Calc::blocktime(tofb, tonb).toString("hh:mm");
  709. QString blockMinutes = QString::number(Calc::stringToMinutes(blockTime));
  710. ui->tblkTimeLineEdit->setText(blockTime);
  711. auto acft = Aircraft(newData.value("acft").toInt());
  712. if(!acft.data.isEmpty()){// valid aircraft
  713. // SP SE
  714. if(acft.data.value("singlepilot") == "1" && acft.data.value("singleengine") == "1"){
  715. ui->tSPSETimeLineEdit->setText(blockTime);
  716. ui->tSPSELabel->setText(blockTime);
  717. }
  718. // SP ME
  719. if(acft.data.value("singlepilot") == "1" && acft.data.value("multiengine") == "1"){
  720. ui->tSPMETimeLineEdit->setText(blockTime);
  721. ui->tSPMELabel->setText(blockTime);
  722. }
  723. // MP
  724. if(acft.data.value("multipilot") == "1"){
  725. ui->tMPTimeLineEdit->setText(blockTime);
  726. ui->tMPLabel->setText(blockTime);
  727. }
  728. }else{DEBUG("Aircraft Details Empty");}//invalid aircraft
  729. // TOTAL
  730. ui->tblkLabel->setText("<b>" + blockTime + "</b>");
  731. ui->tblkLabel->setStyleSheet("color: green;");
  732. // IFR
  733. if(ui->IfrCheckBox->isChecked()){
  734. ui->tIFRTimeLineEdit->setText(blockTime);
  735. ui->tIFRLabel->setText(blockTime);
  736. }
  737. // Night
  738. QString deptDate = ui->doftDateEdit->date().toString(Qt::ISODate) + 'T' + tofb.toString("hh:mm");
  739. QDateTime deptDateTime = QDateTime::fromString(deptDate,"yyyy-MM-ddThh:mm");
  740. int tblk = blockMinutes.toInt();
  741. const int nightAngle = Settings::read("flightlogging/nightangle").toInt();
  742. QString nightTime = QString::number(
  743. Calc::calculateNightTime(
  744. newData.value("dept"), newData.value("dest"),
  745. deptDateTime, tblk, nightAngle));
  746. ui->tNIGHTTimeLineEdit->setText(Calc::minutesToString(nightTime));
  747. ui->tNIGHTLabel->setText(Calc::minutesToString(nightTime));
  748. // Function times
  749. switch (ui->FunctionComboBox->currentIndex()) {
  750. case 0://PIC
  751. ui->tPICTimeLineEdit->setText(blockTime);
  752. ui->tPICLabel->setText(blockTime);
  753. break;
  754. case 1://PICus
  755. ui->tPICUSTimeLineEdit->setText(blockTime);
  756. ui->tPICUSLabel->setText(blockTime);
  757. break;
  758. case 2://Co-Pilot
  759. ui->tSICTimeLineEdit->setText(blockTime);
  760. ui->tSICLabel->setText(blockTime);
  761. break;
  762. case 3://Dual
  763. ui->tDualTimeLineEdit->setText(blockTime);
  764. ui->tDualLabel->setText(blockTime);
  765. break;
  766. case 4://Instructor
  767. ui->tFITimeLineEdit->setText(blockTime);
  768. ui->tFILabel->setText(blockTime);
  769. }
  770. }
  771. bool NewFlight::verifyInput()
  772. {
  773. QVector<QLineEdit*> notFilled;
  774. for(auto lineEdit : mandatoryLineEdits)
  775. {
  776. if(!allOkBits.testBit(lineEditBitMap[lineEdit]))
  777. {
  778. notFilled.push_back(lineEdit);
  779. }
  780. }
  781. if(!notFilled.empty()){
  782. QMessageBox notFilledMessageBox(this);
  783. auto errorMsg = QString("Not all required fields are filled out.\n\n"
  784. "Plase make sure the following are not empty:\n\n"
  785. "Departure\nDestination\nTime Off Blocks\nTime On Blocks\n"
  786. "Pilot in Command\nAircraft Registration\n\n");
  787. notFilledMessageBox.setText(errorMsg);
  788. notFilledMessageBox.exec();
  789. return false;
  790. } else {
  791. QStringList lineEditText;
  792. for(auto line_edit : mandatoryLineEdits)
  793. {
  794. lineEditText.push_back(line_edit->text());
  795. }
  796. this->result = lineEditText;
  797. emit mandatoryFieldsValid(this);
  798. return true;
  799. }
  800. }
  801. /*!
  802. * ============================================================================
  803. * ============================================================================
  804. * Slots
  805. * ============================================================================
  806. * ============================================================================
  807. */
  808. void NewFlight::on_buttonBox_accepted()
  809. {
  810. DEBUG("OK pressed");
  811. if(verifyInput()){
  812. DEBUG("Input verified");
  813. collectBasicData();
  814. collectAdditionalData();
  815. switch (role) {
  816. case Db::editExisting:
  817. entry.setData(newData);
  818. DEBUG("Editing entry: " << entry.position.first << " - " << entry.position.second);
  819. DEBUG("with Data: " << newData);
  820. DEBUG("Function Times: " << newData.value("tPIC") << newData.value("tPICus") << newData.value("tSIC")
  821. << newData.value("tDual") << newData.value("tFI"));
  822. break;
  823. case Db::createNew:
  824. entry = Flight(newData);
  825. DEBUG("Creating New entry: " << entry.position.first << " - " << entry.position.second);
  826. DEBUG("with Data: " << newData);
  827. break;
  828. }
  829. if(entry.commit()){
  830. accept();
  831. }else{
  832. auto mb = new QMessageBox(this);
  833. auto errorMsg = QString("Unable to commit Flight to Logbook."
  834. "The following error has ocurred:\n\n");
  835. errorMsg.append(entry.error);
  836. mb->setText(errorMsg);
  837. mb->show();
  838. }
  839. }
  840. }
  841. void NewFlight::on_buttonBox_rejected()
  842. {
  843. DEBUG("CANCEL pressed." << newData);
  844. reject();
  845. }
  846. /*!
  847. * \brief onInputRejected Set `line_edit`'s border to red and check if `rgx` matches
  848. * in order to keep text on line. Ensures corresponding LineEdit bit is 0. Only
  849. * valid characters are kept on the line edit.
  850. */
  851. void NewFlight::onInputRejected(QLineEdit* line_edit, QRegularExpression rgx){
  852. //DEBUG("Input rejected" << line_edit->text());
  853. line_edit->setStyleSheet("border: 1px solid red");
  854. this->allOkBits.setBit(this->lineEditBitMap[line_edit], false);
  855. auto text = line_edit->text();
  856. if(!rgx.match(text).hasMatch())
  857. {
  858. line_edit->setText(text);
  859. }
  860. if(INVALID_CHARS_RGX.match(text).hasMatch()){//remove globaly inacceptable chars
  861. text.chop(1);
  862. line_edit->setText(text);
  863. }
  864. }
  865. /*!
  866. * \brief onEditingFinishedCleanup resets styles and sets the corresponding bit to 1
  867. */
  868. void NewFlight::onEditingFinishedCleanup(QLineEdit* line_edit)
  869. {
  870. //DEBUG("Input accepted" << line_edit << line_edit->text());
  871. line_edit->setStyleSheet("");
  872. this->allOkBits.setBit(this->lineEditBitMap[line_edit], true);
  873. }
  874. QStringList* NewFlight::getResult() { return &this->result; }
  875. void NewFlight::on_deptTZ_currentTextChanged(const QString &arg1)
  876. {
  877. DEBUG(arg1);
  878. // currently only UTC time logging is supported
  879. ui->deptTZ->setCurrentIndex(0);
  880. }
  881. void NewFlight::on_destTZ_currentIndexChanged(const QString &arg1)
  882. {
  883. DEBUG(arg1);
  884. // currently only UTC time logging is supported
  885. ui->destTZ->setCurrentIndex(0);
  886. }
  887. /// Departure
  888. void NewFlight::on_deptLocLineEdit_inputRejected()
  889. {
  890. //DEBUG("SENDER --->" << sender());
  891. ui->deptLocLineEdit->setText(ui->deptLocLineEdit->text().toUpper());
  892. onInputRejected(ui->deptLocLineEdit, QRegularExpression(LOC_INVALID_RGX));
  893. }
  894. void NewFlight::on_deptLocLineEdit_textEdited(const QString &arg1)
  895. {
  896. ui->deptLocLineEdit->setText(arg1.toUpper());
  897. }
  898. void NewFlight::on_deptLocLineEdit_editingFinished()
  899. {
  900. //DEBUG(sender()->objectName() << "EDITING FINISHED");
  901. auto line_edit = ui->deptLocLineEdit;
  902. auto text = ui->deptLocLineEdit->text();
  903. // check if iata exists, replace with icao code if it does.
  904. if(text.length() == 3){
  905. text = airportMap.value(text);
  906. }
  907. // Check if 4-letter code is in locationList
  908. if(text.length() == 4 && airports.indexOf(text) == -1){
  909. //DEBUG("Airport not found.");
  910. emit line_edit->inputRejected();
  911. return;
  912. }else{
  913. //DEBUG("Departure accepted: " << text);
  914. line_edit->setText(text);
  915. onEditingFinishedCleanup(line_edit); //reset style sheet
  916. ui->deptNameLabel->setText(Db::singleSelect("name","airports","icao",text,Db::exactMatch));
  917. update();
  918. }
  919. }
  920. void NewFlight::on_tofbTimeLineEdit_inputRejected()
  921. {
  922. onInputRejected(ui->tofbTimeLineEdit, QRegularExpression(TIME_INVALID_RGX));
  923. }
  924. void NewFlight::on_tofbTimeLineEdit_editingFinished()
  925. {
  926. ui->tofbTimeLineEdit->setText(Calc::formatTimeInput(ui->tofbTimeLineEdit->text()));
  927. const auto time = QTime::fromString(ui->tofbTimeLineEdit->text(),"hh:mm");
  928. auto line_edit = ui->tofbTimeLineEdit;
  929. onEditingFinishedCleanup(line_edit);
  930. if(time.isValid()){
  931. int minutes = time.hour() * 60 + time.minute();
  932. QString tofb = QString::number(minutes);
  933. //DEBUG("Time Off Blocks accepted: " << tofb << " minutes - " << Calc::minutesToString(tofb));
  934. }else{
  935. emit line_edit->inputRejected();
  936. }
  937. onEditingFinishedCleanup(line_edit);
  938. update();
  939. }
  940. /// Destination
  941. void NewFlight::on_destLocLineEdit_inputRejected()
  942. {
  943. ui->destLocLineEdit->setText(ui->destLocLineEdit->text().toUpper());
  944. onInputRejected(ui->destLocLineEdit, QRegularExpression(LOC_INVALID_RGX));
  945. }
  946. void NewFlight::on_destLocLineEdit_textEdited(const QString &arg1)
  947. {
  948. ui->destLocLineEdit->setText(arg1.toUpper());
  949. }
  950. void NewFlight::on_destLocLineEdit_editingFinished()
  951. {
  952. //DEBUG(sender()->objectName() << "EDITING FINISHED");
  953. auto line_edit = ui->destLocLineEdit;
  954. auto text = ui->destLocLineEdit->text();
  955. // check if iata exists, replace with icao code if it does.
  956. if(text.length() == 3){
  957. text = airportMap.value(text);
  958. }
  959. // Check if 4-letter code is in locationList
  960. if(text.length() == 4 && airports.indexOf(text) == -1){
  961. //DEBUG("Airport not found.");
  962. emit line_edit->inputRejected();
  963. return;
  964. }else{
  965. //DEBUG("Destination accepted: " << text);
  966. line_edit->setText(text);
  967. onEditingFinishedCleanup(line_edit); //reset style sheet
  968. ui->destNameLabel->setText(Db::singleSelect("name","airports","icao",text,Db::exactMatch));
  969. update();
  970. }
  971. }
  972. void NewFlight::on_tonbTimeLineEdit_inputRejected()
  973. {
  974. onInputRejected(ui->tonbTimeLineEdit, QRegularExpression(TIME_INVALID_RGX));
  975. }
  976. void NewFlight::on_tonbTimeLineEdit_editingFinished()
  977. {
  978. ui->tonbTimeLineEdit->setText(Calc::formatTimeInput(ui->tonbTimeLineEdit->text()));
  979. auto line_edit = ui->tonbTimeLineEdit;
  980. const auto time = QTime::fromString(ui->tonbTimeLineEdit->text(),"hh:mm");
  981. if(time.isValid()){
  982. int minutes = time.hour() * 60 + time.minute();
  983. QString tonb = QString::number(minutes);
  984. newData.insert("tonb",tonb);
  985. //DEBUG("Time On Blocks accepted: " << tonb << " minutes - " << Calc::minutesToString(tonb));
  986. }else{
  987. emit line_edit->inputRejected();
  988. }
  989. onEditingFinishedCleanup(line_edit);
  990. update();
  991. }
  992. /// Date
  993. void NewFlight::on_doftTimeEdit_editingFinished()
  994. {
  995. update();
  996. }
  997. /// Aircraft
  998. void NewFlight::on_acftLineEdit_inputRejected()
  999. {
  1000. ui->acftLineEdit->setText(ui->acftLineEdit->text().toUpper());
  1001. onInputRejected(ui->acftLineEdit, QRegularExpression(AIRCRAFT_INVALID_RGX));
  1002. }
  1003. void NewFlight::on_acftLineEdit_editingFinished()
  1004. {
  1005. auto registrationList = CompletionList(CompleterTarget::registrations).list;
  1006. auto line_edit = ui->acftLineEdit;
  1007. auto text = ui->acftLineEdit->text();
  1008. QStringList match = registrationList.filter(line_edit->text(), Qt::CaseInsensitive);
  1009. //DEBUG("aircraft accepted: " << match);
  1010. if(match.length() != 0) {
  1011. text = match[0];
  1012. line_edit->setText(text.toUpper());
  1013. onEditingFinishedCleanup(line_edit);
  1014. QString column = "make||' '||model||'-'||variant";
  1015. ui->acftTypeLabel->setText(
  1016. Db::singleSelect(column,"tails","registration",text,Db::exactMatch));
  1017. update();
  1018. }else{
  1019. emit line_edit->inputRejected();
  1020. addNewAircraftMessageBox(line_edit);
  1021. }
  1022. }
  1023. /// Pilot(s)
  1024. void NewFlight::on_picNameLineEdit_inputRejected()
  1025. {
  1026. onInputRejected(ui->picNameLineEdit, QRegularExpression(PILOT_NAME_INVALID_RGX));
  1027. }
  1028. void NewFlight::on_picNameLineEdit_editingFinished()
  1029. {
  1030. auto line_edit = ui->picNameLineEdit;
  1031. auto text = line_edit->text();
  1032. if(text == "self" || text == "SELF") // Logbook owner is PIC
  1033. {
  1034. //DEBUG("Pilot selected: " << text);
  1035. onEditingFinishedCleanup(line_edit);
  1036. ui->FunctionComboBox->setCurrentIndex(0);
  1037. update();
  1038. return;
  1039. }else //check if entry is in pilotList
  1040. {
  1041. QStringList pilotList = CompletionList(CompleterTarget::pilots).list;
  1042. QStringList match = pilotList.filter(line_edit->text().remove(" "), Qt::CaseInsensitive);
  1043. if(match.length()!= 0)
  1044. {
  1045. QString pic = match[0];
  1046. line_edit->setText(pic.insert(pic.indexOf(',')+1," "));
  1047. //DEBUG("Pilot selected: " << pic);
  1048. onEditingFinishedCleanup(line_edit);
  1049. update();
  1050. }else
  1051. {
  1052. DEBUG("Pilot not found.");
  1053. emit line_edit->inputRejected();
  1054. addNewPilotMessageBox(line_edit);
  1055. }
  1056. }
  1057. }
  1058. /*
  1059. * ============================================================================
  1060. * The above entris are mandatory for logging a flight,
  1061. * the rest of the entries are either optional or can
  1062. * be determined from the entries already made.
  1063. * ============================================================================
  1064. */
  1065. void NewFlight::on_secondPilotNameLineEdit_inputRejected()
  1066. {
  1067. onInputRejected(ui->secondPilotNameLineEdit, QRegularExpression(PILOT_NAME_INVALID_RGX));
  1068. }
  1069. void NewFlight::on_secondPilotNameLineEdit_editingFinished()
  1070. {
  1071. auto line_edit = ui->secondPilotNameLineEdit;
  1072. auto text = line_edit->text();
  1073. if(text == "self" || text == "SELF") // Logbook owner is Pilot
  1074. {
  1075. //DEBUG("Pilot selected: " << text);
  1076. onEditingFinishedCleanup(line_edit);
  1077. ui->FunctionComboBox->setCurrentIndex(1);
  1078. return;
  1079. }else //check if entry is in pilotList
  1080. {
  1081. QStringList pilotList = CompletionList(CompleterTarget::pilots).list;
  1082. QStringList match = pilotList.filter(line_edit->text().remove(" "), Qt::CaseInsensitive);
  1083. if(match.length()!= 0)
  1084. {
  1085. QString pic = match[0];
  1086. line_edit->setText(pic.insert(pic.indexOf(',')+1," "));
  1087. //DEBUG("Pilot selected: " << pic);
  1088. onEditingFinishedCleanup(line_edit);
  1089. }else
  1090. {
  1091. DEBUG("Pilot not found.");
  1092. emit line_edit->inputRejected();
  1093. addNewPilotMessageBox(line_edit);
  1094. }
  1095. }
  1096. }
  1097. void NewFlight::on_thirdPilotNameLineEdit_inputRejected()
  1098. {
  1099. onInputRejected(ui->thirdPilotNameLineEdit, QRegularExpression(PILOT_NAME_INVALID_RGX));
  1100. }
  1101. void NewFlight::on_thirdPilotNameLineEdit_editingFinished()
  1102. {
  1103. auto line_edit = ui->thirdPilotNameLineEdit;
  1104. auto text = line_edit->text();
  1105. if(text == "self" || text == "SELF") // Logbook owner is Pilot
  1106. {
  1107. //DEBUG("Pilot selected: " << text);
  1108. onEditingFinishedCleanup(line_edit);
  1109. return;
  1110. }else //check if entry is in pilotList
  1111. {
  1112. QStringList pilotList = CompletionList(CompleterTarget::pilots).list;
  1113. QStringList match = pilotList.filter(line_edit->text().remove(" "), Qt::CaseInsensitive);
  1114. if(match.length()!= 0)
  1115. {
  1116. QString pic = match[0];
  1117. line_edit->setText(pic.insert(pic.indexOf(',')+1," "));
  1118. //DEBUG("Pilot selected: " << pic);
  1119. onEditingFinishedCleanup(line_edit);
  1120. }else
  1121. {
  1122. DEBUG("Pilot not found.");
  1123. emit line_edit->inputRejected();
  1124. addNewPilotMessageBox(line_edit);
  1125. }
  1126. }
  1127. }
  1128. void NewFlight::on_FlightNumberLineEdit_textChanged(const QString &arg1)
  1129. {
  1130. ui->FlightNumberLineEdit->setText(arg1.toUpper());
  1131. }
  1132. /*
  1133. * ============================================================================
  1134. * Extras Tab - These are for user convenience. From many of
  1135. * these selections, determinations can be made on how to log
  1136. * details, so that the user does not have to enter each item
  1137. * manually. See also fillExtrasLineEdits()
  1138. * ============================================================================
  1139. */
  1140. void NewFlight::on_setAsDefaultButton_clicked()
  1141. {
  1142. writeSettings();
  1143. }
  1144. void NewFlight::on_restoreDefaultButton_clicked()
  1145. {
  1146. readSettings();
  1147. }
  1148. /*!
  1149. * \brief On a given flight, time can either be logged as Pilot Flying (PF) or
  1150. * Pilot Monitoring (PM). Cases where controls are changed during the flight
  1151. * are rare and can be logged by manually editing the extras.
  1152. */
  1153. void NewFlight::on_PilotFlyingCheckBox_stateChanged(int)
  1154. {
  1155. DEBUG("PF checkbox state changed.");
  1156. if(ui->PilotFlyingCheckBox->isChecked()){
  1157. ui->TakeoffSpinBox->setValue(1);
  1158. ui->TakeoffCheckBox->setCheckState(Qt::Checked);
  1159. ui->LandingSpinBox->setValue(1);
  1160. ui->LandingCheckBox->setCheckState(Qt::Checked);
  1161. }else if(!ui->PilotFlyingCheckBox->isChecked()){
  1162. ui->TakeoffSpinBox->setValue(0);
  1163. ui->TakeoffCheckBox->setCheckState(Qt::Unchecked);
  1164. ui->LandingSpinBox->setValue(0);
  1165. ui->LandingCheckBox->setCheckState(Qt::Unchecked);
  1166. }
  1167. }
  1168. void NewFlight::on_IfrCheckBox_stateChanged()
  1169. {
  1170. update();
  1171. }
  1172. void NewFlight::on_ApproachComboBox_currentTextChanged(const QString &arg1)
  1173. {
  1174. if(arg1 == "ILS CAT III"){ //for a CAT III approach an Autoland is mandatory, so we can preselect it.
  1175. ui->AutolandCheckBox->setCheckState(Qt::Checked);
  1176. ui->AutolandSpinBox->setValue(1);
  1177. }else{
  1178. ui->AutolandCheckBox->setCheckState(Qt::Unchecked);
  1179. ui->AutolandSpinBox->setValue(0);
  1180. }
  1181. }
  1182. /*
  1183. * Extra Times - These line edits should be filled out automatically,
  1184. * based on the ui selections and the user provided input. However,
  1185. * manual adjustments are possible to cater for situations where for
  1186. * example one portion of the flight is logged under different rules
  1187. * than the rest of it.
  1188. *
  1189. * For example,
  1190. * if we know the aircraft details we can determine how to log these times.
  1191. * Some times are mutually exclusive, others can be combined.
  1192. *
  1193. * For example,
  1194. * for a commercial Passenger flight, the commander can log all time as
  1195. * Total Time and PIC time. If the aircraft is a multi-engine jet he can
  1196. * also log Multi-Pilot time, and if he is an instructor, instructor time.
  1197. *
  1198. * It is not possible, however to log flight time as VFR or IFR time
  1199. * simultaneously, as a flight at any given point in time can only follow
  1200. * one set of rules. It is possible, to change flight rules and log the first
  1201. * x minutes as VFR and the rest of it as IFR, for example. Hence the need
  1202. * for the possibility to edit these times manually.
  1203. *
  1204. * The most complex time to determine is night time, see documentation of
  1205. * the calc class for details.
  1206. *
  1207. * In General, the idea is to automatically fill as much as possible, but
  1208. * if the user decides to change these times, accept the inputs, as long as
  1209. * they are generally valid. We cannot cater for all possibilities, so as long
  1210. * as the time the user has put in is a valid time <= Total Time, it can be
  1211. * accepted to the database.
  1212. */
  1213. inline bool NewFlight::isLessOrEqualToTotalTime(QString timeString)
  1214. {
  1215. if(newData.value("tblk").isEmpty()){
  1216. DEBUG("Total Time not set.");
  1217. auto mb = new QMessageBox(this);
  1218. mb->setText("Please fill out Departure and Arrival Time\n"
  1219. "before manually editing these times.");
  1220. mb->show();
  1221. return false;
  1222. } else {
  1223. int minutes = Calc::stringToMinutes(timeString);
  1224. if (minutes <= newData.value("tblk").toInt()) {
  1225. return true;
  1226. } else {
  1227. auto mb = new QMessageBox(this);
  1228. mb->setText("Cannot be more than Total Time of Flight:<br><br><center><b>"
  1229. + Calc::minutesToString(newData.value("tblk"))
  1230. + "</b></center><br>");
  1231. mb->show();
  1232. return false;
  1233. }
  1234. }
  1235. }
  1236. void NewFlight::on_tblkTimeLineEdit_editingFinished()
  1237. {
  1238. const auto &le = ui->tblkTimeLineEdit;
  1239. le->setText(Calc::formatTimeInput(le->text()));
  1240. const auto &text = le->text();
  1241. newData.insert("tblk",QString::number(Calc::stringToMinutes(text)));
  1242. le->setText(QString());
  1243. }
  1244. void NewFlight::on_tSPSETimeLineEdit_editingFinished()
  1245. {
  1246. const auto &le = ui->tSPSETimeLineEdit;
  1247. le->setText(Calc::formatTimeInput(le->text()));
  1248. const auto &text = le->text();
  1249. if(isLessOrEqualToTotalTime(text)){
  1250. newData.insert("tSPSE",QString::number(Calc::stringToMinutes(text)));
  1251. } else {
  1252. le->setText(QString());
  1253. }
  1254. }
  1255. void NewFlight::on_tSPMETimeLineEdit_editingFinished()
  1256. {
  1257. const auto &le = ui->tSPMETimeLineEdit;
  1258. le->setText(Calc::formatTimeInput(le->text()));
  1259. const auto &text = le->text();
  1260. if(isLessOrEqualToTotalTime(text)){
  1261. newData.insert("tSPME",QString::number(Calc::stringToMinutes(text)));
  1262. } else {
  1263. le->setText(QString());
  1264. }
  1265. }
  1266. void NewFlight::on_tMPTimeLineEdit_editingFinished()
  1267. {
  1268. const auto &le = ui->tMPTimeLineEdit;
  1269. le->setText(Calc::formatTimeInput(le->text()));
  1270. const auto &text = le->text();
  1271. if(isLessOrEqualToTotalTime(text)){
  1272. newData.insert("tMP",QString::number(Calc::stringToMinutes(text)));
  1273. } else {
  1274. le->setText(QString());
  1275. }
  1276. }
  1277. void NewFlight::on_tIFRTimeLineEdit_editingFinished()
  1278. {
  1279. const auto &le = ui->tIFRTimeLineEdit;
  1280. le->setText(Calc::formatTimeInput(le->text()));
  1281. const auto &text = le->text();
  1282. if(isLessOrEqualToTotalTime(text)){
  1283. newData.insert("tIFR",QString::number(Calc::stringToMinutes(text)));
  1284. } else {
  1285. le->setText(QString());
  1286. }
  1287. }
  1288. void NewFlight::on_tNIGHTTimeLineEdit_editingFinished()
  1289. {
  1290. const auto &le = ui->tNIGHTTimeLineEdit;
  1291. le->setText(Calc::formatTimeInput(le->text()));
  1292. const auto &text = le->text();
  1293. if(isLessOrEqualToTotalTime(text)){
  1294. newData.insert("tNIGHT",QString::number(Calc::stringToMinutes(text)));
  1295. } else {
  1296. le->setText(QString());
  1297. }
  1298. }
  1299. void NewFlight::on_tPICTimeLineEdit_editingFinished()
  1300. {
  1301. const auto &le = ui->tPICTimeLineEdit;
  1302. le->setText(Calc::formatTimeInput(le->text()));
  1303. const auto &text = le->text();
  1304. if(isLessOrEqualToTotalTime(text)){
  1305. newData.insert("tPIC",QString::number(Calc::stringToMinutes(text)));
  1306. } else {
  1307. le->setText(QString());
  1308. }
  1309. }
  1310. void NewFlight::on_tSICTimeLineEdit_editingFinished()
  1311. {
  1312. const auto &le = ui->tSICTimeLineEdit;
  1313. le->setText(Calc::formatTimeInput(le->text()));
  1314. const auto &text = le->text();
  1315. if(isLessOrEqualToTotalTime(text)){
  1316. newData.insert("tSIC",QString::number(Calc::stringToMinutes(text)));
  1317. } else {
  1318. le->setText(QString());
  1319. }
  1320. }
  1321. void NewFlight::on_tDualTimeLineEdit_editingFinished()
  1322. {
  1323. const auto &le = ui->tDualTimeLineEdit;
  1324. le->setText(Calc::formatTimeInput(le->text()));
  1325. const auto &text = le->text();
  1326. if(isLessOrEqualToTotalTime(text)){
  1327. newData.insert("tDual",QString::number(Calc::stringToMinutes(text)));
  1328. } else {
  1329. le->setText(QString());
  1330. }
  1331. }
  1332. void NewFlight::on_tFITimeLineEdit_editingFinished()
  1333. {
  1334. const auto &le = ui->tFITimeLineEdit;
  1335. le->setText(Calc::formatTimeInput(le->text()));
  1336. const auto &text = le->text();
  1337. if(isLessOrEqualToTotalTime(text)){
  1338. newData.insert("tFI",QString::number(Calc::stringToMinutes(text)));
  1339. } else {
  1340. le->setText(QString());
  1341. }
  1342. }
  1343. void NewFlight::on_manualEditingCheckBox_stateChanged(int arg1)
  1344. {
  1345. QList<QLineEdit*> LE = {ui->tSPSETimeLineEdit, ui->tSPMETimeLineEdit, ui->tMPTimeLineEdit, ui->tIFRTimeLineEdit,
  1346. ui->tNIGHTTimeLineEdit,ui->tPICTimeLineEdit, ui->tPICUSTimeLineEdit, ui->tSICTimeLineEdit,
  1347. ui->tDualTimeLineEdit, ui->tFITimeLineEdit,};
  1348. switch (arg1) {
  1349. case 0:
  1350. for(const auto& le : LE){
  1351. le->setFocusPolicy(Qt::NoFocus);
  1352. doUpdate = true;
  1353. update();
  1354. }
  1355. break;
  1356. case 2:
  1357. for(const auto& le : LE){
  1358. le->setFocusPolicy(Qt::StrongFocus);
  1359. doUpdate = false;
  1360. }
  1361. break;
  1362. default:
  1363. break;
  1364. }
  1365. }
  1366. void NewFlight::on_FunctionComboBox_currentTextChanged()
  1367. {
  1368. DEBUG("Current Index:" << ui->FunctionComboBox->currentIndex());
  1369. update();
  1370. }
  1371. void NewFlight::on_TakeoffSpinBox_valueChanged(int arg1)
  1372. {
  1373. if(arg1 > 0) {
  1374. ui->TakeoffCheckBox->setChecked(true);
  1375. }
  1376. }
  1377. void NewFlight::on_LandingSpinBox_valueChanged(int arg1)
  1378. {
  1379. if(arg1 > 0) {
  1380. ui->LandingCheckBox->setChecked(true);
  1381. }
  1382. }
  1383. void NewFlight::on_AutolandSpinBox_valueChanged(int arg1)
  1384. {
  1385. if(arg1 > 0) {
  1386. ui->AutolandCheckBox->setChecked(true);
  1387. }
  1388. }
  1389. void NewFlight::on_TakeoffCheckBox_stateChanged(int arg1)
  1390. {
  1391. if(arg1 == 0) {
  1392. ui->TakeoffSpinBox->setValue(0);
  1393. } else if ( arg1 == 2) {
  1394. ui->TakeoffSpinBox->setValue(1);
  1395. }
  1396. }
  1397. void NewFlight::on_LandingCheckBox_stateChanged(int arg1)
  1398. {
  1399. if(arg1 == 0) {
  1400. ui->LandingSpinBox->setValue(0);
  1401. } else if ( arg1 == 2) {
  1402. ui->LandingSpinBox->setValue(1);
  1403. }
  1404. }
  1405. void NewFlight::on_AutolandCheckBox_stateChanged(int arg1)
  1406. {
  1407. if(arg1 == 0) {
  1408. ui->AutolandSpinBox->setValue(0);
  1409. } else if ( arg1 == 2) {
  1410. ui->AutolandSpinBox->setValue(1);
  1411. }
  1412. }