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 if ( !action->toolTip().isEmpty() )
106 const QStringList parts = action->toolTip().split(
'\n' );
107 QString formatted = QStringLiteral(
"<b>%1</b>" ).arg( parts.at( 0 ) );
108 if ( parts.count() > 1 )
110 for (
int i = 1; i < parts.count(); ++i )
111 formatted += QStringLiteral(
"<p>%1</p>" ).arg( parts.at( i ) );
114 action->setToolTip( formatted );
115 updateActionToolTip( action, sequence );
126 QgsLogger::warning( QStringLiteral(
"Duplicate shortcut registered: %1" ).arg( shortcut->objectName() ) );
129 mShortcuts.insert( shortcut, defaultSequence );
130 connect( shortcut, &QObject::destroyed,
this, &QgsShortcutsManager::shortcutDestroyed );
132 QString shortcutName = shortcut->objectName();
136 QString keySequence = settings.
value( mSettingsPath + shortcutName, defaultSequence ).toString();
138 shortcut->setKey( keySequence );
145 if ( !mActions.contains( action ) )
148 mActions.remove( action );
154 if ( !mShortcuts.contains( shortcut ) )
157 mShortcuts.remove( shortcut );
163 return mActions.keys();
168 return mShortcuts.keys();
173 QList< QObject * > list;
174 ActionsHash::const_iterator actionIt = mActions.constBegin();
175 for ( ; actionIt != mActions.constEnd(); ++actionIt )
177 list << actionIt.key();
179 ShortcutsHash::const_iterator shortcutIt = mShortcuts.constBegin();
180 for ( ; shortcutIt != mShortcuts.constEnd(); ++shortcutIt )
182 list << shortcutIt.key();
189 if ( QAction *action = qobject_cast< QAction * >(
object ) )
191 else if ( QShortcut *shortcut = qobject_cast< QShortcut * >(
object ) )
199 return mActions.value( action, QString() );
204 return mShortcuts.value( shortcut, QString() );
219 if ( QAction *action = qobject_cast< QAction * >(
object ) )
221 else if ( QShortcut *shortcut = qobject_cast< QShortcut * >(
object ) )
229 action->setShortcut( sequence );
230 this->updateActionToolTip( action, sequence );
232 QString actionText = action->text();
233 actionText.remove(
'&' );
237 settings.
setValue( mSettingsPath + actionText, sequence );
243 shortcut->setKey( sequence );
245 QString shortcutText = shortcut->objectName();
249 settings.
setValue( mSettingsPath + shortcutText, sequence );
265 if ( sequence.isEmpty() )
268 for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
270 if ( it.key()->shortcut() == sequence )
279 if ( sequence.isEmpty() )
282 for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
284 if ( it.key()->key() == sequence )
293 for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
295 if ( it.key()->text() == name )
304 for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
306 if ( it.key()->objectName() == name )
313 void QgsShortcutsManager::actionDestroyed()
315 mActions.remove( qobject_cast<QAction *>( sender() ) );
318 void QgsShortcutsManager::shortcutDestroyed()
320 mShortcuts.remove( qobject_cast<QShortcut *>( sender() ) );
323 void QgsShortcutsManager::updateActionToolTip( QAction *action,
const QString &sequence )
325 QString current = action->toolTip();
327 QRegExp rx(
"\\(.*\\)" );
328 current.replace( rx, QString() );
330 if ( !sequence.isEmpty() )
332 action->setToolTip( current +
" (" + sequence +
")" );
336 action->setToolTip( current );