Browse Source

Bugfixes in NewFlightDialog

Some minor fixes and changes, mostly in the NewFlightDialog and associated classes
- Improved time input verification
- Improved detection of inconsistencies in pilot name inputs
Felix Turowsky 1 year ago
parent
commit
ddb74dd79f

+ 2 - 5
mainwindow.cpp

@@ -25,7 +25,6 @@
 #include "src/database/database.h"
 #include "src/classes/style.h"
 #include "src/gui/dialogues/firstrundialog.h"
-#include "src/gui/dialogues/newsimdialog.h"
 #include "src/database/databasecache.h"
 #include "src/classes/settings.h"
 // Quick and dirty Debug area
@@ -169,14 +168,12 @@ void MainWindow::connectWidgets()
 {
     QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
                      logbookWidget,  &LogbookTableEditWidget::viewSelectionChanged);
-
-    QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
-                     this,           &MainWindow::onStyleChanged);
-
     QObject::connect(this,			 &MainWindow::addFlightEntryRequested,
                      logbookWidget,  &LogbookTableEditWidget::addEntryRequested);
     QObject::connect(this,			 &MainWindow::addSimulatorEntryRequested,
                      logbookWidget,  &LogbookTableEditWidget::addSimulatorEntryRequested);
+    QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
+                     this,           &MainWindow::onStyleChanged);
 }
 
 void MainWindow::onDatabaseInvalid()

+ 3 - 9
mainwindow.h

@@ -169,15 +169,9 @@ protected:
      */
     void resizeEvent(QResizeEvent *event) override
     {
-        //DEB << "SIZE:" << this->size();
-        int icon_size;
-        if (this->height() < 760)
-            icon_size = (this->height() / 16);
-        else
-            icon_size = (this->height() / 14);
-
-        auto tool_bar = this->findChild<QToolBar*>();
-        tool_bar->setIconSize(QSize(icon_size, icon_size));
+        const auto icon_size = this->height() / 14;
+        const auto toolBar = this->findChild<QToolBar*>();
+        toolBar->setIconSize(QSize(icon_size, icon_size));
         event->accept();
     }
 

+ 2 - 0
src/classes/time.cpp

@@ -95,6 +95,8 @@ Time Time::blockTime(const Time &offBlocks, const Time &onBlocks)
         // take-off and landing on the same day
         return Time(onBlocks.m_minutes - offBlocks.m_minutes, offBlocks.m_format);
     } else {
+        if(offBlocks.m_minutes == onBlocks.m_minutes)
+            return Time(0, offBlocks.m_format);
         // landing the day after take off
         int minutesToMidnight = MINUTES_PER_DAY - offBlocks.m_minutes;
         return Time(minutesToMidnight + onBlocks.m_minutes, offBlocks.m_format);

+ 80 - 14
src/gui/dialogues/newflightdialog.cpp

@@ -66,6 +66,7 @@ NewFlightDialog::~NewFlightDialog()
 
 void NewFlightDialog::setPilotFunction()
 {
+    const QString &self = DBCache->getPilotNamesMap().value(1);
     if(Settings::getPilotFunction() == OPL::PilotFunction::PIC){
         ui->picNameLineEdit->setText(self);
         ui->functionComboBox->setCurrentIndex(0);
@@ -513,8 +514,7 @@ void NewFlightDialog::onTimeLineEdit_editingFinished()
     const auto line_edit = this->findChild<QLineEdit*>(sender()->objectName());
 
     if(!verifyUserInput(line_edit, TimeInput(line_edit->text(), m_format))) {
-        if(!addNewDatabaseElement(line_edit, OPL::DbTable::Pilots))
-            onBadInputReceived(line_edit);
+        onBadInputReceived(line_edit);
     }
 }
 
@@ -628,18 +628,45 @@ void NewFlightDialog::on_approachComboBox_currentTextChanged(const QString &arg1
  */
 void NewFlightDialog::on_functionComboBox_currentIndexChanged(int index)
 {
-    if (index == static_cast<int>(OPL::PilotFunction::PIC)) {
+    int picPilotId = DBCache->getPilotNamesMap().key(ui->picNameLineEdit->text());
+    int sicPilotId = DBCache->getPilotNamesMap().key(ui->sicNameLineEdit->text());
+    int thirdPilotId = DBCache->getPilotNamesMap().key(ui->thirdPilotNameLineEdit->text());
+    const QString &self = DBCache->getPilotNamesMap().value(1);
+
+    switch (index) {
+    case static_cast<int>(OPL::PilotFunction::PIC):
+        DEB << "PIC selected...";
         ui->picNameLineEdit->setText(self);
         emit ui->picNameLineEdit->editingFinished();
-        if (DBCache->getPilotNamesMap().key(ui->picNameLineEdit->text())
-         == DBCache->getPilotNamesMap().key(ui->sicNameLineEdit->text()))
-                ui->sicNameLineEdit->setText(QString());
-    } else if (index == static_cast<int>(OPL::PilotFunction::SIC)) {
+        picPilotId = DBCache->getPilotNamesMap().key(ui->picNameLineEdit->text());
+
+        if (picPilotId == sicPilotId) {
+            ui->sicNameLineEdit->setText(QString());
+            onBadInputReceived(ui->sicNameLineEdit);
+        }
+        if (picPilotId == thirdPilotId) {
+            ui->thirdPilotNameLineEdit->setText(QString());
+            onBadInputReceived(ui->thirdPilotNameLineEdit);
+        }
+        break;
+    case static_cast<int>(OPL::PilotFunction::SIC):
+        DEB << "SIC selected...";
         ui->sicNameLineEdit->setText(self);
         emit ui->sicNameLineEdit->editingFinished();
-        if (DBCache->getPilotNamesMap().key(ui->picNameLineEdit->text())
-         == DBCache->getPilotNamesMap().key(ui->sicNameLineEdit->text()))
+        sicPilotId = DBCache->getPilotNamesMap().key(ui->sicNameLineEdit->text());
+
+        if (sicPilotId == picPilotId) {
             ui->picNameLineEdit->setText(QString());
+            onBadInputReceived(ui->picNameLineEdit);
+        }
+        if (sicPilotId == thirdPilotId) {
+            ui->thirdPilotNameLineEdit->setText(QString());
+            onBadInputReceived(ui->thirdPilotNameLineEdit);
+        }
+        break;
+    default:
+        break;
+
     }
 }
 
@@ -652,7 +679,7 @@ void NewFlightDialog::on_functionComboBox_currentIndexChanged(int index)
  * \param error_msg - the error string displayed to the user
  * \return
  */
-bool NewFlightDialog::checkPilotFunctionsValid()
+bool NewFlightDialog::pilotFunctionsInvalid()
 {
     int pic_id = DBCache->getPilotNamesMap().key(ui->picNameLineEdit->text());
     int function_index = ui->functionComboBox->currentIndex();
@@ -661,17 +688,52 @@ bool NewFlightDialog::checkPilotFunctionsValid()
         if (!(function_index == static_cast<int>(OPL::PilotFunction::PIC) || function_index == static_cast<int>(OPL::PilotFunction::FI))) {
             INFO(tr("The PIC is set to %1 but the Pilot Function is set to %2")
                     .arg(ui->picNameLineEdit->text(), ui->functionComboBox->currentText()));
-            return false;
+            return true;
         }
     } else {
         if (function_index == static_cast<int>(OPL::PilotFunction::PIC) || function_index == static_cast<int>(OPL::PilotFunction::FI)) {
             INFO(tr("The Pilot Function is set to %1, but the PIC is set to %2")
                     .arg(ui->functionComboBox->currentText(), ui->picNameLineEdit->text()));
-            return false;
+            return true;
         }
     }
 
-    return true;
+    return false;
+}
+
+bool NewFlightDialog::duplicateNamesPresent()
+{
+    const int picId = DBCache->getPilotNamesMap().key(ui->picNameLineEdit->text());
+    const int sicId = DBCache->getPilotNamesMap().key(ui->sicNameLineEdit->text());
+    const int thirdPilotId = DBCache->getPilotNamesMap().key(ui->thirdPilotNameLineEdit->text());
+
+    // this is a bit explicit but better point out to the user what the case is
+    if (picId == sicId) {
+        INFO(tr("PIC and SIC names are the same."));
+        return true;
+    }
+    if (picId == thirdPilotId && picId > 0) {
+        INFO(tr("PIC and third Pilot names are the same."));
+        return true;
+    }
+    if (sicId == thirdPilotId && sicId > 0) {
+        INFO(tr("SIC and third Pilot names are the same."));
+        return true;
+    }
+
+    return false;
+}
+
+bool NewFlightDialog::flightTimeIsZero()
+{
+    const OPL::Time tofb = OPL::Time::fromString(ui->tofbTimeLineEdit->text(), m_format);
+    const OPL::Time tonb = OPL::Time::fromString(ui->tonbTimeLineEdit->text(), m_format);
+    const int block_minutes = OPL::Time::blockMinutes(tofb, tonb);
+    if(block_minutes == 0) {
+        INFO(tr("Total time of flight is zero."));
+        return true;
+    }
+    return false;
 }
 
 /*!
@@ -706,7 +768,11 @@ void NewFlightDialog::on_buttonBox_accepted()
         return;
     }
 
-    if(!checkPilotFunctionsValid())
+    if(pilotFunctionsInvalid())
+        return;
+    if(duplicateNamesPresent())
+        return;
+    if(flightTimeIsZero())
         return;
 
     // If input verification passed, collect input and submit to database

+ 4 - 2
src/gui/dialogues/newflightdialog.h

@@ -110,7 +110,7 @@ private:
      * The list is ordered like the ValidationItem enum so that indexed access is possible using the enum.
      */
     static const inline QList<QLineEdit*> *mandatoryLineEdits;
-    static const inline QLatin1String self = QLatin1String("self");
+    // static const inline QLatin1String self = QLatin1String("self");
     static const inline QHash<int, QString> pilotFuncionsMap = {
                                            {0, OPL::FlightEntry::TPIC},
                                            {1, OPL::FlightEntry::TPICUS},
@@ -177,7 +177,9 @@ private:
     bool userWantsToAddNewEntry(OPL::DbTable table);
 
 
-    bool checkPilotFunctionsValid();
+    bool pilotFunctionsInvalid();
+    bool duplicateNamesPresent();
+    bool flightTimeIsZero();
     OPL::RowData_T prepareFlightEntryData();
 
     const static inline auto CAT_3 = QLatin1String(OPL::GLOBALS->getApproachTypes()[3].toLatin1());

+ 10 - 1
src/gui/verification/timeinput.cpp

@@ -28,6 +28,8 @@ QString TimeInput::fixup() const
 
 const QString TimeInput::fixDefaultFormat() const
 {
+    if(input == QString())
+        return QStringLiteral("00:00");
     // try inserting a ':' for hhmm inputs
     QString fixed = input;
     if (input.contains(':')) { // contains seperator
@@ -43,7 +45,14 @@ const QString TimeInput::fixDefaultFormat() const
         }
     }
 
-    return OPL::Time::fromString(fixed, m_format).toString();
+    OPL::Time fixedTime = OPL::Time::fromString(fixed, m_format);
+    if(fixedTime.isValid()) {
+        return OPL::Time::fromString(fixed, m_format).toString();
+    } else {
+        return QString();
+    }
+
+
 }
 
 const QString TimeInput::fixDecimalFormat() const