Skip to content

Commit

Permalink
[FEATURE] Move QgsShortcutsManager, QgsConfigureShortcutsDialog to gui
Browse files Browse the repository at this point in the history
This allows plugins to reuse the shortcuts manager if they
want to add the ability for users to customise their shortcut
key sequences.

The shortcut manager has been extended to also handle customisation
of QShortcut objects as well as QActions.
  • Loading branch information
nyalldawson committed Jun 2, 2016
1 parent 3a005cd commit e0c87ff
Show file tree
Hide file tree
Showing 14 changed files with 1,365 additions and 272 deletions.
2 changes: 2 additions & 0 deletions python/gui/gui.sip
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
%Include qgscomposerruler.sip %Include qgscomposerruler.sip
%Include qgscomposerview.sip %Include qgscomposerview.sip
%Include qgscompoundcolorwidget.sip %Include qgscompoundcolorwidget.sip
%Include qgsconfigureshortcutsdialog.sip
%Include qgscredentialdialog.sip %Include qgscredentialdialog.sip
%Include qgsdatadefinedbutton.sip %Include qgsdatadefinedbutton.sip
%Include qgsdetaileditemdata.sip %Include qgsdetaileditemdata.sip
Expand Down Expand Up @@ -147,6 +148,7 @@
%Include qgsscalevisibilitydialog.sip %Include qgsscalevisibilitydialog.sip
%Include qgsscalewidget.sip %Include qgsscalewidget.sip
%Include qgssearchquerybuilder.sip %Include qgssearchquerybuilder.sip
%Include qgsshortcutsmanager.sip
%Include qgsslider.sip %Include qgsslider.sip
%Include qgssublayersdialog.sip %Include qgssublayersdialog.sip
%Include qgssvgannotationitem.sip %Include qgssvgannotationitem.sip
Expand Down
27 changes: 27 additions & 0 deletions python/gui/qgsconfigureshortcutsdialog.sip
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,27 @@
/** \ingroup gui
* \class QgsConfigureShortcutsDialog
* Reusable dialog for allowing users to configure shortcuts contained in a QgsShortcutsManager.
* \note added in QGIS 2.16
*/

class QgsConfigureShortcutsDialog: QDialog
{
%TypeHeaderCode
#include <qgsconfigureshortcutsdialog.h>
%End
public:

/** Constructor for QgsConfigureShortcutsDialog.
* @param parent parent widget
* @param manager associated QgsShortcutsManager, or leave as null to use the default
* singleton QgsShortcutsManager instance.
*/
QgsConfigureShortcutsDialog( QWidget* parent /TransferThis/ = nullptr, QgsShortcutsManager* manager = nullptr );

~QgsConfigureShortcutsDialog();

protected:
void keyPressEvent( QKeyEvent * event );
void keyReleaseEvent( QKeyEvent * event );

};
198 changes: 198 additions & 0 deletions python/gui/qgsshortcutsmanager.sip
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,198 @@
/** \ingroup gui
* \class QgsShortcutsManager
* Shortcuts manager is a class that contains a list of QActions and QShortcuts
that have been registered and their shortcuts can be changed.
* \note added in QGIS 2.16
*/

class QgsShortcutsManager: QObject
{
%TypeHeaderCode
#include <qgsshortcutsmanager.h>
%End

public:

//! Return the singleton instance of the manager.
static QgsShortcutsManager* instance();

/** Constructor for QgsShortcutsManager.
* @param parent parent object
* @param settingsRoot root QSettings path for storing settings, eg "/myplugin/shortcuts". Leave
* as the default value to store settings alongside built in QGIS shortcuts, but care must be
* taken to not register actions which conflict with the built in QGIS actions.
*/
QgsShortcutsManager( QObject *parent /TransferThis/ = nullptr, const QString& settingsRoot = "/shortcuts/" );

/** Automatically registers all QActions and QShortcuts which are children of the
* passed object.
* @param object parent object containing actions and shortcuts to register
* @param recursive set to true to recursively add child actions and shortcuts
* @see registerAllChildActions()
* @see registerAllChildShortcuts()
*/
void registerAllChildren( QObject* object, bool recursive = false );

/** Automatically registers all QActions which are children of the passed object.
* @param object parent object containing actions to register
* @param recursive set to true to recursively add child actions
* @see registerAction()
* @see registerAllChildren()
* @see registerAllChildShortcuts()
*/
void registerAllChildActions( QObject* object, bool recursive = false );

/** Automatically registers all QShortcuts which are children of the passed object.
* @param object parent object containing shortcuts to register
* @param recursive set to true to recursively add child shortcuts
* @see registerShortcut()
* @see registerAllChildren()
* @see registerAllChildActions()
*/
void registerAllChildShortcuts( QObject* object, bool recursive = false );

/** Registers an action with the manager so the shortcut can be configured in GUI.
* @param action action to register. The action must have a unique text string for
* identification.
* @param defaultSequence default key sequence for action
* @returns true if action was successfully registered
* @see registerShortcut()
* @see unregisterAction()
* @see registerAllChildActions()
*/
bool registerAction( QAction* action, const QString& defaultShortcut = QString() );

/** Registers a QShortcut with the manager so the shortcut can be configured in GUI.
* @param shortcut QShortcut to register. The shortcut must have a unique QObject::objectName() for
* identification.
* @param defaultSequence default key sequence for shortcut
* @returns true if shortcut was successfully registered
* @see registerAction()
* @see registerAllChildShortcuts()
*/
bool registerShortcut( QShortcut* shortcut, const QString& defaultSequence = QString() );

/** Removes an action from the manager.
* @param action action to remove
* @returns true if action was previously registered in manager and has been removed, or
* false if action was not previously registered in manager
* @see registerAction()
* @see unregisterShortcut()
*/
bool unregisterAction( QAction* action );

/** Removes a shortcut from the manager.
* @param shortcut shortcut to remove
* @returns true if shortcut was previously registered in manager and has been removed, or
* false if shortcut was not previously registered in manager
* @see registerShortcut()
* @see unregisterAction()
*/
bool unregisterShortcut( QShortcut* shortcut );

/** Returns a list of all actions in the manager.
* @see listShortcuts()
* @see listAll()
*/
QList<QAction*> listActions() const;

/** Returns a list of shortcuts in the manager.
* @see listActions()
* @see listAll()
*/
QList<QShortcut*> listShortcuts() const;

/** Returns a list of both actions and shortcuts in the manager.
* @see listAction()
* @see listShortcuts()
*/
QList<QObject*> listAll() const;

/** Returns the default sequence for an object (either a QAction or QShortcut).
* An empty return string indicates no shortcut.
* @param object QAction or QShortcut to return default key sequence for
* @see defaultKeySequence()
*/
QString objectDefaultKeySequence( QObject* object ) const;

/** Returns the default sequence for an action. An empty return string indicates
* no default sequence.
* @param action action to return default key sequence for
* @see objectDefaultKeySequence()
*/
QString defaultKeySequence( QAction* action ) const;

/** Returns the default sequence for a shortcut. An empty return string indicates
* no default sequence.
* @param shortcut shortcut to return default key sequence for
* @see objectDefaultKeySequence()
*/
QString defaultKeySequence( QShortcut* shortcut ) const;

/** Modifies an action or shortcut's key sequence.
* @param name name of action or shortcut to modify. Must match the action's QAction::text() or the
* shortcut's QObject::objectName()
* @param sequence new shortcut key sequence
* @see setObjectKeySequence()
*/
bool setKeySequence( const QString& name, const QString& sequence );

/** Modifies an object's (either a QAction or a QShortcut) key sequence.
* @param object QAction or QShortcut to modify
* @param sequence new shortcut key sequence
* @see setKeySequence()
*/
bool setObjectKeySequence( QObject* object, const QString& sequence );

/** Modifies an action's key sequence.
* @param action action to modify
* @param sequence new shortcut key sequence
* @see setObjectKeySequence()
*/
bool setKeySequence( QAction* action, const QString& sequence );

/** Modifies a shortcuts's key sequence.
* @param shortcut QShortcut to modify
* @param sequence new shortcut key sequence
* @see setObjectKeySequence()
*/
bool setKeySequence( QShortcut* shortcut, const QString& sequence );

/** Returns the object (QAction or QShortcut) matching the specified key sequence,
* @param sequence key sequence to find
* @returns object with matching sequence, or nullptr if not found
* @see actionForSequence()
* @see shortcutForSequence()
*/
QObject* objectForSequence( const QKeySequence& sequence ) const;

/** Returns the action which is associated for a shortcut sequence, or nullptr if no action is associated.
* @param sequence shortcut key sequence
* @see objectForSequence()
* @see shortcutForSequence()
*/
QAction* actionForSequence( const QKeySequence& sequence ) const;

/** Returns the shortcut which is associated for a key sequence, or nullptr if no shortcut is associated.
* @param sequence shortcut key sequence
* @see objectForSequence()
* @see actionForSequence()
*/
QShortcut* shortcutForSequence( const QKeySequence& sequence ) const;

/** Returns an action by its name, or nullptr if nothing found.
* @param name action name. Must match QAction's text.
* @see shortcutByName()
*/
QAction* actionByName( const QString& name ) const;

/** Returns a shortcut by its name, or nullptr if nothing found
* @param name shortcut name. Must match QShortcut's QObject::objectName() property.
* @see actionByName()
*/
QShortcut* shortcutByName( const QString& name ) const;

//! Returns the root settings path used to store shortcut customisation.
QString settingsPath() const;

};
4 changes: 0 additions & 4 deletions src/app/CMakeLists.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ SET(QGIS_APP_SRCS
qgsbookmarks.cpp qgsbookmarks.cpp
qgsbrowserdockwidget.cpp qgsbrowserdockwidget.cpp
qgsclipboard.cpp qgsclipboard.cpp
qgsconfigureshortcutsdialog.cpp
qgscustomization.cpp qgscustomization.cpp
qgscustomprojectiondialog.cpp qgscustomprojectiondialog.cpp
qgsdecorationitem.cpp qgsdecorationitem.cpp
Expand Down Expand Up @@ -116,7 +115,6 @@ SET(QGIS_APP_SRCS
qgsrelationadddlg.cpp qgsrelationadddlg.cpp
qgsstatisticalsummarydockwidget.cpp qgsstatisticalsummarydockwidget.cpp
qgstextannotationdialog.cpp qgstextannotationdialog.cpp
qgsshortcutsmanager.cpp
qgssnappingdialog.cpp qgssnappingdialog.cpp
qgssvgannotationdialog.cpp qgssvgannotationdialog.cpp
qgsundowidget.cpp qgsundowidget.cpp
Expand Down Expand Up @@ -196,7 +194,6 @@ SET (QGIS_APP_MOC_HDRS
qgsbookmarks.h qgsbookmarks.h
qgsbrowserdockwidget.h qgsbrowserdockwidget.h
qgsclipboard.h qgsclipboard.h
qgsconfigureshortcutsdialog.h
qgscustomization.h qgscustomization.h
qgscustomprojectiondialog.h qgscustomprojectiondialog.h
qgsdecorationitem.h qgsdecorationitem.h
Expand Down Expand Up @@ -229,7 +226,6 @@ SET (QGIS_APP_MOC_HDRS
qgsmaplayerstyleguiutils.h qgsmaplayerstyleguiutils.h
qgsrulebasedlabelingwidget.h qgsrulebasedlabelingwidget.h
qgssavestyletodbdialog.h qgssavestyletodbdialog.h
qgsshortcutsmanager.h
qgsstatusbarcoordinateswidget.h qgsstatusbarcoordinateswidget.h
qgsstatusbarmagnifierwidget.h qgsstatusbarmagnifierwidget.h
qgsstatusbarscalewidget.h qgsstatusbarscalewidget.h
Expand Down
13 changes: 10 additions & 3 deletions src/app/qgisapp.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -938,15 +938,22 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh


QShortcut* zoomInShortCut = new QShortcut( QKeySequence( tr( "Ctrl++" ) ), this ); QShortcut* zoomInShortCut = new QShortcut( QKeySequence( tr( "Ctrl++" ) ), this );
connect( zoomInShortCut, SIGNAL( activated() ), mMapCanvas, SLOT( zoomIn() ) ); connect( zoomInShortCut, SIGNAL( activated() ), mMapCanvas, SLOT( zoomIn() ) );
zoomInShortCut->setObjectName( "ZoomInToCanvas" );
zoomInShortCut->setWhatsThis( "Zoom in to canvas" );
QShortcut* zoomShortCut2 = new QShortcut( QKeySequence( tr( "Ctrl+=" ) ), this ); QShortcut* zoomShortCut2 = new QShortcut( QKeySequence( tr( "Ctrl+=" ) ), this );
connect( zoomShortCut2, SIGNAL( activated() ), mMapCanvas, SLOT( zoomIn() ) ); connect( zoomShortCut2, SIGNAL( activated() ), mMapCanvas, SLOT( zoomIn() ) );
zoomShortCut2->setObjectName( "ZoomInToCanvas2" );
zoomShortCut2->setWhatsThis( "Zoom in to canvas (secondary)" );
QShortcut* zoomOutShortCut = new QShortcut( QKeySequence( tr( "Ctrl+-" ) ), this ); QShortcut* zoomOutShortCut = new QShortcut( QKeySequence( tr( "Ctrl+-" ) ), this );
connect( zoomOutShortCut, SIGNAL( activated() ), mMapCanvas, SLOT( zoomOut() ) ); connect( zoomOutShortCut, SIGNAL( activated() ), mMapCanvas, SLOT( zoomOut() ) );
zoomOutShortCut->setObjectName( "ZoomOutOfCanvas" );
zoomOutShortCut->setWhatsThis( "Zoom out of canvas" );


//also make ctrl+alt+= a shortcut to switch to zoom in map tool //also make ctrl+alt+= a shortcut to switch to zoom in map tool
QShortcut* zoomInToolShortCut = new QShortcut( QKeySequence( tr( "Ctrl+Alt+=" ) ), this ); QShortcut* zoomInToolShortCut = new QShortcut( QKeySequence( tr( "Ctrl+Alt+=" ) ), this );
connect( zoomInToolShortCut, SIGNAL( activated() ), this, SLOT( zoomIn() ) ); connect( zoomInToolShortCut, SIGNAL( activated() ), this, SLOT( zoomIn() ) );

zoomInToolShortCut->setObjectName( "Zoom in" );
zoomInToolShortCut->setWhatsThis( "Zoom in (secondary)" );


// Show a nice tip of the day // Show a nice tip of the day
if ( settings.value( QString( "/qgis/showTips%1" ).arg( QGis::QGIS_VERSION_INT / 100 ), true ).toBool() ) if ( settings.value( QString( "/qgis/showTips%1" ).arg( QGis::QGIS_VERSION_INT / 100 ), true ).toBool() )
Expand All @@ -970,7 +977,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
#endif #endif


// supposedly all actions have been added, now register them to the shortcut manager // supposedly all actions have been added, now register them to the shortcut manager
QgsShortcutsManager::instance()->registerAllChildrenActions( this ); QgsShortcutsManager::instance()->registerAllChildren( this );


QgsProviderRegistry::instance()->registerGuis( this ); QgsProviderRegistry::instance()->registerGuis( this );


Expand Down Expand Up @@ -8526,7 +8533,7 @@ void QgisApp::versionReplyFinished()


void QgisApp::configureShortcuts() void QgisApp::configureShortcuts()
{ {
QgsConfigureShortcutsDialog dlg; QgsConfigureShortcutsDialog dlg( this );
dlg.exec(); dlg.exec();
} }


Expand Down
Loading

0 comments on commit e0c87ff

Please sign in to comment.