Browse Source

Refactored debugwidget

Now makes use of standardpaths, no more need to restart application after database changes.
Felix 4 years ago
parent
commit
7cc6f1d19a
2 changed files with 62 additions and 51 deletions
  1. 60 51
      src/gui/widgets/debugwidget.cpp
  2. 2 0
      src/gui/widgets/debugwidget.h

+ 60 - 51
src/gui/widgets/debugwidget.cpp

@@ -1,6 +1,9 @@
 #include "debugwidget.h"
 #include "ui_debugwidget.h"
 #include "src/classes/astandardpaths.h"
+#include "src/gui/widgets/logbookwidget.h"
+#include "src/gui/widgets/pilotswidget.h"
+#include "src/gui/widgets/aircraftwidget.h"
 
 DebugWidget::DebugWidget(QWidget *parent) :
     QWidget(parent),
@@ -24,10 +27,9 @@ void DebugWidget::on_resetUserTablesPushButton_clicked()
     ATimer timer(this);
     QMessageBox result;
     if (ADataBaseSetup::resetToDefault()){
-        result.setText("Database successfully reset.\n\nRestarting app.");
+        result.setText("Database successfully reset");
         result.exec();
-        qApp->quit();
-        QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
+        touchDatabase();
     } else {
         result.setText("Errors have occurred. Check console for Debug output. ");
         result.exec();
@@ -37,52 +39,47 @@ void DebugWidget::on_resetUserTablesPushButton_clicked()
 void DebugWidget::on_resetDatabasePushButton_clicked()
 {
     ATimer timer(this);
-    QMessageBox mb(this);
-    //check if template dir exists and create if needed.
-    QDir dir("data/templates");
-    DEB << dir.path();
-    if (!dir.exists())
-        dir.mkpath(".");
+    QMessageBox message_box(this);
+
     // download latest csv
-    QStringList templateTables = {"aircraft", "airports", "changelog"};
-    QString linkStub = "https://raw.githubusercontent.com/fiffty-50/openpilotlog/";
-    linkStub.append(ui->branchLineEdit->text());
-    linkStub.append("/assets/database/templates/");
-    for (const auto& table : templateTables) {
+    QString link_stub = "https://raw.githubusercontent.com/fiffty-50/openpilotlog/";
+    link_stub.append(ui->branchLineEdit->text()); // optionally select branch for development
+    link_stub.append("/assets/database/templates/");
+
+    QStringList template_tables = {"aircraft", "airports", "changelog"};
+    QDir template_dir(AStandardPaths::absPathOf(AStandardPaths::Templates));
+    for (const auto& table : template_tables) {
         QEventLoop loop;
         ADownload* dl = new ADownload;
-        connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
-        dl->setTarget(QUrl(linkStub + table + ".csv"));
-        dl->setFileName("data/templates/" + table + ".csv");
+        QObject::connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
+        dl->setTarget(QUrl(link_stub % table % QStringLiteral(".csv")));
+        dl->setFileName(template_dir.filePath(table % QStringLiteral(".csv")));
         dl->download();
         loop.exec(); // event loop waits for download done signal before allowing loop to continue
         dl->deleteLater();
     }
 
-    //close database connection
+    // back up old db
     aDB()->disconnect();
+    ADataBaseSetup::backupOldData();
 
-    // back up and remove old database
-    auto oldDatabase = QFile("data/logbook.db");
-    if (oldDatabase.exists()) {
-        auto dateString = QDateTime::currentDateTime().toString(Qt::ISODate);
-        DEB << "Backing up old database as: " << "logbook-backup-" + dateString + ".db";
-        if (oldDatabase.copy("data/logbook-backup-" + dateString + ".db")) {
-            oldDatabase.remove();
-            DEB << "Old Database removed.";
-        }
-
-    }
     // re-connct and create new database
     aDB()->connect();
-
     if (ADataBaseSetup::createDatabase()) {
-        mb.setText("Database has been successfully reset.\n\nRestarting application.");
-        mb.exec();
-        qApp->quit();
-        QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
+        DEB << "Database has been successfully created.";
+    } else {
+        message_box.setText("Errors have ocurred creating the database.<br>"
+                            "Check console for details.");
+        message_box.exec();
+    }
+    if (ADataBaseSetup::importDefaultData()) {
+        message_box.setText("Database has been successfully reset.");
+        touchDatabase();
+        message_box.exec();
     } else {
-        mb.setText("Errors have ocurred. Check console for details.");
+        message_box.setText("Errors have ocurred while importing templates.<br>"
+                            "Check console for details.");
+        message_box.exec();
     }
 }
 
@@ -94,24 +91,21 @@ void DebugWidget::downloadFinished()
 void DebugWidget::on_fillUserDataPushButton_clicked()
 {
     ATimer timer(this);
-    QMessageBox mb(this);
-    //check if template dir exists and create if needed.
-    QDir dir("data/templates");
-    DEB << dir.path();
-    if (!dir.exists())
-        dir.mkpath(".");
+    QMessageBox message_box(this);
     // download latest csv
     QStringList userTables = {"pilots", "tails", "flights"};
     QString linkStub = "https://raw.githubusercontent.com/fiffty-50/openpilotlog/";
     linkStub.append(ui->branchLineEdit->text());
     linkStub.append("/assets/database/templates/sample_");
+    QDir template_dir(AStandardPaths::absPathOf(AStandardPaths::Templates));
 
     for (const auto& table : userTables) {
         QEventLoop loop;
         ADownload* dl = new ADownload;
         connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
         dl->setTarget(QUrl(linkStub + table + ".csv"));
-        dl->setFileName("data/templates/sample_" + table + ".csv");
+        dl->setFileName(template_dir.filePath("sample_" + table % QStringLiteral(".csv")));
+        //dl->setFileName("data/templates/sample_" + table + ".csv");
         dl->download();
         loop.exec(); // event loop waits for download done signal before allowing loop to continue
         dl->deleteLater();
@@ -120,26 +114,28 @@ void DebugWidget::on_fillUserDataPushButton_clicked()
     allGood.resize(userTables.size());
 
     for (const auto& table : userTables) {
-        auto data = aReadCsv("data/templates/sample_" + table + ".csv");
+        auto data = aReadCsv(AStandardPaths::absPathOf(AStandardPaths::Templates)
+                             + "/sample_" + table + ".csv");
         allGood.setBit(userTables.indexOf(table), ADataBaseSetup::commitData(data, table));
     }
 
     if (allGood.count(true) != userTables.size()) {
-        mb.setText("Errors have ocurred. Check console for details.");
-        mb.exec();
+        message_box.setText("Errors have ocurred. Check console for details.");
+        message_box.exec();
         return;
     }
 
-    mb.setText("User tables successfully populated.\n\nRestarting app.");
-    mb.exec();
-    qApp->quit();
-    QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
+    message_box.setText("User tables successfully populated.");
+    message_box.exec();
+    touchDatabase();
 }
 
 void DebugWidget::on_selectCsvPushButton_clicked()
 {
     auto fileName = QFileDialog::getOpenFileName(this,
-                                                 tr("Open CSV File for import"), QDir::homePath(), tr("CSV files (*.csv)"));
+                                                 tr("Open CSV File for import"),
+                                                 AStandardPaths::absPathOf(AStandardPaths::Templates),
+                                                 tr("CSV files (*.csv)"));
     ui->importCsvLineEdit->setText(fileName);
 }
 
@@ -169,7 +165,20 @@ void DebugWidget::on_importCsvPushButton_clicked()
 
 void DebugWidget::on_debugPushButton_clicked()
 {
-    DEB << AStandardPaths::allPaths()[AStandardPaths::Database	];
+    touchDatabase();
+}
+
+/*!
+ * \brief Acess the database to trigger aDB()::databaseUpdated
+ */
+void DebugWidget::touchDatabase()
+{
+    QMap<QString, QVariant> debugData;
+    debugData.insert("lastname","debugLastName");
+    debugData.insert("firstname","debugFirstName");
+    auto pilot = APilotEntry(1);
+    pilot.setData(debugData);
+    aDB()->commit(pilot);
 }
 
 /* //Comparing two functions template

+ 2 - 0
src/gui/widgets/debugwidget.h

@@ -51,6 +51,8 @@ private:
     Ui::DebugWidget *ui;
 
     bool downloadComplete = false;
+
+    void touchDatabase();
 };
 
 #endif // DEBUGWIDGET_H