From 7436638b51ef7b121afd5ea8135e4a3ce5913fa4 Mon Sep 17 00:00:00 2001 From: Jan Arve Saether Date: Wed, 20 Jul 2016 16:42:40 +0200 Subject: Use Q_GLOBAL_STATIC instead of static members of QWindowPrivate Having the members static in QWindowPrivate only gives the benefit of tidyness, but there's still the problem of initialization order and thread-safety. Q_GLOBAL_STATIC solves those issues for us. Change-Id: I8e1279959d0bb2b16fd720cb7f4e9afb6eda6355 Reviewed-by: Shawn Rutledge --- src/quick/items/qquickevents.cpp | 59 ++++++++++++++++++++++++++++++++++++-- src/quick/items/qquickevents_p_p.h | 4 +++ src/quick/items/qquickwindow.cpp | 41 ++------------------------ src/quick/items/qquickwindow_p.h | 5 ---- 4 files changed, 63 insertions(+), 46 deletions(-) (limited to 'src') diff --git a/src/quick/items/qquickevents.cpp b/src/quick/items/qquickevents.cpp index e4a080d2b7..80a4e6bd9a 100644 --- a/src/quick/items/qquickevents.cpp +++ b/src/quick/items/qquickevents.cpp @@ -441,6 +441,61 @@ Item { \l inverted always returns false. */ +typedef QHash PointerDeviceForTouchDeviceHash; +Q_GLOBAL_STATIC(PointerDeviceForTouchDeviceHash, g_touchDevices) + +Q_GLOBAL_STATIC_WITH_ARGS(QQuickPointerDevice, g_genericMouseDevice, + (QQuickPointerDevice::Mouse, + QQuickPointerDevice::GenericPointer, + QQuickPointerDevice::Position | QQuickPointerDevice::Scroll | QQuickPointerDevice::Hover, + 1, 3, QLatin1String("core pointer"), 0)) + +typedef QHash PointerDeviceForDeviceIdHash; +Q_GLOBAL_STATIC(PointerDeviceForDeviceIdHash, g_tabletDevices) + +QQuickPointerDevice *QQuickPointerDevice::touchDevice(QTouchDevice *d) +{ + if (g_touchDevices->contains(d)) + return g_touchDevices->value(d); + + QQuickPointerDevice::DeviceType type = QQuickPointerDevice::TouchScreen; + QString name; + int maximumTouchPoints = 10; + QQuickPointerDevice::Capabilities caps = QQuickPointerDevice::Capabilities(QTouchDevice::Position); + if (d) { + QQuickPointerDevice::Capabilities caps = + static_cast(static_cast(d->capabilities()) & 0x0F); + if (d->type() == QTouchDevice::TouchPad) { + type = QQuickPointerDevice::TouchPad; + caps |= QQuickPointerDevice::Scroll; + } + name = d->name(); + maximumTouchPoints = d->maximumTouchPoints(); + } else { + qWarning() << "QQuickWindowPrivate::touchDevice: creating touch device from nullptr device in QTouchEvent"; + } + + QQuickPointerDevice *dev = new QQuickPointerDevice(type, QQuickPointerDevice::Finger, + caps, maximumTouchPoints, 0, name, 0); + g_touchDevices->insert(d, dev); + return dev; +} + +QQuickPointerDevice *QQuickPointerDevice::genericMouseDevice() +{ + return g_genericMouseDevice; +} + +QQuickPointerDevice *QQuickPointerDevice::tabletDevice(qint64 id) +{ + auto it = g_tabletDevices->find(id); + if (it != g_tabletDevices->end()) + return it.value(); + + // ### Figure out how to populate the tablet devices + return nullptr; +} + QQuickEventTouchPoint::QQuickEventTouchPoint(QQuickPointerTouchEvent *parent) : QQuickEventPoint(parent), m_rotation(0), m_pressure(0) @@ -465,7 +520,7 @@ QQuickPointerEvent::~QQuickPointerEvent() QQuickPointerEvent *QQuickPointerMouseEvent::reset(QEvent *event) { auto ev = static_cast(event); - m_device = QQuickWindowPrivate::genericMouseDevice; + m_device = QQuickPointerDevice::genericMouseDevice(); m_event = ev; m_button = ev->button(); m_pressedButtons = ev->buttons(); @@ -490,7 +545,7 @@ QQuickPointerEvent *QQuickPointerMouseEvent::reset(QEvent *event) { QQuickPointerEvent *QQuickPointerTouchEvent::reset(QEvent *event) { auto ev = static_cast(event); - m_device = QQuickWindowPrivate::touchDevice(ev->device()); + m_device = QQuickPointerDevice::touchDevice(ev->device()); m_event = ev; m_button = Qt::NoButton; m_pressedButtons = Qt::NoButton; diff --git a/src/quick/items/qquickevents_p_p.h b/src/quick/items/qquickevents_p_p.h index 9e37955914..f2e06b69df 100644 --- a/src/quick/items/qquickevents_p_p.h +++ b/src/quick/items/qquickevents_p_p.h @@ -499,6 +499,10 @@ public: qint64 uniqueId() const { return m_uniqueId; } QQuickPointerEvent *pointerEvent() const { return m_event; } + static QQuickPointerDevice *touchDevice(QTouchDevice *d); + static QQuickPointerDevice *genericMouseDevice(); + static QQuickPointerDevice *tabletDevice(qint64); + private: DeviceType m_deviceType; PointerType m_pointerType; diff --git a/src/quick/items/qquickwindow.cpp b/src/quick/items/qquickwindow.cpp index 0c88c511eb..73b04f825a 100644 --- a/src/quick/items/qquickwindow.cpp +++ b/src/quick/items/qquickwindow.cpp @@ -93,10 +93,6 @@ extern Q_GUI_EXPORT QImage qt_gl_read_framebuffer(const QSize &size, bool alpha_ bool QQuickWindowPrivate::defaultAlphaBuffer = false; -QQuickPointerDevice *QQuickWindowPrivate::genericMouseDevice(nullptr); -QHash QQuickWindowPrivate::touchDevices; -QHash QQuickWindowPrivate::tabletDevices; - void QQuickWindowPrivate::updateFocusItemTransform() { #ifndef QT_NO_IM @@ -507,11 +503,6 @@ QQuickWindowPrivate::QQuickWindowPrivate() #ifndef QT_NO_DRAGANDDROP dragGrabber = new QQuickDragGrabber; #endif - if (!genericMouseDevice) { - genericMouseDevice = new QQuickPointerDevice(QQuickPointerDevice::Mouse, QQuickPointerDevice::GenericPointer, - QQuickPointerDevice::Position | QQuickPointerDevice::Scroll | QQuickPointerDevice::Hover, - 1, 3, QStringLiteral("core pointer"), 0); - } } QQuickWindowPrivate::~QQuickWindowPrivate() @@ -1874,34 +1865,6 @@ bool QQuickWindowPrivate::deliverNativeGestureEvent(QQuickItem *item, QNativeGes } #endif // QT_NO_GESTURES -QQuickPointerDevice *QQuickWindowPrivate::touchDevice(QTouchDevice *d) -{ - if (touchDevices.contains(d)) - return touchDevices.value(d); - - QQuickPointerDevice::DeviceType type = QQuickPointerDevice::TouchScreen; - QString name; - int maximumTouchPoints = 10; - QQuickPointerDevice::Capabilities caps = QQuickPointerDevice::Capabilities(QTouchDevice::Position); - if (d) { - QQuickPointerDevice::Capabilities caps = - static_cast(static_cast(d->capabilities()) & 0x0F); - if (d->type() == QTouchDevice::TouchPad) { - type = QQuickPointerDevice::TouchPad; - caps |= QQuickPointerDevice::Scroll; - } - name = d->name(); - maximumTouchPoints = d->maximumTouchPoints(); - } else { - qWarning() << "QQuickWindowPrivate::touchDevice: creating touch device from nullptr device in QTouchEvent"; - } - - QQuickPointerDevice *dev = new QQuickPointerDevice(type, QQuickPointerDevice::Finger, - caps, maximumTouchPoints, 0, name, 0); - touchDevices.insert(d, dev); - return dev; -} - bool QQuickWindowPrivate::deliverTouchCancelEvent(QTouchEvent *event) { qCDebug(DBG_TOUCH) << event; @@ -2151,13 +2114,13 @@ QQuickPointerEvent *QQuickWindowPrivate::pointerEventInstance(QEvent *event) case QEvent::MouseButtonRelease: case QEvent::MouseButtonDblClick: case QEvent::MouseMove: - dev = genericMouseDevice; + dev = QQuickPointerDevice::genericMouseDevice(); break; case QEvent::TouchBegin: case QEvent::TouchUpdate: case QEvent::TouchEnd: case QEvent::TouchCancel: - dev = touchDevice(static_cast(event)->device()); + dev = QQuickPointerDevice::touchDevice(static_cast(event)->device()); break; // TODO tablet event types default: diff --git a/src/quick/items/qquickwindow_p.h b/src/quick/items/qquickwindow_p.h index 7e66aef9f4..15ac701c54 100644 --- a/src/quick/items/qquickwindow_p.h +++ b/src/quick/items/qquickwindow_p.h @@ -154,7 +154,6 @@ public: #ifndef QT_NO_GESTURES bool deliverNativeGestureEvent(QQuickItem *, QNativeGestureEvent *); #endif - static QQuickPointerDevice *touchDevice(QTouchDevice *d); // entry point of events to the window void handleTouchEvent(QTouchEvent *); @@ -274,10 +273,6 @@ public: mutable QQuickWindowIncubationController *incubationController; - static QQuickPointerDevice *genericMouseDevice; - static QHash touchDevices; - static QHash tabletDevices; - static bool defaultAlphaBuffer; static bool dragOverThreshold(qreal d, Qt::Axis axis, QMouseEvent *event, int startDragThreshold = -1); -- cgit v1.2.3