QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsalgorithmannotations.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmannotations.cpp
3  ------------------------------
4  begin : September 2021
5  copyright : (C) 2021 by Nyall Dawson
6  email : nyall dot dawson 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 "qgsannotationlayer.h"
20 
22 
23 QString QgsTransferAnnotationsFromMainAlgorithm::name() const
24 {
25  return QStringLiteral( "transferannotationsfrommain" );
26 }
27 
28 QString QgsTransferAnnotationsFromMainAlgorithm::displayName() const
29 {
30  return QObject::tr( "Transfer annotations from main layer" );
31 }
32 
33 QStringList QgsTransferAnnotationsFromMainAlgorithm::tags() const
34 {
35  return QObject::tr( "annotations,drawing,cosmetic,objects" ).split( ',' );
36 }
37 
38 QString QgsTransferAnnotationsFromMainAlgorithm::group() const
39 {
40  return QObject::tr( "Cartography" );
41 }
42 
43 QString QgsTransferAnnotationsFromMainAlgorithm::groupId() const
44 {
45  return QStringLiteral( "cartography" );
46 }
47 
48 QgsProcessingAlgorithm::Flags QgsTransferAnnotationsFromMainAlgorithm::flags() const
49 {
51 }
52 
53 
54 QString QgsTransferAnnotationsFromMainAlgorithm::shortHelpString() const
55 {
56  return QObject::tr( "Transfer all annotations from the main annotation layer in a project to a new annotation layer." );
57 }
58 
59 QgsTransferAnnotationsFromMainAlgorithm *QgsTransferAnnotationsFromMainAlgorithm::createInstance() const
60 {
61  return new QgsTransferAnnotationsFromMainAlgorithm();
62 }
63 
64 void QgsTransferAnnotationsFromMainAlgorithm::initAlgorithm( const QVariantMap & )
65 {
66  addParameter( new QgsProcessingParameterString( QStringLiteral( "LAYER_NAME" ), QObject::tr( "New layer name" ), QObject::tr( "Annotations" ) ) );
67 
68  addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "New annotation layer" ) ) );
69 }
70 
71 QVariantMap QgsTransferAnnotationsFromMainAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
72 {
73  if ( !context.project() )
74  throw QgsProcessingException( QObject::tr( "No project available." ) );
75 
77  if ( !main )
78  throw QgsProcessingException( QObject::tr( "Could not load main annotation layer for project." ) );
79 
80  std::unique_ptr< QgsAnnotationLayer > newLayer( main->clone() );
81  newLayer->setName( parameterAsString( parameters, QStringLiteral( "LAYER_NAME" ), context ) );
82  main->clear();
83 
84  QVariantMap outputs;
85  outputs.insert( QStringLiteral( "OUTPUT" ), newLayer->id() );
86 
87  context.project()->addMapLayer( newLayer.release() );
88 
89  return outputs;
90 }
91 
main
int main(int argc, char *argv[])
Definition: qgis_map_serv.cpp:45
QgsProcessingContext::project
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Definition: qgsprocessingcontext.h:121
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsProcessingParameterString
A string parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2647
QgsProcessingAlgorithm::FlagRequiresProject
@ FlagRequiresProject
The algorithm requires that a valid QgsProject is available from the processing context in order to e...
Definition: qgsprocessingalgorithm.h:84
qgsannotationlayer.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:76
QgsAnnotationLayer
Represents a map layer containing a set of georeferenced annotations, e.g. markers,...
Definition: qgsannotationlayer.h:46
QgsProcessingOutputMapLayer
A map layer output for processing algorithms, where layers may be either vector or raster.
Definition: qgsprocessingoutputs.h:156
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:90
qgsalgorithmannotations.h
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsProject::mainAnnotationLayer
QgsAnnotationLayer * mainAnnotationLayer()
Returns the main annotation layer associated with the project.
Definition: qgsproject.cpp:3925
QgsProject::addMapLayer
QgsMapLayer * addMapLayer(QgsMapLayer *mapLayer, bool addToLegend=true, bool takeOwnership=true)
Add a layer to the map of loaded layers.
Definition: qgsproject.cpp:3886