Move handling of snap indicators from map tools to a new class

This commit is contained in:
Martin Dobias 2017-10-24 09:04:59 +01:00
parent a48c2e8fd4
commit 1edc1a790f
10 changed files with 202 additions and 80 deletions

View File

@ -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

View File

@ -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 *
************************************************************************/

View File

@ -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"
@ -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 );
@ -248,7 +245,6 @@ QgsNodeTool::QgsNodeTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget
QgsNodeTool::~QgsNodeTool()
{
delete mSnapMarker;
delete mEdgeCenterMarker;
delete mFeatureBand;
delete mFeatureBandMarkers;
@ -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();
@ -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() );
@ -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
@ -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 )

View File

@ -27,6 +27,7 @@ class QRubberBand;
class QgsGeometryValidator;
class QgsNodeEditor;
class QgsSelectedFeature;
class QgsSnapIndicator;
class QgsVertexMarker;
//! helper structure for a vertex being dragged
@ -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

View File

@ -333,6 +333,7 @@ SET(QGIS_GUI_SRCS
qgssearchquerybuilder.cpp
qgsshortcutsmanager.cpp
qgsslider.cpp
qgssnapindicator.cpp
qgssublayersdialog.cpp
qgssubstitutionlistwidget.cpp
qgssqlcomposerdialog.cpp
@ -710,6 +711,7 @@ SET(QGIS_GUI_HDRS
qgsmapmouseevent.h
qgsmaptip.h
qgsrubberband.h
qgssnapindicator.h
qgssqlcomposerdialog.h
qgstablewidgetitem.h
qgsuserinputdockwidget.h

View File

@ -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"
@ -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 ) );
@ -57,8 +60,6 @@ QgsMapToolCapture::QgsMapToolCapture( QgsMapCanvas *canvas, QgsAdvancedDigitizin
QgsMapToolCapture::~QgsMapToolCapture()
{
delete mSnappingMarker;
stopCapturing();
if ( mValidator )
@ -81,8 +82,7 @@ void QgsMapToolCapture::deactivate()
if ( mTempRubberBand )
mTempRubberBand->hide();
delete mSnappingMarker;
mSnappingMarker = nullptr;
mSnapIndicator->setMatch( QgsPointLocator::Match() );
QgsMapToolAdvancedDigitizing::deactivate();
}
@ -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 )
{

View File

@ -26,6 +26,7 @@
#include "qgis_gui.h"
class QgsRubberBand;
class QgsSnapIndicator;
class QgsVertexMarker;
class QgsMapLayer;
class QgsGeometryValidator;
@ -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,

View File

@ -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();
}

View File

@ -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

View File

@ -16,9 +16,9 @@
#include <QMouseEvent>
#include "qgsmaptoolemitpoint.h"
#include "qgssnapindicator.h"
#include "qgssnappingutils.h"
#include "qgspointxy.h"
#include "qgsvertexmarker.h"
#include "qgsmapcanvas.h"
#include "ui_qgsmapcoordsdialogbase.h"
@ -33,40 +33,22 @@ class QgsGeorefMapToolEmitPoint : public QgsMapTool
explicit QgsGeorefMapToolEmitPoint( QgsMapCanvas *canvas )
: QgsMapTool( canvas )
{
mSnapIndicator.reset( new QgsSnapIndicator( canvas ) );
}
virtual ~QgsGeorefMapToolEmitPoint()
{
delete mSnappingMarker;
mSnappingMarker = nullptr;
}
void canvasMoveEvent( QgsMapMouseEvent *e ) override
{
MappedPoint mapped = mapPoint( e );
if ( !mapped.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( mapped.point );
}
mSnapIndicator->setMatch( mapPointMatch( e ) );
}
void canvasPressEvent( QgsMapMouseEvent *e ) override
{
MappedPoint mapped = mapPoint( e );
emit canvasClicked( mapped.point, e->button() );
QgsPointLocator::Match m = mapPointMatch( e );
emit canvasClicked( m.isValid() ? m.point() : toMapCoordinates( e->pos() ), e->button() );
}
void canvasReleaseEvent( QgsMapMouseEvent *e ) override
@ -77,8 +59,7 @@ class QgsGeorefMapToolEmitPoint : public QgsMapTool
void deactivate() override
{
delete mSnappingMarker;
mSnappingMarker = 0;
mSnapIndicator->setMatch( QgsPointLocator::Match() );
QgsMapTool::deactivate();
}
@ -88,26 +69,14 @@ class QgsGeorefMapToolEmitPoint : public QgsMapTool
void mouseReleased();
private:
struct MappedPoint
{
MappedPoint() {}
QgsPointXY point;
bool snapped = false;
};
MappedPoint mapPoint( QMouseEvent *e )
QgsPointLocator::Match mapPointMatch( QMouseEvent *e )
{
QgsPointXY pnt = toMapCoordinates( e->pos() );
QgsSnappingUtils *snappingUtils = canvas()->snappingUtils();
QgsPointLocator::Match match = snappingUtils->snapToMap( pnt );
MappedPoint ret;
ret.snapped = match.isValid();
ret.point = ret.snapped ? match.point() : pnt;
return ret;
return canvas()->snappingUtils()->snapToMap( pnt );
}
QgsVertexMarker *mSnappingMarker = nullptr;
std::unique_ptr<QgsSnapIndicator> mSnapIndicator;
};
class QgsMapCoordsDialog : public QDialog, private Ui::QgsMapCoordsDialogBase