Skip to content

Commit cd7180e

Browse files
committedJan 20, 2015
Added convenience method snappingUtils() to QgsMapCanvas
Map tools can use canvas.snappingUtils().snapToMap(pt) to do the snapping.
1 parent cf22adc commit cd7180e

17 files changed

+131
-62
lines changed
 

‎python/gui/qgisinterface.sip

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ class QgisInterface : QObject
3232

3333
virtual QgsLayerTreeView* layerTreeView() = 0;
3434

35-
/**
36-
* Get access to snapping utilities - for use in map tools
37-
*
38-
* @note added in 2.8
39-
*/
40-
virtual QgsSnappingUtils* snappingUtils() = 0;
41-
4235
public slots: // TODO: do these functions really need to be slots?
4336

4437
/* Exposed functions */

‎python/gui/qgsmapcanvas.sip

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,23 @@ class QgsMapCanvas : QGraphicsView
309309
* @note added in 2.3 */
310310
QgsPreviewEffect::PreviewMode previewMode() const;
311311

312+
/** Return snapping utility class that is associated with map canvas.
313+
* If no snapping utils instance has been associated previously, an internal will be created for convenience
314+
* (so map tools do not need to test for existence of the instance).
315+
*
316+
* Main canvas in QGIS returns an instance which is always up-to-date with the project's snapping configuration.
317+
* @note added in 2.8
318+
*/
319+
QgsSnappingUtils* snappingUtils() const;
320+
/** Assign an instance of snapping utils to the map canvas.
321+
* The instance is not owned by the canvas, so it is possible to use one instance in multiple canvases.
322+
*
323+
* For main canvas in QGIS, do not associate a different instance from the existing one (it is updated from
324+
* the project's snapping configuration).
325+
* @note added in 2.8
326+
*/
327+
void setSnappingUtils( QgsSnappingUtils* utils );
328+
312329
public slots:
313330

314331
/**Repaints the canvas map*/
@@ -437,6 +454,10 @@ class QgsMapCanvas : QGraphicsView
437454
//! @note added in 2.4
438455
void mapUnitsChanged();
439456

457+
//! Emitted when the current layer is changed
458+
//! @note added in 2.8
459+
void currentLayerChanged( QgsMapLayer* layer );
460+
440461
protected:
441462
//! Overridden standard event to be gestures aware
442463
bool event( QEvent * e );

‎src/app/qgisapp.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
#include "qgslayertreeviewdefaultactions.h"
149149
#include "qgslogger.h"
150150
#include "qgsmapcanvas.h"
151+
#include "qgsmapcanvassnappingutils.h"
151152
#include "qgsmaplayer.h"
152153
#include "qgsmaplayerregistry.h"
153154
#include "qgsmaplayerstyleguiutils.h"
@@ -192,7 +193,6 @@
192193
#include "qgsshortcutsmanager.h"
193194
#include "qgssinglebandgrayrenderer.h"
194195
#include "qgssnappingdialog.h"
195-
#include "qgssnappingutils.h"
196196
#include "qgssponsors.h"
197197
#include "qgssvgannotationitem.h"
198198
#include "qgstextannotationitem.h"
@@ -405,8 +405,6 @@ void QgisApp::activeLayerChanged( QgsMapLayer* layer )
405405
{
406406
if ( mMapCanvas )
407407
mMapCanvas->setCurrentLayer( layer );
408-
if ( mSnappingUtils )
409-
mSnappingUtils->setCurrentLayer( qobject_cast<QgsVectorLayer*>( layer ) );
410408
}
411409

412410
/**
@@ -571,12 +569,11 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
571569
mAdvancedDigitizingDockWidget = new QgsAdvancedDigitizingDockWidget( mMapCanvas, this );
572570
mAdvancedDigitizingDockWidget->setObjectName( "AdvancedDigitizingTools" );
573571

574-
mSnappingUtils = new QgsSnappingUtils( this );
572+
mSnappingUtils = new QgsMapCanvasSnappingUtils( mMapCanvas, this );
573+
mMapCanvas->setSnappingUtils( mSnappingUtils );
575574
connect( QgsProject::instance(), SIGNAL( snapSettingsChanged() ), mSnappingUtils, SLOT( readConfigFromProject() ) );
576575
connect( this, SIGNAL( newProject() ), mSnappingUtils, SLOT( readConfigFromProject() ) );
577576
connect( this, SIGNAL( projectRead() ), mSnappingUtils, SLOT( readConfigFromProject() ) );
578-
connect( mMapCanvas, SIGNAL( extentsChanged() ), this, SLOT( updateSnappingUtilsMapSettings() ) );
579-
connect( mMapCanvas, SIGNAL( destinationCrsChanged() ), this, SLOT( updateSnappingUtilsMapSettings() ) );
580577

581578
createActions();
582579
createActionGroups();
@@ -4369,11 +4366,6 @@ void QgisApp::updateFilterLegendByMap()
43694366
layerTreeView()->layerTreeModel()->setLegendFilterByMap( &mMapCanvas->mapSettings() );
43704367
}
43714368

4372-
void QgisApp::updateSnappingUtilsMapSettings()
4373-
{
4374-
mSnappingUtils->setMapSettings( mMapCanvas->mapSettings() );
4375-
}
4376-
43774369
void QgisApp::saveMapAsImage()
43784370
{
43794371
QPair< QString, QString> myFileNameAndFilter = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );

‎src/app/qgisapp.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,11 +1214,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
12141214
void updateFilterLegendByMap();
12151215
void setFilterLegendByMapEnabled( bool enabled );
12161216

1217-
public:
1218-
QgsSnappingUtils* snappingUtils() { return mSnappingUtils; }
1219-
private slots:
1220-
void updateSnappingUtilsMapSettings();
1221-
12221217
/** Make the user feel dizzy */
12231218
void dizzy();
12241219

‎src/app/qgisappinterface.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ QgsLayerTreeView*QgisAppInterface::layerTreeView()
8888
return qgis->layerTreeView();
8989
}
9090

91-
QgsSnappingUtils* QgisAppInterface::snappingUtils()
92-
{
93-
return qgis->snappingUtils();
94-
}
95-
9691
void QgisAppInterface::zoomFull()
9792
{
9893
qgis->zoomFull();

‎src/app/qgisappinterface.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ class APP_EXPORT QgisAppInterface : public QgisInterface
5151

5252
QgsLayerTreeView* layerTreeView() override;
5353

54-
QgsSnappingUtils* snappingUtils();
55-
5654
/* Exposed functions */
5755

5856
//! Zoom map to full extent

‎src/app/qgsmapmouseevent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void QgsMapMouseEvent::setPoint( const QgsPoint& point )
5454

5555
bool QgsMapMouseEvent::snapPoint()
5656
{
57-
mSnapMatch = QgisApp::instance()->snappingUtils()->snapToMap( mMapPoint );
57+
mSnapMatch = mMapTool->canvas()->snappingUtils()->snapToMap( mMapPoint );
5858
mMapPoint = mSnapMatch.isValid() ? mSnapMatch.point() : mOriginalPoint;
5959
return mSnapMatch.isValid();
6060
}

‎src/app/qgsmaptoolmeasureangle.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgsmaptopixel.h"
2222
#include "qgsproject.h"
2323
#include "qgsrubberband.h"
24+
#include "qgssnappingutils.h"
2425
#include <QMouseEvent>
2526
#include <QSettings>
2627
#include <cmath>
@@ -31,7 +32,6 @@ QgsMapToolMeasureAngle::QgsMapToolMeasureAngle( QgsMapCanvas* canvas )
3132
, mResultDisplay( 0 )
3233
{
3334
mToolName = tr( "Measure angle" );
34-
mSnapper.setMapCanvas( canvas );
3535

3636
connect( canvas, SIGNAL( destinationCrsChanged() ),
3737
this, SLOT( updateSettings() ) );
@@ -143,15 +143,8 @@ void QgsMapToolMeasureAngle::createRubberBand()
143143

144144
QgsPoint QgsMapToolMeasureAngle::snapPoint( const QPoint& p )
145145
{
146-
QList<QgsSnappingResult> snappingResults;
147-
if ( mSnapper.snapToBackgroundLayers( p, snappingResults ) != 0 || snappingResults.size() < 1 )
148-
{
149-
return mCanvas->getCoordinateTransform()->toMapCoordinates( p );
150-
}
151-
else
152-
{
153-
return snappingResults.constBegin()->snappedVertex;
154-
}
146+
QgsPointLocator::Match m = mCanvas->snappingUtils()->snapToMap( p );
147+
return m.isValid() ? m.point() : mCanvas->getCoordinateTransform()->toMapCoordinates( p );
155148
}
156149

157150
void QgsMapToolMeasureAngle::updateSettings()

‎src/app/qgsmaptoolmeasureangle.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#define QGSMAPTOOLMEASUREANGLE_H
1818

1919
#include "qgsmaptool.h"
20-
#include "qgsmapcanvassnapper.h"
2120
#include "qgspoint.h"
2221
#include "qgsdistancearea.h"
2322

@@ -49,7 +48,6 @@ class APP_EXPORT QgsMapToolMeasureAngle: public QgsMapTool
4948
QList<QgsPoint> mAnglePoints;
5049
QgsRubberBand* mRubberBand;
5150
QgsDisplayAngle* mResultDisplay;
52-
QgsMapCanvasSnapper mSnapper;
5351

5452
/**Creates a new rubber band and deletes the old one*/
5553
void createRubberBand();

‎src/app/qgsmeasuretool.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgsmaptopixel.h"
2121
#include "qgsrubberband.h"
2222
#include "qgsvectorlayer.h"
23+
#include "qgssnappingutils.h"
2324
#include "qgstolerance.h"
2425

2526
#include "qgsmeasuredialog.h"
@@ -47,7 +48,6 @@ QgsMeasureTool::QgsMeasureTool( QgsMapCanvas* canvas, bool measureArea )
4748

4849
mDialog = new QgsMeasureDialog( this, Qt::WindowStaysOnTopHint );
4950
mDialog->restorePosition();
50-
mSnapper.setMapCanvas( canvas );
5151

5252
connect( canvas, SIGNAL( destinationCrsChanged() ),
5353
this, SLOT( updateSettings() ) );
@@ -238,15 +238,9 @@ void QgsMeasureTool::addPoint( QgsPoint &point )
238238
}
239239
}
240240

241+
241242
QgsPoint QgsMeasureTool::snapPoint( const QPoint& p )
242243
{
243-
QList<QgsSnappingResult> snappingResults;
244-
if ( mSnapper.snapToBackgroundLayers( p, snappingResults ) != 0 || snappingResults.size() < 1 )
245-
{
246-
return mCanvas->getCoordinateTransform()->toMapCoordinates( p );
247-
}
248-
else
249-
{
250-
return snappingResults.constBegin()->snappedVertex;
251-
}
244+
QgsPointLocator::Match m = mCanvas->snappingUtils()->snapToMap( p );
245+
return m.isValid() ? m.point() : mCanvas->getCoordinateTransform()->toMapCoordinates( p );
252246
}

‎src/app/qgsmeasuretool.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ class APP_EXPORT QgsMeasureTool : public QgsMapTool
9797
// project projection
9898
bool mWrongProjectProjection;
9999

100-
QgsMapCanvasSnapper mSnapper;
101-
102100
//! Returns the snapped (map) coordinate
103101
//@param p (pixel) coordinate
104102
QgsPoint snapPoint( const QPoint& p );

‎src/gui/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ qgsmapcanvas.cpp
177177
qgsmapcanvasitem.cpp
178178
qgsmapcanvasmap.cpp
179179
qgsmapcanvassnapper.cpp
180+
qgsmapcanvassnappingutils.cpp
180181
qgsmaplayeractionregistry.cpp
181182
qgsmaplayercombobox.cpp
182183
qgsmaplayermodel.cpp
@@ -284,6 +285,7 @@ SET(QGIS_GUI_MOC_HDRS
284285
qgsludialog.h
285286
qgsmanageconnectionsdialog.h
286287
qgsmapcanvas.h
288+
qgsmapcanvassnappingutils.h
287289
qgsmaplayeractionregistry.h
288290
qgsmaplayercombobox.h
289291
qgsmaplayermodel.h
@@ -442,6 +444,7 @@ SET(QGIS_GUI_HDRS
442444
qgsmapcanvasitem.h
443445
qgsmapcanvasmap.h
444446
qgsmapcanvassnapper.h
447+
qgsmapcanvassnappingutils.h
445448
qgsmaptip.h
446449
qgsmaptoolpan.h
447450
qgsmaptoolzoom.h

‎src/gui/qgisinterface.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,6 @@ class GUI_EXPORT QgisInterface : public QObject
7878

7979
virtual QgsLayerTreeView* layerTreeView() = 0;
8080

81-
/**
82-
* Get access to snapping utilities - for use in map tools
83-
*
84-
* @note added in 2.8
85-
*/
86-
virtual QgsSnappingUtils* snappingUtils() = 0;
87-
8881
public slots: // TODO: do these functions really need to be slots?
8982

9083
/* Exposed functions */

‎src/gui/qgsmapcanvas.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ email : sherman at mrcc.com
4444
#include "qgslogger.h"
4545
#include "qgsmapcanvas.h"
4646
#include "qgsmapcanvasmap.h"
47+
#include "qgsmapcanvassnappingutils.h"
4748
#include "qgsmaplayer.h"
4849
#include "qgsmaplayerregistry.h"
4950
#include "qgsmaptoolpan.h"
@@ -190,6 +191,7 @@ QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
190191
, mDrawRenderingStats( false )
191192
, mCache( 0 )
192193
, mPreviewEffect( 0 )
194+
, mSnappingUtils( 0 )
193195
{
194196
setObjectName( name );
195197
mScene = new QGraphicsScene();
@@ -340,6 +342,7 @@ QgsMapLayer* QgsMapCanvas::layer( int index )
340342
void QgsMapCanvas::setCurrentLayer( QgsMapLayer* layer )
341343
{
342344
mCurrentLayer = layer;
345+
emit currentLayerChanged( layer );
343346
}
344347

345348
double QgsMapCanvas::scale()
@@ -1743,6 +1746,22 @@ QgsPreviewEffect::PreviewMode QgsMapCanvas::previewMode() const
17431746
return mPreviewEffect->mode();
17441747
}
17451748

1749+
QgsSnappingUtils* QgsMapCanvas::snappingUtils() const
1750+
{
1751+
if ( !mSnappingUtils )
1752+
{
1753+
// associate a dummy instance, but better than null pointer
1754+
QgsMapCanvas* c = const_cast<QgsMapCanvas*>( this );
1755+
c->mSnappingUtils = new QgsMapCanvasSnappingUtils( c, c );
1756+
}
1757+
return mSnappingUtils;
1758+
}
1759+
1760+
void QgsMapCanvas::setSnappingUtils( QgsSnappingUtils* utils )
1761+
{
1762+
mSnappingUtils = utils;
1763+
}
1764+
17461765
void QgsMapCanvas::readProject( const QDomDocument & doc )
17471766
{
17481767
QDomNodeList nodes = doc.elementsByTagName( "mapcanvas" );

‎src/gui/qgsmapcanvas.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class QgsMapSettings;
6565
class QgsMapCanvasMap;
6666
class QgsMapOverviewCanvas;
6767
class QgsMapTool;
68+
class QgsSnappingUtils;
6869

6970
/** \ingroup gui
7071
* A class that stores visibility and presence in overview flags together
@@ -378,6 +379,23 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
378379
* @note added in 2.3 */
379380
QgsPreviewEffect::PreviewMode previewMode() const;
380381

382+
/** Return snapping utility class that is associated with map canvas.
383+
* If no snapping utils instance has been associated previously, an internal will be created for convenience
384+
* (so map tools do not need to test for existence of the instance).
385+
*
386+
* Main canvas in QGIS returns an instance which is always up-to-date with the project's snapping configuration.
387+
* @note added in 2.8
388+
*/
389+
QgsSnappingUtils* snappingUtils() const;
390+
/** Assign an instance of snapping utils to the map canvas.
391+
* The instance is not owned by the canvas, so it is possible to use one instance in multiple canvases.
392+
*
393+
* For main canvas in QGIS, do not associate a different instance from the existing one (it is updated from
394+
* the project's snapping configuration).
395+
* @note added in 2.8
396+
*/
397+
void setSnappingUtils( QgsSnappingUtils* utils );
398+
381399
public slots:
382400

383401
/**Repaints the canvas map*/
@@ -517,6 +535,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
517535
//! @note added in 2.4
518536
void mapUnitsChanged();
519537

538+
//! Emitted when the current layer is changed
539+
//! @note added in 2.8
540+
void currentLayerChanged( QgsMapLayer* layer );
541+
520542
protected:
521543
#ifdef HAVE_TOUCH
522544
//! Overridden standard event to be gestures aware
@@ -652,6 +674,9 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
652674
QgsPreviewEffect* mPreviewEffect;
653675

654676
QgsRectangle imageRect( const QImage& img );
677+
678+
QgsSnappingUtils* mSnappingUtils;
679+
655680
}; // class QgsMapCanvas
656681

657682

0 commit comments

Comments
 (0)