From 16c2622fe7e8e43bdb6447399c816cd22c3c2f58 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Samuel=20R=C3=B8dal?= Date: Wed, 11 Jan 2012 13:16:24 +0100 Subject: [PATCH] Made it possible to report screen changes through QWindowSystemInterface. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This makes it possible for platform plugin independent code (such as generic plugins) to report changes to screen properties. An example would be an accelerometer plugin that reports orientation changes without knowing anything about the windowing system. Change-Id: I984984b6d064327772c264bc942269422451da37 Reviewed-by: Jørgen Lind --- src/gui/kernel/kernel.pri | 2 + src/gui/kernel/qguiapplication.cpp | 6 ++ src/gui/kernel/qplatformintegration_qpa.cpp | 10 ++-- src/gui/kernel/qplatformscreen_qpa.cpp | 9 +-- src/gui/kernel/qplatformscreen_qpa.h | 2 + src/gui/kernel/qplatformscreen_qpa_p.h | 74 +++++++++++++++++++++++++ src/gui/kernel/qscreen.cpp | 38 +++++-------- src/gui/kernel/qscreen.h | 1 + src/gui/kernel/qscreen_p.h | 80 +++++++++++++++++++++++++++ src/gui/kernel/qwindowsysteminterface_qpa.cpp | 16 +++--- src/gui/kernel/qwindowsysteminterface_qpa.h | 8 +-- src/gui/kernel/qwindowsysteminterface_qpa_p.h | 21 ++++--- tests/auto/gui/kernel/qscreen/tst_qscreen.cpp | 12 ++++ 13 files changed, 222 insertions(+), 57 deletions(-) create mode 100644 src/gui/kernel/qplatformscreen_qpa_p.h create mode 100644 src/gui/kernel/qscreen_p.h diff --git a/src/gui/kernel/kernel.pri b/src/gui/kernel/kernel.pri index 57ed3a9..bf552c9 100644 --- a/src/gui/kernel/kernel.pri +++ b/src/gui/kernel/kernel.pri @@ -13,6 +13,7 @@ HEADERS += \ kernel/qplatformintegration_qpa.h \ kernel/qplatformdrag_qpa.h \ kernel/qplatformscreen_qpa.h \ + kernel/qplatformscreen_qpa_p.h \ kernel/qplatforminputcontext_qpa.h \ kernel/qplatformintegrationfactory_qpa_p.h \ kernel/qplatformintegrationplugin_qpa.h \ @@ -50,6 +51,7 @@ HEADERS += \ kernel/qsessionmanager.h \ kernel/qwindowdefs.h \ kernel/qscreen.h \ + kernel/qscreen_p.h \ kernel/qstylehints.h \ kernel/qtouchdevice.h \ kernel/qtouchdevice_p.h diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 96b9222..c674573 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -57,6 +57,7 @@ #include #include #include +#include #include #include @@ -1021,6 +1022,8 @@ void QGuiApplicationPrivate::reportScreenOrientationChange(QWindowSystemInterfac return; QScreen *s = e->screen.data(); + s->d_func()->currentOrientation = e->orientation; + emit s->currentOrientationChanged(s->currentOrientation()); QScreenOrientationChangeEvent event(s, s->currentOrientation()); @@ -1037,6 +1040,7 @@ void QGuiApplicationPrivate::reportGeometryChange(QWindowSystemInterfacePrivate: return; QScreen *s = e->screen.data(); + s->d_func()->geometry = e->geometry; emit s->sizeChanged(s->size()); emit s->geometryChanged(s->geometry()); @@ -1058,6 +1062,7 @@ void QGuiApplicationPrivate::reportAvailableGeometryChange( return; QScreen *s = e->screen.data(); + s->d_func()->availableGeometry = e->availableGeometry; emit s->availableSizeChanged(s->availableSize()); emit s->availableGeometryChanged(s->availableGeometry()); @@ -1073,6 +1078,7 @@ void QGuiApplicationPrivate::reportLogicalDotsPerInchChange(QWindowSystemInterfa return; QScreen *s = e->screen.data(); + s->d_func()->logicalDpi = QDpi(e->dpiX, e->dpiY); emit s->logicalDotsPerInchXChanged(s->logicalDotsPerInchX()); emit s->logicalDotsPerInchYChanged(s->logicalDotsPerInchY()); diff --git a/src/gui/kernel/qplatformintegration_qpa.cpp b/src/gui/kernel/qplatformintegration_qpa.cpp index b35af1e..23ecf3a 100644 --- a/src/gui/kernel/qplatformintegration_qpa.cpp +++ b/src/gui/kernel/qplatformintegration_qpa.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include QT_BEGIN_NAMESPACE @@ -255,11 +256,10 @@ QVariant QPlatformIntegration::styleHint(StyleHint hint) const */ void QPlatformIntegration::screenAdded(QPlatformScreen *ps) { - QScreen *screen = ps ? ps->screen() : 0; - if (screen && !QGuiApplicationPrivate::screen_list.contains(screen)) { - QGuiApplicationPrivate::screen_list << screen; - emit qGuiApp->screenAdded(screen); - } + QScreen *screen = new QScreen(ps); + ps->d_func()->screen = screen; + QGuiApplicationPrivate::screen_list << screen; + emit qGuiApp->screenAdded(screen); } class QPlatformTheme *QPlatformIntegration::platformTheme() const diff --git a/src/gui/kernel/qplatformscreen_qpa.cpp b/src/gui/kernel/qplatformscreen_qpa.cpp index 7b9e73f..26c685f 100644 --- a/src/gui/kernel/qplatformscreen_qpa.cpp +++ b/src/gui/kernel/qplatformscreen_qpa.cpp @@ -42,23 +42,18 @@ #include "qplatformscreen_qpa.h" #include #include +#include #include #include #include QT_BEGIN_NAMESPACE -class QPlatformScreenPrivate -{ -public: - QScreen *screen; -}; - QPlatformScreen::QPlatformScreen() : d_ptr(new QPlatformScreenPrivate) { Q_D(QPlatformScreen); - d->screen = new QScreen(this); + d->screen = 0; } QPlatformScreen::~QPlatformScreen() diff --git a/src/gui/kernel/qplatformscreen_qpa.h b/src/gui/kernel/qplatformscreen_qpa.h index 492c5c9..586a292 100644 --- a/src/gui/kernel/qplatformscreen_qpa.h +++ b/src/gui/kernel/qplatformscreen_qpa.h @@ -120,6 +120,8 @@ protected: private: Q_DISABLE_COPY(QPlatformScreen) + + friend class QPlatformIntegration; }; QT_END_NAMESPACE diff --git a/src/gui/kernel/qplatformscreen_qpa_p.h b/src/gui/kernel/qplatformscreen_qpa_p.h new file mode 100644 index 0000000..67c222b --- /dev/null +++ b/src/gui/kernel/qplatformscreen_qpa_p.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtGui module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QPLATFORMSCREEN_QPA_P_H +#define QPLATFORMSCREEN_QPA_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Gui) + +class QScreen; + +class QPlatformScreenPrivate +{ +public: + QScreen *screen; +}; + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif // QPLATFORMSCREEN_QPA_P_H diff --git a/src/gui/kernel/qscreen.cpp b/src/gui/kernel/qscreen.cpp index 1089c84..a1ed301 100644 --- a/src/gui/kernel/qscreen.cpp +++ b/src/gui/kernel/qscreen.cpp @@ -40,23 +40,13 @@ ****************************************************************************/ #include "qscreen.h" +#include "qscreen_p.h" #include "qplatformscreen_qpa.h" #include QT_BEGIN_NAMESPACE -class QScreenPrivate : public QObjectPrivate -{ -public: - QScreenPrivate(QPlatformScreen *screen) - : platformScreen(screen) - { - } - - QPlatformScreen *platformScreen; -}; - /*! \class QScreen \brief The QScreen class is used to query screen properties. @@ -119,7 +109,7 @@ int QScreen::depth() const QSize QScreen::size() const { Q_D(const QScreen); - return d->platformScreen->geometry().size(); + return d->geometry.size(); } /*! @@ -184,7 +174,7 @@ qreal QScreen::physicalDotsPerInch() const qreal QScreen::logicalDotsPerInchX() const { Q_D(const QScreen); - return d->platformScreen->logicalDpi().first; + return d->logicalDpi.first; } /*! @@ -198,7 +188,7 @@ qreal QScreen::logicalDotsPerInchX() const qreal QScreen::logicalDotsPerInchY() const { Q_D(const QScreen); - return d->platformScreen->logicalDpi().second; + return d->logicalDpi.second; } /*! @@ -216,7 +206,7 @@ qreal QScreen::logicalDotsPerInchY() const qreal QScreen::logicalDotsPerInch() const { Q_D(const QScreen); - QDpi dpi = d->platformScreen->logicalDpi(); + QDpi dpi = d->logicalDpi; return (dpi.first + dpi.second) * qreal(0.5); } @@ -246,7 +236,7 @@ QSizeF QScreen::physicalSize() const QSize QScreen::availableSize() const { Q_D(const QScreen); - return d->platformScreen->availableGeometry().size(); + return d->availableGeometry.size(); } /*! @@ -259,7 +249,7 @@ QSize QScreen::availableSize() const QRect QScreen::geometry() const { Q_D(const QScreen); - return d->platformScreen->geometry(); + return d->geometry; } /*! @@ -272,7 +262,7 @@ QRect QScreen::geometry() const QRect QScreen::availableGeometry() const { Q_D(const QScreen); - return d->platformScreen->availableGeometry(); + return d->availableGeometry; } /*! @@ -315,10 +305,9 @@ QSize QScreen::virtualSize() const */ QRect QScreen::virtualGeometry() const { - Q_D(const QScreen); QRect result; - foreach (QPlatformScreen *platformScreen, d->platformScreen->virtualSiblings()) - result |= platformScreen->geometry(); + foreach (QScreen *screen, virtualSiblings()) + result |= screen->geometry(); return result; } @@ -347,10 +336,9 @@ QSize QScreen::availableVirtualSize() const */ QRect QScreen::availableVirtualGeometry() const { - Q_D(const QScreen); QRect result; - foreach (QPlatformScreen *platformScreen, d->platformScreen->virtualSiblings()) - result |= platformScreen->availableGeometry(); + foreach (QScreen *screen, virtualSiblings()) + result |= screen->availableGeometry(); return result; } @@ -383,7 +371,7 @@ Qt::ScreenOrientation QScreen::primaryOrientation() const Qt::ScreenOrientation QScreen::currentOrientation() const { Q_D(const QScreen); - return d->platformScreen->currentOrientation(); + return d->currentOrientation; } // i must be power of two diff --git a/src/gui/kernel/qscreen.h b/src/gui/kernel/qscreen.h index 5a85e28..7291e2a 100644 --- a/src/gui/kernel/qscreen.h +++ b/src/gui/kernel/qscreen.h @@ -140,6 +140,7 @@ private: Q_DISABLE_COPY(QScreen) friend class QGuiApplicationPrivate; + friend class QPlatformIntegration; friend class QPlatformScreen; }; diff --git a/src/gui/kernel/qscreen_p.h b/src/gui/kernel/qscreen_p.h new file mode 100644 index 0000000..50742c9 --- /dev/null +++ b/src/gui/kernel/qscreen_p.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtGui module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSCREEN_P_H +#define QSCREEN_P_H + +#include +#include + +#include + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Gui) + +class QScreenPrivate : public QObjectPrivate +{ +public: + QScreenPrivate(QPlatformScreen *screen) + : platformScreen(screen) + { + currentOrientation = screen->currentOrientation(); + geometry = screen->geometry(); + availableGeometry = screen->availableGeometry(); + logicalDpi = screen->logicalDpi(); + } + + Qt::ScreenOrientation currentOrientation; + QRect geometry; + QRect availableGeometry; + QDpi logicalDpi; + + QPlatformScreen *platformScreen; +}; + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif // QSCREEN_P_H diff --git a/src/gui/kernel/qwindowsysteminterface_qpa.cpp b/src/gui/kernel/qwindowsysteminterface_qpa.cpp index 8ab4137..29d134c 100644 --- a/src/gui/kernel/qwindowsysteminterface_qpa.cpp +++ b/src/gui/kernel/qwindowsysteminterface_qpa.cpp @@ -275,31 +275,31 @@ void QWindowSystemInterface::handleTouchEvent(QWindow *tlw, ulong timestamp, QTo QWindowSystemInterfacePrivate::queueWindowSystemEvent(e); } -void QWindowSystemInterface::handleScreenOrientationChange(QScreen *screen) +void QWindowSystemInterface::handleScreenOrientationChange(QScreen *screen, Qt::ScreenOrientation orientation) { QWindowSystemInterfacePrivate::ScreenOrientationEvent *e = - new QWindowSystemInterfacePrivate::ScreenOrientationEvent(screen); + new QWindowSystemInterfacePrivate::ScreenOrientationEvent(screen, orientation); QWindowSystemInterfacePrivate::queueWindowSystemEvent(e); } -void QWindowSystemInterface::handleScreenGeometryChange(QScreen *screen) +void QWindowSystemInterface::handleScreenGeometryChange(QScreen *screen, const QRect &geometry) { QWindowSystemInterfacePrivate::ScreenGeometryEvent *e = - new QWindowSystemInterfacePrivate::ScreenGeometryEvent(screen); + new QWindowSystemInterfacePrivate::ScreenGeometryEvent(screen, geometry); QWindowSystemInterfacePrivate::queueWindowSystemEvent(e); } -void QWindowSystemInterface::handleScreenAvailableGeometryChange(QScreen *screen) +void QWindowSystemInterface::handleScreenAvailableGeometryChange(QScreen *screen, const QRect &availableGeometry) { QWindowSystemInterfacePrivate::ScreenAvailableGeometryEvent *e = - new QWindowSystemInterfacePrivate::ScreenAvailableGeometryEvent(screen); + new QWindowSystemInterfacePrivate::ScreenAvailableGeometryEvent(screen, availableGeometry); QWindowSystemInterfacePrivate::queueWindowSystemEvent(e); } -void QWindowSystemInterface::handleScreenLogicalDotsPerInchChange(QScreen *screen) +void QWindowSystemInterface::handleScreenLogicalDotsPerInchChange(QScreen *screen, qreal dpiX, qreal dpiY) { QWindowSystemInterfacePrivate::ScreenLogicalDotsPerInchEvent *e = - new QWindowSystemInterfacePrivate::ScreenLogicalDotsPerInchEvent(screen); + new QWindowSystemInterfacePrivate::ScreenLogicalDotsPerInchEvent(screen, dpiX, dpiY); QWindowSystemInterfacePrivate::queueWindowSystemEvent(e); } diff --git a/src/gui/kernel/qwindowsysteminterface_qpa.h b/src/gui/kernel/qwindowsysteminterface_qpa.h index d860916..4a17fb9 100644 --- a/src/gui/kernel/qwindowsysteminterface_qpa.h +++ b/src/gui/kernel/qwindowsysteminterface_qpa.h @@ -122,10 +122,10 @@ public: static Qt::DropAction handleDrop(QWindow *w, QMimeData *dropData, const QPoint &p); // Changes to the screen - static void handleScreenOrientationChange(QScreen *screen); - static void handleScreenGeometryChange(QScreen *screen); - static void handleScreenAvailableGeometryChange(QScreen *screen); - static void handleScreenLogicalDotsPerInchChange(QScreen *screen); + static void handleScreenOrientationChange(QScreen *screen, Qt::ScreenOrientation newOrientation); + static void handleScreenGeometryChange(QScreen *screen, const QRect &newGeometry); + static void handleScreenAvailableGeometryChange(QScreen *screen, const QRect &newAvailableGeometry); + static void handleScreenLogicalDotsPerInchChange(QScreen *screen, qreal newDpiX, qreal newDpiY); // For event dispatcher implementations static bool sendWindowSystemEvents(QAbstractEventDispatcher *eventDispatcher, QEventLoop::ProcessEventsFlags flags); diff --git a/src/gui/kernel/qwindowsysteminterface_qpa_p.h b/src/gui/kernel/qwindowsysteminterface_qpa_p.h index 8bda78d..886c1d0 100644 --- a/src/gui/kernel/qwindowsysteminterface_qpa_p.h +++ b/src/gui/kernel/qwindowsysteminterface_qpa_p.h @@ -197,30 +197,35 @@ public: class ScreenOrientationEvent : public WindowSystemEvent { public: - ScreenOrientationEvent(QScreen *s) - : WindowSystemEvent(ScreenOrientation), screen(s) { } + ScreenOrientationEvent(QScreen *s, Qt::ScreenOrientation o) + : WindowSystemEvent(ScreenOrientation), screen(s), orientation(o) { } QWeakPointer screen; + Qt::ScreenOrientation orientation; }; class ScreenGeometryEvent : public WindowSystemEvent { public: - ScreenGeometryEvent(QScreen *s) - : WindowSystemEvent(ScreenGeometry), screen(s) { } + ScreenGeometryEvent(QScreen *s, const QRect &g) + : WindowSystemEvent(ScreenGeometry), screen(s), geometry(g) { } QWeakPointer screen; + QRect geometry; }; class ScreenAvailableGeometryEvent : public WindowSystemEvent { public: - ScreenAvailableGeometryEvent(QScreen *s) - : WindowSystemEvent(ScreenAvailableGeometry), screen(s) { } + ScreenAvailableGeometryEvent(QScreen *s, const QRect &g) + : WindowSystemEvent(ScreenAvailableGeometry), screen(s), availableGeometry(g) { } QWeakPointer screen; + QRect availableGeometry; }; class ScreenLogicalDotsPerInchEvent : public WindowSystemEvent { public: - ScreenLogicalDotsPerInchEvent(QScreen *s) - : WindowSystemEvent(ScreenLogicalDotsPerInch), screen(s) { } + ScreenLogicalDotsPerInchEvent(QScreen *s, qreal dx, qreal dy) + : WindowSystemEvent(ScreenLogicalDotsPerInch), screen(s), dpiX(dx), dpiY(dy) { } QWeakPointer screen; + qreal dpiX; + qreal dpiY; }; class MapEvent : public WindowSystemEvent { diff --git a/tests/auto/gui/kernel/qscreen/tst_qscreen.cpp b/tests/auto/gui/kernel/qscreen/tst_qscreen.cpp index a168203..ab798a7 100644 --- a/tests/auto/gui/kernel/qscreen/tst_qscreen.cpp +++ b/tests/auto/gui/kernel/qscreen/tst_qscreen.cpp @@ -52,6 +52,7 @@ private slots: void angleBetween(); void transformBetween_data(); void transformBetween(); + void orientationChange(); }; void tst_QScreen::angleBetween_data() @@ -163,5 +164,16 @@ void tst_QScreen::transformBetween() QCOMPARE(QScreen::transformBetween(a, b, rect), expected); } +void tst_QScreen::orientationChange() +{ + QScreen *screen = QGuiApplication::primaryScreen(); + + QWindowSystemInterface::handleScreenOrientationChange(screen, Qt::LandscapeOrientation); + QTRY_COMPARE(screen->currentOrientation(), Qt::LandscapeOrientation); + + QWindowSystemInterface::handleScreenOrientationChange(screen, Qt::PortraitOrientation); + QTRY_COMPARE(screen->currentOrientation(), Qt::PortraitOrientation); +} + #include QTEST_MAIN(tst_QScreen); -- 2.7.4