Browse Source

Started Refactoring of Logbookwidget

changed UI of LogbookWidget, added more powerful filter options (yet to be implemented) and streamlined setup and use of model and view.
Felix Turo 4 years ago
parent
commit
64d0a4c53c

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

@@ -68,6 +68,7 @@ void AircraftWidget::setupModelAndView()
     view->sortByColumn(sortColumn, Qt::DescendingOrder);
 
     view->show();
+    selection = view->selectionModel();
 
     QObject::connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
                      this, &AircraftWidget::tableView_selectionChanged);
@@ -155,7 +156,6 @@ void AircraftWidget::tableView_selectionChanged()
         /// stack does not seem to solve the problem since the Dialog does not get destroyed
         /// until either accept() or reject() is emitted so I went for this solution.
     }
-    auto *selection = ui->tableView->selectionModel();
     selectedTails.clear();
 
     for (const auto& row : selection->selectedRows()) {

+ 2 - 0
src/gui/widgets/aircraftwidget.h

@@ -62,6 +62,8 @@ private:
 
     QTableView *view;
 
+    QItemSelectionModel* selection;
+
     qint32 sortColumn;
 
     QVector<qint32> selectedTails;

+ 53 - 50
src/gui/widgets/logbookwidget.cpp

@@ -24,8 +24,6 @@ LogbookWidget::LogbookWidget(QWidget *parent) :
     ui(new Ui::LogbookWidget)
 {
     ui->setupUi(this);
-    ui->filterDateEdit->setDate(QDate::currentDate());
-    ui->filterDateEdit_2->setDate(QDate::currentDate());
     ui->newFlightButton->setFocus();
 
     //customContextMenu for tablewidget
@@ -33,10 +31,11 @@ LogbookWidget::LogbookWidget(QWidget *parent) :
     menu->addAction(ui->actionEdit_Flight);
     menu->addAction(ui->actionDelete_Flight);
 
-    //message Box
+    //Initialise message Box
     nope = new QMessageBox(this);
 
-    refreshView(ASettings::read("logbook/view").toInt());
+    prepareModelAndView(ASettings::read("logbook/view").toInt());
+    connectSignalsAndSlots();
 }
 
 LogbookWidget::~LogbookWidget()
@@ -48,39 +47,35 @@ LogbookWidget::~LogbookWidget()
  * Functions
  */
 
-void LogbookWidget::refreshView(int view_id)
+void LogbookWidget::prepareModelAndView(int view_id)
 {
     switch (view_id) {
     case 0:
-        defaultView();
+        setupDefaultView();
         break;
     case 1:
-        easaView();
+        setupEasaView();
         break;
     default:
-        defaultView();
+        setupDefaultView();
     }
-    QTableView *view = ui->tableView;
-    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);
-    connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
-            this, SLOT(tableView_selectionChanged()));
 }
 
-void LogbookWidget::defaultView()
+void LogbookWidget::connectSignalsAndSlots()
+{
+    selection = view->selectionModel();
+    QObject::connect(view->selectionModel(), &QItemSelectionModel::selectionChanged,
+                     this, &LogbookWidget::flightsTableView_selectionChanged);
+}
+
+void LogbookWidget::setupDefaultView()
 {
     DEB("Loading Default View...");
-    QSqlTableModel *model = new QSqlTableModel;
+    model = new QSqlTableModel;
     model->setTable("viewDefault");
     model->select();
 
-    QTableView *view = ui->tableView;
+    view = ui->tableView;
     view->setModel(model);
 
     view->setColumnWidth(1, 120);
@@ -94,17 +89,26 @@ void LogbookWidget::defaultView()
     view->setColumnWidth(9, 120);
     view->setColumnWidth(10, 90);
 
+    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);
+
     view->show();
 }
 
-void LogbookWidget::easaView()
+void LogbookWidget::setupEasaView()
 {
     DEB("Loading EASA View...");
-    QSqlTableModel *model = new QSqlTableModel;
+    model = new QSqlTableModel;
     model->setTable("viewEASA");
     model->select();
 
-    QTableView *view = ui->tableView;
+    view = ui->tableView;
     view->setModel(model);
 
     view->setColumnWidth(1,120);
@@ -129,6 +133,15 @@ void LogbookWidget::easaView()
     view->setColumnWidth(20,60);
     view->setColumnWidth(21,120);
 
+    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);
+
     view->show();
 }
 
@@ -136,13 +149,11 @@ void LogbookWidget::easaView()
  * Slots
  */
 
-void LogbookWidget::tableView_selectionChanged()//
+void LogbookWidget::flightsTableView_selectionChanged()//
 {
-    auto *selection = ui->tableView->selectionModel();
-
     selectedFlights.clear();
     for (const auto& row : selection->selectedRows()) {
-        selectedFlights << row.data().toInt();
+        selectedFlights.append(row.data().toInt());
         DEB("Selected Flight(s) with ID: " << selectedFlights);
     }
 }
@@ -152,7 +163,7 @@ void LogbookWidget::on_newFlightButton_clicked()
     auto nf = new NewFlightDialog(this, Db::createNew);
     nf->setAttribute(Qt::WA_DeleteOnClose);
     nf->exec();
-    refreshView(ASettings::read("logbook/view").toInt());
+    model->select();
 }
 
 void LogbookWidget::on_editFlightButton_clicked()
@@ -161,7 +172,7 @@ void LogbookWidget::on_editFlightButton_clicked()
         auto ef = new NewFlightDialog(this,Flight(selectedFlights.first()), Db::editExisting);
         ef->setAttribute(Qt::WA_DeleteOnClose);
         ef->exec();
-        refreshView(ASettings::read("logbook/view").toInt());
+        model->select();
     } else if (selectedFlights.isEmpty()) {
         nope->setText("No flight selected.\n");
         nope->exec();
@@ -205,7 +216,7 @@ void LogbookWidget::on_deleteFlightPushButton_clicked()
                 auto entry = Flight(selectedFlight);
                 entry.remove();
             }
-            refreshView(ASettings::read("logbook/view").toInt());
+            prepareModelAndView(ASettings::read("logbook/view").toInt());
         }
     } else if (selectedFlights.length() == 0) {
         nope->setIcon(QMessageBox::Information);
@@ -228,30 +239,16 @@ void LogbookWidget::on_deleteFlightPushButton_clicked()
                 auto entry = Flight(selectedFlight);
                 entry.remove();
             }
-            refreshView(ASettings::read("logbook/view").toInt());
+            prepareModelAndView(ASettings::read("logbook/view").toInt());
         }
     }
 }
 
-void LogbookWidget::on_filterFlightsByDateButton_clicked()
-{
-    QDate date(ui->filterDateEdit->date());
-    QString startdate = date.toString("yyyy-MM-dd");
-    date = ui->filterDateEdit_2->date();
-    QString enddate = date.toString("yyyy-MM-dd");
-    QString datefilter = "Date BETWEEN '" + startdate + "' AND '" + enddate + QLatin1Char('\'');
-
-    QSqlTableModel *DateFilteredModel = new QSqlTableModel;
-    DateFilteredModel ->setTable("Logbook");
-    DateFilteredModel ->setFilter(datefilter);
-    DateFilteredModel->select();
-
-    ui->tableView->setModel(DateFilteredModel);
-}
-
 void LogbookWidget::on_showAllButton_clicked()
 {
-    refreshView(ASettings::read("logbook/view").toInt());
+    ui->flightSearchLlineEdit->setText(QString());
+    model->setFilter(QString());
+    model->select();
 }
 
 void LogbookWidget::on_tableView_customContextMenuRequested(const QPoint &pos)
@@ -273,3 +270,9 @@ void LogbookWidget::on_tableView_doubleClicked()
 {
     emit ui->editFlightButton->clicked();
 }
+
+void LogbookWidget::on_flightSearchLlineEdit_textChanged(const QString &arg1)
+{
+    // to do:
+    // model->setFilter(arg1); depending on flightSearchComboBox Selection
+}

+ 17 - 11
src/gui/widgets/logbookwidget.h

@@ -25,6 +25,7 @@
 #include <chrono>
 #include <QDebug>
 #include <QMenu>
+#include <QTableView>
 
 #include "src/classes/asettings.h"
 #include "src/database/db.h"
@@ -35,19 +36,14 @@ namespace Ui {
 class LogbookWidget;
 }
 
-
-
 class LogbookWidget : public QWidget
 {
     Q_OBJECT
 
-
 public:
     explicit LogbookWidget(QWidget *parent = nullptr);
     ~LogbookWidget();
 
-    QVector<qint32> selectedFlights;
-
 private slots:
     void on_newFlightButton_clicked();
 
@@ -55,11 +51,9 @@ private slots:
 
     void on_deleteFlightPushButton_clicked();
 
-    void on_filterFlightsByDateButton_clicked();
-
     void on_showAllButton_clicked();
 
-    void tableView_selectionChanged();
+    void flightsTableView_selectionChanged();
 
     void on_tableView_customContextMenuRequested(const QPoint &pos);
 
@@ -69,18 +63,30 @@ private slots:
 
     void on_tableView_doubleClicked();
 
+    void on_flightSearchLlineEdit_textChanged(const QString &arg1);
+
 private:
     Ui::LogbookWidget *ui;
 
+    QTableView* view;
+
+    QSqlTableModel* model;
+
+    QItemSelectionModel* selection;
+
     QMenu* menu;
 
     QMessageBox* nope;
 
-    void refreshView(int view_id);
+    QVector<qint32> selectedFlights;
+
+    void prepareModelAndView(int view_id);
+
+    void setupDefaultView();
 
-    void defaultView();
+    void setupEasaView();
 
-    void easaView();
+    void connectSignalsAndSlots();
 
 
 };

+ 115 - 73
src/gui/widgets/logbookwidget.ui

@@ -6,114 +6,156 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>1353</width>
-    <height>613</height>
+    <width>1280</width>
+    <height>720</height>
    </rect>
   </property>
   <property name="windowTitle">
    <string>Form</string>
   </property>
   <layout class="QGridLayout" name="gridLayout">
-   <item row="2" column="0">
-    <widget class="QPushButton" name="editFlightButton">
-     <property name="text">
-      <string>Edit Flight</string>
+   <item row="0" column="0" colspan="3">
+    <widget class="QTableView" name="tableView">
+     <property name="font">
+      <font>
+       <family>Cantarell</family>
+      </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="3" column="0">
-    <widget class="QPushButton" name="deleteFlightPushButton">
+   <item row="1" column="0" rowspan="2">
+    <widget class="QPushButton" name="newFlightButton">
+     <property name="minimumSize">
+      <size>
+       <width>200</width>
+       <height>0</height>
+      </size>
+     </property>
      <property name="text">
-      <string>Delete Flight</string>
+      <string>New Flight</string>
      </property>
     </widget>
    </item>
    <item row="1" column="1">
-    <widget class="QDateEdit" name="filterDateEdit">
-     <property name="dateTime">
-      <datetime>
-       <hour>23</hour>
-       <minute>0</minute>
-       <second>0</second>
-       <year>1900</year>
-       <month>1</month>
-       <day>1</day>
-      </datetime>
-     </property>
-     <property name="displayFormat">
-      <string>yyyy-MM-dd</string>
-     </property>
-     <property name="calendarPopup">
-      <bool>true</bool>
-     </property>
-     <property name="timeSpec">
-      <enum>Qt::UTC</enum>
-     </property>
-     <property name="date">
-      <date>
-       <year>1900</year>
-       <month>1</month>
-       <day>1</day>
-      </date>
+    <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="1" column="2">
-    <widget class="QDateEdit" name="filterDateEdit_2">
-     <property name="dateTime">
-      <datetime>
-       <hour>23</hour>
-       <minute>0</minute>
-       <second>0</second>
-       <year>2019</year>
-       <month>12</month>
-       <day>13</day>
-      </datetime>
-     </property>
-     <property name="displayFormat">
-      <string>yyyy-MM-dd</string>
-     </property>
-     <property name="calendarPopup">
-      <bool>true</bool>
-     </property>
-     <property name="timeSpec">
-      <enum>Qt::UTC</enum>
+    <widget class="QLineEdit" name="flightSearchLlineEdit">
+     <property name="whatsThis">
+      <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>
     </widget>
    </item>
-   <item row="2" column="1" colspan="2">
-    <widget class="QPushButton" name="filterFlightsByDateButton">
+   <item row="2" column="2" rowspan="2">
+    <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="3" column="0">
+    <widget class="QPushButton" name="editFlightButton">
+     <property name="minimumSize">
+      <size>
+       <width>200</width>
+       <height>0</height>
+      </size>
+     </property>
      <property name="text">
-      <string>Filter Flights by Date</string>
+      <string>Edit Flight</string>
      </property>
     </widget>
    </item>
-   <item row="1" column="0">
-    <widget class="QPushButton" name="newFlightButton">
+   <item row="3" column="1">
+    <widget class="QLabel" name="flightSearchInLabel">
+     <property name="minimumSize">
+      <size>
+       <width>200</width>
+       <height>0</height>
+      </size>
+     </property>
      <property name="text">
-      <string>New Flight</string>
+      <string>Search in</string>
+     </property>
+     <property name="alignment">
+      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
      </property>
     </widget>
    </item>
-   <item row="0" column="0" colspan="3">
-    <widget class="QTableView" name="tableView">
-     <property name="font">
-      <font>
-       <family>Cantarell</family>
-      </font>
+   <item row="4" column="0">
+    <widget class="QPushButton" name="deleteFlightPushButton">
+     <property name="minimumSize">
+      <size>
+       <width>200</width>
+       <height>0</height>
+      </size>
      </property>
-     <property name="styleSheet">
-      <string notr="true"/>
+     <property name="text">
+      <string>Delete Flight</string>
      </property>
-     <property name="selectionMode">
-      <enum>QAbstractItemView::SingleSelection</enum>
+    </widget>
+   </item>
+   <item row="4" column="1">
+    <widget class="QLabel" name="spacerLabel">
+     <property name="minimumSize">
+      <size>
+       <width>200</width>
+       <height>0</height>
+      </size>
      </property>
-     <property name="selectionBehavior">
-      <enum>QAbstractItemView::SelectRows</enum>
+     <property name="text">
+      <string/>
+     </property>
+     <property name="alignment">
+      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
      </property>
     </widget>
    </item>
-   <item row="3" column="1" colspan="2">
+   <item row="4" column="2">
     <widget class="QPushButton" name="showAllButton">
      <property name="text">
       <string>Show All Flights</string>

+ 3 - 0
src/gui/widgets/pilotswidget.cpp

@@ -57,6 +57,9 @@ void PilotsWidget::setupModelAndView()
     view->setSortingEnabled(true);
     view->sortByColumn(sortColumn, Qt::AscendingOrder);
 
+    view->show();
+    selection = view->selectionModel();
+
     QObject::connect(ui->pilotsTableView->selectionModel(), &QItemSelectionModel::selectionChanged,
                      this, &PilotsWidget::tableView_selectionChanged);
     QObject::connect(ui->pilotsTableView->horizontalHeader(), &QHeaderView::sectionClicked,

+ 2 - 0
src/gui/widgets/pilotswidget.h

@@ -62,6 +62,8 @@ private:
 
     QTableView *view;
 
+    QItemSelectionModel* selection;
+
     qint32 sortColumn;
 
     QVector<qint32> selectedPilots;