LeechCraft
0.6.70-19424-g027e980fd1
Modular cross-platform feature rich live environment.
Toggle main menu visibility
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>
12
#include <
util/sll/unreachable.h
>
13
#include "
util.h
"
14
15
namespace
LC::Util
16
{
17
TagsFilterModel::TagsFilterModel
(QObject *parent)
18
:
FixedStringFilterProxyModel
{ parent }
19
, Separator_ {
GetDefaultTagsSeparator
() }
20
{
21
}
22
23
void
TagsFilterModel::SetTagsRole
(
int
role,
const
ITagsManager
& tm)
24
{
25
beginFilterChange ();
26
TagsRole_ = role;
27
TagsManager_ = &tm;
28
endFilterChange (Direction::Rows);
29
}
30
31
void
TagsFilterModel::SetSeparator
(
const
QString& separator)
32
{
33
beginFilterChange ();
34
Separator_ = separator;
35
endFilterChange (Direction::Rows);
36
}
37
38
void
TagsFilterModel::SetTagsInclusionMode
(
TagsFilterModel::TagsInclusionMode
mode)
39
{
40
beginFilterChange ();
41
TagsMode_ = mode;
42
endFilterChange (Direction::Rows);
43
}
44
45
void
TagsFilterModel::SetTagsMode
(
bool
tags)
46
{
47
beginFilterChange ();
48
NormalMode_ = !tags;
49
endFilterChange (Direction::Rows);
50
}
51
52
void
TagsFilterModel::UpdateDerivedFilter
(
const
QString&
string
)
53
{
54
FilterTags_.clear ();
55
for
(
const
auto
& s : QStringView {
string
}.split (Separator_, Qt::SkipEmptyParts))
56
FilterTags_ << s.trimmed ().toString ();
57
}
58
59
bool
TagsFilterModel::filterAcceptsRow
(
int
sourceRow,
const
QModelIndex& index)
const
60
{
61
if
(NormalMode_)
62
return
FixedStringFilterProxyModel::filterAcceptsRow
(sourceRow, index);
63
64
return
FilterTagsMode (sourceRow, index);
65
}
66
67
QStringList
TagsFilterModel::GetTagsForIndex
(
int
row)
const
68
{
69
if
(TagsRole_ < 0)
70
throw
std::runtime_error {
"no tags role for the default TagsFilterModel::GetTagsForIndex() implementation"
};
71
72
const
auto
model = sourceModel ();
73
if
(!model)
74
return
{};
75
76
return
TagsManager_->GetTags (model->index (row, 0).data (TagsRole_).value<
QList<TagId>
> ());
77
}
78
79
bool
TagsFilterModel::FilterTagsMode (
int
sourceRow,
const
QModelIndex&)
const
80
{
81
if
(FilterTags_.isEmpty ())
82
return
true
;
83
84
const
auto
& itemTags =
GetTagsForIndex
(sourceRow);
85
const
auto
hasTag = [&] (
const
QString& tag) {
return
itemTags.contains (tag); };
86
switch
(TagsMode_)
87
{
88
case
TagsInclusionMode::Any
:
89
return
std::ranges::any_of (FilterTags_, hasTag);
90
case
TagsInclusionMode::All
:
91
return
std::ranges::all_of (FilterTags_, hasTag);
92
}
93
94
Unreachable
();
95
}
96
}
ITagsManager
Tags manager's interface.
Definition
itagsmanager.h:91
LC::Util::FixedStringFilterProxyModel::FixedStringFilterProxyModel
FixedStringFilterProxyModel(QObject *=nullptr)
Definition
fixedstringfilterproxymodel.cpp:24
LC::Util::FixedStringFilterProxyModel::filterAcceptsRow
bool filterAcceptsRow(int row, const QModelIndex &parent) const override
Definition
fixedstringfilterproxymodel.cpp:124
LC::Util::TagsFilterModel::filterAcceptsRow
bool filterAcceptsRow(int, const QModelIndex &) const override
Definition
tagsfiltermodel.cpp:59
LC::Util::TagsFilterModel::SetTagsInclusionMode
void SetTagsInclusionMode(TagsInclusionMode mode)
Sets the tags inclusion mode.
Definition
tagsfiltermodel.cpp:38
LC::Util::TagsFilterModel::SetTagsMode
void SetTagsMode(bool enabled)
Sets whether the tags filtering mode is enabled.
Definition
tagsfiltermodel.cpp:45
LC::Util::TagsFilterModel::TagsInclusionMode
TagsInclusionMode
Describes the modes of matching two sets of tags.
Definition
tagsfiltermodel.h:43
LC::Util::TagsFilterModel::TagsInclusionMode::All
@ All
Filter string tags should be a subset of row tags.
Definition
tagsfiltermodel.h:56
LC::Util::TagsFilterModel::TagsInclusionMode::Any
@ Any
Tags intersection should be non-empty.
Definition
tagsfiltermodel.h:49
LC::Util::TagsFilterModel::GetTagsForIndex
virtual QStringList GetTagsForIndex(int row) const
Returns the list of tags for the given row.
Definition
tagsfiltermodel.cpp:67
LC::Util::TagsFilterModel::SetSeparator
void SetSeparator(const QString &separator)
Sets the separator for the tags.
Definition
tagsfiltermodel.cpp:31
LC::Util::TagsFilterModel::TagsFilterModel
TagsFilterModel(QObject *parent=nullptr)
Creates the model with the given parent.
Definition
tagsfiltermodel.cpp:17
LC::Util::TagsFilterModel::SetTagsRole
void SetTagsRole(int role, const ITagsManager &tm)
Makes the default GetTagsForIndex() read the tags from role.
Definition
tagsfiltermodel.cpp:23
LC::Util::TagsFilterModel::UpdateDerivedFilter
void UpdateDerivedFilter(const QString &) override
Definition
tagsfiltermodel.cpp:52
QList
Definition
ianrulesstorage.h:16
LC::Util
Definition
icoreproxy.h:34
LC::Util::Unreachable
void Unreachable()
Definition
unreachable.h:15
LC::Util::GetDefaultTagsSeparator
QString GetDefaultTagsSeparator()
Definition
util.cpp:14
util.h
tagsfiltermodel.h
unreachable.h
src
util
tags
tagsfiltermodel.cpp
Generated by
1.18.0