LeechCraft
0.6.70-16373-g319c272718
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 <QStringList>
11
#include <QtDebug>
12
#include <
util/sll/unreachable.h
>
13
#include "
util.h
"
14
15
namespace
LC::Util
16
{
17
TagsFilterModel::TagsFilterModel
(QObject *parent)
18
: QSortFilterProxyModel (parent)
19
, Separator_ (
GetDefaultTagsSeparator
())
20
{
21
}
22
23
void
TagsFilterModel::SetSeparator
(
const
QString
&
separator
)
24
{
25
Separator_ =
separator
;
26
27
if
(
dynamicSortFilter
())
28
invalidateFilter
();
29
}
30
31
void
TagsFilterModel::SetTagsInclusionMode
(
TagsFilterModel::TagsInclusionMode
mode
)
32
{
33
TagsMode_ =
mode
;
34
35
if
(
dynamicSortFilter
())
36
invalidateFilter
();
37
}
38
39
void
TagsFilterModel::SetTagsMode
(
bool
tags
)
40
{
41
NormalMode_ = !
tags
;
42
43
if
(
dynamicSortFilter
())
44
invalidateFilter
();
45
}
46
47
bool
TagsFilterModel::filterAcceptsRow
(
int
sourceRow
,
const
QModelIndex
& index)
const
48
{
49
return
NormalMode_ ?
50
FilterNormalMode (
sourceRow
, index) :
51
FilterTagsMode (
sourceRow
, index);
52
}
53
54
bool
TagsFilterModel::FilterNormalMode (
int
sourceRow
,
const
QModelIndex
& index)
const
55
{
56
if
(index.isValid () &&
sourceModel
()->rowCount (index))
57
return
true
;
58
59
const
auto
&
pattern
=
filterRegExp
().pattern ();
60
if
(
pattern
.isEmpty ())
61
return
true
;
62
63
for
(
int
i
= 0,
cc
=
sourceModel
()->columnCount (index);
i
<
cc
; ++
i
)
64
{
65
const
auto
&
rowIdx
=
sourceModel
()->index (
sourceRow
,
i
, index);
66
const
auto
&
str
=
rowIdx
.data ().toString ();
67
if
(
str
.contains (
pattern
) ||
filterRegExp
().
exactMatch
(
str
))
68
return
true
;
69
}
70
71
return
false
;
72
}
73
74
bool
TagsFilterModel::FilterTagsMode (
int
sourceRow
,
const
QModelIndex
&)
const
75
{
76
QList<QStringView>
filterTags
;
77
const
auto
&
pattern
=
filterRegExp
().pattern ();
78
for
(
const
auto
&
s
:
QStringView
{
pattern
}.
split
(Separator_,
Qt
::
SkipEmptyParts
))
79
filterTags
<<
s
.
trimmed
();
80
81
if
(
filterTags
.isEmpty ())
82
return
true
;
83
84
const
auto
&
itemTags
=
GetTagsForIndex
(
sourceRow
);
85
const
auto
hasTag
= [&] (
QStringView
tag
) {
return
itemTags
.contains (
tag
); };
86
switch
(TagsMode_)
87
{
88
case
TagsInclusionMode::Any
:
89
return
std::any_of (
filterTags
.begin (),
filterTags
.end (),
hasTag
);
90
case
TagsInclusionMode::All
:
91
return
std::all_of (
filterTags
.begin (),
filterTags
.end (),
hasTag
);
92
}
93
94
Util::Unreachable
();
95
}
96
}
LC::Util::TagsFilterModel::filterAcceptsRow
bool filterAcceptsRow(int, const QModelIndex &) const override
Reimplemented from QSortFilterProxyModel::filterAcceptsRow().
Definition
tagsfiltermodel.cpp:47
LC::Util::TagsFilterModel::SetTagsInclusionMode
void SetTagsInclusionMode(TagsInclusionMode mode)
Sets the tags inclusion mode.
Definition
tagsfiltermodel.cpp:31
LC::Util::TagsFilterModel::SetTagsMode
void SetTagsMode(bool enabled)
Sets whether the tags filtering mode is enabled.
Definition
tagsfiltermodel.cpp:39
LC::Util::TagsFilterModel::TagsInclusionMode
TagsInclusionMode
Describes the modes of matching two sets of tags.
Definition
tagsfiltermodel.h:39
LC::Util::TagsFilterModel::TagsInclusionMode::All
@ All
Filter string tags should be a subset of row tags.
LC::Util::TagsFilterModel::TagsInclusionMode::Any
@ Any
Tags intersection should be non-empty.
LC::Util::TagsFilterModel::SetSeparator
void SetSeparator(const QString &separator)
Sets the separator for the tags.
Definition
tagsfiltermodel.cpp:23
LC::Util::TagsFilterModel::TagsFilterModel
TagsFilterModel(QObject *parent=nullptr)
Creates the model with the given parent.
Definition
tagsfiltermodel.cpp:17
LC::Util::TagsFilterModel::GetTagsForIndex
virtual QStringList GetTagsForIndex(int row) const =0
Returns the list of tags for the given row.
QList
Definition
ianrulesstorage.h:14
LC::Util
Definition
icoreproxy.h:34
LC::Util::Filter
Container< T > Filter(const Container< T > &c, F f)
Definition
prelude.h:118
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.10.0