Sfoglia il codice sorgente

Removed old LogbookWidget

LoogbookWidget has been replaced by LogbookTableEditWidget.

Fixed a bug where adding a new flight while an edit window is open would crash the application
Felix Turowsky 1 anno fa
parent
commit
2b1bed568e

+ 1 - 3
CMakeLists.txt

@@ -69,9 +69,7 @@ set(PROJECT_SOURCES
     src/gui/widgets/debugwidget.h
     src/gui/widgets/debugwidget.cpp
     src/gui/widgets/debugwidget.ui
-    src/gui/widgets/logbookwidget.h
-    src/gui/widgets/logbookwidget.cpp
-    src/gui/widgets/logbookwidget.ui
+
     src/gui/widgets/settingswidget.h
     src/gui/widgets/settingswidget.cpp
     src/gui/widgets/settingswidget.ui

+ 1 - 2
mainwindow.h

@@ -33,7 +33,6 @@
 
 #include "src/gui/widgets/homewidget.h"
 #include "src/gui/widgets/settingswidget.h"
-#include "src/gui/widgets/logbookwidget.h"
 #include "src/gui/widgets/tableeditwidget.h"
 #include "src/gui/widgets/debugwidget.h"
 #include "src/classes/style.h"
@@ -119,7 +118,7 @@ private:
 
     HomeWidget* homeWidget;
 
-    LogbookTableEditWidget* logbookWidget;
+    LogbookTableEditWidget* logbookWidget; // This widget has a slot not present in TableEditWidget
     
     TableEditWidget* tailsWidget;
 

+ 7 - 0
src/gui/widgets/logbooktableeditwidget.cpp

@@ -81,6 +81,13 @@ void LogbookTableEditWidget::filterTextChanged(const QString &filterString)
 
 void LogbookTableEditWidget::addEntryRequested()
 {
+    // close open edit dialog(s), if present
+    if(m_stackedWidget->count() > 2) {
+        while (m_stackedWidget->count() > 2) {
+            m_stackedWidget->removeWidget(m_stackedWidget->widget(2));
+        }
+    }
+
     auto nfd = NewFlightDialog(this);
     nfd.exec();
 }

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

@@ -49,6 +49,7 @@ public:
 
 public slots:
     void viewSelectionChanged(SettingsWidget::SettingSignal widget);
+
 private slots:
     virtual void filterTextChanged(const QString &filterString) override;
     virtual void addEntryRequested() override;

+ 0 - 328
src/gui/widgets/logbookwidget.cpp

@@ -1,328 +0,0 @@
-/*
- *openPilotLog - A FOSS Pilot Logbook Application
- *Copyright (C) 2020-2023 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 "src/classes/styleddatedelegate.h"
-#include "src/classes/time.h"
-#include "src/database/database.h"
-#include "logbookwidget.h"
-#include "ui_logbookwidget.h"
-#include "src/database/row.h"
-#include "src/database/database.h"
-#include "src/classes/settings.h"
-#include "src/gui/dialogues/newflightdialog.h"
-#include "src/gui/dialogues/newsimdialog.h"
-
-LogbookWidget::LogbookWidget(QWidget *parent) :
-    QWidget(parent),
-    ui(new Ui::LogbookWidget)
-{
-    ui->setupUi(this);
-
-    OPL::GLOBALS->fillViewNamesComboBox(ui->viewsComboBox);
-
-    //customContextMenu for tablewidget
-    menu  = new QMenu(this);
-    menu->addAction(ui->actionEdit_Flight);
-    menu->addAction(ui->actionDelete_Flight);
-
-    // Initalise the display Model and view
-    displayModel = new QSqlTableModel(this);
-    view = ui->tableView;
-
-    setupModelAndView(Settings::getLogbookView());
-    connectSignalsAndSlots();
-
-}
-
-LogbookWidget::~LogbookWidget()
-{
-    delete ui;
-}
-
-/*
- * Functions
- */
-
-/*!
- * \brief LogbookWidget::setupModelAndView configures the QTableView and populates the model with data
- * according to the current view.
- * \param view_id - retreived from Settings::Main::LogbookView
- */
-void LogbookWidget::setupModelAndView(OPL::LogbookView logbookView)
-{
-    int view_id = static_cast<int>(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->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
-    view->verticalHeader()->hide();
-    view->setAlternatingRowColors(true);
-    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::getDisplayFormat(), this);
-    view->setItemDelegateForColumn(1, dateDelegate);
-    view->resizeColumnsToContents();
-}
-
-void LogbookWidget::connectSignalsAndSlots()
-{
-    selectionModel = view->selectionModel();
-    QObject::connect(view->selectionModel(), &QItemSelectionModel::selectionChanged,
-                     this, &LogbookWidget::flightsTableView_selectionChanged);
-}
-
-const QString LogbookWidget::getFlightSummary(const OPL::FlightEntry &flight) const
-{
-    if(!flight.isValid())
-        return QString();
-
-    auto tableData = flight.getData();
-    QString flight_summary;
-    auto space = QLatin1Char(' ');
-    flight_summary.append(tableData.value(OPL::FlightEntry::DOFT).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);
-//    flight_summary.append(OPL::Time(tableData.value(OPL::FlightEntry::TONB).toInt()).toString()
-//                          + space);
-    flight_summary.append(tableData.value(OPL::FlightEntry::DEST).toString());
-
-    return flight_summary;
-}
-
-void LogbookWidget::changeEvent(QEvent *event)
-{
-    if (event != nullptr)
-        if(event->type() == QEvent::LanguageChange)
-            ui->retranslateUi(this);
-}
-
-/*!
- * \brief LogbookWidget::flightsTableView_selectionChanged saves the selected row(s)
- * to the selectedFlights member variable.
- */
-void LogbookWidget::flightsTableView_selectionChanged()
-{
-    selectedEntries.clear();
-    for (const auto& row : selectionModel->selectedRows()) {
-        selectedEntries.append(row.data().toInt());
-        DEB << "Selected Flight(s) with ID: " << selectedEntries;
-    }
-    if (selectedEntries.length() == 1) {
-        if (isFlight(selectedEntries.first()))
-            on_actionEdit_Flight_triggered();
-        else
-            on_actionEdit_Sim_triggered();
-    }
-}
-
-/*!
- * \brief If a row is selected, query information
- * about the affected row(s) and ask the user to confirm deletion.
- */
-void LogbookWidget::on_actionDelete_Flight_triggered()
-{
-    DEB << "Flights selected: " << selectedEntries.length();
-    if (selectedEntries.length() == 0) {
-        WARN(tr("<br>No flight selected.<br>"));
-        return;
-    } else if (selectedEntries.length() > 0 && selectedEntries.length() <= 10) {
-        QVector<OPL::FlightEntry> flights_list;
-
-        for (const auto &flight_id : qAsConst(selectedEntries)) {
-            flights_list.append(DB->getFlightEntry(flight_id));
-        }
-
-        QString flights_list_string;
-
-        for (auto &flight : flights_list) {
-            flights_list_string.append(getFlightSummary(flight));
-            flights_list_string.append(QStringLiteral("&nbsp;&nbsp;&nbsp;&nbsp;<br>"));
-        }
-
-        QMessageBox confirm(this);
-        confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
-        confirm.setDefaultButton(QMessageBox::No);
-        confirm.setIcon(QMessageBox::Question);
-        confirm.setWindowTitle("Delete Flight");
-        confirm.setText(tr("The following flight(s) will be deleted:<br><br><b><tt>"
-                           "%1<br></b></tt>"
-                           "Deleting flights is irreversible.<br>Do you want to proceed?"
-                           ).arg(flights_list_string));
-        if (confirm.exec() == QMessageBox::Yes) {
-            for (auto& flight : flights_list) {
-                DEB << "Deleting flight: " << flight;
-                if(!DB->remove(flight)) {
-                    WARN(tr("<br>Unable to delete.<br><br>The following error has ocurred: %1"
-                                       ).arg(DB->lastError.text()));
-                    return;
-                }
-            }
-            INFO(tr("%1 flights have been deleted successfully."
-                                   ).arg(QString::number(selectedEntries.length())));
-            displayModel->select();
-        }
-    } else if (selectedEntries.length() > 10) {
-        QMessageBox confirm;
-        confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
-        confirm.setDefaultButton(QMessageBox::No);
-        confirm.setIcon(QMessageBox::Warning);
-        confirm.setWindowTitle("Delete Flight");
-        confirm.setText(tr("You have selected %1 flights.<br><br>"
-                           "Deleting flights is irreversible.<br><br>"
-                           "Are you sure you want to proceed?"
-                           ).arg(QString::number(selectedEntries.length())));
-        if(confirm.exec() == QMessageBox::Yes) {
-            if (!DB->removeMany(OPL::DbTable::Flights, selectedEntries)) {
-                WARN(tr("Unable to delete. No changes have been made to the database. The following error has ocurred:<br><br>%1").arg(DB->lastError.text()));
-                return;
-            }
-            INFO(tr("%1 flights have been deleted successfully."
-                                   ).arg(QString::number(selectedEntries.length())));
-            displayModel->select();
-        }
-        displayModel->select();
-    }
-}
-
-
-void LogbookWidget::on_tableView_customContextMenuRequested(const QPoint &pos)
-{
-    menu->popup(ui->tableView->viewport()->mapToGlobal(pos));
-}
-
-void LogbookWidget::on_tableView_doubleClicked()
-{
-    on_actionEdit_Flight_triggered();
-}
-
-void LogbookWidget::on_flightSearchComboBox_currentIndexChanged(int)
-{
-    //emit ui->showAllButton->clicked();
-}
-
-/*!
- * \brief LogbookWidget::refresh Refreshes the view to reflect changes in the database.
- */
-void LogbookWidget::refresh()
-{
-    displayModel->select();
-    view->resizeColumnsToContents();
-}
-
-void LogbookWidget::onLogbookWidget_viewSelectionChanged(SettingsWidget::SettingSignal signal)
-{
-    if (signal == SettingsWidget::SettingSignal::LogbookWidget)
-        setupModelAndView(Settings::getLogbookView());
-}
-
-//void LogbookWidget::on_showAllButton_clicked()
-//{
-//    ui->flightSearchLlineEdit->setText(QString());
-//    displayModel->setFilter(QString());
-//    displayModel->select();
-//}
-
-/*!
- * \brief LogbookWidget::on_flightSearchLlineEdit_textChanged applies a filter to the
- * display model allowing the user to search for flights by specified elements (date, aircraft,
- * Pilot Name)
- */
-void LogbookWidget::on_flightSearchLlineEdit_textChanged(const QString &arg1)
-{
-    if(arg1.length() == 0) {
-        displayModel->setFilter("");
-        displayModel->select();
-        return;
-    }
-
-    if (ui->flightSearchComboBox->currentIndex() < 3) {
-        displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
-                                + arg1 + QLatin1String("%\""));
-        return;
-    } else if (ui->flightSearchComboBox->currentIndex() == 3) { // registration
-        displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
-                                + arg1 + QLatin1String("%\""));
-        return;
-    } else if (ui->flightSearchComboBox->currentIndex() == 4) { // Name Pic
-        displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
-                                + arg1 + QLatin1String("%\""));
-        return;
-    }
-}
-
-/*!
- * \brief LogbookWidget::repopulateModel (public slot) - cleanly re-populates the model to cater for a change
- * to the database connection (for example, when a backup is created or restored)
- */
-void LogbookWidget::repopulateModel()
-{
-    // unset the current model and delete it to avoid leak
-    view->setModel(nullptr);
-    delete displayModel;
-    // create a new model and populate it
-    displayModel = new QSqlTableModel(this);
-    setupModelAndView(Settings::getLogbookView());
-    connectSignalsAndSlots();
-}
-
-void LogbookWidget::on_viewsComboBox_currentIndexChanged(int index)
-{
-    setupModelAndView(OPL::LogbookView(index));
-}
-
-void LogbookWidget::on_actionEdit_Flight_triggered()
-{
-    if(selectedEntries.length() == 1){
-        NewFlightDialog nff(selectedEntries.first(), this);
-        ui->stackedWidget->addWidget(&nff);
-        ui->stackedWidget->setCurrentWidget(&nff);
-        nff.setWindowFlag(Qt::Widget);
-        nff.exec();
-        displayModel->select();
-    } else if (selectedEntries.isEmpty()) {
-        WARN(tr("<br>No flight selected.<br>"));
-    } else {
-        WARN(tr("<br>More than one flight selected."
-                               "<br><br>Editing multiple entries is not yet supported."));
-    }
-}
-
-void LogbookWidget::on_actionEdit_Sim_triggered()
-{
-    if (selectedEntries.length() == 1) {
-        NewSimDialog nsd((selectedEntries.first() * -1), this); // simulator entries have inverse row ID's in the model
-        ui->stackedWidget->addWidget(&nsd);
-        ui->stackedWidget->setCurrentWidget(&nsd);
-        nsd.setWindowFlag(Qt::Widget);
-        nsd.exec();
-        displayModel->select();
-    } else if (selectedEntries.isEmpty()) {
-        WARN(tr("<br>No flight selected.<br>"));
-    } else {
-        WARN(tr("<br>More than one flight selected."
-                               "<br><br>Editing multiple entries is not yet supported."));
-    }
-}
-

+ 0 - 116
src/gui/widgets/logbookwidget.h

@@ -1,116 +0,0 @@
-/*
- *openPilotLog - A FOSS Pilot Logbook Application
- *Copyright (C) 2020-2023 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 LOGBOOKWIDGET_H
-#define LOGBOOKWIDGET_H
-
-#include <QWidget>
-#include <QItemSelection>
-#include <QSqlTableModel>
-#include <QMessageBox>
-#include <QDebug>
-#include <QMenu>
-#include <QTableView>
-#include "src/database/flightentry.h"
-#include "src/gui/widgets/settingswidget.h"
-
-namespace Ui {
-class LogbookWidget;
-}
-
-/*!
- * \brief The LogbookWidget displays data from the database in a QSqlTableView fed by a QSqlQuery Model
- *
- * \details The LogbookWidget is the primary display interface for flights logged in the database. It fetches and stores
- * flight data from the database via a QSqlQueryModel and displays it in a QTableView. With the way the flight data is
- * written in the database, it would not be human-readable, so some processing is done on the database side to present
- * a nicely formatted, human-readable display. This is achieved by means of a [SQL View](https://sqlite.org/lang_createview.html).
- *
- * The user can select a view from a list of available views in the SettingsWidget.
- *
- */
-class LogbookWidget : public QWidget
-{
-    Q_OBJECT
-
-public:
-    explicit LogbookWidget(QWidget *parent = nullptr);
-    ~LogbookWidget();
-
-private slots:
-    void flightsTableView_selectionChanged();
-    void on_tableView_customContextMenuRequested(const QPoint &pos);
-    void on_actionDelete_Flight_triggered();
-
-    void on_tableView_doubleClicked();
-    void on_flightSearchLlineEdit_textChanged(const QString &arg1);
-    void on_flightSearchComboBox_currentIndexChanged(int);
-
-    void on_viewsComboBox_currentIndexChanged(int index);
-
-    void on_actionEdit_Flight_triggered();
-    void on_actionEdit_Sim_triggered();
-
-public slots:
-    void refresh();
-    void onLogbookWidget_viewSelectionChanged(SettingsWidget::SettingSignal signal);
-    void repopulateModel();
-
-private:
-    Ui::LogbookWidget *ui;
-
-    QTableView* view;
-
-    QSqlTableModel* displayModel;
-
-    QItemSelectionModel* selectionModel;
-
-    QMenu* menu;
-
-    QList<int> selectedEntries;
-
-    void setupModelAndView(OPL::LogbookView logbookView);
-    void connectSignalsAndSlots();
-
-    const QString getFlightSummary(const OPL::FlightEntry &flight) const;
-
-    /*!
-     * \brief isFlight Determines whether an entry shown in a view is a Flight or a Simulator.
-     * \param model_row_id the row id in the QSqlTableModel used for displaying
-     * \details In the composite views (SQL UNION) with Simulators included, the row_id of the
-     * simulator entries is inverted to a negative value. A positive row id is thus a row id from
-     * the flights table, whereas a negative rowid is a row id from the simulators table.
-     */
-    inline bool isFlight(int model_row_id) { return model_row_id > 0; }
-
-    const static inline QHash<int, QString> FILTER_MAP = {
-        {0, QStringLiteral("Date LIKE \"%")},
-        {1, QStringLiteral("Dept LIKE \"%")},
-        {2, QStringLiteral("Dest LIKE \"%")},
-        {3, QStringLiteral("Registration LIKE \"%")},
-        {4, QStringLiteral("\"Name PIC\" LIKE \"%")}
-    };
-    const static inline QRegularExpression NON_WORD_CHAR = QRegularExpression("\\W");
-
-protected:
-    /*!
-     * \brief Handles change events, like updating the UI to new localisation
-     */
-    void changeEvent(QEvent* event) override;
-};
-
-#endif // LOGBOOKWIDGET_H

+ 0 - 169
src/gui/widgets/logbookwidget.ui

@@ -1,169 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<ui version="4.0">
- <class>LogbookWidget</class>
- <widget class="QWidget" name="LogbookWidget">
-  <property name="geometry">
-   <rect>
-    <x>0</x>
-    <y>0</y>
-    <width>1280</width>
-    <height>720</height>
-   </rect>
-  </property>
-  <property name="windowTitle">
-   <string>Form</string>
-  </property>
-  <layout class="QGridLayout" name="gridLayout">
-   <item row="1" column="0">
-    <widget class="QTableView" name="tableView">
-     <property name="font">
-      <font/>
-     </property>
-     <property name="styleSheet">
-      <string notr="true"/>
-     </property>
-     <property name="selectionMode">
-      <enum>QAbstractItemView::SingleSelection</enum>
-     </property>
-     <property name="selectionBehavior">
-      <enum>QAbstractItemView::SelectRows</enum>
-     </property>
-    </widget>
-   </item>
-   <item row="2" column="0">
-    <widget class="QStackedWidget" name="stackedWidget">
-     <widget class="QWidget" name="defaultPage">
-      <layout class="QGridLayout" name="gridLayout_2">
-       <item row="0" column="0">
-        <widget class="QLabel" name="flightSearchLabel">
-         <property name="minimumSize">
-          <size>
-           <width>200</width>
-           <height>0</height>
-          </size>
-         </property>
-         <property name="text">
-          <string>Search Flight</string>
-         </property>
-         <property name="alignment">
-          <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
-         </property>
-        </widget>
-       </item>
-       <item row="0" column="1">
-        <widget class="QLineEdit" name="flightSearchLlineEdit">
-         <property name="toolTip">
-          <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Enter the searchterm you want to filter your flights by.&lt;/p&gt;&lt;p&gt;For dates, make sure to use the format YYYY-MM-DD&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
-         </property>
-         <property name="whatsThis">
-          <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
-         </property>
-        </widget>
-       </item>
-       <item row="1" column="0">
-        <widget class="QLabel" name="flightSearchInLabel">
-         <property name="minimumSize">
-          <size>
-           <width>200</width>
-           <height>0</height>
-          </size>
-         </property>
-         <property name="text">
-          <string>Search by</string>
-         </property>
-         <property name="alignment">
-          <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
-         </property>
-        </widget>
-       </item>
-       <item row="1" column="1">
-        <widget class="QComboBox" name="flightSearchComboBox">
-         <item>
-          <property name="text">
-           <string>Date of Flight</string>
-          </property>
-         </item>
-         <item>
-          <property name="text">
-           <string>Departure Airport</string>
-          </property>
-         </item>
-         <item>
-          <property name="text">
-           <string>Destination Airport</string>
-          </property>
-         </item>
-         <item>
-          <property name="text">
-           <string>Aircraft Registration</string>
-          </property>
-         </item>
-         <item>
-          <property name="text">
-           <string>Pilot Name</string>
-          </property>
-         </item>
-        </widget>
-       </item>
-       <item row="2" column="0" colspan="2">
-        <widget class="QLabel" name="label">
-         <property name="text">
-          <string>Select a Flight from the list to show or edit details.</string>
-         </property>
-         <property name="alignment">
-          <set>Qt::AlignCenter</set>
-         </property>
-        </widget>
-       </item>
-      </layout>
-     </widget>
-    </widget>
-   </item>
-   <item row="0" column="0">
-    <widget class="QComboBox" name="viewsComboBox"/>
-   </item>
-  </layout>
-  <widget class="QLabel" name="spacerLabel">
-   <property name="geometry">
-    <rect>
-     <x>284</x>
-     <y>410</y>
-     <width>200</width>
-     <height>16</height>
-    </rect>
-   </property>
-   <property name="minimumSize">
-    <size>
-     <width>200</width>
-     <height>0</height>
-    </size>
-   </property>
-   <property name="text">
-    <string/>
-   </property>
-   <property name="alignment">
-    <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
-   </property>
-  </widget>
-  <action name="actionEdit_Flight">
-   <property name="text">
-    <string>Edit Flight</string>
-   </property>
-  </action>
-  <action name="actionDelete_Flight">
-   <property name="text">
-    <string>Delete Flight</string>
-   </property>
-  </action>
-  <action name="actionEdit_Sim">
-   <property name="text">
-    <string>Edit_Sim</string>
-   </property>
-   <property name="toolTip">
-    <string>Edit an existing Simulator Entry</string>
-   </property>
-  </action>
- </widget>
- <resources/>
- <connections/>
-</ui>