Quantum GIS API Documentation  1.8
src/core/qgstolerance.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgstolerance.cpp  -  wrapper for tolerance handling
00003     ----------------------
00004     begin                : March 2009
00005     copyright            : (C) 2009 by Richard Kostecky
00006     email                : csf.kostej at gmail dot com
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include "qgstolerance.h"
00017 #include <QSettings>
00018 #include <QPoint>
00019 #include <cmath>
00020 
00021 double QgsTolerance::toleranceInMapUnits( double tolerance, QgsMapLayer* layer, QgsMapRenderer* renderer, UnitType units )
00022 {
00023   if ( units == MapUnits )
00024   {
00025     return tolerance;
00026   }
00027   double mapUnitsPerPixel = computeMapUnitPerPixel( layer, renderer );
00028   return tolerance * mapUnitsPerPixel;
00029 }
00030 
00031 
00032 double QgsTolerance::vertexSearchRadius( QgsMapLayer* layer, QgsMapRenderer* renderer )
00033 {
00034   QSettings settings;
00035   double tolerance = settings.value( "/qgis/digitizing/search_radius_vertex_edit", 10 ).toDouble();
00036   UnitType units = ( QgsTolerance::UnitType ) settings.value( "/qgis/digitizing/search_radius_vertex_edit_unit", QgsTolerance::Pixels ).toInt();
00037   return toleranceInMapUnits( tolerance, layer, renderer, units );
00038 }
00039 
00040 
00041 double QgsTolerance::defaultTolerance( QgsMapLayer* layer, QgsMapRenderer* renderer )
00042 {
00043   QSettings settings;
00044   double tolerance = settings.value( "/qgis/digitizing/default_snapping_tolerance", 0 ).toDouble();
00045   UnitType units = ( QgsTolerance::UnitType ) settings.value( "/qgis/digitizing/default_snapping_tolerance_unit", 0 ).toInt();
00046   return toleranceInMapUnits( tolerance, layer, renderer, units );
00047 }
00048 
00049 
00050 double QgsTolerance::computeMapUnitPerPixel( QgsMapLayer* layer, QgsMapRenderer* renderer )
00051 {
00052   if ( ! renderer->hasCrsTransformEnabled() )
00053   {
00054     // if the on-the-fly projections are not enabled, layer units pre pixel are the same as map units per pixel
00055     return renderer->mapUnitsPerPixel();
00056   }
00057 
00058   // the layer is projected. Find out how many pixels are in one map unit - either horizontal and vertical direction
00059   // this check might not work correctly in some cases
00060   // (on a large area the pixels projected around "0,0" can have different properties from the actual point)
00061   QgsPoint p1 = toLayerCoordinates( layer, renderer, QPoint( 0, 1 ) );
00062   QgsPoint p2 = toLayerCoordinates( layer, renderer, QPoint( 0, 2 ) );
00063   QgsPoint p3 = toLayerCoordinates( layer, renderer, QPoint( 1, 0 ) );
00064   QgsPoint p4 = toLayerCoordinates( layer, renderer, QPoint( 2, 0 ) );
00065   double x = p1.sqrDist( p2 );
00066   double y = p3.sqrDist( p4 );
00067   if ( x > y )
00068   {
00069     return sqrt( x );
00070   }
00071   else
00072   {
00073     return sqrt( y );
00074   }
00075 }
00076 
00077 
00078 QgsPoint QgsTolerance::toLayerCoordinates( QgsMapLayer* layer, QgsMapRenderer* renderer, const QPoint& point )
00079 {
00080   QgsPoint pt = renderer->coordinateTransform()->toMapCoordinates( point );
00081   return renderer->mapToLayerCoordinates( layer, pt );
00082 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines