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
26QT_REQUIRE_CONFIG(treewidget);
27
28QT_BEGIN_NAMESPACE
29
30class QTreeWidgetItem;
31class QTreeWidgetItemIterator;
32class QTreeModelPrivate;
33
34class QTreeModel : public QAbstractItemModel
35{
36 Q_OBJECT
37 friend class QTreeWidget;
38 friend class QTreeWidgetPrivate;
39 friend class QTreeWidgetItem;
40 friend class QTreeWidgetItemPrivate;
41 friend class QTreeWidgetItemIterator;
42 friend class QTreeWidgetItemIteratorPrivate;
43
44public:
45 explicit QTreeModel(int columns = 0, QTreeWidget *parent = nullptr);
46 ~QTreeModel();
47
48 inline QTreeWidget *view() const
49 { return qobject_cast<QTreeWidget*>(object: QObject::parent()); }
50
51 void clear();
52 void setColumnCount(int columns);
53
54 QTreeWidgetItem *item(const QModelIndex &index) const;
55 void itemChanged(QTreeWidgetItem *item);
56
57 QModelIndex index(const QTreeWidgetItem *item, int column) const;
58 QModelIndex index(int row, int column, const QModelIndex &parent) const override;
59 QModelIndex parent(const QModelIndex &child) const override;
60 int rowCount(const QModelIndex &parent) const override;
61 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
62 bool hasChildren(const QModelIndex &parent) const override;
63
64 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
65 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
66 bool clearItemData(const QModelIndex &index) override;
67 QMap<int, QVariant> itemData(const QModelIndex &index) const override;
68
69 QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
70 bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
71 int role) override;
72
73 Qt::ItemFlags flags(const QModelIndex &index) const override;
74
75 void sort(int column, Qt::SortOrder order) override;
76 void ensureSorted(int column, Qt::SortOrder order,
77 int start, int end, const QModelIndex &parent);
78 static bool itemLessThan(const QPair<QTreeWidgetItem*,int> &left,
79 const QPair<QTreeWidgetItem*,int> &right);
80 static bool itemGreaterThan(const QPair<QTreeWidgetItem*,int> &left,
81 const QPair<QTreeWidgetItem*,int> &right);
82 static QList<QTreeWidgetItem*>::iterator sortedInsertionIterator(
83 const QList<QTreeWidgetItem*>::iterator &begin,
84 const QList<QTreeWidgetItem*>::iterator &end,
85 Qt::SortOrder order, QTreeWidgetItem *item);
86
87 bool insertRows(int row, int count, const QModelIndex &) override;
88 bool insertColumns(int column, int count, const QModelIndex &) override;
89
90 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
91
92 // dnd
93 QStringList mimeTypes() const override;
94 QMimeData *mimeData(const QModelIndexList &indexes) const override;
95 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
96 int row, int column, const QModelIndex &parent) override;
97 Qt::DropActions supportedDropActions() const override;
98
99 QMimeData *internalMimeData() const;
100
101 inline QModelIndex createIndexFromItem(int row, int col, QTreeWidgetItem *item) const
102 { return createIndex(arow: row, acolumn: col, adata: item); }
103
104protected:
105 QTreeModel(QTreeModelPrivate &, QTreeWidget *parent = nullptr);
106 void emitDataChanged(QTreeWidgetItem *item, int column, const QList<int> &roles);
107 void beginInsertItems(QTreeWidgetItem *parent, int row, int count);
108 void endInsertItems();
109 void beginRemoveItems(QTreeWidgetItem *parent, int row, int count);
110 void endRemoveItems();
111 void sortItems(QList<QTreeWidgetItem*> *items, int column, Qt::SortOrder order);
112 void timerEvent(QTimerEvent *) override;
113
114private:
115 QTreeWidgetItem *rootItem;
116 QTreeWidgetItem *headerItem;
117
118 mutable QModelIndexList cachedIndexes;
119 QList<QTreeWidgetItemIterator*> iterators;
120
121 mutable QBasicTimer sortPendingTimer;
122 mutable bool skipPendingSort = false; // no sorting during internal operations
123 bool inline executePendingSort() const;
124
125 bool isChanging() const;
126
127private:
128 Q_DECLARE_PRIVATE(QTreeModel)
129public:
130 struct SkipSorting
131 {
132 const QTreeModel * const model;
133 const bool previous;
134 SkipSorting(const QTreeModel *m) : model(m), previous(model ? model->skipPendingSort : false)
135 { if (model) model->skipPendingSort = true; }
136 ~SkipSorting() { if (model) model->skipPendingSort = previous; }
137 };
138 friend struct SkipSorting;
139};
140
141QT_BEGIN_INCLUDE_NAMESPACE
142#include "private/qabstractitemmodel_p.h"
143QT_END_INCLUDE_NAMESPACE
144
145class QTreeModelPrivate : public QAbstractItemModelPrivate
146{
147 Q_DECLARE_PUBLIC(QTreeModel)
148 void executePendingOperations() const override;
149};
150
151class QTreeWidgetItemPrivate
152{
153public:
154 QTreeWidgetItemPrivate(QTreeWidgetItem *item)
155 : q(item), disabled(false), selected(false), hidden(false), rowGuess(-1),
156 policy(QTreeWidgetItem::DontShowIndicatorWhenChildless) {}
157 void propagateDisabled(QTreeWidgetItem *item);
158 void updateHiddenStatus(QTreeWidgetItem *item, bool inserting);
159 void sortChildren(int column, Qt::SortOrder order, bool climb);
160 QTreeWidgetItem *q;
161 QVariantList display;
162 uint disabled : 1;
163 uint selected : 1;
164 uint hidden : 1;
165 int rowGuess;
166 QTreeWidgetItem::ChildIndicatorPolicy policy;
167};
168
169
170inline bool QTreeModel::executePendingSort() const
171{
172 if (!skipPendingSort && sortPendingTimer.isActive() && !isChanging()) {
173 sortPendingTimer.stop();
174 int column = view()->header()->sortIndicatorSection();
175 Qt::SortOrder order = view()->header()->sortIndicatorOrder();
176 QTreeModel *that = const_cast<QTreeModel*>(this);
177 that->sort(column, order);
178 return true;
179 }
180 return false;
181}
182
183class QTreeWidgetPrivate : public QTreeViewPrivate
184{
185 friend class QTreeModel;
186 Q_DECLARE_PUBLIC(QTreeWidget)
187public:
188 QTreeWidgetPrivate() : QTreeViewPrivate(), explicitSortColumn(-1) {}
189 inline QTreeModel *treeModel() const { return qobject_cast<QTreeModel*>(object: model); }
190 inline QModelIndex index(const QTreeWidgetItem *item, int column = 0) const
191 { return treeModel()->index(item, column); }
192 inline QTreeWidgetItem *item(const QModelIndex &index) const
193 { return treeModel()->item(index); }
194 void _q_emitItemPressed(const QModelIndex &index);
195 void _q_emitItemClicked(const QModelIndex &index);
196 void _q_emitItemDoubleClicked(const QModelIndex &index);
197 void _q_emitItemActivated(const QModelIndex &index);
198 void _q_emitItemEntered(const QModelIndex &index);
199 void _q_emitItemChanged(const QModelIndex &index);
200 void _q_emitItemExpanded(const QModelIndex &index);
201 void _q_emitItemCollapsed(const QModelIndex &index);
202 void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &index);
203 void _q_sort();
204 void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
205 void _q_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
206
207 // used by QTreeWidgetItem::sortChildren to make sure the column argument is used
208 int explicitSortColumn;
209};
210
211QT_END_NAMESPACE
212
213#endif // QTREEWIDGET_P_H
214

source code of qtbase/src/widgets/itemviews/qtreewidget_p.h