@@ -395,105 +395,79 @@ void QgsMapToolNodeTool::canvasPressEvent( QMouseEvent * e )
395395 QgsVectorLayer *vlayer = mSelectedFeature ->vlayer ();
396396 Q_ASSERT ( vlayer );
397397
398- // some feature already selected
399- QgsPoint layerCoordPoint = toLayerCoordinates ( vlayer, e->pos () );
398+ // try to find a piece of currently selected geometry
399+ QgsFeatureIdFilter filterFid ( mSelectedFeature ->featureId () );
400+ QgsPointLocator::Match mSel = mCanvas ->snappingUtils ()->snapToCurrentLayer ( e->pos (), QgsPointLocator::Vertex | QgsPointLocator::Edge, &filterFid );
400401
401- double tol = QgsTolerance::vertexSearchRadius ( vlayer, mCanvas ->mapSettings () );
402-
403- // get geometry and find if snapping is near it
404- int atVertex, beforeVertex, afterVertex;
405- double dist;
406- QgsPoint closestLayerVertex = mSelectedFeature ->geometry ()->closestVertex ( layerCoordPoint, atVertex, beforeVertex, afterVertex, dist );
407- dist = sqrt ( dist );
408-
409- QgsPointLocator::Match m = mCanvas ->snappingUtils ()->snapToCurrentLayer ( e->pos (), QgsPointLocator::Vertex );
410- if ( dist <= tol )
402+ if ( mSel .hasVertex () )
411403 {
412- // some vertex selected
404+ // mouse pressed on a vertex:
405+ // - if clicked on already selected vertex - deselect it
406+ // - if clicked on vertex that is not selected - select it
407+ // - if clicked with CTRL - invert selection state of the vertex
408+ // - if pressed+dragging on already selected vertex - will move selected vertices
409+
413410 mMoving = true ;
414- mClosestMapVertex = toMapCoordinates ( vlayer, closestLayerVertex );
415- if ( mMoving )
411+ mClosestMapVertex = mSel .point ();
412+ int atVertex = mSel .vertexIndex ();
413+
414+ if ( mSelectedFeature ->isSelected ( atVertex ) )
416415 {
417- if ( mSelectedFeature ->isSelected ( atVertex ) )
418- {
419- mDeselectOnRelease = atVertex;
420- }
421- else if ( mCtrl )
422- {
423- mSelectedFeature ->invertVertexSelection ( atVertex );
424- }
425- else
426- {
427- mSelectedFeature ->deselectAllVertexes ();
428- mSelectedFeature ->selectVertex ( atVertex );
429- }
416+ mDeselectOnRelease = atVertex;
417+ }
418+ else if ( mCtrl )
419+ {
420+ mSelectedFeature ->invertVertexSelection ( atVertex );
430421 }
431422 else
432423 {
433- // select another feature
434- mAnother = m.featureId ();
435- mSelectAnother = true ;
424+ mSelectedFeature ->deselectAllVertexes ();
425+ mSelectedFeature ->selectVertex ( atVertex );
436426 }
437427 }
438- else
428+ else if ( mSel . hasEdge () )
439429 {
440- // no near vertex to snap
441- // unless point layer, try segment
442- QgsPointLocator::Match m2;
443- QgsFeatureIdFilter filterFid ( mSelectedFeature ->featureId () );
444- if ( mIsPoint )
445- m2 = mCanvas ->snappingUtils ()->snapToCurrentLayer ( e->pos (), QgsPointLocator::Vertex, &filterFid );
446- else
447- m2 = mCanvas ->snappingUtils ()->snapToCurrentLayer ( e->pos (), QgsPointLocator::Edge, &filterFid );
430+ // mouse pressed on an edge:
431+ // - if clicked - select just vertices of that edge
432+ // - if clicked with CTRL - invert selection state of vertices of the edge
433+ // - if pressed+dragging - will move vertices of the edge
434+
435+ mMoving = true ;
436+ QgsPoint p1, p2;
437+ mSel .edgePoints ( p1, p2 );
438+ mClosestMapVertex = p1.sqrDist ( mSel .point () ) < p2.sqrDist ( mSel .point () ) ? p1 : p2;
448439
449- if ( m2. isValid () )
440+ if ( ! mCtrl )
450441 {
451- mAnother = 0 ;
452- mSelectAnother = false ;
442+ mSelectedFeature ->deselectAllVertexes ();
443+ mSelectedFeature ->selectVertex ( mSel .vertexIndex () + 1 );
444+ mSelectedFeature ->selectVertex ( mSel .vertexIndex () );
453445 }
454446 else
455447 {
456- mAnother = m. featureId ( );
457- mSelectAnother = true ;
448+ mSelectedFeature -> invertVertexSelection ( mSel . vertexIndex () + 1 );
449+ mSelectedFeature -> invertVertexSelection ( mSel . vertexIndex () ) ;
458450 }
459451
460- if ( !mSelectAnother )
461- {
462- mMoving = true ;
463- mClosestMapVertex = toMapCoordinates ( vlayer, closestLayerVertex );
452+ }
453+ else
454+ {
455+ // nothing from the feature is acceptable:
456+ // - if clicked - try to select a different feature. if nothing is around, at least deselect all vertices
464457
465- if ( mIsPoint )
466- {
467- if ( !mCtrl )
468- {
469- mSelectedFeature ->deselectAllVertexes ();
470- mSelectedFeature ->selectVertex ( m2.vertexIndex () );
471- }
472- else
473- {
474- mSelectedFeature ->invertVertexSelection ( m2.vertexIndex () );
475- }
476- }
477- else
478- {
479- if ( !mCtrl )
480- {
481- mSelectedFeature ->deselectAllVertexes ();
482- mSelectedFeature ->selectVertex ( m2.vertexIndex () + 1 );
483- mSelectedFeature ->selectVertex ( m2.vertexIndex () );
484- }
485- else
486- {
487- mSelectedFeature ->invertVertexSelection ( m2.vertexIndex () + 1 );
488- mSelectedFeature ->invertVertexSelection ( m2.vertexIndex () );
489- }
490- }
458+ QgsPointLocator::Match m = mCanvas ->snappingUtils ()->snapToCurrentLayer ( e->pos (), QgsPointLocator::Vertex | QgsPointLocator::Edge );
459+ if ( m.isValid () )
460+ {
461+ // if this will be just a click, on release we will select this new feature
462+ mAnother = m.featureId ();
463+ mSelectAnother = true ;
491464 }
492465 else if ( !mCtrl )
493466 {
494467 mSelectedFeature ->deselectAllVertexes ();
495468 }
496469 }
470+
497471 }
498472}
499473
0 commit comments