File indexing completed on 2024-04-14 14:30:02

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1997 Nicolas Hadacek <hadacek@kde.org>
0004     SPDX-FileCopyrightText: 2001, 2001 Ellis Whitehead <ellis@kde.org>
0005     SPDX-FileCopyrightText: 2006 Hamish Rodda <rodda@kde.org>
0006     SPDX-FileCopyrightText: 2007 Roberto Raggi <roberto@kdevelop.org>
0007     SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
0008     SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>
0009 
0010     SPDX-License-Identifier: LGPL-2.0-or-later
0011 */
0012 
0013 #ifndef KSHORTCUTSDIALOG_H
0014 #define KSHORTCUTSDIALOG_H
0015 
0016 #include <kxmlgui_export.h>
0017 
0018 #include <QDialog>
0019 #include <memory>
0020 
0021 #include "kshortcutseditor.h"
0022 
0023 /**
0024  * @class KShortcutsDialog kshortcutsdialog.h KShortcutsDialog
0025  *
0026  * @short Dialog for configuration of KActionCollection and KGlobalAccel.
0027  *
0028  * This dialog can be used to configure keyboard shortcuts associated with actions
0029  * in KActionCollection and KGlobalAccel. It integrates the KShortcutsEditor widget and
0030  * offers various functionalities related to keyboard shortcuts (e.g. reset all shortcuts
0031  * to defaults, manage shortcuts schemes ...etc).
0032  *
0033  * Several static methods are supplied, which provide a convenient interface to the dialog.
0034  * The most common, and most encouraged, use is with a KActionCollection.
0035  *
0036  * @code
0037  * KShortcutsDialog::configure(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, parent);
0038  *
0039  * // Alternatively, since 5.84, you can use:
0040  * KShortcutsDialog::showDialog(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, parent);
0041  * @endcode
0042  *
0043  * By default this dialog is modal (since 4.3). If you don't want that, call @c setModal(false)
0044  * and then the non-static configure() method to show the dialog.
0045  *
0046  * If you need to run some extra code when the dialog is closed, connect to the signals
0047  * emitted by @c QDialog (accepted(), rejected(), finished() ...etc.). However, note
0048  * that if that extra code depends on the changed settings having already been saved,
0049  * connect to the @c saved() signal instead to be on the safe side (if you connect to
0050  * e.g. @c QDialog::accepted() your function might be called before the changes have
0051  * actually been saved).
0052  *
0053  * @note Starting from 5.95, if there are no Global shortcuts in any of the action
0054  * collections shown in the dialog, the relevant columns ("Global" and "Global Alternate")
0055  * will be hidden.
0056  *
0057  * Example:
0058  * @code
0059  * // Create the dialog; alternatively you can use the other constructor if e.g.
0060  * // you need to only show certain action types, or disallow single letter shortcuts
0061  * KShortcutsDialog *dlg = new KShortcutsDialog(parent);
0062  *
0063  * // Set the Qt::WA_DeleteOnClose attribute, so that the dialog is automatically
0064  * // deleted after it's closed
0065  * dlg->setAttribute(Qt::WA_DeleteOnClose);
0066  *
0067  * // Add an action collection (otherwise the dialog would be empty)
0068  * dlg->addCollection(actionCollection());
0069  *
0070  * // Run some extra code after the settings are saved
0071  * connect(dlg, &KShortcutsDialog::saved, this, &ClassFoo::doExtraStuff);
0072  *
0073  * // Called with "true" so that the changes are saved if the dialog is accepted,
0074  * // see the configure(bool) method for more details
0075  * dlg->configure(true);
0076  * @endcode
0077  *
0078  * @image html kshortcutsdialog.png "KShortcutsDialog"
0079  *
0080  * @author Nicolas Hadacek <hadacek@via.ecp.fr>
0081  * @author Hamish Rodda <rodda@kde.org> (KDE 4 porting)
0082  * @author Michael Jansen <kde@michael-jansen.biz>
0083  */
0084 class KXMLGUI_EXPORT KShortcutsDialog : public QDialog
0085 {
0086     Q_OBJECT
0087 
0088 public:
0089     /**
0090      * Constructs a KShortcutsDialog as a child of @p parent.
0091      *
0092      * @param actionTypes sets the action types to be shown in the dialog,
0093      * this is an OR'ed combination of @c KShortcutsEditor::ActionTypes;
0094      * the default is @c KShortcutsEditor::AllActions
0095      * @param allowLetterShortcuts set to false if unmodified alphanumeric
0096      * keys ('A', '1', etc.) are not permissible shortcuts; @see @c KShortcutsEditor::LetterShortcuts
0097      * @param parent parent widget of the dialog, if not @c nullptr will be
0098      * used by the window manager to place the dialog relative to it
0099      */
0100     explicit KShortcutsDialog(KShortcutsEditor::ActionTypes actionTypes = KShortcutsEditor::AllActions,
0101                               KShortcutsEditor::LetterShortcuts allowLetterShortcuts = KShortcutsEditor::LetterShortcutsAllowed,
0102                               QWidget *parent = nullptr);
0103     /**
0104      * Constructs a KShortcutsDialog as a child of @p parent, with the default settings
0105      * (@ref KShortcutsEditor::AllActions and @ref KShortcutsEditor::LetterShortcutsAllowed).
0106      *
0107      * Typically a @ref KActionCollection is added by using @ref addCollection().
0108      *
0109      * @param parent the parent widget of the dialog, if not @c nullptr will be
0110      * used by the window manager to place the dialog relative to it
0111      *
0112      * @since 5.85
0113      */
0114     explicit KShortcutsDialog(QWidget *parent);
0115 
0116     /**
0117      * Destructor. Deletes all resources used by a KShortcutsDialog object.
0118      */
0119     ~KShortcutsDialog() override;
0120 
0121     /**
0122      * Add all actions of the collection to the ones displayed and configured
0123      * by the dialog.
0124      *
0125      * @param title the title associated with the collection (if null, the
0126      * KAboutData::progName() of the collection's componentData is used)
0127      */
0128     void addCollection(KActionCollection *collection, const QString &title = {});
0129 
0130     /**
0131      * @return the list of action collections that are available for configuration in the dialog.
0132      */
0133     QList<KActionCollection *> actionCollections() const;
0134 
0135     /**
0136      * Run the dialog and call writeSettings() on the action collections
0137      * that were added if @p saveSettings is true.
0138      *
0139      * If the dialog is modal this method will use @c QDialog::exec() to open the dialog,
0140      * otherwise it will use @c QDialog::show().
0141      */
0142     // TODO KF6 remove this method, see configure() static method for more details
0143     bool configure(bool saveSettings = true);
0144 
0145     /** @see QWidget::sizeHint() */
0146     QSize sizeHint() const override;
0147 
0148 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 84)
0149     /**
0150      * Pops up a modal dialog for configuring key settings. The new
0151      * shortcut settings will become active if the user presses OK.
0152      *
0153      * @param collection the KActionCollection to configure
0154      * @param allowLetterShortcuts set to KShortcutsEditor::LetterShortcutsDisallowed if unmodified alphanumeric
0155      *  keys ('A', '1', etc.) are not permissible shortcuts.
0156      * @param parent the parent widget to attach to
0157      * @param bSaveSettings if true, the settings will also be saved back
0158      * by calling writeSettings() on the action collections that were added.
0159      *
0160      * @return Accept if the dialog was closed with OK, Reject otherwise.
0161      *
0162      * @deprecated since 5.84, use KShortcutsDialog::showDialog() instead.
0163      */
0164     KXMLGUI_DEPRECATED_VERSION(5, 84, "Use KShortcutsDialog::showDialog() instead, see API documentation for code examples.")
0165     static int configure(KActionCollection *collection,
0166                          KShortcutsEditor::LetterShortcuts allowLetterShortcuts = KShortcutsEditor::LetterShortcutsAllowed,
0167                          QWidget *parent = nullptr,
0168                          bool bSaveSettings = true);
0169 #endif
0170 
0171     /**
0172      * This static method shows a modal dialog that can be used to configure
0173      * the shortcuts associated with each action in @p collection. The new
0174      * shortcut settings will be saved and applied if the dialog is accepted.
0175      *
0176      * The dialog is opened by using @c QDialog::show(), and it will be deleted
0177      * automatically when it's closed.
0178      *
0179      * Example:
0180      * @code
0181      * // Typical usage is with a KActionCollection:
0182      * KShortcutsDialog::showDialog(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, parent);
0183      * @endcode
0184      *
0185      * @param collection the KActionCollection to configure
0186      * @param allowLetterShortcuts set to @c KShortcutsEditor::LetterShortcutsDisallowed
0187      * if unmodified alphanumeric keys ('A', '1', etc.) are not permissible shortcuts
0188      * @param parent the parent widget of the dialog, if not @c nullptr it will be used
0189      * by the window manager to place the dialog relative to it
0190      *
0191      * @since 5.84
0192      */
0193     static void showDialog(KActionCollection *collection,
0194                            KShortcutsEditor::LetterShortcuts allowLetterShortcuts = KShortcutsEditor::LetterShortcutsAllowed,
0195                            QWidget *parent = nullptr);
0196 
0197     /**
0198      * Imports a shortcuts set up from @p path
0199      *
0200      * @since 5.15
0201      */
0202     void importConfiguration(const QString &path);
0203 
0204     /**
0205      * Exports a shortcuts set up from @p path
0206      *
0207      * @since 5.15
0208      */
0209     void exportConfiguration(const QString &path) const;
0210 
0211     /**
0212      * Reloads the list of schemes in the "Manage Schemes" section
0213      *
0214      * This is useful if the available scheme files changed for some reason
0215      * eg. because KNewStuff was used
0216      *
0217      * @since 5.97
0218      */
0219     void refreshSchemes();
0220 
0221     /**
0222      * This adds a @c QAction to the "More Actions" menu
0223      *
0224      * This is useful to add for example an action to download more
0225      * keyboard schemes via KNewStuff
0226      *
0227      * @since 5.97
0228      */
0229     void addActionToSchemesMoreButton(QAction *action);
0230 
0231 public Q_SLOTS:
0232     /**
0233      * @reimp
0234      */
0235     void accept() override;
0236 
0237 Q_SIGNALS:
0238     /**
0239      * Emitted after the dialog is accepted (by clicking the OK button) and settings are saved.
0240      * Connect to this signal if you need to run some extra code after the settings are saved.
0241      */
0242     void saved();
0243 
0244 private:
0245     friend class KShortcutsDialogPrivate;
0246     std::unique_ptr<class KShortcutsDialogPrivate> const d;
0247 
0248     Q_DISABLE_COPY(KShortcutsDialog)
0249 };
0250 
0251 #endif // KSHORTCUTSDIALOG_H