Skip to content

Commit

Permalink
dbusmenu: Add support for shortcuts
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
mitya57 committed Feb 2, 2016
1 parent 751e2ce commit f5ea4ba
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
30 changes: 22 additions & 8 deletions src/gui/kernel/qkeysequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1312,14 +1333,7 @@ QString QKeySequencePrivate::encodeString(int key, QKeySequence::SequenceFormat
}
}
}

#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
Expand Down
2 changes: 2 additions & 0 deletions src/gui/kernel/qkeysequence_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class QKeySequencePrivate
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
Expand Down
37 changes: 33 additions & 4 deletions src/platformsupport/dbusmenu/qdbusmenutypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <QDebug>
#include <QtEndian>
#include <QBuffer>
#include <private/qkeysequence_p.h>
#include <qpa/qplatformmenu.h>
#include "qdbusplatformmenu_p.h"

Expand Down Expand Up @@ -165,6 +166,7 @@ void QDBusMenuItem::registerDBusTypes()
qDBusRegisterMetaType<QDBusMenuLayoutItemList>();
qDBusRegisterMetaType<QDBusMenuEvent>();
qDBusRegisterMetaType<QDBusMenuEventList>();
qDBusRegisterMetaType<QDBusMenuShortcut>();
}

QDBusMenuItem::QDBusMenuItem(const QDBusPlatformMenuItem *item)
Expand All @@ -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());
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions src/platformsupport/dbusmenu/qdbusmenutypes_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class QDBusPlatformMenu;
class QDBusPlatformMenuItem;
class QDBusMenuItem;
typedef QVector<QDBusMenuItem> QDBusMenuItemList;
typedef QVector<QStringList> QDBusMenuShortcut;

class QDBusMenuItem
{
Expand All @@ -67,6 +68,7 @@ class QDBusMenuItem

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;
Expand Down Expand Up @@ -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

0 comments on commit f5ea4ba

Please sign in to comment.