QGIS/src/app/qgsmaptoolcircle3points.cpp

81 lines
2.5 KiB
C++

/***************************************************************************
qgmaptoolcircle3points.h - map tool for adding circle
from 3 points
---------------------
begin : July 2017
copyright : (C) 2017 by Loïc Bartoletti
email : lbartoletti at tuxfamily dot org
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsmaptoolcircle3points.h"
#include "qgsgeometryrubberband.h"
#include "qgslinestring.h"
#include "qgsmapcanvas.h"
#include "qgspoint.h"
#include <QMouseEvent>
QgsMapToolCircle3Points::QgsMapToolCircle3Points( QgsMapToolCapture *parentTool,
QgsMapCanvas *canvas, CaptureMode mode )
: QgsMapToolAddCircle( parentTool, canvas, mode )
{
}
void QgsMapToolCircle3Points::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
{
QgsPoint mapPoint( e->mapPoint() );
if ( e->button() == Qt::LeftButton )
{
if ( mPoints.size() < 2 )
mPoints.append( mapPoint );
if ( !mPoints.isEmpty() && !mTempRubberBand )
{
mTempRubberBand = createGeometryRubberBand( ( mode() == CapturePolygon ) ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry, true );
mTempRubberBand->show();
}
}
else if ( e->button() == Qt::RightButton )
{
deactivate();
if ( mParentTool )
{
mParentTool->canvasReleaseEvent( e );
}
}
}
void QgsMapToolCircle3Points::cadCanvasMoveEvent( QgsMapMouseEvent *e )
{
QgsPoint mapPoint( e->mapPoint() );
if ( mTempRubberBand )
{
switch ( mPoints.size() )
{
case 1:
{
std::unique_ptr<QgsLineString> line( new QgsLineString() );
line->addVertex( mPoints.at( 0 ) );
line->addVertex( mapPoint );
mTempRubberBand->setGeometry( line.release() );
}
break;
case 2:
{
mCircle = QgsCircle().from3Points( mPoints.at( 0 ), mPoints.at( 1 ), mapPoint );
mTempRubberBand->setGeometry( mCircle.toCircularString( true ) );
}
break;
default:
break;
}
}
}