Skip to content

Commit

Permalink
Merge pull request #2282 from mhugent/snapper_backward_compatibility
Browse files Browse the repository at this point in the history
Better backwards compatibility for old snapping code
  • Loading branch information
mhugent committed Aug 31, 2015
2 parents 5abb560 + 649b1eb commit a4f5d9a
Showing 1 changed file with 83 additions and 73 deletions.
156 changes: 83 additions & 73 deletions src/gui/qgsmapcanvassnapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,61 @@ int QgsMapCanvasSnapper::snapToBackgroundLayers( const QgsPoint& point, QList<Qg
mSnapper->setSnapMode( QgsSnapper::SnapWithResultsWithinTolerances );
}

QgsVectorLayer* currentVectorLayer = dynamic_cast<QgsVectorLayer*>( mMapCanvas->currentLayer() );
if ( !currentVectorLayer )
{
return 1;
}

//read snapping settings from project
bool snappingDefinedInProject, ok;
QStringList layerIdList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingList", QStringList(), &snappingDefinedInProject );
QStringList enabledList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingEnabledList", QStringList(), &ok );
QStringList toleranceList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingToleranceList", QStringList(), &ok );
QStringList toleranceUnitList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingToleranceUnitList", QStringList(), &ok );
QStringList snapToList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnapToList", QStringList(), &ok );
QStringList layerIdList, enabledList, toleranceList, toleranceUnitList, snapToList;

bool ok, snappingDefinedInProject;

QSettings settings;
QString snappingMode = QgsProject::instance()->readEntry( "Digitizing", "/SnappingMode", "current_layer", &snappingDefinedInProject );
QString defaultSnapToleranceUnit = snappingDefinedInProject ? QgsProject::instance()->readEntry( "Digitizing", "/DefaultSnapToleranceUnit" ) : settings.value( "/qgis/digitizing/default_snapping_tolerance_unit", "0" ).toString();
QString defaultSnapType = snappingDefinedInProject ? QgsProject::instance()->readEntry( "Digitizing", "/DefaultSnapType" ) : settings.value( "/qgis/digitizing/default_snap_mode", "off" ).toString();
QString defaultSnapTolerance = snappingDefinedInProject ? QString::number( QgsProject::instance()->readDoubleEntry( "Digitizing", "/DefaultSnapTolerance" ) ) : settings.value( "/qgis/digitizing/default_snapping_tolerance", "0" ).toString();

if ( !snappingDefinedInProject && defaultSnapType == "off" )
{
return 0;
}

if ( snappingMode == "current_layer" || !snappingDefinedInProject )
{
layerIdList.append( currentVectorLayer->id() );
enabledList.append( "enabled" );
toleranceList.append( defaultSnapTolerance );
toleranceUnitList.append( defaultSnapToleranceUnit );
snapToList.append( defaultSnapType );
}
else if ( snappingMode == "all_layers" )
{
QList<QgsMapLayer*> allLayers = mMapCanvas->layers();
QList<QgsMapLayer*>::const_iterator layerIt = allLayers.constBegin();
for ( ; layerIt != allLayers.constEnd(); ++layerIt )
{
if ( !( *layerIt ) )
{
continue;
}
layerIdList.append(( *layerIt )->id() );
enabledList.append( "enabled" );
toleranceList.append( defaultSnapTolerance );
toleranceUnitList.append( defaultSnapToleranceUnit );
snapToList.append( defaultSnapType );
}
}
else //advanced
{
layerIdList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingList", QStringList(), &ok );
enabledList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingEnabledList", QStringList(), &ok );
toleranceList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingToleranceList", QStringList(), &ok );
toleranceUnitList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingToleranceUnitList", QStringList(), &ok );
snapToList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnapToList", QStringList(), &ok );
}

if ( !( layerIdList.size() == enabledList.size() &&
layerIdList.size() == toleranceList.size() &&
Expand All @@ -170,89 +218,51 @@ int QgsMapCanvasSnapper::snapToBackgroundLayers( const QgsPoint& point, QList<Qg
QList<QgsSnapper::SnapLayer> snapLayers;
QgsSnapper::SnapLayer snapLayer;

// Use snapping information from the project
if ( snappingDefinedInProject )


// set layers, tolerances, snap to segment/vertex to QgsSnapper
QStringList::const_iterator layerIt( layerIdList.constBegin() );
QStringList::const_iterator tolIt( toleranceList.constBegin() );
QStringList::const_iterator tolUnitIt( toleranceUnitList.constBegin() );
QStringList::const_iterator snapIt( snapToList.constBegin() );
QStringList::const_iterator enabledIt( enabledList.constBegin() );
for ( ; layerIt != layerIdList.constEnd(); ++layerIt, ++tolIt, ++tolUnitIt, ++snapIt, ++enabledIt )
{
// set layers, tolerances, snap to segment/vertex to QgsSnapper
QStringList::const_iterator layerIt( layerIdList.constBegin() );
QStringList::const_iterator tolIt( toleranceList.constBegin() );
QStringList::const_iterator tolUnitIt( toleranceUnitList.constBegin() );
QStringList::const_iterator snapIt( snapToList.constBegin() );
QStringList::const_iterator enabledIt( enabledList.constBegin() );
for ( ; layerIt != layerIdList.constEnd(); ++layerIt, ++tolIt, ++tolUnitIt, ++snapIt, ++enabledIt )
if ( *enabledIt != "enabled" )
{
if ( *enabledIt != "enabled" )
{
// skip layer if snapping is not enabled
continue;
}

//layer
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( QgsMapLayerRegistry::instance()->mapLayer( *layerIt ) );
if ( !vlayer || !vlayer->hasGeometryType() )
continue;
// skip layer if snapping is not enabled
continue;
}

snapLayer.mLayer = vlayer;
//layer
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( QgsMapLayerRegistry::instance()->mapLayer( *layerIt ) );
if ( !vlayer || !vlayer->hasGeometryType() )
continue;

//tolerance
snapLayer.mTolerance = tolIt->toDouble();
snapLayer.mUnitType = ( QgsTolerance::UnitType ) tolUnitIt->toInt();
snapLayer.mLayer = vlayer;

// segment or vertex
if ( *snapIt == "to_vertex" )
{
snapLayer.mSnapTo = QgsSnapper::SnapToVertex;
}
else if ( *snapIt == "to_segment" )
{
snapLayer.mSnapTo = QgsSnapper::SnapToSegment;
}
else
{
// to vertex and segment
snapLayer.mSnapTo = QgsSnapper::SnapToVertexAndSegment;
}
//tolerance
snapLayer.mTolerance = tolIt->toDouble();
snapLayer.mUnitType = ( QgsTolerance::UnitType ) tolUnitIt->toInt();

snapLayers.append( snapLayer );
// segment or vertex
if ( *snapIt == "to vertex" || *snapIt == "to_vertex" )
{
snapLayer.mSnapTo = QgsSnapper::SnapToVertex;
}
}
else
{
// nothing in project. Use default snapping tolerance to vertex of current layer
QgsMapLayer* currentLayer = mMapCanvas->currentLayer();
if ( !currentLayer )
return 2;

QgsVectorLayer* currentVectorLayer = qobject_cast<QgsVectorLayer *>( currentLayer );
if ( !currentVectorLayer )
return 3;

snapLayer.mLayer = currentVectorLayer;

//default snap mode
QSettings settings;
QString defaultSnapString = settings.value( "/qgis/digitizing/default_snap_mode", "off" ).toString();
if ( defaultSnapString == "to segment" )
else if ( *snapIt == "to segment" || *snapIt == "to_segment" )
{
snapLayer.mSnapTo = QgsSnapper::SnapToSegment;
}
else if ( defaultSnapString == "to vertex and segment" )
else if ( *snapIt == "to vertex and segment" || *snapIt == "to_vertex_and_segment" )
{
snapLayer.mSnapTo = QgsSnapper::SnapToVertexAndSegment;
}
else if ( defaultSnapString == "to vertex" )
{
snapLayer.mSnapTo = QgsSnapper::SnapToVertex;
}
else
else //off
{
return 0;
continue;
}

//default snapping tolerance (returned in map units)
snapLayer.mTolerance = QgsTolerance::defaultTolerance( currentVectorLayer, mMapCanvas->mapSettings() );
snapLayer.mUnitType = QgsTolerance::LayerUnits;

snapLayers.append( snapLayer );
}

Expand Down

1 comment on commit a4f5d9a

@3nids
Copy link
Member

@3nids 3nids commented on a4f5d9a Aug 31, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhugent I see in the node tool that you enabled the snapping (mSnapOnPress = true) but you still use the old snapping class.
In such case, the snapping is called twice, the old on being much slower.
It would be nice to remove calling to the old classes.

From what I understand, QgsMapAdvancedDigitizing/QgsMapMouseEvent which would allow to snap to the current layer, is this correct?

I can add the missing part if needed.

Please sign in to comment.