Skip to content
Permalink
Browse files

Refactor annotations

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 c853f4fe89f94676b8c080427bfc0d0a3109464f
Showing with 2,402 additions and 1,715 deletions.
  1. +1 −0 doc/CMakeLists.txt
  2. +1 −0 python/CMakeLists.txt
  3. +62 −0 python/core/annotations/qgsannotation.sip
  4. +26 −0 python/core/annotations/qgshtmlannotation.sip
  5. +20 −0 python/core/annotations/qgssvgannotation.sip
  6. +20 −0 python/core/annotations/qgstextannotation.sip
  7. +11 −1 python/core/composer/qgscomposerutils.sip
  8. +5 −1 python/core/core.sip
  9. +0 −63 python/core/qgsannotation.sip
  10. +1 −5 python/gui/gui.sip
  11. +0 −132 python/gui/qgsannotationitem.sip
  12. +0 −30 python/gui/qgsformannotationitem.sip
  13. +0 −27 python/gui/qgshtmlannotationitem.sip
  14. +46 −0 python/gui/qgsmapcanvasannotationitem.sip
  15. +0 −18 python/gui/qgssvgannotationitem.sip
  16. +0 −19 python/gui/qgstextannotationitem.sip
  17. +1 −0 src/app/CMakeLists.txt
  18. +50 −27 src/app/qgisapp.cpp
  19. +2 −2 src/app/qgisapp.h
  20. +23 −14 src/app/qgsannotationwidget.cpp
  21. +4 −4 src/app/qgsannotationwidget.h
  22. +12 −9 src/app/qgsformannotationdialog.cpp
  23. +4 −4 src/app/qgsformannotationdialog.h
  24. +12 −9 src/app/qgshtmlannotationdialog.cpp
  25. +4 −4 src/app/qgshtmlannotationdialog.h
  26. +103 −80 src/app/qgsmaptoolannotation.cpp
  27. +15 −9 src/app/qgsmaptoolannotation.h
  28. +3 −8 src/app/qgsmaptoolformannotation.cpp
  29. +1 −1 src/app/qgsmaptoolformannotation.h
  30. +3 −8 src/app/qgsmaptoolhtmlannotation.cpp
  31. +1 −1 src/app/qgsmaptoolhtmlannotation.h
  32. +3 −8 src/app/qgsmaptoolsvgannotation.cpp
  33. +1 −1 src/app/qgsmaptoolsvgannotation.h
  34. +3 −9 src/app/qgsmaptooltextannotation.cpp
  35. +1 −1 src/app/qgsmaptooltextannotation.h
  36. +18 −10 src/app/qgssvgannotationdialog.cpp
  37. +4 −4 src/app/qgssvgannotationdialog.h
  38. +22 −21 src/app/qgstextannotationdialog.cpp
  39. +5 −6 src/app/qgstextannotationdialog.h
  40. +13 −1 src/core/CMakeLists.txt
  41. +373 −0 src/core/annotations/qgsannotation.cpp
  42. +326 −0 src/core/annotations/qgsannotation.h
  43. +104 −0 src/core/annotations/qgsannotationmanager.cpp
  44. +93 −0 src/core/annotations/qgsannotationmanager.h
  45. +53 −83 src/{gui/qgshtmlannotationitem.cpp → core/annotations/qgshtmlannotation.cpp}
  46. +46 −25 src/{gui/qgshtmlannotationitem.h → core/annotations/qgshtmlannotation.h}
  47. +21 −34 src/{gui/qgssvgannotationitem.cpp → core/annotations/qgssvgannotation.cpp}
  48. +34 −15 src/{gui/qgssvgannotationitem.h → core/annotations/qgssvgannotation.h}
  49. +24 −49 src/{gui/qgstextannotationitem.cpp → core/annotations/qgstextannotation.cpp}
  50. +67 −0 src/core/annotations/qgstextannotation.h
  51. +1 −1 src/core/composer/qgscomposerarrow.cpp
  52. +18 −19 src/core/composer/qgscomposermap.cpp
  53. +3 −2 src/core/composer/qgscomposermap.h
  54. +2 −2 src/core/composer/qgscomposermapgrid.cpp
  55. +1 −1 src/core/composer/qgscomposermapoverview.cpp
  56. +2 −2 src/core/composer/qgscomposernodesitem.cpp
  57. +1 −1 src/core/composer/qgscomposerpolygon.cpp
  58. +1 −1 src/core/composer/qgscomposerpolyline.cpp
  59. +1 −1 src/core/composer/qgscomposershape.cpp
  60. +11 −6 src/core/composer/qgscomposerutils.cpp
  61. +11 −1 src/core/composer/qgscomposerutils.h
  62. +1 −1 src/core/composer/qgspaperitem.cpp
  63. +0 −90 src/core/qgsannotation.h
  64. +5 −10 src/gui/CMakeLists.txt
  65. +0 −508 src/gui/qgsannotationitem.cpp
  66. +0 −188 src/gui/qgsannotationitem.h
  67. +44 −66 src/gui/{qgsformannotationitem.cpp → qgsformannotation.cpp}
  68. +18 −16 src/gui/{qgsformannotationitem.h → qgsformannotation.h}
  69. +2 −1 src/gui/qgsmapcanvas.cpp
  70. +228 −0 src/gui/qgsmapcanvasannotationitem.cpp
  71. +106 −0 src/gui/qgsmapcanvasannotationitem.h
  72. +0 −47 src/gui/qgstextannotationitem.h
  73. +1 −0 tests/src/core/CMakeLists.txt
  74. +60 −8 tests/src/core/testqgscomposerutils.cpp
  75. +2 −0 tests/src/python/CMakeLists.txt
  76. +131 −0 tests/src/python/test_qgsannotation.py
  77. +110 −0 tests/src/python/test_qgsmapcanvasannotationitem.py
  78. BIN tests/testdata/control_images/annotations/expected_html_annotation/expected_html_annotation.png
  79. BIN tests/testdata/control_images/annotations/expected_relative_style/expected_relative_style.png
  80. BIN tests/testdata/control_images/annotations/expected_svg_annotation/expected_svg_annotation.png
  81. BIN tests/testdata/control_images/annotations/expected_text_annotation/expected_text_annotation.png
@@ -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
@@ -93,6 +93,7 @@ INCLUDE_DIRECTORIES(SYSTEM
)
INCLUDE_DIRECTORIES(
../src/core
../src/core/annotations
../src/core/auth
../src/core/pal
../src/core/composer
@@ -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 );

};
@@ -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;

};
@@ -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;

};
@@ -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;

};
@@ -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 );
};
@@ -13,7 +13,6 @@

%Include qgis.sip

%Include qgsannotation.sip
%Include qgsapplication.sip
%Include qgsaction.sip
%Include qgsactionmanager.sip
@@ -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

This file was deleted.

@@ -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
@@ -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
@@ -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
@@ -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
@@ -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

0 comments on commit c853f4f

Please sign in to comment.
You can’t perform that action at this time.