1 /*
2     Copyright (c) 2020, Lukas Holecek <hluk@email.cz>
3 
4     This file is part of CopyQ.
5 
6     CopyQ is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10 
11     CopyQ 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
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with CopyQ.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "shortcuts.h"
21 
22 #include <QKeySequence>
23 #include <QObject>
24 #include <QString>
25 
26 namespace {
27 
indexOfKeyHint(const QString & name)28 int indexOfKeyHint(const QString &name)
29 {
30     bool amp = false;
31     int i = 0;
32 
33     for (const auto &c : name) {
34         if (c == '&')
35             amp = !amp;
36         else if (amp)
37             return i - 1;
38         ++i;
39     }
40 
41     return -1;
42 }
43 
44 } // namespace
45 
shortcutToRemove()46 QString shortcutToRemove()
47 {
48 #ifdef Q_OS_MAC
49     return QObject::tr("Backspace", "Key to remove item or MIME on OS X");
50 #else
51     return QObject::tr("Delete", "Key to remove item or MIME");
52 #endif
53 }
54 
portableShortcutText(const QKeySequence & shortcut)55 QString portableShortcutText(const QKeySequence &shortcut)
56 {
57     // WORKAROUND: Qt has convert some keys to upper case which
58     //             breaks some shortcuts on some keyboard layouts.
59     return shortcut.toString(QKeySequence::PortableText).toLower();
60 }
61 
toPortableShortcutText(const QString & shortcutNativeText)62 QString toPortableShortcutText(const QString &shortcutNativeText)
63 {
64     return portableShortcutText(
65                 QKeySequence(shortcutNativeText, QKeySequence::NativeText));
66 }
67 
hasKeyHint(const QString & name)68 bool hasKeyHint(const QString &name)
69 {
70     return indexOfKeyHint(name) != -1;
71 }
72 
removeKeyHint(QString * name)73 QString &removeKeyHint(QString *name)
74 {
75     const int i = indexOfKeyHint(*name);
76     return i == -1 ? *name : name->remove(i, 1);
77 }
78