File indexing completed on 2024-10-06 06:44:58

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     /**
0149      * This static method shows a modal dialog that can be used to configure
0150      * the shortcuts associated with each action in @p collection. The new
0151      * shortcut settings will be saved and applied if the dialog is accepted.
0152      *
0153      * The dialog is opened by using @c QDialog::show(), and it will be deleted
0154      * automatically when it's closed.
0155      *
0156      * Example:
0157      * @code
0158      * // Typical usage is with a KActionCollection:
0159      * KShortcutsDialog::showDialog(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, parent);
0160      * @endcode
0161      *
0162      * @param collection the KActionCollection to configure
0163      * @param allowLetterShortcuts set to @c KShortcutsEditor::LetterShortcutsDisallowed
0164      * if unmodified alphanumeric keys ('A', '1', etc.) are not permissible shortcuts
0165      * @param parent the parent widget of the dialog, if not @c nullptr it will be used
0166      * by the window manager to place the dialog relative to it
0167      *
0168      * @since 5.84
0169      */
0170     static void showDialog(KActionCollection *collection,
0171                            KShortcutsEditor::LetterShortcuts allowLetterShortcuts = KShortcutsEditor::LetterShortcutsAllowed,
0172                            QWidget *parent = nullptr);
0173 
0174     /**
0175      * Imports a shortcuts set up from @p path
0176      *
0177      * @since 5.15
0178      */
0179     void importConfiguration(const QString &path);
0180 
0181     /**
0182      * Exports a shortcuts set up from @p path
0183      *
0184      * @since 5.15
0185      */
0186     void exportConfiguration(const QString &path) const;
0187 
0188     /**
0189      * Reloads the list of schemes in the "Manage Schemes" section
0190      *
0191      * This is useful if the available scheme files changed for some reason
0192      * eg. because KNewStuff was used
0193      *
0194      * @since 5.97
0195      */
0196     void refreshSchemes();
0197 
0198     /**
0199      * This adds a @c QAction to the "More Actions" menu
0200      *
0201      * This is useful to add for example an action to download more
0202      * keyboard schemes via KNewStuff
0203      *
0204      * @since 5.97
0205      */
0206     void addActionToSchemesMoreButton(QAction *action);
0207 
0208 public Q_SLOTS:
0209     /**
0210      * @reimp
0211      */
0212     void accept() override;
0213 
0214 Q_SIGNALS:
0215     /**
0216      * Emitted after the dialog is accepted (by clicking the OK button) and settings are saved.
0217      * Connect to this signal if you need to run some extra code after the settings are saved.
0218      */
0219     void saved();
0220 
0221 private:
0222     friend class KShortcutsDialogPrivate;
0223     std::unique_ptr<class KShortcutsDialogPrivate> const d;
0224 
0225     Q_DISABLE_COPY(KShortcutsDialog)
0226 };
0227 
0228 #endif // KSHORTCUTSDIALOG_H