Skip to content

Commit

Permalink
Attach QgsSourceSelectProviderRegistry to QgsGui
Browse files Browse the repository at this point in the history
And make it a singleton
  • Loading branch information
elpaso committed Sep 4, 2017
1 parent e868599 commit 90f8730
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
6 changes: 6 additions & 0 deletions python/gui/qgsgui.sip
Expand Up @@ -38,6 +38,12 @@ class QgsGui
:rtype: QgsEditorWidgetRegistry
%End

static QgsSourceSelectProviderRegistry *sourceSelectProviderRegistry();
%Docstring
Returns the global source select provider registry, used for managing all known source select widget factories.
:rtype: QgsSourceSelectProviderRegistry
%End

static QgsShortcutsManager *shortcutsManager();
%Docstring
Returns the global shortcuts manager, used for managing a QAction and QShortcut sequences.
Expand Down
3 changes: 3 additions & 0 deletions python/gui/qgssourceselectproviderregistry.sip
Expand Up @@ -14,6 +14,9 @@ class QgsSourceSelectProviderRegistry
This class keeps a list of source select providers that may add items to the QgsDataSourceManagerDialog
When created, it automatically adds providers from data provider plugins (e.g. PostGIS, WMS, ...)

QgsSourceSelectProviderRegistry is not usually directly created, but rather accessed through
QgsGui.sourceSelectProviderRegistry().

.. versionadded:: 3.0
%End

Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsgui.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgseditorwidgetregistry.h"
#include "qgslayertreeembeddedwidgetregistry.h"
#include "qgsmaplayeractionregistry.h"
#include "qgssourceselectproviderregistry.h"
#include "qgslayoutitemregistry.h"
#include "qgslayoutitemguiregistry.h"
#include "qgslayoutviewrubberband.h"
Expand All @@ -45,6 +46,11 @@ QgsEditorWidgetRegistry *QgsGui::editorWidgetRegistry()
return instance()->mEditorWidgetRegistry;
}

QgsSourceSelectProviderRegistry *QgsGui::sourceSelectProviderRegistry()
{
return instance()->mSourceSelectProviderRegistry;
}

QgsShortcutsManager *QgsGui::shortcutsManager()
{
return instance()->mShortcutsManager;
Expand All @@ -71,6 +77,7 @@ QgsGui::~QgsGui()
delete mLayerTreeEmbeddedWidgetRegistry;
delete mEditorWidgetRegistry;
delete mMapLayerActionRegistry;
delete mSourceSelectProviderRegistry;
delete mShortcutsManager;
delete mNative;
}
Expand All @@ -87,6 +94,7 @@ QgsGui::QgsGui()
mShortcutsManager = new QgsShortcutsManager();
mLayerTreeEmbeddedWidgetRegistry = new QgsLayerTreeEmbeddedWidgetRegistry();
mMapLayerActionRegistry = new QgsMapLayerActionRegistry();
mSourceSelectProviderRegistry = new QgsSourceSelectProviderRegistry();
mLayoutItemGuiRegistry = new QgsLayoutItemGuiRegistry();
mLayoutItemGuiRegistry->populate();
}
7 changes: 7 additions & 0 deletions src/gui/qgsgui.h
Expand Up @@ -25,6 +25,7 @@ class QgsEditorWidgetRegistry;
class QgsShortcutsManager;
class QgsLayerTreeEmbeddedWidgetRegistry;
class QgsMapLayerActionRegistry;
class QgsSourceSelectProviderRegistry;
class QgsNative;
class QgsLayoutItemGuiRegistry;

Expand Down Expand Up @@ -61,6 +62,11 @@ class GUI_EXPORT QgsGui
*/
static QgsEditorWidgetRegistry *editorWidgetRegistry();

/**
* Returns the global source select provider registry, used for managing all known source select widget factories.
*/
static QgsSourceSelectProviderRegistry *sourceSelectProviderRegistry();

/**
* Returns the global shortcuts manager, used for managing a QAction and QShortcut sequences.
*/
Expand Down Expand Up @@ -89,6 +95,7 @@ class GUI_EXPORT QgsGui

QgsNative *mNative = nullptr;
QgsEditorWidgetRegistry *mEditorWidgetRegistry = nullptr;
QgsSourceSelectProviderRegistry *mSourceSelectProviderRegistry = nullptr;
QgsShortcutsManager *mShortcutsManager = nullptr;
QgsLayerTreeEmbeddedWidgetRegistry *mLayerTreeEmbeddedWidgetRegistry = nullptr;
QgsMapLayerActionRegistry *mMapLayerActionRegistry = nullptr;
Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgssourceselectproviderregistry.h
Expand Up @@ -25,6 +25,9 @@ class QgsSourceSelectProvider;
* This class keeps a list of source select providers that may add items to the QgsDataSourceManagerDialog
* When created, it automatically adds providers from data provider plugins (e.g. PostGIS, WMS, ...)
*
* QgsSourceSelectProviderRegistry is not usually directly created, but rather accessed through
* QgsGui::sourceSelectProviderRegistry().
*
* \since QGIS 3.0
*/
class GUI_EXPORT QgsSourceSelectProviderRegistry
Expand Down
18 changes: 13 additions & 5 deletions tests/src/python/test_qgssourceselectprovider.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
"""
Test the QgsSourceSelectProvider class
Test the QgsSourceSelectProvider
and QgsSourceSelectProviderRegistry classes
Run with: ctest -V -R PyQgsSourceSelectProvider
Expand All @@ -12,7 +13,7 @@

import os
import tempfile
from qgis.gui import (QgsSourceSelectProvider, QgsSourceSelectProviderRegistry, QgsAbstractDataSourceWidget)
from qgis.gui import (QgsGui, QgsSourceSelectProvider, QgsSourceSelectProviderRegistry, QgsAbstractDataSourceWidget)
from qgis.testing import start_app, unittest
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QWidget
Expand Down Expand Up @@ -90,9 +91,8 @@ def testConcreteClass(self):
self.assertEqual(provider.ordering(), 1)
self.assertTrue(isinstance(provider.icon(), QIcon))

def testRegistry(self):
def _testRegistry(self, registry):

registry = QgsSourceSelectProviderRegistry()
registry.addProvider(ConcreteSourceSelectProvider())
registry.addProvider(ConcreteSourceSelectProvider2())

Expand All @@ -113,13 +113,21 @@ def testRegistry(self):
# Get not existent by name
self.assertFalse(registry.providerByName('Oh This Is Missing!'))

# Get providers by provider key
# Get providers by data provider key
self.assertGreater(len(registry.providersByKey('MyTestProviderKey')), 0)
self.assertGreater(len(registry.providersByKey('MyTestProviderKey2')), 0)

# Get not existent by key
self.assertEqual(len(registry.providersByKey('Oh This Is Missing!')), 0)

def testRegistry(self):
registry = QgsSourceSelectProviderRegistry()
self._testRegistry(registry)

def testRegistrySingleton(self):
registry = QgsGui.sourceSelectProviderRegistry()
self._testRegistry(registry)


if __name__ == '__main__':
unittest.main()

0 comments on commit 90f8730

Please sign in to comment.