QGIS API Documentation 3.43.0-Master (0cdc48caa8d)
qgsalgorithmtruncatetable.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmtruncatetable.cpp
3 ---------------------
4 begin : December 2019
5 copyright : (C) 2019 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgsvectorlayer.h"
20
22
23QString QgsTruncateTableAlgorithm::name() const
24{
25 return QStringLiteral( "truncatetable" );
26}
27
28QString QgsTruncateTableAlgorithm::displayName() const
29{
30 return QObject::tr( "Truncate table" );
31}
32
33QStringList QgsTruncateTableAlgorithm::tags() const
34{
35 return QObject::tr( "empty,delete,layer,clear,features" ).split( ',' );
36}
37
38QString QgsTruncateTableAlgorithm::group() const
39{
40 return QObject::tr( "Vector general" );
41}
42
43QString QgsTruncateTableAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeneral" );
46}
47
48QString QgsTruncateTableAlgorithm::shortHelpString() const
49{
50 return QObject::tr( "This algorithm truncates a layer, by deleting all features from within the layer." )
51 + QStringLiteral( "\n\n" )
52 + QObject::tr( "Warning — this algorithm modifies the layer in place, and deleted features cannot be restored!" );
53}
54
55QString QgsTruncateTableAlgorithm::shortDescription() const
56{
57 return QObject::tr( "Truncates a layer, by deleting all features from within the layer." );
58}
59
60Qgis::ProcessingAlgorithmFlags QgsTruncateTableAlgorithm::flags() const
61{
63}
64
65QgsTruncateTableAlgorithm *QgsTruncateTableAlgorithm::createInstance() const
66{
67 return new QgsTruncateTableAlgorithm();
68}
69
70void QgsTruncateTableAlgorithm::initAlgorithm( const QVariantMap & )
71{
72 addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
73 addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Truncated layer" ) ) );
74}
75
76QVariantMap QgsTruncateTableAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
77{
78 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
79
80 if ( !layer )
81 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
82
83 if ( !layer->dataProvider()->truncate() )
84 {
85 throw QgsProcessingException( QObject::tr( "Could not truncate table." ) );
86 }
87
88 QVariantMap outputs;
89 outputs.insert( QStringLiteral( "OUTPUT" ), layer->id() );
90 return outputs;
91}
92
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3476
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
QString id
Definition qgsmaplayer.h:80
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A vector layer output for processing algorithms.
A vector layer (with or without geometry) parameter for processing algorithms.
virtual bool truncate()
Removes all features from the layer.
Represents a vector layer which manages a vector based dataset.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.