Skip to content

Commit

Permalink
Merge pull request #5967 from opengisch/tracker_optimisation
Browse files Browse the repository at this point in the history
Tracker optimisation focus
  • Loading branch information
nirvn authored Feb 6, 2025
2 parents aa55495 + bccb67e commit e91c10f
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/core/featuremodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ bool FeatureModel::create()

if ( mBatchMode )
{
isSuccess = mLayer->addFeature( mFeature );
isSuccess = mLayer->addFeature( mFeature, QgsFeatureSink::FastInsert );
if ( isSuccess )
{
mFeature.setId( createdFeatureId );
Expand Down
21 changes: 21 additions & 0 deletions src/core/layertreemapcanvasbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,27 @@ void LayerTreeMapCanvasBridge::layerInTrackingChanged( QgsVectorLayer *layer, bo
if ( layer )
{
QgsLayerTreeLayer *nodeLayer = mRoot->findLayer( layer->id() );
if ( layer->geometryType() == Qgis::GeometryType::Point )
{
// Disable feature count while tracking to avoid needless CPU cycles wasted updating a collapsed legend
if ( tracking )
{
QVariant showFeatureCountValue = nodeLayer->customProperty( QStringLiteral( "showFeatureCount" ) );
if ( showFeatureCountValue.isValid() && showFeatureCountValue.toInt() != 0 )
{
nodeLayer->setCustomProperty( QStringLiteral( "showFeatureCount" ), 0 );
nodeLayer->setCustomProperty( QStringLiteral( "previousShowFeatureCount" ), showFeatureCountValue );
}
}
else
{
QVariant previousShowFeatureCount = nodeLayer->customProperty( QStringLiteral( "previousShowFeatureCount" ) );
if ( previousShowFeatureCount.isValid() )
{
nodeLayer->setCustomProperty( QStringLiteral( "showFeatureCount" ), previousShowFeatureCount );
}
}
}
mModel->setLayerInTracking( nodeLayer, tracking );
}
}
68 changes: 64 additions & 4 deletions src/core/rubberbandshape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void RubberbandShape::setModel( RubberbandModel *model )

mRubberbandModel = model;

if ( mRubberbandModel )
if ( mRubberbandModel && !mFreeze )
{
connect( mRubberbandModel, &RubberbandModel::vertexChanged, this, &RubberbandShape::markDirty );
connect( mRubberbandModel, &RubberbandModel::verticesRemoved, this, &RubberbandShape::markDirty );
Expand Down Expand Up @@ -85,7 +85,7 @@ void RubberbandShape::setVertexModel( VertexModel *vertexModel )

mVertexModel = vertexModel;

if ( mVertexModel )
if ( mVertexModel && !mFreeze )
{
connect( mVertexModel, &VertexModel::dataChanged, this, &RubberbandShape::markDirty );
connect( mVertexModel, &VertexModel::vertexCountChanged, this, &RubberbandShape::markDirty );
Expand All @@ -97,6 +97,62 @@ void RubberbandShape::setVertexModel( VertexModel *vertexModel )
emit vertexModelChanged();
}

bool RubberbandShape::freeze() const
{
return mFreeze;
}

void RubberbandShape::setFreeze( bool freeze )
{
if ( mFreeze == freeze )
return;

mFreeze = freeze;
emit freezeChanged();

if ( mFreeze )
{
if ( mVertexModel )
{
disconnect( mVertexModel, &VertexModel::dataChanged, this, &RubberbandShape::markDirty );
disconnect( mVertexModel, &VertexModel::vertexCountChanged, this, &RubberbandShape::markDirty );
disconnect( mVertexModel, &VertexModel::geometryChanged, this, &RubberbandShape::markDirty );
}
if ( mRubberbandModel )
{
disconnect( mRubberbandModel, &RubberbandModel::vertexChanged, this, &RubberbandShape::markDirty );
disconnect( mRubberbandModel, &RubberbandModel::verticesRemoved, this, &RubberbandShape::markDirty );
disconnect( mRubberbandModel, &RubberbandModel::verticesInserted, this, &RubberbandShape::markDirty );
}
if ( mMapSettings )
{
disconnect( mMapSettings, &QgsQuickMapSettings::visibleExtentChanged, this, &RubberbandShape::visibleExtentChanged );
disconnect( mMapSettings, &QgsQuickMapSettings::rotationChanged, this, &RubberbandShape::rotationChanged );
}
}
else
{
if ( mVertexModel )
{
connect( mVertexModel, &VertexModel::dataChanged, this, &RubberbandShape::markDirty );
connect( mVertexModel, &VertexModel::vertexCountChanged, this, &RubberbandShape::markDirty );
connect( mVertexModel, &VertexModel::geometryChanged, this, &RubberbandShape::markDirty );
}
if ( mRubberbandModel )
{
connect( mRubberbandModel, &RubberbandModel::vertexChanged, this, &RubberbandShape::markDirty );
connect( mRubberbandModel, &RubberbandModel::verticesRemoved, this, &RubberbandShape::markDirty );
connect( mRubberbandModel, &RubberbandModel::verticesInserted, this, &RubberbandShape::markDirty );
}
if ( mMapSettings && !mFreeze )
{
connect( mMapSettings, &QgsQuickMapSettings::visibleExtentChanged, this, &RubberbandShape::visibleExtentChanged );
connect( mMapSettings, &QgsQuickMapSettings::rotationChanged, this, &RubberbandShape::rotationChanged );
}

markDirty();
}
}

QgsQuickMapSettings *RubberbandShape::mapSettings() const
{
Expand All @@ -115,8 +171,12 @@ void RubberbandShape::setMapSettings( QgsQuickMapSettings *mapSettings )
}

mMapSettings = mapSettings;
connect( mMapSettings, &QgsQuickMapSettings::visibleExtentChanged, this, &RubberbandShape::visibleExtentChanged );
connect( mMapSettings, &QgsQuickMapSettings::rotationChanged, this, &RubberbandShape::rotationChanged );

if ( mMapSettings && !mFreeze )
{
connect( mMapSettings, &QgsQuickMapSettings::visibleExtentChanged, this, &RubberbandShape::visibleExtentChanged );
connect( mMapSettings, &QgsQuickMapSettings::rotationChanged, this, &RubberbandShape::rotationChanged );
}

markDirty();

Expand Down
11 changes: 11 additions & 0 deletions src/core/rubberbandshape.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class RubberbandShape : public QQuickItem
{
Q_OBJECT

//! When set to TRUE, changes to the linked rubber band or vertex model as well as map settings will be ignored and the rubber band shape data will be left untouched
Q_PROPERTY( bool freeze READ freeze WRITE setFreeze NOTIFY freezeChanged )

Q_PROPERTY( RubberbandModel *model READ model WRITE setModel NOTIFY modelChanged )
Q_PROPERTY( VertexModel *vertexModel READ vertexModel WRITE setVertexModel NOTIFY vertexModelChanged )
Q_PROPERTY( QgsQuickMapSettings *mapSettings READ mapSettings WRITE setMapSettings NOTIFY mapSettingsChanged )
Expand Down Expand Up @@ -66,6 +69,11 @@ class RubberbandShape : public QQuickItem
QgsQuickMapSettings *mapSettings() const;
void setMapSettings( QgsQuickMapSettings *mapSettings );

//! \copydoc freeze
bool freeze() const;
//! \copydoc freeze
void setFreeze( bool freeze );

//! \copydoc color
QColor color() const;
//! \copydoc color
Expand Down Expand Up @@ -96,6 +104,8 @@ class RubberbandShape : public QQuickItem
void modelChanged();
void vertexModelChanged();
void mapSettingsChanged();
//! \copydoc freeze
void freezeChanged();
//! \copydoc color
void colorChanged();
//! \copydoc outlineColor
Expand All @@ -121,6 +131,7 @@ class RubberbandShape : public QQuickItem
RubberbandModel *mRubberbandModel = nullptr;
VertexModel *mVertexModel = nullptr;
QgsQuickMapSettings *mMapSettings = nullptr;
bool mFreeze = false;
bool mDirty = false;
QColor mColor = QColor( 192, 57, 43, 150 );
QColor mOutlineColor = QColor( 255, 255, 255, 100 );
Expand Down
54 changes: 36 additions & 18 deletions src/core/tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,63 +310,66 @@ void Tracker::processPositionInformation( const GnssPositionInformation &positio

mLastDevicePositionTimestamp = positionInformation.utcDateTime();

double measureValue = 0.0;
switch ( mMeasureType )
{
case Tracker::SecondsSinceStart:
mRubberbandModel->setMeasureValue( positionInformation.utcDateTime().toSecsSinceEpoch() - mStartPositionTimestamp.toSecsSinceEpoch() );
measureValue = positionInformation.utcDateTime().toSecsSinceEpoch() - mStartPositionTimestamp.toSecsSinceEpoch();
break;
case Tracker::Timestamp:
mRubberbandModel->setMeasureValue( positionInformation.utcDateTime().toSecsSinceEpoch() );
measureValue = positionInformation.utcDateTime().toSecsSinceEpoch();
break;
case Tracker::GroundSpeed:
mRubberbandModel->setMeasureValue( positionInformation.speed() );
measureValue = positionInformation.speed();
break;
case Tracker::Bearing:
mRubberbandModel->setMeasureValue( positionInformation.direction() );
measureValue = positionInformation.direction();
break;
case Tracker::HorizontalAccuracy:
mRubberbandModel->setMeasureValue( positionInformation.hacc() );
measureValue = positionInformation.hacc();
break;
case Tracker::VerticalAccuracy:
mRubberbandModel->setMeasureValue( positionInformation.vacc() );
measureValue = positionInformation.vacc();
break;
case Tracker::PDOP:
mRubberbandModel->setMeasureValue( positionInformation.pdop() );
measureValue = positionInformation.pdop();
break;
case Tracker::HDOP:
mRubberbandModel->setMeasureValue( positionInformation.hdop() );
measureValue = positionInformation.hdop();
break;
case Tracker::VDOP:
mRubberbandModel->setMeasureValue( positionInformation.vdop() );
measureValue = positionInformation.vdop();
break;
}

whileBlocking( mRubberbandModel )->setMeasureValue( measureValue );
mRubberbandModel->setCurrentCoordinate( projectedPosition );
}

void Tracker::replayPositionInformationList( const QList<GnssPositionInformation> &positionInformationList, QgsQuickCoordinateTransformer *coordinateTransformer )
{
bool wasActive = false;
if ( mIsActive )
{
wasActive = true;
stop();
}
const qint64 startTime = QDateTime::currentMSecsSinceEpoch();

mIsReplaying = true;
emit isReplayingChanged();

const Qgis::GeometryType geometryType = mRubberbandModel->geometryType();
mFeatureModel->setBatchMode( geometryType == Qgis::GeometryType::Point );
const bool isPointGeometry = geometryType == Qgis::GeometryType::Point;
mFeatureModel->setBatchMode( isPointGeometry );

connect( mRubberbandModel, &RubberbandModel::currentCoordinateChanged, this, &Tracker::positionReceived );
for ( const GnssPositionInformation &positionInformation : positionInformationList )
{
if ( isPointGeometry )
{
mFeatureModel->setPositionInformation( positionInformation );
}
processPositionInformation( positionInformation,
coordinateTransformer ? coordinateTransformer->transformPosition( QgsPoint( positionInformation.longitude(), positionInformation.latitude(), positionInformation.elevation() ) ) : QgsPoint() );
}
disconnect( mRubberbandModel, &RubberbandModel::currentCoordinateChanged, this, &Tracker::positionReceived );
mFeatureModel->setBatchMode( false );

mFeatureModel->setBatchMode( false );
const int vertexCount = mRubberbandModel->vertexCount();
if ( ( geometryType == Qgis::GeometryType::Line && vertexCount > 2 ) || ( geometryType == Qgis::GeometryType::Polygon && vertexCount > 3 ) )
{
Expand All @@ -386,10 +389,25 @@ void Tracker::replayPositionInformationList( const QList<GnssPositionInformation
mIsReplaying = false;
emit isReplayingChanged();

if ( wasActive )
if ( mIsSuspended )
{
mIsSuspended = false;
emit isSuspendedChanged();
start();
}

const qint64 endTime = QDateTime::currentMSecsSinceEpoch();
qInfo() << QStringLiteral( "Tracker position information replay duration: %1ms" ).arg( endTime - startTime );
}

void Tracker::suspendUntilReplay()
{
if ( mIsActive )
{
mIsSuspended = true;
emit isSuspendedChanged();
stop();
}
}

void Tracker::rubberbandModelVertexCountChanged()
Expand Down
8 changes: 8 additions & 0 deletions src/core/tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Tracker : public QObject
Q_OBJECT

Q_PROPERTY( bool isActive READ isActive NOTIFY isActiveChanged )
Q_PROPERTY( bool isSuspended READ isSuspended NOTIFY isSuspendedChanged )
Q_PROPERTY( bool isReplaying READ isReplaying NOTIFY isReplayingChanged )

Q_PROPERTY( bool visible READ visible WRITE setVisible NOTIFY visibleChanged )
Expand Down Expand Up @@ -134,6 +135,9 @@ class Tracker : public QObject
//! Returns whether the tracker has been started
bool isActive() const { return mIsActive; }

//! Returns whether the track has been suspended
bool isSuspended() const { return mIsSuspended; }

//! Returns whether the tracker is replaying positions
bool isReplaying() const { return mIsReplaying; }

Expand All @@ -148,8 +152,11 @@ class Tracker : public QObject
//! Replays a list of position information taking into account the tracker settings
void replayPositionInformationList( const QList<GnssPositionInformation> &positionInformationList, QgsQuickCoordinateTransformer *coordinateTransformer = nullptr );

void suspendUntilReplay();

signals:
void isActiveChanged();
void isSuspendedChanged();
void isReplayingChanged();
void visibleChanged();
void vectorLayerChanged();
Expand Down Expand Up @@ -177,6 +184,7 @@ class Tracker : public QObject
void trackPosition();

bool mIsActive = false;
bool mIsSuspended = false;
bool mIsReplaying = false;

RubberbandModel *mRubberbandModel = nullptr;
Expand Down
14 changes: 13 additions & 1 deletion src/core/trackingmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,25 @@ void TrackingModel::replayPositionInformationList( const QList<GnssPositionInfor
for ( int i = 0; i < mTrackers.size(); i++ )
{
Tracker *tracker = mTrackers[i];
if ( tracker->isActive() )
if ( tracker->isSuspended() )
{
tracker->replayPositionInformationList( positionInformationList, coordinateTransformer );
}
}
}

void TrackingModel::suspendUntilReplay()
{
for ( int i = 0; i < mTrackers.size(); i++ )
{
Tracker *tracker = mTrackers[i];
if ( tracker->isActive() )
{
tracker->suspendUntilReplay();
}
}
}

void TrackingModel::setTrackerVisibility( QgsVectorLayer *layer, bool visible )
{
if ( trackerIterator( layer ) != mTrackers.constEnd() )
Expand Down
2 changes: 2 additions & 0 deletions src/core/trackingmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class TrackingModel : public QAbstractItemModel
//! Replays a list of position information for all active trackers
Q_INVOKABLE void replayPositionInformationList( const QList<GnssPositionInformation> &positionInformationList, QgsQuickCoordinateTransformer *coordinateTransformer = nullptr );

Q_INVOKABLE void suspendUntilReplay();

void reset();

/**
Expand Down
1 change: 1 addition & 0 deletions src/qml/TrackingSession.qml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Item {
Rubberband {
id: rubberband
visible: tracker.visible
freeze: tracker.isReplaying

color: Qt.rgba(Math.min(0.75, Math.random()), Math.min(0.75, Math.random()), Math.min(0.75, Math.random()), 0.6)
geometryType: Qgis.GeometryType.Line
Expand Down
Loading

1 comment on commit e91c10f

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.