newflightdialog.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #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 <QTabWidget>
  31. #include <QKeyEvent>
  32. #include "src/functions/atime.h"
  33. #include "src/classes/aflightentry.h"
  34. #include "src/classes/apilotentry.h"
  35. #include "src/classes/atailentry.h"
  36. namespace Ui {
  37. class NewFlight;
  38. }
  39. class NewFlightDialog : public QDialog
  40. {
  41. Q_OBJECT
  42. public:
  43. /*!
  44. * \brief NewFlightDialog create a new flight and add it to the logbook.
  45. */
  46. explicit NewFlightDialog(QWidget *parent = nullptr);
  47. /*!
  48. * \brief NewFlightDialog Edit an existing logbook entry.
  49. */
  50. explicit NewFlightDialog(int row_id, QWidget *parent = nullptr);
  51. ~NewFlightDialog();
  52. private slots:
  53. void onToUpperTriggered_textChanged(const QString&);
  54. void onPilotNameLineEdit_editingFinished();
  55. void onLocationEditingFinished(QLineEdit*, QLabel*);
  56. void onTimeLineEdit_editingFinished();
  57. void onCompleter_highlighted(const QString&);
  58. void onCompleter_activated(const QString &);
  59. void onCalendarWidget_clicked(const QDate &date);
  60. void onCalendarWidget_selected(const QDate &date);
  61. void onDoftLineEdit_entered();
  62. void on_calendarCheckBox_stateChanged(int arg1);
  63. void on_doftLineEdit_editingFinished();
  64. void on_cancelButton_clicked();
  65. void on_submitButton_clicked();
  66. void on_setAsDefaultButton_clicked();
  67. void on_restoreDefaultButton_clicked();
  68. void on_PilotFlyingCheckBox_stateChanged(int arg1);
  69. void on_IfrCheckBox_stateChanged(int);
  70. void on_manualEditingCheckBox_stateChanged(int arg1);
  71. void on_ApproachComboBox_currentTextChanged(const QString &arg1);
  72. void on_FunctionComboBox_currentIndexChanged(int index);
  73. void on_deptLocLineEdit_editingFinished();
  74. void on_destLocLineEdit_editingFinished();
  75. void on_acftLineEdit_editingFinished();
  76. private:
  77. Ui::NewFlight *ui;
  78. /*!
  79. * \brief a AFlightEntry object that is used to store either position data
  80. * from an old entry, is used to fill the form for editing an entry, or is
  81. * filled with new data for adding a new entry to the logbook.
  82. */
  83. AFlightEntry flightEntry;
  84. // [G]: Initial refactoring based on previous use.
  85. /*!
  86. * \brief Wrapper around Vector of mandatory line edits and their corresponding
  87. * "ok" QBitArray.
  88. */
  89. struct MandatoryLineEdits {
  90. QVector<QLineEdit*> lineEdits;
  91. QBitArray lineEditsOk;
  92. MandatoryLineEdits() = default;
  93. MandatoryLineEdits(std::initializer_list<QLineEdit*> il);
  94. void operator= (std::initializer_list<QLineEdit*> il);
  95. bool contains(QLineEdit* le);
  96. void validate(QLineEdit* le);
  97. void unvalidate(QLineEdit* le);
  98. int countOk();
  99. int size();
  100. bool okAt(int idx);
  101. bool allOk();
  102. QLineEdit* operator[] (int idx);
  103. } mandatoryLineEdits;
  104. QList<QLineEdit*> primaryTimeLineEdits;
  105. QList<QLineEdit*> pilotsLineEdits;
  106. /*!
  107. * To be used by the QCompleters
  108. */
  109. QStringList pilotList;
  110. QStringList tailsList;
  111. QStringList airportList;
  112. /*!
  113. * \brief Used to map user input to database keys
  114. */
  115. QMap<QString, int> pilotsIdMap;
  116. QMap<QString, int> tailsIdMap;
  117. QMap<QString, int> airportIcaoIdMap;
  118. QMap<QString, int> airportIataIdMap;
  119. QMap<QString, int> airportNameIdMap;
  120. Opl::Time::FlightTimeFormat flightTimeFormat;
  121. /*!
  122. * \brief If the user elects to manually edit function times, automatic updating
  123. * is disabled.
  124. */
  125. bool updateEnabled;
  126. void setup();
  127. void readSettings();
  128. void writeSettings();
  129. void setupButtonGroups();
  130. void setPopUpCalendarEnabled(bool state);
  131. void setupRawInputValidation();
  132. void setupSignalsAndSlots();
  133. void formFiller();
  134. void fillDeductibleData();
  135. void onMandatoryLineEditsFilled();
  136. void onGoodInputReceived(QLineEdit*);
  137. void onBadInputReceived(QLineEdit *);
  138. bool eventFilter(QObject *object, QEvent *event);
  139. bool isLessOrEqualThanBlockTime(const QString time_string);
  140. void addNewTail(QLineEdit*);
  141. void addNewPilot(QLineEdit *);
  142. RowData collectInput();
  143. /*!
  144. * \brief converts a time string as used in the UI to an integer of minutes for
  145. * use in the database based on the format in use in the Dialog
  146. */
  147. inline int stringToMinutes(const QString &time_string, Opl::Time::FlightTimeFormat format)
  148. {
  149. return ATime::toMinutes(ATime::fromString(time_string, format));
  150. }
  151. /*!
  152. * \brief minutesToString converts an integer of minutes as received from the database
  153. * to a String to be displayed in the UI, based on the format in use in the Dialog.
  154. */
  155. inline QString minutesToString(const int minutes, Opl::Time::FlightTimeFormat format)
  156. {
  157. return ATime::toString(ATime::fromMinutes(minutes), format);
  158. }
  159. };
  160. #endif // NEWFLIGHT_H