QGIS API Documentation 3.99.0-Master (09f76ad7019)
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 &description,
26 const QString &meshLayerParameterName,
27 const QSet<int> supportedDataType,
28 bool optional ):
29 QgsProcessingParameterDefinition( name, description, QVariantList(), optional, QString() ),
30 mMeshLayerParameterName( meshLayerParameterName ),
31 mSupportedDataType( supportedDataType )
32{
33}
34
36{
37 return new QgsProcessingParameterMeshDatasetGroups( name(), description(), mMeshLayerParameterName, mSupportedDataType );
38}
39
41{
42 return typeName();
43}
44
46{
47 Q_UNUSED( context );
48 return valueIsAcceptable( input, mFlags & Qgis::ProcessingParameterFlag::Optional );
49}
50
52{
53 Q_UNUSED( context );
54 QStringList parts;
55 const QList<int> groups = valueAsDatasetGroup( value );
56 for ( const int g : groups )
57 parts.append( QString::number( g ) );
58
59 return parts.join( ',' ).prepend( '[' ).append( ']' );
60}
61
63{
64 switch ( outputType )
65 {
67 {
68 QString code = u"QgsProcessingParameterMeshDatasetGroups('%1', %2"_s
70 if ( !mMeshLayerParameterName.isEmpty() )
71 code += u", meshLayerParameterName=%1"_s.arg( QgsProcessingUtils::stringToPythonLiteral( mMeshLayerParameterName ) );
72
73 QStringList dt;
74 if ( mSupportedDataType.contains( QgsMeshDatasetGroupMetadata::DataOnFaces ) )
75 dt.append( u"QgsMeshDatasetGroupMetadata.DataOnFaces"_s );
76 if ( mSupportedDataType.contains( QgsMeshDatasetGroupMetadata::DataOnVertices ) )
77 dt.append( u"QgsMeshDatasetGroupMetadata.DataOnVertices"_s );
78 if ( mSupportedDataType.contains( QgsMeshDatasetGroupMetadata::DataOnVolumes ) )
79 dt.append( u"QgsMeshDatasetGroupMetadata.DataOnVolumes"_s );
80 if ( mSupportedDataType.contains( QgsMeshDatasetGroupMetadata::DataOnEdges ) )
81 dt.append( u"QgsMeshDatasetGroupMetadata.DataOnEdges"_s );
82 if ( !dt.isEmpty() )
83 {
84 code += ", supportedDataType=["_L1;
85 code += dt.join( ',' );
86 code += ']';
87 }
88
90 code += ", optional=True"_L1;
91 code += ')';
92 return code;
93 }
94 }
95 return QString();
96}
97
99{
100 if ( mMeshLayerParameterName.isEmpty() )
101 return QStringList();
102 else
103 return QStringList() << mMeshLayerParameterName;
104}
105
107{
108 return mMeshLayerParameterName;
109}
110
112{
113 return mSupportedDataType.contains( dataType );
114}
115
116QList<int> QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( const QVariant &value )
117{
118 if ( !valueIsAcceptable( value, true ) )
119 return QList<int>();
120
121 QList<int> ret;
122
123 if ( value.isValid() )
124 {
125 if ( value.userType() == QMetaType::Type::QVariantList )
126 {
127 const QVariantList varList = value.toList();
128 for ( const QVariant &v : varList )
129 ret << v.toInt();
130 }
131 else
132 {
133 ret << value.toInt();
134 }
135 }
136
137 return ret;
138}
139
141{
143 map.insert( u"mesh_layer"_s, mMeshLayerParameterName );
144 QVariantList dataType;
145 for ( int v : mSupportedDataType )
146 dataType.append( v );
147 map.insert( u"supported_data_type"_s, dataType );
148 return map;
149}
150
152{
154 mMeshLayerParameterName = map.value( u"mesh_layer"_s ).toString();
155 const QVariantList dataType = map.value( u"supported_data_type"_s ).toList();
156 mSupportedDataType.clear();
157 for ( const QVariant &var : dataType )
158 mSupportedDataType.insert( var.toInt() );
159 return true;
160}
161
162bool QgsProcessingParameterMeshDatasetGroups::valueIsAcceptable( const QVariant &input, bool allowEmpty )
163{
164 if ( !input.isValid() )
165 return allowEmpty;
166
167 if ( input.userType() != QMetaType::Type::QVariantList )
168 {
169 bool ok = false;
170 input.toInt( &ok );
171 return ok;
172 }
173 const QVariantList list = input.toList();
174
175 if ( !allowEmpty && list.isEmpty() )
176 return false;
177
178 for ( const QVariant &var : list )
179 {
180 bool ok = false;
181 var.toInt( &ok );
182 if ( !ok )
183 return false;
184 }
185
186 return true;
187}
188
190 const QString &description,
191 const QString &meshLayerParameterName,
192 const QString &datasetGroupParameterName )
193 : QgsProcessingParameterDefinition( name, description, QVariant() )
194 , mMeshLayerParameterName( meshLayerParameterName )
195 , mDatasetGroupParameterName( datasetGroupParameterName )
196{
197
198}
199
201{
202 return new QgsProcessingParameterMeshDatasetTime( name(), description(), mMeshLayerParameterName, mDatasetGroupParameterName );
203}
204
206{
207 return typeName();
208}
209
211{
212 Q_UNUSED( context );
213 return valueIsAcceptable( input, mFlags & Qgis::ProcessingParameterFlag::Optional );
214}
215
216QString QgsProcessingParameterMeshDatasetTime::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
217{
218 Q_UNUSED( context );
219 QStringList parts;
221 parts << u"'type': "_s + type;
222
223 if ( value.toDateTime().isValid() )
224 {
225 QDateTime dateTime = value.toDateTime();
226 dateTime.setTimeSpec( Qt::UTC );
227 parts << u"'value': "_s + QgsProcessingUtils::variantToPythonLiteral( dateTime );
228 }
229 else
230 {
231 const QVariantMap variantTimeDataset = value.toMap();
232 if ( variantTimeDataset.value( u"type"_s ) == "dataset-time-step"_L1 )
233 {
234 const QVariantList datasetIndex = variantTimeDataset.value( u"value"_s ).toList();
235 parts << u"'value': "_s + QString( "[%1,%2]" ).arg( datasetIndex.at( 0 ).toString(), datasetIndex.at( 1 ).toString() );
236 }
237 else if ( variantTimeDataset.value( u"type"_s ) == "defined-date-time"_L1 )
238 {
239 parts << u"'value': "_s + QgsProcessingUtils::variantToPythonLiteral( variantTimeDataset.value( u"value"_s ) );
240 }
241 }
242
243 return parts.join( ',' ).prepend( '{' ).append( '}' );
244}
245
247{
248 switch ( outputType )
249 {
251 {
252 QString code = u"QgsProcessingParameterMeshDatasetTime('%1', '%2'"_s
253 .arg( name(), description() );
254 if ( !mMeshLayerParameterName.isEmpty() )
255 code += u", meshLayerParameterName=%1"_s.arg( QgsProcessingUtils::stringToPythonLiteral( mMeshLayerParameterName ) );
256
257 if ( !mDatasetGroupParameterName.isEmpty() )
258 code += u", datasetGroupParameterName=%1"_s.arg( QgsProcessingUtils::stringToPythonLiteral( mDatasetGroupParameterName ) );
259
261 code += ", optional=True"_L1;
262 code += ')';
263 return code;
264 }
265 }
266 return QString();
267}
268
270{
271 QStringList otherParameters;
272 if ( !mMeshLayerParameterName.isEmpty() )
273 otherParameters << mMeshLayerParameterName;
274
275 if ( !mDatasetGroupParameterName.isEmpty() )
276 otherParameters << mMeshLayerParameterName << mDatasetGroupParameterName;
277
278 return otherParameters;
279}
280
282{
284 map.insert( u"mesh_layer"_s, mMeshLayerParameterName );
285 map.insert( u"dataset_groups"_s, mDatasetGroupParameterName );
286 return map;
287}
288
290{
292 mMeshLayerParameterName = map.value( u"mesh_layer"_s ).toString();
293 mDatasetGroupParameterName = map.value( u"dataset_groups"_s ).toString();
294 return true;
295}
296
298{
299 return mMeshLayerParameterName;
300}
301
303{
304 return mDatasetGroupParameterName;
305}
306
307QString QgsProcessingParameterMeshDatasetTime::valueAsTimeType( const QVariant &value )
308{
309 if ( !valueIsAcceptable( value, false ) )
310 return QString();
311
312 if ( value.toDateTime().isValid() )
313 return u"defined-date-time"_s;
314
315 return value.toMap().value( u"type"_s ).toString();
316}
317
319{
320 if ( !valueIsAcceptable( value, false ) || valueAsTimeType( value ) != "dataset-time-step"_L1 )
321 return QgsMeshDatasetIndex( -1, -1 );
322
323 const QVariantList list = value.toMap().value( u"value"_s ).toList();
324 return QgsMeshDatasetIndex( list.at( 0 ).toInt(), list.at( 1 ).toInt() );
325}
326
328{
329 if ( value.toDateTime().isValid() )
330 {
331 QDateTime dateTime = value.toDateTime();
332 dateTime.setTimeSpec( Qt::UTC );
333 return dateTime;
334 }
335
336 if ( !valueIsAcceptable( value, false ) && valueAsTimeType( value ) != "defined-date-time"_L1 )
337 return QDateTime();
338
339 return value.toMap().value( u"value"_s ).toDateTime();
340}
341
342bool QgsProcessingParameterMeshDatasetTime::valueIsAcceptable( const QVariant &input, bool allowEmpty )
343{
344 if ( !input.isValid() )
345 return allowEmpty;
346
347 if ( input.toDateTime().isValid() )
348 return true;
349
350 if ( input.userType() != QMetaType::Type::QVariantMap )
351 return false;
352
353 const QVariantMap map = input.toMap();
354
355 if ( map.isEmpty() )
356 return allowEmpty;
357
358 if ( ! map.contains( u"type"_s ) )
359 return false;
360
361 const QString type = map.value( u"type"_s ).toString();
362 const QVariant value = map.value( u"value"_s );
363
364 if ( type == "static"_L1 || type == "current-context-time"_L1 )
365 return true;
366
367 if ( type == "dataset-time-step"_L1 )
368 {
369 if ( value.userType() != QMetaType::Type::QVariantList )
370 return false;
371 const QVariantList list = value.toList();
372 if ( value.toList().count() != 2 )
373 return false;
374 if ( list.at( 0 ).userType() != QMetaType::Type::Int || list.at( 1 ).userType() != QMetaType::Type::Int )
375 return false;
376 }
377 else if ( type == "defined-date-time"_L1 )
378 {
379 if ( value.userType() != QMetaType::Type::QDateTime )
380 return false;
381 }
382 else
383 return false;
384
385 return true;
386}
387
@ Optional
Parameter is optional.
Definition qgis.h:3836
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.