Skip to content
Permalink
Browse files

Merge pull request #2595 from SebDieBln/ImproveDigitizingTools

Improve digitizing tools for circular strings
  • Loading branch information
mhugent committed Jan 12, 2016
2 parents 3febba2 + e45603a commit 47ae05fbdb6a80bbdf8ad4f07a7b6dfaeebf61ce
@@ -28,6 +28,7 @@ QgsMapToolAddCircularString::QgsMapToolAddCircularString( QgsMapToolCapture* par
: QgsMapToolCapture( canvas, QgisApp::instance()->cadDockWidget(), mode )
, mParentTool( parentTool )
, mRubberBand( nullptr )
, mTempRubberBand( nullptr )
, mShowCenterPointRubberBand( false )
, mCenterPointRubberBand( nullptr )
{
@@ -41,6 +42,7 @@ QgsMapToolAddCircularString::QgsMapToolAddCircularString( QgsMapCanvas* canvas )
: QgsMapToolCapture( canvas, QgisApp::instance()->cadDockWidget() )
, mParentTool( nullptr )
, mRubberBand( nullptr )
, mTempRubberBand( nullptr )
, mShowCenterPointRubberBand( false )
, mCenterPointRubberBand( nullptr )
{
@@ -53,6 +55,7 @@ QgsMapToolAddCircularString::QgsMapToolAddCircularString( QgsMapCanvas* canvas )
QgsMapToolAddCircularString::~QgsMapToolAddCircularString()
{
delete mRubberBand;
delete mTempRubberBand;
removeCenterPointRubberBand();
}

@@ -80,9 +83,20 @@ void QgsMapToolAddCircularString::keyPressEvent( QKeyEvent* e )
if ( e && e->key() == Qt::Key_R )
{
mShowCenterPointRubberBand = true;

createCenterPointRubberBand();
}

if ( e && e->key() == Qt::Key_Escape )
{
mPoints.clear();
delete mRubberBand;
mRubberBand = nullptr;
delete mTempRubberBand;
mTempRubberBand = nullptr;
removeCenterPointRubberBand();
if ( mParentTool )
mParentTool->keyPressEvent( e );
}
}

void QgsMapToolAddCircularString::keyReleaseEvent( QKeyEvent* e )
@@ -117,6 +131,8 @@ void QgsMapToolAddCircularString::deactivate()
mPoints.clear();
delete mRubberBand;
mRubberBand = nullptr;
delete mTempRubberBand;
mTempRubberBand = nullptr;
removeCenterPointRubberBand();
QgsMapToolCapture::deactivate();
}
@@ -126,6 +142,32 @@ void QgsMapToolAddCircularString::activate()
if ( mParentTool )
{
mParentTool->deleteTempRubberBand();
if ( mPoints.isEmpty() )
{
// if the parent tool has a curve, use its last point as the first point in this curve
const QgsCompoundCurveV2* compoundCurve = mParentTool->captureCurve();
if ( compoundCurve && compoundCurve->nCurves() > 0 )
{
const QgsCurveV2* curve = compoundCurve->curveAt( compoundCurve->nCurves() - 1 );
if ( curve )
{
//mParentTool->captureCurve() is in layer coordinates, but we need map coordinates
QgsPointV2 endPointLayerCoord = curve->endPoint();
QgsPoint mapPoint = toMapCoordinates( mCanvas->currentLayer(), QgsPoint( endPointLayerCoord.x(), endPointLayerCoord.y() ) );
mPoints.append( QgsPointV2( mapPoint ) );
if ( !mTempRubberBand )
{
mTempRubberBand = createGeometryRubberBand(( mode() == CapturePolygon ) ? QGis::Polygon : QGis::Line, true );
mTempRubberBand->show();
}
QgsCircularStringV2* c = new QgsCircularStringV2();
QList< QgsPointV2 > rubberBandPoints = mPoints;
rubberBandPoints.append( QgsPointV2( mapPoint ) );
c->setPoints( rubberBandPoints );
mTempRubberBand->setGeometry( c );
}
}
}
}
QgsMapToolCapture::activate();
}
@@ -140,15 +182,12 @@ void QgsMapToolAddCircularString::createCenterPointRubberBand()
mCenterPointRubberBand = createGeometryRubberBand( QGis::Polygon );
mCenterPointRubberBand->show();

if ( mRubberBand )
if ( mTempRubberBand )
{
const QgsAbstractGeometryV2* rubberBandGeom = mRubberBand->geometry();
const QgsAbstractGeometryV2* rubberBandGeom = mTempRubberBand->geometry();
if ( rubberBandGeom )
{
QgsVertexId idx;
idx.part = 0;
idx.ring = 0;
idx.vertex = mPoints.size();
QgsVertexId idx( 0, 0, 2 );
QgsPointV2 pt = rubberBandGeom->vertexAt( idx );
updateCenterPointRubberBand( pt );
}
@@ -175,17 +214,17 @@ void QgsMapToolAddCircularString::updateCenterPointRubberBand( const QgsPointV2&
csPoints.append( pt );
cs->setPoints( csPoints );

double centerX, centerY;
QgsPointV2 center;
double radius;
QgsGeometryUtils::circleCenterRadius( csPoints.at( 0 ), csPoints.at( 1 ), csPoints.at( 2 ), radius, centerX, centerY );
QgsGeometryUtils::circleCenterRadius( csPoints.at( 0 ), csPoints.at( 1 ), csPoints.at( 2 ), radius, center.rx(), center.ry() );

QgsLineStringV2* segment1 = new QgsLineStringV2();
segment1->addVertex( QgsPointV2( centerX, centerY ) );
segment1->addVertex( center );
segment1->addVertex( csPoints.at( 0 ) );

QgsLineStringV2* segment2 = new QgsLineStringV2();
segment2->addVertex( csPoints.at( 2 ) );
segment2->addVertex( QgsPointV2( centerX, centerY ) );
segment2->addVertex( center );

QgsCompoundCurveV2* cc = new QgsCompoundCurveV2();
cc->addCurve( segment1 );
@@ -40,10 +40,16 @@ class QgsMapToolAddCircularString: public QgsMapToolCapture
protected:
explicit QgsMapToolAddCircularString( QgsMapCanvas* canvas ); //forbidden

/** The parent map tool, e.g. the add feature tool.
* Completed circular strings will be added to this tool by calling its addCurve() method.
* */
QgsMapToolCapture* mParentTool;
/** Circular string points (in map coordinates)*/
QList< QgsPointV2 > mPoints;
//! The rubberband to show the already completed circular strings
QgsGeometryRubberBand* mRubberBand;
//! The rubberband to show the circular string currently working on
QgsGeometryRubberBand* mTempRubberBand;

//center point rubber band
bool mShowCenterPointRubberBand;
@@ -33,35 +33,31 @@ QgsMapToolCircularStringCurvePoint::~QgsMapToolCircularStringCurvePoint()

void QgsMapToolCircularStringCurvePoint::cadCanvasReleaseEvent( QgsMapMouseEvent* e )
{
QgsPointV2 mapPoint( e->mapPoint().x(), e->mapPoint().y() );
QgsPointV2 mapPoint( e->mapPoint() );

if ( e->button() == Qt::LeftButton )
{
if ( mPoints.size() < 1 ) //connection to vertex of previous line segment needed?
{
const QgsCompoundCurveV2* compoundCurve = mParentTool->captureCurve();
if ( compoundCurve )
{
if ( compoundCurve->nCurves() > 0 )
{
const QgsCurveV2* curve = compoundCurve->curveAt( compoundCurve->nCurves() - 1 );
if ( curve )
{
//mParentTool->captureCurve() is in layer coordinates, but we need map coordinates
QgsPointV2 endPointLayerCoord = curve->endPoint();
QgsPoint mapPoint = toMapCoordinates( mCanvas->currentLayer(), QgsPoint( endPointLayerCoord.x(), endPointLayerCoord.y() ) );
mPoints.append( QgsPointV2( mapPoint.x(), mapPoint.y() ) );
}
}
}
}
mPoints.append( mapPoint );
if ( !mCenterPointRubberBand && mShowCenterPointRubberBand )
{
createCenterPointRubberBand();
}

if ( mPoints.size() >= 1 )
if ( !mPoints.isEmpty() )
{
if ( !mTempRubberBand )
{
mTempRubberBand = createGeometryRubberBand(( mode() == CapturePolygon ) ? QGis::Polygon : QGis::Line, true );
mTempRubberBand->show();
}

QgsCircularStringV2* c = new QgsCircularStringV2();
QList< QgsPointV2 > rubberBandPoints = mPoints.mid( mPoints.size() - 1 - ( mPoints.size() + 1 ) % 2 );
rubberBandPoints.append( mapPoint );
c->setPoints( rubberBandPoints );
mTempRubberBand->setGeometry( c );
}
if ( mPoints.size() > 1 && mPoints.size() % 2 )
{
if ( !mRubberBand )
{
@@ -74,9 +70,6 @@ void QgsMapToolCircularStringCurvePoint::cadCanvasReleaseEvent( QgsMapMouseEvent
rubberBandPoints.append( mapPoint );
c->setPoints( rubberBandPoints );
mRubberBand->setGeometry( c );
}
if (( mPoints.size() ) % 2 == 1 )
{
removeCenterPointRubberBand();
}
}
@@ -92,14 +85,11 @@ void QgsMapToolCircularStringCurvePoint::cadCanvasReleaseEvent( QgsMapMouseEvent

void QgsMapToolCircularStringCurvePoint::cadCanvasMoveEvent( QgsMapMouseEvent* e )
{
QgsPointV2 mapPoint( e->mapPoint().x(), e->mapPoint().y() );
QgsVertexId idx;
idx.part = 0;
idx.ring = 0;
idx.vertex = mPoints.size();
if ( mRubberBand )
QgsPointV2 mapPoint( e->mapPoint() );
QgsVertexId idx( 0, 0, 1 + ( mPoints.size() + 1 ) % 2 );
if ( mTempRubberBand )
{
mRubberBand->moveVertex( idx, mapPoint );
mTempRubberBand->moveVertex( idx, mapPoint );
updateCenterPointRubberBand( mapPoint );
}
}

0 comments on commit 47ae05f

Please sign in to comment.
You can’t perform that action at this time.