diff options
author | Morten Johan Sørvig <morten.sorvig@digia.com> | 2012-11-20 11:34:52 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-12-01 08:33:20 +0100 |
commit | 5e61bbe586519c3d9bc636153d32e810da4e59a3 (patch) | |
tree | 67d67ef644be72ee5b3d685c9a22538d7ec1e01d /src/gui/kernel | |
parent | c8dc41bacdc30026cb79d0d6c72255312084bfe3 (diff) |
Basic high-dpi "retina" support for Qt 5.
Bring Qt 5 on par with Qt 4, prepare for more comprehensive
support later on.
Introduce device independent pixels (dips), device pixels,
and devicePixelRatio. Add high-dpi support to QPainter,
QGLWidget, the cocoa platform plugin, mac and fusion styles.
Dips are similar to CSS pixels, Apple points and
Android density-independent pixels. Device pixels
are pixels in the backing store/physical pixels on screen.
devicePixelRatio is the ratio between them, which is
1.0 on standard displays and 2.0 on "retina" displays.
New API:
QImage::devicePixelRatio() and setDevicePixelRatio()
QPixmap::devicePixelRatio() and setDevicePixelRatio()
QWindow::devicePixelRatio()
QScreen::devicePixelRatio()
QGuiApplicaiton::devicePixelRatio()
Change-Id: If98c3ca9bfdf0e1bdbcf7574cd5b912c9ff63856
Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
Diffstat (limited to 'src/gui/kernel')
-rw-r--r-- | src/gui/kernel/qguiapplication.cpp | 29 | ||||
-rw-r--r-- | src/gui/kernel/qguiapplication.h | 1 | ||||
-rw-r--r-- | src/gui/kernel/qplatformscreen.cpp | 12 | ||||
-rw-r--r-- | src/gui/kernel/qplatformscreen.h | 1 | ||||
-rw-r--r-- | src/gui/kernel/qplatformwindow.cpp | 12 | ||||
-rw-r--r-- | src/gui/kernel/qplatformwindow.h | 2 | ||||
-rw-r--r-- | src/gui/kernel/qscreen.cpp | 14 | ||||
-rw-r--r-- | src/gui/kernel/qscreen.h | 2 | ||||
-rw-r--r-- | src/gui/kernel/qwindow.cpp | 18 | ||||
-rw-r--r-- | src/gui/kernel/qwindow.h | 2 |
10 files changed, 92 insertions, 1 deletions
diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 80e13227ee..fb14490a24 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -703,7 +703,34 @@ QList<QScreen *> QGuiApplication::screens() /*! - Returns the top level window at the given position \a pos, if any. + Returns the highest screen device pixel ratio found on + the system. This is the ratio between physical pixels and + device-independent pixels. + + Use this function only when you don't know which window you are targeting. + If you do know the target window use QWindow::devicePixelRatio() instead. + + \sa QWindow::devicePixelRatio(); + \sa QGuiApplicaiton::devicePixelRatio(); +*/ +qreal QGuiApplication::devicePixelRatio() const +{ + // Cache topDevicePixelRatio, iterate through the screen list once only. + static qreal topDevicePixelRatio = 0.0; + if (!qFuzzyIsNull(topDevicePixelRatio)) { + return topDevicePixelRatio; + } + + topDevicePixelRatio = 1.0; // make sure we never return 0. + foreach (QScreen *screen, QGuiApplicationPrivate::screen_list) { + topDevicePixelRatio = qMax(topDevicePixelRatio, screen->devicePixelRatio()); + } + + return topDevicePixelRatio; +} + +/*! + Returns the top level window at the given position, if any. */ QWindow *QGuiApplication::topLevelAt(const QPoint &pos) { diff --git a/src/gui/kernel/qguiapplication.h b/src/gui/kernel/qguiapplication.h index 27ea86ec6e..7c0dbbdcde 100644 --- a/src/gui/kernel/qguiapplication.h +++ b/src/gui/kernel/qguiapplication.h @@ -104,6 +104,7 @@ public: static QScreen *primaryScreen(); static QList<QScreen *> screens(); + qreal devicePixelRatio() const; #ifndef QT_NO_CURSOR static QCursor *overrideCursor(); diff --git a/src/gui/kernel/qplatformscreen.cpp b/src/gui/kernel/qplatformscreen.cpp index 211a9650dc..ab2fdfa409 100644 --- a/src/gui/kernel/qplatformscreen.cpp +++ b/src/gui/kernel/qplatformscreen.cpp @@ -161,6 +161,18 @@ QDpi QPlatformScreen::logicalDpi() const } /*! + Reimplement this function in subclass to return the device pixel + ratio for the screen. This is the ratio between physical pixels + and device-independent pixels. + + \sa QPlatformWindow::devicePixelRatio(); +*/ +qreal QPlatformScreen::devicePixelRatio() const +{ + return 1.0; +} + +/*! Reimplement this function in subclass to return the vertical refresh rate of the screen, in Hz. diff --git a/src/gui/kernel/qplatformscreen.h b/src/gui/kernel/qplatformscreen.h index 493a1cedd0..91365b1cf0 100644 --- a/src/gui/kernel/qplatformscreen.h +++ b/src/gui/kernel/qplatformscreen.h @@ -98,6 +98,7 @@ public: virtual QSizeF physicalSize() const; virtual QDpi logicalDpi() const; + virtual qreal devicePixelRatio() const; virtual qreal refreshRate() const; diff --git a/src/gui/kernel/qplatformwindow.cpp b/src/gui/kernel/qplatformwindow.cpp index 3bf06c6ab1..25b863c9a3 100644 --- a/src/gui/kernel/qplatformwindow.cpp +++ b/src/gui/kernel/qplatformwindow.cpp @@ -355,6 +355,18 @@ Qt::ScreenOrientation QPlatformWindow::requestWindowOrientation(Qt::ScreenOrient return Qt::PrimaryOrientation; } +/*! + Reimplement this function in subclass to return the device pixel ratio + for the window. This is the ratio between physical pixels + and device-independent pixels. + + \sa QPlatformWindow::devicePixelRatio(); +*/ +qreal QPlatformWindow::devicePixelRatio() const +{ + return 1.0; +} + bool QPlatformWindow::setKeyboardGrabEnabled(bool grab) { Q_UNUSED(grab); diff --git a/src/gui/kernel/qplatformwindow.h b/src/gui/kernel/qplatformwindow.h index 12650d6073..607c8e4035 100644 --- a/src/gui/kernel/qplatformwindow.h +++ b/src/gui/kernel/qplatformwindow.h @@ -117,6 +117,8 @@ public: virtual void handleContentOrientationChange(Qt::ScreenOrientation orientation); virtual Qt::ScreenOrientation requestWindowOrientation(Qt::ScreenOrientation orientation); + virtual qreal devicePixelRatio() const; + virtual bool setKeyboardGrabEnabled(bool grab); virtual bool setMouseGrabEnabled(bool grab); diff --git a/src/gui/kernel/qscreen.cpp b/src/gui/kernel/qscreen.cpp index 0c30de498c..f5467ab742 100644 --- a/src/gui/kernel/qscreen.cpp +++ b/src/gui/kernel/qscreen.cpp @@ -214,6 +214,20 @@ qreal QScreen::logicalDotsPerInch() const return (dpi.first + dpi.second) * qreal(0.5); } +/* + Returns the ratio between physical pixels and device-independent pixels for the screen. + + Common values are 1.0 on normal displays and 2.0 on Apple retina displays. + + \sa QWindow::devicePixelRatio(); + \sa QGuiApplicaiton::devicePixelRatio(); +*/ +qreal QScreen::devicePixelRatio() const +{ + Q_D(const QScreen); + return d->platformScreen->devicePixelRatio(); +} + /*! \property QScreen::physicalSize \brief the screen's physical size (in millimeters) diff --git a/src/gui/kernel/qscreen.h b/src/gui/kernel/qscreen.h index 48eaad94f7..fbbd886755 100644 --- a/src/gui/kernel/qscreen.h +++ b/src/gui/kernel/qscreen.h @@ -109,6 +109,8 @@ public: qreal logicalDotsPerInchY() const; qreal logicalDotsPerInch() const; + qreal devicePixelRatio() const; + QSize availableSize() const; QRect availableGeometry() const; diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index c24609e886..aaf2b25ad4 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -806,6 +806,24 @@ Qt::ScreenOrientation QWindow::orientation() const } /*! + Returns the ratio between physical pixels and device-independent pixels + for the window. This value is dependent on the screen the window is on, + and may change when the window is moved. + + Common values are 1.0 on normal displays and 2.0 on Apple "retina" displays. + + \sa QWindow::devicePixelRatio(); + \sa QGuiApplicaiton::devicePixelRatio(); +*/ +qreal QWindow::devicePixelRatio() const +{ + Q_D(const QWindow); + if (!d->platformWindow) + return 1.0; + return d->platformWindow->devicePixelRatio(); +} + +/*! \brief set the screen-occupation state of the window The window \a state represents whether the window appears in the diff --git a/src/gui/kernel/qwindow.h b/src/gui/kernel/qwindow.h index e6c9a3736f..229275d7c7 100644 --- a/src/gui/kernel/qwindow.h +++ b/src/gui/kernel/qwindow.h @@ -184,6 +184,8 @@ public: void reportContentOrientationChange(Qt::ScreenOrientation orientation); Qt::ScreenOrientation contentOrientation() const; + qreal devicePixelRatio() const; + bool requestOrientation(Qt::ScreenOrientation orientation); Qt::ScreenOrientation orientation() const; |