QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
Loading...
Searching...
No Matches
qgsprocessingparameterdxflayers.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingparameterdxflayers.cpp
3 ---------------------
4 Date : September 2020
5 Copyright : (C) 2020 by Alexander Bruy
6 Email : alexander dot bruy 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#include "qgsvectorlayer.h"
18
19
20QgsProcessingParameterDxfLayers::QgsProcessingParameterDxfLayers( const QString &name, const QString &description )
21 : QgsProcessingParameterDefinition( name, description, QVariant(), false )
22{
23}
24
29
31{
32 return typeName();
33}
34
36{
37 if ( !input.isValid() )
39
40 if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( input ) ) )
41 {
42 return true;
43 }
44
45 if ( input.type() == QVariant::String )
46 {
47 if ( input.toString().isEmpty() )
49
50 if ( !context )
51 return true;
52
53 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
54 return mapLayer && ( mapLayer->type() == Qgis::LayerType::Vector );
55 }
56 else if ( input.type() == QVariant::List )
57 {
58 if ( input.toList().isEmpty() )
60
61 const QVariantList layerList = input.toList();
62 for ( const QVariant &variantLayer : layerList )
63 {
64 if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( variantLayer ) ) )
65 continue;
66
67 if ( variantLayer.type() == QVariant::String )
68 {
69 if ( !context )
70 return true;
71
72 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( variantLayer.toString(), *context );
73 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Vector )
74 return false;
75 }
76 else if ( variantLayer.type() == QVariant::Map )
77 {
78 const QVariantMap layerMap = variantLayer.toMap();
79
80 if ( !layerMap.contains( QStringLiteral( "layer" ) ) && !layerMap.contains( QStringLiteral( "attributeIndex" ) ) )
81 return false;
82
83 if ( !context )
84 return true;
85
86 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerMap.value( QStringLiteral( "layer" ) ).toString(), *context );
87 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Vector )
88 return false;
89
90 QgsVectorLayer *vectorLayer = static_cast<QgsVectorLayer *>( mapLayer );
91
92 if ( !vectorLayer )
93 return false;
94
95 if ( layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt() >= vectorLayer->fields().count() )
96 return false;
97 }
98 else
99 {
100 return false;
101 }
102 }
103 return true;
104 }
105 else if ( input.type() == QVariant::StringList )
106 {
107 const auto constToStringList = input.toStringList();
108 if ( constToStringList.isEmpty() )
110
111 if ( !context )
112 return true;
113
114 for ( const QString &v : constToStringList )
115 {
116 if ( !QgsProcessingUtils::mapLayerFromString( v, *context ) )
117 return false;
118 }
119 return true;
120 }
121
122 return false;
123}
124
126{
127 QStringList parts;
128 const QList<QgsDxfExport::DxfLayer> layers = parameterAsLayers( value, context );
129 for ( const QgsDxfExport::DxfLayer &layer : layers )
130 {
131 QStringList layerDefParts;
132 layerDefParts << QStringLiteral( "'layer': " ) + QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer.layer()->source() ) );
133 if ( layer.layerOutputAttributeIndex() >= -1 )
134 layerDefParts << QStringLiteral( "'attributeIndex': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.layerOutputAttributeIndex() );
135
136 const QString layerDef = QStringLiteral( "{%1}" ).arg( layerDefParts.join( ',' ) );
137 parts << layerDef;
138 }
139 return parts.join( ',' ).prepend( '[' ).append( ']' );
140}
141
143{
144 switch ( outputType )
145 {
147 {
148 QString code = QStringLiteral( "QgsProcessingParameterDxfLayers('%1', %2)" )
150 return code;
151 }
152 }
153 return QString();
154}
155
156QString QgsProcessingParameterDxfLayers::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
157{
159}
160
161QVariant QgsProcessingParameterDxfLayers::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
162{
164}
165
166QList<QgsDxfExport::DxfLayer> QgsProcessingParameterDxfLayers::parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context )
167{
168 QList<QgsDxfExport::DxfLayer> layers;
169
170 if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layersVariant ) ) )
171 {
172 layers << QgsDxfExport::DxfLayer( layer );
173 }
174
175 if ( layersVariant.type() == QVariant::String )
176 {
177 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layersVariant.toString(), context );
178 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
179 }
180 else if ( layersVariant.type() == QVariant::List )
181 {
182 const QVariantList layersVariantList = layersVariant.toList();
183 for ( const QVariant &layerItem : layersVariantList )
184 {
185 if ( layerItem.type() == QVariant::Map )
186 {
187 const QVariantMap layerVariantMap = layerItem.toMap();
188 layers << variantMapAsLayer( layerVariantMap, context );
189 }
190 else if ( layerItem.type() == QVariant::String )
191 {
192 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem.toString(), context );
193 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
194 }
195 }
196 }
197 else if ( layersVariant.type() == QVariant::StringList )
198 {
199 const auto layersStringList = layersVariant.toStringList();
200 for ( const QString &layerItem : layersStringList )
201 {
202 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem, context );
203 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
204 }
205 }
206
207 return layers;
208}
209
211{
212 const QVariant layerVariant = layerVariantMap[ QStringLiteral( "layer" ) ];
213
214 QgsVectorLayer *inputLayer = nullptr;
215 if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layerVariant ) ) ) )
216 {
217 // good
218 }
219 else if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerVariant.toString(), context ) ) ) )
220 {
221 // good
222 }
223 else
224 {
225 // bad
226 }
227
228 QgsDxfExport::DxfLayer dxfLayer( inputLayer, layerVariantMap[ QStringLiteral( "attributeIndex" ) ].toInt() );
229 return dxfLayer;
230}
231
233{
234 QVariantMap vm;
235 if ( !layer.layer() )
236 return vm;
237
238 vm[ QStringLiteral( "layer" )] = layer.layer()->id();
239 vm[ QStringLiteral( "attributeIndex" ) ] = layer.layerOutputAttributeIndex();
240 return vm;
241}
@ Vector
Vector layer.
@ Optional
Parameter is optional.
int count() const
Returns number of items.
Base class for all map layer types.
Definition qgsmaplayer.h:75
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Qgis::LayerType type
Definition qgsmaplayer.h:82
Contains information about the context in which a processing algorithm is executed.
Base class for the definition of processing parameters.
QString valueAsStringPrivate(const QVariant &value, QgsProcessingContext &context, bool &ok, ValueAsStringFlags flags) const
Internal method for evaluating values as string.
Qgis::ProcessingParameterFlags mFlags
Parameter flags.
@ AllowMapLayerValues
Enable map layer value handling.
QString description() const
Returns the description for the parameter.
QString name() const
Returns the name of the parameter.
QVariant valueAsJsonObjectPrivate(const QVariant &value, QgsProcessingContext &context, ValueAsStringFlags flags) const
Internal method for evaluating values as JSON objects.
A parameter for Processing algorithms that need a list of input vector layers to export as DXF file -...
static QVariantMap layerAsVariantMap(const QgsDxfExport::DxfLayer &layer)
Converts a single input layer to QVariant representation (a QVariantMap)
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
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...
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...
QgsProcessingParameterDxfLayers(const QString &name, const QString &description=QString())
Constructor for QgsProcessingParameterDxfLayers.
QString type() const override
Unique parameter type name.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QgsDxfExport::DxfLayer variantMapAsLayer(const QVariantMap &layerVariantMap, QgsProcessingContext &context)
Converts a QVariant value (a QVariantMap) to a single input layer.
static QString typeName()
Returns the type name for the parameter class.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
static QList< QgsDxfExport::DxfLayer > parameterAsLayers(const QVariant &layersVariant, QgsProcessingContext &context)
Converts a QVariant value (a QVariantList) to a list of input layers.
QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const override
Returns a version of the parameter input value, which is suitable for use in a JSON object.
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
static QString normalizeLayerSource(const QString &source)
Normalizes a layer source string for safe comparison across different operating system environments.
static QString variantToPythonLiteral(const QVariant &value)
Converts a variant to a Python literal.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags=QgsProcessing::LayerOptionsFlags())
Interprets a string as a map layer within the supplied context.
PythonOutputType
Available Python output types.
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Layers and optional attribute index to split into multiple layers using attribute value as layer name...
QgsVectorLayer * layer() const
Returns the layer.
int layerOutputAttributeIndex() const
Returns the attribute index used to split into multiple layers.