Browse Source

New DataBase functions and NewPilotDialog edited

DataBase has the following new functions:
database - return the database currently in use
disconnect - disconnect the database
getEntryData(Position)
getEntry(DataPosition)
getPilotEntry(RowId)

The NewPilot Dialog is refactored to make use of the new experimental classes.
Felix Turo 4 years ago
parent
commit
bb4b2f0cb7

+ 2 - 0
main.cpp

@@ -22,6 +22,7 @@
 #include <QProcess>
 #include <QSettings>
 #include <QFileInfo>
+#include "src/experimental/DataBase.h"
 
 const auto DATA_DIR = QLatin1String("data");
 /*!
@@ -51,6 +52,7 @@ int main(int argc, char *argv[])
     QSettings settings;
 
 //    Db::connect();
+    experimental::DB()->connect();
 
     QApplication openPilotLog(argc, argv);
     if(!setup()){

+ 31 - 2
src/experimental/DataBase.cpp

@@ -42,6 +42,20 @@ bool DataBase::connect()
     return true;
 }
 
+void DataBase::disconnect()
+{
+    auto db = DataBase::database();
+    db.close();
+    db.removeDatabase(db.connectionName());
+    DEB("Database connection closed.");
+}
+
+QSqlDatabase DataBase::database()
+{
+    auto db = QSqlDatabase::database("qt_sql_default_connection");
+    return db;
+}
+
 bool DataBase::commit(Entry entry)
 {
     if (exists(entry)) {
@@ -87,7 +101,7 @@ bool DataBase::exists(Entry entry)
     q.next();
     int rowId = q.value(0).toInt();
     if (rowId) {
-        DEB("Entry exists with row ID: " << rowId);
+        DEB("Entry " << entry.position << " exists.");
         return true;
     } else {
         DEB("Entry does not exist.");
@@ -207,7 +221,8 @@ TableData DataBase::getEntryData(DataPosition dataPosition)
     return entryData;
 }
 
-TableData DataBase::getEntryDataNew(DataPosition dataPosition)
+/// does the same as geteEntryData, but slower
+/*TableData DataBase::getEntryDataQsqlTableModel(DataPosition dataPosition)
 {
     // check table exists
     if (!tableNames.contains(dataPosition.first)) {
@@ -236,6 +251,20 @@ TableData DataBase::getEntryDataNew(DataPosition dataPosition)
         entryData.insert(column, model.record(0).value(column).toString());
     }
     return entryData;
+}*/
+
+Entry DataBase::getEntry(DataPosition dataPosition)
+{
+    Entry entry(dataPosition);
+    entry.setData(DataBase::getEntryData(dataPosition));
+    return entry;
+}
+
+PilotEntry DataBase::getPilotEntry(RowId rowId)
+{
+    PilotEntry pilotEntry(rowId);
+    pilotEntry.setData(DataBase::getEntryData(pilotEntry.position));
+    return pilotEntry;
 }
 
 DataBase* DB() { return DataBase::getInstance(); }

+ 23 - 7
src/experimental/DataBase.h

@@ -14,15 +14,10 @@
 
 namespace experimental {
 
-// [F] ideas for functions of db class:
-// https://github.com/fiffty-50/openpilotlog/wiki/New-DB-class-brainstorming
-
 /*!
  * \brief The DB class encapsulates the SQL database by providing fast access
  * to hot database data.
  */
-
-
 class DataBase {
 private:
     TableNames tableNames;
@@ -43,7 +38,13 @@ public:
     /*!
      * \brief closes the database connection.
      */
-    bool disconnect();
+    void disconnect();
+
+    /*!
+     * \brief Can be used to access the database connection.
+     * \return The QSqlDatabase object pertaining to the connection.
+     */
+    QSqlDatabase database();
 
     /*!
      * \brief Checks if an entry exists in the database, based on position data
@@ -76,7 +77,22 @@ public:
      */
     TableData getEntryData(DataPosition);
 
-    TableData getEntryDataNew(DataPosition);
+    /*!
+     * \brief retreive an Entry from the database.
+     */
+    Entry getEntry(DataPosition);
+
+    /*!
+     * \brief retreives a PilotEntry from the database.
+     *
+     * This function is a wrapper for DataBase::getEntry(DataPosition),
+     * where the table is already set and which returns a PilotEntry
+     * instead of an Entry. It allows for easy access to a pilot entry
+     * with only the RowId required as input.
+     */
+    PilotEntry getPilotEntry(RowId);
+    ///[F] the same can easily be implemented for tails/flights
+
 
 };
 

+ 18 - 3
src/experimental/Entry.cpp

@@ -4,7 +4,21 @@ namespace experimental {
 
 Entry::Entry(DataPosition position_)
     : position(position_)
-{}
+{
+//    position = position_;
+}
+/// [F] from what I understand the initialiser list should do exactly the same as
+///     the body, but for some reason it doesn't. Example:
+///     auto entry = DB()->getPilotEntry(7);
+///     DEB(entry.position); // returns {"pilots",7}
+///
+///     PilotEntry copiedEntry;
+///     copiedEntry = entry;
+///     DEB(copiedEntry.position);
+///     returns {"pilots",0} with initalizer list
+///     returns {"pilots", 7} with constructor as above.
+///
+///     I think I'm missing something but can't figure it out.
 
 void Entry::setData(TableData table_data)
 {
@@ -17,8 +31,9 @@ const TableData& Entry::getData()
 }
 
 PilotEntry::PilotEntry(int row_id)
-    : Entry::Entry(DataPosition("pilots", row_id))
-{}
+{
+    position = DataPosition("pilots", row_id);
+}
 
 PilotEntry::PilotEntry(TableData fromNewPilotDialog)
     : Entry::Entry(DEFAULT_PILOT_POSITION)

+ 25 - 40
src/gui/dialogues/newpilotdialog.cpp

@@ -55,43 +55,37 @@ static const auto LINE_EDIT_VALIDATORS = QVector({FIRSTNAME_VALID, LASTNAME_VALI
                                            PHONE_VALID,     EMAIL_VALID,
                                            COMPANY_VALID,     EMPLOYEENR_VALID});
 // For creating a new entry
-NewPilotDialog::NewPilotDialog(Db::editRole edRole, QWidget *parent) :
+NewPilotDialog::NewPilotDialog(QWidget *parent) :
     QDialog(parent),
     ui(new Ui::NewPilot)
 {
     DEB("New NewPilotDialog\n");
-    role = edRole;
     ui->setupUi(this);
-
     setupValidators();
     setupCompleter();
+
+    using namespace experimental;
+    //    connect(DB(), &DataBase::commitUnsuccessful,
+    //            this, &NewPilotDialog::onCommitUnsuccessful);
+    pilotEntry = PilotEntry();
+    ui->piclastnameLineEdit->setFocus();
 }
-// For editing an existing entry
-NewPilotDialog::NewPilotDialog(Pilot existingEntry, Db::editRole edRole, QWidget *parent) :
+
+NewPilotDialog::NewPilotDialog(int rowId, QWidget *parent) :
     QDialog(parent),
     ui(new Ui::NewPilot)
 {
-    DEB("New NewPilotDialog\n");
-    oldEntry = existingEntry;
-    role = edRole;
     ui->setupUi(this);
-
     setupValidators();
     setupCompleter();
 
-    formFiller();
-    ui->piclastnameLineEdit->setFocus();
-}
-
-NewPilotDialog::NewPilotDialog(experimental::PilotEntry oldEntry, Db::editRole, QWidget *parent) :
-    QDialog(parent),
-    ui(new Ui::NewPilot)
-{
     using namespace experimental;
 //    connect(DB(), &DataBase::commitUnsuccessful,
 //            this, &NewPilotDialog::onCommitUnsuccessful);
-    oldPilotEntry = oldEntry;
-    //to do
+    pilotEntry = DB()->getPilotEntry(rowId);
+    DEB("Pilot Entry position: " << pilotEntry.position);
+    formFiller();
+    ui->piclastnameLineEdit->setFocus();
 }
 
 NewPilotDialog::~NewPilotDialog()
@@ -141,16 +135,12 @@ void NewPilotDialog::setupCompleter()
 void NewPilotDialog::formFiller()
 {
     DEB("Filling Form...");
-    DEB(oldEntry);
-    auto line_edits = parent()->findChildren<QLineEdit *>();
+    auto line_edits = this->findChildren<QLineEdit *>();
 
     for (const auto &le : line_edits) {
-        QString key = le->objectName();
-        key.chop(8);//remove "LineEdit"
-        QString value = oldEntry.data.value(key);
-        if (!value.isEmpty()) {
-            le->setText(value);
-        }
+        QString key = le->objectName().remove("LineEdit");
+        QString value = pilotEntry.getData().value(key);
+        le->setText(value);
     }
 }
 
@@ -172,20 +162,15 @@ void NewPilotDialog::submitForm()
     displayName.append(QLatin1String(", "));
     displayName.append(ui->picfirstnameLineEdit->text().left(1));
     displayName.append(QLatin1Char('.'));
-    newData.insert("displayname",displayName);
+    newData.insert("displayname", displayName);
 
     using namespace experimental;
 
-    switch (role) {
-    case Db::editExisting:
-        oldEntry.setData(newData);
-        DB()->commit(oldPilotEntry);
-        // to do: handle unsuccessful commit
-        break;
-    case Db::createNew:
-        auto newEntry = PilotEntry(newData);
-        experimental::DB()->commit(oldPilotEntry);
-        // to do: handle unsuccessful commit
-        break;
-    }
+    pilotEntry.setData(newData);
+    DEB("Pilot entry position: " << pilotEntry.position);
+    DEB("Pilot entry data: " << pilotEntry.getData());
+    DB()->commit(pilotEntry);
+    // to do: create signals and slots to handle unsuccessful commit
+    // onSuccessfulCommit, accept();
+    // onError, show QMessageBox and prompt for user Input
 }

+ 5 - 9
src/gui/dialogues/newpilotdialog.h

@@ -27,8 +27,8 @@
 #include "src/classes/completionlist.h"
 
 #include "src/experimental/DataBase.h"
-//#include "src/experimental/Entry.h"
-//#include "src/experimental/Decl.h"
+#include "src/experimental/Entry.h"
+#include "src/experimental/Decl.h"
 
 namespace Ui {
 class NewPilot;
@@ -38,19 +38,15 @@ class NewPilotDialog : public QDialog
 {
     Q_OBJECT
 public:
-    explicit NewPilotDialog(Db::editRole, QWidget *parent = nullptr);
-    explicit NewPilotDialog(Pilot, Db::editRole, QWidget *parent = nullptr);
-    explicit NewPilotDialog(experimental::PilotEntry oldEntry, Db::editRole, QWidget *parent = nullptr);
+    explicit NewPilotDialog(QWidget *parent = nullptr);
+    explicit NewPilotDialog(int rowId, QWidget *parent = nullptr);
     ~NewPilotDialog();
 private slots:
     void on_buttonBox_accepted();
 private:
     Ui::NewPilot *ui;
 
-    Db::editRole role;
-    Pilot oldEntry;
-
-    experimental::PilotEntry oldPilotEntry;
+    experimental::PilotEntry pilotEntry;
 
     void setupValidators();
 

+ 12 - 4
src/gui/widgets/homewidget.cpp

@@ -40,9 +40,17 @@ void HomeWidget::on_pushButton_clicked()
 {
     using namespace experimental;
     DB()->connect();
-    //DataPosition dp = {"pilots", 7};
-    //DEB(DB()->getEntryDataNew(dp));
-    long customFunc = 0;
+
+    auto entry = DB()->getPilotEntry(7);
+    DEB(entry.position);
+    DEB(entry.getData());
+
+    PilotEntry copiedEntry;
+    copiedEntry = entry;
+    DEB(copiedEntry.position);
+    DEB(copiedEntry.getData());
+
+    /*long customFunc = 0;
     long qSqlTableModelFunc = 0;
     for (int i = 10; i < 100; i++) {
         DataPosition dp = {"flights", i};
@@ -59,5 +67,5 @@ void HomeWidget::on_pushButton_clicked()
         qSqlTableModelFunc += duration2.count();
     }
     DEB("Average execution time: (custom Func)        " << customFunc/10000 << "ms");
-    DEB("Average execution time: (qSqlTableModelFunc) " << qSqlTableModelFunc/10000 << "ms");
+    DEB("Average execution time: (qSqlTableModelFunc) " << qSqlTableModelFunc/10000 << "ms");*/
 }

+ 5 - 5
src/gui/widgets/pilotswidget.cpp

@@ -39,12 +39,12 @@ void PilotsWidget::tableView_selectionChanged()//const QItemSelection &index, co
     selectedPilots.clear();
 
     for (const auto& row : selection->selectedRows()) {
-        selectedPilots << row.data().toInt();
+        selectedPilots.append(row.data().toInt());
         DEB("Selected Tails(s) with ID: " << selectedPilots);
     }
     if(selectedPilots.length() == 1) {
 
-        NewPilotDialog* np = new NewPilotDialog(Pilot(selectedPilots.first()), Db::editExisting, this);
+        NewPilotDialog* np = new NewPilotDialog(selectedPilots.first(), this);
         connect(np, SIGNAL(accepted()), this, SLOT(pilot_editing_finished()));
         connect(np, SIGNAL(rejected()), this, SLOT(pilot_editing_finished()));
         np->setWindowFlag(Qt::Widget);
@@ -63,10 +63,10 @@ void PilotsWidget::tableView_headerClicked(int column)
 
 void PilotsWidget::on_newButton_clicked()
 {
-    NewPilotDialog* np = new NewPilotDialog(Db::createNew, this);
+    NewPilotDialog* np = new NewPilotDialog(this);
     np->setAttribute(Qt::WA_DeleteOnClose);
-    connect(np, SIGNAL(accepted()),
-            this, SLOT(pilot_editing_finished()));
+    connect(np, SIGNAL(accepted()), this, SLOT(pilot_editing_finished()));
+    connect(np, SIGNAL(rejected()), this, SLOT(pilot_editing_finished()));
     np->exec();
 }