diff --git a/src/plugins/georeferencer/qgsmapcoordsdialog.cpp b/src/plugins/georeferencer/qgsmapcoordsdialog.cpp index 67d3dd11dd66..e6d0200ba789 100644 --- a/src/plugins/georeferencer/qgsmapcoordsdialog.cpp +++ b/src/plugins/georeferencer/qgsmapcoordsdialog.cpp @@ -17,6 +17,7 @@ #include #include "qgsmapcanvas.h" +#include "qgsmapcanvassnapper.h" #include "qgsgeorefvalidators.h" #include "qgsmapcoordsdialog.h" @@ -41,6 +42,9 @@ QgsMapCoordsDialog::QgsMapCoordsDialog( QgsMapCanvas* qgisCanvas, QgsPoint pixel mToolEmitPoint = new QgsGeorefMapToolEmitPoint( qgisCanvas ); mToolEmitPoint->setButton( mPointFromCanvasPushButton ); + QSettings s; + mSnapToBackgroundLayerBox->setChecked( s.value( "/Plugin-GeoReferencer/snapToBackgroundLayers", QVariant(false) ).toBool() ); + connect( mPointFromCanvasPushButton, SIGNAL( clicked( bool ) ), this, SLOT( setToolEmitPoint( bool ) ) ); connect( mToolEmitPoint, SIGNAL( canvasClicked( const QgsPoint&, Qt::MouseButton ) ), @@ -81,7 +85,8 @@ void QgsMapCoordsDialog::on_buttonBox_accepted() y = dmsToDD( leYCoord->text() ); emit pointAdded( mPixelCoords, QgsPoint( x, y ) ); - + QSettings s; + s.setValue( "/Plugin-GeoReferencer/snapToBackgroundLayers", mSnapToBackgroundLayerBox->isChecked() ); close(); } @@ -90,10 +95,30 @@ void QgsMapCoordsDialog::maybeSetXY( const QgsPoint & xy, Qt::MouseButton button // Only LeftButton should set point if ( Qt::LeftButton == button ) { + QgsPoint mapCoordPoint = xy; + if ( mQgisCanvas && mSnapToBackgroundLayerBox->isChecked() ) + { + const QgsMapToPixel* mtp = mQgisCanvas->getCoordinateTransform(); + if ( mtp ) + { + QgsPoint canvasPos = mtp->transform( xy.x(), xy.y() ); + QPoint snapStartPoint( canvasPos.x(), canvasPos.y() ); + QgsMapCanvasSnapper snapper( mQgisCanvas ); + QList snapResults; + if ( snapper.snapToBackgroundLayers( snapStartPoint, snapResults ) == 0 ) + { + if ( snapResults.size() > 0 ) + { + mapCoordPoint = snapResults.at( 0 ).snappedVertex; + } + } + } + } + leXCoord->clear(); leYCoord->clear(); - leXCoord->setText( QString::number( xy.x(), 'f', 7 ) ); - leYCoord->setText( QString::number( xy.y(), 'f', 7 ) ); + leXCoord->setText( QString::number( mapCoordPoint.x(), 'f', 7 ) ); + leYCoord->setText( QString::number( mapCoordPoint.y(), 'f', 7 ) ); } parentWidget()->showNormal(); diff --git a/src/plugins/georeferencer/qgsmapcoordsdialogbase.ui b/src/plugins/georeferencer/qgsmapcoordsdialogbase.ui index 1a9c25a44d31..e1d4d8c33183 100644 --- a/src/plugins/georeferencer/qgsmapcoordsdialogbase.ui +++ b/src/plugins/georeferencer/qgsmapcoordsdialogbase.ui @@ -55,7 +55,14 @@ - + + + + Snap to background layers + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok