1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef QTREEVIEW_P_H
|
---|
43 | #define QTREEVIEW_P_H
|
---|
44 |
|
---|
45 | //
|
---|
46 | // W A R N I N G
|
---|
47 | // -------------
|
---|
48 | //
|
---|
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.
|
---|
52 | //
|
---|
53 | // We mean it.
|
---|
54 | //
|
---|
55 |
|
---|
56 | #include "private/qabstractitemview_p.h"
|
---|
57 |
|
---|
58 | #ifndef QT_NO_TREEVIEW
|
---|
59 |
|
---|
60 | QT_BEGIN_NAMESPACE
|
---|
61 |
|
---|
62 | struct QTreeViewItem
|
---|
63 | {
|
---|
64 | QTreeViewItem() : expanded(false), spanning(false), total(0), level(0), height(0) {}
|
---|
65 | QModelIndex index; // we remove items whenever the indexes are invalidated
|
---|
66 | uint expanded : 1;
|
---|
67 | uint spanning : 1;
|
---|
68 | uint total : 30; // total number of children visible
|
---|
69 | uint level : 16; // indentation
|
---|
70 | int height : 16; // row height
|
---|
71 | };
|
---|
72 |
|
---|
73 | class QTreeViewPrivate : public QAbstractItemViewPrivate
|
---|
74 | {
|
---|
75 | Q_DECLARE_PUBLIC(QTreeView)
|
---|
76 | public:
|
---|
77 |
|
---|
78 | QTreeViewPrivate()
|
---|
79 | : QAbstractItemViewPrivate(),
|
---|
80 | header(0), indent(20), lastViewedItem(0), defaultItemHeight(-1),
|
---|
81 | uniformRowHeights(false), rootDecoration(true),
|
---|
82 | itemsExpandable(true), sortingEnabled(false),
|
---|
83 | expandsOnDoubleClick(true),
|
---|
84 | allColumnsShowFocus(false),
|
---|
85 | animationsEnabled(false), columnResizeTimerID(0),
|
---|
86 | autoExpandDelay(-1), hoverBranch(-1), geometryRecursionBlock(false) {}
|
---|
87 |
|
---|
88 | ~QTreeViewPrivate() {}
|
---|
89 | void initialize();
|
---|
90 |
|
---|
91 | struct AnimatedOperation
|
---|
92 | {
|
---|
93 | enum Type { Expand, Collapse };
|
---|
94 | int item;
|
---|
95 | int top;
|
---|
96 | int duration;
|
---|
97 | Type type;
|
---|
98 | QPixmap before;
|
---|
99 | QPixmap after;
|
---|
100 | };
|
---|
101 |
|
---|
102 | void expand(int item, bool emitSignal);
|
---|
103 | void collapse(int item, bool emitSignal);
|
---|
104 |
|
---|
105 | void prepareAnimatedOperation(int item, AnimatedOperation::Type type);
|
---|
106 | void beginAnimatedOperation();
|
---|
107 | void _q_endAnimatedOperation();
|
---|
108 | void drawAnimatedOperation(QPainter *painter) const;
|
---|
109 | QPixmap renderTreeToPixmapForAnimation(const QRect &rect) const;
|
---|
110 |
|
---|
111 | inline QRect animationRect() const
|
---|
112 | { return QRect(0, animatedOperation.top, viewport->width(),
|
---|
113 | viewport->height() - animatedOperation.top); }
|
---|
114 |
|
---|
115 | void _q_currentChanged(const QModelIndex&, const QModelIndex&);
|
---|
116 | void _q_columnsAboutToBeRemoved(const QModelIndex &, int, int);
|
---|
117 | void _q_columnsRemoved(const QModelIndex &, int, int);
|
---|
118 | void _q_modelAboutToBeReset();
|
---|
119 | void _q_animate();
|
---|
120 | void _q_sortIndicatorChanged(int column, Qt::SortOrder order);
|
---|
121 | void _q_modelDestroyed();
|
---|
122 |
|
---|
123 | void layout(int item);
|
---|
124 |
|
---|
125 | int pageUp(int item) const;
|
---|
126 | int pageDown(int item) const;
|
---|
127 |
|
---|
128 | int itemHeight(int item) const;
|
---|
129 | int indentationForItem(int item) const;
|
---|
130 | int coordinateForItem(int item) const;
|
---|
131 | int itemAtCoordinate(int coordinate) const;
|
---|
132 |
|
---|
133 | int viewIndex(const QModelIndex &index) const;
|
---|
134 | QModelIndex modelIndex(int i, int column = 0) const;
|
---|
135 |
|
---|
136 | int firstVisibleItem(int *offset = 0) const;
|
---|
137 | int columnAt(int x) const;
|
---|
138 | bool hasVisibleChildren( const QModelIndex& parent) const;
|
---|
139 |
|
---|
140 | void relayout(const QModelIndex &parent);
|
---|
141 | bool expandOrCollapseItemAtPos(const QPoint &pos);
|
---|
142 |
|
---|
143 | void updateScrollBars();
|
---|
144 |
|
---|
145 | int itemDecorationAt(const QPoint &pos) const;
|
---|
146 | QRect itemDecorationRect(const QModelIndex &index) const;
|
---|
147 |
|
---|
148 |
|
---|
149 | QList<QPair<int, int> > columnRanges(const QModelIndex &topIndex, const QModelIndex &bottomIndex) const;
|
---|
150 | void select(const QModelIndex &start, const QModelIndex &stop, QItemSelectionModel::SelectionFlags command);
|
---|
151 |
|
---|
152 | QPair<int,int> startAndEndColumns(const QRect &rect) const;
|
---|
153 |
|
---|
154 | void updateChildCount(const int parentItem, const int delta);
|
---|
155 | void rowsRemoved(const QModelIndex &parent,
|
---|
156 | int start, int end, bool before);
|
---|
157 |
|
---|
158 | void paintAlternatingRowColors(QPainter *painter, QStyleOptionViewItemV4 *option, int y, int bottom) const;
|
---|
159 |
|
---|
160 | QHeaderView *header;
|
---|
161 | int indent;
|
---|
162 |
|
---|
163 | mutable QVector<QTreeViewItem> viewItems;
|
---|
164 | mutable int lastViewedItem;
|
---|
165 | int defaultItemHeight; // this is just a number; contentsHeight() / numItems
|
---|
166 | bool uniformRowHeights; // used when all rows have the same height
|
---|
167 | bool rootDecoration;
|
---|
168 | bool itemsExpandable;
|
---|
169 | bool sortingEnabled;
|
---|
170 | bool expandsOnDoubleClick;
|
---|
171 | bool allColumnsShowFocus;
|
---|
172 |
|
---|
173 | // used for drawing
|
---|
174 | mutable QPair<int,int> leftAndRight;
|
---|
175 | mutable int current;
|
---|
176 | mutable bool spanning;
|
---|
177 |
|
---|
178 | // used when expanding and collapsing items
|
---|
179 | QSet<QPersistentModelIndex> expandedIndexes;
|
---|
180 | QStack<bool> expandParent;
|
---|
181 | AnimatedOperation animatedOperation;
|
---|
182 | bool animationsEnabled;
|
---|
183 |
|
---|
184 | inline bool storeExpanded(const QPersistentModelIndex &idx) {
|
---|
185 | if (expandedIndexes.contains(idx))
|
---|
186 | return false;
|
---|
187 | expandedIndexes.insert(idx);
|
---|
188 | return true;
|
---|
189 | }
|
---|
190 |
|
---|
191 | inline bool isIndexExpanded(const QModelIndex &idx) const {
|
---|
192 | //We first check if the idx is a QPersistentModelIndex, because creating QPersistentModelIndex is slow
|
---|
193 | return isPersistent(idx) && expandedIndexes.contains(idx);
|
---|
194 | }
|
---|
195 |
|
---|
196 | // used when hiding and showing items
|
---|
197 | QSet<QPersistentModelIndex> hiddenIndexes;
|
---|
198 |
|
---|
199 | inline bool isRowHidden(const QModelIndex &idx) const {
|
---|
200 | //We first check if the idx is a QPersistentModelIndex, because creating QPersistentModelIndex is slow
|
---|
201 | return isPersistent(idx) && hiddenIndexes.contains(idx);
|
---|
202 | }
|
---|
203 |
|
---|
204 | inline bool isItemHiddenOrDisabled(int i) const {
|
---|
205 | if (i < 0 || i >= viewItems.count())
|
---|
206 | return false;
|
---|
207 | const QModelIndex index = viewItems.at(i).index;
|
---|
208 | return isRowHidden(index) || !isIndexEnabled(index);
|
---|
209 | }
|
---|
210 |
|
---|
211 | inline int above(int item) const
|
---|
212 | { int i = item; while (isItemHiddenOrDisabled(--item)){} return item < 0 ? i : item; }
|
---|
213 | inline int below(int item) const
|
---|
214 | { int i = item; while (isItemHiddenOrDisabled(++item)){} return item >= viewItems.count() ? i : item; }
|
---|
215 | inline void invalidateHeightCache(int item) const
|
---|
216 | { viewItems[item].height = 0; }
|
---|
217 |
|
---|
218 | // used for spanning rows
|
---|
219 | QVector<QPersistentModelIndex> spanningIndexes;
|
---|
220 |
|
---|
221 | // used for updating resized columns
|
---|
|
---|