QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
27
32
34{
35 return typeName();
36}
37
39{
40 if ( !input.isValid() )
42
43 QgsMapLayer *mapLayer = nullptr;
44 QgsVectorLayer *vectorLayer = input.value<QgsVectorLayer *>();
45 if ( vectorLayer )
46 {
47 return vectorLayer->isSpatial();
48 }
49
50 if ( input.userType() == QMetaType::Type::QString )
51 {
52 if ( input.toString().isEmpty() )
54
55 if ( !context )
56 return true;
57
58 mapLayer = QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
59 return mapLayer && ( mapLayer->type() == Qgis::LayerType::Vector && mapLayer->isSpatial() );
60 }
61 else if ( input.userType() == QMetaType::Type::QVariantList )
62 {
63 if ( input.toList().isEmpty() )
65
66 const QVariantList layerList = input.toList();
67 for ( const QVariant &variantLayer : layerList )
68 {
69 vectorLayer = input.value<QgsVectorLayer *>();
70 if ( vectorLayer )
71 {
72 if ( vectorLayer->isSpatial() )
73 continue;
74 else
75 return false;
76 }
77
78 if ( variantLayer.userType() == QMetaType::Type::QString )
79 {
80 if ( !context )
81 return true;
82
83 mapLayer = QgsProcessingUtils::mapLayerFromString( variantLayer.toString(), *context );
84 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Vector || !mapLayer->isSpatial() )
85 return false;
86 }
87 else if ( variantLayer.userType() == QMetaType::Type::QVariantMap )
88 {
89 const QVariantMap layerMap = variantLayer.toMap();
90
91 if ( !layerMap.contains( u"layer"_s )
92 && !layerMap.contains( u"attributeIndex"_s )
93 && !layerMap.contains( u"overriddenLayerName"_s )
94 && !layerMap.contains( u"buildDataDefinedBlocks"_s )
95 && !layerMap.contains( u"dataDefinedBlocksMaximumNumberOfClasses"_s ) )
96 return false;
97
98 if ( !context )
99 return true;
100
101 vectorLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerMap.value( u"layer"_s ).toString(), *context ) );
102 if ( !vectorLayer || !vectorLayer->isSpatial() )
103 return false;
104
105 if ( layerMap.value( u"attributeIndex"_s ).toInt() >= vectorLayer->fields().count() )
106 return false;
107 }
108 else
109 {
110 return false;
111 }
112 }
113 return true;
114 }
115 else if ( input.userType() == QMetaType::Type::QStringList )
116 {
117 const auto constToStringList = input.toStringList();
118 if ( constToStringList.isEmpty() )
120
121 if ( !context )
122 return true;
123
124 for ( const QString &v : constToStringList )
125 {
126 mapLayer = QgsProcessingUtils::mapLayerFromString( v, *context );
127 if ( !mapLayer )
128 return false;
129
130 if ( mapLayer->type() == Qgis::LayerType::Vector && mapLayer->isSpatial() )
131 continue;
132 else
133 return false;
134 }
135 return true;
136 }
137
138 return false;
139}
140
142{
143 QStringList parts;
144 const QList<QgsDxfExport::DxfLayer> layers = parameterAsLayers( value, context );
145 for ( const QgsDxfExport::DxfLayer &layer : layers )
146 {
147 QStringList layerDefParts;
148 layerDefParts << u"'layer': "_s + QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer.layer()->source() ) );
149
150 if ( layer.layerOutputAttributeIndex() >= -1 )
151 layerDefParts << u"'attributeIndex': "_s + QgsProcessingUtils::variantToPythonLiteral( layer.layerOutputAttributeIndex() );
152
153 layerDefParts << u"'overriddenLayerName': "_s + QgsProcessingUtils::stringToPythonLiteral( layer.overriddenName() );
154
155 layerDefParts << u"'buildDataDefinedBlocks': "_s + QgsProcessingUtils::variantToPythonLiteral( layer.buildDataDefinedBlocks() );
156
157 layerDefParts << u"'dataDefinedBlocksMaximumNumberOfClasses': "_s + QgsProcessingUtils::variantToPythonLiteral( layer.dataDefinedBlocksMaximumNumberOfClasses() );
158
159 const QString layerDef = u"{%1}"_s.arg( layerDefParts.join( ',' ) );
160 parts << layerDef;
161 }
162 return parts.join( ',' ).prepend( '[' ).append( ']' );
163}
164
166{
167 switch ( outputType )
168 {
170 {
171 QString code = u"QgsProcessingParameterDxfLayers('%1', %2)"_s.arg( name(), QgsProcessingUtils::stringToPythonLiteral( description() ) );
172 return code;
173 }
174 }
175 return QString();
176}
177
178QString QgsProcessingParameterDxfLayers::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
179{
181}
182
183QVariant QgsProcessingParameterDxfLayers::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
184{
186}
187
188QList<QgsDxfExport::DxfLayer> QgsProcessingParameterDxfLayers::parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context )
189{
190 QList<QgsDxfExport::DxfLayer> layers;
191
192 if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layersVariant ) ) )
193 {
194 layers << QgsDxfExport::DxfLayer( layer );
195 }
196
197 if ( layersVariant.userType() == QMetaType::Type::QString )
198 {
199 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layersVariant.toString(), context );
200 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
201 }
202 else if ( layersVariant.userType() == QMetaType::Type::QVariantList )
203 {
204 const QVariantList layersVariantList = layersVariant.toList();
205 for ( const QVariant &layerItem : layersVariantList )
206 {
207 if ( layerItem.userType() == QMetaType::Type::QVariantMap )
208 {
209 const QVariantMap layerVariantMap = layerItem.toMap();
210 layers << variantMapAsLayer( layerVariantMap, context );
211 }
212 else if ( layerItem.userType() == QMetaType::Type::QString )
213 {
214 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem.toString(), context );
215 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
216 }
217 }
218 }
219 else if ( layersVariant.userType() == QMetaType::Type::QStringList )
220 {
221 const auto layersStringList = layersVariant.toStringList();
222 for ( const QString &layerItem : layersStringList )
223 {
224 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem, context );
225 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
226 }
227 }
228
229 return layers;
230}
231
233{
234 const QVariant layerVariant = layerVariantMap[u"layer"_s];
235
236 QgsVectorLayer *inputLayer = nullptr;
237 if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layerVariant ) ) ) )
238 {
239 // good
240 }
241 else if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerVariant.toString(), context ) ) ) )
242 {
243 // good
244 }
245 else
246 {
247 // bad
248 }
249
250 QgsDxfExport::DxfLayer dxfLayer(
251 inputLayer,
252 layerVariantMap[u"attributeIndex"_s].toInt(),
253 layerVariantMap[u"buildDataDefinedBlocks"_s].toBool(),
254 layerVariantMap[u"dataDefinedBlocksMaximumNumberOfClasses"_s].toInt(),
255 layerVariantMap[u"overriddenLayerName"_s].toString()
256 );
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:207
@ Optional
Parameter is optional.
Definition qgis.h:3882
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...
Encapsulates the properties of a vector layer containing features that will be exported to the DXF fi...
QString overriddenName() const
Returns the overridden layer name to be used in the exported DXF.
bool buildDataDefinedBlocks() const
Returns true if data defined point block symbols should be created.
QgsVectorLayer * layer() const
Returns the source vector 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 the source layer's features into multiple exported DXF laye...