Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtreeview_p.h
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
5#ifndef QTREEVIEW_P_H
6#define QTREEVIEW_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtWidgets/private/qtwidgetsglobal_p.h>
20#include "qtreeview.h"
21#include "private/qabstractitemview_p.h"
22#include <QtCore/qabstractitemmodel.h>
23#include <QtCore/qbasictimer.h>
24#include <QtCore/qlist.h>
25#if QT_CONFIG(animation)
26#include <QtCore/qvariantanimation.h>
27#endif
28
29#include <array>
30
32
33QT_BEGIN_NAMESPACE
34
35struct QTreeViewItem
36{
37 QTreeViewItem() : parentItem(-1), expanded(false), spanning(false), hasChildren(false),
38 hasMoreSiblings(false), total(0), level(0), height(0) {}
39 QModelIndex index; // we remove items whenever the indexes are invalidated
40 int parentItem; // parent item index in viewItems
41 uint expanded : 1;
42 uint spanning : 1;
43 uint hasChildren : 1; // if the item has visible children (even if collapsed)
44 uint hasMoreSiblings : 1;
45 uint total : 28; // total number of children visible
46 uint level : 16; // indentation
47 int height : 16; // row height
48};
49
51
52class Q_WIDGETS_EXPORT QTreeViewPrivate : public QAbstractItemViewPrivate
53{
54 Q_DECLARE_PUBLIC(QTreeView)
55public:
56
57 QTreeViewPrivate()
58 : QAbstractItemViewPrivate(),
59 header(nullptr), indent(20), lastViewedItem(0), defaultItemHeight(-1),
60 uniformRowHeights(false), rootDecoration(true),
61 itemsExpandable(true), sortingEnabled(false),
62 expandsOnDoubleClick(true),
63 allColumnsShowFocus(false), customIndent(false), current(0), spanning(false),
64 animationsEnabled(false),
65 autoExpandDelay(-1), hoverBranch(-1), geometryRecursionBlock(false), hasRemovedItems(false),
66 treePosition(0) {}
67
68 ~QTreeViewPrivate() {}
69 void initialize();
70 void clearConnections();
71 int logicalIndexForTree() const;
72 inline bool isTreePosition(int logicalIndex) const
73 {
74 return logicalIndex == logicalIndexForTree();
75 }
76
77 QItemViewPaintPairs draggablePaintPairs(const QModelIndexList &indexes, QRect *r) const override;
78 void adjustViewOptionsForIndex(QStyleOptionViewItem *option, const QModelIndex &current) const override;
79
80#if QT_CONFIG(animation)
81 struct AnimatedOperation : public QVariantAnimation
82 {
83 int item;
84 QPixmap before;
85 QPixmap after;
86 QWidget *viewport;
87 AnimatedOperation() : item(0) { setEasingCurve(QEasingCurve::InOutQuad); }
88 int top() const { return startValue().toInt(); }
89 QRect rect() const { QRect rect = viewport->rect(); rect.moveTop(top()); return rect; }
90 void updateCurrentValue(const QVariant &) override { viewport->update(rect()); }
91 void updateState(State state, State) override { if (state == Stopped) before = after = QPixmap(); }
92 } animatedOperation;
93 void prepareAnimatedOperation(int item, QVariantAnimation::Direction d);
94 void beginAnimatedOperation();
95 void drawAnimatedOperation(QPainter *painter) const;
96 QPixmap renderTreeToPixmapForAnimation(const QRect &rect) const;
97 void endAnimatedOperation();
98#endif // animation
99
100 void expand(int item, bool emitSignal);
101 void collapse(int item, bool emitSignal);
102
103 void columnsAboutToBeRemoved(const QModelIndex &, int, int) override;
104 void columnsRemoved(const QModelIndex &, int, int) override;
105 void modelAboutToBeReset();
106 void sortIndicatorChanged(int column, Qt::SortOrder order);
107 void modelDestroyed() override;
108 QRect intersectedRect(const QRect rect, const QModelIndex &topLeft, const QModelIndex &bottomRight) const override;
109
110 void layout(int item, bool recusiveExpanding = false, bool afterIsUninitialized = false);
111
112 int pageUp(int item) const;
113 int pageDown(int item) const;
114 int itemForKeyHome() const;
115 int itemForKeyEnd() const;
116
117 int itemHeight(int item) const;
118 int indentationForItem(int item) const;
119 int coordinateForItem(int item) const;
120 int itemAtCoordinate(int coordinate) const;
121
122 int viewIndex(const QModelIndex &index) const;
123 QModelIndex modelIndex(int i, int column = 0) const;
124
125 void insertViewItems(int pos, int count, const QTreeViewItem &viewItem);
126 void removeViewItems(int pos, int count);
127#if 0
128 bool checkViewItems() const;
129#endif
130
131 int firstVisibleItem(int *offset = nullptr) const;
132 int lastVisibleItem(int firstVisual = -1, int offset = -1) const;
133 int columnAt(int x) const;
134 bool hasVisibleChildren( const QModelIndex& parent) const;
135
136 bool expandOrCollapseItemAtPos(const QPoint &pos);
137
138 void updateScrollBars();
139
140 int itemDecorationAt(const QPoint &pos) const;
141 QRect itemDecorationRect(const QModelIndex &index) const;
142
143 QList<std::pair<int, int>> columnRanges(const QModelIndex &topIndex,
144 const QModelIndex &bottomIndex) const;
145 void select(const QModelIndex &start, const QModelIndex &stop, QItemSelectionModel::SelectionFlags command);
146
147 std::pair<int,int> startAndEndColumns(const QRect &rect) const;
148
149 void updateChildCount(const int parentItem, const int delta);
150
151 void paintAlternatingRowColors(QPainter *painter, QStyleOptionViewItem *option, int y, int bottom) const;
152
153 // logicalIndices: vector of currently visibly logical indices
154 // itemPositions: vector of view item positions (beginning/middle/end/onlyone)
155 void calcLogicalIndices(QList<int> *logicalIndices,
156 QList<QStyleOptionViewItem::ViewItemPosition> *itemPositions, int left,
157 int right) const;
158 int widthHintForIndex(const QModelIndex &index, int hint, const QStyleOptionViewItem &option, int i) const;
159
160 enum RectRule {
161 FullRow,
162 SingleSection,
163 AddRowIndicatorToFirstSection
164 };
165
166 // Base class will get the first visual rect including row indicator
167 QRect visualRect(const QModelIndex &index) const override
168 {
169 return visualRect(index, AddRowIndicatorToFirstSection);
170 }
171
172 QRect visualRect(const QModelIndex &index, RectRule rule) const;
173
174 QHeaderView *header;
175 int indent;
176
177 mutable QList<QTreeViewItem> viewItems;
178 mutable int lastViewedItem;
179 int defaultItemHeight; // this is just a number; contentsHeight() / numItems
180 bool uniformRowHeights; // used when all rows have the same height
181 bool rootDecoration;
182 bool itemsExpandable;
183 bool sortingEnabled;
184 bool expandsOnDoubleClick;
185 bool allColumnsShowFocus;
186 bool customIndent;
187
188 // used for drawing
189 mutable std::pair<int,int> leftAndRight;
190 mutable int current;
191 mutable bool spanning;
192
193 // used when expanding and collapsing items
194 QSet<QPersistentModelIndex> expandedIndexes;
195 bool animationsEnabled;
196
197 inline bool storeExpanded(const QPersistentModelIndex &idx) {
198 if (expandedIndexes.contains(idx))
199 return false;
200 expandedIndexes.insert(idx);
201 return true;
202 }
203
204 inline bool isIndexExpanded(const QModelIndex &idx) const {
205 //We first check if the idx is a QPersistentModelIndex, because creating QPersistentModelIndex is slow
206 return !(idx.flags() & Qt::ItemNeverHasChildren) && isPersistent(idx) && expandedIndexes.contains(idx);
207 }
208
209 // used when hiding and showing items
210 QSet<QPersistentModelIndex> hiddenIndexes;
211
212 inline bool isRowHidden(const QModelIndex &idx) const {
213 if (hiddenIndexes.isEmpty())
214 return false;
215 //We first check if the idx is a QPersistentModelIndex, because creating QPersistentModelIndex is slow
216 return isPersistent(idx) && hiddenIndexes.contains(idx);
217 }
218
219 inline bool isItemHiddenOrDisabled(int i) const {
220 if (i < 0 || i >= viewItems.size())
221 return false;
222 const QModelIndex index = viewItems.at(i).index;
223 return isRowHidden(index) || !isIndexEnabled(index);
224 }
225
226 inline int above(int item) const
227 { int i = item; while (isItemHiddenOrDisabled(--item)){} return item < 0 ? i : item; }
228 inline int below(int item) const
229 { int i = item; while (isItemHiddenOrDisabled(++item)){} return item >= viewItems.size() ? i : item; }
230 inline void invalidateHeightCache(int item) const
231 { viewItems[item].height = 0; }
232
233 inline int accessibleTable2Index(const QModelIndex &index) const {
234 return (viewIndex(index) + (header ? 1 : 0)) * model->columnCount()+index.column();
235 }
236
237 int accessibleTree2Index(const QModelIndex &index) const;
238
239 void updateIndentationFromStyle();
240
241 // used for spanning rows
242 QSet<QPersistentModelIndex> spanningIndexes;
243
244 // used for updating resized columns
245 QBasicTimer columnResizeTimer;
246 QList<int> columnsToUpdate;
247
248 // used for the automatic opening of nodes during DND
249 int autoExpandDelay;
250 QBasicTimer openTimer;
251
252 // used for drawing highlighted expand/collapse indicators
253 mutable int hoverBranch;
254
255 // used for blocking recursion when calling setViewportMargins from updateGeometries
256 bool geometryRecursionBlock;
257
258 // If we should clean the set
259 bool hasRemovedItems;
260
261 // tree position
262 int treePosition;
263
264 // pending accessibility update
265#if QT_CONFIG(accessibility)
266 bool pendingAccessibilityUpdate = false;
267#endif
268 void updateAccessibility();
269
270 QMetaObject::Connection animationConnection;
271 QMetaObject::Connection selectionmodelConnection;
272 std::array<QMetaObject::Connection, 2> modelConnections;
273 std::array<QMetaObject::Connection, 5> headerConnections;
274 QMetaObject::Connection sortHeaderConnection;
275};
276
277QT_END_NAMESPACE
278
279#endif // QTREEVIEW_P_H
QT_REQUIRE_CONFIG(treeview)
Q_DECLARE_TYPEINFO(QTreeViewItem, Q_RELOCATABLE_TYPE)