filter: implement searching for tags and people in mobile filter

Implement tag- and people-filtering in the mobile version of
DiveFilter. As opposed to the desktop version, this has no
different modes: it always searches "startswith" and "all of".
I.e. all of the search strings must match and a tag / person
is considered as matching if it starts with the search term.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-02-19 21:36:28 +01:00 committed by Dirk Hohndel
parent f3a3d93b85
commit c1e33aac21
2 changed files with 51 additions and 3 deletions

View file

@ -2,6 +2,7 @@
#include "divefilter.h"
#include "divelist.h"
#include "qthelper.h"
static void updateDiveStatus(dive *d, bool newStatus, ShownChange &change)
{
@ -15,6 +16,38 @@ static void updateDiveStatus(dive *d, bool newStatus, ShownChange &change)
#ifdef SUBSURFACE_MOBILE
// Check if a string-list contains at least one string that starts with the second argument.
// Comparison is non case sensitive and removes white space.
static bool listContainsSuperstring(const QStringList &list, const QString &s)
{
return std::any_of(list.begin(), list.end(), [&s](const QString &s2)
{ return s2.startsWith(s, Qt::CaseInsensitive); } );
}
static bool check(const QStringList &items, const QStringList &list)
{
return std::all_of(items.begin(), items.end(), [&list](const QString &item)
{ return listContainsSuperstring(list, item); });
}
bool hasTags(const QStringList &tags, const struct dive *d)
{
if (tags.isEmpty())
return true;
QStringList dive_tags = get_taglist_string(d->tag_list).split(",");
dive_tags.append(gettextFromC::tr(divemode_text_ui[d->dc.divemode]));
return check(tags, dive_tags);
}
bool hasPersons(const QStringList &people, const struct dive *d)
{
if (people.isEmpty())
return true;
QStringList dive_people = QString(d->buddy).split(",", QString::SkipEmptyParts)
+ QString(d->divemaster).split(",", QString::SkipEmptyParts);
return check(people, dive_people);
}
DiveFilter *DiveFilter::instance()
{
static DiveFilter self;
@ -40,6 +73,14 @@ ShownChange DiveFilter::update(const QVector<dive *> &dives) const
for (dive *d: dives)
updateDiveStatus(d, fulltext_dive_matches(d, filterData.fullText, StringFilterMode::STARTSWITH), res);
break;
case FilterData::Mode::PEOPLE:
for (dive *d: dives)
updateDiveStatus(d, hasPersons(filterData.tags, d), res);
break;
case FilterData::Mode::TAGS:
for (dive *d: dives)
updateDiveStatus(d, hasTags(filterData.tags, d), res);
break;
}
res.currentChanged = old_current != current_dive;
@ -65,6 +106,14 @@ ShownChange DiveFilter::updateAll() const
updateDiveStatus(d, ft.dive_matches(d), res);
break;
}
case FilterData::Mode::PEOPLE:
for_each_dive(i, d)
updateDiveStatus(d, hasPersons(filterData.tags, d), res);
break;
case FilterData::Mode::TAGS:
for_each_dive(i, d)
updateDiveStatus(d, hasTags(filterData.tags, d), res);
break;
}
res.currentChanged = old_current != current_dive;
@ -82,7 +131,6 @@ void DiveFilter::setFilter(const FilterData &data)
#include "desktop-widgets/mapwidget.h"
#include "desktop-widgets/mainwindow.h"
#include "desktop-widgets/divelistview.h"
#include "core/qthelper.h"
#include "core/trip.h"
#include "core/divesite.h"
#include "qt-models/filtermodels.h"

View file

@ -4,6 +4,7 @@
#define DIVE_FILTER_H
#include <QVector>
#include <QStringList>
#include "fulltext.h"
struct dive;
@ -31,7 +32,7 @@ struct FilterData {
Mode mode = Mode::NONE;
FullTextQuery fullText; // For fulltext
QString text; // For people and tags
QStringList tags; // For people and tags
};
class DiveFilter {
@ -50,7 +51,6 @@ private:
#else
#include <QDateTime>
#include <QStringList>
struct dive_trip;
struct dive_site;