newflightdialog.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2023 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 NewFlightDialog_H
  19. #define NewFlightDialog_H
  20. #include <QDialog>
  21. #include <QDebug>
  22. #include <QLineEdit>
  23. #include <QList>
  24. #include <QBitArray>
  25. #include "src/database/flightentry.h"
  26. #include "src/gui/verification/userinput.h"
  27. #include "src/opl.h"
  28. #include "src/gui/verification/validationstate.h"
  29. namespace Ui {
  30. class NewFlightDialog;
  31. }
  32. /*!
  33. * \brief The NewFlightDialog is used to add or edit entries from the flights table in the database
  34. * \details NewFlightDialog offers two constructors, one is used to create a new flight entry from scratch, where the other one is used
  35. * to edit an existing entry. This entry is identified by its ROW ID, which is used to retreive the entry data and pre-populate the
  36. * user interface with the data from the database.
  37. *
  38. * The flights table is the core of the application's database and care needs to be taken when interfacing with it.
  39. *
  40. * To ensure only good data is written to the database, the ValidationState class is used. It contains a QBitArray with each bit representing
  41. * a mandatory data point. The array is initialized to all false and progressively changed to true as entry data is validated. An entry can
  42. * only be submitted if all the verification bits are set.
  43. *
  44. * Inputs from the user are verified with a two-step process. The first level of verification is accomplished by QRegularExpressionValidator, which limits the
  45. * user to only inputting generally acceptable data (like numbers for date or time, or characters for airport identifiers). If the input
  46. * passes this sanity check, the line edits emit the editingFinished() signal, which triggers a more granular and sophisticated set of input
  47. * verification, broadly based on cross-checking the entered data against known good values. The ACompletionData class is used to provide
  48. * QHashs of known good values from the database and their respective ROW_IDs. If user-entered data has been matched to a known good database
  49. * value, the data is considered acceptable. This means that in order to, for example, log a flight with a certain Pilot, that this pilot
  50. * already has to exist in the pilots table. If this is not the case, the user is prompted to add a new pilot (or aircraft) to the database
  51. * before proceeding. In order to make this matching process seamless for the user, the completionData also contains a set of QStringLists
  52. * for each of the database tables which are used to create QCompleters that provide pop-up completion on the respective QLineEdits.
  53. *
  54. * Once the user is satisfied with his entries, a final set of input verification is triggered and the entry is submitted to the database,
  55. * see on_buttonBox_accepted() and Database::commit()
  56. */
  57. class NewFlightDialog : public QDialog
  58. {
  59. Q_OBJECT
  60. public:
  61. /*!
  62. * \brief NewFlightDialog - Creates a NewFlightDialog that can be used to add a new flight entry to the logbook
  63. */
  64. explicit NewFlightDialog(QWidget *parent = nullptr);
  65. /*!
  66. * \brief NewFlightDialog - Creates a NewFlightDialog that can be used to edit an existing entry in the logbook
  67. * \param row_id - The database ROW ID of the entry to be edited
  68. */
  69. explicit NewFlightDialog(int row_id, QWidget* parent = nullptr);
  70. ~NewFlightDialog();
  71. private:
  72. Ui::NewFlightDialog *ui;
  73. ValidationState validationState;
  74. /*!
  75. * \brief a AFlightEntry object that is used to store either position data
  76. * from an old entry, is used to fill the form for editing an entry, or is
  77. * filled with new data for adding a new entry to the logbook.
  78. */
  79. OPL::FlightEntry flightEntry;
  80. /*!
  81. * \brief timeLineEdits - Line Edits for time Off Blocks and Time On Blocks
  82. */
  83. static const inline QList<QLineEdit*>* timeLineEdits;
  84. /*!
  85. * \brief locationLineEdits - Line Edits for Departure and Destination Airports
  86. */
  87. static const inline QList<QLineEdit*>* locationLineEdits;
  88. /*!
  89. * \brief pilotNameLineEdits - Line Edits for Pilot in Command, Second in Command (Co-Pilot) and Third Pilot
  90. */
  91. static const inline QList<QLineEdit*>* pilotNameLineEdits;
  92. /*!
  93. * \brief mandatoryLineEdits - Contains the Line Edits that are needed for logging a complete flight from A to B.
  94. * The list is ordered like the ValidationItem enum so that indexed access is possible using the enum.
  95. */
  96. static const inline QList<QLineEdit*>* mandatoryLineEdits;
  97. static const inline QLatin1String self = QLatin1String("self");
  98. void init();
  99. void setupRawInputValidation();
  100. void setupSignalsAndSlots();
  101. void readSettings();
  102. void fillWithEntryData();
  103. bool verifyUserInput(QLineEdit *line_edit, const UserInput &input);
  104. /*!
  105. * \brief onGoodInputReceived - Sets a verification bit for the line edit that has been edited.
  106. * \details When a Line Edit of the mandatoryLineEdits list is edited, on editingFinished(), the received input is
  107. * evaluated and if considered a good input, the validation bit in validationState is set.
  108. */
  109. void onGoodInputReceived(QLineEdit *line_edit);
  110. /*!
  111. * \brief onBadInputReceived Unsets a verification bit for the line edit that has been edited.
  112. * \details When a Line Edit of the mandatoryLineEdits list is edited, on editingFinished(), the received input is
  113. * evaluated and if considered a bad input, the validation bit in validationState is unset.
  114. */
  115. void onBadInputReceived(QLineEdit *line_edit);
  116. void updateBlockTimeLabel();
  117. void setNightCheckboxes();
  118. bool addNewTail(QLineEdit& parent_line_edit);
  119. bool addNewPilot(QLineEdit& parent_line_edit);
  120. bool checkPilotFunctionsValid();
  121. OPL::RowData_T prepareFlightEntryData();
  122. const static inline auto CAT_3 = QLatin1String(OPL::GLOBALS->getApproachTypes()[3].toLatin1());
  123. private slots:
  124. void toUpper(const QString& text);
  125. void onTimeLineEdit_editingFinished();
  126. void onPilotNameLineEdit_editingFinshed();
  127. void onLocationLineEdit_editingFinished();
  128. void on_acftLineEdit_editingFinished();
  129. void on_doftLineEdit_editingFinished();
  130. void on_buttonBox_accepted();
  131. void on_pilotFlyingCheckBox_stateChanged(int arg1);
  132. void on_approachComboBox_currentTextChanged(const QString &arg1);
  133. void on_functionComboBox_currentIndexChanged(int index);
  134. protected:
  135. bool eventFilter(QObject* object, QEvent* event) override;
  136. };
  137. #endif // NewFlightDialog_H