Skip to content

Commit

Permalink
Better control of CAD dock widget operation from map tools
Browse files Browse the repository at this point in the history
  • 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.
6 changes: 6 additions & 0 deletions doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -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}
-----------------

Expand Down
50 changes: 42 additions & 8 deletions python/gui/qgsmaptooladvanceddigitizing.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion python/gui/qgsmaptoolcapture.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions src/app/nodetool/qgsnodetool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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() )
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
14 changes: 0 additions & 14 deletions src/app/qgsmaptooladdcircularstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/app/qgsmaptooladdcircularstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptooladdfeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptooladdpart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
6 changes: 3 additions & 3 deletions src/gui/qgsadvanceddigitizingdockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
49 changes: 33 additions & 16 deletions src/gui/qgsmaptooladvanceddigitizing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand All @@ -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()
Expand All @@ -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();
}
53 changes: 44 additions & 9 deletions src/gui/qgsmaptooladvanceddigitizing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/gui/qgsmaptoolcapture.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/grass/qgsgrassaddfeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
Loading

0 comments on commit a2b2567

Please sign in to comment.