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>
This commit is contained in:
Thorbjørn Martsum 2013-08-26 12:14:03 +02:00 committed by The Qt Project
parent 8f4b6f1cd1
commit 0bcfa3d5d9
4 changed files with 66 additions and 3 deletions

View File

@ -81,6 +81,7 @@ QDataStream &operator>>(QDataStream &in, QHeaderViewPrivate::SectionItem &sectio
#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;

View File

@ -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);

View File

@ -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;

View File

@ -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 };