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
62QT_REQUIRE_CONFIG(treewidget);
63
64QT_BEGIN_NAMESPACE
65
66class QTreeWidgetItem;
67class QTreeWidgetItemIterator;
68class QTreeModelPrivate;
69
70class 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
80public:
81 explicit QTreeModel(int columns = 0, QTreeWidget *parent = nullptr);
82 ~QTreeModel();
83
84 inline QTreeWidget *view() const
85 { return qobject_cast<QTreeWidget*>(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 bool clearItemData(const QModelIndex &index) override;
103 QMap<int, QVariant> itemData(const QModelIndex &index) const override;
104
105 QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
106 bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
107 int role) override;
108
109 Qt::ItemFlags flags(const QModelIndex &index) const override;
110
111 void sort(int column, Qt::SortOrder order) override;
112 void ensureSorted(int column, Qt::SortOrder order,
113 int start, int end, const QModelIndex &parent);
114 static bool itemLessThan(const QPair<QTreeWidgetItem*,int> &left,
115 const QPair<QTreeWidgetItem*,int> &right);
116 static bool itemGreaterThan(const QPair<QTreeWidgetItem*,int> &left,
117 const QPair<QTreeWidgetItem*,int> &right);
118 static QList<QTreeWidgetItem*>::iterator sortedInsertionIterator(
119 const QList<QTreeWidgetItem*>::iterator &begin,
120 const QList<QTreeWidgetItem*>::iterator &end,
121 Qt::SortOrder order, QTreeWidgetItem *item);
122
123 bool insertRows(int row, int count, const QModelIndex &) override;
124 bool insertColumns(int column, int count, const QModelIndex &) override;
125
126 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
127
128 // dnd
129 QStringList mimeTypes() const override;
130 QMimeData *mimeData(const QModelIndexList &indexes) const override;
131 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
132 int row, int column, const QModelIndex &parent) override;
133 Qt::DropActions supportedDropActions() const override;
134
135 QMimeData *internalMimeData() const;
136
137 inline QModelIndex createIndexFromItem(int row, int col, QTreeWidgetItem *item) const
138 { return createIndex(row, col, item); }
139
140protected:
141 QTreeModel(QTreeModelPrivate &, QTreeWidget *parent = nullptr);
142 void emitDataChanged(QTreeWidgetItem *item, int column, const QList<int> &roles);
143 void beginInsertItems(QTreeWidgetItem *parent, int row, int count);
144 void endInsertItems();
145 void beginRemoveItems(QTreeWidgetItem *parent, int row, int count);
146 void endRemoveItems();
147 void sortItems(QList<QTreeWidgetItem*> *items, int column, Qt::SortOrder order);
148 void timerEvent(QTimerEvent *) override;
149
150private:
151 QTreeWidgetItem *rootItem;
152 QTreeWidgetItem *headerItem;
153
154 mutable QModelIndexList cachedIndexes;
155 QList<QTreeWidgetItemIterator*> iterators;
156
157 mutable QBasicTimer sortPendingTimer;
158 mutable bool skipPendingSort; //while doing internal operation we don't care about sorting
159 bool inline executePendingSort() const;
160
161 bool isChanging() const;
162
163private:
164 Q_DECLARE_PRIVATE(QTreeModel)
165public:
166 struct SkipSorting
167 {
168 const QTreeModel * const model;
169 const bool previous;
170 SkipSorting(const QTreeModel *m) : model(m), previous(model->skipPendingSort)
171 { model->skipPendingSort = true; }
172 ~SkipSorting() { model->skipPendingSort = previous; }
173 };
174 friend struct SkipSorting;
175};
176
177QT_BEGIN_INCLUDE_NAMESPACE
178#include "private/qabstractitemmodel_p.h"
179QT_END_INCLUDE_NAMESPACE
180
181class QTreeModelPrivate : public QAbstractItemModelPrivate
182{
183 Q_DECLARE_PUBLIC(QTreeModel)
184};
185
186class QTreeWidgetItemPrivate
187{
188public:
189 QTreeWidgetItemPrivate(QTreeWidgetItem *item)
190 : q(item), disabled(false), selected(false), hidden(false), rowGuess(-1),
191 policy(QTreeWidgetItem::DontShowIndicatorWhenChildless) {}
192 void propagateDisabled(QTreeWidgetItem *item);
193 void updateHiddenStatus(QTreeWidgetItem *item, bool inserting);
194 void sortChildren(int column, Qt::SortOrder order, bool climb);
195 QTreeWidgetItem *q;
196 QVariantList display;
197 uint disabled : 1;
198 uint selected : 1;
199 uint hidden : 1;
200 int rowGuess;
201 QTreeWidgetItem::ChildIndicatorPolicy policy;
202};
203
204
205inline bool QTreeModel::executePendingSort() const
206{
207 if (!skipPendingSort && sortPendingTimer.isActive() && !isChanging()) {
208 sortPendingTimer.stop();
209 int column = view()->header()->sortIndicatorSection();
210 Qt::SortOrder order = view()->header()->sortIndicatorOrder();
211 QTreeModel *that = const_cast<QTreeModel*>(this);
212 that->sort(column, order);
213 return true;
214 }
215 return false;
216}
217
218class QTreeWidgetPrivate : public QTreeViewPrivate
219{
220 friend class QTreeModel;
221 Q_DECLARE_PUBLIC(QTreeWidget)
222public:
223 QTreeWidgetPrivate() : QTreeViewPrivate(), explicitSortColumn(-1) {}
224 inline QTreeModel *treeModel() const { return qobject_cast<QTreeModel*>(model); }
225 inline QModelIndex index(const QTreeWidgetItem *item, int column = 0) const
226 { return treeModel()->index(item, column); }
227 inline QTreeWidgetItem *item(const QModelIndex &index) const
228 { return treeModel()->item(index); }
229 void _q_emitItemPressed(const QModelIndex &index);
230 void _q_emitItemClicked(const QModelIndex &index);
231 void _q_emitItemDoubleClicked(const QModelIndex &index);
232 void _q_emitItemActivated(const QModelIndex &index);
233 void _q_emitItemEntered(const QModelIndex &index);
234 void _q_emitItemChanged(const QModelIndex &index);
235 void _q_emitItemExpanded(const QModelIndex &index);
236 void _q_emitItemCollapsed(const QModelIndex &index);
237 void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &index);
238 void _q_sort();
239 void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
240 void _q_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
241
242 // used by QTreeWidgetItem::sortChildren to make sure the column argument is used
243 int explicitSortColumn;
244};
245
246QT_END_NAMESPACE
247
248#endif // QTREEWIDGET_P_H
249