Browse Source

Rework of date/time interface

Date and Time should always be handled with formatting information. This has a small overhead, but allows for using different (and even custom) date and time formats throughout the application.
Felix Turowsky 1 year ago
parent
commit
7bb55d4619

+ 11 - 4
mainwindow.cpp

@@ -33,10 +33,17 @@
 // Quick and dirty Debug area
 void MainWindow::doDebugStuff()
 {
-    LogbookTableEditWidget *widget = new LogbookTableEditWidget(this);
-    widget->init();
-    widget->setWindowFlags(Qt::Dialog);
-    widget->show();
+    const OPL::DateTimeFormat defaultFormat(
+        OPL::DateTimeFormat::DateFormat::Default,
+        QStringLiteral("yyyy-MM-dd"),
+        OPL::DateTimeFormat::TimeFormat::Default,
+        QStringLiteral("hh:mm")
+        );
+    Settings::setDisplayFormat(defaultFormat);
+//    LogbookTableEditWidget *widget = new LogbookTableEditWidget(this);
+//    widget->init();
+//    widget->setWindowFlags(Qt::Dialog);
+//    widget->show();
 }
 
 MainWindow::MainWindow(QWidget *parent)

+ 34 - 10
src/classes/date.cpp

@@ -1,21 +1,45 @@
 #include "date.h"
+#include <QLocale>
 
 namespace OPL {
 
-Date::Date(int julianDay)
+Date::Date(int julianDay, const DateTimeFormat &format)
+    : m_format(format)
 {
-    date = QDate::fromJulianDay(julianDay);
+    m_date = QDate::fromJulianDay(julianDay);
 }
 
-const QString Date::toString(Format format) const
+Date::Date(const QString &textDate, const DateTimeFormat &format)
+    : m_format(format)
 {
-    switch (format) {
-    case Default:
-        return date.toString(Qt::ISODate);
-    case SystemLocaleLong:
-        return date.toString(QLocale::system().dateFormat(QLocale::LongFormat));
-    case SystemLocaleShort:
-        return date.toString(QLocale::system().dateFormat(QLocale::ShortFormat));
+    switch(format.dateFormat()) {
+    case DateTimeFormat::DateFormat::Default:
+        m_date = QDate::fromString(textDate, Qt::ISODate);
+        break;
+    case DateTimeFormat::DateFormat::Custom:
+        m_date = QDate::fromString(textDate, format.dateFormatString());
+        break;
+    case DateTimeFormat::DateFormat::SystemLocale:
+        m_date = QDate::fromString(QLocale::system().dateFormat(QLocale::ShortFormat));
+        break;
+    default:
+        break;
+    }
+}
+
+Date::Date(const QDate &date, const DateTimeFormat &format)
+    : m_format(format), m_date(date)
+{}
+
+const QString Date::toString() const
+{
+    switch (m_format.dateFormat()) {
+    case DateTimeFormat::DateFormat::Default:
+        return m_date.toString(Qt::ISODate);
+    case DateTimeFormat::DateFormat::SystemLocale:
+        return m_date.toString(QLocale::system().dateFormat(QLocale::ShortFormat));
+    case DateTimeFormat::DateFormat::Custom:
+        return m_date.toString(m_format.dateFormatString());
     default:
         return QString();
     }

+ 16 - 36
src/classes/date.h

@@ -1,7 +1,7 @@
 #ifndef DATE_H
 #define DATE_H
 #include <QDate>
-#include <QLocale>
+#include "src/opl.h"
 namespace OPL {
 
 /*!
@@ -12,54 +12,34 @@ namespace OPL {
  *
  * Storing a given date as an integer value allows for easy conversion to localised strings
  * as well as calculations like date ranges.
+ *
+ * Julian day is also used to store a date in the database.
  */
 class Date
 {
 public:
 
-    /*!
-     * \brief Enumerates how dates can be formatted to a localised format.
-     * \value Default - The Application standard, Equivalent to Qt::ISODate
-     * \value SystemLocaleLong - The current system locale, elaborate
-     * \value SystemLocaleShort - The current system locale, short
-     */
-    enum Format {Default, SystemLocaleLong, SystemLocaleShort};
-
     Date() = delete;
+    Date(int julianDay, const DateTimeFormat &format);
+    Date(const QString &textDate, const DateTimeFormat &format);
+    Date(const QDate &date, const DateTimeFormat &format);
 
-    Date(int julianDay);
-
-    const QString toString(Format format = Format::Default) const;
-
-    const bool isValid() const { return date.isValid(); }
-
-    const static inline Date fromString(const QString &dateString, Format format = Default) {
-        switch (format) {
-        case Default:
-            return Date(QDate::fromString(dateString, Qt::ISODate).toJulianDay());
-        case SystemLocaleLong:
-            return Date(QDate::fromString(dateString, QLocale::system().dateFormat(QLocale::LongFormat)).toJulianDay());
-        case SystemLocaleShort:
-            return Date(QDate::fromString(dateString, QLocale::system().dateFormat(QLocale::ShortFormat)).toJulianDay());
-        default:
-            return Date(0);
-            break;
-        }
-    }
+    const QString toString() const;
+    const bool isValid() const { return m_date.isValid(); }
 
-    const inline int toJulianDay() const { return date.toJulianDay(); }
+    const inline int toJulianDay() const { return m_date.toJulianDay(); }
+    const static inline Date today(const DateTimeFormat &format) { return Date(QDate::currentDate().toJulianDay(), format); }
 
-    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(); }
+//    void setDateFormat(const DateFormat_ &format) {m_format = format}
+    // todo copy constructor
 
 private:
-    QDate date;
-
+    QDate m_date;
+    DateTimeFormat m_format;
 };
 
+
+
 } // namespace OPL
 
 #endif // DATE_H

+ 27 - 1
src/classes/settings.cpp

@@ -35,7 +35,6 @@ void Settings::resetToDefaults()
     setApplicationStyle(QStringLiteral("Fusion"));
     setUseSystemFont(true);
     setLogbookView(OPL::LogbookView::Default);
-    setDateFormat(OPL::Date::Format::Default);
     setLogAsPilotFlying(true);
     setNightAngle(-6);
     setShowSelfAs(0);
@@ -43,6 +42,33 @@ void Settings::resetToDefaults()
     setCurrencyWarningThreshold(90);
     setPilotSortColumn(0);
     setTailSortColumn(0);
+    setDisplayFormat(OPL::DateTimeFormat());
+
     sync();
 }
 
+OPL::DateTimeFormat Settings::getDisplayFormat()
+{
+    using namespace OPL;
+
+    // date format
+    const DateTimeFormat::DateFormat dateFormat = static_cast<DateTimeFormat::DateFormat>(
+        settingsInstance->value(FORMAT_DATE_FORMAT, 0).toInt());
+    const QString dateFormatString = settingsInstance->value(FORMAT_DATE_STRING, QStringLiteral("yyyy-MM-dd")).toString();
+    // time format
+    const DateTimeFormat::TimeFormat timeFormat = static_cast<DateTimeFormat::TimeFormat>(
+        settingsInstance->value(FORMAT_TIME_FORMAT, 0).toInt());
+    const QString timeFormatString = settingsInstance->value(FORMAT_TIME_STRING, QStringLiteral("hh:mm")).toString();
+
+    return DateTimeFormat(dateFormat, dateFormatString, timeFormat, timeFormatString);
+
+}
+
+void Settings::setDisplayFormat(const OPL::DateTimeFormat &format)
+{
+    settingsInstance->setValue(FORMAT_DATE_FORMAT, static_cast<int>(format.dateFormat()));
+    settingsInstance->setValue(FORMAT_DATE_STRING, format.dateFormatString());
+    settingsInstance->setValue(FORMAT_TIME_FORMAT, static_cast<int>(format.timeFormat()));
+    settingsInstance->setValue(FORMAT_TIME_STRING, format.timeFormatString());
+}
+

+ 10 - 6
src/classes/settings.h

@@ -18,7 +18,6 @@
 #ifndef SETTINGS_H
 #define SETTINGS_H
 #include "src/opl.h"
-#include "src/classes/date.h"
 #include <QtCore>
 #include <QSettings>
 
@@ -108,14 +107,14 @@ public:
     static void setLogbookView(OPL::LogbookView view) { (settingsInstance->setValue(MAIN_LOGBOOK_VIEW, static_cast<int>(view))); }
 
     /*!
-     * \brief returns the date format to be used in the application
+     * \brief returns the Display Format used in the application
      */
-    static OPL::Date::Format getDateFormat() { return OPL::Date::Format(settingsInstance->value(MAIN_DATE_FORMAT, OPL::Date::Format::Default).toInt()); }
+    static OPL::DateTimeFormat getDisplayFormat();
 
     /*!
-     * \brief sets the date format to be used in the application
+     * \brief sets the Display Format used in the application
      */
-    static void setDateFormat(OPL::Date::Format format) { (settingsInstance->setValue(MAIN_DATE_FORMAT, static_cast<int>(format))); }
+    static void setDisplayFormat(const OPL::DateTimeFormat &format);
 
     /*!
      * \brief returns the default pilot function for new flights
@@ -238,7 +237,12 @@ private:
     const static inline QString MAIN_FONT_SIZE 		 	= QStringLiteral("main/fontSize");
     const static inline QString MAIN_USE_SYSTEM_FONT 	= QStringLiteral("main/useSystemFont");
     const static inline QString MAIN_LOGBOOK_VIEW 	 	= QStringLiteral("main/logbookView");
-    const static inline QString MAIN_DATE_FORMAT 	 	= QStringLiteral("main/dateFormat");
+
+    const static inline QString FORMAT_DATE_FORMAT 	= QStringLiteral("format/dateFormat");
+    const static inline QString FORMAT_DATE_STRING 	= QStringLiteral("format/dateFormatString");
+    const static inline QString FORMAT_TIME_FORMAT	= QStringLiteral("format/timeFormat");
+    const static inline QString FORMAT_TIME_STRING 	= QStringLiteral("format/timeFormatString");
+
 
 };
 

+ 4 - 4
src/classes/styleddatedelegate.cpp

@@ -1,13 +1,13 @@
 #include "styleddatedelegate.h"
+#include "src/classes/date.h"
 
-StyledDateDelegate::StyledDateDelegate(OPL::Date::Format dateFormat, QObject *parent)
+StyledDateDelegate::StyledDateDelegate(const OPL::DateTimeFormat &dateFormat, QObject *parent)
     :
     QStyledItemDelegate(parent),
-    m_dateFormat(dateFormat)
+    m_format(dateFormat)
 {}
 
 QString StyledDateDelegate::displayText(const QVariant &value, const QLocale &locale) const
 {
-    const OPL::Date date(value.toInt());
-    return date.toString(m_dateFormat);
+    return OPL::Date(value.toInt(), m_format).toString();
 }

+ 3 - 3
src/classes/styleddatedelegate.h

@@ -2,7 +2,7 @@
 #define STYLEDDATEDELEGATE_H
 
 #include <QStyledItemDelegate>
-#include "src/classes/date.h"
+#include "src/opl.h"
 
 /*!
  * \brief The StyledDateDelegate class is used to display a database date value human-readable.
@@ -13,11 +13,11 @@
 class StyledDateDelegate : public QStyledItemDelegate
 {
 public:
-    StyledDateDelegate(OPL::Date::Format dateFormat, QObject * parent = nullptr);
+    StyledDateDelegate(const OPL::DateTimeFormat &dateFormat, QObject * parent = nullptr);
 
     QString displayText(const QVariant &value, const QLocale &locale) const override;
 private:
-    OPL::Date::Format m_dateFormat;
+    OPL::DateTimeFormat m_format;
 };
 
 #endif // STYLEDDATEDELEGATE_H

+ 2 - 2
src/database/currencyentry.cpp

@@ -47,9 +47,9 @@ void CurrencyEntry::setExpiryDate(const Date &date)
     setData(data);
 }
 
-const Date CurrencyEntry::getExpiryDate() const
+const Date CurrencyEntry::getExpiryDate(const OPL::DateTimeFormat &format) const
 {
-    return OPL::Date(getData().value(EXPIRYDATE).toInt());
+    return OPL::Date(getData().value(EXPIRYDATE).toInt(), format);
 }
 
 } // namespace OPL

+ 1 - 1
src/database/currencyentry.h

@@ -44,7 +44,7 @@ public:
     const QString getName() const;
 
     void setExpiryDate(const OPL::Date &date);
-    const OPL::Date getExpiryDate() const;
+    const OPL::Date getExpiryDate(const OPL::DateTimeFormat &format) const;
 
 
 

+ 1 - 1
src/database/flightentry.cpp

@@ -46,7 +46,7 @@ const QString FlightEntry::getFlightSummary() const
     auto tableData = getData();
     QString flight_summary;
     auto space = QLatin1Char(' ');
-    flight_summary.append(OPL::Date(tableData.value(OPL::FlightEntry::DOFT).toInt()).toString() + space);
+    flight_summary.append(OPL::Date(tableData.value(OPL::FlightEntry::DOFT).toInt(), DateTimeFormat()).toString() + space);
     flight_summary.append(tableData.value(OPL::FlightEntry::DEPT).toString() + space);
     flight_summary.append(OPL::Time(tableData.value(OPL::FlightEntry::TOFB).toInt()).toString()
                           + space);

+ 3 - 3
src/functions/datetime.h

@@ -69,11 +69,11 @@ public:
      * \brief dateTimeToString formats a QDateTime object into a string in a uniform way.
      * \return
      */
-    static inline const QString dateTimeToString (const QDateTime& date_time, OPL::DateTimeFormat format) {
+    static inline const QString dateTimeToString (const QDateTime& date_time, OPL::DateTimeFormat_deprecated format) {
         switch (format) {
-        case OPL::DateTimeFormat::Default:
+        case OPL::DateTimeFormat_deprecated::Default:
             return date_time.toString(Qt::ISODate);
-        case OPL::DateTimeFormat::Backup:
+        case OPL::DateTimeFormat_deprecated::Backup:
             return date_time.toString(QStringLiteral("yyyy_MM_dd_T_hh_mm"));
         default:
             return QString();

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

@@ -370,7 +370,7 @@ bool FirstRunDialog::writeCurrencies()
         const QDate date = pair.second->date();
         if(date != today) {
             int julianDay = date.toJulianDay();
-            currencyEntry.setExpiryDate(OPL::Date(julianDay));
+            currencyEntry.setExpiryDate(OPL::Date(julianDay, m_format));
         }
 
         if(!DB->commit(currencyEntry))

+ 8 - 0
src/gui/dialogues/firstrundialog.h

@@ -96,6 +96,14 @@ private:
     Ui::FirstRunDialog *ui;
     bool useRessourceData;
 
+    //TODO load from settings
+    OPL::DateTimeFormat m_format = OPL::DateTimeFormat(
+        OPL::DateTimeFormat::DateFormat::Default,
+        QStringLiteral("yyyy-MM-dd"),
+        OPL::DateTimeFormat::TimeFormat::Default,
+        QStringLiteral("hh:mm")
+        );
+
     /*!
      * \brief finishSetup - once all the necessary data is entered by the user, this functions executes the steps necessary
      * to collect the data, process it and create the database

+ 10 - 7
src/gui/dialogues/newflightdialog.cpp

@@ -46,7 +46,7 @@ NewFlightDialog::NewFlightDialog(QWidget *parent)
     init();
     setPilotFunction();
 
-    ui->doftLineEdit->setText(OPL::Date::today().toString(dateFormat));
+    ui->doftLineEdit->setText(OPL::Date::today(m_format).toString());
     emit ui->doftLineEdit->editingFinished();
 }
 
@@ -187,7 +187,8 @@ void NewFlightDialog::readSettings()
     ui->approachComboBox->setCurrentText(Settings::getApproachType());
     ui->flightRulesComboBox->setCurrentIndex(Settings::getLogIfr());
     ui->flightNumberLineEdit->setText(Settings::getFlightNumberPrefix());
-    dateFormat = Settings::getDateFormat();
+
+    m_format = Settings::getDisplayFormat();
 }
 
 /*!
@@ -198,13 +199,15 @@ void NewFlightDialog::fillWithEntryData()
 {
     DEB << "Restoring Flight: ";
     DEB << flightEntry;
+    using namespace OPL;
 
     const auto &flight_data = flightEntry.getData();
 
     // Date of Flight
-    QDate date = QDate::fromJulianDay(flight_data.value(OPL::FlightEntry::DOFT).toInt());
+    const QDate date = QDate::fromJulianDay(flight_data.value(FlightEntry::DOFT).toInt());
     calendar->setSelectedDate(date);
-    ui->doftLineEdit->setText(OPL::Date::fromJulianDay(date.toJulianDay()).toString(dateFormat));
+    ui->doftLineEdit->setText(Date(date, m_format).toString());
+//    ui->doftLineEdit->setText(OPL::Date::fromJulianDay(date.toJulianDay()).toString(dateFormat));
 
     // Location
     ui->deptLocationLineEdit->setText(flight_data.value(OPL::FlightEntry::DEPT).toString());
@@ -563,13 +566,13 @@ void NewFlightDialog::on_acftLineEdit_editingFinished()
 void NewFlightDialog::on_doftLineEdit_editingFinished()
 {
     const auto line_edit = ui->doftLineEdit;
-    OPL::Date date = OPL::Date::fromString(ui->doftLineEdit->text(), dateFormat);
+    OPL::Date date(ui->doftLineEdit->text(), m_format);
 
 
     LOG << "Date: " << date.toString();
     LOG << "is valid? " << date.isValid();
 
-    line_edit->setText(date.toString(dateFormat));
+    line_edit->setText(date.toString());
     if(ui->doftLineEdit->text().isEmpty()) {
         onBadInputReceived(line_edit);
         return;
@@ -713,7 +716,7 @@ void NewFlightDialog::on_calendarPushButton_clicked()
 void NewFlightDialog::calendarDateSelected()
 {
     calendar->setVisible(false);
-    ui->doftLineEdit->setText(OPL::Date(calendar->selectedDate().toJulianDay()).toString(dateFormat));
+    ui->doftLineEdit->setText(OPL::Date(calendar->selectedDate(), m_format).toString());
     emit ui->doftLineEdit->editingFinished();
 }
 

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

@@ -84,8 +84,7 @@ private:
     Ui::NewFlightDialog *ui;
     ValidationState validationState;
     QCalendarWidget *calendar;
-
-    OPL::Date::Format dateFormat;
+    OPL::DateTimeFormat m_format;
 
     /*!
      * \brief a AFlightEntry object that is used to store either position data

+ 11 - 11
src/gui/dialogues/newsimdialog.cpp

@@ -14,12 +14,10 @@ NewSimDialog::NewSimDialog(QWidget *parent)
     : EntryEditDialog(parent),
     ui(new Ui::NewSimDialog)
 {
-    //entry = ASimulatorEntry();
     ui->setupUi(this);
-
-    m_dateFormat = Settings::getDateFormat();
-    ui->dateLineEdit->setText(OPL::Date::today().toString(m_dateFormat));
     init();
+
+    ui->dateLineEdit->setText(OPL::Date::today(m_format).toString());
 }
 /*!
  * \brief create a NewSimDialog to edit an existing Simulator Entry
@@ -30,9 +28,9 @@ NewSimDialog::NewSimDialog(int row_id, QWidget *parent)
     ui(new Ui::NewSimDialog)
 {
     ui->setupUi(this);
-    m_dateFormat = Settings::getDateFormat();
-    entry = DB->getSimEntry(row_id);
     init();
+
+    entry = DB->getSimEntry(row_id);
     fillEntryData();
 }
 
@@ -49,6 +47,8 @@ void NewSimDialog::init()
     completer->setCompletionMode(QCompleter::PopupCompletion);
     completer->setFilterMode(Qt::MatchContains);
     ui->aircraftTypeLineEdit->setCompleter(completer);
+
+    m_format = Settings::getDisplayFormat();
 }
 
 /*!
@@ -57,7 +57,7 @@ void NewSimDialog::init()
 void NewSimDialog::fillEntryData()
 {
     const auto& data = entry.getData();
-    ui->dateLineEdit->setText(OPL::Date::fromJulianDay(data.value(OPL::SimulatorEntry::DATE).toInt()).toString(m_dateFormat));
+    ui->dateLineEdit->setText(OPL::Date(data.value(OPL::SimulatorEntry::DATE).toInt(), m_format).toString());
     ui->totalTimeLineEdit->setText(OPL::Time(data.value(OPL::SimulatorEntry::TIME).toInt()).toString());
     ui->deviceTypeComboBox->setCurrentIndex(data.value(OPL::SimulatorEntry::TYPE).toInt());
     ui->aircraftTypeLineEdit->setText(data.value(OPL::SimulatorEntry::ACFT).toString());
@@ -72,9 +72,9 @@ NewSimDialog::~NewSimDialog()
 
 void NewSimDialog::on_dateLineEdit_editingFinished()
 {
-    const auto date = OPL::Date::fromString(ui->dateLineEdit->text());
+    const auto date = OPL::Date(ui->dateLineEdit->text(), m_format);
     if(date.isValid()) {
-        ui->dateLineEdit->setText(date.toString(m_dateFormat));
+        ui->dateLineEdit->setText(date.toString());
         ui->dateLineEdit->setStyleSheet(QString());
         return;
     } else {
@@ -121,7 +121,7 @@ void NewSimDialog::on_helpPushButton_clicked()
 bool NewSimDialog::verifyInput(QString& error_msg)
 {
     // Date
-    const auto date = OPL::Date::fromString(ui->dateLineEdit->text());
+    const auto date = OPL::Date(ui->dateLineEdit->text(), m_format);
 
     if (!date.isValid()) {
         ui->dateLineEdit->setStyleSheet(OPL::CssStyles::RED_BORDER);
@@ -156,7 +156,7 @@ OPL::RowData_T NewSimDialog::collectInput()
 {
     OPL::RowData_T new_entry;
     // Date
-    const auto date = OPL::Date::fromString(ui->dateLineEdit->text());
+    const auto date = OPL::Date(ui->dateLineEdit->text(), m_format);
     new_entry.insert(OPL::SimulatorEntry::DATE, date.toJulianDay());
     // Time
     new_entry.insert(OPL::SimulatorEntry::TIME, OPL::Time::fromString(ui->totalTimeLineEdit->text()).toMinutes());

+ 7 - 1
src/gui/dialogues/newsimdialog.h

@@ -42,7 +42,13 @@ private slots:
 
 private:
     Ui::NewSimDialog *ui;
-    OPL::Date::Format m_dateFormat;
+    //TODO load from settings
+    OPL::DateTimeFormat m_format = OPL::DateTimeFormat(
+        OPL::DateTimeFormat::DateFormat::Default,
+        QStringLiteral("yyyy-MM-dd"),
+        OPL::DateTimeFormat::TimeFormat::Default,
+        QStringLiteral("hh:mm")
+        );
 
     void init();
     void fillEntryData();

+ 1 - 1
src/gui/widgets/backupwidget.cpp

@@ -87,7 +87,7 @@ const QString BackupWidget::backupName()
 {
     auto owner = DB->getPilotEntry(1);
     return  QString("logbook_backup_%1_%2.db").arg(
-                OPL::DateTime::dateTimeToString(QDateTime::currentDateTime(), OPL::DateTimeFormat::Backup),
+        OPL::DateTime::dateTimeToString(QDateTime::currentDateTime(), OPL::DateTimeFormat_deprecated::Backup),
                                                   owner.getLastName()
                 );
 }

+ 1 - 1
src/gui/widgets/currencywidget.cpp

@@ -49,7 +49,7 @@ void CurrencyWidget::setupModelAndView()
     view->setAlternatingRowColors(true);
     view->hideColumn(ROWID_COLUMN);
 
-    const auto dateDelegate = new StyledDateDelegate(Settings::getDateFormat(), this);
+    const auto dateDelegate = new StyledDateDelegate(Settings::getDisplayFormat(), this);
     view->setItemDelegateForColumn(EXPIRY_DATE_COLUMN, dateDelegate);
 }
 

+ 1 - 1
src/gui/widgets/logbooktableeditwidget.cpp

@@ -158,7 +158,7 @@ void LogbookTableEditWidget::setupDelegates()
     }
 
     // julian day to Date Format
-    const auto dateDelegate = new StyledDateDelegate(Settings::getDateFormat(), m_model);
+    const auto dateDelegate = new StyledDateDelegate(Settings::getDisplayFormat(), m_model);
     m_view->setItemDelegateForColumn(OPL::LogbookViewInfo::getDateColumn(m_logbookView), dateDelegate);
 
     // pilot_id to names

+ 6 - 1
src/gui/widgets/logbooktableeditwidget.h

@@ -15,7 +15,8 @@
  * The user can select a view from a list of available views in the SettingsWidget.
  *
  * Some of the selectable views aggregate data from the flights and simulators table. In those views, flight
- * entries have positive row id's whereas the simulators have inverse (negative) row id's.
+ * entries have positive row id's whereas the simulators have inverse (negative) row id's. This necessitates
+ * checking the row id's value before deciding on the apropriate EntryEditDialog.
  */
 class LogbookTableEditWidget : public TableEditWidget
 {
@@ -26,6 +27,10 @@ public:
 
 private:
     OPL::LogbookView m_logbookView;
+
+    /*!
+     * \brief Set up the QStyledItemDelegate instances that transform the database values to user-readable values.
+     */
     void setupDelegates();
 
     static constexpr int COL_ROWID = 0;

+ 1 - 1
src/gui/widgets/logbookwidget.cpp

@@ -80,7 +80,7 @@ void LogbookWidget::setupModelAndView(OPL::LogbookView logbookView)
     view->hideColumn(0);
 
     // 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);
+    const auto dateDelegate = new StyledDateDelegate(Settings::getDisplayFormat(), this);
     view->setItemDelegateForColumn(1, dateDelegate);
     view->resizeColumnsToContents();
 }

+ 57 - 1
src/opl.h

@@ -118,6 +118,62 @@ struct ToLdgCount_T {
         : toDay(toDay), toNight(toNight), ldgDay(ldgDay), ldgNight(ldgNight) {}
 };
 
+/*!
+ * \brief The DateFormat struct encapsulates how date and time values are displayed.
+ * \details Stores how the user wishes to display and enter Date and Time Entries.
+ * These are stored numerically in the database and thus need to be converted to
+ * human-readably form.
+ */
+struct DateTimeFormat {
+    /*!
+     * \brief Enumerates how dates can be formatted to a localised format.
+     * \value Default - The Application standard, Equivalent to Qt::ISODate
+     * \value SystemLocale - The current system locale date format
+     */
+    enum class DateFormat { Default, SystemLocale, Custom };
+
+    /*!
+     * \brief Enumerates how time values can be formatted
+     * \value Default - The application default is 'hh:mm'
+     * \value Decimal - Time as Decmial hours (01:30 == 1.5)
+     * \value Custom - A user-provided custom format string
+     */
+    enum class TimeFormat { Default, Decimal, Custom };
+
+    /*!
+     * \brief Initialise a DateTimeFormat instance with default values
+     */
+    DateTimeFormat()
+        : m_dateFormat(DateFormat::Default),
+        m_dateFormatString(QStringLiteral("yyyy-MM-dd")),
+        m_timeFormat(TimeFormat::Default),
+        m_timeFormatString(QStringLiteral("hh:mm"))
+        {}
+
+    DateTimeFormat(DateFormat dateFormat_,
+                   const QString &dateFormatString_,
+                   TimeFormat timeFormat_,
+                   const QString &timeFormatString_)
+        :
+            m_dateFormat(dateFormat_),
+        m_dateFormatString(dateFormatString_),
+        m_timeFormat(timeFormat_),
+            m_timeFormatString(timeFormatString_) {}
+
+
+public:
+    DateFormat dateFormat() const { return m_dateFormat; }
+    TimeFormat timeFormat() const { return m_timeFormat; }
+    const QString &dateFormatString() const { return m_dateFormatString; }
+    const QString &timeFormatString() const { return m_timeFormatString; }
+
+private:
+    DateFormat m_dateFormat;
+    TimeFormat m_timeFormat;
+    QString m_dateFormatString;
+    QString m_timeFormatString;
+};
+
 /*!
  * \brief ADateFormats enumerates the accepted date formats for QDateEdits
  * \todo At the moment, only ISODate is accepet as a valid date format.
@@ -126,7 +182,7 @@ enum class DateFormat {ISODate, DE, EN };
 
 enum class FlightTimeFormat {Default, Decimal};
 
-enum class DateTimeFormat {Default, Backup};
+enum class DateTimeFormat_deprecated {Default, Backup};
 
 /*!
  * \brief PilotFunction