oldnewflightdialog.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2022 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. #ifndef NEWFLIGHT_H
  19. #define NEWFLIGHT_H
  20. #include <QDialog>
  21. #include <QRegularExpression>
  22. #include <QMessageBox>
  23. #include <QDebug>
  24. #include <QCompleter>
  25. #include <QStringList>
  26. #include <QButtonGroup>
  27. #include <QBitArray>
  28. #include <QLineEdit>
  29. #include <QCalendarWidget>
  30. #include <QComboBox>
  31. #include <QTabWidget>
  32. #include <QKeyEvent>
  33. #include "src/functions/atime.h"
  34. #include "src/classes/aflightentry.h"
  35. #include "src/classes/apilotentry.h"
  36. #include "src/classes/atailentry.h"
  37. #include "src/database/database.h"
  38. #include "src/classes/acompletiondata.h"
  39. namespace Ui {
  40. class NewFlight;
  41. }
  42. /*!
  43. * \brief The NewFlightDialog enables the user to add a new flight or edit an existing one.
  44. * \details
  45. * - Most line edits have validators and completers.
  46. * - Validators are based on regular expressions, serving as raw input validation
  47. * - The Completers are based off the database and provide auto-completion
  48. * - mandatory line edits only emit editing finished if their content has passed
  49. * raw input validation or focus is lost.
  50. * - Editing finished triggers validating inputs by mapping them to Database values
  51. * where required and results in either pass or fail.
  52. * - A QBitArray is mainained containing the state of validity of the mandatory line edits
  53. * - The deducted entries are automatically filled if the necessary mandatory entries
  54. * are valid.
  55. * - Comitting an entry to the database is only allowed if all mandatory inputs are valid.
  56. */
  57. class NewFlightDialog : public QDialog
  58. {
  59. Q_OBJECT
  60. public:
  61. /*!
  62. * \brief NewFlightDialog create a new flight and add it to the logbook.
  63. */
  64. explicit NewFlightDialog(ACompletionData &completion_data, QWidget *parent = nullptr);
  65. /*!
  66. * \brief NewFlightDialog Edit an existing logbook entry.
  67. */
  68. explicit NewFlightDialog(ACompletionData &completion_data, int row_id, QWidget *parent = nullptr);
  69. ~NewFlightDialog();
  70. /*!
  71. * \brief The ValidationSetupData struct encapsulates the items required to initialise
  72. * the line edits with QValidators and QCompleters
  73. */
  74. struct ValidationSetupData
  75. {
  76. ValidationSetupData(QStringList* completion_data, const QRegularExpression* validation_RegEx)
  77. : completionData(completion_data), validationRegEx(validation_RegEx){};
  78. ValidationSetupData(const QRegularExpression* validation_RegEx)
  79. : completionData(nullptr), validationRegEx(validation_RegEx){};
  80. const QStringList* completionData;
  81. const QRegularExpression* validationRegEx;
  82. };
  83. private slots:
  84. void onToUpperTriggered_textChanged(const QString&);
  85. void onPilotNameLineEdit_editingFinished();
  86. void onLocationEditingFinished(QLineEdit*, QLabel*);
  87. void onTimeLineEdit_editingFinished();
  88. void onCompleter_highlighted(const QString&);
  89. void onCompleter_activated(const QString &);
  90. void onCalendarWidget_clicked(const QDate &date);
  91. void on_doftLineEdit_editingFinished();
  92. void on_cancelButton_clicked();
  93. void on_submitButton_clicked();
  94. void on_setAsDefaultButton_clicked();
  95. void on_restoreDefaultButton_clicked();
  96. void on_PilotFlyingCheckBox_stateChanged(int arg1);
  97. void on_IfrCheckBox_stateChanged(int);
  98. void on_manualEditingCheckBox_stateChanged(int arg1);
  99. void on_ApproachComboBox_currentTextChanged(const QString &arg1);
  100. void on_FunctionComboBox_currentIndexChanged(int index);
  101. void on_deptLocLineEdit_editingFinished();
  102. void on_destLocLineEdit_editingFinished();
  103. void on_acftLineEdit_editingFinished();
  104. void on_deptTZComboBox_currentIndexChanged(int index);
  105. void on_destTZComboBox_currentIndexChanged(int index);
  106. void on_calendarPushButton_clicked();
  107. private:
  108. Ui::NewFlight *ui;
  109. /*!
  110. * \brief a AFlightEntry object that is used to store either position data
  111. * from an old entry, is used to fill the form for editing an entry, or is
  112. * filled with new data for adding a new entry to the logbook.
  113. */
  114. AFlightEntry flightEntry;
  115. QVector<QLineEdit*> mandatoryLineEdits;
  116. QVector<QLineEdit*> primaryTimeLineEdits;
  117. QVector<QLineEdit*> pilotsLineEdits;
  118. /*!
  119. * \brief mandatoryLineEditsValid holds the minimum required information to create a
  120. * valid database entries.
  121. */
  122. QBitArray mandatoryLineEditsValid;
  123. enum mandatoryLineEdit {
  124. doft = 0,
  125. dept = 1,
  126. dest = 2,
  127. tofb = 3,
  128. tonb = 4,
  129. pic = 5,
  130. acft = 6
  131. };
  132. void validateMandatoryLineEdit(mandatoryLineEdit line_edit){mandatoryLineEditsValid.setBit(line_edit, true);}
  133. void invalidateMandatoryLineEdit(mandatoryLineEdit line_edit){mandatoryLineEditsValid.setBit(line_edit, false);}
  134. bool timeLineEditsValid(){return mandatoryLineEditsValid[mandatoryLineEdit::tofb]
  135. && mandatoryLineEditsValid[mandatoryLineEdit::tonb];}
  136. bool acftLineEditValid(){return mandatoryLineEditsValid[mandatoryLineEdit::acft];}
  137. bool locLineEditsValid(){return mandatoryLineEditsValid[mandatoryLineEdit::dept]
  138. && mandatoryLineEditsValid[mandatoryLineEdit::dest];}
  139. bool allMandatoryLineEditsValid(){return mandatoryLineEditsValid.count(true) == 7;}
  140. //debug
  141. void validationStatus();
  142. /*!
  143. * Contains completion data for QCompleters and mapping user input
  144. */
  145. ACompletionData completionData;
  146. Opl::Time::FlightTimeFormat flightTimeFormat;
  147. /*!
  148. * \brief If the user elects to manually edit function times, automatic updating
  149. * is disabled.
  150. */
  151. bool updateEnabled;
  152. void setup();
  153. void readSettings();
  154. void setupUi();
  155. void writeSettings();
  156. void setupButtonGroups();
  157. void setupRawInputValidation();
  158. void setupSignalsAndSlots();
  159. void formFiller();
  160. void fillDeductibleData();
  161. void onMandatoryLineEditsFilled();
  162. void onGoodInputReceived(QLineEdit*);
  163. void onBadInputReceived(QLineEdit *);
  164. bool eventFilter(QObject *object, QEvent *event);
  165. bool isLessOrEqualThanBlockTime(const QString time_string);
  166. void addNewTail(QLineEdit*);
  167. void addNewPilot(QLineEdit *);
  168. /*!
  169. * \brief Collects user input from the line edits and processes it to be ready
  170. * for database submission.
  171. */
  172. RowData_T collectInput();
  173. /*!
  174. * \brief converts a time string as used in the UI to an integer of minutes for
  175. * use in the database based on the format in use in the Dialog
  176. */
  177. inline int stringToMinutes(const QString &time_string, Opl::Time::FlightTimeFormat format)
  178. {
  179. return ATime::toMinutes(ATime::fromString(time_string, format));
  180. }
  181. /*!
  182. * \brief minutesToString converts an integer of minutes as received from the database
  183. * to a String to be displayed in the UI, based on the format in use in the Dialog.
  184. */
  185. inline QString minutesToString(const int minutes, Opl::Time::FlightTimeFormat format)
  186. {
  187. return ATime::toString(ATime::qTimefromMinutes(minutes), format);
  188. }
  189. };
  190. #endif // NEWFLIGHT_H