QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgstolerance.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgstolerance.cpp - wrapper for tolerance handling
3  ----------------------
4  begin : March 2009
5  copyright : (C) 2009 by Richard Kostecky
6  email : csf.kostej at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgstolerance.h"
17 #include <QSettings>
18 #include <QPoint>
19 #include <cmath>
20 
21 double QgsTolerance::toleranceInMapUnits( double tolerance, QgsMapLayer* layer, QgsMapRenderer* renderer, UnitType units )
22 {
23  if ( units == MapUnits )
24  {
25  return tolerance;
26  }
27  double mapUnitsPerPixel = computeMapUnitPerPixel( layer, renderer );
28  return tolerance * mapUnitsPerPixel;
29 }
30 
31 
33 {
34  QSettings settings;
35  double tolerance = settings.value( "/qgis/digitizing/search_radius_vertex_edit", 10 ).toDouble();
36  UnitType units = ( QgsTolerance::UnitType ) settings.value( "/qgis/digitizing/search_radius_vertex_edit_unit", QgsTolerance::Pixels ).toInt();
37  return toleranceInMapUnits( tolerance, layer, renderer, units );
38 }
39 
40 
42 {
43  QSettings settings;
44  double tolerance = settings.value( "/qgis/digitizing/default_snapping_tolerance", 0 ).toDouble();
45  UnitType units = ( QgsTolerance::UnitType ) settings.value( "/qgis/digitizing/default_snapping_tolerance_unit", 0 ).toInt();
46  return toleranceInMapUnits( tolerance, layer, renderer, units );
47 }
48 
49 
51 {
52  if ( ! renderer->hasCrsTransformEnabled() )
53  {
54  // 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();
56  }
57 
58  // the layer is projected. Find out how many pixels are in one map unit - either horizontal and vertical direction
59  // this check might not work correctly in some cases
60  // (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 ) );
65  double x = p1.sqrDist( p2 );
66  double y = p3.sqrDist( p4 );
67  if ( x > y )
68  {
69  return sqrt( x );
70  }
71  else
72  {
73  return sqrt( y );
74  }
75 }
76 
77 
78 QgsPoint QgsTolerance::toLayerCoordinates( QgsMapLayer* layer, QgsMapRenderer* renderer, const QPoint& point )
79 {
80  QgsPoint pt = renderer->coordinateTransform()->toMapCoordinates( point );
81  return renderer->mapToLayerCoordinates( layer, pt );
82 }