QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
28
33
35{
36 return typeName();
37}
38
40{
41 if ( !input.isValid() )
43
44 QgsMapLayer *mapLayer = nullptr;
45 QgsVectorLayer *vectorLayer = input.value<QgsVectorLayer *>();
46 if ( vectorLayer )
47 {
48 return vectorLayer->isSpatial();
49 }
50
51 if ( input.userType() == QMetaType::Type::QString )
52 {
53 if ( input.toString().isEmpty() )
55
56 if ( !context )
57 return true;
58
59 mapLayer = QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
60 return mapLayer && ( mapLayer->type() == Qgis::LayerType::Vector && mapLayer->isSpatial() );
61 }
62 else if ( input.userType() == QMetaType::Type::QVariantList )
63 {
64 if ( input.toList().isEmpty() )
66
67 const QVariantList layerList = input.toList();
68 for ( const QVariant &variantLayer : layerList )
69 {
70 vectorLayer = input.value<QgsVectorLayer *>();
71 if ( vectorLayer )
72 {
73 if ( vectorLayer->isSpatial() )
74 continue;
75 else
76 return false;
77 }
78
79 if ( variantLayer.userType() == QMetaType::Type::QString )
80 {
81 if ( !context )
82 return true;
83
84 mapLayer = QgsProcessingUtils::mapLayerFromString( variantLayer.toString(), *context );
85 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Vector || !mapLayer->isSpatial() )
86 return false;
87 }
88 else if ( variantLayer.userType() == QMetaType::Type::QVariantMap )
89 {
90 const QVariantMap layerMap = variantLayer.toMap();
91
92 if ( !layerMap.contains( u"layer"_s ) &&
93 !layerMap.contains( u"attributeIndex"_s ) &&
94 !layerMap.contains( u"overriddenLayerName"_s ) &&
95 !layerMap.contains( u"buildDataDefinedBlocks"_s ) &&
96 !layerMap.contains( u"dataDefinedBlocksMaximumNumberOfClasses"_s ) )
97 return false;
98
99 if ( !context )
100 return true;
101
102 vectorLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerMap.value( u"layer"_s ).toString(), *context ) );
103 if ( !vectorLayer || !vectorLayer->isSpatial() )
104 return false;
105
106 if ( layerMap.value( u"attributeIndex"_s ).toInt() >= vectorLayer->fields().count() )
107 return false;
108 }
109 else
110 {
111 return false;
112 }
113 }
114 return true;
115 }
116 else if ( input.userType() == QMetaType::Type::QStringList )
117 {
118 const auto constToStringList = input.toStringList();
119 if ( constToStringList.isEmpty() )
121
122 if ( !context )
123 return true;
124
125 for ( const QString &v : constToStringList )
126 {
127 mapLayer = QgsProcessingUtils::mapLayerFromString( v, *context );
128 if ( !mapLayer )
129 return false;
130
131 if ( mapLayer->type() == Qgis::LayerType::Vector && mapLayer->isSpatial() )
132 continue;
133 else
134 return false;
135 }
136 return true;
137 }
138
139 return false;
140}
141
143{
144 QStringList parts;
145 const QList<QgsDxfExport::DxfLayer> layers = parameterAsLayers( value, context );
146 for ( const QgsDxfExport::DxfLayer &layer : layers )
147 {
148 QStringList layerDefParts;
149 layerDefParts << u"'layer': "_s + QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer.layer()->source() ) );
150
151 if ( layer.layerOutputAttributeIndex() >= -1 )
152 layerDefParts << u"'attributeIndex': "_s + QgsProcessingUtils::variantToPythonLiteral( layer.layerOutputAttributeIndex() );
153
154 layerDefParts << u"'overriddenLayerName': "_s + QgsProcessingUtils::stringToPythonLiteral( layer.overriddenName() );
155
156 layerDefParts << u"'buildDataDefinedBlocks': "_s + QgsProcessingUtils::variantToPythonLiteral( layer.buildDataDefinedBlocks() );
157
158 layerDefParts << u"'dataDefinedBlocksMaximumNumberOfClasses': "_s + QgsProcessingUtils::variantToPythonLiteral( layer.dataDefinedBlocksMaximumNumberOfClasses() );
159
160 const QString layerDef = u"{%1}"_s.arg( layerDefParts.join( ',' ) );
161 parts << layerDef;
162 }
163 return parts.join( ',' ).prepend( '[' ).append( ']' );
164}
165
167{
168 switch ( outputType )
169 {
171 {
172 QString code = u"QgsProcessingParameterDxfLayers('%1', %2)"_s
174 return code;
175 }
176 }
177 return QString();
178}
179
180QString QgsProcessingParameterDxfLayers::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
181{
183}
184
185QVariant QgsProcessingParameterDxfLayers::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
186{
188}
189
190QList<QgsDxfExport::DxfLayer> QgsProcessingParameterDxfLayers::parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context )
191{
192 QList<QgsDxfExport::DxfLayer> layers;
193
194 if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layersVariant ) ) )
195 {
196 layers << QgsDxfExport::DxfLayer( layer );
197 }
198
199 if ( layersVariant.userType() == QMetaType::Type::QString )
200 {
201 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layersVariant.toString(), context );
202 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
203 }
204 else if ( layersVariant.userType() == QMetaType::Type::QVariantList )
205 {
206 const QVariantList layersVariantList = layersVariant.toList();
207 for ( const QVariant &layerItem : layersVariantList )
208 {
209 if ( layerItem.userType() == QMetaType::Type::QVariantMap )
210 {
211 const QVariantMap layerVariantMap = layerItem.toMap();
212 layers << variantMapAsLayer( layerVariantMap, context );
213 }
214 else if ( layerItem.userType() == QMetaType::Type::QString )
215 {
216 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem.toString(), context );
217 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
218 }
219 }
220 }
221 else if ( layersVariant.userType() == QMetaType::Type::QStringList )
222 {
223 const auto layersStringList = layersVariant.toStringList();
224 for ( const QString &layerItem : layersStringList )
225 {
226 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem, context );
227 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
228 }
229 }
230
231 return layers;
232}
233
235{
236 const QVariant layerVariant = layerVariantMap[ u"layer"_s ];
237
238 QgsVectorLayer *inputLayer = nullptr;
239 if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layerVariant ) ) ) )
240 {
241 // good
242 }
243 else if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerVariant.toString(), context ) ) ) )
244 {
245 // good
246 }
247 else
248 {
249 // bad
250 }
251
252 QgsDxfExport::DxfLayer dxfLayer( inputLayer,
253 layerVariantMap[ u"attributeIndex"_s ].toInt(),
254 layerVariantMap[ u"buildDataDefinedBlocks"_s ].toBool(),
255 layerVariantMap[ u"dataDefinedBlocksMaximumNumberOfClasses"_s ].toInt(),
256 layerVariantMap[ u"overriddenLayerName"_s ].toString() );
257 return dxfLayer;
258}
259
261{
262 QVariantMap vm;
263 if ( !layer.layer() )
264 return vm;
265
266 vm[ u"layer"_s] = layer.layer()->id();
267 vm[ u"attributeIndex"_s ] = layer.layerOutputAttributeIndex();
268 vm[ u"overriddenLayerName"_s ] = layer.overriddenName();
269 vm[ u"buildDataDefinedBlocks"_s ] = layer.buildDataDefinedBlocks();
270 vm[ u"dataDefinedBlocksMaximumNumberOfClasses"_s ] = layer.dataDefinedBlocksMaximumNumberOfClasses();
271 return vm;
272}
@ Vector
Vector layer.
Definition qgis.h:194
@ Optional
Parameter is optional.
Definition qgis.h:3836
int count
Definition qgsfields.h:50
Base class for all map layer types.
Definition qgsmaplayer.h:83
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:86
Qgis::LayerType type
Definition qgsmaplayer.h:93
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.