Akonadi

entitymimetypefiltermodel.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Bruno Virlet <bruno.virlet@gmail.com>
3 SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "entitymimetypefiltermodel.h"
9#include "akonadicore_debug.h"
10
11#include <QString>
12#include <QStringList>
13
14using namespace Akonadi;
15
16namespace Akonadi
17{
18/**
19 * @internal
20 */
21class EntityMimeTypeFilterModelPrivate
22{
23public:
24 explicit EntityMimeTypeFilterModelPrivate(EntityMimeTypeFilterModel *parent)
25 : q_ptr(parent)
26 , m_headerGroup(EntityTreeModel::EntityTreeHeaders)
27 {
28 }
29
30 Q_DECLARE_PUBLIC(EntityMimeTypeFilterModel)
32
33 QStringList includedMimeTypes;
34 QStringList excludedMimeTypes;
35
36 QPersistentModelIndex m_rootIndex;
37
38 EntityTreeModel::HeaderGroup m_headerGroup;
39};
40
41} // namespace Akonadi
42
44 : QSortFilterProxyModel(parent)
45 , d_ptr(new EntityMimeTypeFilterModelPrivate(this))
46{
47}
48
50
52{
54 d->includedMimeTypes << typeList;
56}
57
59{
61 d->excludedMimeTypes << typeList;
63}
64
66{
68 d->includedMimeTypes << type;
70}
71
73{
75 d->excludedMimeTypes << type;
77}
78
79bool EntityMimeTypeFilterModel::filterAcceptsColumn(int sourceColumn, const QModelIndex &sourceParent) const
80{
81 if (sourceColumn >= columnCount(mapFromSource(sourceParent))) {
82 return false;
83 }
84 return QSortFilterProxyModel::filterAcceptsColumn(sourceColumn, sourceParent);
85}
86
87bool EntityMimeTypeFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
88{
90 const QModelIndex idx = sourceModel()->index(sourceRow, 0, sourceParent);
91
92 const QString rowMimetype = idx.data(EntityTreeModel::MimeTypeRole).toString();
93
94 if (d->excludedMimeTypes.contains(rowMimetype)) {
95 return false;
96 }
97
98 if (d->includedMimeTypes.isEmpty() || d->includedMimeTypes.contains(rowMimetype)) {
99 const auto item = idx.data(EntityTreeModel::ItemRole).value<Akonadi::Item>();
100
101 if (item.isValid() && !item.hasPayload()) {
102 qCDebug(AKONADICORE_LOG) << "Item " << item.id() << " doesn't have payload";
103 return false;
104 }
105
106 return true;
107 }
108
109 return false;
110}
111
113{
115 return d->includedMimeTypes;
116}
117
119{
121 return d->excludedMimeTypes;
122}
123
125{
127 d->includedMimeTypes.clear();
128 d->excludedMimeTypes.clear();
130}
131
133{
135 if (d->m_headerGroup != headerGroup) {
136 d->m_headerGroup = headerGroup; // this changes the column count
137 invalidateFilter(); // and filterAcceptsColumn depends on the column count
138 }
139}
140
141QVariant EntityMimeTypeFilterModel::headerData(int section, Qt::Orientation orientation, int role) const
142{
143 if (!sourceModel()) {
144 return QVariant();
145 }
146
148 role += (EntityTreeModel::TerminalUserRole * d->m_headerGroup);
149 return sourceModel()->headerData(section, orientation, role);
150}
151
152QModelIndexList EntityMimeTypeFilterModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const
153{
154 if (!sourceModel()) {
155 return QModelIndexList();
156 }
157
158 if (role < Qt::UserRole) {
159 return QSortFilterProxyModel::match(start, role, value, hits, flags);
160 }
161
162 QModelIndexList list;
163 QModelIndex proxyIndex;
164 const auto matches = sourceModel()->match(mapToSource(start), role, value, hits, flags);
165 for (const auto &idx : matches) {
166 proxyIndex = mapFromSource(idx);
167 if (proxyIndex.isValid()) {
168 list.push_back(proxyIndex);
169 }
170 }
171
172 return list;
173}
174
175int EntityMimeTypeFilterModel::columnCount(const QModelIndex &parent) const
176{
178
179 if (!sourceModel()) {
180 return 0;
181 }
182
184 if (!value.isValid()) {
185 return 0;
186 }
187
188 return value.toInt();
189}
190
191bool EntityMimeTypeFilterModel::hasChildren(const QModelIndex &parent) const
192{
193 if (!sourceModel()) {
194 return false;
195 }
196
197 // QSortFilterProxyModel implementation is buggy in that it emits rowsAboutToBeInserted etc
198 // only after the source model has emitted rowsInserted, instead of emitting it when the
199 // source model emits rowsAboutToBeInserted. That means that the source and the proxy are out
200 // of sync around the time of insertions, so we can't use the optimization below.
201 return rowCount(parent) > 0;
202#if 0
203
204 if (!parent.isValid()) {
205 return sourceModel()->hasChildren(parent);
206 }
207
209 if (EntityTreeModel::ItemListHeaders == d->m_headerGroup) {
210 return false;
211 }
212
213 if (EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup) {
214 QModelIndex childIndex = parent.child(0, 0);
215 while (childIndex.isValid()) {
217 if (col.isValid()) {
218 return true;
219 }
220 childIndex = childIndex.sibling(childIndex.row() + 1, childIndex.column());
221 }
222 }
223 return false;
224#endif
225}
226
227bool EntityMimeTypeFilterModel::canFetchMore(const QModelIndex &parent) const
228{
230 if (EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup) {
231 return false;
232 }
234}
235
236#include "moc_entitymimetypefiltermodel.cpp"
Represents a collection of PIM items.
Definition collection.h:62
A proxy model that filters entities by mime type.
void setHeaderGroup(EntityTreeModel::HeaderGroup headerGroup)
Sets the header set of the filter model.
QStringList mimeTypeInclusionFilters() const
Returns the list of mime type inclusion filters.
void addMimeTypeInclusionFilter(const QString &mimeType)
Add mime type to be shown by the filter.
void addMimeTypeExclusionFilter(const QString &mimeType)
Add mime type to be excluded by the filter.
void addMimeTypeInclusionFilters(const QStringList &mimeTypes)
Add mime types to be shown by the filter.
~EntityMimeTypeFilterModel() override
Destroys the entity mime type filter model.
void clearFilters()
Clear all mime type filters.
EntityMimeTypeFilterModel(QObject *parent=nullptr)
Creates a new entity mime type filter model.
QStringList mimeTypeExclusionFilters() const
Returns the list of mime type exclusion filters.
void addMimeTypeExclusionFilters(const QStringList &mimeTypes)
Add mimetypes to filter out.
A model for collections and items together.
HeaderGroup
Describes what header information the model shall return.
@ CollectionTreeHeaders
Header information for a collection-only tree.
@ ItemListHeaders
Header information for a list of items.
@ TerminalUserRole
Last role for user extensions. Don't use a role beyond this or headerData will break.
@ CollectionRole
The collection.
@ MimeTypeRole
The mimetype of the entity.
Represents a PIM item stored in Akonadi storage.
Definition item.h:100
Id id() const
Returns the unique identifier of the item.
Definition item.cpp:63
Q_SCRIPTABLE Q_NOREPLY void start()
Helper integration between Akonadi and Qt.
KIOCORE_EXPORT QStringList list(const QString &fileClass)
void push_back(parameter_type value)
int column() const const
QVariant data(int role) const const
bool isValid() const const
int row() const const
QModelIndex sibling(int row, int column) const const
QObject * parent() const const
virtual bool canFetchMore(const QModelIndex &parent) const const override
virtual bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const const
virtual Qt::ItemFlags flags(const QModelIndex &index) const const override
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
virtual QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const const override
virtual int rowCount(const QModelIndex &parent) const const override
UserRole
typedef MatchFlags
Orientation
void * data()
bool isValid() const const
int toInt(bool *ok) const const
QString toString() const const
T value() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:52:52 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.