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