dbusmenu: Add support for shortcuts

To do that, split out and export a QKeySequencePrivate::keyName() function
that returns the key text representation without the modifiers.

The implementation is compatible with libdbusmenu-qt.

Change-Id: Ibc3190a3949813a1610728cf89574027e5d2778d
Reviewed-by: Shawn Rutledge <shawn.rutledge@theqtcompany.com>
This commit is contained in:
Dmitry Shachnev 2016-01-21 20:15:02 +03:00
parent 751e2ce44f
commit f5ea4baa0d
4 changed files with 60 additions and 12 deletions

View File

@ -1261,7 +1261,28 @@ QString QKeySequencePrivate::encodeString(int key, QKeySequence::SequenceFormat
if ((key & Qt::KeypadModifier) == Qt::KeypadModifier)
addKey(s, nativeText ? QCoreApplication::translate("QShortcut", "Num") : QString::fromLatin1("Num"), format);
QString p = keyName(key, format);
#if defined(Q_OS_OSX)
if (nativeText)
s += p;
else
#endif
addKey(s, p, format);
return s;
}
/*!
\internal
Returns the text representation of the key \a key, which can be used i.e.
when the sequence is serialized. This does not take modifiers into account
(see encodeString() for a version that does).
This static method is used by encodeString() and by the D-Bus menu exporter.
*/
QString QKeySequencePrivate::keyName(int key, QKeySequence::SequenceFormat format)
{
bool nativeText = (format == QKeySequence::NativeText);
key &= ~(Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier | Qt::KeypadModifier);
QString p;
@ -1312,14 +1333,7 @@ NonSymbol:
}
}
}
#if defined(Q_OS_MACX)
if (nativeText)
s += p;
else
#endif
addKey(s, p, format);
return s;
return p;
}
/*!
Matches the sequence with \a seq. Returns ExactMatch if

View File

@ -75,6 +75,8 @@ public:
QAtomicInt ref;
int key[MaxKeyCount];
static QString encodeString(int key, QKeySequence::SequenceFormat format);
// used in dbusmenu
Q_GUI_EXPORT static QString keyName(int key, QKeySequence::SequenceFormat format);
static int decodeString(const QString &keyStr, QKeySequence::SequenceFormat format);
};
#endif // QT_NO_SHORTCUT

View File

@ -42,6 +42,7 @@
#include <QDebug>
#include <QtEndian>
#include <QBuffer>
#include <private/qkeysequence_p.h>
#include <qpa/qplatformmenu.h>
#include "qdbusplatformmenu_p.h"
@ -165,6 +166,7 @@ void QDBusMenuItem::registerDBusTypes()
qDBusRegisterMetaType<QDBusMenuLayoutItemList>();
qDBusRegisterMetaType<QDBusMenuEvent>();
qDBusRegisterMetaType<QDBusMenuEventList>();
qDBusRegisterMetaType<QDBusMenuShortcut>();
}
QDBusMenuItem::QDBusMenuItem(const QDBusPlatformMenuItem *item)
@ -183,13 +185,11 @@ QDBusMenuItem::QDBusMenuItem(const QDBusPlatformMenuItem *item)
m_properties.insert(QLatin1String("toggle-type"), QLatin1String("checkmark"));
m_properties.insert(QLatin1String("toggle-state"), item->isChecked() ? 1 : 0);
}
/* TODO support shortcuts
const QKeySequence &scut = item->shortcut();
if (!scut.isEmpty()) {
QDBusMenuShortcut shortcut(scut);
properties.insert(QLatin1String("shortcut"), QVariant::fromValue(shortcut));
QDBusMenuShortcut shortcut = convertKeySequence(scut);
m_properties.insert(QLatin1String("shortcut"), QVariant::fromValue(shortcut));
}
*/
const QIcon &icon = item->icon();
if (!icon.name().isEmpty()) {
m_properties.insert(QLatin1String("icon-name"), icon.name());
@ -226,6 +226,35 @@ QString QDBusMenuItem::convertMnemonic(const QString &label)
return ret;
}
QDBusMenuShortcut QDBusMenuItem::convertKeySequence(const QKeySequence &sequence)
{
QDBusMenuShortcut shortcut;
for (int i = 0; i < sequence.count(); ++i) {
QStringList tokens;
int key = sequence[i];
if (key & Qt::MetaModifier)
tokens << QStringLiteral("Super");
if (key & Qt::ControlModifier)
tokens << QStringLiteral("Control");
if (key & Qt::AltModifier)
tokens << QStringLiteral("Alt");
if (key & Qt::ShiftModifier)
tokens << QStringLiteral("Shift");
if (key & Qt::KeypadModifier)
tokens << QStringLiteral("Num");
QString keyName = QKeySequencePrivate::keyName(key, QKeySequence::PortableText);
if (keyName == QLatin1String("+"))
tokens << QStringLiteral("plus");
else if (keyName == QLatin1String("-"))
tokens << QStringLiteral("minus");
else
tokens << keyName;
shortcut << tokens;
}
return shortcut;
}
const QDBusArgument &operator<<(QDBusArgument &arg, const QDBusMenuEvent &ev)
{
arg.beginStructure();

View File

@ -58,6 +58,7 @@ class QDBusPlatformMenu;
class QDBusPlatformMenuItem;
class QDBusMenuItem;
typedef QVector<QDBusMenuItem> QDBusMenuItemList;
typedef QVector<QStringList> QDBusMenuShortcut;
class QDBusMenuItem
{
@ -67,6 +68,7 @@ public:
static QDBusMenuItemList items(const QList<int> &ids, const QStringList &propertyNames);
static QString convertMnemonic(const QString &label);
static QDBusMenuShortcut convertKeySequence(const QKeySequence &sequence);
static void registerDBusTypes();
int m_id;
@ -140,5 +142,6 @@ Q_DECLARE_METATYPE(QDBusMenuLayoutItem)
Q_DECLARE_METATYPE(QDBusMenuLayoutItemList)
Q_DECLARE_METATYPE(QDBusMenuEvent)
Q_DECLARE_METATYPE(QDBusMenuEventList)
Q_DECLARE_METATYPE(QDBusMenuShortcut)
#endif