summaryrefslogtreecommitdiffstats
path: root/src/widgets/kernel/qboxlayout.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/kernel/qboxlayout.cpp')
-rw-r--r--src/widgets/kernel/qboxlayout.cpp147
1 files changed, 67 insertions, 80 deletions
diff --git a/src/widgets/kernel/qboxlayout.cpp b/src/widgets/kernel/qboxlayout.cpp
index a23b89a8a5..501883e85a 100644
--- a/src/widgets/kernel/qboxlayout.cpp
+++ b/src/widgets/kernel/qboxlayout.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtWidgets module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qapplication.h"
#include "qboxlayout.h"
@@ -63,7 +27,7 @@ struct QBoxLayoutItem
}
int mhfw(int w) {
if (item->hasHeightForWidth()) {
- return item->heightForWidth(w);
+ return item->minimumHeightForWidth(w);
} else {
return item->minimumSize().height();
}
@@ -124,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()
@@ -149,8 +114,8 @@ void QBoxLayoutPrivate::effectiveMargins(int *left, int *top, int *right, int *b
#ifdef Q_OS_MAC
Q_Q(const QBoxLayout);
if (horz(dir)) {
- QBoxLayoutItem *leftBox = 0;
- QBoxLayoutItem *rightBox = 0;
+ QBoxLayoutItem *leftBox = nullptr;
+ QBoxLayoutItem *rightBox = nullptr;
if (left || right) {
leftBox = list.value(0);
@@ -194,8 +159,8 @@ void QBoxLayoutPrivate::effectiveMargins(int *left, int *top, int *right, int *b
}
}
} else { // vertical layout
- QBoxLayoutItem *topBox = 0;
- QBoxLayoutItem *bottomBox = 0;
+ QBoxLayoutItem *topBox = nullptr;
+ QBoxLayoutItem *bottomBox = nullptr;
if (top || bottom) {
topBox = list.value(0);
@@ -268,7 +233,7 @@ void QBoxLayoutPrivate::setupGeom()
hasHfw = false;
- int n = list.count();
+ int n = list.size();
geomArray.clear();
QList<QLayoutStruct> a(n);
@@ -400,7 +365,7 @@ void QBoxLayoutPrivate::setupGeom()
void QBoxLayoutPrivate::calcHfw(int w)
{
QList<QLayoutStruct> &a = geomArray;
- int n = a.count();
+ int n = a.size();
int h = 0;
int mh = 0;
@@ -443,6 +408,14 @@ QLayoutItem* QBoxLayoutPrivate::replaceAt(int index, QLayoutItem *item)
return r;
}
+int QBoxLayoutPrivate::validateIndex(int index) const
+{
+ if (index < 0)
+ return list.size(); // append
+
+ Q_ASSERT_X(index >= 0 && index <= list.size(), "QBoxLayout::insert", "index out of range");
+ return index;
+}
/*!
\class QBoxLayout
@@ -547,7 +520,11 @@ QLayoutItem* QBoxLayoutPrivate::replaceAt(int index, QLayoutItem *item)
Constructs a new QBoxLayout with direction \a dir and parent widget \a
parent.
- \sa direction()
+ The layout is set directly as the top-level layout for \a parent.
+ There can be only one top-level layout for a widget. It is returned
+ by QWidget::layout().
+
+ \sa direction(), QWidget::setLayout()
*/
QBoxLayout::QBoxLayout(Direction dir, QWidget *parent)
: QLayout(*new QBoxLayoutPrivate, nullptr, parent)
@@ -703,7 +680,7 @@ void QBoxLayout::invalidate()
int QBoxLayout::count() const
{
Q_D(const QBoxLayout);
- return d->list.count();
+ return d->list.size();
}
/*!
@@ -712,7 +689,7 @@ int QBoxLayout::count() const
QLayoutItem *QBoxLayout::itemAt(int index) const
{
Q_D(const QBoxLayout);
- return index >= 0 && index < d->list.count() ? d->list.at(index)->item : nullptr;
+ return index >= 0 && index < d->list.size() ? d->list.at(index)->item : nullptr;
}
/*!
@@ -721,7 +698,7 @@ QLayoutItem *QBoxLayout::itemAt(int index) const
QLayoutItem *QBoxLayout::takeAt(int index)
{
Q_D(QBoxLayout);
- if (index < 0 || index >= d->list.count())
+ if (index < 0 || index >= d->list.size())
return nullptr;
QBoxLayoutItem *b = d->list.takeAt(index);
QLayoutItem *item = b->item;
@@ -772,7 +749,7 @@ void QBoxLayout::setGeometry(const QRect &r)
QList<QLayoutStruct> a = d->geomArray;
int pos = horz(d->dir) ? s.x() : s.y();
int space = horz(d->dir) ? s.width() : s.height();
- int n = a.count();
+ int n = a.size();
if (d->hasHfw && !horz(d->dir)) {
for (int i = 0; i < n; i++) {
QBoxLayoutItem *box = d->list.at(i);
@@ -843,9 +820,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();
@@ -863,9 +838,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);
@@ -888,9 +861,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);
@@ -915,9 +886,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);
@@ -939,8 +908,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();
@@ -973,8 +941,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);
@@ -1231,11 +1198,16 @@ QBoxLayout::Direction QBoxLayout::direction() const
\snippet layouts/layouts.cpp 4
\snippet layouts/layouts.cpp 5
- First, we create the widgets we want in the layout. Then, we
- create the QHBoxLayout object and add the widgets into the
- layout. Finally, we call QWidget::setLayout() to install the
- QHBoxLayout object onto the widget. At that point, the widgets in
- the layout are reparented to have \c window as their parent.
+ First, we create the widgets we want to add to the layout. Then,
+ we create the QHBoxLayout object, setting \c window as parent by
+ passing it in the constructor; next we add the widgets to the
+ layout. \c window will be the parent of the widgets that are
+ added to the layout.
+
+ If you don't pass a parent \c window to the constructor, you can
+ at a later point use QWidget::setLayout() to install the QHBoxLayout
+ object onto \c window. At that point, the widgets in the layout are
+ reparented to have \c window as their parent.
\image qhboxlayout-with-5-children.png Horizontal box layout with five child widgets
@@ -1244,8 +1216,13 @@ QBoxLayout::Direction QBoxLayout::direction() const
/*!
- Constructs a new top-level horizontal box with
- parent \a parent.
+ Constructs a new top-level horizontal box with parent \a parent.
+
+ The layout is set directly as the top-level layout for \a parent.
+ There can be only one top-level layout for a widget. It is returned
+ by QWidget::layout().
+
+ \sa QWidget::setLayout()
*/
QHBoxLayout::QHBoxLayout(QWidget *parent)
: QBoxLayout(LeftToRight, parent)
@@ -1294,11 +1271,16 @@ QHBoxLayout::~QHBoxLayout()
\snippet layouts/layouts.cpp 10
\snippet layouts/layouts.cpp 11
- First, we create the widgets we want in the layout. Then, we
- create the QVBoxLayout object and add the widgets into the
- layout. Finally, we call QWidget::setLayout() to install the
- QVBoxLayout object onto the widget. At that point, the widgets in
- the layout are reparented to have \c window as their parent.
+ First, we create the widgets we want to add to the layout. Then,
+ we create the QVBoxLayout object, setting \c window as parent by
+ passing it in the constructor; next we add the widgets to the
+ layout. \c window will be the parent of the widgets that are
+ added to the layout.
+
+ If you don't pass a parent \c window to the constructor, you can
+ at a later point use QWidget::setLayout() to install the QVBoxLayout
+ object onto \c window. At that point, the widgets in the layout are
+ reparented to have \c window as their parent.
\image qvboxlayout-with-5-children.png Horizontal box layout with five child widgets
@@ -1306,8 +1288,13 @@ QHBoxLayout::~QHBoxLayout()
*/
/*!
- Constructs a new top-level vertical box with
- parent \a parent.
+ Constructs a new top-level vertical box with parent \a parent.
+
+ The layout is set directly as the top-level layout for \a parent.
+ There can be only one top-level layout for a widget. It is returned
+ by QWidget::layout().
+
+ \sa QWidget::setLayout()
*/
QVBoxLayout::QVBoxLayout(QWidget *parent)
: QBoxLayout(TopToBottom, parent)