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

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