Skip to content
Permalink
Browse files

[tracer] simpler and lazier updates of tracer configuration when some…

…thing changes
  • Loading branch information
wonder-sk committed Jan 11, 2016
1 parent e7d277f commit 726fcfc304a4d4f5b89a46916b277163b6513225
Showing with 45 additions and 43 deletions.
  1. +5 −2 src/core/qgstracer.cpp
  2. +13 −2 src/core/qgstracer.h
  3. +24 −35 src/gui/qgsmapcanvastracer.cpp
  4. +3 −4 src/gui/qgsmapcanvastracer.h
@@ -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;
}

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

// configuration from derived class?
configure();

return initGraph();
}

@@ -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 )
@@ -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
{
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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() )
@@ -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();

@@ -138,5 +127,5 @@ void QgsMapCanvasTracer::onCurrentLayerChanged()
{
// no need to bother if we are not snapping
if ( mCanvas->snappingUtils()->snapToMapMode() == QgsSnappingUtils::SnapCurrentLayer )
updateLayerSettings();
invalidateGraph();
}
@@ -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
@@ -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:

0 comments on commit 726fcfc

Please sign in to comment.
You can’t perform that action at this time.