Skip to content
Permalink
Browse files

Add a processing parameter registry

to manage parameter metadata in a single place.
  • Loading branch information
m-kuhn committed Mar 1, 2018
1 parent 8b78440 commit eba96fb9cc6df4921195005ce005ef1457c84ad3
@@ -45,6 +45,8 @@
QgsProcessingOutputMapLayer,
QgsProcessingOutputMultipleLayers)

from .parameters import initializeParameters

import processing
from processing.core.ProcessingConfig import ProcessingConfig
from processing.gui.MessageBarProgress import MessageBarProgress
@@ -67,6 +69,7 @@

class Processing(object):
BASIC_PROVIDERS = []
REGISTERED_PARAMETERS = dict()

@staticmethod
def activateProvider(providerOrName, activate=True):
@@ -97,6 +100,7 @@ def initialize():
if QgsApplication.processingRegistry().addProvider(p):
Processing.BASIC_PROVIDERS.append(p)
# And initialize
initializeParameters()
ProcessingConfig.initialize()
ProcessingConfig.readSettings()
RenderingStyles.loadStyles()
@@ -107,6 +111,33 @@ def deinitialize():
QgsApplication.processingRegistry().removeProvider(p)

Processing.BASIC_PROVIDERS = []
Processing.REGISTERED_PARAMETERS = dict()

@staticmethod
def registerParameter(id, name, parameter, metadata=dict(), description=None):
"""Register a new parameter.
The ``name`` is a human readable translated string, the ``parameter`` is a class type with the base class ``qgis.core.QgsProcessingParameterDefinition``,
the ``metadata`` is a dictionary with additional metadata, mainly used for widget wrappers.
"""
Processing.REGISTERED_PARAMETERS[id] = {
'name': name,
'parameter': parameter,
'metadata': metadata,
'description': description
}

@staticmethod
def unregisterParameter(name):
"""Unregister a registered parameter with the given name.
"""
del Processing.REGISTERED_PARAMETERS[name]

@staticmethod
def registeredParameters():
"""Returns a set of registered parameters.
Each entry is a tuple consisting of a human readable name and the class.
"""
return Processing.REGISTERED_PARAMETERS

@staticmethod
def runAlgorithm(algOrName, parameters, onFinish=None, feedback=None, context=None):
@@ -41,12 +41,14 @@
QgsProcessingParameterDefinition,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterBand,
QgsProcessingParameterBoolean,
QgsProcessingParameterCrs,
QgsProcessingParameterRange,
QgsProcessingParameterPoint,
QgsProcessingParameterEnum,
QgsProcessingParameterExtent,
QgsProcessingParameterExpression,
QgsProcessingParameterMatrix,
QgsProcessingParameterFile,
QgsProcessingParameterField,
@@ -55,10 +57,36 @@
QgsProcessingParameterFolderDestination,
QgsProcessingParameterRasterDestination,
QgsProcessingParameterString,
QgsProcessingParameterMapLayer,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterNumber)

from PyQt5.QtCore import QCoreApplication

PARAMETER_NUMBER = 'Number'
PARAMETER_RASTER = 'Raster Layer'
PARAMETER_TABLE = 'Vector Layer'
PARAMETER_VECTOR = 'Vector Features'
PARAMETER_STRING = 'String'
PARAMETER_EXPRESSION = 'Expression'
PARAMETER_BOOLEAN = 'Boolean'
PARAMETER_TABLE_FIELD = 'Vector Field'
PARAMETER_EXTENT = 'Extent'
PARAMETER_FILE = 'File'
PARAMETER_POINT = 'Point'
PARAMETER_CRS = 'CRS'
PARAMETER_MULTIPLE = 'Multiple Input'
PARAMETER_BAND = 'Raster Band'
PARAMETER_MAP_LAYER = 'Map Layer'
PARAMETER_RANGE = 'Range'
PARAMETER_ENUM = 'Enum'
PARAMETER_MATRIX = 'Matrix'
PARAMETER_VECTOR_DESTINATION = 'Vector Destination'
PARAMETER_FILE_DESTINATION = 'File Destination'
PARAMETER_FOLDER_DESTINATION = 'Folder Destination'
PARAMETER_RASTER_DESTINATION = 'Raster Destination'


def getParameterFromString(s):
# Try the parameter definitions used in description files
@@ -209,3 +237,53 @@ def getParameterFromString(s):
param = QgsProcessingParameters.parameterFromScriptCode(s)
if param:
return param


def initializeParameters():
from processing.core.Processing import Processing

"""
ModelerParameterDefinitionDialog.PARAMETER_TABLE: QCoreApplication.translate('Processing',
'A vector layer parameter, e.g. for algorithms which change layer styles, edit layers in place, or other operations which affect an entire layer.'),
"""

Processing.registerParameter(PARAMETER_MAP_LAYER, QCoreApplication.translate('Processing', 'Map Layer'),
QgsProcessingParameterMapLayer,
description=QCoreApplication.translate('Processing', 'A generic map layer parameter, which accepts either vector or raster layers.'))
Processing.registerParameter(PARAMETER_BAND, QCoreApplication.translate('Processing', 'Raster Band'),
QgsProcessingParameterBand,
description=QCoreApplication.translate('Processing', 'A raster band parameter, for selecting an existing band from a raster source.'))
Processing.registerParameter(PARAMETER_EXPRESSION, QCoreApplication.translate('Processing', 'Expression'),
QgsProcessingParameterExpression,
description=QCoreApplication.translate('Processing', 'A QGIS expression parameter, which presents an expression builder widget to users.'))
Processing.registerParameter(PARAMETER_RASTER, QCoreApplication.translate('Processing', 'Raster Layer'), QgsProcessingParameterRasterLayer,
description=QCoreApplication.translate('Processing', 'A raster layer parameter.'))
Processing.registerParameter(PARAMETER_TABLE, QCoreApplication.translate('Processing', 'Vector Layer'), QgsProcessingParameterVectorLayer,
description=QCoreApplication.translate('Processing', 'A vector feature parameter, e.g. for algorithms which operate on the features within a layer.'))
Processing.registerParameter(PARAMETER_BOOLEAN, QCoreApplication.translate('Processing', 'Boolean'), QgsProcessingParameterBoolean,
description=QCoreApplication.translate('Processing', 'A boolean parameter, for true/false values.'))
Processing.registerParameter(PARAMETER_CRS, QCoreApplication.translate('Processing', 'CRS'), QgsProcessingParameterCrs,
description=QCoreApplication.translate('Processing', 'A coordinate reference system (CRS) input parameter.'))
Processing.registerParameter(PARAMETER_RANGE, QCoreApplication.translate('Processing', 'Range'), QgsProcessingParameterRange)
Processing.registerParameter(PARAMETER_POINT, QCoreApplication.translate('Processing', 'Point'), QgsProcessingParameterPoint,
description=QCoreApplication.translate('Processing', 'A geographic point parameter.'))
Processing.registerParameter(PARAMETER_ENUM, QCoreApplication.translate('Processing', 'Enum'), QgsProcessingParameterEnum)
Processing.registerParameter(PARAMETER_EXTENT, QCoreApplication.translate('Processing', 'Extent'), QgsProcessingParameterExtent,
description=QCoreApplication.translate('Processing', 'A map extent parameter.'))
Processing.registerParameter(PARAMETER_MATRIX, QCoreApplication.translate('Processing', 'Matrix'), QgsProcessingParameterMatrix)
Processing.registerParameter(PARAMETER_FILE, QCoreApplication.translate('Processing', 'File'), QgsProcessingParameterFile,
description=QCoreApplication.translate('Processing', 'A file parameter, for use with non-map layer file sources.'))
Processing.registerParameter(PARAMETER_TABLE_FIELD, QCoreApplication.translate('Processing', 'Field'), QgsProcessingParameterField,
description=QCoreApplication.translate('Processing', 'A vector field parameter, for selecting an existing field from a vector source.'))
Processing.registerParameter(PARAMETER_VECTOR_DESTINATION, QCoreApplication.translate('Processing', 'Vector Destination'), QgsProcessingParameterVectorDestination)
Processing.registerParameter(PARAMETER_FILE_DESTINATION, QCoreApplication.translate('Processing', 'File Destination'), QgsProcessingParameterFileDestination)
Processing.registerParameter(PARAMETER_FOLDER_DESTINATION, QCoreApplication.translate('Processing', 'Folder Destination'), QgsProcessingParameterFolderDestination)
Processing.registerParameter(PARAMETER_RASTER_DESTINATION, QCoreApplication.translate('Processing', 'Raster Destination'), QgsProcessingParameterRasterDestination)
Processing.registerParameter(PARAMETER_STRING, QCoreApplication.translate('Processing', 'String'), QgsProcessingParameterString,
description=QCoreApplication.translate('Processing', 'A freeform string parameter.'))
Processing.registerParameter(PARAMETER_MULTIPLE, QCoreApplication.translate('Processing', 'Multiple Layers'), QgsProcessingParameterMultipleLayers,
description=QCoreApplication.translate('Processing', 'An input allowing selection of multiple sources, including multiple map layers or file sources.'))
Processing.registerParameter(PARAMETER_VECTOR, QCoreApplication.translate('Processing', 'Feature Source'), QgsProcessingParameterFeatureSource)
Processing.registerParameter(PARAMETER_NUMBER, QCoreApplication.translate('Processing', 'Number'), QgsProcessingParameterNumber,
description=QCoreApplication.translate('Processing', 'A numeric parameter, including float or integer values.'))
Processing.registeredParameters()
@@ -154,12 +154,14 @@ def _dragEnterEvent(event):
event.ignore()

def _dropEvent(event):
from processing.core.Processing import Processing

if event.mimeData().hasText():
text = event.mimeData().text()
if text in ModelerParameterDefinitionDialog.paramTypes:
self.addInputOfType(text, event.pos())
itemId = event.mimeData().text()
if itemId in Processing.registeredParameters():
self.addInputOfType(itemId, event.pos())
else:
alg = QgsApplication.processingRegistry().createAlgorithmById(text)
alg = QgsApplication.processingRegistry().createAlgorithmById(itemId)
if alg is not None:
self._addAlgorithm(alg, event.pos())
event.accept()
@@ -545,25 +547,24 @@ def repaintModel(self, controls=True):

def addInput(self):
item = self.inputsTree.currentItem()
paramType = str(item.text(0))
self.addInputOfType(paramType)
param = item.data(0, Qt.UserRole)
self.addInputOfType(param)

def addInputOfType(self, paramType, pos=None):
if paramType in ModelerParameterDefinitionDialog.paramTypes:
dlg = ModelerParameterDefinitionDialog(self.model, paramType)
dlg.exec_()
if dlg.param is not None:
if pos is None:
pos = self.getPositionForParameterItem()
if isinstance(pos, QPoint):
pos = QPointF(pos)
component = QgsProcessingModelParameter(dlg.param.name())
component.setDescription(dlg.param.name())
component.setPosition(pos)
self.model.addModelParameter(dlg.param, component)
self.repaintModel()
# self.view.ensureVisible(self.scene.getLastParameterItem())
self.hasChanged = True
dlg = ModelerParameterDefinitionDialog(self.model, paramType)
dlg.exec_()
if dlg.param is not None:
if pos is None:
pos = self.getPositionForParameterItem()
if isinstance(pos, QPoint):
pos = QPointF(pos)
component = QgsProcessingModelParameter(dlg.param.name())
component.setDescription(dlg.param.name())
component.setPosition(pos)
self.model.addModelParameter(dlg.param, component)
self.repaintModel()
# self.view.ensureVisible(self.scene.getLastParameterItem())
self.hasChanged = True

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

def fillInputsTree(self):
from processing.core.Processing import Processing

icon = QIcon(os.path.join(pluginPath, 'images', 'input.svg'))
parametersItem = QTreeWidgetItem()
parametersItem.setText(0, self.tr('Parameters'))
for paramType in sorted(ModelerParameterDefinitionDialog.paramTypes):
sortedParams = sorted(Processing.registeredParameters().items())
for param in sortedParams:
paramItem = QTreeWidgetItem()
paramItem.setText(0, paramType)
paramItem.setText(0, param[0])
paramItem.setData(0, Qt.UserRole, param[1]['parameter'])
paramItem.setIcon(0, icon)
paramItem.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled)
paramItem.setToolTip(0, ModelerParameterDefinitionDialog.inputTooltip(paramType))
paramItem.setToolTip(0, param[1]['description'])
parametersItem.addChild(paramItem)
self.inputsTree.addTopLevelItem(parametersItem)
parametersItem.setExpanded(True)

0 comments on commit eba96fb

Please sign in to comment.
You can’t perform that action at this time.