Akonadi

recursivecollectionfilterproxymodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Stephen Kelly <[email protected]>
3  SPDX-FileCopyrightText: 2012-2022 Laurent Montel <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "recursivecollectionfilterproxymodel.h"
9 
10 #include "collectionutils.h"
11 #include "entitytreemodel.h"
12 #include "mimetypechecker.h"
13 
14 using namespace Akonadi;
15 
16 namespace Akonadi
17 {
18 class RecursiveCollectionFilterProxyModelPrivate
19 {
20  Q_DECLARE_PUBLIC(RecursiveCollectionFilterProxyModel)
22 
23 public:
24  explicit RecursiveCollectionFilterProxyModelPrivate(RecursiveCollectionFilterProxyModel *model)
25  : q_ptr(model)
26  {
27  }
28 
29  QSet<QString> includedMimeTypes;
32  bool checkOnlyChecked = false;
33  bool excludeUnifiedMailBox = false;
34 };
35 
36 } // namespace Akonadi
37 
39  : QSortFilterProxyModel(parent)
40  , d_ptr(new RecursiveCollectionFilterProxyModelPrivate(this))
41 {
43 }
44 
46 
47 bool RecursiveCollectionFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
48 {
50 
51  const QModelIndex rowIndex = sourceModel()->index(sourceRow, 0, sourceParent);
52  const auto collection = rowIndex.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
53  if (!collection.isValid()) {
54  return false;
55  }
56  if (CollectionUtils::isUnifiedMailbox(collection)) {
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 
76 {
78  d->includedMimeTypes << mimeType;
79  d->checker.setWantedMimeTypes(d->includedMimeTypes.values());
81 }
82 
84 {
86  d->includedMimeTypes.unite(QSet<QString>(mimeTypes.begin(), mimeTypes.end()));
87  d->checker.setWantedMimeTypes(d->includedMimeTypes.values());
89 }
90 
92 {
94  d->includedMimeTypes.clear();
95  d->checker.setWantedMimeTypes(QStringList());
97 }
98 
100 {
102  d->includedMimeTypes = QSet<QString>(mimeTypes.begin(), mimeTypes.end());
103  d->checker.setWantedMimeTypes(d->includedMimeTypes.values());
105 }
106 
108 {
110  return d->includedMimeTypes.values();
111 }
112 
113 int Akonadi::RecursiveCollectionFilterProxyModel::columnCount(const QModelIndex &index) const
114 {
115  // Optimization: we know that we're not changing the number of columns, so skip QSortFilterProxyModel
116  return sourceModel()->columnCount(mapToSource(index));
117 }
118 
120 {
122  if (d->pattern != pattern) {
123  d->pattern = pattern;
124  invalidate();
125  }
126 }
127 
129 {
131  if (d->checkOnlyChecked != checked) {
132  d->checkOnlyChecked = checked;
133  invalidate();
134  }
135 }
136 
138 {
140  if (d->excludeUnifiedMailBox != exclude) {
141  d->excludeUnifiedMailBox = exclude;
142  invalidate();
143  }
144 }
A model to filter out collections of non-matching content types.
CheckStateRole
CaseInsensitive
QString pattern(Mode mode=Reading)
RecursiveCollectionFilterProxyModel(QObject *parent=nullptr)
Creates a new recursive collection filter proxy model.
T value() const const
void setSearchPattern(const QString &pattern)
Add search pattern.
Represents a collection of PIM items.
Definition: collection.h:61
void addContentMimeTypeInclusionFilters(const QStringList &mimeTypes)
Add content mime types to be shown by the filter.
void setRecursiveFilteringEnabled(bool recursive)
QVariant data(int role) const const
ItemIsUserCheckable
virtual QStringList mimeTypes() const const override
@ CollectionRole
The collection.
QStringList contentMimeTypeInclusionFilters() const
Returns the currently included mimetypes in the filter.
int toInt(bool *ok) const const
void setIncludeCheckedOnly(bool checked)
Show only checked item.
void setExcludeUnifiedMailBox(bool exclude)
Don't show unified mailbox.
Helper for checking MIME types of Collections and Items.
QList::iterator begin()
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
~RecursiveCollectionFilterProxyModel() override
Destroys the recursive collection filter proxy model.
QList::iterator end()
void setContentMimeTypeInclusionFilters(const QStringList &mimeTypes)
Replace the content mime types to be shown by the filter.
void addContentMimeTypeInclusionFilter(const QString &mimeType)
Add content mime type to be shown by the filter.
Q_D(Todo)
QString toString() const const
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:48 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.