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/qtreeview.h> |
44 | #include <QtWidgets/qtreewidgetitemiterator.h> |
45 | #include <QtWidgets/qtwidgetsglobal.h> |
46 | #include <QtCore/qlist.h> |
47 | #include <QtCore/qvariant.h> |
48 | |
49 | QT_REQUIRE_CONFIG(treewidget); |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | class QTreeWidget; |
54 | class QTreeModel; |
55 | class QWidgetItemData; |
56 | class QTreeWidgetItemPrivate; |
57 | |
58 | class 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; |
65 | public: |
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, 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>(data(column, Qt::DecorationRole)); } |
110 | inline void setIcon(int column, const QIcon &icon); |
111 | |
112 | inline QString statusTip(int column) const |
113 | { return data(column, Qt::StatusTipRole).toString(); } |
114 | inline void setStatusTip(int column, const QString &statusTip); |
115 | |
116 | #if QT_CONFIG(tooltip) |
117 | inline QString toolTip(int column) const |
118 | { return data(column, 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, 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>(data(column, Qt::FontRole)); } |
130 | inline void setFont(int column, const QFont &font); |
131 | |
132 | inline int textAlignment(int column) const |
133 | { return data(column, Qt::TextAlignmentRole).toInt(); } |
134 | inline void setTextAlignment(int column, int alignment) |
135 | { setData(column, Qt::TextAlignmentRole, alignment); } |
136 | |
137 | inline QBrush background(int column) const |
138 | { return qvariant_cast<QBrush>(data(column, Qt::BackgroundRole)); } |
139 | inline void setBackground(int column, const QBrush &brush) |
140 | { setData(column, Qt::BackgroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); } |
141 | |
142 | inline QBrush foreground(int column) const |
143 | { return qvariant_cast<QBrush>(data(column, Qt::ForegroundRole)); } |
144 | inline void setForeground(int column, const QBrush &brush) |
145 | { setData(column, Qt::ForegroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); } |
146 | |
147 | inline Qt::CheckState checkState(int column) const |
148 | { return static_cast<Qt::CheckState>(data(column, Qt::CheckStateRole).toInt()); } |
149 | inline void setCheckState(int column, Qt::CheckState state) |
150 | { setData(column, Qt::CheckStateRole, state); } |
151 | |
152 | inline QSize sizeHint(int column) const |
153 | { return qvariant_cast<QSize>(data(column, Qt::SizeHintRole)); } |
154 | inline void setSizeHint(int column, const QSize &size) |
155 | { setData(column, Qt::SizeHintRole, size.isValid() ? QVariant(size) : QVariant()); } |
156 | |
157 | virtual QVariant data(int column, int role) const; |
158 | virtual void setData(int column, int role, const QVariant &value); |
159 | |
160 | virtual bool operator<(const QTreeWidgetItem &other) const; |
161 | |
162 | #ifndef QT_NO_DATASTREAM |
163 | virtual void read(QDataStream &in); |
164 | virtual void write(QDataStream &out) const; |
165 | #endif |
166 | QTreeWidgetItem &operator=(const QTreeWidgetItem &other); |
167 | |
168 | inline QTreeWidgetItem *parent() const { return par; } |
169 | inline QTreeWidgetItem *child(int index) const { |
170 | if (index < 0 || index >= children.size()) |
171 | return nullptr; |
172 | executePendingSort(); |
173 | return children.at(index); |
174 | } |
175 | inline int childCount() const { return children.count(); } |
176 | inline int columnCount() const { return values.count(); } |
177 | inline int indexOfChild(QTreeWidgetItem *child) const; |
178 | |
179 | void addChild(QTreeWidgetItem *child); |
180 | void insertChild(int index, QTreeWidgetItem *child); |
181 | void removeChild(QTreeWidgetItem *child); |
182 | QTreeWidgetItem *takeChild(int index); |
183 | |
184 | void addChildren(const QList<QTreeWidgetItem*> &children); |
185 | void insertChildren(int index, const QList<QTreeWidgetItem*> &children); |
186 | QList<QTreeWidgetItem*> takeChildren(); |
187 | |
188 | inline int type() const { return rtti; } |
189 | inline void sortChildren(int column, Qt::SortOrder order) |
190 | { sortChildren(column, order, false); } |
191 | |
192 | protected: |
193 | void emitDataChanged(); |
194 | |
195 | private: |
196 | void sortChildren(int column, Qt::SortOrder order, bool climb); |
197 | QVariant childrenCheckState(int column) const; |
198 | void itemChanged(); |
199 | void executePendingSort() const; |
200 | QTreeModel *treeModel(QTreeWidget *v = nullptr) const; |
201 | |
202 | int rtti; |
203 | // One item has a vector of column entries. Each column has a vector of (role, value) pairs. |
204 | QList<QList<QWidgetItemData>> values; |
205 | QTreeWidget *view; |
206 | QTreeWidgetItemPrivate *d; |
207 | QTreeWidgetItem *par; |
208 | QList<QTreeWidgetItem*> children; |
209 | Qt::ItemFlags itemFlags; |
210 | }; |
211 | |
212 | inline void QTreeWidgetItem::setText(int column, const QString &atext) |
213 | { setData(column, Qt::DisplayRole, atext); } |
214 | |
215 | inline void QTreeWidgetItem::setIcon(int column, const QIcon &aicon) |
216 | { setData(column, Qt::DecorationRole, aicon); } |
217 | |
218 | #if QT_CONFIG(statustip) |
219 | inline void QTreeWidgetItem::setStatusTip(int column, const QString &astatusTip) |
220 | { setData(column, Qt::StatusTipRole, astatusTip); } |
221 | #endif |
222 | |
223 | #if QT_CONFIG(tooltip) |
224 | inline void QTreeWidgetItem::setToolTip(int column, const QString &atoolTip) |
225 | { setData(column, Qt::ToolTipRole, atoolTip); } |
226 | #endif |
227 | |
228 | #if QT_CONFIG(whatsthis) |
229 | inline void QTreeWidgetItem::setWhatsThis(int column, const QString &awhatsThis) |
230 | { setData(column, Qt::WhatsThisRole, awhatsThis); } |
231 | #endif |
232 | |
233 | inline void QTreeWidgetItem::setFont(int column, const QFont &afont) |
234 | { setData(column, Qt::FontRole, afont); } |
235 | |
236 | inline int QTreeWidgetItem::indexOfChild(QTreeWidgetItem *achild) const |
237 | { executePendingSort(); return children.indexOf(achild); } |
238 | |
239 | #ifndef QT_NO_DATASTREAM |
240 | Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &out, const QTreeWidgetItem &item); |
241 | Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &in, QTreeWidgetItem &item); |
242 | #endif |
243 | |
244 | class QTreeWidgetPrivate; |
245 | |
246 | class Q_WIDGETS_EXPORT QTreeWidget : public QTreeView |
247 | { |
248 | Q_OBJECT |
249 | Q_PROPERTY(int columnCount READ columnCount WRITE setColumnCount) |
250 | Q_PROPERTY(int topLevelItemCount READ topLevelItemCount) |
251 | |
252 | friend class QTreeModel; |
253 | friend class QTreeWidgetItem; |
254 | public: |
255 | explicit QTreeWidget(QWidget *parent = nullptr); |
256 | ~QTreeWidget(); |
257 | |
258 | int columnCount() const; |
259 | void setColumnCount(int columns); |
260 | |
261 | QTreeWidgetItem *invisibleRootItem() const; |
262 | QTreeWidgetItem *topLevelItem(int index) const; |
263 | int topLevelItemCount() const; |
264 | void insertTopLevelItem(int index, QTreeWidgetItem *item); |
265 | void addTopLevelItem(QTreeWidgetItem *item); |
266 | QTreeWidgetItem *takeTopLevelItem(int index); |
267 | int indexOfTopLevelItem(QTreeWidgetItem *item) const; |
268 | |
269 | void insertTopLevelItems(int index, const QList<QTreeWidgetItem*> &items); |
270 | void addTopLevelItems(const QList<QTreeWidgetItem*> &items); |
271 | |
272 | QTreeWidgetItem *() const; |
273 | void (QTreeWidgetItem *item); |
274 | void (const QStringList &labels); |
275 | inline void setHeaderLabel(const QString &label); |
276 | |
277 | QTreeWidgetItem *currentItem() const; |
278 | int currentColumn() const; |
279 | void setCurrentItem(QTreeWidgetItem *item); |
280 | void setCurrentItem(QTreeWidgetItem *item, int column); |
281 | void setCurrentItem(QTreeWidgetItem *item, int column, QItemSelectionModel::SelectionFlags command); |
282 | |
283 | QTreeWidgetItem *itemAt(const QPoint &p) const; |
284 | inline QTreeWidgetItem *itemAt(int x, int y) const; |
285 | QRect visualItemRect(const QTreeWidgetItem *item) const; |
286 | |
287 | int sortColumn() const; |
288 | void sortItems(int column, Qt::SortOrder order); |
289 | |
290 | void editItem(QTreeWidgetItem *item, int column = 0); |
291 | void openPersistentEditor(QTreeWidgetItem *item, int column = 0); |
292 | void closePersistentEditor(QTreeWidgetItem *item, int column = 0); |
293 | using QAbstractItemView::isPersistentEditorOpen; |
294 | bool isPersistentEditorOpen(QTreeWidgetItem *item, int column = 0) const; |
295 | |
296 | QWidget *itemWidget(QTreeWidgetItem *item, int column) const; |
297 | void setItemWidget(QTreeWidgetItem *item, int column, QWidget *widget); |
298 | inline void removeItemWidget(QTreeWidgetItem *item, int column); |
299 | |
300 | QList<QTreeWidgetItem*> selectedItems() const; |
301 | QList<QTreeWidgetItem*> findItems(const QString &text, Qt::MatchFlags flags, |
302 | int column = 0) const; |
303 | |
304 | QTreeWidgetItem *itemAbove(const QTreeWidgetItem *item) const; |
305 | QTreeWidgetItem *itemBelow(const QTreeWidgetItem *item) const; |
306 | |
307 | QModelIndex indexFromItem(const QTreeWidgetItem *item, int column = 0) const; |
308 | QTreeWidgetItem *itemFromIndex(const QModelIndex &index) const; |
309 | |
310 | void setSelectionModel(QItemSelectionModel *selectionModel) override; |
311 | |
312 | public Q_SLOTS: |
313 | void scrollToItem(const QTreeWidgetItem *item, |
314 | QAbstractItemView::ScrollHint hint = EnsureVisible); |
315 | void expandItem(const QTreeWidgetItem *item); |
316 | void collapseItem(const QTreeWidgetItem *item); |
317 | void clear(); |
318 | |
319 | Q_SIGNALS: |
320 | void itemPressed(QTreeWidgetItem *item, int column); |
321 | void itemClicked(QTreeWidgetItem *item, int column); |
322 | void itemDoubleClicked(QTreeWidgetItem *item, int column); |
323 | void itemActivated(QTreeWidgetItem *item, int column); |
324 | void itemEntered(QTreeWidgetItem *item, int column); |
325 | void itemChanged(QTreeWidgetItem *item, int column); |
326 | void itemExpanded(QTreeWidgetItem *item); |
327 | void itemCollapsed(QTreeWidgetItem *item); |
328 | void currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous); |
329 | void itemSelectionChanged(); |
330 | |
331 | protected: |
332 | bool event(QEvent *e) override; |
333 | virtual QStringList mimeTypes() const; |
334 | virtual QMimeData *mimeData(const QList<QTreeWidgetItem *> &items) const; |
335 | virtual bool dropMimeData(QTreeWidgetItem *parent, int index, |
336 | const QMimeData *data, Qt::DropAction action); |
337 | virtual Qt::DropActions supportedDropActions() const; |
338 | |
339 | #if QT_CONFIG(draganddrop) |
340 | void dropEvent(QDropEvent *event) override; |
341 | #endif |
342 | private: |
343 | void setModel(QAbstractItemModel *model) override; |
344 | |
345 | Q_DECLARE_PRIVATE(QTreeWidget) |
346 | Q_DISABLE_COPY(QTreeWidget) |
347 | |
348 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemPressed(const QModelIndex &index)) |
349 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemClicked(const QModelIndex &index)) |
350 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemDoubleClicked(const QModelIndex &index)) |
351 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemActivated(const QModelIndex &index)) |
352 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemEntered(const QModelIndex &index)) |
353 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &index)) |
354 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemExpanded(const QModelIndex &index)) |
355 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemCollapsed(const QModelIndex &index)) |
356 | Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex ¤t)) |
357 | Q_PRIVATE_SLOT(d_func(), void _q_sort()) |
358 | Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)) |
359 | Q_PRIVATE_SLOT(d_func(), void _q_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)) |
360 | }; |
361 | |
362 | inline void QTreeWidget::removeItemWidget(QTreeWidgetItem *item, int column) |
363 | { setItemWidget(item, column, nullptr); } |
364 | |
365 | inline QTreeWidgetItem *QTreeWidget::itemAt(int ax, int ay) const |
366 | { return itemAt(QPoint(ax, ay)); } |
367 | |
368 | inline void QTreeWidget::(const QString &alabel) |
369 | { setHeaderLabels(QStringList(alabel)); } |
370 | |
371 | inline void QTreeWidgetItem::setDisabled(bool disabled) |
372 | { setFlags(disabled ? (flags() & ~Qt::ItemIsEnabled) : flags() | Qt::ItemIsEnabled); } |
373 | |
374 | inline bool QTreeWidgetItem::isDisabled() const |
375 | { return !(flags() & Qt::ItemIsEnabled); } |
376 | |
377 | QT_END_NAMESPACE |
378 | |
379 | #endif // QTREEWIDGET_H |
380 | |