diff options
-rw-r--r-- | src/imports/controls/Popup.qml | 6 | ||||
-rw-r--r-- | src/imports/controls/material/Popup.qml | 6 | ||||
-rw-r--r-- | src/imports/controls/universal/Popup.qml | 6 | ||||
-rw-r--r-- | src/templates/qquickpopup.cpp | 20 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_popup.qml | 60 |
5 files changed, 88 insertions, 10 deletions
diff --git a/src/imports/controls/Popup.qml b/src/imports/controls/Popup.qml index 60ea326a..309c317b 100644 --- a/src/imports/controls/Popup.qml +++ b/src/imports/controls/Popup.qml @@ -40,8 +40,10 @@ import Qt.labs.templates 1.0 as T T.Popup { id: control - implicitWidth: Math.max(background ? background.implicitWidth : 0, contentWidth + leftPadding + rightPadding) - implicitHeight: Math.max(background ? background.implicitHeight : 0, contentHeight + topPadding + bottomPadding) + implicitWidth: Math.max(background ? background.implicitWidth : 0, + contentWidth > 0 ? contentWidth + leftPadding + rightPadding : 0) + implicitHeight: Math.max(background ? background.implicitHeight : 0, + contentWidth > 0 ? contentHeight + topPadding + bottomPadding : 0) contentWidth: contentItem.implicitWidth || (contentChildren.length === 1 ? contentChildren[0].implicitWidth : 0) contentHeight: contentItem.implicitHeight || (contentChildren.length === 1 ? contentChildren[0].implicitHeight : 0) diff --git a/src/imports/controls/material/Popup.qml b/src/imports/controls/material/Popup.qml index 8fa9f7f7..d882840a 100644 --- a/src/imports/controls/material/Popup.qml +++ b/src/imports/controls/material/Popup.qml @@ -42,8 +42,10 @@ import Qt.labs.controls.material 1.0 T.Popup { id: control - implicitWidth: Math.max(background ? background.implicitWidth : 0, contentWidth + leftPadding + rightPadding) - implicitHeight: Math.max(background ? background.implicitHeight : 0, contentHeight + topPadding + bottomPadding) + implicitWidth: Math.max(background ? background.implicitWidth : 0, + contentWidth > 0 ? contentWidth + leftPadding + rightPadding : 0) + implicitHeight: Math.max(background ? background.implicitHeight : 0, + contentWidth > 0 ? contentHeight + topPadding + bottomPadding : 0) contentWidth: contentItem.implicitWidth || (contentChildren.length === 1 ? contentChildren[0].implicitWidth : 0) contentHeight: contentItem.implicitHeight || (contentChildren.length === 1 ? contentChildren[0].implicitHeight : 0) diff --git a/src/imports/controls/universal/Popup.qml b/src/imports/controls/universal/Popup.qml index d26dd02d..7ab79545 100644 --- a/src/imports/controls/universal/Popup.qml +++ b/src/imports/controls/universal/Popup.qml @@ -41,8 +41,10 @@ import Qt.labs.controls.universal 1.0 T.Popup { id: control - implicitWidth: Math.max(background ? background.implicitWidth : 0, contentWidth + leftPadding + rightPadding) - implicitHeight: Math.max(background ? background.implicitHeight : 0, contentHeight + topPadding + bottomPadding) + implicitWidth: Math.max(background ? background.implicitWidth : 0, + contentWidth > 0 ? contentWidth + leftPadding + rightPadding : 0) + implicitHeight: Math.max(background ? background.implicitHeight : 0, + contentWidth > 0 ? contentHeight + topPadding + bottomPadding : 0) contentWidth: contentItem.implicitWidth || (contentChildren.length === 1 ? contentChildren[0].implicitWidth : 0) contentHeight: contentItem.implicitHeight || (contentChildren.length === 1 ? contentChildren[0].implicitHeight : 0) diff --git a/src/templates/qquickpopup.cpp b/src/templates/qquickpopup.cpp index c298bd9c..5589b095 100644 --- a/src/templates/qquickpopup.cpp +++ b/src/templates/qquickpopup.cpp @@ -496,10 +496,10 @@ void QQuickPopupPositioner::itemParentChanged(QQuickItem *, QQuickItem *parent) addAncestorListeners(parent); } -void QQuickPopupPositioner::itemChildRemoved(QQuickItem *, QQuickItem *child) +void QQuickPopupPositioner::itemChildRemoved(QQuickItem *item, QQuickItem *child) { if (isAncestor(child)) - removeAncestorListeners(child); + removeAncestorListeners(item); } void QQuickPopupPositioner::itemDestroyed(QQuickItem *item) @@ -527,7 +527,19 @@ void QQuickPopupPrivate::reposition() QQuickWindow *window = q->window(); if (window) { - const QRectF bounds = QRectF(0, 0, window->width(), window->height()).marginsRemoved(getMargins()); + const QMarginsF margins = getMargins(); + const QRectF bounds = QRectF(0, 0, window->width(), window->height()).marginsRemoved(margins); + + // push inside the margins + if (margins.top() > 0 && rect.top() < bounds.top()) + rect.moveTop(margins.top()); + if (margins.bottom() > 0 && rect.bottom() > bounds.bottom()) + rect.moveBottom(bounds.bottom()); + if (margins.left() > 0 && rect.left() < bounds.left()) + rect.moveLeft(margins.left()); + if (margins.right() > 0 && rect.right() > bounds.right()) + rect.moveRight(bounds.right()); + if (rect.top() < bounds.top() || rect.bottom() > bounds.bottom()) { // if the popup doesn't fit inside the window, try flipping it around (below <-> above) const QRectF flipped = parentItem->mapRectToScene(QRectF(x, parentItem->height() - y - rect.height(), rect.width(), rect.height())); @@ -588,7 +600,7 @@ bool QQuickPopupPositioner::isAncestor(QQuickItem *item) const if (!m_parentItem) return false; - QQuickItem *parent = m_parentItem->parentItem(); + QQuickItem *parent = m_parentItem; while (parent) { if (parent == item) return true; diff --git a/tests/auto/controls/data/tst_popup.qml b/tests/auto/controls/data/tst_popup.qml index 4eba8dca..d9bef42f 100644 --- a/tests/auto/controls/data/tst_popup.qml +++ b/tests/auto/controls/data/tst_popup.qml @@ -287,6 +287,66 @@ TestCase { control.destroy() } + function test_margins() { + var control = popupControl.createObject(testCase, {width: 100, height: 100}) + verify(control) + + control.open() + verify(control.visible) + + control.margins = 10 + compare(control.margins, 10) + compare(control.topMargin, 10) + compare(control.leftMargin, 10) + compare(control.rightMargin, 10) + compare(control.bottomMargin, 10) + compare(control.contentItem.parent.x, 10) + compare(control.contentItem.parent.y, 10) + + control.topMargin = 20 + compare(control.margins, 10) + compare(control.topMargin, 20) + compare(control.leftMargin, 10) + compare(control.rightMargin, 10) + compare(control.bottomMargin, 10) + compare(control.contentItem.parent.x, 10) + compare(control.contentItem.parent.y, 20) + + control.leftMargin = 20 + compare(control.margins, 10) + compare(control.topMargin, 20) + compare(control.leftMargin, 20) + compare(control.rightMargin, 10) + compare(control.bottomMargin, 10) + compare(control.contentItem.parent.x, 20) + compare(control.contentItem.parent.y, 20) + + control.x = testCase.width + control.y = testCase.height + compare(control.contentItem.parent.x, testCase.width - control.width - 10) + compare(control.contentItem.parent.y, testCase.height - control.height - 10) + + control.rightMargin = 20 + compare(control.margins, 10) + compare(control.topMargin, 20) + compare(control.leftMargin, 20) + compare(control.rightMargin, 20) + compare(control.bottomMargin, 10) + compare(control.contentItem.parent.x, testCase.width - control.width - 20) + compare(control.contentItem.parent.y, testCase.height - control.height - 10) + + control.bottomMargin = 20 + compare(control.margins, 10) + compare(control.topMargin, 20) + compare(control.leftMargin, 20) + compare(control.rightMargin, 20) + compare(control.bottomMargin, 20) + compare(control.contentItem.parent.x, testCase.width - control.width - 20) + compare(control.contentItem.parent.y, testCase.height - control.height - 20) + + control.destroy() + } + function test_background() { var control = popupTemplate.createObject(testCase) verify(control) |