File indexing completed on 2024-10-06 06:45:02

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
0004     SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #ifndef kxmlguifactory_h
0010 #define kxmlguifactory_h
0011 
0012 #include <kxmlgui_export.h>
0013 
0014 #include <QObject>
0015 #include <memory>
0016 
0017 class QAction;
0018 class KXMLGUIFactoryPrivate;
0019 class KXMLGUIClient;
0020 class KXMLGUIBuilder;
0021 
0022 class QDomAttr;
0023 class QDomDocument;
0024 class QDomElement;
0025 class QDomNode;
0026 class QDomNamedNodeMap;
0027 
0028 namespace KXMLGUI
0029 {
0030 struct MergingIndex;
0031 struct ContainerNode;
0032 struct ContainerClient;
0033 class BuildHelper;
0034 }
0035 
0036 /**
0037  * @class KXMLGUIFactory kxmlguifactory.h KXMLGUIFactory
0038  *
0039  * KXMLGUIFactory, together with KXMLGUIClient objects, can be used to create
0040  * a GUI of container widgets (like menus, toolbars, etc.) and container items
0041  * (menu items, toolbar buttons, etc.) from an XML document and action objects.
0042  *
0043  * Each KXMLGUIClient represents a part of the GUI, composed from containers and
0044  * actions. KXMLGUIFactory takes care of building (with the help of a KXMLGUIBuilder)
0045  * and merging the GUI from an unlimited number of clients.
0046  *
0047  * Each client provides XML through a QDomDocument and actions through a
0048  * KActionCollection . The XML document contains the rules for how to merge the
0049  * GUI.
0050  *
0051  * KXMLGUIFactory processes the DOM tree provided by a client and plugs in the client's actions,
0052  * according to the XML and the merging rules of previously inserted clients. Container widgets
0053  * are built via a KXMLGUIBuilder , which has to be provided with the KXMLGUIFactory constructor.
0054  */
0055 class KXMLGUI_EXPORT KXMLGUIFactory : public QObject
0056 {
0057     friend class KXMLGUI::BuildHelper;
0058     Q_OBJECT
0059 public:
0060     /**
0061      * Constructs a KXMLGUIFactory. The provided @p builder KXMLGUIBuilder will be called
0062      * for creating and removing container widgets, when clients are added/removed from the GUI.
0063      *
0064      * Note that the ownership of the given KXMLGUIBuilder object won't be transferred to this
0065      * KXMLGUIFactory, so you have to take care of deleting it properly.
0066      */
0067     explicit KXMLGUIFactory(KXMLGUIBuilder *builder, QObject *parent = nullptr);
0068 
0069     /**
0070      * Destructor
0071      */
0072     ~KXMLGUIFactory() override;
0073 
0074     // XXX move to somewhere else? (Simon)
0075     /// @internal
0076     static QString readConfigFile(const QString &filename, const QString &componentName = QString());
0077     /// @internal
0078     static bool saveConfigFile(const QDomDocument &doc, const QString &filename, const QString &componentName = QString());
0079 
0080     /**
0081      * @internal
0082      * Find or create the ActionProperties element, used when saving custom action properties
0083      */
0084     static QDomElement actionPropertiesElement(QDomDocument &doc);
0085 
0086     /**
0087      * @internal
0088      * Find or create the element for a given action, by name.
0089      * Used when saving custom action properties
0090      */
0091     static QDomElement findActionByName(QDomElement &elem, const QString &sName, bool create);
0092 
0093     /**
0094      * Creates the GUI described by the QDomDocument of the client,
0095      * using the client's actions, and merges it with the previously
0096      * created GUI.
0097      * This also means that the order in which clients are added to the factory
0098      * is relevant; assuming that your application supports plugins, you should
0099      * first add your application to the factory and then the plugin, so that the
0100      * plugin's UI is merged into the UI of your application, and not the other
0101      * way round.
0102      */
0103     void addClient(KXMLGUIClient *client);
0104 
0105     /**
0106      * Removes the GUI described by the client, by unplugging all
0107      * provided actions and removing all owned containers (and storing
0108      * container state information in the given client)
0109      */
0110     void removeClient(KXMLGUIClient *client);
0111 
0112     void plugActionList(KXMLGUIClient *client, const QString &name, const QList<QAction *> &actionList);
0113     void unplugActionList(KXMLGUIClient *client, const QString &name);
0114 
0115     /**
0116      * Returns a list of all clients currently added to this factory
0117      */
0118     QList<KXMLGUIClient *> clients() const;
0119 
0120     /**
0121      * Use this method to get access to a container widget with the name specified with @p containerName
0122      * and which is owned by the @p client. The container name is specified with a "name" attribute in the
0123      * XML document.
0124      *
0125      * This function is particularly useful for getting hold of a popupmenu defined in an XMLUI file.
0126      * For instance:
0127      * \code
0128      * QMenu *popup = static_cast<QMenu*>(guiFactory()->container("my_popup",this));
0129      * \endcode
0130      * where @p "my_popup" is the name of the menu in the XMLUI file, and
0131      * @p "this" is XMLGUIClient which owns the popupmenu (e.g. the mainwindow, or the part, or the plugin...)
0132      *
0133      * @param containerName Name of the container widget
0134      * @param client Owner of the container widget
0135      * @param useTagName Specifies whether to compare the specified name with the name attribute or
0136      *        the tag name.
0137      *
0138      * This method may return nullptr if no container with the given name exists or is not owned by the client.
0139      */
0140     QWidget *container(const QString &containerName, KXMLGUIClient *client, bool useTagName = false);
0141 
0142     QList<QWidget *> containers(const QString &tagName);
0143 
0144     /**
0145      * Use this method to free all memory allocated by the KXMLGUIFactory. This deletes the internal node
0146      * tree and therefore resets the internal state of the class. Please note that the actual GUI is
0147      * NOT touched at all, meaning no containers are deleted nor any actions unplugged. That is
0148      * something you have to do on your own. So use this method only if you know what you are doing :-)
0149      *
0150      * (also note that this will call KXMLGUIClient::setFactory(nullptr) for all inserted clients)
0151      */
0152     void reset();
0153 
0154     /**
0155      * Use this method to free all memory allocated by the KXMLGUIFactory for a specific container,
0156      * including all child containers and actions. This deletes the internal node subtree for the
0157      * specified container. The actual GUI is not touched, no containers are deleted or any actions
0158      * unplugged. Use this method only if you know what you are doing :-)
0159      *
0160      * (also note that this will call KXMLGUIClient::setFactory(nullptr) for all clients of the
0161      * container)
0162      */
0163     void resetContainer(const QString &containerName, bool useTagName = false);
0164 
0165     /**
0166      * Use this method to reset and reread action properties (shortcuts, etc.) for all actions.
0167      * This is needed, for example, when you change shortcuts scheme at runtime.
0168      */
0169     void refreshActionProperties();
0170 
0171 public Q_SLOTS:
0172     /**
0173      * Shows a dialog (KShortcutsDialog) that lists every action in this factory,
0174      * and which can be used to change the shortcuts associated with each action.
0175      *
0176      * This slot can be connected directly to the configure shortcuts action,
0177      * for example:
0178      * @code
0179      * KStandardAction::keyBindings(guiFactory(), &KXMLGUIFactory::showConfigureShortcutsDialog, actionCollection());
0180      * @endcode
0181      *
0182      * This method constructs a KShortcutsDialog with the default arguments
0183      * (KShortcutsEditor::AllActions and KShortcutsEditor::LetterShortcutsAllowed).
0184      *
0185      * @see KShortcutsDialog, KShortcutsEditor::ActionTypes, KShortcutsEditor::LetterShortcuts
0186      *
0187      * By default the changes will be saved back to the @c *ui.rc file
0188      * which they were initially read from.
0189      *
0190      * If you need to run some extra code if the dialog is accepted and the settings
0191      * are saved, you can simply connect to the @ref KXMLGUIFactory::shortcutsSaved()
0192      * signal before calling this method, for example:
0193      * @code
0194      * connect(guiFactory(), &KXMLGUIFactory::shortcutsSaved, this, &MyClass::slotShortcutSaved);
0195      * guiFactory()->showConfigureShortcutsDialog();
0196      * @endcode
0197      *
0198      * @since 5.84
0199      */
0200     void showConfigureShortcutsDialog();
0201 
0202     void changeShortcutScheme(const QString &scheme);
0203 
0204 Q_SIGNALS:
0205     void clientAdded(KXMLGUIClient *client);
0206     void clientRemoved(KXMLGUIClient *client);
0207 
0208     /**
0209      * Emitted when the factory is currently making changes to the GUI,
0210      * i.e. adding or removing clients.
0211      * makingChanges(true) is emitted before any change happens, and
0212      * makingChanges(false) is emitted after the change is done.
0213      * This allows e.g. KMainWindow to know that the GUI is
0214      * being changed programmatically and not by the user (so there is no reason to
0215      * save toolbar settings afterwards).
0216      * @since 4.1.3
0217      */
0218     void makingChanges(bool);
0219 
0220     /**
0221      * Emitted when the shortcuts have been saved (i.e. the user accepted the dialog).
0222      *
0223      * If you're using multiple instances of the same KXMLGUIClient, you probably want to
0224      * connect to this signal and call @c KXMLGUIClient::reloadXML() for each of your
0225      * KXMLGUIClients, so that the other instances update their shortcuts settings.
0226      *
0227      * @since 5.79
0228      */
0229     void shortcutsSaved();
0230 
0231 private:
0232     /// Internal, called by KXMLGUIClient destructor
0233     KXMLGUI_NO_EXPORT void forgetClient(KXMLGUIClient *client);
0234 
0235 private:
0236     friend class KXMLGUIClient;
0237     std::unique_ptr<KXMLGUIFactoryPrivate> const d;
0238 };
0239 
0240 #endif