Skip to content

Commit

Permalink
[processing] Add an algorithm which transfers all items from the
Browse files Browse the repository at this point in the history
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 5234442
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/analysis/CMakeLists.txt
Expand Up @@ -39,6 +39,7 @@ set(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmaffinetransform.cpp processing/qgsalgorithmaffinetransform.cpp
processing/qgsalgorithmaggregate.cpp processing/qgsalgorithmaggregate.cpp
processing/qgsalgorithmangletonearest.cpp processing/qgsalgorithmangletonearest.cpp
processing/qgsalgorithmannotations.cpp
processing/qgsalgorithmapplylayerstyle.cpp processing/qgsalgorithmapplylayerstyle.cpp
processing/qgsalgorithmarraytranslatedfeatures.cpp processing/qgsalgorithmarraytranslatedfeatures.cpp
processing/qgsalgorithmaspect.cpp processing/qgsalgorithmaspect.cpp
Expand Down
92 changes: 92 additions & 0 deletions src/analysis/processing/qgsalgorithmannotations.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
55 changes: 55 additions & 0 deletions src/analysis/processing/qgsalgorithmannotations.h
@@ -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
2 changes: 2 additions & 0 deletions src/analysis/processing/qgsnativealgorithms.cpp
Expand Up @@ -23,6 +23,7 @@
#include "qgsalgorithmaffinetransform.h" #include "qgsalgorithmaffinetransform.h"
#include "qgsalgorithmaggregate.h" #include "qgsalgorithmaggregate.h"
#include "qgsalgorithmangletonearest.h" #include "qgsalgorithmangletonearest.h"
#include "qgsalgorithmannotations.h"
#include "qgsalgorithmapplylayerstyle.h" #include "qgsalgorithmapplylayerstyle.h"
#include "qgsalgorithmarraytranslatedfeatures.h" #include "qgsalgorithmarraytranslatedfeatures.h"
#include "qgsalgorithmaspect.h" #include "qgsalgorithmaspect.h"
Expand Down Expand Up @@ -477,6 +478,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsTaperedBufferAlgorithm() ); addAlgorithm( new QgsTaperedBufferAlgorithm() );
addAlgorithm( new QgsTinMeshCreationAlgorithm() ); addAlgorithm( new QgsTinMeshCreationAlgorithm() );
addAlgorithm( new QgsTransectAlgorithm() ); addAlgorithm( new QgsTransectAlgorithm() );
addAlgorithm( new QgsTransferAnnotationsFromMainAnnotationLayerAlgorithm() );
addAlgorithm( new QgsTransformAlgorithm() ); addAlgorithm( new QgsTransformAlgorithm() );
addAlgorithm( new QgsTranslateAlgorithm() ); addAlgorithm( new QgsTranslateAlgorithm() );
addAlgorithm( new QgsTruncateTableAlgorithm() ); addAlgorithm( new QgsTruncateTableAlgorithm() );
Expand Down
28 changes: 28 additions & 0 deletions tests/src/analysis/testqgsprocessingalgs.cpp
Expand Up @@ -61,6 +61,8 @@
#include "qgsmarkersymbol.h" #include "qgsmarkersymbol.h"
#include "qgsfillsymbol.h" #include "qgsfillsymbol.h"
#include "qgsalgorithmgpsbabeltools.h" #include "qgsalgorithmgpsbabeltools.h"
#include "qgsannotationlayer.h"
#include "qgsannotationmarkeritem.h"


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


private: private:


Expand Down Expand Up @@ -6915,6 +6918,31 @@ void TestQgsProcessingAlgs::uploadGpsData()
feedback.errors.clear(); 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() void TestQgsProcessingAlgs::exportMeshTimeSeries()
{ {
std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:meshexporttimeseries" ) ) ); std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:meshexporttimeseries" ) ) );
Expand Down

0 comments on commit 5234442

Please sign in to comment.