24 , mSettingsPath( settingsRoot )
38 QList< QAction * > actions =
object->findChildren< QAction * >();
39 Q_FOREACH ( QAction *a, actions )
41 registerAction( a, a->shortcut().toString( QKeySequence::NativeText ) );
46 Q_FOREACH ( QObject *child, object->children() )
48 if ( QAction *a = qobject_cast<QAction *>( child ) )
50 registerAction( a, a->shortcut().toString( QKeySequence::NativeText ) );
60 QList< QShortcut * > shortcuts =
object->findChildren< QShortcut * >();
61 Q_FOREACH ( QShortcut *s, shortcuts )
68 Q_FOREACH ( QObject *child, object->children() )
70 if ( QShortcut *s = qobject_cast<QShortcut *>( child ) )
80 if ( mActions.contains( action ) )
86 QgsLogger::warning( QStringLiteral(
"Duplicate shortcut registered: %1" ).arg( action->text() ) );
89 mActions.insert( action, defaultSequence );
90 connect( action, &QObject::destroyed,
this, &QgsShortcutsManager::actionDestroyed );
92 QString actionText = action->text();
93 actionText.remove(
'&' );
97 QString sequence = settings.
value( mSettingsPath + actionText, defaultSequence ).toString();
99 action->setShortcut( sequence );
100 action->setToolTip(
"<b>" + action->toolTip() +
"</b>" );
101 this->updateActionToolTip( action, sequence );
111 QgsLogger::warning( QStringLiteral(
"Duplicate shortcut registered: %1" ).arg( shortcut->objectName() ) );
114 mShortcuts.insert( shortcut, defaultSequence );
115 connect( shortcut, &QObject::destroyed,
this, &QgsShortcutsManager::shortcutDestroyed );
117 QString shortcutName = shortcut->objectName();
121 QString keySequence = settings.
value( mSettingsPath + shortcutName, defaultSequence ).toString();
123 shortcut->setKey( keySequence );
130 if ( !mActions.contains( action ) )
133 mActions.remove( action );
139 if ( !mShortcuts.contains( shortcut ) )
142 mShortcuts.remove( shortcut );
148 return mActions.keys();
153 return mShortcuts.keys();
158 QList< QObject * > list;
159 ActionsHash::const_iterator actionIt = mActions.constBegin();
160 for ( ; actionIt != mActions.constEnd(); ++actionIt )
162 list << actionIt.key();
164 ShortcutsHash::const_iterator shortcutIt = mShortcuts.constBegin();
165 for ( ; shortcutIt != mShortcuts.constEnd(); ++shortcutIt )
167 list << shortcutIt.key();
174 if ( QAction *action = qobject_cast< QAction * >(
object ) )
176 else if ( QShortcut *shortcut = qobject_cast< QShortcut * >(
object ) )
184 return mActions.value( action, QString() );
189 return mShortcuts.value( shortcut, QString() );
204 if ( QAction *action = qobject_cast< QAction * >(
object ) )
206 else if ( QShortcut *shortcut = qobject_cast< QShortcut * >(
object ) )
214 action->setShortcut( sequence );
215 this->updateActionToolTip( action, sequence );
217 QString actionText = action->text();
218 actionText.remove(
'&' );
222 settings.
setValue( mSettingsPath + actionText, sequence );
228 shortcut->setKey( sequence );
230 QString shortcutText = shortcut->objectName();
234 settings.
setValue( mSettingsPath + shortcutText, sequence );
250 if ( sequence.isEmpty() )
253 for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
255 if ( it.key()->shortcut() == sequence )
264 if ( sequence.isEmpty() )
267 for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
269 if ( it.key()->key() == sequence )
278 for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
280 if ( it.key()->text() == name )
289 for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
291 if ( it.key()->objectName() == name )
298 void QgsShortcutsManager::actionDestroyed()
300 mActions.remove( qobject_cast<QAction *>( sender() ) );
303 void QgsShortcutsManager::shortcutDestroyed()
305 mShortcuts.remove( qobject_cast<QShortcut *>( sender() ) );
308 void QgsShortcutsManager::updateActionToolTip( QAction *action,
const QString &sequence )
310 QString current = action->toolTip();
312 QRegExp rx(
"\\(.*\\)" );
313 current.replace( rx, QString() );
315 if ( !sequence.isEmpty() )
317 action->setToolTip( current +
" (" + sequence +
")" );
321 action->setToolTip( current );
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.
This class is a composition of two QSettings instances:
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...
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static void warning(const QString &msg)
Goes to qWarning.
void registerAllChildActions(QObject *object, bool recursive=false)
Automatically registers all QActions which are children of the passed object.
QList< QAction * > listActions() const
Returns a list of all actions in the manager.
bool setObjectKeySequence(QObject *object, const QString &sequence)
Modifies an object's (either a QAction or a QShortcut) key sequence.
void registerAllChildShortcuts(QObject *object, bool recursive=false)
Automatically registers all QShortcuts which are children of the passed object.
QList< QShortcut * > listShortcuts() const
Returns a list of shortcuts in the manager.
bool setKeySequence(const QString &name, const QString &sequence)
Modifies an action or shortcut's key sequence.
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.
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...
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
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.
QgsShortcutsManager(QObject *parent=nullptr, const QString &settingsRoot="/shortcuts/")
Constructor for QgsShortcutsManager.
QObject * objectForSequence(const QKeySequence &sequence) const
Returns the object (QAction or QShortcut) matching the specified key sequence,.