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 #ifndef KSTANDARDACTION_PRIVATE_H
9 #define KSTANDARDACTION_PRIVATE_H
10 
11 #include <QAction>
12 #include <QApplication>
13 
14 #include <KLazyLocalizedString>
15 #include <KLocalizedString>
16 #include <KStandardShortcut>
17 
18 namespace KStandardAction
19 {
20 struct KStandardActionInfo {
21     StandardAction id;
22     KStandardShortcut::StandardShortcut idAccel;
23     const char *psName;
24     const KLazyLocalizedString psLabel;
25     const KLazyLocalizedString psToolTip;
26     const char *psIconName;
27 };
28 // clang-format off
29 static const KStandardActionInfo g_rgActionInfo[] = {
30     { New,           KStandardShortcut::New, "file_new", kli18n("&New"), kli18n("Create new document"), "document-new" },
31     { Open,          KStandardShortcut::Open, "file_open", kli18n("&Open..."), kli18n("Open an existing document"), "document-open" },
32     { OpenRecent,    KStandardShortcut::AccelNone, "file_open_recent", kli18n("Open &Recent"), kli18n("Open a document which was recently opened"), "document-open-recent" },
33     { Save,          KStandardShortcut::Save, "file_save", kli18n("&Save"), kli18n("Save document"), "document-save" },
34     { SaveAs,        KStandardShortcut::SaveAs, "file_save_as", kli18n("Save &As..."), kli18n("Save document under a new name"), "document-save-as" },
35     { Revert,        KStandardShortcut::Revert, "file_revert", kli18n("Re&vert"), kli18n("Revert unsaved changes made to document"), "document-revert" },
36     { Close,         KStandardShortcut::Close, "file_close", kli18n("&Close"), kli18n("Close document"), "document-close" },
37     { Print,         KStandardShortcut::Print, "file_print", kli18n("&Print..."), kli18n("Print document"), "document-print" },
38     { PrintPreview,  KStandardShortcut::PrintPreview, "file_print_preview", kli18n("Print Previe&w"), kli18n("Show a print preview of document"), "document-print-preview" },
39     { Mail,          KStandardShortcut::Mail, "file_mail", kli18n("&Mail..."), kli18n("Send document by mail"), "mail-send" },
40     { Quit,          KStandardShortcut::Quit, "file_quit", kli18n("&Quit"), kli18n("Quit application"), "application-exit" },
41 
42     { Undo,          KStandardShortcut::Undo, "edit_undo", kli18n("&Undo"), kli18n("Undo last action"), "edit-undo" },
43     { Redo,          KStandardShortcut::Redo, "edit_redo", kli18n("Re&do"), kli18n("Redo last undone action"), "edit-redo" },
44     { Cut,           KStandardShortcut::Cut, "edit_cut", kli18n("Cu&t"), kli18n("Cut selection to clipboard"), "edit-cut" },
45     { Copy,          KStandardShortcut::Copy, "edit_copy", kli18n("&Copy"), kli18n("Copy selection to clipboard"), "edit-copy" },
46     { Paste,         KStandardShortcut::Paste, "edit_paste", kli18n("&Paste"), kli18n("Paste clipboard content"), "edit-paste" },
47 #if KCONFIGWIDGETS_BUILD_DEPRECATED_SINCE(5, 39)
48     { PasteText,     KStandardShortcut::Paste, "edit_paste", kli18n("&Paste"), kli18n("Paste clipboard content"), "edit-paste" },
49 #endif
50     { Clear,         KStandardShortcut::Clear, "edit_clear", kli18n("C&lear"), {}, "edit-clear" },
51     { SelectAll,     KStandardShortcut::SelectAll, "edit_select_all", kli18n("Select &All"), {}, "edit-select-all" },
52     { Deselect,      KStandardShortcut::Deselect, "edit_deselect", kli18n("Dese&lect"), {}, "edit-select-none" },
53     { Find,          KStandardShortcut::Find, "edit_find", kli18n("&Find..."), {}, "edit-find" },
54     { FindNext,      KStandardShortcut::FindNext, "edit_find_next", kli18n("Find &Next"), {}, "go-down-search" },
55     { FindPrev,      KStandardShortcut::FindPrev, "edit_find_prev", kli18n("Find Pre&vious"), {}, "go-up-search" },
56     { Replace,       KStandardShortcut::Replace, "edit_replace", kli18n("&Replace..."), {}, "edit-find-replace" },
57 
58     { ActualSize,    KStandardShortcut::ActualSize, "view_actual_size", kli18n("&Actual Size"), kli18n("View document at its actual size"), "zoom-original" },
59     { FitToPage,     KStandardShortcut::FitToPage, "view_fit_to_page", kli18n("&Fit to Page"), kli18n("Zoom to fit page in window"), "zoom-fit-page" },
60     { FitToWidth,    KStandardShortcut::FitToWidth, "view_fit_to_width", kli18n("Fit to Page &Width"), kli18n("Zoom to fit page width in window"), "zoom-fit-width" },
61     { FitToHeight,   KStandardShortcut::FitToHeight, "view_fit_to_height", kli18n("Fit to Page &Height"), kli18n("Zoom to fit page height in window"), "zoom-fit-height" },
62     { ZoomIn,        KStandardShortcut::ZoomIn, "view_zoom_in", kli18n("Zoom &In"), {}, "zoom-in" },
63     { ZoomOut,       KStandardShortcut::ZoomOut, "view_zoom_out", kli18n("Zoom &Out"), {}, "zoom-out" },
64     { Zoom,          KStandardShortcut::Zoom, "view_zoom", kli18n("&Zoom..."), kli18n("Select zoom level"), "zoom" },
65     { Redisplay,     KStandardShortcut::Reload, "view_redisplay", kli18n("&Refresh"), kli18n("Refresh document"), "view-refresh" },
66 
67     { Up,            KStandardShortcut::Up, "go_up", kli18n("&Up"), kli18n("Go up"), "go-up" },
68     // The following three have special i18n() needs for sLabel
69     { Back,          KStandardShortcut::Back, "go_back", {}, {}, "go-previous" },
70     { Forward,       KStandardShortcut::Forward, "go_forward", {}, {}, "go-next" },
71     { Home,          KStandardShortcut::Home, "go_home", {}, {}, "go-home" },
72     { Prior,         KStandardShortcut::Prior, "go_previous", kli18n("&Previous Page"), kli18n("Go to previous page"), "go-previous-view-page" },
73     { Next,          KStandardShortcut::Next, "go_next", kli18n("&Next Page"), kli18n("Go to next page"), "go-next-view-page" },
74     { Goto,          KStandardShortcut::Goto, "go_goto", kli18n("&Go To..."), {}, nullptr },
75     { GotoPage,      KStandardShortcut::GotoPage, "go_goto_page", kli18n("&Go to Page..."), {}, "go-jump" },
76     { GotoLine,      KStandardShortcut::GotoLine, "go_goto_line", kli18n("&Go to Line..."), {}, nullptr },
77     { FirstPage,     KStandardShortcut::Begin, "go_first", kli18n("&First Page"), kli18n("Go to first page"), "go-first-view-page" },
78     { LastPage,      KStandardShortcut::End, "go_last", kli18n("&Last Page"), kli18n("Go to last page"), "go-last-view-page" },
79     { DocumentBack,  KStandardShortcut::DocumentBack, "go_document_back", kli18n("&Back"), kli18n("Go back in document"), "go-previous" },
80     { DocumentForward, KStandardShortcut::DocumentForward, "go_document_forward", kli18n("&Forward"), kli18n("Go forward in document"), "go-next" },
81 
82     { AddBookmark,   KStandardShortcut::AddBookmark, "bookmark_add", kli18n("&Add Bookmark"), {}, "bookmark-new" },
83     { EditBookmarks, KStandardShortcut::EditBookmarks, "bookmark_edit", kli18n("&Edit Bookmarks..."), {}, "bookmarks-organize" },
84 
85     { Spelling,      KStandardShortcut::Spelling, "tools_spelling", kli18n("&Spelling..."), kli18n("Check spelling in document"), "tools-check-spelling" },
86 
87     { ShowMenubar,   KStandardShortcut::ShowMenubar, "options_show_menubar", kli18n("Show &Menubar"), kli18n("Show or hide menubar"), "show-menu" },
88     { ShowToolbar,   KStandardShortcut::ShowToolbar, "options_show_toolbar", kli18n("Show &Toolbar"), kli18n("Show or hide toolbar"), nullptr },
89     { ShowStatusbar, KStandardShortcut::ShowStatusbar, "options_show_statusbar", kli18n("Show St&atusbar"), kli18n("Show or hide statusbar"), nullptr },
90     { FullScreen,    KStandardShortcut::FullScreen, "fullscreen", kli18n("F&ull Screen Mode"), {}, "view-fullscreen" },
91 #if KCONFIGWIDGETS_BUILD_DEPRECATED_SINCE(5, 38)
92     { SaveOptions,   KStandardShortcut::SaveOptions_DEPRECATED_DO_NOT_USE, "options_save_options", kli18n("&Save Settings"), {}, nullptr },
93 #endif
94     { KeyBindings,   KStandardShortcut::KeyBindings, "options_configure_keybinding", kli18n("Configure Keyboard S&hortcuts..."), {}, "configure-shortcuts" },
95     { Preferences,   KStandardShortcut::Preferences, "options_configure", kli18n("&Configure %1..."), {}, "configure" },
96     { ConfigureToolbars, KStandardShortcut::ConfigureToolbars, "options_configure_toolbars", kli18n("Configure Tool&bars..."), {}, "configure-toolbars" },
97     { ConfigureNotifications, KStandardShortcut::ConfigureNotifications, "options_configure_notifications", kli18n("Configure &Notifications..."), {}, "preferences-desktop-notification" },
98 
99     // the idea here is that Contents is used in menus, and Help in dialogs, so both share the same
100     // shortcut
101 #if KCONFIGWIDGETS_BUILD_DEPRECATED_SINCE(5, 38)
102     { Help,          KStandardShortcut::Help, "help", {}, {}, "help-contents" },
103 #endif
104     { HelpContents,  KStandardShortcut::Help, "help_contents", kli18n("%1 &Handbook"), {}, "help-contents" },
105     { WhatsThis,     KStandardShortcut::WhatsThis, "help_whats_this", kli18n("What's &This?"), {}, "help-contextual" },
106     { TipofDay,      KStandardShortcut::TipofDay, "help_show_tip", kli18n("Tip of the &Day"), {}, "help-hint" },
107     { ReportBug,     KStandardShortcut::ReportBug, "help_report_bug", kli18n("&Report Bug..."), {}, "tools-report-bug" },
108     { SwitchApplicationLanguage, KStandardShortcut::SwitchApplicationLanguage, "switch_application_language", kli18n("Configure &Language..."), {}, "preferences-desktop-locale" },
109     { AboutApp,      KStandardShortcut::AccelNone, "help_about_app", kli18n("&About %1"), {}, nullptr },
110     { AboutKDE,      KStandardShortcut::AccelNone, "help_about_kde", kli18n("About &KDE"), {}, "kde" },
111     { DeleteFile,    KStandardShortcut::DeleteFile, "deletefile", kli18n("&Delete"), {}, "edit-delete" },
112     { RenameFile,    KStandardShortcut::RenameFile, "renamefile", kli18n("&Rename..."), {}, "edit-rename" },
113     { MoveToTrash,   KStandardShortcut::MoveToTrash, "movetotrash", kli18n("&Move to Trash"), {}, "trash-empty" },
114     { Donate,        KStandardShortcut::Donate, "help_donate", kli18n("&Donate"), {}, "help-donate"},
115     { HamburgerMenu, KStandardShortcut::AccelNone, "hamburger_menu", kli18n("Open &Menu"), {}, "application-menu" },
116     { ActionNone,    KStandardShortcut::AccelNone, nullptr, {}, {}, nullptr }
117 };
118 // clang-format on
119 
infoPtr(StandardAction id)120 inline const KStandardActionInfo *infoPtr(StandardAction id)
121 {
122     for (uint i = 0; g_rgActionInfo[i].id != ActionNone; i++) {
123         if (g_rgActionInfo[i].id == id) {
124             return &g_rgActionInfo[i];
125         }
126     }
127 
128     return nullptr;
129 }
130 
internal_stdNames()131 static inline QStringList internal_stdNames()
132 {
133     QStringList result;
134 
135     for (uint i = 0; g_rgActionInfo[i].id != ActionNone; i++)
136         if (!g_rgActionInfo[i].psLabel.isEmpty()) {
137             if (QByteArray(g_rgActionInfo[i].psLabel.untranslatedText()).contains("%1"))
138             // Prevents KLocalizedString::toString() from complaining about unsubstituted placeholder.
139             {
140                 result.append(g_rgActionInfo[i].psLabel.subs(QString()).toString());
141             } else {
142                 result.append(g_rgActionInfo[i].psLabel.toString());
143             }
144         }
145 
146     return result;
147 }
148 
149 class AutomaticAction : public QAction
150 {
151     Q_OBJECT
152 
153 public:
154     AutomaticAction(const QIcon &icon, const QString &text, const QList<QKeySequence> &shortcut, const char *slot, QObject *parent);
155 public Q_SLOTS:
cut()156     inline void cut()
157     {
158         invokeEditSlot("cut");
159     }
copy()160     inline void copy()
161     {
162         invokeEditSlot("copy");
163     }
paste()164     inline void paste()
165     {
166         invokeEditSlot("paste");
167     }
clear()168     inline void clear()
169     {
170         invokeEditSlot("clear");
171     }
selectAll()172     inline void selectAll()
173     {
174         invokeEditSlot("selectAll");
175     }
176 
invokeEditSlot(const char * slot)177     void invokeEditSlot(const char *slot)
178     {
179         if (qApp->focusWidget()) {
180             QMetaObject::invokeMethod(qApp->focusWidget(), slot);
181         }
182     }
183 };
184 
185 }
186 
187 #endif
188