From f1e139b9ac02313f541ed6749f05e265f4c8bf13 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 26 Jul 2017 13:22:33 +0200 Subject: Popups: take Window::contentOrientation into account Task-number: QTBUG-62158 Change-Id: I0bcf5b02da6a3500e4324462d5f1249a6178c9fd Reviewed-by: Mitch Curtis --- src/quicktemplates2/qquickoverlay.cpp | 48 ++++++++++++++++++++++++--- src/quicktemplates2/qquickoverlay_p_p.h | 2 ++ src/quicktemplates2/qquickpopuppositioner.cpp | 16 +++++---- 3 files changed, 54 insertions(+), 12 deletions(-) (limited to 'src') 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 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 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(0.0, margins.left()), - qMax(0.0, margins.top()), - p->window->width() - qMax(0.0, margins.left()) - qMax(0.0, margins.right()), - p->window->height() - qMax(0.0, margins.top()) - qMax(0.0, margins.bottom())); + QRectF bounds(qMax(0.0, margins.left()), + qMax(0.0, margins.top()), + p->window->width() - qMax(0.0, margins.left()) - qMax(0.0, margins.right()), + p->window->height() - qMax(0.0, margins.top()) - qMax(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()); } -- cgit v1.2.3