QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
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
20#include "qgsvectorlayer.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsTruncateTableAlgorithm::name() const
29{
30 return u"truncatetable"_s;
31}
32
33QString QgsTruncateTableAlgorithm::displayName() const
34{
35 return QObject::tr( "Truncate table" );
36}
37
38QStringList QgsTruncateTableAlgorithm::tags() const
39{
40 return QObject::tr( "empty,delete,layer,clear,features" ).split( ',' );
41}
42
43QString QgsTruncateTableAlgorithm::group() const
44{
45 return QObject::tr( "Vector general" );
46}
47
48QString QgsTruncateTableAlgorithm::groupId() const
49{
50 return u"vectorgeneral"_s;
51}
52
53QString QgsTruncateTableAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "This algorithm truncates a layer, by deleting all features from within the layer." )
56 + u"\n\n"_s
57 + QObject::tr( "Warning — this algorithm modifies the layer in place, and deleted features cannot be restored!" );
58}
59
60QString QgsTruncateTableAlgorithm::shortDescription() const
61{
62 return QObject::tr( "Truncates a layer, by deleting all features from within the layer." );
63}
64
65Qgis::ProcessingAlgorithmFlags QgsTruncateTableAlgorithm::flags() const
66{
68}
69
70QgsTruncateTableAlgorithm *QgsTruncateTableAlgorithm::createInstance() const
71{
72 return new QgsTruncateTableAlgorithm();
73}
74
75void QgsTruncateTableAlgorithm::initAlgorithm( const QVariantMap & )
76{
77 addParameter( new QgsProcessingParameterVectorLayer( u"INPUT"_s, QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
78 addOutput( new QgsProcessingOutputVectorLayer( u"OUTPUT"_s, QObject::tr( "Truncated layer" ) ) );
79}
80
81QVariantMap QgsTruncateTableAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
82{
83 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, u"INPUT"_s, context );
84
85 if ( !layer )
86 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
87
88 if ( !layer->dataProvider()->truncate() )
89 {
90 throw QgsProcessingException( QObject::tr( "Could not truncate table." ) );
91 }
92
93 QVariantMap outputs;
94 outputs.insert( u"OUTPUT"_s, layer->id() );
95 return outputs;
96}
97
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3610
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3680
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
Definition qgis.h:3659
QString id
Definition qgsmaplayer.h:86
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.