Skip to content

Commit

Permalink
Node tool: Declutter a bit the mouse press event logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jan 20, 2015
1 parent dbe830b commit 006edcb
Showing 1 changed file with 49 additions and 75 deletions.
124 changes: 49 additions & 75 deletions src/app/nodetool/qgsmaptoolnodetool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,105 +395,79 @@ void QgsMapToolNodeTool::canvasPressEvent( QMouseEvent * e )
QgsVectorLayer *vlayer = mSelectedFeature->vlayer();
Q_ASSERT( vlayer );

// some feature already selected
QgsPoint layerCoordPoint = toLayerCoordinates( vlayer, e->pos() );
// try to find a piece of currently selected geometry
QgsFeatureIdFilter filterFid( mSelectedFeature->featureId() );
QgsPointLocator::Match mSel = mCanvas->snappingUtils()->snapToCurrentLayer( e->pos(), QgsPointLocator::Vertex | QgsPointLocator::Edge, &filterFid );

double tol = QgsTolerance::vertexSearchRadius( vlayer, mCanvas->mapSettings() );

// get geometry and find if snapping is near it
int atVertex, beforeVertex, afterVertex;
double dist;
QgsPoint closestLayerVertex = mSelectedFeature->geometry()->closestVertex( layerCoordPoint, atVertex, beforeVertex, afterVertex, dist );
dist = sqrt( dist );

QgsPointLocator::Match m = mCanvas->snappingUtils()->snapToCurrentLayer( e->pos(), QgsPointLocator::Vertex );
if ( dist <= tol )
if ( mSel.hasVertex() )
{
// some vertex selected
// mouse pressed on a vertex:
// - if clicked on already selected vertex - deselect it
// - if clicked on vertex that is not selected - select it
// - if clicked with CTRL - invert selection state of the vertex
// - if pressed+dragging on already selected vertex - will move selected vertices

mMoving = true;
mClosestMapVertex = toMapCoordinates( vlayer, closestLayerVertex );
if ( mMoving )
mClosestMapVertex = mSel.point();
int atVertex = mSel.vertexIndex();

if ( mSelectedFeature->isSelected( atVertex ) )
{
if ( mSelectedFeature->isSelected( atVertex ) )
{
mDeselectOnRelease = atVertex;
}
else if ( mCtrl )
{
mSelectedFeature->invertVertexSelection( atVertex );
}
else
{
mSelectedFeature->deselectAllVertexes();
mSelectedFeature->selectVertex( atVertex );
}
mDeselectOnRelease = atVertex;
}
else if ( mCtrl )
{
mSelectedFeature->invertVertexSelection( atVertex );
}
else
{
// select another feature
mAnother = m.featureId();
mSelectAnother = true;
mSelectedFeature->deselectAllVertexes();
mSelectedFeature->selectVertex( atVertex );
}
}
else
else if ( mSel.hasEdge() )
{
// no near vertex to snap
// unless point layer, try segment
QgsPointLocator::Match m2;
QgsFeatureIdFilter filterFid( mSelectedFeature->featureId() );
if ( mIsPoint )
m2 = mCanvas->snappingUtils()->snapToCurrentLayer( e->pos(), QgsPointLocator::Vertex, &filterFid );
else
m2 = mCanvas->snappingUtils()->snapToCurrentLayer( e->pos(), QgsPointLocator::Edge, &filterFid );
// mouse pressed on an edge:
// - if clicked - select just vertices of that edge
// - if clicked with CTRL - invert selection state of vertices of the edge
// - if pressed+dragging - will move vertices of the edge

mMoving = true;
QgsPoint p1, p2;
mSel.edgePoints( p1, p2 );
mClosestMapVertex = p1.sqrDist( mSel.point() ) < p2.sqrDist( mSel.point() ) ? p1 : p2;

if ( m2.isValid() )
if ( !mCtrl )
{
mAnother = 0;
mSelectAnother = false;
mSelectedFeature->deselectAllVertexes();
mSelectedFeature->selectVertex( mSel.vertexIndex() + 1 );
mSelectedFeature->selectVertex( mSel.vertexIndex() );
}
else
{
mAnother = m.featureId();
mSelectAnother = true;
mSelectedFeature->invertVertexSelection( mSel.vertexIndex() + 1 );
mSelectedFeature->invertVertexSelection( mSel.vertexIndex() );
}

if ( !mSelectAnother )
{
mMoving = true;
mClosestMapVertex = toMapCoordinates( vlayer, closestLayerVertex );
}
else
{
// nothing from the feature is acceptable:
// - if clicked - try to select a different feature. if nothing is around, at least deselect all vertices

if ( mIsPoint )
{
if ( !mCtrl )
{
mSelectedFeature->deselectAllVertexes();
mSelectedFeature->selectVertex( m2.vertexIndex() );
}
else
{
mSelectedFeature->invertVertexSelection( m2.vertexIndex() );
}
}
else
{
if ( !mCtrl )
{
mSelectedFeature->deselectAllVertexes();
mSelectedFeature->selectVertex( m2.vertexIndex() + 1 );
mSelectedFeature->selectVertex( m2.vertexIndex() );
}
else
{
mSelectedFeature->invertVertexSelection( m2.vertexIndex() + 1 );
mSelectedFeature->invertVertexSelection( m2.vertexIndex() );
}
}
QgsPointLocator::Match m = mCanvas->snappingUtils()->snapToCurrentLayer( e->pos(), QgsPointLocator::Vertex | QgsPointLocator::Edge );
if ( m.isValid() )
{
// if this will be just a click, on release we will select this new feature
mAnother = m.featureId();
mSelectAnother = true;
}
else if ( !mCtrl )
{
mSelectedFeature->deselectAllVertexes();
}
}

}
}

Expand Down

0 comments on commit 006edcb

Please sign in to comment.