Skip to content

Commit 6d53f83

Browse files
committed
Add keyboard shortcut to actions in the UI
Also bolds action tooltip text to make it clearer. Shortcuts live inside ({shortcut-here}) in the tooltip
1 parent ad963e6 commit 6d53f83

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎src/gui/qgsshortcutsmanager.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ bool QgsShortcutsManager::registerAction( QAction* action, const QString& defaul
103103
QString sequence = settings.value( mSettingsPath + actionText, defaultSequence ).toString();
104104

105105
action->setShortcut( sequence );
106+
action->setToolTip( "<b>" + action->toolTip() + "</b>" );
107+
this->updateActionToolTip( action, sequence );
106108

107109
return true;
108110
}
@@ -216,6 +218,7 @@ bool QgsShortcutsManager::setObjectKeySequence( QObject* object, const QString&
216218
bool QgsShortcutsManager::setKeySequence( QAction* action, const QString& sequence )
217219
{
218220
action->setShortcut( sequence );
221+
this->updateActionToolTip( action, sequence );
219222

220223
QString actionText = action->text();
221224
actionText.remove( '&' ); // remove the accelerator
@@ -307,3 +310,20 @@ void QgsShortcutsManager::shortcutDestroyed()
307310
{
308311
mShortcuts.remove( qobject_cast<QShortcut*>( sender() ) );
309312
}
313+
314+
void QgsShortcutsManager::updateActionToolTip( QAction *action, QString sequence )
315+
{
316+
QString current = action->toolTip();
317+
// Remove the old shortcut.
318+
QRegExp rx( "\\(.*\\)" );
319+
current.replace( rx, "" );
320+
321+
if ( !sequence.isEmpty() )
322+
{
323+
action->setToolTip( current + " (" + sequence + ")" );
324+
}
325+
else
326+
{
327+
action->setToolTip( current );
328+
}
329+
}

‎src/gui/qgsshortcutsmanager.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ class GUI_EXPORT QgsShortcutsManager : public QObject
231231
ShortcutsHash mShortcuts;
232232
QString mSettingsPath;
233233
static QgsShortcutsManager* sInstance;
234+
235+
/**
236+
* Updates the action to include the shortcut keys. Shortcut keys are
237+
* included between () at the end of the action tooltop.
238+
* @param action The action to append the shortcut.
239+
* @param sequence The shortcut sequence.
240+
*/
241+
void updateActionToolTip( QAction* action, QString sequence );
234242
};
235243

236244
#endif // QGSSHORTCUTSMANAGER_H

0 commit comments

Comments
 (0)