| 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 | |
| 4 | #ifndef QTREEVIEW_P_H |
| 5 | #define QTREEVIEW_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtWidgets/private/qtwidgetsglobal_p.h> |
| 19 | #include "private/qabstractitemview_p.h" |
| 20 | #include <QtCore/qabstractitemmodel.h> |
| 21 | #include <QtCore/qlist.h> |
| 22 | #if QT_CONFIG(animation) |
| 23 | #include <QtCore/qvariantanimation.h> |
| 24 | #endif |
| 25 | |
| 26 | #include <array> |
| 27 | |
| 28 | QT_REQUIRE_CONFIG(treeview); |
| 29 | |
| 30 | QT_BEGIN_NAMESPACE |
| 31 | |
| 32 | struct QTreeViewItem |
| 33 | { |
| 34 | QTreeViewItem() : parentItem(-1), expanded(false), spanning(false), hasChildren(false), |
| 35 | hasMoreSiblings(false), total(0), level(0), height(0) {} |
| 36 | QModelIndex index; // we remove items whenever the indexes are invalidated |
| 37 | int parentItem; // parent item index in viewItems |
| 38 | uint expanded : 1; |
| 39 | uint spanning : 1; |
| 40 | uint hasChildren : 1; // if the item has visible children (even if collapsed) |
| 41 | uint hasMoreSiblings : 1; |
| 42 | uint total : 28; // total number of children visible |
| 43 | uint level : 16; // indentation |
| 44 | int height : 16; // row height |
| 45 | }; |
| 46 | |
| 47 | Q_DECLARE_TYPEINFO(QTreeViewItem, Q_RELOCATABLE_TYPE); |
| 48 | |
| 49 | class Q_WIDGETS_EXPORT QTreeViewPrivate : public QAbstractItemViewPrivate |
| 50 | { |
| 51 | Q_DECLARE_PUBLIC(QTreeView) |
| 52 | public: |
| 53 | |
| 54 | QTreeViewPrivate() |
| 55 | : QAbstractItemViewPrivate(), |
| 56 | header(nullptr), indent(20), lastViewedItem(0), defaultItemHeight(-1), |
| 57 | uniformRowHeights(false), rootDecoration(true), |
| 58 | itemsExpandable(true), sortingEnabled(false), |
| 59 | expandsOnDoubleClick(true), |
| 60 | allColumnsShowFocus(false), customIndent(false), current(0), spanning(false), |
| 61 | animationsEnabled(false), columnResizeTimerID(0), |
| 62 | autoExpandDelay(-1), hoverBranch(-1), geometryRecursionBlock(false), hasRemovedItems(false), |
| 63 | treePosition(0) {} |
| 64 | |
| 65 | ~QTreeViewPrivate() {} |
| 66 | void initialize(); |
| 67 | void clearConnections(); |
| 68 | int logicalIndexForTree() const; |
| 69 | inline bool isTreePosition(int logicalIndex) const |
| 70 | { |
| 71 | return logicalIndex == logicalIndexForTree(); |
| 72 | } |
| 73 | |
| 74 | QItemViewPaintPairs draggablePaintPairs(const QModelIndexList &indexes, QRect *r) const override; |
| 75 | void adjustViewOptionsForIndex(QStyleOptionViewItem *option, const QModelIndex ¤t) const override; |
| 76 | |
| 77 | #if QT_CONFIG(animation) |
| 78 | struct AnimatedOperation : public QVariantAnimation |
| 79 | { |
| 80 | int item; |
| 81 | QPixmap before; |
| 82 | QPixmap after; |
| 83 | QWidget *viewport; |
| 84 | AnimatedOperation() : item(0) { setEasingCurve(QEasingCurve::InOutQuad); } |
| 85 | int top() const { return startValue().toInt(); } |
| 86 | QRect rect() const { QRect rect = viewport->rect(); rect.moveTop(pos: top()); return rect; } |
| 87 | void updateCurrentValue(const QVariant &) override { viewport->update(rect()); } |
| 88 | void updateState(State state, State) override { if (state == Stopped) before = after = QPixmap(); } |
| 89 | } animatedOperation; |
| 90 | void prepareAnimatedOperation(int item, QVariantAnimation::Direction d); |
| 91 | void beginAnimatedOperation(); |
| 92 | void drawAnimatedOperation(QPainter *painter) const; |
| 93 | QPixmap renderTreeToPixmapForAnimation(const QRect &rect) const; |
| 94 | void endAnimatedOperation(); |
| 95 | #endif // animation |
| 96 | |
| 97 | void expand(int item, bool emitSignal); |
| 98 | void collapse(int item, bool emitSignal); |
| 99 | |
| 100 | void columnsAboutToBeRemoved(const QModelIndex &, int, int) override; |
| 101 | void columnsRemoved(const QModelIndex &, int, int) override; |
| 102 | void modelAboutToBeReset(); |
| 103 | void sortIndicatorChanged(int column, Qt::SortOrder order); |
| 104 | void modelDestroyed() override; |
| 105 | QRect intersectedRect(const QRect rect, const QModelIndex &topLeft, const QModelIndex &bottomRight) const override; |
| 106 | |
| 107 | void layout(int item, bool recusiveExpanding = false, bool afterIsUninitialized = false); |
| 108 | |
| 109 | int pageUp(int item) const; |
| 110 | int pageDown(int item) const; |
| 111 | int itemForKeyHome() const; |
| 112 | int itemForKeyEnd() const; |
| 113 | |
| 114 | int itemHeight(int item) const; |
| 115 | int indentationForItem(int item) const; |
| 116 | int coordinateForItem(int item) const; |
| 117 | int itemAtCoordinate(int coordinate) const; |
| 118 | |
| 119 | int viewIndex(const QModelIndex &index) const; |
| 120 | QModelIndex modelIndex(int i, int column = 0) const; |
| 121 | |
| 122 | void insertViewItems(int pos, int count, const QTreeViewItem &viewItem); |
| 123 | void removeViewItems(int pos, int count); |
| 124 | #if 0 |
| 125 | bool checkViewItems() const; |
| 126 | #endif |
| 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, rule: AddRowIndicatorToFirstSection); |
| 167 | } |
| 168 | |
| 169 | QRect visualRect(const QModelIndex &index, RectRule rule) const; |
| 170 | |
| 171 | QHeaderView *; |
| 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(value: idx)) |
| 196 | return false; |
| 197 | expandedIndexes.insert(value: 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(index: idx) && expandedIndexes.contains(value: 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(index: idx) && hiddenIndexes.contains(value: 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(idx: index) || !isIndexEnabled(index); |
| 221 | } |
| 222 | |
| 223 | inline int above(int item) const |
| 224 | { int i = item; while (isItemHiddenOrDisabled(i: --item)){} return item < 0 ? i : item; } |
| 225 | inline int below(int item) const |
| 226 | { int i = item; while (isItemHiddenOrDisabled(i: ++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 | int accessibleTree2Index(const QModelIndex &index) const; |
| 235 | |
| 236 | void updateIndentationFromStyle(); |
| 237 | |
| 238 | // used for spanning rows |
| 239 | QSet<QPersistentModelIndex> spanningIndexes; |
| 240 | |
| 241 | // used for updating resized columns |
| 242 | int columnResizeTimerID; |
| 243 | QList<int> columnsToUpdate; |
| 244 | |
| 245 | // used for the automatic opening of nodes during DND |
| 246 | int autoExpandDelay; |
| 247 | QBasicTimer openTimer; |
| 248 | |
| 249 | // used for drawing highlighted expand/collapse indicators |
| 250 | mutable int hoverBranch; |
| 251 | |
| 252 | // used for blocking recursion when calling setViewportMargins from updateGeometries |
| 253 | bool geometryRecursionBlock; |
| 254 | |
| 255 | // If we should clean the set |
| 256 | bool hasRemovedItems; |
| 257 | |
| 258 | // tree position |
| 259 | int treePosition; |
| 260 | |
| 261 | // pending accessibility update |
| 262 | #if QT_CONFIG(accessibility) |
| 263 | bool pendingAccessibilityUpdate = false; |
| 264 | #endif |
| 265 | void updateAccessibility(); |
| 266 | |
| 267 | QMetaObject::Connection animationConnection; |
| 268 | QMetaObject::Connection selectionmodelConnection; |
| 269 | std::array<QMetaObject::Connection, 2> modelConnections; |
| 270 | std::array<QMetaObject::Connection, 5> ; |
| 271 | QMetaObject::Connection ; |
| 272 | }; |
| 273 | |
| 274 | QT_END_NAMESPACE |
| 275 | |
| 276 | #endif // QTREEVIEW_P_H |
| 277 | |