Skip to content

Commit 4ca08a3

Browse files
committed
Make the georeferencer tool use a snapping cursor
The georeferencer plugin currently has a snap to background layer option that applies a snapping after one has picked a location on the main map. Other tools in the system show a crosshairs that will snap to project snapping options. This change makes the coordinate selection tool in the georeferencer plugin use a snapping crosshairs. Since this tool will use the snapping objects from the project, the "snap to background layers" checkbox has been removed.
1 parent b737850 commit 4ca08a3

File tree

3 files changed

+75
-26
lines changed

3 files changed

+75
-26
lines changed

src/plugins/georeferencer/qgsmapcoordsdialog.cpp

-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <QPushButton>
1717

1818
#include "qgsmapcanvas.h"
19-
#include "qgssnappingutils.h"
2019

2120
#include "qgsgeorefvalidators.h"
2221
#include "qgsmapcoordsdialog.h"
@@ -46,8 +45,6 @@ QgsMapCoordsDialog::QgsMapCoordsDialog( QgsMapCanvas* qgisCanvas, const QgsPoint
4645
mToolEmitPoint = new QgsGeorefMapToolEmitPoint( qgisCanvas );
4746
mToolEmitPoint->setButton( mPointFromCanvasPushButton );
4847

49-
mSnapToBackgroundLayerBox->setChecked( s.value( "/Plugin-GeoReferencer/snapToBackgroundLayers", QVariant( false ) ).toBool() );
50-
5148
connect( mPointFromCanvasPushButton, SIGNAL( clicked( bool ) ), this, SLOT( setToolEmitPoint( bool ) ) );
5249

5350
connect( mToolEmitPoint, SIGNAL( canvasClicked( const QgsPoint&, Qt::MouseButton ) ),
@@ -91,8 +88,6 @@ void QgsMapCoordsDialog::on_buttonBox_accepted()
9188
y = dmsToDD( leYCoord->text() );
9289

9390
emit pointAdded( mPixelCoords, QgsPoint( x, y ) );
94-
QSettings s;
95-
s.setValue( "/Plugin-GeoReferencer/snapToBackgroundLayers", mSnapToBackgroundLayerBox->isChecked() );
9691
close();
9792
}
9893

@@ -102,12 +97,6 @@ void QgsMapCoordsDialog::maybeSetXY( const QgsPoint & xy, Qt::MouseButton button
10297
if ( Qt::LeftButton == button )
10398
{
10499
QgsPoint mapCoordPoint = xy;
105-
if ( mQgisCanvas && mSnapToBackgroundLayerBox->isChecked() )
106-
{
107-
QgsPointLocator::Match m = mQgisCanvas->snappingUtils()->snapToMap( xy );
108-
if ( m.isValid() )
109-
mapCoordPoint = m.point();
110-
}
111100

112101
leXCoord->clear();
113102
leYCoord->clear();

src/plugins/georeferencer/qgsmapcoordsdialog.h

+72-4
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,100 @@
1313
#define MAPCOORDSDIALOG_H
1414

1515
#include <QDialog>
16+
#include <QMouseEvent>
1617

1718
#include "qgsmaptoolemitpoint.h"
19+
#include "qgssnappingutils.h"
1820
#include "qgspoint.h"
21+
#include "qgsvertexmarker.h"
22+
#include "qgsmapcanvas.h"
1923

2024
#include <ui_qgsmapcoordsdialogbase.h>
2125

2226
class QPushButton;
2327

24-
class QgsGeorefMapToolEmitPoint : public QgsMapToolEmitPoint
28+
class QgsGeorefMapToolEmitPoint : public QgsMapTool
2529
{
2630
Q_OBJECT
2731

2832
public:
2933
explicit QgsGeorefMapToolEmitPoint( QgsMapCanvas *canvas )
30-
: QgsMapToolEmitPoint( canvas )
34+
: QgsMapTool( canvas )
3135
{
3236
}
3337

34-
void canvasReleaseEvent( QgsMapMouseEvent* e ) override
38+
virtual ~QgsGeorefMapToolEmitPoint()
3539
{
36-
QgsMapToolEmitPoint::canvasReleaseEvent( e );
40+
delete mSnappingMarker;
41+
mSnappingMarker = nullptr;
42+
}
43+
44+
void canvasMoveEvent(QgsMapMouseEvent *e) override
45+
{
46+
MappedPoint mapped = mapPoint(e);
47+
48+
if ( !mapped.snapped )
49+
{
50+
delete mSnappingMarker;
51+
mSnappingMarker = nullptr;
52+
}
53+
else
54+
{
55+
if ( !mSnappingMarker )
56+
{
57+
mSnappingMarker = new QgsVertexMarker( mCanvas );
58+
mSnappingMarker->setIconType( QgsVertexMarker::ICON_CROSS );
59+
mSnappingMarker->setColor( Qt::magenta );
60+
mSnappingMarker->setPenWidth( 3 );
61+
}
62+
mSnappingMarker->setCenter( mapped.point );
63+
}
64+
}
65+
66+
void canvasPressEvent( QgsMapMouseEvent * e ) override
67+
{
68+
MappedPoint mapped = mapPoint(e);
69+
emit canvasClicked( mapped.point, e->button() );
70+
}
71+
72+
void canvasReleaseEvent( QgsMapMouseEvent *e ) override
73+
{
74+
QgsMapTool::canvasReleaseEvent( e );
3775
emit mouseReleased();
3876
}
3977

78+
void deactivate() override
79+
{
80+
delete mSnappingMarker;
81+
mSnappingMarker = 0;
82+
83+
QgsMapTool::deactivate();
84+
}
85+
4086
signals:
87+
void canvasClicked( const QgsPoint& point, Qt::MouseButton button );
4188
void mouseReleased();
89+
90+
private:
91+
struct MappedPoint
92+
{
93+
QgsPoint point;
94+
bool snapped = false;
95+
};
96+
97+
MappedPoint mapPoint(QMouseEvent *e)
98+
{
99+
QgsPoint pnt = toMapCoordinates( e->pos() );
100+
QgsSnappingUtils* snappingUtils = canvas()->snappingUtils();
101+
auto match = snappingUtils->snapToMap( pnt );
102+
103+
MappedPoint ret;
104+
ret.snapped = match.isValid();
105+
ret.point = ret.snapped ? match.point() : pnt;
106+
return ret;
107+
}
108+
109+
QgsVertexMarker* mSnappingMarker = nullptr;
42110
};
43111

44112
class QgsMapCoordsDialog : public QDialog, private Ui::QgsMapCoordsDialogBase

src/plugins/georeferencer/qgsmapcoordsdialogbase.ui

+3-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<property name="sizeGripEnabled">
1717
<bool>true</bool>
1818
</property>
19-
<layout class="QGridLayout" name="gridLayout" columnstretch="0,1,0,1">
19+
<layout class="QGridLayout" name="gridLayout" columnstretch="0,0,0,0">
2020
<item row="0" column="0" colspan="4">
2121
<widget class="QLabel" name="label">
2222
<property name="text">
@@ -47,21 +47,14 @@
4747
<item row="1" column="1">
4848
<widget class="QLineEdit" name="leXCoord"/>
4949
</item>
50-
<item row="3" column="0" colspan="3">
51-
<widget class="QCheckBox" name="mSnapToBackgroundLayerBox">
52-
<property name="text">
53-
<string>Snap to background layers</string>
54-
</property>
55-
</widget>
56-
</item>
57-
<item row="5" column="0" colspan="4">
50+
<item row="4" column="0" colspan="4">
5851
<widget class="QDialogButtonBox" name="buttonBox">
5952
<property name="standardButtons">
6053
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
6154
</property>
6255
</widget>
6356
</item>
64-
<item row="4" column="0">
57+
<item row="3" column="0">
6558
<spacer name="verticalSpacer">
6659
<property name="orientation">
6760
<enum>Qt::Vertical</enum>
@@ -80,7 +73,6 @@
8073
<tabstops>
8174
<tabstop>leXCoord</tabstop>
8275
<tabstop>leYCoord</tabstop>
83-
<tabstop>mSnapToBackgroundLayerBox</tabstop>
8476
</tabstops>
8577
<resources/>
8678
<connections>

0 commit comments

Comments
 (0)