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
128 int firstVisibleItem(int *offset = nullptr) const;
129 int lastVisibleItem(int firstVisual = -1, int offset = -1) const;
130 int columnAt(int x) const;
131 bool hasVisibleChildren( const QModelIndex& parent) const;
132
133 bool expandOrCollapseItemAtPos(const QPoint &pos);
134
135 void updateScrollBars();
136
137 int itemDecorationAt(const QPoint &pos) const;
138 QRect itemDecorationRect(const QModelIndex &index) const;
139
140 QList<std::pair<int, int>> columnRanges(const QModelIndex &topIndex,
141 const QModelIndex &bottomIndex) const;
142 void select(const QModelIndex &start, const QModelIndex &stop, QItemSelectionModel::SelectionFlags command);
143
144 std::pair<int,int> startAndEndColumns(const QRect &rect) const;
145
146 void updateChildCount(const int parentItem, const int delta);
147
148 void paintAlternatingRowColors(QPainter *painter, QStyleOptionViewItem *option, int y, int bottom) const;
149
150 // logicalIndices: vector of currently visibly logical indices
151 // itemPositions: vector of view item positions (beginning/middle/end/onlyone)
152 void calcLogicalIndices(QList<int> *logicalIndices,
153 QList<QStyleOptionViewItem::ViewItemPosition> *itemPositions, int left,
154 int right) const;
155 int widthHintForIndex(const QModelIndex &index, int hint, const QStyleOptionViewItem &option, int i) const;
156
157 enum RectRule {
158 FullRow,
159 SingleSection,
160 AddRowIndicatorToFirstSection
161 };
162
163 // Base class will get the first visual rect including row indicator
164 QRect visualRect(const QModelIndex &index) const override
165 {
166 return visualRect(index, AddRowIndicatorToFirstSection);
167 }
168
169 QRect visualRect(const QModelIndex &index, RectRule rule) const;
170
171 QHeaderView *header;
172 int indent;
173
174 mutable QList<QTreeViewItem> viewItems;
175 mutable int lastViewedItem;
176 int defaultItemHeight; // this is just a number; contentsHeight() / numItems
177 bool uniformRowHeights; // used when all rows have the same height
178 bool rootDecoration;
179 bool itemsExpandable;
180 bool sortingEnabled;
181 bool expandsOnDoubleClick;
182 bool allColumnsShowFocus;
183 bool customIndent;
184
185 // used for drawing
186 mutable std::pair<int,int> leftAndRight;
187 mutable int current;
188 mutable bool spanning;
189
190 // used when expanding and collapsing items
191 QSet<QPersistentModelIndex> expandedIndexes;
192 bool animationsEnabled;
193
194 inline bool storeExpanded(const QPersistentModelIndex &idx) {
195 if (expandedIndexes.contains(idx))
196 return false;
197 expandedIndexes.insert(idx);
198 return true;
199 }
200
201 inline bool isIndexExpanded(const QModelIndex &idx) const {
202 //We first check if the idx is a QPersistentModelIndex, because creating QPersistentModelIndex is slow
203 return !(idx.flags() & Qt::ItemNeverHasChildren) && isPersistent(idx) && expandedIndexes.contains(idx);
204 }
205
206 // used when hiding and showing items
207 QSet<QPersistentModelIndex> hiddenIndexes;
208
209 inline bool isRowHidden(const QModelIndex &idx) const {
210 if (hiddenIndexes.isEmpty())
211 return false;
212 //We first check if the idx is a QPersistentModelIndex, because creating QPersistentModelIndex is slow
213 return isPersistent(idx) && hiddenIndexes.contains(idx);
214 }
215
216 inline bool isItemHiddenOrDisabled(int i) const {
217 if (i < 0 || i >= viewItems.size())
218 return false;
219 const QModelIndex index = viewItems.at(i).index;
220 return isRowHidden(index) || !isIndexEnabled(index);
221 }
222
223 inline int above(int item) const
224 { int i = item; while (isItemHiddenOrDisabled(--item)){} return item < 0 ? i : item; }
225 inline int below(int item) const
226 { int i = item; while (isItemHiddenOrDisabled(++item)){} return item >= viewItems.size() ? i : item; }
227 inline void invalidateHeightCache(int item) const
228 { viewItems[item].height = 0; }
229
230 inline int accessibleTable2Index(const QModelIndex &index) const {
231 return (viewIndex(index) + (header ? 1 : 0)) * model->columnCount()+index.column();
232 }
233
234#if QT_CONFIG(accessibility)
235 int accessibleChildIndex(const QModelIndex &index) const override;
236#endif
237
238 void updateIndentationFromStyle();
239
240 // used for spanning rows
241 QSet<QPersistentModelIndex> spanningIndexes;
242
243 // used for updating resized columns
244 QBasicTimer columnResizeTimer;
245 QList<int> columnsToUpdate;
246
247 // used for the automatic opening of nodes during DND
248 int autoExpandDelay;
249 QBasicTimer openTimer;
250
251 // used for drawing highlighted expand/collapse indicators
252 mutable int hoverBranch;
253
254 // used for blocking recursion when calling setViewportMargins from updateGeometries
255 bool geometryRecursionBlock;
256
257 // If we should clean the set
258 bool hasRemovedItems;
259
260 // tree position
261 int treePosition;
262
263 // pending accessibility update
264#if QT_CONFIG(accessibility)
265 bool pendingAccessibilityUpdate = false;
266#endif
267 void updateAccessibility();
268
269 QMetaObject::Connection animationConnection;
270 QMetaObject::Connection selectionmodelConnection;
271 std::array<QMetaObject::Connection, 2> modelConnections;
272 std::array<QMetaObject::Connection, 5> headerConnections;
273 QMetaObject::Connection sortHeaderConnection;
274};
275
276QT_END_NAMESPACE
277
278#endif // QTREEVIEW_P_H
\inmodule QtWidgets
Definition qtreeview_p.h:53
QT_REQUIRE_CONFIG(treeview)
Q_DECLARE_TYPEINFO(QTreeViewItem, Q_RELOCATABLE_TYPE)