Add explicit setter for project's transform context

And add transformContextChanged signal, so that we can detect
when the context is changed.
This commit is contained in:
Nyall Dawson 2017-11-06 13:46:44 +10:00
parent 1a73fef4a7
commit 620139054d
5 changed files with 52 additions and 10 deletions

View File

@ -128,17 +128,29 @@ Returns the QgsProject singleton instance
%End
QgsCoordinateTransformContext &transformContext();
QgsCoordinateTransformContext transformContext() const;
%Docstring
Returns a modifiable reference to the project's coordinate transform context, which stores various
Returns a copy of the project's coordinate transform context, which stores various
information regarding which datum transforms should be used when transforming points
from a source to destination coordinate reference system.
.. versionadded:: 3.0
.. seealso:: setTransformContext()
.. seealso:: transformContextChanged()
:rtype: QgsCoordinateTransformContext
%End
void setTransformContext( const QgsCoordinateTransformContext &context );
%Docstring
Sets the project's coordinate transform ``context``, which stores various
information regarding which datum transforms should be used when transforming points
from a source to destination coordinate reference system.
.. versionadded:: 3.0
.. seealso:: transformContext()
.. seealso:: transformContextChanged()
%End
void clear();
%Docstring
Clear the project - removes all settings and resets it back to an empty, default state.
@ -924,6 +936,15 @@ emitted whenever the configuration for snapping has changed
.. seealso:: :py:func:`ellipsoid()`
%End
void transformContextChanged();
%Docstring
Emitted when the project transformContext() is changed.
.. versionadded:: 3.0
.. seealso:: transformContext()
%End
void transactionGroupsChanged();
%Docstring
Emitted whenever a new transaction group has been created or a

View File

@ -476,9 +476,10 @@ QgsCoordinateTransformContext QgsProject::transformContext() const
return mTransformContext;
}
QgsCoordinateTransformContext &QgsProject::transformContext()
void QgsProject::setTransformContext( const QgsCoordinateTransformContext &context )
{
return mTransformContext;
mTransformContext = context;
emit transformContextChanged();
}
void QgsProject::clear()

View File

@ -186,17 +186,21 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
* from a source to destination coordinate reference system.
*
* \since QGIS 3.0
* \see setTransformContext()
* \see transformContextChanged()
*/
QgsCoordinateTransformContext transformContext() const SIP_SKIP;
QgsCoordinateTransformContext transformContext() const;
/**
* Returns a modifiable reference to the project's coordinate transform context, which stores various
* Sets the project's coordinate transform \a context, which stores various
* information regarding which datum transforms should be used when transforming points
* from a source to destination coordinate reference system.
*
* \since QGIS 3.0
* \see transformContext()
* \see transformContextChanged()
*/
QgsCoordinateTransformContext &transformContext();
void setTransformContext( const QgsCoordinateTransformContext &context );
/**
* Clear the project - removes all settings and resets it back to an empty, default state.
@ -920,6 +924,15 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
void ellipsoidChanged( const QString &ellipsoid );
/**
* Emitted when the project transformContext() is changed.
*
* \since QGIS 3.0
* \see transformContext()
*/
void transformContextChanged();
/**
* Emitted whenever a new transaction group has been created or a
* transaction group has been removed.

View File

@ -138,7 +138,9 @@ class TestQgsCoordinateTransform(unittest.TestCase):
Test creating transform using convenience constructor which takes project reference
"""
p = QgsProject()
p.transformContext().addSourceDatumTransform(QgsCoordinateReferenceSystem('EPSG:28356'), 1)
context = p.transformContext()
context.addSourceDatumTransform(QgsCoordinateReferenceSystem('EPSG:28356'), 1)
p.setTransformContext(context)
transform = QgsCoordinateTransform(QgsCoordinateReferenceSystem('EPSG:28356'), QgsCoordinateReferenceSystem('EPSG:28353'), p)
self.assertEqual(transform.sourceDatumTransform(), 1)

View File

@ -20,6 +20,7 @@ from qgis.core import (QgsCoordinateReferenceSystem,
QgsProject)
from qgis.testing import start_app, unittest
from qgis.PyQt.QtXml import QDomDocument
from qgis.PyQt.QtTest import QSignalSpy
app = start_app()
@ -220,7 +221,11 @@ class TestQgsCoordinateTransformContext(unittest.TestCase):
Test project's transform context
"""
project = QgsProject()
project.transformContext().addSourceDatumTransform(QgsCoordinateReferenceSystem('EPSG:3111'), 1)
context_changed_spy = QSignalSpy(project.transformContextChanged)
context = project.transformContext()
context.addSourceDatumTransform(QgsCoordinateReferenceSystem('EPSG:3111'), 1)
project.setTransformContext(context)
self.assertEqual(len(context_changed_spy), 1)
self.assertEqual(project.transformContext().sourceDatumTransforms(), {'EPSG:3111': 1})