Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processing] port add table field algorithm to C++
- Loading branch information
Showing
8 changed files
with
203 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/*************************************************************************** | ||
qgsalgorithmaddtablefield.cpp | ||
----------------------------------- | ||
begin : November 2019 | ||
copyright : (C) 2019 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 "qgsalgorithmaddtablefield.h" | ||
|
||
///@cond PRIVATE | ||
|
||
QString QgsAddTableFieldAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "addfieldtoattributestable" ); | ||
} | ||
|
||
QString QgsAddTableFieldAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "Add field to attributes table" ); | ||
} | ||
|
||
QString QgsAddTableFieldAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "This algorithm adds a new attribute to a vector layer.\n\n" | ||
"The name and characteristics of the attribute are defined as parameters. The new attribute " | ||
"is not added to the input layer but a new layer is generated instead.\n\n" ); | ||
} | ||
|
||
QStringList QgsAddTableFieldAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "add,create,new,attribute,fields" ).split( ',' ); | ||
} | ||
|
||
QString QgsAddTableFieldAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Vector table" ); | ||
} | ||
|
||
QString QgsAddTableFieldAlgorithm::groupId() const | ||
{ | ||
return QStringLiteral( "vectortable" ); | ||
} | ||
|
||
QString QgsAddTableFieldAlgorithm::outputName() const | ||
{ | ||
return QObject::tr( "Added" ); | ||
} | ||
|
||
QList<int> QgsAddTableFieldAlgorithm::inputLayerTypes() const | ||
{ | ||
return QList<int>() << QgsProcessing::TypeVector; | ||
} | ||
|
||
QgsProcessingFeatureSource::Flag QgsAddTableFieldAlgorithm::sourceFlags() const | ||
{ | ||
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks; | ||
} | ||
|
||
QgsAddTableFieldAlgorithm *QgsAddTableFieldAlgorithm::createInstance() const | ||
{ | ||
return new QgsAddTableFieldAlgorithm(); | ||
} | ||
|
||
void QgsAddTableFieldAlgorithm::initParameters( const QVariantMap & ) | ||
{ | ||
addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_NAME" ), QObject::tr( "Field name" ) ) ); | ||
addParameter( new QgsProcessingParameterEnum( QStringLiteral( "FIELD_TYPE" ), QObject::tr( "Field type" ), | ||
QStringList() << QObject::tr( "Integer" ) << QObject::tr( "Float" ) << QObject::tr( "String" ), false, 0 ) ); | ||
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FIELD_LENGTH" ), QObject::tr( "Field length" ), | ||
QgsProcessingParameterNumber::Integer, 10, false, 1, 255 ) ); | ||
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FIELD_PRECISION" ), QObject::tr( "Field precision" ), | ||
QgsProcessingParameterNumber::Integer, 0, false, 0, 10 ) ); | ||
} | ||
|
||
QgsFields QgsAddTableFieldAlgorithm::outputFields( const QgsFields &inputFields ) const | ||
{ | ||
QgsFields outFields = inputFields; | ||
outFields.append( QgsField( mField ) ); | ||
return outFields; | ||
} | ||
|
||
bool QgsAddTableFieldAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) | ||
{ | ||
QString name = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context ); | ||
int type = parameterAsInt( parameters, QStringLiteral( "FIELD_TYPE" ), context ); | ||
int length = parameterAsInt( parameters, QStringLiteral( "FIELD_LENGTH" ), context ); | ||
int precision = parameterAsInt( parameters, QStringLiteral( "FIELD_PRECISION" ), context ); | ||
|
||
mField.setName( name ); | ||
mField.setLength( length ); | ||
mField.setPrecision( precision ); | ||
|
||
switch ( type ) | ||
{ | ||
case 0: | ||
mField.setType( QVariant::Int ); | ||
break; | ||
case 1: | ||
mField.setType( QVariant::Double ); | ||
break; | ||
case 2: | ||
mField.setType( QVariant::String ); | ||
break; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
QgsFeatureList QgsAddTableFieldAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * ) | ||
{ | ||
QgsFeature f = feature; | ||
QgsAttributes attributes = f.attributes(); | ||
attributes.append( QVariant() ); | ||
f.setAttributes( attributes ); | ||
return QgsFeatureList() << f; | ||
} | ||
|
||
bool QgsAddTableFieldAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const | ||
{ | ||
Q_UNUSED( layer ) | ||
return false; | ||
} | ||
|
||
///@endcond |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/*************************************************************************** | ||
qgsalgorithmaddtablefield.h | ||
--------------------------------- | ||
begin : November 2019 | ||
copyright : (C) 2019 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 QGSALGORITHMADDTABLEFIELD_H | ||
#define QGSALGORITHMADDTABLEFIELD_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include "qgis_sip.h" | ||
#include "qgsprocessingalgorithm.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Native add table field algorithm. | ||
*/ | ||
class QgsAddTableFieldAlgorithm : public QgsProcessingFeatureBasedAlgorithm | ||
{ | ||
|
||
public: | ||
|
||
QgsAddTableFieldAlgorithm() = default; | ||
QString name() const override; | ||
QString displayName() const override; | ||
QStringList tags() const override; | ||
QString group() const override; | ||
QString groupId() const override; | ||
QString shortHelpString() const override; | ||
QList<int> inputLayerTypes() const override; | ||
QgsAddTableFieldAlgorithm *createInstance() const override SIP_FACTORY; | ||
|
||
protected: | ||
|
||
void initParameters( const QVariantMap &configuration = QVariantMap() ) override; | ||
QString outputName() const override; | ||
QgsFields outputFields( const QgsFields &inputFields ) const override; | ||
QgsProcessingFeatureSource::Flag sourceFlags() const override; | ||
|
||
bool prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
bool supportInPlaceEdit( const QgsMapLayer *layer ) const override; | ||
|
||
private: | ||
|
||
QgsField mField; | ||
}; | ||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSALGORITHMADDTABLEFIELD_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters