From 0bcfa3d5d9d67912e1d7d62e6d15acedb98cabd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Martsum?= Date: Mon, 26 Aug 2013 12:14:03 +0200 Subject: QHeaderView - add maximum section size When we auto resize it is handy to be able to limit the maximum size (just like the minimum size). [ChangeLog][QtWidgets][QHeaderView]A maximumSize for sections has been introduced. The maximum section size is by default the largest possible section size which in Qt 5.2 has been limited to 1048575 pixels. Task-number: QTBUG-4346 Change-Id: Ida9cbcc11bd5c4498e319df2e6379c69a7033c04 Reviewed-by: Stephen Kelly --- src/widgets/itemviews/qheaderview.cpp | 50 ++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) (limited to 'src/widgets/itemviews/qheaderview.cpp') diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp index 75cd07bb80..239cc84a0a 100644 --- a/src/widgets/itemviews/qheaderview.cpp +++ b/src/widgets/itemviews/qheaderview.cpp @@ -81,6 +81,7 @@ 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 @@ -569,7 +570,7 @@ void QHeaderView::setVisible(bool v) /*! Returns a suitable size hint for the section specified by \a logicalIndex. - \sa sizeHint(), defaultSectionSize(), minimumSectionSize(), + \sa sizeHint(), defaultSectionSize(), minimumSectionSize(), maximumSectionSize() Qt::SizeHintRole */ @@ -587,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()); } /*! @@ -1606,8 +1607,48 @@ void QHeaderView::setMinimumSectionSize(int size) 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 @@ -2417,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; @@ -3236,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; -- cgit v1.2.3