Skip to content
Permalink
Browse files

[tracer] Make tracer follow layers configuration from snapping utils

  • Loading branch information
wonder-sk committed Jan 11, 2016
1 parent 33ea60d commit 3e4839344c386614162953926b72b13e2f22c2d1
Showing with 107 additions and 14 deletions.
  1. +40 −0 src/core/qgssnappingutils.cpp
  2. +21 −6 src/core/qgssnappingutils.h
  3. +44 −8 src/gui/qgsmapcanvastracer.cpp
  4. +2 −0 src/gui/qgsmapcanvastracer.h
@@ -369,15 +369,35 @@ void QgsSnappingUtils::setMapSettings( const QgsMapSettings& settings )
clearAllLocators();
}

void QgsSnappingUtils::setCurrentLayer( QgsVectorLayer* layer )
{
mCurrentLayer = layer;
}

void QgsSnappingUtils::setSnapToMapMode( QgsSnappingUtils::SnapToMapMode mode )
{
if ( mSnapToMapMode == mode )
return;

mSnapToMapMode = mode;
emit configChanged();
}

void QgsSnappingUtils::setDefaultSettings( int type, double tolerance, QgsTolerance::UnitType unit )
{
// force map units - can't use layer units for just any layer
if ( unit == QgsTolerance::LayerUnits )
unit = QgsTolerance::ProjectUnits;

if ( mDefaultType == type && mDefaultTolerance == tolerance && mDefaultUnit == unit )
return;

mDefaultType = type;
mDefaultTolerance = tolerance;
mDefaultUnit = unit;

if ( mSnapToMapMode != SnapAdvanced ) // does not affect advanced mode
emit configChanged();
}

void QgsSnappingUtils::defaultSettings( int& type, double& tolerance, QgsTolerance::UnitType& unit )
@@ -387,6 +407,25 @@ void QgsSnappingUtils::defaultSettings( int& type, double& tolerance, QgsToleran
unit = mDefaultUnit;
}

void QgsSnappingUtils::setLayers( const QList<QgsSnappingUtils::LayerConfig>& layers )
{
if ( mLayers == layers )
return;

mLayers = layers;
if ( mSnapToMapMode == SnapAdvanced ) // only affects advanced mode
emit configChanged();
}

void QgsSnappingUtils::setSnapOnIntersections( bool enabled )
{
if ( mSnapOnIntersection == enabled )
return;

mSnapOnIntersection = enabled;
emit configChanged();
}

const QgsCoordinateReferenceSystem* QgsSnappingUtils::destCRS()
{
return mMapSettings.hasCrsTransformEnabled() ? &mMapSettings.destinationCrs() : nullptr;
@@ -467,6 +506,7 @@ void QgsSnappingUtils::readConfigFromProject()
mLayers.append( LayerConfig( vlayer, t, tolIt->toDouble(), static_cast< QgsTolerance::UnitType >( tolUnitIt->toInt() ) ) );
}

emit configChanged();
}

void QgsSnappingUtils::onLayersWillBeRemoved( const QStringList& layerIds )
@@ -64,7 +64,7 @@ class CORE_EXPORT QgsSnappingUtils : public QObject
const QgsMapSettings& mapSettings() const { return mMapSettings; }

/** Set current layer so that if mode is SnapCurrentLayer we know which layer to use */
void setCurrentLayer( QgsVectorLayer* layer ) { mCurrentLayer = layer; }
void setCurrentLayer( QgsVectorLayer* layer );
QgsVectorLayer* currentLayer() const { return mCurrentLayer; }


@@ -79,7 +79,7 @@ class CORE_EXPORT QgsSnappingUtils : public QObject
};

/** Set how the snapping to map is done */
void setSnapToMapMode( SnapToMapMode mode ) { mSnapToMapMode = mode; }
void setSnapToMapMode( SnapToMapMode mode );
/** Find out how the snapping to map is done */
SnapToMapMode snapToMapMode() const { return mSnapToMapMode; }

@@ -95,9 +95,9 @@ class CORE_EXPORT QgsSnappingUtils : public QObject
/** Find out which strategy is used for indexing - by default hybrid indexing is used */
IndexingStrategy indexingStrategy() const { return mStrategy; }

/** Configure options used when the mode is snap to current layer */
/** Configure options used when the mode is snap to current layer or to all layers */
void setDefaultSettings( int type, double tolerance, QgsTolerance::UnitType unit );
/** Query options used when the mode is snap to current layer */
/** Query options used when the mode is snap to current layer or to all layers */
void defaultSettings( int& type, double& tolerance, QgsTolerance::UnitType& unit );

/**
@@ -107,6 +107,15 @@ class CORE_EXPORT QgsSnappingUtils : public QObject
{
LayerConfig( QgsVectorLayer* l, const QgsPointLocator::Types& t, double tol, QgsTolerance::UnitType u ) : layer( l ), type( t ), tolerance( tol ), unit( u ) {}

bool operator==( const LayerConfig& other ) const
{
return layer == other.layer && type == other.type && tolerance == other.tolerance && unit == other.unit;
}
bool operator!=( const LayerConfig& other ) const
{
return !operator==( other );
}

//! The layer to configure.
QgsVectorLayer* layer;
//! To which geometry properties of this layers a snapping should happen.
@@ -118,19 +127,25 @@ class CORE_EXPORT QgsSnappingUtils : public QObject
};

/** Set layers which will be used for snapping */
void setLayers( const QList<LayerConfig>& layers ) { mLayers = layers; }
void setLayers( const QList<LayerConfig>& layers );
/** Query layers used for snapping */
QList<LayerConfig> layers() const { return mLayers; }

/** Set whether to consider intersections of nearby segments for snapping */
void setSnapOnIntersections( bool enabled ) { mSnapOnIntersection = enabled; }
void setSnapOnIntersections( bool enabled );
/** Query whether to consider intersections of nearby segments for snapping */
bool snapOnIntersections() const { return mSnapOnIntersection; }

public slots:
/** Read snapping configuration from the project */
void readConfigFromProject();

signals:
/** Emitted when snapping configuration has been changed
* @note added in QGIS 2.14
*/
void configChanged();

protected:
//! Called when starting to index - can be overridden and e.g. progress dialog can be provided
virtual void prepareIndexStarting( int count ) { Q_UNUSED( count ); }
@@ -3,6 +3,7 @@
#include "qgsapplication.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayerregistry.h"
#include "qgssnappingutils.h"
#include "qgsvectorlayer.h"

#include <QAction>
@@ -16,15 +17,17 @@ QgsMapCanvasTracer::QgsMapCanvasTracer( QgsMapCanvas* canvas )
sTracers.insert( canvas, this );

connect( canvas, SIGNAL( destinationCrsChanged() ), this, SLOT( updateSettings() ) );
connect( canvas, SIGNAL( layersChanged() ), this, SLOT( updateSettings() ) );
connect( canvas, SIGNAL( layersChanged() ), this, SLOT( updateLayerSettings() ) );
connect( canvas, SIGNAL( extentsChanged() ), this, SLOT( updateSettings() ) );
// TODO: watch for snapping changes
connect( canvas, SIGNAL( currentLayerChanged( QgsMapLayer* ) ), this, SLOT( onCurrentLayerChanged() ) );
connect( canvas->snappingUtils(), SIGNAL( configChanged() ), this, SLOT( updateLayerSettings() ) );

mActionEnableTracing = new QAction( QIcon( QgsApplication::getThemeIcon( "/mActionTracing.png" ) ), tr( "Enable Tracing" ), this );
mActionEnableTracing->setShortcut( Qt::Key_T );
mActionEnableTracing->setCheckable( true );

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

QgsMapCanvasTracer::~QgsMapCanvasTracer()
@@ -38,16 +41,49 @@ QgsMapCanvasTracer* QgsMapCanvasTracer::tracerForCanvas( QgsMapCanvas* canvas )
}

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

void QgsMapCanvasTracer::updateLayerSettings()
{
QList<QgsVectorLayer*> layers;
foreach ( const QString& layerId, mCanvas->mapSettings().layers() )
QStringList visibleLayerIds = mCanvas->mapSettings().layers();

switch ( mCanvas->snappingUtils()->snapToMapMode() )
{
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
if ( vl )
layers << vl;
default:
case QgsSnappingUtils::SnapCurrentLayer:
{
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( mCanvas->currentLayer() );
if ( vl && visibleLayerIds.contains( vl->id() ) )
layers << vl;
}
break;
case QgsSnappingUtils::SnapAllLayers:
foreach ( const QString& layerId, visibleLayerIds )
{
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
if ( vl )
layers << vl;
}
break;
case QgsSnappingUtils::SnapAdvanced:
foreach ( const QgsSnappingUtils::LayerConfig& cfg, mCanvas->snappingUtils()->layers() )
{
if ( visibleLayerIds.contains( cfg.layer->id() ) )
layers << cfg.layer;
}
break;
}

setLayers( layers );
}

setDestinationCrs( mCanvas->mapSettings().destinationCrs() );
setExtent( mCanvas->extent() );
void QgsMapCanvasTracer::onCurrentLayerChanged()
{
// no need to bother if we are not snapping
if ( mCanvas->snappingUtils()->snapToMapMode() == QgsSnappingUtils::SnapCurrentLayer )
updateLayerSettings();
}
@@ -23,6 +23,8 @@ class GUI_EXPORT QgsMapCanvasTracer : public QgsTracer

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

private:
QgsMapCanvas* mCanvas;

0 comments on commit 3e48393

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