Browse Source

crated dbAirport class and updated functions

fiffty-50 4 years ago
parent
commit
f04ff3f9cb
14 changed files with 166 additions and 139 deletions
  1. 4 4
      calc.cpp
  2. 2 0
      calc.h
  3. 113 0
      dbairport.cpp
  4. 21 0
      dbairport.h
  5. 0 113
      dbman.cpp
  6. 4 4
      editflight.cpp
  7. 7 7
      mainwindow.cpp
  8. 2 2
      mainwindow.h
  9. 3 3
      mainwindow.ui
  10. 6 6
      newflight.cpp
  11. 1 0
      newflight.h
  12. 2 0
      openLog.pro
  13. 1 0
      themes/icons/icons.qrc
  14. BIN
      themes/icons/new_flight_icon.jpg

+ 4 - 4
calc.cpp

@@ -292,13 +292,13 @@ double calc::solarElevation(QDateTime utc_time_point, double lat, double lon)
  */
 int calc::calculateNightTime(QString dept, QString dest, QDateTime departureTime, int tblk)
 {
-    double deptLat = db::retreiveIcaoCoordinates(dept)[0];
+    double deptLat = dbAirport::retreiveIcaoCoordinates(dept)[0];
     qDebug() << "calc::calculateNightTime deptLat = " << deptLat;
-    double deptLon = db::retreiveIcaoCoordinates(dept)[1];
+    double deptLon = dbAirport::retreiveIcaoCoordinates(dept)[1];
     qDebug() << "calc::calculateNightTime deptLon = " << deptLon;
-    double destLat = db::retreiveIcaoCoordinates(dest)[0];
+    double destLat = dbAirport::retreiveIcaoCoordinates(dest)[0];
     qDebug() << "calc::calculateNightTime destLat = " << destLat;
-    double destLon = db::retreiveIcaoCoordinates(dest)[1];
+    double destLon = dbAirport::retreiveIcaoCoordinates(dest)[1];
     qDebug() << "calc::calculateNightTime destLon = " << destLon;
 
     QVector<QVector<double>> route = intermediatePointsOnGreatCircle(deptLat, deptLon, destLat, destLon, tblk);

+ 2 - 0
calc.h

@@ -18,6 +18,8 @@
 
 #ifndef CALC_H
 #define CALC_H
+
+#include "dbairport.h"
 #include <QDateTime>
 #include <cmath>
 #include <QDebug>

+ 113 - 0
dbairport.cpp

@@ -0,0 +1,113 @@
+#include "dbairport.h"
+#include "dbman.cpp"
+
+/*!
+     * \brief RetreiveAirportNameFromIcaoOrIata Looks up Airport Name
+     * \param identifier can be ICAO or IATA airport codes.
+     * \return The name of the airport associated with the above code
+     */
+QString dbAirport::retreiveAirportNameFromIcaoOrIata(QString identifier)
+{
+    QString result = "";
+    QSqlQuery query;
+    query.prepare("SELECT name "
+                  "FROM airports WHERE icao LIKE ? OR iata LIKE ?");
+    identifier.append("%");
+    identifier.prepend("%");
+    query.addBindValue(identifier);
+    query.addBindValue(identifier);
+    query.exec();
+    if(query.first())
+    {
+        result.append(query.value(0).toString());
+        return result;
+    }else
+    {
+        result = result.left(result.length()-1);
+        result.append("No matching airport found.");
+        return  result;
+    }
+}
+
+QString dbAirport::retreiveAirportIdFromIcao(QString identifier)
+{
+    QString result;
+    QSqlQuery query;
+    query.prepare("SELECT airport_id FROM airports WHERE icao = ?");
+    query.addBindValue(identifier);
+    query.exec();
+
+    while(query.next())
+    {
+        result.append(query.value(0).toString());
+        //qDebug() << "db::RetreiveAirportIdFromIcao says Airport found! #" << result;
+    }
+
+    return result;
+}
+
+QStringList dbAirport::completeIcaoOrIata(QString icaoStub)
+{
+    QStringList result;
+    QSqlQuery query;
+    query.prepare("SELECT icao FROM airports WHERE icao LIKE ? OR iata LIKE ?");
+    icaoStub.prepend("%"); icaoStub.append("%");
+    query.addBindValue(icaoStub);
+    query.addBindValue(icaoStub);
+    query.exec();
+
+    while(query.next())
+    {
+        result.append(query.value(0).toString());
+        qDebug() << "db::CompleteIcaoOrIata says... Result:" << result;
+    }
+    return result;
+}
+
+/*!
+     * \brief CheckICAOValid Verifies if a user input airport exists in the database
+     * \param identifier can be ICAO or IATA airport codes.
+     * \return bool if airport is in database.
+     */
+bool dbAirport::checkICAOValid(QString identifier)
+{
+    if(identifier.length() == 4)
+    {
+        QString check = retreiveAirportIdFromIcao(identifier);
+        if(check.length() > 0)
+        {
+            //qDebug() << "db::CheckICAOValid says: Check passed!";
+            return 1;
+        }else
+        {
+            //qDebug() << "db::CheckICAOValid says: Check NOT passed! Lookup unsuccessful";
+            return 0;
+        }
+    }else
+    {
+        //qDebug() << "db::CheckICAOValid says: Check NOT passed! Empty String NOT epico!";
+        return 0;
+    }
+}
+
+/*!
+     * \brief retreiveIcaoCoordinates Looks up coordinates (lat,long) for a given airport
+     * \param icao 4-letter code for the airport
+     * \return {lat,lon} in decimal degrees
+     */
+QVector<double> dbAirport::retreiveIcaoCoordinates(QString icao)
+{
+    QSqlQuery query;
+    query.prepare("SELECT lat, long "
+                  "FROM airports "
+                  "WHERE icao = ?");
+    query.addBindValue(icao);
+    query.exec();
+
+    QVector<double> result;
+    while(query.next()) {
+        result.append(query.value(0).toDouble());
+        result.append(query.value(1).toDouble());
+    }
+    return result;
+}

+ 21 - 0
dbairport.h

@@ -0,0 +1,21 @@
+#ifndef DBAIRPORT_H
+#define DBAIRPORT_H
+
+#include <QCoreApplication>
+
+class dbAirport
+{
+public:
+
+    static QString retreiveAirportNameFromIcaoOrIata(QString identifier);
+
+    static QString retreiveAirportIdFromIcao(QString identifier);
+
+    static bool checkICAOValid(QString identifier);
+
+    static QVector<double> retreiveIcaoCoordinates(QString icao);
+
+    static QStringList completeIcaoOrIata(QString icaoStub);
+};
+
+#endif // DBAIRPORT_H

+ 0 - 113
dbman.cpp

@@ -97,120 +97,7 @@ public:
          */
     }
 
-/*
- * Airport Database Related Functions
- */
-
-    /*!
-     * \brief RetreiveAirportNameFromIcaoOrIata Looks up Airport Name
-     * \param identifier can be ICAO or IATA airport codes.
-     * \return The name of the airport associated with the above code
-     */
-    static QString RetreiveAirportNameFromIcaoOrIata(QString identifier)
-    {
-        QString result = "";
-        QSqlQuery query;
-        query.prepare("SELECT name "
-                      "FROM airports WHERE icao LIKE ? OR iata LIKE ?");
-        identifier.append("%");
-        identifier.prepend("%");
-        query.addBindValue(identifier);
-        query.addBindValue(identifier);
-        query.exec();
-        if(query.first())
-        {
-            result.append(query.value(0).toString());
-            return result;
-        }else
-        {
-            result = result.left(result.length()-1);
-            result.append("No matching airport found.");
-            return  result;
-        }
-    }
-
-    static QString RetreiveAirportIdFromIcao(QString identifier)
-    {
-        QString result;
-        QSqlQuery query;
-        query.prepare("SELECT airport_id FROM airports WHERE icao = ?");
-        query.addBindValue(identifier);
-        query.exec();
-
-        while(query.next())
-        {
-            result.append(query.value(0).toString());
-            //qDebug() << "db::RetreiveAirportIdFromIcao says Airport found! #" << result;
-        }
-
-        return result;
-    }
 
-    static QStringList CompleteIcaoOrIata(QString icaoStub)
-    {
-        QStringList result;
-        QSqlQuery query;
-        query.prepare("SELECT icao FROM airports WHERE icao LIKE ? OR iata LIKE ?");
-        icaoStub.prepend("%"); icaoStub.append("%");
-        query.addBindValue(icaoStub);
-        query.addBindValue(icaoStub);
-        query.exec();
-
-        while(query.next())
-        {
-            result.append(query.value(0).toString());
-            qDebug() << "db::CompleteIcaoOrIata says... Result:" << result;
-        }
-        return result;
-    }
-
-    /*!
-     * \brief CheckICAOValid Verifies if a user input airport exists in the database
-     * \param identifier can be ICAO or IATA airport codes.
-     * \return bool if airport is in database.
-     */
-    static bool CheckICAOValid(QString identifier)
-    {
-        if(identifier.length() == 4)
-        {
-            QString check = RetreiveAirportIdFromIcao(identifier);
-            if(check.length() > 0)
-            {
-                //qDebug() << "db::CheckICAOValid says: Check passed!";
-                return 1;
-            }else
-            {
-                //qDebug() << "db::CheckICAOValid says: Check NOT passed! Lookup unsuccessful";
-                return 0;
-            }
-        }else
-        {
-            //qDebug() << "db::CheckICAOValid says: Check NOT passed! Empty String NOT epico!";
-            return 0;
-        }
-    }
-
-    /*!
-     * \brief retreiveIcaoCoordinates Looks up coordinates (lat,long) for a given airport
-     * \param icao 4-letter code for the airport
-     * \return {lat,lon} in decimal degrees
-     */
-    static QVector<double> retreiveIcaoCoordinates(QString icao)
-    {
-        QSqlQuery query;
-        query.prepare("SELECT lat, long "
-                      "FROM airports "
-                      "WHERE icao = ?");
-        query.addBindValue(icao);
-        query.exec();
-
-        QVector<double> result;
-        while(query.next()) {
-            result.append(query.value(0).toDouble());
-            result.append(query.value(1).toDouble());
-        }
-        return result;
-    }
 
     /*
  * Aircraft Database Related Functions

+ 4 - 4
editflight.cpp

@@ -139,10 +139,10 @@ bool EditFlight::verifyInput()
     bool doftValid = true; //doft assumed to be always valid due to QDateTimeEdit constraints
     qDebug() << "EditFlight::verifyInput() says: Date:" << editdoft << " - Valid?\t" << doftValid;
 
-    deptValid = db::CheckICAOValid(editdept);
+    deptValid = dbAirport::checkICAOValid(editdept);
     qDebug() << "EditFlight::verifyInput() says: Departure is:\t" << editdept << " - Valid?\t" << deptValid;
 
-    destValid = db::CheckICAOValid(editdest);
+    destValid = dbAirport::checkICAOValid(editdest);
     qDebug() << "EditFlight::verifyInput() says: Destination is:\t" << editdest << " - Valid?\t" << destValid;
 
     tofbValid = (unsigned)(calc::time_to_minutes(edittofb)-0) <= (1440-0) && edittofb.toString("hh:mm") != ""; // Make sure time is within range, DB 1 day = 1440 minutes. 0 is allowed (midnight) & that it is not empty.
@@ -208,7 +208,7 @@ void EditFlight::on_newDept_textChanged(const QString &arg1)
     if(arg1.length() > 2)
     {
         QString result;
-        result = db::RetreiveAirportNameFromIcaoOrIata(arg1);
+        result = dbAirport::retreiveAirportNameFromIcaoOrIata(arg1);
         ui->deptHintlineEdit->setPlaceholderText(result);
     }
 }
@@ -218,7 +218,7 @@ void EditFlight::on_newDest_textChanged(const QString &arg1)
     if(arg1.length() > 2)
     {
         QString result;
-        result = db::RetreiveAirportNameFromIcaoOrIata(arg1);
+        result = dbAirport::retreiveAirportNameFromIcaoOrIata(arg1);
         ui->destHintlineEdit->setPlaceholderText(result);
     }
 }

+ 7 - 7
mainwindow.cpp

@@ -54,6 +54,7 @@ MainWindow::MainWindow(QWidget *parent)
     ui->actionHome->setIcon(QIcon(":/home_icon.svg"));
     ui->actionSettings->setIcon(QIcon(":/settings_icon.svg"));
     ui->actionQuit->setIcon(QIcon(":/quit_icon.svg"));
+    ui->actionNewFlight->setIcon(QIcon(":/new_flight_icon.jpg"));
 
     // Adds space between toolbar items and actionSetting item
     auto *spacer = new QWidget();
@@ -89,13 +90,6 @@ void MainWindow::nope()
  * Slots
  */
 
-
-void MainWindow::on_actionNew_Flight_triggered()
-{
-    NewFlight nf(this);
-    nf.exec();
-}
-
 void MainWindow::on_actionQuit_triggered()
 {
     QApplication::quit();
@@ -122,3 +116,9 @@ void MainWindow::on_actionSettings_triggered()
     ui->stackedWidget->addWidget(sw);
     ui->stackedWidget->setCurrentWidget(sw);
 }
+
+void MainWindow::on_actionNewFlight_triggered()
+{
+    NewFlight nf(this);
+    nf.exec();
+}

+ 2 - 2
mainwindow.h

@@ -36,8 +36,6 @@ private slots:
 
     void nope();
 
-    void on_actionNew_Flight_triggered();
-
     void on_actionQuit_triggered();
 
     void on_actionHome_triggered();
@@ -46,6 +44,8 @@ private slots:
 
     void on_actionSettings_triggered();
 
+    void on_actionNewFlight_triggered();
+
 private:
     Ui::MainWindow *ui;
 };

+ 3 - 3
mainwindow.ui

@@ -36,13 +36,13 @@
    </attribute>
    <addaction name="actionHome"/>
    <addaction name="actionLogbook"/>
-   <addaction name="actionNew_Flight"/>
+   <addaction name="actionNewFlight"/>
    <addaction name="actionSettings"/>
    <addaction name="actionQuit"/>
   </widget>
-  <action name="actionNew_Flight">
+  <action name="actionNewFlight">
    <property name="text">
-    <string>New Flight</string>
+    <string>NewFlight</string>
    </property>
    <property name="shortcut">
     <string>Ctrl+N</string>

+ 6 - 6
newflight.cpp

@@ -266,10 +266,10 @@ bool NewFlight::verifyInput()
     bool doftValid = true; //doft assumed to be always valid due to QDateTimeEdit constraints
     qDebug() << "NewFlight::verifyInput() says: Date:" << doft << " - Valid?\t" << doftValid;
 
-    deptValid = db::CheckICAOValid(dept);
+    deptValid = dbAirport::checkICAOValid(dept);
     qDebug() << "NewFlight::verifyInput() says: Departure is:\t" << dept << " - Valid?\t" << deptValid;
 
-    destValid = db::CheckICAOValid(dest);
+    destValid = dbAirport::checkICAOValid(dest);
     qDebug() << "NewFlight::verifyInput() says: Destination is:\t" << dest << " - Valid?\t" << destValid;
 
     tofbValid = (unsigned)(calc::time_to_minutes(tofb)-0) <= (1440-0) && tofb.toString("hh:mm") != ""; // Make sure time is within range, DB 1 day = 1440 minutes. 0 is allowed (midnight) & that it is not empty.
@@ -416,7 +416,7 @@ void NewFlight::on_newDept_textEdited(const QString &arg1)
     ui->newDept->setText(arg1.toUpper());
     if(arg1.length()>2)
     {
-        QStringList deptList = db::CompleteIcaoOrIata(arg1);
+        QStringList deptList = dbAirport::completeIcaoOrIata(arg1);
         qDebug() << "deptList = " << deptList;
         QCompleter *deptCompleter = new QCompleter(deptList, this);
         deptCompleter->setCaseSensitivity(Qt::CaseInsensitive);
@@ -440,7 +440,7 @@ void NewFlight::on_newDept_editingFinished()
 
     if(ui->newDept->text().length()>1)
     {
-        QStringList deptList = db::CompleteIcaoOrIata(ui->newDept->text());
+        QStringList deptList = dbAirport::completeIcaoOrIata(ui->newDept->text());
         if(deptList.length() != 0)
         {
             dept = deptList.first();
@@ -465,7 +465,7 @@ void NewFlight::on_newDest_textEdited(const QString &arg1)
     ui->newDest->setText(arg1.toUpper());
     if(arg1.length()>2)
     {
-        QStringList destList = db::CompleteIcaoOrIata(arg1);
+        QStringList destList = dbAirport::completeIcaoOrIata(arg1);
         QCompleter *destCompleter = new QCompleter(destList, this);
         destCompleter->setCaseSensitivity(Qt::CaseInsensitive);
         destCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
@@ -487,7 +487,7 @@ void NewFlight::on_newDest_editingFinished()
     QStringList destList;
     if(ui->newDest->text().length()>1)
     {
-        QStringList destList = db::CompleteIcaoOrIata(ui->newDest->text());
+        QStringList destList = dbAirport::completeIcaoOrIata(ui->newDest->text());
         if(destList.length() != 0)
         {
             dest = destList.first();

+ 1 - 0
newflight.h

@@ -24,6 +24,7 @@
 #include "dbsettings.h"
 #include "dbflight.h"
 #include "dbpilots.h"
+#include "dbairport.h"
 
 namespace Ui {
 class NewFlight;

+ 2 - 0
openLog.pro

@@ -24,6 +24,7 @@ DEFINES *= QT_USE_QSTRINGBUILDER # more efficient use of string concatenation
 
 SOURCES += \
     calc.cpp \
+    dbairport.cpp \
     dbflight.cpp \
     dbman.cpp \
     dbpilots.cpp \
@@ -42,6 +43,7 @@ SOURCES += \
 
 HEADERS += \
     calc.h \
+    dbairport.h \
     dbflight.h \
     dbpilots.h \
     dbsettings.h \

+ 1 - 0
themes/icons/icons.qrc

@@ -5,5 +5,6 @@
         <file>settings_icon.svg</file>
         <file>search_icon.svg</file>
         <file>quit_icon.svg</file>
+        <file>new_flight_icon.jpg</file>
     </qresource>
 </RCC>

BIN
themes/icons/new_flight_icon.jpg