Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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/qgis@10478 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Apr 5, 2009
1 parent d7a5370 commit c87f2c8
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 12 deletions.
1 change: 1 addition & 0 deletions python/core/core.sip
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions python/core/qgssnapper.sip
Expand Up @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions python/core/qgstolerance.sip
@@ -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);

};

3 changes: 2 additions & 1 deletion src/app/qgsmaptoolmovefeature.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgsrubberband.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgstolerance.h"
#include <QMessageBox>
#include <QMouseEvent>
#include <QSettings>
Expand Down Expand Up @@ -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 );

Expand Down
25 changes: 25 additions & 0 deletions src/app/qgsoptions.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgisapp.h"
#include "qgsgenericprojectionselector.h"
#include "qgscoordinatereferencesystem.h"
#include "qgstolerance.h"

#include <QFileDialog>
#include <QSettings>
Expand Down Expand Up @@ -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" ) );
Expand Down Expand Up @@ -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" ) )
Expand Down
6 changes: 6 additions & 0 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
}
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 );
}

Expand Down
31 changes: 29 additions & 2 deletions src/app/qgssnappingdialog.cpp
Expand Up @@ -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() )
{
Expand All @@ -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 );
Expand All @@ -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 );
}
}

Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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 );
}
}
1 change: 1 addition & 0 deletions src/app/qgssnappingdialog.h
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -43,6 +43,7 @@ SET(QGIS_CORE_SRCS
qgssearchtreenode.cpp
qgssnapper.cpp
qgscoordinatereferencesystem.cpp
qgstolerance.cpp
qgsvectordataprovider.cpp
qgsvectorfilewriter.cpp
qgsvectorlayer.cpp
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgssnapper.cpp
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgssnapper.h
Expand Up @@ -19,6 +19,7 @@
#define QGSSNAPPER_H

#include "qgspoint.h"
#include "qgstolerance.h"
#include <QList>
#include <QMultiMap>

Expand Down Expand Up @@ -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 );
Expand Down
43 changes: 43 additions & 0 deletions src/core/qgstolerance.cpp
@@ -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 changes: 63 additions & 0 deletions src/core/qgstolerance.h
@@ -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

0 comments on commit c87f2c8

Please sign in to comment.