Skip to content

Commit 4fdfa94

Browse files
committedJun 3, 2020
[processing] Start porting Aggregates parameters to C++
Port parameter type to c++
1 parent 2f36e18 commit 4fdfa94

File tree

8 files changed

+427
-44
lines changed

8 files changed

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

‎python/core/core_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@
451451
%Include auto_generated/processing/qgsprocessingcontext.sip
452452
%Include auto_generated/processing/qgsprocessingfeedback.sip
453453
%Include auto_generated/processing/qgsprocessingoutputs.sip
454+
%Include auto_generated/processing/qgsprocessingparameteraggregate.sip
454455
%Include auto_generated/processing/qgsprocessingparameterfieldmap.sip
455456
%Include auto_generated/processing/qgsprocessingparameters.sip
456457
%Include auto_generated/processing/qgsprocessingparametertype.sip

‎python/plugins/processing/algs/qgis/Aggregate.py

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
QgsProcessingParameterExpression,
3636
QgsProcessingParameterFeatureSink,
3737
QgsProcessingParameterFeatureSource,
38+
QgsProcessingParameterAggregate,
3839
QgsProcessingException,
3940
QgsProcessingUtils,
4041
QgsWkbTypes,
@@ -72,50 +73,8 @@ def initAlgorithm(self, config=None):
7273
optional=False,
7374
parentLayerParameterName=self.INPUT))
7475

75-
class ParameterAggregates(QgsProcessingParameterDefinition):
76-
77-
def __init__(self, name, description, parentLayerParameterName='INPUT'):
78-
super().__init__(name, description)
79-
self._parentLayerParameter = parentLayerParameterName
80-
81-
def clone(self):
82-
copy = ParameterAggregates(self.name(), self.description(), self._parentLayerParameter)
83-
return copy
84-
85-
def type(self):
86-
return 'aggregates'
87-
88-
def checkValueIsAcceptable(self, value, context=None):
89-
if not isinstance(value, list):
90-
return False
91-
for field_def in value:
92-
if not isinstance(field_def, dict):
93-
return False
94-
if not field_def.get('input', False):
95-
return False
96-
if not field_def.get('aggregate', False):
97-
return False
98-
if not field_def.get('name', False):
99-
return False
100-
if not field_def.get('type', False):
101-
return False
102-
return True
103-
104-
def valueAsPythonString(self, value, context):
105-
return str(value)
106-
107-
def asScriptCode(self):
108-
raise NotImplementedError()
109-
110-
@classmethod
111-
def fromScriptCode(cls, name, description, isOptional, definition):
112-
raise NotImplementedError()
113-
114-
def parentLayerParameter(self):
115-
return self._parentLayerParameter
116-
117-
self.addParameter(ParameterAggregates(self.AGGREGATES,
118-
description=self.tr('Aggregates')))
76+
self.addParameter(QgsProcessingParameterAggregate(self.AGGREGATES,
77+
description=self.tr('Aggregates'), parentLayerParameterName='INPUT'))
11978
self.parameterDefinition(self.AGGREGATES).setMetadata({
12079
'widget_wrapper': 'processing.algs.qgis.ui.AggregatesPanel.AggregatesWidgetWrapper'
12180
})

‎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/qgsprocessingparameteraggregate.cpp
150151
processing/qgsprocessingparameterfieldmap.cpp
151152
processing/qgsprocessingparameters.cpp
152153
processing/qgsprocessingparametertype.cpp
@@ -1270,6 +1271,7 @@ SET(QGIS_CORE_HDRS
12701271
processing/qgsprocessingcontext.h
12711272
processing/qgsprocessingfeedback.h
12721273
processing/qgsprocessingoutputs.h
1274+
processing/qgsprocessingparameteraggregate.h
12731275
processing/qgsprocessingparameterfieldmap.h
12741276
processing/qgsprocessingparameters.h
12751277
processing/qgsprocessingparametertype.h
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/***************************************************************************
2+
qgsprocessingparameteraggregate.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 "qgsprocessingparameteraggregate.h"
17+
18+
#include "qgsvectorlayer.h"
19+
20+
21+
QgsProcessingParameterAggregate::QgsProcessingParameterAggregate( const QString &name, const QString &description, const QString &parentLayerParameterName, bool optional )
22+
: QgsProcessingParameterDefinition( name, description, QVariant(), optional )
23+
, mParentLayerParameterName( parentLayerParameterName )
24+
{
25+
}
26+
27+
QgsProcessingParameterDefinition *QgsProcessingParameterAggregate::clone() const
28+
{
29+
return new QgsProcessingParameterAggregate( *this );
30+
}
31+
32+
QString QgsProcessingParameterAggregate::type() const
33+
{
34+
return typeName();
35+
}
36+
37+
bool QgsProcessingParameterAggregate::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) 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( "input" ) )
58+
return false;
59+
if ( !inputItemMap.contains( "aggregate" ) )
60+
return false;
61+
}
62+
63+
return true;
64+
}
65+
66+
QString QgsProcessingParameterAggregate::valueAsPythonString( const QVariant &value, QgsProcessingContext & ) const
67+
{
68+
return QgsProcessingUtils::variantToPythonLiteral( value );
69+
}
70+
71+
QString QgsProcessingParameterAggregate::asPythonString( QgsProcessing::PythonOutputType outputType ) const
72+
{
73+
switch ( outputType )
74+
{
75+
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
76+
{
77+
QString code = QStringLiteral( "QgsProcessingParameterAggregate('%1', '%2'" ).arg( name(), description() );
78+
if ( !mParentLayerParameterName.isEmpty() )
79+
code += QStringLiteral( ", parentLayerParameterName=%1" ).arg( QgsProcessingUtils::stringToPythonLiteral( mParentLayerParameterName ) );
80+
81+
if ( mFlags & FlagOptional )
82+
code += QStringLiteral( ", optional=True" );
83+
code += ')';
84+
return code;
85+
}
86+
}
87+
return QString();
88+
}
89+
90+
QVariantMap QgsProcessingParameterAggregate::toVariantMap() const
91+
{
92+
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
93+
map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
94+
return map;
95+
}
96+
97+
bool QgsProcessingParameterAggregate::fromVariantMap( const QVariantMap &map )
98+
{
99+
QgsProcessingParameterDefinition::fromVariantMap( map );
100+
mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
101+
return true;
102+
}
103+
104+
QStringList QgsProcessingParameterAggregate::dependsOnOtherParameters() const
105+
{
106+
QStringList depends;
107+
if ( !mParentLayerParameterName.isEmpty() )
108+
depends << mParentLayerParameterName;
109+
return depends;
110+
}
111+
112+
QString QgsProcessingParameterAggregate::parentLayerParameterName() const
113+
{
114+
return mParentLayerParameterName;
115+
}
116+
117+
void QgsProcessingParameterAggregate::setParentLayerParameterName( const QString &name )
118+
{
119+
mParentLayerParameterName = name;
120+
}
121+

0 commit comments

Comments
 (0)
Please sign in to comment.