1 | /* |
2 | This file is part of the KDE libraries |
3 | SPDX-FileCopyrightText: 1999, 2000 Kurt Granroth <granroth@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-only |
6 | */ |
7 | |
8 | #include "kstandardactions.h" |
9 | #include "kstandardactions_p.h" |
10 | #include "moc_kstandardactions_p.cpp" |
11 | |
12 | #include <KStandardShortcutWatcher> |
13 | #include <QGuiApplication> |
14 | |
15 | namespace KStandardActions |
16 | { |
17 | |
18 | QList<StandardAction> actionIds() |
19 | { |
20 | QList<StandardAction> result; |
21 | |
22 | for (uint i = 0; g_rgActionInfo[i].id != ActionNone; i++) { |
23 | result.append(t: g_rgActionInfo[i].id); |
24 | } |
25 | |
26 | return result; |
27 | } |
28 | |
29 | KStandardShortcut::StandardShortcut shortcutForActionId(StandardAction id) |
30 | { |
31 | const KStandardActionsInfo *pInfo = infoPtr(id); |
32 | return (pInfo) ? pInfo->idAccel : KStandardShortcut::AccelNone; |
33 | } |
34 | |
35 | QAction *_kgui_createInternal(StandardAction id, QObject *parent) |
36 | { |
37 | QAction *pAction = new QAction(parent); |
38 | const KStandardActionsInfo *pInfo = infoPtr(id); |
39 | |
40 | // qCDebug(KCONFIG_WIDGETS_LOG) << "KStandardActions::create( " << id << "=" << (pInfo ? pInfo->psName : (const char*)0) << ", " << parent << " )"; // |
41 | // ellis |
42 | |
43 | if (pInfo) { |
44 | QString sLabel; |
45 | QString iconName = pInfo->psIconName.toString(); |
46 | |
47 | switch (id) { |
48 | case Back: |
49 | sLabel = QCoreApplication::translate(context: "go back" , key: "&Back" ); |
50 | if (QGuiApplication::isRightToLeft()) { |
51 | iconName = QStringLiteral("go-next" ); |
52 | } |
53 | break; |
54 | |
55 | case Forward: |
56 | sLabel = QCoreApplication::translate(context: "go forward" , key: "&Forward" ); |
57 | if (QGuiApplication::isRightToLeft()) { |
58 | iconName = QStringLiteral("go-previous" ); |
59 | } |
60 | break; |
61 | |
62 | case Home: |
63 | sLabel = QCoreApplication::translate(context: "home page" , key: "&Home" ); |
64 | break; |
65 | case Preferences: |
66 | case AboutApp: |
67 | case HelpContents: { |
68 | QString appDisplayName = QGuiApplication::applicationDisplayName(); |
69 | if (appDisplayName.isEmpty()) { |
70 | appDisplayName = QCoreApplication::applicationName(); |
71 | } |
72 | sLabel = QCoreApplication::translate(context: "KStandardActions" , key: pInfo->psLabel).arg(a: appDisplayName); |
73 | } break; |
74 | default: |
75 | sLabel = QCoreApplication::translate(context: "KStandardActions" , key: pInfo->psLabel); |
76 | } |
77 | |
78 | if (QGuiApplication::isRightToLeft()) { |
79 | switch (id) { |
80 | case Prior: |
81 | iconName = QStringLiteral("go-next-view-page" ); |
82 | break; |
83 | case Next: |
84 | iconName = QStringLiteral("go-previous-view-page" ); |
85 | break; |
86 | case FirstPage: |
87 | iconName = QStringLiteral("go-last-view-page" ); |
88 | break; |
89 | case LastPage: |
90 | iconName = QStringLiteral("go-first-view-page" ); |
91 | break; |
92 | case DocumentBack: |
93 | iconName = QStringLiteral("go-next" ); |
94 | break; |
95 | case DocumentForward: |
96 | iconName = QStringLiteral("go-previous" ); |
97 | break; |
98 | default: |
99 | break; |
100 | } |
101 | } |
102 | |
103 | if (id == Donate) { |
104 | const QString currencyCode = QLocale().currencySymbol(QLocale::CurrencyIsoCode).toLower(); |
105 | if (!currencyCode.isEmpty()) { |
106 | iconName = QStringLiteral("help-donate-%1" ).arg(a: currencyCode); |
107 | } |
108 | } |
109 | |
110 | QIcon icon = iconName.isEmpty() ? QIcon() : QIcon::fromTheme(name: iconName); |
111 | |
112 | if (id == AboutApp) { |
113 | icon = qGuiApp->windowIcon(); |
114 | } |
115 | |
116 | // Set the text before setting the MenuRole, as on OS X setText will do some heuristic role guessing. |
117 | // This ensures user menu items get the intended role out of the list below. |
118 | pAction->setText(sLabel); |
119 | |
120 | switch (id) { |
121 | case Quit: |
122 | pAction->setMenuRole(QAction::QuitRole); |
123 | break; |
124 | |
125 | case Preferences: |
126 | pAction->setMenuRole(QAction::PreferencesRole); |
127 | break; |
128 | |
129 | case AboutApp: |
130 | pAction->setMenuRole(QAction::AboutRole); |
131 | break; |
132 | |
133 | default: |
134 | pAction->setMenuRole(QAction::NoRole); |
135 | break; |
136 | } |
137 | |
138 | if (!QCoreApplication::translate(context: "KStandardActions" , key: pInfo->psToolTip).isEmpty()) { |
139 | pAction->setToolTip(QCoreApplication::translate(context: "KStandardActions" , key: pInfo->psToolTip)); |
140 | } |
141 | pAction->setIcon(icon); |
142 | |
143 | QList<QKeySequence> cut = KStandardShortcut::shortcut(id: pInfo->idAccel); |
144 | if (!cut.isEmpty()) { |
145 | // emulate KActionCollection::setDefaultShortcuts to allow the use of "configure shortcuts" |
146 | pAction->setShortcuts(cut); |
147 | pAction->setProperty(name: "defaultShortcuts" , value: QVariant::fromValue(value: cut)); |
148 | } |
149 | pAction->connect(sender: KStandardShortcut::shortcutWatcher(), |
150 | signal: &KStandardShortcut::StandardShortcutWatcher::shortcutChanged, |
151 | context: pAction, |
152 | slot: [pAction, shortcut = pInfo->idAccel](KStandardShortcut::StandardShortcut id, const QList<QKeySequence> &newShortcut) { |
153 | if (id != shortcut) { |
154 | return; |
155 | } |
156 | pAction->setShortcuts(newShortcut); |
157 | pAction->setProperty(name: "defaultShortcuts" , value: QVariant::fromValue(value: newShortcut)); |
158 | }); |
159 | |
160 | pAction->setObjectName(pInfo->psName.toString()); |
161 | } |
162 | |
163 | if (pAction && parent && parent->inherits(classname: "KActionCollection" )) { |
164 | // clang-format off |
165 | QMetaObject::invokeMethod(obj: parent, member: "addAction" , Q_ARG(QString, pAction->objectName()), Q_ARG(QAction*, pAction)); |
166 | // clang-format on |
167 | } |
168 | |
169 | return pAction; |
170 | } |
171 | |
172 | QString name(StandardAction id) |
173 | { |
174 | const KStandardActionsInfo *pInfo = infoPtr(id); |
175 | return (pInfo) ? pInfo->psName.toString() : QString(); |
176 | } |
177 | } |
178 | |