LeechCraft
0.6.70-18450-gabe19ee3b0
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::SetFilterString
(
const
QString&
string
)
24
{
25
FilterTags_.clear ();
26
for
(
const
auto
& s :
string
.split (Separator_, Qt::SkipEmptyParts))
27
FilterTags_ << s.trimmed ();
28
29
FixedStringFilterProxyModel::SetFilterString
(
string
);
30
}
31
32
void
TagsFilterModel::SetTagsRole
(
int
role)
33
{
34
TagsRole_ = role;
35
invalidateFilter ();
36
}
37
38
void
TagsFilterModel::SetSeparator
(
const
QString& separator)
39
{
40
Separator_ = separator;
41
invalidateFilter ();
42
}
43
44
void
TagsFilterModel::SetTagsInclusionMode
(
TagsFilterModel::TagsInclusionMode
mode)
45
{
46
TagsMode_ = mode;
47
invalidateFilter ();
48
}
49
50
void
TagsFilterModel::SetTagsMode
(
bool
tags)
51
{
52
NormalMode_ = !tags;
53
invalidateFilter ();
54
}
55
56
bool
TagsFilterModel::filterAcceptsRow
(
int
sourceRow,
const
QModelIndex& index)
const
57
{
58
if
(NormalMode_)
59
return
FixedStringFilterProxyModel::filterAcceptsRow
(sourceRow, index);
60
61
return
FilterTagsMode (sourceRow, index);
62
}
63
64
QStringList
TagsFilterModel::GetTagsForIndex
(
int
row)
const
65
{
66
if
(TagsRole_ < 0)
67
throw
std::runtime_error {
"no tags role for the default TagsFilterModel::GetTagsForIndex() implementation"
};
68
69
const
auto
model = sourceModel ();
70
if
(!model)
71
return
{};
72
73
return
model->index (row, 0).data (TagsRole_).toStringList ();
74
}
75
76
bool
TagsFilterModel::FilterTagsMode (
int
sourceRow,
const
QModelIndex&)
const
77
{
78
if
(FilterTags_.isEmpty ())
79
return
true
;
80
81
const
auto
& itemTags =
GetTagsForIndex
(sourceRow);
82
const
auto
hasTag = [&] (
const
QString& tag) {
return
itemTags.contains (tag); };
83
switch
(TagsMode_)
84
{
85
case
TagsInclusionMode::Any
:
86
return
std::ranges::any_of (FilterTags_, hasTag);
87
case
TagsInclusionMode::All
:
88
return
std::ranges::all_of (FilterTags_, hasTag);
89
}
90
91
Unreachable
();
92
}
93
}
LC::Util::FixedStringFilterProxyModel::SetFilterString
virtual void SetFilterString(const QString &)
Definition
fixedstringfilterproxymodel.cpp:68
LC::Util::FixedStringFilterProxyModel::FixedStringFilterProxyModel
FixedStringFilterProxyModel(QObject *=nullptr)
Definition
fixedstringfilterproxymodel.cpp:14
LC::Util::FixedStringFilterProxyModel::filterAcceptsRow
bool filterAcceptsRow(int row, const QModelIndex &parent) const override
Definition
fixedstringfilterproxymodel.cpp:92
LC::Util::TagsFilterModel::filterAcceptsRow
bool filterAcceptsRow(int, const QModelIndex &) const override
Reimplemented from QSortFilterProxyModel::filterAcceptsRow().
Definition
tagsfiltermodel.cpp:56
LC::Util::TagsFilterModel::SetTagsInclusionMode
void SetTagsInclusionMode(TagsInclusionMode mode)
Sets the tags inclusion mode.
Definition
tagsfiltermodel.cpp:44
LC::Util::TagsFilterModel::SetTagsMode
void SetTagsMode(bool enabled)
Sets whether the tags filtering mode is enabled.
Definition
tagsfiltermodel.cpp:50
LC::Util::TagsFilterModel::SetTagsRole
void SetTagsRole(int role)
Definition
tagsfiltermodel.cpp:32
LC::Util::TagsFilterModel::TagsInclusionMode
TagsInclusionMode
Describes the modes of matching two sets of tags.
Definition
tagsfiltermodel.h:41
LC::Util::TagsFilterModel::TagsInclusionMode::All
@ All
Filter string tags should be a subset of row tags.
Definition
tagsfiltermodel.h:54
LC::Util::TagsFilterModel::TagsInclusionMode::Any
@ Any
Tags intersection should be non-empty.
Definition
tagsfiltermodel.h:47
LC::Util::TagsFilterModel::SetFilterString
void SetFilterString(const QString &) override
Definition
tagsfiltermodel.cpp:23
LC::Util::TagsFilterModel::GetTagsForIndex
virtual QStringList GetTagsForIndex(int row) const
Returns the list of tags for the given row.
Definition
tagsfiltermodel.cpp:64
LC::Util::TagsFilterModel::SetSeparator
void SetSeparator(const QString &separator)
Sets the separator for the tags.
Definition
tagsfiltermodel.cpp:38
LC::Util::TagsFilterModel::TagsFilterModel
TagsFilterModel(QObject *parent=nullptr)
Creates the model with the given parent.
Definition
tagsfiltermodel.cpp:17
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.17.0