Skip to content

Commit 95387cf

Browse files
committedJun 1, 2020
Add map support to QgsProcessingUtils::variantToPythonLiteral
1 parent a8de679 commit 95387cf

File tree

7 files changed

+346
-0
lines changed

7 files changed

+346
-0
lines changed
 
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/processing/qgsprocessingparameterfieldmap.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsProcessingParameterFieldMapping : QgsProcessingParameterDefinition
13+
{
14+
%Docstring
15+
16+
.. note::
17+
18+
This class is not a part of public API.
19+
20+
.. versionadded:: 3.14
21+
%End
22+
23+
%TypeHeaderCode
24+
#include "qgsprocessingparameterfieldmap.h"
25+
%End
26+
public:
27+
QgsProcessingParameterFieldMapping( const QString &name, const QString &description = QString(), const QString &parentLayerParameterName = QString() );
28+
%Docstring
29+
Constructor for QgsProcessingParameterFieldMapping.
30+
%End
31+
32+
virtual QgsProcessingParameterDefinition *clone() const;
33+
34+
virtual QString type() const;
35+
36+
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context ) const;
37+
38+
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
39+
40+
virtual QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const;
41+
42+
virtual QVariantMap toVariantMap() const;
43+
44+
virtual bool fromVariantMap( const QVariantMap &map );
45+
46+
47+
static QString typeName();
48+
%Docstring
49+
Returns the type name for the parameter class.
50+
%End
51+
52+
QString parentLayerParameterName() const;
53+
%Docstring
54+
Returns the name of the parent layer parameter, or an empty string if this is not set.
55+
56+
.. seealso:: :py:func:`setParentLayerParameterName`
57+
%End
58+
59+
void setParentLayerParameterName( const QString &name );
60+
%Docstring
61+
Sets the ``name`` of the parent layer parameter. Use an empty string if this is not required.
62+
63+
.. seealso:: :py:func:`parentLayerParameterName`
64+
%End
65+
66+
};
67+
68+
69+
class QgsProcessingParameterTypeFieldMapping : QgsProcessingParameterType
70+
{
71+
%Docstring
72+
Parameter type definition for QgsProcessingParameterFieldMapping.
73+
74+
.. note::
75+
76+
This class is not a part of public API.
77+
78+
.. versionadded:: 3.14
79+
%End
80+
81+
%TypeHeaderCode
82+
#include "qgsprocessingparameterfieldmap.h"
83+
%End
84+
public:
85+
virtual QgsProcessingParameterDefinition *create( const QString &name ) const /Factory/;
86+
87+
virtual QString description() const;
88+
89+
virtual QString name() const;
90+
91+
virtual QString id() const;
92+
93+
virtual QString pythonImportString() const;
94+
95+
virtual QString className() const;
96+
97+
virtual QStringList acceptedPythonTypes() const;
98+
};
99+
100+
101+
/************************************************************************
102+
* This file has been generated automatically from *
103+
* *
104+
* src/core/processing/qgsprocessingparameterfieldmap.h *
105+
* *
106+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
107+
************************************************************************/

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ SET(QGIS_CORE_SRCS
147147
processing/qgsprocessingcontext.cpp
148148
processing/qgsprocessingfeedback.cpp
149149
processing/qgsprocessingoutputs.cpp
150+
processing/qgsprocessingparameterfieldmap.cpp
150151
processing/qgsprocessingparameters.cpp
151152
processing/qgsprocessingparametertype.cpp
152153
processing/qgsprocessingparametervectortilewriterlayers.cpp
@@ -1269,6 +1270,7 @@ SET(QGIS_CORE_HDRS
12691270
processing/qgsprocessingcontext.h
12701271
processing/qgsprocessingfeedback.h
12711272
processing/qgsprocessingoutputs.h
1273+
processing/qgsprocessingparameterfieldmap.h
12721274
processing/qgsprocessingparameters.h
12731275
processing/qgsprocessingparametertype.h
12741276
processing/qgsprocessingparametertypeimpl.h
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/***************************************************************************
2+
qgsprocessingparameterfieldmap.cpp
3+
-------------------------
4+
begin : June 2020
5+
copyright : (C) 2020 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgsprocessingparameterfieldmap.h"
17+
18+
#include "qgsvectorlayer.h"
19+
20+
21+
QgsProcessingParameterFieldMapping::QgsProcessingParameterFieldMapping( const QString &name, const QString &description, const QString &parentLayerParameterName )
22+
: QgsProcessingParameterDefinition( name, description, QVariant(), false )
23+
, mParentLayerParameterName( parentLayerParameterName )
24+
{
25+
}
26+
27+
QgsProcessingParameterDefinition *QgsProcessingParameterFieldMapping::clone() const
28+
{
29+
return new QgsProcessingParameterFieldMapping( *this );
30+
}
31+
32+
QString QgsProcessingParameterFieldMapping::type() const
33+
{
34+
return typeName();
35+
}
36+
37+
bool QgsProcessingParameterFieldMapping::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context ) const
38+
{
39+
if ( !input.isValid() )
40+
return mFlags & FlagOptional;
41+
42+
if ( input.type() != QVariant::List )
43+
return false;
44+
45+
const QVariantList inputList = input.toList();
46+
for ( const QVariant &inputItem : inputList )
47+
{
48+
if ( inputItem.type() != QVariant::Map )
49+
return false;
50+
51+
const QVariantMap inputItemMap = inputItem.toMap();
52+
53+
if ( !inputItemMap.contains( "name" ) )
54+
return false;
55+
if ( !inputItemMap.contains( "type" ) )
56+
return false;
57+
if ( !inputItemMap.contains( "expression" ) )
58+
return false;
59+
}
60+
61+
return true;
62+
}
63+
64+
QString QgsProcessingParameterFieldMapping::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
65+
{
66+
QStringList parts;
67+
68+
// TODO
69+
70+
return parts.join( ',' ).prepend( '[' ).append( ']' );
71+
}
72+
73+
QString QgsProcessingParameterFieldMapping::asPythonString( QgsProcessing::PythonOutputType outputType ) const
74+
{
75+
switch ( outputType )
76+
{
77+
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
78+
{
79+
QString code = QStringLiteral( "QgsProcessingParameterVectorTileWriterLayers('%1', '%2')" ).arg( name(), description() );
80+
return code;
81+
}
82+
}
83+
return QString();
84+
}
85+
86+
QVariantMap QgsProcessingParameterFieldMapping::toVariantMap() const
87+
{
88+
89+
}
90+
91+
bool QgsProcessingParameterFieldMapping::fromVariantMap( const QVariantMap &map )
92+
{
93+
94+
}
95+
96+
QString QgsProcessingParameterFieldMapping::parentLayerParameterName() const
97+
{
98+
return mParentLayerParameterName;
99+
}
100+
101+
void QgsProcessingParameterFieldMapping::setParentLayerParameterName( const QString &name )
102+
{
103+
mParentLayerParameterName = name;
104+
}
105+
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/***************************************************************************
2+
qgsprocessingparameterfieldmap.h
3+
-------------------------
4+
begin : June 2020
5+
copyright : (C) 2020 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGSPROCESSINGPARAMETERFIELDMAP_H
17+
#define QGSPROCESSINGPARAMETERFIELDMAP_H
18+
19+
#include "qgsprocessingparameters.h"
20+
#include "qgsprocessingparametertype.h"
21+
#include "qgsvectortilewriter.h"
22+
23+
24+
/**
25+
* \ingroup core
26+
* \note This class is not a part of public API.
27+
* \since QGIS 3.14
28+
*/
29+
class CORE_EXPORT QgsProcessingParameterFieldMapping : public QgsProcessingParameterDefinition
30+
{
31+
public:
32+
//! Constructor for QgsProcessingParameterFieldMapping.
33+
QgsProcessingParameterFieldMapping( const QString &name, const QString &description = QString(), const QString &parentLayerParameterName = QString() );
34+
35+
QgsProcessingParameterDefinition *clone() const override;
36+
QString type() const override;
37+
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
38+
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
39+
QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const override;
40+
QVariantMap toVariantMap() const override;
41+
bool fromVariantMap( const QVariantMap &map ) override;
42+
43+
//! Returns the type name for the parameter class.
44+
static QString typeName() { return QStringLiteral( "fields_mapping" ); }
45+
46+
/**
47+
* Returns the name of the parent layer parameter, or an empty string if this is not set.
48+
* \see setParentLayerParameterName()
49+
*/
50+
QString parentLayerParameterName() const;
51+
52+
/**
53+
* Sets the \a name of the parent layer parameter. Use an empty string if this is not required.
54+
* \see parentLayerParameterName()
55+
*/
56+
void setParentLayerParameterName( const QString &name );
57+
58+
private:
59+
QString mParentLayerParameterName;
60+
61+
};
62+
63+
///@cond PRIVATE
64+
65+
/**
66+
* Parameter type definition for QgsProcessingParameterFieldMapping.
67+
*
68+
* \ingroup core
69+
* \note This class is not a part of public API.
70+
* \since QGIS 3.14
71+
*/
72+
class CORE_EXPORT QgsProcessingParameterTypeFieldMapping : public QgsProcessingParameterType
73+
{
74+
public:
75+
QgsProcessingParameterDefinition *create( const QString &name ) const override SIP_FACTORY
76+
{
77+
return new QgsProcessingParameterFieldMapping( name );
78+
}
79+
80+
QString description() const override
81+
{
82+
return QCoreApplication::translate( "Processing", "A mapping of field names to field type definitions and expressions. Used for the refactor fields algorithm." );
83+
}
84+
85+
QString name() const override
86+
{
87+
return QCoreApplication::translate( "Processing", "Fields Mapper" );
88+
}
89+
90+
QString id() const override
91+
{
92+
return QgsProcessingParameterFieldMapping::typeName();
93+
}
94+
95+
QString pythonImportString() const override
96+
{
97+
return QStringLiteral( "from qgis.core import QgsProcessingParameterFieldMapping" );
98+
}
99+
100+
QString className() const override
101+
{
102+
return QStringLiteral( "QgsProcessingParameterFieldMapping" );
103+
}
104+
105+
QStringList acceptedPythonTypes() const override
106+
{
107+
return QStringList() << QObject::tr( "list[dict]: list of field definitions as dictionaries" );
108+
}
109+
};
110+
111+
///@endcond
112+
113+
#endif // QGSPROCESSINGPARAMETERFIELDMAP_H

‎src/core/processing/qgsprocessingregistry.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgsvectorfilewriter.h"
2020
#include "qgsprocessingparametertypeimpl.h"
2121
#include "qgsprocessingparametervectortilewriterlayers.h"
22+
#include "qgsprocessingparameterfieldmap.h"
2223

2324
QgsProcessingRegistry::QgsProcessingRegistry( QObject *parent SIP_TRANSFERTHIS )
2425
: QObject( parent )
@@ -62,6 +63,7 @@ QgsProcessingRegistry::QgsProcessingRegistry( QObject *parent SIP_TRANSFERTHIS )
6263
addParameterType( new QgsProcessingParameterTypeDatabaseSchema() );
6364
addParameterType( new QgsProcessingParameterTypeDatabaseTable() );
6465
addParameterType( new QgsProcessingParameterTypeVectorTileWriterLayers() );
66+
addParameterType( new QgsProcessingParameterTypeFieldMapping() );
6567
}
6668

6769
QgsProcessingRegistry::~QgsProcessingRegistry()

‎src/core/processing/qgsprocessingutils.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,18 @@ QString QgsProcessingUtils::variantToPythonLiteral( const QVariant &value )
587587
return parts.join( ',' ).prepend( '[' ).append( ']' );
588588
}
589589

590+
case QVariant::Map:
591+
{
592+
const QVariantMap map = value.toMap();
593+
QStringList parts;
594+
parts.reserve( map.size() );
595+
for ( auto it = map.constBegin(); it != map.constEnd(); ++it )
596+
{
597+
parts << QStringLiteral( "%1: %2" ).arg( stringToPythonLiteral( it.key() ), variantToPythonLiteral( it.value() ) );
598+
}
599+
return parts.join( ',' ).prepend( '{' ).append( '}' );
600+
}
601+
590602
default:
591603
break;
592604
}

‎tests/src/analysis/testqgsprocessing.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10660,6 +10660,11 @@ void TestQgsProcessing::variantToPythonLiteral()
1066010660
QCOMPARE( QgsProcessingUtils::variantToPythonLiteral( QStringLiteral( "a 'string'" ) ), QStringLiteral( "'a \\'string\\''" ) );
1066110661
QCOMPARE( QgsProcessingUtils::variantToPythonLiteral( QStringLiteral( "a \"string\"" ) ), QStringLiteral( "'a \\\"string\\\"'" ) );
1066210662
QCOMPARE( QgsProcessingUtils::variantToPythonLiteral( QStringLiteral( "a \n str\tin\\g" ) ), QStringLiteral( "'a \\n str\\tin\\\\g'" ) );
10663+
QVariantMap map;
10664+
map.insert( QStringLiteral( "list" ), QVariantList() << 1 << 2 << "a" );
10665+
map.insert( QStringLiteral( "another" ), 4 );
10666+
map.insert( QStringLiteral( "another2" ), QStringLiteral( "test" ) );
10667+
QCOMPARE( QgsProcessingUtils::variantToPythonLiteral( map ), QStringLiteral( "{'another': 4,'another2': 'test','list': [1,2,'a']}" ) );
1066310668
}
1066410669

1066510670
void TestQgsProcessing::stringToPythonLiteral()

0 commit comments

Comments
 (0)
Please sign in to comment.