Skip to content

Commit

Permalink
Add a destination layer property to QgsProjectGpsSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 7, 2022
1 parent 5fc94c3 commit 544641c
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 1 deletion.
33 changes: 33 additions & 0 deletions python/core/auto_generated/project/qgsprojectgpssettings.sip.in
Expand Up @@ -31,6 +31,11 @@ Constructor for QgsProjectGpsSettings with the specified ``parent`` object.

~QgsProjectGpsSettings();

void resolveReferences( const QgsProject *project );
%Docstring
Resolves reference to layers from stored layer ID (if it has not been resolved already)
%End

void reset();
%Docstring
Resets the settings to a default state.
Expand Down Expand Up @@ -69,6 +74,15 @@ layer edit buffer).
.. seealso:: :py:func:`setAutomaticallyCommitFeatures`

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

QgsVectorLayer *destinationLayer() const;
%Docstring
Returns the destination layer to be used for storing features digitized from GPS.

.. seealso:: :py:func:`setDestinationLayer`

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

public slots:
Expand All @@ -92,6 +106,15 @@ layer edit buffer).
.. seealso:: :py:func:`automaticallyCommitFeatures`

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

void setDestinationLayer( QgsVectorLayer *layer );
%Docstring
Sets the destination ``layer`` to be used for storing features digitized from GPS.

.. seealso:: :py:func:`destinationLayer`

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

signals:
Expand All @@ -114,6 +137,16 @@ is changed.
.. seealso:: :py:func:`automaticallyCommitFeatures`

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

void destinationLayerChanged( QgsVectorLayer *layer );
%Docstring
Emitted whenever the destination layer for features digitized from GPS
is changed.

.. seealso:: :py:func:`destinationLayer`

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

};
Expand Down
1 change: 1 addition & 0 deletions src/core/project/qgsproject.cpp
Expand Up @@ -2020,6 +2020,7 @@ bool QgsProject::readProjectFile( const QString &filename, Qgis::ProjectReadFlag
const QDomElement gpsSettingsElement = doc->documentElement().firstChildElement( QStringLiteral( "ProjectGpsSettings" ) );
if ( !gpsSettingsElement.isNull() )
mGpsSettings->readXml( gpsSettingsElement, context );
mGpsSettings->resolveReferences( this );
}

profile.switchTask( tr( "Updating variables" ) );
Expand Down
46 changes: 46 additions & 0 deletions src/core/project/qgsprojectgpssettings.cpp
Expand Up @@ -22,24 +22,42 @@ QgsProjectGpsSettings::QgsProjectGpsSettings( QObject *parent )

}

void QgsProjectGpsSettings::resolveReferences( const QgsProject *project )
{
mDestinationLayer.resolveWeakly( project );

emit destinationLayerChanged( mDestinationLayer.get() );
}

QgsProjectGpsSettings::~QgsProjectGpsSettings() = default;

void QgsProjectGpsSettings::reset()
{
mAutoAddTrackVertices = false;
mAutoCommitFeatures = false;

mDestinationLayer.setLayer( nullptr );

emit automaticallyAddTrackVerticesChanged( false );
emit automaticallyCommitFeaturesChanged( false );
emit destinationLayerChanged( nullptr );
}

bool QgsProjectGpsSettings::readXml( const QDomElement &element, const QgsReadWriteContext & )
{
mAutoAddTrackVertices = element.attribute( QStringLiteral( "autoAddTrackVertices" ), "0" ).toInt();
mAutoCommitFeatures = element.attribute( QStringLiteral( "autoCommitFeatures" ), "0" ).toInt();

const QString layerId = element.attribute( QStringLiteral( "destinationLayer" ) );
const QString layerName = element.attribute( QStringLiteral( "destinationLayerName" ) );
const QString layerSource = element.attribute( QStringLiteral( "destinationLayerSource" ) );
const QString layerProvider = element.attribute( QStringLiteral( "destinationLayerProvider" ) );

mDestinationLayer = QgsVectorLayerRef( layerId, layerName, layerSource, layerProvider );

emit automaticallyAddTrackVerticesChanged( mAutoAddTrackVertices );
emit automaticallyCommitFeaturesChanged( mAutoCommitFeatures );
emit destinationLayerChanged( nullptr ); // wont' be set until resolve is called
return true;
}

Expand All @@ -50,6 +68,18 @@ QDomElement QgsProjectGpsSettings::writeXml( QDomDocument &doc, const QgsReadWri
element.setAttribute( QStringLiteral( "autoAddTrackVertices" ), mAutoAddTrackVertices ? 1 : 0 );
element.setAttribute( QStringLiteral( "autoCommitFeatures" ), mAutoCommitFeatures ? 1 : 0 );

if ( mDestinationLayer )
{
element.setAttribute( QStringLiteral( "destinationLayer" ), mDestinationLayer.layerId );
element.setAttribute( QStringLiteral( "destinationLayerName" ), mDestinationLayer.name );
element.setAttribute( QStringLiteral( "destinationLayerSource" ), mDestinationLayer.source );
element.setAttribute( QStringLiteral( "destinationLayerProvider" ), mDestinationLayer.provider );
}
else
{
element.setAttribute( QStringLiteral( "destinationLayer" ), QString() );
}

return element;
}

Expand All @@ -63,6 +93,11 @@ bool QgsProjectGpsSettings::automaticallyCommitFeatures() const
return mAutoCommitFeatures;
}

QgsVectorLayer *QgsProjectGpsSettings::destinationLayer() const
{
return mDestinationLayer.get();
}

void QgsProjectGpsSettings::setAutomaticallyAddTrackVertices( bool enabled )
{
if ( enabled == mAutoAddTrackVertices )
Expand All @@ -80,3 +115,14 @@ void QgsProjectGpsSettings::setAutomaticallyCommitFeatures( bool enabled )
mAutoCommitFeatures = enabled;
emit automaticallyCommitFeaturesChanged( enabled );
}

void QgsProjectGpsSettings::setDestinationLayer( QgsVectorLayer *layer )
{
if ( layer == mDestinationLayer.get() )
{
return;
}

mDestinationLayer.setLayer( layer );
emit destinationLayerChanged( layer );
}
34 changes: 34 additions & 0 deletions src/core/project/qgsprojectgpssettings.h
Expand Up @@ -18,6 +18,7 @@
#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgis.h"
#include "qgsvectorlayerref.h"

#include <QObject>

Expand All @@ -40,6 +41,7 @@ class CORE_EXPORT QgsProjectGpsSettings : public QObject

Q_PROPERTY( bool automaticallyAddTrackVertices READ automaticallyAddTrackVertices WRITE setAutomaticallyAddTrackVertices NOTIFY automaticallyAddTrackVerticesChanged )
Q_PROPERTY( bool automaticallyCommitFeatures READ automaticallyCommitFeatures WRITE setAutomaticallyCommitFeatures NOTIFY automaticallyCommitFeaturesChanged )
Q_PROPERTY( QgsVectorLayer *destinationLayer READ destinationLayer WRITE setDestinationLayer NOTIFY destinationLayerChanged )

/**
* Constructor for QgsProjectGpsSettings with the specified \a parent object.
Expand All @@ -48,6 +50,11 @@ class CORE_EXPORT QgsProjectGpsSettings : public QObject

~QgsProjectGpsSettings() override;

/**
* Resolves reference to layers from stored layer ID (if it has not been resolved already)
*/
void resolveReferences( const QgsProject *project );

/**
* Resets the settings to a default state.
*/
Expand Down Expand Up @@ -84,6 +91,14 @@ class CORE_EXPORT QgsProjectGpsSettings : public QObject
*/
bool automaticallyCommitFeatures() const;

/**
* Returns the destination layer to be used for storing features digitized from GPS.
*
* \see setDestinationLayer()
* \see destinationLayerChanged()
*/
QgsVectorLayer *destinationLayer() const;

public slots:

/**
Expand All @@ -105,6 +120,14 @@ class CORE_EXPORT QgsProjectGpsSettings : public QObject
*/
void setAutomaticallyCommitFeatures( bool enabled );

/**
* Sets the destination \a layer to be used for storing features digitized from GPS.
*
* \see destinationLayer()
* \see destinationLayerChanged()
*/
void setDestinationLayer( QgsVectorLayer *layer );

signals:

/**
Expand All @@ -125,11 +148,22 @@ class CORE_EXPORT QgsProjectGpsSettings : public QObject
*/
void automaticallyCommitFeaturesChanged( bool enabled );

/**
* Emitted whenever the destination layer for features digitized from GPS
* is changed.
*
* \see destinationLayer()
* \see setDestinationLayer()
*/
void destinationLayerChanged( QgsVectorLayer *layer );

private:

bool mAutoAddTrackVertices = false;
bool mAutoCommitFeatures = false;

QgsVectorLayerRef mDestinationLayer;

};

#endif // QGSPROJECTGPSSETTINGS_H
49 changes: 48 additions & 1 deletion tests/src/python/test_qgsprojectgpssettings.py
Expand Up @@ -11,6 +11,7 @@
__copyright__ = 'Copyright 2022, The QGIS Project'

import qgis # NOQA
import os

from qgis.core import (QgsProjectGpsSettings,
QgsReadWriteContext,
Expand All @@ -20,7 +21,9 @@
QgsLocalDefaultSettings,
QgsUnitTypes,
QgsCoordinateReferenceSystem,
Qgis)
Qgis,
QgsVectorLayer,
QgsProject)

from qgis.PyQt.QtCore import QCoreApplication

Expand Down Expand Up @@ -77,6 +80,26 @@ def testSettings(self):
self.assertEqual(len(spy_auto_commit), 2)
self.assertFalse(spy_auto_commit[-1][0])

layer1 = QgsVectorLayer(os.path.join(unitTestDataPath(), 'lines.shp'), 'layer1')
self.assertTrue(layer1.isValid())
layer2 = QgsVectorLayer(os.path.join(unitTestDataPath(), 'points.shp'), 'layer2')
self.assertTrue(layer2.isValid())

self.assertFalse(p.destinationLayer())
spy = QSignalSpy(p.destinationLayerChanged)
p.setDestinationLayer(layer1)
self.assertEqual(len(spy), 1)
self.assertEqual(spy[0][0], layer1)
self.assertEqual(p.destinationLayer(), layer1)

p.setDestinationLayer(layer1)
self.assertEqual(len(spy), 1)

p.setDestinationLayer(layer2)
self.assertEqual(len(spy), 2)
self.assertEqual(spy[1][0], layer2)
self.assertEqual(p.destinationLayer(), layer2)

def testReset(self):
"""
Test that resetting inherits local default settings
Expand All @@ -87,35 +110,59 @@ def testReset(self):

p.setAutomaticallyCommitFeatures(True)
p.setAutomaticallyAddTrackVertices(True)

layer1 = QgsVectorLayer(os.path.join(unitTestDataPath(), 'lines.shp'), 'layer1')
self.assertTrue(layer1.isValid())
p.setDestinationLayer(layer1)

spy_add_track = QSignalSpy(p.automaticallyAddTrackVerticesChanged)
spy_auto_commit = QSignalSpy(p.automaticallyCommitFeaturesChanged)
spy_dest_layer_changed = QSignalSpy(p.destinationLayerChanged)

p.reset()
self.assertFalse(p.automaticallyAddTrackVertices())
self.assertFalse(p.automaticallyCommitFeatures())
self.assertFalse(p.destinationLayer())

self.assertEqual(len(spy_add_track), 1)
self.assertFalse(spy_auto_commit[-1][0])
self.assertEqual(len(spy_auto_commit), 1)
self.assertFalse(spy_auto_commit[-1][0])
self.assertEqual(len(spy_dest_layer_changed), 1)

def testReadWrite(self):
p = QgsProjectGpsSettings()

p.setAutomaticallyCommitFeatures(True)
p.setAutomaticallyAddTrackVertices(True)

layer1 = QgsVectorLayer(os.path.join(unitTestDataPath(), 'lines.shp'), 'layer1')
self.assertTrue(layer1.isValid())
p.setDestinationLayer(layer1)

project = QgsProject()
project.addMapLayer(layer1)

doc = QDomDocument("testdoc")
elem = p.writeXml(doc, QgsReadWriteContext())

p2 = QgsProjectGpsSettings()
spy = QSignalSpy(p2.automaticallyAddTrackVerticesChanged)
spy2 = QSignalSpy(p2.automaticallyCommitFeaturesChanged)
spy_dest_layer_changed = QSignalSpy(p2.destinationLayerChanged)

self.assertTrue(p2.readXml(elem, QgsReadWriteContext()))
self.assertEqual(len(spy), 1)
self.assertEqual(len(spy2), 1)
self.assertEqual(len(spy_dest_layer_changed), 1)
self.assertTrue(p.automaticallyCommitFeatures())
self.assertTrue(p.automaticallyAddTrackVertices())
# needs to be resolved first
self.assertFalse(p2.destinationLayer())

p2.resolveReferences(project)
self.assertEqual(len(spy_dest_layer_changed), 2)
self.assertEqual(p2.destinationLayer(), layer1)


if __name__ == '__main__':
Expand Down

0 comments on commit 544641c

Please sign in to comment.