Skip to content

Commit eba96fb

Browse files
committed
Add a processing parameter registry
to manage parameter metadata in a single place.
1 parent 8b78440 commit eba96fb

File tree

4 files changed

+174
-104
lines changed

4 files changed

+174
-104
lines changed

python/plugins/processing/core/Processing.py

+31
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
QgsProcessingOutputMapLayer,
4646
QgsProcessingOutputMultipleLayers)
4747

48+
from .parameters import initializeParameters
49+
4850
import processing
4951
from processing.core.ProcessingConfig import ProcessingConfig
5052
from processing.gui.MessageBarProgress import MessageBarProgress
@@ -67,6 +69,7 @@
6769

6870
class Processing(object):
6971
BASIC_PROVIDERS = []
72+
REGISTERED_PARAMETERS = dict()
7073

7174
@staticmethod
7275
def activateProvider(providerOrName, activate=True):
@@ -97,6 +100,7 @@ def initialize():
97100
if QgsApplication.processingRegistry().addProvider(p):
98101
Processing.BASIC_PROVIDERS.append(p)
99102
# And initialize
103+
initializeParameters()
100104
ProcessingConfig.initialize()
101105
ProcessingConfig.readSettings()
102106
RenderingStyles.loadStyles()
@@ -107,6 +111,33 @@ def deinitialize():
107111
QgsApplication.processingRegistry().removeProvider(p)
108112

109113
Processing.BASIC_PROVIDERS = []
114+
Processing.REGISTERED_PARAMETERS = dict()
115+
116+
@staticmethod
117+
def registerParameter(id, name, parameter, metadata=dict(), description=None):
118+
"""Register a new parameter.
119+
The ``name`` is a human readable translated string, the ``parameter`` is a class type with the base class ``qgis.core.QgsProcessingParameterDefinition``,
120+
the ``metadata`` is a dictionary with additional metadata, mainly used for widget wrappers.
121+
"""
122+
Processing.REGISTERED_PARAMETERS[id] = {
123+
'name': name,
124+
'parameter': parameter,
125+
'metadata': metadata,
126+
'description': description
127+
}
128+
129+
@staticmethod
130+
def unregisterParameter(name):
131+
"""Unregister a registered parameter with the given name.
132+
"""
133+
del Processing.REGISTERED_PARAMETERS[name]
134+
135+
@staticmethod
136+
def registeredParameters():
137+
"""Returns a set of registered parameters.
138+
Each entry is a tuple consisting of a human readable name and the class.
139+
"""
140+
return Processing.REGISTERED_PARAMETERS
110141

111142
@staticmethod
112143
def runAlgorithm(algOrName, parameters, onFinish=None, feedback=None, context=None):

python/plugins/processing/core/parameters.py

+78
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@
4141
QgsProcessingParameterDefinition,
4242
QgsProcessingParameterRasterLayer,
4343
QgsProcessingParameterVectorLayer,
44+
QgsProcessingParameterBand,
4445
QgsProcessingParameterBoolean,
4546
QgsProcessingParameterCrs,
4647
QgsProcessingParameterRange,
4748
QgsProcessingParameterPoint,
4849
QgsProcessingParameterEnum,
4950
QgsProcessingParameterExtent,
51+
QgsProcessingParameterExpression,
5052
QgsProcessingParameterMatrix,
5153
QgsProcessingParameterFile,
5254
QgsProcessingParameterField,
@@ -55,10 +57,36 @@
5557
QgsProcessingParameterFolderDestination,
5658
QgsProcessingParameterRasterDestination,
5759
QgsProcessingParameterString,
60+
QgsProcessingParameterMapLayer,
5861
QgsProcessingParameterMultipleLayers,
5962
QgsProcessingParameterFeatureSource,
6063
QgsProcessingParameterNumber)
6164

65+
from PyQt5.QtCore import QCoreApplication
66+
67+
PARAMETER_NUMBER = 'Number'
68+
PARAMETER_RASTER = 'Raster Layer'
69+
PARAMETER_TABLE = 'Vector Layer'
70+
PARAMETER_VECTOR = 'Vector Features'
71+
PARAMETER_STRING = 'String'
72+
PARAMETER_EXPRESSION = 'Expression'
73+
PARAMETER_BOOLEAN = 'Boolean'
74+
PARAMETER_TABLE_FIELD = 'Vector Field'
75+
PARAMETER_EXTENT = 'Extent'
76+
PARAMETER_FILE = 'File'
77+
PARAMETER_POINT = 'Point'
78+
PARAMETER_CRS = 'CRS'
79+
PARAMETER_MULTIPLE = 'Multiple Input'
80+
PARAMETER_BAND = 'Raster Band'
81+
PARAMETER_MAP_LAYER = 'Map Layer'
82+
PARAMETER_RANGE = 'Range'
83+
PARAMETER_ENUM = 'Enum'
84+
PARAMETER_MATRIX = 'Matrix'
85+
PARAMETER_VECTOR_DESTINATION = 'Vector Destination'
86+
PARAMETER_FILE_DESTINATION = 'File Destination'
87+
PARAMETER_FOLDER_DESTINATION = 'Folder Destination'
88+
PARAMETER_RASTER_DESTINATION = 'Raster Destination'
89+
6290

6391
def getParameterFromString(s):
6492
# Try the parameter definitions used in description files
@@ -209,3 +237,53 @@ def getParameterFromString(s):
209237
param = QgsProcessingParameters.parameterFromScriptCode(s)
210238
if param:
211239
return param
240+
241+
242+
def initializeParameters():
243+
from processing.core.Processing import Processing
244+
245+
"""
246+
ModelerParameterDefinitionDialog.PARAMETER_TABLE: QCoreApplication.translate('Processing',
247+
'A vector layer parameter, e.g. for algorithms which change layer styles, edit layers in place, or other operations which affect an entire layer.'),
248+
"""
249+
250+
Processing.registerParameter(PARAMETER_MAP_LAYER, QCoreApplication.translate('Processing', 'Map Layer'),
251+
QgsProcessingParameterMapLayer,
252+
description=QCoreApplication.translate('Processing', 'A generic map layer parameter, which accepts either vector or raster layers.'))
253+
Processing.registerParameter(PARAMETER_BAND, QCoreApplication.translate('Processing', 'Raster Band'),
254+
QgsProcessingParameterBand,
255+
description=QCoreApplication.translate('Processing', 'A raster band parameter, for selecting an existing band from a raster source.'))
256+
Processing.registerParameter(PARAMETER_EXPRESSION, QCoreApplication.translate('Processing', 'Expression'),
257+
QgsProcessingParameterExpression,
258+
description=QCoreApplication.translate('Processing', 'A QGIS expression parameter, which presents an expression builder widget to users.'))
259+
Processing.registerParameter(PARAMETER_RASTER, QCoreApplication.translate('Processing', 'Raster Layer'), QgsProcessingParameterRasterLayer,
260+
description=QCoreApplication.translate('Processing', 'A raster layer parameter.'))
261+
Processing.registerParameter(PARAMETER_TABLE, QCoreApplication.translate('Processing', 'Vector Layer'), QgsProcessingParameterVectorLayer,
262+
description=QCoreApplication.translate('Processing', 'A vector feature parameter, e.g. for algorithms which operate on the features within a layer.'))
263+
Processing.registerParameter(PARAMETER_BOOLEAN, QCoreApplication.translate('Processing', 'Boolean'), QgsProcessingParameterBoolean,
264+
description=QCoreApplication.translate('Processing', 'A boolean parameter, for true/false values.'))
265+
Processing.registerParameter(PARAMETER_CRS, QCoreApplication.translate('Processing', 'CRS'), QgsProcessingParameterCrs,
266+
description=QCoreApplication.translate('Processing', 'A coordinate reference system (CRS) input parameter.'))
267+
Processing.registerParameter(PARAMETER_RANGE, QCoreApplication.translate('Processing', 'Range'), QgsProcessingParameterRange)
268+
Processing.registerParameter(PARAMETER_POINT, QCoreApplication.translate('Processing', 'Point'), QgsProcessingParameterPoint,
269+
description=QCoreApplication.translate('Processing', 'A geographic point parameter.'))
270+
Processing.registerParameter(PARAMETER_ENUM, QCoreApplication.translate('Processing', 'Enum'), QgsProcessingParameterEnum)
271+
Processing.registerParameter(PARAMETER_EXTENT, QCoreApplication.translate('Processing', 'Extent'), QgsProcessingParameterExtent,
272+
description=QCoreApplication.translate('Processing', 'A map extent parameter.'))
273+
Processing.registerParameter(PARAMETER_MATRIX, QCoreApplication.translate('Processing', 'Matrix'), QgsProcessingParameterMatrix)
274+
Processing.registerParameter(PARAMETER_FILE, QCoreApplication.translate('Processing', 'File'), QgsProcessingParameterFile,
275+
description=QCoreApplication.translate('Processing', 'A file parameter, for use with non-map layer file sources.'))
276+
Processing.registerParameter(PARAMETER_TABLE_FIELD, QCoreApplication.translate('Processing', 'Field'), QgsProcessingParameterField,
277+
description=QCoreApplication.translate('Processing', 'A vector field parameter, for selecting an existing field from a vector source.'))
278+
Processing.registerParameter(PARAMETER_VECTOR_DESTINATION, QCoreApplication.translate('Processing', 'Vector Destination'), QgsProcessingParameterVectorDestination)
279+
Processing.registerParameter(PARAMETER_FILE_DESTINATION, QCoreApplication.translate('Processing', 'File Destination'), QgsProcessingParameterFileDestination)
280+
Processing.registerParameter(PARAMETER_FOLDER_DESTINATION, QCoreApplication.translate('Processing', 'Folder Destination'), QgsProcessingParameterFolderDestination)
281+
Processing.registerParameter(PARAMETER_RASTER_DESTINATION, QCoreApplication.translate('Processing', 'Raster Destination'), QgsProcessingParameterRasterDestination)
282+
Processing.registerParameter(PARAMETER_STRING, QCoreApplication.translate('Processing', 'String'), QgsProcessingParameterString,
283+
description=QCoreApplication.translate('Processing', 'A freeform string parameter.'))
284+
Processing.registerParameter(PARAMETER_MULTIPLE, QCoreApplication.translate('Processing', 'Multiple Layers'), QgsProcessingParameterMultipleLayers,
285+
description=QCoreApplication.translate('Processing', 'An input allowing selection of multiple sources, including multiple map layers or file sources.'))
286+
Processing.registerParameter(PARAMETER_VECTOR, QCoreApplication.translate('Processing', 'Feature Source'), QgsProcessingParameterFeatureSource)
287+
Processing.registerParameter(PARAMETER_NUMBER, QCoreApplication.translate('Processing', 'Number'), QgsProcessingParameterNumber,
288+
description=QCoreApplication.translate('Processing', 'A numeric parameter, including float or integer values.'))
289+
Processing.registeredParameters()

python/plugins/processing/modeler/ModelerDialog.py

+29-24
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,14 @@ def _dragEnterEvent(event):
154154
event.ignore()
155155

156156
def _dropEvent(event):
157+
from processing.core.Processing import Processing
158+
157159
if event.mimeData().hasText():
158-
text = event.mimeData().text()
159-
if text in ModelerParameterDefinitionDialog.paramTypes:
160-
self.addInputOfType(text, event.pos())
160+
itemId = event.mimeData().text()
161+
if itemId in Processing.registeredParameters():
162+
self.addInputOfType(itemId, event.pos())
161163
else:
162-
alg = QgsApplication.processingRegistry().createAlgorithmById(text)
164+
alg = QgsApplication.processingRegistry().createAlgorithmById(itemId)
163165
if alg is not None:
164166
self._addAlgorithm(alg, event.pos())
165167
event.accept()
@@ -545,25 +547,24 @@ def repaintModel(self, controls=True):
545547

546548
def addInput(self):
547549
item = self.inputsTree.currentItem()
548-
paramType = str(item.text(0))
549-
self.addInputOfType(paramType)
550+
param = item.data(0, Qt.UserRole)
551+
self.addInputOfType(param)
550552

551553
def addInputOfType(self, paramType, pos=None):
552-
if paramType in ModelerParameterDefinitionDialog.paramTypes:
553-
dlg = ModelerParameterDefinitionDialog(self.model, paramType)
554-
dlg.exec_()
555-
if dlg.param is not None:
556-
if pos is None:
557-
pos = self.getPositionForParameterItem()
558-
if isinstance(pos, QPoint):
559-
pos = QPointF(pos)
560-
component = QgsProcessingModelParameter(dlg.param.name())
561-
component.setDescription(dlg.param.name())
562-
component.setPosition(pos)
563-
self.model.addModelParameter(dlg.param, component)
564-
self.repaintModel()
565-
# self.view.ensureVisible(self.scene.getLastParameterItem())
566-
self.hasChanged = True
554+
dlg = ModelerParameterDefinitionDialog(self.model, paramType)
555+
dlg.exec_()
556+
if dlg.param is not None:
557+
if pos is None:
558+
pos = self.getPositionForParameterItem()
559+
if isinstance(pos, QPoint):
560+
pos = QPointF(pos)
561+
component = QgsProcessingModelParameter(dlg.param.name())
562+
component.setDescription(dlg.param.name())
563+
component.setPosition(pos)
564+
self.model.addModelParameter(dlg.param, component)
565+
self.repaintModel()
566+
# self.view.ensureVisible(self.scene.getLastParameterItem())
567+
self.hasChanged = True
567568

568569
def getPositionForParameterItem(self):
569570
MARGIN = 20
@@ -620,15 +621,19 @@ def _filterItem(self, item, text):
620621
return False
621622

622623
def fillInputsTree(self):
624+
from processing.core.Processing import Processing
625+
623626
icon = QIcon(os.path.join(pluginPath, 'images', 'input.svg'))
624627
parametersItem = QTreeWidgetItem()
625628
parametersItem.setText(0, self.tr('Parameters'))
626-
for paramType in sorted(ModelerParameterDefinitionDialog.paramTypes):
629+
sortedParams = sorted(Processing.registeredParameters().items())
630+
for param in sortedParams:
627631
paramItem = QTreeWidgetItem()
628-
paramItem.setText(0, paramType)
632+
paramItem.setText(0, param[0])
633+
paramItem.setData(0, Qt.UserRole, param[1]['parameter'])
629634
paramItem.setIcon(0, icon)
630635
paramItem.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled)
631-
paramItem.setToolTip(0, ModelerParameterDefinitionDialog.inputTooltip(paramType))
636+
paramItem.setToolTip(0, param[1]['description'])
632637
parametersItem.addChild(paramItem)
633638
self.inputsTree.addTopLevelItem(parametersItem)
634639
parametersItem.setExpanded(True)

0 commit comments

Comments
 (0)