Skip to content

Commit

Permalink
QgsShortcutsManager is no longer a singleton
Browse files Browse the repository at this point in the history
Move the global instance to QgsGui::shortcutsManager()
  • Loading branch information
nyalldawson committed May 14, 2017
1 parent 4a5c9a7 commit 711f6dc
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 28 deletions.
4 changes: 4 additions & 0 deletions doc/api_break.dox
Expand Up @@ -1910,6 +1910,10 @@ QgsServer {#qgis_api_break_3_0_QgsServer}
- QgsServer::handleRequest( const QString &urlstr ) has been removed in favour of the new
- QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &response ) has been added

QgsShortcutsManager {#qgis_api_break_3_0_QgsShortcutsManager}
-------------------

- QgsShortcutsManager::instance() was removed. Use QgsGui::shortcutsManager() instead.


QgsSimpleMarkerSymbolLayer {#qgis_api_break_3_0_QgsSimpleMarkerSymbolLayer}
Expand Down
7 changes: 6 additions & 1 deletion python/gui/qgsgui.sip
Expand Up @@ -34,10 +34,15 @@ class QgsGui
static QgsEditorWidgetRegistry *editorWidgetRegistry();
%Docstring
Returns the global editor widget registry, used for managing all known edit widget factories.
.. versionadded:: 3.0
:rtype: QgsEditorWidgetRegistry
%End

static QgsShortcutsManager *shortcutsManager();
%Docstring
Returns the global shortcuts manager, used for managing a QAction and QShortcut sequences.
:rtype: QgsShortcutsManager
%End

~QgsGui();

private:
Expand Down
3 changes: 0 additions & 3 deletions python/gui/qgsshortcutsmanager.sip
Expand Up @@ -13,9 +13,6 @@ class QgsShortcutsManager: QObject

public:

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

/** Constructor for QgsShortcutsManager.
* @param parent parent object
* @param settingsRoot root QgsSettings path for storing settings, e.g., "/myplugin/shortcuts". Leave
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -1160,7 +1160,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()->registerAllChildren( this );
QgsGui::shortcutsManager()->registerAllChildren( this );

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

Expand Down
5 changes: 3 additions & 2 deletions src/app/qgisappinterface.cpp
Expand Up @@ -31,6 +31,7 @@
#include "qgsapplayertreeviewmenuprovider.h"
#include "qgscomposer.h"
#include "qgscomposerview.h"
#include "qgsgui.h"
#include "qgsmaplayer.h"
#include "qgsmaptooladvanceddigitizing.h"
#include "qgsmapcanvas.h"
Expand Down Expand Up @@ -491,12 +492,12 @@ void QgisAppInterface::removeWindow( QAction *action )

bool QgisAppInterface::registerMainWindowAction( QAction *action, const QString &defaultShortcut )
{
return QgsShortcutsManager::instance()->registerAction( action, defaultShortcut );
return QgsGui::shortcutsManager()->registerAction( action, defaultShortcut );
}

bool QgisAppInterface::unregisterMainWindowAction( QAction *action )
{
return QgsShortcutsManager::instance()->unregisterAction( action );
return QgsGui::shortcutsManager()->unregisterAction( action );
}

void QgisAppInterface::registerMapLayerConfigWidgetFactory( QgsMapLayerConfigWidgetFactory *factory )
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsconfigureshortcutsdialog.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgsshortcutsmanager.h"
#include "qgslogger.h"
#include "qgssettings.h"
#include "qgsgui.h"

#include <QKeyEvent>
#include <QKeySequence>
Expand All @@ -38,7 +39,7 @@ QgsConfigureShortcutsDialog::QgsConfigureShortcutsDialog( QWidget *parent, QgsSh
setupUi( this );

if ( !mManager )
mManager = QgsShortcutsManager::instance();
mManager = QgsGui::shortcutsManager();

connect( btnChangeShortcut, &QAbstractButton::clicked, this, &QgsConfigureShortcutsDialog::changeShortcut );
connect( btnResetShortcut, &QAbstractButton::clicked, this, &QgsConfigureShortcutsDialog::resetShortcut );
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsgui.cpp
Expand Up @@ -17,6 +17,7 @@

#include "qgsgui.h"
#include "qgseditorwidgetregistry.h"
#include "qgsshortcutsmanager.h"

QgsGui *QgsGui::instance()
{
Expand All @@ -29,12 +30,19 @@ QgsEditorWidgetRegistry *QgsGui::editorWidgetRegistry()
return instance()->mEditorWidgetRegistry;
}

QgsShortcutsManager *QgsGui::shortcutsManager()
{
return instance()->mShortcutsManager;
}

QgsGui::~QgsGui()
{
delete mEditorWidgetRegistry;
delete mShortcutsManager;
}

QgsGui::QgsGui()
{
mEditorWidgetRegistry = new QgsEditorWidgetRegistry();
mShortcutsManager = new QgsShortcutsManager();
}
8 changes: 7 additions & 1 deletion src/gui/qgsgui.h
Expand Up @@ -21,6 +21,7 @@
#include "qgis_gui.h"

class QgsEditorWidgetRegistry;
class QgsShortcutsManager;

/**
* \ingroup gui
Expand All @@ -45,17 +46,22 @@ class GUI_EXPORT QgsGui

/**
* Returns the global editor widget registry, used for managing all known edit widget factories.
* \since QGIS 3.0
*/
static QgsEditorWidgetRegistry *editorWidgetRegistry();

/**
* Returns the global shortcuts manager, used for managing a QAction and QShortcut sequences.
*/
static QgsShortcutsManager *shortcutsManager();

~QgsGui();

private:

QgsGui();

QgsEditorWidgetRegistry *mEditorWidgetRegistry = nullptr;
QgsShortcutsManager *mShortcutsManager = nullptr;

#ifdef SIP_RUN
QgsGui( const QgsGui &other );
Expand Down
10 changes: 0 additions & 10 deletions src/gui/qgsshortcutsmanager.cpp
Expand Up @@ -19,16 +19,6 @@

#include <QShortcut>

QgsShortcutsManager *QgsShortcutsManager::sInstance = nullptr;


QgsShortcutsManager *QgsShortcutsManager::instance()
{
if ( !sInstance )
sInstance = new QgsShortcutsManager( nullptr );
return sInstance;
}

QgsShortcutsManager::QgsShortcutsManager( QObject *parent, const QString &settingsRoot )
: QObject( parent )
, mSettingsPath( settingsRoot )
Expand Down
9 changes: 4 additions & 5 deletions src/gui/qgsshortcutsmanager.h
Expand Up @@ -26,7 +26,10 @@ 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.
* that have been registered and their shortcuts can be changed.
*
* QgsShortcutsManager is not usually directly created, but rather accessed through
* QgsGui::shortcutsManager().
* \since QGIS 2.16
*/
class GUI_EXPORT QgsShortcutsManager : public QObject
Expand All @@ -35,9 +38,6 @@ class GUI_EXPORT QgsShortcutsManager : public QObject

public:

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

/** Constructor for QgsShortcutsManager.
* \param parent parent object
* \param settingsRoot root QgsSettings path for storing settings, e.g., "/myplugin/shortcuts". Leave
Expand Down Expand Up @@ -230,7 +230,6 @@ class GUI_EXPORT QgsShortcutsManager : public QObject
ActionsHash mActions;
ShortcutsHash mShortcuts;
QString mSettingsPath;
static QgsShortcutsManager *sInstance;

/**
* Updates the action to include the shortcut keys. Shortcut keys are
Expand Down
8 changes: 4 additions & 4 deletions tests/src/python/test_qgsshortcutsmanager.py
Expand Up @@ -15,7 +15,7 @@
import qgis # NOQA

from qgis.core import QgsSettings
from qgis.gui import QgsShortcutsManager
from qgis.gui import QgsShortcutsManager, QgsGui
from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtWidgets import QWidget, QAction, QShortcut

Expand All @@ -35,13 +35,13 @@ def setUpClass(cls):

def testInstance(self):
""" test retrieving global instance """
self.assertTrue(QgsShortcutsManager.instance())
self.assertTrue(QgsGui.shortcutsManager())

# register an action to the singleton
action = QAction('test', None)
QgsShortcutsManager.instance().registerAction(action)
QgsGui.shortcutsManager().registerAction(action)
# check that the same instance is returned
self.assertEqual(QgsShortcutsManager.instance().listActions(), [action])
self.assertEqual(QgsGui.shortcutsManager().listActions(), [action])
s2 = QgsShortcutsManager()
self.assertEqual(s2.listActions(), [])

Expand Down

0 comments on commit 711f6dc

Please sign in to comment.