Skip to content

Commit 2b08dfd

Browse files
author
wonder
committed
Added possibility to express tolerances for digitising also in pixels in addition to map units. Patch contributed by Richard Kostecky.
git-svn-id: http://svn.osgeo.org/qgis/trunk@10478 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 8363bf0 commit 2b08dfd

16 files changed

+287
-12
lines changed

python/core/core.sip

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
%Include qgscoordinatereferencesystem.sip
6565
%Include qgssymbol.sip
6666
%Include qgssymbologyutils.sip
67+
%Include qgstolerance.sip
6768
%Include qgsuniquevaluerenderer.sip
6869
%Include qgsvectordataprovider.sip
6970
%Include qgsvectorfilewriter.sip

python/core/qgssnapper.sip

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public:
6565
double mTolerance;
6666
/**What snapping type to use (snap to segment or to vertex)*/
6767
QgsSnapper::SnappingType mSnapTo;
68+
/**What unit is used for tolerance*/
69+
QgsTolerance::UnitType mUnitType;
6870
};
6971

7072
QgsSnapper(QgsMapRenderer* mapRender);

python/core/qgstolerance.sip

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
class QgsTolerance
3+
{
4+
%TypeHeaderCode
5+
#include <qgstolerance.h>
6+
%End
7+
8+
public:
9+
/**Type of unit of tolerance value from settings*/
10+
enum UnitType
11+
{
12+
/**Map unit value*/
13+
MapUnits,
14+
/**Pixels unit of tolerance*/
15+
Pixels
16+
};
17+
18+
/**
19+
* Static function to get vertex tolerance value from settings
20+
* @param mapUnitsPerPixel number of map units per pixel
21+
* @return value of vertex tolerance in map units
22+
*/
23+
static double vertexSearchRadius( double mapUnitsPerPixel );
24+
25+
/**
26+
* Static function to get default tolerance value from settings
27+
* @param mapUnitsPerPixel number of map units per pixel
28+
* @return value of default tolerance in map units
29+
*/
30+
static double defaultTolerance( double mapUnitsPerPixel );
31+
32+
/**
33+
* Static function to translate tolerance value into current map unit value
34+
* @param tolerace tolerance value to be translated
35+
* @param mapUnitsPerPixel number of map units per pixel
36+
* @param units type of units to be translated
37+
* @return value of tolerance in map units
38+
*/
39+
static double toleranceInMapUnits(double tolerance, double mapUnitsPerPixel, UnitType units = MapUnits);
40+
41+
};
42+

src/app/qgsmaptoolmovefeature.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgsrubberband.h"
2222
#include "qgsvectordataprovider.h"
2323
#include "qgsvectorlayer.h"
24+
#include "qgstolerance.h"
2425
#include <QMessageBox>
2526
#include <QMouseEvent>
2627
#include <QSettings>
@@ -71,7 +72,7 @@ void QgsMapToolMoveFeature::canvasPressEvent( QMouseEvent * e )
7172
//find first geometry under mouse cursor and store iterator to it
7273
QgsPoint layerCoords = toLayerCoordinates(( QgsMapLayer* )vlayer, e->pos() );
7374
QSettings settings;
74-
double searchRadius = settings.value( "/qgis/digitizing/search_radius_vertex_edit", 10 ).toDouble();
75+
double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->mapUnitsPerPixel() );
7576
QgsRectangle selectRect( layerCoords.x() - searchRadius, layerCoords.y() - searchRadius,
7677
layerCoords.x() + searchRadius, layerCoords.y() + searchRadius );
7778

src/app/qgsoptions.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgisapp.h"
2323
#include "qgsgenericprojectionselector.h"
2424
#include "qgscoordinatereferencesystem.h"
25+
#include "qgstolerance.h"
2526

2627
#include <QFileDialog>
2728
#include <QSettings>
@@ -196,6 +197,25 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
196197
mDefaultSnapModeComboBox->setCurrentIndex( mDefaultSnapModeComboBox->findData( defaultSnapString ) );
197198
mDefaultSnappingToleranceSpinBox->setValue( settings.value( "/qgis/digitizing/default_snapping_tolerance", 0 ).toDouble() );
198199
mSearchRadiusVertexEditSpinBox->setValue( settings.value( "/qgis/digitizing/search_radius_vertex_edit", 10 ).toDouble() );
200+
int index;
201+
if (settings.value( "/qgis/digitizing/default_snapping_tolerance_unit", 0 ).toInt() == QgsTolerance::MapUnits)
202+
{
203+
index = mDefaultSnappingToleranceComboBox->findText( tr( "map units" ) );
204+
}
205+
else
206+
{
207+
index = mDefaultSnappingToleranceComboBox->findText( tr( "pixels" ) );
208+
}
209+
mDefaultSnappingToleranceComboBox->setCurrentIndex( index );
210+
if (settings.value( "/qgis/digitizing/search_radius_vertex_edit_unit", 0 ).toInt() == QgsTolerance::MapUnits)
211+
{
212+
index = mSearchRadiusVertexEditComboBox->findText( tr( "map units" ) );
213+
}
214+
else
215+
{
216+
index = mSearchRadiusVertexEditComboBox->findText( tr( "pixels" ) );
217+
}
218+
mSearchRadiusVertexEditComboBox->setCurrentIndex( settings.value( "/qgis/digitizing/search_radius_vertex_edit_unit", 0 ).toInt() );
199219

200220
//vertex marker
201221
mMarkerStyleComboBox->addItem( tr( "Semi transparent circle" ) );
@@ -374,6 +394,11 @@ void QgsOptions::saveOptions()
374394
settings.setValue( "/qgis/digitizing/default_snap_mode", defaultSnapModeString );
375395
settings.setValue( "/qgis/digitizing/default_snapping_tolerance", mDefaultSnappingToleranceSpinBox->value() );
376396
settings.setValue( "/qgis/digitizing/search_radius_vertex_edit", mSearchRadiusVertexEditSpinBox->value() );
397+
settings.setValue( "/qgis/digitizing/default_snapping_tolerance_unit",
398+
(mDefaultSnappingToleranceComboBox->currentIndex() == 0 ? QgsTolerance::MapUnits : QgsTolerance::Pixels ) );
399+
settings.setValue( "/qgis/digitizing/search_radius_vertex_edit_unit",
400+
(mSearchRadiusVertexEditComboBox->currentIndex() == 0 ? QgsTolerance::MapUnits : QgsTolerance::Pixels ) );
401+
377402

378403
QString markerComboText = mMarkerStyleComboBox->currentText();
379404
if ( markerComboText == tr( "Semi transparent circle" ) )

src/app/qgsprojectproperties.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,13 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
122122
QStringList layerIdList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingList", &ok );
123123
QStringList enabledList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingEnabledList", &ok );
124124
QStringList toleranceList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingToleranceList", &ok );
125+
QStringList toleranceUnitList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingToleranceUnitList", &ok );
125126
QStringList snapToList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnapToList", &ok );
126127

127128
QStringList::const_iterator idIter = layerIdList.constBegin();
128129
QStringList::const_iterator enabledIter = enabledList.constBegin();
129130
QStringList::const_iterator tolIter = toleranceList.constBegin();
131+
QStringList::const_iterator tolUnitIter = toleranceUnitList.constBegin();
130132
QStringList::const_iterator snapToIter = snapToList.constBegin();
131133

132134
QgsMapLayer* currentLayer = 0;
@@ -160,6 +162,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
160162
newEntry.snapTo = 2;
161163
}
162164
newEntry.tolerance = tolIter->toDouble();
165+
newEntry.toleranceUnit = tolUnitIter->toInt();
163166
mSnappingLayerSettings.insert( *idIter, newEntry );
164167
}
165168
}
@@ -306,11 +309,13 @@ void QgsProjectProperties::apply()
306309
QStringList snapToList;
307310
QStringList toleranceList;
308311
QStringList enabledList;
312+
QStringList toleranceUnitList;
309313

310314
for ( layerEntryIt = mSnappingLayerSettings.constBegin(); layerEntryIt != mSnappingLayerSettings.constEnd(); ++layerEntryIt )
311315
{
312316
layerIdList << layerEntryIt.key();
313317
toleranceList << QString::number( layerEntryIt->tolerance, 'f' );
318+
toleranceUnitList << QString::number( (int)layerEntryIt->toleranceUnit );
314319
if ( layerEntryIt->checked )
315320
{
316321
enabledList << "enabled";
@@ -338,6 +343,7 @@ void QgsProjectProperties::apply()
338343
QgsProject::instance()->writeEntry( "Digitizing", "/LayerSnappingList", layerIdList );
339344
QgsProject::instance()->writeEntry( "Digitizing", "/LayerSnapToList", snapToList );
340345
QgsProject::instance()->writeEntry( "Digitizing", "/LayerSnappingToleranceList", toleranceList );
346+
QgsProject::instance()->writeEntry( "Digitizing", "/LayerSnappingToleranceUnitList", toleranceUnitList );
341347
QgsProject::instance()->writeEntry( "Digitizing", "/LayerSnappingEnabledList", enabledList );
342348
}
343349

src/app/qgssnappingdialog.cpp

+29-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ QgsSnappingDialog::QgsSnappingDialog( QgsMapCanvas* canvas, const QMap<QString,
7171
snappingToleranceEdit->setValidator( validator );
7272
mLayerTreeWidget->setItemWidget( newItem, 2, snappingToleranceEdit );
7373

74+
//snap to vertex/ snap to segment
75+
QComboBox* toleranceUnitsComboBox = new QComboBox( mLayerTreeWidget );
76+
toleranceUnitsComboBox->insertItem( 0, tr( "map units" ) );
77+
toleranceUnitsComboBox->insertItem( 1, tr( "pixels" ) );
78+
mLayerTreeWidget->setItemWidget( newItem, 3, toleranceUnitsComboBox );
79+
7480
settingIt = settings.find( currentVectorLayer->getLayerID() );
7581
if ( settingIt != settings.constEnd() )
7682
{
@@ -89,6 +95,15 @@ QgsSnappingDialog::QgsSnappingDialog( QgsMapCanvas* canvas, const QMap<QString,
8995
index = snapToComboBox->findText( tr( "to vertex and segment" ) );
9096
}
9197
snapToComboBox->setCurrentIndex( index );
98+
if ( settingIt.value().toleranceUnit == 0 )//map units
99+
{
100+
index = toleranceUnitsComboBox->findText( tr( "map units" ) );
101+
}
102+
else
103+
{
104+
index = toleranceUnitsComboBox->findText( tr( "pixels" ) );
105+
}
106+
toleranceUnitsComboBox->setCurrentIndex( index );
92107
if ( settingIt.value().checked )
93108
{
94109
newItem->setCheckState( 0, Qt::Checked );
@@ -103,8 +118,9 @@ QgsSnappingDialog::QgsSnappingDialog( QgsMapCanvas* canvas, const QMap<QString,
103118
}
104119
}
105120
mLayerTreeWidget->resizeColumnToContents( 0 );
106-
mLayerTreeWidget->setColumnWidth( 1, 300 ); //hardcoded for now
121+
mLayerTreeWidget->setColumnWidth( 1, 200 ); //hardcoded for now
107122
mLayerTreeWidget->resizeColumnToContents( 2 );
123+
mLayerTreeWidget->resizeColumnToContents( 3 );
108124
}
109125
}
110126

@@ -127,7 +143,9 @@ void QgsSnappingDialog::layerSettings( QMap<QString, LayerEntry>& settings ) con
127143
QString layerId;
128144
QString layerName;
129145
QString snapToItemText;
146+
QString toleranceItemText;
130147
int snapTo;
148+
int toleranceUnit;
131149
double tolerance;
132150
bool checked = false;
133151

@@ -144,6 +162,7 @@ void QgsSnappingDialog::layerSettings( QMap<QString, LayerEntry>& settings ) con
144162
layerId = mLayerIds.at( i );
145163
checked = ( currentItem->checkState( 0 ) == Qt::Checked );
146164
snapToItemText = (( QComboBox* )( mLayerTreeWidget->itemWidget( currentItem, 1 ) ) )->currentText();
165+
toleranceItemText = (( QComboBox* )( mLayerTreeWidget->itemWidget( currentItem, 3 ) ) )->currentText();
147166
if ( snapToItemText == tr( "to vertex" ) )
148167
{
149168
snapTo = 0;
@@ -156,10 +175,18 @@ void QgsSnappingDialog::layerSettings( QMap<QString, LayerEntry>& settings ) con
156175
{
157176
snapTo = 2;
158177
}
178+
if ( toleranceItemText == tr( "map units" ) )
179+
{
180+
toleranceUnit = 0;
181+
}
182+
else //to vertex and segment
183+
{
184+
toleranceUnit = 1;
185+
}
159186
tolerance = (( QLineEdit* )( mLayerTreeWidget->itemWidget( currentItem, 2 ) ) )->text().toDouble();
160187
LayerEntry newEntry;
161188
newEntry.checked = checked; newEntry.snapTo = snapTo; newEntry.layerName = layerName;
162-
newEntry.tolerance = tolerance;
189+
newEntry.tolerance = tolerance; newEntry.toleranceUnit = toleranceUnit;
163190
settings.insert( layerId, newEntry );
164191
}
165192
}

src/app/qgssnappingdialog.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct LayerEntry
2828
int snapTo; //0 = to vertex, 1 = to segment, 2 = to vertex and to segment
2929
QString layerName;
3030
double tolerance;
31+
int toleranceUnit;
3132
};
3233

3334
/**A dialog to enter advanced editing properties, e.g. topological editing, snapping settings

src/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ SET(QGIS_CORE_SRCS
4343
qgssearchtreenode.cpp
4444
qgssnapper.cpp
4545
qgscoordinatereferencesystem.cpp
46+
qgstolerance.cpp
4647
qgsvectordataprovider.cpp
4748
qgsvectorfilewriter.cpp
4849
qgsvectorlayer.cpp

src/core/qgssnapper.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ int QgsSnapper::snapPoint( const QPoint& startPoint, QList<QgsSnappingResult>& s
5656
{
5757
//transform point from map coordinates to layer coordinates
5858
layerCoordPoint = mMapRenderer->mapToLayerCoordinates( snapLayerIt->mLayer, mapCoordPoint );
59-
if ( snapLayerIt->mLayer->snapWithContext( layerCoordPoint, snapLayerIt->mTolerance,
59+
60+
double tolerance = QgsTolerance::toleranceInMapUnits( snapLayerIt->mTolerance, mMapRenderer->mapUnitsPerPixel(), snapLayerIt->mUnitType );
61+
if ( snapLayerIt->mLayer->snapWithContext( layerCoordPoint, tolerance,
6062
currentResultList, snapLayerIt->mSnapTo ) != 0 )
6163
{
6264
//error

src/core/qgssnapper.h

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define QGSSNAPPER_H
2020

2121
#include "qgspoint.h"
22+
#include "qgstolerance.h"
2223
#include <QList>
2324
#include <QMultiMap>
2425

@@ -86,6 +87,8 @@ class CORE_EXPORT QgsSnapper
8687
double mTolerance;
8788
/**What snapping type to use (snap to segment or to vertex)*/
8889
QgsSnapper::SnappingType mSnapTo;
90+
/**What unit is used for tolerance*/
91+
QgsTolerance::UnitType mUnitType;
8992
};
9093

9194
QgsSnapper( QgsMapRenderer* mapRender );

src/core/qgstolerance.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
19+
20+
double QgsTolerance::toleranceInMapUnits(double tolerance, double mapUnitsPerPixel, UnitType units)
21+
{
22+
if (units == MapUnits)
23+
{
24+
return tolerance;
25+
}
26+
return tolerance * mapUnitsPerPixel;
27+
}
28+
29+
double QgsTolerance::vertexSearchRadius( double mapUnitsPerPixel )
30+
{
31+
QSettings settings;
32+
double tolerance = settings.value( "/qgis/digitizing/search_radius_vertex_edit", 10 ).toDouble();
33+
UnitType units = (QgsTolerance::UnitType) settings.value( "/qgis/digitizing/search_radius_vertex_edit_unit", 0 ).toInt();
34+
return toleranceInMapUnits(tolerance, mapUnitsPerPixel, units);
35+
}
36+
37+
double QgsTolerance::defaultTolerance( double mapUnitsPerPixel )
38+
{
39+
QSettings settings;
40+
double tolerance = settings.value( "/qgis/digitizing/default_snapping_tolerance", 0 ).toDouble();
41+
UnitType units = (QgsTolerance::UnitType) settings.value( "/qgis/digitizing/default_snapping_tolerance_unit", 0 ).toInt();
42+
return toleranceInMapUnits(tolerance, mapUnitsPerPixel, units);
43+
}

src/core/qgstolerance.h

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/***************************************************************************
2+
qgstolerance.h - 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+
#ifndef QGSTOLERANCE_H
17+
#define QGSTOLERANCE_H
18+
19+
20+
/** \ingroup core
21+
* This is the class is providing tolerance value in map unit values.
22+
*
23+
* \note This class has been added in version 1.1.
24+
*/
25+
class CORE_EXPORT QgsTolerance
26+
{
27+
28+
public:
29+
/**Type of unit of tolerance value from settings*/
30+
enum UnitType
31+
{
32+
/**Map unit value*/
33+
MapUnits,
34+
/**Pixels unit of tolerance*/
35+
Pixels
36+
};
37+
38+
/**
39+
* Static function to get vertex tolerance value from settings
40+
* @param mapUnitsPerPixel number of map units per pixel
41+
* @return value of vertex tolerance in map units
42+
*/
43+
static double vertexSearchRadius( double mapUnitsPerPixel );
44+
45+
/**
46+
* Static function to get default tolerance value from settings
47+
* @param mapUnitsPerPixel number of map units per pixel
48+
* @return value of default tolerance in map units
49+
*/
50+
static double defaultTolerance( double mapUnitsPerPixel );
51+
52+
/**
53+
* Static function to translate tolerance value into current map unit value
54+
* @param tolerace tolerance value to be translated
55+
* @param mapUnitsPerPixel number of map units per pixel
56+
* @param units type of units to be translated
57+
* @return value of tolerance in map units
58+
*/
59+
static double toleranceInMapUnits(double tolerance, double mapUnitsPerPixel, UnitType units = MapUnits);
60+
61+
};
62+
63+
#endif

0 commit comments

Comments
 (0)