Navigation Menu

Skip to content

Commit

Permalink
Add map support to QgsProcessingUtils::variantToPythonLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 1, 2020
1 parent a8de679 commit 95387cf
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 0 deletions.
@@ -0,0 +1,107 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/processing/qgsprocessingparameterfieldmap.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsProcessingParameterFieldMapping : QgsProcessingParameterDefinition
{
%Docstring

.. note::

This class is not a part of public API.

.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgsprocessingparameterfieldmap.h"
%End
public:
QgsProcessingParameterFieldMapping( const QString &name, const QString &description = QString(), const QString &parentLayerParameterName = QString() );
%Docstring
Constructor for QgsProcessingParameterFieldMapping.
%End

virtual QgsProcessingParameterDefinition *clone() const;

virtual QString type() const;

virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context ) const;

virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;

virtual QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const;

virtual QVariantMap toVariantMap() const;

virtual bool fromVariantMap( const QVariantMap &map );


static QString typeName();
%Docstring
Returns the type name for the parameter class.
%End

QString parentLayerParameterName() const;
%Docstring
Returns the name of the parent layer parameter, or an empty string if this is not set.

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

void setParentLayerParameterName( const QString &name );
%Docstring
Sets the ``name`` of the parent layer parameter. Use an empty string if this is not required.

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

};


class QgsProcessingParameterTypeFieldMapping : QgsProcessingParameterType
{
%Docstring
Parameter type definition for QgsProcessingParameterFieldMapping.

.. note::

This class is not a part of public API.

.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgsprocessingparameterfieldmap.h"
%End
public:
virtual QgsProcessingParameterDefinition *create( const QString &name ) const /Factory/;

virtual QString description() const;

virtual QString name() const;

virtual QString id() const;

virtual QString pythonImportString() const;

virtual QString className() const;

virtual QStringList acceptedPythonTypes() const;
};


/************************************************************************
* This file has been generated automatically from *
* *
* src/core/processing/qgsprocessingparameterfieldmap.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -147,6 +147,7 @@ SET(QGIS_CORE_SRCS
processing/qgsprocessingcontext.cpp
processing/qgsprocessingfeedback.cpp
processing/qgsprocessingoutputs.cpp
processing/qgsprocessingparameterfieldmap.cpp
processing/qgsprocessingparameters.cpp
processing/qgsprocessingparametertype.cpp
processing/qgsprocessingparametervectortilewriterlayers.cpp
Expand Down Expand Up @@ -1269,6 +1270,7 @@ SET(QGIS_CORE_HDRS
processing/qgsprocessingcontext.h
processing/qgsprocessingfeedback.h
processing/qgsprocessingoutputs.h
processing/qgsprocessingparameterfieldmap.h
processing/qgsprocessingparameters.h
processing/qgsprocessingparametertype.h
processing/qgsprocessingparametertypeimpl.h
Expand Down
105 changes: 105 additions & 0 deletions src/core/processing/qgsprocessingparameterfieldmap.cpp
@@ -0,0 +1,105 @@
/***************************************************************************
qgsprocessingparameterfieldmap.cpp
-------------------------
begin : June 2020
copyright : (C) 2020 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************/

#include "qgsprocessingparameterfieldmap.h"

#include "qgsvectorlayer.h"


QgsProcessingParameterFieldMapping::QgsProcessingParameterFieldMapping( const QString &name, const QString &description, const QString &parentLayerParameterName )
: QgsProcessingParameterDefinition( name, description, QVariant(), false )
, mParentLayerParameterName( parentLayerParameterName )
{
}

QgsProcessingParameterDefinition *QgsProcessingParameterFieldMapping::clone() const
{
return new QgsProcessingParameterFieldMapping( *this );
}

QString QgsProcessingParameterFieldMapping::type() const
{
return typeName();
}

bool QgsProcessingParameterFieldMapping::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context ) const
{
if ( !input.isValid() )
return mFlags & FlagOptional;

if ( input.type() != QVariant::List )
return false;

const QVariantList inputList = input.toList();
for ( const QVariant &inputItem : inputList )
{
if ( inputItem.type() != QVariant::Map )
return false;

const QVariantMap inputItemMap = inputItem.toMap();

if ( !inputItemMap.contains( "name" ) )
return false;
if ( !inputItemMap.contains( "type" ) )
return false;
if ( !inputItemMap.contains( "expression" ) )
return false;
}

return true;
}

QString QgsProcessingParameterFieldMapping::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
{
QStringList parts;

// TODO

return parts.join( ',' ).prepend( '[' ).append( ']' );
}

QString QgsProcessingParameterFieldMapping::asPythonString( QgsProcessing::PythonOutputType outputType ) const
{
switch ( outputType )
{
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
{
QString code = QStringLiteral( "QgsProcessingParameterVectorTileWriterLayers('%1', '%2')" ).arg( name(), description() );
return code;
}
}
return QString();
}

QVariantMap QgsProcessingParameterFieldMapping::toVariantMap() const
{

}

bool QgsProcessingParameterFieldMapping::fromVariantMap( const QVariantMap &map )
{

}

QString QgsProcessingParameterFieldMapping::parentLayerParameterName() const
{
return mParentLayerParameterName;
}

void QgsProcessingParameterFieldMapping::setParentLayerParameterName( const QString &name )
{
mParentLayerParameterName = name;
}

113 changes: 113 additions & 0 deletions src/core/processing/qgsprocessingparameterfieldmap.h
@@ -0,0 +1,113 @@
/***************************************************************************
qgsprocessingparameterfieldmap.h
-------------------------
begin : June 2020
copyright : (C) 2020 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************/

#ifndef QGSPROCESSINGPARAMETERFIELDMAP_H
#define QGSPROCESSINGPARAMETERFIELDMAP_H

#include "qgsprocessingparameters.h"
#include "qgsprocessingparametertype.h"
#include "qgsvectortilewriter.h"


/**
* \ingroup core
* \note This class is not a part of public API.
* \since QGIS 3.14
*/
class CORE_EXPORT QgsProcessingParameterFieldMapping : public QgsProcessingParameterDefinition
{
public:
//! Constructor for QgsProcessingParameterFieldMapping.
QgsProcessingParameterFieldMapping( const QString &name, const QString &description = QString(), const QString &parentLayerParameterName = QString() );

QgsProcessingParameterDefinition *clone() const override;
QString type() const override;
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const override;
QVariantMap toVariantMap() const override;
bool fromVariantMap( const QVariantMap &map ) override;

//! Returns the type name for the parameter class.
static QString typeName() { return QStringLiteral( "fields_mapping" ); }

/**
* Returns the name of the parent layer parameter, or an empty string if this is not set.
* \see setParentLayerParameterName()
*/
QString parentLayerParameterName() const;

/**
* Sets the \a name of the parent layer parameter. Use an empty string if this is not required.
* \see parentLayerParameterName()
*/
void setParentLayerParameterName( const QString &name );

private:
QString mParentLayerParameterName;

};

///@cond PRIVATE

/**
* Parameter type definition for QgsProcessingParameterFieldMapping.
*
* \ingroup core
* \note This class is not a part of public API.
* \since QGIS 3.14
*/
class CORE_EXPORT QgsProcessingParameterTypeFieldMapping : public QgsProcessingParameterType
{
public:
QgsProcessingParameterDefinition *create( const QString &name ) const override SIP_FACTORY
{
return new QgsProcessingParameterFieldMapping( name );
}

QString description() const override
{
return QCoreApplication::translate( "Processing", "A mapping of field names to field type definitions and expressions. Used for the refactor fields algorithm." );
}

QString name() const override
{
return QCoreApplication::translate( "Processing", "Fields Mapper" );
}

QString id() const override
{
return QgsProcessingParameterFieldMapping::typeName();
}

QString pythonImportString() const override
{
return QStringLiteral( "from qgis.core import QgsProcessingParameterFieldMapping" );
}

QString className() const override
{
return QStringLiteral( "QgsProcessingParameterFieldMapping" );
}

QStringList acceptedPythonTypes() const override
{
return QStringList() << QObject::tr( "list[dict]: list of field definitions as dictionaries" );
}
};

///@endcond

#endif // QGSPROCESSINGPARAMETERFIELDMAP_H
2 changes: 2 additions & 0 deletions src/core/processing/qgsprocessingregistry.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgsvectorfilewriter.h"
#include "qgsprocessingparametertypeimpl.h"
#include "qgsprocessingparametervectortilewriterlayers.h"
#include "qgsprocessingparameterfieldmap.h"

QgsProcessingRegistry::QgsProcessingRegistry( QObject *parent SIP_TRANSFERTHIS )
: QObject( parent )
Expand Down Expand Up @@ -62,6 +63,7 @@ QgsProcessingRegistry::QgsProcessingRegistry( QObject *parent SIP_TRANSFERTHIS )
addParameterType( new QgsProcessingParameterTypeDatabaseSchema() );
addParameterType( new QgsProcessingParameterTypeDatabaseTable() );
addParameterType( new QgsProcessingParameterTypeVectorTileWriterLayers() );
addParameterType( new QgsProcessingParameterTypeFieldMapping() );
}

QgsProcessingRegistry::~QgsProcessingRegistry()
Expand Down
12 changes: 12 additions & 0 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -587,6 +587,18 @@ QString QgsProcessingUtils::variantToPythonLiteral( const QVariant &value )
return parts.join( ',' ).prepend( '[' ).append( ']' );
}

case QVariant::Map:
{
const QVariantMap map = value.toMap();
QStringList parts;
parts.reserve( map.size() );
for ( auto it = map.constBegin(); it != map.constEnd(); ++it )
{
parts << QStringLiteral( "%1: %2" ).arg( stringToPythonLiteral( it.key() ), variantToPythonLiteral( it.value() ) );
}
return parts.join( ',' ).prepend( '{' ).append( '}' );
}

default:
break;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -10660,6 +10660,11 @@ void TestQgsProcessing::variantToPythonLiteral()
QCOMPARE( QgsProcessingUtils::variantToPythonLiteral( QStringLiteral( "a 'string'" ) ), QStringLiteral( "'a \\'string\\''" ) );
QCOMPARE( QgsProcessingUtils::variantToPythonLiteral( QStringLiteral( "a \"string\"" ) ), QStringLiteral( "'a \\\"string\\\"'" ) );
QCOMPARE( QgsProcessingUtils::variantToPythonLiteral( QStringLiteral( "a \n str\tin\\g" ) ), QStringLiteral( "'a \\n str\\tin\\\\g'" ) );
QVariantMap map;
map.insert( QStringLiteral( "list" ), QVariantList() << 1 << 2 << "a" );
map.insert( QStringLiteral( "another" ), 4 );
map.insert( QStringLiteral( "another2" ), QStringLiteral( "test" ) );
QCOMPARE( QgsProcessingUtils::variantToPythonLiteral( map ), QStringLiteral( "{'another': 4,'another2': 'test','list': [1,2,'a']}" ) );
}

void TestQgsProcessing::stringToPythonLiteral()
Expand Down

0 comments on commit 95387cf

Please sign in to comment.