Skip to content

Commit

Permalink
Always use snapping configuration from the project in adv.digitizing …
Browse files Browse the repository at this point in the history
…dock

It was slightly confusing to have another override for snapping while it is possible
to configure "no snapping" or "all layers" snapping mode in project anyway.
And with the nice snapping toolbar it can be also done very quickly.
  • Loading branch information
wonder-sk committed Aug 16, 2017
1 parent 42933a2 commit 0be0a4e
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 106 deletions.
8 changes: 8 additions & 0 deletions doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ QgsAdvancedDigitizingDockWidget {#qgis_api_break_3_0_QgsAdvancedDigitizin
-------------------------------

- canvasReleaseEvent takes now QgsAdvancedDigitizingDockWidget::CaptureMode as second argument.
- snappingMode() was removed. Advanced digitizing now always uses project's snapping configuration.


QgsApplication {#qgis_api_break_3_0_QgsApplication}
Expand Down Expand Up @@ -1550,6 +1551,13 @@ QgsMapLayerLegend {#qgis_api_break_3_0_QgsMapLayerLegend}
- defaultPluginLegend() was removed. Plugin layers have to provide their legend implementation.


QgsMapMouseEvent {#qgis_api_break_3_0_QgsMapMouseEvent}
----------------

- SnappingMode enum was removed.
- snapPoint() and snapSegment() do not take SnappingMode argument anymore. Snapping is done according to project's snapping configuration.


QgsMapOverviewCanvas {#qgis_api_break_3_0_QgsMapOverviewCanvas}
--------------------

Expand Down
7 changes: 0 additions & 7 deletions python/gui/qgsadvanceddigitizingdockwidget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,6 @@ class QgsAdvancedDigitizingDockWidget : QgsDockWidget
Clear any cached previous clicks and helper lines
%End

QgsMapMouseEvent::SnappingMode snappingMode();
%Docstring
The snapping mode
:return: Snapping mode
:rtype: QgsMapMouseEvent.SnappingMode
%End

virtual void keyPressEvent( QKeyEvent *e );


Expand Down
12 changes: 2 additions & 10 deletions python/gui/qgsmapmouseevent.sip
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ class QgsMapMouseEvent : QMouseEvent
%End
public:

enum SnappingMode
{
NoSnapping,
SnapProjectConfig,
SnapAllLayers,
};

QgsMapMouseEvent( QgsMapCanvas *mapCanvas, QMouseEvent *event );
%Docstring
Creates a new QgsMapMouseEvent. Should only be required to be called from the QgsMapCanvas.
Expand All @@ -59,7 +52,7 @@ class QgsMapMouseEvent : QMouseEvent
\param modifiers Keyboard modifiers
%End

QgsPointXY snapPoint( SnappingMode snappingMode );
QgsPointXY snapPoint();
%Docstring
snapPoint will snap the points using the map canvas snapping utils configuration
.. note::
Expand All @@ -68,12 +61,11 @@ class QgsMapMouseEvent : QMouseEvent
:rtype: QgsPointXY
%End

QList<QgsPointXY> snapSegment( SnappingMode snappingMode, bool *snapped = 0, bool allLayers = false ) const;
QList<QgsPointXY> snapSegment( bool *snapped = 0, bool allLayers = false ) const;
%Docstring
Returns the first snapped segment. If the cached snapped match is a segment, it will simply return it.
Otherwise it will try to snap a segment according to the event's snapping mode. In this case the cache
will not be overwritten.
\param snappingMode Specify if the default project settings or all layers should be used for snapping
\param snapped if given, determines if a segment has been snapped
\param allLayers if true, override snapping mode
:rtype: list of QgsPointXY
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoolannotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent *e )
{
if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::MoveMapPosition )
{
QgsPointXY mapPos = transformCanvasToAnnotation( e->snapPoint( QgsMapMouseEvent::SnapProjectConfig ), annotation );
QgsPointXY mapPos = transformCanvasToAnnotation( e->snapPoint(), annotation );
annotation->setMapPosition( mapPos );
annotation->setRelativePosition( QPointF( e->posF().x() / mCanvas->width(),
e->posF().y() / mCanvas->height() ) );
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsmaptoolmovefeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void QgsMapToolMoveFeature::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
if ( !mRubberBand )
{
// ideally we would snap preferably on the moved feature
e->snapPoint( QgsMapMouseEvent::SnapProjectConfig );
e->snapPoint();

//find first geometry under mouse cursor and store iterator to it
QgsPointXY layerCoords = toLayerCoordinates( vlayer, e->mapPoint() );
Expand Down Expand Up @@ -159,7 +159,7 @@ void QgsMapToolMoveFeature::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
mRubberBand = nullptr;
return;
}
e->snapPoint( QgsMapMouseEvent::SnapProjectConfig );
e->snapPoint();

QgsPointXY startPointLayerCoords = toLayerCoordinates( ( QgsMapLayer * )vlayer, mStartPointMapCoords );
QgsPointXY stopPointLayerCoords = toLayerCoordinates( ( QgsMapLayer * )vlayer, e->mapPoint() );
Expand Down
34 changes: 3 additions & 31 deletions src/gui/qgsadvanceddigitizingdockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ QgsAdvancedDigitizingDockWidget::QgsAdvancedDigitizingDockWidget( QgsMapCanvas *
, mCurrentMapToolSupportsCad( false )
, mCadEnabled( false )
, mConstructionMode( false )
, mSnappingMode( ( QgsMapMouseEvent::SnappingMode ) QgsSettings().value( QStringLiteral( "/Cad/SnappingMode" ), QgsMapMouseEvent::SnapProjectConfig ).toInt() )
, mCommonAngleConstraint( QgsSettings().value( QStringLiteral( "/Cad/CommonAngle" ), 90 ).toInt() )
, mSnappedToVertex( false )
, mSessionActive( false )
Expand Down Expand Up @@ -177,23 +176,6 @@ QgsAdvancedDigitizingDockWidget::QgsAdvancedDigitizingDockWidget( QgsMapCanvas *
angleButtonGroup->addAction( action );
mCommonAngleActions.insert( action, it->first );
}
// snapping on layers
menu->addSeparator();
QActionGroup *snapButtonGroup = new QActionGroup( menu ); // actions are exclusive for snapping modes
mSnappingActions = QMap<QAction *, QgsMapMouseEvent::SnappingMode>();
QList< QPair< QgsMapMouseEvent::SnappingMode, QString > > snappingModes;
snappingModes << QPair<QgsMapMouseEvent::SnappingMode, QString>( QgsMapMouseEvent::NoSnapping, tr( "Do not snap to vertices or segment" ) );
snappingModes << QPair<QgsMapMouseEvent::SnappingMode, QString>( QgsMapMouseEvent::SnapProjectConfig, tr( "Snap according to project configuration" ) );
snappingModes << QPair<QgsMapMouseEvent::SnappingMode, QString>( QgsMapMouseEvent::SnapAllLayers, tr( "Snap to all layers" ) );
for ( QList< QPair< QgsMapMouseEvent::SnappingMode, QString > >::const_iterator it = snappingModes.begin(); it != snappingModes.end(); ++it )
{
QAction *action = new QAction( it->second, menu );
action->setCheckable( true );
action->setChecked( it->first == mSnappingMode );
menu->addAction( action );
snapButtonGroup->addAction( action );
mSnappingActions.insert( action, it->first );
}

mSettingsButton->setMenu( menu );
connect( mSettingsButton, SIGNAL( triggered( QAction * ) ), this, SLOT( settingsButtonTriggered( QAction * ) ) );
Expand Down Expand Up @@ -319,16 +301,6 @@ void QgsAdvancedDigitizingDockWidget::setConstructionMode( bool enabled )

void QgsAdvancedDigitizingDockWidget::settingsButtonTriggered( QAction *action )
{
// snapping
QMap<QAction *, QgsMapMouseEvent::SnappingMode>::const_iterator isn = mSnappingActions.constFind( action );
if ( isn != mSnappingActions.constEnd() )
{
isn.key()->setChecked( true );
mSnappingMode = isn.value();
QgsSettings().setValue( QStringLiteral( "/Cad/SnappingMode" ), ( int )isn.value() );
return;
}

// common angles
QMap<QAction *, int>::const_iterator ica = mCommonAngleActions.constFind( action );
if ( ica != mCommonAngleActions.constEnd() )
Expand Down Expand Up @@ -598,9 +570,9 @@ bool QgsAdvancedDigitizingDockWidget::applyConstraints( QgsMapMouseEvent *e )
QgsDebugMsgLevel( QString( "X: %1 %2 %3" ).arg( mXConstraint->isLocked() ).arg( mXConstraint->relative() ).arg( mXConstraint->value() ), 4 );
QgsDebugMsgLevel( QString( "Y: %1 %2 %3" ).arg( mYConstraint->isLocked() ).arg( mYConstraint->relative() ).arg( mYConstraint->value() ), 4 );

QgsPointXY point = e->snapPoint( mSnappingMode );
QgsPointXY point = e->snapPoint();

mSnappedSegment = e->snapSegment( mSnappingMode );
mSnappedSegment = e->snapSegment();

bool previousPointExist, penulPointExist;
QgsPointXY previousPt = previousPoint( &previousPointExist );
Expand Down Expand Up @@ -903,7 +875,7 @@ bool QgsAdvancedDigitizingDockWidget::alignToSegment( QgsMapMouseEvent *e, CadCo
bool previousPointExist, penulPointExist, mSnappedSegmentExist;
QgsPointXY previousPt = previousPoint( &previousPointExist );
QgsPointXY penultimatePt = penultimatePoint( &penulPointExist );
QList<QgsPointXY> mSnappedSegment = e->snapSegment( mSnappingMode, &mSnappedSegmentExist, true );
QList<QgsPointXY> mSnappedSegment = e->snapSegment( &mSnappedSegmentExist, true );

if ( !previousPointExist || !mSnappedSegmentExist )
{
Expand Down
8 changes: 0 additions & 8 deletions src/gui/qgsadvanceddigitizingdockwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,6 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
*/
void clear();

/**
* The snapping mode
* \returns Snapping mode
*/
QgsMapMouseEvent::SnappingMode snappingMode() { return mSnappingMode; }

void keyPressEvent( QKeyEvent *e ) override;

//! determines if CAD tools are enabled or if map tools behaves "nomally"
Expand Down Expand Up @@ -455,7 +449,6 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
//! is CAD currently enabled for current map tool
bool mCadEnabled;
bool mConstructionMode;
QgsMapMouseEvent::SnappingMode mSnappingMode;

// constraints
std::unique_ptr< CadConstraint > mAngleConstraint;
Expand All @@ -478,7 +471,6 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
// UI
QAction *mEnableAction = nullptr;
QMap< QAction *, int > mCommonAngleActions; // map the common angle actions with their angle values
QMap< QAction *, QgsMapMouseEvent::SnappingMode > mSnappingActions; // map the snapping mode actions with their values

private:
#ifdef SIP_RUN
Expand Down
46 changes: 11 additions & 35 deletions src/gui/qgsmapmouseevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct EdgesOnlyFilter : public QgsPointLocator::MatchFilter

QgsMapMouseEvent::QgsMapMouseEvent( QgsMapCanvas *mapCanvas, QMouseEvent *event )
: QMouseEvent( event->type(), event->pos(), event->button(), event->buttons(), event->modifiers() )
, mSnappingMode( NoSnapping )
, mHasCachedSnapResult( false )
, mOriginalMapPoint( mapCanvas ? mapCanvas->mapSettings().mapToPixel().toMapCoordinates( event->pos() ) : QgsPointXY() )
, mMapPoint( mOriginalMapPoint )
, mPixelPoint( event->pos() )
Expand All @@ -39,47 +39,24 @@ QgsMapMouseEvent::QgsMapMouseEvent( QgsMapCanvas *mapCanvas, QMouseEvent *event

QgsMapMouseEvent::QgsMapMouseEvent( QgsMapCanvas *mapCanvas, QEvent::Type type, QPoint pos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers )
: QMouseEvent( type, pos, button, buttons, modifiers )
, mSnappingMode( NoSnapping )
, mHasCachedSnapResult( false )
, mOriginalMapPoint( mapCanvas ? mapCanvas->mapSettings().mapToPixel().toMapCoordinates( pos ) : QgsPointXY() )
, mMapPoint( mOriginalMapPoint )
, mPixelPoint( pos )
, mMapCanvas( mapCanvas )
{
}

QgsPointXY QgsMapMouseEvent::snapPoint( SnappingMode snappingMode )
QgsPointXY QgsMapMouseEvent::snapPoint()
{
// Use cached result
if ( mSnappingMode == snappingMode )
if ( mHasCachedSnapResult )
return mMapPoint;

mSnappingMode = snappingMode;

if ( snappingMode == NoSnapping )
{
mMapPoint = mOriginalMapPoint;
mPixelPoint = pos();
return mMapPoint;
}
mHasCachedSnapResult = true;

QgsSnappingUtils *snappingUtils = mMapCanvas->snappingUtils();
if ( snappingMode == SnapAllLayers )
{
QgsSnappingConfig canvasConfig = snappingUtils->config();
QgsSnappingConfig localConfig = snappingUtils->config();

localConfig.setMode( QgsSnappingConfig::AllLayers );
localConfig.setType( QgsSnappingConfig::VertexAndSegment );
snappingUtils->setConfig( localConfig );

mSnapMatch = snappingUtils->snapToMap( mMapPoint );

snappingUtils->setConfig( canvasConfig );
}
else
{
mSnapMatch = snappingUtils->snapToMap( mMapPoint );
}
mSnapMatch = snappingUtils->snapToMap( mMapPoint );

if ( mSnapMatch.isValid() )
{
Expand All @@ -95,28 +72,27 @@ QgsPointXY QgsMapMouseEvent::snapPoint( SnappingMode snappingMode )
return mMapPoint;
}

QList<QgsPointXY> QgsMapMouseEvent::snapSegment( SnappingMode snappingMode, bool *snapped, bool allLayers ) const
QList<QgsPointXY> QgsMapMouseEvent::snapSegment( bool *snapped, bool allLayers ) const
{
QList<QgsPointXY> segment;
QgsPointXY pt1, pt2;

// If there's a cached snapping result we use it
if ( snappingMode == mSnappingMode && mSnapMatch.hasEdge() )
if ( mHasCachedSnapResult && mSnapMatch.hasEdge() )
{
mSnapMatch.edgePoints( pt1, pt2 );
segment << pt1 << pt2;
}

else if ( snappingMode != NoSnapping )
else
{
QgsPointLocator::Match match;
if ( snappingMode == SnapProjectConfig && !allLayers )
if ( !allLayers )
{
// run snapToMap with only segments
EdgesOnlyFilter filter;
match = mMapCanvas->snappingUtils()->snapToMap( mOriginalMapPoint, &filter );
}
else if ( snappingMode == SnapAllLayers || allLayers )
else
{
// run snapToMap with only edges on all layers
QgsSnappingUtils *snappingUtils = mMapCanvas->snappingUtils();
Expand Down
15 changes: 4 additions & 11 deletions src/gui/qgsmapmouseevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ class GUI_EXPORT QgsMapMouseEvent : public QMouseEvent

public:

enum SnappingMode
{
NoSnapping,
SnapProjectConfig, //!< Snap according to the configuration set in the snapping settings
SnapAllLayers, //!< Snap to all rendered layers (tolerance and type from defaultSettings())
};

/**
* Creates a new QgsMapMouseEvent. Should only be required to be called from the QgsMapCanvas.
*
Expand All @@ -77,17 +70,16 @@ class GUI_EXPORT QgsMapMouseEvent : public QMouseEvent
* \brief snapPoint will snap the points using the map canvas snapping utils configuration
* \note if snapping did not succeeded, the map point will be reset to its original position
*/
QgsPointXY snapPoint( SnappingMode snappingMode );
QgsPointXY snapPoint();

/**
* Returns the first snapped segment. If the cached snapped match is a segment, it will simply return it.
* Otherwise it will try to snap a segment according to the event's snapping mode. In this case the cache
* will not be overwritten.
* \param snappingMode Specify if the default project settings or all layers should be used for snapping
* \param snapped if given, determines if a segment has been snapped
* \param allLayers if true, override snapping mode
*/
QList<QgsPointXY> snapSegment( SnappingMode snappingMode, bool *snapped = nullptr, bool allLayers = false ) const;
QList<QgsPointXY> snapSegment( bool *snapped = nullptr, bool allLayers = false ) const;

/**
* Returns true if there is a snapped point cached.
Expand Down Expand Up @@ -144,7 +136,8 @@ class GUI_EXPORT QgsMapMouseEvent : public QMouseEvent

QPoint mapToPixelCoordinates( const QgsPointXY &point );

SnappingMode mSnappingMode;
//! Whether snapPoint() was already called
bool mHasCachedSnapResult;

//! Unsnapped point in map coordinates.
QgsPointXY mOriginalMapPoint;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsmaptooladvanceddigitizing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ void QgsMapToolAdvancedDigitizing::cadPointChanged( const QgsPointXY &point )
void QgsMapToolAdvancedDigitizing::snap( QgsMapMouseEvent *e )
{
if ( !mCadDockWidget->cadEnabled() )
e->snapPoint( QgsMapMouseEvent::SnapProjectConfig );
e->snapPoint();
}

0 comments on commit 0be0a4e

Please sign in to comment.