Skip to content

Commit 6ec7f02

Browse files
committedNov 4, 2018
Add a QgsDataItemGuiProvider interface and registry for providers
These providers will be used to control how the browser data items behave within GUI, and to allow separation of GUI related properties of browser items from the core code. A new registry QgsDataItemGuiProviderRegistry has been created (modeled off QgsDataItemProviderRegistry), with an application wide instance available from QgsGui::instance()->dataItemGuiProviderRegistry()
1 parent 0b17db6 commit 6ec7f02

16 files changed

+404
-8
lines changed
 

‎python/core/auto_generated/qgsdataitemproviderregistry.sip.in

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,19 @@ QgsDataItemProviderRegistry is not usually directly created, but rather accessed
3434

3535
QList<QgsDataItemProvider *> providers() const;
3636
%Docstring
37-
Gets list of available providers
37+
Returns the list of available providers.
3838
%End
3939

4040
void addProvider( QgsDataItemProvider *provider /Transfer/ );
4141
%Docstring
42-
Add a provider implementation. Takes ownership of the object.
42+
Adds a ``provider`` implementation to the registry. Ownership of the provider
43+
is transferred to the registry.
4344
%End
4445

4546
void removeProvider( QgsDataItemProvider *provider );
4647
%Docstring
47-
Remove provider implementation from the list (provider object is deleted)
48+
Removes a ``provider`` implementation from the registry.
49+
The provider object is automatically deleted.
4850
%End
4951

5052
private:
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/gui/qgsdataitemguiprovider.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsDataItemGuiProvider
13+
{
14+
%Docstring
15+
16+
Abstract base class for providers which affect how QgsDataItem items behave
17+
within the application GUI.
18+
19+
Providers must be registered via QgsDataItemGuiProviderRegistry.
20+
21+
.. versionadded:: 3.6
22+
%End
23+
24+
%TypeHeaderCode
25+
#include "qgsdataitemguiprovider.h"
26+
%End
27+
public:
28+
29+
virtual ~QgsDataItemGuiProvider();
30+
31+
virtual QString name() = 0;
32+
%Docstring
33+
Returns the provider's name.
34+
%End
35+
36+
};
37+
38+
/************************************************************************
39+
* This file has been generated automatically from *
40+
* *
41+
* src/gui/qgsdataitemguiprovider.h *
42+
* *
43+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
44+
************************************************************************/
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/gui/qgsdataitemguiproviderregistry.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsDataItemGuiProviderRegistry
13+
{
14+
%Docstring
15+
This class keeps a list of data item GUI providers that may affect how QgsDataItems
16+
behave within the application GUI.
17+
18+
QgsDataItemGuiProviderRegistry is not usually directly created, but rather accessed through
19+
QgsGui.instance()->dataItemGuiProviderRegistry().
20+
21+
.. versionadded:: 3.6
22+
%End
23+
24+
%TypeHeaderCode
25+
#include "qgsdataitemguiproviderregistry.h"
26+
%End
27+
public:
28+
29+
QgsDataItemGuiProviderRegistry();
30+
31+
~QgsDataItemGuiProviderRegistry();
32+
33+
34+
QList<QgsDataItemGuiProvider *> providers() const;
35+
%Docstring
36+
Returns the list of available providers.
37+
%End
38+
39+
void addProvider( QgsDataItemGuiProvider *provider /Transfer/ );
40+
%Docstring
41+
Adds a ``provider`` implementation to the registry. Ownership of the provider
42+
is transferred to the registry.
43+
%End
44+
45+
void removeProvider( QgsDataItemGuiProvider *provider );
46+
%Docstring
47+
Removes a ``provider`` implementation from the registry.
48+
The provider object is automatically deleted.
49+
%End
50+
51+
private:
52+
QgsDataItemGuiProviderRegistry( const QgsDataItemGuiProviderRegistry &rh );
53+
};
54+
55+
/************************************************************************
56+
* This file has been generated automatically from *
57+
* *
58+
* src/gui/qgsdataitemguiproviderregistry.h *
59+
* *
60+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
61+
************************************************************************/

‎python/gui/auto_generated/qgsgui.sip.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ Returns the global processing gui registry, used for registering the GUI behavio
7474
Returns the global processing recent algorithm log, used for tracking recently used processing algorithms.
7575

7676
.. versionadded:: 3.4
77+
%End
78+
79+
static QgsDataItemGuiProviderRegistry *dataItemGuiProviderRegistry();
80+
%Docstring
81+
Returns the global data item GUI provider registry, used for tracking providers which affect the browser
82+
GUI.
83+
84+
.. versionadded:: 3.6
7785
%End
7886

7987
static void enableAutoGeometryRestore( QWidget *widget, const QString &key = QString() );

‎python/gui/gui_auto.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Include auto-generated SIP files
22
%Include auto_generated/qgsattributeforminterface.sip
3+
%Include auto_generated/qgsdataitemguiprovider.sip
4+
%Include auto_generated/qgsdataitemguiproviderregistry.sip
35
%Include auto_generated/qgsdetaileditemdata.sip
46
%Include auto_generated/qgsexpressionbuilderdialog.sip
57
%Include auto_generated/qgsgeometryrubberband.sip

‎src/core/qgsdataitemproviderregistry.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,29 @@ class CORE_EXPORT QgsDataItemProviderRegistry
4646
//! QgsDataItemProviderRegistry cannot be copied.
4747
QgsDataItemProviderRegistry &operator=( const QgsDataItemProviderRegistry &rh ) = delete;
4848

49-
//! Gets list of available providers
49+
/**
50+
* Returns the list of available providers.
51+
*/
5052
QList<QgsDataItemProvider *> providers() const { return mProviders; }
5153

52-
//! Add a provider implementation. Takes ownership of the object.
54+
/**
55+
* Adds a \a provider implementation to the registry. Ownership of the provider
56+
* is transferred to the registry.
57+
*/
5358
void addProvider( QgsDataItemProvider *provider SIP_TRANSFER );
5459

55-
//! Remove provider implementation from the list (provider object is deleted)
60+
/**
61+
* Removes a \a provider implementation from the registry.
62+
* The provider object is automatically deleted.
63+
*/
5664
void removeProvider( QgsDataItemProvider *provider );
5765

5866
private:
5967
#ifdef SIP_RUN
6068
QgsDataItemProviderRegistry( const QgsDataItemProviderRegistry &rh );
6169
#endif
6270

63-
//! available providers. this class owns the pointers
71+
//! Available providers, owned by this class
6472
QList<QgsDataItemProvider *> mProviders;
6573

6674
};

‎src/gui/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ SET(QGIS_GUI_SRCS
243243
qgscredentialdialog.cpp
244244
qgscustomdrophandler.cpp
245245
qgscurveeditorwidget.cpp
246+
qgsdataitemguiprovider.cpp
247+
qgsdataitemguiproviderregistry.cpp
246248
qgsdatumtransformdialog.cpp
247249
qgsdatasourceselectdialog.cpp
248250
qgsdetaileditemdata.cpp
@@ -767,6 +769,8 @@ ENDIF(MSVC)
767769
SET(QGIS_GUI_HDRS
768770
qgsattributeforminterface.h
769771
qgsattributeformlegacyinterface.h
772+
qgsdataitemguiprovider.h
773+
qgsdataitemguiproviderregistry.h
770774
qgsdetaileditemdata.h
771775
qgsexpressionbuilderdialog.h
772776
qgsgeometryrubberband.h

‎src/gui/qgsdataitemguiprovider.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/***************************************************************************
2+
qgsdataitemguiprovider.cpp
3+
--------------------------------------
4+
Date : October 2018
5+
Copyright : (C) 2018 by Nyall Dawson
6+
Email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgsdataitemguiprovider.h"
17+
18+
// no implementation currently

‎src/gui/qgsdataitemguiprovider.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/***************************************************************************
2+
qgsdataitemguiprovider.h
3+
--------------------------------------
4+
Date : October 2018
5+
Copyright : (C) 2018 by Nyall Dawson
6+
Email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGSDATAITEMGUIPROVIDER_H
17+
#define QGSDATAITEMGUIPROVIDER_H
18+
19+
#include "qgis_gui.h"
20+
21+
class QString;
22+
23+
/**
24+
* \class QgsDataItemGuiProvider
25+
* \ingroup gui
26+
*
27+
* Abstract base class for providers which affect how QgsDataItem items behave
28+
* within the application GUI.
29+
*
30+
* Providers must be registered via QgsDataItemGuiProviderRegistry.
31+
*
32+
* \since QGIS 3.6
33+
*/
34+
class GUI_EXPORT QgsDataItemGuiProvider
35+
{
36+
public:
37+
38+
virtual ~QgsDataItemGuiProvider() = default;
39+
40+
/**
41+
* Returns the provider's name.
42+
*/
43+
virtual QString name() = 0;
44+
45+
};
46+
47+
#endif // QGSDATAITEMGUIPROVIDER_H
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/***************************************************************************
2+
qgsdataitemguiproviderregistry.cpp
3+
--------------------------------------
4+
Date : October 2018
5+
Copyright : (C) 2018 by Nyall Dawson
6+
Email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgsdataitemguiproviderregistry.h"
17+
#include "qgsdataitemguiprovider.h"
18+
19+
QgsDataItemGuiProviderRegistry::QgsDataItemGuiProviderRegistry()
20+
{
21+
}
22+
23+
QgsDataItemGuiProviderRegistry::~QgsDataItemGuiProviderRegistry()
24+
{
25+
qDeleteAll( mProviders );
26+
}
27+
28+
void QgsDataItemGuiProviderRegistry::addProvider( QgsDataItemGuiProvider *provider )
29+
{
30+
mProviders.append( provider );
31+
}
32+
33+
void QgsDataItemGuiProviderRegistry::removeProvider( QgsDataItemGuiProvider *provider )
34+
{
35+
int index = mProviders.indexOf( provider );
36+
if ( index >= 0 )
37+
delete mProviders.takeAt( index );
38+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/***************************************************************************
2+
qgsdataitemguiproviderregistry.h
3+
--------------------------------------
4+
Date : October 2018
5+
Copyright : (C) 2018 by Nyall Dawson
6+
Email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGSDATAITEMGUIPROVIDERREGISTRY_H
17+
#define QGSDATAITEMGUIPROVIDERREGISTRY_H
18+
19+
#include "qgis_gui.h"
20+
#include "qgis_sip.h"
21+
#include <QList>
22+
23+
class QgsDataItemGuiProvider;
24+
25+
/**
26+
* \class QgsDataItemGuiProviderRegistry
27+
* \ingroup gui
28+
* This class keeps a list of data item GUI providers that may affect how QgsDataItems
29+
* behave within the application GUI.
30+
*
31+
* QgsDataItemGuiProviderRegistry is not usually directly created, but rather accessed through
32+
* QgsGui::instance()->dataItemGuiProviderRegistry().
33+
*
34+
* \since QGIS 3.6
35+
*/
36+
class GUI_EXPORT QgsDataItemGuiProviderRegistry
37+
{
38+
public:
39+
40+
QgsDataItemGuiProviderRegistry();
41+
42+
~QgsDataItemGuiProviderRegistry();
43+
44+
//! QgsDataItemGuiProviderRegistry cannot be copied.
45+
QgsDataItemGuiProviderRegistry( const QgsDataItemGuiProviderRegistry &rh ) = delete;
46+
//! QgsDataItemGuiProviderRegistry cannot be copied.
47+
QgsDataItemGuiProviderRegistry &operator=( const QgsDataItemGuiProviderRegistry &rh ) = delete;
48+
49+
/**
50+
* Returns the list of available providers.
51+
*/
52+
QList<QgsDataItemGuiProvider *> providers() const { return mProviders; }
53+
54+
/**
55+
* Adds a \a provider implementation to the registry. Ownership of the provider
56+
* is transferred to the registry.
57+
*/
58+
void addProvider( QgsDataItemGuiProvider *provider SIP_TRANSFER );
59+
60+
/**
61+
* Removes a \a provider implementation from the registry.
62+
* The provider object is automatically deleted.
63+
*/
64+
void removeProvider( QgsDataItemGuiProvider *provider );
65+
66+
private:
67+
#ifdef SIP_RUN
68+
QgsDataItemGuiProviderRegistry( const QgsDataItemGuiProviderRegistry &rh );
69+
#endif
70+
71+
//! Available providers, owned by this class
72+
QList<QgsDataItemGuiProvider *> mProviders;
73+
74+
};
75+
76+
#endif // QGSDATAITEMGUIPROVIDERREGISTRY_H

‎src/gui/qgsgui.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "qgsprocessingrecentalgorithmlog.h"
4444
#include "qgswindowmanagerinterface.h"
4545
#include "qgssettings.h"
46+
#include "qgsdataitemguiproviderregistry.h"
4647

4748
QgsGui *QgsGui::instance()
4849
{
@@ -95,6 +96,11 @@ QgsProcessingRecentAlgorithmLog *QgsGui::processingRecentAlgorithmLog()
9596
return instance()->mProcessingRecentAlgorithmLog;
9697
}
9798

99+
QgsDataItemGuiProviderRegistry *QgsGui::dataItemGuiProviderRegistry()
100+
{
101+
return instance()->mDataItemGuiProviderRegistry;
102+
}
103+
98104
void QgsGui::enableAutoGeometryRestore( QWidget *widget, const QString &key )
99105
{
100106
if ( widget->objectName().isEmpty() )
@@ -130,6 +136,7 @@ QgsGui::HigFlags QgsGui::higFlags()
130136
QgsGui::~QgsGui()
131137
{
132138
delete mProcessingGuiRegistry;
139+
delete mDataItemGuiProviderRegistry;
133140
delete mProcessingRecentAlgorithmLog;
134141
delete mLayoutItemGuiRegistry;
135142
delete mLayerTreeEmbeddedWidgetRegistry;
@@ -168,4 +175,5 @@ QgsGui::QgsGui()
168175
mWidgetStateHelper = new QgsWidgetStateHelper();
169176
mProcessingRecentAlgorithmLog = new QgsProcessingRecentAlgorithmLog();
170177
mProcessingGuiRegistry = new QgsProcessingGuiRegistry();
178+
mDataItemGuiProviderRegistry = new QgsDataItemGuiProviderRegistry();
171179
}

‎src/gui/qgsgui.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class QgsWidgetStateHelper;
3434
class QgsProcessingGuiRegistry;
3535
class QgsProcessingRecentAlgorithmLog;
3636
class QgsWindowManagerInterface;
37+
class QgsDataItemGuiProviderRegistry;
3738

3839
/**
3940
* \ingroup gui
@@ -105,6 +106,13 @@ class GUI_EXPORT QgsGui
105106
*/
106107
static QgsProcessingRecentAlgorithmLog *processingRecentAlgorithmLog();
107108

109+
/**
110+
* Returns the global data item GUI provider registry, used for tracking providers which affect the browser
111+
* GUI.
112+
* \since QGIS 3.6
113+
*/
114+
static QgsDataItemGuiProviderRegistry *dataItemGuiProviderRegistry();
115+
108116
/**
109117
* Register the widget to allow its position to be automatically saved and restored when open and closed.
110118
* Use this to avoid needing to call saveGeometry() and restoreGeometry() on your widget.
@@ -158,6 +166,7 @@ class GUI_EXPORT QgsGui
158166
QgsLayoutItemGuiRegistry *mLayoutItemGuiRegistry = nullptr;
159167
QgsProcessingGuiRegistry *mProcessingGuiRegistry = nullptr;
160168
QgsProcessingRecentAlgorithmLog *mProcessingRecentAlgorithmLog = nullptr;
169+
QgsDataItemGuiProviderRegistry *mDataItemGuiProviderRegistry = nullptr;
161170
std::unique_ptr< QgsWindowManagerInterface > mWindowManager;
162171

163172
#ifdef SIP_RUN

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ADD_PYTHON_TEST(PyQgsCoordinateTransformContext test_qgscoordinatetransformconte
3434
ADD_PYTHON_TEST(PyQgsDefaultValue test_qgsdefaultvalue.py)
3535
ADD_PYTHON_TEST(PyQgsXmlUtils test_qgsxmlutils.py)
3636
ADD_PYTHON_TEST(PyQgsCoordinateTransform test_qgscoordinatetransform.py)
37+
ADD_PYTHON_TEST(PyQgsDataItemGuiProviderRegistry test_qgsdataitemguiproviderregistry.py)
3738
ADD_PYTHON_TEST(PyQgsDataItemProviderRegistry test_qgsdataitemproviderregistry.py)
3839
ADD_PYTHON_TEST(PyQgsDateTimeEdit test_qgsdatetimeedit.py)
3940
ADD_PYTHON_TEST(PyQgsDateTimeStatisticalSummary test_qgsdatetimestatisticalsummary.py)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsDataItemGuiProviderRegistry
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '27/10/2018'
11+
__copyright__ = 'Copyright 2018, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
17+
from qgis.gui import (QgsGui,
18+
QgsDataItemGuiProvider,
19+
QgsDataItemGuiProviderRegistry)
20+
from qgis.testing import start_app, unittest
21+
22+
app = start_app()
23+
24+
25+
class TestProvider(QgsDataItemGuiProvider):
26+
27+
def __init__(self, name):
28+
super().__init__()
29+
self._name = name
30+
31+
def name(self):
32+
return self._name
33+
34+
35+
class TestQgsDataItemGuiProviderRegistry(unittest.TestCase):
36+
37+
def testAppRegistry(self):
38+
# ensure there is an application instance
39+
self.assertIsNotNone(QgsGui.dataItemGuiProviderRegistry())
40+
41+
def testRegistry(self):
42+
registry = QgsDataItemGuiProviderRegistry()
43+
initial_providers = registry.providers()
44+
45+
# add a new provider
46+
p1 = TestProvider('p1')
47+
registry.addProvider(p1)
48+
self.assertIn(p1, registry.providers())
49+
50+
p2 = TestProvider('p2')
51+
registry.addProvider(p2)
52+
self.assertIn(p1, registry.providers())
53+
self.assertIn(p2, registry.providers())
54+
55+
registry.removeProvider(None)
56+
p3 = TestProvider('p3')
57+
# not in registry yet
58+
registry.removeProvider(p3)
59+
60+
registry.removeProvider(p1)
61+
self.assertNotIn('p1', [p.name() for p in registry.providers()])
62+
self.assertIn(p2, registry.providers())
63+
64+
registry.removeProvider(p2)
65+
self.assertNotIn('p2', [p.name() for p in registry.providers()])
66+
self.assertEqual(registry.providers(), initial_providers)
67+
68+
69+
if __name__ == '__main__':
70+
unittest.main()

‎tests/src/python/test_qgsdataitemproviderregistry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def testRegistry(self):
7373
registry.removeProvider(p2)
7474
self.assertNotIn('p2', [p.name() for p in registry.providers()])
7575
self.assertEqual(registry.providers(), initial_providers)
76-
76+
7777

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

0 commit comments

Comments
 (0)
Please sign in to comment.