diff options
author | Thorbjørn Martsum <tmartsum@gmail.com> | 2013-08-26 12:14:03 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-09-02 16:26:21 +0200 |
commit | 0bcfa3d5d9d67912e1d7d62e6d15acedb98cabd9 (patch) | |
tree | 0ac1b925d96b9f50e89b7d601c6dc96b856e504e | |
parent | 8f4b6f1cd166986f219e72a584fe19379880c2c5 (diff) |
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 <stephen.kelly@kdab.com>
-rw-r--r-- | src/widgets/itemviews/qheaderview.cpp | 50 | ||||
-rw-r--r-- | src/widgets/itemviews/qheaderview.h | 3 | ||||
-rw-r--r-- | src/widgets/itemviews/qheaderview_p.h | 2 | ||||
-rw-r--r-- | tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp | 14 |
4 files changed, 66 insertions, 3 deletions
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,9 +1607,49 @@ 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 \brief the default alignment of the text in each header section @@ -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; diff --git a/src/widgets/itemviews/qheaderview.h b/src/widgets/itemviews/qheaderview.h index 3de3d320bf..b3ed666aa6 100644 --- a/src/widgets/itemviews/qheaderview.h +++ b/src/widgets/itemviews/qheaderview.h @@ -61,6 +61,7 @@ class Q_WIDGETS_EXPORT QHeaderView : public QAbstractItemView Q_PROPERTY(bool cascadingSectionResizes READ cascadingSectionResizes WRITE setCascadingSectionResizes) Q_PROPERTY(int defaultSectionSize READ defaultSectionSize WRITE setDefaultSectionSize) Q_PROPERTY(int minimumSectionSize READ minimumSectionSize WRITE setMinimumSectionSize) + Q_PROPERTY(int maximumSectionSize READ maximumSectionSize WRITE setMaximumSectionSize) Q_PROPERTY(Qt::Alignment defaultAlignment READ defaultAlignment WRITE setDefaultAlignment) Q_ENUMS(ResizeMode) @@ -166,6 +167,8 @@ public: int minimumSectionSize() const; void setMinimumSectionSize(int size); + int maximumSectionSize() const; + void setMaximumSectionSize(int size); Qt::Alignment defaultAlignment() const; void setDefaultAlignment(Qt::Alignment alignment); diff --git a/src/widgets/itemviews/qheaderview_p.h b/src/widgets/itemviews/qheaderview_p.h index a8c620b9d2..b2af821e9b 100644 --- a/src/widgets/itemviews/qheaderview_p.h +++ b/src/widgets/itemviews/qheaderview_p.h @@ -95,6 +95,7 @@ public: stretchSections(0), contentsSections(0), minimumSectionSize(-1), + maximumSectionSize(-1), lastSectionSize(0), sectionIndicatorOffset(0), sectionIndicator(0), @@ -287,6 +288,7 @@ public: int contentsSections; int defaultSectionSize; int minimumSectionSize; + int maximumSectionSize; int lastSectionSize; // $$$ int sectionIndicatorOffset; Qt::Alignment defaultAlignment; diff --git a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp index 09f0161dff..398b1b4d98 100644 --- a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp +++ b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp @@ -2721,6 +2721,20 @@ void tst_QHeaderView::resizeToContentTest() QVERIFY(view->sectionSize(1) > 1); QVERIFY(view->sectionSize(2) > 1); + // Check minimum section size + hh->setMinimumSectionSize(150); + model->setData(idx, QVariant("i")); + hh->resizeSections(QHeaderView::ResizeToContents); + QCOMPARE(hh->sectionSize(3), 150); + hh->setMinimumSectionSize(-1); + + // Check maximumSection size + hh->setMaximumSectionSize(200); + model->setData(idx, QVariant("This is a even longer string that is expected to be more than 200 pixels")); + hh->resizeSections(QHeaderView::ResizeToContents); + QCOMPARE(hh->sectionSize(3), 200); + hh->setMaximumSectionSize(-1); + view->setDefaultSectionSize(25); // To make sure our precalced data are correct. We do not know font height etc. const int precalced_results[] = { -1523279360, -1523279360, -1347156568, 1, 1719705216, 1719705216, 12500 }; |