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 Quick Controls module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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 Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QQUICKTREEMODELADAPTOR_H
41#define QQUICKTREEMODELADAPTOR_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtCore/qset.h>
55#include <QtCore/qpointer.h>
56#include <QtCore/qabstractitemmodel.h>
57#include <QtCore/qitemselectionmodel.h>
58
59QT_BEGIN_NAMESPACE
60
61class QAbstractItemModel;
62
63class QQuickTreeModelAdaptor1 : public QAbstractListModel
64{
65 Q_OBJECT
66 Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged)
67 Q_PROPERTY(QModelIndex rootIndex READ rootIndex WRITE setRootIndex RESET resetRootIndex NOTIFY rootIndexChanged)
68
69 struct TreeItem;
70
71public:
72 explicit QQuickTreeModelAdaptor1(QObject *parent = 0);
73
74 QAbstractItemModel *model() const;
75 const QModelIndex &rootIndex() const;
76 void setRootIndex(const QModelIndex &idx);
77 void resetRootIndex();
78
79 enum {
80 DepthRole = Qt::UserRole - 5,
81 ExpandedRole,
82 HasChildrenRole,
83 HasSiblingRole,
84 ModelIndexRole
85 };
86
87 QHash<int, QByteArray> roleNames() const override;
88 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
89 QVariant data(const QModelIndex &, int role) const override;
90 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
91
92 void clearModelData();
93
94 bool isVisible(const QModelIndex &index);
95 bool childrenVisible(const QModelIndex &index);
96
97 const QModelIndex &mapToModel(const QModelIndex &index) const;
98 Q_INVOKABLE QPersistentModelIndex mapRowToModelIndex(int row) const;
99
100 Q_INVOKABLE QItemSelection selectionForRowRange(const QModelIndex &fromIndex, const QModelIndex &toIndex) const;
101
102 void showModelTopLevelItems(bool doInsertRows = true);
103 void showModelChildItems(const TreeItem &parent, int start, int end, bool doInsertRows = true, bool doExpandPendingRows = true);
104
105 int itemIndex(const QModelIndex &index) const;
106 void expandPendingRows(bool doInsertRows = true);
107 int lastChildIndex(const QModelIndex &index);
108 void removeVisibleRows(int startIndex, int endIndex, bool doRemoveRows = true);
109
110 void expandRow(int n);
111 void collapseRow(int n);
112 bool isExpanded(int row) const;
113
114 Q_INVOKABLE bool isExpanded(const QModelIndex &) const;
115
116 void dump() const;
117 bool testConsistency(bool dumpOnFail = false) const;
118
119signals:
120 void modelChanged(QAbstractItemModel *model);
121 void rootIndexChanged();
122 void expanded(const QModelIndex &index);
123 void collapsed(const QModelIndex &index);
124
125public slots:
126 void expand(const QModelIndex &);
127 void collapse(const QModelIndex &);
128
129 void setModel(QAbstractItemModel *model);
130
131private slots:
132 void modelHasBeenDestroyed();
133 void modelHasBeenReset();
134 void modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRigth, const QVector<int> &roles);
135 void modelLayoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint);
136 void modelLayoutChanged(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint);
137 void modelRowsAboutToBeInserted(const QModelIndex & parent, int start, int end);
138 void modelRowsAboutToBeMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow);
139 void modelRowsAboutToBeRemoved(const QModelIndex & parent, int start, int end);
140 void modelRowsInserted(const QModelIndex & parent, int start, int end);
141 void modelRowsMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow);
142 void modelRowsRemoved(const QModelIndex & parent, int start, int end);
143
144private:
145 struct TreeItem {
146 QPersistentModelIndex index;
147 int depth;
148 bool expanded;
149
150 explicit TreeItem(const QModelIndex &idx = QModelIndex(), int d = 0, int e = false)
151 : index(idx), depth(d), expanded(e)
152 { }
153
154 inline bool operator== (const TreeItem &other) const
155 {
156 return this->index == other.index;
157 }
158 };
159
160 struct DataChangedParams {
161 QModelIndex topLeft;
162 QModelIndex bottomRight;
163 QVector<int> roles;
164 };
165
166 struct SignalFreezer {
167 SignalFreezer(QQuickTreeModelAdaptor1 *parent) : m_parent(parent) {
168 m_parent->enableSignalAggregation();
169 }
170 ~SignalFreezer() { m_parent->disableSignalAggregation(); }
171
172 private:
173 QQuickTreeModelAdaptor1 *m_parent;
174 };
175
176 void enableSignalAggregation();
177 void disableSignalAggregation();
178 bool isAggregatingSignals() const { return m_signalAggregatorStack > 0; }
179 void queueDataChanged(const QModelIndex &topLeft,
180 const QModelIndex &bottomRight,
181 const QVector<int> &roles);
182 void emitQueuedSignals();
183
184 QPointer<QAbstractItemModel> m_model;
185 QPersistentModelIndex m_rootIndex;
186 QList<TreeItem> m_items;
187 QSet<QPersistentModelIndex> m_expandedItems;
188 QList<TreeItem *> m_itemsToExpand;
189 mutable int m_lastItemIndex;
190 bool m_visibleRowsMoved;
191 int m_signalAggregatorStack;
192 QVector<DataChangedParams> m_queuedDataChanged;
193};
194
195QT_END_NAMESPACE
196
197#endif // QQUICKTREEMODELADAPTOR_H
198

source code of qtquickcontrols/src/controls/Private/qquicktreemodeladaptor_p.h