diff options
Diffstat (limited to 'src/widgets/itemviews/qheaderview.cpp')
-rw-r--r-- | src/widgets/itemviews/qheaderview.cpp | 155 |
1 files changed, 147 insertions, 8 deletions
diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp index 864a55d2ea..239cc84a0a 100644 --- a/src/widgets/itemviews/qheaderview.cpp +++ b/src/widgets/itemviews/qheaderview.cpp @@ -80,6 +80,8 @@ QDataStream &operator>>(QDataStream &in, QHeaderViewPrivate::SectionItem §io } #endif // QT_NO_DATASTREAM +static const int maxSizeSection = 1048575; // since section size is in a bitfield (uint 20). See qheaderview_p.h + // if this is changed then the docs in maximumSectionSize should be changed. /*! \class QHeaderView @@ -550,9 +552,25 @@ QSize QHeaderView::sizeHint() const } /*! + \reimp +*/ + +void QHeaderView::setVisible(bool v) +{ + bool actualChange = (v != isVisible()); + QAbstractItemView::setVisible(v); + if (actualChange) { + QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea*>(parentWidget()); + if (parent) + parent->updateGeometry(); + } +} + + +/*! Returns a suitable size hint for the section specified by \a logicalIndex. - \sa sizeHint(), defaultSectionSize(), minimumSectionSize(), + \sa sizeHint(), defaultSectionSize(), minimumSectionSize(), maximumSectionSize() Qt::SizeHintRole */ @@ -570,7 +588,7 @@ int QHeaderView::sectionSizeHint(int logicalIndex) const else size = sectionSizeFromContents(logicalIndex); int hint = d->orientation == Qt::Horizontal ? size.width() : size.height(); - return qMax(minimumSectionSize(), hint); + return qBound(minimumSectionSize(), hint, maximumSectionSize()); } /*! @@ -868,7 +886,7 @@ void QHeaderView::swapSections(int first, int second) void QHeaderView::resizeSection(int logical, int size) { Q_D(QHeaderView); - if (logical < 0 || logical >= count() || size < 0) + if (logical < 0 || logical >= count() || size < 0 || size > maxSizeSection) return; if (isSectionHidden(logical)) { @@ -918,6 +936,18 @@ void QHeaderView::resizeSection(int logical, int size) d->doDelayedResizeSections(); r = d->viewport->rect(); } + + // If the parent is a QAbstractScrollArea with QAbstractScrollArea::AdjustToContents + // then we want to change the geometry on that widget. Not doing it at once can/will + // cause scrollbars flicker as they would be shown at first but then removed. + // In the same situation it will also allow shrinking the whole view when stretchLastSection is set + // (It is default on QTreeViews - and it wouldn't shrink since the last stretch was made before the + // viewport was resized) + + QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea *>(parentWidget()); + if (parent && parent->sizeAdjustPolicy() == QAbstractScrollArea::AdjustToContents) + parent->updateGeometry(); + d->viewport->update(r.normalized()); emit sectionResized(logical, oldSize, size); } @@ -1225,7 +1255,7 @@ void QHeaderView::setSectionResizeMode(ResizeMode mode) property is set to true. This is the default for the horizontal headers provided by QTreeView. - \sa setStretchLastSection() + \sa setStretchLastSection(), resizeContentsPrecision() */ void QHeaderView::setSectionResizeMode(int logicalIndex, ResizeMode mode) @@ -1288,6 +1318,53 @@ QHeaderView::ResizeMode QHeaderView::sectionResizeMode(int logicalIndex) const return d->headerSectionResizeMode(visual); } +/*! + \since 5.2 + Sets how precise QHeaderView should calculate the size when ResizeToContents is used. + A low value will provide a less accurate but fast auto resize while a higher + value will provide a more accurate resize that however can be slow. + + The number \a precision specifies how many sections that should be consider + when calculating the preferred size. + + The default value is 1000 meaning that a horizontal column with auto-resize will look + at maximum 1000 rows on calculating when doing an auto resize. + + Special value 0 means that it will look at only the visible area. + Special value -1 will imply looking at all elements. + + This value is used in QTableView::sizeHintForColumn(), QTableView::sizeHintForRow() + and QTreeView::sizeHintForColumn(). Reimplementing these functions can make this + function not having an effect. + + If \a resizeSectionsNow is set to true (default) it will do adjustment of sections by calling + resizeSections(). (regardless if the precision was changed). + + \sa resizeContentsPrecision(), setSectionResizeMode(), resizeSections(), QTableView::sizeHintForColumn(), QTableView::sizeHintForRow(), QTreeView::sizeHintForColumn() +*/ + +void QHeaderView::setResizeContentsPrecision(int precision, bool resizeSectionsNow) +{ + Q_D(QHeaderView); + d->resizeContentsPrecision = precision; + if (resizeSectionsNow) + resizeSections(); +} + +/*! + \since 5.2 + Returns how precise QHeaderView will calculate on ResizeToContents. + + \sa setResizeContentsPrecision(), setSectionResizeMode() + +*/ + +int QHeaderView::resizeContentsPrecision() const +{ + Q_D(const QHeaderView); + return d->resizeContentsPrecision; +} + // ### Qt 6 - remove this obsolete function /*! \obsolete @@ -1492,7 +1569,7 @@ int QHeaderView::defaultSectionSize() const void QHeaderView::setDefaultSectionSize(int size) { Q_D(QHeaderView); - if (size < 0) + if (size < 0 || size > maxSizeSection) return; d->setDefaultSectionSize(size); } @@ -1527,12 +1604,52 @@ int QHeaderView::minimumSectionSize() const void QHeaderView::setMinimumSectionSize(int size) { Q_D(QHeaderView); - if (size < 0) + if (size < 0 || size > maxSizeSection) return; d->minimumSectionSize = size; + if (d->minimumSectionSize > maximumSectionSize()) + d->maximumSectionSize = size; } /*! + \since 5.2 + \property QHeaderView::maximumSectionSize + \brief the maximum size of the header sections. + + The maximum section size is the largest section size allowed. + The default value for this property is 1048575, which is also the largest + possible size for a section. Setting maximum to -1 will reset the value to + the largest section size. + + With exception of stretch this property is honored by all \l{ResizeMode}{resize modes} + + \sa setSectionResizeMode(), defaultSectionSize +*/ +int QHeaderView::maximumSectionSize() const +{ + Q_D(const QHeaderView); + if (d->maximumSectionSize == -1) + return maxSizeSection; + return d->maximumSectionSize; +} + +void QHeaderView::setMaximumSectionSize(int size) +{ + Q_D(QHeaderView); + if (size == -1) { + d->maximumSectionSize = maxSizeSection; + return; + } + if (size < 0 || size > maxSizeSection) + return; + if (minimumSectionSize() > size) + d->minimumSectionSize = size; + + d->maximumSectionSize = size; +} + + +/*! \since 4.1 \property QHeaderView::defaultAlignment \brief the default alignment of the text in each header section @@ -2341,7 +2458,8 @@ void QHeaderView::mouseMoveEvent(QMouseEvent *e) d->cascadingResize(visual, d->headerSectionSize(visual) + delta); } else { int delta = d->reverse() ? d->firstPos - pos : pos - d->firstPos; - resizeSection(d->section, qMax(d->originalSize + delta, minimumSectionSize())); + int newsize = qBound(minimumSectionSize(), d->originalSize + delta, maximumSectionSize()); + resizeSection(d->section, newsize); } d->lastPos = pos; return; @@ -3160,6 +3278,8 @@ void QHeaderViewPrivate::resizeSections(QHeaderView::ResizeMode globalMode, bool int logicalIndex = q->logicalIndex(i); sectionSize = qMax(viewSectionSizeHint(logicalIndex), q->sectionSizeHint(logicalIndex)); + if (sectionSize > q->maximumSectionSize()) + sectionSize = q->maximumSectionSize(); } section_sizes.append(sectionSize); lengthToStretch -= sectionSize; @@ -3492,7 +3612,7 @@ QHeaderView::ResizeMode QHeaderViewPrivate::headerSectionResizeMode(int visual) { if (visual < 0 || visual >= sectionItems.count()) return globalResizeMode; - return sectionItems.at(visual).resizeMode; + return static_cast<QHeaderView::ResizeMode>(sectionItems.at(visual).resizeMode); } void QHeaderViewPrivate::setGlobalHeaderResizeMode(QHeaderView::ResizeMode mode) @@ -3530,6 +3650,19 @@ int QHeaderViewPrivate::adjustedVisualIndex(int visualIndex) const return visualIndex; } +void QHeaderViewPrivate::setScrollOffset(const QScrollBar *scrollBar, QAbstractItemView::ScrollMode scrollMode) +{ + Q_Q(QHeaderView); + if (scrollMode == QAbstractItemView::ScrollPerItem) { + if (scrollBar->maximum() > 0 && scrollBar->value() == scrollBar->maximum()) + q->setOffsetToLastSection(); + else + q->setOffsetToSectionPosition(scrollBar->value()); + } else { + q->setOffset(scrollBar->value()); + } +} + #ifndef QT_NO_DATASTREAM void QHeaderViewPrivate::write(QDataStream &out) const { @@ -3560,6 +3693,8 @@ void QHeaderViewPrivate::write(QDataStream &out) const out << int(globalResizeMode); out << sectionItems; + if (out.version() >= QDataStream::Qt_5_2) + out << resizeContentsPrecision; } bool QHeaderViewPrivate::read(QDataStream &in) @@ -3611,6 +3746,10 @@ bool QHeaderViewPrivate::read(QDataStream &in) } sectionItems = newSectionItems; recalcSectionStartPos(); + + if (in.version() >= QDataStream::Qt_5_2) + in >> resizeContentsPrecision; + return true; } |