From a87206dbdf2712e83ab061ae85d3ddbcd1a85f03 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 9 Sep 2021 15:04:34 +1000 Subject: [PATCH] Create QgsAbstractAnnotationItemEditOperation as base class for edit operations on annotation layers, with initial implementation as QgsAnnotationItemEditOperationMoveNode This class encapsulates edit operations which apply to annotation layers/items --- .../qgsannotationitemeditoperation.sip.in | 88 +++++++++++++++++ python/core/core_auto.sip | 1 + src/core/CMakeLists.txt | 2 + .../qgsannotationitemeditoperation.cpp | 43 +++++++++ .../qgsannotationitemeditoperation.h | 96 +++++++++++++++++++ tests/src/python/CMakeLists.txt | 1 + .../test_qgsannotationitemeditoperation.py | 45 +++++++++ 7 files changed, 276 insertions(+) create mode 100644 python/core/auto_generated/annotations/qgsannotationitemeditoperation.sip.in create mode 100644 src/core/annotations/qgsannotationitemeditoperation.cpp create mode 100644 src/core/annotations/qgsannotationitemeditoperation.h create mode 100644 tests/src/python/test_qgsannotationitemeditoperation.py diff --git a/python/core/auto_generated/annotations/qgsannotationitemeditoperation.sip.in b/python/core/auto_generated/annotations/qgsannotationitemeditoperation.sip.in new file mode 100644 index 00000000000..2dad4937f79 --- /dev/null +++ b/python/core/auto_generated/annotations/qgsannotationitemeditoperation.sip.in @@ -0,0 +1,88 @@ +/************************************************************************ + * This file has been generated automatically from * + * * + * src/core/annotations/qgsannotationitemeditoperation.h * + * * + * Do not edit manually ! Edit header and run scripts/sipify.pl again * + ************************************************************************/ + + + + +class QgsAbstractAnnotationItemEditOperation +{ +%Docstring(signature="appended") +Abstract base class for annotation item edit operations + +.. versionadded:: 3.22 +%End + +%TypeHeaderCode +#include "qgsannotationitemeditoperation.h" +%End + public: + + QgsAbstractAnnotationItemEditOperation( const QString &itemId ); +%Docstring +Constructor for QgsAbstractAnnotationItemEditOperation, for the specified item id. +%End + + virtual ~QgsAbstractAnnotationItemEditOperation(); + + QString itemId() const; +%Docstring +Returns the associated item ID. +%End + + protected: + + +}; + +class QgsAnnotationItemEditOperationMoveNode : QgsAbstractAnnotationItemEditOperation +{ +%Docstring(signature="appended") +Annotation item edit operation consisting of moving a node + +.. versionadded:: 3.22 +%End + +%TypeHeaderCode +#include "qgsannotationitemeditoperation.h" +%End + public: + + QgsAnnotationItemEditOperationMoveNode( const QString &itemId, QgsVertexId nodeId, const QgsPointXY &before, const QgsPointXY &after ); +%Docstring +Constructor for QgsAnnotationItemEditOperationMoveNode, where the node with the specified ``id`` moves +from ``before`` to ``after`` (in layer coordinates). +%End + + QgsVertexId nodeId() const; +%Docstring +Returns the associated node ID. +%End + + QgsPointXY before() const; +%Docstring +Returns the node position before the move occurred (in layer coordinates). + +.. seealso:: :py:func:`after` +%End + + QgsPointXY after() const; +%Docstring +Returns the node position after the move occurred (in layer coordinates). + +.. seealso:: :py:func:`before` +%End + +}; + +/************************************************************************ + * This file has been generated automatically from * + * * + * src/core/annotations/qgsannotationitemeditoperation.h * + * * + * Do not edit manually ! Edit header and run scripts/sipify.pl again * + ************************************************************************/ diff --git a/python/core/core_auto.sip b/python/core/core_auto.sip index b4cc7d4afa5..72322142d29 100644 --- a/python/core/core_auto.sip +++ b/python/core/core_auto.sip @@ -205,6 +205,7 @@ %Include auto_generated/./3d/qgsabstract3drenderer.sip %Include auto_generated/annotations/qgsannotation.sip %Include auto_generated/annotations/qgsannotationitem.sip +%Include auto_generated/annotations/qgsannotationitemeditoperation.sip %Include auto_generated/annotations/qgsannotationitemnode.sip %Include auto_generated/annotations/qgsannotationitemregistry.sip %Include auto_generated/annotations/qgsannotationlayer.sip diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 51e96c491ae..eba1dd50dcc 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -175,6 +175,7 @@ set(QGIS_CORE_SRCS annotations/qgsannotation.cpp annotations/qgsannotationitem.cpp + annotations/qgsannotationitemeditoperation.cpp annotations/qgsannotationitemregistry.cpp annotations/qgsannotationlayer.cpp annotations/qgsannotationlayerrenderer.cpp @@ -1154,6 +1155,7 @@ set(QGIS_CORE_HDRS annotations/qgsannotation.h annotations/qgsannotationitem.h + annotations/qgsannotationitemeditoperation.h annotations/qgsannotationitemnode.h annotations/qgsannotationitemregistry.h annotations/qgsannotationlayer.h diff --git a/src/core/annotations/qgsannotationitemeditoperation.cpp b/src/core/annotations/qgsannotationitemeditoperation.cpp new file mode 100644 index 00000000000..ed87b8e5195 --- /dev/null +++ b/src/core/annotations/qgsannotationitemeditoperation.cpp @@ -0,0 +1,43 @@ +/*************************************************************************** + qgsannotationitemeditoperation.cpp + ---------------- + 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 "qgsannotationitemeditoperation.h" + +// +// QgsAbstractAnnotationItemEditOperation +// +QgsAbstractAnnotationItemEditOperation::QgsAbstractAnnotationItemEditOperation( const QString &itemId ) + : mItemId( itemId ) +{ + +} + +QgsAbstractAnnotationItemEditOperation::~QgsAbstractAnnotationItemEditOperation() = default; + + +// +// QgsAnnotationItemEditOperationMoveNode +// +QgsAnnotationItemEditOperationMoveNode::QgsAnnotationItemEditOperationMoveNode( const QString &itemId, QgsVertexId nodeId, const QgsPointXY &before, const QgsPointXY &after ) + : QgsAbstractAnnotationItemEditOperation( itemId ) + , mNodeId( nodeId ) + , mBefore( before ) + , mAfter( after ) +{ + +} + + diff --git a/src/core/annotations/qgsannotationitemeditoperation.h b/src/core/annotations/qgsannotationitemeditoperation.h new file mode 100644 index 00000000000..ad8fe57acd4 --- /dev/null +++ b/src/core/annotations/qgsannotationitemeditoperation.h @@ -0,0 +1,96 @@ +/*************************************************************************** + qgsannotationitemeditoperation.h + ---------------- + 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 QGSANNOTATIONITEMEDITOPERATION_H +#define QGSANNOTATIONITEMEDITOPERATION_H + +#include "qgis_core.h" +#include "qgis_sip.h" +#include "qgis.h" +#include "qgspointxy.h" +#include "qgsabstractgeometry.h" +#include "qgsgeometry.h" + +/** + * \ingroup core + * \brief Abstract base class for annotation item edit operations + * \since QGIS 3.22 + */ +class CORE_EXPORT QgsAbstractAnnotationItemEditOperation +{ + public: + + /** + * Constructor for QgsAbstractAnnotationItemEditOperation, for the specified item id. + */ + QgsAbstractAnnotationItemEditOperation( const QString &itemId ); + + virtual ~QgsAbstractAnnotationItemEditOperation(); + + /** + * Returns the associated item ID. + */ + QString itemId() const { return mItemId; } + + protected: + + QString mItemId; + +}; + +/** + * \ingroup core + * \brief Annotation item edit operation consisting of moving a node + * \since QGIS 3.22 + */ +class CORE_EXPORT QgsAnnotationItemEditOperationMoveNode : public QgsAbstractAnnotationItemEditOperation +{ + public: + + /** + * Constructor for QgsAnnotationItemEditOperationMoveNode, where the node with the specified \a id moves + * from \a before to \a after (in layer coordinates). + */ + QgsAnnotationItemEditOperationMoveNode( const QString &itemId, QgsVertexId nodeId, const QgsPointXY &before, const QgsPointXY &after ); + + /** + * Returns the associated node ID. + */ + QgsVertexId nodeId() const { return mNodeId; } + + /** + * Returns the node position before the move occurred (in layer coordinates). + * + * \see after() + */ + QgsPointXY before() const { return mBefore; } + + /** + * Returns the node position after the move occurred (in layer coordinates). + * + * \see before() + */ + QgsPointXY after() const { return mAfter; } + + private: + + QgsVertexId mNodeId; + QgsPointXY mBefore; + QgsPointXY mAfter; + +}; + +#endif // QGSANNOTATIONITEMEDITOPERATION_H diff --git a/tests/src/python/CMakeLists.txt b/tests/src/python/CMakeLists.txt index 27b62a12f6f..1c2f2018f41 100644 --- a/tests/src/python/CMakeLists.txt +++ b/tests/src/python/CMakeLists.txt @@ -20,6 +20,7 @@ ADD_PYTHON_TEST(PyQgsArcGisPortalUtils test_qgsarcgisportalutils.py) ADD_PYTHON_TEST(PyQgsPythonProvider test_provider_python.py) ADD_PYTHON_TEST(PyQgsAggregateCalculator test_qgsaggregatecalculator.py) ADD_PYTHON_TEST(PyQgsAnnotation test_qgsannotation.py) +ADD_PYTHON_TEST(PyQgsAnnotationItemEditOperation test_qgsannotationitemeditoperation.py) ADD_PYTHON_TEST(PyQgsAnnotationItemNode test_qgsannotationitemnode.py) ADD_PYTHON_TEST(PyQgsAnnotationLayer test_qgsannotationlayer.py) ADD_PYTHON_TEST(PyQgsAnnotationLineItem test_qgsannotationlineitem.py) diff --git a/tests/src/python/test_qgsannotationitemeditoperation.py b/tests/src/python/test_qgsannotationitemeditoperation.py new file mode 100644 index 00000000000..f2f5a1382f9 --- /dev/null +++ b/tests/src/python/test_qgsannotationitemeditoperation.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +"""QGIS Unit tests for QgsAnnotationItemEditOperation + +From build dir, run: ctest -R QgsAnnotationItemEditOperation -V + +.. note:: 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. +""" +__author__ = '(C) 2020 by Nyall Dawson' +__date__ = '09/09/2020' +__copyright__ = 'Copyright 2020, The QGIS Project' + +import qgis # NOQA +from qgis.core import ( + QgsAbstractAnnotationItemEditOperation, + QgsAnnotationItemEditOperationMoveNode, + QgsVertexId, + QgsPointXY +) +from qgis.testing import start_app, unittest + +from utilities import unitTestDataPath + +start_app() +TEST_DATA_DIR = unitTestDataPath() + + +class TestQgsAnnotationItemEditOperation(unittest.TestCase): + + def test_basic(self): + base = QgsAbstractAnnotationItemEditOperation('my item') + self.assertEqual(base.itemId(), 'my item') + + def test_move_operation(self): + operation = QgsAnnotationItemEditOperationMoveNode('item id', QgsVertexId(1, 2, 3), QgsPointXY(4, 5), QgsPointXY(6, 7)) + self.assertEqual(operation.itemId(), 'item id') + self.assertEqual(operation.nodeId(), QgsVertexId(1, 2, 3)) + self.assertEqual(operation.before(), QgsPointXY(4, 5)) + self.assertEqual(operation.after(), QgsPointXY(6, 7)) + + +if __name__ == '__main__': + unittest.main()