Skip to content

Commit 2adccb9

Browse files
authored
Merge pull request #4328 from nyalldawson/proc_c7
Porting processing to c++, part 2 (followup to #4311)
2 parents 9c30a0e + 837ee29 commit 2adccb9

File tree

73 files changed

+1057
-737
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1057
-737
lines changed

doc/api_break.dox

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,13 +2163,14 @@ Processing {#qgis_api_break_3_0_Processing}
21632163
----------
21642164

21652165
- Algorithm providers now subclass the c++ QgsProcessingProvider class, and must be adapted to the API for QgsProcessingProvider. Specifically,
2166-
getName() should be replaced with id(), getDescription() with name(), and getIcon with icon().
2166+
getName() should be replaced with id(), getDescription() with name(), and getIcon with icon(). AlgorithmProvider was removed.
21672167
- Algorithm's processAlgorithm method now passes a QgsProcessingFeedback object instead of the loosely defined progress parameter. Algorithms will
21682168
need to update their use of the progress argument to utilize the QgsProcessingFeedback API.
21692169
- Similarly, Python processing scripts no longer have access to a progress variable for reporting their progress. Instead they have a feedback
21702170
object of type QgsProcessingFeedback, and will need to adapt their use of progress reporting to the QgsProcessingFeedback API.
21712171
- SilentProgress was removed. Use the base QgsProcessingFeedback class instead.
2172-
2172+
- algList was removed. Use QgsApplication.processingRegistry() instead.
2173+
- Processing.algs was removed. QgsApplication.processingRegistry().algorithms() instead.
21732174

21742175
Triangulation {#qgis_api_break_3_0_Triangulation}
21752176
-------------

python/core/processing/qgsprocessingalgorithm.sip

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111

12+
1213
class QgsProcessingAlgorithm
1314
{
1415
%Docstring
@@ -37,6 +38,8 @@ class QgsProcessingAlgorithm
3738

3839
virtual ~QgsProcessingAlgorithm();
3940

41+
// QgsProcessingAlgorithm &operator=( const QgsProcessingAlgorithm &other ) = delete;
42+
4043
virtual QString name() const = 0;
4144
%Docstring
4245
Returns the algorithm name, used for identifying the algorithm. This string
@@ -48,6 +51,14 @@ class QgsProcessingAlgorithm
4851
\see tags()
4952
%End
5053

54+
QString id() const;
55+
%Docstring
56+
Returns the unique ID for the algorithm, which is a combination of the algorithm
57+
provider's ID and the algorithms unique name (e.g. "qgis:mergelayers" ).
58+
\see name()
59+
\see provider()
60+
%End
61+
5162
virtual QString displayName() const = 0;
5263
%Docstring
5364
Returns the translated algorithm name, which should be used for any user-visible display
@@ -85,6 +96,13 @@ class QgsProcessingAlgorithm
8596
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
8697
%End
8798

99+
QgsProcessingProvider *provider() const;
100+
%Docstring
101+
Returns the provider to which this algorithm belongs.
102+
%End
103+
104+
private:
105+
QgsProcessingAlgorithm( const QgsProcessingAlgorithm &other );
88106
};
89107
QFlags<QgsProcessingAlgorithm::Flag> operator|(QgsProcessingAlgorithm::Flag f1, QFlags<QgsProcessingAlgorithm::Flag> f2);
90108

python/core/processing/qgsprocessingprovider.sip

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111

12-
class QgsProcessingProvider
12+
class QgsProcessingProvider : QObject
1313
{
1414
%Docstring
1515
Abstract base class for processing providers. An algorithm provider is a set of
@@ -24,7 +24,7 @@ class QgsProcessingProvider
2424

2525
public:
2626

27-
QgsProcessingProvider();
27+
QgsProcessingProvider( QObject *parent /TransferThis/ = 0 );
2828
%Docstring
2929
Constructor for QgsProcessingProvider.
3030
%End
@@ -64,6 +64,12 @@ class QgsProcessingProvider
6464
%Docstring
6565
Returns true if the provider can be activated, or false if it cannot be activated (e.g. due to
6666
missing external dependencies).
67+
\see isActive()
68+
%End
69+
70+
virtual bool isActive() const;
71+
%Docstring
72+
Returns true if the provider is active and able to run algorithms.
6773
%End
6874

6975
virtual QStringList supportedOutputRasterLayerExtensions() const;
@@ -95,6 +101,63 @@ class QgsProcessingProvider
95101
\see supportedOutputVectorLayerExtensions()
96102
%End
97103

104+
virtual bool load();
105+
%Docstring
106+
Loads the provider. This will be called when the plugin is being loaded, and any general
107+
setup actions should occur in an overridden version of this method.
108+
Subclasses should not individually load any algorithms in their load() implementations, as that must
109+
occur within the loadAlgorithms() method. Instead, subclasses should call refreshAlgorithms()
110+
from any overloaded load() method to trigger an initial load of the provider's algorithms.
111+
\returns true if provider could be successfully loaded
112+
\see unload()
113+
%End
114+
115+
virtual void unload();
116+
%Docstring
117+
Unloads the provider. Any tear-down steps required by the provider should be implemented here.
118+
\see load()
119+
%End
120+
121+
void refreshAlgorithms();
122+
%Docstring
123+
Refreshes the algorithms available from the provider, causing it to re-populate with all associated algorithms.
124+
%End
125+
126+
QList< const QgsProcessingAlgorithm * > algorithms() const;
127+
%Docstring
128+
Returns a list of algorithms supplied by this provider.
129+
\see algorithm()
130+
%End
131+
132+
const QgsProcessingAlgorithm *algorithm( const QString &name ) const;
133+
%Docstring
134+
Returns the matching algorithm by name, or a None if no matching
135+
algorithm is contained by this provider.
136+
\see algorithms()
137+
%End
138+
139+
signals:
140+
141+
void algorithmsLoaded();
142+
%Docstring
143+
Emitted when the provider has loaded (or refreshed) its list of available
144+
algorithms.
145+
\see refreshAlgorithms()
146+
%End
147+
148+
protected:
149+
150+
virtual void loadAlgorithms() = 0;
151+
%Docstring
152+
Loads all algorithms belonging to this provider. Subclasses should implement this, calling
153+
addAlgorithm() to register all their associated algorithms.
154+
%End
155+
156+
bool addAlgorithm( QgsProcessingAlgorithm *algorithm /Transfer/ );
157+
%Docstring
158+
Adds an algorithm to the provider. Ownership of the algorithm is transferred to the provider.
159+
%End
160+
98161
private:
99162
QgsProcessingProvider( const QgsProcessingProvider &other );
100163
};

python/core/processing/qgsprocessingregistry.sip

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ class QgsProcessingRegistry : QObject
4242

4343
bool addProvider( QgsProcessingProvider *provider /Transfer/ );
4444
%Docstring
45-
Add a processing provider to the registry. Ownership of the provider is transferred to the registry.
45+
Add a processing provider to the registry. Ownership of the provider is transferred to the registry,
46+
and the provider's parent will be set to the registry.
4647
Returns false if the provider could not be added (eg if a provider with a duplicate ID already exists
4748
in the registry).
49+
Adding a provider to the registry automatically triggers the providers QgsProcessingProvider.load()
50+
method to populate the provider with algorithms.
4851
\see removeProvider()
4952
%End
5053

@@ -67,6 +70,19 @@ class QgsProcessingRegistry : QObject
6770
Returns a matching provider by provider ID.
6871
%End
6972

73+
QList< const QgsProcessingAlgorithm *> algorithms() const;
74+
%Docstring
75+
Returns a list of all available algorithms from registered providers.
76+
\see algorithmById()
77+
%End
78+
79+
const QgsProcessingAlgorithm *algorithmById( const QString &id ) const;
80+
%Docstring
81+
Finds an algorithm by its ID. If no matching algorithm is found, a None
82+
is returned.
83+
\see algorithms()
84+
%End
85+
7086
signals:
7187

7288
void providerAdded( const QString &id );

python/plugins/processing/ProcessingPlugin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
from processing.modeler.ModelerDialog import ModelerDialog
4747
from processing.tools.system import tempFolder
4848
from processing.gui.menus import removeMenus, initializeMenus, createMenus
49-
from processing.core.alglist import algList
5049
from processing.core.ProcessingResults import resultsList
5150

5251
cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]
@@ -164,6 +163,7 @@ def unload(self):
164163
self.iface.unregisterOptionsWidgetFactory(self.options_factory)
165164

166165
removeMenus()
166+
Processing.deinitialize()
167167

168168
def openCommander(self):
169169
if self.commander is None:
@@ -185,7 +185,8 @@ def openModeler(self):
185185
dlg.show()
186186

187187
def updateModel(self):
188-
algList.reloadProvider('model')
188+
model_provider = QgsApplication.processingRegistry().providerById('model')
189+
model_provider.refreshAlgorithms()
189190

190191
def openResults(self):
191192
if self.resultsDock.isVisible():

python/plugins/processing/algs/exampleprovider/ExampleAlgorithmProvider.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,66 +25,67 @@
2525

2626
__revision__ = '$Format:%H$'
2727

28-
from processing.core.AlgorithmProvider import AlgorithmProvider
28+
from qgis.core import QgsProcessingProvider
2929
from processing.core.ProcessingConfig import Setting, ProcessingConfig
3030
from exampleprovider.ExampleAlgorithm import ExampleAlgorithm
3131

3232

33-
class ExampleAlgorithmProvider(AlgorithmProvider):
33+
class ExampleAlgorithmProvider(QgsProcessingProvider):
3434

3535
MY_DUMMY_SETTING = 'MY_DUMMY_SETTING'
3636

3737
def __init__(self):
3838
super().__init__()
3939

40-
# Deactivate provider by default
41-
self.activate = False
42-
43-
# Load algorithms
44-
self.alglist = [ExampleAlgorithm()]
45-
for alg in self.alglist:
46-
alg.provider = self
47-
48-
def initializeSettings(self):
40+
def load(self):
4941
"""In this method we add settings needed to configure our
5042
provider.
51-
52-
Do not forget to call the parent method, since it takes care
53-
or automatically adding a setting for activating or
54-
deactivating the algorithms in the provider.
5543
"""
56-
AlgorithmProvider.initializeSettings(self)
44+
ProcessingConfig.settingIcons[self.name()] = self.icon()
45+
# Deactivate provider by default
46+
ProcessingConfig.addSetting(Setting(self.name(), 'ACTIVATE_EXAMPLE',
47+
'Activate', False))
5748
ProcessingConfig.addSetting(Setting('Example algorithms',
5849
ExampleAlgorithmProvider.MY_DUMMY_SETTING,
5950
'Example setting', 'Default value'))
51+
ProcessingConfig.readSettings()
52+
self.refreshAlgorithms()
53+
return True
6054

6155
def unload(self):
6256
"""Setting should be removed here, so they do not appear anymore
6357
when the plugin is unloaded.
6458
"""
65-
AlgorithmProvider.unload(self)
59+
ProcessingConfig.removeSetting('ACTIVATE_EXAMPLE')
6660
ProcessingConfig.removeSetting(
6761
ExampleAlgorithmProvider.MY_DUMMY_SETTING)
6862

63+
def isActive(self):
64+
"""Return True if the provider is activated and ready to run algorithms"""
65+
return ProcessingConfig.getSetting('ACTIVATE_EXAMPLE')
66+
67+
def setActive(self, active):
68+
ProcessingConfig.setSettingValue('ACTIVATE_EXAMPLE', active)
69+
6970
def id(self):
7071
"""This is the name that will appear on the toolbox group.
7172
7273
It is also used to create the command line name of all the
7374
algorithms from this provider.
7475
"""
75-
return 'Example provider'
76+
return 'example'
7677

7778
def name(self):
78-
"""This is the provired full name.
79+
"""This is the localised full name.
7980
"""
8081
return 'Example algorithms'
8182

8283
def icon(self):
8384
"""We return the default icon.
8485
"""
85-
return AlgorithmProvider.icon(self)
86+
return QgsProcessingProvider.icon(self)
8687

87-
def _loadAlgorithms(self):
88+
def loadAlgorithms(self):
8889
"""Here we fill the list of algorithms in self.algs.
8990
9091
This method is called whenever the list of algorithms should
@@ -98,4 +99,5 @@ def _loadAlgorithms(self):
9899
even if the list does not change, since the self.algs list is
99100
cleared before calling this method.
100101
"""
101-
self.algs = self.alglist
102+
for alg in [ExampleAlgorithm()]:
103+
self.addAlgorithm(alg)

python/plugins/processing/algs/exampleprovider/ProcessingExampleProviderPlugin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import sys
3131
import inspect
3232

33+
from qgis.core import QgsApplication
3334
from processing.core.Processing import Processing
3435
from exampleprovider.ExampleAlgorithmProvider import ExampleAlgorithmProvider
3536

@@ -45,7 +46,7 @@ def __init__(self):
4546
self.provider = ExampleAlgorithmProvider()
4647

4748
def initGui(self):
48-
Processing.addProvider(self.provider)
49+
QgsApplication.processingRegistry().addProvider(self.provider)
4950

5051
def unload(self):
51-
Processing.removeProvider(self.provider)
52+
QgsApplication.processingRegistry().removeProvider(self.provider)

0 commit comments

Comments
 (0)