diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-02-19 21:36:28 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-03-01 10:21:44 -0800 |
commit | c1e33aac214c331f293e6257c9be509421f77c96 (patch) | |
tree | 7d69baa00ca2838d396d28875f91a65cf85cafcc /core | |
parent | f3a3d93b85c61a9b51c75409341a0473a965e911 (diff) | |
download | subsurface-c1e33aac214c331f293e6257c9be509421f77c96.tar.gz |
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>
Diffstat (limited to 'core')
-rw-r--r-- | core/divefilter.cpp | 50 | ||||
-rw-r--r-- | core/divefilter.h | 4 |
2 files changed, 51 insertions, 3 deletions
diff --git a/core/divefilter.cpp b/core/divefilter.cpp index 31ab08ba2..b4052213e 100644 --- a/core/divefilter.cpp +++ b/core/divefilter.cpp @@ -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" diff --git a/core/divefilter.h b/core/divefilter.h index 05add693e..ae5ba9869 100644 --- a/core/divefilter.h +++ b/core/divefilter.h @@ -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; |