iOS: let QIOSScreen change geometry according to interface rotation

Qt expects the screen to change geometry when the "desktop" rotates.
On iOS, we interpret this as when the root view controller changes
orientation, since after all, this is the surface we place QWindows
on top of.

Change-Id: Ia00e68c8f9f0a65aefcc60518ee544fb260d4595
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@digia.com>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@digia.com>
bb10
Tor Arne Vestbø 2013-02-26 13:49:03 +01:00
parent 70b21ec3c1
commit d5d3f5ea8e
5 changed files with 45 additions and 5 deletions

View File

@ -69,9 +69,12 @@ public:
UIScreen *uiScreen() const;
void setPrimaryOrientation(Qt::ScreenOrientation orientation);
private:
UIScreen *m_uiScreen;
QRect m_geometry;
QRect m_availableGeometry;
int m_depth;
QSizeF m_physicalSize;
QIOSOrientationListener *m_orientationListener;

View File

@ -88,11 +88,12 @@ QIOSScreen::QIOSScreen(unsigned int screenIndex)
unscaledDpi = 132;
};
// UIScreen does not report different bounds for different orientations. We
// match this behavior by staying with a fixed QScreen geometry.
CGRect bounds = [m_uiScreen bounds];
m_geometry = QRect(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
CGRect frame = m_uiScreen.applicationFrame;
m_availableGeometry = QRect(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
const qreal millimetersPerInch = 25.4;
m_physicalSize = QSizeF(m_geometry.size()) / unscaledDpi * millimetersPerInch;
@ -111,8 +112,7 @@ QRect QIOSScreen::geometry() const
QRect QIOSScreen::availableGeometry() const
{
CGRect frame = m_uiScreen.applicationFrame;
return QRect(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
return m_availableGeometry;
}
int QIOSScreen::depth() const
@ -150,6 +150,30 @@ void QIOSScreen::setOrientationUpdateMask(Qt::ScreenOrientations mask)
}
}
void QIOSScreen::setPrimaryOrientation(Qt::ScreenOrientation orientation)
{
// Note that UIScreen never changes orientation, but QScreen should. To work around
// this, we let QIOSViewController call us whenever interface orientation changes, and
// use that as primary orientation. After all, the viewcontrollers geometry is what we
// place QWindows on top of. A problem with this approach is that QIOSViewController is
// not in use in a mixed environment, which results in no change to primary orientation.
// We see that as acceptable since Qt should most likely not interfere with orientation
// for that case anyway.
bool portrait = screen()->isPortrait(orientation);
if (portrait && m_geometry.width() < m_geometry.height())
return;
// Switching portrait/landscape means swapping width/height (and adjusting x/y):
CGRect frame = m_uiScreen.applicationFrame;
m_availableGeometry = portrait ? QRect(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)
: QRect(frame.origin.y, m_geometry.width() - frame.size.width - frame.origin.x, frame.size.height, frame.size.width);
m_geometry = QRect(0, 0, m_geometry.height(), m_geometry.width());
m_physicalSize = QSizeF(m_physicalSize.height(), m_physicalSize.width());
QWindowSystemInterface::handleScreenGeometryChange(screen(), m_geometry);
QWindowSystemInterface::handleScreenAvailableGeometryChange(screen(), m_availableGeometry);
}
UIScreen *QIOSScreen::uiScreen() const
{
return m_uiScreen;

View File

@ -40,7 +40,6 @@
****************************************************************************/
#import <UIKit/UIKit.h>
#import <QtCore/qnamespace.h>
@interface QIOSViewController : UIViewController
@end

View File

@ -43,6 +43,7 @@
#include <QtGui/QGuiApplication>
#include <QtGui/QScreen>
#include "qiosscreen.h"
@implementation QIOSViewController
@ -65,5 +66,16 @@
return UIInterfaceOrientationMaskAll;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
Q_UNUSED(fromInterfaceOrientation);
Qt::ScreenOrientation orientation = convertToQtOrientation(self.interfaceOrientation);
if (orientation == -1)
return;
QIOSScreen *qiosScreen = static_cast<QIOSScreen *>(QGuiApplication::primaryScreen()->handle());
qiosScreen->setPrimaryOrientation(orientation);
}
@end

View File

@ -3,6 +3,8 @@ TARGET = qiosmain
PLUGIN_TYPE = platforms
load(qt_plugin)
QT += gui-private
OBJECTIVE_SOURCES = qtmain.mm \
qiosviewcontroller.mm