diff options
-rw-r--r-- | src/quicktemplates2/qquickoverlay.cpp | 48 | ||||
-rw-r--r-- | src/quicktemplates2/qquickoverlay_p_p.h | 2 | ||||
-rw-r--r-- | src/quicktemplates2/qquickpopuppositioner.cpp | 16 | ||||
-rw-r--r-- | tests/auto/popup/data/orientation.qml | 76 | ||||
-rw-r--r-- | tests/auto/popup/tst_popup.cpp | 32 |
5 files changed, 162 insertions, 12 deletions
diff --git a/src/quicktemplates2/qquickoverlay.cpp b/src/quicktemplates2/qquickoverlay.cpp index 8de37d1c..a1d82de0 100644 --- a/src/quicktemplates2/qquickoverlay.cpp +++ b/src/quicktemplates2/qquickoverlay.cpp @@ -169,10 +169,9 @@ QVector<QQuickDrawer *> QQuickOverlayPrivate::stackingOrderDrawers() const return sorted; } -void QQuickOverlayPrivate::itemGeometryChanged(QQuickItem *item, QQuickGeometryChange, const QRectF &) +void QQuickOverlayPrivate::itemGeometryChanged(QQuickItem *, QQuickGeometryChange, const QRectF &) { - Q_Q(QQuickOverlay); - q->setSize(QSizeF(item->width(), item->height())); + updateGeometry(); } QQuickOverlayPrivate::QQuickOverlayPrivate() @@ -342,6 +341,43 @@ void QQuickOverlayPrivate::setMouseGrabberPopup(QQuickPopup *popup) mouseGrabberPopup = popup; } +void QQuickOverlayPrivate::updateGeometry() +{ + Q_Q(QQuickOverlay); + if (!window) + return; + + QPointF pos; + QSizeF size = window->size(); + qreal rotation = 0; + + switch (window->contentOrientation()) { + case Qt::PrimaryOrientation: + case Qt::PortraitOrientation: + size = window->size(); + break; + case Qt::LandscapeOrientation: + rotation = 90; + pos = QPointF((size.width() - size.height()) / 2, -(size.width() - size.height()) / 2); + size.transpose(); + break; + case Qt::InvertedPortraitOrientation: + rotation = 180; + break; + case Qt::InvertedLandscapeOrientation: + rotation = 270; + pos = QPointF((size.width() - size.height()) / 2, -(size.width() - size.height()) / 2); + size.transpose(); + break; + default: + break; + } + + q->setSize(size); + q->setPosition(pos); + q->setRotation(rotation); +} + QQuickOverlay::QQuickOverlay(QQuickItem *parent) : QQuickItem(*(new QQuickOverlayPrivate), parent) { @@ -352,10 +388,12 @@ QQuickOverlay::QQuickOverlay(QQuickItem *parent) setVisible(false); if (parent) { - setSize(QSizeF(parent->width(), parent->height())); + d->updateGeometry(); QQuickItemPrivate::get(parent)->addItemChangeListener(d, QQuickItemPrivate::Geometry); - if (QQuickWindow *window = parent->window()) + if (QQuickWindow *window = parent->window()) { window->installEventFilter(this); + QObjectPrivate::connect(window, &QWindow::contentOrientationChanged, d, &QQuickOverlayPrivate::updateGeometry); + } } } diff --git a/src/quicktemplates2/qquickoverlay_p_p.h b/src/quicktemplates2/qquickoverlay_p_p.h index 5008aa09..dfedf7c0 100644 --- a/src/quicktemplates2/qquickoverlay_p_p.h +++ b/src/quicktemplates2/qquickoverlay_p_p.h @@ -96,6 +96,8 @@ public: void itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF &diff) override; + void updateGeometry(); + QQmlComponent *modal; QQmlComponent *modeless; QVector<QQuickPopup *> allPopups; diff --git a/src/quicktemplates2/qquickpopuppositioner.cpp b/src/quicktemplates2/qquickpopuppositioner.cpp index a8e217f1..95d07690 100644 --- a/src/quicktemplates2/qquickpopuppositioner.cpp +++ b/src/quicktemplates2/qquickpopuppositioner.cpp @@ -116,25 +116,27 @@ void QQuickPopupPositioner::reposition() !p->hasWidth && iw > 0 ? iw : w, !p->hasHeight && ih > 0 ? ih : h); if (m_parentItem) { - rect = m_parentItem->mapRectToScene(rect); + rect.moveTopLeft(m_parentItem->mapToItem(popupItem->parentItem(), rect.topLeft())); if (p->window) { const QMarginsF margins = p->getMargins(); - const QRectF bounds(qMax<qreal>(0.0, margins.left()), - qMax<qreal>(0.0, margins.top()), - p->window->width() - qMax<qreal>(0.0, margins.left()) - qMax<qreal>(0.0, margins.right()), - p->window->height() - qMax<qreal>(0.0, margins.top()) - qMax<qreal>(0.0, margins.bottom())); + QRectF bounds(qMax<qreal>(0.0, margins.left()), + qMax<qreal>(0.0, margins.top()), + p->window->width() - qMax<qreal>(0.0, margins.left()) - qMax<qreal>(0.0, margins.right()), + p->window->height() - qMax<qreal>(0.0, margins.top()) - qMax<qreal>(0.0, margins.bottom())); + if (p->window->contentOrientation() == Qt::LandscapeOrientation || p->window->contentOrientation() == Qt::InvertedLandscapeOrientation) + bounds = bounds.transposed(); // if the popup doesn't fit horizontally inside the window, try flipping it around (left <-> right) if (p->allowHorizontalFlip && (rect.left() < bounds.left() || rect.right() > bounds.right())) { - const QRectF flipped = m_parentItem->mapRectToScene(QRectF(m_parentItem->width() - p->x - rect.width(), p->y, rect.width(), rect.height())); + const QRectF flipped(m_parentItem->width() - rect.x() - rect.width(), rect.y(), rect.width(), rect.height()); if (flipped.intersected(bounds).width() > rect.intersected(bounds).width()) rect.moveLeft(flipped.left()); } // if the popup doesn't fit vertically inside the window, try flipping it around (above <-> below) if (p->allowVerticalFlip && (rect.top() < bounds.top() || rect.bottom() > bounds.bottom())) { - const QRectF flipped = m_parentItem->mapRectToScene(QRectF(p->x, m_parentItem->height() - p->y - rect.height(), rect.width(), rect.height())); + const QRectF flipped(rect.x(), m_parentItem->height() - rect.y() - rect.height(), rect.width(), rect.height()); if (flipped.intersected(bounds).height() > rect.intersected(bounds).height()) rect.moveTop(flipped.top()); } diff --git a/tests/auto/popup/data/orientation.qml b/tests/auto/popup/data/orientation.qml new file mode 100644 index 00000000..cbf69b2b --- /dev/null +++ b/tests/auto/popup/data/orientation.qml @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.6 +import QtQuick.Window 2.2 +import QtQuick.Controls 2.0 + +Window { + width: 600 + height: 300 + + property alias popup: popup + + Rectangle { + width: 60 + height: 30 + anchors.centerIn: parent + border.width: 1 + + Popup { + id: popup + x: parent.width + y: parent.height + width: 30 + height: 60 + visible: true + } + } +} diff --git a/tests/auto/popup/tst_popup.cpp b/tests/auto/popup/tst_popup.cpp index 45d52517..f036f0c9 100644 --- a/tests/auto/popup/tst_popup.cpp +++ b/tests/auto/popup/tst_popup.cpp @@ -79,6 +79,8 @@ private slots: void cursorShape(); void componentComplete(); void closeOnEscapeWithNestedPopups(); + void orientation_data(); + void orientation(); }; void tst_popup::initTestCase() @@ -940,6 +942,36 @@ void tst_popup::closeOnEscapeWithNestedPopups() QCOMPARE(stackView->depth(), 1); } +void tst_popup::orientation_data() +{ + QTest::addColumn<Qt::ScreenOrientation>("orientation"); + QTest::addColumn<QPointF>("position"); + + QTest::newRow("Portrait") << Qt::PortraitOrientation << QPointF(330, 165); + QTest::newRow("Landscape") << Qt::LandscapeOrientation << QPointF(165, 270); + QTest::newRow("InvertedPortrait") << Qt::InvertedPortraitOrientation << QPointF(270, 135); + QTest::newRow("InvertedLandscape") << Qt::InvertedLandscapeOrientation << QPointF(135, 330); +} + +void tst_popup::orientation() +{ + QFETCH(Qt::ScreenOrientation, orientation); + QFETCH(QPointF, position); + + QQuickApplicationHelper helper(this, "orientation.qml"); + + QQuickWindow *window = helper.window; + window->reportContentOrientationChange(orientation); + window->show(); + QVERIFY(QTest::qWaitForWindowActive(window)); + + QQuickPopup *popup = window->property("popup").value<QQuickPopup*>(); + QVERIFY(popup); + popup->open(); + + QCOMPARE(popup->popupItem()->position(), position); +} + QTEST_MAIN(tst_popup) #include "tst_popup.moc" |