Skip to content
Permalink
Browse files
Use an edit operation for annotation item translation, instead of
transform

Will make it easier to add undo/redo support in future
  • Loading branch information
nyalldawson committed Sep 10, 2021
1 parent f76b262 commit a7d299df92a1a451cd3e8351631395b951bab618
Showing with 255 additions and 165 deletions.
  1. +2 −1 python/core/auto_additions/qgsannotationitemeditoperation.py
  2. +0 −19 python/core/auto_generated/annotations/qgsannotationitem.sip.in
  3. +45 −0 python/core/auto_generated/annotations/qgsannotationitemeditoperation.sip.in
  4. +0 −4 python/core/auto_generated/annotations/qgsannotationlineitem.sip.in
  5. +0 −2 python/core/auto_generated/annotations/qgsannotationmarkeritem.sip.in
  6. +0 −2 python/core/auto_generated/annotations/qgsannotationpointtextitem.sip.in
  7. +0 −4 python/core/auto_generated/annotations/qgsannotationpolygonitem.sip.in
  8. +0 −5 src/core/annotations/qgsannotationitem.cpp
  9. +0 −19 src/core/annotations/qgsannotationitem.h
  10. +17 −0 src/core/annotations/qgsannotationitemeditoperation.cpp
  11. +47 −0 src/core/annotations/qgsannotationitemeditoperation.h
  12. +17 −11 src/core/annotations/qgsannotationlineitem.cpp
  13. +0 −2 src/core/annotations/qgsannotationlineitem.h
  14. +14 −6 src/core/annotations/qgsannotationmarkeritem.cpp
  15. +0 −1 src/core/annotations/qgsannotationmarkeritem.h
  16. +14 −9 src/core/annotations/qgsannotationpointtextitem.cpp
  17. +0 −1 src/core/annotations/qgsannotationpointtextitem.h
  18. +17 −11 src/core/annotations/qgsannotationpolygonitem.cpp
  19. +0 −2 src/core/annotations/qgsannotationpolygonitem.h
  20. +44 −35 src/gui/annotations/qgsmaptoolmodifyannotation.cpp
  21. +0 −5 tests/src/core/testqgsannotationitemregistry.cpp
  22. +0 −1 tests/src/gui/testqgsannotationitemguiregistry.cpp
  23. +8 −9 tests/src/python/test_qgsannotationlineitem.py
  24. +10 −3 tests/src/python/test_qgsannotationmarkeritem.py
  25. +9 −2 tests/src/python/test_qgsannotationpointtextitem.py
  26. +11 −11 tests/src/python/test_qgsannotationpolygonitem.py
@@ -2,5 +2,6 @@
# monkey patching scoped based enum
QgsAbstractAnnotationItemEditOperation.Type.MoveNode.__doc__ = "Move a node"
QgsAbstractAnnotationItemEditOperation.Type.DeleteNode.__doc__ = "Delete a node"
QgsAbstractAnnotationItemEditOperation.Type.__doc__ = 'Operation type\n\n' + '* ``MoveNode``: ' + QgsAbstractAnnotationItemEditOperation.Type.MoveNode.__doc__ + '\n' + '* ``DeleteNode``: ' + QgsAbstractAnnotationItemEditOperation.Type.DeleteNode.__doc__
QgsAbstractAnnotationItemEditOperation.Type.TranslateItem.__doc__ = "Translate (move) an item"
QgsAbstractAnnotationItemEditOperation.Type.__doc__ = 'Operation type\n\n' + '* ``MoveNode``: ' + QgsAbstractAnnotationItemEditOperation.Type.MoveNode.__doc__ + '\n' + '* ``DeleteNode``: ' + QgsAbstractAnnotationItemEditOperation.Type.DeleteNode.__doc__ + '\n' + '* ``TranslateItem``: ' + QgsAbstractAnnotationItemEditOperation.Type.TranslateItem.__doc__
# --
@@ -82,15 +82,6 @@ Returns the bounding box of the item's geographic location, in the parent layer'
virtual QgsRectangle boundingBox( QgsRenderContext &context ) const;
%Docstring
Returns the bounding box of the item's geographic location, in the parent layer's coordinate reference system.
%End

virtual bool transform( const QTransform &transform ) = 0;
%Docstring
Transforms the item's geometry using the specified ``transform``.

Returns ``True`` if the transformation was successful.

.. versionadded:: 3.22
%End

virtual void render( QgsRenderContext &context, QgsFeedback *feedback ) = 0;
@@ -212,16 +203,6 @@ exactly 2mm thick when a map is rendered at 1:1000, or 1mm thick when rendered a
.. seealso:: :py:func:`symbologyReferenceScale`

.. seealso:: :py:func:`setUseSymbologyReferenceScale`
%End

virtual QgsGeometry rubberBandGeometry() const;
%Docstring
Returns the geometry to use as a rubber band for map tools which manipulate the item.

The default implementation returns a null geometry, which indicates that the item bounds should
be used as the rubber band.

.. versionadded:: 3.22
%End

protected:
@@ -26,6 +26,7 @@ Abstract base class for annotation item edit operations
{
MoveNode,
DeleteNode,
TranslateItem,
};

QgsAbstractAnnotationItemEditOperation( const QString &itemId );
@@ -128,6 +129,50 @@ Returns the node position before the delete occurred (in layer coordinates).

};


class QgsAnnotationItemEditOperationTranslateItem : QgsAbstractAnnotationItemEditOperation
{
%Docstring(signature="appended")
Annotation item edit operation consisting of translating (moving) an item

.. versionadded:: 3.22
%End

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

QgsAnnotationItemEditOperationTranslateItem( const QString &itemId, double translateX, double translateY );
%Docstring
Constructor for QgsAnnotationItemEditOperationTranslateItem, where the node with the specified ``id`` and translation
(in map units)
%End

virtual Type type() const;


QgsVertexId nodeId() const;
%Docstring
Returns the deleted node ID.
%End

double translationX() const;
%Docstring
Returns the x-axis translation, in layer units.

\since :py:func:`~QgsAnnotationItemEditOperationTranslateItem.translationY`
%End

double translationY() const;
%Docstring
Returns the y-axis translation, in layer units.

\since :py:func:`~QgsAnnotationItemEditOperationTranslateItem.translationX`
%End

};

class QgsAnnotationItemEditOperationTransientResults
{
%Docstring(signature="appended")
@@ -37,10 +37,6 @@ Constructor for QgsAnnotationLineItem, with the specified ``linestring``.

virtual QList< QgsAnnotationItemNode > nodes() const;

virtual QgsGeometry rubberBandGeometry() const;

virtual bool transform( const QTransform &transform );

virtual Qgis::AnnotationItemEditOperationResult applyEdit( QgsAbstractAnnotationItemEditOperation *operation );

virtual QgsAnnotationItemEditOperationTransientResults *transientEditResults( QgsAbstractAnnotationItemEditOperation *operation ) /Factory/;
@@ -57,8 +57,6 @@ Creates a new marker annotation item.

virtual QgsRectangle boundingBox( QgsRenderContext &context ) const;

virtual bool transform( const QTransform &transform );


QgsPointXY geometry() const;
%Docstring
@@ -53,8 +53,6 @@ Creates a new text at point annotation item.

virtual QList< QgsAnnotationItemNode > nodes() const;

virtual bool transform( const QTransform &transform );

virtual Qgis::AnnotationItemEditOperationResult applyEdit( QgsAbstractAnnotationItemEditOperation *operation );

virtual QgsAnnotationItemEditOperationTransientResults *transientEditResults( QgsAbstractAnnotationItemEditOperation *operation ) /Factory/;
@@ -37,10 +37,6 @@ Constructor for QgsAnnotationPolygonItem, with the specified ``polygon`` geometr

virtual QList< QgsAnnotationItemNode > nodes() const;

virtual QgsGeometry rubberBandGeometry() const;

virtual bool transform( const QTransform &transform );

virtual Qgis::AnnotationItemEditOperationResult applyEdit( QgsAbstractAnnotationItemEditOperation *operation );

virtual QgsAnnotationItemEditOperationTransientResults *transientEditResults( QgsAbstractAnnotationItemEditOperation *operation ) /Factory/;
@@ -60,8 +60,3 @@ bool QgsAnnotationItem::readCommonProperties( const QDomElement &element, const
setSymbologyReferenceScale( element.attribute( QStringLiteral( "referenceScale" ) ).toDouble() );
return true;
}

QgsGeometry QgsAnnotationItem::rubberBandGeometry() const
{
return QgsGeometry();
}
@@ -113,15 +113,6 @@ class CORE_EXPORT QgsAnnotationItem
*/
virtual QgsRectangle boundingBox( QgsRenderContext &context ) const { Q_UNUSED( context ) return boundingBox();}

/**
* Transforms the item's geometry using the specified \a transform.
*
* Returns TRUE if the transformation was successful.
*
* \since QGIS 3.22
*/
virtual bool transform( const QTransform &transform ) = 0;

/**
* Renders the item to the specified render \a context.
*
@@ -237,16 +228,6 @@ class CORE_EXPORT QgsAnnotationItem
*/
void setSymbologyReferenceScale( double scale ) { mReferenceScale = scale; }

/**
* Returns the geometry to use as a rubber band for map tools which manipulate the item.
*
* The default implementation returns a null geometry, which indicates that the item bounds should
* be used as the rubber band.
*
* \since QGIS 3.22
*/
virtual QgsGeometry rubberBandGeometry() const;

protected:

/**
@@ -62,3 +62,20 @@ QgsAbstractAnnotationItemEditOperation::Type QgsAnnotationItemEditOperationDelet
{
return Type::DeleteNode;
}

//
// QgsAnnotationItemEditOperationTranslateItem
//

QgsAnnotationItemEditOperationTranslateItem::QgsAnnotationItemEditOperationTranslateItem( const QString &itemId, double translateX, double translateY )
: QgsAbstractAnnotationItemEditOperation( itemId )
, mTranslateX( translateX )
, mTranslateY( translateY )
{

}

QgsAbstractAnnotationItemEditOperation::Type QgsAnnotationItemEditOperationTranslateItem::type() const
{
return Type::TranslateItem;
}
@@ -40,6 +40,7 @@ class CORE_EXPORT QgsAbstractAnnotationItemEditOperation
{
MoveNode, //!< Move a node
DeleteNode, //!< Delete a node
TranslateItem, //!< Translate (move) an item
};

/**
@@ -144,6 +145,52 @@ class CORE_EXPORT QgsAnnotationItemEditOperationDeleteNode : public QgsAbstractA

};


/**
* \ingroup core
* \brief Annotation item edit operation consisting of translating (moving) an item
* \since QGIS 3.22
*/
class CORE_EXPORT QgsAnnotationItemEditOperationTranslateItem : public QgsAbstractAnnotationItemEditOperation
{
public:

/**
* Constructor for QgsAnnotationItemEditOperationTranslateItem, where the node with the specified \a id and translation
* (in map units)
*/
QgsAnnotationItemEditOperationTranslateItem( const QString &itemId, double translateX, double translateY );

Type type() const override;

/**
* Returns the deleted node ID.
*/
QgsVertexId nodeId() const { return mNodeId; }

/**
* Returns the x-axis translation, in layer units.
*
* \since translationY()
*/
double translationX() const { return mTranslateX; }

/**
* Returns the y-axis translation, in layer units.
*
* \since translationX()
*/
double translationY() const { return mTranslateY; }

private:

QgsVertexId mNodeId;
double mTranslateX = 0;
double mTranslateY = 0;


};

/**
* \ingroup core
* \brief Encapsulates the transient results of an in-progress annotation edit operation.
@@ -92,17 +92,6 @@ QList<QgsAnnotationItemNode> QgsAnnotationLineItem::nodes() const
return res;
}

QgsGeometry QgsAnnotationLineItem::rubberBandGeometry() const
{
return QgsGeometry( mCurve->clone() );
}

bool QgsAnnotationLineItem::transform( const QTransform &transform )
{
mCurve->transform( transform );
return true;
}

Qgis::AnnotationItemEditOperationResult QgsAnnotationLineItem::applyEdit( QgsAbstractAnnotationItemEditOperation *operation )
{
switch ( operation->type() )
@@ -122,6 +111,14 @@ Qgis::AnnotationItemEditOperationResult QgsAnnotationLineItem::applyEdit( QgsAbs
return mCurve->isEmpty() ? Qgis::AnnotationItemEditOperationResult::ItemCleared : Qgis::AnnotationItemEditOperationResult::Success;
break;
}

case QgsAbstractAnnotationItemEditOperation::Type::TranslateItem:
{
QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
mCurve->transform( transform );
return Qgis::AnnotationItemEditOperationResult::Success;
}
}

return Qgis::AnnotationItemEditOperationResult::Invalid;
@@ -142,6 +139,15 @@ QgsAnnotationItemEditOperationTransientResults *QgsAnnotationLineItem::transient
break;
}

case QgsAbstractAnnotationItemEditOperation::Type::TranslateItem:
{
QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
modifiedCurve->transform( transform );
return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
}

case QgsAbstractAnnotationItemEditOperation::Type::DeleteNode:
break;
}
@@ -44,8 +44,6 @@ class CORE_EXPORT QgsAnnotationLineItem : public QgsAnnotationItem
void render( QgsRenderContext &context, QgsFeedback *feedback ) override;
bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
QList< QgsAnnotationItemNode > nodes() const override;
QgsGeometry rubberBandGeometry() const override;
bool transform( const QTransform &transform ) override;
Qgis::AnnotationItemEditOperationResult applyEdit( QgsAbstractAnnotationItemEditOperation *operation ) override;
QgsAnnotationItemEditOperationTransientResults *transientEditResults( QgsAbstractAnnotationItemEditOperation *operation ) override SIP_FACTORY;

@@ -95,6 +95,14 @@ Qgis::AnnotationItemEditOperationResult QgsAnnotationMarkerItem::applyEdit( QgsA
{
return Qgis::AnnotationItemEditOperationResult::ItemCleared;
}

case QgsAbstractAnnotationItemEditOperation::Type::TranslateItem:
{
QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
mPoint.setX( mPoint.x() + moveOperation->translationX() );
mPoint.setY( mPoint.y() + moveOperation->translationY() );
return Qgis::AnnotationItemEditOperationResult::Success;
}
}

return Qgis::AnnotationItemEditOperationResult::Invalid;
@@ -110,6 +118,12 @@ QgsAnnotationItemEditOperationTransientResults *QgsAnnotationMarkerItem::transie
return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( moveOperation->after().clone() ) );
}

case QgsAbstractAnnotationItemEditOperation::Type::TranslateItem:
{
QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( new QgsPoint( mPoint.x() + moveOperation->translationX(), mPoint.y() + moveOperation->translationY() ) ) );
}

case QgsAbstractAnnotationItemEditOperation::Type::DeleteNode:
break;
}
@@ -177,12 +191,6 @@ QgsRectangle QgsAnnotationMarkerItem::boundingBox( QgsRenderContext &context ) c
return context.coordinateTransform().transformBoundingBox( boundsMapUnits, QgsCoordinateTransform::ReverseTransform );
}

bool QgsAnnotationMarkerItem::transform( const QTransform &transform )
{
mPoint.transform( transform );
return true;
}

const QgsMarkerSymbol *QgsAnnotationMarkerItem::symbol() const
{
return mSymbol.get();
@@ -56,7 +56,6 @@ class CORE_EXPORT QgsAnnotationMarkerItem : public QgsAnnotationItem
QgsAnnotationMarkerItem *clone() override SIP_FACTORY;
QgsRectangle boundingBox() const override;
QgsRectangle boundingBox( QgsRenderContext &context ) const override;
bool transform( const QTransform &transform ) override;

/**
* Returns the point geometry of the marker.

0 comments on commit a7d299d

Please sign in to comment.