Skip to content

Commit

Permalink
rotate closest feature if selection is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
vinayan authored and mhugent committed Jan 12, 2013
1 parent eef2efc commit 462c78e
Showing 1 changed file with 62 additions and 3 deletions.
65 changes: 62 additions & 3 deletions src/app/qgsmaptoolrotatefeature.cpp
Expand Up @@ -27,13 +27,15 @@
#include <limits>
#include <math.h>
#include "qgsvertexmarker.h"
#include "qgsmessagebar.h"

#define PI 3.14159265

QgsMapToolRotateFeature::QgsMapToolRotateFeature( QgsMapCanvas* canvas ): QgsMapToolEdit( canvas ), mRubberBand( 0 )
{
mRotation = 0;
mAnchorPoint = 0;
mCtrl = false;
}

QgsMapToolRotateFeature::~QgsMapToolRotateFeature()
Expand All @@ -46,6 +48,10 @@ void QgsMapToolRotateFeature::canvasMoveEvent( QMouseEvent * e )
{
if ( mCtrl == true )
{
if ( !mAnchorPoint )
{
return;
}
mAnchorPoint->setCenter( toMapCoordinates( e->pos() ) );
mStartPointMapCoords = toMapCoordinates( e->pos() );
mStPoint = e->pos();
Expand Down Expand Up @@ -87,9 +93,64 @@ void QgsMapToolRotateFeature::canvasPressEvent( QMouseEvent * e )
return;
}

QgsPoint layerCoords = toLayerCoordinates( vlayer, e->pos() );
double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapRenderer() );
QgsRectangle selectRect( layerCoords.x() - searchRadius, layerCoords.y() - searchRadius,
layerCoords.x() + searchRadius, layerCoords.y() + searchRadius );

if ( vlayer->selectedFeatureCount() == 0 )
{
return;
vlayer->select( QgsAttributeList(), selectRect, true );

//find the closest feature
QgsGeometry* pointGeometry = QgsGeometry::fromPoint( layerCoords );
if ( !pointGeometry )
{
return;
}

double minDistance = std::numeric_limits<double>::max();

QgsFeature cf;
QgsFeature f;
while ( vlayer->nextFeature( f ) )
{
if ( f.geometry() )
{
double currentDistance = pointGeometry->distance( *f.geometry() );
if ( currentDistance < minDistance )
{
minDistance = currentDistance;
cf = f;
}
}

}

delete pointGeometry;

if ( minDistance == std::numeric_limits<double>::max() )
{
return;
}

QgsRectangle bound = cf.geometry()->boundingBox();
mStartPointMapCoords = bound.center();

if ( !mAnchorPoint )
{
mAnchorPoint = new QgsVertexMarker( mCanvas );
}
mAnchorPoint->setIconType( QgsVertexMarker::ICON_CROSS );
mAnchorPoint->setCenter( mStartPointMapCoords );

mStPoint = toCanvasCoordinates( mStartPointMapCoords );

mRotatedFeatures.clear();
mRotatedFeatures << cf.id(); //todo: take the closest feature, not the first one...

mRubberBand = createRubberBand();
mRubberBand->setToGeometry( cf.geometry(), vlayer );
}
else
{
Expand Down Expand Up @@ -260,10 +321,8 @@ void QgsMapToolRotateFeature::activate()
mAnchorPoint = new QgsVertexMarker( mCanvas );
mAnchorPoint->setIconType( QgsVertexMarker::ICON_CROSS );
mAnchorPoint->setCenter( mStartPointMapCoords );
mAnchorPoint->acceptTouchEvents();

mStPoint = toCanvasCoordinates( mStartPointMapCoords );
mCtrl = false;

QgsMapTool::activate();
}
Expand Down

0 comments on commit 462c78e

Please sign in to comment.