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