1 /***************************************************************************
2   qgsprocessingparametermeshdataset.h
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 
16 #ifndef QGSPROCESSINGPARAMETERMESHDATASET_H
17 #define QGSPROCESSINGPARAMETERMESHDATASET_H
18 
19 #include "qgsprocessingparameters.h"
20 #include "qgsprocessingparametertype.h"
21 #include "qgsmeshdataset.h"
22 
23 /**
24  * \brief A parameter for processing algorithms that need a list of mesh dataset groups.
25  *
26  * A valid value for this parameter is a list (QVariantList) of dataset groups index in the mesh layer scope
27  * Dataset group index can be evaluated with the method valueAsDatasetGroup()
28  *
29  * \note This parameter is dependent on a mesh layer parameter (see QgsProcessingParameterMeshLayer)
30  *
31  * \ingroup core
32  * \since QGIS 3.18
33  */
34 class CORE_EXPORT QgsProcessingParameterMeshDatasetGroups : public QgsProcessingParameterDefinition
35 {
36   public:
37 
38     /**
39      * Constructor
40      * \param name name of the parameter
41      * \param description description of the parameter
42      * \param meshLayerParameterName name of the associated mesh layer parameter
43      * \param supportedDataType a set of QgsMeshDatasetGroupMetadata::DataType values for data types supported by the parameter
44      * \param optional whether the parameter is optional
45      */
46     QgsProcessingParameterMeshDatasetGroups( const QString &name,
47         const QString &description = QString(),
48         const QString &meshLayerParameterName = QString(),
49         QSet<int> supportedDataType = QSet<int>(),
50         bool optional = false );
51 
52     QgsProcessingParameterDefinition *clone() const override SIP_FACTORY;
53     QString type() const override;
54     bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
55     QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
56     QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const override;
57     QStringList dependsOnOtherParameters() const override;
58 
59     //! Returns the type name for the parameter class.
typeName()60     static QString typeName() { return QStringLiteral( "meshdatasetgroups" ); }
61 
62     //! Returns the name of the mesh layer parameter
63     QString meshLayerParameterName() const;
64 
65     //! Returns whether the data type is supported by the parameter
66     bool isDataTypeSupported( QgsMeshDatasetGroupMetadata::DataType dataType ) const;
67 
68     //! Returns the \a value as a list if dataset group indexes
69     static QList<int> valueAsDatasetGroup( const QVariant &value );
70 
71   private:
72     QString mMeshLayerParameterName;
73     QSet<int> mSupportedDataType;
74 
75     static bool valueIsAcceptable( const QVariant &input, bool allowEmpty );
76 };
77 
78 #ifndef SIP_RUN
79 ///@cond PRIVATE
80 
81 /**
82  * \brief Parameter type definition for QgsProcessingParameterMeshDatasetGroups.
83  *
84  * \ingroup core
85  * \since QGIS 3.18
86  */
87 class CORE_EXPORT QgsProcessingParameterTypeMeshDatasetGroups : public QgsProcessingParameterType
88 {
89   public:
create(const QString & name)90     QgsProcessingParameterDefinition *create( const QString &name ) const override SIP_FACTORY
91     {
92       return new QgsProcessingParameterMeshDatasetGroups( name );
93     }
94 
description()95     QString description() const override
96     {
97       return QCoreApplication::translate( "Processing", "An input allowing selection dataset groups from a mesh layer" );
98     }
99 
name()100     QString name() const override
101     {
102       return QCoreApplication::translate( "Processing", "Mesh Dataset Groups" );
103     }
104 
id()105     QString id() const override
106     {
107       return QgsProcessingParameterMeshDatasetGroups::typeName();
108     }
109 
pythonImportString()110     QString pythonImportString() const override
111     {
112       return QStringLiteral( "from qgis.core import QgsProcessingParameterMeshDatasetGroups" );
113     }
114 
className()115     QString className() const override
116     {
117       return QStringLiteral( "QgsProcessingParameterMeshDatasetGroups" );
118     }
119 
acceptedPythonTypes()120     QStringList acceptedPythonTypes() const override
121     {
122       return QStringList() << QObject::tr( "list[int]: list of dataset group indexes, see QgsProcessingParameterMeshDatasetGroups docs" );
123     }
124 };
125 
126 ///@endcond
127 #endif //SIP_RUN
128 
129 /**
130  * \brief A parameter for processing algorithms that need a list of mesh dataset index from time parameter.
131  *
132  * A valid value for this parameter is a map (QVariantMap) with in this form:
133  *
134  * - "type" : the type of time settings "current-context-time", "defined-date-time", "dataset-time-step" or "none" if all the dataset groups are static
135  * - "value" : nothing if type is "static" or "current-context-time", QDateTime if "defined-date-time" or, for "dataset_time_step",  list of two int representing the dataset index that is the reference for the time step
136  *
137  * \note This parameter is dependent on a mesh layer parameter (\see QgsProcessingParameterMeshLayer)
138  * and on mesh datast group parameter (\see QgsProcessingParameterMeshDatasetGroups)
139  *
140  * \ingroup core
141  * \since QGIS 3.18
142  */
143 class CORE_EXPORT QgsProcessingParameterMeshDatasetTime : public QgsProcessingParameterDefinition
144 {
145   public:
146 
147     /**
148      * Constructor
149      * \param name name of the parameter
150      * \param description description of the parameter
151      * \param meshLayerParameterName name of the associated mesh layer parameter (\see QgsProcessingParameterMeshLayer)
152      * \param datasetGroupParameterName name of the associated dataset group parameter (\see QgsProcessingParameterMeshDatasetGroups)
153      */
154     QgsProcessingParameterMeshDatasetTime(
155       const QString &name,
156       const QString &description = QString(),
157       const QString &meshLayerParameterName = QString(),
158       const QString &datasetGroupParameterName = QString() );
159 
160     QgsProcessingParameterDefinition *clone() const override SIP_FACTORY;
161     QString type() const override;
162     bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
163     QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
164     QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const override;
165     QStringList dependsOnOtherParameters() const override;
166 
167     //! Returns the type name for the parameter class.
typeName()168     static QString typeName() { return QStringLiteral( "meshdatasettime" ); }
169 
170     //! Returns the name of the mesh layer parameter
171     QString meshLayerParameterName() const;
172 
173     //! Returns the name of the dataset groups parameter
174     QString datasetGroupParameterName() const;
175 
176     /**
177      * Returns the \a dataset value time type as a string :
178      * current-context-time : the time is store in the processing context (e.g. current canvas time), in this case the value does not contain any time value
179      * defined-date-time : absolute time of type QDateTime
180      * dataset-time-step : a time step of existing dataset, in this case the time takes the form of a QMeshDatasetIndex with value to the corresponding dataset index
181      * static : dataset groups are all static, in this case the value does not contain any time value
182      */
183     static QString valueAsTimeType( const QVariant &value );
184 
185     /**
186      * Returns the \a value as a QgsMeshDatasetIndex if the value has "dataset-time-step" type.
187      * If the value has the wrong type return an invalid dataset index
188      *
189      * \see valueAsTimeType()
190      */
191     static QgsMeshDatasetIndex timeValueAsDatasetIndex( const QVariant &value );
192 
193     /**
194      * Returns the \a value as a QDateTime if the value has "defined-date-time" type.
195      * If the value has the wrong type return an invalid QDatetime
196      *
197      * \see valueAsTimeType()
198      */
199     static QDateTime timeValueAsDefinedDateTime( const QVariant &value );
200 
201   private:
202     QString mMeshLayerParameterName;
203     QString mDatasetGroupParameterName;
204 
205     static bool valueIsAcceptable( const QVariant &input, bool allowEmpty );
206 };
207 
208 #ifndef SIP_RUN
209 ///@cond PRIVATE
210 
211 /**
212  * \brief Parameter type definition for QgsProcessingParameterMeshDatasetTime.
213  *
214  * \ingroup core
215  * \since QGIS 3.18
216  */
217 class CORE_EXPORT QgsProcessingParameterTypeMeshDatasetTime: public QgsProcessingParameterType
218 {
219   public:
create(const QString & name)220     QgsProcessingParameterDefinition *create( const QString &name ) const override SIP_FACTORY
221     {
222       return new QgsProcessingParameterMeshDatasetTime( name );
223     }
224 
description()225     QString description() const override
226     {
227       return QCoreApplication::translate( "Processing", "An input allowing selection of dataset index from a mesh layer by time setting" );
228     }
229 
name()230     QString name() const override
231     {
232       return QCoreApplication::translate( "Processing", "Mesh Dataset Time" );
233     }
234 
id()235     QString id() const override
236     {
237       return QgsProcessingParameterMeshDatasetTime::typeName();
238     }
239 
pythonImportString()240     QString pythonImportString() const override
241     {
242       return QStringLiteral( "from qgis.core import QgsProcessingParameterMeshDatasetTime" );
243     }
244 
className()245     QString className() const override
246     {
247       return QStringLiteral( "QgsProcessingParameterMeshDatasetTime" );
248     }
249 
acceptedPythonTypes()250     QStringList acceptedPythonTypes() const override
251     {
252       return QStringList() << QObject::tr( "dict{}: dictionary, see QgsProcessingParameterMeshDatasetTime docs" );
253     }
254 };
255 
256 ///@endcond
257 #endif //SIP_RUN
258 
259 
260 #endif // QGSPROCESSINGPARAMETERMESHDATASET_H
261