Skip to content

Commit e91c10f

Browse files
authored
Merge pull request #5967 from opengisch/tracker_optimisation
Tracker optimisation focus
2 parents aa55495 + bccb67e commit e91c10f

10 files changed

+181
-32
lines changed

src/core/featuremodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ bool FeatureModel::create()
822822

823823
if ( mBatchMode )
824824
{
825-
isSuccess = mLayer->addFeature( mFeature );
825+
isSuccess = mLayer->addFeature( mFeature, QgsFeatureSink::FastInsert );
826826
if ( isSuccess )
827827
{
828828
mFeature.setId( createdFeatureId );

src/core/layertreemapcanvasbridge.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,27 @@ void LayerTreeMapCanvasBridge::layerInTrackingChanged( QgsVectorLayer *layer, bo
166166
if ( layer )
167167
{
168168
QgsLayerTreeLayer *nodeLayer = mRoot->findLayer( layer->id() );
169+
if ( layer->geometryType() == Qgis::GeometryType::Point )
170+
{
171+
// Disable feature count while tracking to avoid needless CPU cycles wasted updating a collapsed legend
172+
if ( tracking )
173+
{
174+
QVariant showFeatureCountValue = nodeLayer->customProperty( QStringLiteral( "showFeatureCount" ) );
175+
if ( showFeatureCountValue.isValid() && showFeatureCountValue.toInt() != 0 )
176+
{
177+
nodeLayer->setCustomProperty( QStringLiteral( "showFeatureCount" ), 0 );
178+
nodeLayer->setCustomProperty( QStringLiteral( "previousShowFeatureCount" ), showFeatureCountValue );
179+
}
180+
}
181+
else
182+
{
183+
QVariant previousShowFeatureCount = nodeLayer->customProperty( QStringLiteral( "previousShowFeatureCount" ) );
184+
if ( previousShowFeatureCount.isValid() )
185+
{
186+
nodeLayer->setCustomProperty( QStringLiteral( "showFeatureCount" ), previousShowFeatureCount );
187+
}
188+
}
189+
}
169190
mModel->setLayerInTracking( nodeLayer, tracking );
170191
}
171192
}

src/core/rubberbandshape.cpp

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void RubberbandShape::setModel( RubberbandModel *model )
5151

5252
mRubberbandModel = model;
5353

54-
if ( mRubberbandModel )
54+
if ( mRubberbandModel && !mFreeze )
5555
{
5656
connect( mRubberbandModel, &RubberbandModel::vertexChanged, this, &RubberbandShape::markDirty );
5757
connect( mRubberbandModel, &RubberbandModel::verticesRemoved, this, &RubberbandShape::markDirty );
@@ -85,7 +85,7 @@ void RubberbandShape::setVertexModel( VertexModel *vertexModel )
8585

8686
mVertexModel = vertexModel;
8787

88-
if ( mVertexModel )
88+
if ( mVertexModel && !mFreeze )
8989
{
9090
connect( mVertexModel, &VertexModel::dataChanged, this, &RubberbandShape::markDirty );
9191
connect( mVertexModel, &VertexModel::vertexCountChanged, this, &RubberbandShape::markDirty );
@@ -97,6 +97,62 @@ void RubberbandShape::setVertexModel( VertexModel *vertexModel )
9797
emit vertexModelChanged();
9898
}
9999

100+
bool RubberbandShape::freeze() const
101+
{
102+
return mFreeze;
103+
}
104+
105+
void RubberbandShape::setFreeze( bool freeze )
106+
{
107+
if ( mFreeze == freeze )
108+
return;
109+
110+
mFreeze = freeze;
111+
emit freezeChanged();
112+
113+
if ( mFreeze )
114+
{
115+
if ( mVertexModel )
116+
{
117+
disconnect( mVertexModel, &VertexModel::dataChanged, this, &RubberbandShape::markDirty );
118+
disconnect( mVertexModel, &VertexModel::vertexCountChanged, this, &RubberbandShape::markDirty );
119+
disconnect( mVertexModel, &VertexModel::geometryChanged, this, &RubberbandShape::markDirty );
120+
}
121+
if ( mRubberbandModel )
122+
{
123+
disconnect( mRubberbandModel, &RubberbandModel::vertexChanged, this, &RubberbandShape::markDirty );
124+
disconnect( mRubberbandModel, &RubberbandModel::verticesRemoved, this, &RubberbandShape::markDirty );
125+
disconnect( mRubberbandModel, &RubberbandModel::verticesInserted, this, &RubberbandShape::markDirty );
126+
}
127+
if ( mMapSettings )
128+
{
129+
disconnect( mMapSettings, &QgsQuickMapSettings::visibleExtentChanged, this, &RubberbandShape::visibleExtentChanged );
130+
disconnect( mMapSettings, &QgsQuickMapSettings::rotationChanged, this, &RubberbandShape::rotationChanged );
131+
}
132+
}
133+
else
134+
{
135+
if ( mVertexModel )
136+
{
137+
connect( mVertexModel, &VertexModel::dataChanged, this, &RubberbandShape::markDirty );
138+
connect( mVertexModel, &VertexModel::vertexCountChanged, this, &RubberbandShape::markDirty );
139+
connect( mVertexModel, &VertexModel::geometryChanged, this, &RubberbandShape::markDirty );
140+
}
141+
if ( mRubberbandModel )
142+
{
143+
connect( mRubberbandModel, &RubberbandModel::vertexChanged, this, &RubberbandShape::markDirty );
144+
connect( mRubberbandModel, &RubberbandModel::verticesRemoved, this, &RubberbandShape::markDirty );
145+
connect( mRubberbandModel, &RubberbandModel::verticesInserted, this, &RubberbandShape::markDirty );
146+
}
147+
if ( mMapSettings && !mFreeze )
148+
{
149+
connect( mMapSettings, &QgsQuickMapSettings::visibleExtentChanged, this, &RubberbandShape::visibleExtentChanged );
150+
connect( mMapSettings, &QgsQuickMapSettings::rotationChanged, this, &RubberbandShape::rotationChanged );
151+
}
152+
153+
markDirty();
154+
}
155+
}
100156

101157
QgsQuickMapSettings *RubberbandShape::mapSettings() const
102158
{
@@ -115,8 +171,12 @@ void RubberbandShape::setMapSettings( QgsQuickMapSettings *mapSettings )
115171
}
116172

117173
mMapSettings = mapSettings;
118-
connect( mMapSettings, &QgsQuickMapSettings::visibleExtentChanged, this, &RubberbandShape::visibleExtentChanged );
119-
connect( mMapSettings, &QgsQuickMapSettings::rotationChanged, this, &RubberbandShape::rotationChanged );
174+
175+
if ( mMapSettings && !mFreeze )
176+
{
177+
connect( mMapSettings, &QgsQuickMapSettings::visibleExtentChanged, this, &RubberbandShape::visibleExtentChanged );
178+
connect( mMapSettings, &QgsQuickMapSettings::rotationChanged, this, &RubberbandShape::rotationChanged );
179+
}
120180

121181
markDirty();
122182

src/core/rubberbandshape.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class RubberbandShape : public QQuickItem
3535
{
3636
Q_OBJECT
3737

38+
//! 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
39+
Q_PROPERTY( bool freeze READ freeze WRITE setFreeze NOTIFY freezeChanged )
40+
3841
Q_PROPERTY( RubberbandModel *model READ model WRITE setModel NOTIFY modelChanged )
3942
Q_PROPERTY( VertexModel *vertexModel READ vertexModel WRITE setVertexModel NOTIFY vertexModelChanged )
4043
Q_PROPERTY( QgsQuickMapSettings *mapSettings READ mapSettings WRITE setMapSettings NOTIFY mapSettingsChanged )
@@ -66,6 +69,11 @@ class RubberbandShape : public QQuickItem
6669
QgsQuickMapSettings *mapSettings() const;
6770
void setMapSettings( QgsQuickMapSettings *mapSettings );
6871

72+
//! \copydoc freeze
73+
bool freeze() const;
74+
//! \copydoc freeze
75+
void setFreeze( bool freeze );
76+
6977
//! \copydoc color
7078
QColor color() const;
7179
//! \copydoc color
@@ -96,6 +104,8 @@ class RubberbandShape : public QQuickItem
96104
void modelChanged();
97105
void vertexModelChanged();
98106
void mapSettingsChanged();
107+
//! \copydoc freeze
108+
void freezeChanged();
99109
//! \copydoc color
100110
void colorChanged();
101111
//! \copydoc outlineColor
@@ -121,6 +131,7 @@ class RubberbandShape : public QQuickItem
121131
RubberbandModel *mRubberbandModel = nullptr;
122132
VertexModel *mVertexModel = nullptr;
123133
QgsQuickMapSettings *mMapSettings = nullptr;
134+
bool mFreeze = false;
124135
bool mDirty = false;
125136
QColor mColor = QColor( 192, 57, 43, 150 );
126137
QColor mOutlineColor = QColor( 255, 255, 255, 100 );

src/core/tracker.cpp

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -310,63 +310,66 @@ void Tracker::processPositionInformation( const GnssPositionInformation &positio
310310

311311
mLastDevicePositionTimestamp = positionInformation.utcDateTime();
312312

313+
double measureValue = 0.0;
313314
switch ( mMeasureType )
314315
{
315316
case Tracker::SecondsSinceStart:
316-
mRubberbandModel->setMeasureValue( positionInformation.utcDateTime().toSecsSinceEpoch() - mStartPositionTimestamp.toSecsSinceEpoch() );
317+
measureValue = positionInformation.utcDateTime().toSecsSinceEpoch() - mStartPositionTimestamp.toSecsSinceEpoch();
317318
break;
318319
case Tracker::Timestamp:
319-
mRubberbandModel->setMeasureValue( positionInformation.utcDateTime().toSecsSinceEpoch() );
320+
measureValue = positionInformation.utcDateTime().toSecsSinceEpoch();
320321
break;
321322
case Tracker::GroundSpeed:
322-
mRubberbandModel->setMeasureValue( positionInformation.speed() );
323+
measureValue = positionInformation.speed();
323324
break;
324325
case Tracker::Bearing:
325-
mRubberbandModel->setMeasureValue( positionInformation.direction() );
326+
measureValue = positionInformation.direction();
326327
break;
327328
case Tracker::HorizontalAccuracy:
328-
mRubberbandModel->setMeasureValue( positionInformation.hacc() );
329+
measureValue = positionInformation.hacc();
329330
break;
330331
case Tracker::VerticalAccuracy:
331-
mRubberbandModel->setMeasureValue( positionInformation.vacc() );
332+
measureValue = positionInformation.vacc();
332333
break;
333334
case Tracker::PDOP:
334-
mRubberbandModel->setMeasureValue( positionInformation.pdop() );
335+
measureValue = positionInformation.pdop();
335336
break;
336337
case Tracker::HDOP:
337-
mRubberbandModel->setMeasureValue( positionInformation.hdop() );
338+
measureValue = positionInformation.hdop();
338339
break;
339340
case Tracker::VDOP:
340-
mRubberbandModel->setMeasureValue( positionInformation.vdop() );
341+
measureValue = positionInformation.vdop();
341342
break;
342343
}
343344

345+
whileBlocking( mRubberbandModel )->setMeasureValue( measureValue );
344346
mRubberbandModel->setCurrentCoordinate( projectedPosition );
345347
}
346348

347349
void Tracker::replayPositionInformationList( const QList<GnssPositionInformation> &positionInformationList, QgsQuickCoordinateTransformer *coordinateTransformer )
348350
{
349-
bool wasActive = false;
350-
if ( mIsActive )
351-
{
352-
wasActive = true;
353-
stop();
354-
}
351+
const qint64 startTime = QDateTime::currentMSecsSinceEpoch();
355352

356353
mIsReplaying = true;
357354
emit isReplayingChanged();
358355

359356
const Qgis::GeometryType geometryType = mRubberbandModel->geometryType();
360-
mFeatureModel->setBatchMode( geometryType == Qgis::GeometryType::Point );
357+
const bool isPointGeometry = geometryType == Qgis::GeometryType::Point;
358+
mFeatureModel->setBatchMode( isPointGeometry );
359+
361360
connect( mRubberbandModel, &RubberbandModel::currentCoordinateChanged, this, &Tracker::positionReceived );
362361
for ( const GnssPositionInformation &positionInformation : positionInformationList )
363362
{
363+
if ( isPointGeometry )
364+
{
365+
mFeatureModel->setPositionInformation( positionInformation );
366+
}
364367
processPositionInformation( positionInformation,
365368
coordinateTransformer ? coordinateTransformer->transformPosition( QgsPoint( positionInformation.longitude(), positionInformation.latitude(), positionInformation.elevation() ) ) : QgsPoint() );
366369
}
367370
disconnect( mRubberbandModel, &RubberbandModel::currentCoordinateChanged, this, &Tracker::positionReceived );
368-
mFeatureModel->setBatchMode( false );
369371

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

389-
if ( wasActive )
392+
if ( mIsSuspended )
390393
{
394+
mIsSuspended = false;
395+
emit isSuspendedChanged();
391396
start();
392397
}
398+
399+
const qint64 endTime = QDateTime::currentMSecsSinceEpoch();
400+
qInfo() << QStringLiteral( "Tracker position information replay duration: %1ms" ).arg( endTime - startTime );
401+
}
402+
403+
void Tracker::suspendUntilReplay()
404+
{
405+
if ( mIsActive )
406+
{
407+
mIsSuspended = true;
408+
emit isSuspendedChanged();
409+
stop();
410+
}
393411
}
394412

395413
void Tracker::rubberbandModelVertexCountChanged()

src/core/tracker.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Tracker : public QObject
3535
Q_OBJECT
3636

3737
Q_PROPERTY( bool isActive READ isActive NOTIFY isActiveChanged )
38+
Q_PROPERTY( bool isSuspended READ isSuspended NOTIFY isSuspendedChanged )
3839
Q_PROPERTY( bool isReplaying READ isReplaying NOTIFY isReplayingChanged )
3940

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

138+
//! Returns whether the track has been suspended
139+
bool isSuspended() const { return mIsSuspended; }
140+
137141
//! Returns whether the tracker is replaying positions
138142
bool isReplaying() const { return mIsReplaying; }
139143

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

155+
void suspendUntilReplay();
156+
151157
signals:
152158
void isActiveChanged();
159+
void isSuspendedChanged();
153160
void isReplayingChanged();
154161
void visibleChanged();
155162
void vectorLayerChanged();
@@ -177,6 +184,7 @@ class Tracker : public QObject
177184
void trackPosition();
178185

179186
bool mIsActive = false;
187+
bool mIsSuspended = false;
180188
bool mIsReplaying = false;
181189

182190
RubberbandModel *mRubberbandModel = nullptr;

src/core/trackingmodel.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,25 @@ void TrackingModel::replayPositionInformationList( const QList<GnssPositionInfor
169169
for ( int i = 0; i < mTrackers.size(); i++ )
170170
{
171171
Tracker *tracker = mTrackers[i];
172-
if ( tracker->isActive() )
172+
if ( tracker->isSuspended() )
173173
{
174174
tracker->replayPositionInformationList( positionInformationList, coordinateTransformer );
175175
}
176176
}
177177
}
178178

179+
void TrackingModel::suspendUntilReplay()
180+
{
181+
for ( int i = 0; i < mTrackers.size(); i++ )
182+
{
183+
Tracker *tracker = mTrackers[i];
184+
if ( tracker->isActive() )
185+
{
186+
tracker->suspendUntilReplay();
187+
}
188+
}
189+
}
190+
179191
void TrackingModel::setTrackerVisibility( QgsVectorLayer *layer, bool visible )
180192
{
181193
if ( trackerIterator( layer ) != mTrackers.constEnd() )

src/core/trackingmodel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class TrackingModel : public QAbstractItemModel
7171
//! Replays a list of position information for all active trackers
7272
Q_INVOKABLE void replayPositionInformationList( const QList<GnssPositionInformation> &positionInformationList, QgsQuickCoordinateTransformer *coordinateTransformer = nullptr );
7373

74+
Q_INVOKABLE void suspendUntilReplay();
75+
7476
void reset();
7577

7678
/**

src/qml/TrackingSession.qml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Item {
4848
Rubberband {
4949
id: rubberband
5050
visible: tracker.visible
51+
freeze: tracker.isReplaying
5152

5253
color: Qt.rgba(Math.min(0.75, Math.random()), Math.min(0.75, Math.random()), Math.min(0.75, Math.random()), 0.6)
5354
geometryType: Qgis.GeometryType.Line

0 commit comments

Comments
 (0)