Skip to content

Commit 726fcfc

Browse files
committed
[tracer] simpler and lazier updates of tracer configuration when something changes
1 parent e7d277f commit 726fcfc

File tree

4 files changed

+45
-43
lines changed

4 files changed

+45
-43
lines changed

src/core/qgstracer.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ bool QgsTracer::initGraph()
526526
int timeMake = t3.elapsed();
527527

528528
QgsDebugMsg( QString( "tracer extract %1 ms, noding %2 ms (call %3 ms), make %4 ms" )
529-
.arg ( timeExtract ).arg( timeNoding ).arg ( timeNodingCall ).arg( timeMake ) );
529+
.arg( timeExtract ).arg( timeNoding ).arg( timeNodingCall ).arg( timeMake ) );
530530
return true;
531531
}
532532

@@ -582,6 +582,9 @@ bool QgsTracer::init()
582582
if ( mGraph )
583583
return true;
584584

585+
// configuration from derived class?
586+
configure();
587+
585588
return initGraph();
586589
}
587590

@@ -611,7 +614,7 @@ void QgsTracer::onGeometryChanged( QgsFeatureId fid, QgsGeometry& geom )
611614
invalidateGraph();
612615
}
613616

614-
QVector<QgsPoint> QgsTracer::findShortestPath(const QgsPoint& p1, const QgsPoint& p2, PathError* error )
617+
QVector<QgsPoint> QgsTracer::findShortestPath( const QgsPoint& p1, const QgsPoint& p2, PathError* error )
615618
{
616619
init(); // does nothing if the graph exists already
617620
if ( !mGraph )

src/core/qgstracer.h

+13-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ class CORE_EXPORT QgsTracer : public QObject
6666
//! depending on how big the input layers are. It is not necessary
6767
//! to call this method explicitly - it will be called by findShortestPath()
6868
//! if necessary.
69-
virtual bool init();
69+
bool init();
70+
71+
//! Whether the internal data structures have been initialized
72+
bool isInitialized() const { return mGraph != nullptr; }
7073

7174
enum PathError
7275
{
@@ -86,9 +89,17 @@ class CORE_EXPORT QgsTracer : public QObject
8689
//! Find out whether the point is snapped to a vertex or edge (i.e. it can be used for tracing start/stop)
8790
bool isPointSnapped( const QgsPoint& pt );
8891

92+
protected:
93+
//! Allows derived classes to setup the settings just before the tracer is initialized.
94+
//! This allows the configuration to be set in a lazy way only when it is really necessary.
95+
//! Default implementation does nothing.
96+
virtual void configure() {}
97+
98+
protected slots:
99+
void invalidateGraph();
100+
89101
private:
90102
bool initGraph();
91-
void invalidateGraph();
92103

93104
private slots:
94105
void onFeatureAdded( QgsFeatureId fid );

src/gui/qgsmapcanvastracer.cpp

+24-35
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ QgsMapCanvasTracer::QgsMapCanvasTracer( QgsMapCanvas* canvas, QgsMessageBar* mes
2020
{
2121
sTracers.insert( canvas, this );
2222

23-
connect( canvas, SIGNAL( destinationCrsChanged() ), this, SLOT( updateSettings() ) );
24-
connect( canvas, SIGNAL( layersChanged() ), this, SLOT( updateLayerSettings() ) );
25-
connect( canvas, SIGNAL( extentsChanged() ), this, SLOT( updateSettings() ) );
23+
// when things change we just invalidate the graph - and set up new parameters again only when necessary
24+
connect( canvas, SIGNAL( destinationCrsChanged() ), this, SLOT( invalidateGraph() ) );
25+
connect( canvas, SIGNAL( layersChanged() ), this, SLOT( invalidateGraph() ) );
26+
connect( canvas, SIGNAL( extentsChanged() ), this, SLOT( invalidateGraph() ) );
2627
connect( canvas, SIGNAL( currentLayerChanged( QgsMapLayer* ) ), this, SLOT( onCurrentLayerChanged() ) );
27-
connect( canvas->snappingUtils(), SIGNAL( configChanged() ), this, SLOT( updateLayerSettings() ) );
28+
connect( canvas->snappingUtils(), SIGNAL( configChanged() ), this, SLOT( invalidateGraph() ) );
2829

2930
mActionEnableTracing = new QAction( QIcon( QgsApplication::getThemeIcon( "/mActionTracing.png" ) ), tr( "Enable Tracing" ), this );
3031
mActionEnableTracing->setShortcut( Qt::Key_T );
@@ -33,22 +34,13 @@ QgsMapCanvasTracer::QgsMapCanvasTracer( QgsMapCanvas* canvas, QgsMessageBar* mes
3334
// arbitrarily chosen limit that should allow for fairly fast initialization
3435
// of the underlying graph structure
3536
setMaxFeatureCount( QSettings().value( "/qgis/digitizing/tracing_max_feature_count", 10000 ).toInt() );
36-
37-
updateSettings(); // initialize
38-
updateLayerSettings();
3937
}
4038

4139
QgsMapCanvasTracer::~QgsMapCanvasTracer()
4240
{
4341
sTracers.remove( mCanvas );
4442
}
4543

46-
bool QgsMapCanvasTracer::init()
47-
{
48-
bool res = QgsTracer::init();
49-
return res;
50-
}
51-
5244
QgsMapCanvasTracer* QgsMapCanvasTracer::tracerForCanvas( QgsMapCanvas* canvas )
5345
{
5446
return sTracers.value( canvas, 0 );
@@ -66,23 +58,23 @@ void QgsMapCanvasTracer::reportError( QgsTracer::PathError err, bool addingVerte
6658
QString message;
6759
switch ( err )
6860
{
69-
case ErrTooManyFeatures:
70-
message = tr( "Disabled - there are too many features displayed. Try zooming in or disable some layers." );
71-
break;
72-
case ErrPoint1:
73-
message = tr( "The start point needs to be snapped and in the visible map view" );
74-
break;
75-
case ErrPoint2:
76-
if ( addingVertex )
77-
message = tr( "The end point needs to be snapped" );
78-
break;
79-
case ErrNoPath:
80-
if ( addingVertex )
81-
message = tr( "Endpoints are not connected" );
82-
break;
83-
case ErrNone:
84-
default:
85-
break;
61+
case ErrTooManyFeatures:
62+
message = tr( "Disabled - there are too many features displayed. Try zooming in or disable some layers." );
63+
break;
64+
case ErrPoint1:
65+
message = tr( "The start point needs to be snapped and in the visible map view" );
66+
break;
67+
case ErrPoint2:
68+
if ( addingVertex )
69+
message = tr( "The end point needs to be snapped" );
70+
break;
71+
case ErrNoPath:
72+
if ( addingVertex )
73+
message = tr( "Endpoints are not connected" );
74+
break;
75+
case ErrNone:
76+
default:
77+
break;
8678
}
8779

8880
if ( message.isEmpty() )
@@ -93,14 +85,11 @@ void QgsMapCanvasTracer::reportError( QgsTracer::PathError err, bool addingVerte
9385
mMessageBar->pushItem( mLastMessage );
9486
}
9587

96-
void QgsMapCanvasTracer::updateSettings()
88+
void QgsMapCanvasTracer::configure()
9789
{
9890
setDestinationCrs( mCanvas->mapSettings().destinationCrs() );
9991
setExtent( mCanvas->extent() );
100-
}
10192

102-
void QgsMapCanvasTracer::updateLayerSettings()
103-
{
10493
QList<QgsVectorLayer*> layers;
10594
QStringList visibleLayerIds = mCanvas->mapSettings().layers();
10695

@@ -138,5 +127,5 @@ void QgsMapCanvasTracer::onCurrentLayerChanged()
138127
{
139128
// no need to bother if we are not snapping
140129
if ( mCanvas->snappingUtils()->snapToMapMode() == QgsSnappingUtils::SnapCurrentLayer )
141-
updateLayerSettings();
130+
invalidateGraph();
142131
}

src/gui/qgsmapcanvastracer.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class GUI_EXPORT QgsMapCanvasTracer : public QgsTracer
1818

1919
QAction* actionEnableTracing() { return mActionEnableTracing; }
2020

21-
virtual bool init();
22-
2321
//! Retrieve instance of this class associated with given canvas (if any).
2422
//! The class keeps a simple registry of tracers associated with map canvas
2523
//! instances for easier access to the common tracer by various map tools
@@ -28,9 +26,10 @@ class GUI_EXPORT QgsMapCanvasTracer : public QgsTracer
2826
//! Report a path finding error to the user
2927
void reportError( PathError err, bool addingVertex );
3028

29+
protected:
30+
virtual void configure();
31+
3132
private slots:
32-
void updateSettings();
33-
void updateLayerSettings();
3433
void onCurrentLayerChanged();
3534

3635
private:

0 commit comments

Comments
 (0)