QGIS API Documentation 3.37.0-Master (fa0c88a90b0)
qgsprojectgpssettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprojectgpssettings.cpp
3 ---------------------------
4 begin : November 2022
5 copyright : (C) 2022 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
16
17#include <QDomElement>
18
20 : QObject( parent )
21{
22
23}
24
26{
27 mDestinationLayer.resolveWeakly( project );
28
29 emit destinationLayerChanged( mDestinationLayer.get() );
30
31 if ( mDestinationLayer )
32 {
33 emit destinationTimeStampFieldChanged( mDestinationTimestampFields.value( mDestinationLayer->id() ) );
34 }
35}
36
38
40{
41 mAutoAddTrackVertices = false;
42 mAutoCommitFeatures = false;
43 mDestinationFollowsActiveLayer = true;
44
45 mDestinationLayer.setLayer( nullptr );
46 mDestinationTimestampFields.clear();
47
48 emit automaticallyAddTrackVerticesChanged( mAutoAddTrackVertices );
49 emit automaticallyCommitFeaturesChanged( mAutoCommitFeatures );
50 emit destinationFollowsActiveLayerChanged( mDestinationFollowsActiveLayer );
51 emit destinationLayerChanged( nullptr );
52 emit destinationTimeStampFieldChanged( QString() );
53}
54
55bool QgsProjectGpsSettings::readXml( const QDomElement &element, const QgsReadWriteContext & )
56{
57 mAutoAddTrackVertices = element.attribute( QStringLiteral( "autoAddTrackVertices" ), "0" ).toInt();
58 mAutoCommitFeatures = element.attribute( QStringLiteral( "autoCommitFeatures" ), "0" ).toInt();
59 mDestinationFollowsActiveLayer = element.attribute( QStringLiteral( "destinationFollowsActiveLayer" ), "1" ).toInt();
60
61 const QString layerId = element.attribute( QStringLiteral( "destinationLayer" ) );
62 const QString layerName = element.attribute( QStringLiteral( "destinationLayerName" ) );
63 const QString layerSource = element.attribute( QStringLiteral( "destinationLayerSource" ) );
64 const QString layerProvider = element.attribute( QStringLiteral( "destinationLayerProvider" ) );
65
66 mDestinationLayer = QgsVectorLayerRef( layerId, layerName, layerSource, layerProvider );
67
68 mDestinationTimestampFields.clear();
69 {
70 const QDomElement timeStampElement = element.firstChildElement( QStringLiteral( "timeStampFields" ) );
71 QDomElement layerElement = timeStampElement.firstChildElement();
72 while ( !layerElement.isNull() )
73 {
74 const QString layerId = layerElement.attribute( QStringLiteral( "destinationLayer" ) );
75 const QString field = layerElement.attribute( QStringLiteral( "field" ) );
76 mDestinationTimestampFields[ layerId ] = field;
77 layerElement = layerElement.nextSiblingElement();
78 }
79 }
80
81 emit automaticallyAddTrackVerticesChanged( mAutoAddTrackVertices );
82 emit automaticallyCommitFeaturesChanged( mAutoCommitFeatures );
83 emit destinationFollowsActiveLayerChanged( mDestinationFollowsActiveLayer );
84 emit destinationLayerChanged( nullptr ); // won't be set until resolve is called
85 emit destinationTimeStampFieldChanged( QString() );
86 return true;
87}
88
89QDomElement QgsProjectGpsSettings::writeXml( QDomDocument &doc, const QgsReadWriteContext & ) const
90{
91 QDomElement element = doc.createElement( QStringLiteral( "ProjectGpsSettings" ) );
92
93 element.setAttribute( QStringLiteral( "autoAddTrackVertices" ), mAutoAddTrackVertices ? 1 : 0 );
94 element.setAttribute( QStringLiteral( "autoCommitFeatures" ), mAutoCommitFeatures ? 1 : 0 );
95 element.setAttribute( QStringLiteral( "destinationFollowsActiveLayer" ), mDestinationFollowsActiveLayer ? 1 : 0 );
96
97 if ( mDestinationLayer )
98 {
99 element.setAttribute( QStringLiteral( "destinationLayer" ), mDestinationLayer.layerId );
100 element.setAttribute( QStringLiteral( "destinationLayerName" ), mDestinationLayer.name );
101 element.setAttribute( QStringLiteral( "destinationLayerSource" ), mDestinationLayer.source );
102 element.setAttribute( QStringLiteral( "destinationLayerProvider" ), mDestinationLayer.provider );
103 }
104 else
105 {
106 element.setAttribute( QStringLiteral( "destinationLayer" ), QString() );
107 }
108
109 {
110 QDomElement timeStampElement = doc.createElement( QStringLiteral( "timeStampFields" ) );
111 for ( auto it = mDestinationTimestampFields.constBegin(); it != mDestinationTimestampFields.constEnd(); ++it )
112 {
113 const QString layerId = it.key();
114 if ( QgsProject *project = qobject_cast< QgsProject * >( parent() ) )
115 {
116 // do some housekeeping and don't save removed layers in the project
117 if ( !project->mapLayer( layerId ) )
118 continue;
119 }
120
121 QDomElement layerElement = doc.createElement( QStringLiteral( "field" ) );
122 layerElement.setAttribute( QStringLiteral( "destinationLayer" ), layerId );
123 layerElement.setAttribute( QStringLiteral( "field" ), it.value() );
124 timeStampElement.appendChild( layerElement );
125 }
126 element.appendChild( timeStampElement );
127 }
128
129 return element;
130}
131
133{
134 return mAutoAddTrackVertices;
135}
136
138{
139 return mAutoCommitFeatures;
140}
141
143{
144 return mDestinationFollowsActiveLayer;
145}
146
148{
149 return mDestinationLayer.get();
150}
151
153{
154 return mDestinationTimestampFields;
155}
156
158{
159 if ( QgsVectorLayer *vl = destinationLayer() )
160 {
161 return mDestinationTimestampFields.value( vl->id() );
162 }
163 return QString();
164}
165
167{
168 if ( enabled == mAutoAddTrackVertices )
169 return;
170
171 mAutoAddTrackVertices = enabled;
173}
174
176{
177 if ( enabled == mAutoCommitFeatures )
178 return;
179
180 mAutoCommitFeatures = enabled;
182}
183
185{
186 if ( follow == mDestinationFollowsActiveLayer )
187 return;
188
189 mDestinationFollowsActiveLayer = follow;
191}
192
194{
195 if ( layer == mDestinationLayer.get() )
196 {
197 return;
198 }
199
200 mDestinationLayer.setLayer( layer );
201 emit destinationLayerChanged( layer );
202
203 if ( layer )
204 {
205 emit destinationTimeStampFieldChanged( mDestinationTimestampFields.value( layer->id() ) );
206 }
207 else
208 {
209 emit destinationTimeStampFieldChanged( QString() );
210 }
211}
212
214{
215 if ( !layer )
216 return;
217
218 if ( mDestinationTimestampFields.value( layer->id() ) != field )
219 {
220 mDestinationTimestampFields.insert( layer->id(), field );
221 if ( layer == destinationLayer() )
222 {
224 }
225 }
226}
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
void setAutomaticallyCommitFeatures(bool enabled)
Sets whether features created from GPS locations should be immediately committed to their target laye...
void destinationFollowsActiveLayerChanged(bool follows)
Emitted whenever the destinationFollowsActiveLayer() setting is changed.
void destinationLayerChanged(QgsVectorLayer *layer)
Emitted whenever the destination layer for features digitized from GPS is changed.
void setDestinationFollowsActiveLayer(bool follow)
Sets whether the destination layer for storing features digitized from GPS should follow the current ...
QMap< QString, QString > destinationTimeStampFields() const
Returns the map of destination layer ID to target time stamp field name.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the settings's state from a DOM element.
QgsProjectGpsSettings(QObject *parent=nullptr)
Constructor for QgsProjectGpsSettings with the specified parent object.
void reset()
Resets the settings to a default state.
QgsVectorLayer * destinationLayer
void setDestinationLayer(QgsVectorLayer *layer)
Sets the destination layer to be used for storing features digitized from GPS.
void setAutomaticallyAddTrackVertices(bool enabled)
Sets whether track vertices should be automatically created whenever new locations are received from ...
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Returns a DOM element representing the settings.
void resolveReferences(const QgsProject *project)
Resolves reference to layers from stored layer ID (if it has not been resolved already)
void setDestinationTimeStampField(QgsVectorLayer *layer, const QString &field)
Sets the destination field name for automatically storing timestamps in the specified destination lay...
bool destinationFollowsActiveLayer() const
Returns true if the destination layer for storing features digitized from GPS should follow the curre...
void destinationTimeStampFieldChanged(const QString &field)
Emitted whenever the destination field for automatic time stamps is changed.
~QgsProjectGpsSettings() override
void automaticallyCommitFeaturesChanged(bool enabled)
Emitted whenever the automaticallyCommitFeatures() setting is changed.
void automaticallyAddTrackVerticesChanged(bool enabled)
Emitted whenever the automaticallyAddTrackVertices() setting is changed.
QString destinationTimeStampField() const
Returns the destination time stamp field name for the current destinationLayer(), or an empty string ...
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:107
The class is used as a container of context for various read/write operations on other objects.
Represents a vector layer which manages a vector based data sets.
_LayerRef< QgsVectorLayer > QgsVectorLayerRef
TYPE * resolveWeakly(const QgsProject *project, MatchType matchType=MatchType::All)
Resolves the map layer by attempting to find a matching layer in a project using a weak match.
QString source
Weak reference to layer public source.
QString name
Weak reference to layer name.
TYPE * get() const
Returns a pointer to the layer, or nullptr if the reference has not yet been matched to a layer.
QString provider
Weak reference to layer provider.
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
QString layerId
Original layer ID.