Ver código fonte

refactored readcsv and calc into functions

Free functions are now organized in the src/functions folder. They are either grouped in a namespace or free, but in either way with both header and cpp files to make including them easier and provide uniformity and maintainabilitiy
Felix Turo 4 anos atrás
pai
commit
32b43242ad

+ 6 - 6
openPilotLog.pro

@@ -18,9 +18,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
 SOURCES += \
     main.cpp \
     mainwindow.cpp \
-    src/classes/acalc.cpp \
     src/classes/aircraft.cpp \
-    src/classes/csv.cpp \
     src/classes/download.cpp \
     src/classes/flight.cpp \
     src/classes/pilot.cpp \
@@ -33,7 +31,9 @@ SOURCES += \
     src/database/dbsetup.cpp \
     src/database/entry_deprecated.cpp \
     src/experimental/adatabase.cpp \
-    src/experimental/entry.cpp \
+    src/experimental/aentry.cpp \
+    src/functions/acalc.cpp \
+    src/functions/areadcsv.cpp \
     src/gui/dialogues/firstrundialog.cpp \
     src/gui/dialogues/newflightdialog.cpp \
     src/gui/dialogues/newpilotdialog.cpp \
@@ -49,9 +49,7 @@ SOURCES += \
 HEADERS += \
     debug.h \
     mainwindow.h \
-    src/classes/acalc.h \
     src/classes/aircraft.h \
-    src/classes/csv.h \
     src/classes/download.h \
     src/classes/flight.h \
     src/classes/pilot.h \
@@ -66,7 +64,9 @@ HEADERS += \
     src/experimental/Decl.h \
     src/experimental/UserInput.h \
     src/experimental/adatabase.h \
-    src/experimental/entry.h \
+    src/experimental/aentry.h \
+    src/functions/acalc.h \
+    src/functions/areadcsv.h \
     src/gui/dialogues/firstrundialog.h \
     src/gui/dialogues/newflightdialog.h \
     src/gui/dialogues/newpilotdialog.h \

+ 0 - 14
src/classes/csv.h

@@ -1,14 +0,0 @@
-#ifndef CSV_H
-#define CSV_H
-#include <QtCore>
-
-class Csv
-{
-public:
-    Csv();
-
-    static QVector<QStringList> read(QString filename);
-
-};
-
-#endif // CSV_H

+ 2 - 1
src/database/dbsetup.cpp

@@ -18,6 +18,7 @@
 #include "dbsetup.h"
 #include "debug.h"
 
+
 // Statements for creation of database tables, Revision 12
 
 const QString createTablePilots = "CREATE TABLE \"pilots\" ( "
@@ -291,7 +292,7 @@ bool DbSetup::importDefaultData()
             DEB("Error: " << query.lastError().text());
         }
         //fill with data from csv
-        if (!commitData(Csv::read("data/templates/" + table + ".csv"), table)) {
+        if (!commitData(aReadCsv("data/templates/" + table + ".csv"), table)) {
             DEB("Error importing data.");
             return false;
         }

+ 2 - 3
src/database/dbsetup.h

@@ -21,8 +21,7 @@
 #include <QCoreApplication>
 #include "src/database/db.h"
 #include "src/database/dbinfo.h"
-#include "src/classes/csv.h"
-
+#include "src/functions/areadcsv.h"
 
 /*!
  * \brief The DbSetup class is responsible for the inital setup of the database when
@@ -32,7 +31,7 @@
  */
 class DbSetup
 {
-public: 
+public:
     static void debug();
 
     static bool createDatabase();

+ 9 - 9
src/experimental/adatabase.cpp

@@ -57,7 +57,7 @@ QSqlDatabase ADataBase::database()
     return QSqlDatabase::database("qt_sql_default_connection");
 }
 
-bool ADataBase::commit(Entry entry)
+bool ADataBase::commit(AEntry entry)
 {
     if (exists(entry)) {
         return update(entry);
@@ -66,7 +66,7 @@ bool ADataBase::commit(Entry entry)
     }
 }
 
-bool ADataBase::remove(Entry entry)
+bool ADataBase::remove(AEntry entry)
 {
     if (!exists(entry)) {
         DEB("Error: Entry does not exist.");
@@ -91,7 +91,7 @@ bool ADataBase::remove(Entry entry)
     }
 }
 
-bool ADataBase::exists(Entry entry)
+bool ADataBase::exists(AEntry entry)
 {
     if(entry.getPosition() == DEFAULT_PILOT_POSITION)
         return false;
@@ -113,7 +113,7 @@ bool ADataBase::exists(Entry entry)
 }
 
 
-bool ADataBase::update(Entry updated_entry)
+bool ADataBase::update(AEntry updated_entry)
 {
     auto data = updated_entry.getData();
     QString statement = "UPDATE " + updated_entry.getPosition().tableName + " SET ";
@@ -144,7 +144,7 @@ bool ADataBase::update(Entry updated_entry)
     }
 }
 
-bool ADataBase::insert(Entry new_entry)
+bool ADataBase::insert(AEntry new_entry)
 {
     auto data = new_entry.getData();
     DEB("Inserting...");
@@ -224,16 +224,16 @@ TableData ADataBase::getEntryData(DataPosition data_position)
     return entry_data;
 }
 
-Entry ADataBase::getEntry(DataPosition data_position)
+AEntry ADataBase::getEntry(DataPosition data_position)
 {
-    Entry entry(data_position);
+    AEntry entry(data_position);
     entry.setData(getEntryData(data_position));
     return entry;
 }
 
-PilotEntry ADataBase::getPilotEntry(RowId row_id)
+APilotEntry ADataBase::getPilotEntry(RowId row_id)
 {
-    PilotEntry pilotEntry(row_id);
+    APilotEntry pilotEntry(row_id);
     pilotEntry.setData(getEntryData(pilotEntry.getPosition()));
     return pilotEntry;
 }

+ 8 - 8
src/experimental/adatabase.h

@@ -10,7 +10,7 @@
 #include "src/database/dbinfo.h"
 #include "debug.h"
 
-#include "entry.h"
+#include "aentry.h"
 
 namespace experimental {
 
@@ -57,28 +57,28 @@ public:
     /*!
      * \brief Checks if an entry exists in the database, based on position data
      */
-    bool exists(Entry entry);
+    bool exists(AEntry entry);
 
     /*!
      * \brief commits an entry to the database, calls either insert or update,
      * based on position data
      */
-    bool commit(Entry entry);
+    bool commit(AEntry entry);
 
     /*!
      * \brief Create new entry in the databse based on UserInput
      */
-    bool insert(Entry new_entry);
+    bool insert(AEntry new_entry);
 
     /*!
      * \brief Updates entry in database from existing entry tweaked by the user.
      */
-    bool update(Entry updated_entry);
+    bool update(AEntry updated_entry);
 
     /*!
      * \brief deletes an entry from the database.
      */
-    bool remove(Entry entry);
+    bool remove(AEntry entry);
 
     /*!
      * \brief retreive entry data from the database to create an entry object
@@ -88,7 +88,7 @@ public:
     /*!
      * \brief retreive an Entry from the database.
      */
-    Entry getEntry(DataPosition data_position);
+    AEntry getEntry(DataPosition data_position);
 
     /*!
      * \brief retreives a PilotEntry from the database.
@@ -98,7 +98,7 @@ public:
      * instead of an Entry. It allows for easy access to a pilot entry
      * with only the RowId required as input.
      */
-    PilotEntry getPilotEntry(RowId row_id);
+    APilotEntry getPilotEntry(RowId row_id);
     // [G] TODO: Ensure PilotDialog works great and slowly move to
     // other dialogs
 

+ 13 - 13
src/experimental/aentry.cpp

@@ -1,44 +1,44 @@
-#include "entry.h"
+#include "aentry.h"
 
 namespace experimental {
 
-Entry::Entry(DataPosition position_)
+AEntry::AEntry(DataPosition position_)
     : position(position_)
 {}
 
-Entry::Entry(TableData table_data)
+AEntry::AEntry(TableData table_data)
     : tableData(table_data)
 {}
 
-Entry::Entry(DataPosition position_, TableData table_data)
+AEntry::AEntry(DataPosition position_, TableData table_data)
     : position(position_), tableData(table_data)
 {}
 
-void Entry::setData(TableData table_data)
+void AEntry::setData(TableData table_data)
 {
     tableData = table_data;
 }
 
-const DataPosition& Entry::getPosition()
+const DataPosition& AEntry::getPosition()
 {
     return position;
 }
 
-const TableData& Entry::getData()
+const TableData& AEntry::getData()
 {
     return tableData;
 }
 
-PilotEntry::PilotEntry()
-    : Entry::Entry(DEFAULT_PILOT_POSITION)
+APilotEntry::APilotEntry()
+    : AEntry::AEntry(DEFAULT_PILOT_POSITION)
 {}
 
-PilotEntry::PilotEntry(int row_id)
-    : Entry::Entry(DataPosition("pilots", row_id))
+APilotEntry::APilotEntry(int row_id)
+    : AEntry::AEntry(DataPosition("pilots", row_id))
 {}
 
-PilotEntry::PilotEntry(TableData table_data)
-    : Entry::Entry(DEFAULT_PILOT_POSITION, table_data)
+APilotEntry::APilotEntry(TableData table_data)
+    : AEntry::AEntry(DEFAULT_PILOT_POSITION, table_data)
 {}
 
 }  // namespace experimental

+ 13 - 13
src/experimental/aentry.h

@@ -17,17 +17,17 @@ namespace experimental {
  * \brief The Entry class encapsulates table metadata(table name, row id)
  *  and data for new and existing entries in the database to operate on.
  */
-class Entry {
+class AEntry {
 protected:
     DataPosition position;
     TableData tableData;
 public:
-    Entry() = delete; // Demand specificity from default constructor
-    Entry(const Entry&) = default;
-    Entry& operator=(const Entry&) = default;
-    Entry(DataPosition position_);
-    Entry(TableData table_data);
-    Entry(DataPosition position_, TableData table_data);
+    AEntry() = delete; // Demand specificity from default constructor
+    AEntry(const AEntry&) = default;
+    AEntry& operator=(const AEntry&) = default;
+    AEntry(DataPosition position_);
+    AEntry(TableData table_data);
+    AEntry(DataPosition position_, TableData table_data);
 
     void setData(TableData table_data);
     void setPosition(DataPosition position_);
@@ -37,13 +37,13 @@ public:
 
 };
 
-struct PilotEntry : public Entry {
+struct APilotEntry : public AEntry {
 public:
-    PilotEntry();
-    PilotEntry(const PilotEntry& pe) = default;
-    PilotEntry& operator=(const PilotEntry& pe) = default;
-    PilotEntry(int row_id);
-    PilotEntry(TableData table_data);
+    APilotEntry();
+    APilotEntry(const APilotEntry& pe) = default;
+    APilotEntry& operator=(const APilotEntry& pe) = default;
+    APilotEntry(int row_id);
+    APilotEntry(TableData table_data);
 };
 
 }

+ 1 - 17
src/classes/acalc.cpp → src/functions/acalc.cpp

@@ -1,20 +1,3 @@
-/*
- *openPilot Log - A FOSS Pilot Logbook Application
- *Copyright (C) 2020  Felix Turowsky
- *
- *This program is free software: you can redistribute it and/or modify
- *it under the terms of the GNU General Public License as published by
- *the Free Software Foundation, either version 3 of the License, or
- *(at your option) any later version.
- *
- *This program is distributed in the hope that it will be useful,
- *but WITHOUT ANY WARRANTY; without even the implied warranty of
- *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *GNU General Public License for more details.
- *
- *You should have received a copy of the GNU General Public License
- *along with this program.  If not, see <https://www.gnu.org/licenses/>.
- */
 #include "acalc.h"
 #include "debug.h"
 
@@ -482,3 +465,4 @@ void ACalc::updateNightTimes()
         flt->commit();
     }
 }
+

+ 5 - 23
src/classes/acalc.h → src/functions/acalc.h

@@ -1,23 +1,5 @@
-/*
- *openPilot Log - A FOSS Pilot Logbook Application
- *Copyright (C) 2020  Felix Turowsky
- *
- *This program is free software: you can redistribute it and/or modify
- *it under the terms of the GNU General Public License as published by
- *the Free Software Foundation, either version 3 of the License, or
- *(at your option) any later version.
- *
- *This program is distributed in the hope that it will be useful,
- *but WITHOUT ANY WARRANTY; without even the implied warranty of
- *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *GNU General Public License for more details.
- *
- *You should have received a copy of the GNU General Public License
- *along with this program.  If not, see <https://www.gnu.org/licenses/>.
- */
-
-#ifndef CALC_H
-#define CALC_H
+#ifndef ACALC_H
+#define ACALC_H
 
 #include "src/database/db.h"
 #include "src/classes/aircraft.h"
@@ -27,10 +9,11 @@
 #include <cmath>
 #include <QDebug>
 /*!
- * \brief The calc class provides functionality for various calculations that are performed
+ * \brief The ACalc namespace provides various functions for calculations that are performed
  * outside of the database. This includes tasks like converting different units and formats,
  * or functions calculating block time or night time.
  */
+
 namespace ACalc {
 
 QTime blocktime(QTime tofb, QTime tonb);
@@ -72,5 +55,4 @@ void autoTimes(Flight, Aircraft);
 void updateNightTimes();
 } // namespace ACalc
 
-
-#endif // CALC_H
+#endif // ACALC_H

+ 3 - 7
src/classes/csv.cpp → src/functions/areadcsv.cpp

@@ -1,15 +1,11 @@
-#include "csv.h"
+#include "areadcsv.h"
 
-Csv::Csv()
-{
-
-}
 /*!
- * \brief Csv::read reads from a CSV file
+ * \brief aReadCSV reads from a CSV file
  * \param filename - QString to csv file.
  * \return QVector<QStringList> of the CSV data, where each QStringList is one column of the input file
  */
-QVector<QStringList> Csv::read(QString filename)
+QVector<QStringList> aReadCsv(QString filename)
 {
     QFile csvfile(filename);
     csvfile.open(QIODevice::ReadOnly);

+ 8 - 0
src/functions/areadcsv.h

@@ -0,0 +1,8 @@
+#ifndef AREADCSV_H
+#define AREADCSV_H
+
+#include<QtCore>
+
+QVector<QStringList> aReadCsv(QString filename);
+
+#endif // AREADCSV_H

+ 1 - 1
src/gui/dialogues/newflightdialog.h

@@ -40,7 +40,7 @@
 #include "src/classes/aircraft.h"
 #include "src/classes/strictrxvalidator.h"
 #include "src/classes/settings.h"
-#include "src/classes/acalc.h"
+#include "src/functions/acalc.h"
 #include "src/experimental/adatabase.h"
 
 #include "src/gui/dialogues/newpilotdialog.h"

+ 1 - 1
src/gui/dialogues/newpilotdialog.cpp

@@ -69,7 +69,7 @@ NewPilotDialog::NewPilotDialog(QWidget *parent) :
     DEB("New NewPilotDialog (newEntry)");
     setup();
 
-    pilotEntry = PilotEntry();
+    pilotEntry = APilotEntry();
     ui->piclastnameLineEdit->setFocus();
 }
 

+ 2 - 2
src/gui/dialogues/newpilotdialog.h

@@ -26,7 +26,7 @@
 #include "src/classes/pilot.h"
 
 #include "src/experimental/adatabase.h"
-#include "src/experimental/entry.h"
+#include "src/experimental/aentry.h"
 #include "src/experimental/Decl.h"
 
 namespace Ui {
@@ -52,7 +52,7 @@ private slots:
 private:
     Ui::NewPilot *ui;
 
-    experimental::PilotEntry pilotEntry;
+    experimental::APilotEntry pilotEntry;
 
     inline void setup();
 

+ 1 - 1
src/gui/dialogues/newtaildialog.h

@@ -26,7 +26,7 @@
 #include "src/classes/settings.h"
 #include "src/classes/aircraft.h"
 #include "src/classes/strictrxvalidator.h"
-#include "src/classes/acalc.h"
+#include "src/functions/acalc.h"
 #include "src/database/entry_deprecated.h"
 #include "src/experimental/adatabase.h"
 

+ 3 - 2
src/gui/widgets/debugwidget.cpp

@@ -2,6 +2,7 @@
 #include "ui_debugwidget.h"
 #include "debug.h"
 
+
 DebugWidget::DebugWidget(QWidget *parent) :
     QWidget(parent),
     ui(new Ui::DebugWidget)
@@ -109,7 +110,7 @@ void DebugWidget::on_fillUserDataPushButton_clicked()
     }
     QVector<bool> allGood;
     for (const auto& table : userTables) {
-        auto data = Csv::read("data/templates/sample_" + table + ".csv");
+        auto data = aReadCsv("data/templates/sample_" + table + ".csv");
         allGood.append(DbSetup::commitData(data, table));
     }
 
@@ -142,7 +143,7 @@ void DebugWidget::on_importCsvPushButton_clicked()
 
     if (file.exists() && file.isFile()) {
 
-        if (DbSetup::commitData(Csv::read(file.absoluteFilePath()), ui->tableComboBox->currentText())) {
+        if (DbSetup::commitData(aReadCsv(file.absoluteFilePath()), ui->tableComboBox->currentText())) {
             auto mb = QMessageBox(this);
             mb.setText("Data inserted successfully.");
             mb.exec();

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

@@ -7,10 +7,12 @@
 #include <QFile>
 #include <QFileDialog>
 #include <QMessageBox>
+#include <QProcess>
 #include "src/database/db.h"
 #include "src/database/dbsetup.h"
 #include "src/database/dbinfo.h"
 #include "src/classes/download.h"
+#include "src/functions/areadcsv.h"
 
 namespace Ui {
 class DebugWidget;

+ 1 - 1
src/gui/widgets/homewidget.h

@@ -24,7 +24,7 @@
 #include <QLineEdit>
 #include "src/database/db.h"
 #include "src/classes/stat.h"
-#include "src/classes/acalc.h"
+#include "src/functions/acalc.h"
 #include "src/gui/dialogues/newtaildialog.h"
 #include "src/classes/aircraft.h"
 #include "src/gui/dialogues/newpilotdialog.h"