Skip to content

Commit

Permalink
Also update GPS derived attributes when updating feature form from
Browse files Browse the repository at this point in the history
GPS
  • Loading branch information
nyalldawson committed Nov 23, 2022
1 parent 9665241 commit a876fa4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
42 changes: 30 additions & 12 deletions src/app/gps/qgsappgpsdigitizing.cpp
Expand Up @@ -101,6 +101,14 @@ void QgsUpdateGpsDetailsAction::triggerForFeature( QgsMapLayer *layer, const Qgs
highlight->mPointSymbol = QgsHighlight::PointSymbol::Circle;
dialog->setHighlight( highlight );

const QgsAttributeMap updatedDerivedAttributes = mDigitizing->derivedAttributes();
for ( auto it = updatedDerivedAttributes.constBegin(); it != updatedDerivedAttributes.constEnd(); ++it )
{
const int fieldIndex = it.key();
const QString fieldName = vlayer->fields().at( fieldIndex ).name();
form->changeAttribute( fieldName, it.value() );
}

if ( QgsMessageBar *messageBar = context.messageBar() )
messageBar->pushSuccess( QString(), tr( "Updated feature location from GPS." ) );
}
Expand Down Expand Up @@ -182,6 +190,25 @@ QgsMapCanvas *QgsAppGpsDigitizing::canvas()
return mCanvas;
}

QgsAttributeMap QgsAppGpsDigitizing::derivedAttributes() const
{
QgsVectorLayer *vlayer = QgsProject::instance()->gpsSettings()->destinationLayer();
if ( !vlayer )
return QgsAttributeMap();

QgsAttributeMap attrMap;
const int idx = vlayer->fields().indexOf( QgsProject::instance()->gpsSettings()->destinationTimeStampField() );
if ( idx != -1 )
{
const QVariant ts = timestamp( vlayer, idx );
if ( ts.isValid() )
{
attrMap[ idx ] = ts;
}
}
return attrMap;
}

void QgsAppGpsDigitizing::addVertex( const QgsPoint &wgs84Point )
{
if ( !mRubberBand )
Expand Down Expand Up @@ -285,17 +312,8 @@ void QgsAppGpsDigitizing::createFeature()
}
}

// Handle timestamp
QgsAttributeMap attrMap;
const int idx = vlayer->fields().indexOf( QgsProject::instance()->gpsSettings()->destinationTimeStampField() );
if ( idx != -1 )
{
const QVariant ts = timestamp( vlayer, idx );
if ( ts.isValid() )
{
attrMap[ idx ] = ts;
}
}
// Populate timestamp and other autogenerated attributes
const QgsAttributeMap attrMap = derivedAttributes();

QgsFeature f;
f.setGeometry( geometryLayerCrs );
Expand Down Expand Up @@ -428,7 +446,7 @@ void QgsAppGpsDigitizing::createRubberBand()
mRubberBand->show();
}

QVariant QgsAppGpsDigitizing::timestamp( QgsVectorLayer *vlayer, int idx )
QVariant QgsAppGpsDigitizing::timestamp( QgsVectorLayer *vlayer, int idx ) const
{
const QDateTime timestamp = lastTimestamp();
QVariant value;
Expand Down
8 changes: 7 additions & 1 deletion src/app/gps/qgsappgpsdigitizing.h
Expand Up @@ -24,6 +24,7 @@
#include "qgsgpslogger.h"
#include "qgsmaplayeractionregistry.h"
#include "qgswkbtypes.h"
#include "qgsattributes.h"

class QgsAppGpsConnection;
class QgsMapCanvas;
Expand Down Expand Up @@ -63,6 +64,11 @@ class APP_EXPORT QgsAppGpsDigitizing: public QgsGpsLogger

QgsMapCanvas *canvas();

/**
* Returns the map of attributes which are auto-populated from GPS information, with their current derived values.
*/
QgsAttributeMap derivedAttributes() const;

public slots:
void createFeature();
void createVertexAtCurrentLocation();
Expand All @@ -78,7 +84,7 @@ class APP_EXPORT QgsAppGpsDigitizing: public QgsGpsLogger

private:
void createRubberBand();
QVariant timestamp( QgsVectorLayer *vlayer, int idx );
QVariant timestamp( QgsVectorLayer *vlayer, int idx ) const;

QgsAppGpsConnection *mConnection = nullptr;
QgsMapCanvas *mCanvas = nullptr;
Expand Down

0 comments on commit a876fa4

Please sign in to comment.