iOS: Prepare platform screen for reacting to dynamic updates to properties

Change-Id: Idb378416da2b559ed88eb5a764cacff149264f70
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@digia.com>
bb10
Tor Arne Vestbø 2013-11-18 18:25:24 +01:00 committed by The Qt Project
parent c3e949ac7d
commit 2e2c7327dd
3 changed files with 45 additions and 45 deletions

View File

@ -72,13 +72,14 @@ public:
UIScreen *uiScreen() const;
void setPrimaryOrientation(Qt::ScreenOrientation orientation);
void updateProperties();
private:
UIScreen *m_uiScreen;
QRect m_geometry;
QRect m_availableGeometry;
int m_depth;
uint m_unscaledDpi;
QSizeF m_physicalSize;
QIOSOrientationListener *m_orientationListener;
};

View File

@ -132,25 +132,14 @@ QIOSScreen::QIOSScreen(unsigned int screenIndex)
m_depth = 24;
}
int unscaledDpi = 163; // Regular iPhone DPI
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad
&& !deviceIdentifier.contains(QRegularExpression("^iPad2,[567]$")) /* excluding iPad Mini */) {
unscaledDpi = 132;
};
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;
if (isQtApplication()) {
// When in a non-mixed environment, let QScreen follow the current interface orientation:
setPrimaryOrientation(toQtScreenOrientation(UIDeviceOrientation([UIApplication sharedApplication].statusBarOrientation)));
m_unscaledDpi = 132;
} else {
m_unscaledDpi = 163; // Regular iPhone DPI
}
updateProperties();
}
QIOSScreen::~QIOSScreen()
@ -158,6 +147,41 @@ QIOSScreen::~QIOSScreen()
[m_orientationListener release];
}
void QIOSScreen::updateProperties()
{
UIWindow *uiWindow = 0;
for (uiWindow in [[UIApplication sharedApplication] windows]) {
if (uiWindow.screen == m_uiScreen)
break;
}
bool inPortrait = UIInterfaceOrientationIsPortrait(uiWindow.rootViewController.interfaceOrientation);
QRect geometry = inPortrait ? fromCGRect(m_uiScreen.bounds)
: QRect(m_uiScreen.bounds.origin.x, m_uiScreen.bounds.origin.y,
m_uiScreen.bounds.size.height, m_uiScreen.bounds.size.width);
if (geometry != m_geometry) {
m_geometry = geometry;
const qreal millimetersPerInch = 25.4;
m_physicalSize = QSizeF(m_geometry.size()) / m_unscaledDpi * millimetersPerInch;
QWindowSystemInterface::handleScreenGeometryChange(screen(), m_geometry);
}
QRect availableGeometry = geometry;
CGSize applicationFrameSize = m_uiScreen.applicationFrame.size;
int statusBarHeight = geometry.height() - (inPortrait ? applicationFrameSize.height : applicationFrameSize.width);
availableGeometry.adjust(0, statusBarHeight, 0, 0);
if (availableGeometry != m_availableGeometry) {
m_availableGeometry = availableGeometry;
QWindowSystemInterface::handleScreenAvailableGeometryChange(screen(), m_availableGeometry);
}
}
QRect QIOSScreen::geometry() const
{
return m_geometry;
@ -213,28 +237,6 @@ 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):
m_geometry = QRect(0, 0, m_geometry.height(), m_geometry.width());
m_physicalSize = QSizeF(m_physicalSize.height(), m_physicalSize.width());
m_availableGeometry = fromPortraitToPrimary(fromCGRect(m_uiScreen.applicationFrame), this);
QWindowSystemInterface::handleScreenGeometryChange(screen(), m_geometry);
QWindowSystemInterface::handleScreenAvailableGeometryChange(screen(), m_availableGeometry);
}
UIScreen *QIOSScreen::uiScreen() const
{
return m_uiScreen;

View File

@ -62,19 +62,16 @@
return UIInterfaceOrientationMaskAll;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
Q_UNUSED(duration);
Q_UNUSED(interfaceOrientation);
if (!QCoreApplication::instance())
return; // FIXME: Store orientation for later (?)
Qt::ScreenOrientation orientation = toQtScreenOrientation(UIDeviceOrientation(toInterfaceOrientation));
if (orientation == -1)
return;
QIOSScreen *qiosScreen = static_cast<QIOSScreen *>(QGuiApplication::primaryScreen()->handle());
qiosScreen->setPrimaryOrientation(orientation);
qiosScreen->updateProperties();
}
@end