summaryrefslogtreecommitdiffstats
path: root/qt-models/filtermodels.cpp
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-12-27 10:06:11 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-01-07 09:33:52 -0800
commitf1fc89b978c178fcd0f4f3ddb7629dc7df0d939d (patch)
tree14290d807253d3fc222da25ae9a283c365c59ca6 /qt-models/filtermodels.cpp
parentb5704ddb57d905e66f5f71558bb07b2f165913a9 (diff)
downloadsubsurface-f1fc89b978c178fcd0f4f3ddb7629dc7df0d939d.tar.gz
Dive list: split DiveTripModel into distinct models (tree and list)
The DiveTripModel was used to represent both, trip and list views. Thus many functions had conditionals checking for the current mode and both modes had to be represented by the same data structure. Instead, split the model in two and derive them from a base class, which implements common functions and defines an interface. The model can be switched by a call to resetModel(), which invalidates any pointer obtained by instance(). This is quite surprising behavior. To handle it, straighten out the control flow: DiveListView --> MultiFilterSortModel --> DiveTripModelBase Before, DiveListView accessed DiveTripModelBase directly. A goal of this commit is to enable usage of the same model by mobile and desktop. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'qt-models/filtermodels.cpp')
-rw-r--r--qt-models/filtermodels.cpp17
1 files changed, 9 insertions, 8 deletions
diff --git a/qt-models/filtermodels.cpp b/qt-models/filtermodels.cpp
index 785d36123..4e4134033 100644
--- a/qt-models/filtermodels.cpp
+++ b/qt-models/filtermodels.cpp
@@ -88,13 +88,14 @@ MultiFilterSortModel::MultiFilterSortModel(QObject *parent) : QSortFilterProxyMo
{
setFilterKeyColumn(-1); // filter all columns
setFilterCaseSensitivity(Qt::CaseInsensitive);
- setSourceModel(DiveTripModel::instance());
}
-void MultiFilterSortModel::setLayout(DiveTripModel::Layout layout)
+void MultiFilterSortModel::resetModel(DiveTripModelBase::Layout layout)
{
- DiveTripModel *tripModel = DiveTripModel::instance();
- tripModel->setLayout(layout); // Note: setLayout() resets the whole model
+ DiveTripModelBase::resetModel(layout);
+ // DiveTripModelBase::resetModel() generates a new instance.
+ // Thus, the source model must be reset.
+ setSourceModel(DiveTripModelBase::instance());
}
bool MultiFilterSortModel::showDive(const struct dive *d) const
@@ -143,14 +144,14 @@ bool MultiFilterSortModel::showDive(const struct dive *d) const
bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
QModelIndex index0 = sourceModel()->index(source_row, 0, source_parent);
- struct dive *d = sourceModel()->data(index0, DiveTripModel::DIVE_ROLE).value<struct dive *>();
+ struct dive *d = sourceModel()->data(index0, DiveTripModelBase::DIVE_ROLE).value<struct dive *>();
// For dives, simply check the hidden_by_filter flag
if (d)
return !d->hidden_by_filter;
// Since this is not a dive, it must be a trip
- dive_trip *trip = sourceModel()->data(index0, DiveTripModel::TRIP_ROLE).value<dive_trip *>();
+ dive_trip *trip = sourceModel()->data(index0, DiveTripModelBase::TRIP_ROLE).value<dive_trip *>();
if (!trip)
return false; // Oops. Neither dive nor trip, something is seriously wrong.
@@ -189,7 +190,7 @@ void MultiFilterSortModel::myInvalidate()
invalidateFilter();
// Tell the dive trip model to update the displayed-counts
- DiveTripModel::instance()->filterFinished();
+ DiveTripModelBase::instance()->filterFinished();
emit filterFinished();
#if !defined(SUBSURFACE_MOBILE)
@@ -218,7 +219,7 @@ void MultiFilterSortModel::stopFilterDiveSite()
bool MultiFilterSortModel::lessThan(const QModelIndex &i1, const QModelIndex &i2) const
{
// Hand sorting down to the source model.
- return DiveTripModel::instance()->lessThan(i1, i2);
+ return DiveTripModelBase::instance()->lessThan(i1, i2);
}
void MultiFilterSortModel::filterDataChanged(const FilterData& data)