LeechCraft  0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
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 {
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 
32  {
33  TagsMode_ = mode;
34 
35  if (dynamicSortFilter ())
36  invalidateFilter ();
37  }
38 
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  {
89  return std::any_of (filterTags.begin (), filterTags.end (), hasTag);
91  return std::all_of (filterTags.begin (), filterTags.end (), hasTag);
92  }
93 
95  }
96 }
QString GetDefaultTagsSeparator()
Definition: util.cpp:14
void SetSeparator(const QString &separator)
Sets the separator for the tags.
void SetTagsMode(bool enabled)
Sets whether the tags filtering mode is enabled.
void SetTagsInclusionMode(TagsInclusionMode mode)
Sets the tags inclusion mode.
TagsFilterModel(QObject *parent=nullptr)
Creates the model with the given parent.
void Unreachable()
Definition: unreachable.h:15
Filter string tags should be a subset of row tags.
Tags intersection should be non-empty.
virtual QStringList GetTagsForIndex(int row) const =0
Returns the list of tags for the given row.
TagsInclusionMode
Describes the modes of matching two sets of tags.
bool filterAcceptsRow(int, const QModelIndex &) const override
Reimplemented from QSortFilterProxyModel::filterAcceptsRow().