QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
20 QgsProcessingParameterDxfLayers::QgsProcessingParameterDxfLayers( const QString &name, const QString &description )
21  : QgsProcessingParameterDefinition( name, description, QVariant(), false )
22 {
23 }
24 
26 {
27  return new QgsProcessingParameterDxfLayers( *this );
28 }
29 
31 {
32  return typeName();
33 }
34 
36 {
37  if ( !input.isValid() )
38  return mFlags & FlagOptional;
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() )
48  return mFlags & FlagOptional;
49 
50  if ( !context )
51  return true;
52 
53  QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
54  return mapLayer && ( mapLayer->type() == QgsMapLayerType::VectorLayer );
55  }
56  else if ( input.type() == QVariant::List )
57  {
58  if ( input.toList().isEmpty() )
59  return mFlags & FlagOptional;;
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() != QgsMapLayerType::VectorLayer )
74  return false;
75  }
76  else if ( variantLayer.type() == QVariant::Map )
77  {
78  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() != QgsMapLayerType::VectorLayer )
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() )
109  return mFlags & FlagOptional;
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 
125 QString QgsProcessingParameterDxfLayers::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
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  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')" ).arg( name(), description() );
149  return code;
150  }
151  }
152  return QString();
153 }
154 
155 QList<QgsDxfExport::DxfLayer> QgsProcessingParameterDxfLayers::parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context )
156 {
157  QList<QgsDxfExport::DxfLayer> layers;
158 
159  if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layersVariant ) ) )
160  {
161  layers << QgsDxfExport::DxfLayer( layer );
162  }
163 
164  if ( layersVariant.type() == QVariant::String )
165  {
166  QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layersVariant.toString(), context );
167  layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
168  }
169  else if ( layersVariant.type() == QVariant::List )
170  {
171  const QVariantList layersVariantList = layersVariant.toList();
172  for ( const QVariant &layerItem : layersVariantList )
173  {
174  if ( layerItem.type() == QVariant::Map )
175  {
176  QVariantMap layerVariantMap = layerItem.toMap();
177  layers << variantMapAsLayer( layerVariantMap, context );
178  }
179  else if ( layerItem.type() == QVariant::String )
180  {
181  QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem.toString(), context );
182  layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
183  }
184  }
185  }
186  else if ( layersVariant.type() == QVariant::StringList )
187  {
188  const auto layersStringList = layersVariant.toStringList();
189  for ( const QString &layerItem : layersStringList )
190  {
191  QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem, context );
192  layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
193  }
194  }
195 
196  return layers;
197 }
198 
200 {
201  QVariant layerVariant = layerVariantMap[ QStringLiteral( "layer" ) ];
202 
203  QgsVectorLayer *inputLayer = nullptr;
204  if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layerVariant ) ) ) )
205  {
206  // good
207  }
208  else if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerVariant.toString(), context ) ) ) )
209  {
210  // good
211  }
212  else
213  {
214  // bad
215  }
216 
217  QgsDxfExport::DxfLayer dxfLayer( inputLayer, layerVariantMap[ QStringLiteral( "attributeIndex" ) ].toInt() );
218  return dxfLayer;
219 }
220 
222 {
223  QVariantMap vm;
224  if ( !layer.layer() )
225  return vm;
226 
227  vm[ QStringLiteral( "layer" )] = layer.layer()->id();
228  vm[ QStringLiteral( "attributeIndex" ) ] = layer.layerOutputAttributeIndex();
229  return vm;
230 }
int count() const
Returns number of items.
Definition: qgsfields.cpp:133
Base class for all map layer types.
Definition: qgsmaplayer.h:70
QgsMapLayerType type
Definition: qgsmaplayer.h:77
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Contains information about the context in which a processing algorithm is executed.
Base class for the definition of processing parameters.
QString description() const
Returns the description for the parameter.
QString name() const
Returns the name of the parameter.
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...
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.
static QList< QgsDxfExport::DxfLayer > parameterAsLayers(const QVariant &layersVariant, QgsProcessingContext &context)
Converts a QVariant value (a QVariantList) to a list of input layers.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
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)
Interprets a string as a map layer within the supplied context.
PythonOutputType
Available Python output types.
Definition: qgsprocessing.h:60
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
Definition: qgsprocessing.h:61
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...
Definition: qgsdxfexport.h:73
QgsVectorLayer * layer() const
Returns the layer.
Definition: qgsdxfexport.h:80
int layerOutputAttributeIndex() const
Returns the attribute index used to split into multiple layers.
Definition: qgsdxfexport.h:87