Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] Add API to get an import statement and classname for a p…
…arameter type
  • Loading branch information
nyalldawson committed Jan 25, 2019
1 parent 60f252c commit 3cb6a40
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 0 deletions.
Expand Up @@ -51,6 +51,27 @@ A human readable and translatable short name for this parameter type.
This will be used in comboboxes and list widgets.
%End


virtual QString pythonImportString() const;
%Docstring
Returns a valid Python import string for importing the corresponding parameter type,
e.g. "from qgis.core import :py:class:`QgsProcessingParameterBoolean`".

.. seealso:: :py:func:`className`

.. versionadded:: 3.6
%End


virtual QString className() const;
%Docstring
Returns the corresponding class name for the parameter type.

.. seealso:: :py:func:`pythonImportString`

.. versionadded:: 3.6
%End

virtual QString id() const = 0;
%Docstring
A static id for this type which will be used for storing this parameter type.
Expand Down
6 changes: 6 additions & 0 deletions python/plugins/processing/algs/qgis/FieldsMapper.py
Expand Up @@ -167,6 +167,12 @@ def name(self):
def id(self):
return 'fields_mapping'

def pythonImportString(self):
return 'from processing.algs.qgis.FieldsMapper import FieldsMapper'

def className(self):
return 'FieldsMapper.ParameterFieldsMapping'

def description(self):
return QCoreApplication.translate('Processing', 'A mapping of field names to field type definitions and expressions. Used for the refactor fields algorithm.')

Expand Down
8 changes: 8 additions & 0 deletions python/plugins/processing/tests/QgisAlgorithmsTest.py
Expand Up @@ -100,6 +100,14 @@ def testProcessingException(self):
results, ok = alg.run({}, context, feedback)
self.assertFalse(ok)

def testParameterPythonImport(self):
for t in QgsApplication.processingRegistry().parameterTypes():
import_string = t.pythonImportString()
# check that pythonImportString correctly imports
exec(import_string)
# and now we should be able to instantiate an object!
exec('test = {}(\'id\',\'name\')\nself.assertIsNotNone(test)'.format(t.className()))


if __name__ == '__main__':
nose2.main()
24 changes: 24 additions & 0 deletions src/core/processing/qgsprocessingparametertype.h
Expand Up @@ -68,6 +68,30 @@ class CORE_EXPORT QgsProcessingParameterType
*/
virtual QString name() const = 0;

// TODO QGIS 4.0 -- make pure virtual

/**
* Returns a valid Python import string for importing the corresponding parameter type,
* e.g. "from qgis.core import QgsProcessingParameterBoolean".
*
* \see className()
* \since QGIS 3.6
*/
virtual QString pythonImportString() const { return QString(); }

// TODO QGIS 4.0 -- make pure virtual

/**
* Returns the corresponding class name for the parameter type.
*
* \see pythonImportString()
* \since QGIS 3.6
*/
virtual QString className() const
{
return name(); // this is wrong, but it's better than nothing for subclasses which don't implement this method
}

/**
* A static id for this type which will be used for storing this parameter type.
*/
Expand Down

0 comments on commit 3cb6a40

Please sign in to comment.