summaryrefslogtreecommitdiffstats
path: root/src/widgets/kernel/qlayout.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/kernel/qlayout.cpp')
-rw-r--r--src/widgets/kernel/qlayout.cpp176
1 files changed, 87 insertions, 89 deletions
diff --git a/src/widgets/kernel/qlayout.cpp b/src/widgets/kernel/qlayout.cpp
index 300dded3a4..a826ea75bc 100644
--- a/src/widgets/kernel/qlayout.cpp
+++ b/src/widgets/kernel/qlayout.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 "qlayout.h"
@@ -93,24 +57,28 @@ static int menuBarHeightForWidth(QWidget *menubar, int w)
resized to zero size if there is too little space. To support
children whose heights depend on their widths, implement
hasHeightForWidth() and heightForWidth(). See the
- \l{layouts/borderlayout}{Border Layout} and
- \l{layouts/flowlayout}{Flow Layout} examples for
+ \l{layouts/flowlayout}{Flow Layout} example for
more information about implementing custom layout managers.
Geometry management stops when the layout manager is deleted.
\sa QLayoutItem, {Layout Management}, {Basic Layouts Example},
- {Border Layout Example}, {Flow Layout Example}
+ {Flow Layout Example}
*/
/*!
Constructs a new top-level QLayout, with parent \a parent.
- \a parent may not be \nullptr.
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().
+
+ If \a parent is \nullptr, then you must insert this layout
+ into another layout, or set it as a widget's layout using
+ QWidget::setLayout().
+
+ \sa QWidget::setLayout()
*/
QLayout::QLayout(QWidget *parent)
: QObject(*new QLayoutPrivate, parent)
@@ -120,18 +88,6 @@ QLayout::QLayout(QWidget *parent)
parent->setLayout(this);
}
-/*!
- Constructs a new child QLayout.
-
- This layout has to be inserted into another layout before geometry
- management will work.
-*/
-QLayout::QLayout()
- : QObject(*new QLayoutPrivate, nullptr)
-{
-}
-
-
/*! \internal
*/
QLayout::QLayout(QLayoutPrivate &dd, QLayout *lay, QWidget *w)
@@ -360,6 +316,19 @@ void QLayout::setContentsMargins(const QMargins &margins)
}
/*!
+ \since 6.1
+
+ Unsets any user-defined margins around the layout. The layout will
+ use the default values provided by the style.
+
+ \sa setContentsMargins()
+*/
+void QLayout::unsetContentsMargins()
+{
+ setContentsMargins(-1, -1, -1, -1);
+}
+
+/*!
\since 4.3
For each of \a left, \a top, \a right and \a bottom that is not
@@ -551,10 +520,11 @@ void QLayoutPrivate::doResize()
void QLayout::widgetEvent(QEvent *e)
{
Q_D(QLayout);
- if (!d->enabled)
+ const QEvent::Type type = e->type();
+ if (!d->enabled && type != QEvent::ChildRemoved)
return;
- switch (e->type()) {
+ switch (type) {
case QEvent::Resize:
if (d->activated)
d->doResize();
@@ -564,12 +534,14 @@ void QLayout::widgetEvent(QEvent *e)
case QEvent::ChildRemoved:
{
QChildEvent *c = (QChildEvent *)e;
- if (c->child()->isWidgetType()) {
+ QObject *child = c->child();
+ QObjectPrivate *op = QObjectPrivate::get(child);
+ if (op->wasWidget) {
#if QT_CONFIG(menubar)
- if (c->child() == d->menubar)
+ if (child == d->menubar)
d->menubar = nullptr;
#endif
- removeWidgetRecursively(this, c->child());
+ removeWidgetRecursively(this, child);
}
}
break;
@@ -602,6 +574,28 @@ void QLayout::childEvent(QChildEvent *e)
\internal
Also takes contentsMargins and menu bar into account.
*/
+int QLayout::totalMinimumHeightForWidth(int w) const
+{
+ Q_D(const QLayout);
+ int side=0, top=0;
+ if (d->topLevel) {
+ QWidget *parent = parentWidget();
+ parent->ensurePolished();
+ QWidgetPrivate *wd = parent->d_func();
+ side += wd->leftmargin + wd->rightmargin;
+ top += wd->topmargin + wd->bottommargin;
+ }
+ int h = minimumHeightForWidth(w - side) + top;
+#if QT_CONFIG(menubar)
+ h += menuBarHeightForWidth(d->menubar, w);
+#endif
+ return h;
+}
+
+/*!
+ \internal
+ Also takes contentsMargins and menu bar into account.
+*/
int QLayout::totalHeightForWidth(int w) const
{
Q_D(const QLayout);
@@ -715,24 +709,24 @@ QLayout::~QLayout()
/*!
This function is called from \c addLayout() or \c insertLayout() functions in
- subclasses to add layout \a l as a sub-layout.
+ subclasses to add layout \a childLayout as a sub-layout.
The only scenario in which you need to call it directly is if you
implement a custom layout that supports nested layouts.
\sa QBoxLayout::addLayout(), QBoxLayout::insertLayout(), QGridLayout::addLayout()
*/
-void QLayout::addChildLayout(QLayout *l)
+void QLayout::addChildLayout(QLayout *childLayout)
{
- if (Q_UNLIKELY(l->parent())) {
- qWarning("QLayout::addChildLayout: layout \"%ls\" already has a parent",
- qUtf16Printable(l->objectName()));
+ if (Q_UNLIKELY(childLayout->parent())) {
+ qWarning("QLayout::addChildLayout: layout %s \"%ls\" already has a parent",
+ childLayout->metaObject()->className(), qUtf16Printable(childLayout->objectName()));
return;
}
- l->setParent(this);
+ childLayout->setParent(this);
if (QWidget *mw = parentWidget()) {
- l->d_func()->reparentChildWidgets(mw);
+ childLayout->d_func()->reparentChildWidgets(mw);
}
}
@@ -751,7 +745,7 @@ bool QLayout::adoptLayout(QLayout *layout)
static bool layoutDebug()
{
static int checked_env = -1;
- if(checked_env == -1)
+ if (checked_env == -1)
checked_env = !!qEnvironmentVariableIntValue("QT_LAYOUT_DEBUG");
return checked_env;
@@ -779,7 +773,7 @@ void QLayoutPrivate::reparentChildWidgets(QWidget *mw)
w->metaObject()->className(), qUtf16Printable(w->objectName()));
}
#endif
- bool needShow = mwVisible && !(w->isHidden() && w->testAttribute(Qt::WA_WState_ExplicitShowHide));
+ bool needShow = mwVisible && !QWidgetPrivate::get(w)->isExplicitlyHidden();
if (pw != mw)
w->setParent(mw);
if (needShow)
@@ -835,9 +829,10 @@ bool QLayoutPrivate::checkLayout(QLayout *otherLayout) const
This function is called from \c addWidget() functions in
subclasses to add \a w as a managed widget of a layout.
- If \a w is already managed by a layout, this function will give a warning
- and remove \a w from that layout. This function must therefore be
- called before adding \a w to the layout's data structure.
+ If \a w is already managed by a layout, this function will produce
+ a warning, and remove \a w from that layout. This function must
+ therefore be called before adding \a w to the layout's data
+ structure.
*/
void QLayout::addChildWidget(QWidget *w)
{
@@ -864,7 +859,7 @@ void QLayout::addChildWidget(QWidget *w)
#endif
pw = nullptr;
}
- bool needShow = mw && mw->isVisible() && !(w->isHidden() && w->testAttribute(Qt::WA_WState_ExplicitShowHide));
+ bool needShow = mw && mw->isVisible() && !QWidgetPrivate::get(w)->isExplicitlyHidden();
if (!pw && mw)
w->setParent(mw);
w->setAttribute(Qt::WA_LaidOut);
@@ -1134,7 +1129,7 @@ QLayoutItem *QLayout::replaceWidget(QWidget *from, QWidget *to, Qt::FindChildOpt
\fn QLayoutItem *QLayout::itemAt(int index) const
Must be implemented in subclasses to return the layout item at \a
- index. If there is no such item, the function must return 0.
+ index. If there is no such item, the function must return \nullptr.
Items are numbered consecutively from 0. If an item is deleted, other items will be renumbered.
This function can be used to iterate over a layout. The following
@@ -1177,18 +1172,17 @@ QLayoutItem *QLayout::replaceWidget(QWidget *from, QWidget *to, Qt::FindChildOpt
Returns the index of \a widget, or -1 if \a widget is not found.
- The default implementation iterates over all items using itemAt()
+ The default implementation iterates over all items using itemAt().
*/
-int QLayout::indexOf(QWidget *widget) const
+int QLayout::indexOf(const QWidget *widget) const
{
- int i = 0;
- QLayoutItem *item = itemAt(i);
- while (item) {
- if (item->widget() == widget)
+ const int c = count();
+
+ for (int i = 0; i < c; ++i) {
+ if (itemAt(i)->widget() == widget)
return i;
- ++i;
- item = itemAt(i);
}
+
return -1;
}
@@ -1199,16 +1193,15 @@ int QLayout::indexOf(QWidget *widget) const
Returns the index of \a layoutItem, or -1 if \a layoutItem is not found.
*/
-int QLayout::indexOf(QLayoutItem *layoutItem) const
+int QLayout::indexOf(const QLayoutItem *layoutItem) const
{
- int i = 0;
- QLayoutItem *item = itemAt(i);
- while (item) {
- if (item == layoutItem)
+ const int c = count();
+
+ for (int i = 0; i < c; ++i) {
+ if (itemAt(i) == layoutItem)
return i;
- ++i;
- item = itemAt(i);
}
+
return -1;
}
@@ -1266,7 +1259,7 @@ QLayout::SizeConstraint QLayout::sizeConstraint() const
this layout is set to \a r, provided that this layout supports
setAlignment().
- The result is derived from sizeHint() and expanding(). It is never
+ The result is derived from sizeHint() and expandingDirections(). It is never
larger than \a r.
*/
QRect QLayout::alignmentRect(const QRect &r) const
@@ -1329,6 +1322,11 @@ QRect QLayout::alignmentRect(const QRect &r) const
*/
void QLayout::removeWidget(QWidget *widget)
{
+ if (Q_UNLIKELY(!widget)) {
+ qWarning("QLayout::removeWidget: Cannot remove a null widget.");
+ return;
+ }
+
int i = 0;
QLayoutItem *child;
while ((child = itemAt(i))) {