LeechCraft 0.6.70-18808-g3467692359
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
tagsfiltermodel.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "tagsfiltermodel.h"
10#include <QRegularExpression>
11#include <QStringList>
13#include "util.h"
14
15namespace LC::Util
16{
19 , Separator_ { GetDefaultTagsSeparator () }
20 {
21 }
22
24 {
25 beginFilterChange ();
26 TagsRole_ = role;
27 endFilterChange (Direction::Rows);
28 }
29
30 void TagsFilterModel::SetSeparator (const QString& separator)
31 {
32 beginFilterChange ();
33 Separator_ = separator;
34 endFilterChange (Direction::Rows);
35 }
36
38 {
39 beginFilterChange ();
40 TagsMode_ = mode;
41 endFilterChange (Direction::Rows);
42 }
43
45 {
46 beginFilterChange ();
47 NormalMode_ = !tags;
48 endFilterChange (Direction::Rows);
49 }
50
51 void TagsFilterModel::UpdateDerivedFilter (const QString& string)
52 {
53 FilterTags_.clear ();
54 for (const auto& s : QStringView { string }.split (Separator_, Qt::SkipEmptyParts))
55 FilterTags_ << s.trimmed ().toString ();
56 }
57
58 bool TagsFilterModel::filterAcceptsRow (int sourceRow, const QModelIndex& index) const
59 {
60 if (NormalMode_)
61 return FixedStringFilterProxyModel::filterAcceptsRow (sourceRow, index);
62
63 return FilterTagsMode (sourceRow, index);
64 }
65
66 QStringList TagsFilterModel::GetTagsForIndex (int row) const
67 {
68 if (TagsRole_ < 0)
69 throw std::runtime_error { "no tags role for the default TagsFilterModel::GetTagsForIndex() implementation" };
70
71 const auto model = sourceModel ();
72 if (!model)
73 return {};
74
75 return model->index (row, 0).data (TagsRole_).toStringList ();
76 }
77
78 bool TagsFilterModel::FilterTagsMode (int sourceRow, const QModelIndex&) const
79 {
80 if (FilterTags_.isEmpty ())
81 return true;
82
83 const auto& itemTags = GetTagsForIndex (sourceRow);
84 const auto hasTag = [&] (const QString& tag) { return itemTags.contains (tag); };
85 switch (TagsMode_)
86 {
88 return std::ranges::any_of (FilterTags_, hasTag);
90 return std::ranges::all_of (FilterTags_, hasTag);
91 }
92
93 Unreachable ();
94 }
95}
bool filterAcceptsRow(int row, const QModelIndex &parent) const override
bool filterAcceptsRow(int, const QModelIndex &) const override
void SetTagsInclusionMode(TagsInclusionMode mode)
Sets the tags inclusion mode.
void SetTagsMode(bool enabled)
Sets whether the tags filtering mode is enabled.
TagsInclusionMode
Describes the modes of matching two sets of tags.
@ All
Filter string tags should be a subset of row tags.
@ Any
Tags intersection should be non-empty.
virtual QStringList GetTagsForIndex(int row) const
Returns the list of tags for the given row.
void SetSeparator(const QString &separator)
Sets the separator for the tags.
TagsFilterModel(QObject *parent=nullptr)
Creates the model with the given parent.
void UpdateDerivedFilter(const QString &) override
void Unreachable()
Definition unreachable.h:15
QString GetDefaultTagsSeparator()
Definition util.cpp:14