Skip to content
Permalink
Browse files
Better control of CAD dock widget operation from map tools
  • Loading branch information
wonder-sk committed Sep 10, 2017
1 parent cea7eb8 commit a2b2567
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 68 deletions.
@@ -1624,6 +1624,12 @@ QgsMapTool {#qgis_api_break_3_0_QgsMapTool}
- isTransient() and isEditTool() were removed. Use flags() instead.


QgsMapToolAdvancedDigitizing {#qgis_api_break_3_0_QgsMapToolAdvancedDigitizing}
----------------------------

- setMode() was replaced by setAdvancedDigitizingAllowed() and setAutoSnapEnabled()


QgsMapToolCapture {#qgis_api_break_3_0_QgsMapToolCapture}
-----------------

@@ -64,14 +64,6 @@ Catch the mouse move event, filters it, transforms it to map coordinates and sen
:rtype: CaptureMode
%End

void setMode( CaptureMode mode );
%Docstring
Set capture mode. This should correspond to the layer on which the digitizing
happens.

\param mode Capture Mode
%End

virtual void activate();
%Docstring
Registers this maptool with the cad dock widget
@@ -87,9 +79,51 @@ Catch the mouse move event, filters it, transforms it to map coordinates and sen
:rtype: QgsAdvancedDigitizingDockWidget
%End

bool isAdvancedDigitizingAllowed() const;
%Docstring
Returns whether functionality of advanced digitizing dock widget is currently allowed.

Tools may decide to switch this support on/off based on the current state of the map tool.
For example, in node tool before user picks a vertex to move, advanced digitizing dock
widget should be disabled and only enabled once a vertex is being moved. Other map tools
may keep advanced digitizing allowed all the time.

If true is returned, that does not mean that advanced digitizing is actually active,
because it is up to the user to enable/disable it when it is allowed.
\sa setAdvancedDigitizingAllowed()
.. versionadded:: 3.0
:rtype: bool
%End

bool isAutoSnapEnabled() const;
%Docstring
Returns whether mouse events (press/move/release) should automatically try to snap mouse position
(according to the snapping configuration of map canvas) before passing the mouse coordinates
to the tool. This may be desirable default behavior for some map tools, but not for other map tools.
It is therefore possible to configure the behavior by the map tool.
\sa isAutoSnapEnabled()
.. versionadded:: 3.0
:rtype: bool
%End

protected:

void setAdvancedDigitizingAllowed( bool allowed );
%Docstring
Sets whether functionality of advanced digitizing dock widget is currently allowed.
This method is protected because it should be a decision of the map tool and not from elsewhere.
\sa isAdvancedDigitizingAllowed()
.. versionadded:: 3.0
%End

void setAutoSnapEnabled( bool enabled );
%Docstring
Sets whether mouse events (press/move/release) should automatically try to snap mouse position
This method is protected because it should be a decision of the map tool and not from elsewhere.
\sa isAutoSnapEnabled()
.. versionadded:: 3.0
%End

virtual void cadCanvasPressEvent( QgsMapMouseEvent *e );
%Docstring
Override this method when subclassing this class.
@@ -18,7 +18,7 @@ class QgsMapToolCapture : QgsMapToolAdvancedDigitizing
#include "qgsmaptoolcapture.h"
%End
public:
QgsMapToolCapture( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget, CaptureMode mode = CaptureNone );
QgsMapToolCapture( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget, CaptureMode mode );
%Docstring
constructor
%End
@@ -200,6 +200,8 @@ class MatchCollectingFilter : public QgsPointLocator::MatchFilter
QgsNodeTool::QgsNodeTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDock )
: QgsMapToolAdvancedDigitizing( canvas, cadDock )
{
setAdvancedDigitizingAllowed( false );

mSnapMarker = new QgsVertexMarker( canvas );
mSnapMarker->setIconType( QgsVertexMarker::ICON_CROSS );
mSnapMarker->setColor( Qt::magenta );
@@ -1029,7 +1031,7 @@ void QgsNodeTool::startDragging( QgsMapMouseEvent *e )
return;

// activate advanced digitizing dock
setMode( CaptureLine );
setAdvancedDigitizingAllowed( true );

// adding a new vertex instead of moving a vertex
if ( m.hasEdge() )
@@ -1260,7 +1262,7 @@ void QgsNodeTool::startDraggingAddVertex( const QgsPointLocator::Match &m )
Q_ASSERT( m.hasEdge() );

// activate advanced digitizing dock
setMode( CaptureLine );
setAdvancedDigitizingAllowed( true );

mDraggingVertex.reset( new Vertex( m.layer(), m.featureId(), m.vertexIndex() + 1 ) );
mDraggingVertexType = AddingVertex;
@@ -1290,7 +1292,7 @@ void QgsNodeTool::startDraggingAddVertexAtEndpoint( const QgsPointXY &mapPoint )
Q_ASSERT( mMouseAtEndpoint );

// activate advanced digitizing dock
setMode( CaptureLine );
setAdvancedDigitizingAllowed( true );

mDraggingVertex.reset( new Vertex( mMouseAtEndpoint->layer, mMouseAtEndpoint->fid, mMouseAtEndpoint->vertexId ) );
mDraggingVertexType = AddingEndpoint;
@@ -1315,7 +1317,7 @@ void QgsNodeTool::startDraggingEdge( const QgsPointLocator::Match &m, const QgsP
Q_ASSERT( m.hasEdge() );

// activate advanced digitizing
setMode( CaptureLine );
setAdvancedDigitizingAllowed( true );

mDraggingEdge = true;
mDraggingExtraVertices.clear();
@@ -1354,7 +1356,7 @@ void QgsNodeTool::startDraggingEdge( const QgsPointLocator::Match &m, const QgsP
void QgsNodeTool::stopDragging()
{
// deactivate advanced digitizing
setMode( CaptureNone );
setAdvancedDigitizingAllowed( false );

// stop adv digitizing
QMouseEvent mouseEvent( QEvent::MouseButtonRelease,
@@ -1399,7 +1401,7 @@ void QgsNodeTool::moveEdge( const QgsPointXY &mapPoint )
void QgsNodeTool::moveVertex( const QgsPointXY &mapPoint, const QgsPointLocator::Match *mapPointMatch )
{
// deactivate advanced digitizing
setMode( CaptureNone );
setAdvancedDigitizingAllowed( false );

QgsVectorLayer *dragLayer = mDraggingVertex->layer;
QgsFeatureId dragFid = mDraggingVertex->fid;
@@ -3178,7 +3178,7 @@ void QgisApp::createCanvasTools()
mMapTools.mSvgAnnotation->setAction( mActionSvgAnnotation );
mMapTools.mAnnotation = new QgsMapToolAnnotation( mMapCanvas );
mMapTools.mAnnotation->setAction( mActionAnnotation );
mMapTools.mAddFeature = new QgsMapToolAddFeature( mMapCanvas );
mMapTools.mAddFeature = new QgsMapToolAddFeature( mMapCanvas, QgsMapToolCapture::CaptureNone );
mMapTools.mAddFeature->setAction( mActionAddFeature );
mMapTools.mCircularStringCurvePoint = new QgsMapToolCircularStringCurvePoint( dynamic_cast<QgsMapToolAddFeature *>( mMapTools.mAddFeature ), mMapCanvas );
mMapTools.mCircularStringCurvePoint->setAction( mActionCircularStringCurvePoint );
@@ -40,20 +40,6 @@ QgsMapToolAddCircularString::QgsMapToolAddCircularString( QgsMapToolCapture *par
connect( QgisApp::instance(), &QgisApp::projectRead, this, &QgsMapToolAddCircularString::stopCapturing );
}

QgsMapToolAddCircularString::QgsMapToolAddCircularString( QgsMapCanvas *canvas )
: QgsMapToolCapture( canvas, QgisApp::instance()->cadDockWidget() )
, mParentTool( nullptr )
, mRubberBand( nullptr )
, mTempRubberBand( nullptr )
, mShowCenterPointRubberBand( false )
, mCenterPointRubberBand( nullptr )
{
if ( mCanvas )
{
connect( mCanvas, &QgsMapCanvas::mapToolSet, this, &QgsMapToolAddCircularString::setParentTool );
}
}

QgsMapToolAddCircularString::~QgsMapToolAddCircularString()
{
delete mRubberBand;
@@ -38,7 +38,6 @@ class QgsMapToolAddCircularString: public QgsMapToolCapture
void setParentTool( QgsMapTool *newTool, QgsMapTool *oldTool );

protected:
explicit QgsMapToolAddCircularString( QgsMapCanvas *canvas ); //forbidden

/** The parent map tool, e.g. the add feature tool.
* Completed circular strings will be added to this tool by calling its addCurve() method.
@@ -22,7 +22,7 @@ class APP_EXPORT QgsMapToolAddFeature : public QgsMapToolCapture
Q_OBJECT
public:
//! \since QGIS 2.12
QgsMapToolAddFeature( QgsMapCanvas *canvas, CaptureMode mode = CaptureNone );
QgsMapToolAddFeature( QgsMapCanvas *canvas, CaptureMode mode );
virtual ~QgsMapToolAddFeature();
void cadCanvasReleaseEvent( QgsMapMouseEvent *e ) override;

@@ -28,7 +28,7 @@
#include <QMouseEvent>

QgsMapToolAddPart::QgsMapToolAddPart( QgsMapCanvas *canvas )
: QgsMapToolCapture( canvas, QgisApp::instance()->cadDockWidget() )
: QgsMapToolCapture( canvas, QgisApp::instance()->cadDockWidget(), CaptureNone )
{
mToolName = tr( "Add part" );
connect( QgisApp::instance(), &QgisApp::newProject, this, &QgsMapToolAddPart::stopCapturing );
@@ -872,12 +872,12 @@ bool QgsAdvancedDigitizingDockWidget::alignToSegment( QgsMapMouseEvent *e, CadCo
return false;
}

bool previousPointExist, penulPointExist, mSnappedSegmentExist;
bool previousPointExist, penulPointExist, snappedSegmentExist;
QgsPointXY previousPt = previousPoint( &previousPointExist );
QgsPointXY penultimatePt = penultimatePoint( &penulPointExist );
QList<QgsPointXY> mSnappedSegment = e->snapSegment( &mSnappedSegmentExist, true );
mSnappedSegment = e->snapSegment( &snappedSegmentExist, true );

if ( !previousPointExist || !mSnappedSegmentExist )
if ( !previousPointExist || !snappedSegmentExist )
{
return false;
}
@@ -27,15 +27,21 @@ QgsMapToolAdvancedDigitizing::QgsMapToolAdvancedDigitizing( QgsMapCanvas *canvas

void QgsMapToolAdvancedDigitizing::canvasPressEvent( QgsMapMouseEvent *e )
{
snap( e );
if ( !mCadDockWidget->canvasPressEvent( e ) )
cadCanvasPressEvent( e );
if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
{
if ( mCadDockWidget->canvasPressEvent( e ) )
return; // decided to eat the event and not pass it to the map tool (construction mode)
}
else if ( isAutoSnapEnabled() )
{
e->snapPoint();
}

cadCanvasPressEvent( e );
}

void QgsMapToolAdvancedDigitizing::canvasReleaseEvent( QgsMapMouseEvent *e )
{
snap( e );

QgsAdvancedDigitizingDockWidget::AdvancedDigitizingMode dockMode;
switch ( mCaptureMode )
{
@@ -51,15 +57,32 @@ void QgsMapToolAdvancedDigitizing::canvasReleaseEvent( QgsMapMouseEvent *e )
break;
}

if ( !mCadDockWidget->canvasReleaseEvent( e, dockMode ) )
cadCanvasReleaseEvent( e );
if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
{
if ( mCadDockWidget->canvasReleaseEvent( e, dockMode ) )
return; // decided to eat the event and not pass it to the map tool (construction mode or picking a segment)
}
else if ( isAutoSnapEnabled() )
{
e->snapPoint();
}

cadCanvasReleaseEvent( e );
}

void QgsMapToolAdvancedDigitizing::canvasMoveEvent( QgsMapMouseEvent *e )
{
snap( e );
if ( !mCadDockWidget->canvasMoveEvent( e ) )
cadCanvasMoveEvent( e );
if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
{
if ( mCadDockWidget->canvasMoveEvent( e ) )
return; // decided to eat the event and not pass it to the map tool (never happens currently)
}
else if ( isAutoSnapEnabled() )
{
e->snapPoint();
}

cadCanvasMoveEvent( e );
}

void QgsMapToolAdvancedDigitizing::activate()
@@ -82,9 +105,3 @@ void QgsMapToolAdvancedDigitizing::cadPointChanged( const QgsPointXY &point )
QMouseEvent *ev = new QMouseEvent( QEvent::MouseMove, mCanvas->mouseLastXY(), Qt::NoButton, Qt::NoButton, Qt::NoModifier );
qApp->postEvent( mCanvas->viewport(), ev ); // event queue will delete the event when processed
}

void QgsMapToolAdvancedDigitizing::snap( QgsMapMouseEvent *e )
{
if ( !mCadDockWidget->cadEnabled() )
e->snapPoint();
}
@@ -66,14 +66,6 @@ class GUI_EXPORT QgsMapToolAdvancedDigitizing : public QgsMapToolEdit
*/
CaptureMode mode() const { return mCaptureMode; }

/**
* Set capture mode. This should correspond to the layer on which the digitizing
* happens.
*
* \param mode Capture Mode
*/
void setMode( CaptureMode mode ) { mCaptureMode = mode; }

/**
* Registers this maptool with the cad dock widget
*/
@@ -86,9 +78,49 @@ class GUI_EXPORT QgsMapToolAdvancedDigitizing : public QgsMapToolEdit

QgsAdvancedDigitizingDockWidget *cadDockWidget() const { return mCadDockWidget; }

/**
* Returns whether functionality of advanced digitizing dock widget is currently allowed.
*
* Tools may decide to switch this support on/off based on the current state of the map tool.
* For example, in node tool before user picks a vertex to move, advanced digitizing dock
* widget should be disabled and only enabled once a vertex is being moved. Other map tools
* may keep advanced digitizing allowed all the time.
*
* If true is returned, that does not mean that advanced digitizing is actually active,
* because it is up to the user to enable/disable it when it is allowed.
* \sa setAdvancedDigitizingAllowed()
* \since QGIS 3.0
*/
bool isAdvancedDigitizingAllowed() const { return mAdvancedDigitizingAllowed; }

/**
* Returns whether mouse events (press/move/release) should automatically try to snap mouse position
* (according to the snapping configuration of map canvas) before passing the mouse coordinates
* to the tool. This may be desirable default behavior for some map tools, but not for other map tools.
* It is therefore possible to configure the behavior by the map tool.
* \sa isAutoSnapEnabled()
* \since QGIS 3.0
*/
bool isAutoSnapEnabled() const { return mAutoSnapEnabled; }

protected:

/**
* Sets whether functionality of advanced digitizing dock widget is currently allowed.
* This method is protected because it should be a decision of the map tool and not from elsewhere.
* \sa isAdvancedDigitizingAllowed()
* \since QGIS 3.0
*/
void setAdvancedDigitizingAllowed( bool allowed ) { mAdvancedDigitizingAllowed = allowed; }

/**
* Sets whether mouse events (press/move/release) should automatically try to snap mouse position
* This method is protected because it should be a decision of the map tool and not from elsewhere.
* \sa isAutoSnapEnabled()
* \since QGIS 3.0
*/
void setAutoSnapEnabled( bool enabled ) { mAutoSnapEnabled = enabled; }

/**
* Override this method when subclassing this class.
* This will receive adapted events from the cad system whenever a
@@ -139,7 +171,10 @@ class GUI_EXPORT QgsMapToolAdvancedDigitizing : public QgsMapToolEdit
private:
QgsAdvancedDigitizingDockWidget *mCadDockWidget = nullptr;

void snap( QgsMapMouseEvent *e );
//! Whether to allow use of advanced digitizing dock at this point
bool mAdvancedDigitizingAllowed = true;
//! Whether to snap mouse cursor to map before passing coordinates to cadCanvas*Event()
bool mAutoSnapEnabled = true;
};

#endif // QGSMAPTOOLADVANCEDDIGITIZE_H
@@ -39,7 +39,7 @@ class GUI_EXPORT QgsMapToolCapture : public QgsMapToolAdvancedDigitizing

public:
//! constructor
QgsMapToolCapture( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget, CaptureMode mode = CaptureNone );
QgsMapToolCapture( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget, CaptureMode mode );

virtual ~QgsMapToolCapture();

@@ -23,7 +23,7 @@ class QgsGrassAddFeature : public QgsMapToolAddFeature
{
Q_OBJECT
public:
QgsGrassAddFeature( QgsMapCanvas *canvas, CaptureMode mode = CaptureNone );
QgsGrassAddFeature( QgsMapCanvas *canvas, CaptureMode mode );
virtual ~QgsGrassAddFeature() override;
};

0 comments on commit a2b2567

Please sign in to comment.