Make QDebug operator<< polymorphic for QPointingDevice

At least it will look polymorphic by doing dispatch internally.
Adding pointingDeviceType avoids the need for qobject_cast,
and will probably also be useful in other contexts.

Change-Id: I3b6d13765bdf3add9a8208de6f0e98018e40cc42
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
Shawn Rutledge 2020-06-30 12:06:37 +02:00
parent acbe4190e9
commit 836c0b5a24
3 changed files with 8 additions and 1 deletions

View File

@ -39,6 +39,7 @@
#include "qinputdevice.h" #include "qinputdevice.h"
#include "qinputdevice_p.h" #include "qinputdevice_p.h"
#include "qpointingdevice.h"
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <QLoggingCategory> #include <QLoggingCategory>
@ -278,6 +279,9 @@ bool QInputDevice::operator==(const QInputDevice &other) const
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug debug, const QInputDevice *device) QDebug operator<<(QDebug debug, const QInputDevice *device)
{ {
const QInputDevicePrivate *d = QInputDevicePrivate::get(device);
if (d->pointingDeviceType)
return operator<<(debug, static_cast<const QPointingDevice *>(device));
QDebugStateSaver saver(debug); QDebugStateSaver saver(debug);
debug.nospace(); debug.nospace();
debug.noquote(); debug.noquote();

View File

@ -64,7 +64,8 @@ public:
QInputDevicePrivate(const QString &name, qint64 id, QInputDevice::DeviceType type, QInputDevicePrivate(const QString &name, qint64 id, QInputDevice::DeviceType type,
QInputDevice::Capabilities caps = QInputDevice::Capability::None, QInputDevice::Capabilities caps = QInputDevice::Capability::None,
const QString &seatName = QString()) const QString &seatName = QString())
: name(name), seatName(seatName), id(id), capabilities(caps), deviceType(type) : name(name), seatName(seatName), id(id), capabilities(caps),
deviceType(type), pointingDeviceType(false)
{ {
// if the platform doesn't provide device IDs, make one up, // if the platform doesn't provide device IDs, make one up,
// but try to avoid clashing with OS-provided 32-bit IDs // but try to avoid clashing with OS-provided 32-bit IDs
@ -81,6 +82,7 @@ public:
qint64 id = 0; qint64 id = 0;
qint32 capabilities = static_cast<qint32>(QInputDevice::Capability::None); qint32 capabilities = static_cast<qint32>(QInputDevice::Capability::None);
QInputDevice::DeviceType deviceType = QInputDevice::DeviceType::Unknown; QInputDevice::DeviceType deviceType = QInputDevice::DeviceType::Unknown;
qint16 pointingDeviceType : 1; // actually bool, but pack with deviceType
static void registerDevice(const QInputDevice *dev); static void registerDevice(const QInputDevice *dev);
static void unregisterDevice(const QInputDevice *dev); static void unregisterDevice(const QInputDevice *dev);

View File

@ -70,6 +70,7 @@ public:
maximumTouchPoints(qint8(maxPoints)), buttonCount(qint8(buttonCount)), maximumTouchPoints(qint8(maxPoints)), buttonCount(qint8(buttonCount)),
pointerType(pType) pointerType(pType)
{ {
pointingDeviceType = true;
} }
void * extra = nullptr; // QPA plugins can store platform-specific stuff here void * extra = nullptr; // QPA plugins can store platform-specific stuff here