diff options
Diffstat (limited to 'src/gui/kernel/qkeysequence.cpp')
-rw-r--r-- | src/gui/kernel/qkeysequence.cpp | 82 |
1 files changed, 48 insertions, 34 deletions
diff --git a/src/gui/kernel/qkeysequence.cpp b/src/gui/kernel/qkeysequence.cpp index 5bf22b9394..881d7cc76a 100644 --- a/src/gui/kernel/qkeysequence.cpp +++ b/src/gui/kernel/qkeysequence.cpp @@ -39,6 +39,7 @@ #ifndef QT_NO_SHORTCUT #include "qdebug.h" +#include <QtCore/qhashfunctions.h> #ifndef QT_NO_REGEXP # include "qregexp.h" #endif @@ -290,6 +291,7 @@ void Q_GUI_EXPORT qt_set_sequence_auto_mnemonic(bool b) { qt_sequence_no_mnemoni \row \li InsertParagraphSeparator \li Enter \li Enter \li Enter \li Enter \row \li InsertLineSeparator \li Shift+Enter \li Meta+Enter, Meta+O \li Shift+Enter \li Shift+Enter \row \li Backspace \li (none) \li Meta+H \li (none) \li (none) + \row \li Cancel \li Escape \li Escape, Ctrl+. \li Escape \li Escape \endtable Note that, since the key sequences used for the standard shortcuts differ @@ -751,6 +753,7 @@ static const struct { \value ZoomIn Zoom in. \value ZoomOut Zoom out. \value FullScreen Toggle the window state to/from full screen. + \value Cancel Cancel the current operation. */ /*! @@ -818,6 +821,7 @@ QKeySequence::QKeySequence(const QString &key, QKeySequence::SequenceFormat form assign(key, format); } +Q_STATIC_ASSERT_X(QKeySequencePrivate::MaxKeyCount == 4, "Change docs and ctor impl below"); /*! Constructs a key sequence with up to 4 keys \a k1, \a k2, \a k3 and \a k4. @@ -876,26 +880,19 @@ QKeySequence::~QKeySequence() void QKeySequence::setKey(int key, int index) { - Q_ASSERT_X(index >= 0 && index < 4, "QKeySequence::setKey", "index out of range"); + Q_ASSERT_X(index >= 0 && index < QKeySequencePrivate::MaxKeyCount, "QKeySequence::setKey", "index out of range"); qAtomicDetach(d); d->key[index] = key; } +Q_STATIC_ASSERT_X(QKeySequencePrivate::MaxKeyCount == 4, "Change docs below"); /*! Returns the number of keys in the key sequence. The maximum is 4. */ int QKeySequence::count() const { - if (!d->key[0]) - return 0; - if (!d->key[1]) - return 1; - if (!d->key[2]) - return 2; - if (!d->key[3]) - return 3; - return 4; + return int(std::distance(d->key, std::find(d->key, d->key + QKeySequencePrivate::MaxKeyCount, 0))); } @@ -987,8 +984,8 @@ int QKeySequence::assign(const QString &ks, QKeySequence::SequenceFormat format) int p = 0, diff = 0; // Run through the whole string, but stop - // if we have 4 keys before the end. - while (keyseq.length() && n < 4) { + // if we have MaxKeyCount keys before the end. + while (keyseq.length() && n < QKeySequencePrivate::MaxKeyCount) { // We MUST use something to separate each sequence, and space // does not cut it, since some of the key names have space // in them.. (Let's hope no one translate with a comma in it:) @@ -1022,9 +1019,10 @@ struct QModifKeyName { int qt_key; QString name; }; +Q_DECLARE_TYPEINFO(QModifKeyName, Q_MOVABLE_TYPE); -Q_GLOBAL_STATIC(QList<QModifKeyName>, globalModifs) -Q_GLOBAL_STATIC(QList<QModifKeyName>, globalPortableModifs) +Q_GLOBAL_STATIC(QVector<QModifKeyName>, globalModifs) +Q_GLOBAL_STATIC(QVector<QModifKeyName>, globalPortableModifs) /*! Constructs a single key from the string \a str. @@ -1040,7 +1038,7 @@ int QKeySequencePrivate::decodeString(const QString &str, QKeySequence::Sequence QString accel = str.toLower(); bool nativeText = (format == QKeySequence::NativeText); - QList<QModifKeyName> *gmodifs; + QVector<QModifKeyName> *gmodifs; if (nativeText) { gmodifs = globalModifs(); if (gmodifs->isEmpty()) { @@ -1076,7 +1074,7 @@ int QKeySequencePrivate::decodeString(const QString &str, QKeySequence::Sequence if (!gmodifs) return ret; - QList<QModifKeyName> modifs; + QVector<QModifKeyName> modifs; if (nativeText) { modifs << QModifKeyName(Qt::CTRL, QCoreApplication::translate("QShortcut", "Ctrl").toLower().append(QLatin1Char('+'))) << QModifKeyName(Qt::SHIFT, QCoreApplication::translate("QShortcut", "Shift").toLower().append(QLatin1Char('+'))) @@ -1100,7 +1098,7 @@ int QKeySequencePrivate::decodeString(const QString &str, QKeySequence::Sequence int i = 0; int lastI = 0; while ((i = sl.indexOf(QLatin1Char('+'), i + 1)) != -1) { - const QString sub = sl.mid(lastI, i - lastI + 1); + const QStringRef sub = sl.midRef(lastI, i - lastI + 1); // If we get here the shortcuts contains at least one '+'. We break up // along the following strategy: // Meta+Ctrl++ ( "Meta+", "Ctrl+", "+" ) @@ -1366,7 +1364,7 @@ QKeySequence::operator QVariant() const */ int QKeySequence::operator[](uint index) const { - Q_ASSERT_X(index < 4, "QKeySequence::operator[]", "index out of range"); + Q_ASSERT_X(index < QKeySequencePrivate::MaxKeyCount, "QKeySequence::operator[]", "index out of range"); return d->key[index]; } @@ -1409,6 +1407,16 @@ bool QKeySequence::operator==(const QKeySequence &other) const d->key[3] == other.d->key[3]); } +/*! + \since 5.6 + + Calculates the hash value of \a key, using + \a seed to seed the calculation. +*/ +uint qHash(const QKeySequence &key, uint seed) Q_DECL_NOTHROW +{ + return qHashRange(key.d->key, key.d->key + QKeySequencePrivate::MaxKeyCount, seed); +} /*! Provides an arbitrary comparison of this key sequence and @@ -1424,10 +1432,8 @@ bool QKeySequence::operator==(const QKeySequence &other) const */ bool QKeySequence::operator< (const QKeySequence &other) const { - for (int i = 0; i < 4; ++i) - if (d->key[i] != other.d->key[i]) - return d->key[i] < other.d->key[i]; - return false; + return std::lexicographical_compare(d->key, d->key + QKeySequencePrivate::MaxKeyCount, + other.d->key, other.d->key + QKeySequencePrivate::MaxKeyCount); } /*! @@ -1523,6 +1529,7 @@ QList<QKeySequence> QKeySequence::listFromString(const QString &str, SequenceFor QList<QKeySequence> result; QStringList strings = str.split(QLatin1String("; ")); + result.reserve(strings.count()); foreach (const QString &string, strings) { result << fromString(string, format); } @@ -1565,15 +1572,14 @@ QString QKeySequence::listToString(const QList<QKeySequence> &list, SequenceForm */ QDataStream &operator<<(QDataStream &s, const QKeySequence &keysequence) { - QList<quint32> list; - list << keysequence.d->key[0]; - - if (s.version() >= 5 && keysequence.count() > 1) { - list << keysequence.d->key[1]; - list << keysequence.d->key[2]; - list << keysequence.d->key[3]; + Q_STATIC_ASSERT_X(QKeySequencePrivate::MaxKeyCount == 4, "Forgot to adapt QDataStream &operator<<(QDataStream &s, const QKeySequence &keysequence) to new QKeySequence::MaxKeyCount"); + const bool extended = s.version() >= 5 && keysequence.count() > 1; + s << quint32(extended ? 4 : 1) << quint32(keysequence.d->key[0]); + if (extended) { + s << quint32(keysequence.d->key[1]) + << quint32(keysequence.d->key[2]) + << quint32(keysequence.d->key[3]); } - s << list; return s; } @@ -1588,11 +1594,19 @@ QDataStream &operator<<(QDataStream &s, const QKeySequence &keysequence) */ QDataStream &operator>>(QDataStream &s, QKeySequence &keysequence) { + const quint32 MaxKeys = QKeySequencePrivate::MaxKeyCount; + quint32 c; + s >> c; + quint32 keys[MaxKeys] = {0}; + for (uint i = 0; i < qMin(c, MaxKeys); ++i) { + if (s.atEnd()) { + qWarning("Premature EOF while reading QKeySequence"); + return s; + } + s >> keys[i]; + } qAtomicDetach(keysequence.d); - QList<quint32> list; - s >> list; - for (int i = 0; i < 4; ++i) - keysequence.d->key[i] = list.value(i); + std::copy(keys, keys + MaxKeys, keysequence.d->key); return s; } |