Mobile: correctly update filter text and update all three models

This is even harder because setActiveTrip is called from an action slot from
QML. If the C++ code called from that slot causes the object to which this slot
belongs to be destroyed, we get very strange crashes. The only workaround I
could come up with was to update the filter asynchronously.

This all seems very ugly and fragile.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2019-11-07 00:29:36 +01:00
parent 15674f1a71
commit 2c11544d93

View file

@ -7,6 +7,7 @@
#include "core/ssrf.h" // for LOG_STP
#include "core/errorhelper.h" // for verbose
#include <QDateTime>
#include <QtConcurrent>
#include <QDebug>
// the DiveListSortModel creates the sorted, filtered list of dives that the user
@ -101,8 +102,14 @@ QString CollapsedDiveListSortModel::tripShortDate(const QString &section)
void CollapsedDiveListSortModel::setActiveTrip(const QString &trip)
{
m_activeTrip = trip;
updateFilterState();
invalidateFilter();
// we can't update the filter state from the this function as that is called from
// a slot in the QML code which could cause the object that is executing the slot
// to be destroyed before this function returns.
// Instead do this asynchronously
QtConcurrent::run(QThreadPool::globalInstance(),
[=]{
CollapsedDiveListSortModel::instance()->updateFilterState();
});
}
QString CollapsedDiveListSortModel::activeTrip() const
@ -153,8 +160,8 @@ void CollapsedDiveListSortModel::updateFilterState()
}
// everything up to here can be done even if we don't have a source model
if (sourceModel() != nullptr) {
QVector<int> changedRoles = { DiveListModel::CollapsedRole };
dataChanged(index(0,0), index(rowCount() - 1, 0), changedRoles);
DiveListModel *dlm = DiveListModel::instance();
dlm->dataChanged(dlm->index(0,0), dlm->index(dlm->rowCount() - 1, 0));
}
}
@ -220,7 +227,7 @@ void DiveListSortModel::setSourceModel(QAbstractItemModel *sourceModel)
void DiveListSortModel::setFilter(QString f)
{
filterString = f;
updateFilterState();
CollapsedDiveListSortModel::instance()->updateFilterState();
invalidateFilter();
}