From 2ee182c127967d507921b0c59b1c12c3cc260abc Mon Sep 17 00:00:00 2001 From: Jan Arve Saether <jan-arve.saether@digia.com> Date: Thu, 24 Jan 2013 11:40:09 +0100 Subject: [PATCH] Do not include hidden items in layouts. Note that hidden items also excludes their spacing. So there is no double-spacing due to this. However, items with size of 0 (duh) will have double-spacing. (This is to avoid sudden "jumps" in the layout if the item changes size from 1 to 0) Change-Id: Ib170dc8c575cbd8bbe8bb28de7fa64ae6ab5062d Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com> --- src/qtdesktop/qquicklinearlayout.cpp | 79 ++-- src/qtdesktop/qquicklinearlayout_p.h | 1 + tests/manual/Layout.qml | 682 ++++++++++++++------------- 3 files changed, 395 insertions(+), 367 deletions(-) diff --git a/src/qtdesktop/qquicklinearlayout.cpp b/src/qtdesktop/qquicklinearlayout.cpp index da18ae0b0..29fb026f4 100644 --- a/src/qtdesktop/qquicklinearlayout.cpp +++ b/src/qtdesktop/qquicklinearlayout.cpp @@ -151,6 +151,7 @@ void QQuickComponentsLinearLayout::insertLayoutItem(QQuickItem *item) invalidate(); QObject::connect(item, SIGNAL(destroyed()), this, SLOT(onItemDestroyed())); + QObject::connect(item, SIGNAL(visibleChanged()), this, SLOT(onItemVisibleChanged())); } void QQuickComponentsLinearLayout::removeLayoutItem(QQuickItem *item) @@ -160,6 +161,12 @@ void QQuickComponentsLinearLayout::removeLayoutItem(QQuickItem *item) invalidate(); QObject::disconnect(item, SIGNAL(destroyed()), this, SLOT(onItemDestroyed())); + QObject::disconnect(item, SIGNAL(visibleChanged()), this, SLOT(onItemVisibleChanged())); +} + +void QQuickComponentsLinearLayout::onItemVisibleChanged() +{ + invalidate(); } void QQuickComponentsLinearLayout::onItemDestroyed() @@ -194,54 +201,54 @@ void QQuickComponentsLinearLayout::reconfigureLayout() QQuickComponentsLayoutInfo data; - if (m_orientation == Horizontal) { - data.sizeHint = item->implicitWidth(); - data.minimumSize = info->minimumWidth(); - data.maximumSize = info->maximumWidth(); - data.expansive = (info->horizontalSizePolicy() == QQuickComponentsLayout::Expanding); - data.stretch = info->horizontalSizePolicy() == Expanding ? 1.0 : 0; - } else { - data.sizeHint = item->implicitHeight(); - data.minimumSize = info->minimumHeight(); - data.maximumSize = info->maximumHeight(); - data.expansive = (info->verticalSizePolicy() == QQuickComponentsLayout::Expanding); - data.stretch = info->verticalSizePolicy() == Expanding ? 1.0 : 0; + if (item->isVisible()) { + if (m_orientation == Horizontal) { + data.sizeHint = item->implicitWidth(); + data.minimumSize = info->minimumWidth(); + data.maximumSize = info->maximumWidth(); + data.expansive = (info->horizontalSizePolicy() == QQuickComponentsLayout::Expanding); + data.stretch = info->horizontalSizePolicy() == Expanding ? 1.0 : 0; + } else { + data.sizeHint = item->implicitHeight(); + data.minimumSize = info->minimumHeight(); + data.maximumSize = info->maximumHeight(); + data.expansive = (info->verticalSizePolicy() == QQuickComponentsLayout::Expanding); + data.stretch = info->verticalSizePolicy() == Expanding ? 1.0 : 0; + } + + itemData.append(data); + // sum + totalSizeHint += data.sizeHint; + totalMinimumSize += data.minimumSize; + totalMaximumSize += data.maximumSize; + + // don't count last spacing + if (i < count - 1) + totalSpacing += data.spacing + m_spacing; } - - itemData.append(data); - - // sum - totalSizeHint += data.sizeHint; - totalMinimumSize += data.minimumSize; - totalMaximumSize += data.maximumSize; - - // don't count last spacing - if (i < count - 1) - totalSpacing += data.spacing + m_spacing; } - if (m_orientation == Horizontal) { - qDeclarativeLayoutCalculate(itemData, 0, count, 0, width(), m_spacing); + qreal extent = m_orientation == Horizontal ? width() : height(); + qDeclarativeLayoutCalculate(itemData, 0, itemData.count(), 0, extent, m_spacing); - for (int i = 0; i < count; i++) { - QQuickItem *item = m_items.at(i); - const QQuickComponentsLayoutInfo &data = itemData.at(i); + int i = 0; + int id = 0; + while (i < count) { + QQuickItem *item = m_items.at(i++); + if (!item->isVisible()) + continue; + const QQuickComponentsLayoutInfo &data = itemData.at(id); + if (m_orientation == Horizontal) { item->setX(data.pos); item->setY(height()/2 - item->height()/2); item->setWidth(data.size); - } - } else { - qDeclarativeLayoutCalculate(itemData, 0, count, 0, height(), m_spacing); - - for (int i = 0; i < count; i++) { - QQuickItem *item = m_items.at(i); - const QQuickComponentsLayoutInfo &data = itemData.at(i); - + } else { item->setY(data.pos); item->setX(width()/2 - item->width()/2); item->setHeight(data.size); } + ++id; } // propagate hints to upper levels diff --git a/src/qtdesktop/qquicklinearlayout_p.h b/src/qtdesktop/qquicklinearlayout_p.h index 74dc7bcdf..965c55e6d 100644 --- a/src/qtdesktop/qquicklinearlayout_p.h +++ b/src/qtdesktop/qquicklinearlayout_p.h @@ -83,6 +83,7 @@ protected: void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry); protected slots: + void onItemVisibleChanged(); void onItemDestroyed(); private: diff --git a/tests/manual/Layout.qml b/tests/manual/Layout.qml index c3b1d6247..590008e67 100644 --- a/tests/manual/Layout.qml +++ b/tests/manual/Layout.qml @@ -48,444 +48,464 @@ Item { property real defaultWidth: 30 property real defaultHeight: 30 - TabFrame { - id:frame + Column { anchors.fill: parent + CheckBox { + id: ckHideGreen + text: "Hide green rectangles" + checked: false + width: parent.width + } + TabFrame { + id:frame + width: parent.width - Tab { - title: "Horizontal" - - Column { - spacing: 4 - - anchors { - top: parent.top - left: parent.left - right: parent.right - margins: 10 - } - - // [1] - RowLayout { - height: defaultHeight - anchors.left: parent.left - anchors.right: parent.right - - Rectangle { - color: "red" - height: parent.height - } - Rectangle { - color: "green" - height: parent.height - } - Rectangle { - color: "blue" - height: parent.height - } - } - - // [2] - RowLayout { - height: defaultHeight - anchors.left: parent.left - anchors.right: parent.right + Tab { + title: "Horizontal" - spacing: 5 + Column { + spacing: 4 - Rectangle { - color: "red" - height: parent.height - } - Rectangle { - color: "green" - height: parent.height - } - Item { - implicitWidth: 10 - } - Rectangle { - color: "blue" - height: parent.height + anchors { + top: parent.top + left: parent.left + right: parent.right + margins: 10 } - } - // [3] - RowLayout { - height: defaultHeight - anchors.left: parent.left - anchors.right: parent.right + // [1] + RowLayout { + height: defaultHeight + anchors.left: parent.left + anchors.right: parent.right - Rectangle { - color: "red" - height: parent.height - Layout.minimumWidth: 50 - Layout.maximumWidth: 100 - Layout.horizontalSizePolicy: Layout.Expanding - } - Rectangle { - color: "green" - height: parent.height - Layout.minimumWidth: 100 - Layout.maximumWidth: 200 - Layout.horizontalSizePolicy: Layout.Expanding - } - Rectangle { - color: "blue" - height: parent.height - Layout.minimumWidth: 200 - Layout.maximumWidth: 400 - Layout.horizontalSizePolicy: Layout.Expanding + Rectangle { + color: "red" + height: parent.height + } + Rectangle { + color: "green" + height: parent.height + visible: !ckHideGreen.checked + } + Rectangle { + color: "blue" + height: parent.height + } } - } - - // [4] - RowLayout { - spacing: 100 - height: defaultHeight - anchors.left: parent.left - anchors.right: parent.right - Rectangle { - color: "red" - height: parent.height - Layout.minimumWidth: 100 - Layout.horizontalSizePolicy: Layout.Expanding - } - Rectangle { - color: "green" - height: parent.height - Layout.minimumWidth: 200 - Layout.horizontalSizePolicy: Layout.Expanding - } - Rectangle { - color: "blue" - height: parent.height - Layout.minimumWidth: 300 - Layout.horizontalSizePolicy: Layout.Expanding - } - } + // [2] + RowLayout { + height: defaultHeight + anchors.left: parent.left + anchors.right: parent.right - // [5] - RowLayout { - height: defaultHeight - anchors.left: parent.left - anchors.right: parent.right + spacing: 5 - Rectangle { - color: "red" - height: parent.height - Layout.minimumWidth: 200 - Layout.maximumWidth: 500 - Layout.horizontalSizePolicy: Layout.Expanding + Rectangle { + color: "red" + height: parent.height + } + Rectangle { + color: "green" + height: parent.height + visible: !ckHideGreen.checked + } + Item { + implicitWidth: 10 + } + Rectangle { + color: "blue" + height: parent.height + } } - } - - // [6] - RowLayout { - spacing: 40 - height: defaultHeight - anchors.left: parent.left - anchors.right: parent.right + // [3] RowLayout { - spacing: 10 - height: parent.height + height: defaultHeight + anchors.left: parent.left + anchors.right: parent.right Rectangle { color: "red" height: parent.height + Layout.minimumWidth: 50 + Layout.maximumWidth: 100 + Layout.horizontalSizePolicy: Layout.Expanding + } + Rectangle { + color: "green" + height: parent.height + visible: !ckHideGreen.checked Layout.minimumWidth: 100 + Layout.maximumWidth: 200 Layout.horizontalSizePolicy: Layout.Expanding } Rectangle { color: "blue" height: parent.height Layout.minimumWidth: 200 + Layout.maximumWidth: 400 Layout.horizontalSizePolicy: Layout.Expanding } } + // [4] RowLayout { - spacing: 10 - height: parent.height + spacing: 100 + height: defaultHeight + anchors.left: parent.left + anchors.right: parent.right + Rectangle { + color: "red" + height: parent.height + Layout.minimumWidth: 100 + Layout.horizontalSizePolicy: Layout.Expanding + } Rectangle { color: "green" height: parent.height - Layout.maximumWidth: 300 + visible: !ckHideGreen.checked + Layout.minimumWidth: 200 Layout.horizontalSizePolicy: Layout.Expanding } Rectangle { - color: "red" + color: "blue" height: parent.height - Layout.minimumWidth: 40 - Layout.maximumWidth: 100 + Layout.minimumWidth: 300 Layout.horizontalSizePolicy: Layout.Expanding } } - } - } - } + // [5] + RowLayout { + height: defaultHeight + anchors.left: parent.left + anchors.right: parent.right - Tab { - title: "Vertical" + Rectangle { + color: "red" + height: parent.height + Layout.minimumWidth: 200 + Layout.maximumWidth: 500 + Layout.horizontalSizePolicy: Layout.Expanding + } + } - Row { - spacing: 4 + // [6] + RowLayout { + spacing: 40 + height: defaultHeight + anchors.left: parent.left + anchors.right: parent.right - anchors { - top: parent.top - left: parent.left - bottom: parent.bottom - margins: 10 - } + RowLayout { + spacing: 10 + height: parent.height - // [1] - ColumnLayout { - width: defaultWidth - anchors.top: parent.top - anchors.bottom: parent.bottom + Rectangle { + color: "red" + height: parent.height + Layout.minimumWidth: 100 + Layout.horizontalSizePolicy: Layout.Expanding + } + Rectangle { + color: "blue" + height: parent.height + Layout.minimumWidth: 200 + Layout.horizontalSizePolicy: Layout.Expanding + } + } - Rectangle { - color: "red" - width: parent.width - } - Rectangle { - color: "green" - width: parent.width - } - Rectangle { - color: "blue" - width: parent.width + RowLayout { + spacing: 10 + height: parent.height + + Rectangle { + color: "green" + height: parent.height + visible: !ckHideGreen.checked + Layout.maximumWidth: 300 + Layout.horizontalSizePolicy: Layout.Expanding + } + Rectangle { + color: "red" + height: parent.height + Layout.minimumWidth: 40 + Layout.maximumWidth: 100 + Layout.horizontalSizePolicy: Layout.Expanding + } + } } } + } - // [2] - ColumnLayout { - width: defaultWidth - anchors.top: parent.top - anchors.bottom: parent.bottom - spacing: 5 + Tab { + title: "Vertical" - Rectangle { - color: "red" - width: parent.width - } - Rectangle { - color: "green" - width: parent.width - } - Item { - implicitWidth: 10 - } - Rectangle { - color: "blue" - width: parent.width - } - } - - // [3] - ColumnLayout { - width: defaultWidth - anchors.top: parent.top - anchors.bottom: parent.bottom + Row { + spacing: 4 - Rectangle { - color: "red" - width: parent.width - Layout.minimumHeight: 50 - Layout.maximumHeight: 100 - Layout.verticalSizePolicy: Layout.Expanding - } - Rectangle { - color: "green" - width: parent.width - Layout.minimumHeight: 100 - Layout.maximumHeight: 200 - Layout.verticalSizePolicy: Layout.Expanding - } - Rectangle { - color: "blue" - width: parent.width - Layout.minimumHeight: 200 - Layout.maximumHeight: 400 - Layout.verticalSizePolicy: Layout.Expanding + anchors { + top: parent.top + left: parent.left + bottom: parent.bottom + margins: 10 } - } - // [4] - ColumnLayout { - spacing: 100 - width: defaultWidth - anchors.top: parent.top - anchors.bottom: parent.bottom + // [1] + ColumnLayout { + width: defaultWidth + anchors.top: parent.top + anchors.bottom: parent.bottom - Rectangle { - color: "red" - width: parent.width - Layout.minimumHeight: 100 - Layout.verticalSizePolicy: Layout.Expanding - } - Rectangle { - color: "green" - width: parent.width - Layout.minimumHeight: 200 - Layout.verticalSizePolicy: Layout.Expanding - } - Rectangle { - color: "blue" - width: parent.width - Layout.minimumHeight: 300 - Layout.verticalSizePolicy: Layout.Expanding + Rectangle { + color: "red" + width: parent.width + } + Rectangle { + color: "green" + width: parent.width + visible: !ckHideGreen.checked + } + Rectangle { + color: "blue" + width: parent.width + } } - } - // [5] - ColumnLayout { - width: defaultWidth - anchors.top: parent.top - anchors.bottom: parent.bottom + // [2] + ColumnLayout { + width: defaultWidth + anchors.top: parent.top + anchors.bottom: parent.bottom - Rectangle { - color: "red" - width: parent.width - Layout.minimumHeight: 200 - Layout.maximumHeight: 500 - Layout.verticalSizePolicy: Layout.Expanding - } - } + spacing: 5 - // [6] - ColumnLayout { - spacing: 40 - width: defaultWidth - anchors.top: parent.top - anchors.bottom: parent.bottom + Rectangle { + color: "red" + width: parent.width + } + Rectangle { + color: "green" + width: parent.width + visible: !ckHideGreen.checked + } + Item { + implicitWidth: 10 + } + Rectangle { + color: "blue" + width: parent.width + } + } + // [3] ColumnLayout { - spacing: 10 - width: parent.width + width: defaultWidth + anchors.top: parent.top + anchors.bottom: parent.bottom Rectangle { color: "red" width: parent.width + Layout.minimumHeight: 50 + Layout.maximumHeight: 100 + Layout.verticalSizePolicy: Layout.Expanding + } + Rectangle { + color: "green" + width: parent.width + visible: !ckHideGreen.checked Layout.minimumHeight: 100 + Layout.maximumHeight: 200 Layout.verticalSizePolicy: Layout.Expanding } Rectangle { color: "blue" width: parent.width Layout.minimumHeight: 200 + Layout.maximumHeight: 400 Layout.verticalSizePolicy: Layout.Expanding } } + // [4] ColumnLayout { - spacing: 10 - width: parent.width + spacing: 100 + width: defaultWidth + anchors.top: parent.top + anchors.bottom: parent.bottom + Rectangle { + color: "red" + width: parent.width + Layout.minimumHeight: 100 + Layout.verticalSizePolicy: Layout.Expanding + } Rectangle { color: "green" width: parent.width - Layout.maximumHeight: 300 + visible: !ckHideGreen.checked + Layout.minimumHeight: 200 Layout.verticalSizePolicy: Layout.Expanding } Rectangle { - color: "red" + color: "blue" width: parent.width - Layout.minimumHeight: 40 - Layout.maximumHeight: 100 + Layout.minimumHeight: 300 Layout.verticalSizePolicy: Layout.Expanding } } - } - } - } + // [5] + ColumnLayout { + width: defaultWidth + anchors.top: parent.top + anchors.bottom: parent.bottom - Tab { - title: "Horizontal and Vertical" + Rectangle { + color: "red" + width: parent.width + Layout.minimumHeight: 200 + Layout.maximumHeight: 500 + Layout.verticalSizePolicy: Layout.Expanding + } + } - ColumnLayout { - anchors.fill: parent + // [6] + ColumnLayout { + spacing: 40 + width: defaultWidth + anchors.top: parent.top + anchors.bottom: parent.bottom - // [1] - RowLayout { - height: defaultHeight - anchors.left: parent.left - anchors.right: parent.right + ColumnLayout { + spacing: 10 + width: parent.width - Layout.minimumHeight: 100 + Rectangle { + color: "red" + width: parent.width + Layout.minimumHeight: 100 + Layout.verticalSizePolicy: Layout.Expanding + } + Rectangle { + color: "blue" + width: parent.width + Layout.minimumHeight: 200 + Layout.verticalSizePolicy: Layout.Expanding + } + } - Rectangle { - color: "red" - height: parent.height - Layout.horizontalSizePolicy: Layout.Expanding - } - Rectangle { - color: "green" - height: parent.height - implicitWidth: 100 - } - Rectangle { - color: "blue" - height: parent.height - Layout.horizontalSizePolicy: Layout.Expanding + ColumnLayout { + spacing: 10 + width: parent.width + + Rectangle { + color: "green" + width: parent.width + visible: !ckHideGreen.checked + Layout.maximumHeight: 300 + Layout.verticalSizePolicy: Layout.Expanding + } + Rectangle { + color: "red" + width: parent.width + Layout.minimumHeight: 40 + Layout.maximumHeight: 100 + Layout.verticalSizePolicy: Layout.Expanding + } + } } } + } - // [2] - Rectangle { - color: "yellow" - height: parent.height - anchors.left: parent.left - anchors.right: parent.right - Layout.minimumHeight: 10 - Layout.maximumHeight: 30 - Layout.horizontalSizePolicy: Layout.Expanding - Layout.verticalSizePolicy: Layout.Expanding - } - // [3] - RowLayout { - height: defaultHeight - anchors.left: parent.left - anchors.right: parent.right + Tab { + title: "Horizontal and Vertical" + + ColumnLayout { + anchors.fill: parent + + // [1] + RowLayout { + height: defaultHeight + anchors.left: parent.left + anchors.right: parent.right + + Layout.minimumHeight: 100 + + Rectangle { + color: "red" + height: parent.height + Layout.horizontalSizePolicy: Layout.Expanding + } + Rectangle { + color: "green" + height: parent.height + visible: !ckHideGreen.checked + implicitWidth: 100 + } + Rectangle { + color: "blue" + height: parent.height + Layout.horizontalSizePolicy: Layout.Expanding + } + } + // [2] Rectangle { - color: "red" + color: "yellow" height: parent.height - Layout.maximumHeight: 200 + anchors.left: parent.left + anchors.right: parent.right + Layout.minimumHeight: 10 + Layout.maximumHeight: 30 Layout.horizontalSizePolicy: Layout.Expanding + Layout.verticalSizePolicy: Layout.Expanding } - Rectangle { - color: "blue" - height: parent.height - ColumnLayout { - anchors.fill: parent - spacing: 10 - opacity: 0.6 + // [3] + RowLayout { + height: defaultHeight + anchors.left: parent.left + anchors.right: parent.right - Rectangle { - color: "darkRed" - height: parent.height - anchors.left: parent.left - anchors.right: parent.right - Layout.minimumHeight: 100 - Layout.maximumHeight: 200 - Layout.verticalSizePolicy: Layout.Expanding - } + Rectangle { + color: "red" + height: parent.height + Layout.maximumHeight: 200 + Layout.horizontalSizePolicy: Layout.Expanding + } + Rectangle { + color: "blue" + height: parent.height - Rectangle { - color: "darkGreen" - height: parent.height - anchors.left: parent.left - anchors.right: parent.right - Layout.verticalSizePolicy: Layout.Expanding + ColumnLayout { + anchors.fill: parent + spacing: 10 + opacity: 0.6 + + Rectangle { + color: "darkRed" + height: parent.height + anchors.left: parent.left + anchors.right: parent.right + Layout.minimumHeight: 100 + Layout.maximumHeight: 200 + Layout.verticalSizePolicy: Layout.Expanding + } + + Rectangle { + color: "darkGreen" + height: parent.height + anchors.left: parent.left + anchors.right: parent.right + Layout.verticalSizePolicy: Layout.Expanding + } } } } -- GitLab