openPilotLog
newflightdialog.h
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 
21 #include <QDialog>
22 #include <QRegularExpression>
23 #include <QMessageBox>
24 #include <QDebug>
25 #include <QCompleter>
26 #include <QStringList>
27 #include <QButtonGroup>
28 #include <QBitArray>
29 #include <QLineEdit>
30 #include <QCalendarWidget>
31 #include <QComboBox>
32 #include <QTabWidget>
33 #include <QKeyEvent>
34 #include "src/functions/atime.h"
35 
36 #include "src/classes/aflightentry.h"
37 #include "src/classes/apilotentry.h"
38 #include "src/classes/atailentry.h"
39 #include "src/database/adatabase.h"
40 
41 namespace Ui {
42 class NewFlight;
43 }
44 
45 class NewFlightDialog : public QDialog
46 {
47  Q_OBJECT
48 public:
52  explicit NewFlightDialog(QWidget *parent = nullptr);
56  explicit NewFlightDialog(int row_id, QWidget *parent = nullptr);
57  ~NewFlightDialog();
58 
59 private slots:
60 
61  void onToUpperTriggered_textChanged(const QString&);
62  void onPilotNameLineEdit_editingFinished();
63  void onLocationEditingFinished(QLineEdit*, QLabel*);
64  void onTimeLineEdit_editingFinished();
65  void onCompleter_highlighted(const QString&);
66  void onCompleter_activated(const QString &);
67  void onCalendarWidget_clicked(const QDate &date);
68  void on_doftLineEdit_editingFinished();
69  void on_cancelButton_clicked();
70  void on_submitButton_clicked();
71  void on_setAsDefaultButton_clicked();
72  void on_restoreDefaultButton_clicked();
73  void on_PilotFlyingCheckBox_stateChanged(int arg1);
74  void on_IfrCheckBox_stateChanged(int);
75  void on_manualEditingCheckBox_stateChanged(int arg1);
76  void on_ApproachComboBox_currentTextChanged(const QString &arg1);
77  void on_FunctionComboBox_currentIndexChanged(int index);
78  void on_deptLocLineEdit_editingFinished();
79  void on_destLocLineEdit_editingFinished();
80  void on_acftLineEdit_editingFinished();
81  void on_deptTZComboBox_currentIndexChanged(int index);
82  void on_destTZComboBox_currentIndexChanged(int index);
83 
84  void on_calendarPushButton_clicked();
85 
86 private:
87  Ui::NewFlight *ui;
88 
94  AFlightEntry flightEntry;
95 
96  // [G]: Initial refactoring based on previous use.
101  struct MandatoryLineEdits {
102  QVector<QLineEdit*> lineEdits;
103  QBitArray lineEditsValid;
104 
105  MandatoryLineEdits() = default;
106  MandatoryLineEdits(std::initializer_list<QLineEdit*> init_list);
107  void operator= (std::initializer_list<QLineEdit*> init_list);
108 
109  bool contains(QLineEdit* line_edit);
110  void validate(QLineEdit* line_edit);
111  void unvalidate(QLineEdit* line_edit);
112  int countValid();
113  int size();
114  bool validAt(int idx);
115  bool allValid();
116  QLineEdit* operator[] (int idx);
117 
118  } mandatoryLineEdits;
119 
120  QVector<QLineEdit*> primaryTimeLineEdits;
121  QVector<QLineEdit*> pilotsLineEdits;
122 
126  QStringList pilotList;
127  QStringList tailsList;
128  QStringList airportList;
129 
133  QMap<PilotName_T, PilotRowId_T> pilotsIdMap;
134  QMap<TailRegistration_T, TailId_T> tailsIdMap;
135  QMap<AirportICAO_T, AirportId_T> airportIcaoIdMap;
136  QMap<AirportIATA_T, AirportId_T> airportIataIdMap;
137  QMap<AirportName_T, AirportId_T> airportNameIdMap;
138 
139  Opl::Time::FlightTimeFormat flightTimeFormat;
140 
145  bool updateEnabled;
146 
147  void setup();
148  void readSettings();
149  void writeSettings();
150  void setupButtonGroups();
151  void setupRawInputValidation();
152  void setupSignalsAndSlots();
153  void formFiller();
154  void fillDeductibleData();
155 
156  void onMandatoryLineEditsFilled();
157  void onGoodInputReceived(QLineEdit*);
158  void onBadInputReceived(QLineEdit *);
159  bool eventFilter(QObject *object, QEvent *event);
160  bool isLessOrEqualThanBlockTime(const QString time_string);
161 
162  void addNewTail(QLineEdit*);
163  void addNewPilot(QLineEdit *);
164 
165  RowData_T collectInput();
166 
171  inline int stringToMinutes(const QString &time_string, Opl::Time::FlightTimeFormat format)
172  {
173  return ATime::toMinutes(ATime::fromString(time_string, format));
174  }
175 
180  inline QString minutesToString(const int minutes, Opl::Time::FlightTimeFormat format)
181  {
182  return ATime::toString(ATime::fromMinutes(minutes), format);
183  }
184 };
185 
186 
187 #endif // NEWFLIGHT_H
AFlightEntry
Definition: aflightentry.h:23
NewFlightDialog
Definition: newflightdialog.h:46
NewFlightDialog::NewFlightDialog
NewFlightDialog(QWidget *parent=nullptr)
NewFlightDialog create a new flight and add it to the logbook.
Definition: newflightdialog.cpp:133