From 29176bf2f836ba05206680ae1c00edf786202648 Mon Sep 17 00:00:00 2001 From: Marcel Kummer Date: Wed, 15 Jun 2022 13:13:25 +0200 Subject: QBoxLayout: Pull out handling for negative insertion indices This snippet occurs in every insert function further down. Let's pull it out in preparation for adding an assertion that the index is in range, which would also need to be added to every insert function. Fixes: QTBUG-103775 Change-Id: I419f57434f860df4c947853ac307826994f0198b Pick-to: 6.2 6.3 6.4 Reviewed-by: Volker Hilsheimer --- src/widgets/kernel/qboxlayout.cpp | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'src/widgets/kernel') diff --git a/src/widgets/kernel/qboxlayout.cpp b/src/widgets/kernel/qboxlayout.cpp index f7535a2d70..6384fda896 100644 --- a/src/widgets/kernel/qboxlayout.cpp +++ b/src/widgets/kernel/qboxlayout.cpp @@ -88,6 +88,7 @@ public: void effectiveMargins(int *left, int *top, int *right, int *bottom) const; QLayoutItem* replaceAt(int index, QLayoutItem*) override; + int validateIndex(int index) const; }; QBoxLayoutPrivate::~QBoxLayoutPrivate() @@ -407,6 +408,13 @@ QLayoutItem* QBoxLayoutPrivate::replaceAt(int index, QLayoutItem *item) return r; } +int QBoxLayoutPrivate::validateIndex(int index) const +{ + if (index < 0) + return list.count(); // append + + return index; +} /*! \class QBoxLayout @@ -811,9 +819,7 @@ void QBoxLayout::addItem(QLayoutItem *item) void QBoxLayout::insertItem(int index, QLayoutItem *item) { Q_D(QBoxLayout); - if (index < 0) // append - index = d->list.count(); - + index = d->validateIndex(index); QBoxLayoutItem *it = new QBoxLayoutItem(item); d->list.insert(index, it); invalidate(); @@ -831,9 +837,7 @@ void QBoxLayout::insertItem(int index, QLayoutItem *item) void QBoxLayout::insertSpacing(int index, int size) { Q_D(QBoxLayout); - if (index < 0) // append - index = d->list.count(); - + index = d->validateIndex(index); QLayoutItem *b; if (horz(d->dir)) b = QLayoutPrivate::createSpacerItem(this, size, 0, QSizePolicy::Fixed, QSizePolicy::Minimum); @@ -856,9 +860,7 @@ void QBoxLayout::insertSpacing(int index, int size) void QBoxLayout::insertStretch(int index, int stretch) { Q_D(QBoxLayout); - if (index < 0) // append - index = d->list.count(); - + index = d->validateIndex(index); QLayoutItem *b; if (horz(d->dir)) b = QLayoutPrivate::createSpacerItem(this, 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum); @@ -883,9 +885,7 @@ void QBoxLayout::insertStretch(int index, int stretch) void QBoxLayout::insertSpacerItem(int index, QSpacerItem *spacerItem) { Q_D(QBoxLayout); - if (index < 0) // append - index = d->list.count(); - + index = d->validateIndex(index); QBoxLayoutItem *it = new QBoxLayoutItem(spacerItem); it->magic = true; d->list.insert(index, it); @@ -907,8 +907,7 @@ void QBoxLayout::insertLayout(int index, QLayout *layout, int stretch) return; if (!adoptLayout(layout)) return; - if (index < 0) // append - index = d->list.count(); + index = d->validateIndex(index); QBoxLayoutItem *it = new QBoxLayoutItem(layout, stretch); d->list.insert(index, it); invalidate(); @@ -941,8 +940,7 @@ void QBoxLayout::insertWidget(int index, QWidget *widget, int stretch, if (!d->checkWidget(widget)) return; addChildWidget(widget); - if (index < 0) // append - index = d->list.count(); + index = d->validateIndex(index); QWidgetItem *b = QLayoutPrivate::createWidgetItem(this, widget); b->setAlignment(alignment); -- cgit v1.2.3