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