8BookmarkFilterModel::BookmarkFilterModel(QObject *parent)
9 : QAbstractProxyModel(parent)
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,
28 disconnect(sourceModel, &QAbstractItemModel::layoutChanged,
30 disconnect(sourceModel, &QAbstractItemModel::modelAboutToBeReset,
32 disconnect(sourceModel, &QAbstractItemModel::modelReset,
37 QAbstractProxyModel::setSourceModel(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,
50 connect(sourceModel, &QAbstractItemModel::layoutChanged,
52 connect(sourceModel, &QAbstractItemModel::modelAboutToBeReset,
54 connect(sourceModel, &QAbstractItemModel::modelReset,
57 setupCache(sourceModel->index(0, 0, QModelIndex()).parent());
72 return sourceModel->columnCount();
78 const int row = proxyIndex.row();
79 if (proxyIndex.isValid() && row >= 0 && row < cache.size())
86 return index(cache.indexOf(sourceIndex), 0, QModelIndex());
96 const QModelIndex &index)
const
99 if (row < 0 || column < 0 || cache.size() <= row
100 || !sourceModel || sourceModel->columnCount() <= column) {
101 return QModelIndex();
103 return createIndex(row, 0);
109 return sourceModel->supportedDropActions();
110 return Qt::IgnoreAction;
116 return sourceModel->flags(index);
117 return Qt::NoItemFlags;
123 return sourceModel->data(mapToSource(index), role);
131 return sourceModel->setData(mapToSource(index), value, role);
139 hideBookmarks =
true;
140 setupCache(sourceModel->index(0, 0, QModelIndex()).parent());
149 hideBookmarks =
false;
150 setupCache(sourceModel->index(0, 0, QModelIndex()).parent());
156 const QModelIndex &bottomRight)
158 emit dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight));
167 QModelIndex cachePrevious = parent;
168 if (
BookmarkItem *parentItem = sourceModel->itemFromIndex(parent)) {
174 const QModelIndex &tmp = sourceModel->indexFromItem(child);
180 const QModelIndex &newIndex = sourceModel->indexFromItem(newItem);
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);
198 if (
BookmarkItem *parentItem = sourceModel->itemFromIndex(parent)) {
200 indexToRemove = sourceModel->indexFromItem(child);
201 if (cache.contains(indexToRemove))
202 beginRemoveRows(mapFromSource(parent), start, end);
209 if (cache.contains(indexToRemove)) {
210 cache.removeAll(indexToRemove);
233 setupCache(sourceModel->index(0, 0, QModelIndex()).parent());
240 for (
int i = 0; i < sourceModel->rowCount(parent); ++i)
241 collectItems(sourceModel->index(i, 0, parent));
246 if (parent.isValid()) {
247 bool isFolder = sourceModel->data(parent,
UserRoleFolder).toBool();
248 if ((isFolder && hideBookmarks) || (!isFolder && !hideBookmarks))
249 cache.append(parent);
251 if (sourceModel->hasChildren(parent)) {
252 for (
int i = 0; i < sourceModel->rowCount(parent); ++i)
253 collectItems(sourceModel->index(i, 0, parent));
261 : QSortFilterProxyModel(parent)
267 return qMin(1, QSortFilterProxyModel::columnCount(parent));
274 if (model->rowCount(parent) > 0
275 && model->data(model->index(row, 0, parent),
UserRoleFolder).toBool())
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.
void filterBookmarkFolders()
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
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.