filter: move StringMode out of FilterData

The FilterData struct has the enum StringMode, which describes how
strings are searched (substring, startswith, exact). To make it
more generally accessible, remove it from the class. Since it is
an "enum class", the values don't pollute the global namespace anyway.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-02-14 13:10:24 +01:00 committed by Dirk Hohndel
parent 34549c4cc3
commit 41aae1aebe
3 changed files with 28 additions and 27 deletions

View file

@ -73,16 +73,16 @@ namespace {
// Check whether either all, any or none of the items of the first list is
// in the second list as a super string.
// The mode is controlled by the second argument
bool check(const QStringList &items, const QStringList &list, FilterData::Mode mode, FilterData::StringMode stringMode)
bool check(const QStringList &items, const QStringList &list, FilterData::Mode mode, StringFilterMode stringMode)
{
bool negate = mode == FilterData::Mode::NONE_OF;
bool any_of = mode == FilterData::Mode::ANY_OF;
StrCheck strchk =
stringMode == FilterData::StringMode::SUBSTRING ?
stringMode == StringFilterMode::SUBSTRING ?
[](const QString &s1, const QString &s2) { return s1.contains(s2, Qt::CaseInsensitive); } :
stringMode == FilterData::StringMode::STARTSWITH ?
stringMode == StringFilterMode::STARTSWITH ?
[](const QString &s1, const QString &s2) { return s1.startsWith(s2, Qt::CaseInsensitive); } :
/* FilterData::StringMode::EXACT */
/* StringFilterMode::EXACT */
[](const QString &s1, const QString &s2) { return s1.compare(s2, Qt::CaseInsensitive) == 0; };
auto fun = [&list, negate, strchk](const QString &item)
{ return listContainsSuperstring(list, item, strchk) != negate; };
@ -90,7 +90,7 @@ namespace {
: std::all_of(items.begin(), items.end(), fun);
}
bool hasTags(const QStringList &tags, const struct dive *d, FilterData::Mode mode, FilterData::StringMode stringMode)
bool hasTags(const QStringList &tags, const struct dive *d, FilterData::Mode mode, StringFilterMode stringMode)
{
if (tags.isEmpty())
return true;
@ -99,7 +99,7 @@ namespace {
return check(tags, dive_tags, mode, stringMode);
}
bool hasPersons(const QStringList &people, const struct dive *d, FilterData::Mode mode, FilterData::StringMode stringMode)
bool hasPersons(const QStringList &people, const struct dive *d, FilterData::Mode mode, StringFilterMode stringMode)
{
if (people.isEmpty())
return true;
@ -108,7 +108,7 @@ namespace {
return check(people, dive_people, mode, stringMode);
}
bool hasLocations(const QStringList &locations, const struct dive *d, FilterData::Mode mode, FilterData::StringMode stringMode)
bool hasLocations(const QStringList &locations, const struct dive *d, FilterData::Mode mode, StringFilterMode stringMode)
{
if (locations.isEmpty())
return true;
@ -123,12 +123,12 @@ namespace {
}
// TODO: Finish this implementation.
bool hasEquipment(const QStringList &, const struct dive *, FilterData::Mode, FilterData::StringMode)
bool hasEquipment(const QStringList &, const struct dive *, FilterData::Mode, StringFilterMode)
{
return true;
}
bool hasSuits(const QStringList &suits, const struct dive *d, FilterData::Mode mode, FilterData::StringMode stringMode)
bool hasSuits(const QStringList &suits, const struct dive *d, FilterData::Mode mode, StringFilterMode stringMode)
{
if (suits.isEmpty())
return true;
@ -138,7 +138,7 @@ namespace {
return check(suits, diveSuits, mode, stringMode);
}
bool hasNotes(const QStringList &dnotes, const struct dive *d, FilterData::Mode mode, FilterData::StringMode stringMode)
bool hasNotes(const QStringList &dnotes, const struct dive *d, FilterData::Mode mode, StringFilterMode stringMode)
{
if (dnotes.isEmpty())
return true;

View file

@ -13,6 +13,12 @@ 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.
@ -43,11 +49,6 @@ struct FilterData {
ANY_OF = 1,
NONE_OF = 2
};
enum class StringMode {
SUBSTRING = 0,
STARTSWITH = 1,
EXACT = 2
};
bool validFilter = false;
int minVisibility = 0;
@ -77,12 +78,12 @@ struct FilterData {
Mode dnotesMode = Mode::ALL_OF;
Mode suitMode = Mode::ANY_OF;
Mode equipmentMode = Mode::ALL_OF;
StringMode tagsStringMode = StringMode::SUBSTRING;
StringMode peopleStringMode = StringMode::SUBSTRING;
StringMode locationStringMode = StringMode::SUBSTRING;
StringMode dnotesStringMode = StringMode::SUBSTRING;
StringMode suitStringMode = StringMode::SUBSTRING;
StringMode equipmentStringMode = StringMode::SUBSTRING;
StringFilterMode tagsStringMode = StringFilterMode::SUBSTRING;
StringFilterMode peopleStringMode = StringFilterMode::SUBSTRING;
StringFilterMode locationStringMode = StringFilterMode::SUBSTRING;
StringFilterMode dnotesStringMode = StringFilterMode::SUBSTRING;
StringFilterMode suitStringMode = StringFilterMode::SUBSTRING;
StringFilterMode equipmentStringMode = StringFilterMode::SUBSTRING;
bool logged = true;
bool planned = true;
};

View file

@ -226,12 +226,12 @@ void FilterWidget2::updateFilter()
filterData.suitMode = (FilterData::Mode)ui.suitMode->currentIndex();
filterData.dnotesMode = (FilterData::Mode)ui.dnotesMode->currentIndex();
filterData.equipmentMode = (FilterData::Mode)ui.equipmentMode->currentIndex();
filterData.tagsStringMode = (FilterData::StringMode)ui.tagsStringMode->currentIndex();
filterData.peopleStringMode = (FilterData::StringMode)ui.peopleStringMode->currentIndex();
filterData.locationStringMode = (FilterData::StringMode)ui.locationStringMode->currentIndex();
filterData.suitStringMode = (FilterData::StringMode)ui.suitStringMode->currentIndex();
filterData.dnotesStringMode = (FilterData::StringMode)ui.dnotesStringMode->currentIndex();
filterData.equipmentStringMode = (FilterData::StringMode)ui.equipmentStringMode->currentIndex();
filterData.tagsStringMode = (StringFilterMode)ui.tagsStringMode->currentIndex();
filterData.peopleStringMode = (StringFilterMode)ui.peopleStringMode->currentIndex();
filterData.locationStringMode = (StringFilterMode)ui.locationStringMode->currentIndex();
filterData.suitStringMode = (StringFilterMode)ui.suitStringMode->currentIndex();
filterData.dnotesStringMode = (StringFilterMode)ui.dnotesStringMode->currentIndex();
filterData.equipmentStringMode = (StringFilterMode)ui.equipmentStringMode->currentIndex();
filterData.logged = ui.logged->isChecked();
filterData.planned = ui.planned->isChecked();