Skip to content
Permalink
Browse files

[processing] new parameter for DXF export input layers

  • Loading branch information
alexbruy committed Nov 2, 2020
1 parent 45f12cb commit b7997e0fd866860084c2eb5fc658b5b6caa9149b
@@ -0,0 +1,112 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/processing/qgsprocessingparameterdxflayers.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/



class QgsProcessingParameterDxfLayers : QgsProcessingParameterDefinition
{
%Docstring
A parameter for Processing algorithms that need a list of input vector
layers to export as DXF file - this parameter provides Processing
framework's adapter for QList<QgsDxfExport.DxfLayer>.

A valid value for this parameter is a list (QVariantList), where each
item is a map (QVariantMap) in this form:
{
'layer': string or QgsMapLayer,
'attributeIndex': int
}

Static functions :py:func:`~parametersAsLayers`, :py:func:`~variantMapAsLayer`,
:py:func:`~layerAsVariantMap` provide conversion between QgsDxfExport.DxfLayer
representation and QVariant representation.

.. note::

This class is not a part of public API.

.. versionadded:: 3.18
%End

%TypeHeaderCode
#include "qgsprocessingparameterdxflayers.h"
%End
public:
QgsProcessingParameterDxfLayers( const QString &name, const QString &description = QString() );
%Docstring
Constructor for QgsProcessingParameterDxfLayers.
%End

virtual QgsProcessingParameterDefinition *clone() const;

virtual QString type() const;

virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;

virtual QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const;


static QString typeName();
%Docstring
Returns the type name for the parameter class.
%End
static QList<QgsDxfExport::DxfLayer> parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context );
%Docstring
Converts a QVariant value (a QVariantList) to a list of input layers
%End
static QgsDxfExport::DxfLayer variantMapAsLayer( const QVariantMap &layerVariantMap, QgsProcessingContext &context );
%Docstring
Converts a QVariant value (a QVariantMap) to a single input layer
%End
static QVariantMap layerAsVariantMap( const QgsDxfExport::DxfLayer &layer );
%Docstring
Converts a single input layer to QVariant representation (a QVariantMap)
%End
};


class QgsProcessingParameterTypeDxfLayers : QgsProcessingParameterType
{
%Docstring
Parameter type definition for QgsProcessingParameterDxfLayers.

.. note::

This class is not a part of public API.

.. versionadded:: 3.18
%End

%TypeHeaderCode
#include "qgsprocessingparameterdxflayers.h"
%End
public:
virtual QgsProcessingParameterDefinition *create( const QString &name ) const /Factory/;

virtual QString description() const;

virtual QString name() const;

virtual QString id() const;

virtual QString pythonImportString() const;

virtual QString className() const;

virtual QStringList acceptedPythonTypes() const;
};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/processing/qgsprocessingparameterdxflayers.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -474,6 +474,7 @@
%Include auto_generated/processing/qgsprocessingfeedback.sip
%Include auto_generated/processing/qgsprocessingoutputs.sip
%Include auto_generated/processing/qgsprocessingparameteraggregate.sip
%Include auto_generated/processing/qgsprocessingparameterdxflayers.sip
%Include auto_generated/processing/qgsprocessingparameterfieldmap.sip
%Include auto_generated/processing/qgsprocessingparameters.sip
%Include auto_generated/processing/qgsprocessingparametertininputlayers.sip
@@ -161,6 +161,7 @@ SET(QGIS_CORE_SRCS
processing/qgsprocessingfeedback.cpp
processing/qgsprocessingoutputs.cpp
processing/qgsprocessingparameteraggregate.cpp
processing/qgsprocessingparameterdxflayers.cpp
processing/qgsprocessingparameterfieldmap.cpp
processing/qgsprocessingparameters.cpp
processing/qgsprocessingparametertininputlayers.cpp
@@ -1353,6 +1354,7 @@ SET(QGIS_CORE_HDRS
processing/qgsprocessingfeedback.h
processing/qgsprocessingoutputs.h
processing/qgsprocessingparameteraggregate.h
processing/qgsprocessingparameterdxflayers.h
processing/qgsprocessingparameterfieldmap.h
processing/qgsprocessingparameters.h
processing/qgsprocessingparametertininputlayers.h
@@ -0,0 +1,149 @@
/***************************************************************************
qgsprocessingparameterdxflayers.cpp
---------------------
Date : September 2020
Copyright : (C) 2020 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsprocessingparameterdxflayers.h"
#include "qgsvectorlayer.h"


QgsProcessingParameterDxfLayers::QgsProcessingParameterDxfLayers( const QString &name, const QString &description )
: QgsProcessingParameterDefinition( name, description, QVariant(), false )
{
}

QgsProcessingParameterDefinition *QgsProcessingParameterDxfLayers::clone() const
{
return new QgsProcessingParameterDxfLayers( *this );
}

QString QgsProcessingParameterDxfLayers::type() const
{
return typeName();
}

bool QgsProcessingParameterDxfLayers::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context ) const
{
if ( !input.isValid() )
return mFlags & FlagOptional;

if ( input.type() != QVariant::List )
return false;

const QVariantList layerList = input.toList();
if ( layerList.isEmpty() )
return false;

for ( const QVariant &variantLayer : layerList )
{
if ( variantLayer.type() != QVariant::Map )
return false;

QVariantMap layerMap = variantLayer.toMap();

if ( !layerMap.contains( QStringLiteral( "layer" ) ) || !layerMap.contains( QStringLiteral( "attributeIndex" ) ) )
return false;

if ( !context )
continue; // when called without context, we will skip checking whether the layer can be resolved

QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerMap.value( QStringLiteral( "layer" ) ).toString(), *context );
if ( !mapLayer || mapLayer->type() != QgsMapLayerType::VectorLayer )
return false;

QgsVectorLayer *vectorLayer = static_cast<QgsVectorLayer *>( mapLayer );

if ( !vectorLayer )
return false;

if ( layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt() >= vectorLayer->fields().count() )
return false;
}

return true;
}

QString QgsProcessingParameterDxfLayers::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
{
QStringList parts;
const QList<QgsDxfExport::DxfLayer> layers = parameterAsLayers( value, context );
for ( const QgsDxfExport::DxfLayer &layer : layers )
{
QStringList layerDefParts;
layerDefParts << QStringLiteral( "'layer': " ) + QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer.layer()->source() ) );
if ( layer.layerOutputAttributeIndex() >= -1 )
layerDefParts << QStringLiteral( "'attributeIndex': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.layerOutputAttributeIndex() );

QString layerDef = QStringLiteral( "{%1}" ).arg( layerDefParts.join( ',' ) );
parts << layerDef;
}
return parts.join( ',' ).prepend( '[' ).append( ']' );
}

QString QgsProcessingParameterDxfLayers::asPythonString( QgsProcessing::PythonOutputType outputType ) const
{
switch ( outputType )
{
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
{
QString code = QStringLiteral( "QgsProcessingParameterDxfLayers('%1', '%2')" ).arg( name(), description() );
return code;
}
}
return QString();
}

QList<QgsDxfExport::DxfLayer> QgsProcessingParameterDxfLayers::parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context )
{
QList<QgsDxfExport::DxfLayer> layers;
const QVariantList layersVariantList = layersVariant.toList();
for ( const QVariant &layerItem : layersVariantList )
{
QVariantMap layerVariantMap = layerItem.toMap();
layers << variantMapAsLayer( layerVariantMap, context );
}
return layers;
}

QgsDxfExport::DxfLayer QgsProcessingParameterDxfLayers::variantMapAsLayer( const QVariantMap &layerVariantMap, QgsProcessingContext &context )
{
QVariant layerVariant = layerVariantMap[ QStringLiteral( "layer" ) ];

QgsVectorLayer *inputLayer = nullptr;
if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layerVariant ) ) ) )
{
// good
}
else if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerVariant.toString(), context ) ) ) )
{
// good
}
else
{
// bad
}

QgsDxfExport::DxfLayer dxfLayer( inputLayer, layerVariantMap[ QStringLiteral( "attributeIndex" ) ].toInt() );
return dxfLayer;
}

QVariantMap QgsProcessingParameterDxfLayers::layerAsVariantMap( const QgsDxfExport::DxfLayer &layer )
{
QVariantMap vm;
if ( !layer.layer() )
return vm;

vm[ QStringLiteral( "layer" )] = layer.layer()->id();
vm[ QStringLiteral( "attributeIndex" ) ] = layer. layerOutputAttributeIndex();
return vm;
}
@@ -0,0 +1,112 @@
/***************************************************************************
qgsprocessingparameterdxflayers.h
---------------------
Date : September 2020
Copyright : (C) 2020 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSPROCESSINGPARAMETERDXFLAYERS_H
#define QGSPROCESSINGPARAMETERDXFLAYERS_H

#include "qgsprocessingparameters.h"
#include "qgsprocessingparametertype.h"
#include "qgsdxfexport.h"

/**
* A parameter for Processing algorithms that need a list of input vector
* layers to export as DXF file - this parameter provides Processing
* framework's adapter for QList<QgsDxfExport::DxfLayer>.
*
* A valid value for this parameter is a list (QVariantList), where each
* item is a map (QVariantMap) in this form:
* {
* 'layer': string or QgsMapLayer,
* 'attributeIndex': int
* }
*
* Static functions parametersAsLayers(), variantMapAsLayer(),
* layerAsVariantMap() provide conversion between QgsDxfExport::DxfLayer
* representation and QVariant representation.
*
* \ingroup core
* \note This class is not a part of public API.
* \since QGIS 3.18
*/
class CORE_EXPORT QgsProcessingParameterDxfLayers : public QgsProcessingParameterDefinition
{
public:
//! Constructor for QgsProcessingParameterDxfLayers.
QgsProcessingParameterDxfLayers( const QString &name, const QString &description = QString() );

QgsProcessingParameterDefinition *clone() const override;
QString type() const override;
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const override;

//! Returns the type name for the parameter class.
static QString typeName() { return QStringLiteral( "dxflayers" ); }
//! Converts a QVariant value (a QVariantList) to a list of input layers
static QList<QgsDxfExport::DxfLayer> parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context );
//! Converts a QVariant value (a QVariantMap) to a single input layer
static QgsDxfExport::DxfLayer variantMapAsLayer( const QVariantMap &layerVariantMap, QgsProcessingContext &context );
//! Converts a single input layer to QVariant representation (a QVariantMap)
static QVariantMap layerAsVariantMap( const QgsDxfExport::DxfLayer &layer );
};


/**
* Parameter type definition for QgsProcessingParameterDxfLayers.
*
* \ingroup core
* \note This class is not a part of public API.
* \since QGIS 3.18
*/
class CORE_EXPORT QgsProcessingParameterTypeDxfLayers : public QgsProcessingParameterType
{
public:
QgsProcessingParameterDefinition *create( const QString &name ) const override SIP_FACTORY
{
return new QgsProcessingParameterDxfLayers( name );
}

QString description() const override
{
return QCoreApplication::translate( "Processing", "An input allowing selection of multiple layers for export to DXF file." );
}

QString name() const override
{
return QCoreApplication::translate( "Processing", "DXF Layers" );
}

QString id() const override
{
return QgsProcessingParameterDxfLayers::typeName();
}

QString pythonImportString() const override
{
return QStringLiteral( "from qgis.core import QgsProcessingParameterDxfLayers" );
}

QString className() const override
{
return QStringLiteral( "QgsProcessingParameterDxfLayers" );
}

QStringList acceptedPythonTypes() const override
{
return QStringList() << QObject::tr( "list[dict]: list of input layers as dictionaries, see QgsProcessingParameterDxfLayers docs" );
}
};

#endif // QGSPROCESSINGPARAMETERDXFLAYERS_H

0 comments on commit b7997e0

Please sign in to comment.
You can’t perform that action at this time.