KGlobalAccel

component.h
1 /*
2  SPDX-FileCopyrightText: 2008 Michael Jansen <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #ifndef COMPONENT_H
8 #define COMPONENT_H
9 
10 #include "globalshortcut.h"
11 #include "kglobalshortcutinfo.h"
12 
13 #include "kconfiggroup.h"
14 
15 #include <KGlobalAccel>
16 #include <QHash>
17 #include <QObject>
18 
19 class GlobalShortcut;
22 
23 /**
24  * @author Michael Jansen <[email protected]>
25  */
26 class Component : public QObject
27 {
28  Q_OBJECT
29 
30  Q_CLASSINFO("D-Bus Interface", "org.kde.kglobalaccel.Component")
31 
32  /* clang-format off */
33  Q_SCRIPTABLE Q_PROPERTY(QString friendlyName READ friendlyName)
34  Q_SCRIPTABLE Q_PROPERTY(QString uniqueName READ uniqueName)
35 
36 
37 public:
38 
39 
40  ~Component() override;
41  /* clang-format on */
42 
43  bool activateGlobalShortcutContext(const QString &uniqueName);
44 
45  void activateShortcuts();
46 
47  //! Returns all shortcuts in context @context
48  QList<GlobalShortcut *> allShortcuts(const QString &context = QStringLiteral("default")) const;
49 
50  //! Creates the new global shortcut context @p context
51  bool createGlobalShortcutContext(const QString &context, const QString &friendlyName = QString());
52 
53  //! Return the current context
55 
56  //! Return uniqueName converted to a valid dbus path
57  QDBusObjectPath dbusPath() const;
58 
59  //! Deactivate all currently active shortcuts
60  void deactivateShortcuts(bool temporarily = false);
61 
62  //! Returns the friendly name
63  QString friendlyName() const;
64 
65  //! Returns the currently active shortcut for key
67 
68  //! Returns the shortcut context @p name or nullptr
70  GlobalShortcutContext const *shortcutContext(const QString &name) const;
71 
72  /**
73  * Returns the list of shortcuts (different context) registered with @p
74  * key.
75  */
77 
78  //! Returns the shortcut by unique name. Only the active context is
79  //! searched.
80  GlobalShortcut *getShortcutByName(const QString &uniqueName, const QString &context = QStringLiteral("default")) const;
81 
82  /**
83  * Check if @a key is available for component @p component
84  */
85  bool isShortcutAvailable(const QKeySequence &key, const QString &component, const QString &context) const;
86 
87  //! Load the settings from config group @p config
88  void loadSettings(KConfigGroup &config);
89 
90  //! Sets the human readable name for this component.
91  void setFriendlyName(const QString &);
92 
93  QString uniqueName() const;
94 
95  //! Unregister @a shortcut. This will remove its siblings from all contexts
96  void unregisterShortcut(const QString &uniqueName);
97 
98  void writeSettings(KConfigGroup &config) const;
99 
100 protected:
101  friend class ::GlobalShortcutsRegistry;
102 
103  //! Constructs a component. This is a private constructor, to create a component
104  //! use GlobalShortcutsRegistry::self()->createComponent().
105  Component(const QString &uniqueName, const QString &friendlyName);
106 
107  /**
108  * Create a new globalShortcut by its name
109  * @param uniqueName internal unique name to identify the shortcut
110  * @param friendlyName name for the shortcut to be presented to the user
111  * @param shortcutString string representation of the shortcut, such as "CTRL+S"
112  * @param defaultShortcutString string representation of the default shortcut,
113  * such as "CTRL+S", when the user choses to reset to default
114  * the keyboard shortcut will return to this one.
115  */
117  registerShortcut(const QString &uniqueName, const QString &friendlyName, const QString &shortcutString, const QString &defaultShortcutString);
118 
119 public Q_SLOTS:
120 
121  // For dbus Q_SCRIPTABLE has to be on slots. Scriptable methods are not
122  // exported.
123 
124  /**
125  * Remove all currently not used global shortcuts registrations for this
126  * component and if nothing is left the component too.
127  *
128  * If the method returns true consider all information previously acquired
129  * from this component as void.
130  *
131  * The method will cleanup in all contexts.
132  *
133  * @return @c true if a change was made, @c false if not.
134  */
135  Q_SCRIPTABLE virtual bool cleanUp();
136 
137  /**
138  * Check if the component is currently active.
139  *
140  * A component is active if at least one of it's global shortcuts is
141  * currently present.
142  */
143  Q_SCRIPTABLE bool isActive() const;
144 
145  //! Get all shortcutnames living in @a context
146  Q_SCRIPTABLE QStringList shortcutNames(const QString &context = QStringLiteral("default")) const;
147 
148  //! Returns all shortcut in @a context
149  Q_SCRIPTABLE QList<KGlobalShortcutInfo> allShortcutInfos(const QString &context = QStringLiteral("default")) const;
150 
151  //! Returns the shortcut contexts available for the component.
152  Q_SCRIPTABLE QStringList getShortcutContexts() const;
153 
154  virtual void emitGlobalShortcutPressed(const GlobalShortcut &shortcut);
155  virtual void emitGlobalShortcutReleased(const GlobalShortcut &shortcut);
156 
157  Q_SCRIPTABLE void invokeShortcut(const QString &shortcutName, const QString &context = QStringLiteral("default"));
158 
159 Q_SIGNALS:
160 
161  //! Signals that a action for this component was triggered
162  Q_SCRIPTABLE void globalShortcutPressed(const QString &componentUnique, const QString &shortcutUnique, qlonglong timestamp);
163 
164  //! Signals that a action for this component is not triggered anymore
165  Q_SCRIPTABLE void globalShortcutReleased(const QString &componentUnique, const QString &shortcutUnique, qlonglong timestamp);
166 
167 private:
168  QString _uniqueName;
169  // the name as it would be found in a magazine article about the application,
170  // possibly localized if a localized name exists.
171  QString _friendlyName;
172 
173  GlobalShortcutsRegistry *_registry;
174 
175  GlobalShortcutContext *_current;
177 };
178 
179 #endif /* #ifndef COMPONENT_H */
Q_OBJECTQ_OBJECT
bool isShortcutAvailable(const QKeySequence &key, const QString &component, const QString &context) const
Check if key is available for component component.
Definition: component.cpp:277
virtual Q_SCRIPTABLE bool cleanUp()
Remove all currently not used global shortcuts registrations for this component and if nothing is lef...
Definition: component.cpp:110
Q_PROPERTY(...)
Q_SCRIPTABLE QStringList shortcutNames(const QString &context=QStringLiteral("default")) const
Get all shortcutnames living in context.
Definition: component.cpp:352
Q_SCRIPTABLE bool isActive() const
Check if the component is currently active.
Definition: component.cpp:265
Q_SLOTSQ_SLOTS
GlobalShortcutContext * shortcutContext(const QString &name)
Returns the shortcut context name or nullptr.
Definition: component.cpp:342
Q_CLASSINFO(Name, Value)
Q_SCRIPTABLE void globalShortcutPressed(const QString &componentUnique, const QString &shortcutUnique, qlonglong timestamp)
Signals that a action for this component was triggered.
QList< GlobalShortcut * > getShortcutsByKey(const QKeySequence &key, KGlobalAccel::MatchType type) const
Returns the list of shortcuts (different context) registered with key.
Definition: component.cpp:242
Q_SCRIPTABLE QStringList getShortcutContexts() const
Returns the shortcut contexts available for the component.
Definition: component.cpp:260
QDBusObjectPath dbusPath() const
Return uniqueName converted to a valid dbus path.
Definition: component.cpp:146
QList< GlobalShortcut * > allShortcuts(const QString &context=QStringLiteral("default")) const
Returns all shortcuts in context @context.
Definition: component.cpp:98
Represents a global shortcut.
GlobalShortcutContext * currentContext()
Return the current context.
Definition: component.cpp:141
void deactivateShortcuts(bool temporarily=false)
Deactivate all currently active shortcuts.
Definition: component.cpp:166
void unregisterShortcut(const QString &uniqueName)
Unregister shortcut. This will remove its siblings from all contexts.
Definition: component.cpp:363
bool createGlobalShortcutContext(const QString &context, const QString &friendlyName=QString())
Creates the new global shortcut context context.
Definition: component.cpp:131
Global Shortcut Registry.
Q_SIGNALSQ_SIGNALS
void loadSettings(KConfigGroup &config)
Load the settings from config group config.
Definition: component.cpp:320
GlobalShortcut * getShortcutByKey(const QKeySequence &key, KGlobalAccel::MatchType type) const
Returns the currently active shortcut for key.
Definition: component.cpp:237
Q_SCRIPTABLE void globalShortcutReleased(const QString &componentUnique, const QString &shortcutUnique, qlonglong timestamp)
Signals that a action for this component is not triggered anymore.
Component(const QString &uniqueName, const QString &friendlyName)
Constructs a component.
Definition: component.cpp:56
void setFriendlyName(const QString &)
Sets the human readable name for this component.
Definition: component.cpp:337
GlobalShortcut * getShortcutByName(const QString &uniqueName, const QString &context=QStringLiteral("default")) const
Returns the shortcut by unique name.
Definition: component.cpp:254
GlobalShortcut * registerShortcut(const QString &uniqueName, const QString &friendlyName, const QString &shortcutString, const QString &defaultShortcutString)
Create a new globalShortcut by its name.
Definition: component.cpp:297
Q_SCRIPTABLE QList< KGlobalShortcutInfo > allShortcutInfos(const QString &context=QStringLiteral("default")) const
Returns all shortcut in context.
Definition: component.cpp:104
MatchType
Keysequence match semantics.
Definition: kglobalaccel.h:71
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Mar 23 2023 04:11:22 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.