1 /****************************************************************************
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtGui module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include "private/qabstractitemview_p.h"
57 #include <QtCore/qvariantanimation.h>
58 #include <QtCore/qabstractitemmodel.h>
60 #ifndef QT_NO_TREEVIEW
66 QTreeViewItem() : parentItem(-1), expanded(false), spanning(false), hasChildren(false),
67 hasMoreSiblings(false), total(0), level(0), height(0) {}
68 QModelIndex index; // we remove items whenever the indexes are invalidated
69 int parentItem; // parent item index in viewItems
72 uint hasChildren : 1; // if the item has visible children (even if collapsed)
73 uint hasMoreSiblings : 1;
74 uint total : 28; // total number of children visible
75 uint level : 16; // indentation
76 int height : 16; // row height
79 Q_DECLARE_TYPEINFO(QTreeViewItem, Q_MOVABLE_TYPE);
81 class Q_GUI_EXPORT QTreeViewPrivate : public QAbstractItemViewPrivate
83 Q_DECLARE_PUBLIC(QTreeView)
87 : QAbstractItemViewPrivate(),
88 header(0), indent(20), lastViewedItem(0), defaultItemHeight(-1),
89 uniformRowHeights(false), rootDecoration(true),
90 itemsExpandable(true), sortingEnabled(false),
91 expandsOnDoubleClick(true),
92 allColumnsShowFocus(false), current(0), spanning(false),
93 animationsEnabled(false), columnResizeTimerID(0),
94 autoExpandDelay(-1), hoverBranch(-1), geometryRecursionBlock(false), hasRemovedItems(false) {}
96 ~QTreeViewPrivate() {}
99 QItemViewPaintPairs draggablePaintPairs(const QModelIndexList &indexes, QRect *r) const;
100 void adjustViewOptionsForIndex(QStyleOptionViewItemV4 *option, const QModelIndex ¤t) const;
102 #ifndef QT_NO_ANIMATION
103 struct AnimatedOperation : public QVariantAnimation
109 AnimatedOperation() : item(0) { setEasingCurve(QEasingCurve::InOutQuad); }
110 int top() const { return startValue().toInt(); }
111 QRect rect() const { QRect rect = viewport->rect(); rect.moveTop(top()); return rect; }
112 void updateCurrentValue(const QVariant &) { viewport->update(rect()); }
113 void updateState(State state, State) { if (state == Stopped) before = after = QPixmap(); }
115 void prepareAnimatedOperation(int item, QVariantAnimation::Direction d);
116 void beginAnimatedOperation();
117 void drawAnimatedOperation(QPainter *painter) const;
118 QPixmap renderTreeToPixmapForAnimation(const QRect &rect) const;
119 void _q_endAnimatedOperation();
120 #endif //QT_NO_ANIMATION
122 void expand(int item, bool emitSignal);
123 void collapse(int item, bool emitSignal);
125 void _q_columnsAboutToBeRemoved(const QModelIndex &, int, int);
126 void _q_columnsRemoved(const QModelIndex &, int, int);
127 void _q_modelAboutToBeReset();
128 void _q_sortIndicatorChanged(int column, Qt::SortOrder order);
129 void _q_modelDestroyed();
131 void layout(int item, bool recusiveExpanding = false, bool afterIsUninitialized = false);
133 int pageUp(int item) const;
134 int pageDown(int item) const;
136 int itemHeight(int item) const;
137 int indentationForItem(int item) const;
138 int coordinateForItem(int item) const;
139 int itemAtCoordinate(int coordinate) const;
141 int viewIndex(const QModelIndex &index) const;
142 QModelIndex modelIndex(int i, int column = 0) const;
144 void insertViewItems(int pos, int count, const QTreeViewItem &viewItem);
145 void removeViewItems(int pos, int count);
147 bool checkViewItems() const;
150 int firstVisibleItem(int *offset = 0) const;
151 int columnAt(int x) const;
152 bool hasVisibleChildren( const QModelIndex& parent) const;
154 bool expandOrCollapseItemAtPos(const QPoint &pos);
156 void updateScrollBars();
158 int itemDecorationAt(const QPoint &pos) const;
159 QRect itemDecorationRect(const QModelIndex &index) const;
162 QList<QPair<int, int> > columnRanges(const QModelIndex &topIndex, const QModelIndex &bottomIndex) const;
163 void select(const QModelIndex &start, const QModelIndex &stop, QItemSelectionModel::SelectionFlags command);
165 QPair<int,int> startAndEndColumns(const QRect &rect) const;
167 void updateChildCount(const int parentItem, const int delta);
169 void paintAlternatingRowColors(QPainter *painter, QStyleOptionViewItemV4 *option, int y, int bottom) const;
171 // logicalIndices: vector of currently visibly logical indices
172 // itemPositions: vector of view item positions (beginning/middle/end/onlyone)
173 void calcLogicalIndices(QVector<int> *logicalIndices, QVector<QStyleOptionViewItemV4::ViewItemPosition> *itemPositions, int left, int right) const;
178 mutable QVector<QTreeViewItem> viewItems;
179 mutable int lastViewedItem;
180 int defaultItemHeight; // this is just a number; contentsHeight() / numItems
181 bool uniformRowHeights; // used when all rows have the same height
183 bool itemsExpandable;
185 bool expandsOnDoubleClick;
186 bool allColumnsShowFocus;
189 mutable QPair<int,int> leftAndRight;
191 mutable bool spanning;
193 // used when expanding and collapsing items
194 QSet<QPersistentModelIndex> expandedIndexes;
195 bool animationsEnabled;
197 inline bool storeExpanded(const QPersistentModelIndex &idx) {
198 if (expandedIndexes.contains(idx))
200 expandedIndexes.insert(idx);
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 isPersistent(idx) && expandedIndexes.contains(idx);
209 // used when hiding and showing items
210 QSet<QPersistentModelIndex> hiddenIndexes;
212 inline bool isRowHidden(const QModelIndex &idx) const {
213 if (hiddenIndexes.isEmpty())
215 //We first check if the idx is a QPersistentModelIndex, because creating QPersistentModelIndex is slow
216 return isPersistent(idx) && hiddenIndexes.contains(idx);
219 inline bool isItemHiddenOrDisabled(int i) const {
220 if (i < 0 || i >= viewItems.count())
222 const QModelIndex index = viewItems.at(i).index;
223 return isRowHidden(index) || !isIndexEnabled(index);
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.count() ? i : item; }
230 inline void invalidateHeightCache(int item) const
231 { viewItems[item].height = 0; }
233 inline int accessibleTable2Index(const QModelIndex &index) const {
234 return (viewIndex(index) + (header ? 1 : 0)) * model->columnCount()+index.column() + 1;
237 // used for spanning rows
238 QVector<QPersistentModelIndex> spanningIndexes;
240 // used for updating resized columns
241 int columnResizeTimerID;
242 QList<int> columnsToUpdate;
244 // used for the automatic opening of nodes during DND
246 QBasicTimer openTimer;
248 // used for drawing hilighted expand/collapse indicators
249 mutable int hoverBranch;
251 // used for blocking recursion when calling setViewportMargins from updateGeometries
252 bool geometryRecursionBlock;
254 // If we should clean the set
255 bool hasRemovedItems;
260 #endif // QT_NO_TREEVIEW
262 #endif // QTREEVIEW_P_H