Skip to content

Commit

Permalink
Hook snapping utils into QGIS interface
Browse files Browse the repository at this point in the history
Now the new snapping engine is ues for add feature map tool
  • Loading branch information
wonder-sk committed Jan 20, 2015
1 parent 1dae560 commit 83770df
Show file tree
Hide file tree
Showing 14 changed files with 278 additions and 55 deletions.
2 changes: 2 additions & 0 deletions python/core/core.sip
Expand Up @@ -78,6 +78,7 @@
%Include qgspluginlayer.sip
%Include qgspluginlayerregistry.sip
%Include qgspoint.sip
%Include qgspointlocator.sip
%Include qgsproject.sip
%Include qgsprojectproperty.sip
%Include qgsprojectversion.sip
Expand All @@ -96,6 +97,7 @@
%Include qgsscaleutils.sip
%Include qgssimplifymethod.sip
%Include qgssnapper.sip
%Include qgssnappingutils.sip
%Include qgsspatialindex.sip
%Include qgstolerance.sip
%Include qgsvectordataprovider.sip
Expand Down
7 changes: 7 additions & 0 deletions python/gui/qgisinterface.sip
Expand Up @@ -32,6 +32,13 @@ class QgisInterface : QObject

virtual QgsLayerTreeView* layerTreeView() = 0;

/**
* Get access to snapping utilities - for use in map tools
*
* @note added in 2.8
*/
virtual QgsSnappingUtils* snappingUtils() = 0;

public slots: // TODO: do these functions really need to be slots?

/* Exposed functions */
Expand Down
17 changes: 17 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -192,6 +192,7 @@
#include "qgsshortcutsmanager.h"
#include "qgssinglebandgrayrenderer.h"
#include "qgssnappingdialog.h"
#include "qgssnappingutils.h"
#include "qgssponsors.h"
#include "qgssvgannotationitem.h"
#include "qgstextannotationitem.h"
Expand Down Expand Up @@ -404,6 +405,8 @@ void QgisApp::activeLayerChanged( QgsMapLayer* layer )
{
if ( mMapCanvas )
mMapCanvas->setCurrentLayer( layer );
if ( mSnappingUtils )
mSnappingUtils->setCurrentLayer( qobject_cast<QgsVectorLayer*>( layer ) );
}

/**
Expand Down Expand Up @@ -488,6 +491,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
#endif
, mComposerManager( 0 )
, mpGpsWidget( 0 )
, mSnappingUtils( 0 )
{
if ( smInstance )
{
Expand Down Expand Up @@ -567,6 +571,13 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
mAdvancedDigitizingDockWidget = new QgsAdvancedDigitizingDockWidget( mMapCanvas, this );
mAdvancedDigitizingDockWidget->setObjectName( "AdvancedDigitizingTools" );

mSnappingUtils = new QgsSnappingUtils( this );
connect( QgsProject::instance(), SIGNAL( snapSettingsChanged() ), mSnappingUtils, SLOT( readConfigFromProject() ) );
connect( this, SIGNAL( newProject() ), mSnappingUtils, SLOT( readConfigFromProject() ) );
connect( this, SIGNAL( projectRead() ), mSnappingUtils, SLOT( readConfigFromProject() ) );
connect( mMapCanvas, SIGNAL( extentsChanged() ), this, SLOT( updateSnappingUtilsMapSettings() ) );
connect( mMapCanvas, SIGNAL( destinationCrsChanged() ), this, SLOT( updateSnappingUtilsMapSettings() ) );

createActions();
createActionGroups();
createMenus();
Expand Down Expand Up @@ -817,6 +828,7 @@ QgisApp::QgisApp()
, mPythonUtils( 0 )
, mComposerManager( 0 )
, mpGpsWidget( 0 )
, mSnappingUtils( 0 )
{
smInstance = this;
setupUi( this );
Expand Down Expand Up @@ -4357,6 +4369,11 @@ void QgisApp::updateFilterLegendByMap()
layerTreeView()->layerTreeModel()->setLegendFilterByMap( &mMapCanvas->mapSettings() );
}

void QgisApp::updateSnappingUtilsMapSettings()
{
mSnappingUtils->setMapSettings( mMapCanvas->mapSettings() );
}

void QgisApp::saveMapAsImage()
{
QPair< QString, QString> myFileNameAndFilter = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );
Expand Down
8 changes: 8 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -59,6 +59,7 @@ class QgsPoint;
class QgsProviderRegistry;
class QgsPythonUtils;
class QgsRectangle;
class QgsSnappingUtils;
class QgsUndoWidget;
class QgsVectorLayer;
class QgsVectorLayerTools;
Expand Down Expand Up @@ -1213,6 +1214,11 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void updateFilterLegendByMap();
void setFilterLegendByMapEnabled( bool enabled );

public:
QgsSnappingUtils* snappingUtils() { return mSnappingUtils; }
private slots:
void updateSnappingUtilsMapSettings();

/** Make the user feel dizzy */
void dizzy();

Expand Down Expand Up @@ -1605,6 +1611,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

QToolButton* mBtnFilterLegend;

QgsSnappingUtils* mSnappingUtils;

#ifdef HAVE_TOUCH
bool gestureEvent( QGestureEvent *event );
void tapAndHoldTriggered( QTapAndHoldGesture *gesture );
Expand Down
5 changes: 5 additions & 0 deletions src/app/qgisappinterface.cpp
Expand Up @@ -88,6 +88,11 @@ QgsLayerTreeView*QgisAppInterface::layerTreeView()
return qgis->layerTreeView();
}

QgsSnappingUtils* QgisAppInterface::snappingUtils()
{
return qgis->snappingUtils();
}

void QgisAppInterface::zoomFull()
{
qgis->zoomFull();
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisappinterface.h
Expand Up @@ -51,6 +51,8 @@ class APP_EXPORT QgisAppInterface : public QgisInterface

QgsLayerTreeView* layerTreeView() override;

QgsSnappingUtils* snappingUtils();

/* Exposed functions */

//! Zoom map to full extent
Expand Down
44 changes: 14 additions & 30 deletions src/app/qgsmapmouseevent.cpp
Expand Up @@ -18,13 +18,13 @@
#include "qgsmaptooladvanceddigitizing.h"
#include "qgsmapcanvas.h"

#include "qgisapp.h"
#include "qgssnappingutils.h"

QgsMapMouseEvent::QgsMapMouseEvent( QgsMapToolAdvancedDigitizing* mapTool, QMouseEvent* event , bool doSnap )
: QMouseEvent( event->type(), event->pos(), event->globalPos(), event->button(), event->buttons(), event->modifiers() )
, mMapPoint( mapTool->canvas()->mapSettings().mapToPixel().toMapCoordinates( event->pos() ) )
, mSnapped( false )
, mMapTool( mapTool )
, mSnapResults()
{
mOriginalPoint = mMapPoint;

Expand All @@ -41,9 +41,7 @@ QgsMapMouseEvent::QgsMapMouseEvent( QgsMapToolAdvancedDigitizing* mapTool, QgsPo
button, button, modifiers )
, mMapPoint( point )
, mOriginalPoint( point )
, mSnapped( false )
, mMapTool( mapTool )
, mSnapResults()
{
if ( doSnap )
snapPoint();
Expand All @@ -56,22 +54,9 @@ void QgsMapMouseEvent::setPoint( const QgsPoint& point )

bool QgsMapMouseEvent::snapPoint()
{
QgsMapCanvasSnapper snapper;
snapper.setMapCanvas( mMapTool->canvas() );
mSnapped = false;
if ( snapper.snapToBackgroundLayers( mMapPoint, mSnapResults ) == 0 )
{
mSnapped = !mSnapResults.isEmpty();
}
if ( mSnapped )
{
mMapPoint = mSnapResults.constBegin()->snappedVertex;
}
else
{
mMapPoint = mOriginalPoint;
}
return mSnapped;
mSnapMatch = QgisApp::instance()->snappingUtils()->snapToMap( mMapPoint );
mMapPoint = mSnapMatch.isValid() ? mSnapMatch.point() : mOriginalPoint;
return mSnapMatch.isValid();
}

QPoint QgsMapMouseEvent::mapToPixelCoordinates( QgsMapCanvas* canvas, const QgsPoint& point )
Expand All @@ -88,24 +73,23 @@ QgsPoint QgsMapMouseEvent::mapPoint( bool* snappedPoint ) const
{
if ( snappedPoint )
{
*snappedPoint = mSnapped && mSnapResults.constBegin()->snappedVertexNr != -1;
*snappedPoint = mSnapMatch.hasVertex();
}
return mMapPoint;
}

QList<QgsPoint> QgsMapMouseEvent::snappedSegment( bool* snapped ) const
{
QList<QgsPoint> segment = QList<QgsPoint>();
if ( mSnapped )
if ( mSnapMatch.hasEdge() )
{
QgsPoint pt1, pt2;
mSnapMatch.edgePoints( pt1, pt2 );
segment << pt1 << pt2;
}
else
{
foreach ( const QgsSnappingResult result, mSnapResults )
{
if ( result.beforeVertexNr != -1 && result.afterVertexNr != -1 )
{
segment << result.beforeVertex << result.afterVertex;
break;
}
}
// TODO: run snapToMap with only segments (resp. all hits)
}

if ( snapped )
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsmapmouseevent.h
Expand Up @@ -20,6 +20,7 @@

#include "qgsmapcanvassnapper.h"
#include "qgspoint.h"
#include "qgspointlocator.h"

class QgsMapToolAdvancedDigitizing;

Expand Down Expand Up @@ -61,10 +62,9 @@ class APP_EXPORT QgsMapMouseEvent : public QMouseEvent
QgsPoint mMapPoint;

QgsPoint mOriginalPoint;
bool mSnapped;

QgsMapToolAdvancedDigitizing* mMapTool;
QList<QgsSnappingResult> mSnapResults;
QgsPointLocator::Match mSnapMatch;
};

#endif // QGSMAPMOUSEEVENT_H

0 comments on commit 83770df

Please sign in to comment.