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 
46 class tst_QTreeView;
47 
48 QT_REQUIRE_CONFIG(treeview);
49 
50 QT_BEGIN_NAMESPACE
51 
52 class QTreeViewPrivate;
53 class QHeaderView;
54 
55 class 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 
70 public:
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 
147     void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>()) override;
148     void selectAll() override;
149 
150 Q_SIGNALS:
151     void expanded(const QModelIndex &index);
152     void collapsed(const QModelIndex &index);
153 
154 public 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 #if QT_DEPRECATED_SINCE(5, 13)
161     QT_DEPRECATED_X ("Use QTreeView::sortByColumn(int column, Qt::SortOrder order) instead")
162     void sortByColumn(int column);
163 #endif
164     void sortByColumn(int column, Qt::SortOrder order);
165     void expandAll();
166     void expandRecursively(const QModelIndex &index, int depth = -1);
167     void collapseAll();
168     void expandToDepth(int depth);
169 
170 protected Q_SLOTS:
171     void columnResized(int column, int oldSize, int newSize);
172     void columnCountChanged(int oldCount, int newCount);
173     void columnMoved();
174     void reexpand();
175     void rowsRemoved(const QModelIndex &parent, int first, int last);
176     void verticalScrollbarValueChanged(int value) override;
177 
178 protected:
179     QTreeView(QTreeViewPrivate &dd, QWidget *parent = nullptr);
180     void scrollContentsBy(int dx, int dy) override;
181     void rowsInserted(const QModelIndex &parent, int start, int end) override;
182     void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) override;
183 
184     QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override;
185     int horizontalOffset() const override;
186     int verticalOffset() const override;
187 
188     void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) override;
189     QRegion visualRegionForSelection(const QItemSelection &selection) const override;
190     QModelIndexList selectedIndexes() const override;
191 
192     void timerEvent(QTimerEvent *event) override;
193     void paintEvent(QPaintEvent *event) override;
194 
195     void drawTree(QPainter *painter, const QRegion &region) const;
196     virtual void drawRow(QPainter *painter,
197                          const QStyleOptionViewItem &options,
198                          const QModelIndex &index) const;
199     virtual void drawBranches(QPainter *painter,
200                               const QRect &rect,
201                               const QModelIndex &index) const;
202 
203     void mousePressEvent(QMouseEvent *event) override;
204     void mouseReleaseEvent(QMouseEvent *event) override;
205     void mouseDoubleClickEvent(QMouseEvent *event) override;
206     void mouseMoveEvent(QMouseEvent *event) override;
207     void keyPressEvent(QKeyEvent *event) override;
208 #if QT_CONFIG(draganddrop)
209     void dragMoveEvent(QDragMoveEvent *event) override;
210 #endif
211     bool viewportEvent(QEvent *event) override;
212 
213     void updateGeometries() override;
214 
215     QSize viewportSizeHint() const override;
216 
217     int sizeHintForColumn(int column) const override;
218     int indexRowSizeHint(const QModelIndex &index) const;
219     int rowHeight(const QModelIndex &index) const;
220 
221     void horizontalScrollbarAction(int action) override;
222 
223     bool isIndexHidden(const QModelIndex &index) const override;
224     void selectionChanged(const QItemSelection &selected,
225                           const QItemSelection &deselected) override;
226     void currentChanged(const QModelIndex &current, const QModelIndex &previous) override;
227 
228 private:
229     friend class ::tst_QTreeView;
230     friend class QAccessibleTable;
231     friend class QAccessibleTree;
232     friend class QAccessibleTableCell;
233     int visualIndex(const QModelIndex &index) const;
234 
235     Q_DECLARE_PRIVATE(QTreeView)
236     Q_DISABLE_COPY(QTreeView)
237 #if QT_CONFIG(animation)
238     Q_PRIVATE_SLOT(d_func(), void _q_endAnimatedOperation())
239 #endif // animation
240     Q_PRIVATE_SLOT(d_func(), void _q_modelAboutToBeReset())
241     Q_PRIVATE_SLOT(d_func(), void _q_sortIndicatorChanged(int column, Qt::SortOrder order))
242 };
243 
244 QT_END_NAMESPACE
245 
246 #endif // QTREEVIEW_H
247