From 8695d8bdb15f52fdfa1a5df1da83b18c4486a9d8 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 12 Apr 2019 16:12:15 +0200 Subject: [PATCH] Dive sites: show dives at selected dive sites When in dive site tab and some dive sites are selected, show only dives at those sites. Simply read the selection and pass it to the filter. Start and stop filtering when switching to and from the tab, respectively. Signed-off-by: Berthold Stoeger --- desktop-widgets/tab-widgets/TabDiveSite.cpp | 32 +++++++++++++++++++++ desktop-widgets/tab-widgets/TabDiveSite.h | 4 +++ qt-models/divelocationmodel.h | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/desktop-widgets/tab-widgets/TabDiveSite.cpp b/desktop-widgets/tab-widgets/TabDiveSite.cpp index 816835dd3..c14cc6d4f 100644 --- a/desktop-widgets/tab-widgets/TabDiveSite.cpp +++ b/desktop-widgets/tab-widgets/TabDiveSite.cpp @@ -3,6 +3,7 @@ #include "core/subsurface-qt/DiveListNotifier.h" #include "core/divesite.h" #include "qt-models/divelocationmodel.h" +#include "qt-models/filtermodels.h" #include "desktop-widgets/command.h" #include @@ -23,6 +24,7 @@ TabDiveSite::TabDiveSite(QWidget *parent) : TabBase(parent) ui.diveSites->view()->setColumnHidden(i, true); connect(ui.diveSites, &TableView::addButtonClicked, this, &TabDiveSite::add); + connect(ui.diveSites->view()->selectionModel(), &QItemSelectionModel::selectionChanged, this, &TabDiveSite::selectionChanged); // Subtle: We depend on this slot being executed after the slot in the model. // This is realized because the model was constructed as a member object and connects in the constructor. @@ -79,3 +81,33 @@ void TabDiveSite::on_filterText_textChanged(const QString &text) { model.setFilter(text); } + +void TabDiveSite::updateFilter() +{ + const QModelIndexList indexes = ui.diveSites->view()->selectionModel()->selectedIndexes(); + QVector sites; + sites.reserve(indexes.size()); + for (const QModelIndex &idx: indexes) { + struct dive_site *ds = model.getDiveSite(idx); + sites.append(ds); + } + MultiFilterSortModel::instance()->startFilterDiveSites(sites); +} + +void TabDiveSite::selectionChanged(const QItemSelection &, const QItemSelection &) +{ + updateFilter(); +} + +void TabDiveSite::showEvent(QShowEvent *) +{ + // If the user switches to the dive site tab and there was already a selection, + // filter on that selection. + updateFilter(); +} + +void TabDiveSite::hideEvent(QHideEvent *) +{ + // If the user switches to a different tab, stop the dive site filtering + MultiFilterSortModel::instance()->stopFilterDiveSites(); +} diff --git a/desktop-widgets/tab-widgets/TabDiveSite.h b/desktop-widgets/tab-widgets/TabDiveSite.h index 1fdaeb989..2f831f71c 100644 --- a/desktop-widgets/tab-widgets/TabDiveSite.h +++ b/desktop-widgets/tab-widgets/TabDiveSite.h @@ -18,9 +18,13 @@ private slots: void diveSiteChanged(struct dive_site *ds, int field); void on_purgeUnused_clicked(); void on_filterText_textChanged(const QString &text); + void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected); private: Ui::TabDiveSite ui; DiveSiteSortedModel model; + void updateFilter(); + void hideEvent(QHideEvent *) override; + void showEvent(QShowEvent *) override; }; #endif diff --git a/qt-models/divelocationmodel.h b/qt-models/divelocationmodel.h index 914672725..a42354737 100644 --- a/qt-models/divelocationmodel.h +++ b/qt-models/divelocationmodel.h @@ -41,7 +41,6 @@ public slots: class DiveSiteSortedModel : public QSortFilterProxyModel { Q_OBJECT private: - struct dive_site *getDiveSite(const QModelIndex &idx); bool filterAcceptsRow(int sourceRow, const QModelIndex &source_parent) const override; bool lessThan(const QModelIndex &i1, const QModelIndex &i2) const override; QString fullText; @@ -54,6 +53,7 @@ public: DiveSiteSortedModel(); QStringList allSiteNames() const; void setFilter(const QString &text); + struct dive_site *getDiveSite(const QModelIndex &idx); }; // To access only divesites at the given GPS coordinates with the exception of a given dive site