Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
bookmarkfiltermodel.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
4
5#include "bookmarkitem.h"
7
8BookmarkFilterModel::BookmarkFilterModel(QObject *parent)
9 : QAbstractProxyModel(parent)
10{
11}
12
13void BookmarkFilterModel::setSourceModel(QAbstractItemModel *_sourceModel)
14{
15 beginResetModel();
16
17 if (sourceModel) {
18 disconnect(sourceModel, &QAbstractItemModel::dataChanged,
19 this, &BookmarkFilterModel::changed);
20 disconnect(sourceModel, &QAbstractItemModel::rowsInserted,
21 this, &BookmarkFilterModel::rowsInserted);
22 disconnect(sourceModel, &QAbstractItemModel::rowsAboutToBeRemoved,
23 this, &BookmarkFilterModel::rowsAboutToBeRemoved);
24 disconnect(sourceModel, &QAbstractItemModel::rowsRemoved,
25 this, &BookmarkFilterModel::rowsRemoved);
26 disconnect(sourceModel, &QAbstractItemModel::layoutAboutToBeChanged,
27 this, &BookmarkFilterModel::layoutAboutToBeChanged);
28 disconnect(sourceModel, &QAbstractItemModel::layoutChanged,
29 this, &BookmarkFilterModel::layoutChanged);
30 disconnect(sourceModel, &QAbstractItemModel::modelAboutToBeReset,
31 this, &BookmarkFilterModel::modelAboutToBeReset);
32 disconnect(sourceModel, &QAbstractItemModel::modelReset,
33 this, &BookmarkFilterModel::modelReset);
34 }
35
36 sourceModel = qobject_cast<BookmarkModel*> (_sourceModel);
37 QAbstractProxyModel::setSourceModel(sourceModel);
38
39 if (sourceModel) {
40 connect(sourceModel, &QAbstractItemModel::dataChanged,
41 this, &BookmarkFilterModel::changed);
42 connect(sourceModel, &QAbstractItemModel::rowsInserted,
43 this, &BookmarkFilterModel::rowsInserted);
44 connect(sourceModel, &QAbstractItemModel::rowsAboutToBeRemoved,
45 this, &BookmarkFilterModel::rowsAboutToBeRemoved);
46 connect(sourceModel, &QAbstractItemModel::rowsRemoved,
47 this, &BookmarkFilterModel::rowsRemoved);
48 connect(sourceModel, &QAbstractItemModel::layoutAboutToBeChanged,
49 this, &BookmarkFilterModel::layoutAboutToBeChanged);
50 connect(sourceModel, &QAbstractItemModel::layoutChanged,
51 this, &BookmarkFilterModel::layoutChanged);
52 connect(sourceModel, &QAbstractItemModel::modelAboutToBeReset,
53 this, &BookmarkFilterModel::modelAboutToBeReset);
54 connect(sourceModel, &QAbstractItemModel::modelReset,
55 this, &BookmarkFilterModel::modelReset);
56
57 setupCache(sourceModel->index(0, 0, QModelIndex()).parent());
58 }
59 endResetModel();
60}
61
62int BookmarkFilterModel::rowCount(const QModelIndex &index) const
63{
64 Q_UNUSED(index);
65 return cache.size();
66}
67
68int BookmarkFilterModel::columnCount(const QModelIndex &index) const
69{
70 Q_UNUSED(index);
71 if (sourceModel)
72 return sourceModel->columnCount();
73 return 0;
74}
75
76QModelIndex BookmarkFilterModel::mapToSource(const QModelIndex &proxyIndex) const
77{
78 const int row = proxyIndex.row();
79 if (proxyIndex.isValid() && row >= 0 && row < cache.size())
80 return cache[row];
81 return QModelIndex();
82}
83
84QModelIndex BookmarkFilterModel::mapFromSource(const QModelIndex &sourceIndex) const
85{
86 return index(cache.indexOf(sourceIndex), 0, QModelIndex());
87}
88
89QModelIndex BookmarkFilterModel::parent(const QModelIndex &child) const
90{
91 Q_UNUSED(child);
92 return QModelIndex();
93}
94
96 const QModelIndex &index) const
97{
98 Q_UNUSED(index);
99 if (row < 0 || column < 0 || cache.size() <= row
100 || !sourceModel || sourceModel->columnCount() <= column) {
101 return QModelIndex();
102 }
103 return createIndex(row, 0);
104}
105
107{
108 if (sourceModel)
109 return sourceModel->supportedDropActions();
110 return Qt::IgnoreAction;
111}
112
113Qt::ItemFlags BookmarkFilterModel::flags(const QModelIndex &index) const
114{
115 if (sourceModel)
116 return sourceModel->flags(index);
117 return Qt::NoItemFlags;
118}
119
120QVariant BookmarkFilterModel::data(const QModelIndex &index, int role) const
121{
122 if (sourceModel)
123 return sourceModel->data(mapToSource(index), role);
124 return QVariant();
125}
126
127bool BookmarkFilterModel::setData(const QModelIndex &index, const QVariant &value,
128 int role)
129{
130 if (sourceModel)
131 return sourceModel->setData(mapToSource(index), value, role);
132 return false;
133}
134
136{
137 if (sourceModel) {
138 beginResetModel();
139 hideBookmarks = true;
140 setupCache(sourceModel->index(0, 0, QModelIndex()).parent());
141 endResetModel();
142 }
143}
144
146{
147 if (sourceModel) {
148 beginResetModel();
149 hideBookmarks = false;
150 setupCache(sourceModel->index(0, 0, QModelIndex()).parent());
151 endResetModel();
152 }
153}
154
155void BookmarkFilterModel::changed(const QModelIndex &topLeft,
156 const QModelIndex &bottomRight)
157{
158 emit dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight));
159}
160
161void BookmarkFilterModel::rowsInserted(const QModelIndex &parent, int start,
162 int end)
163{
164 if (!sourceModel)
165 return;
166
167 QModelIndex cachePrevious = parent;
168 if (BookmarkItem *parentItem = sourceModel->itemFromIndex(parent)) {
169 BookmarkItem *newItem = parentItem->child(start);
170
171 // iterate over tree hirarchie to find the previous folder
172 for (int i = 0; i < parentItem->childCount(); ++i) {
173 if (BookmarkItem *child = parentItem->child(i)) {
174 const QModelIndex &tmp = sourceModel->indexFromItem(child);
175 if (tmp.data(UserRoleFolder).toBool() && child != newItem)
176 cachePrevious = tmp;
177 }
178 }
179
180 const QModelIndex &newIndex = sourceModel->indexFromItem(newItem);
181 const bool isFolder = newIndex.data(UserRoleFolder).toBool();
182 if ((isFolder && hideBookmarks) || (!isFolder && !hideBookmarks)) {
183 beginInsertRows(mapFromSource(parent), start, end);
184 const int index = cache.indexOf(cachePrevious) + 1;
185 if (cache.value(index, QPersistentModelIndex()) != newIndex)
186 cache.insert(index, newIndex);
187 endInsertRows();
188 }
189 }
190}
191
192void BookmarkFilterModel::rowsAboutToBeRemoved(const QModelIndex &parent,
193 int start, int end)
194{
195 if (!sourceModel)
196 return;
197
198 if (BookmarkItem *parentItem = sourceModel->itemFromIndex(parent)) {
199 if (BookmarkItem *child = parentItem->child(start)) {
200 indexToRemove = sourceModel->indexFromItem(child);
201 if (cache.contains(indexToRemove))
202 beginRemoveRows(mapFromSource(parent), start, end);
203 }
204 }
205}
206
207void BookmarkFilterModel::rowsRemoved(const QModelIndex &/*parent*/, int, int)
208{
209 if (cache.contains(indexToRemove)) {
210 cache.removeAll(indexToRemove);
211 endRemoveRows();
212 }
213}
214
215void BookmarkFilterModel::layoutAboutToBeChanged()
216{
217 // TODO: ???
218}
219
220void BookmarkFilterModel::layoutChanged()
221{
222 // TODO: ???
223}
224
225void BookmarkFilterModel::modelAboutToBeReset()
226{
227 beginResetModel();
228}
229
230void BookmarkFilterModel::modelReset()
231{
232 if (sourceModel)
233 setupCache(sourceModel->index(0, 0, QModelIndex()).parent());
234 endResetModel();
235}
236
237void BookmarkFilterModel::setupCache(const QModelIndex &parent)
238{
239 cache.clear();
240 for (int i = 0; i < sourceModel->rowCount(parent); ++i)
241 collectItems(sourceModel->index(i, 0, parent));
242}
243
244void BookmarkFilterModel::collectItems(const QModelIndex &parent)
245{
246 if (parent.isValid()) {
247 bool isFolder = sourceModel->data(parent, UserRoleFolder).toBool();
248 if ((isFolder && hideBookmarks) || (!isFolder && !hideBookmarks))
249 cache.append(parent);
250
251 if (sourceModel->hasChildren(parent)) {
252 for (int i = 0; i < sourceModel->rowCount(parent); ++i)
253 collectItems(sourceModel->index(i, 0, parent));
254 }
255 }
256}
257
258// -- BookmarkTreeModel
259
260BookmarkTreeModel::BookmarkTreeModel(QObject *parent)
261 : QSortFilterProxyModel(parent)
262{
263}
264
265int BookmarkTreeModel::columnCount(const QModelIndex &parent) const
266{
267 return qMin(1, QSortFilterProxyModel::columnCount(parent));
268}
269
270bool BookmarkTreeModel::filterAcceptsRow(int row, const QModelIndex &parent) const
271{
272 Q_UNUSED(row);
273 BookmarkModel *model = qobject_cast<BookmarkModel*> (sourceModel());
274 if (model->rowCount(parent) > 0
275 && model->data(model->index(row, 0, parent), UserRoleFolder).toBool())
276 return true;
277 return false;
278}
Qt::DropActions supportedDropActions() const override
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
Reimplement this function to return the model index in the source model that corresponds to the proxy...
QVariant data(const QModelIndex &index, int role) const override
Returns the data stored under the given role for the item referred to by the index.
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Sets the role data for the item at index to value.
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns the item flags for the given index.
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
Reimplement this function to return the model index in the proxy model that corresponds to the source...
QModelIndex index(int row, int column, const QModelIndex &parent) const override
Returns the index of the item in the model specified by the given row, column and parent index.
int rowCount(const QModelIndex &index) const override
Returns the number of rows under the given parent.
int columnCount(const QModelIndex &index) const override
Returns the number of columns for the children of the given parent.
QModelIndex parent(const QModelIndex &child) const override
void setSourceModel(QAbstractItemModel *sourceModel) override
Sets the given sourceModel to be processed by the proxy model.
BookmarkItem * child(int number) const
int childCount() const
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
Returns true if the item in the row indicated by the given source_row and source_parent should be inc...
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns for the children of the given parent.