Browse Source

Updating of QCompleters

QCompleters in NF Dialog get updated on DB change
Fixed a bug in ADatabase::remove
added init method to ACompletionData instead of retreiving data on Construction to avoid connection attempts to DB before connection is established
Felix Turo 3 years ago
parent
commit
d5fbc2c089

+ 1 - 1
mainwindow.cpp

@@ -37,7 +37,7 @@ MainWindow::MainWindow(QWidget *parent)
     }
 
     // retreive completion lists and maps
-    completionData = ACompletionData();
+    completionData.init();
 
     // Create a spacer for the toolbar to separate left and right parts
     auto *spacer = new QWidget();

+ 1 - 2
src/classes/acompletiondata.cpp

@@ -1,6 +1,6 @@
 #include "acompletiondata.h"
 
-ACompletionData::ACompletionData()
+void ACompletionData::init()
 {
     // retreive user modifiable data
     pilotList   = aDB->getCompletionList(ADatabaseTarget::pilots);
@@ -13,7 +13,6 @@ ACompletionData::ACompletionData()
     airportIataIdMap = aDB->getIdMap(ADatabaseTarget::airport_identifier_iata);
     airportNameIdMap = aDB->getIdMap(ADatabaseTarget::airport_names);
     airportList      = aDB->getCompletionList(ADatabaseTarget::airport_identifier_all);
-
 }
 
 void ACompletionData::update()

+ 5 - 2
src/classes/acompletiondata.h

@@ -10,8 +10,11 @@
  */
 class ACompletionData
 {
-public:
-    ACompletionData();
+public:  
+    /*!
+     * \brief init Retrieves Data and populates Lists and Maps
+     */
+    void init();
 
     /*!
      * \brief updates data from the user modifiable tables

+ 1 - 1
src/database/adatabase.cpp

@@ -213,7 +213,7 @@ bool ADatabase::remove(AEntry entry)
     query.prepare(statement);
     query.addBindValue(entry.getPosition().rowId);
 
-    if (!query.exec())
+    if (query.exec())
     {
         DEB << "Entry " << entry.getPosition() << " removed.";
         emit dataBaseUpdated();

+ 36 - 18
src/gui/dialogues/newflightdialog.cpp

@@ -164,6 +164,7 @@ NewFlightDialog::NewFlightDialog(ACompletionData &completion_data,
 
 NewFlightDialog::~NewFlightDialog()
 {
+    LOG << "Closing NF Dialog";
     delete ui;
 }
 
@@ -268,7 +269,7 @@ void NewFlightDialog::setupRawInputValidation()
     for (const auto &item : line_edit_settings) {
         for (const auto &line_edit : line_edits) {
             if(line_edit->objectName().contains(std::get<0>(item))) {
-                DEB << "Setting up: " << line_edit->objectName();
+                // DEB << "Setting up: " << line_edit->objectName();
                 // Set Validator
                 auto validator = new QRegularExpressionValidator(std::get<2>(item), line_edit);
                 line_edit->setValidator(validator);
@@ -348,7 +349,7 @@ bool NewFlightDialog::eventFilter(QObject* object, QEvent* event)
     if (line_edit != nullptr) {
         if (mandatoryLineEdits.contains(line_edit) && event->type() == QEvent::FocusIn) {
             mandatoryLineEdits.unvalidate(line_edit);
-            DEB << "Editing " << line_edit->objectName();
+            //DEB << "Editing " << line_edit->objectName();
             // set verification bit to false when entering a mandatory line edit
             return false;
         }
@@ -356,7 +357,7 @@ bool NewFlightDialog::eventFilter(QObject* object, QEvent* event)
             // show completion menu when pressing down arrow
             QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
             if (keyEvent->key() == Qt::Key_Down) {
-                DEB << "Key down event.";
+                //DEB << "Key down event.";
                 line_edit->completer()->complete();
             }
             return false;
@@ -365,7 +366,7 @@ bool NewFlightDialog::eventFilter(QObject* object, QEvent* event)
             // show completion menu when pressing down arrow
             QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
             if (keyEvent->key() == Qt::Key_Down) {
-                DEB << "Key down event.";
+                //DEB << "Key down event.";
                 line_edit->completer()->complete();
             }
             return false;
@@ -482,7 +483,7 @@ void NewFlightDialog::fillDeductibleData()
 RowData_T NewFlightDialog::collectInput()
 {
     RowData_T newData;
-    DEB << "Collecting Input...";
+    //DEB << "Collecting Input...";
     //Block Time
     const auto tofb = ATime::fromString(ui->tofbTimeLineEdit->text());
     const auto tonb = ATime::fromString(ui->tonbTimeLineEdit->text());
@@ -795,20 +796,28 @@ void NewFlightDialog::addNewTail(QLineEdit *parent_line_edit)
                                      "<br><br>Would you like to add a new aircraft to the database?"),
                                   QMessageBox::Yes|QMessageBox::No);
     if (reply == QMessageBox::Yes) {
-        DEB << "Add new aircraft selected";
         // create and open new aircraft dialog
         NewTailDialog na(ui->acftLineEdit->text(), this);
-        na.exec();
+        int ret = na.exec();
         // update map and list, set line edit
-        completionData.updateTails();
+        if (ret == QDialog::Accepted) {
+            DEB << "New Tail Entry added. Id:" << aDB->getLastEntry(ADatabaseTable::tails);
 
-        DEB << "New Entry added. Id:" << aDB->getLastEntry(ADatabaseTable::tails);
-        DEB << "AC Map: " << completionData.tailsIdMap;
+            // update completion Data and Completer
+            completionData.updateTails();
+            auto new_model = new QStringListModel(completionData.tailsList, parent_line_edit->completer());
+            parent_line_edit->completer()->setModel(new_model); //setModel deletes old model if it has the completer as parent
 
-        parent_line_edit->setText(completionData.tailsIdMap.key(aDB->getLastEntry(ADatabaseTable::tails)));
-        emit parent_line_edit->editingFinished();
+            // update Line Edit
+            parent_line_edit->setText(completionData.tailsIdMap.key(aDB->getLastEntry(ADatabaseTable::tails)));
+            emit parent_line_edit->editingFinished();
+        } else {
+            parent_line_edit->setText(QString());
+            parent_line_edit->setFocus();
+        }
     } else {
         parent_line_edit->setText(QString());
+        parent_line_edit->setFocus();
     }
 }
 
@@ -827,15 +836,24 @@ void NewFlightDialog::addNewPilot(QLineEdit *parent_line_edit)
                                      "<br><br>Would you like to add a new pilot to the database?"),
                                   QMessageBox::Yes|QMessageBox::No);
     if (reply == QMessageBox::Yes) {
-        DEB << "Add new pilot selected";
         // create and open new pilot dialog
         NewPilotDialog np(this);
-        np.exec();
+        int ret = np.exec();
         // update map and list, set line edit
-        completionData.updatePilots();
-        DEB << "Setting new entry: " << completionData.pilotsIdMap.key(aDB->getLastEntry(ADatabaseTable::pilots));
-        parent_line_edit->setText(completionData.pilotsIdMap.key(aDB->getLastEntry(ADatabaseTable::pilots)));
-        emit parent_line_edit->editingFinished();
+        if (ret == QDialog::Accepted) {
+            DEB << "New Pilot Entry added. Id:" << aDB->getLastEntry(ADatabaseTable::pilots);
+            // update completion Data and Completer
+            completionData.updatePilots();
+            auto new_model = new QStringListModel(completionData.pilotList, parent_line_edit->completer());
+            parent_line_edit->completer()->setModel(new_model); //setModel deletes old model if it has the completer as parent
+
+            // update Line Edit
+            parent_line_edit->setText(completionData.pilotsIdMap.key(aDB->getLastEntry(ADatabaseTable::pilots)));
+            emit parent_line_edit->editingFinished();
+        } else {
+            parent_line_edit->setText(QString());
+            parent_line_edit->setFocus();
+        }
     } else {
         parent_line_edit->setText(QString());
     }

+ 0 - 2
src/gui/widgets/logbookwidget.cpp

@@ -140,7 +140,6 @@ void LogbookWidget::on_newFlightButton_clicked()
     auto old_state = aDB->getUserDataState();
 
     NewFlightDialog nf(completionData, this);
-    nf.setAttribute(Qt::WA_DeleteOnClose);
     nf.exec();
 
     auto new_state = aDB->getUserDataState();
@@ -160,7 +159,6 @@ void LogbookWidget::on_editFlightButton_clicked()
         auto old_state = aDB->getUserDataState();
 
         auto ef = new NewFlightDialog(completionData, selectedFlights.first(), this);
-        ef->setAttribute(Qt::WA_DeleteOnClose);
         ef->exec();
 
         auto new_state = aDB->getUserDataState();