Filter: send filterReset via signal

The old code called directly into the DiveListModel. Instead,
send a signal and hook into the signal from the model. This
will allow us to remove the DiveListModel::instance() function.

This, in turn, is a step towards supporting multiple models
at the same time. However, currently the model manually
sets the hidden_by_filter flag in the core and therefore
only one active model is supported at a time.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-12-09 15:13:53 +01:00 committed by Dirk Hohndel
parent 71307bce42
commit 358fddd24e
3 changed files with 19 additions and 12 deletions

View file

@ -193,7 +193,7 @@ void DiveFilter::startFilterDiveSites(QVector<dive_site *> ds)
// When switching into dive site mode, reload the dive sites. // When switching into dive site mode, reload the dive sites.
// We won't do this in myInvalidate() once we are in dive site mode. // We won't do this in myInvalidate() once we are in dive site mode.
MapWidget::instance()->reload(); MapWidget::instance()->reload();
DiveTripModelBase::instance()->recalculateFilter(); emit diveListNotifier.filterReset();
} }
} }
@ -202,7 +202,7 @@ void DiveFilter::stopFilterDiveSites()
if (--diveSiteRefCount > 0) if (--diveSiteRefCount > 0)
return; return;
dive_sites.clear(); dive_sites.clear();
DiveTripModelBase::instance()->recalculateFilter(); emit diveListNotifier.filterReset();
MapWidget::instance()->reload(); MapWidget::instance()->reload();
} }
@ -215,7 +215,7 @@ void DiveFilter::setFilterDiveSite(QVector<dive_site *> ds)
return; return;
dive_sites = ds; dive_sites = ds;
DiveTripModelBase::instance()->recalculateFilter(); emit diveListNotifier.filterReset();
MapWidget::instance()->setSelected(dive_sites); MapWidget::instance()->setSelected(dive_sites);
MainWindow::instance()->diveList->expandAll(); MainWindow::instance()->diveList->expandAll();
} }
@ -233,6 +233,6 @@ bool DiveFilter::diveSiteMode() const
void DiveFilter::setFilter(const FilterData &data) void DiveFilter::setFilter(const FilterData &data)
{ {
filterData = data; filterData = data;
DiveTripModelBase::instance()->recalculateFilter(); emit diveListNotifier.filterReset();
} }
#endif // SUBSURFACE_MOBILE #endif // SUBSURFACE_MOBILE

View file

@ -575,6 +575,7 @@ DiveTripModelTree::DiveTripModelTree(QObject *parent) : DiveTripModelBase(parent
connect(&diveListNotifier, &DiveListNotifier::divesTimeChanged, this, &DiveTripModelTree::divesTimeChanged); connect(&diveListNotifier, &DiveListNotifier::divesTimeChanged, this, &DiveTripModelTree::divesTimeChanged);
connect(&diveListNotifier, &DiveListNotifier::divesSelected, this, &DiveTripModelTree::divesSelected); connect(&diveListNotifier, &DiveListNotifier::divesSelected, this, &DiveTripModelTree::divesSelected);
connect(&diveListNotifier, &DiveListNotifier::tripChanged, this, &DiveTripModelTree::tripChanged); connect(&diveListNotifier, &DiveListNotifier::tripChanged, this, &DiveTripModelTree::tripChanged);
connect(&diveListNotifier, &DiveListNotifier::filterReset, this, &DiveTripModelTree::filterReset);
// Fill model // Fill model
for (int i = 0; i < dive_table.nr ; ++i) { for (int i = 0; i < dive_table.nr ; ++i) {
@ -745,7 +746,11 @@ bool DiveTripModelTree::calculateFilterForTrip(const std::vector<dive *> &dives,
return showTrip; return showTrip;
} }
void DiveTripModelTree::recalculateFilter() // This recalculates the filters and sends appropriate changed signals.
// Attention: Since this uses / modifies the hidden_by_filter flag of the
// core dive structure, only one DiveTripModel[Tree|List] must exist at
// a given time!
void DiveTripModelTree::filterReset()
{ {
// Collect the changes in a vector used later to send signals. // Collect the changes in a vector used later to send signals.
// This could be solved more efficiently in one pass, but // This could be solved more efficiently in one pass, but
@ -791,7 +796,6 @@ void DiveTripModelTree::recalculateFilter()
} }
emit diveListNotifier.numShownChanged(); emit diveListNotifier.numShownChanged();
emit diveListNotifier.filterReset();
} }
@ -1291,6 +1295,7 @@ DiveTripModelList::DiveTripModelList(QObject *parent) : DiveTripModelBase(parent
//connect(&diveListNotifier, &DiveListNotifier::divesMovedBetweenTrips, this, &DiveTripModelList::divesMovedBetweenTrips); //connect(&diveListNotifier, &DiveListNotifier::divesMovedBetweenTrips, this, &DiveTripModelList::divesMovedBetweenTrips);
connect(&diveListNotifier, &DiveListNotifier::divesTimeChanged, this, &DiveTripModelList::divesTimeChanged); connect(&diveListNotifier, &DiveListNotifier::divesTimeChanged, this, &DiveTripModelList::divesTimeChanged);
connect(&diveListNotifier, &DiveListNotifier::divesSelected, this, &DiveTripModelList::divesSelected); connect(&diveListNotifier, &DiveListNotifier::divesSelected, this, &DiveTripModelList::divesSelected);
connect(&diveListNotifier, &DiveListNotifier::filterReset, this, &DiveTripModelList::filterReset);
// Fill model // Fill model
items.reserve(dive_table.nr); items.reserve(dive_table.nr);
@ -1332,7 +1337,11 @@ dive *DiveTripModelList::diveOrNull(const QModelIndex &index) const
return items[row]; return items[row];
} }
void DiveTripModelList::recalculateFilter() // This recalculates the filters and sends appropriate changed signals.
// Attention: Since this uses / modifies the hidden_by_filter flag of the
// core dive structure, only one DiveTripModel[Tree|List] must exist at
// a given time!
void DiveTripModelList::filterReset()
{ {
// Collect the changes in a vector used later to send signals. // Collect the changes in a vector used later to send signals.
// This could be solved more efficiently in one pass, but // This could be solved more efficiently in one pass, but
@ -1342,7 +1351,7 @@ void DiveTripModelList::recalculateFilter()
changed.reserve(items.size()); changed.reserve(items.size());
{ {
// This marker prevents the UI from getting notifications on selection changes. // This marker prevents the UI from getting notifications on selection changes.
// It is active until the end of the scope. See comment in DiveTripModelTree::recalculateFilter(). // It is active until the end of the scope. See comment in DiveTripModelTree::filterReset().
auto marker = diveListNotifier.enterCommand(); auto marker = diveListNotifier.enterCommand();
DiveFilter *filter = DiveFilter::instance(); DiveFilter *filter = DiveFilter::instance();
@ -1356,7 +1365,6 @@ void DiveTripModelList::recalculateFilter()
sendShownChangedSignals(changed, noParent); sendShownChangedSignals(changed, noParent);
emit diveListNotifier.numShownChanged(); emit diveListNotifier.numShownChanged();
emit diveListNotifier.filterReset();
} }
QVariant DiveTripModelList::data(const QModelIndex &index, int role) const QVariant DiveTripModelList::data(const QModelIndex &index, int role) const

View file

@ -83,7 +83,6 @@ public:
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
DiveTripModelBase(QObject *parent = 0); DiveTripModelBase(QObject *parent = 0);
int columnCount(const QModelIndex&) const; int columnCount(const QModelIndex&) const;
virtual void recalculateFilter() = 0;
// Used for sorting. This is a bit of a layering violation, as sorting should be performed // Used for sorting. This is a bit of a layering violation, as sorting should be performed
// by the higher-up QSortFilterProxyModel, but it makes things so much easier! // by the higher-up QSortFilterProxyModel, but it makes things so much easier!
@ -121,6 +120,7 @@ public slots:
void divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives); void divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives);
void divesSelected(const QVector<dive *> &dives, dive *current); void divesSelected(const QVector<dive *> &dives, dive *current);
void tripChanged(dive_trip *trip, TripField); void tripChanged(dive_trip *trip, TripField);
void filterReset();
public: public:
DiveTripModelTree(QObject *parent = nullptr); DiveTripModelTree(QObject *parent = nullptr);
@ -133,7 +133,6 @@ private:
bool lessThan(const QModelIndex &i1, const QModelIndex &i2) const override; bool lessThan(const QModelIndex &i1, const QModelIndex &i2) const override;
void divesSelectedTrip(dive_trip *trip, const QVector<dive *> &dives, QVector<QModelIndex> &); void divesSelectedTrip(dive_trip *trip, const QVector<dive *> &dives, QVector<QModelIndex> &);
dive *diveOrNull(const QModelIndex &index) const override; dive *diveOrNull(const QModelIndex &index) const override;
void recalculateFilter();
void divesChangedTrip(dive_trip *trip, const QVector<dive *> &dives); void divesChangedTrip(dive_trip *trip, const QVector<dive *> &dives);
void divesTimeChangedTrip(dive_trip *trip, timestamp_t delta, const QVector<dive *> &dives); void divesTimeChangedTrip(dive_trip *trip, timestamp_t delta, const QVector<dive *> &dives);
bool calculateFilterForTrip(const std::vector<dive *> &dives, const DiveFilter *filter, quintptr parentIndex); bool calculateFilterForTrip(const std::vector<dive *> &dives, const DiveFilter *filter, quintptr parentIndex);
@ -188,6 +187,7 @@ public slots:
// Does nothing in list view. // Does nothing in list view.
//void divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector<dive *> &dives); //void divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector<dive *> &dives);
void divesSelected(const QVector<dive *> &dives, dive *current); void divesSelected(const QVector<dive *> &dives, dive *current);
void filterReset();
public: public:
DiveTripModelList(QObject *parent = nullptr); DiveTripModelList(QObject *parent = nullptr);
@ -199,7 +199,6 @@ private:
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
bool lessThan(const QModelIndex &i1, const QModelIndex &i2) const override; bool lessThan(const QModelIndex &i1, const QModelIndex &i2) const override;
dive *diveOrNull(const QModelIndex &index) const override; dive *diveOrNull(const QModelIndex &index) const override;
void recalculateFilter();
std::vector<dive *> items; // TODO: access core data directly std::vector<dive *> items; // TODO: access core data directly
}; };