Browse Source

Replaced LogbookWidget

- Replaced the old LogbookWidget with a new TableEditWidget subclass.
- NewFlightDialog and NewSimDialog now implement EntryEditDialog
Felix Turowsky 1 year ago
parent
commit
1819cf386a

+ 10 - 6
mainwindow.cpp

@@ -89,7 +89,11 @@ void MainWindow::initialiseWidgets()
     homeWidget = new HomeWidget(this);
     ui->stackedWidget->addWidget(homeWidget);
 
-    logbookWidget = new LogbookWidget(this);
+//    logbookWidget = new LogbookWidget(this);
+//    ui->stackedWidget->addWidget(logbookWidget);
+
+    logbookWidget = new LogbookTableEditWidget(this);
+    logbookWidget->init();
     ui->stackedWidget->addWidget(logbookWidget);
 
     tailsWidget = new TailTableEditWidget(this);
@@ -174,10 +178,10 @@ void MainWindow::connectWidgets()
     QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
                      homeWidget,     &HomeWidget::refresh);
 
-    QObject::connect(DB,             &OPL::Database::dataBaseUpdated,
-                     logbookWidget,  &LogbookWidget::refresh);
+//    QObject::connect(DB,             &OPL::Database::dataBaseUpdated,
+//                     logbookWidget,  &LogbookWidget::refresh);
     QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
-                     logbookWidget,  &LogbookWidget::onLogbookWidget_viewSelectionChanged);
+                     logbookWidget,  &LogbookTableEditWidget::viewSelectionChanged);
 
 //    QObject::connect(DB,             &OPL::Database::dataBaseUpdated,
 //                     tailsWidget, &TailsWidget::onAircraftWidget_dataBaseUpdated);
@@ -191,8 +195,8 @@ void MainWindow::connectWidgets()
     QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
                      this,           &MainWindow::onStyleChanged);
 
-    QObject::connect(DB,              &OPL::Database::connectionReset,
-                     logbookWidget,   &LogbookWidget::repopulateModel);
+//    QObject::connect(DB,              &OPL::Database::connectionReset,
+//                     logbookWidget,   &LogbookWidget::repopulateModel);
 //    QObject::connect(DB,              &OPL::Database::connectionReset,
 //                     pilotsWidget,    &PilotsWidget::repopulateModel);
 //    QObject::connect(DB,              &OPL::Database::connectionReset,

+ 3 - 1
mainwindow.h

@@ -29,6 +29,8 @@
 #include <QKeyEvent>
 #include <QToolBar>
 
+#include <src/gui/widgets/logbooktableeditwidget.h>
+
 #include "src/gui/widgets/homewidget.h"
 #include "src/gui/widgets/settingswidget.h"
 #include "src/gui/widgets/logbookwidget.h"
@@ -117,7 +119,7 @@ private:
 
     HomeWidget* homeWidget;
 
-    LogbookWidget* logbookWidget;
+    LogbookTableEditWidget* logbookWidget;
     
     TableEditWidget* tailsWidget;
 

+ 1 - 1
src/classes/date.h

@@ -31,7 +31,7 @@ public:
 
     const QString toString(Format format = Format::Default) const;
 
-    const bool isValid() { return date.isValid(); }
+    const bool isValid() const { return date.isValid(); }
 
     const static inline Date fromString(const QString &dateString, Format format = Default) {
         switch (format) {

+ 1 - 1
src/classes/settings.h

@@ -110,7 +110,7 @@ public:
     /*!
      * \brief returns the date format to be used in the application
      */
-    static OPL::Date::Format getDateFormat() { return OPL::Date::Format(settingsInstance->value(MAIN_DATE_FORMAT).toInt()); }
+    static OPL::Date::Format getDateFormat() { return OPL::Date::Format(settingsInstance->value(MAIN_DATE_FORMAT, OPL::Date::Format::Default).toInt()); }
 
     /*!
      * \brief sets the date format to be used in the application

+ 16 - 1
src/gui/dialogues/newflightdialog.cpp

@@ -76,6 +76,7 @@ void NewFlightDialog::setPilotFunction()
         ui->functionComboBox->setCurrentIndex(2);
         emit ui->sicNameLineEdit->editingFinished();
     }
+    ui->pilotFlyingCheckBox->setCheckState(Qt::Checked);
 }
 
 void NewFlightDialog::init()
@@ -704,7 +705,7 @@ void NewFlightDialog::on_buttonBox_accepted()
 }
 
 
-void NewFlightDialog::on_pushButton_clicked()
+void NewFlightDialog::on_calendarPushButton_clicked()
 {
     calendar->setVisible(true);
 }
@@ -716,3 +717,17 @@ void NewFlightDialog::calendarDateSelected()
     emit ui->doftLineEdit->editingFinished();
 }
 
+// EntryEditDialog interface
+
+void NewFlightDialog::loadEntry(int rowID)
+{
+    flightEntry = DB->getFlightEntry(rowID);
+    fillWithEntryData();
+}
+
+bool NewFlightDialog::deleteEntry(int rowID)
+{
+    const auto entry = DB->getFlightEntry(rowID);
+    return DB->remove(entry);
+}
+

+ 4 - 9
src/gui/dialogues/newflightdialog.h

@@ -25,6 +25,7 @@
 #include <QBitArray>
 
 #include "QtWidgets/qcalendarwidget.h"
+#include "src/database/database.h"
 #include "src/database/flightentry.h"
 #include "src/gui/dialogues/entryeditdialog.h"
 #include "src/gui/verification/userinput.h"
@@ -194,7 +195,7 @@ private slots:
     void on_approachComboBox_currentTextChanged(const QString &arg1);
     void on_functionComboBox_currentIndexChanged(int index);
 
-    void on_pushButton_clicked();
+    void on_calendarPushButton_clicked();
 
     void calendarDateSelected();
 
@@ -203,14 +204,8 @@ protected:
 
     // EntryEditDialog interface
 public:
-    virtual void loadEntry(int rowID) override
-    {
-        LOG << "Not implemented.";
-    }
-    virtual bool deleteEntry(int rowID) override
-    {
-        LOG << "Not implemented.";
-    }
+    virtual void loadEntry(int rowID) override;
+    virtual bool deleteEntry(int rowID) override;
 };
 
 

+ 2 - 2
src/gui/dialogues/newflightdialog.ui

@@ -6,7 +6,7 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>767</width>
+    <width>799</width>
     <height>391</height>
    </rect>
   </property>
@@ -312,7 +312,7 @@
     </widget>
    </item>
    <item row="0" column="0">
-    <widget class="QPushButton" name="pushButton">
+    <widget class="QPushButton" name="calendarPushButton">
      <property name="text">
       <string>Date Of flight</string>
      </property>

+ 30 - 18
src/gui/dialogues/newsimdialog.cpp

@@ -4,31 +4,33 @@
 #include "ui_newsimdialog.h"
 #include "src/opl.h"
 #include "src/classes/time.h"
-#include "src/functions/datetime.h"
 #include "src/database/database.h"
+#include "src/classes/settings.h"
 #include <QCompleter>
 /*!
  * \brief create a NewSimDialog to add a new Simulator Entry to the database
  */
-NewSimDialog::NewSimDialog(QWidget *parent) :
-    QDialog(parent),
+NewSimDialog::NewSimDialog(QWidget *parent)
+    : EntryEditDialog(parent),
     ui(new Ui::NewSimDialog)
 {
     //entry = ASimulatorEntry();
     ui->setupUi(this);
-    ui->dateLineEdit->setText(OPL::DateTime::currentDate());
+
+    m_dateFormat = Settings::getDateFormat();
+    ui->dateLineEdit->setText(OPL::Date::today().toString(m_dateFormat));
     init();
 }
 /*!
  * \brief create a NewSimDialog to edit an existing Simulator Entry
  * \param row_id of the entry to be edited
  */
-NewSimDialog::NewSimDialog(int row_id, QWidget *parent) :
-    QDialog(parent),
+NewSimDialog::NewSimDialog(int row_id, QWidget *parent)
+    : EntryEditDialog(parent),
     ui(new Ui::NewSimDialog)
 {
-
     ui->setupUi(this);
+    m_dateFormat = Settings::getDateFormat();
     entry = DB->getSimEntry(row_id);
     init();
     fillEntryData();
@@ -55,7 +57,7 @@ void NewSimDialog::init()
 void NewSimDialog::fillEntryData()
 {
     const auto& data = entry.getData();
-    ui->dateLineEdit->setText(data.value(OPL::SimulatorEntry::DATE).toString());
+    ui->dateLineEdit->setText(OPL::Date::fromJulianDay(data.value(OPL::SimulatorEntry::DATE).toInt()).toString(m_dateFormat));
     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());
@@ -70,12 +72,9 @@ NewSimDialog::~NewSimDialog()
 
 void NewSimDialog::on_dateLineEdit_editingFinished()
 {
-    auto text = ui->dateLineEdit->text();
-
-    OPL::DateFormat date_format = OPL::DateFormat::ISODate;
-    auto date = OPL::DateTime::parseInput(text, date_format);
-    if (date.isValid()) {
-        ui->dateLineEdit->setText(OPL::DateTime::dateToString(date, date_format));
+    const auto date = OPL::Date::fromString(ui->dateLineEdit->text());
+    if(date.isValid()) {
+        ui->dateLineEdit->setText(date.toString(m_dateFormat));
         ui->dateLineEdit->setStyleSheet(QString());
         return;
     } else {
@@ -122,9 +121,7 @@ void NewSimDialog::on_helpPushButton_clicked()
 bool NewSimDialog::verifyInput(QString& error_msg)
 {
     // Date
-    auto text = ui->dateLineEdit->text();
-    OPL::DateFormat date_format = OPL::DateFormat::ISODate;
-    const auto date = OPL::DateTime::parseInput(text, date_format);
+    const auto date = OPL::Date::fromString(ui->dateLineEdit->text());
 
     if (!date.isValid()) {
         ui->dateLineEdit->setStyleSheet(OPL::CssStyles::RED_BORDER);
@@ -159,7 +156,8 @@ OPL::RowData_T NewSimDialog::collectInput()
 {
     OPL::RowData_T new_entry;
     // Date
-    new_entry.insert(OPL::SimulatorEntry::DATE, ui->dateLineEdit->text());
+    const auto date = OPL::Date::fromString(ui->dateLineEdit->text());
+    new_entry.insert(OPL::SimulatorEntry::DATE, date.toJulianDay());
     // Time
     new_entry.insert(OPL::SimulatorEntry::TIME, OPL::Time::fromString(ui->totalTimeLineEdit->text()).toMinutes());
     // Device Type
@@ -193,3 +191,17 @@ void NewSimDialog::on_buttonBox_accepted()
     else
         WARN(tr("Unable to commit entry to database. The following error has ocurred <br><br>%1").arg(DB->lastError.text()));
 }
+
+// EntryEdit interface
+void NewSimDialog::loadEntry(int rowID)
+{
+    entry = DB->getSimEntry(rowID);
+    init();
+    fillEntryData();
+}
+
+bool NewSimDialog::deleteEntry(int rowID)
+{
+    const auto entry = DB->getSimEntry(rowID);
+    return DB->remove(entry);
+}

+ 11 - 2
src/gui/dialogues/newsimdialog.h

@@ -2,7 +2,10 @@
 #define NEWSIMDIALOG_H
 
 #include <QDialog>
+#include "src/database/database.h"
 #include "src/database/simulatorentry.h"
+#include "src/gui/dialogues/entryeditdialog.h"
+#include "src/classes/date.h"
 
 namespace Ui {
 class NewSimDialog;
@@ -17,7 +20,7 @@ class NewSimDialog;
  *
  * A QCompleter provides in-line completion for the aircraft type field.
  */
-class NewSimDialog : public QDialog
+class NewSimDialog : public EntryEditDialog
 {
     Q_OBJECT
 
@@ -39,12 +42,18 @@ private slots:
 
 private:
     Ui::NewSimDialog *ui;
+    OPL::Date::Format m_dateFormat;
+
     void init();
     void fillEntryData();
     bool verifyInput(QString &error_msg);
     OPL::RowData_T collectInput();
-
     OPL::SimulatorEntry entry;
+
+    // EntryEditDialog interface
+public:
+    virtual void loadEntry(int rowID) override;
+    virtual bool deleteEntry(int rowID) override;
 };
 
 #endif // NEWSIMDIALOG_H

+ 83 - 6
src/gui/widgets/logbooktableeditwidget.cpp

@@ -2,12 +2,12 @@
 #include "src/classes/settings.h"
 #include "src/classes/styleddatedelegate.h"
 #include "src/classes/styledpilotdelegate.h"
-#include "src/classes/styledregistrationdelegate.h"
 #include "src/classes/styledtimedelegate.h"
 #include "src/classes/styledtypedelegate.h"
 #include "src/database/database.h"
 #include "src/database/views/logbookviewinfo.h"
 #include "src/gui/dialogues/newflightdialog.h"
+#include "src/gui/dialogues/newsimdialog.h"
 
 LogbookTableEditWidget::LogbookTableEditWidget(QWidget *parent)
     : TableEditWidget(Vertical, parent)
@@ -45,18 +45,29 @@ void LogbookTableEditWidget::setupUI()
 {
     TableEditWidget::setupUI();
     m_addNewEntryPushButton->setText(tr("Add new Flight"));
-    m_deleteEntryPushButton->setText(tr("Delete selected Flight"));
-    TODO << "Set up filters";
+    m_deleteEntryPushButton->setText(tr("Delete selected Entry"));
+    m_filterWidget->hide();
+    m_stackedWidget->hide();
 }
 
 QString LogbookTableEditWidget::deleteErrorString(int rowId)
 {
-    return "Not implemented";
+    return tr("<br>Unable to delete.<br><br>The following error has ocurred: %1"
+              ).arg(DB->lastError.text());
 }
 
 QString LogbookTableEditWidget::confirmDeleteString(int rowId)
 {
-    return "Not implemented";
+    if(rowId > 0) {
+    const auto selectedEntry = DB->getFlightEntry(rowId);
+    return tr("The following flight will be deleted:<br><br><b><tt>"
+               "%1<br></b></tt><br><br>"
+               "Deleting flights is irreversible.<br>Do you want to proceed?"
+              ).arg(selectedEntry.getFlightSummary());
+
+    }
+
+    return tr("Deleting entries is irreversible.<br>Do you want to proceed?");
 }
 
 EntryEditDialog *LogbookTableEditWidget::getEntryEditDialog(QWidget *parent)
@@ -64,9 +75,75 @@ EntryEditDialog *LogbookTableEditWidget::getEntryEditDialog(QWidget *parent)
     return new NewFlightDialog(parent);
 }
 
+void LogbookTableEditWidget::viewSelectionChanged(SettingsWidget::SettingSignal widget)
+{
+    if(widget == SettingsWidget::SettingSignal::LogbookWidget)
+        setupModelAndView();
+}
+
 void LogbookTableEditWidget::filterTextChanged(const QString &filterString)
+{}
+
+void LogbookTableEditWidget::addEntryRequested()
+{
+    auto nfd = NewFlightDialog(this);
+    nfd.exec();
+}
+
+void LogbookTableEditWidget::editEntryRequested(const QModelIndex &selectedIndex)
 {
-    LOG << "Not implemented.";
+    m_stackedWidget->show();
+    const auto idx = m_view->selectionModel()->currentIndex();
+    const auto rowId = m_model->index(idx.row(), 0).data().toInt();
+    if(rowId > 0) {
+        auto nfd = NewFlightDialog(rowId, this);
+        m_stackedWidget->addWidget(&nfd);
+        m_stackedWidget->setCurrentWidget(&nfd);
+        nfd.exec();
+    } else {
+        auto nsd = NewSimDialog(rowId * -1, this);
+        m_stackedWidget->addWidget(&nsd);
+        m_stackedWidget->setCurrentWidget(&nsd);
+        nsd.exec();
+    }
+    m_stackedWidget->hide();
+}
+
+void LogbookTableEditWidget::deleteEntryRequested()
+{
+    const QModelIndex selectedIndex = m_view->selectionModel()->currentIndex();
+    if(!selectedIndex.isValid()) {
+        WARN(tr("No entry selected."));
+        return;
+    }
+    m_stackedWidget->hide();
+
+    int rowId = m_model->index(selectedIndex.row(), 0).data().toInt();
+    m_view->selectionModel()->reset();
+
+    // get user confirmation
+    QMessageBox confirm(this);
+    confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
+    confirm.setDefaultButton(QMessageBox::No);
+    confirm.setIcon(QMessageBox::Question);
+    confirm.setWindowTitle(tr("Confirm Deletion"));
+
+    confirm.setText(confirmDeleteString(rowId));
+    if (confirm.exec() == QMessageBox::Yes) {
+        if(rowId > 0) {
+            const auto selectedEntry = DB->getFlightEntry(rowId);
+            if(!DB->remove(selectedEntry))
+                WARN(deleteErrorString(rowId));
+        } else {
+            const auto selectedEntry = DB->getSimEntry(rowId * - 1);
+            if(!DB->remove(selectedEntry))
+                WARN(deleteErrorString(rowId));
+        }
+    }
+
+    // re-set stackedWidget for Vertical Layout
+//    m_stackedWidget->setCurrentWidget(m_filterWidget);
+//    m_stackedWidget->show();
 }
 
 // private implementations

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

@@ -1,6 +1,7 @@
 #ifndef LOGBOOKTABLEEDITWIDGET_H
 #define LOGBOOKTABLEEDITWIDGET_H
 
+#include "settingswidget.h"
 #include "tableeditwidget.h"
 #include "src/opl.h"
 #include <QObject>
@@ -37,8 +38,13 @@ public:
     virtual QString confirmDeleteString(int rowId) override;
     virtual EntryEditDialog *getEntryEditDialog(QWidget *parent) override;
 
+public slots:
+    void viewSelectionChanged(SettingsWidget::SettingSignal widget);
 private slots:
     virtual void filterTextChanged(const QString &filterString) override;
+    virtual void addEntryRequested() override;
+    virtual void editEntryRequested(const QModelIndex &selectedIndex) override;
+    virtual void deleteEntryRequested() override;
 };
 
 #endif // LOGBOOKTABLEEDITWIDGET_H

+ 5 - 5
src/gui/widgets/pilottableeditwidget.h

@@ -16,11 +16,11 @@ public:
 
 
 private:
-    const int COL_ROWID = 0;
-    const int COL_LASTNAME = 1;
-    const int COL_FIRSTNAME = 2;
-    const int COL_COMPANY = 4;
-    const int COLS_TO_HIDE[5] = {0, 3, 5, 6, 7};
+    static constexpr int COL_ROWID = 0;
+    static constexpr int COL_LASTNAME = 1;
+    static constexpr int COL_FIRSTNAME = 2;
+    static constexpr int COL_COMPANY = 4;
+    static constexpr int COLS_TO_HIDE[5] = {0, 3, 5, 6, 7};
 
     const QString COLUMN_1_NAME = tr("First Name");
     const QString COLUMN_2_NAME = tr("Last Name");

+ 6 - 5
src/gui/widgets/tableeditwidget.h

@@ -93,6 +93,7 @@ public:
      * EntryEditDialog for the selected table.
      */
     virtual EntryEditDialog *getEntryEditDialog(QWidget *parent = nullptr) = 0;
+//    virtual EntryEditDialog getEntryEditDialog(QWidget *parent = nullptr) = 0;
 
 protected:
     Orientation m_orientation;
@@ -100,7 +101,7 @@ protected:
     QTableView *m_view = new QTableView(this);
     QWidget *m_filterWidget = nullptr;
     QWidget *m_buttonWidget = nullptr;
-    EntryEditDialog *m_entryEditDialog = nullptr;
+    EntryEditDialog* m_entryEditDialog = nullptr;
 
     QPushButton *m_addNewEntryPushButton = new QPushButton(this);
     QPushButton *m_deleteEntryPushButton = new QPushButton(this);
@@ -125,10 +126,10 @@ private:
     void setupButtonWidget();
 
 private slots:
-    void addEntryRequested();
-    void editEntryRequested(const QModelIndex &selectedIndex);
-    void deleteEntryRequested();
-    void sortColumnChanged(int newSortColumn);
+    virtual void addEntryRequested();
+    virtual void editEntryRequested(const QModelIndex &selectedIndex);
+    virtual void deleteEntryRequested();
+    virtual void sortColumnChanged(int newSortColumn);
 
     /*!
      * \brief Set a filter on the model