Navigation Menu

Skip to content

Commit

Permalink
[tracer] simpler and lazier updates of tracer configuration when some…
Browse files Browse the repository at this point in the history
…thing changes
  • Loading branch information
wonder-sk committed Jan 11, 2016
1 parent e7d277f commit 726fcfc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 43 deletions.
7 changes: 5 additions & 2 deletions src/core/qgstracer.cpp
Expand Up @@ -526,7 +526,7 @@ bool QgsTracer::initGraph()
int timeMake = t3.elapsed();

QgsDebugMsg( QString( "tracer extract %1 ms, noding %2 ms (call %3 ms), make %4 ms" )
.arg ( timeExtract ).arg( timeNoding ).arg ( timeNodingCall ).arg( timeMake ) );
.arg( timeExtract ).arg( timeNoding ).arg( timeNodingCall ).arg( timeMake ) );
return true;
}

Expand Down Expand Up @@ -582,6 +582,9 @@ bool QgsTracer::init()
if ( mGraph )
return true;

// configuration from derived class?
configure();

return initGraph();
}

Expand Down Expand Up @@ -611,7 +614,7 @@ void QgsTracer::onGeometryChanged( QgsFeatureId fid, QgsGeometry& geom )
invalidateGraph();
}

QVector<QgsPoint> QgsTracer::findShortestPath(const QgsPoint& p1, const QgsPoint& p2, PathError* error )
QVector<QgsPoint> QgsTracer::findShortestPath( const QgsPoint& p1, const QgsPoint& p2, PathError* error )
{
init(); // does nothing if the graph exists already
if ( !mGraph )
Expand Down
15 changes: 13 additions & 2 deletions src/core/qgstracer.h
Expand Up @@ -66,7 +66,10 @@ class CORE_EXPORT QgsTracer : public QObject
//! depending on how big the input layers are. It is not necessary
//! to call this method explicitly - it will be called by findShortestPath()
//! if necessary.
virtual bool init();
bool init();

//! Whether the internal data structures have been initialized
bool isInitialized() const { return mGraph != nullptr; }

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

protected:
//! Allows derived classes to setup the settings just before the tracer is initialized.
//! This allows the configuration to be set in a lazy way only when it is really necessary.
//! Default implementation does nothing.
virtual void configure() {}

protected slots:
void invalidateGraph();

private:
bool initGraph();
void invalidateGraph();

private slots:
void onFeatureAdded( QgsFeatureId fid );
Expand Down
59 changes: 24 additions & 35 deletions src/gui/qgsmapcanvastracer.cpp
Expand Up @@ -20,11 +20,12 @@ QgsMapCanvasTracer::QgsMapCanvasTracer( QgsMapCanvas* canvas, QgsMessageBar* mes
{
sTracers.insert( canvas, this );

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

mActionEnableTracing = new QAction( QIcon( QgsApplication::getThemeIcon( "/mActionTracing.png" ) ), tr( "Enable Tracing" ), this );
mActionEnableTracing->setShortcut( Qt::Key_T );
Expand All @@ -33,22 +34,13 @@ QgsMapCanvasTracer::QgsMapCanvasTracer( QgsMapCanvas* canvas, QgsMessageBar* mes
// arbitrarily chosen limit that should allow for fairly fast initialization
// of the underlying graph structure
setMaxFeatureCount( QSettings().value( "/qgis/digitizing/tracing_max_feature_count", 10000 ).toInt() );

updateSettings(); // initialize
updateLayerSettings();
}

QgsMapCanvasTracer::~QgsMapCanvasTracer()
{
sTracers.remove( mCanvas );
}

bool QgsMapCanvasTracer::init()
{
bool res = QgsTracer::init();
return res;
}

QgsMapCanvasTracer* QgsMapCanvasTracer::tracerForCanvas( QgsMapCanvas* canvas )
{
return sTracers.value( canvas, 0 );
Expand All @@ -66,23 +58,23 @@ void QgsMapCanvasTracer::reportError( QgsTracer::PathError err, bool addingVerte
QString message;
switch ( err )
{
case ErrTooManyFeatures:
message = tr( "Disabled - there are too many features displayed. Try zooming in or disable some layers." );
break;
case ErrPoint1:
message = tr( "The start point needs to be snapped and in the visible map view" );
break;
case ErrPoint2:
if ( addingVertex )
message = tr( "The end point needs to be snapped" );
break;
case ErrNoPath:
if ( addingVertex )
message = tr( "Endpoints are not connected" );
break;
case ErrNone:
default:
break;
case ErrTooManyFeatures:
message = tr( "Disabled - there are too many features displayed. Try zooming in or disable some layers." );
break;
case ErrPoint1:
message = tr( "The start point needs to be snapped and in the visible map view" );
break;
case ErrPoint2:
if ( addingVertex )
message = tr( "The end point needs to be snapped" );
break;
case ErrNoPath:
if ( addingVertex )
message = tr( "Endpoints are not connected" );
break;
case ErrNone:
default:
break;
}

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

void QgsMapCanvasTracer::updateSettings()
void QgsMapCanvasTracer::configure()
{
setDestinationCrs( mCanvas->mapSettings().destinationCrs() );
setExtent( mCanvas->extent() );
}

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

Expand Down Expand Up @@ -138,5 +127,5 @@ void QgsMapCanvasTracer::onCurrentLayerChanged()
{
// no need to bother if we are not snapping
if ( mCanvas->snappingUtils()->snapToMapMode() == QgsSnappingUtils::SnapCurrentLayer )
updateLayerSettings();
invalidateGraph();
}
7 changes: 3 additions & 4 deletions src/gui/qgsmapcanvastracer.h
Expand Up @@ -18,8 +18,6 @@ class GUI_EXPORT QgsMapCanvasTracer : public QgsTracer

QAction* actionEnableTracing() { return mActionEnableTracing; }

virtual bool init();

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

protected:
virtual void configure();

private slots:
void updateSettings();
void updateLayerSettings();
void onCurrentLayerChanged();

private:
Expand Down

0 comments on commit 726fcfc

Please sign in to comment.