Browse Source

added statistics functionality to HomeWidget

fiffty-50 4 năm trước cách đây
mục cha
commit
d24070ee02
5 tập tin đã thay đổi với 189 bổ sung11 xóa
  1. 91 0
      dbman.cpp
  2. 23 3
      homewidget.cpp
  3. 2 0
      homewidget.h
  4. 71 8
      homewidget.ui
  5. 2 0
      logbookwidget.cpp

+ 91 - 0
dbman.cpp

@@ -311,6 +311,97 @@ public:
         }
     }
 
+    /*!
+     * \brief retreiveTotalTime Looks up Total Blocktime in the flights database
+     * \return Amount of Total Block Time in blockminutes
+     */
+    static QString retreiveTotalTime()
+    {
+        QSqlQuery query;
+        query.prepare("SELECT SUM(tblk) FROM flights");
+        query.exec();
+
+        QString result;
+        while (query.next()){
+          result = query.value(0).toString();
+        }
+        return result;
+    }
+
+    /*!
+     * \brief retreiveTotalTimeThisCalendarYear Looks up Total Block Time in
+     * the current calendar year
+     * \return Total Amount of Blockminutes
+     */
+    static QString retreiveTotalTimeThisCalendarYear()
+    {
+        QDate today = QDate::currentDate();
+        QDate start;
+        start.setDate(today.year(),1,1);
+        QString startdate = start.toString(Qt::ISODate);
+
+        QSqlQuery query;
+        query.prepare("SELECT SUM(tblk) FROM flights WHERE doft >= ?");
+        query.addBindValue(startdate);
+        query.exec();
+
+        QString result;
+        while (query.next()){
+            result = query.value(0).toString();
+        }
+
+        qDebug() << "db::retreiveTotalTimeThisCalendarYear: Total minutes: " << result;
+        return result;
+    }
+    /*!
+     * \brief retreiveTotalTimeRollingYear Looks up Total Time in the last 365 days.
+     * \return Total Blockminutes
+     */
+    static QString retreiveTotalTimeRollingYear()
+    {
+        QDate start = QDate::fromJulianDay(QDate::currentDate().toJulianDay() - 365);
+        QString startdate = start.toString(Qt::ISODate);
+
+        QSqlQuery query;
+        query.prepare("SELECT SUM(tblk) FROM flights WHERE doft >= ?");
+        query.addBindValue(startdate);
+        query.exec();
+
+        QString result;
+        while (query.next()){
+            result = query.value(0).toString();
+        }
+
+        qDebug() << "db::retreiveTotalTimeRollingYear: Total minutes: " << result;
+        return result;
+    }
+    /*!
+     * \brief retreiveCurrencyTakeoffLanding Looks up the amount of Take Offs and
+     * Landings performed in the last 90 days.
+     * \return {TO,LDG}
+     */
+    static QVector<QString> retreiveCurrencyTakeoffLanding()
+    {
+        QDate start = QDate::fromJulianDay(QDate::currentDate().toJulianDay() - 90);
+
+        QSqlQuery query;
+        query.prepare("SELECT "
+                      "SUM(extras.TOday + extras.TOnight) AS 'TakeOFF', "
+                      "SUM(extras.LDGday + extras.LDGnight) AS 'Landing' "
+                      "FROM flights "
+                      "INNER JOIN extras on flights.id = extras.extras_id "
+                      "WHERE doft >= ?");
+        query.addBindValue(start.toString(Qt::ISODate));
+        query.exec();
+
+        QVector<QString> result(2,"0"); // make sure to return a valid vector even if result 0
+        while (query.next()){
+            result.insert(0,query.value(0).toString());
+            result.insert(1,query.value(1).toString());
+        }
+        qDebug() << "db::retreiveCurrencyTakeoffLanding: " << result[0] << " TO, " <<result[1] << " LDG";
+        return result;
+    }
     /*
      * Pilots Database Related Functions
      */

+ 23 - 3
homewidget.cpp

@@ -1,5 +1,7 @@
 #include "homewidget.h"
 #include "ui_homewidget.h"
+#include "calc.h"
+#include "dbman.cpp"
 
 #include <QDebug>
 
@@ -15,12 +17,30 @@ homeWidget::homeWidget(QWidget *parent) :
      * To Do: Functions to retreive values from DB
      */
 
-    ui->totalTimeDisplayLabel->setText("123:45");
-    ui->blockHoursDisplayLabel->setText("123:45");
-    ui->currencyDisplayLabel->setText("17 Take Offs\n15 Landings");
+    ui->totalTimeDisplayLabel->setText(calc::minutes_to_string(db::retreiveTotalTime()));
+
+    QString blockMinutesThisYear = db::retreiveTotalTimeThisCalendarYear();
+    ui->blockHoursCalDisplayLabel->setText(calc::minutes_to_string(blockMinutesThisYear));
+    if (blockMinutesThisYear.toInt() > 900*60) {
+        qDebug() << "More than 900 block hours this calendar year!";
+        // set Text Red
+    }
+    QString blockMinutesRollingYear = db::retreiveTotalTimeRollingYear();
+    ui->blockHoursRolDisplayLabel->setText(calc::minutes_to_string(blockMinutesRollingYear));
+    QVector<QString> currency = db::retreiveCurrencyTakeoffLanding();
+    ui->currencyDisplayLabel->setText(currency[0] + " Take Offs\n" + currency[1] + " Landings");
+    if (currency[0].toInt() < 3 || currency[1].toInt() < 3){
+        qDebug() << "Less than 3 TO/LDG in last 90 days!";
+        //set Text Red
+    }
 }
 
 homeWidget::~homeWidget()
 {
     delete ui;
 }
+
+void homeWidget::on_debugButton_clicked()
+{
+    ui->debugLineEdit->setText(db::retreiveTotalTimeRollingYear());
+}

+ 2 - 0
homewidget.h

@@ -18,6 +18,8 @@ public:
 private slots:
 
 
+    void on_debugButton_clicked();
+
 private:
     Ui::homeWidget *ui;
 };

+ 71 - 8
homewidget.ui

@@ -7,7 +7,7 @@
     <x>0</x>
     <y>0</y>
     <width>1330</width>
-    <height>609</height>
+    <height>624</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -127,9 +127,9 @@
       </spacer>
      </item>
      <item row="1" column="1">
-      <widget class="QLabel" name="blockHoursLabel">
+      <widget class="QLabel" name="blockHoursCalendarYearLabel">
        <property name="text">
-        <string>Block hours this year:</string>
+        <string>Block hours (this year):</string>
        </property>
       </widget>
      </item>
@@ -147,7 +147,7 @@
       </spacer>
      </item>
      <item row="1" column="3">
-      <widget class="QLabel" name="blockHoursDisplayLabel">
+      <widget class="QLabel" name="blockHoursCalDisplayLabel">
        <property name="text">
         <string>hours</string>
        </property>
@@ -167,7 +167,7 @@
       </spacer>
      </item>
      <item row="2" column="0">
-      <spacer name="horizontalSpacer_9">
+      <spacer name="horizontalSpacer_12">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
@@ -180,13 +180,66 @@
       </spacer>
      </item>
      <item row="2" column="1">
+      <widget class="QLabel" name="blockHoursRollingLabel">
+       <property name="text">
+        <string>Block hours (rolling year):</string>
+       </property>
+      </widget>
+     </item>
+     <item row="2" column="2">
+      <spacer name="horizontalSpacer_10">
+       <property name="orientation">
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>40</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+     <item row="2" column="3">
+      <widget class="QLabel" name="blockHoursRolDisplayLabel">
+       <property name="text">
+        <string>hours</string>
+       </property>
+      </widget>
+     </item>
+     <item row="2" column="4">
+      <spacer name="horizontalSpacer_11">
+       <property name="orientation">
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>40</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+     <item row="3" column="0">
+      <spacer name="horizontalSpacer_9">
+       <property name="orientation">
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>40</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+     <item row="3" column="1">
       <widget class="QLabel" name="currencyLabel">
        <property name="text">
         <string>Currency (last 90 days):</string>
        </property>
       </widget>
      </item>
-     <item row="2" column="2">
+     <item row="3" column="2">
       <spacer name="horizontalSpacer_3">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
@@ -199,14 +252,14 @@
        </property>
       </spacer>
      </item>
-     <item row="2" column="3">
+     <item row="3" column="3">
       <widget class="QLabel" name="currencyDisplayLabel">
        <property name="text">
         <string>TO/LDG</string>
        </property>
       </widget>
      </item>
-     <item row="2" column="4">
+     <item row="3" column="4">
       <spacer name="horizontalSpacer_4">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
@@ -234,6 +287,16 @@
      </property>
     </spacer>
    </item>
+   <item row="5" column="0">
+    <widget class="QPushButton" name="debugButton">
+     <property name="text">
+      <string>Debug</string>
+     </property>
+    </widget>
+   </item>
+   <item row="6" column="0">
+    <widget class="QLineEdit" name="debugLineEdit"/>
+   </item>
   </layout>
  </widget>
  <resources/>

+ 2 - 0
logbookwidget.cpp

@@ -9,6 +9,8 @@
 #include <chrono>
 #include <QDebug>
 
+//To Do: Update Selection in Tableview on arrow key press.
+
 qint32 SelectedFlight = -1; // Variable to store selected row in QTableView
 
 logbookWidget::logbookWidget(QWidget *parent) :