QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
21  const QString &description,
22  const QString &meshLayerParameterName,
23  const QSet<int> supportedDataType,
24  bool optional ):
25  QgsProcessingParameterDefinition( name, description, QVariantList(), optional, QString() ),
26  mMeshLayerParameterName( meshLayerParameterName ),
27  mSupportedDataType( supportedDataType )
28 {
29 }
30 
32 {
33  return new QgsProcessingParameterMeshDatasetGroups( name(), description(), mMeshLayerParameterName, mSupportedDataType );
34 }
35 
37 {
38  return typeName();
39 }
40 
42 {
43  Q_UNUSED( context );
44  return valueIsAcceptable( input, mFlags & FlagOptional );
45 }
46 
47 QString QgsProcessingParameterMeshDatasetGroups::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
48 {
49  Q_UNUSED( context );
50  QStringList parts;
51  const QList<int> groups = valueAsDatasetGroup( value );
52  for ( const int g : groups )
53  parts.append( QString::number( g ) );
54 
55  return parts.join( ',' ).prepend( '[' ).append( ']' );
56 }
57 
59 {
60  switch ( outputType )
61  {
63  {
64  QString code = QStringLiteral( "QgsProcessingParameterMeshDatasetGroups('%1', %2" )
66  if ( !mMeshLayerParameterName.isEmpty() )
67  code += QStringLiteral( ", meshLayerParameterName=%1" ).arg( QgsProcessingUtils::stringToPythonLiteral( mMeshLayerParameterName ) );
68 
69  QStringList dt;
70  if ( mSupportedDataType.contains( QgsMeshDatasetGroupMetadata::DataOnFaces ) )
71  dt.append( QStringLiteral( "QgsMeshDatasetGroupMetadata.DataOnFaces" ) );
72  if ( mSupportedDataType.contains( QgsMeshDatasetGroupMetadata::DataOnVertices ) )
73  dt.append( QStringLiteral( "QgsMeshDatasetGroupMetadata.DataOnVertices" ) );
74  if ( mSupportedDataType.contains( QgsMeshDatasetGroupMetadata::DataOnVolumes ) )
75  dt.append( QStringLiteral( "QgsMeshDatasetGroupMetadata.DataOnVolumes" ) );
76  if ( mSupportedDataType.contains( QgsMeshDatasetGroupMetadata::DataOnEdges ) )
77  dt.append( QStringLiteral( "QgsMeshDatasetGroupMetadata.DataOnEdges" ) );
78  if ( !dt.isEmpty() )
79  {
80  code += QLatin1String( ", supportedDataType=[" );
81  code += dt.join( ',' );
82  code += ']';
83  }
84 
85  if ( mFlags & FlagOptional )
86  code += QLatin1String( ", optional=True" );
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 
112 QList<int> QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( const QVariant &value )
113 {
114  if ( !valueIsAcceptable( value, true ) )
115  return QList<int>();
116 
117  QList<int> ret;
118 
119  // if invalid or empty, return only the group 0
120  if ( !value.isValid() )
121  ret << 0;
122  else
123  {
124  if ( value.type() == QVariant::List )
125  {
126  const QVariantList varList = value.toList();
127  if ( varList.isEmpty() )
128  ret << 0;
129  else
130  for ( const QVariant &v : varList )
131  ret << v.toInt();
132  }
133  else
134  {
135  ret << value.toInt();
136  }
137  }
138 
139  return ret;
140 }
141 
143 {
145  map.insert( QStringLiteral( "mesh_layer" ), mMeshLayerParameterName );
146  QVariantList dataType;
147  for ( int v : mSupportedDataType )
148  dataType.append( v );
149  map.insert( QStringLiteral( "supported_data_type" ), dataType );
150  return map;
151 }
152 
153 bool QgsProcessingParameterMeshDatasetGroups::fromVariantMap( const QVariantMap &map )
154 {
156  mMeshLayerParameterName = map.value( QStringLiteral( "mesh_layer" ) ).toString();
157  const QVariantList dataType = map.value( QStringLiteral( "supported_data_type" ) ).toList();
158  mSupportedDataType.clear();
159  for ( const QVariant &var : dataType )
160  mSupportedDataType.insert( var.toInt() );
161  return true;
162 }
163 
164 bool QgsProcessingParameterMeshDatasetGroups::valueIsAcceptable( const QVariant &input, bool allowEmpty )
165 {
166  if ( !input.isValid() )
167  return allowEmpty;
168 
169  if ( input.type() != QVariant::List )
170  {
171  bool ok = false;
172  input.toInt( &ok );
173  return ok;
174  }
175  const QVariantList list = input.toList();
176 
177  if ( !allowEmpty && list.isEmpty() )
178  return false;
179 
180  for ( const QVariant &var : list )
181  {
182  bool ok = false;
183  var.toInt( &ok );
184  if ( !ok )
185  return false;
186  }
187 
188  return true;
189 }
190 
192  const QString &description,
193  const QString &meshLayerParameterName,
194  const QString &datasetGroupParameterName )
195  : QgsProcessingParameterDefinition( name, description, QVariant() )
196  , mMeshLayerParameterName( meshLayerParameterName )
197  , mDatasetGroupParameterName( datasetGroupParameterName )
198 {
199 
200 }
201 
203 {
204  return new QgsProcessingParameterMeshDatasetTime( name(), description(), mMeshLayerParameterName, mDatasetGroupParameterName );
205 }
206 
208 {
209  return typeName();
210 }
211 
213 {
214  Q_UNUSED( context );
215  return valueIsAcceptable( input, mFlags & FlagOptional );
216 }
217 
218 QString QgsProcessingParameterMeshDatasetTime::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
219 {
220  Q_UNUSED( context );
221  QStringList parts;
223  parts << QStringLiteral( "'type': " ) + type;
224 
225  if ( value.toDateTime().isValid() )
226  {
227  QDateTime dateTime = value.toDateTime();
228  dateTime.setTimeSpec( Qt::UTC );
229  parts << QStringLiteral( "'value': " ) + QgsProcessingUtils::variantToPythonLiteral( dateTime );
230  }
231  else
232  {
233  const QVariantMap variantTimeDataset = value.toMap();
234  if ( variantTimeDataset.value( QStringLiteral( "type" ) ) == QLatin1String( "dataset-time-step" ) )
235  {
236  const QVariantList datasetIndex = variantTimeDataset.value( QStringLiteral( "value" ) ).toList();
237  parts << QStringLiteral( "'value': " ) + QString( "[%1,%2]" ).arg( datasetIndex.at( 0 ).toString(), datasetIndex.at( 1 ).toString() );
238  }
239  else if ( variantTimeDataset.value( QStringLiteral( "type" ) ) == QLatin1String( "defined-date-time" ) )
240  {
241  parts << QStringLiteral( "'value': " ) + QgsProcessingUtils::variantToPythonLiteral( variantTimeDataset.value( QStringLiteral( "value" ) ) );
242  }
243  }
244 
245  return parts.join( ',' ).prepend( '{' ).append( '}' );
246 }
247 
249 {
250  switch ( outputType )
251  {
253  {
254  QString code = QStringLiteral( "QgsProcessingParameterMeshDatasetTime('%1', '%2'" )
255  .arg( name(), description() );
256  if ( !mMeshLayerParameterName.isEmpty() )
257  code += QStringLiteral( ", meshLayerParameterName=%1" ).arg( QgsProcessingUtils::stringToPythonLiteral( mMeshLayerParameterName ) );
258 
259  if ( !mDatasetGroupParameterName.isEmpty() )
260  code += QStringLiteral( ", datasetGroupParameterName=%1" ).arg( QgsProcessingUtils::stringToPythonLiteral( mDatasetGroupParameterName ) );
261 
262  if ( mFlags & FlagOptional )
263  code += QLatin1String( ", optional=True" );
264  code += ')';
265  return code;
266  }
267  }
268  return QString();
269 }
270 
272 {
273  QStringList otherParameters;
274  if ( !mMeshLayerParameterName.isEmpty() )
275  otherParameters << mMeshLayerParameterName;
276 
277  if ( !mDatasetGroupParameterName.isEmpty() )
278  otherParameters << mMeshLayerParameterName << mDatasetGroupParameterName;
279 
280  return otherParameters;
281 }
282 
284 {
286  map.insert( QStringLiteral( "mesh_layer" ), mMeshLayerParameterName );
287  map.insert( QStringLiteral( "dataset_groups" ), mDatasetGroupParameterName );
288  return map;
289 }
290 
291 bool QgsProcessingParameterMeshDatasetTime::fromVariantMap( const QVariantMap &map )
292 {
294  mMeshLayerParameterName = map.value( QStringLiteral( "mesh_layer" ) ).toString();
295  mDatasetGroupParameterName = map.value( QStringLiteral( "dataset_groups" ) ).toString();
296  return true;
297 }
298 
300 {
301  return mMeshLayerParameterName;
302 }
303 
305 {
306  return mDatasetGroupParameterName;
307 }
308 
309 QString QgsProcessingParameterMeshDatasetTime::valueAsTimeType( const QVariant &value )
310 {
311  if ( !valueIsAcceptable( value, false ) )
312  return QString();
313 
314  if ( value.toDateTime().isValid() )
315  return QStringLiteral( "defined-date-time" );
316 
317  return value.toMap().value( QStringLiteral( "type" ) ).toString();
318 }
319 
321 {
322  if ( !valueIsAcceptable( value, false ) || valueAsTimeType( value ) != QLatin1String( "dataset-time-step" ) )
323  return QgsMeshDatasetIndex( -1, -1 );
324 
325  const QVariantList list = value.toMap().value( QStringLiteral( "value" ) ).toList();
326  return QgsMeshDatasetIndex( list.at( 0 ).toInt(), list.at( 1 ).toInt() );
327 }
328 
330 {
331  if ( value.toDateTime().isValid() )
332  {
333  QDateTime dateTime = value.toDateTime();
334  dateTime.setTimeSpec( Qt::UTC );
335  return dateTime;
336  }
337 
338  if ( !valueIsAcceptable( value, false ) && valueAsTimeType( value ) != QLatin1String( "defined-date-time" ) )
339  return QDateTime();
340 
341  return value.toMap().value( QStringLiteral( "value" ) ).toDateTime();
342 }
343 
344 bool QgsProcessingParameterMeshDatasetTime::valueIsAcceptable( const QVariant &input, bool allowEmpty )
345 {
346  if ( !input.isValid() )
347  return allowEmpty;
348 
349  if ( input.toDateTime().isValid() )
350  return true;
351 
352  if ( input.type() != QVariant::Map )
353  return false;
354 
355  const QVariantMap map = input.toMap();
356 
357  if ( map.isEmpty() )
358  return allowEmpty;
359 
360  if ( ! map.contains( QStringLiteral( "type" ) ) )
361  return false;
362 
363  const QString type = map.value( QStringLiteral( "type" ) ).toString();
364  const QVariant value = map.value( QStringLiteral( "value" ) );
365 
366  if ( type == QLatin1String( "static" ) || type == QLatin1String( "current-context-time" ) )
367  return true;
368 
369  if ( type == QLatin1String( "dataset-time-step" ) )
370  {
371  if ( value.type() != QVariant::List )
372  return false;
373  const QVariantList list = value.toList();
374  if ( value.toList().count() != 2 )
375  return false;
376  if ( list.at( 0 ).type() != QVariant::Int || list.at( 1 ).type() != QVariant::Int )
377  return false;
378  }
379  else if ( type == QLatin1String( "defined-date-time" ) )
380  {
381  if ( value.type() != QVariant::DateTime )
382  return false;
383  }
384  else
385  return false;
386 
387  return true;
388 }
389 
QgsProcessingParameterMeshDatasetGroups::dependsOnOtherParameters
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
QgsProcessingParameterDefinition::toVariantMap
virtual QVariantMap toVariantMap() const
Saves this parameter to a QVariantMap.
Definition: qgsprocessingparameters.cpp:2895
QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup
static QList< int > valueAsDatasetGroup(const QVariant &value)
Returns the value as a list if dataset group indexes.
QgsMeshDatasetGroupMetadata::DataOnVolumes
@ DataOnVolumes
Data is defined on volumes.
Definition: qgsmeshdataset.h:360
QgsProcessingParameterMeshDatasetTime::clone
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterMeshDatasetTime::fromVariantMap
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterDefinition::description
QString description() const
Returns the description for the parameter.
Definition: qgsprocessingparameters.h:502
QgsProcessingParameterMeshDatasetTime::datasetGroupParameterName
QString datasetGroupParameterName() const
Returns the name of the dataset groups parameter.
QgsProcessingParameterMeshDatasetGroups::isDataTypeSupported
bool isDataTypeSupported(QgsMeshDatasetGroupMetadata::DataType dataType) const
Returns whether the data type is supported by the parameter.
QgsProcessingParameterDefinition
Base class for the definition of processing parameters.
Definition: qgsprocessingparameters.h:334
QgsProcessingParameterMeshDatasetTime::checkValueIsAcceptable
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingParameterMeshDatasetGroups::checkValueIsAcceptable
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingUtils::stringToPythonLiteral
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
Definition: qgsprocessingutils.cpp:701
QgsProcessingParameterMeshDatasetTime::timeValueAsDefinedDateTime
static QDateTime timeValueAsDefinedDateTime(const QVariant &value)
Returns the value as a QDateTime if the value has "defined-date-time" type.
QgsProcessingParameterMeshDatasetGroups::valueAsPythonString
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...
QgsProcessingParameterMeshDatasetGroups::QgsProcessingParameterMeshDatasetGroups
QgsProcessingParameterMeshDatasetGroups(const QString &name, const QString &description=QString(), const QString &meshLayerParameterName=QString(), QSet< int > supportedDataType=QSet< int >(), bool optional=false)
Constructor.
QgsMeshDatasetGroupMetadata::DataOnFaces
@ DataOnFaces
Data is defined on faces.
Definition: qgsmeshdataset.h:358
qgsprocessingparametermeshdataset.h
QgsMeshDatasetGroupMetadata::DataOnVertices
@ DataOnVertices
Data is defined on vertices.
Definition: qgsmeshdataset.h:359
QgsProcessing::PythonQgsProcessingAlgorithmSubclass
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
Definition: qgsprocessing.h:64
QgsProcessingParameterMeshDatasetTime::timeValueAsDatasetIndex
static QgsMeshDatasetIndex timeValueAsDatasetIndex(const QVariant &value)
Returns the value as a QgsMeshDatasetIndex if the value has "dataset-time-step" type.
QgsProcessingParameterDefinition::fromVariantMap
virtual bool fromVariantMap(const QVariantMap &map)
Restores this parameter to a QVariantMap.
Definition: qgsprocessingparameters.cpp:2909
QgsProcessingParameterDefinition::mFlags
Flags mFlags
Parameter flags.
Definition: qgsprocessingparameters.h:861
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsProcessingParameterMeshDatasetGroups::type
QString type() const override
Unique parameter type name.
QgsMeshDatasetIndex
QgsMeshDatasetIndex is index that identifies the dataset group (e.g. wind speed) and a dataset in thi...
Definition: qgsmeshdataset.h:48
QgsProcessingParameterDefinition::name
QString name() const
Returns the name of the parameter.
Definition: qgsprocessingparameters.h:488
QgsProcessingParameterMeshDatasetGroups::clone
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterMeshDatasetTime::valueAsTimeType
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...
QgsProcessingParameterMeshDatasetTime::typeName
static QString typeName()
Returns the type name for the parameter class.
Definition: qgsprocessingparametermeshdataset.h:172
QgsMeshDatasetGroupMetadata::DataOnEdges
@ DataOnEdges
Data is defined on edges.
Definition: qgsmeshdataset.h:361
QgsProcessingParameterMeshDatasetGroups::typeName
static QString typeName()
Returns the type name for the parameter class.
Definition: qgsprocessingparametermeshdataset.h:75
QgsProcessingParameterMeshDatasetTime::asPythonString
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterMeshDatasetTime::QgsProcessingParameterMeshDatasetTime
QgsProcessingParameterMeshDatasetTime(const QString &name, const QString &description=QString(), const QString &meshLayerParameterName=QString(), const QString &datasetGroupParameterName=QString())
Constructor.
QgsProcessingParameterMeshDatasetTime::dependsOnOtherParameters
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
QgsProcessingParameterMeshDatasetGroups::fromVariantMap
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsMeshDatasetGroupMetadata::DataType
DataType
Location of where data is specified for datasets in the dataset group.
Definition: qgsmeshdataset.h:356
QgsProcessingUtils::variantToPythonLiteral
static QString variantToPythonLiteral(const QVariant &value)
Converts a variant to a Python literal.
Definition: qgsprocessingutils.cpp:599
QgsProcessingParameterMeshDatasetGroups::asPythonString
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterMeshDatasetTime::type
QString type() const override
Unique parameter type name.
QgsProcessingParameterMeshDatasetGroups::toVariantMap
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsProcessingParameterDefinition::FlagOptional
@ FlagOptional
Parameter is optional.
Definition: qgsprocessingparameters.h:453
QgsProcessingParameterMeshDatasetTime::toVariantMap
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsProcessingParameterMeshDatasetGroups::meshLayerParameterName
QString meshLayerParameterName() const
Returns the name of the mesh layer parameter.
QgsProcessing::PythonOutputType
PythonOutputType
Available Python output types.
Definition: qgsprocessing.h:62
QgsProcessingParameterMeshDatasetTime::meshLayerParameterName
QString meshLayerParameterName() const
Returns the name of the mesh layer parameter.
QgsProcessingParameterMeshDatasetTime::valueAsPythonString
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...