QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
23 QString QgsTruncateTableAlgorithm::name() const
24 {
25  return QStringLiteral( "truncatetable" );
26 }
27 
28 QString QgsTruncateTableAlgorithm::displayName() const
29 {
30  return QObject::tr( "Truncate table" );
31 }
32 
33 QStringList QgsTruncateTableAlgorithm::tags() const
34 {
35  return QObject::tr( "empty,delete,layer,clear,features" ).split( ',' );
36 }
37 
38 QString QgsTruncateTableAlgorithm::group() const
39 {
40  return QObject::tr( "Vector general" );
41 }
42 
43 QString QgsTruncateTableAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeneral" );
46 }
47 
48 QString 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 
55 QgsProcessingAlgorithm::Flags QgsTruncateTableAlgorithm::flags() const
56 {
58 }
59 
60 QgsTruncateTableAlgorithm *QgsTruncateTableAlgorithm::createInstance() const
61 {
62  return new QgsTruncateTableAlgorithm();
63 }
64 
65 void QgsTruncateTableAlgorithm::initAlgorithm( const QVariantMap & )
66 {
67  addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
68  addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Truncated layer" ) ) );
69 }
70 
71 QVariantMap QgsTruncateTableAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
72 {
73  QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
74 
75  if ( !layer )
76  throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
77 
78  if ( !layer->dataProvider()->truncate() )
79  {
80  throw QgsProcessingException( QObject::tr( "Could not truncate table." ) );
81  }
82 
83  QVariantMap outputs;
84  outputs.insert( QStringLiteral( "OUTPUT" ), layer->id() );
85  return outputs;
86 }
87 
QgsVectorLayer::dataProvider
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Definition: qgsvectorlayer.cpp:627
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsProcessingOutputVectorLayer
Definition: qgsprocessingoutputs.h:179
QgsVectorDataProvider::truncate
virtual bool truncate()
Removes all features from the layer.
Definition: qgsvectordataprovider.cpp:98
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsMapLayer::id
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Definition: qgsmaplayer.cpp:148
QgsProcessingParameterVectorLayer
Definition: qgsprocessingparameters.h:2382
qgsalgorithmtruncatetable.h
qgsvectorlayer.h
QgsProcessingAlgorithm::FlagNoThreading
@ FlagNoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
Definition: qgsprocessingalgorithm.h:75
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsProcessingAlgorithm::flags
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Definition: qgsprocessingalgorithm.cpp:88
QgsProcessingException
Definition: qgsexception.h:82