Skip to content
Permalink
Browse files

code cleanup and improved readability in circular maptools

  • Loading branch information
SebDieBln committed Dec 18, 2015
1 parent 3cd9383 commit 6ef1706dfa86e0a2b012b21c7e0145c9360a53dc
@@ -83,7 +83,6 @@ void QgsMapToolAddCircularString::keyPressEvent( QKeyEvent* e )
if ( e && e->key() == Qt::Key_R )
{
mShowCenterPointRubberBand = true;

createCenterPointRubberBand();
}
}
@@ -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 );
}
@@ -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 );
@@ -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 )
{
@@ -29,7 +29,7 @@ void QgsMapToolCircularStringCurvePoint::cadCanvasReleaseEvent( QgsMapMouseEvent
createCenterPointRubberBand();
}

if ( mPoints.size() >= 1 )
if ( !mPoints.isEmpty() )
{
if ( !mTempRubberBand )
{
@@ -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 )
{
@@ -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 );
@@ -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 )
{
@@ -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 )
{
@@ -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();
@@ -80,7 +78,7 @@ void QgsMapToolCircularStringRadius::cadCanvasReleaseEvent( QgsMapMouseEvent* e
}
else
{
mPoints.append( QgsPointV2( mTemporaryEndPointX, mTemporaryEndPointY ) );
mPoints.append( mTemporaryEndPoint );
deleteRadiusSpinBox();
}
recalculateCircularString();
@@ -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 );
}
}

@@ -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
{
@@ -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 )
@@ -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();
};

0 comments on commit 6ef1706

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