Browse Source

StyledDelegate for Dates

The logbook views now get styled by custom delegates and the database stores date values as julian days.
Felix Turowsky 1 year ago
parent
commit
49265c3675

+ 10 - 13
CMakeLists.txt

@@ -120,8 +120,16 @@ set(PROJECT_SOURCES
     src/classes/time.cpp
     src/classes/easaftl.h
     src/classes/easaftl.cpp
-    src/classes/defaultlogbookview.h
-    src/classes/defaultlogbookview.cpp
+    src/classes/date.h
+    src/classes/date.cpp
+    src/classes/styleddatedelegate.h
+    src/classes/styleddatedelegate.cpp
+    src/classes/styledtimedelegate.h
+    src/classes/styledtimedelegate.cpp
+    src/classes/styledpilotdelegate.h
+    src/classes/styledpilotdelegate.cpp
+    src/classes/styledregistrationdelegate.h
+    src/classes/styledregistrationdelegate.cpp
 
     # Database Entries
     src/database/flightentry.h
@@ -138,17 +146,6 @@ set(PROJECT_SOURCES
     src/database/simulatorentry.cpp
     src/database/currencyentry.h
     src/database/currencyentry.cpp
-    src/classes/date.h
-    src/classes/date.cpp
-    src/classes/styleddatedelegate.h
-    src/classes/styleddatedelegate.cpp
-    src/classes/styledtimedelegate.h
-    src/classes/styledtimedelegate.cpp
-    src/classes/styledpilotdelegate.h
-    src/classes/styledpilotdelegate.cpp
-    src/classes/styledregistrationdelegate.h
-    src/classes/styledregistrationdelegate.cpp
-
     src/database/previousexperienceentry.h
     src/database/previousexperienceentry.cpp
 

+ 1 - 7
mainwindow.cpp

@@ -17,7 +17,6 @@
  */
 #include <QToolBar>
 #include "mainwindow.h"
-#include "src/classes/defaultlogbookview.h"
 #include "ui_mainwindow.h"
 #include "src/database/database.h"
 #include "src/classes/style.h"
@@ -30,12 +29,7 @@
 // Quick and dirty Debug area
 void MainWindow::doDebugStuff()
 {
-    auto widget = new QWidget(this);
-    widget->setWindowFlags(Qt::Dialog);
-    auto layout = new QGridLayout();
-    layout->addWidget(new DefaultLogbookView(this));
-    widget->setLayout(layout);
-    widget->show();
+
 }
 
 MainWindow::MainWindow(QWidget *parent)

+ 4 - 0
src/classes/date.h

@@ -31,6 +31,10 @@ public:
 
     const static inline Date fromJulianDay(int julianDay) { return Date(julianDay); }
 
+    const static inline Date today() { return Date(QDate::currentDate().toJulianDay()); }
+
+    const static inline QString julianDayToString(int julianDay) { return Date(julianDay).toString(); }
+
     const QString toString(Format format = Format::Default) const;
 
 private:

+ 0 - 65
src/classes/defaultlogbookview.cpp

@@ -1,65 +0,0 @@
-#include "defaultlogbookview.h"
-#include "QtWidgets/qheaderview.h"
-#include "src/classes/styleddatedelegate.h"
-#include "src/classes/styledpilotdelegate.h"
-#include "src/classes/styledregistrationdelegate.h"
-#include "src/classes/styledtimedelegate.h"
-#include "src/database/database.h"
-#include "src/classes/settings.h"
-
-DefaultLogbookView::DefaultLogbookView(QWidget *parent)
-    : QTableView(parent)
-{
-    auto model = new QSqlTableModel(this, DB->database());
-    model->setTable(OPL::GLOBALS->getDbTableName(OPL::DbTable::Flights));
-    model->select();
-
-    model->setHeaderData(COL_DATE, Qt::Horizontal, tr("Date of Flight"));
-    model->setHeaderData(COL_DEPT, Qt::Horizontal, tr("Dept"));
-    model->setHeaderData(COL_DEST, Qt::Horizontal, tr("Dest"));
-    model->setHeaderData(COL_TOFB, Qt::Horizontal, tr("Time"));
-    model->setHeaderData(COL_TONB, Qt::Horizontal, tr("Time"));
-    model->setHeaderData(COL_PIC, Qt::Horizontal, tr("Name PIC"));
-    model->setHeaderData(COL_ACFT, Qt::Horizontal, tr("Registration"));
-    model->setHeaderData(COL_FLIGHT_NR, Qt::Horizontal, tr("Flight #"));
-    model->setHeaderData(COL_REMARKS, Qt::Horizontal, tr("Remarks"));
-
-    // set the item delegates for converting db values to human readable formatting
-    const auto dateDelegate = new StyledDateDelegate(Settings::getDateFormat(), this);
-    setItemDelegateForColumn(COL_DATE, dateDelegate);
-
-    const auto timeDelegate = new StyledTimeDelegate(this);
-    setItemDelegateForColumn(COL_TOFB, timeDelegate);
-    setItemDelegateForColumn(COL_TONB, timeDelegate);
-    setItemDelegateForColumn(COL_TBLK, timeDelegate);
-
-    const auto pilotDelegate = new StyledPilotDelegate(this);
-    setItemDelegateForColumn(COL_PIC, pilotDelegate);
-
-    const auto registrationDelegate = new StyledRegistrationDelegate(this);
-    setItemDelegateForColumn(COL_ACFT, registrationDelegate);
-
-
-    setModel(model);
-    setSelectionBehavior(QAbstractItemView::SelectRows);
-    setSelectionMode(QAbstractItemView::ExtendedSelection);
-    setEditTriggers(QAbstractItemView::NoEditTriggers);
-    setContextMenuPolicy(Qt::CustomContextMenu);
-    resizeColumnsToContents();
-    horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
-    verticalHeader()->hide();
-    setAlternatingRowColors(true);
-
-    for(int i = 0; i < COLUMN_COUNT; i++) {
-        hideColumn(i);
-    }
-    showColumn(COL_DATE);
-    showColumn(COL_ACFT);
-    showColumn(COL_PIC);
-    showColumn(COL_TONB);
-    showColumn(COL_DEPT);
-    showColumn(COL_DEST);
-    showColumn(COL_TOFB);
-    showColumn(COL_FLIGHT_NR);
-    showColumn(COL_REMARKS);
-}

+ 0 - 49
src/classes/defaultlogbookview.h

@@ -1,49 +0,0 @@
-#ifndef DEFAULTLOGBOOKVIEW_H
-#define DEFAULTLOGBOOKVIEW_H
-
-#include <QTableView>
-
-class DefaultLogbookView : public QTableView
-{
-public:
-    DefaultLogbookView(QWidget *parent = nullptr);
-private:
-    const static int COL_ID   = 0;
-    const static int COL_DATE = 1;
-    const static int COL_DEPT = 2;
-    const static int COL_DEST = 3;
-    const static int COL_TOFB = 4;
-    const static int COL_TONB = 5;
-    const static int COL_PIC  = 6;
-    const static int COL_ACFT = 7;
-    const static int COL_TBLK = 8;
-    const static int COL_TSPSE = 9;
-    const static int COL_TSPME = 10;
-    const static int COL_TMP = 11;
-    const static int COL_TNIGHT = 12;
-    const static int COL_TIFR = 13;
-    const static int COL_TPIC = 14;
-    const static int COL_TPICUS = 15;
-    const static int COL_TSIC = 16;
-    const static int COL_TDUAL = 17;
-    const static int COL_TFI = 18;
-    const static int COL_TSIM = 19;// do not use, use sim table TODO: remove from DB
-    const static int COL_PF = 20;
-    const static int COL_TO_DAY = 21;
-    const static int COL_TO_NIGHT = 22;
-    const static int COL_LDG_DAY = 23;
-    const static int COL_LDG_NIGHT = 24;
-    const static int COL_AUTOLAND = 25;
-    const static int COL_SECOND_PILOT = 26;
-    const static int COL_THIRD_PILOT = 27;
-    const static int COL_APP_TYPE = 28;
-    const static int COL_FLIGHT_NR = 29;
-    const static int COL_REMARKS = 30;
-
-    const static int COLUMN_COUNT = 31;
-
-
-
-};
-
-#endif // DEFAULTLOGBOOKVIEW_H

+ 12 - 1
src/database/databasecache.cpp

@@ -58,6 +58,11 @@ const IdMap DatabaseCache::fetchMap(CompleterTarget target)
     case Tails:
         statement.append(QStringLiteral("SELECT ROWID, registration FROM tails"));
         break;
+    case Types:
+        statement.append(QStringLiteral("SELECT ROWID, make||' '||model FROM tails WHERE model IS NOT NULL AND variant IS NULL "
+                                        " UNION "
+                                        " SELECT ROWID, make||' '||model||'-'||variant FROM tails WHERE variant IS NOT NULL"));
+        break;
     case AircraftTypes:
         statement.append(QStringLiteral("SELECT ROWID, make||' '||model FROM aircraft WHERE model IS NOT NULL AND variant IS NULL "
                          "UNION "
@@ -132,6 +137,7 @@ void DatabaseCache::updateTails()
             reg = copy + " (" + reg + QLatin1Char(')');
         }
     }
+    typesMap = fetchMap(Types);
 }
 
 void DatabaseCache::updateAirports()
@@ -144,7 +150,7 @@ void DatabaseCache::updateAirports()
 
 void DatabaseCache::updateSimulators()
 {
-    TODO << "not yet implemented";
+    TODO << "Simulators map not yet cached";
 }
 
 void DatabaseCache::updatePilots()
@@ -239,6 +245,11 @@ const IdMap &DatabaseCache::getTailsMap() const
     return tailsMap;
 }
 
+const IdMap &DatabaseCache::getTypesMap() const
+{
+    return typesMap;
+}
+
 
 
 } // namespace OPL

+ 9 - 1
src/database/databasecache.h

@@ -44,7 +44,7 @@ public:
     DatabaseCache(DatabaseCache const&) = delete;
     void operator=(DatabaseCache const&) = delete;
 
-    enum CompleterTarget {PilotNames, Tails, AircraftTypes, AirportsAny, AirportsICAO, AirportNames, AirportsIATA, Companies};
+    enum CompleterTarget {PilotNames, Tails, AircraftTypes, AirportsAny, AirportsICAO, AirportNames, AirportsIATA, Companies, Types};
 
     void init();
 
@@ -52,6 +52,7 @@ public:
     const IdMap &getAirportsMapIATA() const;
     const IdMap &getPilotNamesMap() const;
     const IdMap &getTailsMap() const;
+    const IdMap &getTypesMap() const;
 
     const QStringList &getPilotNamesList() const;
     const QStringList &getTailsList() const;
@@ -74,7 +75,14 @@ private:
     IdMap airportsMapIATA;
     IdMap airportsMapNames;
     IdMap pilotNamesMap;
+    /*!
+     * \brief key: tail_id / value: registration
+     */
     IdMap tailsMap;
+    /*!
+     * \brief key: tail_id value: type string ("Boeing 737-800")
+     */
+    IdMap typesMap;
     IdMap aircraftMap;
     // Lists
     QStringList pilotNamesList;

+ 5 - 3
src/gui/dialogues/newflightdialog.cpp

@@ -29,6 +29,7 @@
 #include "src/opl.h"
 #include "src/functions/datetime.h"
 #include "src/classes/settings.h"
+#include "src/classes/date.h"
 #include "src/functions/calc.h"
 #include "src/gui/dialogues/newtaildialog.h"
 #include "src/gui/dialogues/newpilotdialog.h"
@@ -44,7 +45,7 @@ NewFlightDialog::NewFlightDialog(QWidget *parent)
     init();
     setPilotFunction();
 
-    ui->doftLineEdit->setText(OPL::DateTime::currentDate());
+    ui->doftLineEdit->setText(OPL::Date::today().toString(dateFormat));
     emit ui->doftLineEdit->editingFinished();
 }
 
@@ -176,6 +177,7 @@ void NewFlightDialog::readSettings()
     ui->approachComboBox->setCurrentText(Settings::getApproachType());
     ui->flightRulesComboBox->setCurrentIndex(Settings::getLogIfr());
     ui->flightNumberLineEdit->setText(Settings::getFlightNumberPrefix());
+    dateFormat = Settings::getDateFormat();
 }
 
 /*!
@@ -190,7 +192,7 @@ void NewFlightDialog::fillWithEntryData()
     const auto &flight_data = flightEntry.getData();
 
     // Date of Flight
-    ui->doftLineEdit->setText(flight_data.value(OPL::FlightEntry::DOFT).toString());
+    ui->doftLineEdit->setText(OPL::Date::julianDayToString(flight_data.value(OPL::FlightEntry::DOFT).toInt()));
     // Location
     ui->deptLocationLineEdit->setText(flight_data.value(OPL::FlightEntry::DEPT).toString());
     ui->destLocationLineEdit->setText(flight_data.value(OPL::FlightEntry::DEST).toString());
@@ -386,7 +388,7 @@ OPL::RowData_T NewFlightDialog::prepareFlightEntryData()
     const auto night_time_data = OPL::Calc::NightTimeValues(ui->deptLocationLineEdit->text(), ui->destLocationLineEdit->text(),
                            departure_date_time, block_minutes, Settings::getNightAngle());
     // Mandatory data
-    new_data.insert(OPL::FlightEntry::DOFT, ui->doftLineEdit->text());
+    new_data.insert(OPL::FlightEntry::DOFT, QDate::fromString(ui->doftLineEdit->text(), Qt::ISODate).toJulianDay());
     new_data.insert(OPL::FlightEntry::DEPT, ui->deptLocationLineEdit->text());
     new_data.insert(OPL::FlightEntry::TOFB, tofb.toMinutes());
     new_data.insert(OPL::FlightEntry::DEST, ui->destLocationLineEdit->text());

+ 3 - 0
src/gui/dialogues/newflightdialog.h

@@ -28,6 +28,7 @@
 #include "src/gui/verification/userinput.h"
 #include "src/opl.h"
 #include "src/gui/verification/validationstate.h"
+#include "src/classes/date.h"
 
 namespace Ui {
 class NewFlightDialog;
@@ -78,6 +79,8 @@ private:
     Ui::NewFlightDialog *ui;
     ValidationState validationState;
 
+    OPL::Date::Format dateFormat;
+
     /*!
      * \brief a AFlightEntry object that is used to store either position data
      * from an old entry, is used to fill the form for editing an entry, or is

+ 7 - 3
src/gui/widgets/logbookwidget.cpp

@@ -15,7 +15,7 @@
  *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 "src/classes/defaultlogbookview.h"
+#include "src/classes/styleddatedelegate.h"
 #include "src/classes/time.h"
 #include "src/database/database.h"
 #include "logbookwidget.h"
@@ -68,17 +68,21 @@ void LogbookWidget::setupModelAndView(OPL::LogbookView logbookView)
     displayModel->setTable(OPL::GLOBALS->getViewIdentifier(OPL::LogbookView(view_id)));
     displayModel->select();
 
+
     view->setModel(displayModel);
     view->setSelectionBehavior(QAbstractItemView::SelectRows);
     view->setSelectionMode(QAbstractItemView::ExtendedSelection);
     view->setEditTriggers(QAbstractItemView::NoEditTriggers);
     view->setContextMenuPolicy(Qt::CustomContextMenu);
-    view->resizeColumnsToContents();
     view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
     view->verticalHeader()->hide();
     view->setAlternatingRowColors(true);
     view->hideColumn(0);
-    view->show();
+
+    // Set a custom delegate on the date column to style the date according to the users preference
+    const auto dateDelegate = new StyledDateDelegate(Settings::getDateFormat(), this);
+    view->setItemDelegateForColumn(1, dateDelegate);
+    view->resizeColumnsToContents();
 }
 
 void LogbookWidget::connectSignalsAndSlots()