QGIS API Documentation 3.39.0-Master (e8f1b343c48)
Loading...
Searching...
No Matches
qgsprocessingoutputs.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingoutputs.cpp
3 -------------------------
4 begin : May 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgsvariantutils.h"
20
21#include <QUrl>
22#include <QDir>
23
24QgsProcessingOutputDefinition::QgsProcessingOutputDefinition( const QString &name, const QString &description )
25 : mName( name )
26 , mDescription( description )
27{}
28
29QString QgsProcessingOutputDefinition::valueAsString( const QVariant &value, QgsProcessingContext &, bool &ok ) const
30{
31 if ( QgsVariantUtils::isNull( value ) )
32 {
33 ok = true;
34 return QObject::tr( "NULL" );
35 }
36
37 if ( value.userType() == QMetaType::Type::QString )
38 {
39 ok = true;
40 return value.toString();
41 }
42 ok = false;
43 return QString();
44}
45
46QString QgsProcessingOutputDefinition::valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
47{
48 return valueAsString( value, context, ok );
49}
50
52 : QgsProcessingOutputDefinition( name, description )
53 , mDataType( type )
54{}
55
60
65
66QgsProcessingOutputRasterLayer::QgsProcessingOutputRasterLayer( const QString &name, const QString &description )
67 : QgsProcessingOutputDefinition( name, description )
68{}
69
70QgsProcessingOutputPointCloudLayer::QgsProcessingOutputPointCloudLayer( const QString &name, const QString &description )
71 : QgsProcessingOutputDefinition( name, description )
72{}
73
74QgsProcessingOutputVectorTileLayer::QgsProcessingOutputVectorTileLayer( const QString &name, const QString &description )
75 : QgsProcessingOutputDefinition( name, description )
76{}
77
78QgsProcessingOutputHtml::QgsProcessingOutputHtml( const QString &name, const QString &description )
79 : QgsProcessingOutputDefinition( name, description )
80{}
81
82QString QgsProcessingOutputHtml::valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
83{
84 if ( value.userType() == QMetaType::Type::QString && !value.toString().isEmpty() )
85 {
86 ok = true;
87 return QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( value.toString() ).toString(), QDir::toNativeSeparators( value.toString() ) );
88 }
89
90 return valueAsString( value, context, ok );
91}
92
93QgsProcessingOutputNumber::QgsProcessingOutputNumber( const QString &name, const QString &description )
94 : QgsProcessingOutputDefinition( name, description )
95{}
96
97QString QgsProcessingOutputNumber::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
98{
99 ok = false;
100 switch ( value.userType() )
101 {
102 case QMetaType::Type::Int:
103 case QMetaType::Type::UInt:
104 case QMetaType::Type::LongLong:
105 case QMetaType::Type::ULongLong:
106 case QMetaType::Type::Double:
107 ok = true;
108 return value.toString();
109 default:
110 break;
111 }
112 return QgsProcessingOutputDefinition::valueAsString( value, context, ok );
113}
114
115QgsProcessingOutputString::QgsProcessingOutputString( const QString &name, const QString &description )
116 : QgsProcessingOutputDefinition( name, description )
117{}
118
119QgsProcessingOutputBoolean::QgsProcessingOutputBoolean( const QString &name, const QString &description )
120 : QgsProcessingOutputDefinition( name, description )
121{}
122
123QString QgsProcessingOutputBoolean::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
124{
125 ok = false;
126 if ( value.userType() == QMetaType::Type::Bool )
127 {
128 ok = true;
129 return value.toBool() ? QObject::tr( "True" ) : QObject::tr( "False" );
130 }
131 return QgsProcessingOutputDefinition::valueAsString( value, context, ok );
132}
133
134QgsProcessingOutputFolder::QgsProcessingOutputFolder( const QString &name, const QString &description )
135 : QgsProcessingOutputDefinition( name, description )
136{}
137
138QString QgsProcessingOutputFolder::valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
139{
140 if ( value.userType() == QMetaType::Type::QString && !value.toString().isEmpty() )
141 {
142 ok = true;
143 return QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( value.toString() ).toString(), QDir::toNativeSeparators( value.toString() ) );
144 }
145
146 return valueAsString( value, context, ok );
147}
148
149QgsProcessingOutputFile::QgsProcessingOutputFile( const QString &name, const QString &description )
150 : QgsProcessingOutputDefinition( name, description )
151{}
152
153QString QgsProcessingOutputFile::valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
154{
155 if ( value.userType() == QMetaType::Type::QString && !value.toString().isEmpty() )
156 {
157 ok = true;
158 return QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( value.toString() ).toString(), QDir::toNativeSeparators( value.toString() ) );
159 }
160
161 return valueAsString( value, context, ok );
162}
163
164QgsProcessingOutputMapLayer::QgsProcessingOutputMapLayer( const QString &name, const QString &description )
165 : QgsProcessingOutputDefinition( name, description )
166{}
167
169{
170 return typeName();
171}
172
173QgsProcessingOutputMultipleLayers::QgsProcessingOutputMultipleLayers( const QString &name, const QString &description )
174 : QgsProcessingOutputDefinition( name, description )
175{}
176
178{
179 return typeName();
180}
181
182QString QgsProcessingOutputMultipleLayers::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
183{
184 ok = false;
185 switch ( value.userType() )
186 {
187 case QMetaType::Type::QVariantList:
188 {
189 ok = true;
190 const QVariantList list = value.toList();
191
192 QStringList layerNames;
193 for ( const QVariant &v : list )
194 {
195 layerNames << v.toString();
196 }
197 return layerNames.join( QLatin1String( ", " ) );
198 }
199
200 case QMetaType::Type::QStringList:
201 {
202 ok = true;
203 const QStringList list = value.toStringList();
204 return list.join( QLatin1String( ", " ) );
205 }
206
207 default:
208 break;
209 }
210 return QgsProcessingOutputDefinition::valueAsString( value, context, ok );
211}
212
214 : QgsProcessingOutputDefinition( name, description )
215{}
216
217QgsProcessingOutputVariant::QgsProcessingOutputVariant( const QString &name, const QString &description )
218 : QgsProcessingOutputDefinition( name, description )
219{
220
221}
222
224{
225 return typeName();
226}
227
228QString QgsProcessingOutputVariant::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
229{
230 ok = false;
231 switch ( value.userType() )
232 {
233 case QMetaType::Type::Int:
234 case QMetaType::Type::UInt:
235 case QMetaType::Type::LongLong:
236 case QMetaType::Type::ULongLong:
237 case QMetaType::Type::Double:
238 ok = true;
239 return value.toString();
240 case QMetaType::Type::Bool:
241 ok = true;
242 return value.toBool() ? QObject::tr( "True" ) : QObject::tr( "False" );
243
244 case QMetaType::Type::QVariantList:
245 {
246 ok = true;
247 const QVariantList list = value.toList();
248
249 QStringList names;
250 for ( const QVariant &v : list )
251 {
252 names << v.toString();
253 }
254 return names.join( QLatin1String( ", " ) );
255 }
256
257 case QMetaType::Type::QStringList:
258 {
259 ok = true;
260 const QStringList list = value.toStringList();
261 return list.join( QLatin1String( ", " ) );
262 }
263
264 default:
265 break;
266 }
267 return QgsProcessingOutputDefinition::valueAsString( value, context, ok );
268}
ProcessingSourceType
Processing data source types.
Definition qgis.h:3242
Contains information about the context in which a processing algorithm is executed.
QgsProcessingOutputBoolean(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputNumber.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter output value (if possible).
QgsProcessingOutputConditionalBranch(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputConditionalBranch.
Base class for the definition of processing outputs.
virtual QString valueAsFormattedString(const QVariant &value, QgsProcessingContext &context, bool &ok) const
Returns a HTML string version of the parameter output value (if possible).
virtual QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const
Returns a string version of the parameter output value (if possible).
QgsProcessingOutputDefinition(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputDefinition.
QString valueAsFormattedString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a HTML string version of the parameter output value (if possible).
QgsProcessingOutputFile(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputFile.
QgsProcessingOutputFolder(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputFolder.
QString valueAsFormattedString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a HTML string version of the parameter output value (if possible).
QgsProcessingOutputHtml(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputHtml.
QString valueAsFormattedString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a HTML string version of the parameter output value (if possible).
static QString typeName()
Returns the type name for the output class.
QgsProcessingOutputMapLayer(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputMapLayer.
QString type() const override
Unique output type name.
static QString typeName()
Returns the type name for the output class.
QgsProcessingOutputMultipleLayers(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputMultipleLayers.
QString type() const override
Unique output type name.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter output value (if possible).
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter output value (if possible).
QgsProcessingOutputNumber(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputNumber.
QgsProcessingOutputPointCloudLayer(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputPointCloudLayer.
QgsProcessingOutputRasterLayer(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputRasterLayer.
QgsProcessingOutputString(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputString.
static QString typeName()
Returns the type name for the output class.
QgsProcessingOutputVariant(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputVariant.
QString type() const override
Unique output type name.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter output value (if possible).
QgsProcessingOutputVectorLayer(const QString &name, const QString &description=QString(), Qgis::ProcessingSourceType type=Qgis::ProcessingSourceType::VectorAnyGeometry)
Constructor for QgsProcessingOutputVectorLayer.
QString type() const override
Unique output type name.
void setDataType(Qgis::ProcessingSourceType type)
Sets the layer type for the output layer.
Qgis::ProcessingSourceType dataType() const
Returns the layer type for the output layer.
QgsProcessingOutputVectorTileLayer(const QString &name, const QString &description=QString())
Constructor for QgsProcessingOutputVectorTileLayer.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.