|
15 | 15 |
|
16 | 16 | #include "qgstolerance.h"
|
17 | 17 | #include <QSettings>
|
| 18 | +#include <QPoint> |
| 19 | +#include <math.h> |
18 | 20 |
|
19 | 21 |
|
20 |
| -double QgsTolerance::toleranceInMapUnits( double tolerance, double mapUnitsPerPixel, UnitType units ) |
| 22 | + |
| 23 | +double QgsTolerance::toleranceInMapUnits( double tolerance, QgsMapLayer* layer, QgsMapRenderer* renderer, UnitType units ) |
21 | 24 | {
|
22 | 25 | if ( units == MapUnits )
|
23 | 26 | {
|
24 | 27 | return tolerance;
|
25 | 28 | }
|
| 29 | + double mapUnitsPerPixel = computeMapUnitPerPixel( layer, renderer ); |
26 | 30 | return tolerance * mapUnitsPerPixel;
|
27 | 31 | }
|
28 | 32 |
|
29 |
| -double QgsTolerance::vertexSearchRadius( double mapUnitsPerPixel ) |
| 33 | + |
| 34 | +double QgsTolerance::vertexSearchRadius( QgsMapLayer* layer, QgsMapRenderer* renderer ) |
30 | 35 | {
|
31 | 36 | QSettings settings;
|
32 | 37 | double tolerance = settings.value( "/qgis/digitizing/search_radius_vertex_edit", 10 ).toDouble();
|
33 | 38 | UnitType units = ( QgsTolerance::UnitType ) settings.value( "/qgis/digitizing/search_radius_vertex_edit_unit", 0 ).toInt();
|
34 |
| - return toleranceInMapUnits( tolerance, mapUnitsPerPixel, units ); |
| 39 | + return toleranceInMapUnits( tolerance, layer, renderer, units ); |
35 | 40 | }
|
36 | 41 |
|
37 |
| -double QgsTolerance::defaultTolerance( double mapUnitsPerPixel ) |
| 42 | + |
| 43 | +double QgsTolerance::defaultTolerance( QgsMapLayer* layer, QgsMapRenderer* renderer ) |
38 | 44 | {
|
39 | 45 | QSettings settings;
|
40 | 46 | double tolerance = settings.value( "/qgis/digitizing/default_snapping_tolerance", 0 ).toDouble();
|
41 | 47 | UnitType units = ( QgsTolerance::UnitType ) settings.value( "/qgis/digitizing/default_snapping_tolerance_unit", 0 ).toInt();
|
42 |
| - return toleranceInMapUnits( tolerance, mapUnitsPerPixel, units ); |
| 48 | + return toleranceInMapUnits( tolerance, layer, renderer, units ); |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +double QgsTolerance::computeMapUnitPerPixel( QgsMapLayer* layer, QgsMapRenderer* renderer ) |
| 53 | +{ |
| 54 | + if ( ! renderer->hasCrsTransformEnabled() ) |
| 55 | + { |
| 56 | + // if the on-the-fly projections are not enabled, layer units pre pixel are the same as map units per pixel |
| 57 | + return renderer->mapUnitsPerPixel(); |
| 58 | + } |
| 59 | + |
| 60 | + // the layer is projected. Find out how many pixels are in one map unit - either horizontal and vertical direction |
| 61 | + // this check might not work correctly in some cases |
| 62 | + // (on a large area the pixels projected around "0,0" can have different properties from the actual point) |
| 63 | + QgsPoint p1 = toLayerCoordinates(layer, renderer, QPoint(0,1)); |
| 64 | + QgsPoint p2 = toLayerCoordinates(layer, renderer, QPoint(0,2)); |
| 65 | + QgsPoint p3 = toLayerCoordinates(layer, renderer, QPoint(1,0)); |
| 66 | + QgsPoint p4 = toLayerCoordinates(layer, renderer, QPoint(2,0)); |
| 67 | + double x = p1.sqrDist(p2); |
| 68 | + double y = p3.sqrDist(p4); |
| 69 | + if (x > y) |
| 70 | + { |
| 71 | + return sqrt(x); |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + return sqrt(y); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +QgsPoint QgsTolerance::toLayerCoordinates( QgsMapLayer* layer, QgsMapRenderer* renderer, const QPoint& point ) |
| 81 | +{ |
| 82 | + QgsPoint pt = renderer->coordinateTransform()->toMapCoordinates( point ); |
| 83 | + return renderer->mapToLayerCoordinates( layer, pt ); |
43 | 84 | }
|
0 commit comments