QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsshortcutsmanager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsshortcutsmanager.cpp
3  ---------------------
4  begin : May 2009
5  copyright : (C) 2009 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsshortcutsmanager.h"
17 #include "qgslogger.h"
18 #include <QSettings>
19 #include <QShortcut>
20 
21 QgsShortcutsManager* QgsShortcutsManager::mInstance = nullptr;
22 
23 
25 {
26  if ( !mInstance )
27  mInstance = new QgsShortcutsManager( nullptr );
28  return mInstance;
29 }
30 
32  : QObject( parent )
33  , mSettingsPath( settingsRoot )
34 {
35 }
36 
37 void QgsShortcutsManager::registerAllChildren( QObject* object, bool recursive )
38 {
39  registerAllChildActions( object, recursive );
40  registerAllChildShortcuts( object, recursive );
41 }
42 
44 {
45  if ( recursive )
46  {
47  QList< QAction* > actions = object->findChildren< QAction* >();
48  Q_FOREACH ( QAction* a, actions )
49  {
50  registerAction( a, a->shortcut() );
51  }
52  }
53  else
54  {
55  Q_FOREACH ( QObject* child, object->children() )
56  {
57  if ( QAction* a = qobject_cast<QAction*>( child ) )
58  {
59  registerAction( a, a->shortcut() );
60  }
61  }
62  }
63 }
64 
66 {
67  if ( recursive )
68  {
69  QList< QShortcut* > shortcuts = object->findChildren< QShortcut* >();
70  Q_FOREACH ( QShortcut* s, shortcuts )
71  {
72  registerShortcut( s, s->key() );
73  }
74  }
75  else
76  {
77  Q_FOREACH ( QObject* child, object->children() )
78  {
79  if ( QShortcut* s = qobject_cast<QShortcut*>( child ) )
80  {
81  registerShortcut( s, s->key() );
82  }
83  }
84  }
85 }
86 
87 bool QgsShortcutsManager::registerAction( QAction* action, const QString& defaultSequence )
88 {
89 #ifdef QGISDEBUG
90  // if using a debug build, warn on duplicate actions
91  if ( actionByName( action->text() ) || shortcutByName( action->text() ) )
92  QgsLogger::warning( QString( "Duplicate shortcut registered: %1" ).arg( action->text() ) );
93 #endif
94 
95  mActions.insert( action, defaultSequence );
96  connect( action, SIGNAL( destroyed() ), this, SLOT( actionDestroyed() ) );
97 
98  QString actionText = action->text();
99  actionText.remove( '&' ); // remove the accelerator
100 
101  // load overridden value from settings
102  QSettings settings;
103  QString sequence = settings.value( mSettingsPath + actionText, defaultSequence ).toString();
104 
105  action->setShortcut( sequence );
106 
107  return true;
108 }
109 
110 bool QgsShortcutsManager::registerShortcut( QShortcut* shortcut, const QString& defaultSequence )
111 {
112 #ifdef QGISDEBUG
113  // if using a debug build, warn on duplicate actions
114  if ( actionByName( shortcut->objectName() ) || shortcutByName( shortcut->objectName() ) )
115  QgsLogger::warning( QString( "Duplicate shortcut registered: %1" ).arg( shortcut->objectName() ) );
116 #endif
117 
118  mShortcuts.insert( shortcut, defaultSequence );
119  connect( shortcut, SIGNAL( destroyed() ), this, SLOT( shortcutDestroyed() ) );
120 
121  QString shortcutName = shortcut->objectName();
122 
123  // load overridden value from settings
124  QSettings settings;
125  QString keySequence = settings.value( mSettingsPath + shortcutName, defaultSequence ).toString();
126 
127  shortcut->setKey( keySequence );
128 
129  return true;
130 }
131 
133 {
134  if ( !mActions.contains( action ) )
135  return false;
136 
137  mActions.remove( action );
138  return true;
139 }
140 
142 {
143  if ( !mShortcuts.contains( shortcut ) )
144  return false;
145 
146  mShortcuts.remove( shortcut );
147  return true;
148 }
149 
151 {
152  return mActions.keys();
153 }
154 
156 {
157  return mShortcuts.keys();
158 }
159 
161 {
162  QList< QObject* > list;
163  ActionsHash::const_iterator actionIt = mActions.constBegin();
164  for ( ; actionIt != mActions.constEnd(); ++actionIt )
165  {
166  list << actionIt.key();
167  }
168  ShortcutsHash::const_iterator shortcutIt = mShortcuts.constBegin();
169  for ( ; shortcutIt != mShortcuts.constEnd(); ++shortcutIt )
170  {
171  list << shortcutIt.key();
172  }
173  return list;
174 }
175 
177 {
178  if ( QAction* action = qobject_cast< QAction* >( object ) )
179  return defaultKeySequence( action );
180  else if ( QShortcut* shortcut = qobject_cast< QShortcut* >( object ) )
181  return defaultKeySequence( shortcut );
182  else
183  return QString();
184 }
185 
187 {
188  return mActions.value( action, QString() );
189 }
190 
192 {
193  return mShortcuts.value( shortcut, QString() );
194 }
195 
197 {
198  if ( QAction* action = actionByName( name ) )
199  return setKeySequence( action, sequence );
200  else if ( QShortcut* shortcut = shortcutByName( name ) )
201  return setKeySequence( shortcut, sequence );
202  else
203  return false;
204 }
205 
207 {
208  if ( QAction* action = qobject_cast< QAction* >( object ) )
209  return setKeySequence( action, sequence );
210  else if ( QShortcut* shortcut = qobject_cast< QShortcut* >( object ) )
211  return setKeySequence( shortcut, sequence );
212  else
213  return false;
214 }
215 
216 bool QgsShortcutsManager::setKeySequence( QAction* action, const QString& sequence )
217 {
218  action->setShortcut( sequence );
219 
220  QString actionText = action->text();
221  actionText.remove( '&' ); // remove the accelerator
222 
223  // save to settings
224  QSettings settings;
225  settings.setValue( mSettingsPath + actionText, sequence );
226  return true;
227 }
228 
229 bool QgsShortcutsManager::setKeySequence( QShortcut* shortcut, const QString& sequence )
230 {
231  shortcut->setKey( sequence );
232 
233  QString shortcutText = shortcut->objectName();
234 
235  // save to settings
236  QSettings settings;
237  settings.setValue( mSettingsPath + shortcutText, sequence );
238  return true;
239 }
240 
242 {
243  if ( QAction* action = actionForSequence( sequence ) )
244  return action;
245  else if ( QShortcut* shortcut = shortcutForSequence( sequence ) )
246  return shortcut;
247  else
248  return nullptr;
249 }
250 
252 {
253  if ( sequence.isEmpty() )
254  return nullptr;
255 
256  for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
257  {
258  if ( it.key()->shortcut() == sequence )
259  return it.key();
260  }
261 
262  return nullptr;
263 }
264 
266 {
267  if ( sequence.isEmpty() )
268  return nullptr;
269 
270  for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
271  {
272  if ( it.key()->key() == sequence )
273  return it.key();
274  }
275 
276  return nullptr;
277 }
278 
280 {
281  for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
282  {
283  if ( it.key()->text() == name )
284  return it.key();
285  }
286 
287  return nullptr;
288 }
289 
291 {
292  for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
293  {
294  if ( it.key()->objectName() == name )
295  return it.key();
296  }
297 
298  return nullptr;
299 }
300 
301 void QgsShortcutsManager::actionDestroyed()
302 {
303  mActions.remove( qobject_cast<QAction*>( sender() ) );
304 }
305 
306 void QgsShortcutsManager::shortcutDestroyed()
307 {
308  mShortcuts.remove( qobject_cast<QShortcut*>( sender() ) );
309 }
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
QString objectDefaultKeySequence(QObject *object) const
Returns the default sequence for an object (either a QAction or QShortcut).
QList< QObject * > listAll() const
Returns a list of both actions and shortcuts in the manager.
iterator insert(const Key &key, const T &value)
bool unregisterShortcut(QShortcut *shortcut)
Removes a shortcut from the manager.
QShortcut * shortcutForSequence(const QKeySequence &sequence) const
Returns the shortcut which is associated for a key sequence, or nullptr if no shortcut is associated...
QObject * sender() const
static void warning(const QString &msg)
Goes to qWarning.
Definition: qgslogger.cpp:124
void registerAllChildActions(QObject *object, bool recursive=false)
Automatically registers all QActions which are children of the passed object.
const QObjectList & children() const
QList< QAction * > listActions() const
Returns a list of all actions in the manager.
Shortcuts manager is a class that contains a list of QActions and QShortcuts that have been registere...
bool setObjectKeySequence(QObject *object, const QString &sequence)
Modifies an object&#39;s (either a QAction or a QShortcut) key sequence.
QString & remove(int position, int n)
void registerAllChildShortcuts(QObject *object, bool recursive=false)
Automatically registers all QShortcuts which are children of the passed object.
static QgsShortcutsManager * instance()
Return the singleton instance of the manager.
QList< QShortcut * > listShortcuts() const
Returns a list of shortcuts in the manager.
void setValue(const QString &key, const QVariant &value)
const char * name() const
bool setKeySequence(const QString &name, const QString &sequence)
Modifies an action or shortcut&#39;s key sequence.
const_iterator constEnd() const
bool registerShortcut(QShortcut *shortcut, const QString &defaultSequence=QString())
Registers a QShortcut with the manager so the shortcut can be configured in GUI.
QAction * actionForSequence(const QKeySequence &sequence) const
Returns the action which is associated for a shortcut sequence, or nullptr if no action is associated...
QString defaultKeySequence(QAction *action) const
Returns the default sequence for an action.
int remove(const Key &key)
QList< Key > keys() const
const T value(const Key &key) const
QShortcut * shortcutByName(const QString &name) const
Returns a shortcut by its name, or nullptr if nothing found.
bool registerAction(QAction *action, const QString &defaultShortcut=QString())
Registers an action with the manager so the shortcut can be configured in GUI.
void registerAllChildren(QObject *object, bool recursive=false)
Automatically registers all QActions and QShortcuts which are children of the passed object...
bool isEmpty() const
QVariant value(const QString &key, const QVariant &defaultValue) const
const_iterator constBegin() const
QAction * actionByName(const QString &name) const
Returns an action by its name, or nullptr if nothing found.
bool unregisterAction(QAction *action)
Removes an action from the manager.
bool contains(const Key &key) const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
QString toString() const
QgsShortcutsManager(QObject *parent=nullptr, const QString &settingsRoot="/shortcuts/")
Constructor for QgsShortcutsManager.
void destroyed(QObject *obj)
QObject * objectForSequence(const QKeySequence &sequence) const
Returns the object (QAction or QShortcut) matching the specified key sequence,.