1 /* This file is part of the dbusmenu-qt library
2 Copyright 2009 Canonical
3 Author: Aurelien Gateau <aurelien.gateau@canonical.com>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License (LGPL) as published by the Free Software Foundation;
8 either version 2 of the License, or (at your option) any later
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
21 #include "dbusmenushortcut_p.h"
24 #include <QtGui/QKeySequence>
29 static const int QT_COLUMN = 0;
30 static const int DM_COLUMN = 1;
32 static void processKeyTokens(QStringList* tokens, int srcCol, int dstCol)
37 const char* operator[](int col) const { return col == 0 ? zero : one; }
39 static const Row table[] =
42 // Special cases for compatibility with libdbusmenu-glib which uses
43 // "plus" for "+" and "minus" for "-".
44 // cf https://bugs.launchpad.net/libdbusmenu-qt/+bug/712565
50 const Row* ptr = table;
51 for (; ptr->zero != 0; ++ptr) {
52 const char* from = (*ptr)[srcCol];
53 const char* to = (*ptr)[dstCol];
54 tokens->replaceInStrings(from, to);
58 DBusMenuShortcut DBusMenuShortcut::fromKeySequence(const QKeySequence& sequence)
60 QString string = sequence.toString();
61 DBusMenuShortcut shortcut;
62 QStringList tokens = string.split(", ");
63 Q_FOREACH(QString token, tokens) {
64 // Hack: Qt::CTRL | Qt::Key_Plus is turned into the string "Ctrl++",
65 // but we don't want the call to token.split() to consider the
66 // second '+' as a separator so we replace it with its final value.
67 token.replace("++", "+plus");
68 QStringList keyTokens = token.split('+');
69 processKeyTokens(&keyTokens, QT_COLUMN, DM_COLUMN);
70 shortcut << keyTokens;
75 QKeySequence DBusMenuShortcut::toKeySequence() const
78 Q_FOREACH(const QStringList& keyTokens_, *this) {
79 QStringList keyTokens = keyTokens_;
80 processKeyTokens(&keyTokens, DM_COLUMN, QT_COLUMN);
81 tmp << keyTokens.join(QLatin1String("+"));
83 QString string = tmp.join(QLatin1String(", "));
84 return QKeySequence::fromString(string);