Filter: use hidden_by_filter also on mobile

Desktop used the hidden_in_filter flag in struct dive, mobile
used its own vector plus a new showndives member in struct dive_trip.

Unifiy these to use the same core-facility, viz. hidden_by_filter.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-11-22 23:28:06 +01:00 committed by Dirk Hohndel
parent 70897dd1b7
commit 1ebf5a99ed
3 changed files with 16 additions and 38 deletions

View file

@ -288,7 +288,6 @@ typedef struct dive_trip
char *location; char *location;
char *notes; char *notes;
struct dive_table dives; struct dive_table dives;
int showndives;
/* Used by the io-routines to mark trips that have already been written. */ /* Used by the io-routines to mark trips that have already been written. */
bool saved; bool saved;
bool autogen; bool autogen;

View file

@ -12,7 +12,7 @@ DiveListSortModel::DiveListSortModel(QObject *parent) : QSortFilterProxyModel(pa
void DiveListSortModel::updateFilterState() void DiveListSortModel::updateFilterState()
{ {
if (filterString.isEmpty()) { if (filterString.isEmpty()) {
filteredRows.clear(); resetFilter();
return; return;
} }
// store this in local variables to avoid having to call these methods over and over // store this in local variables to avoid having to call these methods over and over
@ -21,13 +21,11 @@ void DiveListSortModel::updateFilterState()
// get the underlying model and re-calculate the filter value for each dive // get the underlying model and re-calculate the filter value for each dive
DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel()); DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel());
filteredRows.clear();
filteredRows.resize(mySourceModel->rowCount());
for (int i = 0; i < mySourceModel->rowCount(); i++) { for (int i = 0; i < mySourceModel->rowCount(); i++) {
QString fullText = includeNotes? mySourceModel->at(i)->fullText() : mySourceModel->at(i)->fullTextNoNotes(); DiveObjectHelper *d = mySourceModel->at(i);
filteredRows.at(i) = fullText.contains(filterString, cs); QString fullText = includeNotes? d->fullText() : d->fullTextNoNotes();
d->getDive()->hidden_by_filter = !fullText.contains(filterString, cs);
} }
updateDivesShownInTrips();
} }
void DiveListSortModel::setSourceModel(QAbstractItemModel *sourceModel) void DiveListSortModel::setSourceModel(QAbstractItemModel *sourceModel)
@ -39,23 +37,23 @@ void DiveListSortModel::setFilter(QString f)
filterString = f; filterString = f;
updateFilterState(); updateFilterState();
invalidateFilter(); invalidateFilter();
updateDivesShownInTrips();
} }
void DiveListSortModel::resetFilter() void DiveListSortModel::resetFilter()
{ {
filterString = ""; int i;
filteredRows.clear(); struct dive *d;
for_each_dive(i, d)
d->hidden_by_filter = false;
invalidateFilter(); invalidateFilter();
updateDivesShownInTrips();
} }
// filtering is way too slow on mobile. Maybe we should roll our own? // filtering is way too slow on mobile. Maybe we should roll our own?
bool DiveListSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const bool DiveListSortModel::filterAcceptsRow(int source_row, const QModelIndex &) const
{ {
Q_UNUSED(source_parent) DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel());
DiveObjectHelper *d = mySourceModel->at(source_row);
return filteredRows.size() > source_row ? filteredRows[source_row] : true; return d && !d->getDive()->hidden_by_filter;
} }
int DiveListSortModel::shown() int DiveListSortModel::shown()
@ -84,7 +82,6 @@ void DiveListSortModel::clear()
{ {
DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel()); DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel());
mySourceModel->clear(); mySourceModel->clear();
filteredRows.clear();
} }
void DiveListSortModel::addAllDives() void DiveListSortModel::addAllDives()
@ -94,24 +91,6 @@ void DiveListSortModel::addAllDives()
updateFilterState(); updateFilterState();
} }
void DiveListSortModel::updateDivesShownInTrips()
{
// if filtering is active, reset all the counts to zero, otherwise set them to the full count
struct dive_trip *dt = dive_trip_list;
int rc = rowCount();
while (dt) {
dt->showndives = rc ? 0 : dt->dives.nr;
dt = dt->next;
}
for (int i = 0; i < rowCount(); i++) {
QVariant v = data(index(i, 0), DiveListModel::DiveRole);
DiveObjectHelper *d = v.value<DiveObjectHelper *>();
dt = d->getDive()->divetrip;
if (dt)
dt->showndives++;
}
}
// In QML, section headings can only be strings. To identify dives that // In QML, section headings can only be strings. To identify dives that
// belong to the same trip, a string containing the trip-pointer in hexadecimal // belong to the same trip, a string containing the trip-pointer in hexadecimal
// encoding is passed in. To format the trip heading, the string is then // encoding is passed in. To format the trip heading, the string is then
@ -131,7 +110,9 @@ QString DiveListSortModel::tripTitle(const QVariant &tripIn)
dive_trip *dt = tripIn.value<dive_trip *>(); dive_trip *dt = tripIn.value<dive_trip *>();
if (!dt) if (!dt)
return QString(); return QString();
QString numDives = tr("(%n dive(s))", "", dt->showndives); QString numDives = tr("(%n dive(s))", "", dt->dives.nr);
int shown = trip_shown_dives(dt);
QString shownDives = shown != dt->dives.nr ? QStringLiteral(" ") + tr("(%L1 shown)").arg(shown) : QString();
QString title(dt->location); QString title(dt->location);
if (title.isEmpty()) { if (title.isEmpty()) {
@ -149,7 +130,7 @@ QString DiveListSortModel::tripTitle(const QVariant &tripIn)
else else
title = firstMonth + " " + firstYear + " - " + lastMonth + " " + lastYear; title = firstMonth + " " + firstYear + " - " + lastMonth + " " + lastYear;
} }
return QStringLiteral("%1 %2").arg(title, numDives); return QStringLiteral("%1 %2%3").arg(title, numDives, shownDives);
} }
QString DiveListSortModel::tripShortDate(const QVariant &tripIn) QString DiveListSortModel::tripShortDate(const QVariant &tripIn)

View file

@ -24,11 +24,9 @@ public slots:
void setFilter(QString f); void setFilter(QString f);
void resetFilter(); void resetFilter();
int shown(); int shown();
void updateDivesShownInTrips();
protected: protected:
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const; bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
private: private:
std::vector<unsigned char> filteredRows; // using unsigned char because using 'bool' turns this into a bitfield
QString filterString; QString filterString;
void updateFilterState(); void updateFilterState();
}; };