Skip to content

Commit

Permalink
When we use the node tool we able to select a feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Nov 5, 2015
1 parent 2f9db5a commit 580f70a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
49 changes: 42 additions & 7 deletions src/app/nodetool/qgsmaptoolnodetool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "qgsproject.h"
#include "qgsgeometryrubberband.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"

#include <QMouseEvent>
#include <QRubberBand>
Expand Down Expand Up @@ -168,6 +169,22 @@ void QgsMapToolNodeTool::canvasMoveEvent( QgsMapMouseEvent* e )
}
}

QgsFeature QgsMapToolNodeTool::getFeatureAtPoint( QgsMapMouseEvent* e )
{
QgsFeature feature;
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
if ( vlayer == NULL ) {
return feature;
}

QgsFeatureRequest request;
request.setFilterRect( QgsRectangle( e->mapPoint().x(), e->mapPoint().y(), e->mapPoint().x(), e->mapPoint().y() ) );
QgsFeatureIterator features = vlayer->getFeatures( request );
features.nextFeature( feature );

return feature;
}

void QgsMapToolNodeTool::canvasPressEvent( QgsMapMouseEvent* e )
{
QgsDebugCall;
Expand All @@ -186,14 +203,23 @@ void QgsMapToolNodeTool::canvasPressEvent( QgsMapMouseEvent* e )

if ( snapResults.size() < 1 )
{
emit messageEmitted( tr( "could not snap to a segment on the current layer." ) );
return;
QgsFeature feature = getFeatureAtPoint( e );
if ( feature.geometry() == NULL ) {
emit messageEmitted(tr( "could not snap to a segment on the current layer." ) );
return;
}
else {
// remove previous warning
emit messageDiscarded();
mSelectedFeature = new QgsSelectedFeature( feature.id(), vlayer, mCanvas );
}
}
else {
// remove previous warning
emit messageDiscarded();

// remove previous warning
emit messageDiscarded();

mSelectedFeature = new QgsSelectedFeature( snapResults[0].snappedAtGeometry, vlayer, mCanvas );
mSelectedFeature = new QgsSelectedFeature( snapResults[0].snappedAtGeometry, vlayer, mCanvas );
}
connect( QgisApp::instance()->layerTreeView(), SIGNAL( currentLayerChanged( QgsMapLayer* ) ), this, SLOT( currentLayerChanged( QgsMapLayer* ) ) );
connect( mSelectedFeature, SIGNAL( destroyed() ), this, SLOT( selectedFeatureDestroyed() ) );
connect( vlayer, SIGNAL( editingStopped() ), this, SLOT( editingToggled() ) );
Expand Down Expand Up @@ -254,7 +280,7 @@ void QgsMapToolNodeTool::canvasPressEvent( QgsMapMouseEvent* e )
// no near vertex to snap
// unless point layer, try segment
if ( !mIsPoint )
mSnapper.snapToCurrentLayer( e->pos(), snapResults, QgsSnapper::SnapToSegment, tol );
mSnapper.snapToCurrentLayer( e->pos(), snapResults, QgsSnapper::SnapToSegment, tol, QList<QgsPoint>(), true );

if ( snapResults.size() > 0 )
{
Expand Down Expand Up @@ -310,6 +336,15 @@ void QgsMapToolNodeTool::canvasPressEvent( QgsMapMouseEvent* e )
else if ( !ctrlModifier )
{
mSelectedFeature->deselectAllVertexes();

QgsFeature feature = getFeatureAtPoint( e );
if ( feature.geometry() == NULL ) {
return;
}
else {
mAnother = feature.id();
mSelectAnother = true;
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/app/nodetool/qgsmaptoolnodetool.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class QgsMapToolNodeTool: public QgsMapToolEdit
void editingToggled();

private:
/**
* Get the feature on the mouse click
*/
QgsFeature getFeatureAtPoint( QgsMapMouseEvent* e );

/**
* Deletes the rubber band pointers and clears mRubberBands
*/
Expand Down
9 changes: 7 additions & 2 deletions src/gui/qgsmapcanvassnapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ void QgsMapCanvasSnapper::setMapCanvas( QgsMapCanvas* canvas )
int QgsMapCanvasSnapper::snapToCurrentLayer( const QPoint& p, QList<QgsSnappingResult>& results,
QgsSnapper::SnappingType snap_to,
double snappingTol,
const QList<QgsPoint>& excludePoints )
const QList<QgsPoint>& excludePoints,
bool allResutInTolerance )
{
results.clear();

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

//topological editing on?
int topologicalEditing = QgsProject::instance()->readNumEntry( "Digitizing", "/TopologicalEditing", 0 );
if ( topologicalEditing == 0 )
if ( allResutInTolerance )
{
mSnapper->setSnapMode( QgsSnapper::SnapWithResultsWithinTolerances);
}
else if ( topologicalEditing == 0 )
{
mSnapper->setSnapMode( QgsSnapper::SnapWithOneResult );
}
Expand Down
6 changes: 4 additions & 2 deletions src/gui/qgsmapcanvassnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ class GUI_EXPORT QgsMapCanvasSnapper
@param results list to which the results are appended
@param snap_to snap to vertex or to segment
@param snappingTol snapping tolerance. -1 means that the search radius for vertex edits is taken
@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*/
int snapToCurrentLayer( const QPoint& p, QList<QgsSnappingResult>& results, QgsSnapper::SnappingType snap_to, double snappingTol = -1, const QList<QgsPoint>& excludePoints = QList<QgsPoint>() );
@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
@param allResutInTolerance return all thew results in the tollerance
*/
int snapToCurrentLayer( const QPoint& p, QList<QgsSnappingResult>& results, QgsSnapper::SnappingType snap_to, double snappingTol = -1, const QList<QgsPoint>& excludePoints = QList<QgsPoint>(), bool allResutInTolerance = false );
/** Snaps to the background layers. This method is useful to align the features of the
edited layers to those of other layers (as described in the project properties).
Uses snap mode QgsSnapper::SnapWithOneResult. Therefore, only the
Expand Down

0 comments on commit 580f70a

Please sign in to comment.