From d01f0213b837d71dd77e29752ebcbf161f227a11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Mon, 20 Jan 2014 09:11:35 +0100 Subject: [PATCH] Cocoa: Establish pattern for accessing globals Use a static QCocoaIntegration pointer instead of QGuiApplication. This removes the need to call out of the platform plugin as well as the casting from "platform" to "cocoa" types. Change-Id: If432b3567811223b73a67548e475e07d63635b73 Reviewed-by: Gabriel de Dietrich --- .../cocoa/qcocoaapplicationdelegate.mm | 2 +- .../platforms/cocoa/qcocoaintegration.h | 30 ++++++++++++------- .../platforms/cocoa/qcocoaintegration.mm | 28 ++++++++++++----- .../platforms/cocoa/qcocoanativeinterface.h | 3 +- src/plugins/platforms/cocoa/qnsview.mm | 13 ++++---- .../platforms/cocoa/qnsviewaccessibility.mm | 4 +-- 6 files changed, 50 insertions(+), 30 deletions(-) diff --git a/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm b/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm index f9767ce716..327ca00ad6 100644 --- a/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm +++ b/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm @@ -114,7 +114,7 @@ static void cleanupCocoaApplicationDelegate() - (void)updateScreens:(NSNotification *)notification { Q_UNUSED(notification); - if (QCocoaIntegration *ci = dynamic_cast(QGuiApplicationPrivate::platformIntegration())) + if (QCocoaIntegration *ci = QCocoaIntegration::instance()) ci->updateScreens(); } diff --git a/src/plugins/platforms/cocoa/qcocoaintegration.h b/src/plugins/platforms/cocoa/qcocoaintegration.h index 111329aaee..8babfcf8ae 100644 --- a/src/plugins/platforms/cocoa/qcocoaintegration.h +++ b/src/plugins/platforms/cocoa/qcocoaintegration.h @@ -46,6 +46,10 @@ #include "qcocoaautoreleasepool.h" #include "qcocoacursor.h" +#include "qcocoawindow.h" +#include "qcocoanativeinterface.h" +#include "qcocoainputcontext.h" +#include "qcocoaaccessibility.h" #include "qcocoaclipboard.h" #include "qcocoadrag.h" #include "qcocoaservices.h" @@ -53,6 +57,7 @@ #include #include +#include QT_BEGIN_NAMESPACE @@ -103,23 +108,25 @@ public: QCocoaIntegration(); ~QCocoaIntegration(); + static QCocoaIntegration *instance(); + bool hasCapability(QPlatformIntegration::Capability cap) const; QPlatformWindow *createPlatformWindow(QWindow *window) const; QPlatformOpenGLContext *createPlatformOpenGLContext(QOpenGLContext *context) const; QPlatformBackingStore *createPlatformBackingStore(QWindow *widget) const; QAbstractEventDispatcher *createEventDispatcher() const; - QPlatformFontDatabase *fontDatabase() const; - QPlatformNativeInterface *nativeInterface() const; - QPlatformInputContext *inputContext() const; - QPlatformAccessibility *accessibility() const; - QPlatformClipboard *clipboard() const; - QPlatformDrag *drag() const; + QCoreTextFontDatabase *fontDatabase() const; + QCocoaNativeInterface *nativeInterface() const; + QCocoaInputContext *inputContext() const; + QCocoaAccessibility *accessibility() const; + QCocoaClipboard *clipboard() const; + QCocoaDrag *drag() const; QStringList themeNames() const; QPlatformTheme *createPlatformTheme(const QString &name) const; - QPlatformServices *services() const; + QCocoaServices *services() const; QVariant styleHint(StyleHint hint) const; QList possibleKeys(const QKeyEvent *event) const; @@ -128,18 +135,19 @@ public: QCocoaScreen *screenAtIndex(int index); private: + static QCocoaIntegration *mInstance; - QScopedPointer mFontDb; + QScopedPointer mFontDb; - QScopedPointer mInputContext; + QScopedPointer mInputContext; #ifndef QT_NO_ACCESSIBILITY - QScopedPointer mAccessibility; + QScopedPointer mAccessibility; #endif QScopedPointer mPlatformTheme; QList mScreens; QCocoaClipboard *mCocoaClipboard; QScopedPointer mCocoaDrag; - QScopedPointer mNativeInterface; + QScopedPointer mNativeInterface; QScopedPointer mServices; QScopedPointer mKeyboardMapper; }; diff --git a/src/plugins/platforms/cocoa/qcocoaintegration.mm b/src/plugins/platforms/cocoa/qcocoaintegration.mm index 5f01274d98..e8cf5ca69b 100644 --- a/src/plugins/platforms/cocoa/qcocoaintegration.mm +++ b/src/plugins/platforms/cocoa/qcocoaintegration.mm @@ -58,7 +58,6 @@ #include #include -#include #include static void initResources() @@ -214,6 +213,8 @@ QPixmap QCocoaScreen::grabWindow(WId window, int x, int y, int width, int height return windowPixmap; } +QCocoaIntegration *QCocoaIntegration::mInstance = 0; + QCocoaIntegration::QCocoaIntegration() : mFontDb(new QCoreTextFontDatabase()) , mInputContext(new QCocoaInputContext) @@ -226,6 +227,10 @@ QCocoaIntegration::QCocoaIntegration() , mServices(new QCocoaServices) , mKeyboardMapper(new QCocoaKeyMapper) { + if (mInstance != 0) + qWarning("Creating multiple Cocoa platform integrations is not supported"); + mInstance = this; + initResources(); QCocoaAutoReleasePool pool; @@ -273,6 +278,8 @@ QCocoaIntegration::QCocoaIntegration() QCocoaIntegration::~QCocoaIntegration() { + mInstance = 0; + qt_resetNSApplicationSendEvent(); QCocoaAutoReleasePool pool; @@ -296,6 +303,11 @@ QCocoaIntegration::~QCocoaIntegration() } } +QCocoaIntegration *QCocoaIntegration::instance() +{ + return mInstance; +} + /*! \brief Synchronizes the screen list, adds new screens, removes deleted ones */ @@ -388,22 +400,22 @@ QAbstractEventDispatcher *QCocoaIntegration::createEventDispatcher() const return new QCocoaEventDispatcher; } -QPlatformFontDatabase *QCocoaIntegration::fontDatabase() const +QCoreTextFontDatabase *QCocoaIntegration::fontDatabase() const { return mFontDb.data(); } -QPlatformNativeInterface *QCocoaIntegration::nativeInterface() const +QCocoaNativeInterface *QCocoaIntegration::nativeInterface() const { return mNativeInterface.data(); } -QPlatformInputContext *QCocoaIntegration::inputContext() const +QCocoaInputContext *QCocoaIntegration::inputContext() const { return mInputContext.data(); } -QPlatformAccessibility *QCocoaIntegration::accessibility() const +QCocoaAccessibility *QCocoaIntegration::accessibility() const { #ifndef QT_NO_ACCESSIBILITY return mAccessibility.data(); @@ -412,12 +424,12 @@ QPlatformAccessibility *QCocoaIntegration::accessibility() const #endif } -QPlatformClipboard *QCocoaIntegration::clipboard() const +QCocoaClipboard *QCocoaIntegration::clipboard() const { return mCocoaClipboard; } -QPlatformDrag *QCocoaIntegration::drag() const +QCocoaDrag *QCocoaIntegration::drag() const { return mCocoaDrag.data(); } @@ -434,7 +446,7 @@ QPlatformTheme *QCocoaIntegration::createPlatformTheme(const QString &name) cons return QPlatformIntegration::createPlatformTheme(name); } -QPlatformServices *QCocoaIntegration::services() const +QCocoaServices *QCocoaIntegration::services() const { return mServices.data(); } diff --git a/src/plugins/platforms/cocoa/qcocoanativeinterface.h b/src/plugins/platforms/cocoa/qcocoanativeinterface.h index 5c59c73847..4bcb348acb 100644 --- a/src/plugins/platforms/cocoa/qcocoanativeinterface.h +++ b/src/plugins/platforms/cocoa/qcocoanativeinterface.h @@ -137,6 +137,7 @@ private: static void setContentBorderThickness(QWindow *window, int topThickness, int bottomThickness); }; +QT_END_NAMESPACE + #endif // QCOCOANATIVEINTERFACE_H -QT_END_NAMESPACE diff --git a/src/plugins/platforms/cocoa/qnsview.mm b/src/plugins/platforms/cocoa/qnsview.mm index f7b129aea1..849a75f03c 100644 --- a/src/plugins/platforms/cocoa/qnsview.mm +++ b/src/plugins/platforms/cocoa/qnsview.mm @@ -308,10 +308,9 @@ static QTouchDevice *touchDevice = 0; m_platformWindow->exposeWindow(); } else if (notificationName == NSWindowDidChangeScreenNotification) { if (m_window) { - QCocoaIntegration *ci = static_cast(QGuiApplicationPrivate::platformIntegration()); NSUInteger screenIndex = [[NSScreen screens] indexOfObject:self.window.screen]; if (screenIndex != NSNotFound) { - QCocoaScreen *cocoaScreen = ci->screenAtIndex(screenIndex); + QCocoaScreen *cocoaScreen = QCocoaIntegration::instance()->screenAtIndex(screenIndex); QWindowSystemInterface::handleWindowScreenChanged(m_window, cocoaScreen->screen()); } } @@ -552,7 +551,7 @@ static QTouchDevice *touchDevice = 0; [self convertFromScreen:[NSEvent mouseLocation] toWindowPoint:&qtWindowPoint andScreenPoint:&qtScreenPoint]; ulong timestamp = [theEvent timestamp] * 1000; - QCocoaDrag* nativeDrag = static_cast(QGuiApplicationPrivate::platformIntegration()->drag()); + QCocoaDrag* nativeDrag = QCocoaIntegration::instance()->drag(); nativeDrag->setLastMouseEvent(theEvent, self); Qt::KeyboardModifiers keyboardModifiers = [QNSView convertKeyModifiers:[theEvent modifierFlags]]; @@ -1602,7 +1601,7 @@ static QTabletEvent::TabletDevice wacomTabletDevice(NSEvent *theEvent) - (NSDragOperation) draggingSourceOperationMaskForLocal:(BOOL)isLocal { Q_UNUSED(isLocal); - QCocoaDrag* nativeDrag = static_cast(QGuiApplicationPrivate::platformIntegration()->drag()); + QCocoaDrag* nativeDrag = QCocoaIntegration::instance()->drag(); return qt_mac_mapDropActions(nativeDrag->currentDrag()->supportedActions()); } @@ -1633,7 +1632,7 @@ static QTabletEvent::TabletDevice wacomTabletDevice(NSEvent *theEvent) QPlatformDragQtResponse response(false, Qt::IgnoreAction, QRect()); if ([sender draggingSource] != nil) { - QCocoaDrag* nativeDrag = static_cast(QGuiApplicationPrivate::platformIntegration()->drag()); + QCocoaDrag* nativeDrag = QCocoaIntegration::instance()->drag(); response = QWindowSystemInterface::handleDrag(m_window, nativeDrag->platformDropData(), qt_windowPoint, qtAllowed); } else { QCocoaDropData mimeData([sender draggingPasteboard]); @@ -1661,14 +1660,14 @@ static QTabletEvent::TabletDevice wacomTabletDevice(NSEvent *theEvent) QPlatformDropQtResponse response(false, Qt::IgnoreAction); if ([sender draggingSource] != nil) { - QCocoaDrag* nativeDrag = static_cast(QGuiApplicationPrivate::platformIntegration()->drag()); + QCocoaDrag* nativeDrag = QCocoaIntegration::instance()->drag(); response = QWindowSystemInterface::handleDrop(m_window, nativeDrag->platformDropData(), qt_windowPoint, qtAllowed); } else { QCocoaDropData mimeData([sender draggingPasteboard]); response = QWindowSystemInterface::handleDrop(m_window, &mimeData, qt_windowPoint, qtAllowed); } if (response.isAccepted()) { - QCocoaDrag* nativeDrag = static_cast(QGuiApplicationPrivate::platformIntegration()->drag()); + QCocoaDrag* nativeDrag = QCocoaIntegration::instance()->drag(); nativeDrag->setAcceptedAction(response.acceptedAction()); } return response.isAccepted(); diff --git a/src/plugins/platforms/cocoa/qnsviewaccessibility.mm b/src/plugins/platforms/cocoa/qnsviewaccessibility.mm index e8f26aa8c4..a438950a55 100644 --- a/src/plugins/platforms/cocoa/qnsviewaccessibility.mm +++ b/src/plugins/platforms/cocoa/qnsviewaccessibility.mm @@ -45,7 +45,7 @@ #include "qcocoahelpers.h" #include "qcocoaaccessibility.h" #include "qcocoaaccessibilityelement.h" -#include +#include "qcocoaintegration.h" #include #include @@ -63,7 +63,7 @@ - (id)accessibilityAttributeValue:(NSString *)attribute { // activate accessibility updates - QGuiApplicationPrivate::platformIntegration()->accessibility()->setActive(true); + QCocoaIntegration::instance()->accessibility()->setActive(true); if ([attribute isEqualToString:NSAccessibilityRoleAttribute]) { if (m_window->accessibleRoot())