File indexing completed on 2024-04-28 05:35:32

0001 /* This file is part of the dbusmenu-qt library
0002     SPDX-FileCopyrightText: 2009 Canonical
0003     SPDX-FileContributor: Aurelien Gateau <aurelien.gateau@canonical.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 #include "dbusmenushortcut_p.h"
0008 
0009 // Qt
0010 #include <QKeySequence>
0011 
0012 static const int QT_COLUMN = 0;
0013 static const int DM_COLUMN = 1;
0014 
0015 static void processKeyTokens(QStringList *tokens, int srcCol, int dstCol)
0016 {
0017     struct Row {
0018         const char *zero;
0019         const char *one;
0020         const char *operator[](int col) const
0021         {
0022             return col == 0 ? zero : one;
0023         }
0024     };
0025     static const Row table[] = {{"Meta", "Super"},
0026                                 {"Ctrl", "Control"},
0027                                 // Special cases for compatibility with libdbusmenu-glib which uses
0028                                 // "plus" for "+" and "minus" for "-".
0029                                 // cf https://bugs.launchpad.net/libdbusmenu-qt/+bug/712565
0030                                 {"+", "plus"},
0031                                 {"-", "minus"},
0032                                 {nullptr, nullptr}};
0033 
0034     const Row *ptr = table;
0035     for (; ptr->zero != nullptr; ++ptr) {
0036         const char *from = (*ptr)[srcCol];
0037         const char *to = (*ptr)[dstCol];
0038         tokens->replaceInStrings(from, to);
0039     }
0040 }
0041 
0042 DBusMenuShortcut DBusMenuShortcut::fromKeySequence(const QKeySequence &sequence)
0043 {
0044     QString string = sequence.toString();
0045     DBusMenuShortcut shortcut;
0046     QStringList tokens = string.split(QStringLiteral(", "));
0047     Q_FOREACH (QString token, tokens) {
0048         // Hack: Qt::CTRL | Qt::Key_Plus is turned into the string "Ctrl++",
0049         // but we don't want the call to token.split() to consider the
0050         // second '+' as a separator so we replace it with its final value.
0051         token.replace(QLatin1String("++"), QLatin1String("+plus"));
0052         QStringList keyTokens = token.split('+');
0053         processKeyTokens(&keyTokens, QT_COLUMN, DM_COLUMN);
0054         shortcut << keyTokens;
0055     }
0056     return shortcut;
0057 }
0058 
0059 QKeySequence DBusMenuShortcut::toKeySequence() const
0060 {
0061     QStringList tmp;
0062     Q_FOREACH (const QStringList &keyTokens_, *this) {
0063         QStringList keyTokens = keyTokens_;
0064         processKeyTokens(&keyTokens, DM_COLUMN, QT_COLUMN);
0065         tmp << keyTokens.join(QLatin1String("+"));
0066     }
0067     QString string = tmp.join(QLatin1String(", "));
0068     return QKeySequence::fromString(string);
0069 }