summaryrefslogtreecommitdiffstats
path: root/src/gui/kernel/qscreen.cpp
diff options
context:
space:
mode:
authorSamuel Rødal <samuel.rodal@nokia.com>2011-10-04 13:11:30 +0200
committerQt by Nokia <qt-info@nokia.com>2011-10-07 22:23:30 +0200
commitc66b7cf55b6913dcf33d49d4f6bf9db51ccf39a8 (patch)
tree430983856f993de5618aa5d8d0198b2db98eda97 /src/gui/kernel/qscreen.cpp
parent090d825e1d853a2ed875329a8350b0a727c3527e (diff)
Added Orientation API to QScreen and QWindow.
QScreen now has a primary and current orientation, and a QWindow can set its orientation as well. The current screen orientation is just a hint to the application. Change-Id: I4635982cfac2d16634d4edd5c6ab78e9d0ac55a4 Reviewed-on: http://codereview.qt-project.org/5988 Reviewed-by: Paul Olav Tvete <paul.tvete@nokia.com> Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com> Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
Diffstat (limited to 'src/gui/kernel/qscreen.cpp')
-rw-r--r--src/gui/kernel/qscreen.cpp118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/gui/kernel/qscreen.cpp b/src/gui/kernel/qscreen.cpp
index c2049af1e5..1f0d2f9fe6 100644
--- a/src/gui/kernel/qscreen.cpp
+++ b/src/gui/kernel/qscreen.cpp
@@ -278,4 +278,122 @@ QRect QScreen::availableVirtualGeometry() const
return result;
}
+/*!
+ Gets the primary screen orientation.
+
+ The primary screen orientation is the orientation that corresponds
+ to an un-rotated screen buffer. When the current orientation is equal
+ to the primary orientation no rotation needs to be done by the
+ application.
+*/
+Qt::ScreenOrientation QScreen::primaryOrientation() const
+{
+ Q_D(const QScreen);
+ return d->platformScreen->primaryOrientation();
+}
+
+/*!
+ Gets the current orientation of the screen.
+
+ The current orientation is a hint to the application saying
+ what the preferred application orientation should be, based on the
+ current orientation of the physical display and / or other factors.
+
+ \sa primaryOrientation()
+ \sa currentOrientationChanged()
+*/
+Qt::ScreenOrientation QScreen::currentOrientation() const
+{
+ Q_D(const QScreen);
+ return d->platformScreen->currentOrientation();
+}
+
+/*!
+ Convenience function to compute the angle of rotation to get from
+ rotation \a a to rotation \a b.
+
+ The result will be 0, 90, 180, or 270.
+*/
+int QScreen::angleBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b)
+{
+ if (a == Qt::UnknownOrientation || b == Qt::UnknownOrientation || a == b)
+ return 0;
+
+ int delta = int(b) - int(a);
+
+ if (delta < 0)
+ delta = delta + 4;
+
+ int angles[] = { 0, 90, 180, 270 };
+ return angles[delta];
+}
+
+/*!
+ Convenience function to compute a transform that maps from the coordinate system
+ defined by orientation \a a into the coordinate system defined by orientation
+ \a b and target dimensions \a target.
+
+ Example, \a a is Qt::Landscape, \a b is Qt::Portrait, and \a target is QRect(0, 0, w, h)
+ the resulting transform will be such that the point QPoint(0, 0) is mapped to QPoint(0, w),
+ and QPoint(h, w) is mapped to QPoint(0, h). Thus, the landscape coordinate system QRect(0, 0, h, w)
+ is mapped (with a 90 degree rotation) into the portrait coordinate system QRect(0, 0, w, h).
+*/
+QTransform QScreen::transformBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b, const QRect &target)
+{
+ if (a == Qt::UnknownOrientation || b == Qt::UnknownOrientation || a == b)
+ return QTransform();
+
+ int angle = angleBetween(a, b);
+
+ QTransform result;
+ switch (angle) {
+ case 90:
+ result.translate(target.width(), 0);
+ break;
+ case 180:
+ result.translate(target.width(), target.height());
+ break;
+ case 270:
+ result.translate(0, target.height());
+ break;
+ default:
+ Q_ASSERT(false);
+ }
+ result.rotate(angle);
+
+ return result;
+}
+
+/*!
+ Maps the rect between two screen orientations.
+
+ This will flip the x and y dimensions of the rectangle if orientation \a is
+ Qt::PortraitOrientation or Qt::InvertedPortraitOrientation and orientation \b is
+ Qt::LandscapeOrientation or Qt::InvertedLandscapeOrientation, or vice versa.
+*/
+QRect QScreen::mapBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b, const QRect &rect)
+{
+ if (a == Qt::UnknownOrientation || b == Qt::UnknownOrientation || a == b)
+ return rect;
+
+ if ((a == Qt::PortraitOrientation || a == Qt::InvertedPortraitOrientation)
+ != (b == Qt::PortraitOrientation || b == Qt::InvertedPortraitOrientation))
+ {
+ return QRect(rect.y(), rect.x(), rect.height(), rect.width());
+ }
+
+ return rect;
+}
+
+/*!
+ \fn QScreen::currentOrientationChanged()
+
+ This signal is emitted when the current orientation of the screen
+ changes. The current orientation is a hint to the application saying
+ what the preferred application orientation should be, based on the
+ current orientation of the physical display and / or other factors.
+
+ \sa currentOrientation()
+*/
+
QT_END_NAMESPACE