Browse Source

Rework of newPilot Line Edits

fiffty-50 4 years ago
parent
commit
533b0c746b
6 changed files with 115 additions and 103 deletions
  1. 14 0
      dbpilots.cpp
  2. 3 1
      dbpilots.h
  3. 2 1
      homewidget.cpp
  4. 2 2
      mainwindow.cpp
  5. 87 92
      newflight.cpp
  6. 7 7
      newflight.h

+ 14 - 0
dbpilots.cpp

@@ -140,6 +140,7 @@ QStringList dbPilots::retreivePilotNameFromString(QString searchstring)
  */
 QStringList dbPilots::newPicGetString(QString searchstring)
 {
+    qWarning() << "newPicGetString is deprecated";
     QStringList result;
     QStringList searchlist;
 
@@ -258,6 +259,19 @@ QStringList dbPilots::newPicGetString(QString searchstring)
     }
 }
 
+QStringList dbPilots::retreivePilotList()
+{
+    QSqlQuery query;
+    query.prepare("SELECT piclastname, picfirstname FROM pilots");
+    query.exec();
+
+    QStringList result;
+    while (query.next()) {
+        result.append(query.value(0).toString() + ", " + query.value(1).toString());
+    }
+    return result;
+}
+
 QString dbPilots::newPicGetId(QString name)
 {
     QString result;

+ 3 - 1
dbpilots.h

@@ -34,7 +34,9 @@ public:
 
     static QStringList retreivePilotNameFromString(QString searchstring);
 
-    static QStringList newPicGetString(QString searchstring);
+    static QStringList newPicGetString(QString searchstring); //deprecated
+
+    static QStringList retreivePilotList();
 
     static QString newPicGetId(QString name);
 };

+ 2 - 1
homewidget.cpp

@@ -61,5 +61,6 @@ homeWidget::~homeWidget()
 void homeWidget::on_debugButton_clicked()
 {
 //    ui->debugLineEdit->setText(dbAircraft::retreiveAircraftDetails("102")[1]);
-    dbAirport::retreiveIataIcaoList();
+    qDebug() << "Debug: ";
+    dbPilots::retreivePilotList();
 }

+ 2 - 2
mainwindow.cpp

@@ -121,7 +121,7 @@ void MainWindow::on_actionNewFlight_triggered()
 {
     auto locationList = dbAirport::retreiveIataIcaoList();
     auto registrationList = dbAircraft::retreiveRegistrationList();
-    QStringList pilotNameList;
-    NewFlight nf(this, locationList, registrationList, pilotNameList);
+    auto pilotList = dbPilots::retreivePilotList();
+    NewFlight nf(this, locationList, registrationList, pilotList);
     nf.exec();
 }

+ 87 - 92
newflight.cpp

@@ -177,7 +177,7 @@ void NewFlight::restoreSettings()
 
 NewFlight::NewFlight(QWidget *parent, QStringList locationList,
                                       QStringList registrationList,
-                                      QStringList pilotNameList) :
+                                      QStringList pilotList) :
     QDialog(parent),
     ui(new Ui::NewFlight)
 {
@@ -202,7 +202,15 @@ NewFlight::NewFlight(QWidget *parent, QStringList locationList,
     aircraftCompleter->setCompletionMode(QCompleter::PopupCompletion);
     aircraftCompleter->setFilterMode(Qt::MatchContains);
     ui->newAcft->setCompleter(aircraftCompleter);
-    // To Do: Aircraft and Pilot Names Completer
+    // Pilot Line Edits Auto Completion
+    auto *pilotCompleter = new QCompleter(pilotList);
+    pilotCompleter->setCaseSensitivity(Qt::CaseInsensitive);
+    pilotCompleter->setCompletionMode(QCompleter::PopupCompletion);
+    pilotCompleter->setFilterMode(Qt::MatchContains);
+    ui->newPicNameLineEdit->setCompleter(pilotCompleter);
+    ui->secondPilotNameLineEdit->setCompleter(pilotCompleter);;
+    ui->thirdPilotNameLineEdit->setCompleter(pilotCompleter);
+
 
     // Groups for CheckBoxes
     QButtonGroup *FlightRulesGroup = new QButtonGroup(this);
@@ -424,7 +432,6 @@ void NewFlight::on_newAcft_inputRejected()
 void NewFlight::on_newAcft_editingFinished()
 {
     acft = "-1";// set invalid
-    QString registration;
 
     auto registrationList = dbAircraft::retreiveRegistrationList();
     auto line_edit = ui->newAcft;
@@ -453,148 +460,132 @@ void NewFlight::on_newAcft_editingFinished()
 
 /// Pilot(s)
 
-void NewFlight::on_newPicNameLineEdit_inputRejected()
+/*!
+ * \brief NewFlight::addNewPilotMessageBox If the user input is not in the pilotNameList, the user
+ * is prompted if he wants to add a new entry to the database
+ */
+void NewFlight::addNewPilotMessageBox()
 {
-    onInputRejected(ui->newPicNameLineEdit, QRegularExpression(PILOT_NAME_INVALID_RGX));
+    QMessageBox::StandardButton reply;
+    reply = QMessageBox::question(this, "No Pilot found",
+                                  "No pilot found.\n Would you like to add a new pilot to the database?",
+                                  QMessageBox::Yes|QMessageBox::No);
+    if (reply == QMessageBox::Yes)
+    {
+        qDebug() << "Add new pilot selected";
+        DEBUG("This feature is not yet available.");
+        // create and open new pilot dialog
+    }
 }
 
-void NewFlight::on_newPicNameLineEdit_textEdited(const QString &arg1)
+void NewFlight::on_newPicNameLineEdit_inputRejected()
 {
-    if(arg1.length()>2)
-    {
-        QStringList picList = dbPilots::newPicGetString(arg1);
-        QCompleter *picCompleter = new QCompleter(picList, this);
-        picCompleter->setCaseSensitivity(Qt::CaseInsensitive);
-        picCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
-        ui->newPicNameLineEdit->setCompleter(picCompleter);
-     }
+    onInputRejected(ui->newPicNameLineEdit, QRegularExpression(PILOT_NAME_INVALID_RGX));
 }
+
 void NewFlight::on_newPicNameLineEdit_editingFinished()
 {
+    auto line_edit = ui->newPicNameLineEdit;
     pic = "-1"; // set invalid
-    if(ui->newPicNameLineEdit->text() == "self")
+    if(ui->newPicNameLineEdit->text() == "self") // Logbook owner is PIC
     {
-        pic = "1";
-    }else
+        pic = "self";
+        DEBUG("Pilot selected: " << pic);
+    }else //check if entry is in pilotList
     {
-        QString picname;
-        QStringList picList = dbPilots::newPicGetString(ui->newPicNameLineEdit->text());
-        qDebug() << picList;
-        if(picList.length()!= 0)
+        QStringList pilotList = dbPilots::retreivePilotList();
+        QStringList match = pilotList.filter(line_edit->text(), Qt::CaseInsensitive);
+
+        if(match.length()!= 0)
         {
-            picname = picList[0];
-            ui->newPicNameLineEdit->setText(picname);
-            pic = dbPilots::newPicGetId(picname);
+            pic = match[0];
+            line_edit->setText(pic);
+            DEBUG("Pilot selected: " << pic);
+            onEditingFinished(line_edit);
         }else
         {
-            QMessageBox::StandardButton reply;
-            reply = QMessageBox::question(this, "No Pilot found", "No pilot found.\n Would you like to add a new pilot to the database?",
-                                          QMessageBox::Yes|QMessageBox::No);
-            if (reply == QMessageBox::Yes)
-            {
-                qDebug() << "Add new pilot selected";
-                nope();
-            }
+            DEBUG("Pilot not found.");
+            emit line_edit->inputRejected();
+            addNewPilotMessageBox();
         }
     }
 }
 
 
 /*!
- * ===================================================
+ * ============================================================================
  * The above entris are mandatory for logging a flight,
  * the rest of the entries are either optional or can
  * be determined from the entries already made.
+ * ============================================================================
  */
-void NewFlight::on_secondPilotNameLineEdit_textEdited(const QString &arg1)
+
+void NewFlight::on_secondPilotNameLineEdit_inputRejected()
 {
-    if(arg1.length()>2)
-    {
-        QStringList picList = dbPilots::newPicGetString(arg1);
-        QCompleter *picCompleter = new QCompleter(picList, this);
-        picCompleter->setCaseSensitivity(Qt::CaseInsensitive);
-        picCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
-        ui->secondPilotNameLineEdit->setCompleter(picCompleter);
-     }
+    onInputRejected(ui->secondPilotNameLineEdit, QRegularExpression(PILOT_NAME_INVALID_RGX));
 }
 
 void NewFlight::on_secondPilotNameLineEdit_editingFinished()
 {
+    auto line_edit = ui->secondPilotNameLineEdit;
     secondPilot = "-1"; // set invalid
-    if(ui->secondPilotNameLineEdit->text() == "self")
+    if(ui->newPicNameLineEdit->text() == "self") // Logbook owner is PIC
     {
-        secondPilot = "1";
-    }else
+        secondPilot = "self";
+        DEBUG("Pilot selected: " << secondPilot);
+    }else //check if entry is in pilotList
     {
-        QString picname;
-        QStringList picList = dbPilots::newPicGetString(ui->secondPilotNameLineEdit->text());
-        qDebug() << picList;
-        if(picList.length()!= 0)
+        QStringList pilotList = dbPilots::retreivePilotList();
+        QStringList match = pilotList.filter(line_edit->text(), Qt::CaseInsensitive);
+
+        if(match.length()!= 0)
         {
-            picname = picList[0];
-            ui->secondPilotNameLineEdit->setText(picname);
-            secondPilot = dbPilots::newPicGetId(picname);
+            secondPilot = match[0];
+            line_edit->setText(secondPilot);
+            DEBUG("Pilot selected: " << secondPilot);
+            onEditingFinished(line_edit);
         }else
         {
-            ui->secondPilotNameLineEdit->setStyleSheet("border: 1px solid red");
-            /*QMessageBox::StandardButton reply;
-            reply = QMessageBox::question(this, "No Pilot found", "No pilot found.\n Would you like to add a new pilot to the database?",
-                                          QMessageBox::Yes|QMessageBox::No);
-            if (reply == QMessageBox::Yes)
-            {
-                qDebug() << "Add new pilot selected";
-                nope();
-            }*/
+            DEBUG("Pilot not found.");
+            emit line_edit->inputRejected();
+            addNewPilotMessageBox();
         }
     }
 }
 
-void NewFlight::on_thirdPilotNameLineEdit_textEdited(const QString &arg1)
+void NewFlight::on_thirdPilotNameLineEdit_inputRejected()
 {
-    if(arg1.length()>2)
-    {
-        QStringList picList = dbPilots::newPicGetString(arg1);
-        QCompleter *picCompleter = new QCompleter(picList, this);
-        picCompleter->setCaseSensitivity(Qt::CaseInsensitive);
-        picCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
-        ui->thirdPilotNameLineEdit->setCompleter(picCompleter);
-     }
+    onInputRejected(ui->thirdPilotNameLineEdit, QRegularExpression(PILOT_NAME_INVALID_RGX));
 }
 
 void NewFlight::on_thirdPilotNameLineEdit_editingFinished()
 {
+    auto line_edit = ui->thirdPilotNameLineEdit;
     thirdPilot = "-1"; // set invalid
-    if(ui->thirdPilotNameLineEdit->text() == "self")
+    if(ui->newPicNameLineEdit->text() == "self") // Logbook owner is PIC
     {
-        thirdPilot = "1";
-    }else
+        thirdPilot = "self";
+        DEBUG("Pilot selected: " << thirdPilot);
+    }else //check if entry is in pilotList
     {
-        QString picname;
-        QStringList picList = dbPilots::newPicGetString(ui->thirdPilotNameLineEdit->text());
-        qDebug() << picList;
-        if(picList.length()!= 0)
+        QStringList pilotList = dbPilots::retreivePilotList();
+        QStringList match = pilotList.filter(line_edit->text(), Qt::CaseInsensitive);
+
+        if(match.length()!= 0)
         {
-            picname = picList[0];
-            ui->thirdPilotNameLineEdit->setText(picname);
-            thirdPilot = dbPilots::newPicGetId(picname);
+            thirdPilot = match[0];
+            line_edit->setText(thirdPilot);
+            DEBUG("Pilot selected: " << thirdPilot);
+            onEditingFinished(line_edit);
         }else
         {
-            ui->thirdPilotNameLineEdit->setStyleSheet("border: 1px solid red");
-            /*QMessageBox::StandardButton reply;
-            reply = QMessageBox::question(this, "No Pilot found", "No pilot found.\n Would you like to add a new pilot to the database?",
-                                          QMessageBox::Yes|QMessageBox::No);
-            if (reply == QMessageBox::Yes)
-            {
-                qDebug() << "Add new pilot selected";
-                nope();
-            }*/
+            DEBUG("Pilot not found.");
+            emit line_edit->inputRejected();
+            addNewPilotMessageBox();
         }
     }
 }
 
-
-
-
 void NewFlight::on_FlightNumberLineEdit_editingFinished()
 {
     qDebug() << "tbd: FlightNumber slot";
@@ -603,10 +594,12 @@ void NewFlight::on_FlightNumberLineEdit_editingFinished()
 }
 
 /*
+ * ============================================================================
  * Extras Tab - These are for user convenience. From many of
  * these selections, determinations can be made on how to log
  * details, so that the user does not have to enter each item
  * manually. See also fillExtrasLineEdits()
+ * ============================================================================
  */
 
 void NewFlight::on_setAsDefaultButton_clicked()
@@ -1085,3 +1078,5 @@ void NewFlight::on_tabWidget_currentChanged(int index)
 }
 
 
+
+

+ 7 - 7
newflight.h

@@ -48,7 +48,7 @@ class NewFlight : public QDialog
 public:
     explicit NewFlight(QWidget *parent, QStringList locationList,
                                         QStringList registrationList,
-                                        QStringList pilotNameList);
+                                        QStringList pilotList);
     ~NewFlight();
 
 private slots:
@@ -56,6 +56,8 @@ private slots:
 
     void on_verifyButton_clicked(); //debug button
 
+    void addNewPilotMessageBox();
+
     QVector<QString> collectInput();
 
     void fillExtrasLineEdits();
@@ -92,8 +94,6 @@ private slots:
 
     void on_newPicNameLineEdit_inputRejected();
 
-    void on_newPicNameLineEdit_textEdited(const QString &arg1);
-
     void on_newPicNameLineEdit_editingFinished();
 
     void on_setAsDefaultButton_clicked();
@@ -134,12 +134,8 @@ private slots:
 
     void on_simTimeLineEdit_editingFinished();
 
-    void on_secondPilotNameLineEdit_textEdited(const QString &arg1);
-
     void on_secondPilotNameLineEdit_editingFinished();
 
-    void on_thirdPilotNameLineEdit_textEdited(const QString &arg1);
-
     void on_thirdPilotNameLineEdit_editingFinished();
 
     void on_FlightNumberLineEdit_editingFinished();
@@ -160,6 +156,10 @@ private slots:
 
 
 
+    void on_secondPilotNameLineEdit_inputRejected();
+
+    void on_thirdPilotNameLineEdit_inputRejected();
+
 private:
     Ui::NewFlight *ui;
 };