Skip to content

Commit

Permalink
Move annotation layer renderer to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 5, 2020
1 parent 35d9a65 commit 21a2599
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 77 deletions.
4 changes: 2 additions & 2 deletions python/core/auto_additions/qgsmaplayer.py
Expand Up @@ -12,8 +12,8 @@
QgsMapLayer.VectorTileLayer = QgsMapLayerType.VectorTileLayer
QgsMapLayer.VectorTileLayer.__doc__ = "Added in 3.14"
QgsMapLayer.AnnotationLayer = QgsMapLayerType.AnnotationLayer
QgsMapLayer.AnnotationLayer.__doc__ = "Contains freeform, georeferenced annotations. Added in QGIS 3.10"
QgsMapLayerType.__doc__ = 'Types of layers that can be added to a map\n\n.. versionadded:: 3.8\n\n' + '* ``VectorLayer``: ' + QgsMapLayerType.VectorLayer.__doc__ + '\n' + '* ``RasterLayer``: ' + QgsMapLayerType.RasterLayer.__doc__ + '\n' + '* ``PluginLayer``: ' + QgsMapLayerType.PluginLayer.__doc__ + '\n' + '* ``MeshLayer``: ' + QgsMapLayerType.MeshLayer.__doc__ + '\n' + '* ``VectorTileLayer``: ' + QgsMapLayerType.VectorTileLayer.__doc__
QgsMapLayer.AnnotationLayer.__doc__ = "Contains freeform, georeferenced annotations. Added in QGIS 3.16"
QgsMapLayerType.__doc__ = 'Types of layers that can be added to a map\n\n.. versionadded:: 3.8\n\n' + '* ``VectorLayer``: ' + QgsMapLayerType.VectorLayer.__doc__ + '\n' + '* ``RasterLayer``: ' + QgsMapLayerType.RasterLayer.__doc__ + '\n' + '* ``PluginLayer``: ' + QgsMapLayerType.PluginLayer.__doc__ + '\n' + '* ``MeshLayer``: ' + QgsMapLayerType.MeshLayer.__doc__ + '\n' + '* ``VectorTileLayer``: ' + QgsMapLayerType.VectorTileLayer.__doc__ + '\n' + '* ``AnnotationLayer``: ' + QgsMapLayerType.AnnotationLayer.__doc__
# --
QgsMapLayer.LayerFlag.baseClass = QgsMapLayer
QgsMapLayer.LayerFlags.baseClass = QgsMapLayer
Expand Down
17 changes: 0 additions & 17 deletions python/core/auto_generated/annotations/qgsannotationlayer.sip.in
Expand Up @@ -88,23 +88,6 @@ Constructor for LayerOptions.

};

class QgsAnnotationLayerRenderer : QgsMapLayerRenderer
{

%TypeHeaderCode
#include "qgsannotationlayer.h"
%End
public:

QgsAnnotationLayerRenderer( QgsAnnotationLayer *layer, QgsRenderContext &context );
~QgsAnnotationLayerRenderer();
virtual QgsFeedback *feedback() const;

virtual bool render();


};

/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
2 changes: 1 addition & 1 deletion python/core/auto_generated/qgsmaplayer.sip.in
Expand Up @@ -20,7 +20,7 @@ enum class QgsMapLayerType
RasterLayer,
PluginLayer,
MeshLayer,
VectorTileLayer,
VectorTileLayer
AnnotationLayer,
};

Expand Down
4 changes: 4 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -125,6 +125,8 @@ SET(QGIS_CORE_SRCS
auth/qgsauthmethodregistry.cpp

annotations/qgsannotation.cpp
annotations/qgsannotationlayer.cpp
annotations/qgsannotationlayerrenderer.cpp
annotations/qgsannotationmanager.cpp
annotations/qgshtmlannotation.cpp
annotations/qgssvgannotation.cpp
Expand Down Expand Up @@ -1045,6 +1047,8 @@ SET(QGIS_CORE_HDRS
3d/qgsabstract3drenderer.h

annotations/qgsannotation.h
annotations/qgsannotationlayer.h
annotations/qgsannotationlayerrenderer.h
annotations/qgsannotationmanager.h
annotations/qgsannotationregistry.h
annotations/qgshtmlannotation.h
Expand Down
43 changes: 1 addition & 42 deletions src/core/annotations/qgsannotationlayer.cpp
Expand Up @@ -15,7 +15,7 @@
***************************************************************************/

#include "qgsannotationlayer.h"
#include "qgsfeedback.h"
#include "qgsannotationlayerrenderer.h"
#include <QUuid>

QgsAnnotationLayer::QgsAnnotationLayer( const QString &name, const LayerOptions &options )
Expand Down Expand Up @@ -130,44 +130,3 @@ QRectF QgsAnnotationLayer::margin() const
}
#endif

QgsAnnotationLayerRenderer::QgsAnnotationLayerRenderer( QgsAnnotationLayer *layer, QgsRenderContext &context )
: QgsMapLayerRenderer( layer->id(), &context )
, mFeedback( qgis::make_unique< QgsFeedback >() )
{
// clone items from layer
const QMap< QString, QgsAnnotationItem * > items = layer->items();
mItems.reserve( items.size() );
for ( auto it = items.constBegin(); it != items.constEnd(); ++it )
{
if ( it.value() )
mItems << ( *it )->clone();
}

// std::sort( mItems.begin(), mItems.end(), []( QgsAnnotationItem * a, QgsAnnotationItem * b ) { return a->zIndex() < b->zIndex(); } );
}

QgsAnnotationLayerRenderer::~QgsAnnotationLayerRenderer()
{
qDeleteAll( mItems );
}

QgsFeedback *QgsAnnotationLayerRenderer::feedback() const
{
return mFeedback.get();
}

bool QgsAnnotationLayerRenderer::render()
{
QgsRenderContext &context = *renderContext();

for ( QgsAnnotationItem *item : qgis::as_const( mItems ) )
{
if ( mFeedback->isCanceled() )
break;


renderContext()->setCoordinateTransform( QgsCoordinateTransform( item->crs(), context.coordinateTransform().destinationCrs(), context.transformContext() ) );
item->render( context, mFeedback.get() );
}
return true;
}
15 changes: 0 additions & 15 deletions src/core/annotations/qgsannotationlayer.h
Expand Up @@ -100,19 +100,4 @@ class CORE_EXPORT QgsAnnotationLayer : public QgsMapLayer
double mOpacity = 100;
};

class CORE_EXPORT QgsAnnotationLayerRenderer : public QgsMapLayerRenderer
{
public:

QgsAnnotationLayerRenderer( QgsAnnotationLayer *layer, QgsRenderContext &context );
~QgsAnnotationLayerRenderer() override;
QgsFeedback *feedback() const override;
bool render() override;

private:
QVector< QgsAnnotationItem *> mItems;
std::unique_ptr< QgsFeedback > mFeedback;

};

#endif // QGSANNOTATIONLAYER_H
60 changes: 60 additions & 0 deletions src/core/annotations/qgsannotationlayerrenderer.cpp
@@ -0,0 +1,60 @@
/***************************************************************************
qgsannotationlayerrenderer.cpp
------------------
copyright : (C) 2019 by Sandro Mani
email : smani at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* 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 "qgsannotationlayerrenderer.h"
#include "qgsannotationlayer.h"
#include "qgsfeedback.h"

QgsAnnotationLayerRenderer::QgsAnnotationLayerRenderer( QgsAnnotationLayer *layer, QgsRenderContext &context )
: QgsMapLayerRenderer( layer->id(), &context )
, mFeedback( qgis::make_unique< QgsFeedback >() )
{
// clone items from layer
const QMap< QString, QgsAnnotationItem * > items = layer->items();
mItems.reserve( items.size() );
for ( auto it = items.constBegin(); it != items.constEnd(); ++it )
{
if ( it.value() )
mItems << ( *it )->clone();
}

// std::sort( mItems.begin(), mItems.end(), []( QgsAnnotationItem * a, QgsAnnotationItem * b ) { return a->zIndex() < b->zIndex(); } );
}

QgsAnnotationLayerRenderer::~QgsAnnotationLayerRenderer()
{
qDeleteAll( mItems );
}

QgsFeedback *QgsAnnotationLayerRenderer::feedback() const
{
return mFeedback.get();
}

bool QgsAnnotationLayerRenderer::render()
{
QgsRenderContext &context = *renderContext();

for ( QgsAnnotationItem *item : qgis::as_const( mItems ) )
{
if ( mFeedback->isCanceled() )
break;

renderContext()->setCoordinateTransform( QgsCoordinateTransform( item->crs(), context.coordinateTransform().destinationCrs(), context.transformContext() ) );
item->render( context, mFeedback.get() );
}
return true;
}
44 changes: 44 additions & 0 deletions src/core/annotations/qgsannotationlayerrenderer.h
@@ -0,0 +1,44 @@
/***************************************************************************
qgsannotationlayerrenderer.h
----------------
copyright : (C) 2019 by Sandro Mani
email : smani at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* 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 QGSANNOTATIONLAYERRENDERER_H
#define QGSANNOTATIONLAYERRENDERER_H

#define SIP_NO_FILE

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgsmaplayerrenderer.h"

class QgsAnnotationItem;
class QgsAnnotationLayer;

class CORE_EXPORT QgsAnnotationLayerRenderer : public QgsMapLayerRenderer
{
public:

QgsAnnotationLayerRenderer( QgsAnnotationLayer *layer, QgsRenderContext &context );
~QgsAnnotationLayerRenderer() override;
QgsFeedback *feedback() const override;
bool render() override;

private:
QVector< QgsAnnotationItem *> mItems;
std::unique_ptr< QgsFeedback > mFeedback;

};

#endif // QGSANNOTATIONLAYERRENDERER_H

0 comments on commit 21a2599

Please sign in to comment.