Qt 4.8
qheaderview_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 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.
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 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.
24 **
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.
28 **
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.
36 **
37 **
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 
65 
67 {
69 
70 public:
71  enum StateVersion { VersionMarker = 0xff };
72 
74  : state(NoState),
75  offset(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  resizeRecursionBlock(false),
94  stretchSections(0),
97  lastSectionSize(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);
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 {
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() {
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 
157  orientation = o;
158  defaultSectionSize = (o == Qt::Horizontal ? 100
159  : qMax(q_func()->minimumSectionSize(), 30));
161  ? Qt::Alignment(Qt::AlignCenter)
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 bool hasAutoResizeSections() const {
175  }
176 
178 
179  inline void invalidateCachedSizeHint() const {
180  cachedSizeHint = QSize();
181  }
182 
183  inline void initializeIndexMapping() const {
188  for (int s = 0; s < sectionCount; ++s) {
189  visualIndices[s] = s;
190  logicalIndices[s] = s;
191  }
192  }
193  }
194 
195  inline void clearCascadingSections() {
199  }
200 
201  inline void saveCascadingSectionSize(int visual, int size) {
202  if (!cascadingSectionSize.contains(visual)) {
203  cascadingSectionSize.insert(visual, size);
206  }
207  }
208 
209  inline bool sectionIsCascadable(int visual) const {
211  }
212 
213  inline int modelSectionCount() const {
214  return (orientation == Qt::Horizontal
216  : model->rowCount(root));
217  }
218 
219  inline bool modelIsEmpty() const {
220  return (model->rowCount(root) == 0 || model->columnCount(root) == 0);
221  }
222 
223  inline void doDelayedResizeSections() {
224  if (!delayedResize.isActive())
225  delayedResize.start(0, q_func());
226  }
227 
228  inline void executePostedResize() const {
229  if (delayedResize.isActive() && state == NoState) {
230  const_cast<QHeaderView*>(q_func())->resizeSections();
231  }
232  }
233 
234  void clear();
235  void flipSortIndicator(int section);
236  void cascadingResize(int visual, int newSize);
237 
239 
240  int offset;
245 
246  mutable QVector<int> visualIndices; // visualIndex = visualIndices.at(logicalIndex)
247  mutable QVector<int> logicalIndices; // logicalIndex = row or column in the model
248  mutable QBitArray sectionSelected; // from logical index to bit
249  mutable QBitArray sectionHidden; // from visual index to bit
250  mutable QHash<int, int> hiddenSectionSize; // from logical index to section size
251  mutable QHash<int, int> cascadingSectionSize; // from visual index to section size
254 
257 
258  int lastPos;
259  int firstPos;
261  int section; // used for resizing and moving sections
262  int target;
263  int pressed;
264  int hover;
265 
266  int length;
278  int lastSectionSize; // $$$
280  Qt::Alignment defaultAlignment;
284 
285  // header section spans
286 
287  struct SectionSpan {
288  int size;
289  int count;
291  inline SectionSpan() : size(0), count(0), resizeMode(QHeaderView::Interactive) {}
292  inline SectionSpan(int length, int sections, QHeaderView::ResizeMode mode)
293  : size(length), count(sections), resizeMode(mode) {}
294  inline int sectionSize() const { return (count > 0 ? size / count : 0); }
295 #ifndef QT_NO_DATASTREAM
296  inline void write(QDataStream &out) const
297  { out << size; out << count; out << (int)resizeMode; }
298  inline void read(QDataStream &in)
299  { in >> size; in >> count; int m; in >> m; resizeMode = (QHeaderView::ResizeMode)m; }
300 #endif
301  };
302 
304 
305  void createSectionSpan(int start, int end, int size, QHeaderView::ResizeMode mode);
306  void removeSectionsFromSpans(int start, int end);
307  void resizeSectionSpan(int visualIndex, int oldSize, int newSize);
308  void setDefaultSectionSize(int size);
309 
310  inline int headerSectionCount() const { // for debugging
311  int count = 0;
312  for (int i = 0; i < sectionSpans.count(); ++i)
313  count += sectionSpans.at(i).count;
314  return count;
315  }
316 
317  inline int headerLength() const { // for debugging
318  int len = 0;
319  for (int i = 0; i < sectionSpans.count(); ++i)
320  len += sectionSpans.at(i).size;
321  return len;
322  }
323 
324  inline void removeSpans(const QList<int> &spans) {
325  for (int i = spans.count() - 1; i >= 0; --i) {
326  length -= sectionSpans.at(spans.at(i)).size;
327  sectionSpans.remove(spans.at(i));
328  }
329  }
330 
331  inline int sectionSpanIndex(int visual) const {
332  int section_start = 0;
333  for (int i = 0; i < sectionSpans.count(); ++i) {
334  int section_end = section_start + sectionSpans.at(i).count - 1;
335  if (visual >= section_start && visual <= section_end)
336  return i;
337  section_start = section_end + 1;
338  }
339  return -1;
340  }
341 
342  int headerSectionSize(int visual) const;
343  int headerSectionPosition(int visual) const;
344  int headerVisualIndexAt(int position) const;
345 
346  // resize mode
350 
351  // other
352  int viewSectionSizeHint(int logical) const;
353  int adjustedVisualIndex(int visualIndex) const;
354 
355 #ifndef QT_NO_DATASTREAM
356  void write(QDataStream &out) const;
357  bool read(QDataStream &in);
358 #endif
359 
360 };
361 
363 
364 #endif // QT_NO_ITEMVIEWS
365 
366 #endif // QHEADERVIEW_P_H
void clear()
Clears the contents of the bit array and makes it empty.
Definition: qbitarray.h:85
void initializeIndexMapping() const
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of columns for the children of the given parent.
QLabel * sectionIndicator
QHash< int, int > cascadingSectionSize
QHash< int, int > hiddenSectionSize
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
void setBit(int i)
Sets the bit at index position i to 1.
Definition: qbitarray.h:128
virtual QAbstractItemView::DropIndicatorPosition position(const QPoint &pos, const QRect &rect, const QModelIndex &idx) const
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int viewSectionSizeHint(int logical) const
void _q_layoutChanged()
This slot is called when the layout is changed.
void clear()
Removes all items from the hash.
Definition: qhash.h:574
int headerSectionSize(int visual) const
void setGlobalHeaderResizeMode(QHeaderView::ResizeMode mode)
void remove(int i)
Removes the element at index position i.
Definition: qvector.h:374
void executePostedResize() const
The QStyleOptionHeader class is used to describe the parameters for drawing a header.
Definition: qstyleoption.h:251
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
bool isColumnSelected(int column, const QModelIndex &parent) const
Returns true if all items are selected in the column with the given parent.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
int headerVisualIndexAt(int position) const
void cascadingResize(int visual, int newSize)
void setupSectionIndicator(int section, int position)
bool rowIntersectsSelection(int row) const
void write(QDataStream &out) const
QHeaderView::ResizeMode headerSectionResizeMode(int visual) const
bool isActive() const
Returns true if the timer is running and has not been stopped; otherwise returns false.
Definition: qbasictimer.h:62
void updateHiddenSections(int logicalFirst, int logicalLast)
QPointer< QItemSelectionModel > selectionModel
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QBitArray sectionHidden
ResizeMode
The resize mode specifies the behavior of the header sections.
Definition: qheaderview.h:72
void removeSpans(const QList< int > &spans)
bool isRowSelected(int row) const
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
int headerSectionPosition(int visual) const
void prepareSectionSelected()
bool rowIntersectsSelection(int row, const QModelIndex &parent) const
Returns true if there are any items selected in the row with the given parent.
void read(QDataStream &in)
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
int lastVisibleVisualIndex() const
Returns the last visible (ie.
QVector< int > visualIndices
SortOrder
Definition: qnamespace.h:189
bool fill(bool val, int size=-1)
Sets every bit in the bit array to value, returning true if successful; otherwise returns false...
Definition: qbitarray.h:117
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int visualIndex(int logicalIndex) const
QBasicTimer delayedResize
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
bool reverse() const
QStyleOptionHeader getStyleOption() const
bool sectionIsCascadable(int visual) const
void setVisualIndexHidden(int visual, bool hidden)
int headerLength() const
int modelSectionCount() const
bool isVisualIndexHidden(int visual) const
The State element defines configurations of objects and properties.
int headerSectionCount() const
void removeSectionsFromSpans(int start, int end)
QHeaderView::ResizeMode globalResizeMode
bool hasAutoResizeSections() const
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QVector< int > logicalIndices
QPersistentModelIndex root
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54
int adjustedVisualIndex(int visualIndex) const
bool isSectionSelected(int section) const
bool hasSelection() const
Returns true if the selection model contains any selection ranges; otherwise returns false...
bool columnIntersectsSelection(int column, const QModelIndex &parent) const
Returns true if there are any items selected in the column with the given parent. ...
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
QBitArray sectionSelected
void resizeSections(QHeaderView::ResizeMode globalMode, bool useGlobalMode=false)
Go through and resize all of the sections applying stretchLastSection, manualy stretches, sizes, and useGlobalMode.
void clearCascadingSections()
QVector< SectionSpan > sectionSpans
The QModelIndex class is used to locate data in a data model.
int sectionHandleAt(int position)
void setDefaultValues(Qt::Orientation o)
bool columnIntersectsSelection(int column) const
void resizeSectionSpan(int visualIndex, int oldSize, int newSize)
Definition: qnamespace.h:54
The QLabel widget provides a text or image display.
Definition: qlabel.h:55
QHeaderView::ResizeMode resizeMode
int count() const
Same as size().
Definition: qbitarray.h:74
void saveCascadingSectionSize(int visual, int size)
int sectionSpanIndex(int visual) const
The QBasicTimer class provides timer events for objects.
Definition: qbasictimer.h:55
void setDefaultSectionSize(int size)
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
Qt::Alignment defaultAlignment
bool sectionIntersectsSelection(int logical) const
void createSectionSpan(int start, int end, int size, QHeaderView::ResizeMode mode)
SectionSpan(int length, int sections, QHeaderView::ResizeMode mode)
Qt::SortOrder sortIndicatorOrder
void flipSortIndicator(int section)
bool at(int i) const
Returns the value of the bit at index position i.
Definition: qbitarray.h:146
void invalidateCachedSizeHint() const
void _q_layoutAboutToBeChanged()
static const KeyPair *const end
enum QHeaderViewPrivate::State state
Orientation
Definition: qnamespace.h:174
void updateSectionIndicator(int section, int position)
QAbstractItemModel * model
void _q_sectionsRemoved(const QModelIndex &, int, int)
void setHeaderSectionResizeMode(int visual, QHeaderView::ResizeMode mode)
void doDelayedResizeSections()
int logicalIndex(int visualIndex) const
bool isEmpty() const
Returns true if this bit array has size 0; otherwise returns false.
Definition: qbitarray.h:78
Qt::Orientation orientation
bool isColumnSelected(int column) const
void start(int msec, QObject *obj)
Starts (or restarts) the timer with a msec milliseconds timeout.
QList< QPersistentModelIndex > persistentHiddenSections
bool isRowSelected(int row, const QModelIndex &parent) const
Returns true if all items are selected in the row with the given parent.
bool modelIsEmpty() const
The QHeaderView class provides a header row or header column for item views.
Definition: qheaderview.h:58