Skip to content

Commit

Permalink
Add keyboard shortcut to actions in the UI
Browse files Browse the repository at this point in the history
Also bolds action tooltip text to make it clearer.
Shortcuts live inside ({shortcut-here}) in the tooltip
  • Loading branch information
NathanW2 committed Jan 21, 2017
1 parent ad963e6 commit 6d53f83
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/gui/qgsshortcutsmanager.cpp
Expand Up @@ -103,6 +103,8 @@ bool QgsShortcutsManager::registerAction( QAction* action, const QString& defaul
QString sequence = settings.value( mSettingsPath + actionText, defaultSequence ).toString();

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

return true;
}
Expand Down Expand Up @@ -216,6 +218,7 @@ bool QgsShortcutsManager::setObjectKeySequence( QObject* object, const QString&
bool QgsShortcutsManager::setKeySequence( QAction* action, const QString& sequence )
{
action->setShortcut( sequence );
this->updateActionToolTip( action, sequence );

QString actionText = action->text();
actionText.remove( '&' ); // remove the accelerator
Expand Down Expand Up @@ -307,3 +310,20 @@ void QgsShortcutsManager::shortcutDestroyed()
{
mShortcuts.remove( qobject_cast<QShortcut*>( sender() ) );
}

void QgsShortcutsManager::updateActionToolTip( QAction *action, QString sequence )
{
QString current = action->toolTip();
// Remove the old shortcut.
QRegExp rx( "\\(.*\\)" );
current.replace( rx, "" );

if ( !sequence.isEmpty() )
{
action->setToolTip( current + " (" + sequence + ")" );
}
else
{
action->setToolTip( current );
}
}
8 changes: 8 additions & 0 deletions src/gui/qgsshortcutsmanager.h
Expand Up @@ -231,6 +231,14 @@ class GUI_EXPORT QgsShortcutsManager : public QObject
ShortcutsHash mShortcuts;
QString mSettingsPath;
static QgsShortcutsManager* sInstance;

/**
* Updates the action to include the shortcut keys. Shortcut keys are
* included between () at the end of the action tooltop.

This comment has been minimized.

Copy link
@DelazJ

DelazJ Jan 21, 2017

Contributor

tooltip

This comment has been minimized.

Copy link
@NathanW2

NathanW2 Jan 21, 2017

Author Member

Opps thanks.

* @param action The action to append the shortcut.
* @param sequence The shortcut sequence.
*/
void updateActionToolTip( QAction* action, QString sequence );
};

#endif // QGSSHORTCUTSMANAGER_H

0 comments on commit 6d53f83

Please sign in to comment.