File indexing completed on 2024-03-24 15:26:00

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999, 2000 Kurt Granroth <granroth@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #ifndef KSTANDARDACTION_PRIVATE_H
0009 #define KSTANDARDACTION_PRIVATE_H
0010 
0011 #include <QAction>
0012 #include <QApplication>
0013 
0014 #include <KLazyLocalizedString>
0015 #include <KLocalizedString>
0016 #include <KStandardShortcut>
0017 
0018 namespace KStandardAction
0019 {
0020 struct KStandardActionInfo {
0021     StandardAction id;
0022     KStandardShortcut::StandardShortcut idAccel;
0023     const char *psName;
0024     const KLazyLocalizedString psLabel;
0025     const KLazyLocalizedString psToolTip;
0026     const char *psIconName;
0027 };
0028 // clang-format off
0029 static const KStandardActionInfo g_rgActionInfo[] = {
0030     { New,           KStandardShortcut::New, "file_new", kli18n("&New"), kli18n("Create new document"), "document-new" },
0031     { Open,          KStandardShortcut::Open, "file_open", kli18n("&Open..."), kli18n("Open an existing document"), "document-open" },
0032     { OpenRecent,    KStandardShortcut::AccelNone, "file_open_recent", kli18n("Open &Recent"), kli18n("Open a document which was recently opened"), "document-open-recent" },
0033     { Save,          KStandardShortcut::Save, "file_save", kli18n("&Save"), kli18n("Save document"), "document-save" },
0034     { SaveAs,        KStandardShortcut::SaveAs, "file_save_as", kli18n("Save &As..."), kli18n("Save document under a new name"), "document-save-as" },
0035     { Revert,        KStandardShortcut::Revert, "file_revert", kli18n("Re&vert"), kli18n("Revert unsaved changes made to document"), "document-revert" },
0036     { Close,         KStandardShortcut::Close, "file_close", kli18n("&Close"), kli18n("Close document"), "document-close" },
0037     { Print,         KStandardShortcut::Print, "file_print", kli18n("&Print..."), kli18n("Print document"), "document-print" },
0038     { PrintPreview,  KStandardShortcut::PrintPreview, "file_print_preview", kli18n("Print Previe&w"), kli18n("Show a print preview of document"), "document-print-preview" },
0039     { Mail,          KStandardShortcut::Mail, "file_mail", kli18n("&Mail..."), kli18n("Send document by mail"), "mail-send" },
0040     { Quit,          KStandardShortcut::Quit, "file_quit", kli18n("&Quit"), kli18n("Quit application"), "application-exit" },
0041 
0042     { Undo,          KStandardShortcut::Undo, "edit_undo", kli18n("&Undo"), kli18n("Undo last action"), "edit-undo" },
0043     { Redo,          KStandardShortcut::Redo, "edit_redo", kli18n("Re&do"), kli18n("Redo last undone action"), "edit-redo" },
0044     { Cut,           KStandardShortcut::Cut, "edit_cut", kli18n("Cu&t"), kli18n("Cut selection to clipboard"), "edit-cut" },
0045     { Copy,          KStandardShortcut::Copy, "edit_copy", kli18n("&Copy"), kli18n("Copy selection to clipboard"), "edit-copy" },
0046     { Paste,         KStandardShortcut::Paste, "edit_paste", kli18n("&Paste"), kli18n("Paste clipboard content"), "edit-paste" },
0047 #if KCONFIGWIDGETS_BUILD_DEPRECATED_SINCE(5, 39)
0048     { PasteText,     KStandardShortcut::Paste, "edit_paste", kli18n("&Paste"), kli18n("Paste clipboard content"), "edit-paste" },
0049 #endif
0050     { Clear,         KStandardShortcut::Clear, "edit_clear", kli18n("C&lear"), {}, "edit-clear" },
0051     { SelectAll,     KStandardShortcut::SelectAll, "edit_select_all", kli18n("Select &All"), {}, "edit-select-all" },
0052     { Deselect,      KStandardShortcut::Deselect, "edit_deselect", kli18n("Dese&lect"), {}, "edit-select-none" },
0053     { Find,          KStandardShortcut::Find, "edit_find", kli18n("&Find..."), {}, "edit-find" },
0054     { FindNext,      KStandardShortcut::FindNext, "edit_find_next", kli18n("Find &Next"), {}, "go-down-search" },
0055     { FindPrev,      KStandardShortcut::FindPrev, "edit_find_prev", kli18n("Find Pre&vious"), {}, "go-up-search" },
0056     { Replace,       KStandardShortcut::Replace, "edit_replace", kli18n("&Replace..."), {}, "edit-find-replace" },
0057 
0058     { ActualSize,    KStandardShortcut::ActualSize, "view_actual_size", kli18n("Zoom to &Actual Size"), kli18n("View document at its actual size"), "zoom-original" },
0059     { FitToPage,     KStandardShortcut::FitToPage, "view_fit_to_page", kli18n("&Fit to Page"), kli18n("Zoom to fit page in window"), "zoom-fit-page" },
0060     { FitToWidth,    KStandardShortcut::FitToWidth, "view_fit_to_width", kli18n("Fit to Page &Width"), kli18n("Zoom to fit page width in window"), "zoom-fit-width" },
0061     { FitToHeight,   KStandardShortcut::FitToHeight, "view_fit_to_height", kli18n("Fit to Page &Height"), kli18n("Zoom to fit page height in window"), "zoom-fit-height" },
0062     { ZoomIn,        KStandardShortcut::ZoomIn, "view_zoom_in", kli18n("Zoom &In"), {}, "zoom-in" },
0063     { ZoomOut,       KStandardShortcut::ZoomOut, "view_zoom_out", kli18n("Zoom &Out"), {}, "zoom-out" },
0064     { Zoom,          KStandardShortcut::Zoom, "view_zoom", kli18n("&Zoom..."), kli18n("Select zoom level"), "zoom" },
0065     { Redisplay,     KStandardShortcut::Reload, "view_redisplay", kli18n("&Refresh"), kli18n("Refresh document"), "view-refresh" },
0066 
0067     { Up,            KStandardShortcut::Up, "go_up", kli18n("&Up"), kli18n("Go up"), "go-up" },
0068     // The following three have special i18n() needs for sLabel
0069     { Back,          KStandardShortcut::Back, "go_back", {}, {}, "go-previous" },
0070     { Forward,       KStandardShortcut::Forward, "go_forward", {}, {}, "go-next" },
0071     { Home,          KStandardShortcut::Home, "go_home", {}, {}, "go-home" },
0072     { Prior,         KStandardShortcut::Prior, "go_previous", kli18n("&Previous Page"), kli18n("Go to previous page"), "go-previous-view-page" },
0073     { Next,          KStandardShortcut::Next, "go_next", kli18n("&Next Page"), kli18n("Go to next page"), "go-next-view-page" },
0074     { Goto,          KStandardShortcut::Goto, "go_goto", kli18n("&Go To..."), {}, nullptr },
0075     { GotoPage,      KStandardShortcut::GotoPage, "go_goto_page", kli18n("&Go to Page..."), {}, "go-jump" },
0076     { GotoLine,      KStandardShortcut::GotoLine, "go_goto_line", kli18n("&Go to Line..."), {}, nullptr },
0077     { FirstPage,     KStandardShortcut::Begin, "go_first", kli18n("&First Page"), kli18n("Go to first page"), "go-first-view-page" },
0078     { LastPage,      KStandardShortcut::End, "go_last", kli18n("&Last Page"), kli18n("Go to last page"), "go-last-view-page" },
0079     { DocumentBack,  KStandardShortcut::DocumentBack, "go_document_back", kli18n("&Back"), kli18n("Go back in document"), "go-previous" },
0080     { DocumentForward, KStandardShortcut::DocumentForward, "go_document_forward", kli18n("&Forward"), kli18n("Go forward in document"), "go-next" },
0081 
0082     { AddBookmark,   KStandardShortcut::AddBookmark, "bookmark_add", kli18n("&Add Bookmark"), {}, "bookmark-new" },
0083     { EditBookmarks, KStandardShortcut::EditBookmarks, "bookmark_edit", kli18n("&Edit Bookmarks..."), {}, "bookmarks-organize" },
0084 
0085     { Spelling,      KStandardShortcut::Spelling, "tools_spelling", kli18n("&Spelling..."), kli18n("Check spelling in document"), "tools-check-spelling" },
0086 
0087     { ShowMenubar,   KStandardShortcut::ShowMenubar, "options_show_menubar", kli18n("Show &Menubar"), kli18n("Show or hide menubar"), "show-menu" },
0088     { ShowToolbar,   KStandardShortcut::ShowToolbar, "options_show_toolbar", kli18n("Show &Toolbar"), kli18n("Show or hide toolbar"), nullptr },
0089     { ShowStatusbar, KStandardShortcut::ShowStatusbar, "options_show_statusbar", kli18n("Show St&atusbar"), kli18n("Show or hide statusbar"), nullptr },
0090     { FullScreen,    KStandardShortcut::FullScreen, "fullscreen", kli18n("F&ull Screen Mode"), {}, "view-fullscreen" },
0091 #if KCONFIGWIDGETS_BUILD_DEPRECATED_SINCE(5, 38)
0092     { SaveOptions,   KStandardShortcut::SaveOptions_DEPRECATED_DO_NOT_USE, "options_save_options", kli18n("&Save Settings"), {}, nullptr },
0093 #endif
0094     { KeyBindings,   KStandardShortcut::KeyBindings, "options_configure_keybinding", kli18n("Configure Keyboard S&hortcuts..."), {}, "configure-shortcuts" },
0095     { Preferences,   KStandardShortcut::Preferences, "options_configure", kli18n("&Configure %1..."), {}, "configure" },
0096     { ConfigureToolbars, KStandardShortcut::ConfigureToolbars, "options_configure_toolbars", kli18n("Configure Tool&bars..."), {}, "configure-toolbars" },
0097     { ConfigureNotifications, KStandardShortcut::ConfigureNotifications, "options_configure_notifications", kli18n("Configure &Notifications..."), {}, "preferences-desktop-notification" },
0098 
0099     // the idea here is that Contents is used in menus, and Help in dialogs, so both share the same
0100     // shortcut
0101 #if KCONFIGWIDGETS_BUILD_DEPRECATED_SINCE(5, 38)
0102     { Help,          KStandardShortcut::Help, "help", {}, {}, "help-contents" },
0103 #endif
0104     { HelpContents,  KStandardShortcut::Help, "help_contents", kli18n("%1 &Handbook"), {}, "help-contents" },
0105     { WhatsThis,     KStandardShortcut::WhatsThis, "help_whats_this", kli18n("What's &This?"), {}, "help-contextual" },
0106     { TipofDay,      KStandardShortcut::TipofDay, "help_show_tip", kli18n("Tip of the &Day"), {}, "help-hint" },
0107     { ReportBug,     KStandardShortcut::ReportBug, "help_report_bug", kli18n("&Report Bug..."), {}, "tools-report-bug" },
0108     { SwitchApplicationLanguage, KStandardShortcut::SwitchApplicationLanguage, "switch_application_language", kli18n("Configure &Language..."), {}, "preferences-desktop-locale" },
0109     { AboutApp,      KStandardShortcut::AccelNone, "help_about_app", kli18n("&About %1"), {}, nullptr },
0110     { AboutKDE,      KStandardShortcut::AccelNone, "help_about_kde", kli18n("About &KDE"), {}, "kde" },
0111     { DeleteFile,    KStandardShortcut::DeleteFile, "deletefile", kli18n("&Delete"), {}, "edit-delete" },
0112     { RenameFile,    KStandardShortcut::RenameFile, "renamefile", kli18n("&Rename..."), {}, "edit-rename" },
0113     { MoveToTrash,   KStandardShortcut::MoveToTrash, "movetotrash", kli18n("&Move to Trash"), {}, "trash-empty" },
0114     { Donate,        KStandardShortcut::Donate, "help_donate", kli18n("&Donate"), {}, "help-donate"},
0115     { HamburgerMenu, KStandardShortcut::AccelNone, "hamburger_menu", kli18n("Open &Menu"), {}, "application-menu" },
0116     { ActionNone,    KStandardShortcut::AccelNone, nullptr, {}, {}, nullptr }
0117 };
0118 // clang-format on
0119 
0120 inline const KStandardActionInfo *infoPtr(StandardAction id)
0121 {
0122     for (uint i = 0; g_rgActionInfo[i].id != ActionNone; i++) {
0123         if (g_rgActionInfo[i].id == id) {
0124             return &g_rgActionInfo[i];
0125         }
0126     }
0127 
0128     return nullptr;
0129 }
0130 
0131 static inline QStringList internal_stdNames()
0132 {
0133     QStringList result;
0134 
0135     for (uint i = 0; g_rgActionInfo[i].id != ActionNone; i++)
0136         if (!g_rgActionInfo[i].psLabel.isEmpty()) {
0137             if (QByteArray(g_rgActionInfo[i].psLabel.untranslatedText()).contains("%1"))
0138             // Prevents KLocalizedString::toString() from complaining about unsubstituted placeholder.
0139             {
0140                 result.append(g_rgActionInfo[i].psLabel.subs(QString()).toString());
0141             } else {
0142                 result.append(g_rgActionInfo[i].psLabel.toString());
0143             }
0144         }
0145 
0146     return result;
0147 }
0148 
0149 class AutomaticAction : public QAction
0150 {
0151     Q_OBJECT
0152 
0153 public:
0154     AutomaticAction(const QIcon &icon, const QString &text, KStandardShortcut::StandardShortcut standardShortcut, const char *slot, QObject *parent);
0155 public Q_SLOTS:
0156     inline void cut()
0157     {
0158         invokeEditSlot("cut");
0159     }
0160     inline void copy()
0161     {
0162         invokeEditSlot("copy");
0163     }
0164     inline void paste()
0165     {
0166         invokeEditSlot("paste");
0167     }
0168     inline void clear()
0169     {
0170         invokeEditSlot("clear");
0171     }
0172     inline void selectAll()
0173     {
0174         invokeEditSlot("selectAll");
0175     }
0176 
0177     void invokeEditSlot(const char *slot)
0178     {
0179         if (qApp->focusWidget()) {
0180             QMetaObject::invokeMethod(qApp->focusWidget(), slot);
0181         }
0182     }
0183 };
0184 
0185 }
0186 
0187 #endif