From 065423896dee0fe1cc6e2bd13e7e8d1b1cb3e181 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 5 May 2019 12:18:04 +0200 Subject: Filter: add reference counting for dive-site mode The dive-site-edit and dive-site-table tabs both put the filter into a special dive-site mode. When switching between both, it could happen that the one got its show befor the other got its hide event. Thus, the first would start dive-site filtering and the second stop it. Now the app was not in filter mode even though it should. To solve this problem, add reference counting for the filter's dive-site mode. In both tabs call the enter/exit functions on show/hide. In the dive-site-table tab, when the selection changes, use a set function that doesn't modify the reference count. Signed-off-by: Berthold Stoeger --- desktop-widgets/tab-widgets/TabDiveSite.cpp | 11 ++++++++--- desktop-widgets/tab-widgets/TabDiveSite.h | 1 + qt-models/filtermodels.cpp | 12 +++++++++++- qt-models/filtermodels.h | 9 +++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/desktop-widgets/tab-widgets/TabDiveSite.cpp b/desktop-widgets/tab-widgets/TabDiveSite.cpp index 113f8eafe..7deff3d9d 100644 --- a/desktop-widgets/tab-widgets/TabDiveSite.cpp +++ b/desktop-widgets/tab-widgets/TabDiveSite.cpp @@ -83,7 +83,7 @@ void TabDiveSite::on_filterText_textChanged(const QString &text) model.setFilter(text); } -void TabDiveSite::updateFilter() +QVector TabDiveSite::selectedDiveSites() { const QModelIndexList indexes = ui.diveSites->view()->selectionModel()->selectedIndexes(); QVector sites; @@ -92,7 +92,12 @@ void TabDiveSite::updateFilter() struct dive_site *ds = model.getDiveSite(idx); sites.append(ds); } - MultiFilterSortModel::instance()->startFilterDiveSites(sites); + return sites; +} + +void TabDiveSite::updateFilter() +{ + MultiFilterSortModel::instance()->setFilterDiveSite(selectedDiveSites()); } void TabDiveSite::selectionChanged(const QItemSelection &, const QItemSelection &) @@ -104,7 +109,7 @@ void TabDiveSite::showEvent(QShowEvent *) { // If the user switches to the dive site tab and there was already a selection, // filter on that selection. - updateFilter(); + MultiFilterSortModel::instance()->startFilterDiveSites(selectedDiveSites()); } void TabDiveSite::hideEvent(QHideEvent *) diff --git a/desktop-widgets/tab-widgets/TabDiveSite.h b/desktop-widgets/tab-widgets/TabDiveSite.h index 2f831f71c..ead94af5b 100644 --- a/desktop-widgets/tab-widgets/TabDiveSite.h +++ b/desktop-widgets/tab-widgets/TabDiveSite.h @@ -22,6 +22,7 @@ private slots: private: Ui::TabDiveSite ui; DiveSiteSortedModel model; + QVector selectedDiveSites(); void updateFilter(); void hideEvent(QHideEvent *) override; void showEvent(QShowEvent *) override; diff --git a/qt-models/filtermodels.cpp b/qt-models/filtermodels.cpp index bcadce613..ce0d5ff42 100644 --- a/qt-models/filtermodels.cpp +++ b/qt-models/filtermodels.cpp @@ -105,7 +105,8 @@ MultiFilterSortModel *MultiFilterSortModel::instance() } MultiFilterSortModel::MultiFilterSortModel(QObject *parent) : QSortFilterProxyModel(parent), - divesDisplayed(0) + divesDisplayed(0), + diveSiteRefCount(0) { setFilterKeyColumn(-1); // filter all columns setFilterCaseSensitivity(Qt::CaseInsensitive); @@ -269,15 +270,24 @@ void MultiFilterSortModel::clearFilter() void MultiFilterSortModel::startFilterDiveSites(QVector ds) { dive_sites = ds; + ++diveSiteRefCount; myInvalidate(); } void MultiFilterSortModel::stopFilterDiveSites() { + if (--diveSiteRefCount > 0) + return; dive_sites.clear(); myInvalidate(); } +void MultiFilterSortModel::setFilterDiveSite(QVector ds) +{ + dive_sites = ds; + myInvalidate(); +} + const QVector &MultiFilterSortModel::filteredDiveSites() const { return dive_sites; diff --git a/qt-models/filtermodels.h b/qt-models/filtermodels.h index b91b91a04..b726c2d28 100644 --- a/qt-models/filtermodels.h +++ b/qt-models/filtermodels.h @@ -70,6 +70,7 @@ slots: void myInvalidate(); void clearFilter(); void startFilterDiveSites(QVector ds); + void setFilterDiveSite(QVector ds); void stopFilterDiveSites(); void filterChanged(const QModelIndex &from, const QModelIndex &to, const QVector &roles); void resetModel(DiveTripModelBase::Layout layout); @@ -86,6 +87,14 @@ private: QVector dive_sites; void countsChanged(); FilterData filterData; + + // We use ref-counting for the dive site mode. The reason is that when switching + // between two tabs that both need dive site mode, the following course of + // events may happen: + // 1) The new tab appears -> enter dive site mode. + // 2) The old tab gets its hide() signal -> exit dive site mode. + // The filter is now not in dive site mode, even if it should + int diveSiteRefCount; }; #endif -- cgit v1.2.3-70-g09d2