From b39df8bf92a530783144dbcf5cae939742ff2d23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20R=C3=B8dal?= Date: Fri, 13 Jan 2012 10:31:11 +0100 Subject: Made window orientation API more flexible. Previously we only had QWindow::setOrientation() which was a hint about the orientation the window's contents were rendered in. However, it's necessary to separate between the orientation corresponding to the window buffer layout and orientation of the contents. A game for example might typically want to use a landscape buffer even on a portrait device. Thus, we replace QWindow::orientation() with QWindow::reportContentOrientationChange() and QWindow::requestWindowOrientation(). Change-Id: I1f07362192daf36c45519cb05b43ac352f1945b5 Reviewed-by: Lars Knoll --- examples/opengl/paintedwindow/main.cpp | 9 ----- examples/opengl/paintedwindow/paintedwindow.cpp | 48 +++++++++++++------------ 2 files changed, 26 insertions(+), 31 deletions(-) (limited to 'examples/opengl') diff --git a/examples/opengl/paintedwindow/main.cpp b/examples/opengl/paintedwindow/main.cpp index a235646aac..6ca3c245ca 100644 --- a/examples/opengl/paintedwindow/main.cpp +++ b/examples/opengl/paintedwindow/main.cpp @@ -40,7 +40,6 @@ #include #include -#include #include "paintedwindow.h" @@ -48,15 +47,7 @@ int main(int argc, char **argv) { QGuiApplication app(argc, argv); - QScreen *screen = QGuiApplication::primaryScreen(); - - QRect screenGeometry = screen->availableGeometry(); - - QPoint center = screenGeometry.center(); - QRect windowRect(0, 0, 480, 640); - PaintedWindow window; - window.setGeometry(QRect(center - windowRect.center(), windowRect.size())); window.show(); app.exec(); diff --git a/examples/opengl/paintedwindow/paintedwindow.cpp b/examples/opengl/paintedwindow/paintedwindow.cpp index 51b6ec0ee0..bdb4eaf66c 100644 --- a/examples/opengl/paintedwindow/paintedwindow.cpp +++ b/examples/opengl/paintedwindow/paintedwindow.cpp @@ -70,13 +70,22 @@ PaintedWindow::PaintedWindow() m_animation->setEndValue(qreal(1)); m_animation->setDuration(500); - setOrientation(QGuiApplication::primaryScreen()->primaryOrientation()); + requestWindowOrientation(Qt::PortraitOrientation); + + QRect screenGeometry = screen()->availableGeometry(); + + QPoint center = screenGeometry.center(); + QRect windowRect = screen()->isLandscape(windowOrientation()) ? QRect(0, 0, 640, 480) : QRect(0, 0, 480, 640); + setGeometry(QRect(center - windowRect.center(), windowRect.size())); + m_rotation = 0; - m_targetOrientation = orientation(); - m_nextTargetOrientation = Qt::UnknownOrientation; + reportContentOrientationChange(screen()->orientation()); - connect(screen(), SIGNAL(currentOrientationChanged(Qt::ScreenOrientation)), this, SLOT(orientationChanged(Qt::ScreenOrientation))); + m_targetOrientation = contentOrientation(); + m_nextTargetOrientation = Qt::PrimaryOrientation; + + connect(screen(), SIGNAL(orientationChanged(Qt::ScreenOrientation)), this, SLOT(orientationChanged(Qt::ScreenOrientation))); connect(m_animation, SIGNAL(finished()), this, SLOT(rotationDone())); connect(this, SIGNAL(rotationChanged(qreal)), this, SLOT(paint())); } @@ -93,7 +102,7 @@ void PaintedWindow::exposeEvent(QExposeEvent *) void PaintedWindow::mousePressEvent(QMouseEvent *) { - Qt::ScreenOrientation o = orientation(); + Qt::ScreenOrientation o = contentOrientation(); switch (o) { case Qt::LandscapeOrientation: orientationChanged(Qt::PortraitOrientation); @@ -116,7 +125,7 @@ void PaintedWindow::mousePressEvent(QMouseEvent *) void PaintedWindow::orientationChanged(Qt::ScreenOrientation newOrientation) { - if (orientation() == newOrientation) + if (contentOrientation() == newOrientation) return; if (m_animation->state() == QAbstractAnimation::Running) { @@ -124,8 +133,6 @@ void PaintedWindow::orientationChanged(Qt::ScreenOrientation newOrientation) return; } - Qt::ScreenOrientation screenOrientation = screen()->primaryOrientation(); - QRect rect(0, 0, width(), height()); m_prevImage = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied); @@ -135,16 +142,16 @@ void PaintedWindow::orientationChanged(Qt::ScreenOrientation newOrientation) QPainter p; p.begin(&m_prevImage); - p.setTransform(QScreen::transformBetween(orientation(), screenOrientation, rect)); - paint(&p, QScreen::mapBetween(orientation(), screenOrientation, rect)); + p.setTransform(screen()->transformBetween(contentOrientation(), windowOrientation(), rect)); + paint(&p, screen()->mapBetween(contentOrientation(), windowOrientation(), rect)); p.end(); p.begin(&m_nextImage); - p.setTransform(QScreen::transformBetween(newOrientation, screenOrientation, rect)); - paint(&p, QScreen::mapBetween(newOrientation, screenOrientation, rect)); + p.setTransform(screen()->transformBetween(newOrientation, windowOrientation(), rect)); + paint(&p, screen()->mapBetween(newOrientation, windowOrientation(), rect)); p.end(); - m_deltaRotation = QScreen::angleBetween(newOrientation, orientation()); + m_deltaRotation = screen()->angleBetween(newOrientation, contentOrientation()); if (m_deltaRotation > 180) m_deltaRotation = 180 - m_deltaRotation; @@ -154,11 +161,11 @@ void PaintedWindow::orientationChanged(Qt::ScreenOrientation newOrientation) void PaintedWindow::rotationDone() { - setOrientation(m_targetOrientation); - if (m_nextTargetOrientation != Qt::UnknownOrientation) { + reportContentOrientationChange(m_targetOrientation); + if (m_nextTargetOrientation != Qt::PrimaryOrientation) { Q_ASSERT(m_animation->state() != QAbstractAnimation::Running); orientationChanged(m_nextTargetOrientation); - m_nextTargetOrientation = Qt::UnknownOrientation; + m_nextTargetOrientation = Qt::PrimaryOrientation; } } @@ -174,9 +181,6 @@ void PaintedWindow::paint() { m_context->makeCurrent(this); - Qt::ScreenOrientation screenOrientation = screen()->primaryOrientation(); - Qt::ScreenOrientation appOrientation = orientation(); - QRect rect(0, 0, width(), height()); QOpenGLPaintDevice device(size()); @@ -189,7 +193,7 @@ void PaintedWindow::paint() painter.setCompositionMode(QPainter::CompositionMode_SourceOver); painter.fillPath(path, Qt::blue); - if (orientation() != m_targetOrientation) { + if (contentOrientation() != m_targetOrientation) { painter.setRenderHint(QPainter::SmoothPixmapTransform); painter.save(); painter.translate(width() / 2, height() / 2); @@ -203,9 +207,9 @@ void PaintedWindow::paint() painter.setOpacity(m_rotation); painter.drawImage(0, 0, m_nextImage); } else { - QRect mapped = QScreen::mapBetween(appOrientation, screenOrientation, rect); + QRect mapped = screen()->mapBetween(contentOrientation(), windowOrientation(), rect); - painter.setTransform(QScreen::transformBetween(appOrientation, screenOrientation, rect)); + painter.setTransform(screen()->transformBetween(contentOrientation(), windowOrientation(), rect)); paint(&painter, mapped); painter.end(); } -- cgit v1.2.3