1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
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 The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
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 #include <QtCore/qvariantanimation.h>
58 #include <QtCore/qabstractitemmodel.h>
59 
60 #ifndef QT_NO_TREEVIEW
61 
62 QT_BEGIN_NAMESPACE
63 
64 struct QTreeViewItem
65 {
QTreeViewItemQTreeViewItem66     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
70     uint expanded : 1;
71     uint spanning : 1;
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
77 };
78 
79 Q_DECLARE_TYPEINFO(QTreeViewItem, Q_MOVABLE_TYPE);
80 
81 class Q_GUI_EXPORT QTreeViewPrivate : public QAbstractItemViewPrivate
82 {
Q_DECLARE_PUBLIC(QTreeView)83     Q_DECLARE_PUBLIC(QTreeView)
84 public:
85 
86     QTreeViewPrivate()
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) {}
95 
~QTreeViewPrivate()96     ~QTreeViewPrivate() {}
97     void initialize();
98 
99     QItemViewPaintPairs draggablePaintPairs(const QModelIndexList &indexes, QRect *r) const;
100     void adjustViewOptionsForIndex(QStyleOptionViewItemV4 *option, const QModelIndex &current) const;
101 
102 #ifndef QT_NO_ANIMATION
103     struct AnimatedOperation : public QVariantAnimation
104     {
105         int item;
106         QPixmap before;
107         QPixmap after;
108         QWidget *viewport;
AnimatedOperationAnimatedOperation109         AnimatedOperation() : item(0) { setEasingCurve(QEasingCurve::InOutQuad); }
topAnimatedOperation110         int top() const { return startValue().toInt(); }
rectAnimatedOperation111         QRect rect() const { QRect rect = viewport->rect(); rect.moveTop(top()); return rect; }
updateCurrentValueAnimatedOperation112         void updateCurrentValue(const QVariant &) { viewport->update(rect()); }
updateStateAnimatedOperation113         void updateState(State state, State) { if (state == Stopped) before = after = QPixmap(); }
114     } animatedOperation;
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
121 
122     void expand(int item, bool emitSignal);
123     void collapse(int item, bool emitSignal);
124 
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();
130 
131     void layout(int item, bool recusiveExpanding = false, bool afterIsUninitialized = false);
132 
133     int pageUp(int item) const;
134     int pageDown(int item) const;
135 
136     int itemHeight(int item) const;
137     int indentationForItem(int item) const;
138     int coordinateForItem(int item) const;
139     int itemAtCoordinate(int coordinate) const;
140 
141     int viewIndex(const QModelIndex &index) const;
142     QModelIndex modelIndex(int i, int column = 0) const;
143 
144     void insertViewItems(int pos, int count, const QTreeViewItem &viewItem);
145     void removeViewItems(int pos, int count);
146 #if 0
147     bool checkViewItems() const;
148 #endif
149 
150     int firstVisibleItem(int *offset = 0) const;
151     int columnAt(int x) const;
152     bool hasVisibleChildren( const QModelIndex& parent) const;
153 
154     bool expandOrCollapseItemAtPos(const QPoint &pos);
155 
156     void updateScrollBars();
157 
158     int itemDecorationAt(const QPoint &pos) const;
159     QRect itemDecorationRect(const QModelIndex &index) const;
160 
161 
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);
164 
165     QPair<int,int> startAndEndColumns(const QRect &rect) const;
166 
167     void updateChildCount(const int parentItem, const int delta);
168 
169     void paintAlternatingRowColors(QPainter *painter, QStyleOptionViewItemV4 *option, int y, int bottom) const;
170 
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;
174 
175     QHeaderView *header;
176     int indent;
177 
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
182     bool rootDecoration;
183     bool itemsExpandable;
184     bool sortingEnabled;
185     bool expandsOnDoubleClick;
186     bool allColumnsShowFocus;
187 
188     // used for drawing
189     mutable QPair<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 
storeExpanded(const QPersistentModelIndex & idx)197     inline bool storeExpanded(const QPersistentModelIndex &idx) {
198         if (expandedIndexes.contains(idx))
199             return false;
200         expandedIndexes.insert(idx);
201         return true;
202     }
203 
isIndexExpanded(const QModelIndex & 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);
207     }
208 
209     // used when hiding and showing items
210     QSet<QPersistentModelIndex> hiddenIndexes;
211 
isRowHidden(const QModelIndex & idx)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 
isItemHiddenOrDisabled(int i)219     inline bool isItemHiddenOrDisabled(int i) const {
220         if (i < 0 || i >= viewItems.count())
221             return false;
222         const QModelIndex index = viewItems.at(i).index;
223         return isRowHidden(index) || !isIndexEnabled(index);
224     }
225 
above(int item)226     inline int above(int item) const
227         { int i = item; while (isItemHiddenOrDisabled(--item)){} return item < 0 ? i : item; }
below(int item)228     inline int below(int item) const
229         { int i = item; while (isItemHiddenOrDisabled(++item)){} return item >= viewItems.count() ? i : item; }
invalidateHeightCache(int item)230     inline void invalidateHeightCache(int item) const
231         { viewItems[item].height = 0; }
232 
accessibleTable2Index(const QModelIndex & index)233     inline int accessibleTable2Index(const QModelIndex &index) const {
234         return (viewIndex(index) + (header ? 1 : 0)) * model->columnCount()+index.column() + 1;
235     }
236 
237     // used for spanning rows
238     QVector<QPersistentModelIndex> spanningIndexes;
239 
240     // used for updating resized columns
241     int columnResizeTimerID;
242     QList<int> columnsToUpdate;
243 
244     // used for the automatic opening of nodes during DND
245     int autoExpandDelay;
246     QBasicTimer openTimer;
247 
248     // used for drawing hilighted expand/collapse indicators
249     mutable int hoverBranch;
250 
251     // used for blocking recursion when calling setViewportMargins from updateGeometries
252     bool geometryRecursionBlock;
253 
254     // If we should clean the set
255     bool hasRemovedItems;
256 };
257 
258 QT_END_NAMESPACE
259 
260 #endif // QT_NO_TREEVIEW
261 
262 #endif // QTREEVIEW_P_H
263