Skip to content

Commit

Permalink
code cleanup and improved readability in circular maptools
Browse files Browse the repository at this point in the history
  • Loading branch information
SebDieBln committed Dec 21, 2015
1 parent 3cd9383 commit 6ef1706
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 38 deletions.
14 changes: 5 additions & 9 deletions src/app/qgsmaptooladdcircularstring.cpp
Expand Up @@ -83,7 +83,6 @@ void QgsMapToolAddCircularString::keyPressEvent( QKeyEvent* e )
if ( e && e->key() == Qt::Key_R )
{
mShowCenterPointRubberBand = true;

createCenterPointRubberBand();
}
}
Expand Down Expand Up @@ -176,10 +175,7 @@ void QgsMapToolAddCircularString::createCenterPointRubberBand()
const QgsAbstractGeometryV2* rubberBandGeom = mTempRubberBand->geometry();
if ( rubberBandGeom )
{
QgsVertexId idx;
idx.part = 0;
idx.ring = 0;
idx.vertex = 2;
QgsVertexId idx( 0, 0, 2 );
QgsPointV2 pt = rubberBandGeom->vertexAt( idx );
updateCenterPointRubberBand( pt );
}
Expand All @@ -206,17 +202,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 );
Expand Down
13 changes: 5 additions & 8 deletions src/app/qgsmaptoolcircularstringcurvepoint.cpp
Expand Up @@ -19,7 +19,7 @@ QgsMapToolCircularStringCurvePoint::~QgsMapToolCircularStringCurvePoint()

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

if ( e->button() == Qt::LeftButton )
{
Expand All @@ -29,7 +29,7 @@ void QgsMapToolCircularStringCurvePoint::cadCanvasReleaseEvent( QgsMapMouseEvent
createCenterPointRubberBand();
}

if ( mPoints.size() >= 1 )
if ( !mPoints.isEmpty() )
{
if ( !mTempRubberBand )
{
Expand All @@ -43,7 +43,7 @@ void QgsMapToolCircularStringCurvePoint::cadCanvasReleaseEvent( QgsMapMouseEvent
c->setPoints( rubberBandPoints );
mTempRubberBand->setGeometry( c );
}
if ( mPoints.size() > 1 && ( mPoints.size() ) % 2 == 1 )
if ( mPoints.size() > 1 && mPoints.size() % 2 )
{
if ( !mRubberBand )
{
Expand Down Expand Up @@ -71,11 +71,8 @@ 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 = 1 + ( mPoints.size() + 1 ) % 2;
QgsPointV2 mapPoint( e->mapPoint() );
QgsVertexId idx( 0, 0, 1 + ( mPoints.size() + 1 ) % 2 );
if ( mTempRubberBand )
{
mTempRubberBand->moveVertex( idx, mapPoint );
Expand Down
32 changes: 14 additions & 18 deletions src/app/qgsmaptoolcircularstringradius.cpp
Expand Up @@ -27,8 +27,7 @@

QgsMapToolCircularStringRadius::QgsMapToolCircularStringRadius( QgsMapToolCapture* parentTool, QgsMapCanvas* canvas, CaptureMode mode )
: QgsMapToolAddCircularString( parentTool, canvas, mode ),
mTemporaryEndPointX( 0.0 ),
mTemporaryEndPointY( 0.0 ),
mTemporaryEndPoint( QgsPointV2() ),
mRadius( 0.0 ),
mRadiusSpinBox( nullptr )
{
Expand All @@ -48,7 +47,7 @@ void QgsMapToolCircularStringRadius::deactivate()

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

if ( e->button() == Qt::LeftButton )
{
Expand All @@ -60,15 +59,14 @@ void QgsMapToolCircularStringRadius::cadCanvasReleaseEvent( QgsMapMouseEvent* e
{
if ( mPoints.size() % 2 )
{
mTemporaryEndPointX = mapPoint.x();
mTemporaryEndPointY = mapPoint.y();
mTemporaryEndPoint = mapPoint;

//initial radius is distance( tempPoint - mPoints.last ) / 2.0
double minRadius = sqrt( QgsGeometryUtils::sqrDistance2D( mPoints.last(), QgsPointV2( mTemporaryEndPointX, mTemporaryEndPointY ) ) ) / 2.0;
double minRadius = sqrt( QgsGeometryUtils::sqrDistance2D( mPoints.last(), mTemporaryEndPoint ) ) / 2.0;
mRadius = minRadius + minRadius / 10.0;

QgsPointV2 result;
if ( QgsGeometryUtils::segmentMidPoint( mPoints.last(), QgsPointV2( mTemporaryEndPointX, mTemporaryEndPointY ), result, mRadius, QgsPointV2( mapPoint.x(), mapPoint.y() ) ) )
if ( QgsGeometryUtils::segmentMidPoint( mPoints.last(), mTemporaryEndPoint, result, mRadius, QgsPointV2( mapPoint.x(), mapPoint.y() ) ) )
{
mPoints.append( result );
createRadiusSpinBox();
Expand All @@ -80,7 +78,7 @@ void QgsMapToolCircularStringRadius::cadCanvasReleaseEvent( QgsMapMouseEvent* e
}
else
{
mPoints.append( QgsPointV2( mTemporaryEndPointX, mTemporaryEndPointY ) );
mPoints.append( mTemporaryEndPoint );
deleteRadiusSpinBox();
}
recalculateCircularString();
Expand All @@ -102,10 +100,9 @@ void QgsMapToolCircularStringRadius::cadCanvasMoveEvent( QgsMapMouseEvent* e )
{
if ( !mPoints.isEmpty() )
{
mLastMouseMapPos.setX( e->mapPoint().x() );
mLastMouseMapPos.setY( e->mapPoint().y() );
mLastMouseMapPos = QgsPointV2( e->mapPoint() );
recalculateCircularString();
updateCenterPointRubberBand( QgsPointV2( mTemporaryEndPointX, mTemporaryEndPointY ) );
updateCenterPointRubberBand( mTemporaryEndPoint );
}
}

Expand All @@ -127,15 +124,15 @@ void QgsMapToolCircularStringRadius::recalculateCircularString()
{
//recalculate midpoint on circle segment
QgsPointV2 midPoint;
if ( !QgsGeometryUtils::segmentMidPoint( mPoints.at( mPoints.size() - 2 ), QgsPointV2( mTemporaryEndPointX, mTemporaryEndPointY ), midPoint, mRadius,
if ( !QgsGeometryUtils::segmentMidPoint( mPoints.at( mPoints.size() - 2 ), mTemporaryEndPoint, midPoint, mRadius,
mLastMouseMapPos ) )
{
return;
}
mPoints.replace( mPoints.size() - 1, midPoint );
rubberBandPoints.append( mPoints.at( mPoints.size() - 2 ) );
rubberBandPoints.append( mPoints.last() );
rubberBandPoints.append( QgsPointV2( mTemporaryEndPointX, mTemporaryEndPointY ) );
rubberBandPoints.append( mTemporaryEndPoint );
}
else
{
Expand Down Expand Up @@ -165,13 +162,12 @@ void QgsMapToolCircularStringRadius::createRadiusSpinBox()

void QgsMapToolCircularStringRadius::deleteRadiusSpinBox()
{
if ( !mRadiusSpinBox )
if ( mRadiusSpinBox )
{
return;
QgisApp::instance()->statusBar()->removeWidget( mRadiusSpinBox );
delete mRadiusSpinBox;
mRadiusSpinBox = nullptr;
}
QgisApp::instance()->statusBar()->removeWidget( mRadiusSpinBox );
delete mRadiusSpinBox;
mRadiusSpinBox = nullptr;
}

void QgsMapToolCircularStringRadius::updateRadiusFromSpinBox( double radius )
Expand Down
7 changes: 4 additions & 3 deletions src/app/qgsmaptoolcircularstringradius.h
Expand Up @@ -37,15 +37,16 @@ class QgsMapToolCircularStringRadius: public QgsMapToolAddCircularString
void updateRadiusFromSpinBox( double radius );

private:
double mTemporaryEndPointX;
double mTemporaryEndPointY;
QgsPointV2 mTemporaryEndPoint;
double mRadius;
QgsPointV2 mLastMouseMapPos;
QDoubleSpinBox* mRadiusSpinBox;

//recalculate circular string and rubber band depending on mRadius/mLeft and endpoints
//! recalculate the rubberband and the temporary rubberband
void recalculateCircularString();
//! (re-)create the spin box to enter the radius
void createRadiusSpinBox();
//! delete the spin box to enter the radius, if it exists
void deleteRadiusSpinBox();
};

Expand Down

0 comments on commit 6ef1706

Please sign in to comment.