Skip to content

Commit

Permalink
Move handling of snap indicators from map tools to a new class
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Oct 24, 2017
1 parent a48c2e8 commit 1edc1a7
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 80 deletions.
1 change: 1 addition & 0 deletions python/gui/gui_auto.sip
Expand Up @@ -188,6 +188,7 @@
%Include qgssearchquerybuilder.sip
%Include qgsshortcutsmanager.sip
%Include qgsslider.sip
%Include qgssnapindicator.sip
%Include qgsstatusbar.sip
%Include qgssublayersdialog.sip
%Include qgssubstitutionlistwidget.sip
Expand Down
59 changes: 59 additions & 0 deletions python/gui/qgssnapindicator.sip
@@ -0,0 +1,59 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgssnapindicator.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/






class QgsSnapIndicator
{
%Docstring
Class that shows snapping marker on map canvas for the current snapping match.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgssnapindicator.h"
%End
public:
QgsSnapIndicator( QgsMapCanvas *canvas );
%Docstring
Constructs an indicator for the given map canvas
%End
~QgsSnapIndicator();

void setMatch( const QgsPointLocator::Match &match );
%Docstring
Sets snapping match that should be displayed in map canvas. Invalid match hides the indicator
%End
QgsPointLocator::Match match() const;
%Docstring
Returns currently displayed snapping match
:rtype: QgsPointLocator.Match
%End

void setVisible( bool visible = true );
%Docstring
Sets whether the snapping indicator is visible
%End
bool isVisible() const;
%Docstring
Returns whether the snapping indicator is visible
:rtype: bool
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgssnapindicator.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
25 changes: 8 additions & 17 deletions src/app/nodetool/qgsnodetool.cpp
Expand Up @@ -28,6 +28,7 @@
#include "qgsproject.h"
#include "qgsrubberband.h"
#include "qgssettings.h"
#include "qgssnapindicator.h"
#include "qgssnappingutils.h"
#include "qgsvectorlayer.h"
#include "qgsvertexmarker.h"
Expand Down Expand Up @@ -204,11 +205,7 @@ QgsNodeTool::QgsNodeTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget
{
setAdvancedDigitizingAllowed( false );

mSnapMarker = new QgsVertexMarker( canvas );
mSnapMarker->setIconType( QgsVertexMarker::ICON_CROSS );
mSnapMarker->setColor( Qt::magenta );
mSnapMarker->setPenWidth( 3 );
mSnapMarker->setVisible( false );
mSnapIndicator.reset( new QgsSnapIndicator( canvas ) );

mEdgeCenterMarker = new QgsVertexMarker( canvas );
mEdgeCenterMarker->setIconType( QgsVertexMarker::ICON_CROSS );
Expand Down Expand Up @@ -248,7 +245,6 @@ QgsNodeTool::QgsNodeTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget

QgsNodeTool::~QgsNodeTool()
{
delete mSnapMarker;
delete mEdgeCenterMarker;
delete mFeatureBand;
delete mFeatureBandMarkers;
Expand All @@ -263,6 +259,8 @@ void QgsNodeTool::deactivate()
removeTemporaryRubberBands();
cleanupNodeEditor();

mSnapIndicator->setMatch( QgsPointLocator::Match() );

QHash< QPair<QgsVectorLayer *, QgsFeatureId>, GeometryValidation>::iterator it = mValidations.begin();
for ( ; it != mValidations.end(); ++it )
it->cleanup();
Expand Down Expand Up @@ -508,16 +506,7 @@ void QgsNodeTool::cadCanvasMoveEvent( QgsMapMouseEvent *e )

void QgsNodeTool::mouseMoveDraggingVertex( QgsMapMouseEvent *e )
{
if ( e->mapPointMatch().isValid() )
{
mSnapMarker->setCenter( e->mapPoint() );
mSnapMarker->setVisible( true );
}
else
{
mSnapMarker->setVisible( false );
}

mSnapIndicator->setMatch( e->mapPointMatch() );
mEdgeCenterMarker->setVisible( false );

moveDragBands( e->mapPoint() );
Expand Down Expand Up @@ -554,7 +543,7 @@ void QgsNodeTool::moveDragBands( const QgsPointXY &mapPoint )

void QgsNodeTool::mouseMoveDraggingEdge( QgsMapMouseEvent *e )
{
mSnapMarker->setVisible( false );
mSnapIndicator->setMatch( QgsPointLocator::Match() );
mEdgeCenterMarker->setVisible( false );

QgsPointXY mapPoint = toMapCoordinates( e->pos() ); // do not use e.mapPoint() as it may be snapped
Expand Down Expand Up @@ -1349,6 +1338,8 @@ void QgsNodeTool::stopDragging()
clearDragBands();

setHighlightedNodesVisible( true ); // highlight can be shown again

mSnapIndicator->setMatch( QgsPointLocator::Match() );
}

QgsPointXY QgsNodeTool::matchToLayerPoint( const QgsVectorLayer *destLayer, const QgsPointXY &mapPoint, const QgsPointLocator::Match *match )
Expand Down
3 changes: 2 additions & 1 deletion src/app/nodetool/qgsnodetool.h
Expand Up @@ -27,6 +27,7 @@ class QRubberBand;
class QgsGeometryValidator;
class QgsNodeEditor;
class QgsSelectedFeature;
class QgsSnapIndicator;
class QgsVertexMarker;

//! helper structure for a vertex being dragged
Expand Down Expand Up @@ -202,7 +203,7 @@ class APP_EXPORT QgsNodeTool : public QgsMapToolAdvancedDigitizing
// members used for temporary highlight of stuff

//! marker of a snap match (if any) when dragging a vertex
QgsVertexMarker *mSnapMarker = nullptr;
std::unique_ptr<QgsSnapIndicator> mSnapIndicator;

/**
* marker in the middle of an edge while pointer is close to a vertex and not dragging anything
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -333,6 +333,7 @@ SET(QGIS_GUI_SRCS
qgssearchquerybuilder.cpp
qgsshortcutsmanager.cpp
qgsslider.cpp
qgssnapindicator.cpp
qgssublayersdialog.cpp
qgssubstitutionlistwidget.cpp
qgssqlcomposerdialog.cpp
Expand Down Expand Up @@ -710,6 +711,7 @@ SET(QGIS_GUI_HDRS
qgsmapmouseevent.h
qgsmaptip.h
qgsrubberband.h
qgssnapindicator.h
qgssqlcomposerdialog.h
qgstablewidgetitem.h
qgsuserinputdockwidget.h
Expand Down
26 changes: 5 additions & 21 deletions src/gui/qgsmaptoolcapture.cpp
Expand Up @@ -27,6 +27,7 @@
#include "qgsmapmouseevent.h"
#include "qgspolygon.h"
#include "qgsrubberband.h"
#include "qgssnapindicator.h"
#include "qgsvectorlayer.h"
#include "qgsvertexmarker.h"
#include "qgssettings.h"
Expand All @@ -48,6 +49,8 @@ QgsMapToolCapture::QgsMapToolCapture( QgsMapCanvas *canvas, QgsAdvancedDigitizin
mCaptureModeFromLayer = mode == CaptureNone;
mCapturing = false;

mSnapIndicator.reset( new QgsSnapIndicator( canvas ) );

QPixmap mySelectQPixmap = QPixmap( ( const char ** ) capture_point_cursor );
setCursor( QCursor( mySelectQPixmap, 8, 8 ) );

Expand All @@ -57,8 +60,6 @@ QgsMapToolCapture::QgsMapToolCapture( QgsMapCanvas *canvas, QgsAdvancedDigitizin

QgsMapToolCapture::~QgsMapToolCapture()
{
delete mSnappingMarker;

stopCapturing();

if ( mValidator )
Expand All @@ -81,8 +82,7 @@ void QgsMapToolCapture::deactivate()
if ( mTempRubberBand )
mTempRubberBand->hide();

delete mSnappingMarker;
mSnappingMarker = nullptr;
mSnapIndicator->setMatch( QgsPointLocator::Match() );

QgsMapToolAdvancedDigitizing::deactivate();
}
Expand Down Expand Up @@ -309,25 +309,9 @@ bool QgsMapToolCapture::tracingAddVertex( const QgsPointXY &point )
void QgsMapToolCapture::cadCanvasMoveEvent( QgsMapMouseEvent *e )
{
QgsMapToolAdvancedDigitizing::cadCanvasMoveEvent( e );
bool snapped = e->isSnapped();
QgsPointXY point = e->mapPoint();

if ( !snapped )
{
delete mSnappingMarker;
mSnappingMarker = nullptr;
}
else
{
if ( !mSnappingMarker )
{
mSnappingMarker = new QgsVertexMarker( mCanvas );
mSnappingMarker->setIconType( QgsVertexMarker::ICON_CROSS );
mSnappingMarker->setColor( Qt::magenta );
mSnappingMarker->setPenWidth( 3 );
}
mSnappingMarker->setCenter( point );
}
mSnapIndicator->setMatch( e->mapPointMatch() );

if ( !mTempRubberBand && mCaptureCurve.numPoints() > 0 )
{
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsmaptoolcapture.h
Expand Up @@ -26,6 +26,7 @@
#include "qgis_gui.h"

class QgsRubberBand;
class QgsSnapIndicator;
class QgsVertexMarker;
class QgsMapLayer;
class QgsGeometryValidator;
Expand Down Expand Up @@ -248,7 +249,7 @@ class GUI_EXPORT QgsMapToolCapture : public QgsMapToolAdvancedDigitizing

bool mCaptureModeFromLayer;

QgsVertexMarker *mSnappingMarker = nullptr;
std::unique_ptr<QgsSnapIndicator> mSnapIndicator;

/**
* Keeps point (in map units) snapped to a segment where we most recently finished tracing,
Expand Down
59 changes: 59 additions & 0 deletions src/gui/qgssnapindicator.cpp
@@ -0,0 +1,59 @@
/***************************************************************************
qgssnapindicator.cpp
--------------------------------------
Date : October 2017
Copyright : (C) 2017 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgssnapindicator.h"

#include "qgsvertexmarker.h"


QgsSnapIndicator::QgsSnapIndicator( QgsMapCanvas *canvas )
: mCanvas( canvas )
{
}

QgsSnapIndicator::~QgsSnapIndicator()
{
}

void QgsSnapIndicator::setMatch( const QgsPointLocator::Match &match )
{
mMatch = match;

if ( !mMatch.isValid() )
{
mSnappingMarker.reset();
}
else
{
if ( !mSnappingMarker )
{
mSnappingMarker.reset( new QgsVertexMarker( mCanvas ) );
mSnappingMarker->setIconType( QgsVertexMarker::ICON_CROSS );
mSnappingMarker->setColor( Qt::magenta );
mSnappingMarker->setPenWidth( 3 );
}
mSnappingMarker->setCenter( match.point() );
}
}

void QgsSnapIndicator::setVisible( bool visible )
{
mSnappingMarker->setVisible( visible );
}

bool QgsSnapIndicator::isVisible() const
{
return mSnappingMarker->isVisible();
}
55 changes: 55 additions & 0 deletions src/gui/qgssnapindicator.h
@@ -0,0 +1,55 @@
/***************************************************************************
qgssnapindicator.h
--------------------------------------
Date : October 2017
Copyright : (C) 2017 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSSNAPINDICATOR_H
#define QGSSNAPINDICATOR_H

#include "qgspointlocator.h"

#include "qgis_gui.h"

class QgsMapCanvas;
class QgsVertexMarker;


/**
* \ingroup gui
* Class that shows snapping marker on map canvas for the current snapping match.
* \since QGIS 3.0
*/
class GUI_EXPORT QgsSnapIndicator
{
public:
//! Constructs an indicator for the given map canvas
QgsSnapIndicator( QgsMapCanvas *canvas );
~QgsSnapIndicator();

//! Sets snapping match that should be displayed in map canvas. Invalid match hides the indicator
void setMatch( const QgsPointLocator::Match &match );
//! Returns currently displayed snapping match
QgsPointLocator::Match match() const { return mMatch; }

//! Sets whether the snapping indicator is visible
void setVisible( bool visible = true );
//! Returns whether the snapping indicator is visible
bool isVisible() const;

private:
QgsMapCanvas *mCanvas;
QgsPointLocator::Match mMatch;
std::unique_ptr<QgsVertexMarker> mSnappingMarker;
};

#endif // QGSSNAPINDICATOR_H

0 comments on commit 1edc1a7

Please sign in to comment.