source: trunk/src/gui/itemviews/qheaderview_p.h@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 12.4 KB
Line 
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 QHEADERVIEW_P_H
43#define QHEADERVIEW_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_ITEMVIEWS
59
60#include "QtCore/qbitarray.h"
61#include "QtGui/qapplication.h"
62#include "QtGui/qlabel.h"
63
64QT_BEGIN_NAMESPACE
65
66class QHeaderViewPrivate: public QAbstractItemViewPrivate
67{
68 Q_DECLARE_PUBLIC(QHeaderView)
69
70public:
71 enum StateVersion { VersionMarker = 0xff };
72
73 QHeaderViewPrivate()
74 : state(NoState),
75 offset(0),
76 sortIndicatorOrder(Qt::DescendingOrder),
77 sortIndicatorSection(0),
78 sortIndicatorShown(false),
79 lastPos(-1),
80 firstPos(-1),
81 originalSize(-1),
82 section(-1),
83 target(-1),
84 pressed(-1),
85 hover(-1),
86 length(0),
87 sectionCount(0),
88 movableSections(false),
89 clickableSections(false),
90 highlightSelected(false),
91 stretchLastSection(false),
92 cascadingResizing(false),
93 forceInitializing(false),
94 stretchSections(0),
95 contentsSections(0),
96 minimumSectionSize(-1),
97 lastSectionSize(0),
98 sectionIndicatorOffset(0),
99 sectionIndicator(0),
100 globalResizeMode(QHeaderView::Interactive)
101 {}
102
103
104 int lastVisibleVisualIndex() const;
105 int sectionHandleAt(int position);
106 void setupSectionIndicator(int section, int position);
107 void updateSectionIndicator(int section, int position);
108 void updateHiddenSections(int logicalFirst, int logicalLast);
109 void resizeSections(QHeaderView::ResizeMode globalMode, bool useGlobalMode = false);
110 void _q_sectionsRemoved(const QModelIndex &,int,int);
111 void _q_layoutAboutToBeChanged();
112 void _q_layoutChanged();
113
114 bool isSectionSelected(int section) const;
115
116 inline bool rowIntersectsSelection(int row) const {
117 return (selectionModel ? selectionModel->rowIntersectsSelection(row, root) : false);
118 }
119
120 inline bool columnIntersectsSelection(int column) const {
121 return (selectionModel ? selectionModel->columnIntersectsSelection(column, root) : false);
122 }
123
124 inline bool sectionIntersectsSelection(int logical) const {
125 return (orientation == Qt::Horizontal ? columnIntersectsSelection(logical) : rowIntersectsSelection(logical));
126 }
127
128 inline bool isRowSelected(int row) const {
129 return (selectionModel ? selectionModel->isRowSelected(row, root) : false);
130 }
131
132 inline bool isColumnSelected(int column) const {
133 return (selectionModel ? selectionModel->isColumnSelected(column, root) : false);
134 }
135
136 inline void prepareSectionSelected() {
137 if (!selectionModel || !selectionModel->hasSelection())
138 sectionSelected.clear();
139 else if (sectionSelected.count() != sectionCount * 2)
140 sectionSelected.fill(false, sectionCount * 2);
141 else sectionSelected.fill(false);
142 }
143
144 inline bool reverse() const {
145 return orientation == Qt::Horizontal && q_func()->isRightToLeft();
146 }
147
148 inline int logicalIndex(int visualIndex) const {
149 return logicalIndices.isEmpty() ? visualIndex : logicalIndices.at(visualIndex);
150 }
151
152 inline int visualIndex(int logicalIndex) const {
153 return visualIndices.isEmpty() ? logicalIndex : visualIndices.at(logicalIndex);
154 }
155
156 inline void setDefaultValues(Qt::Orientation o) {
157 orientation = o;
158 defaultSectionSize = (o == Qt::Horizontal ? 100
159 : qMax(q_func()->minimumSectionSize(), 30));
160 defaultAlignment = (o == Qt::Horizontal
161 ? Qt::Alignment(Qt::AlignCenter)
162 : Qt::AlignLeft|Qt::AlignVCenter);
163 }
164
165 inline bool isVisualIndexHidden(int visual) const {
166 return !sectionHidden.isEmpty() && sectionHidden.at(visual);
167 }
168
169 inline void setVisualIndexHidden(int visual, bool hidden) {
170 if (!sectionHidden.isEmpty()) sectionHidden.setBit(visual, hidden);
171 }
172
173 inline QHeaderView::ResizeMode visualIndexResizeMode(int visual) const {
174 return headerSectionResizeMode(visual);
175 }
176
177 inline bool hasAutoResizeSections() const {
178 return stretchSections || stretchLastSection || contentsSections;
179 }
180
181 QStyleOptionHeader getStyleOption() const;
182
183 inline void invalidateCachedSizeHint() const {
184 cachedSizeHint = QSize();
185 }
186
187 inline void initializeIndexMapping() const {
188 if (visualIndices.count() != sectionCount
189 || logicalIndices.count() != sectionCount) {
190 visualIndices.resize(sectionCount);
191 logicalIndices.resize(sectionCount);
192 for (int s = 0; s < sectionCount; ++s) {
193 visualIndices[s] = s;
194 logicalIndices[s] = s;
195 }
196 }
197 }
198
199 inline void clearCascadingSections() {
200 firstCascadingSection = sectionCount;
201 lastCascadingSection = 0;
202 cascadingSectionSize.clear();
203 }
204
205 inline void saveCascadingSectionSize(int visual, int size) {
206 if (!cascadingSectionSize.contains(visual)) {
207 cascadingSectionSize.insert(visual, size);
208 firstCascadingSection = qMin(firstCascadingSection, visual);
209 lastCascadingSection = qMax(lastCascadingSection, visual);
210 }
211 }
212
213 inline bool sectionIsCascadable(int visual) const {
214 return visualIndexResizeMode(visual) == QHeaderView::Interactive;
215 }
216
217 inline int modelSectionCount() const {
218 return (orientation == Qt::Horizontal
219 ? model->columnCount(root)
220 : model->rowCount(root));
221 }
222
223 inline bool modelIsEmpty() const {
224 return (model->rowCount(root) == 0 || model->columnCount(root) == 0);
225 }
226
227 inline void doDelayedResizeSections() {
228 if (!delayedResize.isActive())
229 delayedResize.start(0, q_func());
230 }
231
232 inline void executePostedResize() const {
233 if (delayedResize.isActive() && state == NoState) {
234 delayedResize.stop();
235 const_cast<QHeaderView*>(q_func())->resizeSections();
236 }
237 }
238
239 void clear();
240 void flipSortIndicator(int section);
241 void cascadingResize(int visual, int newSize);
242
243 enum State { NoState, ResizeSection, MoveSection, SelectSections, NoClear } state;
244
245 int offset;
246 Qt::Orientation orientation;
247 Qt::SortOrder sortIndicatorOrder;
248 int sortIndicatorSection;
249 bool sortIndicatorShown;
250
251 mutable QVector<int> visualIndices; // visualIndex = visualIndices.at(logicalIndex)
252 mutable QVector<int> logicalIndices; // logicalIndex = row or column in the model
253 mutable QBitArray sectionSelected; // from logical index to bit
254 mutable QBitArray sectionHidden; // from visual index to bit
255 mutable QHash<int, int> hiddenSectionSize; // from logical index to section size
256 mutable QHash<int, int> cascadingSectionSize; // from visual index to section size
257 mutable QSize cachedSizeHint;
258 mutable QBasicTimer delayedResize;
259
260 int firstCascadingSection;
261 int lastCascadingSection;
262
263 int lastPos;
264 int firstPos;
265 int originalSize;
266 int section; // used for resizing and moving sections
267 int target;
268 int pressed;
269 int hover;
270
271 int length;
272 int sectionCount;
273 bool movableSections;
274 bool clickableSections;
275 bool highlightSelected;
276 bool stretchLastSection;
277 bool cascadingResizing;
278 bool forceInitializing;
279 int stretchSections;
280 int contentsSections;
281 int defaultSectionSize;
282 int minimumSectionSize;
283 int lastSectionSize; // $$$
284 int sectionIndicatorOffset;
285 Qt::Alignment defaultAlignment;
286 QLabel *sectionIndicator;
287 QHeaderView::ResizeMode globalResizeMode;
288 QList<QPersistentModelIndex> persistentHiddenSections;
289
290 // header section spans
291
292 struct SectionSpan {
293 int size;
294 int count;
295 QHeaderView::ResizeMode resizeMode;
296 inline SectionSpan() : size(0), count(0), resizeMode(QHeaderView::Interactive) {}
297 inline SectionSpan(int length, int sections, QHeaderView::ResizeMode mode)
298 : size(length), count(sections), resizeMode(mode) {}
299 inline int sectionSize() const { return (count > 0 ? size / count : 0); }
300#ifndef QT_NO_DATASTREAM
301 inline void write(QDataStream &out) const
302 { out << size; out << count; out << (int)resizeMode; }
303 inline void read(QDataStream &in)
304 { in >> size; in >> count; int m; in >> m; resizeMode = (QHeaderView::ResizeMode)m; }
305#endif
306 };
307
308 QVector<SectionSpan> sectionSpans;
309
310 void createSectionSpan(int start, int end, int size, QHeaderView::ResizeMode mode);
311 void removeSectionsFromSpans(int start, int end);
312 void resizeSectionSpan(int visualIndex, int oldSize, int newSize);
313
314 inline int headerSectionCount() const { // for debugging
315 int count = 0;
316 for (int i = 0; i < sectionSpans.count(); ++i)
317 count += sectionSpans.at(i).count;
318 return count;
319 }
320
321 inline int headerLength() const { // for debugging
322 int len = 0;
323 for (int i = 0; i < sectionSpans.count(); ++i)
324 len += sectionSpans.at(i).size;
325 return len;
326 }
327
328 inline void removeSpans(const QList<int> &spans) {
329 for (int i = spans.count() - 1; i >= 0; --i) {
330 length -= sectionSpans.at(spans.at(i)).size;
331 sectionSpans.remove(spans.at(i));
332 }
333 }
334
335 inline int sectionSpanIndex(int visual) const {
336 int section_start = 0;
337 for (int i = 0; i < sectionSpans.count(); ++i) {
338 int section_end = section_start + sectionSpans.at(i).count - 1;
339 if (visual >= section_start && visual <= section_end)
340 return i;
341 section_start = section_end + 1;
342 }
343 return -1;
344 }
345
346 int headerSectionSize(int visual) const;
347 int headerSectionPosition(int visual) const;
348 int headerVisualIndexAt(int position) const;
349
350 // resize mode
351 void setHeaderSectionResizeMode(int visual, QHeaderView::ResizeMode mode);
352 QHeaderView::ResizeMode headerSectionResizeMode(int visual) const;
353 void setGlobalHeaderResizeMode(QHeaderView::ResizeMode mode);
354
355 // other
356 int viewSectionSizeHint(int logical) const;
357 int adjustedVisualIndex(int visualIndex) const;
358
359#ifndef QT_NO_DATASTREAM
360 void write(QDataStream &out) const;
361 bool read(QDataStream &in);
362#endif
363
364};
365
366QT_END_NAMESPACE
367
368#endif // QT_NO_ITEMVIEWS
369
370#endif // QHEADERVIEW_P_H
Note: See TracBrowser for help on using the repository browser.