1717#include < QPushButton>
1818
1919#include " qgsmapcanvas.h"
20+ #include " qgsmapcanvassnapper.h"
2021
2122#include " qgsgeorefvalidators.h"
2223#include " qgsmapcoordsdialog.h"
@@ -41,6 +42,9 @@ QgsMapCoordsDialog::QgsMapCoordsDialog( QgsMapCanvas* qgisCanvas, QgsPoint pixel
4142 mToolEmitPoint = new QgsGeorefMapToolEmitPoint ( qgisCanvas );
4243 mToolEmitPoint ->setButton ( mPointFromCanvasPushButton );
4344
45+ QSettings s;
46+ mSnapToBackgroundLayerBox ->setChecked ( s.value ( " /Plugin-GeoReferencer/snapToBackgroundLayers" , QVariant (false ) ).toBool () );
47+
4448 connect ( mPointFromCanvasPushButton , SIGNAL ( clicked ( bool ) ), this , SLOT ( setToolEmitPoint ( bool ) ) );
4549
4650 connect ( mToolEmitPoint , SIGNAL ( canvasClicked ( const QgsPoint&, Qt::MouseButton ) ),
@@ -81,7 +85,8 @@ void QgsMapCoordsDialog::on_buttonBox_accepted()
8185 y = dmsToDD ( leYCoord->text () );
8286
8387 emit pointAdded ( mPixelCoords , QgsPoint ( x, y ) );
84-
88+ QSettings s;
89+ s.setValue ( " /Plugin-GeoReferencer/snapToBackgroundLayers" , mSnapToBackgroundLayerBox ->isChecked () );
8590 close ();
8691}
8792
@@ -90,10 +95,30 @@ void QgsMapCoordsDialog::maybeSetXY( const QgsPoint & xy, Qt::MouseButton button
9095 // Only LeftButton should set point
9196 if ( Qt::LeftButton == button )
9297 {
98+ QgsPoint mapCoordPoint = xy;
99+ if ( mQgisCanvas && mSnapToBackgroundLayerBox ->isChecked () )
100+ {
101+ const QgsMapToPixel* mtp = mQgisCanvas ->getCoordinateTransform ();
102+ if ( mtp )
103+ {
104+ QgsPoint canvasPos = mtp->transform ( xy.x (), xy.y () );
105+ QPoint snapStartPoint ( canvasPos.x (), canvasPos.y () );
106+ QgsMapCanvasSnapper snapper ( mQgisCanvas );
107+ QList<QgsSnappingResult> snapResults;
108+ if ( snapper.snapToBackgroundLayers ( snapStartPoint, snapResults ) == 0 )
109+ {
110+ if ( snapResults.size () > 0 )
111+ {
112+ mapCoordPoint = snapResults.at ( 0 ).snappedVertex ;
113+ }
114+ }
115+ }
116+ }
117+
93118 leXCoord->clear ();
94119 leYCoord->clear ();
95- leXCoord->setText ( QString::number ( xy .x (), ' f' , 7 ) );
96- leYCoord->setText ( QString::number ( xy .y (), ' f' , 7 ) );
120+ leXCoord->setText ( QString::number ( mapCoordPoint .x (), ' f' , 7 ) );
121+ leYCoord->setText ( QString::number ( mapCoordPoint .y (), ' f' , 7 ) );
97122 }
98123
99124 parentWidget ()->showNormal ();
0 commit comments