Browse Source

Added a calendar widget to NewFlightDialog

Added a pop-up calendar widget to the NewFlightDialog and adjusted input parsing / verification to read/write Julian Days to the database.
Felix Turowsky 1 year ago
parent
commit
be74a7e63c

+ 16 - 0
src/classes/date.h

@@ -29,6 +29,20 @@ public:
 
     Date(int julianDay);
 
+    const static inline Date fromString(const QString &dateString, Format format = Default) {
+        switch (format) {
+        case Default:
+            return Date(QDate::fromString(dateString, Qt::ISODate).toJulianDay());
+        case SystemLocaleLong:
+            return Date(QDate::fromString(dateString, QLocale::system().dateFormat(QLocale::LongFormat)).toJulianDay());
+        case SystemLocaleShort:
+            return Date(QDate::fromString(dateString, QLocale::system().dateFormat(QLocale::ShortFormat)).toJulianDay());
+        default:
+            return Date(0);
+            break;
+        }
+    }
+
     const static inline Date fromJulianDay(int julianDay) { return Date(julianDay); }
 
     const static inline Date today() { return Date(QDate::currentDate().toJulianDay()); }
@@ -37,6 +51,8 @@ public:
 
     const QString toString(Format format = Format::Default) const;
 
+    const bool isValid() { return date.isValid(); }
+
 private:
     QDate date;
 

+ 33 - 11
src/gui/dialogues/newflightdialog.cpp

@@ -16,6 +16,7 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "newflightdialog.h"
+#include "QtWidgets/qcalendarwidget.h"
 #include "src/classes/time.h"
 #include "src/database/database.h"
 #include "src/database/databasecache.h"
@@ -100,6 +101,11 @@ void NewFlightDialog::init()
     OPL::GLOBALS->loadApproachTypes(ui->approachComboBox);
     OPL::GLOBALS->loadPilotFunctios(ui->functionComboBox);
 
+    // allocate a widget for date selection
+    calendar = new QCalendarWidget(this);
+    calendar->setVisible(false);
+    calendar->setWindowFlags(Qt::Dialog); // pop-up calendar
+
     setupRawInputValidation();
     setupSignalsAndSlots();
     readSettings();
@@ -146,6 +152,9 @@ void NewFlightDialog::setupSignalsAndSlots()
     for (const auto& line_edit : *pilotNameLineEdits)
         QObject::connect(line_edit, &QLineEdit::editingFinished,
                          this, &NewFlightDialog::onPilotNameLineEdit_editingFinshed);
+
+    QObject::connect(calendar, &QCalendarWidget::selectionChanged,
+                     this, &NewFlightDialog::calendarDateSelected);
 }
 
 /*!
@@ -551,20 +560,20 @@ void NewFlightDialog::on_doftLineEdit_editingFinished()
 {
     const auto line_edit = ui->doftLineEdit;
     auto text = ui->doftLineEdit->text();
-    auto label = ui->doftDisplayLabel;
-
-    TODO << "Non-default Date formats not implemented yet.";
-    OPL::DateFormat date_format = OPL::DateFormat::ISODate;
-    auto date = OPL::DateTime::parseInput(text, date_format);
-    if (date.isValid()) {
-        label->setText(date.toString(Qt::TextDate));
-        line_edit->setText(OPL::DateTime::dateToString(date, date_format));
-        onGoodInputReceived(line_edit);
+
+    OPL::Date date = OPL::Date::fromString(ui->doftLineEdit->text(), dateFormat);
+
+
+    LOG << "Date: " << date.toString();
+    LOG << "is valid? " << date.isValid();
+
+    line_edit->setText(date.toString(dateFormat));
+    if(ui->doftLineEdit->text().isEmpty()) {
+        onBadInputReceived(line_edit);
         return;
     }
 
-    label->setText(tr("Invalid Date."));
-    onBadInputReceived(line_edit);
+    onGoodInputReceived(line_edit);
 }
 
 void NewFlightDialog::on_pilotFlyingCheckBox_stateChanged(int arg1)
@@ -693,3 +702,16 @@ void NewFlightDialog::on_buttonBox_accepted()
     }
 }
 
+
+void NewFlightDialog::on_pushButton_clicked()
+{
+    calendar->setVisible(true);
+}
+
+void NewFlightDialog::calendarDateSelected()
+{
+    calendar->setVisible(false);
+    ui->doftLineEdit->setText(OPL::Date(calendar->selectedDate().toJulianDay()).toString(dateFormat));
+    emit ui->doftLineEdit->editingFinished();
+}
+

+ 6 - 0
src/gui/dialogues/newflightdialog.h

@@ -24,6 +24,7 @@
 #include <QList>
 #include <QBitArray>
 
+#include "QtWidgets/qcalendarwidget.h"
 #include "src/database/flightentry.h"
 #include "src/gui/verification/userinput.h"
 #include "src/opl.h"
@@ -78,6 +79,7 @@ public:
 private:
     Ui::NewFlightDialog *ui;
     ValidationState validationState;
+    QCalendarWidget *calendar;
 
     OPL::Date::Format dateFormat;
 
@@ -189,6 +191,10 @@ private slots:
     void on_approachComboBox_currentTextChanged(const QString &arg1);
     void on_functionComboBox_currentIndexChanged(int index);
 
+    void on_pushButton_clicked();
+
+    void calendarDateSelected();
+
 protected:
     bool eventFilter(QObject* object, QEvent* event) override;
 };

+ 198 - 223
src/gui/dialogues/newflightdialog.ui

@@ -14,15 +14,43 @@
    <string>Add New Flight</string>
   </property>
   <layout class="QGridLayout" name="gridLayout">
-   <item row="0" column="0">
-    <widget class="QLabel" name="doftLabel">
+   <item row="13" column="0">
+    <widget class="QLabel" name="tblkLabel">
      <property name="text">
-      <string>Date of flight</string>
+      <string>Total Time</string>
      </property>
     </widget>
    </item>
-   <item row="0" column="1">
-    <widget class="QLineEdit" name="doftLineEdit">
+   <item row="7" column="0">
+    <widget class="QLabel" name="functionLabel">
+     <property name="text">
+      <string>Function</string>
+     </property>
+    </widget>
+   </item>
+   <item row="9" column="1">
+    <widget class="QComboBox" name="flightRulesComboBox">
+     <item>
+      <property name="text">
+       <string>VFR</string>
+      </property>
+     </item>
+     <item>
+      <property name="text">
+       <string>IFR</string>
+      </property>
+     </item>
+    </widget>
+   </item>
+   <item row="10" column="0">
+    <widget class="QLabel" name="pilotFlyingLabel">
+     <property name="text">
+      <string>Pilot Flying</string>
+     </property>
+    </widget>
+   </item>
+   <item row="4" column="1">
+    <widget class="QLineEdit" name="destLocationLineEdit">
      <property name="minimumSize">
       <size>
        <width>160</width>
@@ -35,13 +63,37 @@
        <height>16777215</height>
       </size>
      </property>
-     <property name="placeholderText">
-      <string>YYYY-MM-DD</string>
+    </widget>
+   </item>
+   <item row="8" column="0">
+    <widget class="QLabel" name="approachLabel">
+     <property name="text">
+      <string>Approach</string>
+     </property>
+    </widget>
+   </item>
+   <item row="9" column="0">
+    <widget class="QLabel" name="flightRulesLabel">
+     <property name="text">
+      <string>Flight Rules</string>
      </property>
     </widget>
    </item>
-   <item row="0" column="2">
-    <widget class="QLabel" name="doftDisplayLabel">
+   <item row="13" column="3" colspan="2">
+    <widget class="QDialogButtonBox" name="buttonBox">
+     <property name="enabled">
+      <bool>true</bool>
+     </property>
+     <property name="orientation">
+      <enum>Qt::Horizontal</enum>
+     </property>
+     <property name="standardButtons">
+      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+     </property>
+    </widget>
+   </item>
+   <item row="13" column="2">
+    <widget class="QLabel" name="submissionReadyLabel">
      <property name="minimumSize">
       <size>
        <width>200</width>
@@ -59,57 +111,52 @@
      </property>
     </widget>
    </item>
-   <item row="0" column="3">
-    <widget class="QLabel" name="acftLabel">
-     <property name="text">
-      <string>Aircraft</string>
-     </property>
-     <property name="alignment">
-      <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
+   <item row="2" column="2">
+    <widget class="QLabel" name="deptNameLabel">
+     <property name="enabled">
+      <bool>false</bool>
      </property>
-    </widget>
-   </item>
-   <item row="0" column="4">
-    <widget class="QLineEdit" name="acftLineEdit">
      <property name="minimumSize">
       <size>
-       <width>160</width>
+       <width>200</width>
        <height>0</height>
       </size>
      </property>
      <property name="maximumSize">
       <size>
-       <width>120</width>
+       <width>200</width>
        <height>16777215</height>
       </size>
      </property>
+     <property name="font">
+      <font>
+       <italic>true</italic>
+      </font>
+     </property>
+     <property name="text">
+      <string/>
+     </property>
     </widget>
    </item>
-   <item row="1" column="0">
-    <widget class="QLabel" name="deptLabel">
+   <item row="10" column="1">
+    <widget class="QCheckBox" name="pilotFlyingCheckBox">
      <property name="text">
-      <string>Departure</string>
+      <string/>
      </property>
     </widget>
    </item>
-   <item row="1" column="1">
-    <widget class="QLineEdit" name="deptLocationLineEdit">
-     <property name="minimumSize">
-      <size>
-       <width>160</width>
-       <height>0</height>
-      </size>
-     </property>
-     <property name="maximumSize">
-      <size>
-       <width>120</width>
-       <height>16777215</height>
-      </size>
+   <item row="6" column="0">
+    <widget class="QLabel" name="tonbLabel">
+     <property name="text">
+      <string>On Blocks</string>
      </property>
     </widget>
    </item>
-   <item row="1" column="2">
-    <widget class="QLabel" name="deptNameLabel">
+   <item row="8" column="1">
+    <widget class="QComboBox" name="approachComboBox"/>
+   </item>
+   <item row="5" column="2">
+    <widget class="QLabel" name="tofbSpacerLabel">
      <property name="enabled">
       <bool>false</bool>
      </property>
@@ -125,51 +172,35 @@
        <height>16777215</height>
       </size>
      </property>
-     <property name="font">
-      <font>
-       <italic>true</italic>
-      </font>
-     </property>
      <property name="text">
       <string/>
      </property>
     </widget>
    </item>
-   <item row="1" column="3">
-    <widget class="QLabel" name="picLabel">
-     <property name="text">
-      <string>PIC</string>
-     </property>
-     <property name="alignment">
-      <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
+   <item row="6" column="2">
+    <widget class="QLabel" name="tonbSpacerLabel">
+     <property name="enabled">
+      <bool>false</bool>
      </property>
-    </widget>
-   </item>
-   <item row="1" column="4">
-    <widget class="QLineEdit" name="picNameLineEdit">
      <property name="minimumSize">
       <size>
-       <width>160</width>
+       <width>200</width>
        <height>0</height>
       </size>
      </property>
      <property name="maximumSize">
       <size>
-       <width>120</width>
+       <width>200</width>
        <height>16777215</height>
       </size>
      </property>
-    </widget>
-   </item>
-   <item row="2" column="0">
-    <widget class="QLabel" name="destLabel">
      <property name="text">
-      <string>Destination</string>
+      <string/>
      </property>
     </widget>
    </item>
-   <item row="2" column="1">
-    <widget class="QLineEdit" name="destLocationLineEdit">
+   <item row="6" column="1">
+    <widget class="QLineEdit" name="tonbTimeLineEdit">
      <property name="minimumSize">
       <size>
        <width>160</width>
@@ -184,7 +215,7 @@
      </property>
     </widget>
    </item>
-   <item row="2" column="2">
+   <item row="4" column="2">
     <widget class="QLabel" name="destNameLabel">
      <property name="enabled">
       <bool>false</bool>
@@ -211,18 +242,8 @@
      </property>
     </widget>
    </item>
-   <item row="2" column="3">
-    <widget class="QLabel" name="sicLabel">
-     <property name="text">
-      <string>SIC</string>
-     </property>
-     <property name="alignment">
-      <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
-     </property>
-    </widget>
-   </item>
-   <item row="2" column="4">
-    <widget class="QLineEdit" name="sicNameLineEdit">
+   <item row="5" column="1">
+    <widget class="QLineEdit" name="tofbTimeLineEdit">
      <property name="minimumSize">
       <size>
        <width>160</width>
@@ -237,15 +258,18 @@
      </property>
     </widget>
    </item>
-   <item row="3" column="0">
-    <widget class="QLabel" name="tofbLabel">
+   <item row="7" column="1">
+    <widget class="QComboBox" name="functionComboBox"/>
+   </item>
+   <item row="2" column="0">
+    <widget class="QLabel" name="deptLabel">
      <property name="text">
-      <string>Off Blocks</string>
+      <string>Departure</string>
      </property>
     </widget>
    </item>
-   <item row="3" column="1">
-    <widget class="QLineEdit" name="tofbTimeLineEdit">
+   <item row="2" column="1">
+    <widget class="QLineEdit" name="deptLocationLineEdit">
      <property name="minimumSize">
       <size>
        <width>160</width>
@@ -260,40 +284,42 @@
      </property>
     </widget>
    </item>
-   <item row="3" column="2">
-    <widget class="QLabel" name="tofbSpacerLabel">
-     <property name="enabled">
-      <bool>false</bool>
+   <item row="5" column="0">
+    <widget class="QLabel" name="tofbLabel">
+     <property name="text">
+      <string>Off Blocks</string>
+     </property>
+    </widget>
+   </item>
+   <item row="4" column="0">
+    <widget class="QLabel" name="destLabel">
+     <property name="text">
+      <string>Destination</string>
      </property>
+    </widget>
+   </item>
+   <item row="13" column="1">
+    <widget class="QLabel" name="tblkDisplayLabel">
      <property name="minimumSize">
       <size>
-       <width>200</width>
+       <width>160</width>
        <height>0</height>
       </size>
      </property>
-     <property name="maximumSize">
-      <size>
-       <width>200</width>
-       <height>16777215</height>
-      </size>
-     </property>
      <property name="text">
-      <string/>
+      <string>00:00</string>
      </property>
     </widget>
    </item>
-   <item row="3" column="3">
-    <widget class="QLabel" name="thirdPilotLabel">
+   <item row="0" column="0">
+    <widget class="QPushButton" name="pushButton">
      <property name="text">
-      <string>Third Pilot</string>
-     </property>
-     <property name="alignment">
-      <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
+      <string>Date Of flight</string>
      </property>
     </widget>
    </item>
-   <item row="3" column="4">
-    <widget class="QLineEdit" name="thirdPilotNameLineEdit">
+   <item row="0" column="1">
+    <widget class="QLineEdit" name="doftLineEdit">
      <property name="minimumSize">
       <size>
        <width>160</width>
@@ -306,17 +332,23 @@
        <height>16777215</height>
       </size>
      </property>
+     <property name="placeholderText">
+      <string>YYYY-MM-DD</string>
+     </property>
     </widget>
    </item>
-   <item row="4" column="0">
-    <widget class="QLabel" name="tonbLabel">
+   <item row="0" column="3">
+    <widget class="QLabel" name="acftLabel">
      <property name="text">
-      <string>On Blocks</string>
+      <string>Aircraft</string>
+     </property>
+     <property name="alignment">
+      <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
      </property>
     </widget>
    </item>
-   <item row="4" column="1">
-    <widget class="QLineEdit" name="tonbTimeLineEdit">
+   <item row="0" column="4">
+    <widget class="QLineEdit" name="acftLineEdit">
      <property name="minimumSize">
       <size>
        <width>160</width>
@@ -331,32 +363,36 @@
      </property>
     </widget>
    </item>
-   <item row="4" column="2">
-    <widget class="QLabel" name="tonbSpacerLabel">
-     <property name="enabled">
-      <bool>false</bool>
+   <item row="2" column="3">
+    <widget class="QLabel" name="picLabel">
+     <property name="text">
+      <string>PIC</string>
+     </property>
+     <property name="alignment">
+      <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
      </property>
+    </widget>
+   </item>
+   <item row="2" column="4">
+    <widget class="QLineEdit" name="picNameLineEdit">
      <property name="minimumSize">
       <size>
-       <width>200</width>
+       <width>160</width>
        <height>0</height>
       </size>
      </property>
      <property name="maximumSize">
       <size>
-       <width>200</width>
+       <width>120</width>
        <height>16777215</height>
       </size>
      </property>
-     <property name="text">
-      <string/>
-     </property>
     </widget>
    </item>
    <item row="4" column="3">
-    <widget class="QLabel" name="flightNumberLabel">
+    <widget class="QLabel" name="sicLabel">
      <property name="text">
-      <string>Flight Number</string>
+      <string>SIC</string>
      </property>
      <property name="alignment">
       <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
@@ -364,7 +400,7 @@
     </widget>
    </item>
    <item row="4" column="4">
-    <widget class="QLineEdit" name="flightNumberLineEdit">
+    <widget class="QLineEdit" name="sicNameLineEdit">
      <property name="minimumSize">
       <size>
        <width>160</width>
@@ -379,20 +415,10 @@
      </property>
     </widget>
    </item>
-   <item row="5" column="0">
-    <widget class="QLabel" name="functionLabel">
-     <property name="text">
-      <string>Function</string>
-     </property>
-    </widget>
-   </item>
-   <item row="5" column="1">
-    <widget class="QComboBox" name="functionComboBox"/>
-   </item>
    <item row="5" column="3">
-    <widget class="QLabel" name="remarksLabel">
+    <widget class="QLabel" name="thirdPilotLabel">
      <property name="text">
-      <string>Remarks</string>
+      <string>Third Pilot</string>
      </property>
      <property name="alignment">
       <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
@@ -400,7 +426,7 @@
     </widget>
    </item>
    <item row="5" column="4">
-    <widget class="QLineEdit" name="remarksLineEdit">
+    <widget class="QLineEdit" name="thirdPilotNameLineEdit">
      <property name="minimumSize">
       <size>
        <width>160</width>
@@ -415,123 +441,78 @@
      </property>
     </widget>
    </item>
-   <item row="6" column="0">
-    <widget class="QLabel" name="approachLabel">
-     <property name="text">
-      <string>Approach</string>
-     </property>
-    </widget>
-   </item>
-   <item row="6" column="1">
-    <widget class="QComboBox" name="approachComboBox"/>
-   </item>
    <item row="6" column="3">
-    <widget class="QLabel" name="takeOffLabel">
-     <property name="text">
-      <string>Take Off</string>
-     </property>
-    </widget>
-   </item>
-   <item row="6" column="4">
-    <widget class="QSpinBox" name="takeOffSpinBox"/>
-   </item>
-   <item row="7" column="0">
-    <widget class="QLabel" name="flightRulesLabel">
-     <property name="text">
-      <string>Flight Rules</string>
-     </property>
-    </widget>
-   </item>
-   <item row="7" column="1">
-    <widget class="QComboBox" name="flightRulesComboBox">
-     <item>
-      <property name="text">
-       <string>VFR</string>
-      </property>
-     </item>
-     <item>
-      <property name="text">
-       <string>IFR</string>
-      </property>
-     </item>
-    </widget>
-   </item>
-   <item row="7" column="3">
-    <widget class="QLabel" name="landingLabel">
-     <property name="text">
-      <string>Landing</string>
-     </property>
-    </widget>
-   </item>
-   <item row="7" column="4">
-    <widget class="QSpinBox" name="landingSpinBox"/>
-   </item>
-   <item row="8" column="0">
-    <widget class="QLabel" name="pilotFlyingLabel">
-     <property name="text">
-      <string>Pilot Flying</string>
-     </property>
-    </widget>
-   </item>
-   <item row="8" column="1">
-    <widget class="QCheckBox" name="pilotFlyingCheckBox">
+    <widget class="QLabel" name="flightNumberLabel">
      <property name="text">
-      <string/>
+      <string>Flight Number</string>
      </property>
-    </widget>
-   </item>
-   <item row="9" column="0">
-    <widget class="QLabel" name="tblkLabel">
-     <property name="text">
-      <string>Total Time</string>
+     <property name="alignment">
+      <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
      </property>
     </widget>
    </item>
-   <item row="9" column="1">
-    <widget class="QLabel" name="tblkDisplayLabel">
+   <item row="6" column="4">
+    <widget class="QLineEdit" name="flightNumberLineEdit">
      <property name="minimumSize">
       <size>
        <width>160</width>
        <height>0</height>
       </size>
      </property>
+     <property name="maximumSize">
+      <size>
+       <width>120</width>
+       <height>16777215</height>
+      </size>
+     </property>
+    </widget>
+   </item>
+   <item row="7" column="3">
+    <widget class="QLabel" name="remarksLabel">
      <property name="text">
-      <string>00:00</string>
+      <string>Remarks</string>
+     </property>
+     <property name="alignment">
+      <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
      </property>
     </widget>
    </item>
-   <item row="9" column="2">
-    <widget class="QLabel" name="submissionReadyLabel">
+   <item row="7" column="4">
+    <widget class="QLineEdit" name="remarksLineEdit">
      <property name="minimumSize">
       <size>
-       <width>200</width>
+       <width>160</width>
        <height>0</height>
       </size>
      </property>
      <property name="maximumSize">
       <size>
-       <width>200</width>
+       <width>120</width>
        <height>16777215</height>
       </size>
      </property>
+    </widget>
+   </item>
+   <item row="8" column="3">
+    <widget class="QLabel" name="takeOffLabel">
      <property name="text">
-      <string/>
+      <string>Take Off</string>
      </property>
     </widget>
    </item>
-   <item row="9" column="3" colspan="2">
-    <widget class="QDialogButtonBox" name="buttonBox">
-     <property name="enabled">
-      <bool>true</bool>
-     </property>
-     <property name="orientation">
-      <enum>Qt::Horizontal</enum>
-     </property>
-     <property name="standardButtons">
-      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+   <item row="8" column="4">
+    <widget class="QSpinBox" name="takeOffSpinBox"/>
+   </item>
+   <item row="9" column="3">
+    <widget class="QLabel" name="landingLabel">
+     <property name="text">
+      <string>Landing</string>
      </property>
     </widget>
    </item>
+   <item row="9" column="4">
+    <widget class="QSpinBox" name="landingSpinBox"/>
+   </item>
   </layout>
  </widget>
  <tabstops>
@@ -539,12 +520,6 @@
   <tabstop>destLocationLineEdit</tabstop>
   <tabstop>tofbTimeLineEdit</tabstop>
   <tabstop>tonbTimeLineEdit</tabstop>
-  <tabstop>picNameLineEdit</tabstop>
-  <tabstop>sicNameLineEdit</tabstop>
-  <tabstop>thirdPilotNameLineEdit</tabstop>
-  <tabstop>flightNumberLineEdit</tabstop>
-  <tabstop>remarksLineEdit</tabstop>
-  <tabstop>takeOffSpinBox</tabstop>
   <tabstop>approachComboBox</tabstop>
  </tabstops>
  <resources/>