QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 QgsMapLayer *mapLayer = nullptr;
41 QgsVectorLayer *vectorLayer = input.value<QgsVectorLayer *>();
42 if ( vectorLayer )
43 {
44 return vectorLayer->isSpatial();
45 }
46
47 if ( input.userType() == QMetaType::Type::QString )
48 {
49 if ( input.toString().isEmpty() )
51
52 if ( !context )
53 return true;
54
55 mapLayer = QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
56 return mapLayer && ( mapLayer->type() == Qgis::LayerType::Vector && mapLayer->isSpatial() );
57 }
58 else if ( input.userType() == QMetaType::Type::QVariantList )
59 {
60 if ( input.toList().isEmpty() )
62
63 const QVariantList layerList = input.toList();
64 for ( const QVariant &variantLayer : layerList )
65 {
66 vectorLayer = input.value<QgsVectorLayer *>();
67 if ( vectorLayer )
68 {
69 if ( vectorLayer->isSpatial() )
70 continue;
71 else
72 return false;
73 }
74
75 if ( variantLayer.userType() == QMetaType::Type::QString )
76 {
77 if ( !context )
78 return true;
79
80 mapLayer = QgsProcessingUtils::mapLayerFromString( variantLayer.toString(), *context );
81 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Vector || !mapLayer->isSpatial() )
82 return false;
83 }
84 else if ( variantLayer.userType() == QMetaType::Type::QVariantMap )
85 {
86 const QVariantMap layerMap = variantLayer.toMap();
87
88 if ( !layerMap.contains( QStringLiteral( "layer" ) ) &&
89 !layerMap.contains( QStringLiteral( "attributeIndex" ) ) &&
90 !layerMap.contains( QStringLiteral( "overriddenLayerName" ) ) &&
91 !layerMap.contains( QStringLiteral( "buildDataDefinedBlocks" ) ) &&
92 !layerMap.contains( QStringLiteral( "dataDefinedBlocksMaximumNumberOfClasses" ) ) )
93 return false;
94
95 if ( !context )
96 return true;
97
98 mapLayer = QgsProcessingUtils::mapLayerFromString( layerMap.value( QStringLiteral( "layer" ) ).toString(), *context );
99 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Vector || !mapLayer->isSpatial() )
100 return false;
101
102 vectorLayer = static_cast<QgsVectorLayer *>( mapLayer );
103
104 if ( !vectorLayer )
105 return false;
106
107 if ( layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt() >= vectorLayer->fields().count() )
108 return false;
109 }
110 else
111 {
112 return false;
113 }
114 }
115 return true;
116 }
117 else if ( input.userType() == QMetaType::Type::QStringList )
118 {
119 const auto constToStringList = input.toStringList();
120 if ( constToStringList.isEmpty() )
122
123 if ( !context )
124 return true;
125
126 for ( const QString &v : constToStringList )
127 {
128 mapLayer = QgsProcessingUtils::mapLayerFromString( v, *context );
129 if ( !mapLayer )
130 return false;
131
132 if ( mapLayer->type() == Qgis::LayerType::Vector && mapLayer->isSpatial() )
133 continue;
134 else
135 return false;
136 }
137 return true;
138 }
139
140 return false;
141}
142
144{
145 QStringList parts;
146 const QList<QgsDxfExport::DxfLayer> layers = parameterAsLayers( value, context );
147 for ( const QgsDxfExport::DxfLayer &layer : layers )
148 {
149 QStringList layerDefParts;
150 layerDefParts << QStringLiteral( "'layer': " ) + QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer.layer()->source() ) );
151
152 if ( layer.layerOutputAttributeIndex() >= -1 )
153 layerDefParts << QStringLiteral( "'attributeIndex': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.layerOutputAttributeIndex() );
154
155 layerDefParts << QStringLiteral( "'overriddenLayerName': " ) + QgsProcessingUtils::stringToPythonLiteral( layer.overriddenName() );
156
157 layerDefParts << QStringLiteral( "'buildDataDefinedBlocks': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.buildDataDefinedBlocks() );
158
159 layerDefParts << QStringLiteral( "'dataDefinedBlocksMaximumNumberOfClasses': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.dataDefinedBlocksMaximumNumberOfClasses() );
160
161 const QString layerDef = QStringLiteral( "{%1}" ).arg( layerDefParts.join( ',' ) );
162 parts << layerDef;
163 }
164 return parts.join( ',' ).prepend( '[' ).append( ']' );
165}
166
168{
169 switch ( outputType )
170 {
172 {
173 QString code = QStringLiteral( "QgsProcessingParameterDxfLayers('%1', %2)" )
175 return code;
176 }
177 }
178 return QString();
179}
180
181QString QgsProcessingParameterDxfLayers::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
182{
184}
185
186QVariant QgsProcessingParameterDxfLayers::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
187{
189}
190
191QList<QgsDxfExport::DxfLayer> QgsProcessingParameterDxfLayers::parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context )
192{
193 QList<QgsDxfExport::DxfLayer> layers;
194
195 if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layersVariant ) ) )
196 {
197 layers << QgsDxfExport::DxfLayer( layer );
198 }
199
200 if ( layersVariant.userType() == QMetaType::Type::QString )
201 {
202 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layersVariant.toString(), context );
203 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
204 }
205 else if ( layersVariant.userType() == QMetaType::Type::QVariantList )
206 {
207 const QVariantList layersVariantList = layersVariant.toList();
208 for ( const QVariant &layerItem : layersVariantList )
209 {
210 if ( layerItem.userType() == QMetaType::Type::QVariantMap )
211 {
212 const QVariantMap layerVariantMap = layerItem.toMap();
213 layers << variantMapAsLayer( layerVariantMap, context );
214 }
215 else if ( layerItem.userType() == QMetaType::Type::QString )
216 {
217 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem.toString(), context );
218 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
219 }
220 }
221 }
222 else if ( layersVariant.userType() == QMetaType::Type::QStringList )
223 {
224 const auto layersStringList = layersVariant.toStringList();
225 for ( const QString &layerItem : layersStringList )
226 {
227 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem, context );
228 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
229 }
230 }
231
232 return layers;
233}
234
236{
237 const QVariant layerVariant = layerVariantMap[ QStringLiteral( "layer" ) ];
238
239 QgsVectorLayer *inputLayer = nullptr;
240 if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layerVariant ) ) ) )
241 {
242 // good
243 }
244 else if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerVariant.toString(), context ) ) ) )
245 {
246 // good
247 }
248 else
249 {
250 // bad
251 }
252
253 QgsDxfExport::DxfLayer dxfLayer( inputLayer,
254 layerVariantMap[ QStringLiteral( "attributeIndex" ) ].toInt(),
255 layerVariantMap[ QStringLiteral( "buildDataDefinedBlocks" ) ].toBool(),
256 layerVariantMap[ QStringLiteral( "dataDefinedBlocksMaximumNumberOfClasses" ) ].toInt(),
257 layerVariantMap[ QStringLiteral( "overriddenLayerName" ) ].toString() );
258 return dxfLayer;
259}
260
262{
263 QVariantMap vm;
264 if ( !layer.layer() )
265 return vm;
266
267 vm[ QStringLiteral( "layer" )] = layer.layer()->id();
268 vm[ QStringLiteral( "attributeIndex" ) ] = layer.layerOutputAttributeIndex();
269 vm[ QStringLiteral( "overriddenLayerName" ) ] = layer.overriddenName();
270 vm[ QStringLiteral( "buildDataDefinedBlocks" ) ] = layer.buildDataDefinedBlocks();
271 vm[ QStringLiteral( "dataDefinedBlocksMaximumNumberOfClasses" ) ] = layer.dataDefinedBlocksMaximumNumberOfClasses();
272 return vm;
273}
@ Vector
Vector layer.
@ Optional
Parameter is optional.
int count
Definition qgsfields.h:50
Base class for all map layer types.
Definition qgsmaplayer.h:76
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
QString id
Definition qgsmaplayer.h:79
Qgis::LayerType type
Definition qgsmaplayer.h:86
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.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
Layers and optional attribute index to split into multiple layers using attribute value as layer name...
QString overriddenName() const
Returns the overridden layer name to be used in the exported DXF.
bool buildDataDefinedBlocks() const
Flag if data defined point block symbols should be created.
QgsVectorLayer * layer() const
Returns the layer.
int dataDefinedBlocksMaximumNumberOfClasses() const
Returns the maximum number of data defined symbol classes for which blocks are created.
int layerOutputAttributeIndex() const
Returns the attribute index used to split into multiple layers.