Skip to content

Commit 1b7fcc2

Browse files
committed
Update QgsTolerance to use QgsMapSettings instead of QgsMapRenderer
1 parent 61c48be commit 1b7fcc2

11 files changed

+102
-48
lines changed

src/app/nodetool/qgsmaptoolnodetool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ void QgsMapToolNodeTool::canvasPressEvent( QMouseEvent * e )
379379
// some feature already selected
380380
QgsPoint layerCoordPoint = toLayerCoordinates( vlayer, e->pos() );
381381

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

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

657657
QList<QgsSnappingResult> snapResults;
658658
mMoving = false;
659-
double tol = QgsTolerance::vertexSearchRadius( vlayer, mCanvas->mapRenderer() );
659+
double tol = QgsTolerance::vertexSearchRadius( vlayer, mCanvas->mapSettings() );
660660
mSnapper.snapToCurrentLayer( e->pos(), snapResults, QgsSnapper::SnapToSegment, tol );
661661
if ( snapResults.size() < 1 ||
662662
snapResults.first().snappedAtGeometry != mSelectedFeature->featureId() ||

src/app/qgsmaptoolmovefeature.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void QgsMapToolMoveFeature::canvasPressEvent( QMouseEvent * e )
6969
//find first geometry under mouse cursor and store iterator to it
7070
QgsPoint layerCoords = toLayerCoordinates( vlayer, e->pos() );
7171
QSettings settings;
72-
double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapRenderer() );
72+
double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapSettings() );
7373
QgsRectangle selectRect( layerCoords.x() - searchRadius, layerCoords.y() - searchRadius,
7474
layerCoords.x() + searchRadius, layerCoords.y() + searchRadius );
7575

src/app/qgsmaptoolrotatefeature.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void QgsMapToolRotateFeature::canvasPressEvent( QMouseEvent * e )
100100
}
101101

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

src/app/qgsmaptoolsimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ void QgsMapToolSimplify::canvasPressEvent( QMouseEvent * e )
264264

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

267-
double r = QgsTolerance::vertexSearchRadius( vlayer, mCanvas->mapRenderer() );
267+
double r = QgsTolerance::vertexSearchRadius( vlayer, mCanvas->mapSettings() );
268268
QgsRectangle selectRect = QgsRectangle( layerCoords.x() - r, layerCoords.y() - r,
269269
layerCoords.x() + r, layerCoords.y() + r );
270270
QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( selectRect ).setSubsetOfAttributes( QgsAttributeList() ) );

src/core/qgsmaprenderer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,4 +1268,10 @@ QgsMapRenderer::BlendMode QgsMapRenderer::getBlendModeEnum( const QPainter::Comp
12681268
}
12691269
}
12701270

1271+
const QgsMapSettings& QgsMapRenderer::mapSettings() const
1272+
{
1273+
// TODO: keep up-to-date with real settings
1274+
return mMapSettings;
1275+
}
1276+
12711277
bool QgsMapRenderer::mDrawing = false;

src/core/qgsmaprenderer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgsrectangle.h"
2727
#include "qgsrendercontext.h"
2828
#include "qgsfeature.h"
29+
#include "qgsmapsettings.h"
2930

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

293+
//! bridge to QgsMapSettings
294+
const QgsMapSettings& mapSettings() const;
295+
292296
signals:
293297

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

385+
QgsMapSettings mMapSettings;
386+
381387
private:
382388
const QgsCoordinateTransform* tr( QgsMapLayer *layer );
383389
};

src/core/qgssnapper.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgssnapper.h"
19+
#include "qgsmapsettings.h"
1920
#include "qgsmaprenderer.h"
2021
#include "qgsmaptopixel.h"
2122
#include "qgsvectorlayer.h"
@@ -24,14 +25,15 @@
2425
#include <cmath>
2526

2627

27-
QgsSnapper::QgsSnapper( QgsMapRenderer* mapRenderer ): mMapRenderer( mapRenderer )
28+
QgsSnapper::QgsSnapper( QgsMapRenderer* mapRenderer )
29+
: mMapSettings( mapRenderer->mapSettings() )
2830
{
2931

3032
}
3133

32-
QgsSnapper::QgsSnapper()
34+
QgsSnapper::QgsSnapper( const QgsMapSettings& mapSettings )
35+
: mMapSettings( mapSettings )
3336
{
34-
3537
}
3638

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

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

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

60-
double tolerance = QgsTolerance::toleranceInMapUnits( snapLayerIt->mTolerance, snapLayerIt->mLayer, mMapRenderer, snapLayerIt->mUnitType );
62+
double tolerance = QgsTolerance::toleranceInMapUnits( snapLayerIt->mTolerance, snapLayerIt->mLayer, mMapSettings, snapLayerIt->mUnitType );
6163
if ( snapLayerIt->mLayer->snapWithContext( layerCoordPoint, tolerance,
6264
currentResultList, snapLayerIt->mSnapTo ) != 0 )
6365
{
@@ -71,9 +73,9 @@ int QgsSnapper::snapPoint( const QPoint& startPoint, QList<QgsSnappingResult>& s
7173
//for each snapping result: transform start point, snap point and other points into map coordinates to find out distance
7274
//store results in snapping result list
7375
newResult = currentResultIt.value();
74-
newResult.snappedVertex = mMapRenderer->layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().snappedVertex );
75-
newResult.beforeVertex = mMapRenderer->layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().beforeVertex );
76-
newResult.afterVertex = mMapRenderer->layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().afterVertex );
76+
newResult.snappedVertex = mMapSettings.layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().snappedVertex );
77+
newResult.beforeVertex = mMapSettings.layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().beforeVertex );
78+
newResult.afterVertex = mMapSettings.layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().afterVertex );
7779
snappingResultList.insert( sqrt( newResult.snappedVertex.sqrDist( mapCoordPoint ) ), newResult );
7880
}
7981
}

src/core/qgssnapper.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <QMultiMap>
2727

2828
class QgsMapRenderer;
29+
class QgsMapSettings;
2930
class QgsVectorLayer;
3031
class QPoint;
3132

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

96-
QgsSnapper( QgsMapRenderer* mapRender );
97+
//!@ deprecated since 2.1 - use constructor with QgsMapSettings
98+
Q_DECL_DEPRECATED QgsSnapper( QgsMapRenderer* mapRender );
99+
100+
explicit QgsSnapper( const QgsMapSettings& mapSettings );
101+
97102
~QgsSnapper();
98103
/**Does the snapping operation
99104
@param startPoint the start point for snapping (in pixel coordinates)
@@ -107,15 +112,13 @@ class CORE_EXPORT QgsSnapper
107112
void setSnapMode( QgsSnapper::SnappingMode snapMode );
108113

109114
private:
110-
/**Don't use the default constructor*/
111-
QgsSnapper();
112115

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

116-
/**The maprender object contains information about the output coordinate system
119+
/**The map settings object contains information about the output coordinate system
117120
of the map and about the relationship between pixel space and map space*/
118-
QgsMapRenderer* mMapRenderer;
121+
const QgsMapSettings& mMapSettings;
119122
/**Snap mode to apply*/
120123
QgsSnapper::SnappingMode mSnapMode;
121124
/**List of layers to which snapping is applied*/

src/core/qgstolerance.cpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,64 @@
1818
#include <QPoint>
1919
#include <cmath>
2020

21-
double QgsTolerance::toleranceInMapUnits( double tolerance, QgsMapLayer* layer, QgsMapRenderer* renderer, UnitType units )
21+
double QgsTolerance::toleranceInMapUnits( double tolerance, QgsMapLayer *layer, const QgsMapSettings& mapSettings, QgsTolerance::UnitType units )
2222
{
2323
if ( units == MapUnits )
2424
{
2525
return tolerance;
2626
}
27-
double mapUnitsPerPixel = computeMapUnitPerPixel( layer, renderer );
27+
double mapUnitsPerPixel = computeMapUnitPerPixel( layer, mapSettings );
2828
return tolerance * mapUnitsPerPixel;
2929
}
3030

31+
double QgsTolerance::toleranceInMapUnits( double tolerance, QgsMapLayer* layer, QgsMapRenderer* renderer, UnitType units )
32+
{
33+
return toleranceInMapUnits( tolerance, layer, renderer->mapSettings(), units );
34+
}
3135

32-
double QgsTolerance::vertexSearchRadius( QgsMapLayer* layer, QgsMapRenderer* renderer )
36+
double QgsTolerance::vertexSearchRadius(QgsMapLayer *layer, const QgsMapSettings &mapSettings)
3337
{
3438
QSettings settings;
3539
double tolerance = settings.value( "/qgis/digitizing/search_radius_vertex_edit", 10 ).toDouble();
3640
UnitType units = ( QgsTolerance::UnitType ) settings.value( "/qgis/digitizing/search_radius_vertex_edit_unit", QgsTolerance::Pixels ).toInt();
37-
return toleranceInMapUnits( tolerance, layer, renderer, units );
41+
return toleranceInMapUnits( tolerance, layer, mapSettings, units );
3842
}
3943

44+
double QgsTolerance::vertexSearchRadius( QgsMapLayer* layer, QgsMapRenderer* renderer )
45+
{
46+
return vertexSearchRadius( layer, renderer->mapSettings() );
47+
}
4048

41-
double QgsTolerance::defaultTolerance( QgsMapLayer* layer, QgsMapRenderer* renderer )
49+
double QgsTolerance::defaultTolerance( QgsMapLayer *layer, const QgsMapSettings& mapSettings )
4250
{
4351
QSettings settings;
4452
double tolerance = settings.value( "/qgis/digitizing/default_snapping_tolerance", 0 ).toDouble();
4553
UnitType units = ( QgsTolerance::UnitType ) settings.value( "/qgis/digitizing/default_snapping_tolerance_unit", 0 ).toInt();
46-
return toleranceInMapUnits( tolerance, layer, renderer, units );
54+
return toleranceInMapUnits( tolerance, layer, mapSettings, units );
55+
}
56+
57+
58+
double QgsTolerance::defaultTolerance( QgsMapLayer* layer, QgsMapRenderer* renderer )
59+
{
60+
return defaultTolerance( layer, renderer->mapSettings() );
4761
}
4862

4963

50-
double QgsTolerance::computeMapUnitPerPixel( QgsMapLayer* layer, QgsMapRenderer* renderer )
64+
double QgsTolerance::computeMapUnitPerPixel(QgsMapLayer* layer, const QgsMapSettings& mapSettings )
5165
{
52-
if ( ! renderer->hasCrsTransformEnabled() )
66+
if ( ! mapSettings.hasCrsTransformEnabled() )
5367
{
5468
// if the on-the-fly projections are not enabled, layer units pre pixel are the same as map units per pixel
55-
return renderer->mapUnitsPerPixel();
69+
return mapSettings.mapUnitsPerPixel();
5670
}
5771

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

7791

78-
QgsPoint QgsTolerance::toLayerCoordinates( QgsMapLayer* layer, QgsMapRenderer* renderer, const QPoint& point )
92+
QgsPoint QgsTolerance::toLayerCoordinates( QgsMapLayer* layer, const QgsMapSettings& mapSettings, const QPoint& point )
7993
{
80-
QgsPoint pt = renderer->coordinateTransform()->toMapCoordinates( point );
81-
return renderer->mapToLayerCoordinates( layer, pt );
94+
QgsPoint pt = mapSettings.mapToPixel().toMapCoordinates( point );
95+
return mapSettings.mapToLayerCoordinates( layer, pt );
8296
}

src/core/qgstolerance.h

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,40 @@ class CORE_EXPORT QgsTolerance
4343
* The value is read from settings and transformed if necessary.
4444
* @return value of vertex tolerance in map units
4545
*/
46-
static double vertexSearchRadius( QgsMapLayer* layer, QgsMapRenderer* renderer );
46+
static double vertexSearchRadius( QgsMapLayer* layer, const QgsMapSettings& mapSettings );
47+
48+
/**
49+
* Static function to get vertex tolerance value for a layer.
50+
* The value is read from settings and transformed if necessary.
51+
* @return value of vertex tolerance in map units
52+
*/
53+
//! @deprecated since 2.1 - use override with QgsMapSettings
54+
Q_DECL_DEPRECATED static double vertexSearchRadius( QgsMapLayer* layer, QgsMapRenderer* renderer );
55+
56+
/**
57+
* Static function to get default tolerance value for a layer.
58+
* The value is read from settings and transformed if necessary.
59+
* @return value of default tolerance in map units
60+
*/
61+
static double defaultTolerance( QgsMapLayer* layer, const QgsMapSettings& mapSettings );
4762

4863
/**
4964
* Static function to get default tolerance value for a layer.
5065
* The value is read from settings and transformed if necessary.
5166
* @return value of default tolerance in map units
5267
*/
53-
static double defaultTolerance( QgsMapLayer* layer, QgsMapRenderer* renderer );
68+
//! @deprecated since 2.1 - use override with QgsMapSettings
69+
Q_DECL_DEPRECATED static double defaultTolerance( QgsMapLayer* layer, QgsMapRenderer* renderer );
70+
71+
/**
72+
* Static function to translate tolerance value into current map unit value
73+
* @param tolerance tolerance value to be translated
74+
* @param layer reference layer
75+
* @param mapSettings settings of the map
76+
* @param units type of units to be translated
77+
* @return value of tolerance in map units
78+
*/
79+
static double toleranceInMapUnits( double tolerance, QgsMapLayer* layer, const QgsMapSettings& mapSettings, UnitType units = MapUnits );
5480

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

6592
private:
66-
static double computeMapUnitPerPixel( QgsMapLayer* layer, QgsMapRenderer* renderer );
67-
static QgsPoint toLayerCoordinates( QgsMapLayer* layer, QgsMapRenderer* renderer, const QPoint& point );
93+
static double computeMapUnitPerPixel( QgsMapLayer* layer, const QgsMapSettings& mapSettings );
94+
static QgsPoint toLayerCoordinates(QgsMapLayer* layer, const QgsMapSettings& mapSettings, const QPoint& point );
6895

6996
};
7097

0 commit comments

Comments
 (0)