Browse Source

filtering the model is now virtual

The filtering of the model depends on column names and some implementation specific logic, so it is now a pure virtual function.
Felix Turowsky 1 year ago
parent
commit
44aa9ee9e1

+ 3 - 1
src/gui/dialogues/newpilotdialog.cpp

@@ -58,7 +58,9 @@ NewPilotDialog::~NewPilotDialog()
 void NewPilotDialog::setup()
 {
     ui->setupUi(this);
-    ui->companyLineEdit->setCompleter(QCompleterProvider.getCompleter(CompleterProvider::Companies));
+    auto completer = QCompleterProvider.getCompleter(CompleterProvider::Companies);
+    completer->setCompletionMode(QCompleter::InlineCompletion);
+    ui->companyLineEdit->setCompleter(completer);
 }
 
 void NewPilotDialog::on_buttonBox_accepted()

+ 25 - 3
src/gui/widgets/pilottableeditwidget.cpp

@@ -23,8 +23,8 @@ void PilotTableEditWidget::setupModelAndView()
     view->setSelectionMode(QAbstractItemView::SingleSelection);
     view->setSelectionBehavior(QAbstractItemView::SelectRows);
     view->setEditTriggers(QAbstractItemView::NoEditTriggers);
-    view->resizeColumnsToContents();
     view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
+    view->resizeColumnsToContents();
     view->verticalHeader()->hide();
     view->setAlternatingRowColors(true);
     for(const int i : COLS_TO_HIDE)
@@ -34,10 +34,13 @@ void PilotTableEditWidget::setupModelAndView()
 
 void PilotTableEditWidget::setupUI()
 {
+    // the base class does most of the setup
     TableEditWidget::setupUI();
+
+    // only need to set the table specific labels and combo box items
     addNewEntryPushButton->setText(tr("Add New Pilot"));
     deleteEntryPushButton->setText(tr("Delete Selected Pilot"));
-    filterSelectionComboBox->addItems({""});
+    filterSelectionComboBox->addItems(FILTER_COLUMNS);
 }
 
 EntryEditDialog *PilotTableEditWidget::getEntryEditDialog(QWidget *parent)
@@ -84,7 +87,26 @@ QString PilotTableEditWidget::confirmDeleteString(int rowId)
 {
     const auto entry = DB->getPilotEntry(rowId);
     return tr("You are deleting the following pilot:<br><br><b><tt>"
-           "%1, %2</b></tt><br><br>Are you sure?").arg(entry.getLastName(), entry.getFirstName());
+              "%1, %2</b></tt><br><br>Are you sure?").arg(entry.getLastName(), entry.getFirstName());
+}
+
+void PilotTableEditWidget::filterTextChanged(const QString &filterText)
+{
+    if(filterText.isEmpty()) {
+        model->setFilter(QStringLiteral("%1 > 1").arg(OPL::PilotEntry::ROWID)); // hide self
+        return;
+    }
+
+    int i = filterSelectionComboBox->currentIndex();
+    const QString filter =
+        QLatin1Char('\"')
+        + FILTER_COLUMN_NAMES.at(i)
+        + QLatin1String("\" LIKE '%")
+        + filterText
+        + QLatin1String("%' AND ")
+        + OPL::PilotEntry::ROWID
+        + QLatin1String(" > 1");
+    model->setFilter(filter);
 }
 
 

+ 8 - 2
src/gui/widgets/pilottableeditwidget.h

@@ -2,6 +2,7 @@
 #define PILOTTABLEEDITWIDGET_H
 
 #include "tableeditwidget.h"
+#include "src/database/pilotentry.h"
 
 class PilotTableEditWidget : public TableEditWidget
 {
@@ -20,6 +21,12 @@ private:
     const int COL_COMPANY = 4;
     const int COLS_TO_HIDE[5] = {0, 3, 5, 6, 7};
 
+    const static inline QStringList FILTER_COLUMNS = { tr("First Name"), tr("Last Name"), tr("Company") };
+    const static inline QStringList FILTER_COLUMN_NAMES = {
+                                                            OPL::PilotEntry::FIRSTNAME,
+                                                            OPL::PilotEntry::LASTNAME,
+                                                            OPL::PilotEntry::COMPANY };
+
     /*!
     * \brief Informs the user that deleting a Pilot has been unsuccessful
     *
@@ -33,9 +40,8 @@ private:
 
     virtual QString confirmDeleteString(int rowId) override;
 
-
 private slots:
-//    virtual void deleteEntryRequested() override;
+    virtual void filterTextChanged(const QString &filterText) override;
 };
 
 #endif // PILOTTABLEEDITWIDGET_H

+ 20 - 29
src/gui/widgets/tableeditwidget.cpp

@@ -1,7 +1,6 @@
 #include "tableeditwidget.h"
 #include "src/database/database.h"
 #include "src/opl.h"
-#include "src/classes/settings.h"
 #include <QGridLayout>
 #include <QLabel>
 
@@ -43,18 +42,38 @@ void TableEditWidget::setupUI()
     gridLayout->addWidget(setupFilterWidget(), row, colL);
 }
 
+QWidget *TableEditWidget::setupFilterWidget()
+{
+    // place the filter items in a grid layout so they occupy one cell in parent layout
+    QWidget *widget = new QWidget(this);
+    QGridLayout *layout = new QGridLayout(widget);
+
+    // one row, three columns
+    layout->addWidget(new QLabel(tr("Filter"), this), 0, 0);
+    layout->addWidget(filterLineEdit,				  0, 1);
+    layout->addWidget(filterSelectionComboBox,		  0, 2);
+
+    return widget;
+}
+
 void TableEditWidget::setupSignalsAndSlots()
 {
+    // refresh the view when the database is updated
     QObject::connect(DB,             		   &OPL::Database::dataBaseUpdated,
                      this,     		 		   &TableEditWidget::databaseContentChanged);
+    // filter the view
     QObject::connect(filterLineEdit,  		   &QLineEdit::textChanged,
                      this,                     &TableEditWidget::filterTextChanged);
+    // sort the view by column
     QObject::connect(view->horizontalHeader(), &QHeaderView::sectionClicked,
                      this,                     &TableEditWidget::sortColumnChanged);
+    // Edit an entry
     QObject::connect(view,					   &QTableView::clicked,
                      this, 			    	   &TableEditWidget::editEntryRequested);
+    // Add a new entry
     QObject::connect(addNewEntryPushButton,    &QPushButton::clicked,
                      this, 					   &TableEditWidget::addEntryRequested);
+    // Delete a selected entry
     QObject::connect(deleteEntryPushButton,    &QPushButton::clicked,
                      this, 					   &TableEditWidget::deleteEntryRequested);
 }
@@ -70,7 +89,6 @@ void TableEditWidget::addEntryRequested()
     editDialog->exec();
 
     stackedWidget->hide();
-
 }
 
 
@@ -89,7 +107,6 @@ void TableEditWidget::editEntryRequested(const QModelIndex &selectedIndex)
     editEntryDialog->exec();
 
     stackedWidget->hide();
-
 }
 
 void TableEditWidget::deleteEntryRequested()
@@ -120,35 +137,9 @@ void TableEditWidget::deleteEntryRequested()
     }
 }
 
-void TableEditWidget::filterTextChanged(const QString &filterString)
-{
-    TODO << "Create map <index, column Name> and implement func in derived";
-    model->setFilter(QLatin1Char('\"') + filterSelectionComboBox->currentText()
-                     + QLatin1String("\" LIKE '%") + filterString
-                     + QLatin1String("%' AND ID > 1"));
-}
-
 void TableEditWidget::sortColumnChanged(int newSortColumn)
 {
     view->sortByColumn(newSortColumn, Qt::AscendingOrder);
-    Settings::setPilotSortColumn(newSortColumn);
-}
-
-QWidget *TableEditWidget::setupFilterWidget()
-{
-    QWidget *widget = new QWidget(this);
-    QGridLayout *layout = new QGridLayout(widget);
-
-    int row =  0;
-    int colL = 0;
-    int colM = 1;
-    int colR = 2;
-
-    layout->addWidget(new QLabel(tr("Filter"), this), row, colL);
-    layout->addWidget(filterLineEdit, row, colM);
-    layout->addWidget(filterSelectionComboBox, row, colR);
-
-    return widget;
 }
 
 void TableEditWidget::clearStackedWidget()

+ 1 - 1
src/gui/widgets/tableeditwidget.h

@@ -97,8 +97,8 @@ private slots:
     void addEntryRequested();
     void editEntryRequested(const QModelIndex &selectedIndex);
     void deleteEntryRequested();
-    void filterTextChanged(const QString &filterString);
     void sortColumnChanged(int newSortColumn);
+    virtual void filterTextChanged(const QString &filterString) = 0;
 
 public slots:
     void databaseContentChanged();