1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QTREEWIDGET_P_H |
5 | #define QTREEWIDGET_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. This header file may change |
12 | // from version to version without notice, or even be removed. |
13 | // |
14 | // We mean it. |
15 | // |
16 | |
17 | #include <QtWidgets/private/qtwidgetsglobal_p.h> |
18 | #include <QtCore/qabstractitemmodel.h> |
19 | #include <private/qabstractitemmodel_p.h> |
20 | #include <QtCore/qpair.h> |
21 | #include <QtCore/qbasictimer.h> |
22 | #include <QtWidgets/qtreewidget.h> |
23 | #include <private/qtreeview_p.h> |
24 | #include <QtWidgets/qheaderview.h> |
25 | |
26 | #include <array> |
27 | |
28 | QT_REQUIRE_CONFIG(treewidget); |
29 | |
30 | QT_BEGIN_NAMESPACE |
31 | |
32 | class QTreeWidgetItem; |
33 | class QTreeWidgetItemIterator; |
34 | class QTreeModelPrivate; |
35 | |
36 | class QTreeModel : public QAbstractItemModel |
37 | { |
38 | Q_OBJECT |
39 | friend class QTreeWidget; |
40 | friend class QTreeWidgetPrivate; |
41 | friend class QTreeWidgetItem; |
42 | friend class QTreeWidgetItemPrivate; |
43 | friend class QTreeWidgetItemIterator; |
44 | friend class QTreeWidgetItemIteratorPrivate; |
45 | |
46 | public: |
47 | explicit QTreeModel(int columns = 0, QTreeWidget *parent = nullptr); |
48 | ~QTreeModel(); |
49 | |
50 | inline QTreeWidget *view() const |
51 | { return qobject_cast<QTreeWidget*>(object: QObject::parent()); } |
52 | |
53 | void clear(); |
54 | void setColumnCount(int columns); |
55 | |
56 | QTreeWidgetItem *item(const QModelIndex &index) const; |
57 | void itemChanged(QTreeWidgetItem *item); |
58 | |
59 | QModelIndex index(const QTreeWidgetItem *item, int column) const; |
60 | QModelIndex index(int row, int column, const QModelIndex &parent) const override; |
61 | QModelIndex parent(const QModelIndex &child) const override; |
62 | int rowCount(const QModelIndex &parent) const override; |
63 | int columnCount(const QModelIndex &parent = QModelIndex()) const override; |
64 | bool hasChildren(const QModelIndex &parent) const override; |
65 | |
66 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
67 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
68 | bool clearItemData(const QModelIndex &index) override; |
69 | QMap<int, QVariant> itemData(const QModelIndex &index) const override; |
70 | |
71 | QVariant (int section, Qt::Orientation orientation, int role) const override; |
72 | bool (int section, Qt::Orientation orientation, const QVariant &value, |
73 | int role) override; |
74 | |
75 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
76 | |
77 | void sort(int column, Qt::SortOrder order) override; |
78 | void ensureSorted(int column, Qt::SortOrder order, |
79 | int start, int end, const QModelIndex &parent); |
80 | static bool itemLessThan(const QPair<QTreeWidgetItem*,int> &left, |
81 | const QPair<QTreeWidgetItem*,int> &right); |
82 | static bool itemGreaterThan(const QPair<QTreeWidgetItem*,int> &left, |
83 | const QPair<QTreeWidgetItem*,int> &right); |
84 | static QList<QTreeWidgetItem*>::iterator sortedInsertionIterator( |
85 | const QList<QTreeWidgetItem*>::iterator &begin, |
86 | const QList<QTreeWidgetItem*>::iterator &end, |
87 | Qt::SortOrder order, QTreeWidgetItem *item); |
88 | |
89 | bool insertRows(int row, int count, const QModelIndex &) override; |
90 | bool insertColumns(int column, int count, const QModelIndex &) override; |
91 | |
92 | bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; |
93 | |
94 | // dnd |
95 | QStringList mimeTypes() const override; |
96 | QMimeData *mimeData(const QModelIndexList &indexes) const override; |
97 | bool dropMimeData(const QMimeData *data, Qt::DropAction action, |
98 | int row, int column, const QModelIndex &parent) override; |
99 | Qt::DropActions supportedDropActions() const override; |
100 | |
101 | QMimeData *internalMimeData() const; |
102 | |
103 | inline QModelIndex createIndexFromItem(int row, int col, QTreeWidgetItem *item) const |
104 | { return createIndex(arow: row, acolumn: col, adata: item); } |
105 | |
106 | protected: |
107 | QTreeModel(QTreeModelPrivate &, QTreeWidget *parent = nullptr); |
108 | void emitDataChanged(QTreeWidgetItem *item, int column, const QList<int> &roles); |
109 | void beginInsertItems(QTreeWidgetItem *parent, int row, int count); |
110 | void endInsertItems(); |
111 | void beginRemoveItems(QTreeWidgetItem *parent, int row, int count); |
112 | void endRemoveItems(); |
113 | void sortItems(QList<QTreeWidgetItem*> *items, int column, Qt::SortOrder order); |
114 | void timerEvent(QTimerEvent *) override; |
115 | |
116 | private: |
117 | QTreeWidgetItem *rootItem; |
118 | QTreeWidgetItem *; |
119 | |
120 | mutable QModelIndexList cachedIndexes; |
121 | QList<QTreeWidgetItemIterator*> iterators; |
122 | |
123 | mutable QBasicTimer sortPendingTimer; |
124 | mutable bool skipPendingSort = false; // no sorting during internal operations |
125 | bool inline executePendingSort() const; |
126 | |
127 | bool isChanging() const; |
128 | |
129 | private: |
130 | Q_DECLARE_PRIVATE(QTreeModel) |
131 | public: |
132 | struct SkipSorting |
133 | { |
134 | const QTreeModel * const model; |
135 | const bool previous; |
136 | SkipSorting(const QTreeModel *m) : model(m), previous(model ? model->skipPendingSort : false) |
137 | { if (model) model->skipPendingSort = true; } |
138 | ~SkipSorting() { if (model) model->skipPendingSort = previous; } |
139 | }; |
140 | friend struct SkipSorting; |
141 | }; |
142 | |
143 | QT_BEGIN_INCLUDE_NAMESPACE |
144 | #include "private/qabstractitemmodel_p.h" |
145 | QT_END_INCLUDE_NAMESPACE |
146 | |
147 | class QTreeModelPrivate : public QAbstractItemModelPrivate |
148 | { |
149 | Q_DECLARE_PUBLIC(QTreeModel) |
150 | void executePendingOperations() const override; |
151 | }; |
152 | |
153 | class QTreeWidgetItemPrivate |
154 | { |
155 | public: |
156 | QTreeWidgetItemPrivate(QTreeWidgetItem *item) |
157 | : q(item), disabled(false), selected(false), hidden(false), rowGuess(-1), |
158 | policy(QTreeWidgetItem::DontShowIndicatorWhenChildless) {} |
159 | void propagateDisabled(QTreeWidgetItem *item); |
160 | void updateHiddenStatus(QTreeWidgetItem *item, bool inserting); |
161 | void sortChildren(int column, Qt::SortOrder order, bool climb); |
162 | QTreeWidgetItem *q; |
163 | QVariantList display; |
164 | uint disabled : 1; |
165 | uint selected : 1; |
166 | uint hidden : 1; |
167 | int rowGuess; |
168 | QTreeWidgetItem::ChildIndicatorPolicy policy; |
169 | }; |
170 | |
171 | |
172 | inline bool QTreeModel::executePendingSort() const |
173 | { |
174 | if (!skipPendingSort && sortPendingTimer.isActive() && !isChanging()) { |
175 | sortPendingTimer.stop(); |
176 | int column = view()->header()->sortIndicatorSection(); |
177 | Qt::SortOrder order = view()->header()->sortIndicatorOrder(); |
178 | QTreeModel *that = const_cast<QTreeModel*>(this); |
179 | that->sort(column, order); |
180 | return true; |
181 | } |
182 | return false; |
183 | } |
184 | |
185 | class QTreeWidgetPrivate : public QTreeViewPrivate |
186 | { |
187 | friend class QTreeModel; |
188 | Q_DECLARE_PUBLIC(QTreeWidget) |
189 | public: |
190 | QTreeWidgetPrivate() : QTreeViewPrivate(), explicitSortColumn(-1) {} |
191 | void clearConnections(); |
192 | inline QTreeModel *treeModel() const { return qobject_cast<QTreeModel*>(object: model); } |
193 | inline QModelIndex index(const QTreeWidgetItem *item, int column = 0) const |
194 | { return treeModel()->index(item, column); } |
195 | inline QTreeWidgetItem *item(const QModelIndex &index) const |
196 | { return treeModel()->item(index); } |
197 | void emitItemPressed(const QModelIndex &index); |
198 | void emitItemClicked(const QModelIndex &index); |
199 | void emitItemDoubleClicked(const QModelIndex &index); |
200 | void emitItemActivated(const QModelIndex &index); |
201 | void emitItemEntered(const QModelIndex &index); |
202 | void emitItemChanged(const QModelIndex &index); |
203 | void emitItemExpanded(const QModelIndex &index); |
204 | void emitItemCollapsed(const QModelIndex &index); |
205 | void emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &index); |
206 | void sort(); |
207 | void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); |
208 | void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected); |
209 | |
210 | // used by QTreeWidgetItem::sortChildren to make sure the column argument is used |
211 | int explicitSortColumn; |
212 | |
213 | std::array<QMetaObject::Connection, 12> connections; |
214 | }; |
215 | |
216 | QT_END_NAMESPACE |
217 | |
218 | #endif // QTREEWIDGET_P_H |
219 | |