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_H
41#define QTREEWIDGET_H
42
43#include <QtWidgets/qtwidgetsglobal.h>
44#include <QtWidgets/qtreeview.h>
45#include <QtWidgets/qtreewidgetitemiterator.h>
46#include <QtCore/qvariant.h>
47#include <QtCore/qvector.h>
48
49QT_REQUIRE_CONFIG(treewidget);
50
51QT_BEGIN_NAMESPACE
52
53class QTreeWidget;
54class QTreeModel;
55class QWidgetItemData;
56class QTreeWidgetItemPrivate;
57
58class Q_WIDGETS_EXPORT QTreeWidgetItem
59{
60 friend class QTreeModel;
61 friend class QTreeWidget;
62 friend class QTreeWidgetPrivate;
63 friend class QTreeWidgetItemIterator;
64 friend class QTreeWidgetItemPrivate;
65public:
66 enum ItemType { Type = 0, UserType = 1000 };
67 explicit QTreeWidgetItem(int type = Type);
68 explicit QTreeWidgetItem(const QStringList &strings, int type = Type);
69 explicit QTreeWidgetItem(QTreeWidget *treeview, int type = Type);
70 QTreeWidgetItem(QTreeWidget *treeview, const QStringList &strings, int type = Type);
71 QTreeWidgetItem(QTreeWidget *treeview, QTreeWidgetItem *after, int type = Type);
72 explicit QTreeWidgetItem(QTreeWidgetItem *parent, int type = Type);
73 QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &strings, int type = Type);
74 QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *after, int type = Type);
75 QTreeWidgetItem(const QTreeWidgetItem &other);
76 virtual ~QTreeWidgetItem();
77
78 virtual QTreeWidgetItem *clone() const;
79
80 inline QTreeWidget *treeWidget() const { return view; }
81
82 void setSelected(bool select);
83 bool isSelected() const;
84
85 void setHidden(bool hide);
86 bool isHidden() const;
87
88 void setExpanded(bool expand);
89 bool isExpanded() const;
90
91 void setFirstColumnSpanned(bool span);
92 bool isFirstColumnSpanned() const;
93
94 inline void setDisabled(bool disabled);
95 inline bool isDisabled() const;
96
97 enum ChildIndicatorPolicy { ShowIndicator, DontShowIndicator, DontShowIndicatorWhenChildless };
98 void setChildIndicatorPolicy(QTreeWidgetItem::ChildIndicatorPolicy policy);
99 QTreeWidgetItem::ChildIndicatorPolicy childIndicatorPolicy() const;
100
101 Qt::ItemFlags flags() const;
102 void setFlags(Qt::ItemFlags flags);
103
104 inline QString text(int column) const
105 { return data(column, role: Qt::DisplayRole).toString(); }
106 inline void setText(int column, const QString &text);
107
108 inline QIcon icon(int column) const
109 { return qvariant_cast<QIcon>(v: data(column, role: Qt::DecorationRole)); }
110 inline void setIcon(int column, const QIcon &icon);
111
112 inline QString statusTip(int column) const
113 { return data(column, role: Qt::StatusTipRole).toString(); }
114 inline void setStatusTip(int column, const QString &statusTip);
115
116#ifndef QT_NO_TOOLTIP
117 inline QString toolTip(int column) const
118 { return data(column, role: Qt::ToolTipRole).toString(); }
119 inline void setToolTip(int column, const QString &toolTip);
120#endif
121
122#if QT_CONFIG(whatsthis)
123 inline QString whatsThis(int column) const
124 { return data(column, role: Qt::WhatsThisRole).toString(); }
125 inline void setWhatsThis(int column, const QString &whatsThis);
126#endif
127
128 inline QFont font(int column) const
129 { return qvariant_cast<QFont>(v: data(column, role: Qt::FontRole)); }
130 inline void setFont(int column, const QFont &font);
131
132 inline int textAlignment(int column) const
133 { return data(column, role: Qt::TextAlignmentRole).toInt(); }
134 inline void setTextAlignment(int column, int alignment)
135 { setData(column, role: Qt::TextAlignmentRole, value: alignment); }
136
137#if QT_DEPRECATED_SINCE(5, 13)
138 QT_DEPRECATED_X ("Use QTreeWidgetItem::background() instead")
139 inline QColor backgroundColor(int column) const
140 { return qvariant_cast<QColor>(v: data(column, role: Qt::BackgroundRole)); }
141 QT_DEPRECATED_X ("Use QTreeWidgetItem::setBackground() instead")
142 inline void setBackgroundColor(int column, const QColor &color)
143 { setData(column, role: Qt::BackgroundRole, value: color); }
144#endif
145
146 inline QBrush background(int column) const
147 { return qvariant_cast<QBrush>(v: data(column, role: Qt::BackgroundRole)); }
148 inline void setBackground(int column, const QBrush &brush)
149 { setData(column, role: Qt::BackgroundRole, value: brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
150
151#if QT_DEPRECATED_SINCE(5, 13)
152 QT_DEPRECATED_X ("Use QTreeWidgetItem::foreground() instead")
153 inline QColor textColor(int column) const
154 { return qvariant_cast<QColor>(v: data(column, role: Qt::ForegroundRole)); }
155 QT_DEPRECATED_X ("Use QTreeWidgetItem::setForeground() instead")
156 inline void setTextColor(int column, const QColor &color)
157 { setData(column, role: Qt::ForegroundRole, value: color); }
158#endif
159
160 inline QBrush foreground(int column) const
161 { return qvariant_cast<QBrush>(v: data(column, role: Qt::ForegroundRole)); }
162 inline void setForeground(int column, const QBrush &brush)
163 { setData(column, role: Qt::ForegroundRole, value: brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
164
165 inline Qt::CheckState checkState(int column) const
166 { return static_cast<Qt::CheckState>(data(column, role: Qt::CheckStateRole).toInt()); }
167 inline void setCheckState(int column, Qt::CheckState state)
168 { setData(column, role: Qt::CheckStateRole, value: state); }
169
170 inline QSize sizeHint(int column) const
171 { return qvariant_cast<QSize>(v: data(column, role: Qt::SizeHintRole)); }
172 inline void setSizeHint(int column, const QSize &size)
173 { setData(column, role: Qt::SizeHintRole, value: size.isValid() ? QVariant(size) : QVariant()); }
174
175 virtual QVariant data(int column, int role) const;
176 virtual void setData(int column, int role, const QVariant &value);
177
178 virtual bool operator<(const QTreeWidgetItem &other) const;
179
180#ifndef QT_NO_DATASTREAM
181 virtual void read(QDataStream &in);
182 virtual void write(QDataStream &out) const;
183#endif
184 QTreeWidgetItem &operator=(const QTreeWidgetItem &other);
185
186 inline QTreeWidgetItem *parent() const { return par; }
187 inline QTreeWidgetItem *child(int index) const {
188 if (index < 0 || index >= children.size())
189 return nullptr;
190 executePendingSort();
191 return children.at(i: index);
192 }
193 inline int childCount() const { return children.count(); }
194 inline int columnCount() const { return values.count(); }
195 inline int indexOfChild(QTreeWidgetItem *child) const;
196
197 void addChild(QTreeWidgetItem *child);
198 void insertChild(int index, QTreeWidgetItem *child);
199 void removeChild(QTreeWidgetItem *child);
200 QTreeWidgetItem *takeChild(int index);
201
202 void addChildren(const QList<QTreeWidgetItem*> &children);
203 void insertChildren(int index, const QList<QTreeWidgetItem*> &children);
204 QList<QTreeWidgetItem*> takeChildren();
205
206 inline int type() const { return rtti; }
207 inline void sortChildren(int column, Qt::SortOrder order)
208 { sortChildren(column, order, climb: false); }
209
210protected:
211 void emitDataChanged();
212
213private:
214 void sortChildren(int column, Qt::SortOrder order, bool climb);
215 QVariant childrenCheckState(int column) const;
216 void itemChanged();
217 void executePendingSort() const;
218 QTreeModel *treeModel(QTreeWidget *v = nullptr) const;
219
220 int rtti;
221 // One item has a vector of column entries. Each column has a vector of (role, value) pairs.
222 QVector< QVector<QWidgetItemData> > values;
223 QTreeWidget *view;
224 QTreeWidgetItemPrivate *d;
225 QTreeWidgetItem *par;
226 QList<QTreeWidgetItem*> children;
227 Qt::ItemFlags itemFlags;
228};
229
230inline void QTreeWidgetItem::setText(int column, const QString &atext)
231{ setData(column, role: Qt::DisplayRole, value: atext); }
232
233inline void QTreeWidgetItem::setIcon(int column, const QIcon &aicon)
234{ setData(column, role: Qt::DecorationRole, value: aicon); }
235
236#if QT_CONFIG(statustip)
237inline void QTreeWidgetItem::setStatusTip(int column, const QString &astatusTip)
238{ setData(column, role: Qt::StatusTipRole, value: astatusTip); }
239#endif
240
241#ifndef QT_NO_TOOLTIP
242inline void QTreeWidgetItem::setToolTip(int column, const QString &atoolTip)
243{ setData(column, role: Qt::ToolTipRole, value: atoolTip); }
244#endif
245
246#if QT_CONFIG(whatsthis)
247inline void QTreeWidgetItem::setWhatsThis(int column, const QString &awhatsThis)
248{ setData(column, role: Qt::WhatsThisRole, value: awhatsThis); }
249#endif
250
251inline void QTreeWidgetItem::setFont(int column, const QFont &afont)
252{ setData(column, role: Qt::FontRole, value: afont); }
253
254inline int QTreeWidgetItem::indexOfChild(QTreeWidgetItem *achild) const
255{ executePendingSort(); return children.indexOf(t: achild); }
256
257#ifndef QT_NO_DATASTREAM
258Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &out, const QTreeWidgetItem &item);
259Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &in, QTreeWidgetItem &item);
260#endif
261
262class QTreeWidgetPrivate;
263
264class Q_WIDGETS_EXPORT QTreeWidget : public QTreeView
265{
266 Q_OBJECT
267 Q_PROPERTY(int columnCount READ columnCount WRITE setColumnCount)
268 Q_PROPERTY(int topLevelItemCount READ topLevelItemCount)
269
270 friend class QTreeModel;
271 friend class QTreeWidgetItem;
272public:
273 explicit QTreeWidget(QWidget *parent = nullptr);
274 ~QTreeWidget();
275
276 int columnCount() const;
277 void setColumnCount(int columns);
278
279 QTreeWidgetItem *invisibleRootItem() const;
280 QTreeWidgetItem *topLevelItem(int index) const;
281 int topLevelItemCount() const;
282 void insertTopLevelItem(int index, QTreeWidgetItem *item);
283 void addTopLevelItem(QTreeWidgetItem *item);
284 QTreeWidgetItem *takeTopLevelItem(int index);
285 int indexOfTopLevelItem(QTreeWidgetItem *item) const;
286
287 void insertTopLevelItems(int index, const QList<QTreeWidgetItem*> &items);
288 void addTopLevelItems(const QList<QTreeWidgetItem*> &items);
289
290 QTreeWidgetItem *headerItem() const;
291 void setHeaderItem(QTreeWidgetItem *item);
292 void setHeaderLabels(const QStringList &labels);
293 inline void setHeaderLabel(const QString &label);
294
295 QTreeWidgetItem *currentItem() const;
296 int currentColumn() const;
297 void setCurrentItem(QTreeWidgetItem *item);
298 void setCurrentItem(QTreeWidgetItem *item, int column);
299 void setCurrentItem(QTreeWidgetItem *item, int column, QItemSelectionModel::SelectionFlags command);
300
301 QTreeWidgetItem *itemAt(const QPoint &p) const;
302 inline QTreeWidgetItem *itemAt(int x, int y) const;
303 QRect visualItemRect(const QTreeWidgetItem *item) const;
304
305 int sortColumn() const;
306 void sortItems(int column, Qt::SortOrder order);
307
308 void editItem(QTreeWidgetItem *item, int column = 0);
309 void openPersistentEditor(QTreeWidgetItem *item, int column = 0);
310 void closePersistentEditor(QTreeWidgetItem *item, int column = 0);
311 using QAbstractItemView::isPersistentEditorOpen;
312 bool isPersistentEditorOpen(QTreeWidgetItem *item, int column = 0) const;
313
314 QWidget *itemWidget(QTreeWidgetItem *item, int column) const;
315 void setItemWidget(QTreeWidgetItem *item, int column, QWidget *widget);
316 inline void removeItemWidget(QTreeWidgetItem *item, int column);
317
318#if QT_DEPRECATED_SINCE(5, 13)
319 QT_DEPRECATED_X ("Use QTreeWidgetItem::isSelected() instead")
320 bool isItemSelected(const QTreeWidgetItem *item) const;
321 QT_DEPRECATED_X ("Use QTreeWidgetItem::setSelected() instead")
322 void setItemSelected(const QTreeWidgetItem *item, bool select);
323#endif
324 QList<QTreeWidgetItem*> selectedItems() const;
325 QList<QTreeWidgetItem*> findItems(const QString &text, Qt::MatchFlags flags,
326 int column = 0) const;
327
328#if QT_DEPRECATED_SINCE(5, 13)
329 QT_DEPRECATED_X ("Use QTreeWidgetItem::isHidden() instead")
330 bool isItemHidden(const QTreeWidgetItem *item) const;
331 QT_DEPRECATED_X ("Use QTreeWidgetItem::setHidden() instead")
332 void setItemHidden(const QTreeWidgetItem *item, bool hide);
333
334 QT_DEPRECATED_X ("Use QTreeWidgetItem::isExpanded() instead")
335 bool isItemExpanded(const QTreeWidgetItem *item) const;
336 QT_DEPRECATED_X ("Use QTreeWidgetItem::setExpanded() instead")
337 void setItemExpanded(const QTreeWidgetItem *item, bool expand);
338
339 QT_DEPRECATED_X ("Use QTreeWidgetItem::isFirstColumnSpanned() instead")
340 bool isFirstItemColumnSpanned(const QTreeWidgetItem *item) const;
341 QT_DEPRECATED_X ("Use QTreeWidgetItem::setFirstColumnSpanned() instead")
342 void setFirstItemColumnSpanned(const QTreeWidgetItem *item, bool span);
343#endif
344
345 QTreeWidgetItem *itemAbove(const QTreeWidgetItem *item) const;
346 QTreeWidgetItem *itemBelow(const QTreeWidgetItem *item) const;
347
348 void setSelectionModel(QItemSelectionModel *selectionModel) override;
349
350public Q_SLOTS:
351 void scrollToItem(const QTreeWidgetItem *item,
352 QAbstractItemView::ScrollHint hint = EnsureVisible);
353 void expandItem(const QTreeWidgetItem *item);
354 void collapseItem(const QTreeWidgetItem *item);
355 void clear();
356
357Q_SIGNALS:
358 void itemPressed(QTreeWidgetItem *item, int column);
359 void itemClicked(QTreeWidgetItem *item, int column);
360 void itemDoubleClicked(QTreeWidgetItem *item, int column);
361 void itemActivated(QTreeWidgetItem *item, int column);
362 void itemEntered(QTreeWidgetItem *item, int column);
363 // ### Qt 6: add changed roles
364 void itemChanged(QTreeWidgetItem *item, int column);
365 void itemExpanded(QTreeWidgetItem *item);
366 void itemCollapsed(QTreeWidgetItem *item);
367 void currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
368 void itemSelectionChanged();
369
370protected:
371 bool event(QEvent *e) override;
372 virtual QStringList mimeTypes() const;
373#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
374 virtual QMimeData *mimeData(const QList<QTreeWidgetItem *> &items) const;
375#else
376 virtual QMimeData *mimeData(const QList<QTreeWidgetItem*> items) const;
377#endif
378 virtual bool dropMimeData(QTreeWidgetItem *parent, int index,
379 const QMimeData *data, Qt::DropAction action);
380 virtual Qt::DropActions supportedDropActions() const;
381
382#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
383public:
384#else
385protected:
386#endif
387 QList<QTreeWidgetItem*> items(const QMimeData *data) const;
388
389 QModelIndex indexFromItem(const QTreeWidgetItem *item, int column = 0) const;
390#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
391 QModelIndex indexFromItem(QTreeWidgetItem *item, int column = 0) const; // ### Qt 6: remove
392#endif
393 QTreeWidgetItem *itemFromIndex(const QModelIndex &index) const;
394
395protected:
396#if QT_CONFIG(draganddrop)
397 void dropEvent(QDropEvent *event) override;
398#endif
399private:
400 void setModel(QAbstractItemModel *model) override;
401
402 Q_DECLARE_PRIVATE(QTreeWidget)
403 Q_DISABLE_COPY(QTreeWidget)
404
405 Q_PRIVATE_SLOT(d_func(), void _q_emitItemPressed(const QModelIndex &index))
406 Q_PRIVATE_SLOT(d_func(), void _q_emitItemClicked(const QModelIndex &index))
407 Q_PRIVATE_SLOT(d_func(), void _q_emitItemDoubleClicked(const QModelIndex &index))
408 Q_PRIVATE_SLOT(d_func(), void _q_emitItemActivated(const QModelIndex &index))
409 Q_PRIVATE_SLOT(d_func(), void _q_emitItemEntered(const QModelIndex &index))
410 Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &index))
411 Q_PRIVATE_SLOT(d_func(), void _q_emitItemExpanded(const QModelIndex &index))
412 Q_PRIVATE_SLOT(d_func(), void _q_emitItemCollapsed(const QModelIndex &index))
413 Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current))
414 Q_PRIVATE_SLOT(d_func(), void _q_sort())
415 Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight))
416 Q_PRIVATE_SLOT(d_func(), void _q_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected))
417};
418
419inline void QTreeWidget::removeItemWidget(QTreeWidgetItem *item, int column)
420{ setItemWidget(item, column, widget: nullptr); }
421
422inline QTreeWidgetItem *QTreeWidget::itemAt(int ax, int ay) const
423{ return itemAt(p: QPoint(ax, ay)); }
424
425inline void QTreeWidget::setHeaderLabel(const QString &alabel)
426{ setHeaderLabels(QStringList(alabel)); }
427
428inline void QTreeWidgetItem::setDisabled(bool disabled)
429{ setFlags(disabled ? (flags() & ~Qt::ItemIsEnabled) : flags() | Qt::ItemIsEnabled); }
430
431inline bool QTreeWidgetItem::isDisabled() const
432{ return !(flags() & Qt::ItemIsEnabled); }
433
434QT_END_NAMESPACE
435
436#endif // QTREEWIDGET_H
437

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