QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgsprocessingparametermeshdataset.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingparametermeshdataset.cpp
3 ---------------------
4 Date : October 2020
5 Copyright : (C) 2020 by Vincent Cloarec
6 Email : vcloarec 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
17
18#include <QString>
19
20using namespace Qt::StringLiterals;
21
25 const QString &name, const QString &description, const QString &meshLayerParameterName, const QSet<int> supportedDataType, bool optional
26)
27 : QgsProcessingParameterDefinition( name, description, QVariantList(), optional, QString() )
28 , mMeshLayerParameterName( meshLayerParameterName )
29 , mSupportedDataType( supportedDataType )
30{}
31
33{
34 return new QgsProcessingParameterMeshDatasetGroups( name(), description(), mMeshLayerParameterName, mSupportedDataType );
35}
36
38{
39 return typeName();
40}
41
43{
44 Q_UNUSED( context );
45 return valueIsAcceptable( input, mFlags & Qgis::ProcessingParameterFlag::Optional );
46}
47
49{
50 Q_UNUSED( context );
51 QStringList parts;
52 const QList<int> groups = valueAsDatasetGroup( value );
53 for ( const int g : groups )
54 parts.append( QString::number( g ) );
55
56 return parts.join( ',' ).prepend( '[' ).append( ']' );
57}
58
60{
61 switch ( outputType )
62 {
64 {
65 QString code = u"QgsProcessingParameterMeshDatasetGroups('%1', %2"_s.arg( name(), QgsProcessingUtils::stringToPythonLiteral( description() ) );
66 if ( !mMeshLayerParameterName.isEmpty() )
67 code += u", meshLayerParameterName=%1"_s.arg( QgsProcessingUtils::stringToPythonLiteral( mMeshLayerParameterName ) );
68
69 QStringList dt;
70 if ( mSupportedDataType.contains( QgsMeshDatasetGroupMetadata::DataOnFaces ) )
71 dt.append( u"QgsMeshDatasetGroupMetadata.DataOnFaces"_s );
72 if ( mSupportedDataType.contains( QgsMeshDatasetGroupMetadata::DataOnVertices ) )
73 dt.append( u"QgsMeshDatasetGroupMetadata.DataOnVertices"_s );
74 if ( mSupportedDataType.contains( QgsMeshDatasetGroupMetadata::DataOnVolumes ) )
75 dt.append( u"QgsMeshDatasetGroupMetadata.DataOnVolumes"_s );
76 if ( mSupportedDataType.contains( QgsMeshDatasetGroupMetadata::DataOnEdges ) )
77 dt.append( u"QgsMeshDatasetGroupMetadata.DataOnEdges"_s );
78 if ( !dt.isEmpty() )
79 {
80 code += ", supportedDataType=["_L1;
81 code += dt.join( ',' );
82 code += ']';
83 }
84
86 code += ", optional=True"_L1;
87 code += ')';
88 return code;
89 }
90 }
91 return QString();
92}
93
95{
96 if ( mMeshLayerParameterName.isEmpty() )
97 return QStringList();
98 else
99 return QStringList() << mMeshLayerParameterName;
100}
101
103{
104 return mMeshLayerParameterName;
105}
106
108{
109 return mSupportedDataType.contains( dataType );
110}
111
112QList<int> QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( const QVariant &value )
113{
114 if ( !valueIsAcceptable( value, true ) )
115 return QList<int>();
116
117 QList<int> ret;
118
119 if ( value.isValid() )
120 {
121 if ( value.userType() == QMetaType::Type::QVariantList )
122 {
123 const QVariantList varList = value.toList();
124 for ( const QVariant &v : varList )
125 ret << v.toInt();
126 }
127 else
128 {
129 ret << value.toInt();
130 }
131 }
132
133 return ret;
134}
135
137{
139 map.insert( u"mesh_layer"_s, mMeshLayerParameterName );
140 QVariantList dataType;
141 for ( int v : mSupportedDataType )
142 dataType.append( v );
143 map.insert( u"supported_data_type"_s, dataType );
144 return map;
145}
146
148{
150 mMeshLayerParameterName = map.value( u"mesh_layer"_s ).toString();
151 const QVariantList dataType = map.value( u"supported_data_type"_s ).toList();
152 mSupportedDataType.clear();
153 for ( const QVariant &var : dataType )
154 mSupportedDataType.insert( var.toInt() );
155 return true;
156}
157
158bool QgsProcessingParameterMeshDatasetGroups::valueIsAcceptable( const QVariant &input, bool allowEmpty )
159{
160 if ( !input.isValid() )
161 return allowEmpty;
162
163 if ( input.userType() != QMetaType::Type::QVariantList )
164 {
165 bool ok = false;
166 input.toInt( &ok );
167 return ok;
168 }
169 const QVariantList list = input.toList();
170
171 if ( !allowEmpty && list.isEmpty() )
172 return false;
173
174 for ( const QVariant &var : list )
175 {
176 bool ok = false;
177 var.toInt( &ok );
178 if ( !ok )
179 return false;
180 }
181
182 return true;
183}
184
185QgsProcessingParameterMeshDatasetTime::QgsProcessingParameterMeshDatasetTime( const QString &name, const QString &description, const QString &meshLayerParameterName, const QString &datasetGroupParameterName )
186 : QgsProcessingParameterDefinition( name, description, QVariant() )
187 , mMeshLayerParameterName( meshLayerParameterName )
188 , mDatasetGroupParameterName( datasetGroupParameterName )
189{}
190
192{
193 return new QgsProcessingParameterMeshDatasetTime( name(), description(), mMeshLayerParameterName, mDatasetGroupParameterName );
194}
195
197{
198 return typeName();
199}
200
202{
203 Q_UNUSED( context );
204 return valueIsAcceptable( input, mFlags & Qgis::ProcessingParameterFlag::Optional );
205}
206
207QString QgsProcessingParameterMeshDatasetTime::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
208{
209 Q_UNUSED( context );
210 QStringList parts;
212 parts << u"'type': "_s + type;
213
214 if ( value.toDateTime().isValid() )
215 {
216 QDateTime dateTime = value.toDateTime();
217 dateTime.setTimeSpec( Qt::UTC );
218 parts << u"'value': "_s + QgsProcessingUtils::variantToPythonLiteral( dateTime );
219 }
220 else
221 {
222 const QVariantMap variantTimeDataset = value.toMap();
223 if ( variantTimeDataset.value( u"type"_s ) == "dataset-time-step"_L1 )
224 {
225 const QVariantList datasetIndex = variantTimeDataset.value( u"value"_s ).toList();
226 parts << u"'value': "_s + QString( "[%1,%2]" ).arg( datasetIndex.at( 0 ).toString(), datasetIndex.at( 1 ).toString() );
227 }
228 else if ( variantTimeDataset.value( u"type"_s ) == "defined-date-time"_L1 )
229 {
230 parts << u"'value': "_s + QgsProcessingUtils::variantToPythonLiteral( variantTimeDataset.value( u"value"_s ) );
231 }
232 }
233
234 return parts.join( ',' ).prepend( '{' ).append( '}' );
235}
236
238{
239 switch ( outputType )
240 {
242 {
243 QString code = u"QgsProcessingParameterMeshDatasetTime('%1', '%2'"_s.arg( name(), description() );
244 if ( !mMeshLayerParameterName.isEmpty() )
245 code += u", meshLayerParameterName=%1"_s.arg( QgsProcessingUtils::stringToPythonLiteral( mMeshLayerParameterName ) );
246
247 if ( !mDatasetGroupParameterName.isEmpty() )
248 code += u", datasetGroupParameterName=%1"_s.arg( QgsProcessingUtils::stringToPythonLiteral( mDatasetGroupParameterName ) );
249
251 code += ", optional=True"_L1;
252 code += ')';
253 return code;
254 }
255 }
256 return QString();
257}
258
260{
261 QStringList otherParameters;
262 if ( !mMeshLayerParameterName.isEmpty() )
263 otherParameters << mMeshLayerParameterName;
264
265 if ( !mDatasetGroupParameterName.isEmpty() )
266 otherParameters << mMeshLayerParameterName << mDatasetGroupParameterName;
267
268 return otherParameters;
269}
270
272{
274 map.insert( u"mesh_layer"_s, mMeshLayerParameterName );
275 map.insert( u"dataset_groups"_s, mDatasetGroupParameterName );
276 return map;
277}
278
280{
282 mMeshLayerParameterName = map.value( u"mesh_layer"_s ).toString();
283 mDatasetGroupParameterName = map.value( u"dataset_groups"_s ).toString();
284 return true;
285}
286
288{
289 return mMeshLayerParameterName;
290}
291
293{
294 return mDatasetGroupParameterName;
295}
296
297QString QgsProcessingParameterMeshDatasetTime::valueAsTimeType( const QVariant &value )
298{
299 if ( !valueIsAcceptable( value, false ) )
300 return QString();
301
302 if ( value.toDateTime().isValid() )
303 return u"defined-date-time"_s;
304
305 return value.toMap().value( u"type"_s ).toString();
306}
307
309{
310 if ( !valueIsAcceptable( value, false ) || valueAsTimeType( value ) != "dataset-time-step"_L1 )
311 return QgsMeshDatasetIndex( -1, -1 );
312
313 const QVariantList list = value.toMap().value( u"value"_s ).toList();
314 return QgsMeshDatasetIndex( list.at( 0 ).toInt(), list.at( 1 ).toInt() );
315}
316
318{
319 if ( value.toDateTime().isValid() )
320 {
321 QDateTime dateTime = value.toDateTime();
322 dateTime.setTimeSpec( Qt::UTC );
323 return dateTime;
324 }
325
326 if ( !valueIsAcceptable( value, false ) && valueAsTimeType( value ) != "defined-date-time"_L1 )
327 return QDateTime();
328
329 return value.toMap().value( u"value"_s ).toDateTime();
330}
331
332bool QgsProcessingParameterMeshDatasetTime::valueIsAcceptable( const QVariant &input, bool allowEmpty )
333{
334 if ( !input.isValid() )
335 return allowEmpty;
336
337 if ( input.toDateTime().isValid() )
338 return true;
339
340 if ( input.userType() != QMetaType::Type::QVariantMap )
341 return false;
342
343 const QVariantMap map = input.toMap();
344
345 if ( map.isEmpty() )
346 return allowEmpty;
347
348 if ( !map.contains( u"type"_s ) )
349 return false;
350
351 const QString type = map.value( u"type"_s ).toString();
352 const QVariant value = map.value( u"value"_s );
353
354 if ( type == "static"_L1 || type == "current-context-time"_L1 )
355 return true;
356
357 if ( type == "dataset-time-step"_L1 )
358 {
359 if ( value.userType() != QMetaType::Type::QVariantList )
360 return false;
361 const QVariantList list = value.toList();
362 if ( value.toList().count() != 2 )
363 return false;
364 if ( list.at( 0 ).userType() != QMetaType::Type::Int || list.at( 1 ).userType() != QMetaType::Type::Int )
365 return false;
366 }
367 else if ( type == "defined-date-time"_L1 )
368 {
369 if ( value.userType() != QMetaType::Type::QDateTime )
370 return false;
371 }
372 else
373 return false;
374
375 return true;
376}
377
@ Optional
Parameter is optional.
Definition qgis.h:3882
DataType
Location of where data is specified for datasets in the dataset group.
@ DataOnEdges
Data is defined on edges.
@ DataOnFaces
Data is defined on faces.
@ DataOnVertices
Data is defined on vertices.
@ DataOnVolumes
Data is defined on volumes.
An index that identifies the dataset group (e.g.
Contains information about the context in which a processing algorithm is executed.
Base class for the definition of processing parameters.
Qgis::ProcessingParameterFlags mFlags
Parameter flags.
QString description() const
Returns the description for the parameter.
virtual QVariantMap toVariantMap() const
Saves this parameter to a QVariantMap.
QString name() const
Returns the name of the parameter.
virtual bool fromVariantMap(const QVariantMap &map)
Restores this parameter to a QVariantMap.
QgsProcessingParameterMeshDatasetGroups(const QString &name, const QString &description=QString(), const QString &meshLayerParameterName=QString(), QSet< int > supportedDataType=QSet< int >(), bool optional=false)
Constructor.
static QString typeName()
Returns the type name for the parameter class.
static QList< int > valueAsDatasetGroup(const QVariant &value)
Returns the value as a list if dataset group indexes.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString type() const override
Unique parameter type name.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QString meshLayerParameterName() const
Returns the name of the mesh layer parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonOutputType::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
bool isDataTypeSupported(QgsMeshDatasetGroupMetadata::DataType dataType) const
Returns whether the data type is supported by the parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonOutputType::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterMeshDatasetTime(const QString &name, const QString &description=QString(), const QString &meshLayerParameterName=QString(), const QString &datasetGroupParameterName=QString())
Constructor.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QString valueAsTimeType(const QVariant &value)
Returns the dataset value time type as a string : current-context-time : the time is store in the pro...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QString datasetGroupParameterName() const
Returns the name of the dataset groups parameter.
QString meshLayerParameterName() const
Returns the name of the mesh layer parameter.
static QgsMeshDatasetIndex timeValueAsDatasetIndex(const QVariant &value)
Returns the value as a QgsMeshDatasetIndex if the value has "dataset-time-step" type.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
static QString typeName()
Returns the type name for the parameter class.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QDateTime timeValueAsDefinedDateTime(const QVariant &value)
Returns the value as a QDateTime if the value has "defined-date-time" type.
QString type() const override
Unique parameter type name.
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
static QString variantToPythonLiteral(const QVariant &value)
Converts a variant to a Python literal.
PythonOutputType
Available Python output types.
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.