Akonadi

recursivecollectionfilterproxymodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Stephen Kelly <[email protected]>
3  SPDX-FileCopyrightText: 2012-2020 Laurent Montel <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "recursivecollectionfilterproxymodel.h"
9 
10 #include "entitytreemodel.h"
11 #include "mimetypechecker.h"
12 
13 
14 using namespace Akonadi;
15 
16 namespace Akonadi
17 {
18 
19 class RecursiveCollectionFilterProxyModelPrivate
20 {
21  Q_DECLARE_PUBLIC(RecursiveCollectionFilterProxyModel)
23 public:
24  explicit RecursiveCollectionFilterProxyModelPrivate(RecursiveCollectionFilterProxyModel *model)
25  : q_ptr(model)
26  {
27 
28  }
29 
30  QSet<QString> includedMimeTypes;
33  bool checkOnlyChecked = false;
34 };
35 
36 } // namespace Akonadi
37 
39  : QSortFilterProxyModel(parent)
40  , d_ptr(new RecursiveCollectionFilterProxyModelPrivate(this))
41 {
43 }
44 
46 {
47  delete d_ptr;
48 }
49 
50 bool RecursiveCollectionFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
51 {
53 
54  const QModelIndex rowIndex = sourceModel()->index(sourceRow, 0, sourceParent);
56  if (!collection.isValid()) {
57  return false;
58  }
59  const bool checked = (rowIndex.data(Qt::CheckStateRole).toInt() == Qt::Checked);
60  const bool isCheckable = sourceModel()->flags(rowIndex) & Qt::ItemIsUserCheckable;
61  if (isCheckable && (d->checkOnlyChecked && !checked)) {
62  return false;
63  }
64 
65  const bool collectionWanted = d->checker.isWantedCollection(collection);
66  if (collectionWanted) {
67  if (!d->pattern.isEmpty()) {
68  const QString text = rowIndex.data(Qt::DisplayRole).toString();
69  return text.contains(d->pattern, Qt::CaseInsensitive);
70  }
71  }
72  return collectionWanted;
73 }
74 
75 
77 {
79  d->includedMimeTypes << mimeType;
80  d->checker.setWantedMimeTypes(d->includedMimeTypes.values());
82 }
83 
85 {
87 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
88  d->includedMimeTypes.unite(mimeTypes.toSet());
89 #else
90  d->includedMimeTypes.unite(QSet<QString>(mimeTypes.begin(), mimeTypes.end()));
91 #endif
92  d->checker.setWantedMimeTypes(d->includedMimeTypes.values());
94 }
95 
97 {
99  d->includedMimeTypes.clear();
100  d->checker.setWantedMimeTypes(QStringList());
102 }
103 
105 {
107 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
108  d->includedMimeTypes = mimeTypes.toSet();
109 #else
110  d->includedMimeTypes = QSet<QString>(mimeTypes.begin(), mimeTypes.end());
111 #endif
112  d->checker.setWantedMimeTypes(d->includedMimeTypes.values());
114 }
115 
117 {
119  return d->includedMimeTypes.values();
120 }
121 
122 int Akonadi::RecursiveCollectionFilterProxyModel::columnCount(const QModelIndex &index) const
123 {
124  // Optimization: we know that we're not changing the number of columns, so skip QSortFilterProxyModel
125  return sourceModel()->columnCount(mapToSource(index));
126 }
127 
129 {
131  if (d->pattern != pattern) {
132  d->pattern = pattern;
133  invalidate();
134  }
135 }
136 
138 {
140  if (d->checkOnlyChecked != checked) {
141  d->checkOnlyChecked = checked;
142  invalidate();
143  }
144 }
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const =0
bool isValid() const
Returns whether the collection is valid.
Definition: collection.cpp:124
void setRecursiveFilteringEnabled(bool recursive)
Represents a collection of PIM items.
Definition: collection.h:63
void setSearchPattern(const QString &pattern)
Add search pattern.
T value() const const
QSet< T > toSet() const const
Helper for checking MIME types of Collections and Items.
void addContentMimeTypeInclusionFilters(const QStringList &mimeTypes)
Add content mime types to be shown by the filter.
int toInt(bool *ok) const const
QString pattern(Mode mode=Reading)
CaseInsensitive
CheckStateRole
A model to filter out collections of non-matching content types.
void setIncludeCheckedOnly(bool checked)
Show only checked item.
virtual QStringList mimeTypes() const const override
QList::iterator end()
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
~RecursiveCollectionFilterProxyModel() override
Destroys the recursive collection filter proxy model.
void setContentMimeTypeInclusionFilters(const QStringList &mimeTypes)
Replace the content mime types to be shown by the filter.
QAbstractItemModel * sourceModel() const const
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
QVariant data(int role) const const
Helper integration between Akonadi and Qt.
void addContentMimeTypeInclusionFilter(const QString &mimeType)
Add content mime type to be shown by the filter.
virtual int columnCount(const QModelIndex &parent) const const =0
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
QStringList contentMimeTypeInclusionFilters() const
Returns the currently included mimetypes in the filter.
QString toString() const const
RecursiveCollectionFilterProxyModel(QObject *parent=nullptr)
Creates a new recursive collection filter proxy model.
QList::iterator begin()
ItemIsUserCheckable
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Jul 12 2020 23:16:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.