Get rid of QKeyEventEx

This class was added when we needed more information in QKeyEvent but
couldn't extend it. And we couldn't use the d pointer because the copy
constructor and copy assignment operators in QEvent were
implicit. That is now fixed.

But since this is Qt 5, we can change QKeyEvent to include the extra
information.

Task-number: QTBUG-25070
Change-Id: Iba4ac3378ca70583fcaa8caf96bca8ef75e30701
Reviewed-by: Marius Storm-Olsen <marius.storm-olsen@nokia.com>
This commit is contained in:
Thiago Macieira 2012-04-04 15:36:21 -03:00 committed by Qt by Nokia
parent 8c4a17aace
commit e915d31010
5 changed files with 62 additions and 85 deletions

View File

@ -717,10 +717,40 @@ QWheelEvent::QWheelEvent(const QPointF &pos, const QPointF& globalPos,
*/
QKeyEvent::QKeyEvent(Type type, int key, Qt::KeyboardModifiers modifiers, const QString& text,
bool autorep, ushort count)
: QInputEvent(type, modifiers), txt(text), k(key), c(count), autor(autorep)
: QInputEvent(type, modifiers), txt(text), k(key),
nScanCode(0), nVirtualKey(0), nModifiers(0),
c(count), autor(autorep)
{
}
/*!
Constructs a key event object.
The \a type parameter must be QEvent::KeyPress, QEvent::KeyRelease,
or QEvent::ShortcutOverride.
Int \a key is the code for the Qt::Key that the event loop should listen
for. If \a key is 0, the event is not a result of a known key; for
example, it may be the result of a compose sequence or keyboard macro.
The \a modifiers holds the keyboard modifiers, and the given \a text
is the Unicode text that the key generated. If \a autorep is true,
isAutoRepeat() will be true. \a count is the number of keys involved
in the event.
In addition to the normal key event data, also contains \a nativeScanCode,
\a nativeVirtualKey and \a nativeModifiers. This extra data is used by the
shortcut system, to determine which shortcuts to trigger.
*/
QKeyEvent::QKeyEvent(Type type, int key, Qt::KeyboardModifiers modifiers,
quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers,
const QString &text, bool autorep, ushort count)
: QInputEvent(type, modifiers), txt(text), k(key),
nScanCode(nativeScanCode), nVirtualKey(nativeVirtualKey), nModifiers(nativeModifiers),
c(count), autor(autorep)
{
}
/*!
\internal
*/
@ -729,16 +759,9 @@ QKeyEvent::~QKeyEvent()
}
/*!
\fn QKeyEvent *QKeyEvent::createExtendedKeyEvent(Type type, int key, Qt::KeyboardModifiers modifiers, quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers, const QString& text, bool autorep, ushort count)
\internal
*/
QKeyEvent *QKeyEvent::createExtendedKeyEvent(Type type, int key, Qt::KeyboardModifiers modifiers,
quint32 nativeScanCode, quint32 nativeVirtualKey,
quint32 nativeModifiers,
const QString& text, bool autorep, ushort count)
{
return new QKeyEventEx(type, key, modifiers, text, autorep, count,
nativeScanCode, nativeVirtualKey, nativeModifiers);
}
/*!
\fn bool QKeyEvent::hasExtendedInfo() const
@ -746,6 +769,7 @@ QKeyEvent *QKeyEvent::createExtendedKeyEvent(Type type, int key, Qt::KeyboardMod
*/
/*!
\fn quint32 QKeyEvent::nativeScanCode() const
\since 4.2
Returns the native scan code of the key event. If the key event
@ -758,13 +782,9 @@ QKeyEvent *QKeyEvent::createExtendedKeyEvent(Type type, int key, Qt::KeyboardMod
way to get the scan code from Carbon or Cocoa. The function always
returns 1 (or 0 in the case explained above).
*/
quint32 QKeyEvent::nativeScanCode() const
{
return (reinterpret_cast<const QKeyEvent*>(d) != this
? 0 : reinterpret_cast<const QKeyEventEx*>(this)->nScanCode);
}
/*!
\fn quint32 QKeyEvent::nativeVirtualKey() const
\since 4.2
Returns the native virtual key, or key sym of the key event.
@ -772,13 +792,9 @@ quint32 QKeyEvent::nativeScanCode() const
Note: The native virtual key may be 0, even if the key event contains extended information.
*/
quint32 QKeyEvent::nativeVirtualKey() const
{
return (reinterpret_cast<const QKeyEvent*>(d) != this
? 0 : reinterpret_cast<const QKeyEventEx*>(this)->nVirtualKey);
}
/*!
\fn quint32 QKeyEvent::nativeModifiers() const
\since 4.2
Returns the native modifiers of a key event.
@ -786,44 +802,6 @@ quint32 QKeyEvent::nativeVirtualKey() const
Note: The native modifiers may be 0, even if the key event contains extended information.
*/
quint32 QKeyEvent::nativeModifiers() const
{
return (reinterpret_cast<const QKeyEvent*>(d) != this
? 0 : reinterpret_cast<const QKeyEventEx*>(this)->nModifiers);
}
/*!
\internal
Creates an extended key event object, which in addition to the normal key event data, also
contains the native scan code, virtual key and modifiers. This extra data is used by the
shortcut system, to determine which shortcuts to trigger.
*/
QKeyEventEx::QKeyEventEx(Type type, int key, Qt::KeyboardModifiers modifiers,
const QString &text, bool autorep, ushort count,
quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers)
: QKeyEvent(type, key, modifiers, text, autorep, count),
nScanCode(nativeScanCode), nVirtualKey(nativeVirtualKey), nModifiers(nativeModifiers)
{
d = reinterpret_cast<QEventPrivate*>(this);
}
/*!
\internal
Creates a copy of an other extended key event.
*/
QKeyEventEx::QKeyEventEx(const QKeyEventEx &other)
: QKeyEvent(QEvent::Type(other.t), other.k, other.modState, other.txt, other.autor, other.c),
nScanCode(other.nScanCode), nVirtualKey(other.nVirtualKey), nModifiers(other.nModifiers)
{
d = reinterpret_cast<QEventPrivate*>(this);
}
/*!
\internal
*/
QKeyEventEx::~QKeyEventEx()
{
}
/*!
\fn int QKeyEvent::key() const

View File

@ -243,6 +243,9 @@ class Q_GUI_EXPORT QKeyEvent : public QInputEvent
public:
QKeyEvent(Type type, int key, Qt::KeyboardModifiers modifiers, const QString& text = QString(),
bool autorep = false, ushort count = 1);
QKeyEvent(Type type, int key, Qt::KeyboardModifiers modifiers,
quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers,
const QString &text = QString(), bool autorep = false, ushort count = 1);
~QKeyEvent();
int key() const { return k; }
@ -254,22 +257,35 @@ public:
inline bool isAutoRepeat() const { return autor; }
inline int count() const { return int(c); }
inline quint32 nativeScanCode() const { return nScanCode; }
inline quint32 nativeVirtualKey() const { return nVirtualKey; }
inline quint32 nativeModifiers() const { return nModifiers; }
// Functions for the extended key event information
static QKeyEvent *createExtendedKeyEvent(Type type, int key, Qt::KeyboardModifiers modifiers,
#if QT_DEPRECATED_SINCE(5, 0)
static inline QKeyEvent *createExtendedKeyEvent(Type type, int key, Qt::KeyboardModifiers modifiers,
quint32 nativeScanCode, quint32 nativeVirtualKey,
quint32 nativeModifiers,
const QString& text = QString(), bool autorep = false,
ushort count = 1);
inline bool hasExtendedInfo() const { return reinterpret_cast<const QKeyEvent*>(d) == this; }
quint32 nativeScanCode() const;
quint32 nativeVirtualKey() const;
quint32 nativeModifiers() const;
ushort count = 1)
{
return new QKeyEvent(type, key, modifiers,
nativeScanCode, nativeVirtualKey, nativeModifiers,
text, autorep, count);
}
inline bool hasExtendedInfo() const { return true; }
#endif
protected:
QString txt;
int k;
quint32 nScanCode;
quint32 nVirtualKey;
quint32 nModifiers;
ushort c;
uint autor:1;
ushort autor:1;
// ushort reserved:15;
};

View File

@ -60,24 +60,6 @@ QT_BEGIN_NAMESPACE
// We mean it.
//
// ### Qt 5: remove
class QKeyEventEx : public QKeyEvent
{
public:
QKeyEventEx(Type type, int key, Qt::KeyboardModifiers modifiers,
const QString &text, bool autorep, ushort count,
quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers);
QKeyEventEx(const QKeyEventEx &other);
~QKeyEventEx();
protected:
quint32 nScanCode;
quint32 nVirtualKey;
quint32 nModifiers;
friend class QKeyEvent;
};
class QTouchEventTouchPointPrivate
{
public:

View File

@ -1122,8 +1122,9 @@ void QGuiApplicationPrivate::processKeyEvent(QWindowSystemInterfacePrivate::KeyE
if (!window)
return;
QKeyEventEx ev(e->keyType, e->key, e->modifiers, e->unicode, e->repeat, e->repeatCount,
e->nativeScanCode, e->nativeVirtualKey, e->nativeModifiers);
QKeyEvent ev(e->keyType, e->key, e->modifiers,
e->nativeScanCode, e->nativeVirtualKey, e->nativeModifiers,
e->unicode, e->repeat, e->repeatCount);
ev.setTimestamp(e->timestamp);
QGuiApplication::sendSpontaneousEvent(window, &ev);
}

View File

@ -180,7 +180,7 @@ bool QWindowSystemInterface::tryHandleSynchronousExtendedShortcutEvent(QWindow *
{
QGuiApplicationPrivate::modifier_buttons = mods;
QKeyEventEx qevent(QEvent::ShortcutOverride, k, mods, text, autorep, count, nativeScanCode, nativeVirtualKey, nativeModifiers);
QKeyEvent qevent(QEvent::ShortcutOverride, k, mods, nativeScanCode, nativeVirtualKey, nativeModifiers, text, autorep, count);
qevent.setTimestamp(timestamp);
return QGuiApplicationPrivate::instance()->shortcutMap.tryShortcutEvent(w, &qevent);
}