QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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#include "moc_qgsprojectgpssettings.cpp"
17
18#include <QDomElement>
19
21 : QObject( parent )
22{
23
24}
25
27{
28 mDestinationLayer.resolveWeakly( project );
29
30 emit destinationLayerChanged( mDestinationLayer.get() );
31
32 if ( mDestinationLayer )
33 {
34 emit destinationTimeStampFieldChanged( mDestinationTimestampFields.value( mDestinationLayer->id() ) );
35 }
36}
37
39
41{
42 mAutoAddTrackVertices = false;
43 mAutoCommitFeatures = false;
44 mDestinationFollowsActiveLayer = true;
45
46 mDestinationLayer.setLayer( nullptr );
47 mDestinationTimestampFields.clear();
48
49 emit automaticallyAddTrackVerticesChanged( mAutoAddTrackVertices );
50 emit automaticallyCommitFeaturesChanged( mAutoCommitFeatures );
51 emit destinationFollowsActiveLayerChanged( mDestinationFollowsActiveLayer );
52 emit destinationLayerChanged( nullptr );
53 emit destinationTimeStampFieldChanged( QString() );
54}
55
56bool QgsProjectGpsSettings::readXml( const QDomElement &element, const QgsReadWriteContext & )
57{
58 mAutoAddTrackVertices = element.attribute( QStringLiteral( "autoAddTrackVertices" ), "0" ).toInt();
59 mAutoCommitFeatures = element.attribute( QStringLiteral( "autoCommitFeatures" ), "0" ).toInt();
60 mDestinationFollowsActiveLayer = element.attribute( QStringLiteral( "destinationFollowsActiveLayer" ), "1" ).toInt();
61
62 const QString layerId = element.attribute( QStringLiteral( "destinationLayer" ) );
63 const QString layerName = element.attribute( QStringLiteral( "destinationLayerName" ) );
64 const QString layerSource = element.attribute( QStringLiteral( "destinationLayerSource" ) );
65 const QString layerProvider = element.attribute( QStringLiteral( "destinationLayerProvider" ) );
66
67 mDestinationLayer = QgsVectorLayerRef( layerId, layerName, layerSource, layerProvider );
68
69 mDestinationTimestampFields.clear();
70 {
71 const QDomElement timeStampElement = element.firstChildElement( QStringLiteral( "timeStampFields" ) );
72 QDomElement layerElement = timeStampElement.firstChildElement();
73 while ( !layerElement.isNull() )
74 {
75 const QString layerId = layerElement.attribute( QStringLiteral( "destinationLayer" ) );
76 const QString field = layerElement.attribute( QStringLiteral( "field" ) );
77 mDestinationTimestampFields[ layerId ] = field;
78 layerElement = layerElement.nextSiblingElement();
79 }
80 }
81
82 emit automaticallyAddTrackVerticesChanged( mAutoAddTrackVertices );
83 emit automaticallyCommitFeaturesChanged( mAutoCommitFeatures );
84 emit destinationFollowsActiveLayerChanged( mDestinationFollowsActiveLayer );
85 emit destinationLayerChanged( nullptr ); // won't be set until resolve is called
86 emit destinationTimeStampFieldChanged( QString() );
87 return true;
88}
89
90QDomElement QgsProjectGpsSettings::writeXml( QDomDocument &doc, const QgsReadWriteContext & ) const
91{
92 QDomElement element = doc.createElement( QStringLiteral( "ProjectGpsSettings" ) );
93
94 element.setAttribute( QStringLiteral( "autoAddTrackVertices" ), mAutoAddTrackVertices ? 1 : 0 );
95 element.setAttribute( QStringLiteral( "autoCommitFeatures" ), mAutoCommitFeatures ? 1 : 0 );
96 element.setAttribute( QStringLiteral( "destinationFollowsActiveLayer" ), mDestinationFollowsActiveLayer ? 1 : 0 );
97
98 if ( mDestinationLayer )
99 {
100 element.setAttribute( QStringLiteral( "destinationLayer" ), mDestinationLayer.layerId );
101 element.setAttribute( QStringLiteral( "destinationLayerName" ), mDestinationLayer.name );
102 element.setAttribute( QStringLiteral( "destinationLayerSource" ), mDestinationLayer.source );
103 element.setAttribute( QStringLiteral( "destinationLayerProvider" ), mDestinationLayer.provider );
104 }
105 else
106 {
107 element.setAttribute( QStringLiteral( "destinationLayer" ), QString() );
108 }
109
110 {
111 QDomElement timeStampElement = doc.createElement( QStringLiteral( "timeStampFields" ) );
112 for ( auto it = mDestinationTimestampFields.constBegin(); it != mDestinationTimestampFields.constEnd(); ++it )
113 {
114 const QString layerId = it.key();
115 if ( QgsProject *project = qobject_cast< QgsProject * >( parent() ) )
116 {
117 // do some housekeeping and don't save removed layers in the project
118 if ( !project->mapLayer( layerId ) )
119 continue;
120 }
121
122 QDomElement layerElement = doc.createElement( QStringLiteral( "field" ) );
123 layerElement.setAttribute( QStringLiteral( "destinationLayer" ), layerId );
124 layerElement.setAttribute( QStringLiteral( "field" ), it.value() );
125 timeStampElement.appendChild( layerElement );
126 }
127 element.appendChild( timeStampElement );
128 }
129
130 return element;
131}
132
134{
135 return mAutoAddTrackVertices;
136}
137
139{
140 return mAutoCommitFeatures;
141}
142
144{
145 return mDestinationFollowsActiveLayer;
146}
147
149{
150 return mDestinationLayer.get();
151}
152
154{
155 return mDestinationTimestampFields;
156}
157
159{
160 if ( QgsVectorLayer *vl = destinationLayer() )
161 {
162 return mDestinationTimestampFields.value( vl->id() );
163 }
164 return QString();
165}
166
168{
169 if ( enabled == mAutoAddTrackVertices )
170 return;
171
172 mAutoAddTrackVertices = enabled;
174}
175
177{
178 if ( enabled == mAutoCommitFeatures )
179 return;
180
181 mAutoCommitFeatures = enabled;
183}
184
186{
187 if ( follow == mDestinationFollowsActiveLayer )
188 return;
189
190 mDestinationFollowsActiveLayer = follow;
192}
193
195{
196 if ( layer == mDestinationLayer.get() )
197 {
198 return;
199 }
200
201 mDestinationLayer.setLayer( layer );
202 emit destinationLayerChanged( layer );
203
204 if ( layer )
205 {
206 emit destinationTimeStampFieldChanged( mDestinationTimestampFields.value( layer->id() ) );
207 }
208 else
209 {
210 emit destinationTimeStampFieldChanged( QString() );
211 }
212}
213
215{
216 if ( !layer )
217 return;
218
219 if ( mDestinationTimestampFields.value( layer->id() ) != field )
220 {
221 mDestinationTimestampFields.insert( layer->id(), field );
222 if ( layer == destinationLayer() )
223 {
225 }
226 }
227}
QString id
Definition qgsmaplayer.h:79
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.