Skip to content
Permalink
Browse files
[processing] Add an algorithm which transfers all items from the
main annotation layer in a project to a secondary annotation layer

Useful for moving items created in the main layer to a secondary
layer, so that the item placement can be adjusted within the
layer stack
  • Loading branch information
nyalldawson committed Sep 10, 2021
1 parent c2c0f25 commit 523444229521bd601769a8c250e4dd427ef6742e
@@ -39,6 +39,7 @@ set(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmaffinetransform.cpp
processing/qgsalgorithmaggregate.cpp
processing/qgsalgorithmangletonearest.cpp
processing/qgsalgorithmannotations.cpp
processing/qgsalgorithmapplylayerstyle.cpp
processing/qgsalgorithmarraytranslatedfeatures.cpp
processing/qgsalgorithmaspect.cpp
@@ -0,0 +1,92 @@
/***************************************************************************
qgsalgorithmannotations.cpp
------------------------------
begin : September 2021
copyright : (C) 2021 by Nyall Dawson
email : nyall dot dawson 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 "qgsalgorithmannotations.h"
#include "qgsannotationlayer.h"

///@cond PRIVATE

QString QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm::name() const
{
return QStringLiteral( "transferannotationsfrommain" );
}

QString QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm::displayName() const
{
return QObject::tr( "Transfer annotations from main layer" );
}

QStringList QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm::tags() const
{
return QObject::tr( "annotations,drawing,cosmetic,objects" ).split( ',' );
}

QString QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm::group() const
{
return QObject::tr( "Cartography" );
}

QString QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm::groupId() const
{
return QStringLiteral( "cartography" );
}

QgsProcessingAlgorithm::Flags QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagNoThreading | QgsProcessingAlgorithm::FlagRequiresProject;
}


QString QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm::shortHelpString() const
{
return QObject::tr( "Transfer all annotations from the main annotation layer in a project to a new annotation layer." );
}

QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm *QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm::createInstance() const
{
return new QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm();
}

void QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterString( QStringLiteral( "LAYER_NAME" ), QObject::tr( "New layer name" ), QObject::tr( "Annotations" ) ) );

addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "New annotation layer" ) ) );
}

QVariantMap QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
if ( !context.project() )
throw QgsProcessingException( QObject::tr( "No project available." ) );

QgsAnnotationLayer *main = context.project()->mainAnnotationLayer();
if ( !main )
throw QgsProcessingException( QObject::tr( "Could not load main annotation layer for project." ) );

std::unique_ptr< QgsAnnotationLayer > newLayer( main->clone() );
newLayer->setName( parameterAsString( parameters, QStringLiteral( "LAYER_NAME" ), context ) );
main->clear();

QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), newLayer->id() );

context.project()->addMapLayer( newLayer.release() );

return outputs;
}

///@endcond
@@ -0,0 +1,55 @@
/***************************************************************************
qgsalgorithmannotations.h
------------------------------
begin : September 2021
copyright : (C) 2021 by Nyall Dawson
email : nyall dot dawson 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 QGSALGORITHMANNOTATIONS_H
#define QGSALGORITHMANNOTATIONS_H

#define SIP_NO_FILE

#include "qgis_sip.h"
#include "qgsprocessingalgorithm.h"

///@cond PRIVATE

/**
* Native transfer annotations from main annotation layer algorithm
*/
class QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm : public QgsProcessingAlgorithm
{

public:

QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
QString group() const override;
QString groupId() const override;
Flags flags() const override;
QString shortHelpString() const override;
QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm *createInstance() const override SIP_FACTORY;

protected:

QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
};

///@endcond PRIVATE

#endif // QGSALGORITHMANNOTATIONS_H
@@ -23,6 +23,7 @@
#include "qgsalgorithmaffinetransform.h"
#include "qgsalgorithmaggregate.h"
#include "qgsalgorithmangletonearest.h"
#include "qgsalgorithmannotations.h"
#include "qgsalgorithmapplylayerstyle.h"
#include "qgsalgorithmarraytranslatedfeatures.h"
#include "qgsalgorithmaspect.h"
@@ -477,6 +478,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsTaperedBufferAlgorithm() );
addAlgorithm( new QgsTinMeshCreationAlgorithm() );
addAlgorithm( new QgsTransectAlgorithm() );
addAlgorithm( new QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm() );
addAlgorithm( new QgsTransformAlgorithm() );
addAlgorithm( new QgsTranslateAlgorithm() );
addAlgorithm( new QgsTruncateTableAlgorithm() );
@@ -61,6 +61,8 @@
#include "qgsmarkersymbol.h"
#include "qgsfillsymbol.h"
#include "qgsalgorithmgpsbabeltools.h"
#include "qgsannotationlayer.h"
#include "qgsannotationmarkeritem.h"

class TestQgsProcessingAlgs: public QObject
{
@@ -189,6 +191,7 @@ class TestQgsProcessingAlgs: public QObject
void convertGpsData();
void downloadGpsData();
void uploadGpsData();
void transferMainAnnotationLayer();

private:

@@ -6915,6 +6918,31 @@ void TestQgsProcessingAlgs::uploadGpsData()
feedback.errors.clear();
}

void TestQgsProcessingAlgs::transferMainAnnotationLayer()
{
std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:transferannotationsfrommain" ) ) );
QVERIFY( alg != nullptr );

QgsProject p;
p.mainAnnotationLayer()->addItem( new QgsAnnotationMarkerItem( QgsPoint( 1, 2 ) ) );

std::unique_ptr< QgsProcessingContext > context = std::make_unique< QgsProcessingContext >();
context->setProject( &p );
QgsProcessingFeedback feedback;
QVariantMap results;
bool ok = false;

QVariantMap parameters;
parameters.insert( QStringLiteral( "LAYER_NAME" ), QStringLiteral( "my annotations" ) );
results = alg->run( parameters, *context, &feedback, &ok );
QVERIFY( ok );

QCOMPARE( p.mainAnnotationLayer()->items().size(), 0 );
QgsAnnotationLayer *newLayer = qobject_cast< QgsAnnotationLayer * >( p.mapLayer( results.value( QStringLiteral( "OUTPUT" ) ).toString() ) );
QCOMPARE( newLayer->name(), QStringLiteral( "my annotations" ) );
QCOMPARE( newLayer->items().size(), 1 );
}

void TestQgsProcessingAlgs::exportMeshTimeSeries()
{
std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:meshexporttimeseries" ) ) );

0 comments on commit 5234442

Please sign in to comment.