Browse Source

Implemented logbook refresh suggestion from fifty-50

Georgios Kotzampopoulos 4 years ago
parent
commit
18f068ac38

+ 3 - 0
mainwindow.cpp

@@ -126,6 +126,9 @@ void MainWindow::connectWidgets()
                      pilotsWidget,   &PilotsWidget::onPilotsWidget_databaseUpdated);
     QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
                      pilotsWidget,   &PilotsWidget::onPilotsWidget_settingChanged);
+
+    QObject::connect(aDB,             &ADatabase::connectionReset,
+                     logbookWidget,   &LogbookWidget::repopulateModel);
 }
 
 /*

+ 3 - 0
src/database/adatabase.cpp

@@ -734,6 +734,7 @@ bool ADatabase::createBackup(const QString& dest_file)
 
     INFO << "Backed up old database as:" << dest_file;
     ADatabase::connect();
+    emit connectionReset();
     return true;
 }
 
@@ -768,5 +769,7 @@ bool ADatabase::restoreBackup(const QString& backup_file)
 
     INFO << "Backup successfully restored!";
     ADatabase::connect();
+    emit connectionReset();
     return true;
 }
+

+ 5 - 0
src/database/adatabase.h

@@ -299,6 +299,11 @@ signals:
      * the user interface so that a user is always presented with up-to-date information.
      */
     void dataBaseUpdated();
+    /*!
+     * \brief connectionReset is emitted whenever the database connection is reset, for
+     * example when creating or restoring a backup.
+     */
+    void connectionReset();
 };
 
 #endif // ADATABASE_H

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

@@ -285,3 +285,13 @@ void LogbookWidget::on_flightSearchLlineEdit_textChanged(const QString &arg1)
         return;
     }
 }
+
+void LogbookWidget::repopulateModel()
+{
+    // unset the current model and delete it to avoid leak
+    view->setModel(nullptr);
+    delete displayModel;
+    // create a new model and populate it
+    displayModel = new QSqlTableModel(this);
+    setupModelAndView(ASettings::read(ASettings::Main::LogbookView).toInt());
+}

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

@@ -55,6 +55,11 @@ private slots:
 public slots:
     void refresh();
     void onLogbookWidget_viewSelectionChanged(SettingsWidget::SettingSignal signal);
+    /*!
+     * \brief LogbookWidget::repopulateModel (public slot) - re-populates the model to cater for a change
+     * to the database connection (for example, when a backup is created)
+     */
+    void repopulateModel();
 
 private:
     Ui::LogbookWidget *ui;