QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
18#include "qgsvectorlayer.h"
19
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 vectorLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerMap.value( QStringLiteral( "layer" ) ).toString(), *context ) );
99 if ( !vectorLayer || !vectorLayer->isSpatial() )
100 return false;
101
102 if ( layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt() >= vectorLayer->fields().count() )
103 return false;
104 }
105 else
106 {
107 return false;
108 }
109 }
110 return true;
111 }
112 else if ( input.userType() == QMetaType::Type::QStringList )
113 {
114 const auto constToStringList = input.toStringList();
115 if ( constToStringList.isEmpty() )
117
118 if ( !context )
119 return true;
120
121 for ( const QString &v : constToStringList )
122 {
123 mapLayer = QgsProcessingUtils::mapLayerFromString( v, *context );
124 if ( !mapLayer )
125 return false;
126
127 if ( mapLayer->type() == Qgis::LayerType::Vector && mapLayer->isSpatial() )
128 continue;
129 else
130 return false;
131 }
132 return true;
133 }
134
135 return false;
136}
137
139{
140 QStringList parts;
141 const QList<QgsDxfExport::DxfLayer> layers = parameterAsLayers( value, context );
142 for ( const QgsDxfExport::DxfLayer &layer : layers )
143 {
144 QStringList layerDefParts;
145 layerDefParts << QStringLiteral( "'layer': " ) + QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer.layer()->source() ) );
146
147 if ( layer.layerOutputAttributeIndex() >= -1 )
148 layerDefParts << QStringLiteral( "'attributeIndex': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.layerOutputAttributeIndex() );
149
150 layerDefParts << QStringLiteral( "'overriddenLayerName': " ) + QgsProcessingUtils::stringToPythonLiteral( layer.overriddenName() );
151
152 layerDefParts << QStringLiteral( "'buildDataDefinedBlocks': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.buildDataDefinedBlocks() );
153
154 layerDefParts << QStringLiteral( "'dataDefinedBlocksMaximumNumberOfClasses': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.dataDefinedBlocksMaximumNumberOfClasses() );
155
156 const QString layerDef = QStringLiteral( "{%1}" ).arg( layerDefParts.join( ',' ) );
157 parts << layerDef;
158 }
159 return parts.join( ',' ).prepend( '[' ).append( ']' );
160}
161
163{
164 switch ( outputType )
165 {
167 {
168 QString code = QStringLiteral( "QgsProcessingParameterDxfLayers('%1', %2)" )
170 return code;
171 }
172 }
173 return QString();
174}
175
176QString QgsProcessingParameterDxfLayers::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
177{
179}
180
181QVariant QgsProcessingParameterDxfLayers::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
182{
184}
185
186QList<QgsDxfExport::DxfLayer> QgsProcessingParameterDxfLayers::parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context )
187{
188 QList<QgsDxfExport::DxfLayer> layers;
189
190 if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layersVariant ) ) )
191 {
192 layers << QgsDxfExport::DxfLayer( layer );
193 }
194
195 if ( layersVariant.userType() == QMetaType::Type::QString )
196 {
197 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layersVariant.toString(), context );
198 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
199 }
200 else if ( layersVariant.userType() == QMetaType::Type::QVariantList )
201 {
202 const QVariantList layersVariantList = layersVariant.toList();
203 for ( const QVariant &layerItem : layersVariantList )
204 {
205 if ( layerItem.userType() == QMetaType::Type::QVariantMap )
206 {
207 const QVariantMap layerVariantMap = layerItem.toMap();
208 layers << variantMapAsLayer( layerVariantMap, context );
209 }
210 else if ( layerItem.userType() == QMetaType::Type::QString )
211 {
212 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem.toString(), context );
213 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
214 }
215 }
216 }
217 else if ( layersVariant.userType() == QMetaType::Type::QStringList )
218 {
219 const auto layersStringList = layersVariant.toStringList();
220 for ( const QString &layerItem : layersStringList )
221 {
222 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem, context );
223 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
224 }
225 }
226
227 return layers;
228}
229
231{
232 const QVariant layerVariant = layerVariantMap[ QStringLiteral( "layer" ) ];
233
234 QgsVectorLayer *inputLayer = nullptr;
235 if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layerVariant ) ) ) )
236 {
237 // good
238 }
239 else if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerVariant.toString(), context ) ) ) )
240 {
241 // good
242 }
243 else
244 {
245 // bad
246 }
247
248 QgsDxfExport::DxfLayer dxfLayer( inputLayer,
249 layerVariantMap[ QStringLiteral( "attributeIndex" ) ].toInt(),
250 layerVariantMap[ QStringLiteral( "buildDataDefinedBlocks" ) ].toBool(),
251 layerVariantMap[ QStringLiteral( "dataDefinedBlocksMaximumNumberOfClasses" ) ].toInt(),
252 layerVariantMap[ QStringLiteral( "overriddenLayerName" ) ].toString() );
253 return dxfLayer;
254}
255
257{
258 QVariantMap vm;
259 if ( !layer.layer() )
260 return vm;
261
262 vm[ QStringLiteral( "layer" )] = layer.layer()->id();
263 vm[ QStringLiteral( "attributeIndex" ) ] = layer.layerOutputAttributeIndex();
264 vm[ QStringLiteral( "overriddenLayerName" ) ] = layer.overriddenName();
265 vm[ QStringLiteral( "buildDataDefinedBlocks" ) ] = layer.buildDataDefinedBlocks();
266 vm[ QStringLiteral( "dataDefinedBlocksMaximumNumberOfClasses" ) ] = layer.dataDefinedBlocksMaximumNumberOfClasses();
267 return vm;
268}
@ Vector
Vector layer.
Definition qgis.h:191
@ Optional
Parameter is optional.
Definition qgis.h:3765
int count
Definition qgsfields.h:50
Base class for all map layer types.
Definition qgsmaplayer.h:80
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:83
Qgis::LayerType type
Definition qgsmaplayer.h:90
Contains information about the context in which a processing algorithm is executed.
QString valueAsStringPrivate(const QVariant &value, QgsProcessingContext &context, bool &ok, ValueAsStringFlags flags) const
Internal method for evaluating values as string.
Qgis::ProcessingParameterFlags mFlags
Parameter flags.
QString description() const
Returns the description for the parameter.
QgsProcessingParameterDefinition(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, const QString &help=QString())
Constructor for QgsProcessingParameterDefinition.
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.
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 dataset.
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.