Skip to content

Commit 580f70a

Browse files
committedNov 5, 2015
When we use the node tool we able to select a feature
1 parent 2f9db5a commit 580f70a

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed
 

‎src/app/nodetool/qgsmaptoolnodetool.cpp

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgsproject.h"
2727
#include "qgsgeometryrubberband.h"
2828
#include "qgsvectorlayer.h"
29+
#include "qgsvectordataprovider.h"
2930

3031
#include <QMouseEvent>
3132
#include <QRubberBand>
@@ -168,6 +169,22 @@ void QgsMapToolNodeTool::canvasMoveEvent( QgsMapMouseEvent* e )
168169
}
169170
}
170171

172+
QgsFeature QgsMapToolNodeTool::getFeatureAtPoint( QgsMapMouseEvent* e )
173+
{
174+
QgsFeature feature;
175+
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
176+
if ( vlayer == NULL ) {
177+
return feature;
178+
}
179+
180+
QgsFeatureRequest request;
181+
request.setFilterRect( QgsRectangle( e->mapPoint().x(), e->mapPoint().y(), e->mapPoint().x(), e->mapPoint().y() ) );
182+
QgsFeatureIterator features = vlayer->getFeatures( request );
183+
features.nextFeature( feature );
184+
185+
return feature;
186+
}
187+
171188
void QgsMapToolNodeTool::canvasPressEvent( QgsMapMouseEvent* e )
172189
{
173190
QgsDebugCall;
@@ -186,14 +203,23 @@ void QgsMapToolNodeTool::canvasPressEvent( QgsMapMouseEvent* e )
186203

187204
if ( snapResults.size() < 1 )
188205
{
189-
emit messageEmitted( tr( "could not snap to a segment on the current layer." ) );
190-
return;
206+
QgsFeature feature = getFeatureAtPoint( e );
207+
if ( feature.geometry() == NULL ) {
208+
emit messageEmitted(tr( "could not snap to a segment on the current layer." ) );
209+
return;
210+
}
211+
else {
212+
// remove previous warning
213+
emit messageDiscarded();
214+
mSelectedFeature = new QgsSelectedFeature( feature.id(), vlayer, mCanvas );
215+
}
191216
}
217+
else {
218+
// remove previous warning
219+
emit messageDiscarded();
192220

193-
// remove previous warning
194-
emit messageDiscarded();
195-
196-
mSelectedFeature = new QgsSelectedFeature( snapResults[0].snappedAtGeometry, vlayer, mCanvas );
221+
mSelectedFeature = new QgsSelectedFeature( snapResults[0].snappedAtGeometry, vlayer, mCanvas );
222+
}
197223
connect( QgisApp::instance()->layerTreeView(), SIGNAL( currentLayerChanged( QgsMapLayer* ) ), this, SLOT( currentLayerChanged( QgsMapLayer* ) ) );
198224
connect( mSelectedFeature, SIGNAL( destroyed() ), this, SLOT( selectedFeatureDestroyed() ) );
199225
connect( vlayer, SIGNAL( editingStopped() ), this, SLOT( editingToggled() ) );
@@ -254,7 +280,7 @@ void QgsMapToolNodeTool::canvasPressEvent( QgsMapMouseEvent* e )
254280
// no near vertex to snap
255281
// unless point layer, try segment
256282
if ( !mIsPoint )
257-
mSnapper.snapToCurrentLayer( e->pos(), snapResults, QgsSnapper::SnapToSegment, tol );
283+
mSnapper.snapToCurrentLayer( e->pos(), snapResults, QgsSnapper::SnapToSegment, tol, QList<QgsPoint>(), true );
258284

259285
if ( snapResults.size() > 0 )
260286
{
@@ -310,6 +336,15 @@ void QgsMapToolNodeTool::canvasPressEvent( QgsMapMouseEvent* e )
310336
else if ( !ctrlModifier )
311337
{
312338
mSelectedFeature->deselectAllVertexes();
339+
340+
QgsFeature feature = getFeatureAtPoint( e );
341+
if ( feature.geometry() == NULL ) {
342+
return;
343+
}
344+
else {
345+
mAnother = feature.id();
346+
mSelectAnother = true;
347+
}
313348
}
314349
}
315350
}

‎src/app/nodetool/qgsmaptoolnodetool.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ class QgsMapToolNodeTool: public QgsMapToolEdit
6161
void editingToggled();
6262

6363
private:
64+
/**
65+
* Get the feature on the mouse click
66+
*/
67+
QgsFeature getFeatureAtPoint( QgsMapMouseEvent* e );
68+
6469
/**
6570
* Deletes the rubber band pointers and clears mRubberBands
6671
*/

‎src/gui/qgsmapcanvassnapper.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ void QgsMapCanvasSnapper::setMapCanvas( QgsMapCanvas* canvas )
6262
int QgsMapCanvasSnapper::snapToCurrentLayer( const QPoint& p, QList<QgsSnappingResult>& results,
6363
QgsSnapper::SnappingType snap_to,
6464
double snappingTol,
65-
const QList<QgsPoint>& excludePoints )
65+
const QList<QgsPoint>& excludePoints,
66+
bool allResutInTolerance )
6667
{
6768
results.clear();
6869

@@ -71,7 +72,11 @@ int QgsMapCanvasSnapper::snapToCurrentLayer( const QPoint& p, QList<QgsSnappingR
7172

7273
//topological editing on?
7374
int topologicalEditing = QgsProject::instance()->readNumEntry( "Digitizing", "/TopologicalEditing", 0 );
74-
if ( topologicalEditing == 0 )
75+
if ( allResutInTolerance )
76+
{
77+
mSnapper->setSnapMode( QgsSnapper::SnapWithResultsWithinTolerances);
78+
}
79+
else if ( topologicalEditing == 0 )
7580
{
7681
mSnapper->setSnapMode( QgsSnapper::SnapWithOneResult );
7782
}

‎src/gui/qgsmapcanvassnapper.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ class GUI_EXPORT QgsMapCanvasSnapper
5050
@param results list to which the results are appended
5151
@param snap_to snap to vertex or to segment
5252
@param snappingTol snapping tolerance. -1 means that the search radius for vertex edits is taken
53-
@param excludePoints a list with (map coordinate) points that should be excluded in the snapping result. Useful e.g. for vertex moves where a vertex should not be snapped to its original position*/
54-
int snapToCurrentLayer( const QPoint& p, QList<QgsSnappingResult>& results, QgsSnapper::SnappingType snap_to, double snappingTol = -1, const QList<QgsPoint>& excludePoints = QList<QgsPoint>() );
53+
@param excludePoints a list with (map coordinate) points that should be excluded in the snapping result. Useful e.g. for vertex moves where a vertex should not be snapped to its original position
54+
@param allResutInTolerance return all thew results in the tollerance
55+
*/
56+
int snapToCurrentLayer( const QPoint& p, QList<QgsSnappingResult>& results, QgsSnapper::SnappingType snap_to, double snappingTol = -1, const QList<QgsPoint>& excludePoints = QList<QgsPoint>(), bool allResutInTolerance = false );
5557
/** Snaps to the background layers. This method is useful to align the features of the
5658
edited layers to those of other layers (as described in the project properties).
5759
Uses snap mode QgsSnapper::SnapWithOneResult. Therefore, only the

0 commit comments

Comments
 (0)
Please sign in to comment.