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

KDE's Doxygen guidelines are available online.