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
This commit is contained in:
wonder 2009-04-05 16:46:13 +00:00
parent 8363bf063e
commit 2b08dfda2b
16 changed files with 287 additions and 12 deletions

View File

@ -64,6 +64,7 @@
%Include qgscoordinatereferencesystem.sip
%Include qgssymbol.sip
%Include qgssymbologyutils.sip
%Include qgstolerance.sip
%Include qgsuniquevaluerenderer.sip
%Include qgsvectordataprovider.sip
%Include qgsvectorfilewriter.sip

View File

@ -65,6 +65,8 @@ public:
double mTolerance;
/**What snapping type to use (snap to segment or to vertex)*/
QgsSnapper::SnappingType mSnapTo;
/**What unit is used for tolerance*/
QgsTolerance::UnitType mUnitType;
};
QgsSnapper(QgsMapRenderer* mapRender);

View File

@ -0,0 +1,42 @@
class QgsTolerance
{
%TypeHeaderCode
#include <qgstolerance.h>
%End
public:
/**Type of unit of tolerance value from settings*/
enum UnitType
{
/**Map unit value*/
MapUnits,
/**Pixels unit of tolerance*/
Pixels
};
/**
* Static function to get vertex tolerance value from settings
* @param mapUnitsPerPixel number of map units per pixel
* @return value of vertex tolerance in map units
*/
static double vertexSearchRadius( double mapUnitsPerPixel );
/**
* Static function to get default tolerance value from settings
* @param mapUnitsPerPixel number of map units per pixel
* @return value of default tolerance in map units
*/
static double defaultTolerance( double mapUnitsPerPixel );
/**
* Static function to translate tolerance value into current map unit value
* @param tolerace tolerance value to be translated
* @param mapUnitsPerPixel number of map units per pixel
* @param units type of units to be translated
* @return value of tolerance in map units
*/
static double toleranceInMapUnits(double tolerance, double mapUnitsPerPixel, UnitType units = MapUnits);
};

View File

@ -21,6 +21,7 @@
#include "qgsrubberband.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgstolerance.h"
#include <QMessageBox>
#include <QMouseEvent>
#include <QSettings>
@ -71,7 +72,7 @@ void QgsMapToolMoveFeature::canvasPressEvent( QMouseEvent * e )
//find first geometry under mouse cursor and store iterator to it
QgsPoint layerCoords = toLayerCoordinates(( QgsMapLayer* )vlayer, e->pos() );
QSettings settings;
double searchRadius = settings.value( "/qgis/digitizing/search_radius_vertex_edit", 10 ).toDouble();
double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->mapUnitsPerPixel() );
QgsRectangle selectRect( layerCoords.x() - searchRadius, layerCoords.y() - searchRadius,
layerCoords.x() + searchRadius, layerCoords.y() + searchRadius );

View File

@ -22,6 +22,7 @@
#include "qgisapp.h"
#include "qgsgenericprojectionselector.h"
#include "qgscoordinatereferencesystem.h"
#include "qgstolerance.h"
#include <QFileDialog>
#include <QSettings>
@ -196,6 +197,25 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
mDefaultSnapModeComboBox->setCurrentIndex( mDefaultSnapModeComboBox->findData( defaultSnapString ) );
mDefaultSnappingToleranceSpinBox->setValue( settings.value( "/qgis/digitizing/default_snapping_tolerance", 0 ).toDouble() );
mSearchRadiusVertexEditSpinBox->setValue( settings.value( "/qgis/digitizing/search_radius_vertex_edit", 10 ).toDouble() );
int index;
if (settings.value( "/qgis/digitizing/default_snapping_tolerance_unit", 0 ).toInt() == QgsTolerance::MapUnits)
{
index = mDefaultSnappingToleranceComboBox->findText( tr( "map units" ) );
}
else
{
index = mDefaultSnappingToleranceComboBox->findText( tr( "pixels" ) );
}
mDefaultSnappingToleranceComboBox->setCurrentIndex( index );
if (settings.value( "/qgis/digitizing/search_radius_vertex_edit_unit", 0 ).toInt() == QgsTolerance::MapUnits)
{
index = mSearchRadiusVertexEditComboBox->findText( tr( "map units" ) );
}
else
{
index = mSearchRadiusVertexEditComboBox->findText( tr( "pixels" ) );
}
mSearchRadiusVertexEditComboBox->setCurrentIndex( settings.value( "/qgis/digitizing/search_radius_vertex_edit_unit", 0 ).toInt() );
//vertex marker
mMarkerStyleComboBox->addItem( tr( "Semi transparent circle" ) );
@ -374,6 +394,11 @@ void QgsOptions::saveOptions()
settings.setValue( "/qgis/digitizing/default_snap_mode", defaultSnapModeString );
settings.setValue( "/qgis/digitizing/default_snapping_tolerance", mDefaultSnappingToleranceSpinBox->value() );
settings.setValue( "/qgis/digitizing/search_radius_vertex_edit", mSearchRadiusVertexEditSpinBox->value() );
settings.setValue( "/qgis/digitizing/default_snapping_tolerance_unit",
(mDefaultSnappingToleranceComboBox->currentIndex() == 0 ? QgsTolerance::MapUnits : QgsTolerance::Pixels ) );
settings.setValue( "/qgis/digitizing/search_radius_vertex_edit_unit",
(mSearchRadiusVertexEditComboBox->currentIndex() == 0 ? QgsTolerance::MapUnits : QgsTolerance::Pixels ) );
QString markerComboText = mMarkerStyleComboBox->currentText();
if ( markerComboText == tr( "Semi transparent circle" ) )

View File

@ -122,11 +122,13 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
QStringList layerIdList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingList", &ok );
QStringList enabledList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingEnabledList", &ok );
QStringList toleranceList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingToleranceList", &ok );
QStringList toleranceUnitList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingToleranceUnitList", &ok );
QStringList snapToList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnapToList", &ok );
QStringList::const_iterator idIter = layerIdList.constBegin();
QStringList::const_iterator enabledIter = enabledList.constBegin();
QStringList::const_iterator tolIter = toleranceList.constBegin();
QStringList::const_iterator tolUnitIter = toleranceUnitList.constBegin();
QStringList::const_iterator snapToIter = snapToList.constBegin();
QgsMapLayer* currentLayer = 0;
@ -160,6 +162,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
newEntry.snapTo = 2;
}
newEntry.tolerance = tolIter->toDouble();
newEntry.toleranceUnit = tolUnitIter->toInt();
mSnappingLayerSettings.insert( *idIter, newEntry );
}
}
@ -306,11 +309,13 @@ void QgsProjectProperties::apply()
QStringList snapToList;
QStringList toleranceList;
QStringList enabledList;
QStringList toleranceUnitList;
for ( layerEntryIt = mSnappingLayerSettings.constBegin(); layerEntryIt != mSnappingLayerSettings.constEnd(); ++layerEntryIt )
{
layerIdList << layerEntryIt.key();
toleranceList << QString::number( layerEntryIt->tolerance, 'f' );
toleranceUnitList << QString::number( (int)layerEntryIt->toleranceUnit );
if ( layerEntryIt->checked )
{
enabledList << "enabled";
@ -338,6 +343,7 @@ void QgsProjectProperties::apply()
QgsProject::instance()->writeEntry( "Digitizing", "/LayerSnappingList", layerIdList );
QgsProject::instance()->writeEntry( "Digitizing", "/LayerSnapToList", snapToList );
QgsProject::instance()->writeEntry( "Digitizing", "/LayerSnappingToleranceList", toleranceList );
QgsProject::instance()->writeEntry( "Digitizing", "/LayerSnappingToleranceUnitList", toleranceUnitList );
QgsProject::instance()->writeEntry( "Digitizing", "/LayerSnappingEnabledList", enabledList );
}

View File

@ -71,6 +71,12 @@ QgsSnappingDialog::QgsSnappingDialog( QgsMapCanvas* canvas, const QMap<QString,
snappingToleranceEdit->setValidator( validator );
mLayerTreeWidget->setItemWidget( newItem, 2, snappingToleranceEdit );
//snap to vertex/ snap to segment
QComboBox* toleranceUnitsComboBox = new QComboBox( mLayerTreeWidget );
toleranceUnitsComboBox->insertItem( 0, tr( "map units" ) );
toleranceUnitsComboBox->insertItem( 1, tr( "pixels" ) );
mLayerTreeWidget->setItemWidget( newItem, 3, toleranceUnitsComboBox );
settingIt = settings.find( currentVectorLayer->getLayerID() );
if ( settingIt != settings.constEnd() )
{
@ -89,6 +95,15 @@ QgsSnappingDialog::QgsSnappingDialog( QgsMapCanvas* canvas, const QMap<QString,
index = snapToComboBox->findText( tr( "to vertex and segment" ) );
}
snapToComboBox->setCurrentIndex( index );
if ( settingIt.value().toleranceUnit == 0 )//map units
{
index = toleranceUnitsComboBox->findText( tr( "map units" ) );
}
else
{
index = toleranceUnitsComboBox->findText( tr( "pixels" ) );
}
toleranceUnitsComboBox->setCurrentIndex( index );
if ( settingIt.value().checked )
{
newItem->setCheckState( 0, Qt::Checked );
@ -103,8 +118,9 @@ QgsSnappingDialog::QgsSnappingDialog( QgsMapCanvas* canvas, const QMap<QString,
}
}
mLayerTreeWidget->resizeColumnToContents( 0 );
mLayerTreeWidget->setColumnWidth( 1, 300 ); //hardcoded for now
mLayerTreeWidget->setColumnWidth( 1, 200 ); //hardcoded for now
mLayerTreeWidget->resizeColumnToContents( 2 );
mLayerTreeWidget->resizeColumnToContents( 3 );
}
}
@ -127,7 +143,9 @@ void QgsSnappingDialog::layerSettings( QMap<QString, LayerEntry>& settings ) con
QString layerId;
QString layerName;
QString snapToItemText;
QString toleranceItemText;
int snapTo;
int toleranceUnit;
double tolerance;
bool checked = false;
@ -144,6 +162,7 @@ void QgsSnappingDialog::layerSettings( QMap<QString, LayerEntry>& settings ) con
layerId = mLayerIds.at( i );
checked = ( currentItem->checkState( 0 ) == Qt::Checked );
snapToItemText = (( QComboBox* )( mLayerTreeWidget->itemWidget( currentItem, 1 ) ) )->currentText();
toleranceItemText = (( QComboBox* )( mLayerTreeWidget->itemWidget( currentItem, 3 ) ) )->currentText();
if ( snapToItemText == tr( "to vertex" ) )
{
snapTo = 0;
@ -156,10 +175,18 @@ void QgsSnappingDialog::layerSettings( QMap<QString, LayerEntry>& settings ) con
{
snapTo = 2;
}
if ( toleranceItemText == tr( "map units" ) )
{
toleranceUnit = 0;
}
else //to vertex and segment
{
toleranceUnit = 1;
}
tolerance = (( QLineEdit* )( mLayerTreeWidget->itemWidget( currentItem, 2 ) ) )->text().toDouble();
LayerEntry newEntry;
newEntry.checked = checked; newEntry.snapTo = snapTo; newEntry.layerName = layerName;
newEntry.tolerance = tolerance;
newEntry.tolerance = tolerance; newEntry.toleranceUnit = toleranceUnit;
settings.insert( layerId, newEntry );
}
}

View File

@ -28,6 +28,7 @@ struct LayerEntry
int snapTo; //0 = to vertex, 1 = to segment, 2 = to vertex and to segment
QString layerName;
double tolerance;
int toleranceUnit;
};
/**A dialog to enter advanced editing properties, e.g. topological editing, snapping settings

View File

@ -43,6 +43,7 @@ SET(QGIS_CORE_SRCS
qgssearchtreenode.cpp
qgssnapper.cpp
qgscoordinatereferencesystem.cpp
qgstolerance.cpp
qgsvectordataprovider.cpp
qgsvectorfilewriter.cpp
qgsvectorlayer.cpp

View File

@ -56,7 +56,9 @@ int QgsSnapper::snapPoint( const QPoint& startPoint, QList<QgsSnappingResult>& s
{
//transform point from map coordinates to layer coordinates
layerCoordPoint = mMapRenderer->mapToLayerCoordinates( snapLayerIt->mLayer, mapCoordPoint );
if ( snapLayerIt->mLayer->snapWithContext( layerCoordPoint, snapLayerIt->mTolerance,
double tolerance = QgsTolerance::toleranceInMapUnits( snapLayerIt->mTolerance, mMapRenderer->mapUnitsPerPixel(), snapLayerIt->mUnitType );
if ( snapLayerIt->mLayer->snapWithContext( layerCoordPoint, tolerance,
currentResultList, snapLayerIt->mSnapTo ) != 0 )
{
//error

View File

@ -19,6 +19,7 @@
#define QGSSNAPPER_H
#include "qgspoint.h"
#include "qgstolerance.h"
#include <QList>
#include <QMultiMap>
@ -86,6 +87,8 @@ class CORE_EXPORT QgsSnapper
double mTolerance;
/**What snapping type to use (snap to segment or to vertex)*/
QgsSnapper::SnappingType mSnapTo;
/**What unit is used for tolerance*/
QgsTolerance::UnitType mUnitType;
};
QgsSnapper( QgsMapRenderer* mapRender );

43
src/core/qgstolerance.cpp Normal file
View File

@ -0,0 +1,43 @@
/***************************************************************************
qgstolerance.cpp - wrapper for tolerance handling
----------------------
begin : March 2009
copyright : (C) 2009 by Richard Kostecky
email : csf.kostej at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgstolerance.h"
#include <QSettings>
double QgsTolerance::toleranceInMapUnits(double tolerance, double mapUnitsPerPixel, UnitType units)
{
if (units == MapUnits)
{
return tolerance;
}
return tolerance * mapUnitsPerPixel;
}
double QgsTolerance::vertexSearchRadius( double mapUnitsPerPixel )
{
QSettings settings;
double tolerance = settings.value( "/qgis/digitizing/search_radius_vertex_edit", 10 ).toDouble();
UnitType units = (QgsTolerance::UnitType) settings.value( "/qgis/digitizing/search_radius_vertex_edit_unit", 0 ).toInt();
return toleranceInMapUnits(tolerance, mapUnitsPerPixel, units);
}
double QgsTolerance::defaultTolerance( double mapUnitsPerPixel )
{
QSettings settings;
double tolerance = settings.value( "/qgis/digitizing/default_snapping_tolerance", 0 ).toDouble();
UnitType units = (QgsTolerance::UnitType) settings.value( "/qgis/digitizing/default_snapping_tolerance_unit", 0 ).toInt();
return toleranceInMapUnits(tolerance, mapUnitsPerPixel, units);
}

63
src/core/qgstolerance.h Normal file
View File

@ -0,0 +1,63 @@
/***************************************************************************
qgstolerance.h - wrapper for tolerance handling
----------------------
begin : March 2009
copyright : (C) 2009 by Richard Kostecky
email : csf.kostej at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSTOLERANCE_H
#define QGSTOLERANCE_H
/** \ingroup core
* This is the class is providing tolerance value in map unit values.
*
* \note This class has been added in version 1.1.
*/
class CORE_EXPORT QgsTolerance
{
public:
/**Type of unit of tolerance value from settings*/
enum UnitType
{
/**Map unit value*/
MapUnits,
/**Pixels unit of tolerance*/
Pixels
};
/**
* Static function to get vertex tolerance value from settings
* @param mapUnitsPerPixel number of map units per pixel
* @return value of vertex tolerance in map units
*/
static double vertexSearchRadius( double mapUnitsPerPixel );
/**
* Static function to get default tolerance value from settings
* @param mapUnitsPerPixel number of map units per pixel
* @return value of default tolerance in map units
*/
static double defaultTolerance( double mapUnitsPerPixel );
/**
* Static function to translate tolerance value into current map unit value
* @param tolerace tolerance value to be translated
* @param mapUnitsPerPixel number of map units per pixel
* @param units type of units to be translated
* @return value of tolerance in map units
*/
static double toleranceInMapUnits(double tolerance, double mapUnitsPerPixel, UnitType units = MapUnits);
};
#endif

View File

@ -21,8 +21,10 @@
#include "qgsmaptopixel.h"
#include "qgsproject.h"
#include "qgsvectorlayer.h"
#include "qgstolerance.h"
#include <QSettings>
QgsMapCanvasSnapper::QgsMapCanvasSnapper( QgsMapCanvas* canvas ): mMapCanvas( canvas ), mSnapper( 0 )
{
if ( canvas )
@ -92,13 +94,14 @@ int QgsMapCanvasSnapper::snapToCurrentLayer( const QPoint& p, QList<QgsSnappingR
QgsSnapper::SnapLayer snapLayer;
snapLayer.mLayer = vlayer;
snapLayer.mSnapTo = snap_to;
snapLayer.mUnitType = QgsTolerance::MapUnits;
QSettings settings;
if ( snappingTol < 0 )
{
//use search tolerance for vertex editing
snapLayer.mTolerance = settings.value( "/qgis/digitizing/search_radius_vertex_edit", 50 ).toDouble();
snapLayer.mTolerance = QgsTolerance::vertexSearchRadius(mMapCanvas->mapUnitsPerPixel());
}
else
{
@ -149,9 +152,10 @@ int QgsMapCanvasSnapper::snapToBackgroundLayers( const QPoint& p, QList<QgsSnapp
}
QStringList enabledList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingEnabledList", &ok );
QStringList toleranceList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingToleranceList", &ok );
QStringList toleranceUnitList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnappingToleranceUnitList", &ok );
QStringList snapToList = QgsProject::instance()->readListEntry( "Digitizing", "/LayerSnapToList", &ok );
if ( !( layerIdList.size() == enabledList.size() && layerIdList.size() == toleranceList.size() && layerIdList.size() == snapToList.size() ) )
if ( !( layerIdList.size() == enabledList.size() && layerIdList.size() == toleranceList.size() && layerIdList.size() == toleranceUnitList.size() && layerIdList.size() == snapToList.size() ) )
{
return 1; //lists must have the same size, otherwise something is wrong
}
@ -168,10 +172,11 @@ int QgsMapCanvasSnapper::snapToBackgroundLayers( const QPoint& p, QList<QgsSnapp
QStringList::const_iterator layerIt = layerIdList.constBegin();
QStringList::const_iterator tolIt = toleranceList.constBegin();
QStringList::const_iterator tolUnitIt = toleranceUnitList.constBegin();
QStringList::const_iterator snapIt = snapToList.constBegin();
QStringList::const_iterator enabledIt = enabledList.constBegin();
for ( ; layerIt != layerIdList.constEnd(); ++layerIt, ++tolIt, ++snapIt, ++enabledIt )
for ( ; layerIt != layerIdList.constEnd(); ++layerIt, ++tolIt, ++tolUnitIt, ++snapIt, ++enabledIt )
{
if (( *enabledIt ) != "enabled" ) //skip layer if snapping is not enabled
{
@ -191,6 +196,7 @@ int QgsMapCanvasSnapper::snapToBackgroundLayers( const QPoint& p, QList<QgsSnapp
//tolerance
snapLayer.mTolerance = tolIt->toDouble();
snapLayer.mUnitType = (QgsTolerance::UnitType) tolUnitIt->toInt();
//segment or vertex
if (( *snapIt ) == "to_vertex" )
@ -241,8 +247,9 @@ int QgsMapCanvasSnapper::snapToBackgroundLayers( const QPoint& p, QList<QgsSnapp
snapLayer.mSnapTo = QgsSnapper::SnapToVertex;
}
//default snapping tolerance
snapLayer.mTolerance = settings.value( "/qgis/digitizing/default_snapping_tolerance", 0 ).toDouble();
//default snapping tolerance (returned in map units)
snapLayer.mTolerance = QgsTolerance::defaultTolerance(mMapCanvas->mapUnitsPerPixel());
snapLayer.mUnitType = QgsTolerance::MapUnits;
snapLayers.append( snapLayer );
}

View File

@ -659,7 +659,7 @@
</property>
</spacer>
</item>
<item row="0" column="4" >
<item row="0" column="4" colspan="2" >
<widget class="QComboBox" name="mDefaultSnapModeComboBox" >
<property name="sizePolicy" >
<sizepolicy vsizetype="Fixed" hsizetype="Expanding" >
@ -729,6 +729,40 @@
</property>
</widget>
</item>
<item row="1" column="5" >
<widget class="QComboBox" name="mDefaultSnappingToleranceComboBox" >
<property name="sizePolicy" >
<sizepolicy vsizetype="Fixed" hsizetype="Expanding" >
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<item>
<property name="text" >
<string>map units</string>
</property>
</item>
<item>
<property name="text" >
<string>pixels</string>
</property>
</item>
</widget>
</item>
<item row="2" column="5" >
<widget class="QComboBox" name="mSearchRadiusVertexEditComboBox" >
<item>
<property name="text" >
<string>map units</string>
</property>
</item>
<item>
<property name="text" >
<string>pixels</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>

View File

@ -13,10 +13,22 @@
<string>Snapping options</string>
</property>
<layout class="QGridLayout" >
<property name="margin" >
<property name="leftMargin" >
<number>9</number>
</property>
<property name="spacing" >
<property name="topMargin" >
<number>9</number>
</property>
<property name="rightMargin" >
<number>9</number>
</property>
<property name="bottomMargin" >
<number>9</number>
</property>
<property name="horizontalSpacing" >
<number>6</number>
</property>
<property name="verticalSpacing" >
<number>6</number>
</property>
<item row="0" column="0" >
@ -36,6 +48,11 @@
<string>Tolerance</string>
</property>
</column>
<column>
<property name="text" >
<string>Units</string>
</property>
</column>
</widget>
</item>
<item row="1" column="0" >