Browse Source

Merge pull request #60 from fiffty-50/develop-newflightdialog-lineedit-refactor

Refactor of mandatoryLineEdits
Felix Turowsky 4 years ago
parent
commit
becef44d95
2 changed files with 100 additions and 41 deletions
  1. 76 34
      src/gui/dialogues/newflightdialog.cpp
  2. 24 7
      src/gui/dialogues/newflightdialog.h

+ 76 - 34
src/gui/dialogues/newflightdialog.cpp

@@ -55,9 +55,49 @@ static const auto MANDATORY_LINE_EDITS_DISPLAY_NAMES = QMap<int, QString> {
     {6, QObject::tr("Aircraft Registration")}
 };
 
-
-
-
+//
+// MandatoryLineEdits definition
+// Ugly but works
+NewFlightDialog::MandatoryLineEdits::MandatoryLineEdits(std::initializer_list<QLineEdit*> init_list)
+    : lineEdits(init_list), lineEditsValid(QBitArray(init_list.size()))
+{}
+void NewFlightDialog::MandatoryLineEdits::operator= (std::initializer_list<QLineEdit*> init_list)
+{
+    lineEdits = init_list;
+    lineEditsValid.resize(init_list.size());
+}
+bool NewFlightDialog::MandatoryLineEdits::contains(QLineEdit* line_edit)
+{
+    return lineEdits.contains(line_edit);
+}
+void NewFlightDialog::MandatoryLineEdits::validate(QLineEdit* line_edit)
+{
+    lineEditsValid.setBit(lineEdits.indexOf(line_edit), true);
+}
+void NewFlightDialog::MandatoryLineEdits::unvalidate(QLineEdit* line_edit)
+{
+    lineEditsValid.setBit(lineEdits.indexOf(line_edit), false);
+}
+int NewFlightDialog::MandatoryLineEdits::countValid()
+{
+    return lineEditsValid.count(true);
+}
+int NewFlightDialog::MandatoryLineEdits::size()
+{
+    return lineEditsValid.size();
+}
+bool NewFlightDialog::MandatoryLineEdits::validAt(int idx)
+{
+    return lineEditsValid[idx];
+}
+bool NewFlightDialog::MandatoryLineEdits::allValid()
+{
+    return lineEditsValid.count(true) == lineEdits.size();
+}
+QLineEdit* NewFlightDialog::MandatoryLineEdits::operator[] (int idx)
+{
+    return lineEdits[idx];
+}
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 ///                                      Construction                                           ///
@@ -255,6 +295,8 @@ void NewFlightDialog::setupRawInputValidation()
             }
         }
     }
+
+    // [G]: TODO cleanup
     // populate Mandatory Line Edits list and prepare QBitArray
     mandatoryLineEdits = {
         ui->doftLineEdit,
@@ -265,6 +307,7 @@ void NewFlightDialog::setupRawInputValidation()
         ui->picNameLineEdit,
         ui->acftLineEdit,
     };
+
     primaryTimeLineEdits = {
         ui->tofbTimeLineEdit,
         ui->tonbTimeLineEdit
@@ -275,7 +318,6 @@ void NewFlightDialog::setupRawInputValidation()
         ui->thirdPilotNameLineEdit
     };
 
-    mandatoryLineEditsGood.resize(mandatoryLineEdits.size());
 }
 
 void NewFlightDialog::setupSignalsAndSlots()
@@ -302,7 +344,7 @@ void NewFlightDialog::setupSignalsAndSlots()
         }
     }
 #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
-    for (const auto &line_edit : qAsConst(mandatoryLineEdits)) {
+    for (const auto &line_edit : qAsConst(mandatoryLineEdits.lineEdits)) {
         if(line_edit->objectName().contains(QStringLiteral("doft")))
             break;
         QObject::connect(line_edit->completer(), QOverload<const QString &>::of(&QCompleter::highlighted),
@@ -351,7 +393,7 @@ bool NewFlightDialog::eventFilter(QObject* object, QEvent* event)
     auto line_edit = qobject_cast<QLineEdit*>(object);
     if (line_edit != nullptr) {
         if (mandatoryLineEdits.contains(line_edit) && event->type() == QEvent::FocusIn) {
-            mandatoryLineEditsGood.setBit(mandatoryLineEdits.indexOf(line_edit), false);
+            mandatoryLineEdits.unvalidate(line_edit);
             DEB << "Editing " << line_edit->objectName();
             // set verification bit to false when entering a mandatory line edit
             return false;
@@ -386,7 +428,7 @@ bool NewFlightDialog::eventFilter(QObject* object, QEvent* event)
 void NewFlightDialog::fillDeductibleData()
 {
     // check if mandatory line edits are valid
-    if (mandatoryLineEditsGood.count(true) != 7) {
+    if (!mandatoryLineEdits.allValid()) {
         return;
     }
 
@@ -437,7 +479,7 @@ void NewFlightDialog::fillDeductibleData()
         ui->tIFRLabel->setText(block_time_string);
     }
     // Night
-    auto dept_date = ui->doftLineEdit->text() + 'T'
+    QString dept_date = ui->doftLineEdit->text() + 'T'
             + ATime::toString(tofb);
     auto dept_date_time = QDateTime::fromString(dept_date, QStringLiteral("yyyy-MM-ddThh:mm"));
     const int night_angle = ASettings::read(ASettings::FlightLogging::NightAngle).toInt();
@@ -527,7 +569,7 @@ RowData NewFlightDialog::collectInput()
         newData.insert(Opl::Db::FLIGHTS_TIFR, EMPTY_STRING);
     }
     // Night
-    const auto dept_date = ui->doftLineEdit->text() + 'T'
+    const QString dept_date = ui->doftLineEdit->text() + 'T'
             + ATime::toString(tofb);
     const auto dept_date_time = QDateTime::fromString(dept_date, QStringLiteral("yyyy-MM-ddThh:mm"));
     const int night_angle = ASettings::read(ASettings::FlightLogging::NightAngle).toInt();
@@ -609,7 +651,7 @@ RowData NewFlightDialog::collectInput()
             newData.insert(Opl::Db::FLIGHTS_LDGDAY, 0);
             newData.insert(Opl::Db::FLIGHTS_LDGNIGHT, ui->LandingSpinBox->value());
         } else { //check
-            const auto dest_date = ui->doftLineEdit->text() + 'T'
+            const QString dest_date = ui->doftLineEdit->text() + 'T'
                     + ATime::toString(tonb);
             const auto dest_date_time = QDateTime::fromString(dest_date, QStringLiteral("yyyy-MM-ddThh:mm"));
             if (ACalc::isNight(ui->destLocLineEdit->text(), dest_date_time,  night_angle)) {
@@ -650,7 +692,7 @@ void NewFlightDialog::formFiller()
 
     for (const auto& data_key : flightEntry.getData().keys()) {
         auto rx = QRegularExpression(data_key + QStringLiteral("LineEdit"));//acftLineEdit
-        for(const auto& leName : line_edits_names){
+        for(const auto& leName : qAsConst(line_edits_names)){
             if(rx.match(leName).hasMatch())  {
                 //DEB << "Loc Match found: " << key << " - " << leName);
                 auto line_edit = this->findChild<QLineEdit *>(leName);
@@ -662,7 +704,7 @@ void NewFlightDialog::formFiller()
             }
         }
         rx = QRegularExpression(data_key + QStringLiteral("Loc\\w+?"));
-        for(const auto& leName : line_edits_names){
+        for(const auto& leName : qAsConst(line_edits_names)){
             if(rx.match(leName).hasMatch())  {
                 //DEB << "Loc Match found: " << key << " - " << leName);
                 auto line_edit = this->findChild<QLineEdit *>(leName);
@@ -674,7 +716,7 @@ void NewFlightDialog::formFiller()
             }
         }
         rx = QRegularExpression(data_key + QStringLiteral("Time\\w+?"));
-        for(const auto& leName : line_edits_names){
+        for(const auto& leName : qAsConst(line_edits_names)){
             if(rx.match(leName).hasMatch())  {
                 //DEB << "Time Match found: " << key << " - " << leName);
                 auto line_edits = this->findChild<QLineEdit *>(leName);
@@ -688,7 +730,7 @@ void NewFlightDialog::formFiller()
             }
         }
         rx = QRegularExpression(data_key + QStringLiteral("Name\\w+?"));
-        for(const auto& leName : line_edits_names){
+        for(const auto& leName : qAsConst(line_edits_names)){
             if(rx.match(leName).hasMatch())  {
                 auto line_edits = this->findChild<QLineEdit *>(leName);
                 if(line_edits != nullptr){
@@ -756,14 +798,14 @@ void NewFlightDialog::formFiller()
         ui->AutolandSpinBox->setValue(AL);
     }
 
-    for(const auto& le : mandatoryLineEdits){
+    for(const auto& le : qAsConst(mandatoryLineEdits.lineEdits)){
         emit le->editingFinished();
     }
 }
 
 bool NewFlightDialog::isLessOrEqualThanBlockTime(const QString time_string)
 {
-    if (mandatoryLineEditsGood.count(true) != 7){
+    if (!mandatoryLineEdits.allValid()){
         QMessageBox message_box(this);
         message_box.setText(tr("Unable to determine total block time.<br>"
                                "Please fill out all Mandatory Fields<br>"
@@ -864,14 +906,14 @@ void NewFlightDialog::on_cancelButton_clicked()
 
 void NewFlightDialog::on_submitButton_clicked()
 {
-    for (const auto &line_edit : mandatoryLineEdits) {
+    for (const auto &line_edit : qAsConst(mandatoryLineEdits.lineEdits)) {
         emit line_edit->editingFinished();
     }
-    DEB << "editing finished emitted. good count: " << mandatoryLineEditsGood.count(true);
-    if (mandatoryLineEditsGood.count(true) != 7) {
+    DEB << "editing finished emitted. good count: " << mandatoryLineEdits.countValid();
+    if (!mandatoryLineEdits.allValid()) {
         QString missing_items;
-        for (int i=0; i < mandatoryLineEditsGood.size(); i++) {
-            if (!mandatoryLineEditsGood[i]){
+        for (int i=0; i < mandatoryLineEdits.size(); i++) {
+            if (!mandatoryLineEdits.validAt(i)){
                 missing_items.append(MANDATORY_LINE_EDITS_DISPLAY_NAMES.value(i) + "<br>");
                 mandatoryLineEdits[i]->setStyleSheet(QStringLiteral("border: 1px solid red"));
             }
@@ -887,7 +929,7 @@ void NewFlightDialog::on_submitButton_clicked()
         return;
     }
 
-    DEB << "Submit Button clicked. Mandatory good (out of 7): " << mandatoryLineEditsGood.count(true);
+    DEB << "Submit Button clicked. Mandatory good " << mandatoryLineEdits.size() << "out of: " << mandatoryLineEdits.countValid();
     auto newData = collectInput();
     DEB << "Setting Data for flightEntry...";
     flightEntry.setData(newData);
@@ -918,14 +960,14 @@ void NewFlightDialog::onGoodInputReceived(QLineEdit *line_edit)
     line_edit->setStyleSheet("");
 
     if (mandatoryLineEdits.contains(line_edit))
-        mandatoryLineEditsGood.setBit(mandatoryLineEdits.indexOf(line_edit), true);
+        mandatoryLineEdits.validate(line_edit);
 
-    if (mandatoryLineEditsGood.count(true) == 7)
+    if (mandatoryLineEdits.allValid())
         onMandatoryLineEditsFilled();
 
-    DEB << "Mandatory good: " << mandatoryLineEditsGood.count(true)
-        << " (out of 7) " << mandatoryLineEditsGood;
-
+    DEB << "Mandatory good: " << mandatoryLineEdits.countValid()
+        << " out of " << mandatoryLineEdits.size()
+        << " : " << mandatoryLineEdits.lineEdits;
 }
 
 void NewFlightDialog::onBadInputReceived(QLineEdit *line_edit)
@@ -933,8 +975,8 @@ void NewFlightDialog::onBadInputReceived(QLineEdit *line_edit)
     DEB << line_edit->objectName() << " - Bad input received - " << line_edit->text();
     line_edit->setStyleSheet(QStringLiteral("border: 1px solid red"));
 
-    DEB << "Mandatory Good: " << mandatoryLineEditsGood.count(true) << " out of "
-        << mandatoryLineEditsGood.size() << ". Array: " << mandatoryLineEditsGood;
+    DEB << "Mandatory Good: " << mandatoryLineEdits.countValid() << " out of "
+        << mandatoryLineEdits.size() << ". Array: " << mandatoryLineEdits.lineEditsValid;
 }
 
 // capitalize input for dept, dest and registration input
@@ -952,14 +994,14 @@ void NewFlightDialog::onToUpperTriggered_textChanged(const QString &text)
 // update is disabled if the user chose to manually edit extra times
 void NewFlightDialog::onMandatoryLineEditsFilled()
 {
-    if (!(mandatoryLineEditsGood.count(true) == 7)) {
+    if (!mandatoryLineEdits.allValid()) {
         DEB << "erroneously called.";
         return;
     };
 
     if (updateEnabled)
         fillDeductibleData();
-    DEB << mandatoryLineEditsGood;
+    DEB << mandatoryLineEdits.lineEditsValid;
 }
 
 // make sure that when using keyboard to scroll through completer sugggestions, line edit is up to date
@@ -1256,13 +1298,13 @@ void NewFlightDialog::on_PilotFlyingCheckBox_stateChanged(int)
 
 void NewFlightDialog::on_IfrCheckBox_stateChanged(int)
 {
-    if (mandatoryLineEditsGood.count(true) == 7 && updateEnabled)
+    if (mandatoryLineEdits.allValid() && updateEnabled)
         onMandatoryLineEditsFilled();
 }
 
 void NewFlightDialog::on_manualEditingCheckBox_stateChanged(int arg1)
 {
-    if (!(mandatoryLineEditsGood.count(true) == 7) && ui->manualEditingCheckBox->isChecked()) {
+    if (!(mandatoryLineEdits.allValid()) && ui->manualEditingCheckBox->isChecked()) {
         QMessageBox message_box(this);
         message_box.setText(tr("Before editing times manually, please fill out the required fields "
                                "in the flight data tab, so that total time can be calculated."));
@@ -1280,7 +1322,7 @@ void NewFlightDialog::on_manualEditingCheckBox_stateChanged(int arg1)
             le->setStyleSheet("");
         }
         updateEnabled = true;
-        if (mandatoryLineEditsGood.count(true) == 7 && updateEnabled)
+        if (mandatoryLineEdits.allValid() && updateEnabled)
             onMandatoryLineEditsFilled();
         break;
     case 2:

+ 24 - 7
src/gui/dialogues/newflightdialog.h

@@ -92,15 +92,32 @@ private:
      */
     AFlightEntry flightEntry;
 
-    QList<QLineEdit*> mandatoryLineEdits;
-    QList<QLineEdit*> primaryTimeLineEdits;
-    QList<QLineEdit*> pilotsLineEdits;
-
+    // [G]: Initial refactoring based on previous use.
     /*!
-     * \brief holds a bit for each mandatory line edit that is flipped
-     * according to its validity state
+     * \brief Wrapper around Vector of mandatory line edits and their corresponding
+     * "ok" QBitArray.
      */
-    QBitArray mandatoryLineEditsGood;
+    struct MandatoryLineEdits {
+        QVector<QLineEdit*> lineEdits;
+        QBitArray lineEditsValid;
+
+        MandatoryLineEdits() = default;
+        MandatoryLineEdits(std::initializer_list<QLineEdit*> init_list);
+        void operator= (std::initializer_list<QLineEdit*> init_list);
+
+        bool contains(QLineEdit* line_edit);
+        void validate(QLineEdit* line_edit);
+        void unvalidate(QLineEdit* line_edit);
+        int countValid();
+        int size();
+        bool validAt(int idx);
+        bool allValid();
+        QLineEdit* operator[] (int idx);
+
+    } mandatoryLineEdits;
+
+    QVector<QLineEdit*> primaryTimeLineEdits;
+    QVector<QLineEdit*> pilotsLineEdits;
 
     /*!
      * To be used by the QCompleters