浏览代码

fixed bug in scratchpad function

fiffty-50 4 年之前
父节点
当前提交
be1b3764f3
共有 2 个文件被更改,包括 71 次插入59 次删除
  1. 68 57
      dbman.cpp
  2. 3 2
      newflight.cpp

+ 68 - 57
dbman.cpp

@@ -39,7 +39,7 @@ public:
             QSqlDatabase db = QSqlDatabase::addDatabase(DRIVER);
 
             //QString pathtodb = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
-            //db.setDatabaseName(pathtodb+"/flog.db");
+            //db.setDatabaseName(pathtodb+"/logbook.db");
             //qDebug() << "Database: " << pathtodb+"/logbook.db";
             db.setDatabaseName("logbook.db");
 
@@ -52,7 +52,7 @@ public:
     }
     static void initexample()
     {
-        QSqlQuery query("CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT)");
+        QSqlQuery query("CREATE TABLE flights (id INTEGER PRIMARY KEY, date NUMERIC)");
 
         if(!query.isActive())
             qWarning() << "MainWindow::DatabaseInit - ERROR: " << query.lastError().text();
@@ -99,10 +99,15 @@ public:
     /*
      * Flights Database Related Functions
      */
+    /*!
+     * \brief SelectFlightDate Retreives Flights from the database
+     * \param doft Date of flight for filtering result set. "ALL" means no filter.
+     * \return Flight(s) for selected date.
+     */
     static QVector<QString> SelectFlightDate(QString doft)
     {
         QSqlQuery query;
-        if (doft == "ALL")
+        if (doft == "ALL") // Special Selector
         {
             query.prepare("SELECT * FROM flights ORDER BY doft DESC, tofb ASC");
             qDebug() << "All flights selected";
@@ -165,6 +170,11 @@ public:
         return flight;
 
     }
+    /*!
+     * \brief SelectFlightById Retreives a single flight from the database.
+     * \param flight_id Primary Key of flights database
+     * \return Flight details of selected flight.
+     */
     static QVector<QString> SelectFlightById(QString flight_id)
     {
         QSqlQuery query;
@@ -191,11 +201,22 @@ public:
         flight.append(query.value(7).toString());
         flight.append(query.value(8).toString());
 
-
         qDebug() << "db::SelectFlightById - retreived flight: " << flight;
         return flight;
-
     }
+
+    /*!
+     * \brief CreateFlightVectorFromInput Converts input from NewFlight Window into database format
+     * \param doft Date of flight
+     * \param dept Place of Departure
+     * \param tofb Time Off Blocks (UTC)
+     * \param dest Place of Destination
+     * \param tonb Time On Blocks (UTC)
+     * \param tblk Total Block Time
+     * \param pic Pilot in command
+     * \param acft Aircraft
+     * \return Vector of values ready for committing
+     */
     static QVector<QString> CreateFlightVectorFromInput(QString doft, QString dept, QTime tofb, QString dest, QTime tonb, QTime tblk, QString pic, QString acft)
     {
 
@@ -212,7 +233,12 @@ public:
         //qDebug() << flight;
         return flight;
     }
-    static int CommitFlight(QVector<QString> flight)// flight vector shall always have length 9
+    /*!
+     * \brief CommitFlight Inserts prepared flight vector into database. Also creates
+     * a corresponding entry in the extras database to ensure matching IDs.
+     * \param flight a Vector of values in database format
+     */
+    static void CommitFlight(QVector<QString> flight)// flight vector shall always have length 9
     {
         QSqlQuery query;
         query.prepare("INSERT INTO flights (doft, dept, tofb, dest, tonb, tblk, pic, acft) "
@@ -232,12 +258,14 @@ public:
         QSqlQuery query2;
         query2.prepare("INSERT INTO extras DEFAULT VALUES");
         query2.exec();
-
         qDebug() << "Creating extras entry" << query2.lastError().text();
-        return 0;
     }
-
-    static int CommitToScratchpad(QVector<QString> flight)// to store input mask
+    /*!
+     * \brief CommitToScratchpad Commits the inputs of the NewFlight window to a scratchpad
+     * to make them available for restoring entries when the input fields are being reloaded.
+     * \param flight The input data, which was not accepted for commiting to the flights table.
+     */
+    static void CommitToScratchpad(QVector<QString> flight)// to store input mask
     {
         //qDebug() << "Saving invalid flight to scratchpad";
         QSqlQuery query;
@@ -254,8 +282,11 @@ public:
         query.bindValue(":acft", flight[8].toInt());
         query.exec();
         qDebug() << query.lastError().text();
-        return 0;
     }
+    /*!
+     * \brief CheckScratchpad Verifies if the scratchpad contains data
+     * \return true if scratchpad contains data
+     */
     static bool CheckScratchpad() // see if scratchpad is empty
     {
         //qDebug() << "Checking if scratchpad contains data";
@@ -274,6 +305,20 @@ public:
             return 0;
         }
     }
+    /*!
+     * \brief ClearScratchpad Deletes data contained in the scratchpad
+     */
+    static void ClearScratchpad()
+    {
+        qDebug() << "Deleting scratchpad";
+        QSqlQuery query;
+        query.prepare("DELETE FROM scratchpad;");
+        query.exec();
+    }
+    /*!
+     * \brief RetreiveScratchpad Selects data from scratchpad
+     * \return Vector of data contained in scratchpad
+     */
     static QVector<QString> RetreiveScratchpad()
     {
         //qDebug() << "Retreiving invalid flight from scratchpad";
@@ -289,57 +334,23 @@ public:
             return flight;
         }
 
-        query.previous();// To go back to index 0
-        //query.last(); // this can be very slow, used to determine query size since .size is not supported by sqlite
-        //int numRows = query.at() + 1; // Number of rows (flights) in the query
-        //query.first();
-        //query.previous();// Go back to index 0
-
-        //QVector<QString> flight(numRows * 9); // Every flight has 9 fields in the database
-        QVector<QString> flight(9);
-        int index = 0; // counter for output vector
-
+        query.previous();
+        QVector<QString> flight;
         while (query.next()) {
-            QString id = query.value(0).toString();
-            QString doft = query.value(1).toString();
-            QString dept = query.value(2).toString();
-            QString tofb = calc::minutes_to_string((query.value(3).toString()));
-            QString dest = query.value(4).toString();
-            QString tonb = calc::minutes_to_string((query.value(5).toString()));
-            QString tblk = calc::minutes_to_string((query.value(6).toString()));
-            QString pic = query.value(7).toString();
-            QString acft = query.value(8).toString();
-            //qDebug() << id << doft << dept << tofb << dest << tonb << tblk << pic << acft << endl;
-            flight[index] = id;
-            ++index;
-            flight[index] = doft;
-            ++index;
-            flight[index] = dept;
-            ++index;
-            flight[index] = tofb;
-            ++index;
-            flight[index] = dest;
-            ++index;
-            flight[index] = tonb;
-            ++index;
-            flight[index] = tblk;
-            ++index;
-            flight[index] = pic;
-            ++index;
-            flight[index] = acft;
-            ++index;
+            flight.append(query.value(0).toString());
+            flight.append(query.value(1).toString());
+            flight.append(query.value(2).toString());
+            flight.append(calc::minutes_to_string((query.value(3).toString())));
+            flight.append(query.value(4).toString());
+            flight.append(calc::minutes_to_string((query.value(5).toString())));
+            flight.append(calc::minutes_to_string((query.value(6).toString())));
+            flight.append(query.value(7).toString());
+            flight.append(query.value(8).toString());
         }
-
         ClearScratchpad();
         return flight;
     }
-    static void ClearScratchpad()
-    {
-        qDebug() << "Deleting scratchpad";
-        QSqlQuery query;
-        query.prepare("DELETE FROM scratchpad;");
-        query.exec();
-    }
+
 
     static bool DeleteFlightById(QString flight_id)
     {

+ 3 - 2
newflight.cpp

@@ -29,7 +29,7 @@
 #include <QButtonGroup>
 
 
-void NewFlight::on_verifyButton_clicked()
+void NewFlight::on_verifyButton_clicked()//debug
 {
     /*on_newDoft_editingFinished();// - activate slots in case user has been active in last input before clicking submit
     on_newTonb_editingFinished();
@@ -312,6 +312,7 @@ bool NewFlight::verifyInput()
 void NewFlight::returnInput(QVector<QString> flight)
 {
     // Re-populates the input masks with the selected fields if input was erroneous to allow for corrections to be made
+    qDebug() << "return Input: " << flight;
     ui->newDoft->setDate(QDate::fromString(flight[1],Qt::ISODate));
     ui->newDept->setText(flight[2]);
     ui->newTofb->setText(flight[3]);
@@ -341,7 +342,7 @@ void NewFlight::storeSettings()
 }
 void NewFlight::restoreSettings()
 {
-    qDebug() << "Restoring Settings...";
+    qDebug() << "Restoring Settings...";//crashes if db is empty due to QVector index out of range.
     ui->FunctionComboBox->setCurrentText(db::retreiveSetting("100")[1]);
     ui->ApproachComboBox->setCurrentText(db::retreiveSetting("101")[1]);
     ui->PilotFlyingCheckBox->setChecked(db::retreiveSetting("102")[1].toInt());