Browse Source

Reduced use of signals

Removed usage of signals and slots where not neccessary.

- Added database member variable lastError which replaces sqlError signal and holds error information.

- Logbookwidget, for (un)successful deletion now uses if statements

- NewTailDialog and NewPilotDialog now use if statements and boolean returns for accepting the dialog
Felix Turo 4 years ago
parent
commit
1e592b3477

+ 3 - 3
mainwindow.cpp

@@ -132,11 +132,11 @@ void MainWindow::on_actionDebug_triggered()
 
 void MainWindow::connectWidgets()
 {
-    QObject::connect(experimental::aDB(), &experimental::ADataBase::commitSuccessful,
+    QObject::connect(experimental::aDB(), &experimental::ADataBase::updated,
                      logbookWidget, &LogbookWidget::onDatabaseChanged);
-    QObject::connect(experimental::aDB(), &experimental::ADataBase::commitSuccessful,
+    QObject::connect(experimental::aDB(), &experimental::ADataBase::updated,
                      pilotsWidget, &PilotsWidget::onDatabaseChanged);
-    QObject::connect(experimental::aDB(), &experimental::ADataBase::commitSuccessful,
+    QObject::connect(experimental::aDB(), &experimental::ADataBase::updated,
                      aircraftWidget, &AircraftWidget::onDatabaseChanged);
 }
 

+ 34 - 19
src/experimental/adatabase.cpp

@@ -86,7 +86,8 @@ bool ADataBase::commit(AEntry entry)
 bool ADataBase::remove(AEntry entry)
 {
     if (!exists(entry)) {
-        DEB("Error: Entry does not exist.");
+        DEB("Error: Database entry not found.");
+        lastError = "Database Error: Database entry not found.";
         return false;
     }
 
@@ -97,13 +98,14 @@ bool ADataBase::remove(AEntry entry)
     if (query.lastError().type() == QSqlError::NoError)
     {
         DEB("Entry " << entry.getPosition().tableName << entry.getPosition().rowId << " removed.");
-        emit deleteSuccessful();
+        emit updated();
+        lastError = QString();
         return true;
     } else {
         DEB("Unable to delete.");
         DEB("Query: " << statement);
         DEB("Query Error: " << query.lastError().text());
-        emit sqlError(query.lastError(), statement);
+        lastError = query.lastError().text();
         return false;
     }
 }
@@ -117,7 +119,7 @@ bool ADataBase::removeMany(QList<DataPosition> data_position_list)
 
     for (const auto data_position : data_position_list) {
         if (!exists(data_position)) {
-            DEB("Error: Entry does not exist.");
+            lastError = "Database Error: Database entry not found.";
             errorCount++;
         }
         QString statement = "DELETE FROM " + data_position.first +
@@ -134,16 +136,17 @@ bool ADataBase::removeMany(QList<DataPosition> data_position_list)
         query.prepare("COMMIT");
         query.exec();
         if(query.lastError().type() == QSqlError::NoError) {
-            emit deleteSuccessful();
+            emit updated();
+            lastError = QString();
             return true;
         } else {
-            emit sqlError(query.lastError(), "Transaction unsuccessful. Error count: " + QString::number(errorCount));
+            lastError = "Transaction unsuccessful (Interrupted). Error count: " + QString::number(errorCount);
             return false;
         }
     } else {
         query.prepare("ROLLBACK");
         query.exec();
-        emit sqlError(query.lastError(), "Transaction unsuccessful(rolled back). Error count: " + QString::number(errorCount));
+        lastError = "Transaction unsuccessful (no changes have been made). Error count: " + QString::number(errorCount);
         return false;
     }
 }
@@ -162,8 +165,9 @@ bool ADataBase::exists(AEntry entry)
     query.exec();
     //this returns either 1 or 0 since row ids are unique
     if (!query.isActive()) {
-        emit sqlError(query.lastError(), statement);
+        lastError = query.lastError().text();
         DEB("Query Error: " << query.lastError().text() << statement);
+        return false;
     }
     query.next();
     int rowId = query.value(0).toInt();
@@ -171,7 +175,8 @@ bool ADataBase::exists(AEntry entry)
         DEB("Entry " << entry.getPosition() << " exists.");
         return true;
     } else {
-        DEB("Entry does not exist.");
+        DEB("Database entry not found.");
+        lastError = "Database Error: Database entry not found.";
         return false;
     }
 }
@@ -190,7 +195,7 @@ bool ADataBase::exists(DataPosition data_position)
     query.exec();
     //this returns either 1 or 0 since row ids are unique
     if (!query.isActive()) {
-        emit sqlError(query.lastError(), statement);
+        lastError = query.lastError().text();
         DEB("Query Error: " << query.lastError().text() << statement);
     }
     query.next();
@@ -200,6 +205,7 @@ bool ADataBase::exists(DataPosition data_position)
         return true;
     } else {
         DEB("No entry exists at DataPosition: " << data_position);
+        lastError = "Database Error: Database entry not found.";
         return false;
     }
 }
@@ -225,13 +231,14 @@ bool ADataBase::update(AEntry updated_entry)
     if (query.lastError().type() == QSqlError::NoError)
     {
         DEB("Entry successfully committed.");
-        emit commitSuccessful();
+        emit updated();
+        lastError = QString();
         return true;
     } else {
         DEB("Unable to commit.");
         DEB("Query: " << statement);
         DEB("Query Error: " << query.lastError().text());
-        emit sqlError(query.lastError(), statement);
+        lastError = query.lastError().text();
         return false;
     }
 }
@@ -263,13 +270,14 @@ bool ADataBase::insert(AEntry new_entry)
     if (query.lastError().type() == QSqlError::NoError)
     {
         DEB("Entry successfully committed.");
-        emit commitSuccessful();
+        emit updated();
+        lastError = QString();
         return true;
     } else {
         DEB("Unable to commit.");
         DEB("Query: " << statement);
         DEB("Query Error: " << query.lastError().text());
-        emit sqlError(query.lastError(), statement);
+        lastError = query.lastError().text();
         return false;
     }
 
@@ -294,12 +302,14 @@ TableData ADataBase::getEntryData(DataPosition data_position)
     if (check_query.lastError().type() != QSqlError::NoError) {
         DEB("SQL error: " << check_query.lastError().text());
         DEB("Statement: " << statement);
+        lastError = check_query.lastError().text();
         return TableData();
     }
 
     check_query.next();
     if (check_query.value(0).toInt() == 0) {
         DEB("No Entry found for row id: " << data_position.second );
+        lastError = "Database Error: Database entry not found.";
         return TableData();
     }
 
@@ -316,6 +326,7 @@ TableData ADataBase::getEntryData(DataPosition data_position)
     if (select_query.lastError().type() != QSqlError::NoError) {
         DEB("SQL error: " << select_query.lastError().text());
         DEB("Statement: " << statement);
+        lastError = select_query.lastError().text();
         return TableData();
     }
 
@@ -395,8 +406,10 @@ const QStringList ADataBase::getCompletionList(ADataBase::DatabaseTarget target)
     query.setForwardOnly(true);
     query.exec();
 
-    if(!query.isActive())
-        emit sqlError(query.lastError(), statement);
+    if(!query.isActive()) {
+        lastError = query.lastError().text();
+        return QStringList();
+    }
 
     QStringList completer_list;
     while (query.next())
@@ -445,7 +458,7 @@ const QMap<QString, int> ADataBase::getIdMap(ADataBase::DatabaseTarget target)
         DEB("No result found. Check Query and Error.");
         DEB("Query: " << statement);
         DEB("Error: " << query.lastError().text());
-        emit sqlError(query.lastError(), statement);
+        lastError = query.lastError().text();
         return QMap<QString, int>();
     } else {
         QVector<QString> query_result;
@@ -478,6 +491,7 @@ int ADataBase::getLastEntry(ADataBase::DatabaseTarget target)
     if (query.first()) {
         return query.value(0).toInt();
     } else {
+        lastError = "Database Error: Database entry not found.";
         DEB("No entry found.");
         return 0;
     }
@@ -492,7 +506,7 @@ QVector<QString> ADataBase::customQuery(QString statement, int return_values)
         DEB("No result found. Check Query and Error.");
         DEB("Error: " << query.lastError().text());
         DEB("Statement: " << statement);
-        emit sqlError(query.lastError(), statement);
+        lastError = query.lastError().text();
         return QVector<QString>();
     } else {
         query.first();
@@ -503,7 +517,8 @@ QVector<QString> ADataBase::customQuery(QString statement, int return_values)
                 result.append(query.value(i).toString());
             }
         }
-        emit commitSuccessful();
+        emit updated();
+        lastError = QString();
         return result;
     }
 }

+ 9 - 6
src/experimental/adatabase.h

@@ -51,6 +51,7 @@ public:
     ADataBase(const ADataBase&) = delete;
     void operator=(const ADataBase&) = delete;
     static ADataBase* getInstance();
+    QString lastError;
 
     /*!
      * \brief The CompleterTarget enum provides the items for which QCompleter
@@ -179,13 +180,15 @@ public:
     const QMap<QString, int> getIdMap(DatabaseTarget);
 
     int getLastEntry(DatabaseTarget);
-signals:
-    void commitSuccessful();
-
-    void deleteSuccessful();
-
-    void sqlError(const QSqlError &sqlError, const QString &sqlStatement);
 
+signals:
+    /*!
+     * \brief updated is emitted whenever the database contents have been updated.
+     * This can be either a commit, update or remove. This signal should be used to
+     * trigger an update to the models of the views displaying database contents in
+     * the user interface so that a user is always presented with up-to-date information.
+     */
+    void updated();
 };
 
 /*!

+ 6 - 3
src/gui/dialogues/newflightdialog.cpp

@@ -866,13 +866,16 @@ void NewFlightDialog::on_submitButton_clicked()
     DEB("Committing...");
     if (!aDB()->commit(flightEntry)) {
         auto message_box = QMessageBox(this);
-        message_box.setText("An error has ocurred. Your entry has not been saved.");
+        message_box.setText("The following error has ocurred:\n\n"
+                            + aDB()->lastError
+                            + "\n\nYour entry has not been saved.");
         message_box.setIcon(QMessageBox::Warning);
         message_box.exec();
         return;
-        /// [F] To do: get error info and display here.
+    } else {
+        QDialog::accept();
     }
-    QDialog::accept();
+
 }
 
 

+ 10 - 27
src/gui/dialogues/newpilotdialog.cpp

@@ -112,17 +112,6 @@ void NewPilotDialog::setup()
     completer->setCompletionMode(QCompleter::InlineCompletion);
     completer->setCaseSensitivity(Qt::CaseSensitive);
     ui->companyLineEdit->setCompleter(completer);
-
-    ///[F] moved connecting the slots here because
-    /// - no need to declare the slots public as would be the case if connected in mainwindow
-    /// - only one place where slots are connected vs. several places (mainwindow, pilotswidget),
-    ///   makes it easier to maintain.
-    /// - these signals and slots are specific to this dialog, for communication with
-    ///   other widgets we have the QDialog::accepted() and QDialog::rejected signals.
-    QObject::connect(aDB(), &ADataBase::commitSuccessful,
-                     this, &NewPilotDialog::onCommitSuccessful);
-    QObject::connect(aDB(), &ADataBase::sqlError,
-                     this, &NewPilotDialog::onCommitUnsuccessful);
 }
 
 void NewPilotDialog::on_buttonBox_accepted()
@@ -136,21 +125,6 @@ void NewPilotDialog::on_buttonBox_accepted()
     }
 }
 
-void NewPilotDialog::onCommitSuccessful()
-{
-    accept();
-}
-
-void NewPilotDialog::onCommitUnsuccessful(const QSqlError &sqlError, const QString &)
-{
-    auto mb = QMessageBox(this);
-    mb.setIcon(QMessageBox::Critical);
-    mb.setText("The following error has ocurred.\n\n"
-               + sqlError.text()
-               + "\n\nYour entry has not been saved.");
-    mb.exec();
-}
-
 void NewPilotDialog::formFiller()
 {
     DEB("Filling Form...");
@@ -178,5 +152,14 @@ void NewPilotDialog::submitForm()
     pilotEntry.setData(new_data);
     DEB("Pilot entry position: " << pilotEntry.getPosition());
     DEB("Pilot entry data: " << pilotEntry.getData());
-    aDB()->commit(pilotEntry);
+    if (!aDB()->commit(pilotEntry)) {
+        auto message_box = QMessageBox(this);
+        message_box.setText("The following error has ocurred:\n\n"
+                            + aDB()->lastError
+                            + "\n\nThe entry has not been saved.");
+        message_box.exec();
+        return;
+    } else {
+        QDialog::accept();
+    }
 }

+ 0 - 4
src/gui/dialogues/newpilotdialog.h

@@ -44,10 +44,6 @@ public:
 
 private slots:
     void on_buttonBox_accepted();
-
-    void onCommitSuccessful();
-
-    void onCommitUnsuccessful(const QSqlError &sqlError, const QString &);
 private:
     Ui::NewPilot *ui;
 

+ 12 - 21
src/gui/dialogues/newtaildialog.cpp

@@ -37,7 +37,6 @@ NewTailDialog::NewTailDialog(QString new_registration, QWidget *parent) :
     DEB("new NewTailDialog (experimental)");
     ui->setupUi(this);
 
-    connectSignals();
     setupCompleter();
     setupValidators();
 
@@ -60,7 +59,6 @@ NewTailDialog::NewTailDialog(int row_id, QWidget *parent) :
     ui->searchLineEdit->hide();
     ui->line->hide();
 
-    connectSignals();
     setupValidators();
     entry = aDB()->getTailEntry(row_id);
     fillForm(entry, false);
@@ -108,15 +106,6 @@ void NewTailDialog::setupValidators()
     }
 }
 
-void NewTailDialog::connectSignals()
-{
-    using namespace experimental;
-    QObject::connect(aDB(), &ADataBase::commitSuccessful,
-                     this,  &NewTailDialog::onCommitSuccessful);
-    QObject::connect(aDB(), &ADataBase::sqlError,
-                     this,  &NewTailDialog::onCommitUnsuccessful);
-}
-
 /*!
  * \brief NewTailDialog::fillForm populates the Dialog with the
  * information contained in an entry object. This can be either
@@ -239,7 +228,16 @@ void NewTailDialog::submitForm()
     //create db object
 
     entry.setData(new_data);
-    aDB()->commit(entry);
+    if (!aDB()->commit(entry)) {
+        auto message_box = QMessageBox(this);
+        message_box.setText("The following error has ocurred:\n\n"
+                            + aDB()->lastError
+                            + "\n\nThe entry has not been saved.");
+        message_box.exec();
+        return;
+    } else {
+        QDialog::accept();
+    }
 }
 
 /// Slots
@@ -332,18 +330,11 @@ void NewTailDialog::on_registrationLineEdit_textChanged(const QString &arg1)
     ui->registrationLineEdit->setText(arg1.toUpper());
 }
 
-void NewTailDialog::onCommitSuccessful()
-{
-    ACalc::updateAutoTimes(entry.getPosition().second); // To do: update to use new db architecture with new ATailEntry
-    accept();
-}
-
-void NewTailDialog::onCommitUnsuccessful(const QSqlError &sqlError, const QString &)
-{
+/*
     auto mb = QMessageBox(this);
     mb.setIcon(QMessageBox::Critical);
     mb.setText("The following error has ocurred.\n\n"
                + sqlError.text()
                + "\n\nYour entry has not been saved.");
     mb.exec();
-}
+*/

+ 0 - 5
src/gui/dialogues/newtaildialog.h

@@ -67,8 +67,6 @@ private:
 
     void setupValidators();
 
-    void connectSignals();
-
     void fillForm(experimental::AEntry entry, bool is_template);
 
     void submitForm();
@@ -91,9 +89,6 @@ private slots:
 
     void onSearchCompleterActivated();
 
-    void onCommitSuccessful();
-
-    void onCommitUnsuccessful(const QSqlError &sqlError, const QString &);
 };
 
 #endif // NEWTAIL_H

+ 20 - 21
src/gui/widgets/logbookwidget.cpp

@@ -19,6 +19,8 @@
 #include "ui_logbookwidget.h"
 #include "src/testing/adebug.h"
 
+using namespace experimental;
+
 const QMap<int, QString> FILTER_MAP = {
     {0, "Date LIKE \"%"},
     {1, "Dept LIKE \"%"},
@@ -75,11 +77,6 @@ void LogbookWidget::connectSignalsAndSlots()
     selection = view->selectionModel();
     QObject::connect(view->selectionModel(), &QItemSelectionModel::selectionChanged,
                      this, &LogbookWidget::flightsTableView_selectionChanged);
-    using namespace experimental;
-    QObject::connect(aDB(), &ADataBase::deleteSuccessful,
-                     this, &LogbookWidget::onDeletedSuccessfully);
-    QObject::connect(aDB(), &ADataBase::sqlError,
-                     this, &LogbookWidget::onDeleteUnsuccessful);
 }
 
 void LogbookWidget::setupDefaultView()
@@ -230,8 +227,14 @@ void LogbookWidget::on_deleteFlightPushButton_clicked()
         if (reply == QMessageBox::Yes) {
             for (auto& flight : flights_list) {
                 DEB("Deleting flight: " << flight.summary());
-                experimental::aDB()->remove(flight);
+                if(!aDB()->remove(flight)) {
+                    messageBox->setText(" Error "); // [F]: To Do: error info
+                    messageBox->exec();
+                    return;
+                }
             }
+            messageBox->setText(QString::number(selectedFlights.length()) + " flights have been deleted successfully.");
+            messageBox->exec();
             displayModel->select();
         }
     } else if (selectedFlights.length() > 10) {
@@ -246,16 +249,25 @@ void LogbookWidget::on_deleteFlightPushButton_clicked()
         confirm.setText(warningMsg);
         int reply = confirm.exec();
         if(reply == QMessageBox::Yes) {
-            QList<experimental::DataPosition> selected_flights;
+            QList<DataPosition> selected_flights;
             for (const auto& flight_id : selectedFlights) {
                 selected_flights.append({"flights", flight_id});
             }
-            experimental::aDB()->removeMany(selected_flights);
+            if (!aDB()->removeMany(selected_flights)) {
+
+                messageBox->setText(" Error "); // [F]: To Do: error info
+                messageBox->exec();
+                return;
+            }
+            messageBox->setText(QString::number(selectedFlights.length()) + " flights have been deleted successfully.");
+            messageBox->exec();
             displayModel->select();
         }
+        displayModel->select();
     }
 }
 
+
 void LogbookWidget::on_tableView_customContextMenuRequested(const QPoint &pos)
 {
     menu->popup(ui->tableView->viewport()->mapToGlobal(pos));
@@ -266,19 +278,6 @@ void LogbookWidget::on_actionDelete_Flight_triggered()
     emit ui->deleteFlightPushButton->clicked();
 }
 
-void LogbookWidget::onDeletedSuccessfully()
-{
-    messageBox->setText(QString::number(selectedFlights.length()) + " entries have been deleted.");
-    messageBox->exec();
-}
-
-void LogbookWidget::onDeleteUnsuccessful(const QSqlError error)
-{
-    messageBox->setText("Error deleting " + QString::number(selectedFlights.length())
-                        + " flights.\n\nThe following error has ocurred:\n\n" + error.text());
-    messageBox->exec();
-}
-
 void LogbookWidget::on_actionEdit_Flight_triggered()
 {
     emit ui->editFlightButton->clicked();

+ 0 - 3
src/gui/widgets/logbookwidget.h

@@ -60,9 +60,6 @@ private slots:
 
     void on_actionDelete_Flight_triggered();
 
-    void onDeletedSuccessfully();
-    void onDeleteUnsuccessful(const QSqlError);
-
     void on_actionEdit_Flight_triggered();
 
     void on_tableView_doubleClicked();