Skip to content

Commit

Permalink
Register meta type for geometry processing parameter
Browse files Browse the repository at this point in the history
    Fix QgsProcessingParameterGeometry argument list
  • Loading branch information
dmarteau authored and nyalldawson committed Sep 14, 2020
1 parent aa0a8ab commit 6262965
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 14 deletions.
Expand Up @@ -1574,7 +1574,7 @@ A geometry parameter for processing algorithms.
%End
public:

QgsProcessingParameterGeometry( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(), bool optional = false, const QList<QgsWkbTypes::GeometryType> &geometryTypes = QList<QgsWkbTypes::GeometryType>() );
QgsProcessingParameterGeometry( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(), bool optional = false, const QList< int > &geometryTypes = QList< int >() );
%Docstring
Constructor for QgsProcessingParameterGeometry.

Expand Down Expand Up @@ -1602,14 +1602,14 @@ Returns the type name for the parameter class.
virtual bool fromVariantMap( const QVariantMap &map );


QList<QgsWkbTypes::GeometryType> geometryTypes() const;
QList<int> geometryTypes() const;
%Docstring
Returns the parameter allowed geometries.

.. seealso:: :py:func:`setGeometryTypes`
%End

void setGeometryTypes( QList<QgsWkbTypes::GeometryType> geometryTypes );
void setGeometryTypes( const QList<int> &geometryTypes );
%Docstring
Sets the allowed ``geometryTypes``.

Expand Down
14 changes: 7 additions & 7 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -2951,7 +2951,7 @@ QgsProcessingParameterPoint *QgsProcessingParameterPoint::fromScriptCode( const
}

QgsProcessingParameterGeometry::QgsProcessingParameterGeometry( const QString &name, const QString &description,
const QVariant &defaultValue, bool optional, const QList<QgsWkbTypes::GeometryType> &geometryTypes )
const QVariant &defaultValue, bool optional, const QList<int> &geometryTypes )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional ),
mGeomTypes( geometryTypes )
{
Expand Down Expand Up @@ -3099,9 +3099,9 @@ QString QgsProcessingParameterGeometry::asScriptCode() const
code += QStringLiteral( "optional " );
code += type() + ' ';

for ( QgsWkbTypes::GeometryType type : mGeomTypes )
for ( int type : mGeomTypes )
{
switch ( type )
switch ( static_cast<QgsWkbTypes::GeometryType>( type ) )
{
case QgsWkbTypes::PointGeometry:
code += QStringLiteral( "point " );
Expand Down Expand Up @@ -3165,9 +3165,9 @@ QString QgsProcessingParameterGeometry::asPythonString( const QgsProcessing::Pyt

QStringList options;
options.reserve( mGeomTypes.size() );
for ( QgsWkbTypes::GeometryType type : mGeomTypes )
for ( int type : mGeomTypes )
{
options << QStringLiteral( " QgsWkbTypes.%1" ).arg( geomTypeToString( type ) );
options << QStringLiteral( " QgsWkbTypes.%1" ).arg( geomTypeToString( static_cast<QgsWkbTypes::GeometryType>( type ) ) );
}
code += QStringLiteral( ", geometryTypes=[%1 ]" ).arg( options.join( ',' ) );
}
Expand All @@ -3184,7 +3184,7 @@ QVariantMap QgsProcessingParameterGeometry::toVariantMap() const
{
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
QVariantList types;
for ( QgsWkbTypes::GeometryType type : mGeomTypes )
for ( int type : mGeomTypes )
{
types << type;
}
Expand All @@ -3199,7 +3199,7 @@ bool QgsProcessingParameterGeometry::fromVariantMap( const QVariantMap &map )
const QVariantList values = map.value( QStringLiteral( "geometrytypes" ) ).toList();
for ( const QVariant &val : values )
{
mGeomTypes << static_cast<QgsWkbTypes::GeometryType>( val.toInt() );
mGeomTypes << val.toInt();
}
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -1616,7 +1616,7 @@ class CORE_EXPORT QgsProcessingParameterGeometry : public QgsProcessingParameter
* The \a geometryTypes argument allows for specifying a list of geometry types acceptable for this
* parameter. Passing a empty list will allow for any type of geometry.
*/
QgsProcessingParameterGeometry( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(), bool optional = false, const QList<QgsWkbTypes::GeometryType> &geometryTypes = QList<QgsWkbTypes::GeometryType>() );
QgsProcessingParameterGeometry( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(), bool optional = false, const QList< int > &geometryTypes = QList< int >() );

/**
* Returns the type name for the parameter class.
Expand All @@ -1635,13 +1635,13 @@ class CORE_EXPORT QgsProcessingParameterGeometry : public QgsProcessingParameter
* Returns the parameter allowed geometries.
* \see setGeometryTypes()
*/
QList<QgsWkbTypes::GeometryType> geometryTypes() const { return mGeomTypes; }
QList<int> geometryTypes() const { return mGeomTypes; }

/**
* Sets the allowed \a geometryTypes.
* \see geometryTypes()
*/
void setGeometryTypes( QList<QgsWkbTypes::GeometryType> geometryTypes ) { mGeomTypes = geometryTypes; }
void setGeometryTypes( const QList<int> &geometryTypes ) { mGeomTypes = geometryTypes; }

/**
* Creates a new parameter using the definition from a script code.
Expand All @@ -1650,7 +1650,7 @@ class CORE_EXPORT QgsProcessingParameterGeometry : public QgsProcessingParameter

private:

QList<QgsWkbTypes::GeometryType> mGeomTypes;
QList<int> mGeomTypes;

};

Expand Down
1 change: 1 addition & 0 deletions src/core/qgsapplication.cpp
Expand Up @@ -224,6 +224,7 @@ void QgsApplication::init( QString profileFolder )
qRegisterMetaType<Qgis::MessageLevel>( "Qgis::MessageLevel" );
qRegisterMetaType<QgsReferencedRectangle>( "QgsReferencedRectangle" );
qRegisterMetaType<QgsReferencedPointXY>( "QgsReferencedPointXY" );
qRegisterMetaType<QgsReferencedGeometry>( "QgsReferencedGeometry" );
qRegisterMetaType<QgsLayoutRenderContext::Flags>( "QgsLayoutRenderContext::Flags" );
qRegisterMetaType<QgsStyle::StyleEntity>( "QgsStyle::StyleEntity" );
qRegisterMetaType<QgsCoordinateReferenceSystem>( "QgsCoordinateReferenceSystem" );
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -207,6 +207,7 @@ ADD_PYTHON_TEST(PyQgsProcessingRecentAlgorithmLog test_qgsprocessingrecentalgori
ADD_PYTHON_TEST(PyQgsProcessingInPlace test_qgsprocessinginplace.py)
ADD_PYTHON_TEST(PyQgsProcessingAlgRunner test_qgsprocessingalgrunner.py)
ADD_PYTHON_TEST(PyQgsProcessingAlgDecorator test_processing_alg_decorator.py)
ADD_PYTHON_TEST(PyQgsProcessingParameters test_qgsprocessingparameters.py)
ADD_PYTHON_TEST(PyQgsImportIntoPostGIS test_processing_importintopostgis.py)
ADD_PYTHON_TEST(PyQgsProjectionSelectionWidgets test_qgsprojectionselectionwidgets.py)
ADD_PYTHON_TEST(PyQgsProjectMetadata test_qgsprojectmetadata.py)
Expand Down
63 changes: 63 additions & 0 deletions tests/src/python/test_qgsprocessingparameters.py
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for Processing algorithm runner(s).
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'David Marteau'
__date__ = '2020-09'
__copyright__ = 'Copyright 2020, The QGIS Project'

import re
from qgis.PyQt.QtCore import QCoreApplication
from qgis.testing import start_app, unittest
from qgis.core import QgsProcessingAlgRunnerTask

from processing.core.Processing import Processing
from processing.core.ProcessingConfig import ProcessingConfig
from qgis.testing import start_app, unittest
from qgis.core import (
QgsApplication,
QgsSettings,
QgsProcessingContext,
QgsProcessingAlgRunnerTask,
QgsProcessingAlgorithm,
QgsProject,
QgsProcessingFeedback,
QgsProcessingParameterGeometry,
QgsWkbTypes,
)

start_app()



class TestQgsProcessingParameters(unittest.TestCase):

@classmethod
def setUpClass(cls):
"""Run before all tests"""
QCoreApplication.setOrganizationName("QGIS_Test")
QCoreApplication.setOrganizationDomain(
"QGIS_TestPyQgsProcessingParameters.com")
QCoreApplication.setApplicationName("QGIS_TestPyQgsProcessingParameters")
QgsSettings().clear()
Processing.initialize()
cls.registry = QgsApplication.instance().processingRegistry()

def test_qgsprocessinggometry(self): # spellok
""" Test QgsProcessingParameterGeometry initialisation
"""
geomtypes = [QgsWkbTypes.PointGeometry, QgsWkbTypes.PolygonGeometry]
param = QgsProcessingParameterGeometry( name='test', geometryTypes=geomtypes)

types = param.geometryTypes()

self.assertEqual( param.geometryTypes(), geomtypes)



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

0 comments on commit 6262965

Please sign in to comment.