File indexing completed on 2023-12-03 10:55:13

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     { Clear,         KStandardShortcut::Clear, "edit_clear", kli18n("C&lear"), {}, "edit-clear" },
0048     { SelectAll,     KStandardShortcut::SelectAll, "edit_select_all", kli18n("Select &All"), {}, "edit-select-all" },
0049     { Deselect,      KStandardShortcut::Deselect, "edit_deselect", kli18n("Dese&lect"), {}, "edit-select-none" },
0050     { Find,          KStandardShortcut::Find, "edit_find", kli18n("&Find..."), {}, "edit-find" },
0051     { FindNext,      KStandardShortcut::FindNext, "edit_find_next", kli18n("Find &Next"), {}, "go-down-search" },
0052     { FindPrev,      KStandardShortcut::FindPrev, "edit_find_prev", kli18n("Find Pre&vious"), {}, "go-up-search" },
0053     { Replace,       KStandardShortcut::Replace, "edit_replace", kli18n("&Replace..."), {}, "edit-find-replace" },
0054 
0055     { ActualSize,    KStandardShortcut::ActualSize, "view_actual_size", kli18n("Zoom to &Actual Size"), kli18n("View document at its actual size"), "zoom-original" },
0056     { FitToPage,     KStandardShortcut::FitToPage, "view_fit_to_page", kli18n("&Fit to Page"), kli18n("Zoom to fit page in window"), "zoom-fit-page" },
0057     { FitToWidth,    KStandardShortcut::FitToWidth, "view_fit_to_width", kli18n("Fit to Page &Width"), kli18n("Zoom to fit page width in window"), "zoom-fit-width" },
0058     { FitToHeight,   KStandardShortcut::FitToHeight, "view_fit_to_height", kli18n("Fit to Page &Height"), kli18n("Zoom to fit page height in window"), "zoom-fit-height" },
0059     { ZoomIn,        KStandardShortcut::ZoomIn, "view_zoom_in", kli18n("Zoom &In"), {}, "zoom-in" },
0060     { ZoomOut,       KStandardShortcut::ZoomOut, "view_zoom_out", kli18n("Zoom &Out"), {}, "zoom-out" },
0061     { Zoom,          KStandardShortcut::Zoom, "view_zoom", kli18n("&Zoom..."), kli18n("Select zoom level"), "zoom" },
0062     { Redisplay,     KStandardShortcut::Reload, "view_redisplay", kli18n("&Refresh"), kli18n("Refresh document"), "view-refresh" },
0063 
0064     { Up,            KStandardShortcut::Up, "go_up", kli18n("&Up"), kli18n("Go up"), "go-up" },
0065     // The following three have special i18n() needs for sLabel
0066     { Back,          KStandardShortcut::Back, "go_back", {}, {}, "go-previous" },
0067     { Forward,       KStandardShortcut::Forward, "go_forward", {}, {}, "go-next" },
0068     { Home,          KStandardShortcut::Home, "go_home", {}, {}, "go-home" },
0069     { Prior,         KStandardShortcut::Prior, "go_previous", kli18n("&Previous Page"), kli18n("Go to previous page"), "go-previous-view-page" },
0070     { Next,          KStandardShortcut::Next, "go_next", kli18n("&Next Page"), kli18n("Go to next page"), "go-next-view-page" },
0071     { Goto,          KStandardShortcut::Goto, "go_goto", kli18n("&Go To..."), {}, nullptr },
0072     { GotoPage,      KStandardShortcut::GotoPage, "go_goto_page", kli18n("&Go to Page..."), {}, "go-jump" },
0073     { GotoLine,      KStandardShortcut::GotoLine, "go_goto_line", kli18n("&Go to Line..."), {}, nullptr },
0074     { FirstPage,     KStandardShortcut::Begin, "go_first", kli18n("&First Page"), kli18n("Go to first page"), "go-first-view-page" },
0075     { LastPage,      KStandardShortcut::End, "go_last", kli18n("&Last Page"), kli18n("Go to last page"), "go-last-view-page" },
0076     { DocumentBack,  KStandardShortcut::DocumentBack, "go_document_back", kli18n("&Back"), kli18n("Go back in document"), "go-previous" },
0077     { DocumentForward, KStandardShortcut::DocumentForward, "go_document_forward", kli18n("&Forward"), kli18n("Go forward in document"), "go-next" },
0078 
0079     { AddBookmark,   KStandardShortcut::AddBookmark, "bookmark_add", kli18n("&Add Bookmark"), {}, "bookmark-new" },
0080     { EditBookmarks, KStandardShortcut::EditBookmarks, "bookmark_edit", kli18n("&Edit Bookmarks..."), {}, "bookmarks-organize" },
0081 
0082     { Spelling,      KStandardShortcut::Spelling, "tools_spelling", kli18n("&Spelling..."), kli18n("Check spelling in document"), "tools-check-spelling" },
0083 
0084     { ShowMenubar,   KStandardShortcut::ShowMenubar, "options_show_menubar", kli18n("Show &Menubar"), kli18n("Show or hide menubar"), "show-menu" },
0085     { ShowToolbar,   KStandardShortcut::ShowToolbar, "options_show_toolbar", kli18n("Show &Toolbar"), kli18n("Show or hide toolbar"), nullptr },
0086     { ShowStatusbar, KStandardShortcut::ShowStatusbar, "options_show_statusbar", kli18n("Show St&atusbar"), kli18n("Show or hide statusbar"), nullptr },
0087     { FullScreen,    KStandardShortcut::FullScreen, "fullscreen", kli18n("F&ull Screen Mode"), {}, "view-fullscreen" },
0088     { KeyBindings,   KStandardShortcut::KeyBindings, "options_configure_keybinding", kli18n("Configure Keyboard S&hortcuts..."), {}, "configure-shortcuts" },
0089     { Preferences,   KStandardShortcut::Preferences, "options_configure", kli18n("&Configure %1..."), {}, "configure" },
0090     { ConfigureToolbars, KStandardShortcut::ConfigureToolbars, "options_configure_toolbars", kli18n("Configure Tool&bars..."), {}, "configure-toolbars" },
0091     { ConfigureNotifications, KStandardShortcut::ConfigureNotifications, "options_configure_notifications", kli18n("Configure &Notifications..."), {}, "preferences-desktop-notification" },
0092 
0093     // the idea here is that Contents is used in menus, and Help in dialogs, so both share the same
0094     // shortcut
0095     { HelpContents,  KStandardShortcut::Help, "help_contents", kli18n("%1 &Handbook"), {}, "help-contents" },
0096     { WhatsThis,     KStandardShortcut::WhatsThis, "help_whats_this", kli18n("What's &This?"), {}, "help-contextual" },
0097     { TipofDay,      KStandardShortcut::TipofDay, "help_show_tip", kli18n("Tip of the &Day"), {}, "help-hint" },
0098     { ReportBug,     KStandardShortcut::ReportBug, "help_report_bug", kli18n("&Report Bug..."), {}, "tools-report-bug" },
0099     { SwitchApplicationLanguage, KStandardShortcut::SwitchApplicationLanguage, "switch_application_language", kli18n("Configure &Language..."), {}, "preferences-desktop-locale" },
0100     { AboutApp,      KStandardShortcut::AccelNone, "help_about_app", kli18n("&About %1"), {}, nullptr },
0101     { AboutKDE,      KStandardShortcut::AccelNone, "help_about_kde", kli18n("About &KDE"), {}, "kde" },
0102     { DeleteFile,    KStandardShortcut::DeleteFile, "deletefile", kli18n("&Delete"), {}, "edit-delete" },
0103     { RenameFile,    KStandardShortcut::RenameFile, "renamefile", kli18n("&Rename..."), {}, "edit-rename" },
0104     { MoveToTrash,   KStandardShortcut::MoveToTrash, "movetotrash", kli18n("&Move to Trash"), {}, "trash-empty" },
0105     { Donate,        KStandardShortcut::Donate, "help_donate", kli18n("&Donate"), {}, "help-donate"},
0106     { HamburgerMenu, KStandardShortcut::AccelNone, "hamburger_menu", kli18n("Open &Menu"), {}, "application-menu" },
0107     { ActionNone,    KStandardShortcut::AccelNone, nullptr, {}, {}, nullptr }
0108 };
0109 // clang-format on
0110 
0111 inline const KStandardActionInfo *infoPtr(StandardAction id)
0112 {
0113     for (uint i = 0; g_rgActionInfo[i].id != ActionNone; i++) {
0114         if (g_rgActionInfo[i].id == id) {
0115             return &g_rgActionInfo[i];
0116         }
0117     }
0118 
0119     return nullptr;
0120 }
0121 
0122 static inline QStringList internal_stdNames()
0123 {
0124     QStringList result;
0125 
0126     for (uint i = 0; g_rgActionInfo[i].id != ActionNone; i++)
0127         if (!g_rgActionInfo[i].psLabel.isEmpty()) {
0128             if (QByteArray(g_rgActionInfo[i].psLabel.untranslatedText()).contains("%1"))
0129             // Prevents KLocalizedString::toString() from complaining about unsubstituted placeholder.
0130             {
0131                 result.append(g_rgActionInfo[i].psLabel.subs(QString()).toString());
0132             } else {
0133                 result.append(g_rgActionInfo[i].psLabel.toString());
0134             }
0135         }
0136 
0137     return result;
0138 }
0139 
0140 class AutomaticAction : public QAction
0141 {
0142     Q_OBJECT
0143 
0144 public:
0145     AutomaticAction(const QIcon &icon, const QString &text, KStandardShortcut::StandardShortcut standardShortcut, const char *slot, QObject *parent);
0146 public Q_SLOTS:
0147     inline void cut()
0148     {
0149         invokeEditSlot("cut");
0150     }
0151     inline void copy()
0152     {
0153         invokeEditSlot("copy");
0154     }
0155     inline void paste()
0156     {
0157         invokeEditSlot("paste");
0158     }
0159     inline void clear()
0160     {
0161         invokeEditSlot("clear");
0162     }
0163     inline void selectAll()
0164     {
0165         invokeEditSlot("selectAll");
0166     }
0167 
0168     void invokeEditSlot(const char *slot)
0169     {
0170         if (qApp->focusWidget()) {
0171             QMetaObject::invokeMethod(qApp->focusWidget(), slot);
0172         }
0173     }
0174 };
0175 
0176 }
0177 
0178 #endif