Skip to content
Permalink
Browse files

Update QgsTolerance to use QgsMapSettings instead of QgsMapRenderer

  • Loading branch information
wonder-sk committed Nov 11, 2013
1 parent 61c48be commit 1b7fcc26649721cac1411b110d99fa972f673c92
@@ -379,7 +379,7 @@ void QgsMapToolNodeTool::canvasPressEvent( QMouseEvent * e )
// some feature already selected
QgsPoint layerCoordPoint = toLayerCoordinates( vlayer, e->pos() );

double tol = QgsTolerance::vertexSearchRadius( vlayer, mCanvas->mapRenderer() );
double tol = QgsTolerance::vertexSearchRadius( vlayer, mCanvas->mapSettings() );

// get geometry and find if snapping is near it
int atVertex, beforeVertex, afterVertex;
@@ -656,7 +656,7 @@ void QgsMapToolNodeTool::canvasDoubleClickEvent( QMouseEvent * e )

QList<QgsSnappingResult> snapResults;
mMoving = false;
double tol = QgsTolerance::vertexSearchRadius( vlayer, mCanvas->mapRenderer() );
double tol = QgsTolerance::vertexSearchRadius( vlayer, mCanvas->mapSettings() );
mSnapper.snapToCurrentLayer( e->pos(), snapResults, QgsSnapper::SnapToSegment, tol );
if ( snapResults.size() < 1 ||
snapResults.first().snappedAtGeometry != mSelectedFeature->featureId() ||
@@ -69,7 +69,7 @@ void QgsMapToolMoveFeature::canvasPressEvent( QMouseEvent * e )
//find first geometry under mouse cursor and store iterator to it
QgsPoint layerCoords = toLayerCoordinates( vlayer, e->pos() );
QSettings settings;
double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapRenderer() );
double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapSettings() );
QgsRectangle selectRect( layerCoords.x() - searchRadius, layerCoords.y() - searchRadius,
layerCoords.x() + searchRadius, layerCoords.y() + searchRadius );

@@ -100,7 +100,7 @@ void QgsMapToolRotateFeature::canvasPressEvent( QMouseEvent * e )
}

QgsPoint layerCoords = toLayerCoordinates( vlayer, e->pos() );
double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapRenderer() );
double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapSettings() );
QgsRectangle selectRect( layerCoords.x() - searchRadius, layerCoords.y() - searchRadius,
layerCoords.x() + searchRadius, layerCoords.y() + searchRadius );

@@ -264,7 +264,7 @@ void QgsMapToolSimplify::canvasPressEvent( QMouseEvent * e )

QgsPoint layerCoords = mCanvas->getCoordinateTransform()->toMapPoint( e->pos().x(), e->pos().y() );

double r = QgsTolerance::vertexSearchRadius( vlayer, mCanvas->mapRenderer() );
double r = QgsTolerance::vertexSearchRadius( vlayer, mCanvas->mapSettings() );
QgsRectangle selectRect = QgsRectangle( layerCoords.x() - r, layerCoords.y() - r,
layerCoords.x() + r, layerCoords.y() + r );
QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( selectRect ).setSubsetOfAttributes( QgsAttributeList() ) );
@@ -1268,4 +1268,10 @@ QgsMapRenderer::BlendMode QgsMapRenderer::getBlendModeEnum( const QPainter::Comp
}
}

const QgsMapSettings& QgsMapRenderer::mapSettings() const
{
// TODO: keep up-to-date with real settings
return mMapSettings;
}

bool QgsMapRenderer::mDrawing = false;
@@ -26,6 +26,7 @@
#include "qgsrectangle.h"
#include "qgsrendercontext.h"
#include "qgsfeature.h"
#include "qgsmapsettings.h"

class QDomDocument;
class QDomNode;
@@ -289,6 +290,9 @@ class CORE_EXPORT QgsMapRenderer : public QObject
//! Added in 1.9
static QgsMapRenderer::BlendMode getBlendModeEnum( const QPainter::CompositionMode blendMode );

//! bridge to QgsMapSettings
const QgsMapSettings& mapSettings() const;

signals:

//! @deprecated in 2.1 - not emitted anymore
@@ -378,6 +382,8 @@ class CORE_EXPORT QgsMapRenderer : public QObject
//! Locks rendering loop for concurrent draws
QMutex mRenderMutex;

QgsMapSettings mMapSettings;

private:
const QgsCoordinateTransform* tr( QgsMapLayer *layer );
};
@@ -16,6 +16,7 @@
***************************************************************************/

#include "qgssnapper.h"
#include "qgsmapsettings.h"
#include "qgsmaprenderer.h"
#include "qgsmaptopixel.h"
#include "qgsvectorlayer.h"
@@ -24,14 +25,15 @@
#include <cmath>


QgsSnapper::QgsSnapper( QgsMapRenderer* mapRenderer ): mMapRenderer( mapRenderer )
QgsSnapper::QgsSnapper( QgsMapRenderer* mapRenderer )
: mMapSettings( mapRenderer->mapSettings() )
{

}

QgsSnapper::QgsSnapper()
QgsSnapper::QgsSnapper( const QgsMapSettings& mapSettings )
: mMapSettings( mapSettings )
{

}

QgsSnapper::~QgsSnapper()
@@ -47,17 +49,17 @@ int QgsSnapper::snapPoint( const QPoint& startPoint, QList<QgsSnappingResult>& s
QMultiMap<double, QgsSnappingResult> currentResultList; //snapping results of examined layer

//start point in (output) map coordinates
QgsPoint mapCoordPoint = mMapRenderer->coordinateTransform()->toMapCoordinates( startPoint.x(), startPoint.y() );
QgsPoint mapCoordPoint = mMapSettings.mapToPixel().toMapCoordinates( startPoint.x(), startPoint.y() );
QgsPoint layerCoordPoint; //start point in layer coordinates
QgsSnappingResult newResult;

QList<QgsSnapper::SnapLayer>::iterator snapLayerIt;
for ( snapLayerIt = mSnapLayers.begin(); snapLayerIt != mSnapLayers.end(); ++snapLayerIt )
{
//transform point from map coordinates to layer coordinates
layerCoordPoint = mMapRenderer->mapToLayerCoordinates( snapLayerIt->mLayer, mapCoordPoint );
layerCoordPoint = mMapSettings.mapToLayerCoordinates( snapLayerIt->mLayer, mapCoordPoint );

double tolerance = QgsTolerance::toleranceInMapUnits( snapLayerIt->mTolerance, snapLayerIt->mLayer, mMapRenderer, snapLayerIt->mUnitType );
double tolerance = QgsTolerance::toleranceInMapUnits( snapLayerIt->mTolerance, snapLayerIt->mLayer, mMapSettings, snapLayerIt->mUnitType );
if ( snapLayerIt->mLayer->snapWithContext( layerCoordPoint, tolerance,
currentResultList, snapLayerIt->mSnapTo ) != 0 )
{
@@ -71,9 +73,9 @@ int QgsSnapper::snapPoint( const QPoint& startPoint, QList<QgsSnappingResult>& s
//for each snapping result: transform start point, snap point and other points into map coordinates to find out distance
//store results in snapping result list
newResult = currentResultIt.value();
newResult.snappedVertex = mMapRenderer->layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().snappedVertex );
newResult.beforeVertex = mMapRenderer->layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().beforeVertex );
newResult.afterVertex = mMapRenderer->layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().afterVertex );
newResult.snappedVertex = mMapSettings.layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().snappedVertex );
newResult.beforeVertex = mMapSettings.layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().beforeVertex );
newResult.afterVertex = mMapSettings.layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().afterVertex );
snappingResultList.insert( sqrt( newResult.snappedVertex.sqrDist( mapCoordPoint ) ), newResult );
}
}
@@ -26,6 +26,7 @@
#include <QMultiMap>

class QgsMapRenderer;
class QgsMapSettings;
class QgsVectorLayer;
class QPoint;

@@ -93,7 +94,11 @@ class CORE_EXPORT QgsSnapper
QgsTolerance::UnitType mUnitType;
};

QgsSnapper( QgsMapRenderer* mapRender );
//!@ deprecated since 2.1 - use constructor with QgsMapSettings
Q_DECL_DEPRECATED QgsSnapper( QgsMapRenderer* mapRender );

explicit QgsSnapper( const QgsMapSettings& mapSettings );

~QgsSnapper();
/**Does the snapping operation
@param startPoint the start point for snapping (in pixel coordinates)
@@ -107,15 +112,13 @@ class CORE_EXPORT QgsSnapper
void setSnapMode( QgsSnapper::SnappingMode snapMode );

private:
/**Don't use the default constructor*/
QgsSnapper();

/**Removes the snapping results that contains points in exclude list*/
void cleanResultList( QMultiMap<double, QgsSnappingResult>& list, const QList<QgsPoint>& excludeList ) const;

/**The maprender object contains information about the output coordinate system
/**The map settings object contains information about the output coordinate system
of the map and about the relationship between pixel space and map space*/
QgsMapRenderer* mMapRenderer;
const QgsMapSettings& mMapSettings;
/**Snap mode to apply*/
QgsSnapper::SnappingMode mSnapMode;
/**List of layers to which snapping is applied*/
@@ -18,50 +18,64 @@
#include <QPoint>
#include <cmath>

double QgsTolerance::toleranceInMapUnits( double tolerance, QgsMapLayer* layer, QgsMapRenderer* renderer, UnitType units )
double QgsTolerance::toleranceInMapUnits( double tolerance, QgsMapLayer *layer, const QgsMapSettings& mapSettings, QgsTolerance::UnitType units )
{
if ( units == MapUnits )
{
return tolerance;
}
double mapUnitsPerPixel = computeMapUnitPerPixel( layer, renderer );
double mapUnitsPerPixel = computeMapUnitPerPixel( layer, mapSettings );
return tolerance * mapUnitsPerPixel;
}

double QgsTolerance::toleranceInMapUnits( double tolerance, QgsMapLayer* layer, QgsMapRenderer* renderer, UnitType units )
{
return toleranceInMapUnits( tolerance, layer, renderer->mapSettings(), units );
}

double QgsTolerance::vertexSearchRadius( QgsMapLayer* layer, QgsMapRenderer* renderer )
double QgsTolerance::vertexSearchRadius(QgsMapLayer *layer, const QgsMapSettings &mapSettings)
{
QSettings settings;
double tolerance = settings.value( "/qgis/digitizing/search_radius_vertex_edit", 10 ).toDouble();
UnitType units = ( QgsTolerance::UnitType ) settings.value( "/qgis/digitizing/search_radius_vertex_edit_unit", QgsTolerance::Pixels ).toInt();
return toleranceInMapUnits( tolerance, layer, renderer, units );
return toleranceInMapUnits( tolerance, layer, mapSettings, units );
}

double QgsTolerance::vertexSearchRadius( QgsMapLayer* layer, QgsMapRenderer* renderer )
{
return vertexSearchRadius( layer, renderer->mapSettings() );
}

double QgsTolerance::defaultTolerance( QgsMapLayer* layer, QgsMapRenderer* renderer )
double QgsTolerance::defaultTolerance( QgsMapLayer *layer, const QgsMapSettings& mapSettings )
{
QSettings settings;
double tolerance = settings.value( "/qgis/digitizing/default_snapping_tolerance", 0 ).toDouble();
UnitType units = ( QgsTolerance::UnitType ) settings.value( "/qgis/digitizing/default_snapping_tolerance_unit", 0 ).toInt();
return toleranceInMapUnits( tolerance, layer, renderer, units );
return toleranceInMapUnits( tolerance, layer, mapSettings, units );
}


double QgsTolerance::defaultTolerance( QgsMapLayer* layer, QgsMapRenderer* renderer )
{
return defaultTolerance( layer, renderer->mapSettings() );
}


double QgsTolerance::computeMapUnitPerPixel( QgsMapLayer* layer, QgsMapRenderer* renderer )
double QgsTolerance::computeMapUnitPerPixel(QgsMapLayer* layer, const QgsMapSettings& mapSettings )
{
if ( ! renderer->hasCrsTransformEnabled() )
if ( ! mapSettings.hasCrsTransformEnabled() )
{
// if the on-the-fly projections are not enabled, layer units pre pixel are the same as map units per pixel
return renderer->mapUnitsPerPixel();
return mapSettings.mapUnitsPerPixel();
}

// the layer is projected. Find out how many pixels are in one map unit - either horizontal and vertical direction
// this check might not work correctly in some cases
// (on a large area the pixels projected around "0,0" can have different properties from the actual point)
QgsPoint p1 = toLayerCoordinates( layer, renderer, QPoint( 0, 1 ) );
QgsPoint p2 = toLayerCoordinates( layer, renderer, QPoint( 0, 2 ) );
QgsPoint p3 = toLayerCoordinates( layer, renderer, QPoint( 1, 0 ) );
QgsPoint p4 = toLayerCoordinates( layer, renderer, QPoint( 2, 0 ) );
QgsPoint p1 = toLayerCoordinates( layer, mapSettings, QPoint( 0, 1 ) );
QgsPoint p2 = toLayerCoordinates( layer, mapSettings, QPoint( 0, 2 ) );
QgsPoint p3 = toLayerCoordinates( layer, mapSettings, QPoint( 1, 0 ) );
QgsPoint p4 = toLayerCoordinates( layer, mapSettings, QPoint( 2, 0 ) );
double x = p1.sqrDist( p2 );
double y = p3.sqrDist( p4 );
if ( x > y )
@@ -75,8 +89,8 @@ double QgsTolerance::computeMapUnitPerPixel( QgsMapLayer* layer, QgsMapRenderer*
}


QgsPoint QgsTolerance::toLayerCoordinates( QgsMapLayer* layer, QgsMapRenderer* renderer, const QPoint& point )
QgsPoint QgsTolerance::toLayerCoordinates( QgsMapLayer* layer, const QgsMapSettings& mapSettings, const QPoint& point )
{
QgsPoint pt = renderer->coordinateTransform()->toMapCoordinates( point );
return renderer->mapToLayerCoordinates( layer, pt );
QgsPoint pt = mapSettings.mapToPixel().toMapCoordinates( point );
return mapSettings.mapToLayerCoordinates( layer, pt );
}
@@ -43,14 +43,40 @@ class CORE_EXPORT QgsTolerance
* The value is read from settings and transformed if necessary.
* @return value of vertex tolerance in map units
*/
static double vertexSearchRadius( QgsMapLayer* layer, QgsMapRenderer* renderer );
static double vertexSearchRadius( QgsMapLayer* layer, const QgsMapSettings& mapSettings );

/**
* Static function to get vertex tolerance value for a layer.
* The value is read from settings and transformed if necessary.
* @return value of vertex tolerance in map units
*/
//! @deprecated since 2.1 - use override with QgsMapSettings
Q_DECL_DEPRECATED static double vertexSearchRadius( QgsMapLayer* layer, QgsMapRenderer* renderer );

/**
* Static function to get default tolerance value for a layer.
* The value is read from settings and transformed if necessary.
* @return value of default tolerance in map units
*/
static double defaultTolerance( QgsMapLayer* layer, const QgsMapSettings& mapSettings );

/**
* Static function to get default tolerance value for a layer.
* The value is read from settings and transformed if necessary.
* @return value of default tolerance in map units
*/
static double defaultTolerance( QgsMapLayer* layer, QgsMapRenderer* renderer );
//! @deprecated since 2.1 - use override with QgsMapSettings
Q_DECL_DEPRECATED static double defaultTolerance( QgsMapLayer* layer, QgsMapRenderer* renderer );

/**
* Static function to translate tolerance value into current map unit value
* @param tolerance tolerance value to be translated
* @param layer reference layer
* @param mapSettings settings of the map
* @param units type of units to be translated
* @return value of tolerance in map units
*/
static double toleranceInMapUnits( double tolerance, QgsMapLayer* layer, const QgsMapSettings& mapSettings, UnitType units = MapUnits );

/**
* Static function to translate tolerance value into current map unit value
@@ -60,11 +86,12 @@ class CORE_EXPORT QgsTolerance
* @param units type of units to be translated
* @return value of tolerance in map units
*/
static double toleranceInMapUnits( double tolerance, QgsMapLayer* layer, QgsMapRenderer* renderer, UnitType units = MapUnits );
//! @deprecated since 2.1 - use the override with QgsMapSettings
Q_DECL_DEPRECATED static double toleranceInMapUnits( double tolerance, QgsMapLayer* layer, QgsMapRenderer* renderer, UnitType units = MapUnits );

private:
static double computeMapUnitPerPixel( QgsMapLayer* layer, QgsMapRenderer* renderer );
static QgsPoint toLayerCoordinates( QgsMapLayer* layer, QgsMapRenderer* renderer, const QPoint& point );
static double computeMapUnitPerPixel( QgsMapLayer* layer, const QgsMapSettings& mapSettings );
static QgsPoint toLayerCoordinates(QgsMapLayer* layer, const QgsMapSettings& mapSettings, const QPoint& point );

};

@@ -33,11 +33,7 @@ QgsMapCanvasSnapper::QgsMapCanvasSnapper( QgsMapCanvas* canvas )
if ( !canvas )
return;

QgsMapRenderer *canvasRender = canvas->mapRenderer();
if ( !canvasRender )
return;

mSnapper = new QgsSnapper( canvasRender );
mSnapper = new QgsSnapper( canvas->mapSettings() );
}

QgsMapCanvasSnapper::QgsMapCanvasSnapper(): mMapCanvas( 0 ), mSnapper( 0 )
@@ -55,7 +51,7 @@ void QgsMapCanvasSnapper::setMapCanvas( QgsMapCanvas* canvas )
delete mSnapper;
if ( mMapCanvas )
{
mSnapper = new QgsSnapper( canvas->mapRenderer() );
mSnapper = new QgsSnapper( canvas->mapSettings() );
}
else
{
@@ -101,7 +97,7 @@ int QgsMapCanvasSnapper::snapToCurrentLayer( const QPoint& p, QList<QgsSnappingR
if ( snappingTol < 0 )
{
//use search tolerance for vertex editing
snapLayer.mTolerance = QgsTolerance::vertexSearchRadius( vlayer, mMapCanvas->mapRenderer() );
snapLayer.mTolerance = QgsTolerance::vertexSearchRadius( vlayer, mMapCanvas->mapSettings() );
}
else
{
@@ -247,7 +243,7 @@ int QgsMapCanvasSnapper::snapToBackgroundLayers( const QPoint& p, QList<QgsSnapp
}

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

snapLayers.append( snapLayer );

0 comments on commit 1b7fcc2

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