Skip to content

Commit

Permalink
Refactor annotations
Browse files Browse the repository at this point in the history
Splits the rendering component of annotations out from map
canvas item component.

A new core abstract base class QgsAnnotation handles the
management of the common properties associated with an
annotation, and handles rendering the annotation onto a
QgsRenderContext destination.

Existing annotation classes have been ported to this, and
with the exception of the form annotation moved into core.
Form annotations are dependant on editor widgets and must
remain in GUI.

A new QgsMapCanvasAnnotationItem item class implements
a QgsMapCanvasItem which draws an annotation inside the
canvas, and handles synchronising the position and size
of the canvas item with the QgsAnnotation position/size.

This allows annotations to be safely used in a multi-canvas
environment, with a single QgsAnnotation being displayed
in multiple canvases (even if the canvases have different
extent/crs/etc).

Additionally it allows annotations to be directly rendered
to a map (eg in composer) without going through the
gui based Qt graphics scene framework.

Also removes lots of duplicate code, and adds some basic
unit tests for annotations.
  • Loading branch information
nyalldawson committed Jan 29, 2017
1 parent 8ba609e commit c853f4f
Show file tree
Hide file tree
Showing 81 changed files with 2,402 additions and 1,715 deletions.
1 change: 1 addition & 0 deletions doc/CMakeLists.txt
Expand Up @@ -48,6 +48,7 @@ IF(WITH_APIDOC)
SET(DOXYGEN_INPUT
${CMAKE_SOURCE_DIR}/doc
${CMAKE_SOURCE_DIR}/src/core
${CMAKE_SOURCE_DIR}/src/core/annotations
${CMAKE_SOURCE_DIR}/src/core/auth
${CMAKE_SOURCE_DIR}/src/core/composer
${CMAKE_SOURCE_DIR}/src/core/diagram
Expand Down
1 change: 1 addition & 0 deletions python/CMakeLists.txt
Expand Up @@ -93,6 +93,7 @@ INCLUDE_DIRECTORIES(SYSTEM
)
INCLUDE_DIRECTORIES(
../src/core
../src/core/annotations
../src/core/auth
../src/core/pal
../src/core/composer
Expand Down
62 changes: 62 additions & 0 deletions python/core/annotations/qgsannotation.sip
@@ -0,0 +1,62 @@
class QgsAnnotation : QObject
{
%TypeHeaderCode
#include <qgsannotation.h>
%End
public:

QgsAnnotation( QObject* parent /TransferThis/ = nullptr );

bool isVisible() const;
void setVisible( bool visible );

bool hasFixedMapPosition() const;
void setHasFixedMapPosition( bool fixed );

QgsPoint mapPosition() const;
void setMapPosition( const QgsPoint& position );

QgsCoordinateReferenceSystem mapPositionCrs() const;
void setMapPositionCrs( const QgsCoordinateReferenceSystem& crs );

QPointF relativePosition() const;
void setRelativePosition( QPointF position );

void setFrameOffsetFromReferencePoint( QPointF offset );
QPointF frameOffsetFromReferencePoint() const;

void setFrameSize( QSizeF size );
QSizeF frameSize() const;

void setFrameBorderWidth( double width );
double frameBorderWidth() const;

void setFrameColor( const QColor& color );
QColor frameColor() const;

void setFrameBackgroundColor( const QColor& color );
QColor frameBackgroundColor() const;

void render( QgsRenderContext& context ) const;

virtual void writeXml( QDomElement& elem, QDomDocument & doc ) const = 0;
virtual void readXml( const QDomElement& itemElem, const QDomDocument& doc ) = 0;

void setMarkerSymbol( QgsMarkerSymbol* symbol /Transfer/ );
QgsMarkerSymbol* markerSymbol() const;

signals:

void appearanceChanged();
void moved();

protected:

virtual void renderAnnotation( QgsRenderContext& context, QSizeF size ) const = 0;

virtual QSizeF minimumFrameSize() const;

void _writeXml( QDomElement& itemElem, QDomDocument& doc ) const;
void _readXml( const QDomElement& annotationElem, const QDomDocument& doc );

};
26 changes: 26 additions & 0 deletions python/core/annotations/qgshtmlannotation.sip
@@ -0,0 +1,26 @@
class QgsHtmlAnnotation : QgsAnnotation
{
%TypeHeaderCode
#include <qgshtmlannotation.h>
%End
public:

QgsHtmlAnnotation( QObject* parent /TransferThis/ = nullptr, QgsVectorLayer* vlayer = nullptr, bool hasFeature = false, int feature = 0 );

~QgsHtmlAnnotation();

QSizeF minimumFrameSize() const;

void setSourceFile( const QString& htmlFile );
QString sourceFile() const;

virtual void writeXml( QDomElement& elem, QDomDocument & doc ) const;
virtual void readXml( const QDomElement& itemElem, const QDomDocument& doc );

QgsVectorLayer* vectorLayer() const;

protected:

void renderAnnotation( QgsRenderContext& context, QSizeF size ) const;

};
20 changes: 20 additions & 0 deletions python/core/annotations/qgssvgannotation.sip
@@ -0,0 +1,20 @@
class QgsSvgAnnotation : QgsAnnotation
{
%TypeHeaderCode
#include <qgssvgannotation.h>
%End
public:

QgsSvgAnnotation( QObject* parent /TransferThis/ = nullptr );

virtual void writeXml( QDomElement& elem, QDomDocument & doc ) const;
virtual void readXml( const QDomElement& itemElem, const QDomDocument& doc );

void setFilePath( const QString& file );
QString filePath() const;

protected:

void renderAnnotation( QgsRenderContext& context, QSizeF size ) const;

};
20 changes: 20 additions & 0 deletions python/core/annotations/qgstextannotation.sip
@@ -0,0 +1,20 @@
class QgsTextAnnotation : QgsAnnotation
{
%TypeHeaderCode
#include <qgstextannotation.h>
%End
public:

QgsTextAnnotation( QObject* parent /TransferThis/ = nullptr );

const QTextDocument* document() const;
void setDocument( const QTextDocument* doc );

virtual void writeXml( QDomElement& elem, QDomDocument & doc ) const;
virtual void readXml( const QDomElement& itemElem, const QDomDocument& doc );

protected:

void renderAnnotation( QgsRenderContext& context, QSizeF size ) const;

};
12 changes: 11 additions & 1 deletion python/core/composer/qgscomposerutils.sip
Expand Up @@ -222,11 +222,21 @@ class QgsComposerUtils
*/
static void drawText( QPainter* painter, const QRectF& rect, const QString& text, const QFont& font, const QColor& color = QColor(), const Qt::AlignmentFlag halignment = Qt::AlignLeft, const Qt::AlignmentFlag valignment = Qt::AlignTop, const int flags = Qt::TextWordWrap );

/**
* Creates a render context suitable for the specified composer map and QPainter destination.
* This method returns a new QgsRenderContext which matches the scale and settings of the
* target map.
* @note added in QGIS 3.0
* @see createRenderContextForComposition()
*/
static QgsRenderContext createRenderContextForMap( QgsComposerMap* map, QPainter* painter );

/**
* Creates a render context suitable for the specified composition and QPainter destination.
* This method returns a new QgsRenderContext which matches the scale and settings from the composition's
* QgsComposition::referenceMap().
* @note added in QGIS 3.0
* @see createRenderContextForMap()
*/
static QgsRenderContext createRenderContext( QgsComposition* composition, QPainter* painter );
static QgsRenderContext createRenderContextForComposition( QgsComposition* composition, QPainter* painter );
};
6 changes: 5 additions & 1 deletion python/core/core.sip
Expand Up @@ -13,7 +13,6 @@

%Include qgis.sip

%Include qgsannotation.sip
%Include qgsapplication.sip
%Include qgsaction.sip
%Include qgsactionmanager.sip
Expand Down Expand Up @@ -174,6 +173,11 @@
%Include qgsmapthemecollection.sip
%Include qgsxmlutils.sip

%Include annotations/qgsannotation.sip
%Include annotations/qgshtmlannotation.sip
%Include annotations/qgssvgannotation.sip
%Include annotations/qgstextannotation.sip

%Include auth/qgsauthcertutils.sip
%Include auth/qgsauthconfig.sip
// %Include auth/qgsauthcrypto.sip
Expand Down
63 changes: 0 additions & 63 deletions python/core/qgsannotation.sip

This file was deleted.

6 changes: 1 addition & 5 deletions python/gui/gui.sip
Expand Up @@ -20,7 +20,6 @@
%Include qgsactionmenu.sip
%Include qgsadvanceddigitizingcanvasitem.sip
%Include qgsadvanceddigitizingdockwidget.sip
%Include qgsannotationitem.sip
%Include qgsattributedialog.sip
%Include qgsattributeeditorcontext.sip
%Include qgsattributeform.sip
Expand Down Expand Up @@ -81,7 +80,6 @@
%Include qgsfilterlineedit.sip
%Include qgsfloatingwidget.sip
%Include qgsfocuswatcher.sip
%Include qgsformannotationitem.sip
%Include qgsgenericprojectionselector.sip
%Include qgsgeometryrubberband.sip
%Include qgsgradientcolorrampdialog.sip
Expand All @@ -90,7 +88,6 @@
%Include qgshelp.sip
%Include qgshighlight.sip
%Include qgshistogramwidget.sip
%Include qgshtmlannotationitem.sip
%Include qgsidentifymenu.sip
%Include qgskeyvaluewidget.sip
%Include qgslistwidget.sip
Expand All @@ -100,6 +97,7 @@
%Include qgsludialog.sip
%Include qgsmanageconnectionsdialog.sip
%Include qgsmapcanvas.sip
%Include qgsmapcanvasannotationitem.sip
%Include qgsmapcanvasitem.sip
%Include qgsmapcanvassnapper.sip
%Include qgsmapcanvassnappingutils.sip
Expand Down Expand Up @@ -159,12 +157,10 @@
%Include qgssourceselectdialog.sip
%Include qgssublayersdialog.sip
%Include qgssubstitutionlistwidget.sip
%Include qgssvgannotationitem.sip
%Include qgstablewidgetbase.sip
%Include qgstabwidget.sip
%Include qgstablewidgetitem.sip
%Include qgstaskmanagerwidget.sip
%Include qgstextannotationitem.sip
%Include qgstextformatwidget.sip
%Include qgstextpreview.sip
%Include qgstreewidgetitem.sip
Expand Down

0 comments on commit c853f4f

Please sign in to comment.