From 2bd4c6f6ec8bdd65af28a5158a456f03171d2c0a Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 16 Feb 2020 22:31:06 +0100 Subject: filter: connect fulltext filter to frontend There are now three filter modes: 1) Dive site 2) Fulltext 3) Normal When doing a fulltext search, get the dives that match the fulltext filter and then apply the other filters on that list. Signed-off-by: Berthold Stoeger --- core/divefilter.cpp | 42 +++++++++++++++++++++++++++++++++--------- core/divefilter.h | 11 ++++------- 2 files changed, 37 insertions(+), 16 deletions(-) (limited to 'core') diff --git a/core/divefilter.cpp b/core/divefilter.cpp index 1372f5a0f..2953d2e65 100644 --- a/core/divefilter.cpp +++ b/core/divefilter.cpp @@ -21,14 +21,14 @@ ShownChange DiveFilter::updateAll() const #include "desktop-widgets/mapwidget.h" #include "desktop-widgets/mainwindow.h" #include "desktop-widgets/divelistview.h" +#include "core/fulltext.h" #include "core/qthelper.h" #include "core/trip.h" #include "core/divesite.h" #include "qt-models/filtermodels.h" -void DiveFilter::updateDiveStatus(dive *d, ShownChange &change) const +void DiveFilter::updateDiveStatus(dive *d, bool newStatus, ShownChange &change) const { - bool newStatus = showDive(d); if (filter_dive(d, newStatus)) { if (newStatus) change.newShown.push_back(d); @@ -40,9 +40,17 @@ void DiveFilter::updateDiveStatus(dive *d, ShownChange &change) const ShownChange DiveFilter::update(const QVector &dives) const { dive *old_current = current_dive; + ShownChange res; - for (dive *d: dives) - updateDiveStatus(d, res); + bool doDS = diveSiteMode(); + bool doFullText = filterData.fullText.doit(); + for (dive *d: dives) { + // There are three modes: divesite, fulltext, normal + bool newStatus = doDS ? dive_sites.contains(d->dive_site) : + doFullText ? fulltext_dive_matches(d, filterData.fullText, filterData.fulltextStringMode) && showDive(d) : + showDive(d); + updateDiveStatus(d, newStatus, res); + } res.currentChanged = old_current != current_dive; return res; } @@ -50,9 +58,28 @@ ShownChange DiveFilter::update(const QVector &dives) const ShownChange DiveFilter::updateAll() const { dive *old_current = current_dive; + ShownChange res; - for (int i = 0; i < dive_table.nr; ++i) - updateDiveStatus(get_dive(i), res); + int i; + dive *d; + // There are three modes: divesite, fulltext, normal + if (diveSiteMode()) { + for_each_dive(i, d) { + bool newStatus = dive_sites.contains(d->dive_site); + updateDiveStatus(d, newStatus, res); + } + } else if (filterData.fullText.doit()) { + FullTextResult ft = fulltext_find_dives(filterData.fullText, filterData.fulltextStringMode); + for_each_dive(i, d) { + bool newStatus = ft.dive_matches(d) && showDive(d); + updateDiveStatus(d, newStatus, res); + } + } else { + for_each_dive(i, d) { + bool newStatus = showDive(d); + updateDiveStatus(d, newStatus, res); + } + } res.currentChanged = old_current != current_dive; return res; } @@ -161,9 +188,6 @@ DiveFilter::DiveFilter() : diveSiteRefCount(0) bool DiveFilter::showDive(const struct dive *d) const { - if (diveSiteMode()) - return dive_sites.contains(d->dive_site); - if (!filterData.validFilter) return true; diff --git a/core/divefilter.h b/core/divefilter.h index 129fe0888..d035bbfcb 100644 --- a/core/divefilter.h +++ b/core/divefilter.h @@ -13,12 +13,6 @@ struct ShownChange { bool currentChanged; }; -enum class StringFilterMode { - SUBSTRING = 0, - STARTSWITH = 1, - EXACT = 2 -}; - // The dive filter for mobile is currently much simpler than for desktop. // Therefore, for now we have two completely separate implementations. // This should be unified in the future. @@ -36,6 +30,7 @@ private: #else +#include "fulltext.h" #include #include @@ -72,12 +67,14 @@ struct FilterData { QStringList suit; QStringList dnotes; QStringList equipment; + FullTextQuery fullText; Mode tagsMode = Mode::ALL_OF; Mode peopleMode = Mode::ALL_OF; Mode locationMode = Mode::ANY_OF; Mode dnotesMode = Mode::ALL_OF; Mode suitMode = Mode::ANY_OF; Mode equipmentMode = Mode::ALL_OF; + StringFilterMode fulltextStringMode = StringFilterMode::STARTSWITH; StringFilterMode tagsStringMode = StringFilterMode::SUBSTRING; StringFilterMode peopleStringMode = StringFilterMode::SUBSTRING; StringFilterMode locationStringMode = StringFilterMode::SUBSTRING; @@ -102,7 +99,7 @@ public: ShownChange updateAll() const; // Update filter status of all dives and return dives whose status changed private: DiveFilter(); - void updateDiveStatus(dive *d, ShownChange &change) const; + void updateDiveStatus(dive *d, bool newStatus, ShownChange &change) const; bool showDive(const struct dive *d) const; // Should that dive be shown? QVector dive_sites; -- cgit v1.2.3-70-g09d2