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 QtWidgets 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 QTREEWIDGET_P_H |
41 | #define QTREEWIDGET_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. This header file may change |
48 | // from version to version without notice, or even be removed. |
49 | // |
50 | // We mean it. |
51 | // |
52 | |
53 | #include <QtWidgets/private/qtwidgetsglobal_p.h> |
54 | #include <QtCore/qabstractitemmodel.h> |
55 | #include <private/qabstractitemmodel_p.h> |
56 | #include <QtCore/qpair.h> |
57 | #include <QtCore/qbasictimer.h> |
58 | #include <QtWidgets/qtreewidget.h> |
59 | #include <private/qtreeview_p.h> |
60 | #include <QtWidgets/qheaderview.h> |
61 | |
62 | QT_REQUIRE_CONFIG(treewidget); |
63 | |
64 | QT_BEGIN_NAMESPACE |
65 | |
66 | class QTreeWidgetItem; |
67 | class QTreeWidgetItemIterator; |
68 | class QTreeModelPrivate; |
69 | |
70 | class QTreeModel : public QAbstractItemModel |
71 | { |
72 | Q_OBJECT |
73 | friend class QTreeWidget; |
74 | friend class QTreeWidgetPrivate; |
75 | friend class QTreeWidgetItem; |
76 | friend class QTreeWidgetItemPrivate; |
77 | friend class QTreeWidgetItemIterator; |
78 | friend class QTreeWidgetItemIteratorPrivate; |
79 | |
80 | public: |
81 | explicit QTreeModel(int columns = 0, QTreeWidget *parent = nullptr); |
82 | ~QTreeModel(); |
83 | |
84 | inline QTreeWidget *view() const |
85 | { return qobject_cast<QTreeWidget*>(object: QObject::parent()); } |
86 | |
87 | void clear(); |
88 | void setColumnCount(int columns); |
89 | |
90 | QTreeWidgetItem *item(const QModelIndex &index) const; |
91 | void itemChanged(QTreeWidgetItem *item); |
92 | |
93 | QModelIndex index(const QTreeWidgetItem *item, int column) const; |
94 | QModelIndex index(int row, int column, const QModelIndex &parent) const override; |
95 | QModelIndex parent(const QModelIndex &child) const override; |
96 | int rowCount(const QModelIndex &parent) const override; |
97 | int columnCount(const QModelIndex &parent = QModelIndex()) const override; |
98 | bool hasChildren(const QModelIndex &parent) const override; |
99 | |
100 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
101 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
102 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
103 | bool clearItemData(const QModelIndex &index) override; |
104 | #endif |
105 | QMap<int, QVariant> itemData(const QModelIndex &index) const override; |
106 | |
107 | QVariant (int section, Qt::Orientation orientation, int role) const override; |
108 | bool (int section, Qt::Orientation orientation, const QVariant &value, |
109 | int role) override; |
110 | |
111 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
112 | |
113 | void sort(int column, Qt::SortOrder order) override; |
114 | void ensureSorted(int column, Qt::SortOrder order, |
115 | int start, int end, const QModelIndex &parent); |
116 | static bool itemLessThan(const QPair<QTreeWidgetItem*,int> &left, |
117 | const QPair<QTreeWidgetItem*,int> &right); |
118 | static bool itemGreaterThan(const QPair<QTreeWidgetItem*,int> &left, |
119 | const QPair<QTreeWidgetItem*,int> &right); |
120 | static QList<QTreeWidgetItem*>::iterator sortedInsertionIterator( |
121 | const QList<QTreeWidgetItem*>::iterator &begin, |
122 | const QList<QTreeWidgetItem*>::iterator &end, |
123 | Qt::SortOrder order, QTreeWidgetItem *item); |
124 | |
125 | bool insertRows(int row, int count, const QModelIndex &) override; |
126 | bool insertColumns(int column, int count, const QModelIndex &) override; |
127 | |
128 | bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; |
129 | |
130 | // dnd |
131 | QStringList mimeTypes() const override; |
132 | QMimeData *mimeData(const QModelIndexList &indexes) const override; |
133 | bool dropMimeData(const QMimeData *data, Qt::DropAction action, |
134 | int row, int column, const QModelIndex &parent) override; |
135 | Qt::DropActions supportedDropActions() const override; |
136 | |
137 | QMimeData *internalMimeData() const; |
138 | |
139 | inline QModelIndex createIndexFromItem(int row, int col, QTreeWidgetItem *item) const |
140 | { return createIndex(arow: row, acolumn: col, adata: item); } |
141 | |
142 | protected: |
143 | QTreeModel(QTreeModelPrivate &, QTreeWidget *parent = nullptr); |
144 | void emitDataChanged(QTreeWidgetItem *item, int column, const QVector<int> &roles); |
145 | void beginInsertItems(QTreeWidgetItem *parent, int row, int count); |
146 | void endInsertItems(); |
147 | void beginRemoveItems(QTreeWidgetItem *parent, int row, int count); |
148 | void endRemoveItems(); |
149 | void sortItems(QList<QTreeWidgetItem*> *items, int column, Qt::SortOrder order); |
150 | void timerEvent(QTimerEvent *) override; |
151 | |
152 | private: |
153 | QTreeWidgetItem *rootItem; |
154 | QTreeWidgetItem *; |
155 | |
156 | mutable QModelIndexList cachedIndexes; |
157 | QList<QTreeWidgetItemIterator*> iterators; |
158 | |
159 | mutable QBasicTimer sortPendingTimer; |
160 | mutable bool skipPendingSort; //while doing internal operation we don't care about sorting |
161 | bool inline executePendingSort() const; |
162 | |
163 | bool isChanging() const; |
164 | |
165 | private: |
166 | Q_DECLARE_PRIVATE(QTreeModel) |
167 | public: |
168 | struct SkipSorting |
169 | { |
170 | const QTreeModel * const model; |
171 | const bool previous; |
172 | SkipSorting(const QTreeModel *m) : model(m), previous(model->skipPendingSort) |
173 | { model->skipPendingSort = true; } |
174 | ~SkipSorting() { model->skipPendingSort = previous; } |
175 | }; |
176 | friend struct SkipSorting; |
177 | }; |
178 | |
179 | QT_BEGIN_INCLUDE_NAMESPACE |
180 | #include "private/qabstractitemmodel_p.h" |
181 | QT_END_INCLUDE_NAMESPACE |
182 | |
183 | class QTreeModelPrivate : public QAbstractItemModelPrivate |
184 | { |
185 | Q_DECLARE_PUBLIC(QTreeModel) |
186 | }; |
187 | |
188 | class QTreeWidgetItemPrivate |
189 | { |
190 | public: |
191 | QTreeWidgetItemPrivate(QTreeWidgetItem *item) |
192 | : q(item), disabled(false), selected(false), hidden(false), rowGuess(-1), |
193 | policy(QTreeWidgetItem::DontShowIndicatorWhenChildless) {} |
194 | void propagateDisabled(QTreeWidgetItem *item); |
195 | void updateHiddenStatus(QTreeWidgetItem *item, bool inserting); |
196 | void sortChildren(int column, Qt::SortOrder order, bool climb); |
197 | QTreeWidgetItem *q; |
198 | QVariantList display; |
199 | uint disabled : 1; |
200 | uint selected : 1; |
201 | uint hidden : 1; |
202 | int rowGuess; |
203 | QTreeWidgetItem::ChildIndicatorPolicy policy; |
204 | }; |
205 | |
206 | |
207 | inline bool QTreeModel::executePendingSort() const |
208 | { |
209 | if (!skipPendingSort && sortPendingTimer.isActive() && !isChanging()) { |
210 | sortPendingTimer.stop(); |
211 | int column = view()->header()->sortIndicatorSection(); |
212 | Qt::SortOrder order = view()->header()->sortIndicatorOrder(); |
213 | QTreeModel *that = const_cast<QTreeModel*>(this); |
214 | that->sort(column, order); |
215 | return true; |
216 | } |
217 | return false; |
218 | } |
219 | |
220 | class QTreeWidgetPrivate : public QTreeViewPrivate |
221 | { |
222 | friend class QTreeModel; |
223 | Q_DECLARE_PUBLIC(QTreeWidget) |
224 | public: |
225 | QTreeWidgetPrivate() : QTreeViewPrivate(), explicitSortColumn(-1) {} |
226 | inline QTreeModel *treeModel() const { return qobject_cast<QTreeModel*>(object: model); } |
227 | inline QModelIndex index(const QTreeWidgetItem *item, int column = 0) const |
228 | { return treeModel()->index(item, column); } |
229 | inline QTreeWidgetItem *item(const QModelIndex &index) const |
230 | { return treeModel()->item(index); } |
231 | void _q_emitItemPressed(const QModelIndex &index); |
232 | void _q_emitItemClicked(const QModelIndex &index); |
233 | void _q_emitItemDoubleClicked(const QModelIndex &index); |
234 | void _q_emitItemActivated(const QModelIndex &index); |
235 | void _q_emitItemEntered(const QModelIndex &index); |
236 | void _q_emitItemChanged(const QModelIndex &index); |
237 | void _q_emitItemExpanded(const QModelIndex &index); |
238 | void _q_emitItemCollapsed(const QModelIndex &index); |
239 | void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &index); |
240 | void _q_sort(); |
241 | void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); |
242 | void _q_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected); |
243 | |
244 | // used by QTreeWidgetItem::sortChildren to make sure the column argument is used |
245 | int explicitSortColumn; |
246 | }; |
247 | |
248 | QT_END_NAMESPACE |
249 | |
250 | #endif // QTREEWIDGET_P_H |
251 | |