1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Assistant of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 #ifndef BOOKMARKFILTERMODEL_H
29 #define BOOKMARKFILTERMODEL_H
30 
31 #include <QtCore/QPersistentModelIndex>
32 
33 #include <QtCore/QAbstractProxyModel>
34 #include <QtCore/QSortFilterProxyModel>
35 
36 QT_BEGIN_NAMESPACE
37 
38 class BookmarkItem;
39 class BookmarkModel;
40 
41 typedef QList<QPersistentModelIndex> PersistentModelIndexCache;
42 
43 class BookmarkFilterModel : public QAbstractProxyModel
44 {
45     Q_OBJECT
46 public:
47     explicit BookmarkFilterModel(QObject *parent = nullptr);
48 
49     void setSourceModel(QAbstractItemModel *sourceModel) override;
50 
51     int rowCount(const QModelIndex &index) const override;
52     int columnCount(const QModelIndex &index) const override;
53 
54     QModelIndex mapToSource(const QModelIndex &proxyIndex) const override;
55     QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override;
56 
57     QModelIndex parent(const QModelIndex &child) const override;
58     QModelIndex index(int row, int column, const QModelIndex &parent) const override;
59 
60     Qt::DropActions supportedDropActions () const override;
61     Qt::ItemFlags flags(const QModelIndex &index) const override;
62 
63     QVariant data(const QModelIndex &index, int role) const override;
64     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
65 
66     void filterBookmarks();
67     void filterBookmarkFolders();
68 
69 private slots:
70     void changed(const QModelIndex &topLeft, const QModelIndex &bottomRight);
71     void rowsInserted(const QModelIndex &parent, int start, int end);
72     void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
73     void rowsRemoved(const QModelIndex &parent, int start, int end);
74     void layoutAboutToBeChanged();
75     void layoutChanged();
76     void modelAboutToBeReset();
77     void modelReset();
78 
79 private:
80     void setupCache(const QModelIndex &parent);
81     void collectItems(const QModelIndex &parent);
82 
83 private:
84     BookmarkModel *sourceModel = nullptr;
85     PersistentModelIndexCache cache;
86     QPersistentModelIndex indexToRemove;
87     bool hideBookmarks = true;
88 };
89 
90 // -- BookmarkTreeModel
91 
92 class BookmarkTreeModel : public QSortFilterProxyModel
93 {
94     Q_OBJECT
95 public:
96     BookmarkTreeModel(QObject *parent = nullptr);
97     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
98 
99 protected:
100     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
101 };
102 
103 QT_END_NAMESPACE
104 
105 #endif // BOOKMARKFILTERMODEL_H
106