📄 qkeysequence.cpp
字号:
}/*! Destroys the key sequence. */QKeySequence::~QKeySequence(){ if (!d->ref.deref()) delete d;}/*! \internal KeySequences should never be modified, but rather just created. Internally though we do need to modify to keep pace in event delivery.*/void QKeySequence::setKey(int key, int index){ Q_ASSERT_X(index >= 0 && index < 4, "QKeySequence::setKey", "index out of range"); qAtomicDetach(d); d->key[index] = key;}/*! Returns the number of keys in the key sequence. The maximum is 4. */uint 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;}/*! Returns true if the key sequence is empty; otherwise returns false.*/bool QKeySequence::isEmpty() const{ return !d->key[0];}/*! Returns the shortcut key sequence for the mnemonic in \a text, or an empty key sequence if no mnemonics are found. For example, mnemonic("E&xit") returns \c{Qt::ALT+Qt::Key_X}, mnemonic("&Quit") returns \c{ALT+Key_Q}, and mnemonic("Quit") returns an empty QKeySequence. We provide a \l{accelerators.html}{list of common mnemonics} in English. At the time of writing, Microsoft and Open Group do not appear to have issued equivalent recommendations for other languages.*/QKeySequence QKeySequence::mnemonic(const QString &text){ if(qt_sequence_no_mnemonics) return QKeySequence(); int p = 0; while (p >= 0) { p = text.indexOf(QLatin1Char('&'), p) + 1; if (p <= 0 || p >= (int)text.length()) break; if (text.at(p) != QLatin1Char('&')) { QChar c = text.at(p); if (c.isPrint()) { c = c.toUpper(); return QKeySequence(c.unicode() + Qt::ALT); } } p++; } return QKeySequence();}/*! \fn int QKeySequence::assign(const QString &keys) Adds the given \a keys to the key sequence. \a keys may contain up to four key codes, provided they are separated by a comma; for example, "Alt+X,Ctrl+S,Z". The return value is the number of key codes added.*/int QKeySequence::assign(const QString &ks){ QString keyseq = ks; QString part; int n = 0; 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) { // 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:) p = keyseq.indexOf(QLatin1Char(',')); if (-1 != p) { if (p == keyseq.count() - 1) { // Last comma 'Ctrl+,' p = -1; } else { if (QLatin1Char(',') == keyseq.at(p+1)) // e.g. 'Ctrl+,, Shift+,,' p++; if (QLatin1Char(' ') == keyseq.at(p+1)) { // Space after comma diff = 1; p++; } else { diff = 0; } } } part = keyseq.left(-1 == p ? keyseq.length() : p - diff); keyseq = keyseq.right(-1 == p ? 0 : keyseq.length() - (p + 1)); d->key[n] = decodeString(part); ++n; } return n;}struct QModifKeyName { QModifKeyName() { } QModifKeyName(int q, QChar n) : qt_key(q), name(n) { } QModifKeyName(int q, const QString &n) : qt_key(q), name(n) { } int qt_key; QString name;};Q_GLOBAL_STATIC(QList<QModifKeyName>, globalModifs)Q_GLOBAL_STATIC(QList<QModifKeyName>, globalPortableModifs)/*! Constructs a single key from the string \a str.*/int QKeySequence::decodeString(const QString &str){ return QKeySequencePrivate::decodeString(str, NativeText);}int QKeySequencePrivate::decodeString(const QString &str, QKeySequence::SequenceFormat format){ int ret = 0; QString accel = str.toLower(); bool nativeText = (format == QKeySequence::NativeText); QList<QModifKeyName> *gmodifs; if (nativeText) { gmodifs = globalModifs(); if (gmodifs->isEmpty()) {#ifdef QMAC_CTRL *gmodifs << QModifKeyName(Qt::CTRL, QMAC_CTRL);#endif#ifdef QMAC_ALT *gmodifs << QModifKeyName(Qt::ALT, QMAC_ALT);#endif#ifdef QMAC_META *gmodifs << QModifKeyName(Qt::META, QMAC_META);#endif#ifdef QMAC_SHIFT *gmodifs << QModifKeyName(Qt::SHIFT, QMAC_SHIFT);#endif *gmodifs << QModifKeyName(Qt::CTRL, QLatin1String("ctrl+")) << QModifKeyName(Qt::SHIFT, QLatin1String("shift+")) << QModifKeyName(Qt::ALT, QLatin1String("alt+")) << QModifKeyName(Qt::META, QLatin1String("meta+")); } } else { gmodifs = globalPortableModifs(); if (gmodifs->isEmpty()) { *gmodifs << QModifKeyName(Qt::CTRL, QLatin1String("ctrl+")) << QModifKeyName(Qt::SHIFT, QLatin1String("shift+")) << QModifKeyName(Qt::ALT, QLatin1String("alt+")) << QModifKeyName(Qt::META, QLatin1String("meta+")); } } if (!gmodifs) return ret; QList<QModifKeyName> modifs = *gmodifs; if (nativeText) { modifs << QModifKeyName(Qt::CTRL, QShortcut::tr("Ctrl").toLower().append(QLatin1Char('+'))) << QModifKeyName(Qt::SHIFT, QShortcut::tr("Shift").toLower().append(QLatin1Char('+'))) << QModifKeyName(Qt::ALT, QShortcut::tr("Alt").toLower().append(QLatin1Char('+'))) << QModifKeyName(Qt::ALT, QShortcut::tr("Meta").toLower().append(QLatin1Char('+'))); } QString sl = accel; for (int i = 0; i < modifs.size(); ++i) { const QModifKeyName &mkf = modifs.at(i); if (sl.contains(mkf.name)) { ret |= mkf.qt_key; accel.remove(mkf.name); sl = accel; } } int p = accel.lastIndexOf(QLatin1Char('+'), str.length() - 2); // -2 so that Ctrl++ works if(p > 0) accel = accel.mid(p + 1); int fnum = 0; if (accel.length() == 1) { ret |= accel[0].toUpper().unicode(); } else if (accel[0] == QLatin1Char('f') && (fnum = accel.mid(1).toInt()) && (fnum >= 1) && (fnum <= 35)) { ret |= Qt::Key_F1 + fnum - 1; } else { // For NativeText, check the traslation table first, // if we don't find anything then try it out with just the untranlated stuff. // PortableText will only try the untranlated table. bool found = false; for (int tran = 0; tran < 2; ++tran) { if (!nativeText) ++tran; for (int i = 0; keyname[i].name; ++i) { QString keyName(tran == 0 ? QShortcut::tr(keyname[i].name) : QString::fromLatin1(keyname[i].name)); if (accel == keyName.toLower()) { ret |= keyname[i].key; found = true; break; } } if (found) break; } } return ret;}/*! Creates a shortcut string for \a key. For example, Qt::CTRL+Qt::Key_O gives "Ctrl+O". The strings, "Ctrl", "Shift", etc. are translated (using QObject::tr()) in the "QShortcut" context. */QString QKeySequence::encodeString(int key){ return QKeySequencePrivate::encodeString(key, NativeText);}static inline void addKey(QString &str, const QString &theKey, QKeySequence::SequenceFormat format){ if (!str.isEmpty()) str += (format == QKeySequence::NativeText) ? QShortcut::tr("+") : QString(QLatin1String("+")); str += theKey;}QString QKeySequencePrivate::encodeString(int key, QKeySequence::SequenceFormat format){ bool nativeText = (format == QKeySequence::NativeText); QString s;#if defined(Q_WS_MAC) if (nativeText) { // On MAC the order is Meta, Alt, Shift, Control. if ((key & Qt::META) == Qt::META) s += QMAC_META; if ((key & Qt::ALT) == Qt::ALT) s += QMAC_ALT; if ((key & Qt::SHIFT) == Qt::SHIFT) s += QMAC_SHIFT; if ((key & Qt::CTRL) == Qt::CTRL) s += QMAC_CTRL; } else#endif { // On other systems the order is Meta, Control, Alt, Shift if ((key & Qt::META) == Qt::META) s = nativeText ? QShortcut::tr("Meta") : QString(QLatin1String("Meta")); if ((key & Qt::CTRL) == Qt::CTRL) addKey(s, nativeText ? QShortcut::tr("Ctrl") : QString(QLatin1String("Ctrl")), format); if ((key & Qt::ALT) == Qt::ALT) addKey(s, nativeText ? QShortcut::tr("Alt") : QString(QLatin1String("Alt")), format); if ((key & Qt::SHIFT) == Qt::SHIFT) addKey(s, nativeText ? QShortcut::tr("Shift") : QString(QLatin1String("Shift")), format); } key &= ~(Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier); QString p; if (key && key < Qt::Key_Escape && key != Qt::Key_Space) { if (key < 0x10000) { p = QChar(key & 0xffff).toUpper(); } else { p = QChar((key-0x10000)/0x400+0xd800); p += QChar((key-0x10000)%400+0xdc00); } } else if (key >= Qt::Key_F1 && key <= Qt::Key_F35) { p = nativeText ? QShortcut::tr("F%1").arg(key - Qt::Key_F1 + 1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -