1 | /* |
2 | Copyright (c) 2009 Stephen Kelly <steveire@gmail.com> |
3 | Copyright (c) 2012 Laurent Montel <montel@kde.org> |
4 | |
5 | This library is free software; you can redistribute it and/or modify it |
6 | under the terms of the GNU Library General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or (at your |
8 | option) any later version. |
9 | |
10 | This library is distributed in the hope that it will be useful, but WITHOUT |
11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
13 | License for more details. |
14 | |
15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to the |
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 | 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "recursivecollectionfilterproxymodel.h" |
22 | |
23 | #include "entitytreemodel.h" |
24 | #include "mimetypechecker.h" |
25 | |
26 | #include <kdebug.h> |
27 | |
28 | using namespace Akonadi; |
29 | |
30 | namespace Akonadi |
31 | { |
32 | |
33 | class RecursiveCollectionFilterProxyModelPrivate |
34 | { |
35 | Q_DECLARE_PUBLIC(RecursiveCollectionFilterProxyModel) |
36 | RecursiveCollectionFilterProxyModel *q_ptr; |
37 | public: |
38 | RecursiveCollectionFilterProxyModelPrivate(RecursiveCollectionFilterProxyModel *model) |
39 | : q_ptr(model) |
40 | , checkOnlyChecked(false) |
41 | { |
42 | |
43 | } |
44 | |
45 | QSet<QString> includedMimeTypes; |
46 | Akonadi::MimeTypeChecker checker; |
47 | QString pattern; |
48 | bool checkOnlyChecked; |
49 | }; |
50 | |
51 | } |
52 | |
53 | RecursiveCollectionFilterProxyModel::RecursiveCollectionFilterProxyModel(QObject *parent) |
54 | : KRecursiveFilterProxyModel(parent) |
55 | , d_ptr(new RecursiveCollectionFilterProxyModelPrivate(this)) |
56 | { |
57 | |
58 | } |
59 | |
60 | RecursiveCollectionFilterProxyModel::~RecursiveCollectionFilterProxyModel() |
61 | { |
62 | delete d_ptr; |
63 | } |
64 | |
65 | bool RecursiveCollectionFilterProxyModel::acceptRow(int sourceRow, const QModelIndex &sourceParent) const |
66 | { |
67 | Q_D(const RecursiveCollectionFilterProxyModel); |
68 | |
69 | const QModelIndex rowIndex = sourceModel()->index(sourceRow, 0, sourceParent); |
70 | const Akonadi::Collection collection = rowIndex.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>(); |
71 | if (!collection.isValid()) { |
72 | return false; |
73 | } |
74 | const bool checked = (rowIndex.data(Qt::CheckStateRole).toInt() == Qt::Checked); |
75 | const bool isCheckable = sourceModel()->flags(rowIndex) & Qt::ItemIsUserCheckable; |
76 | if (isCheckable && (d->checkOnlyChecked && !checked)) { |
77 | return false; |
78 | } |
79 | |
80 | const bool collectionWanted = d->checker.isWantedCollection(collection); |
81 | if (collectionWanted) { |
82 | if (!d->pattern.isEmpty()) { |
83 | const QString text = rowIndex.data(Qt::DisplayRole).toString(); |
84 | return text.contains(d->pattern, Qt::CaseInsensitive); |
85 | } |
86 | } |
87 | return collectionWanted; |
88 | } |
89 | |
90 | void RecursiveCollectionFilterProxyModel::addContentMimeTypeInclusionFilter(const QString &mimeType) |
91 | { |
92 | Q_D(RecursiveCollectionFilterProxyModel); |
93 | d->includedMimeTypes << mimeType; |
94 | d->checker.setWantedMimeTypes(d->includedMimeTypes.toList()); |
95 | invalidateFilter(); |
96 | } |
97 | |
98 | void RecursiveCollectionFilterProxyModel::addContentMimeTypeInclusionFilters(const QStringList &mimeTypes) |
99 | { |
100 | Q_D(RecursiveCollectionFilterProxyModel); |
101 | d->includedMimeTypes.unite(mimeTypes.toSet()); |
102 | d->checker.setWantedMimeTypes(d->includedMimeTypes.toList()); |
103 | invalidateFilter(); |
104 | } |
105 | |
106 | void RecursiveCollectionFilterProxyModel::clearFilters() |
107 | { |
108 | Q_D(RecursiveCollectionFilterProxyModel); |
109 | d->includedMimeTypes.clear(); |
110 | d->checker.setWantedMimeTypes(QStringList()); |
111 | invalidateFilter(); |
112 | } |
113 | |
114 | void RecursiveCollectionFilterProxyModel::setContentMimeTypeInclusionFilters(const QStringList &mimeTypes) |
115 | { |
116 | Q_D(RecursiveCollectionFilterProxyModel); |
117 | d->includedMimeTypes = mimeTypes.toSet(); |
118 | d->checker.setWantedMimeTypes(d->includedMimeTypes.toList()); |
119 | invalidateFilter(); |
120 | } |
121 | |
122 | QStringList RecursiveCollectionFilterProxyModel::contentMimeTypeInclusionFilters() const |
123 | { |
124 | Q_D(const RecursiveCollectionFilterProxyModel); |
125 | return d->includedMimeTypes.toList(); |
126 | } |
127 | |
128 | int Akonadi::RecursiveCollectionFilterProxyModel::columnCount(const QModelIndex &index) const |
129 | { |
130 | // Optimization: we know that we're not changing the number of columns, so skip QSortFilterProxyModel |
131 | return sourceModel()->columnCount(mapToSource(index)); |
132 | } |
133 | |
134 | void Akonadi::RecursiveCollectionFilterProxyModel::setSearchPattern(const QString &pattern) |
135 | { |
136 | Q_D(RecursiveCollectionFilterProxyModel); |
137 | d->pattern = pattern; |
138 | invalidate(); |
139 | } |
140 | |
141 | void Akonadi::RecursiveCollectionFilterProxyModel::setIncludeCheckedOnly(bool checked) |
142 | { |
143 | Q_D(RecursiveCollectionFilterProxyModel); |
144 | d->checkOnlyChecked = checked; |
145 | invalidate(); |
146 | } |
147 | |