newflightdialog.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2021 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/adatabase.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. private slots:
  71. void onToUpperTriggered_textChanged(const QString&);
  72. void onPilotNameLineEdit_editingFinished();
  73. void onLocationEditingFinished(QLineEdit*, QLabel*);
  74. void onTimeLineEdit_editingFinished();
  75. void onCompleter_highlighted(const QString&);
  76. void onCompleter_activated(const QString &);
  77. void onCalendarWidget_clicked(const QDate &date);
  78. void on_doftLineEdit_editingFinished();
  79. void on_cancelButton_clicked();
  80. void on_submitButton_clicked();
  81. void on_setAsDefaultButton_clicked();
  82. void on_restoreDefaultButton_clicked();
  83. void on_PilotFlyingCheckBox_stateChanged(int arg1);
  84. void on_IfrCheckBox_stateChanged(int);
  85. void on_manualEditingCheckBox_stateChanged(int arg1);
  86. void on_ApproachComboBox_currentTextChanged(const QString &arg1);
  87. void on_FunctionComboBox_currentIndexChanged(int index);
  88. void on_deptLocLineEdit_editingFinished();
  89. void on_destLocLineEdit_editingFinished();
  90. void on_acftLineEdit_editingFinished();
  91. void on_deptTZComboBox_currentIndexChanged(int index);
  92. void on_destTZComboBox_currentIndexChanged(int index);
  93. void on_calendarPushButton_clicked();
  94. private:
  95. Ui::NewFlight *ui;
  96. /*!
  97. * \brief a AFlightEntry object that is used to store either position data
  98. * from an old entry, is used to fill the form for editing an entry, or is
  99. * filled with new data for adding a new entry to the logbook.
  100. */
  101. AFlightEntry flightEntry;
  102. QVector<QLineEdit*> mandatoryLineEdits;
  103. QVector<QLineEdit*> primaryTimeLineEdits;
  104. QVector<QLineEdit*> pilotsLineEdits;
  105. /*!
  106. * \brief mandatoryLineEditsValid holds the minimum required information to create a
  107. * valid database entries.
  108. */
  109. QBitArray mandatoryLineEditsValid;
  110. enum mandatoryLineEdit {
  111. doft = 0,
  112. dept = 1,
  113. dest = 2,
  114. tofb = 3,
  115. tonb = 4,
  116. pic = 5,
  117. acft = 6
  118. };
  119. void validateMandatoryLineEdit(mandatoryLineEdit line_edit){mandatoryLineEditsValid.setBit(line_edit, true);}
  120. void invalidateMandatoryLineEdit(mandatoryLineEdit line_edit){mandatoryLineEditsValid.setBit(line_edit, false);}
  121. bool timeLineEditsValid(){return mandatoryLineEditsValid[mandatoryLineEdit::tofb]
  122. && mandatoryLineEditsValid[mandatoryLineEdit::tonb];}
  123. bool acftLineEditValid(){return mandatoryLineEditsValid[mandatoryLineEdit::acft];}
  124. bool locLineEditsValid(){return mandatoryLineEditsValid[mandatoryLineEdit::dept]
  125. && mandatoryLineEditsValid[mandatoryLineEdit::dest];}
  126. bool allMandatoryLineEditsValid(){return mandatoryLineEditsValid.count(true) == 7;}
  127. //debug
  128. void validationStatus();
  129. /*!
  130. * Contains completion data for QCompleters and mapping user input
  131. */
  132. ACompletionData completionData;
  133. Opl::Time::FlightTimeFormat flightTimeFormat;
  134. /*!
  135. * \brief If the user elects to manually edit function times, automatic updating
  136. * is disabled.
  137. */
  138. bool updateEnabled;
  139. void setup();
  140. void readSettings();
  141. void writeSettings();
  142. void setupButtonGroups();
  143. void setupRawInputValidation();
  144. void setupSignalsAndSlots();
  145. void formFiller();
  146. void fillDeductibleData();
  147. void onMandatoryLineEditsFilled();
  148. void onGoodInputReceived(QLineEdit*);
  149. void onBadInputReceived(QLineEdit *);
  150. bool eventFilter(QObject *object, QEvent *event);
  151. bool isLessOrEqualThanBlockTime(const QString time_string);
  152. void addNewTail(QLineEdit*);
  153. void addNewPilot(QLineEdit *);
  154. /*!
  155. * \brief Collects user input from the line edits and processes it to be ready
  156. * for database submission.
  157. */
  158. RowData_T collectInput();
  159. /*!
  160. * \brief converts a time string as used in the UI to an integer of minutes for
  161. * use in the database based on the format in use in the Dialog
  162. */
  163. inline int stringToMinutes(const QString &time_string, Opl::Time::FlightTimeFormat format)
  164. {
  165. return ATime::toMinutes(ATime::fromString(time_string, format));
  166. }
  167. /*!
  168. * \brief minutesToString converts an integer of minutes as received from the database
  169. * to a String to be displayed in the UI, based on the format in use in the Dialog.
  170. */
  171. inline QString minutesToString(const int minutes, Opl::Time::FlightTimeFormat format)
  172. {
  173. return ATime::toString(ATime::fromMinutes(minutes), format);
  174. }
  175. };
  176. #endif // NEWFLIGHT_H