KXmlGui

kactioncollection.h
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <[email protected]>
4  SPDX-FileCopyrightText: 1999 Simon Hausmann <[email protected]>
5  SPDX-FileCopyrightText: 2000 Nicolas Hadacek <[email protected]>
6  SPDX-FileCopyrightText: 2000 Kurt Granroth <[email protected]>
7  SPDX-FileCopyrightText: 2000 Michael Koch <[email protected]>
8  SPDX-FileCopyrightText: 2001 Holger Freyther <[email protected]>
9  SPDX-FileCopyrightText: 2002 Ellis Whitehead <[email protected]>
10  SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <[email protected]>
11 
12  SPDX-License-Identifier: LGPL-2.0-only
13 */
14 
15 #ifndef KACTIONCOLLECTION_H
16 #define KACTIONCOLLECTION_H
17 
18 #include <KStandardAction>
19 #include <kxmlgui_export.h>
20 
21 #include <QAction>
22 #include <QObject>
23 #include <memory>
24 
25 class KXMLGUIClient;
26 class KConfigGroup;
27 class QActionGroup;
28 class QString;
29 
30 /**
31  * @class KActionCollection kactioncollection.h KActionCollection
32  *
33  * \short A container for a set of QAction objects.
34  *
35  * KActionCollection manages a set of QAction objects. It
36  * allows them to be grouped for organized presentation of configuration to the user,
37  * saving + loading of configuration, and optionally for automatic plugging into
38  * specified widget(s).
39  *
40  * Additionally, KActionCollection provides several convenience functions for locating
41  * named actions, and actions grouped by QActionGroup.
42  *
43  * \note If you create your own action collection and need to assign shortcuts
44  * to the actions within, you have to call associateWidget() or
45  * addAssociatedWidget() to have them working.
46  */
47 class KXMLGUI_EXPORT KActionCollection : public QObject
48 {
49  friend class KXMLGUIClient;
50 
51  Q_OBJECT
52 
53  Q_PROPERTY(QString configGroup READ configGroup WRITE setConfigGroup)
54  Q_PROPERTY(bool configIsGlobal READ configIsGlobal WRITE setConfigGlobal)
55 
56 public:
57  /**
58  * Constructor. Allows specification of a component name other than the default
59  * application name, where needed (remember to call setComponentDisplayName() too).
60  */
61  explicit KActionCollection(QObject *parent, const QString &cName = QString());
62 
63  /**
64  * Destructor.
65  */
66  ~KActionCollection() override;
67 
68  /**
69  * Access the list of all action collections in existence for this app
70  */
71  static const QList<KActionCollection *> &allCollections();
72 
73  /**
74  * Clears the entire action collection, deleting all actions.
75  */
76  void clear();
77 
78  /**
79  * Associate all actions in this collection to the given @p widget.
80  * Unlike addAssociatedWidget(), this method only adds all current actions
81  * in the collection to the given widget. Any action added after this call
82  * will not be added to the given widget automatically.
83  * So this is just a shortcut for a foreach loop and a widget->addAction call.
84  */
85  void associateWidget(QWidget *widget) const;
86 
87  /**
88  * Associate all actions in this collection to the given @p widget, including any actions
89  * added after this association is made.
90  *
91  * This does not change the action's shortcut context, so if you need to have the actions only
92  * trigger when the widget has focus, you'll need to set the shortcut context on each action
93  * to Qt::WidgetShortcut (or better still, Qt::WidgetWithChildrenShortcut with Qt 4.4+)
94  */
95  void addAssociatedWidget(QWidget *widget);
96 
97  /**
98  * Remove an association between all actions in this collection and the given @p widget, i.e.
99  * remove those actions from the widget, and stop associating newly added actions as well.
100  */
101  void removeAssociatedWidget(QWidget *widget);
102 
103  /**
104  * Return a list of all associated widgets.
105  */
106  QList<QWidget *> associatedWidgets() const;
107 
108  /**
109  * Clear all associated widgets and remove the actions from those widgets.
110  */
111  void clearAssociatedWidgets();
112 
113  /**
114  * Returns the KConfig group with which settings will be loaded and saved.
115  */
116  QString configGroup() const;
117 
118  /**
119  * Returns whether this action collection's configuration should be global to KDE ( @c true ),
120  * or specific to the application ( @c false ).
121  */
122  bool configIsGlobal() const;
123 
124  /**
125  * Sets @p group as the KConfig group with which settings will be loaded and saved.
126  */
127  void setConfigGroup(const QString &group);
128 
129  /**
130  * Set whether this action collection's configuration should be global to KDE ( @c true ),
131  * or specific to the application ( @c false ).
132  */
133  void setConfigGlobal(bool global);
134 
135  /**
136  * Read all key associations from @p config.
137  *
138  * If @p config is zero, read all key associations from the
139  * application's configuration file KSharedConfig::openConfig(),
140  * in the group set by setConfigGroup().
141  */
142  void readSettings(KConfigGroup *config = nullptr);
143 
144  /**
145  * Import from @p config all configurable global key associations.
146  *
147  * \since 4.1
148  *
149  * \param config Config object to read from
150  */
151  void importGlobalShortcuts(KConfigGroup *config);
152 
153  /**
154  * Export the current configurable global key associations to @p config.
155  *
156  * \since 4.1
157  *
158  * \param config Config object to save to
159  * \param writeDefaults set to true to write settings which are already at defaults.
160  */
161  void exportGlobalShortcuts(KConfigGroup *config, bool writeDefaults = false) const;
162 
163  /**
164  * Write the current configurable key associations to @p config. What the
165  * function does if @p config is zero depends. If this action collection
166  * belongs to a KXMLGUIClient the setting are saved to the kxmlgui
167  * definition file. If not the settings are written to the applications
168  * config file.
169  *
170  * \note @p oneAction and @p writeDefaults have no meaning for the kxmlgui
171  * configuration file.
172  *
173  * \param config Config object to save to, or null (see above)
174  * \param writeDefaults set to true to write settings which are already at defaults.
175  * \param oneAction pass an action here if you just want to save the values for one action, eg.
176  * if you know that action is the only one which has changed.
177  */
178  void writeSettings(KConfigGroup *config = nullptr, bool writeDefaults = false, QAction *oneAction = nullptr) const;
179 
180  /**
181  * Returns the number of actions in the collection.
182  *
183  * This is equivalent to actions().count().
184  */
185  int count() const;
186 
187  /**
188  * Returns whether the action collection is empty or not.
189  */
190  bool isEmpty() const;
191 
192  /**
193  * Return the QAction* at position @p index in the action collection.
194  *
195  * This is equivalent to actions().value(index);
196  */
197  QAction *action(int index) const;
198 
199  /**
200  * Get the action with the given \p name from the action collection.
201  *
202  * @param name Name of the QAction
203  * @return A pointer to the QAction in the collection which matches the parameters or
204  * null if nothing matches.
205  */
206  QAction *action(const QString &name) const;
207 
208  /**
209  * Returns the list of QActions which belong to this action collection.
210  *
211  * The list is guaranteed to be in the same order the action were put into
212  * the collection.
213  */
214  QList<QAction *> actions() const;
215 
216  /**
217  * Returns the list of QActions without an QAction::actionGroup() which belong to this action collection.
218  */
219  const QList<QAction *> actionsWithoutGroup() const;
220 
221  /**
222  * Returns the list of all QActionGroups associated with actions in this action collection.
223  */
224  const QList<QActionGroup *> actionGroups() const;
225 
226  /**
227  * Set the @p componentName associated with this action collection.
228  *
229  * \warning Don't call this method on a KActionCollection that contains
230  * actions. This is not supported.
231  *
232  * \param componentData the name which is to be associated with this action collection,
233  * or QString() to indicate the app name. This is used to load/save settings into XML files.
234  * KXMLGUIClient::setComponentName takes care of calling this.
235  */
237 
238  /** The component name with which this class is associated. */
239  QString componentName() const;
240 
241  /**
242  * Set the component display name associated with this action collection.
243  * (e.g. for the toolbar editor)
244  * KXMLGUIClient::setComponentName takes care of calling this.
245  */
246  void setComponentDisplayName(const QString &displayName);
247 
248  /** The display name for the associated component. */
249  QString componentDisplayName() const;
250 
251  /**
252  * The parent KXMLGUIClient, or null if not available.
253  */
254  const KXMLGUIClient *parentGUIClient() const;
255 
256 Q_SIGNALS:
257  /**
258  * Indicates that @p action was inserted into this action collection.
259  */
260  void inserted(QAction *action);
261 
262 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
263  /**
264  * Indicates that @p action was removed from this action collection.
265  * @deprecated Since 5.0, use changed() (added in 5.66) instead.
266  */
267  KXMLGUI_DEPRECATED_VERSION(5, 0, "removed() is sometimes emitted with partially destroyed objects; use changed() instead (added in 5.66)")
268  QT_MOC_COMPAT void removed(QAction *action);
269 #endif
270 
271  /**
272  * Emitted when an action has been inserted into, or removed from, this action collection.
273  * @since 5.66
274  */
275  void changed();
276 
277 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
278  /**
279  * Indicates that @p action was highlighted (hovered over).
280  * @deprecated Since 5.0. Replaced by actionHovered(QAction* action);
281  */
282  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KActionCollection::actionHovered(QAction* action)")
283  QT_MOC_COMPAT void actionHighlighted(QAction *action);
284 #endif
285 
286  /**
287  * Indicates that @p action was hovered.
288  */
289  void actionHovered(QAction *action);
290 
291  /**
292  * Indicates that @p action was triggered
293  */
294  void actionTriggered(QAction *action);
295 
296 protected:
297  /// Overridden to perform connections when someone wants to know whether an action was highlighted or triggered
298  void connectNotify(const QMetaMethod &signal) override;
299 
300 protected Q_SLOTS:
301  virtual void slotActionTriggered();
302 
303 #if KXMLGUI_ENABLE_DEPRECATED_SINCE(5, 0)
304  /**
305  * @internal
306  * @deprecated Since 5.0. Replaced by slotActionHovered();
307  */
308  KXMLGUI_DEPRECATED_VERSION(5, 0, "Use KActionCollection::slotActionHovered()")
309  QT_MOC_COMPAT virtual void slotActionHighlighted();
310 #endif
311 
312 private Q_SLOTS:
313  void slotActionHovered();
314 
315 public:
316  /**
317  * Add an action under the given name to the collection.
318  *
319  * Inserting an action that was previously inserted under a different name will replace the
320  * old entry, i.e. the action will not be available under the old name anymore but only under
321  * the new one.
322  *
323  * Inserting an action under a name that is already used for another action will replace
324  * the other action in the collection (but will not delete it).
325  *
326  * If KAuthorized::authorizeAction() reports that the action is not
327  * authorized, it will be disabled and hidden.
328  *
329  * The ownership of the action object is not transferred.
330  * If the action is destroyed it will be removed automatically from the KActionCollection.
331  *
332  * @param name The name by which the action be retrieved again from the collection.
333  * @param action The action to add.
334  * @return the same as the action given as parameter. This is just for convenience
335  * (chaining calls) and consistency with the other addAction methods, you can also
336  * simply ignore the return value.
337  */
338  Q_INVOKABLE QAction *addAction(const QString &name, QAction *action);
339 
340  /**
341  * Adds a list of actions to the collection.
342  *
343  * The objectName of the actions is used as their internal name in the collection.
344  *
345  * The ownership of the action objects is not transferred.
346  * If the action is destroyed it will be removed automatically from the KActionCollection.
347  *
348  * Uses addAction(const QString&, QAction*).
349  *
350  * @param actions the list of the actions to add.
351  *
352  * @see addAction()
353  * @since 5.0
354  */
355  void addActions(const QList<QAction *> &actions);
356 
357  /**
358  * Removes an action from the collection and deletes it.
359  * @param action The action to remove.
360  */
361  void removeAction(QAction *action);
362 
363  /**
364  * Removes an action from the collection.
365  *
366  * The ownership of the action object is not changed.
367  *
368  * @param action the action to remove.
369  */
370  QAction *takeAction(QAction *action);
371 
372  /**
373  * Creates a new standard action, adds it to the collection and connects the
374  * action's triggered(bool) signal to the specified receiver/member. The
375  * newly created action is also returned.
376  *
377  * @note Using KStandardAction::OpenRecent will cause a different signal than
378  * triggered(bool) to be used, see KStandardAction for more information.
379  *
380  * The action can be retrieved later from the collection by its standard name as per
381  * KStandardAction::stdName.
382  *
383  * The KActionCollection takes ownership of the action object.
384  *
385  * @param actionType The standard action type of the action to create.
386  * @param receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no
387  * connection is desired.
388  * @param member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no
389  * connection is desired.
390  * @return new action of the given type ActionType.
391  */
392  QAction *addAction(KStandardAction::StandardAction actionType, const QObject *receiver = nullptr, const char *member = nullptr);
393 
394  /**
395  * Creates a new standard action, adds to the collection under the given name
396  * and connects the action's triggered(bool) signal to the specified
397  * receiver/member. The newly created action is also returned.
398  *
399  * @note Using KStandardAction::OpenRecent will cause a different signal than
400  * triggered(bool) to be used, see KStandardAction for more information.
401  *
402  * The action can be retrieved later from the collection by the specified name.
403  *
404  * The KActionCollection takes ownership of the action object.
405  *
406  * @param actionType The standard action type of the action to create.
407  * @param name The name by which the action be retrieved again from the collection.
408  * @param receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no
409  * connection is desired.
410  * @param member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no
411  * connection is desired.
412  * @return new action of the given type ActionType.
413  */
414  QAction *addAction(KStandardAction::StandardAction actionType, const QString &name, const QObject *receiver = nullptr, const char *member = nullptr);
415 
416 /**
417  * This is the same as addAction(KStandardAction::StandardAction actionType, const QString &name, const QObject *receiver, const char *member) using
418  * new style connect syntax.
419  *
420  * @param actionType The standard action type of the action to create.
421  * @param name The name by which the action be retrieved again from the collection.
422  * @param receiver The QObject to connect the triggered(bool) signal to.
423  * @param slot The slot or lambda to connect the triggered(bool) signal to.
424  * @return new action of the given type ActionType.
425  *
426  * @see addAction(KStandardAction::StandardAction, const QString &, const QObject *, const char *)
427  * @since 5.80
428  */
429 #ifdef K_DOXYGEN
430  inline QAction *addAction(KStandardAction::StandardAction actionType, const QString &name, const Receiver *receiver, Func slot)
431 #else
432  template<class Receiver, class Func>
433  inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, QAction>::type *
434  addAction(KStandardAction::StandardAction actionType, const QString &name, const Receiver *receiver, Func slot)
435 #endif
436  {
437  QAction *action = KStandardAction::create(actionType, receiver, slot, nullptr);
438  action->setParent(this);
439  action->setObjectName(name);
440  return addAction(name, action);
441  }
442 
443  /**
444  * Creates a new action under the given name to the collection and connects
445  * the action's triggered(bool) signal to the specified receiver/member. The
446  * newly created action is returned.
447  *
448  * NOTE: KDE prior to 4.2 used the triggered() signal instead of the triggered(bool)
449  * signal.
450  *
451  * Inserting an action that was previously inserted under a different name will replace the
452  * old entry, i.e. the action will not be available under the old name anymore but only under
453  * the new one.
454  *
455  * Inserting an action under a name that is already used for another action will replace
456  * the other action in the collection.
457  *
458  * The KActionCollection takes ownership of the action object.
459  *
460  * @param name The name by which the action be retrieved again from the collection.
461  * @param receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no
462  * connection is desired.
463  * @param member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no
464  * connection is desired.
465  * @return new action of the given type ActionType.
466  */
467  QAction *addAction(const QString &name, const QObject *receiver = nullptr, const char *member = nullptr);
468 
469  /**
470  * Creates a new action under the given name, adds it to the collection and connects the action's triggered(bool)
471  * signal to the specified receiver/member. The receiver slot may accept either a bool or no
472  * parameters at all (i.e. slotTriggered(bool) or slotTriggered() ).
473  * The type of the action is specified by the template parameter ActionType.
474  *
475  * NOTE: KDE prior to 4.2 connected the triggered() signal instead of the triggered(bool)
476  * signal.
477  *
478  * The KActionCollection takes ownership of the action object.
479  *
480  * @param name The internal name of the action (e.g. "file-open").
481  * @param receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no
482  * connection is desired.
483  * @param member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no
484  * connection is desired.
485  * @return new action of the given type ActionType.
486  *
487  * @see addAction()
488  */
489  template<class ActionType>
490  ActionType *add(const QString &name, const QObject *receiver = nullptr, const char *member = nullptr)
491  {
492  ActionType *a = new ActionType(this);
493  if (receiver && member) {
494  connect(a, SIGNAL(triggered(bool)), receiver, member);
495  }
496  addAction(name, a);
497  return a;
498  }
499 
500 /**
501  * This is the same as add(const QString &name, const QObject *receiver, const char *member) using
502  * new style connect syntax.
503  *
504  * @param name The internal name of the action (e.g. "file-open").
505  * @param receiver The QObject to connect the triggered(bool) signal to.
506  * @param slot The slot or lambda to connect the triggered(bool) signal to.
507  * @return new action of the given type ActionType.
508  *
509  * @see add(const QString &, const QObject *, const char *)
510  * @since 5.28
511  */
512 #ifdef K_DOXYGEN
513  template<class ActionType>
514  inline ActionType *add(const QString &name, const Receiver *receiver, Func slot)
515 #else
516  template<class ActionType, class Receiver, class Func>
517  inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, ActionType>::type *
518  add(const QString &name, const Receiver *receiver, Func slot)
519 #endif
520  {
521  ActionType *a = new ActionType(this);
522  connect(a, &QAction::triggered, receiver, slot);
523  addAction(name, a);
524  return a;
525  }
526 
527 /**
528  * This is the same as addAction(const QString &name, const QObject *receiver, const char *member) using
529  * new style connect syntax.
530  *
531  * @param name The internal name of the action (e.g. "file-open").
532  * @param receiver The QObject to connect the triggered(bool) signal to.
533  * @param slot The slot or lambda to connect the triggered(bool) signal to.
534  * @return new action of the given type ActionType.
535  *
536  * @see addAction(const QString &, const QObject *, const char *)
537  * @since 5.28
538  */
539 #ifdef K_DOXYGEN
540  inline QAction *addAction(const QString &name, const Receiver *receiver, Func slot)
541 #else
542  template<class Receiver, class Func>
543  inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, QAction>::type *
544  addAction(const QString &name, const Receiver *receiver, Func slot)
545 #endif
546  {
547  return add<QAction>(name, receiver, slot);
548  }
549 
550  /**
551  * Get the default primary shortcut for the given action.
552  *
553  * @param action the action for which the default primary shortcut should be returned.
554  * @return the default primary shortcut of the given action
555  * @since 5.0
556  */
557  QKeySequence defaultShortcut(QAction *action) const;
558 
559  /**
560  * Get the default shortcuts for the given action.
561  *
562  * @param action the action for which the default shortcuts should be returned.
563  * @return the default shortcuts of the given action
564  * @since 5.0
565  */
566  QList<QKeySequence> defaultShortcuts(QAction *action) const;
567 
568  // TODO KF6: Make setDefaultShortcut static
569  /**
570  * Set the default shortcut for the given action.
571  * Since 5.2, this also calls action->setShortcut(shortcut), i.e. the default shortcut is
572  * made active initially.
573  *
574  * @param action the action for which the default shortcut should be set.
575  * @param shortcut the shortcut to use for the given action in its specified shortcutContext()
576  * @since 5.0
577  */
578  void setDefaultShortcut(QAction *action, const QKeySequence &shortcut);
579 
580  /**
581  * Set the default shortcuts for the given action.
582  * Since 5.2, this also calls action->setShortcuts(shortcuts), i.e. the default shortcut is
583  * made active initially.
584  *
585  * @param action the action for which the default shortcut should be set.
586  * @param shortcuts the shortcuts to use for the given action in its specified shortcutContext()
587  * @since 5.0
588  */
589  Q_INVOKABLE void setDefaultShortcuts(QAction *action, const QList<QKeySequence> &shortcuts);
590 
591  /**
592  * Returns true if the given action's shortcuts may be configured by the user.
593  *
594  * @param action the action for the hint should be verified.
595  * @since 5.0
596  */
597  bool isShortcutsConfigurable(QAction *action) const;
598 
599  /**
600  * Indicate whether the user may configure the action's shortcuts.
601  *
602  * @param action the action for the hint should be verified.
603  * @param configurable set to true if the shortcuts of the given action may be configured by the user, otherwise false.
604  * @since 5.0
605  */
606  void setShortcutsConfigurable(QAction *action, bool configurable);
607 
608 private:
609  KActionCollection(const KXMLGUIClient *parent); // used by KXMLGUIClient
610 
611  friend class KActionCollectionPrivate;
612  std::unique_ptr<class KActionCollectionPrivate> const d;
613 };
614 
615 #endif
QAction * addAction(const QString &name, const Receiver *receiver, Func slot)
This is the same as addAction(const QString &name, const QObject *receiver, const char *member) using...
A container for a set of QAction objects.
void triggered(bool checked)
A KXMLGUIClient can be used with KXMLGUIFactory to create a GUI from actions and an XML document...
Definition: kxmlguiclient.h:39
QAction * create(StandardAction id, const QObject *recvr, Func slot, QObject *parent)
virtual QString componentName() const
const QLatin1String name
void setObjectName(const QString &name)
ActionType * add(const QString &name, const QObject *receiver=nullptr, const char *member=nullptr)
Creates a new action under the given name, adds it to the collection and connects the action&#39;s trigge...
virtual void connectNotify(const QMetaMethod &signal)
Q_PROPERTY(...)
void setParent(QObject *parent)
virtual void setComponentName(const QString &componentName, const QString &componentDisplayName)
Sets the component name for this part.
QAction * action(const char *name) const
Retrieves an action of the client by name.
ActionType * add(const QString &name, const Receiver *receiver, Func slot)
This is the same as add(const QString &name, const QObject *receiver, const char *member) using new s...
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QAction * addAction(KStandardAction::StandardAction actionType, const QString &name, const Receiver *receiver, Func slot)
This is the same as addAction(KStandardAction::StandardAction actionType, const QString &name...
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Dec 6 2021 22:45:45 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.