Browse Source

Removed old widgets and added orientation

The TableEditWidget class now has two layout options - Horizontal and Vertical. Which one is a better suit depends on the data displayed in the table. With less columns the horizontal layout seems more suitable whereas with a higher numbe of columns the vertical layout is preferable.
Felix Turowsky 1 year ago
parent
commit
e93ab029c4

+ 7 - 15
CMakeLists.txt

@@ -57,33 +57,25 @@ set(PROJECT_SOURCES
     src/gui/dialogues/entryeditdialog.cpp
 
     # Widgets
-    src/gui/widgets/tailswidget.h
-    src/gui/widgets/tailswidget.cpp
-    src/gui/widgets/aircraftwidget.ui
-    src/gui/widgets/airportwidget.h
-    src/gui/widgets/airportwidget.cpp
-    src/gui/widgets/airportwidget.ui
+    src/gui/widgets/homewidget.h
+    src/gui/widgets/homewidget.cpp
+    src/gui/widgets/homewidget.ui
+    src/gui/widgets/totalswidget.h
+    src/gui/widgets/totalswidget.cpp
+    src/gui/widgets/totalswidget.ui
     src/gui/widgets/backupwidget.h
     src/gui/widgets/backupwidget.cpp
     src/gui/widgets/backupwidget.ui
     src/gui/widgets/debugwidget.h
     src/gui/widgets/debugwidget.cpp
     src/gui/widgets/debugwidget.ui
-    src/gui/widgets/homewidget.h
-    src/gui/widgets/homewidget.cpp
-    src/gui/widgets/homewidget.ui
     src/gui/widgets/logbookwidget.h
     src/gui/widgets/logbookwidget.cpp
     src/gui/widgets/logbookwidget.ui
-    src/gui/widgets/pilotswidget.h
-    src/gui/widgets/pilotswidget.cpp
-    src/gui/widgets/pilotswidget.ui
     src/gui/widgets/settingswidget.h
     src/gui/widgets/settingswidget.cpp
     src/gui/widgets/settingswidget.ui
-    src/gui/widgets/totalswidget.h
-    src/gui/widgets/totalswidget.cpp
-    src/gui/widgets/totalswidget.ui
+
     src/gui/widgets/tableeditwidget.h
     src/gui/widgets/tableeditwidget.cpp
     src/gui/widgets/pilottableeditwidget.h

+ 0 - 0
src/gui/widgets/aircraftwidget.ui → deprecated/aircraftwidget.ui


+ 0 - 0
src/gui/widgets/airportwidget.cpp → deprecated/airportwidget.cpp


+ 0 - 0
src/gui/widgets/airportwidget.h → deprecated/airportwidget.h


+ 0 - 0
src/gui/widgets/airportwidget.ui → deprecated/airportwidget.ui


+ 0 - 0
src/gui/widgets/pilotswidget.cpp → deprecated/pilotswidget.cpp


+ 0 - 0
src/gui/widgets/pilotswidget.h → deprecated/pilotswidget.h


+ 0 - 0
src/gui/widgets/pilotswidget.ui → deprecated/pilotswidget.ui


+ 0 - 0
src/gui/widgets/tailswidget.cpp → deprecated/tailswidget.cpp


+ 0 - 0
src/gui/widgets/tailswidget.h → deprecated/tailswidget.h


+ 0 - 5
mainwindow.h

@@ -33,14 +33,9 @@
 #include "src/gui/widgets/settingswidget.h"
 #include "src/gui/widgets/logbookwidget.h"
 #include "src/gui/widgets/tableeditwidget.h"
-#include "src/gui/widgets/tailswidget.h"
-#include "src/gui/widgets/airportwidget.h"
-#include "src/gui/widgets/airportwidget.h"
-#include "src/gui/widgets/pilotswidget.h"
 #include "src/gui/widgets/debugwidget.h"
 #include "src/classes/style.h"
 
-enum Style {Light, Dark};
 QT_BEGIN_NAMESPACE
 namespace Ui {
 class MainWindow;

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

@@ -3,7 +3,7 @@
 #include "src/gui/dialogues/newairportdialog.h"
 
 AirportTableEditWidget::AirportTableEditWidget(QWidget *parent)
-    : TableEditWidget{parent}
+    : TableEditWidget(Vertical, parent)
 {}
 
 void AirportTableEditWidget::setupModelAndView()

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

@@ -6,7 +6,7 @@
 #include <QGridLayout>
 
 PilotTableEditWidget::PilotTableEditWidget(QWidget *parent)
-    : TableEditWidget(parent)
+    : TableEditWidget(Horizontal, parent)
 {}
 
 void PilotTableEditWidget::setupModelAndView()

+ 84 - 30
src/gui/widgets/tableeditwidget.cpp

@@ -4,8 +4,8 @@
 #include <QGridLayout>
 #include <QLabel>
 
-TableEditWidget::TableEditWidget(QWidget *parent)
-    : QWidget{parent}
+TableEditWidget::TableEditWidget(Orientation orientation, QWidget *parent)
+    : QWidget{parent}, _orientation(orientation)
 {}
 
 void TableEditWidget::init() {
@@ -17,8 +17,25 @@ void TableEditWidget::setupUI()
 {
     // Setting up the model and view is done in the derived class
     setupModelAndView();
+    _entryEditDialog = getEntryEditDialog(this);
+    stackedWidget->addWidget(_entryEditDialog);
+
+    // set up the UI
+    switch (_orientation) {
+    case Horizontal:
+        setupHorizontalUI();
+        break;
+    case Vertical:
+        setupVerticalUI();
+    default:
+        break;
+    }
+
+}
 
-    // Set up the editing widget
+void TableEditWidget::setupHorizontalUI()
+{
+    // In the horizontal view, the editing widget is hidden on the right hand side
     stackedWidget->hide();
 
     // create a 2-column grid layout and fill the cells
@@ -33,16 +50,38 @@ void TableEditWidget::setupUI()
     gridLayout->addWidget(stackedWidget, row, colR, allRowSpan, 1);
     row++;
 
-    gridLayout->addWidget(addNewEntryPushButton, row, colL);
+    setupButtonWidget();
+    gridLayout->addWidget(_buttonWidget);
+    row++;
+
+    setupFilterWidget();
+    gridLayout->addWidget(_filterWidget, row, colL);
+}
+
+void TableEditWidget::setupVerticalUI()
+{
+    // create a single column grid layout and fill the cells
+    int col = 0;
+    int row = 0;
+    auto gridLayout = new QGridLayout(this);
+
+    gridLayout->addWidget(view, row, col);
+    row++;
+
+    gridLayout->addWidget(stackedWidget, row, col);
     row++;
 
-    gridLayout->addWidget(deleteEntryPushButton, row, colL);
+    setupButtonWidget();
+    gridLayout->addWidget(_buttonWidget);
     row++;
 
-    gridLayout->addWidget(setupFilterWidget(), row, colL);
+    setupFilterWidget();
+    stackedWidget->addWidget(_filterWidget);
+    stackedWidget->setCurrentWidget(_filterWidget);
+    gridLayout->addWidget(stackedWidget);
 }
 
-QWidget *TableEditWidget::setupFilterWidget()
+void TableEditWidget::setupFilterWidget()
 {
     // place the filter items in a grid layout so they occupy one cell in parent layout
     QWidget *widget = new QWidget(this);
@@ -53,7 +92,28 @@ QWidget *TableEditWidget::setupFilterWidget()
     layout->addWidget(filterLineEdit,				  0, 1);
     layout->addWidget(filterSelectionComboBox,		  0, 2);
 
-    return widget;
+    _filterWidget = widget;
+}
+
+void TableEditWidget::setupButtonWidget()
+{
+    auto buttonWidget = new QWidget(this);
+    auto buttonGridLayout = new QGridLayout(buttonWidget);
+
+    switch (_orientation) {
+    case Horizontal:
+        buttonGridLayout->addWidget(addNewEntryPushButton, 0, 0);
+        buttonGridLayout->addWidget(deleteEntryPushButton, 1, 0);
+        break;
+    case Vertical:
+        buttonGridLayout->addWidget(addNewEntryPushButton, 0, 0);
+        buttonGridLayout->addWidget(deleteEntryPushButton, 0, 1);
+    default:
+        break;
+    }
+
+    buttonWidget->setLayout(buttonGridLayout);
+    _buttonWidget = buttonWidget;
 }
 
 void TableEditWidget::setupSignalsAndSlots()
@@ -87,19 +147,22 @@ void TableEditWidget::addEntryRequested()
 
 void TableEditWidget::editEntryRequested(const QModelIndex &selectedIndex)
 {
-    clearStackedWidget();
-
-    // create a Dialog for editing the selected entry and put it on the stackedWidget
     int rowId = model->index(selectedIndex.row(), 0).data().toInt();
-    auto editEntryDialog = getEntryEditDialog(this);
-    editEntryDialog->loadEntry(rowId);
-
-    stackedWidget->addWidget(editEntryDialog);
-    stackedWidget->setCurrentWidget(editEntryDialog);
-    stackedWidget->show();
-    editEntryDialog->exec();
-
-    stackedWidget->hide();
+    _entryEditDialog->loadEntry(rowId);
+    stackedWidget->setCurrentWidget(_entryEditDialog);
+
+    switch (_orientation) {
+    case Horizontal:
+        stackedWidget->show();
+        _entryEditDialog->exec();
+        stackedWidget->hide();
+        break;
+    case Vertical:
+        _entryEditDialog->exec();
+        stackedWidget->setCurrentWidget(_filterWidget);
+    default:
+        break;
+    }
 }
 
 void TableEditWidget::deleteEntryRequested()
@@ -111,7 +174,7 @@ void TableEditWidget::deleteEntryRequested()
     }
 
     stackedWidget->hide();
-    clearStackedWidget();
+    //clearStackedWidget();
     int rowId = model->index(selectedIndex.row(), 0).data().toInt();
     view->selectionModel()->reset();
 
@@ -135,15 +198,6 @@ void TableEditWidget::sortColumnChanged(int newSortColumn)
     view->sortByColumn(newSortColumn, Qt::AscendingOrder);
 }
 
-void TableEditWidget::clearStackedWidget()
-{
-    while (stackedWidget->count() > 0) {
-        QWidget *orphan = stackedWidget->currentWidget();
-        stackedWidget->removeWidget(orphan);
-        delete orphan;
-    }
-}
-
 void TableEditWidget::databaseContentChanged()
 {
     model->select();

+ 33 - 10
src/gui/widgets/tableeditwidget.h

@@ -15,15 +15,39 @@
 /*!
  * \brief The TableEditWidget class is a base class for widgets which enable
  * editing certain tables in the datbase.
+ * \details The TableEditWidget consists of a QTableView which displays the data
+ * from a given table. The data is held in a QSqlTableModel. The views edit triggers
+ * are disabled. Whenever a row is selected in the view, the selected entry is displayed
+ * for editing in a suitable EntryEditDialog which is responsible for the verification of
+ * the user input as well as reading and writing to and from the database.
+ *
+ * The TableEditWidget has two Orientation options: Horizontal and Vertical
+ *
+ * In the Horizontal layout, the table view is split horizontally to make space
+ * for the TableEditWidget on the right hand side, whereas on the Vertical layout
+ * it is split vertically with the TableEditWidget occupying the lower half of the screen.
+ *
+ * When implementing the TableEditWidget it is important to set up the model and call the
+ * Base Class implementation of setupUI before performing any specialisations. Before the
+ * TableEditWidget is shown, the init method must be run.
  */
 class TableEditWidget : public QWidget
 {
     Q_OBJECT
 public:
+    /*!
+     * \brief Determines how the layout is created
+     * \details <ul>
+     * <li> Horizontal: The edit widget is shown besides the table </li>
+     * <li> Vertical: The edit widget is shown below the table </li>
+     * </ul>
+     */
+    enum Orientation {Horizontal, Vertical};
+
     /*!
      * \brief Create a new TableEditWidget
      */
-    explicit TableEditWidget(QWidget *parent = nullptr);
+    explicit TableEditWidget(Orientation orientation = Horizontal, QWidget *parent = nullptr);
 
     /*!
      * \brief Initialises the dialog by calling its virtual setup functions.
@@ -71,8 +95,12 @@ public:
     virtual EntryEditDialog *getEntryEditDialog(QWidget *parent = nullptr) = 0;
 
 protected:
+    Orientation _orientation;
     QSqlTableModel *model = nullptr;
     QTableView *view = new QTableView(this);
+    QWidget *_filterWidget = nullptr;
+    QWidget *_buttonWidget = nullptr;
+    EntryEditDialog *_entryEditDialog = nullptr;
 
     QPushButton *addNewEntryPushButton = new QPushButton(this);
     QPushButton *deleteEntryPushButton = new QPushButton(this);
@@ -82,16 +110,11 @@ protected:
     QComboBox *filterSelectionComboBox = new QComboBox(this);
 
 private:
+    void setupHorizontalUI();
+    void setupVerticalUI();
     void setupSignalsAndSlots();
-    QWidget *setupFilterWidget();
-
-    /*!
-     * \brief Remove orphaned editing widgets from the stacked widget.
-     * \details When the user selects another entry without finishing editing a previous one,
-     * the previously created entry edit never finished. To prevent a leak, this function is called
-     * to clean up stale dialogs
-     */
-    void clearStackedWidget();
+    void setupFilterWidget();
+    void setupButtonWidget();
 
 private slots:
     void addEntryRequested();

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

@@ -3,7 +3,7 @@
 #include "src/gui/dialogues/newtaildialog.h"
 
 TailTableEditWidget::TailTableEditWidget(QWidget *parent)
-    : TableEditWidget{parent}
+    : TableEditWidget(Horizontal, parent)
 {}
 
 void TailTableEditWidget::setupModelAndView()