Browse Source

added simplified constructors for flight/pilot/aircraft classes

fiffty-50 4 years ago
parent
commit
cd6c5a5451

+ 38 - 1
src/classes/aircraft.cpp

@@ -19,5 +19,42 @@
 
 // Debug Makro
 #define DEB(expr) \
-    qDebug() << "aircraft ::" << __func__ << "\t" << expr
+    qDebug() << __PRETTY_FUNCTION__ << "\t" << expr
 
+
+Aircraft::Aircraft(int tail_id)
+{
+    //retreive database layout
+    const auto dbContent = DbInfo();
+    //Check database for row id
+    QString statement = "SELECT COUNT(*) FROM tails WHERE _rowid_=" + QString::number(tail_id);
+    QSqlQuery q(statement);
+    q.next();
+    int rows = q.value(0).toInt();
+    if (rows == 0) {
+        DEB("No Entry found for row id: " << tail_id );
+        position.second = 0;
+    } else {
+        DEB("Retreiving data for row id: " << tail_id);
+        QString statement = "SELECT * FROM tails WHERE _rowid_=" + QString::number(tail_id);
+        DEB("Executing SQL...");
+        DEB(statement);
+
+        QSqlQuery q(statement);
+        q.exec();
+        q.next();
+        for (int i = 0; i < dbContent.format.value("tails").length(); i++) {
+            data.insert(dbContent.format.value("tails")[i], q.value(i).toString());
+        }
+
+        QString error = q.lastError().text();
+        if (error.length() > 2) {
+            DEB("Error: " << q.lastError().text());
+            position.second = 0;
+            position.first = "invalid";
+        } else {
+            position.second = tail_id;
+            position.first = "tails";
+        }
+    }
+}

+ 2 - 0
src/classes/aircraft.h

@@ -27,6 +27,8 @@
 class Aircraft : public Entry
 {
     using Entry::Entry;
+public:
+    Aircraft(int acft_id);
 };
 
 #endif // AIRCRAFT_H

+ 39 - 1
src/classes/calc.cpp

@@ -16,7 +16,9 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "calc.h"
-//#include "dbman.cpp"
+// Debug Makro
+#define DEB(expr) \
+    qDebug() << __PRETTY_FUNCTION__ << "\t" << expr
 /*!
  * \brief Calc::blocktime Calculates Block Time for a given departure and arrival time
  * \param tofb QTime Time Off Blocks
@@ -409,3 +411,39 @@ QString Calc::formatTimeInput(QString userinput)
     }
     return output;
 }
+
+/*!
+ * \brief Calc::updateAutoTimes When the details of an aircraft are changed,
+ * this function recalculates deductable times for this aircraft and updates
+ * the database accordingly.
+ * \param acft An aircraft object.
+ * \return
+ */
+void Calc::updateAutoTimes(int acft_id)
+{
+    //find all flights for aircraft
+    auto flightList = Db::multiSelect(QVector<QString>{"id"},"flights","acft",
+                                      QString::number(acft_id),Db::exactMatch);
+    auto acft = Aircraft("tails",acft_id);
+    for (const auto& item : flightList) {
+        auto flt = Flight("flights",item.toInt());
+
+        if(acft.data.value("singlepilot") == "1" && acft.data.value("singleengine") == "1") {
+            DEB("SPSE");
+            flt.data.insert("tSPSE",flt.data.value("tblk"));
+            flt.data.insert("tSPME","");
+            flt.data.insert("tMP","");
+        } else if ((acft.data.value("singlepilot") == "1" && acft.data.value("multiengine") == "1")) {
+            DEB("SPME");
+            flt.data.insert("tSPME",flt.data.value("tblk"));
+            flt.data.insert("tSPSE","");
+            flt.data.insert("tMP","");
+        } else if ((acft.data.value("multipilot") == "1")) {
+            DEB("MPME");
+            flt.data.insert("tMP",flt.data.value("tblk"));
+            flt.data.insert("tSPSE","");
+            flt.data.insert("tSPME","");
+        }
+        flt.commit();
+    }
+}

+ 6 - 0
src/classes/calc.h

@@ -20,6 +20,8 @@
 #define CALC_H
 
 #include "src/database/db.h"
+#include "src/classes/aircraft.h"
+#include "src/classes/flight.h"
 #include <QDateTime>
 #include <cmath>
 #include <QDebug>
@@ -61,6 +63,10 @@ public:
     static int calculateNightTime(QString dept, QString dest, QDateTime departureTime, int tblk);
 
     static QString formatTimeInput(QString userinput);
+
+    static void updateAutoTimes(int acft_id);
+
+    static void autoTimes(Flight, Aircraft);
 };
 
 

+ 42 - 0
src/classes/flight.cpp

@@ -16,4 +16,46 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "flight.h"
+// Debug Makro
+#define DEB(expr) \
+    qDebug() << __PRETTY_FUNCTION__ << "\t" << expr
 
+
+Flight::Flight(int flight_id)
+{
+    //retreive database layout
+    const auto dbContent = DbInfo();
+    auto table = QLatin1String("flights");
+
+    //Check database for row id
+    QString statement = "SELECT COUNT(*) FROM " + table + " WHERE _rowid_=" + QString::number(flight_id);
+    QSqlQuery q(statement);
+    q.next();
+    int rows = q.value(0).toInt();
+    if (rows == 0) {
+        DEB("No Entry found for row id: " << flight_id );
+        position.second = 0;
+    } else {
+        DEB("Retreiving data for row id: " << flight_id);
+        QString statement = "SELECT * FROM " + table + " WHERE _rowid_=" + QString::number(flight_id);
+        DEB("Executing SQL...");
+        DEB(statement);
+
+        QSqlQuery q(statement);
+        q.exec();
+        q.next();
+        for (int i = 0; i < dbContent.format.value(table).length(); i++) {
+            data.insert(dbContent.format.value(table)[i], q.value(i).toString());
+        }
+
+        QString error = q.lastError().text();
+        if (error.length() > 2) {
+            DEB("Error: " << q.lastError().text());
+            position.second = 0;
+            position.first = "invalid";
+        } else {
+            position.second = flight_id;
+            position.first = "flights";
+        }
+    }
+}

+ 2 - 0
src/classes/flight.h

@@ -27,6 +27,8 @@
 class Flight : public Entry
 {
     using Entry::Entry;
+public:
+    Flight(int flight_id);
 };
 
 #endif // FLIGHT_H

+ 8 - 2
src/gui/widgets/homewidget.cpp

@@ -38,8 +38,14 @@ HomeWidget::~HomeWidget()
 
 void HomeWidget::on_pushButton_clicked()
 {
-    FirstRunDialog dialog;
-    dialog.exec();
+    //FirstRunDialog dialog;
+    //dialog.exec();
+    //for (int i=1;i<25;i++) {
+    //    Calc::updateAutoTimes(i);
+    //}
+    DEB(Aircraft(1));
+    DEB(Flight(1));
+
 }
 
 void HomeWidget::showTotals()

+ 27 - 31
src/gui/widgets/logbookwidget.cpp

@@ -27,11 +27,32 @@ LogbookWidget::LogbookWidget(QWidget *parent) :
     ui(new Ui::LogbookWidget)
 {
     ui->setupUi(this);
+    refreshView();
+}
+
+LogbookWidget::~LogbookWidget()
+{
+    delete ui;
+}
+
+void LogbookWidget::setSelectedFlight(const qint32 &value)
+{
+    selectedFlight = value;
+}
+
+void LogbookWidget::tableView_selectionChanged(const QItemSelection &index,
+                                               const QItemSelection &)// TO DO
+{
+    setSelectedFlight(index.indexes()[0].data().toInt());
+    DEB("Selected flight with ID#: " << selectedFlight);
+}
+
+void LogbookWidget::refreshView()
+{
     ui->filterDateEdit->setDate(QDate::currentDate());
     ui->filterDateEdit_2->setDate(QDate::currentDate());
     ui->newFlightButton->setFocus();
-
-    auto start = std::chrono::high_resolution_clock::now(); // timer for performance testing
+    //auto start = std::chrono::high_resolution_clock::now(); // timer for performance testing
 
     QSqlTableModel *model = new QSqlTableModel;
     model->setTable("Logbook");
@@ -57,33 +78,15 @@ LogbookWidget::LogbookWidget(QWidget *parent) :
     view->setAlternatingRowColors(true);
     view->hideColumn(0);
     view->show();
-
-    auto stop = std::chrono::high_resolution_clock::now();
-    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
-    DEB("Time taken for lookup and rendering: " << duration.count() << " microseconds");
+    //auto stop = std::chrono::high_resolution_clock::now();
+    //auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
+    //DEB("Time taken for lookup and rendering: " << duration.count() << " microseconds");
 
     connect(ui->tableView->selectionModel(),
             SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
             SLOT(tableView_selectionChanged(const QItemSelection &, const QItemSelection &)));
 }
 
-LogbookWidget::~LogbookWidget()
-{
-    delete ui;
-}
-
-void LogbookWidget::setSelectedFlight(const qint32 &value)
-{
-    selectedFlight = value;
-}
-
-void LogbookWidget::tableView_selectionChanged(const QItemSelection &index,
-                                               const QItemSelection &)// TO DO
-{
-    setSelectedFlight(index.indexes()[0].data().toInt());
-    DEB("Selected flight with ID#: " << selectedFlight);
-}
-
 
 
 void LogbookWidget::on_newFlightButton_clicked()
@@ -126,14 +129,7 @@ void LogbookWidget::on_deleteFlightPushButton_clicked()
             DEB("Deleting flight with ID# " << selectedFlight);
             auto en = new Flight("flights", selectedFlight);
             en->remove();
-
-            QSqlTableModel *ShowAllModel = new QSqlTableModel; //refresh view
-            ShowAllModel->setTable("Logbook");
-            ShowAllModel->select();
-            ui->tableView->setModel(ShowAllModel);
-            connect(ui->tableView->selectionModel(),
-                    SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
-                    SLOT(tableView_selectionChanged(const QItemSelection &, const QItemSelection &)));
+            refreshView();
         }
     } else {
         QMessageBox NoFlight;

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

@@ -62,6 +62,8 @@ private slots:
 private:
     Ui::LogbookWidget *ui;
 
+    void refreshView();
+
 };
 
 #endif // LOGBOOKWIDGET_H