mirror of
https://github.com/qgis/QGIS.git
synced 2025-06-20 00:03:07 -04:00
[FEATURE] Move QgsShortcutsManager, QgsConfigureShortcutsDialog to gui
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.
This commit is contained in:
parent
3a005cda36
commit
e0c87ffe6b
@ -54,6 +54,7 @@
|
||||
%Include qgscomposerruler.sip
|
||||
%Include qgscomposerview.sip
|
||||
%Include qgscompoundcolorwidget.sip
|
||||
%Include qgsconfigureshortcutsdialog.sip
|
||||
%Include qgscredentialdialog.sip
|
||||
%Include qgsdatadefinedbutton.sip
|
||||
%Include qgsdetaileditemdata.sip
|
||||
@ -147,6 +148,7 @@
|
||||
%Include qgsscalevisibilitydialog.sip
|
||||
%Include qgsscalewidget.sip
|
||||
%Include qgssearchquerybuilder.sip
|
||||
%Include qgsshortcutsmanager.sip
|
||||
%Include qgsslider.sip
|
||||
%Include qgssublayersdialog.sip
|
||||
%Include qgssvgannotationitem.sip
|
||||
|
27
python/gui/qgsconfigureshortcutsdialog.sip
Normal file
27
python/gui/qgsconfigureshortcutsdialog.sip
Normal file
@ -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
python/gui/qgsshortcutsmanager.sip
Normal file
198
python/gui/qgsshortcutsmanager.sip
Normal file
@ -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;
|
||||
|
||||
};
|
@ -17,7 +17,6 @@ SET(QGIS_APP_SRCS
|
||||
qgsbookmarks.cpp
|
||||
qgsbrowserdockwidget.cpp
|
||||
qgsclipboard.cpp
|
||||
qgsconfigureshortcutsdialog.cpp
|
||||
qgscustomization.cpp
|
||||
qgscustomprojectiondialog.cpp
|
||||
qgsdecorationitem.cpp
|
||||
@ -116,7 +115,6 @@ SET(QGIS_APP_SRCS
|
||||
qgsrelationadddlg.cpp
|
||||
qgsstatisticalsummarydockwidget.cpp
|
||||
qgstextannotationdialog.cpp
|
||||
qgsshortcutsmanager.cpp
|
||||
qgssnappingdialog.cpp
|
||||
qgssvgannotationdialog.cpp
|
||||
qgsundowidget.cpp
|
||||
@ -196,7 +194,6 @@ SET (QGIS_APP_MOC_HDRS
|
||||
qgsbookmarks.h
|
||||
qgsbrowserdockwidget.h
|
||||
qgsclipboard.h
|
||||
qgsconfigureshortcutsdialog.h
|
||||
qgscustomization.h
|
||||
qgscustomprojectiondialog.h
|
||||
qgsdecorationitem.h
|
||||
@ -229,7 +226,6 @@ SET (QGIS_APP_MOC_HDRS
|
||||
qgsmaplayerstyleguiutils.h
|
||||
qgsrulebasedlabelingwidget.h
|
||||
qgssavestyletodbdialog.h
|
||||
qgsshortcutsmanager.h
|
||||
qgsstatusbarcoordinateswidget.h
|
||||
qgsstatusbarmagnifierwidget.h
|
||||
qgsstatusbarscalewidget.h
|
||||
|
@ -938,15 +938,22 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
|
||||
|
||||
QShortcut* zoomInShortCut = new QShortcut( QKeySequence( tr( "Ctrl++" ) ), this );
|
||||
connect( zoomInShortCut, SIGNAL( activated() ), mMapCanvas, SLOT( zoomIn() ) );
|
||||
zoomInShortCut->setObjectName( "ZoomInToCanvas" );
|
||||
zoomInShortCut->setWhatsThis( "Zoom in to canvas" );
|
||||
QShortcut* zoomShortCut2 = new QShortcut( QKeySequence( tr( "Ctrl+=" ) ), this );
|
||||
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 );
|
||||
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
|
||||
QShortcut* zoomInToolShortCut = new QShortcut( QKeySequence( tr( "Ctrl+Alt+=" ) ), this );
|
||||
connect( zoomInToolShortCut, SIGNAL( activated() ), this, SLOT( zoomIn() ) );
|
||||
|
||||
zoomInToolShortCut->setObjectName( "Zoom in" );
|
||||
zoomInToolShortCut->setWhatsThis( "Zoom in (secondary)" );
|
||||
|
||||
// Show a nice tip of the day
|
||||
if ( settings.value( QString( "/qgis/showTips%1" ).arg( QGis::QGIS_VERSION_INT / 100 ), true ).toBool() )
|
||||
@ -970,7 +977,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
|
||||
#endif
|
||||
|
||||
// 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 );
|
||||
|
||||
@ -8526,7 +8533,7 @@ void QgisApp::versionReplyFinished()
|
||||
|
||||
void QgisApp::configureShortcuts()
|
||||
{
|
||||
QgsConfigureShortcutsDialog dlg;
|
||||
QgsConfigureShortcutsDialog dlg( this );
|
||||
dlg.exec();
|
||||
}
|
||||
|
||||
|
@ -1,127 +0,0 @@
|
||||
/***************************************************************************
|
||||
qgsshortcutsmanager.cpp
|
||||
---------------------
|
||||
begin : May 2009
|
||||
copyright : (C) 2009 by Martin Dobias
|
||||
email : wonder dot sk at gmail dot com
|
||||
***************************************************************************
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
#include "qgsshortcutsmanager.h"
|
||||
|
||||
#include <QSettings>
|
||||
|
||||
QgsShortcutsManager* QgsShortcutsManager::mInstance = nullptr;
|
||||
|
||||
QgsShortcutsManager::QgsShortcutsManager( QObject *parent ) : QObject( parent )
|
||||
{
|
||||
}
|
||||
|
||||
QgsShortcutsManager::~QgsShortcutsManager()
|
||||
{
|
||||
mInstance = nullptr;
|
||||
}
|
||||
|
||||
QgsShortcutsManager* QgsShortcutsManager::instance( QObject *parent )
|
||||
{
|
||||
if ( !mInstance )
|
||||
mInstance = new QgsShortcutsManager( parent );
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
bool QgsShortcutsManager::registerAction( QAction* action, const QString& defaultShortcut )
|
||||
{
|
||||
mActions.insert( action, defaultShortcut );
|
||||
connect( action, SIGNAL( destroyed() ), this, SLOT( actionDestroyed() ) );
|
||||
|
||||
QString actionText = action->text();
|
||||
actionText.remove( '&' ); // remove the accelerator
|
||||
|
||||
// load overridden value from settings
|
||||
QSettings settings;
|
||||
QString shortcut = settings.value( "/shortcuts/" + actionText, defaultShortcut ).toString();
|
||||
|
||||
action->setShortcut( shortcut );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QgsShortcutsManager::unregisterAction( QAction* action )
|
||||
{
|
||||
mActions.remove( action );
|
||||
return true;
|
||||
}
|
||||
|
||||
QList<QAction*> QgsShortcutsManager::listActions()
|
||||
{
|
||||
return mActions.keys();
|
||||
}
|
||||
|
||||
QString QgsShortcutsManager::actionDefaultShortcut( QAction* action )
|
||||
{
|
||||
if ( !mActions.contains( action ) )
|
||||
return QString();
|
||||
|
||||
return mActions.value( action );
|
||||
}
|
||||
|
||||
bool QgsShortcutsManager::setActionShortcut( QAction* action, const QString& shortcut )
|
||||
{
|
||||
action->setShortcut( shortcut );
|
||||
|
||||
QString actionText = action->text();
|
||||
actionText.remove( '&' ); // remove the accelerator
|
||||
|
||||
// save to settings
|
||||
QSettings settings;
|
||||
settings.setValue( "/shortcuts/" + actionText, shortcut );
|
||||
return true;
|
||||
}
|
||||
|
||||
QAction* QgsShortcutsManager::actionForShortcut( const QKeySequence& s )
|
||||
{
|
||||
if ( s.isEmpty() )
|
||||
return nullptr;
|
||||
|
||||
for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
|
||||
{
|
||||
if ( it.key()->shortcut() == s )
|
||||
return it.key();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QAction* QgsShortcutsManager::actionByName( const QString& name )
|
||||
{
|
||||
for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
|
||||
{
|
||||
if ( it.key()->text() == name )
|
||||
return it.key();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void QgsShortcutsManager::registerAllChildrenActions( QObject* object )
|
||||
{
|
||||
Q_FOREACH ( QObject* child, object->children() )
|
||||
{
|
||||
if ( child->inherits( "QAction" ) )
|
||||
{
|
||||
QAction* a = qobject_cast<QAction*>( child );
|
||||
registerAction( a, a->shortcut() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void QgsShortcutsManager::actionDestroyed()
|
||||
{
|
||||
mActions.remove( static_cast<QAction*>( sender() ) );
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
/***************************************************************************
|
||||
qgsshortcutsmanager.h
|
||||
---------------------
|
||||
begin : May 2009
|
||||
copyright : (C) 2009 by Martin Dobias
|
||||
email : wonder dot sk at gmail dot com
|
||||
***************************************************************************
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef QGSSHORTCUTSMANAGER_H
|
||||
#define QGSSHORTCUTSMANAGER_H
|
||||
|
||||
#include <QHash>
|
||||
#include <QList>
|
||||
#include <QAction>
|
||||
|
||||
/**
|
||||
Shortcuts manager is a singleton class that contains a list of actions from main window
|
||||
that have been registered and their shortcut can be changed.
|
||||
*/
|
||||
class APP_EXPORT QgsShortcutsManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
//! return instance of the manager
|
||||
static QgsShortcutsManager* instance( QObject *parent = nullptr );
|
||||
|
||||
//! register all actions which are children of the passed object
|
||||
void registerAllChildrenActions( QObject* object );
|
||||
|
||||
//! add action to the manager so the shortcut can be changed in GUI
|
||||
bool registerAction( QAction* action, const QString& defaultShortcut = QString() );
|
||||
|
||||
//! remove action from the manager
|
||||
bool unregisterAction( QAction* action );
|
||||
|
||||
//! get list of actions in the manager
|
||||
QList<QAction*> listActions();
|
||||
|
||||
//! return default shortcut for action. Empty string means no shortcut
|
||||
QString actionDefaultShortcut( QAction* action );
|
||||
|
||||
//! modify action's shortcut
|
||||
bool setActionShortcut( QAction* action, const QString& shortcut );
|
||||
|
||||
//! return action which is associated for the shortcut, NULL if no action is associated
|
||||
QAction* actionForShortcut( const QKeySequence& s );
|
||||
|
||||
// return action by it's name. NULL if nothing found
|
||||
QAction* actionByName( const QString& name );
|
||||
|
||||
~QgsShortcutsManager();
|
||||
|
||||
public slots:
|
||||
void actionDestroyed();
|
||||
|
||||
protected:
|
||||
QgsShortcutsManager( QObject *parent );
|
||||
|
||||
typedef QHash<QAction*, QString> ActionsHash;
|
||||
|
||||
ActionsHash mActions;
|
||||
static QgsShortcutsManager* mInstance;
|
||||
};
|
||||
|
||||
#endif // QGSSHORTCUTSMANAGER_H
|
@ -190,6 +190,7 @@ SET(QGIS_GUI_SRCS
|
||||
qgscomposerruler.cpp
|
||||
qgscomposerview.cpp
|
||||
qgscompoundcolorwidget.cpp
|
||||
qgsconfigureshortcutsdialog.cpp
|
||||
qgscredentialdialog.cpp
|
||||
qgscursors.cpp
|
||||
qgsdatadefinedbutton.cpp
|
||||
@ -283,6 +284,7 @@ SET(QGIS_GUI_SRCS
|
||||
qgsscalevisibilitydialog.cpp
|
||||
qgsscalewidget.cpp
|
||||
qgssearchquerybuilder.cpp
|
||||
qgsshortcutsmanager.cpp
|
||||
qgsslider.cpp
|
||||
qgssublayersdialog.cpp
|
||||
qgssqlcomposerdialog.cpp
|
||||
@ -343,6 +345,7 @@ SET(QGIS_GUI_MOC_HDRS
|
||||
qgscomposerruler.h
|
||||
qgscomposerview.h
|
||||
qgscompoundcolorwidget.h
|
||||
qgsconfigureshortcutsdialog.h
|
||||
qgscredentialdialog.h
|
||||
qgsdatadefinedbutton.h
|
||||
qgsdatumtransformdialog.h
|
||||
@ -427,6 +430,7 @@ SET(QGIS_GUI_MOC_HDRS
|
||||
qgsscalevisibilitydialog.h
|
||||
qgsscalewidget.h
|
||||
qgssearchquerybuilder.h
|
||||
qgsshortcutsmanager.h
|
||||
qgsslider.h
|
||||
qgssqlcomposerdialog.h
|
||||
qgssublayersdialog.h
|
||||
|
@ -1,6 +1,6 @@
|
||||
/***************************************************************************
|
||||
qgsconfigureshortcutsdialog.cpp
|
||||
---------------------
|
||||
-------------------------------
|
||||
begin : May 2009
|
||||
copyright : (C) 2009 by Martin Dobias
|
||||
email : wonder dot sk at gmail dot com
|
||||
@ -23,17 +23,24 @@
|
||||
#include <QKeySequence>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include <QShortcut>
|
||||
#include <QDomDocument>
|
||||
#include <QFileDialog>
|
||||
#include <QTextStream>
|
||||
#include <QSettings>
|
||||
|
||||
QgsConfigureShortcutsDialog::QgsConfigureShortcutsDialog( QWidget* parent )
|
||||
QgsConfigureShortcutsDialog::QgsConfigureShortcutsDialog( QWidget* parent, QgsShortcutsManager* manager )
|
||||
: QDialog( parent )
|
||||
, mManager( manager )
|
||||
, mGettingShortcut( false )
|
||||
, mModifiers( 0 )
|
||||
, mKey( 0 )
|
||||
{
|
||||
setupUi( this );
|
||||
|
||||
if ( !mManager )
|
||||
mManager = QgsShortcutsManager::instance();
|
||||
|
||||
connect( btnChangeShortcut, SIGNAL( clicked() ), this, SLOT( changeShortcut() ) );
|
||||
connect( btnResetShortcut, SIGNAL( clicked() ), this, SLOT( resetShortcut() ) );
|
||||
connect( btnSetNoShortcut, SIGNAL( clicked() ), this, SLOT( setNoShortcut() ) );
|
||||
@ -73,20 +80,38 @@ void QgsConfigureShortcutsDialog::restoreState()
|
||||
|
||||
void QgsConfigureShortcutsDialog::populateActions()
|
||||
{
|
||||
QList<QAction*> actions = QgsShortcutsManager::instance()->listActions();
|
||||
QList<QObject*> objects = mManager->listAll();
|
||||
|
||||
QList<QTreeWidgetItem *> items;
|
||||
items.reserve( actions.count() );
|
||||
for ( int i = 0; i < actions.count(); ++i )
|
||||
items.reserve( objects.count() );
|
||||
Q_FOREACH ( QObject* obj, objects )
|
||||
{
|
||||
QString actionText = actions[i]->text();
|
||||
actionText.remove( '&' ); // remove the accelerator
|
||||
QString actionText;
|
||||
QString sequence;
|
||||
QIcon icon;
|
||||
|
||||
if ( QAction* action = qobject_cast< QAction* >( obj ) )
|
||||
{
|
||||
actionText = action->text();
|
||||
actionText.remove( '&' ); // remove the accelerator
|
||||
sequence = action->shortcut().toString();
|
||||
icon = action->icon();
|
||||
}
|
||||
else if ( QShortcut* shortcut = qobject_cast< QShortcut* >( obj ) )
|
||||
{
|
||||
actionText = shortcut->whatsThis();
|
||||
sequence = shortcut->key().toString();
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
QStringList lst;
|
||||
lst << actionText << actions[i]->shortcut().toString();
|
||||
lst << actionText << sequence;
|
||||
QTreeWidgetItem* item = new QTreeWidgetItem( lst );
|
||||
item->setIcon( 0, actions[i]->icon() );
|
||||
item->setData( 0, Qt::UserRole, qVariantFromValue(( QObject* )actions[i] ) );
|
||||
item->setIcon( 0, icon );
|
||||
item->setData( 0, Qt::UserRole, qVariantFromValue( obj ) );
|
||||
items.append( item );
|
||||
}
|
||||
|
||||
@ -131,7 +156,7 @@ void QgsConfigureShortcutsDialog::saveShortcuts()
|
||||
root.setAttribute( "locale", settings.value( "locale/userLocale", "en_US" ).toString() );
|
||||
doc.appendChild( root );
|
||||
|
||||
settings.beginGroup( "/shortcuts/" );
|
||||
settings.beginGroup( mManager->settingsPath() );
|
||||
QStringList keys = settings.childKeys();
|
||||
|
||||
QString actionText;
|
||||
@ -215,7 +240,6 @@ void QgsConfigureShortcutsDialog::loadShortcuts()
|
||||
return;
|
||||
}
|
||||
|
||||
QAction* action;
|
||||
QString actionName;
|
||||
QString actionShortcut;
|
||||
|
||||
@ -224,9 +248,8 @@ void QgsConfigureShortcutsDialog::loadShortcuts()
|
||||
{
|
||||
actionName = child.attribute( "name" );
|
||||
actionShortcut = child.attribute( "shortcut" );
|
||||
action = QgsShortcutsManager::instance()->actionByName( actionName );
|
||||
if ( action )
|
||||
QgsShortcutsManager::instance()->setActionShortcut( action, actionShortcut );
|
||||
mManager->setKeySequence( actionName, actionShortcut );
|
||||
|
||||
child = child.nextSiblingElement();
|
||||
}
|
||||
|
||||
@ -242,13 +265,9 @@ void QgsConfigureShortcutsDialog::changeShortcut()
|
||||
|
||||
void QgsConfigureShortcutsDialog::resetShortcut()
|
||||
{
|
||||
QAction* action = currentAction();
|
||||
if ( !action )
|
||||
return;
|
||||
|
||||
// set default shortcut
|
||||
QString shortcut = QgsShortcutsManager::instance()->actionDefaultShortcut( action );
|
||||
setCurrentActionShortcut( shortcut );
|
||||
QObject* object = currentObject();
|
||||
QString sequence = mManager->objectDefaultKeySequence( object );
|
||||
setCurrentActionShortcut( sequence );
|
||||
}
|
||||
|
||||
void QgsConfigureShortcutsDialog::setNoShortcut()
|
||||
@ -258,11 +277,12 @@ void QgsConfigureShortcutsDialog::setNoShortcut()
|
||||
|
||||
QAction* QgsConfigureShortcutsDialog::currentAction()
|
||||
{
|
||||
if ( !treeActions->currentItem() )
|
||||
return nullptr;
|
||||
return qobject_cast<QAction*>( currentObject() );
|
||||
}
|
||||
|
||||
QObject* action = treeActions->currentItem()->data( 0, Qt::UserRole ).value<QObject*>();
|
||||
return qobject_cast<QAction*>( action );
|
||||
QShortcut* QgsConfigureShortcutsDialog::currentShortcut()
|
||||
{
|
||||
return qobject_cast<QShortcut*>( currentObject() );
|
||||
}
|
||||
|
||||
void QgsConfigureShortcutsDialog::actionChanged( QTreeWidgetItem *current, QTreeWidgetItem *previous )
|
||||
@ -272,20 +292,33 @@ void QgsConfigureShortcutsDialog::actionChanged( QTreeWidgetItem *current, QTree
|
||||
// cancel previous shortcut setting (if any)
|
||||
setGettingShortcut( false );
|
||||
|
||||
QAction* action = currentAction();
|
||||
if ( !action )
|
||||
QString shortcut;
|
||||
QKeySequence sequence;
|
||||
if ( QAction* action = currentAction() )
|
||||
{
|
||||
// show which one is the default action
|
||||
shortcut = mManager->defaultKeySequence( action );
|
||||
sequence = action->shortcut();
|
||||
}
|
||||
else if ( QShortcut* object = currentShortcut() )
|
||||
{
|
||||
// show which one is the default action
|
||||
shortcut = mManager->defaultKeySequence( object );
|
||||
sequence = object->key();
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// show which one is the default action
|
||||
QString shortcut = QgsShortcutsManager::instance()->actionDefaultShortcut( action );
|
||||
if ( shortcut.isEmpty() )
|
||||
shortcut = tr( "None" );
|
||||
btnResetShortcut->setText( tr( "Set default (%1)" ).arg( shortcut ) );
|
||||
|
||||
// if there's no shortcut, disable set none
|
||||
btnSetNoShortcut->setEnabled( !action->shortcut().isEmpty() );
|
||||
btnSetNoShortcut->setEnabled( !sequence.isEmpty() );
|
||||
// if the shortcut is default, disable set default
|
||||
btnResetShortcut->setEnabled( action->shortcut() != QKeySequence( shortcut ) );
|
||||
btnResetShortcut->setEnabled( sequence != QKeySequence( shortcut ) );
|
||||
}
|
||||
|
||||
void QgsConfigureShortcutsDialog::keyPressEvent( QKeyEvent * event )
|
||||
@ -369,6 +402,15 @@ void QgsConfigureShortcutsDialog::keyReleaseEvent( QKeyEvent * event )
|
||||
}
|
||||
}
|
||||
|
||||
QObject* QgsConfigureShortcutsDialog::currentObject()
|
||||
{
|
||||
if ( !treeActions->currentItem() )
|
||||
return nullptr;
|
||||
|
||||
QObject* object = treeActions->currentItem()->data( 0, Qt::UserRole ).value<QObject*>();
|
||||
return object;
|
||||
}
|
||||
|
||||
void QgsConfigureShortcutsDialog::updateShortcutText()
|
||||
{
|
||||
// update text of the button so that user can see what has typed already
|
||||
@ -394,33 +436,44 @@ void QgsConfigureShortcutsDialog::setGettingShortcut( bool getting )
|
||||
|
||||
void QgsConfigureShortcutsDialog::setCurrentActionShortcut( const QKeySequence& s )
|
||||
{
|
||||
QAction* action = currentAction();
|
||||
if ( !action )
|
||||
QObject* object = currentObject();
|
||||
if ( !object )
|
||||
return;
|
||||
|
||||
// first check whether this action is not taken already
|
||||
QAction* otherAction = QgsShortcutsManager::instance()->actionForShortcut( s );
|
||||
if ( otherAction )
|
||||
QObject* otherObject = mManager->objectForSequence( s );
|
||||
if ( otherObject == object )
|
||||
return;
|
||||
|
||||
if ( otherObject )
|
||||
{
|
||||
QString otherActionText = otherAction->text();
|
||||
otherActionText.remove( '&' ); // remove the accelerator
|
||||
QString otherText;
|
||||
if ( QAction* otherAction = qobject_cast< QAction* >( otherObject ) )
|
||||
{
|
||||
otherText = otherAction->text();
|
||||
otherText.remove( '&' ); // remove the accelerator
|
||||
}
|
||||
else if ( QShortcut* otherShortcut = qobject_cast< QShortcut* >( otherObject ) )
|
||||
{
|
||||
otherText = otherShortcut->whatsThis();
|
||||
}
|
||||
|
||||
int res = QMessageBox::question( this, tr( "Shortcut conflict" ),
|
||||
tr( "This shortcut is already assigned to action %1. Reassign?" ).arg( otherActionText ),
|
||||
tr( "This shortcut is already assigned to action %1. Reassign?" ).arg( otherText ),
|
||||
QMessageBox::Yes | QMessageBox::No );
|
||||
|
||||
if ( res != QMessageBox::Yes )
|
||||
return;
|
||||
|
||||
// reset action of the conflicting other action!
|
||||
QgsShortcutsManager::instance()->setActionShortcut( otherAction, QString() );
|
||||
QList<QTreeWidgetItem*> items = treeActions->findItems( otherActionText, Qt::MatchExactly );
|
||||
mManager->setObjectKeySequence( otherObject, QString() );
|
||||
QList<QTreeWidgetItem*> items = treeActions->findItems( otherText, Qt::MatchExactly );
|
||||
if ( !items.isEmpty() ) // there should be exactly one
|
||||
items[0]->setText( 1, QString() );
|
||||
}
|
||||
|
||||
// update manager
|
||||
QgsShortcutsManager::instance()->setActionShortcut( action, s.toString() );
|
||||
mManager->setObjectKeySequence( object, s.toString() );
|
||||
|
||||
// update gui
|
||||
treeActions->currentItem()->setText( 1, s.toString() );
|
@ -1,6 +1,6 @@
|
||||
/***************************************************************************
|
||||
qgsconfigureshortcutsdialog.h
|
||||
---------------------
|
||||
-----------------------------
|
||||
begin : May 2009
|
||||
copyright : (C) 2009 by Martin Dobias
|
||||
email : wonder dot sk at gmail dot com
|
||||
@ -19,28 +19,36 @@
|
||||
#include <QDialog>
|
||||
|
||||
#include "ui_qgsconfigureshortcutsdialog.h"
|
||||
#include "qgsshortcutsmanager.h"
|
||||
|
||||
class APP_EXPORT QgsConfigureShortcutsDialog : public QDialog, private Ui::QgsConfigureShortcutsDialog
|
||||
class QShortcut;
|
||||
|
||||
/** \ingroup gui
|
||||
* \class QgsConfigureShortcutsDialog
|
||||
* Reusable dialog for allowing users to configure shortcuts contained in a QgsShortcutsManager.
|
||||
* \note added in QGIS 2.16
|
||||
*/
|
||||
|
||||
class GUI_EXPORT QgsConfigureShortcutsDialog : public QDialog, private Ui::QgsConfigureShortcutsDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QgsConfigureShortcutsDialog( QWidget* parent = nullptr );
|
||||
~QgsConfigureShortcutsDialog();
|
||||
|
||||
void populateActions();
|
||||
/** 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 = nullptr, QgsShortcutsManager* manager = nullptr );
|
||||
|
||||
~QgsConfigureShortcutsDialog();
|
||||
|
||||
protected:
|
||||
void keyPressEvent( QKeyEvent * event ) override;
|
||||
void keyReleaseEvent( QKeyEvent * event ) override;
|
||||
|
||||
QAction* currentAction();
|
||||
|
||||
void setGettingShortcut( bool getting );
|
||||
void setCurrentActionShortcut( const QKeySequence& s );
|
||||
void updateShortcutText();
|
||||
|
||||
public slots:
|
||||
private slots:
|
||||
void changeShortcut();
|
||||
void resetShortcut();
|
||||
void setNoShortcut();
|
||||
@ -49,21 +57,35 @@ class APP_EXPORT QgsConfigureShortcutsDialog : public QDialog, private Ui::QgsCo
|
||||
|
||||
void actionChanged( QTreeWidgetItem* current, QTreeWidgetItem* previous );
|
||||
|
||||
protected:
|
||||
private:
|
||||
|
||||
//! Saves the dialog window state
|
||||
void saveState();
|
||||
|
||||
//! Restores the dialog window state
|
||||
void restoreState();
|
||||
|
||||
//! Populates the dialog with all actions from the manager
|
||||
void populateActions();
|
||||
|
||||
//! Returns the currently selected shortcut object (QAction or QShortcut)
|
||||
QObject* currentObject();
|
||||
|
||||
//! Returns the currently selected action, or null if no action selected
|
||||
QAction* currentAction();
|
||||
|
||||
//! Returns the currently selected QShortcut, or null if no shortcut selected
|
||||
QShortcut* currentShortcut();
|
||||
|
||||
void setGettingShortcut( bool getting );
|
||||
void setCurrentActionShortcut( const QKeySequence& s );
|
||||
void updateShortcutText();
|
||||
|
||||
QgsShortcutsManager* mManager;
|
||||
|
||||
bool mGettingShortcut;
|
||||
int mModifiers, mKey;
|
||||
|
||||
private:
|
||||
/*!
|
||||
* Function to save dialog window state
|
||||
*/
|
||||
void saveState();
|
||||
|
||||
/*!
|
||||
* Function to restore dialog window state
|
||||
*/
|
||||
void restoreState();
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif //QGSCONFIGURESHORTCUTSDIALOG_H
|
297
src/gui/qgsshortcutsmanager.cpp
Normal file
297
src/gui/qgsshortcutsmanager.cpp
Normal file
@ -0,0 +1,297 @@
|
||||
/***************************************************************************
|
||||
qgsshortcutsmanager.cpp
|
||||
---------------------
|
||||
begin : May 2009
|
||||
copyright : (C) 2009 by Martin Dobias
|
||||
email : wonder dot sk at gmail dot com
|
||||
***************************************************************************
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
#include "qgsshortcutsmanager.h"
|
||||
|
||||
#include <QSettings>
|
||||
#include <QShortcut>
|
||||
|
||||
QgsShortcutsManager* QgsShortcutsManager::mInstance = nullptr;
|
||||
|
||||
|
||||
QgsShortcutsManager* QgsShortcutsManager::instance()
|
||||
{
|
||||
if ( !mInstance )
|
||||
mInstance = new QgsShortcutsManager( nullptr );
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
QgsShortcutsManager::QgsShortcutsManager( QObject *parent, const QString& settingsRoot )
|
||||
: QObject( parent )
|
||||
, mSettingsPath( settingsRoot )
|
||||
{
|
||||
}
|
||||
|
||||
void QgsShortcutsManager::registerAllChildren( QObject* object, bool recursive )
|
||||
{
|
||||
registerAllChildActions( object, recursive );
|
||||
registerAllChildShortcuts( object, recursive );
|
||||
}
|
||||
|
||||
void QgsShortcutsManager::registerAllChildActions( QObject* object, bool recursive )
|
||||
{
|
||||
if ( recursive )
|
||||
{
|
||||
QList< QAction* > actions = object->findChildren< QAction* >();
|
||||
Q_FOREACH ( QAction* a, actions )
|
||||
{
|
||||
registerAction( a, a->shortcut() );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Q_FOREACH ( QObject* child, object->children() )
|
||||
{
|
||||
if ( QAction* a = qobject_cast<QAction*>( child ) )
|
||||
{
|
||||
registerAction( a, a->shortcut() );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void QgsShortcutsManager::registerAllChildShortcuts( QObject* object, bool recursive )
|
||||
{
|
||||
if ( recursive )
|
||||
{
|
||||
QList< QShortcut* > shortcuts = object->findChildren< QShortcut* >();
|
||||
Q_FOREACH ( QShortcut* s, shortcuts )
|
||||
{
|
||||
registerShortcut( s, s->key() );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Q_FOREACH ( QObject* child, object->children() )
|
||||
{
|
||||
if ( QShortcut* s = qobject_cast<QShortcut*>( child ) )
|
||||
{
|
||||
registerShortcut( s, s->key() );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool QgsShortcutsManager::registerAction( QAction* action, const QString& defaultSequence )
|
||||
{
|
||||
mActions.insert( action, defaultSequence );
|
||||
connect( action, SIGNAL( destroyed() ), this, SLOT( actionDestroyed() ) );
|
||||
|
||||
QString actionText = action->text();
|
||||
actionText.remove( '&' ); // remove the accelerator
|
||||
|
||||
// load overridden value from settings
|
||||
QSettings settings;
|
||||
QString sequence = settings.value( mSettingsPath + actionText, defaultSequence ).toString();
|
||||
|
||||
action->setShortcut( sequence );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QgsShortcutsManager::registerShortcut( QShortcut* shortcut, const QString& defaultSequence )
|
||||
{
|
||||
mShortcuts.insert( shortcut, defaultSequence );
|
||||
connect( shortcut, SIGNAL( destroyed() ), this, SLOT( shortcutDestroyed() ) );
|
||||
|
||||
QString shortcutName = shortcut->objectName();
|
||||
|
||||
// load overridden value from settings
|
||||
QSettings settings;
|
||||
QString keySequence = settings.value( mSettingsPath + shortcutName, defaultSequence ).toString();
|
||||
|
||||
shortcut->setKey( keySequence );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QgsShortcutsManager::unregisterAction( QAction* action )
|
||||
{
|
||||
if ( !mActions.contains( action ) )
|
||||
return false;
|
||||
|
||||
mActions.remove( action );
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QgsShortcutsManager::unregisterShortcut( QShortcut* shortcut )
|
||||
{
|
||||
if ( !mShortcuts.contains( shortcut ) )
|
||||
return false;
|
||||
|
||||
mShortcuts.remove( shortcut );
|
||||
return true;
|
||||
}
|
||||
|
||||
QList<QAction*> QgsShortcutsManager::listActions() const
|
||||
{
|
||||
return mActions.keys();
|
||||
}
|
||||
|
||||
QList<QShortcut*> QgsShortcutsManager::listShortcuts() const
|
||||
{
|
||||
return mShortcuts.keys();
|
||||
}
|
||||
|
||||
QList<QObject*> QgsShortcutsManager::listAll() const
|
||||
{
|
||||
QList< QObject* > list;
|
||||
ActionsHash::const_iterator actionIt = mActions.constBegin();
|
||||
for ( ; actionIt != mActions.constEnd(); ++actionIt )
|
||||
{
|
||||
list << actionIt.key();
|
||||
}
|
||||
ShortcutsHash::const_iterator shortcutIt = mShortcuts.constBegin();
|
||||
for ( ; shortcutIt != mShortcuts.constEnd(); ++shortcutIt )
|
||||
{
|
||||
list << shortcutIt.key();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
QString QgsShortcutsManager::objectDefaultKeySequence( QObject* object ) const
|
||||
{
|
||||
if ( QAction* action = qobject_cast< QAction* >( object ) )
|
||||
return defaultKeySequence( action );
|
||||
else if ( QShortcut* shortcut = qobject_cast< QShortcut* >( object ) )
|
||||
return defaultKeySequence( shortcut );
|
||||
else
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString QgsShortcutsManager::defaultKeySequence( QAction* action ) const
|
||||
{
|
||||
return mActions.value( action, QString() );
|
||||
}
|
||||
|
||||
QString QgsShortcutsManager::defaultKeySequence( QShortcut* shortcut ) const
|
||||
{
|
||||
return mShortcuts.value( shortcut, QString() );
|
||||
}
|
||||
|
||||
bool QgsShortcutsManager::setKeySequence( const QString& name, const QString& sequence )
|
||||
{
|
||||
if ( QAction* action = actionByName( name ) )
|
||||
return setKeySequence( action, sequence );
|
||||
else if ( QShortcut* shortcut = shortcutByName( name ) )
|
||||
return setKeySequence( shortcut, sequence );
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool QgsShortcutsManager::setObjectKeySequence( QObject* object, const QString& sequence )
|
||||
{
|
||||
if ( QAction* action = qobject_cast< QAction* >( object ) )
|
||||
return setKeySequence( action, sequence );
|
||||
else if ( QShortcut* shortcut = qobject_cast< QShortcut* >( object ) )
|
||||
return setKeySequence( shortcut, sequence );
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool QgsShortcutsManager::setKeySequence( QAction* action, const QString& sequence )
|
||||
{
|
||||
action->setShortcut( sequence );
|
||||
|
||||
QString actionText = action->text();
|
||||
actionText.remove( '&' ); // remove the accelerator
|
||||
|
||||
// save to settings
|
||||
QSettings settings;
|
||||
settings.setValue( mSettingsPath + actionText, sequence );
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QgsShortcutsManager::setKeySequence( QShortcut* shortcut, const QString& sequence )
|
||||
{
|
||||
shortcut->setKey( sequence );
|
||||
|
||||
QString shortcutText = shortcut->objectName();
|
||||
|
||||
// save to settings
|
||||
QSettings settings;
|
||||
settings.setValue( mSettingsPath + shortcutText, sequence );
|
||||
return true;
|
||||
}
|
||||
|
||||
QObject* QgsShortcutsManager::objectForSequence( const QKeySequence& sequence ) const
|
||||
{
|
||||
if ( QAction* action = actionForSequence( sequence ) )
|
||||
return action;
|
||||
else if ( QShortcut* shortcut = shortcutForSequence( sequence ) )
|
||||
return shortcut;
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QAction* QgsShortcutsManager::actionForSequence( const QKeySequence& sequence ) const
|
||||
{
|
||||
if ( sequence.isEmpty() )
|
||||
return nullptr;
|
||||
|
||||
for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
|
||||
{
|
||||
if ( it.key()->shortcut() == sequence )
|
||||
return it.key();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QShortcut* QgsShortcutsManager::shortcutForSequence( const QKeySequence& sequence ) const
|
||||
{
|
||||
if ( sequence.isEmpty() )
|
||||
return nullptr;
|
||||
|
||||
for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
|
||||
{
|
||||
if ( it.key()->key() == sequence )
|
||||
return it.key();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QAction* QgsShortcutsManager::actionByName( const QString& name ) const
|
||||
{
|
||||
for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
|
||||
{
|
||||
if ( it.key()->text() == name )
|
||||
return it.key();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QShortcut* QgsShortcutsManager::shortcutByName( const QString& name ) const
|
||||
{
|
||||
for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
|
||||
{
|
||||
if ( it.key()->objectName() == name )
|
||||
return it.key();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void QgsShortcutsManager::actionDestroyed()
|
||||
{
|
||||
mActions.remove( qobject_cast<QAction*>( sender() ) );
|
||||
}
|
||||
|
||||
void QgsShortcutsManager::shortcutDestroyed()
|
||||
{
|
||||
mShortcuts.remove( qobject_cast<QShortcut*>( sender() ) );
|
||||
}
|
235
src/gui/qgsshortcutsmanager.h
Normal file
235
src/gui/qgsshortcutsmanager.h
Normal file
@ -0,0 +1,235 @@
|
||||
/***************************************************************************
|
||||
qgsshortcutsmanager.h
|
||||
---------------------
|
||||
begin : May 2009
|
||||
copyright : (C) 2009 by Martin Dobias
|
||||
email : wonder dot sk at gmail dot com
|
||||
***************************************************************************
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef QGSSHORTCUTSMANAGER_H
|
||||
#define QGSSHORTCUTSMANAGER_H
|
||||
|
||||
#include <QHash>
|
||||
#include <QList>
|
||||
#include <QAction>
|
||||
|
||||
class QShortcut;
|
||||
|
||||
/** \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 GUI_EXPORT QgsShortcutsManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
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 = 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 { return mSettingsPath; }
|
||||
|
||||
private slots:
|
||||
|
||||
void actionDestroyed();
|
||||
void shortcutDestroyed();
|
||||
|
||||
private:
|
||||
|
||||
typedef QHash< QAction*, QString > ActionsHash;
|
||||
typedef QHash< QShortcut*, QString > ShortcutsHash;
|
||||
|
||||
ActionsHash mActions;
|
||||
ShortcutsHash mShortcuts;
|
||||
QString mSettingsPath;
|
||||
static QgsShortcutsManager* mInstance;
|
||||
};
|
||||
|
||||
#endif // QGSSHORTCUTSMANAGER_H
|
@ -74,6 +74,7 @@ ADD_PYTHON_TEST(PyQgsTabfileProvider test_provider_tabfile.py)
|
||||
ADD_PYTHON_TEST(PyQgsOGRProvider test_provider_ogr.py)
|
||||
ADD_PYTHON_TEST(PyQgsSearchWidgetToolButton test_qgssearchwidgettoolbutton.py)
|
||||
ADD_PYTHON_TEST(PyQgsSearchWidgetWrapper test_qgssearchwidgetwrapper.py)
|
||||
ADD_PYTHON_TEST(PyQgsShortcutsManager test_qgsshortcutsmanager.py)
|
||||
ADD_PYTHON_TEST(PyQgsSpatialIndex test_qgsspatialindex.py)
|
||||
ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_provider_spatialite.py)
|
||||
ADD_PYTHON_TEST(PyQgsSQLStatement test_qgssqlstatement.py)
|
||||
|
451
tests/src/python/test_qgsshortcutsmanager.py
Normal file
451
tests/src/python/test_qgsshortcutsmanager.py
Normal file
@ -0,0 +1,451 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""QGIS Unit tests for QgsActionManager.
|
||||
|
||||
.. note:: This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
"""
|
||||
__author__ = 'Nyall Dawson'
|
||||
__date__ = '28/05/2016'
|
||||
__copyright__ = 'Copyright 2016, The QGIS Project'
|
||||
# This will get replaced with a git SHA1 when you do a git archive
|
||||
__revision__ = '$Format:%H$'
|
||||
|
||||
import qgis # switch sip api
|
||||
|
||||
from qgis.gui import (QgsShortcutsManager)
|
||||
from qgis.PyQt.QtCore import QSettings, QCoreApplication
|
||||
from qgis.PyQt.QtGui import QAction, QShortcut, QKeySequence
|
||||
from qgis.PyQt.QtWidgets import QWidget
|
||||
|
||||
from qgis.testing import (start_app,
|
||||
unittest
|
||||
)
|
||||
|
||||
|
||||
class TestQgsShortcutsManager(unittest.TestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""Run before all tests"""
|
||||
QCoreApplication.setOrganizationName("QGIS_Test")
|
||||
QCoreApplication.setOrganizationDomain("QGIS_TestPyQgsWFSProviderGUI.com")
|
||||
QCoreApplication.setApplicationName("QGIS_TestPyQgsWFSProviderGUI")
|
||||
QSettings().clear()
|
||||
start_app()
|
||||
|
||||
def testInstance(self):
|
||||
""" test retrieving global instance """
|
||||
self.assertTrue(QgsShortcutsManager.instance())
|
||||
|
||||
# register an action to the singleton
|
||||
action = QAction('test', None)
|
||||
QgsShortcutsManager.instance().registerAction(action)
|
||||
# check that the same instance is returned
|
||||
self.assertEqual(QgsShortcutsManager.instance().listActions(), [action])
|
||||
s2 = QgsShortcutsManager()
|
||||
self.assertEqual(s2.listActions(), [])
|
||||
|
||||
def testConstructor(self):
|
||||
""" test constructing managers"""
|
||||
s = QgsShortcutsManager(None, '/my_path/')
|
||||
self.assertEqual(s.settingsPath(), '/my_path/')
|
||||
|
||||
def testSettingsPath(self):
|
||||
""" test that settings path is respected """
|
||||
|
||||
QSettings().clear()
|
||||
|
||||
s1 = QgsShortcutsManager(None, '/path1/')
|
||||
s2 = QgsShortcutsManager(None, '/path2/')
|
||||
|
||||
action1 = QAction('action', None)
|
||||
s1.registerAction(action1)
|
||||
s1.setKeySequence(action1, 'B')
|
||||
|
||||
action2 = QAction('action', None)
|
||||
s2.registerAction(action2)
|
||||
s2.setKeySequence(action2, 'C')
|
||||
|
||||
# test retrieving
|
||||
r1 = QgsShortcutsManager(None, '/path1/')
|
||||
r2 = QgsShortcutsManager(None, '/path2/')
|
||||
|
||||
raction1 = QAction('action', None)
|
||||
r1.registerAction(raction1)
|
||||
raction2 = QAction('action', None)
|
||||
r2.registerAction(raction2)
|
||||
|
||||
self.assertEqual(raction1.shortcut().toString(), 'B')
|
||||
self.assertEqual(raction2.shortcut().toString(), 'C')
|
||||
|
||||
def testRegisterAction(self):
|
||||
""" test registering actions """
|
||||
QSettings().clear()
|
||||
|
||||
s = QgsShortcutsManager(None)
|
||||
|
||||
action1 = QAction('action1', None)
|
||||
action1.setShortcut('x')
|
||||
self.assertTrue(s.registerAction(action1, 'A'))
|
||||
action2 = QAction('action2', None)
|
||||
action2.setShortcut('y')
|
||||
self.assertTrue(s.registerAction(action2, 'B'))
|
||||
|
||||
# actions should have been set to default sequences
|
||||
self.assertEqual(action1.shortcut().toString(), 'A')
|
||||
self.assertEqual(action2.shortcut().toString(), 'B')
|
||||
|
||||
# test that adding an action should set its shortcut automatically
|
||||
s.setKeySequence('action1', 'C')
|
||||
s.setKeySequence('action2', 'D')
|
||||
|
||||
s = QgsShortcutsManager(None)
|
||||
self.assertTrue(s.registerAction(action1, 'A'))
|
||||
self.assertTrue(s.registerAction(action2, 'B'))
|
||||
|
||||
# actions should have been set to previous shortcuts
|
||||
self.assertEqual(action1.shortcut().toString(), 'C')
|
||||
self.assertEqual(action2.shortcut().toString(), 'D')
|
||||
|
||||
# test registering an action containing '&' in name
|
||||
s = QgsShortcutsManager(None)
|
||||
action = QAction('&action1', None)
|
||||
self.assertTrue(s.registerAction(action))
|
||||
self.assertEqual(action1.shortcut().toString(), 'C')
|
||||
|
||||
def testRegisterShortcut(self):
|
||||
""" test registering shortcuts """
|
||||
QSettings().clear()
|
||||
|
||||
s = QgsShortcutsManager(None)
|
||||
|
||||
shortcut1 = QShortcut(None)
|
||||
shortcut1.setKey('x')
|
||||
shortcut1.setObjectName('shortcut1')
|
||||
self.assertTrue(s.registerShortcut(shortcut1, 'A'))
|
||||
shortcut2 = QShortcut(None)
|
||||
shortcut2.setKey('y')
|
||||
shortcut2.setObjectName('shortcut2')
|
||||
self.assertTrue(s.registerShortcut(shortcut2, 'B'))
|
||||
|
||||
# shortcuts should have been set to default sequences
|
||||
self.assertEqual(shortcut1.key().toString(), 'A')
|
||||
self.assertEqual(shortcut2.key().toString(), 'B')
|
||||
|
||||
# test that adding a shortcut should set its sequence automatically
|
||||
s.setKeySequence(shortcut1, 'C')
|
||||
s.setKeySequence(shortcut2, 'D')
|
||||
|
||||
s = QgsShortcutsManager(None)
|
||||
self.assertTrue(s.registerShortcut(shortcut1, 'A'))
|
||||
self.assertTrue(s.registerShortcut(shortcut2, 'B'))
|
||||
|
||||
# shortcuts should have been set to previous sequences
|
||||
self.assertEqual(shortcut1.key().toString(), 'C')
|
||||
self.assertEqual(shortcut2.key().toString(), 'D')
|
||||
|
||||
def testRegisterAll(self):
|
||||
""" test registering all children """
|
||||
|
||||
w = QWidget()
|
||||
action1 = QAction('action1', w)
|
||||
shortcut1 = QShortcut(w)
|
||||
shortcut1.setObjectName('shortcut1')
|
||||
w2 = QWidget(w)
|
||||
action2 = QAction('action2', w2)
|
||||
shortcut2 = QShortcut(w2)
|
||||
shortcut2.setObjectName('shortcut2')
|
||||
|
||||
# recursive
|
||||
s = QgsShortcutsManager()
|
||||
s.registerAllChildActions(w, True)
|
||||
self.assertEqual(set(s.listActions()), set([action1, action2]))
|
||||
s.registerAllChildShortcuts(w, True)
|
||||
self.assertEqual(set(s.listShortcuts()), set([shortcut1, shortcut2]))
|
||||
|
||||
# non recursive
|
||||
s = QgsShortcutsManager()
|
||||
s.registerAllChildActions(w, False)
|
||||
self.assertEqual(set(s.listActions()), set([action1]))
|
||||
s.registerAllChildShortcuts(w, False)
|
||||
self.assertEqual(set(s.listShortcuts()), set([shortcut1]))
|
||||
|
||||
# recursive
|
||||
s = QgsShortcutsManager()
|
||||
s.registerAllChildren(w, True)
|
||||
self.assertEqual(set(s.listActions()), set([action1, action2]))
|
||||
self.assertEqual(set(s.listShortcuts()), set([shortcut1, shortcut2]))
|
||||
|
||||
# non recursive
|
||||
s = QgsShortcutsManager()
|
||||
s.registerAllChildren(w, False)
|
||||
self.assertEqual(set(s.listActions()), set([action1]))
|
||||
self.assertEqual(set(s.listShortcuts()), set([shortcut1]))
|
||||
|
||||
def testUnregister(self):
|
||||
""" test unregistering from manager """
|
||||
|
||||
QSettings().clear()
|
||||
|
||||
s = QgsShortcutsManager(None)
|
||||
|
||||
shortcut1 = QShortcut(None)
|
||||
shortcut1.setKey('x')
|
||||
shortcut1.setObjectName('shortcut1')
|
||||
shortcut2 = QShortcut(None)
|
||||
shortcut2.setKey('y')
|
||||
shortcut2.setObjectName('shortcut2')
|
||||
|
||||
action1 = QAction('action1', None)
|
||||
action1.setShortcut('x')
|
||||
action2 = QAction('action2', None)
|
||||
action2.setShortcut('y')
|
||||
|
||||
# try unregistering objects not registered in manager
|
||||
self.assertFalse(s.unregisterShortcut(shortcut1))
|
||||
self.assertFalse(s.unregisterAction(action1))
|
||||
|
||||
# try unregistering objects from manager
|
||||
s.registerShortcut(shortcut1)
|
||||
s.registerShortcut(shortcut2)
|
||||
s.registerAction(action1)
|
||||
s.registerAction(action2)
|
||||
|
||||
self.assertEqual(set(s.listActions()), set([action1, action2]))
|
||||
self.assertEqual(set(s.listShortcuts()), set([shortcut1, shortcut2]))
|
||||
|
||||
self.assertTrue(s.unregisterAction(action1))
|
||||
self.assertTrue(s.unregisterShortcut(shortcut1))
|
||||
|
||||
self.assertEqual(set(s.listActions()), set([action2]))
|
||||
self.assertEqual(set(s.listShortcuts()), set([shortcut2]))
|
||||
|
||||
self.assertTrue(s.unregisterAction(action2))
|
||||
self.assertTrue(s.unregisterShortcut(shortcut2))
|
||||
|
||||
def testList(self):
|
||||
""" test listing registered objects """
|
||||
|
||||
QSettings().clear()
|
||||
|
||||
s = QgsShortcutsManager(None)
|
||||
|
||||
self.assertEqual(s.listActions(), [])
|
||||
self.assertEqual(s.listShortcuts(), [])
|
||||
self.assertEqual(s.listAll(), [])
|
||||
|
||||
shortcut1 = QShortcut(None)
|
||||
shortcut2 = QShortcut(None)
|
||||
action1 = QAction('action1', None)
|
||||
action2 = QAction('action2', None)
|
||||
s.registerShortcut(shortcut1)
|
||||
s.registerShortcut(shortcut2)
|
||||
s.registerAction(action1)
|
||||
s.registerAction(action2)
|
||||
|
||||
self.assertEqual(set(s.listActions()), set([action1, action2]))
|
||||
self.assertEqual(set(s.listShortcuts()), set([shortcut1, shortcut2]))
|
||||
self.assertEqual(set(s.listAll()), set([action1, action2, shortcut1, shortcut2]))
|
||||
|
||||
def testDefault(self):
|
||||
""" test retrieving default sequences """
|
||||
|
||||
QSettings().clear()
|
||||
|
||||
s = QgsShortcutsManager(None)
|
||||
|
||||
shortcut1 = QShortcut(None)
|
||||
shortcut2 = QShortcut(None)
|
||||
action1 = QAction('action1', None)
|
||||
action2 = QAction('action2', None)
|
||||
|
||||
# test while not yet registered
|
||||
self.assertEqual(s.defaultKeySequence(shortcut1), '')
|
||||
self.assertEqual(s.defaultKeySequence(action1), '')
|
||||
self.assertEqual(s.objectDefaultKeySequence(shortcut1), '')
|
||||
self.assertEqual(s.objectDefaultKeySequence(action1), '')
|
||||
|
||||
# now register them
|
||||
s.registerShortcut(shortcut1, 'A')
|
||||
s.registerShortcut(shortcut2, 'B')
|
||||
s.registerAction(action1, 'C')
|
||||
s.registerAction(action2, 'D')
|
||||
|
||||
self.assertEqual(s.defaultKeySequence(shortcut1), 'A')
|
||||
self.assertEqual(s.defaultKeySequence(shortcut2), 'B')
|
||||
self.assertEqual(s.defaultKeySequence(action1), 'C')
|
||||
self.assertEqual(s.defaultKeySequence(action2), 'D')
|
||||
self.assertEqual(s.objectDefaultKeySequence(shortcut1), 'A')
|
||||
self.assertEqual(s.objectDefaultKeySequence(shortcut2), 'B')
|
||||
self.assertEqual(s.objectDefaultKeySequence(action1), 'C')
|
||||
self.assertEqual(s.objectDefaultKeySequence(action2), 'D')
|
||||
|
||||
def testSetSequence(self):
|
||||
""" test setting key sequences """
|
||||
|
||||
QSettings().clear()
|
||||
|
||||
s = QgsShortcutsManager(None)
|
||||
|
||||
shortcut1 = QShortcut(None)
|
||||
shortcut1.setObjectName('shortcut1')
|
||||
shortcut2 = QShortcut(None)
|
||||
shortcut2.setObjectName('shortcut2')
|
||||
action1 = QAction('action1', None)
|
||||
action2 = QAction('action2', None)
|
||||
|
||||
s.registerShortcut(shortcut1, 'A')
|
||||
s.registerShortcut(shortcut2, 'B')
|
||||
s.registerAction(action1, 'C')
|
||||
s.registerAction(action2, 'D')
|
||||
|
||||
# test setting by action/shortcut
|
||||
self.assertTrue(s.setKeySequence(shortcut1, 'E'))
|
||||
self.assertTrue(s.setKeySequence(shortcut2, 'F'))
|
||||
self.assertTrue(s.setKeySequence(action1, 'G'))
|
||||
self.assertTrue(s.setKeySequence(action2, 'H'))
|
||||
|
||||
# test that action/shortcuts have been updated
|
||||
self.assertEqual(shortcut1.key().toString(), 'E')
|
||||
self.assertEqual(shortcut2.key().toString(), 'F')
|
||||
self.assertEqual(action1.shortcut().toString(), 'G')
|
||||
self.assertEqual(action2.shortcut().toString(), 'H')
|
||||
|
||||
# new manager
|
||||
s = QgsShortcutsManager(None)
|
||||
# new shortcuts
|
||||
shortcut1 = QShortcut(None)
|
||||
shortcut1.setObjectName('shortcut1')
|
||||
shortcut2 = QShortcut(None)
|
||||
shortcut2.setObjectName('shortcut2')
|
||||
action1 = QAction('action1', None)
|
||||
action2 = QAction('action2', None)
|
||||
|
||||
# register them
|
||||
s.registerShortcut(shortcut1, 'A')
|
||||
s.registerShortcut(shortcut2, 'B')
|
||||
s.registerAction(action1, 'C')
|
||||
s.registerAction(action2, 'D')
|
||||
|
||||
# check that previously set sequence has been restored
|
||||
self.assertEqual(shortcut1.key().toString(), 'E')
|
||||
self.assertEqual(shortcut2.key().toString(), 'F')
|
||||
self.assertEqual(action1.shortcut().toString(), 'G')
|
||||
self.assertEqual(action2.shortcut().toString(), 'H')
|
||||
|
||||
# same test, using setObjectKeySequence
|
||||
QSettings().clear()
|
||||
s = QgsShortcutsManager(None)
|
||||
shortcut1 = QShortcut(None)
|
||||
shortcut1.setObjectName('shortcut1')
|
||||
action1 = QAction('action1', None)
|
||||
s.registerShortcut(shortcut1, 'A')
|
||||
s.registerAction(action1, 'C')
|
||||
self.assertTrue(s.setObjectKeySequence(shortcut1, 'E'))
|
||||
self.assertTrue(s.setObjectKeySequence(action1, 'G'))
|
||||
self.assertEqual(shortcut1.key().toString(), 'E')
|
||||
self.assertEqual(action1.shortcut().toString(), 'G')
|
||||
s = QgsShortcutsManager(None)
|
||||
shortcut1 = QShortcut(None)
|
||||
shortcut1.setObjectName('shortcut1')
|
||||
action1 = QAction('action1', None)
|
||||
s.registerShortcut(shortcut1, 'A')
|
||||
s.registerAction(action1, 'C')
|
||||
self.assertEqual(shortcut1.key().toString(), 'E')
|
||||
self.assertEqual(action1.shortcut().toString(), 'G')
|
||||
|
||||
# same test, using setKeySequence by name
|
||||
QSettings().clear()
|
||||
s = QgsShortcutsManager(None)
|
||||
shortcut1 = QShortcut(None)
|
||||
shortcut1.setObjectName('shortcut1')
|
||||
action1 = QAction('action1', None)
|
||||
s.registerShortcut(shortcut1, 'A')
|
||||
s.registerAction(action1, 'C')
|
||||
self.assertFalse(s.setKeySequence('invalid_name', 'E'))
|
||||
self.assertTrue(s.setKeySequence('shortcut1', 'E'))
|
||||
self.assertTrue(s.setKeySequence('action1', 'G'))
|
||||
self.assertEqual(shortcut1.key().toString(), 'E')
|
||||
self.assertEqual(action1.shortcut().toString(), 'G')
|
||||
s = QgsShortcutsManager(None)
|
||||
shortcut1 = QShortcut(None)
|
||||
shortcut1.setObjectName('shortcut1')
|
||||
action1 = QAction('action1', None)
|
||||
s.registerShortcut(shortcut1, 'A')
|
||||
s.registerAction(action1, 'C')
|
||||
self.assertEqual(shortcut1.key().toString(), 'E')
|
||||
self.assertEqual(action1.shortcut().toString(), 'G')
|
||||
|
||||
def testBySequence(self):
|
||||
""" test retrieving by sequence """
|
||||
QSettings().clear()
|
||||
|
||||
shortcut1 = QShortcut(None)
|
||||
shortcut1.setObjectName('shortcut1')
|
||||
shortcut2 = QShortcut(None)
|
||||
shortcut2.setObjectName('shortcut2')
|
||||
action1 = QAction('action1', None)
|
||||
action2 = QAction('action2', None)
|
||||
|
||||
s = QgsShortcutsManager(None)
|
||||
self.assertFalse(s.actionForSequence('E'))
|
||||
self.assertFalse(s.objectForSequence('F'))
|
||||
|
||||
s.registerShortcut(shortcut1, 'E')
|
||||
s.registerShortcut(shortcut2, 'A')
|
||||
s.registerAction(action1, 'F')
|
||||
s.registerAction(action2, 'B')
|
||||
# use another way of registering sequences
|
||||
self.assertTrue(s.setKeySequence(shortcut2, 'G'))
|
||||
self.assertTrue(s.setKeySequence(action2, 'H'))
|
||||
|
||||
self.assertEqual(s.objectForSequence('E'), shortcut1)
|
||||
self.assertEqual(s.objectForSequence('F'), action1)
|
||||
self.assertEqual(s.objectForSequence('G'), shortcut2)
|
||||
self.assertEqual(s.objectForSequence('H'), action2)
|
||||
self.assertFalse(s.objectForSequence('A'))
|
||||
self.assertFalse(s.objectForSequence('B'))
|
||||
|
||||
self.assertEqual(s.shortcutForSequence('E'), shortcut1)
|
||||
self.assertFalse(s.shortcutForSequence('F'))
|
||||
self.assertEqual(s.shortcutForSequence('G'), shortcut2)
|
||||
self.assertFalse(s.shortcutForSequence('H'))
|
||||
self.assertFalse(s.actionForSequence('E'))
|
||||
self.assertEqual(s.actionForSequence('F'), action1)
|
||||
self.assertFalse(s.actionForSequence('G'))
|
||||
self.assertEqual(s.actionForSequence('H'), action2)
|
||||
|
||||
def testByName(self):
|
||||
"""" test retrieving actions and shortcuts by name """
|
||||
QSettings().clear()
|
||||
|
||||
shortcut1 = QShortcut(None)
|
||||
shortcut1.setObjectName('shortcut1')
|
||||
shortcut2 = QShortcut(None)
|
||||
shortcut2.setObjectName('shortcut2')
|
||||
action1 = QAction('action1', None)
|
||||
action2 = QAction('action2', None)
|
||||
|
||||
s = QgsShortcutsManager(None)
|
||||
self.assertFalse(s.actionByName('action1'))
|
||||
self.assertFalse(s.shortcutByName('shortcut1'))
|
||||
|
||||
s.registerShortcut(shortcut1)
|
||||
s.registerShortcut(shortcut2)
|
||||
s.registerAction(action1)
|
||||
s.registerAction(action2)
|
||||
|
||||
self.assertEqual(s.shortcutByName('shortcut1'), shortcut1)
|
||||
self.assertFalse(s.shortcutByName('action1'))
|
||||
self.assertEqual(s.shortcutByName('shortcut2'), shortcut2)
|
||||
self.assertFalse(s.shortcutByName('action2'))
|
||||
self.assertFalse(s.actionByName('shortcut1'))
|
||||
self.assertEqual(s.actionByName('action1'), action1)
|
||||
self.assertFalse(s.actionByName('shortcut2'))
|
||||
self.assertEqual(s.actionByName('action2'), action2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user