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 QTREEVIEW_H
41#define QTREEVIEW_H
42
43#include <QtWidgets/qtwidgetsglobal.h>
44#include <QtWidgets/qabstractitemview.h>
45
46class tst_QTreeView;
47
48QT_REQUIRE_CONFIG(treeview);
49
50QT_BEGIN_NAMESPACE
51
52class QTreeViewPrivate;
53class QHeaderView;
54
55class Q_WIDGETS_EXPORT QTreeView : public QAbstractItemView
56{
57 Q_OBJECT
58 Q_PROPERTY(int autoExpandDelay READ autoExpandDelay WRITE setAutoExpandDelay)
59 Q_PROPERTY(int indentation READ indentation WRITE setIndentation RESET resetIndentation)
60 Q_PROPERTY(bool rootIsDecorated READ rootIsDecorated WRITE setRootIsDecorated)
61 Q_PROPERTY(bool uniformRowHeights READ uniformRowHeights WRITE setUniformRowHeights)
62 Q_PROPERTY(bool itemsExpandable READ itemsExpandable WRITE setItemsExpandable)
63 Q_PROPERTY(bool sortingEnabled READ isSortingEnabled WRITE setSortingEnabled)
64 Q_PROPERTY(bool animated READ isAnimated WRITE setAnimated)
65 Q_PROPERTY(bool allColumnsShowFocus READ allColumnsShowFocus WRITE setAllColumnsShowFocus)
66 Q_PROPERTY(bool wordWrap READ wordWrap WRITE setWordWrap)
67 Q_PROPERTY(bool headerHidden READ isHeaderHidden WRITE setHeaderHidden)
68 Q_PROPERTY(bool expandsOnDoubleClick READ expandsOnDoubleClick WRITE setExpandsOnDoubleClick)
69
70public:
71 explicit QTreeView(QWidget *parent = nullptr);
72 ~QTreeView();
73
74 void setModel(QAbstractItemModel *model) override;
75 void setRootIndex(const QModelIndex &index) override;
76 void setSelectionModel(QItemSelectionModel *selectionModel) override;
77
78 QHeaderView *header() const;
79 void setHeader(QHeaderView *header);
80
81 int autoExpandDelay() const;
82 void setAutoExpandDelay(int delay);
83
84 int indentation() const;
85 void setIndentation(int i);
86 void resetIndentation();
87
88 bool rootIsDecorated() const;
89 void setRootIsDecorated(bool show);
90
91 bool uniformRowHeights() const;
92 void setUniformRowHeights(bool uniform);
93
94 bool itemsExpandable() const;
95 void setItemsExpandable(bool enable);
96
97 bool expandsOnDoubleClick() const;
98 void setExpandsOnDoubleClick(bool enable);
99
100 int columnViewportPosition(int column) const;
101 int columnWidth(int column) const;
102 void setColumnWidth(int column, int width);
103 int columnAt(int x) const;
104
105 bool isColumnHidden(int column) const;
106 void setColumnHidden(int column, bool hide);
107
108 bool isHeaderHidden() const;
109 void setHeaderHidden(bool hide);
110
111 bool isRowHidden(int row, const QModelIndex &parent) const;
112 void setRowHidden(int row, const QModelIndex &parent, bool hide);
113
114 bool isFirstColumnSpanned(int row, const QModelIndex &parent) const;
115 void setFirstColumnSpanned(int row, const QModelIndex &parent, bool span);
116
117 bool isExpanded(const QModelIndex &index) const;
118 void setExpanded(const QModelIndex &index, bool expand);
119
120 void setSortingEnabled(bool enable);
121 bool isSortingEnabled() const;
122
123 void setAnimated(bool enable);
124 bool isAnimated() const;
125
126 void setAllColumnsShowFocus(bool enable);
127 bool allColumnsShowFocus() const;
128
129 void setWordWrap(bool on);
130 bool wordWrap() const;
131
132 void setTreePosition(int logicalIndex);
133 int treePosition() const;
134
135 void keyboardSearch(const QString &search) override;
136
137 QRect visualRect(const QModelIndex &index) const override;
138 void scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible) override;
139 QModelIndex indexAt(const QPoint &p) const override;
140 QModelIndex indexAbove(const QModelIndex &index) const;
141 QModelIndex indexBelow(const QModelIndex &index) const;
142
143 void doItemsLayout() override;
144 void reset() override;
145
146 void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
147 const QList<int> &roles = QList<int>()) override;
148 void selectAll() override;
149
150Q_SIGNALS:
151 void expanded(const QModelIndex &index);
152 void collapsed(const QModelIndex &index);
153
154public Q_SLOTS:
155 void hideColumn(int column);
156 void showColumn(int column);
157 void expand(const QModelIndex &index);
158 void collapse(const QModelIndex &index);
159 void resizeColumnToContents(int column);
160 void sortByColumn(int column, Qt::SortOrder order);
161 void expandAll();
162 void expandRecursively(const QModelIndex &index, int depth = -1);
163 void collapseAll();
164 void expandToDepth(int depth);
165
166protected Q_SLOTS:
167 void columnResized(int column, int oldSize, int newSize);
168 void columnCountChanged(int oldCount, int newCount);
169 void columnMoved();
170 void reexpand();
171 void rowsRemoved(const QModelIndex &parent, int first, int last);
172 void verticalScrollbarValueChanged(int value) override;
173
174protected:
175 QTreeView(QTreeViewPrivate &dd, QWidget *parent = nullptr);
176 void scrollContentsBy(int dx, int dy) override;
177 void rowsInserted(const QModelIndex &parent, int start, int end) override;
178 void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) override;
179
180 QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override;
181 int horizontalOffset() const override;
182 int verticalOffset() const override;
183
184 void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) override;
185 QRegion visualRegionForSelection(const QItemSelection &selection) const override;
186 QModelIndexList selectedIndexes() const override;
187
188 void changeEvent(QEvent *event) override;
189 void timerEvent(QTimerEvent *event) override;
190 void paintEvent(QPaintEvent *event) override;
191
192 void drawTree(QPainter *painter, const QRegion &region) const;
193 virtual void drawRow(QPainter *painter,
194 const QStyleOptionViewItem &options,
195 const QModelIndex &index) const;
196 virtual void drawBranches(QPainter *painter,
197 const QRect &rect,
198 const QModelIndex &index) const;
199
200 void mousePressEvent(QMouseEvent *event) override;
201 void mouseReleaseEvent(QMouseEvent *event) override;
202 void mouseDoubleClickEvent(QMouseEvent *event) override;
203 void mouseMoveEvent(QMouseEvent *event) override;
204 void keyPressEvent(QKeyEvent *event) override;
205#if QT_CONFIG(draganddrop)
206 void dragMoveEvent(QDragMoveEvent *event) override;
207#endif
208 bool viewportEvent(QEvent *event) override;
209
210 void updateGeometries() override;
211
212 QSize viewportSizeHint() const override;
213
214 int sizeHintForColumn(int column) const override;
215 int indexRowSizeHint(const QModelIndex &index) const;
216 int rowHeight(const QModelIndex &index) const;
217
218 void horizontalScrollbarAction(int action) override;
219
220 bool isIndexHidden(const QModelIndex &index) const override;
221 void selectionChanged(const QItemSelection &selected,
222 const QItemSelection &deselected) override;
223 void currentChanged(const QModelIndex &current, const QModelIndex &previous) override;
224
225private:
226 friend class ::tst_QTreeView;
227 friend class QAccessibleTable;
228 friend class QAccessibleTree;
229 friend class QAccessibleTableCell;
230 int visualIndex(const QModelIndex &index) const;
231
232 Q_DECLARE_PRIVATE(QTreeView)
233 Q_DISABLE_COPY(QTreeView)
234#if QT_CONFIG(animation)
235 Q_PRIVATE_SLOT(d_func(), void _q_endAnimatedOperation())
236#endif // animation
237 Q_PRIVATE_SLOT(d_func(), void _q_modelAboutToBeReset())
238 Q_PRIVATE_SLOT(d_func(), void _q_sortIndicatorChanged(int column, Qt::SortOrder order))
239};
240
241QT_END_NAMESPACE
242
243#endif // QTREEVIEW_H
244