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(); int timeMake = t3.elapsed();


QgsDebugMsg( QString( "tracer extract %1 ms, noding %2 ms (call %3 ms), make %4 ms" ) 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; return true;
} }


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


// configuration from derived class?
configure();

return initGraph(); return initGraph();
} }


Expand Down Expand Up @@ -611,7 +614,7 @@ void QgsTracer::onGeometryChanged( QgsFeatureId fid, QgsGeometry& geom )
invalidateGraph(); 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 init(); // does nothing if the graph exists already
if ( !mGraph ) 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 //! depending on how big the input layers are. It is not necessary
//! to call this method explicitly - it will be called by findShortestPath() //! to call this method explicitly - it will be called by findShortestPath()
//! if necessary. //! if necessary.
virtual bool init(); bool init();

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


enum PathError 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) //! 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 ); 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: private:
bool initGraph(); bool initGraph();
void invalidateGraph();


private slots: private slots:
void onFeatureAdded( QgsFeatureId fid ); 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 ); sTracers.insert( canvas, this );


connect( canvas, SIGNAL( destinationCrsChanged() ), this, SLOT( updateSettings() ) ); // when things change we just invalidate the graph - and set up new parameters again only when necessary
connect( canvas, SIGNAL( layersChanged() ), this, SLOT( updateLayerSettings() ) ); connect( canvas, SIGNAL( destinationCrsChanged() ), this, SLOT( invalidateGraph() ) );
connect( canvas, SIGNAL( extentsChanged() ), this, SLOT( updateSettings() ) ); connect( canvas, SIGNAL( layersChanged() ), this, SLOT( invalidateGraph() ) );
connect( canvas, SIGNAL( extentsChanged() ), this, SLOT( invalidateGraph() ) );
connect( canvas, SIGNAL( currentLayerChanged( QgsMapLayer* ) ), this, SLOT( onCurrentLayerChanged() ) ); 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 = new QAction( QIcon( QgsApplication::getThemeIcon( "/mActionTracing.png" ) ), tr( "Enable Tracing" ), this );
mActionEnableTracing->setShortcut( Qt::Key_T ); 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 // arbitrarily chosen limit that should allow for fairly fast initialization
// of the underlying graph structure // of the underlying graph structure
setMaxFeatureCount( QSettings().value( "/qgis/digitizing/tracing_max_feature_count", 10000 ).toInt() ); setMaxFeatureCount( QSettings().value( "/qgis/digitizing/tracing_max_feature_count", 10000 ).toInt() );

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


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


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

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


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


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


void QgsMapCanvasTracer::updateLayerSettings()
{
QList<QgsVectorLayer*> layers; QList<QgsVectorLayer*> layers;
QStringList visibleLayerIds = mCanvas->mapSettings().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 // no need to bother if we are not snapping
if ( mCanvas->snappingUtils()->snapToMapMode() == QgsSnappingUtils::SnapCurrentLayer ) 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; } QAction* actionEnableTracing() { return mActionEnableTracing; }


virtual bool init();

//! Retrieve instance of this class associated with given canvas (if any). //! Retrieve instance of this class associated with given canvas (if any).
//! The class keeps a simple registry of tracers associated with map canvas //! The class keeps a simple registry of tracers associated with map canvas
//! instances for easier access to the common tracer by various map tools //! 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 //! Report a path finding error to the user
void reportError( PathError err, bool addingVertex ); void reportError( PathError err, bool addingVertex );


protected:
virtual void configure();

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


private: private:
Expand Down

0 comments on commit 726fcfc

Please sign in to comment.