File indexing completed on 2024-05-12 16:02:26

0001 /*
0002  *  SPDX-FileCopyrightText: 2015 Michael Abrahams <miabraha@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #ifndef KIS_ACTION_REGISTRY_H
0008 #define KIS_ACTION_REGISTRY_H
0009 
0010 #include <QString>
0011 #include <QKeySequence>
0012 #include <QDomElement>
0013 #include <QAction>
0014 #include <QList>
0015 
0016 #include "kritawidgetutils_export.h"
0017 
0018 
0019 class KisKActionCollection;
0020 class QDomElement;
0021 class KConfigBase;
0022 class KisShortcutsDialog;
0023 
0024 /**
0025  * KisActionRegistry is intended to manage the global action configuration data
0026  * for Krita. The data come from four sources:
0027  * - .action files, containing static action configuration data in XML format,
0028  * - .rc configuration files, originally from XMLGUI and now in WidgetUtils,
0029  * - kritashortcutsrc, containing temporary shortcut configuration, and
0030  * - .shortcuts scheme files providing sets of default shortcuts, also from XMLGUI
0031  *
0032  * This class can be used as a factory by calling makeQAction. It can be used to
0033  * add standard properties such as default shortcuts and default tooltip to an
0034  * existing action with propertizeAction. If you have a custom action class
0035  * which needs to add other properties, you can use propertizeAction to add any
0036  * sort of data you wish to the .action configuration file.
0037  *
0038  * This class is also in charge of displaying the shortcut configuration dialog.
0039  * The interplay between this class, KisKActionCollection, KisShortcutsEditor and
0040  * so on can be complex, and is sometimes synchronized by file I/O by reading
0041  * and writing the configuration files mentioned above.
0042  *
0043  * It is a global static.  Grab an ::instance().
0044  */
0045 class KRITAWIDGETUTILS_EXPORT KisActionRegistry : public QObject
0046 {
0047     Q_OBJECT
0048 
0049 public:
0050     static KisActionRegistry *instance();
0051 
0052     /**
0053      * @return true if the given action exists
0054      */
0055     bool hasAction(const QString &name) const;
0056 
0057 
0058     /**
0059      * @return value @p property for an action @p name.
0060      *
0061      * Allow flexible info structure for KisActions, etc.
0062      */
0063     QString getActionProperty(const QString &name, const QString &property);
0064 
0065     /**
0066      * Produces a new QAction based on the .action data files.
0067      *
0068      * N.B. this action will not be saved in the registry.
0069      */
0070     QAction *makeQAction(const QString &name, QObject *parent = 0);
0071 
0072     /**
0073      * Fills the standard QAction properties of an action.
0074      *
0075      * @return true if the action was loaded successfully.
0076      */
0077     bool propertizeAction(const QString &name, QAction *a);
0078 
0079     /**
0080      * Called when "OK" button is pressed in settings dialog.
0081      */
0082     void settingsPageSaved();
0083 
0084     /**
0085      * Reload custom shortcuts from kritashortcutsrc
0086      */
0087     void loadCustomShortcuts();
0088 
0089     /**
0090      * Call after settings are changed.
0091      */
0092     void notifySettingsUpdated();
0093 
0094     // If config == 0, reload defaults
0095     void applyShortcutScheme(const KConfigBase *config = 0);
0096 
0097     struct ActionCategory {
0098         ActionCategory();
0099         ActionCategory(const QString &_componentName, const QString &_categoryName);
0100         QString componentName;
0101         QString categoryName;
0102 
0103         bool isValid() const;
0104 
0105     private:
0106         bool m_isValid = false;
0107     };
0108 
0109     ActionCategory fetchActionCategory(const QString &name) const;
0110 
0111     /**
0112      * Constructor.  Please don't touch!
0113      */
0114     KisActionRegistry();
0115     ~KisActionRegistry();
0116 
0117     /**
0118      * @brief loadShortcutScheme
0119      * @param schemeName
0120      */
0121     void loadShortcutScheme(const QString &schemeName);
0122 
0123     // Undocumented
0124     void updateShortcut(const QString &name, QAction *ac);
0125 
0126     bool sanityCheckPropertized(const QString &name);
0127 
0128     QList<QString> registeredShortcutIds() const;
0129 
0130 Q_SIGNALS:
0131     void shortcutsUpdated();
0132 
0133 private:
0134 
0135     class Private;
0136     const QScopedPointer<Private> d;
0137 };
0138 
0139 #endif /* KIS_ACTION_REGISTRY_H */