24 , mSettingsPath( settingsRoot )
38 QList< QAction * > actions =
object->findChildren< QAction * >();
39 const auto constActions = actions;
40 for ( QAction *a : constActions )
42 registerAction( a, a->shortcut().toString( QKeySequence::NativeText ) );
47 const auto constChildren =
object->children();
48 for ( QObject *child : constChildren )
50 if ( QAction *a = qobject_cast<QAction *>( child ) )
52 registerAction( a, a->shortcut().toString( QKeySequence::NativeText ) );
62 QList< QShortcut * > shortcuts =
object->findChildren< QShortcut * >();
63 const auto constShortcuts = shortcuts;
64 for ( QShortcut *s : constShortcuts )
71 const auto constChildren =
object->children();
72 for ( QObject *child : constChildren )
74 if ( QShortcut *s = qobject_cast<QShortcut *>( child ) )
84 if ( mActions.contains( action ) )
90 QgsLogger::warning( QStringLiteral(
"Duplicate shortcut registered: %1" ).arg( action->text() ) );
93 mActions.insert( action, defaultSequence );
94 connect( action, &QObject::destroyed,
this, &QgsShortcutsManager::actionDestroyed );
96 QString actionText = action->text();
97 actionText.remove(
'&' );
101 QString sequence = settings.
value( mSettingsPath + actionText, defaultSequence ).toString();
103 action->setShortcut( sequence );
104 action->setToolTip(
"<b>" + action->toolTip() +
"</b>" );
105 updateActionToolTip( action, sequence );
115 QgsLogger::warning( QStringLiteral(
"Duplicate shortcut registered: %1" ).arg( shortcut->objectName() ) );
118 mShortcuts.insert( shortcut, defaultSequence );
119 connect( shortcut, &QObject::destroyed,
this, &QgsShortcutsManager::shortcutDestroyed );
121 QString shortcutName = shortcut->objectName();
125 QString keySequence = settings.
value( mSettingsPath + shortcutName, defaultSequence ).toString();
127 shortcut->setKey( keySequence );
134 if ( !mActions.contains( action ) )
137 mActions.remove( action );
143 if ( !mShortcuts.contains( shortcut ) )
146 mShortcuts.remove( shortcut );
152 return mActions.keys();
157 return mShortcuts.keys();
162 QList< QObject * > list;
163 ActionsHash::const_iterator actionIt = mActions.constBegin();
164 for ( ; actionIt != mActions.constEnd(); ++actionIt )
166 list << actionIt.key();
168 ShortcutsHash::const_iterator shortcutIt = mShortcuts.constBegin();
169 for ( ; shortcutIt != mShortcuts.constEnd(); ++shortcutIt )
171 list << shortcutIt.key();
178 if ( QAction *action = qobject_cast< QAction * >(
object ) )
180 else if ( QShortcut *shortcut = qobject_cast< QShortcut * >(
object ) )
188 return mActions.value( action, QString() );
193 return mShortcuts.value( shortcut, QString() );
208 if ( QAction *action = qobject_cast< QAction * >(
object ) )
210 else if ( QShortcut *shortcut = qobject_cast< QShortcut * >(
object ) )
218 action->setShortcut( sequence );
219 this->updateActionToolTip( action, sequence );
221 QString actionText = action->text();
222 actionText.remove(
'&' );
226 settings.
setValue( mSettingsPath + actionText, sequence );
232 shortcut->setKey( sequence );
234 QString shortcutText = shortcut->objectName();
238 settings.
setValue( mSettingsPath + shortcutText, sequence );
254 if ( sequence.isEmpty() )
257 for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
259 if ( it.key()->shortcut() == sequence )
268 if ( sequence.isEmpty() )
271 for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
273 if ( it.key()->key() == sequence )
282 for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
284 if ( it.key()->text() == name )
293 for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
295 if ( it.key()->objectName() == name )
302 void QgsShortcutsManager::actionDestroyed()
304 mActions.remove( qobject_cast<QAction *>( sender() ) );
307 void QgsShortcutsManager::shortcutDestroyed()
309 mShortcuts.remove( qobject_cast<QShortcut *>( sender() ) );
312 void QgsShortcutsManager::updateActionToolTip( QAction *action,
const QString &sequence )
314 QString current = action->toolTip();
316 QRegExp rx(
"\\(.*\\)" );
317 current.replace( rx, QString() );
319 if ( !sequence.isEmpty() )
321 action->setToolTip( current +
" (" + sequence +
")" );
325 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,.