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