Skip to content

Commit

Permalink
Handle mouse events of adv.digitizing map tools in the map tool base …
Browse files Browse the repository at this point in the history
…class
  • Loading branch information
wonder-sk committed Sep 10, 2017
1 parent ae713b4 commit f938b60
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 124 deletions.
2 changes: 1 addition & 1 deletion doc/api_break.dox
Expand Up @@ -474,7 +474,7 @@ QgsAction {#qgis_api_break_3_0_QgsAction}
QgsAdvancedDigitizingDockWidget {#qgis_api_break_3_0_QgsAdvancedDigitizingDockWidget}
-------------------------------

- canvasReleaseEvent() does not take second argument anymore. Map tools are expected to clear CAD points themselves whenever needed.
- canvasPressEvent(), canvasReleaseEvent(), canvasMoveEvent() were removed. Handling of events is done in QgsMapToolAdvancedDigitizing.
- snappingMode() was removed. Advanced digitizing now always uses project's snapping configuration.


Expand Down
43 changes: 19 additions & 24 deletions python/gui/qgsadvanceddigitizingdockwidget.sip
Expand Up @@ -172,46 +172,30 @@ class QgsAdvancedDigitizingDockWidget : QgsDockWidget
Disables the CAD tools when hiding the dock
%End

bool canvasPressEvent( QgsMapMouseEvent *e );
%Docstring
Will react on a canvas press event

\param e A mouse event (may be modified)
:return: If the event is hidden (construction mode hides events from the maptool)
:rtype: bool
%End

bool canvasReleaseEvent( QgsMapMouseEvent *e );
bool canvasKeyPressEventFilter( QKeyEvent *e );
%Docstring
Will react on a canvas release event
Filter key events to e.g. toggle construction mode or adapt constraints

\param e A mouse event (may be modified)
:return: If the event is hidden (construction mode hides events from the maptool)
:rtype: bool
%End

bool canvasMoveEvent( QgsMapMouseEvent *e );
bool applyConstraints( QgsMapMouseEvent *e );
%Docstring
Will react on a canvas move event

\param e A mouse event (may be modified)
:return: If the event is hidden (construction mode hides events from the maptool)
:return: false if no solution was found (invalid constraints)
:rtype: bool
%End

bool canvasKeyPressEventFilter( QKeyEvent *e );
bool alignToSegment( QgsMapMouseEvent *e, QgsAdvancedDigitizingDockWidget::CadConstraint::LockMode lockMode = QgsAdvancedDigitizingDockWidget::CadConstraint::HardLock );
%Docstring
Filter key events to e.g. toggle construction mode or adapt constraints

\param e A mouse event (may be modified)
:return: If the event is hidden (construction mode hides events from the maptool)
.. versionadded:: 3.0
:rtype: bool
%End

bool applyConstraints( QgsMapMouseEvent *e );
void releaseLocks( bool releaseRepeatingLocks = true );
%Docstring
:return: false if no solution was found (invalid constraints)
:rtype: bool
.. versionadded:: 3.0
%End

void clear();
Expand Down Expand Up @@ -269,6 +253,12 @@ Constraint on a common angle
%Docstring
Removes all points from the CAD point list
.. versionadded:: 3.0
%End

void addPoint( const QgsPointXY &point );
%Docstring
Adds point to the CAD point list
.. versionadded:: 3.0
%End

void setPoints( const QList<QgsPointXY> &points );
Expand Down Expand Up @@ -341,6 +331,11 @@ return the action used to enable/disable the tools
Disable the widget. Normally done automatically from QgsMapToolAdvancedDigitizing.deactivate().
%End

void updateCadPaintItem();
%Docstring
.. versionadded:: 3.0
%End

signals:

void pushWarning( const QString &message );
Expand Down
74 changes: 14 additions & 60 deletions src/gui/qgsadvanceddigitizingdockwidget.cpp
Expand Up @@ -864,6 +864,15 @@ bool QgsAdvancedDigitizingDockWidget::applyConstraints( QgsMapMouseEvent *e )
}
}

if ( res )
{
emit popWarning();
}
else
{
emit pushWarning( tr( "Some constraints are incompatible. Resulting point might be incorrect." ) );
}

return res;
}

Expand Down Expand Up @@ -952,66 +961,6 @@ bool QgsAdvancedDigitizingDockWidget::alignToSegment( QgsMapMouseEvent *e, CadCo
return true;
}

bool QgsAdvancedDigitizingDockWidget::canvasPressEvent( QgsMapMouseEvent *e )
{
applyConstraints( e );
return mCadEnabled && mConstructionMode;
}

bool QgsAdvancedDigitizingDockWidget::canvasReleaseEvent( QgsMapMouseEvent *e )
{
if ( !mCadEnabled )
return false;

emit popWarning();

if ( e->button() == Qt::RightButton )
{
clear();
return false;
}

applyConstraints( e );

if ( alignToSegment( e ) )
{
// launch a fake move event so rubber bands of map tools will be adapted with new constraints
// emit pointChanged( e );

// Parallel or perpendicular mode and snapped to segment
// this has emitted the lockAngle signal
return true;
}

addPoint( e->mapPoint() );

releaseLocks( false );

return mConstructionMode;
}

bool QgsAdvancedDigitizingDockWidget::canvasMoveEvent( QgsMapMouseEvent *e )
{
if ( !mCadEnabled )
return false;

if ( !applyConstraints( e ) )
{
emit pushWarning( tr( "Some constraints are incompatible. Resulting point might be incorrect." ) );
}
else
{
emit popWarning();
}

// perpendicular/parallel constraint
// do a soft lock when snapping to a segment
alignToSegment( e, CadConstraint::SoftLock );
mCadPaintItem->update();

return false;
}

bool QgsAdvancedDigitizingDockWidget::canvasKeyPressEventFilter( QKeyEvent *e )
{
// event on map tool
Expand Down Expand Up @@ -1267,6 +1216,11 @@ void QgsAdvancedDigitizingDockWidget::disable()
setCadEnabled( false );
}

void QgsAdvancedDigitizingDockWidget::updateCadPaintItem()
{
mCadPaintItem->update();
}

void QgsAdvancedDigitizingDockWidget::addPoint( const QgsPointXY &point )
{
if ( !pointsCount() )
Expand Down
53 changes: 19 additions & 34 deletions src/gui/qgsadvanceddigitizingdockwidget.h
Expand Up @@ -204,30 +204,6 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
*/
void hideEvent( QHideEvent * ) override;

/**
* Will react on a canvas press event
*
* \param e A mouse event (may be modified)
* \returns If the event is hidden (construction mode hides events from the maptool)
*/
bool canvasPressEvent( QgsMapMouseEvent *e );

/**
* Will react on a canvas release event
*
* \param e A mouse event (may be modified)
* \returns If the event is hidden (construction mode hides events from the maptool)
*/
bool canvasReleaseEvent( QgsMapMouseEvent *e );

/**
* Will react on a canvas move event
*
* \param e A mouse event (may be modified)
* \returns If the event is hidden (construction mode hides events from the maptool)
*/
bool canvasMoveEvent( QgsMapMouseEvent *e );

/**
* Filter key events to e.g. toggle construction mode or adapt constraints
*
Expand All @@ -240,6 +216,16 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
//! \returns false if no solution was found (invalid constraints)
bool applyConstraints( QgsMapMouseEvent *e );

//! align to segment for additional constraint.
//! If additional constraints are used, this will determine the angle to be locked depending on the snapped segment.
//! \since QGIS 3.0
bool alignToSegment( QgsMapMouseEvent *e, QgsAdvancedDigitizingDockWidget::CadConstraint::LockMode lockMode = QgsAdvancedDigitizingDockWidget::CadConstraint::HardLock );

//! unlock all constraints
//! \param releaseRepeatingLocks set to false to preserve the lock for any constraints set to repeating lock mode
//! \since QGIS 3.0
void releaseLocks( bool releaseRepeatingLocks = true );

/**
* Clear any cached previous clicks and helper lines
*/
Expand Down Expand Up @@ -271,6 +257,11 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
*/
void clearPoints();

/** Adds point to the CAD point list
* \since QGIS 3.0
*/
void addPoint( const QgsPointXY &point );

/**
* Configures list of current CAD points
*
Expand Down Expand Up @@ -332,6 +323,10 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
*/
void disable();

//! Updates canvas item that displays constraints on the ma
//! \since QGIS 3.0
void updateCadPaintItem();

signals:

/**
Expand Down Expand Up @@ -369,10 +364,6 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
//! will be converted to their calculated value
void constraintFocusOut();

//! unlock all constraints
//! \param releaseRepeatingLocks set to false to preserve the lock for any constraints set to repeating lock mode
void releaseLocks( bool releaseRepeatingLocks = true );

//! set the relative properties of constraints
void setConstraintRelative( bool activate );

Expand Down Expand Up @@ -404,10 +395,6 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private

QList<QgsPointXY> snapSegment( const QgsPointLocator::Match &snapMatch );

//! align to segment for additional constraint.
//! If additional constraints are used, this will determine the angle to be locked depending on the snapped segment.
bool alignToSegment( QgsMapMouseEvent *e, CadConstraint::LockMode lockMode = CadConstraint::HardLock );

/**
* Returns the first snapped segment. Will try to snap a segment according to the event's snapping mode.
* \param originalMapPoint point to be snapped (in map coordinates)
Expand All @@ -416,8 +403,6 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
*/
QList<QgsPointXY> snapSegment( const QgsPointXY &originalMapPoint, bool *snapped = nullptr, bool allLayers = false ) const;

//! add point to the CAD point list
void addPoint( const QgsPointXY &point );
//! update the current point in the CAD point list
void updateCurrentPoint( const QgsPointXY &point );
//! remove previous point in the CAD point list
Expand Down
35 changes: 30 additions & 5 deletions src/gui/qgsmaptooladvanceddigitizing.cpp
Expand Up @@ -28,7 +28,9 @@ void QgsMapToolAdvancedDigitizing::canvasPressEvent( QgsMapMouseEvent *e )
{
if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
{
if ( mCadDockWidget->canvasPressEvent( e ) )
mCadDockWidget->applyConstraints( e ); // updates event's map point

if ( mCadDockWidget->constructionMode() )
return; // decided to eat the event and not pass it to the map tool (construction mode)
}
else if ( isAutoSnapEnabled() )
Expand All @@ -43,8 +45,27 @@ void QgsMapToolAdvancedDigitizing::canvasReleaseEvent( QgsMapMouseEvent *e )
{
if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
{
if ( mCadDockWidget->canvasReleaseEvent( e ) )
return; // decided to eat the event and not pass it to the map tool (construction mode or picking a segment)
if ( e->button() == Qt::RightButton )
{
mCadDockWidget->clear();
}
else
{
mCadDockWidget->applyConstraints( e ); // updates event's map point

if ( mCadDockWidget->alignToSegment( e ) )
{
// Parallel or perpendicular mode and snapped to segment: do not pass the event to map tool
return;
}

mCadDockWidget->addPoint( e->mapPoint() );

mCadDockWidget->releaseLocks( false );

if ( mCadDockWidget->constructionMode() )
return; // decided to eat the event and not pass it to the map tool (construction mode)
}
}
else if ( isAutoSnapEnabled() )
{
Expand All @@ -58,8 +79,12 @@ void QgsMapToolAdvancedDigitizing::canvasMoveEvent( QgsMapMouseEvent *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)
mCadDockWidget->applyConstraints( e ); // updates event's map point

// perpendicular/parallel constraint
// do a soft lock when snapping to a segment
mCadDockWidget->alignToSegment( e, QgsAdvancedDigitizingDockWidget::CadConstraint::SoftLock );
mCadDockWidget->updateCadPaintItem();
}
else if ( isAutoSnapEnabled() )
{
Expand Down

0 comments on commit f938b60

Please sign in to comment.