OSDN Git Service

initial import
[kde/libdbusmenu-qt.git] / src / dbusmenushortcut_p.cpp
1 /* This file is part of the dbusmenu-qt library
2    Copyright 2009 Canonical
3    Author: Aurelien Gateau <aurelien.gateau@canonical.com>
4
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
9    version.
10
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.
15
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.
20 */
21 #include "dbusmenushortcut_p.h"
22
23 // Qt
24 #include <QtGui/QKeySequence>
25
26 // Local
27 #include "debug_p.h"
28
29 static const int QT_COLUMN = 0;
30 static const int DM_COLUMN = 1;
31
32 static void processKeyTokens(QStringList* tokens, int srcCol, int dstCol)
33 {
34     struct Row {
35         const char* zero;
36         const char* one;
37         const char* operator[](int col) const { return col == 0 ? zero : one; }
38     };
39     static const Row table[] =
40     { {"Meta", "Super"},
41       {"Ctrl", "Control"},
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
45       {"+", "plus"},
46       {"-", "minus"},
47       {0, 0}
48     };
49
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);
55     }
56 }
57
58 DBusMenuShortcut DBusMenuShortcut::fromKeySequence(const QKeySequence& sequence)
59 {
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;
71     }
72     return shortcut;
73 }
74
75 QKeySequence DBusMenuShortcut::toKeySequence() const
76 {
77     QStringList tmp;
78     Q_FOREACH(const QStringList& keyTokens_, *this) {
79         QStringList keyTokens = keyTokens_;
80         processKeyTokens(&keyTokens, DM_COLUMN, QT_COLUMN);
81         tmp << keyTokens.join(QLatin1String("+"));
82     }
83     QString string = tmp.join(QLatin1String(", "));
84     return QKeySequence::fromString(string);
85 }