Skip to content
Permalink
Browse files
move QgsMapToolCaptureRubberband class to its own file
  • Loading branch information
3nids committed Jan 3, 2022
1 parent a64443f commit 795bb6a245b6beb071bedafcb1c76d86267c87e5
@@ -12,7 +12,6 @@




class QgsMapToolCapture : QgsMapToolAdvancedDigitizing
{

@@ -535,6 +535,7 @@ set(QGIS_GUI_SRCS
qgsmaptool.cpp
qgsmaptooladvanceddigitizing.cpp
qgsmaptoolcapture.cpp
qgsmaptoolcapturerubberband.cpp
qgsmaptooledit.cpp
qgsmaptooldigitizefeature.cpp
qgsmaptoolemitpoint.cpp
@@ -787,6 +788,7 @@ set(QGIS_GUI_HDRS
qgsmaptool.h
qgsmaptooladvanceddigitizing.h
qgsmaptoolcapture.h
qgsmaptoolcapturerubberband.h
qgsmaptooldigitizefeature.h
qgsmaptooledit.h
qgsmaptoolemitpoint.h
@@ -32,16 +32,14 @@
#include "qgsapplication.h"
#include "qgsadvanceddigitizingdockwidget.h"
#include "qgsproject.h"
#include "qgsgeometryrubberband.h"
#include "qgsmaptoolcapturerubberband.h"

#include <QAction>
#include <QCursor>
#include <QPixmap>
#include <QStatusBar>


///@cond PRIVATE

QgsMapToolCapture::QgsMapToolCapture( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget, CaptureMode mode )
: QgsMapToolAdvancedDigitizing( canvas, cadDockWidget )
, mCaptureMode( mode )
@@ -1085,198 +1083,3 @@ void QgsMapToolCapture::updateExtraSnapLayer()
}
}

QgsMapToolCaptureRubberBand::QgsMapToolCaptureRubberBand( QgsMapCanvas *mapCanvas, QgsWkbTypes::GeometryType geomType ):
QgsGeometryRubberBand( mapCanvas, geomType )
{
setVertexDrawingEnabled( false );
}

QgsCurve *QgsMapToolCaptureRubberBand::curve()
{
if ( mPoints.empty() )
return nullptr;

switch ( mStringType )
{
case QgsWkbTypes::LineString:
return new QgsLineString( mPoints ) ;
break;
case QgsWkbTypes::CircularString:
if ( mPoints.count() != 3 )
return nullptr;
return new QgsCircularString(
mPoints[0],
mPoints[1],
mPoints[2] ) ;
break;
default:
return nullptr;
}
}

bool QgsMapToolCaptureRubberBand::curveIsComplete() const
{
return ( mStringType == QgsWkbTypes::LineString && mPoints.count() > 1 ) ||
( mStringType == QgsWkbTypes::CircularString && mPoints.count() > 2 );
}

void QgsMapToolCaptureRubberBand::reset( QgsWkbTypes::GeometryType geomType, QgsWkbTypes::Type stringType, const QgsPoint &firstPolygonPoint )
{
if ( !( geomType == QgsWkbTypes::LineGeometry || geomType == QgsWkbTypes::PolygonGeometry ) )
return;

mPoints.clear();
mFirstPolygonPoint = firstPolygonPoint;
setStringType( stringType );
setRubberBandGeometryType( geomType );
}

void QgsMapToolCaptureRubberBand::setRubberBandGeometryType( QgsWkbTypes::GeometryType geomType )
{
QgsGeometryRubberBand::setGeometryType( geomType );
updateCurve();
}

void QgsMapToolCaptureRubberBand::addPoint( const QgsPoint &point, bool doUpdate )
{
if ( mPoints.count() == 0 )
mPoints.append( point );

mPoints.append( point );

if ( doUpdate )
updateCurve();
}

void QgsMapToolCaptureRubberBand::movePoint( const QgsPoint &point )
{
if ( mPoints.count() > 0 )
mPoints.last() = point ;

updateCurve();
}

void QgsMapToolCaptureRubberBand::movePoint( int index, const QgsPoint &point )
{
if ( mPoints.count() > 0 && mPoints.size() > index )
mPoints[index] = point;

updateCurve();
}

int QgsMapToolCaptureRubberBand::pointsCount()
{
return mPoints.size();
}

QgsWkbTypes::Type QgsMapToolCaptureRubberBand::stringType() const
{
return mStringType;
}

void QgsMapToolCaptureRubberBand::setStringType( const QgsWkbTypes::Type &type )
{
if ( ( type != QgsWkbTypes::CircularString && type != QgsWkbTypes::LineString ) || type == mStringType )
return;

mStringType = type;
if ( type == QgsWkbTypes::LineString && mPoints.count() == 3 )
{
mPoints.removeAt( 1 );
}

setVertexDrawingEnabled( type == QgsWkbTypes::CircularString );
updateCurve();
}

QgsPoint QgsMapToolCaptureRubberBand::lastPoint() const
{
if ( mPoints.empty() )
return QgsPoint();

return mPoints.last();
}

QgsPoint QgsMapToolCaptureRubberBand::pointFromEnd( int posFromEnd ) const
{
if ( posFromEnd < mPoints.size() )
return mPoints.at( mPoints.size() - 1 - posFromEnd );
else
return QgsPoint();
}

void QgsMapToolCaptureRubberBand::removeLastPoint()
{
if ( mPoints.count() > 1 )
mPoints.removeLast();

updateCurve();
}

void QgsMapToolCaptureRubberBand::setGeometry( QgsAbstractGeometry *geom )
{
QgsGeometryRubberBand::setGeometry( geom );
}

void QgsMapToolCaptureRubberBand::updateCurve()
{
std::unique_ptr<QgsCurve> curve;
switch ( mStringType )
{
case QgsWkbTypes::LineString:
curve.reset( createLinearString() );
break;
case QgsWkbTypes::CircularString:
curve.reset( createCircularString() );
break;
default:
return;
break;
}

if ( geometryType() == QgsWkbTypes::PolygonGeometry )
{
std::unique_ptr<QgsCurvePolygon> geom( new QgsCurvePolygon );
geom->setExteriorRing( curve.release() );
setGeometry( geom.release() );
}
else
{
setGeometry( curve.release() );
}
}

QgsCurve *QgsMapToolCaptureRubberBand::createLinearString()
{
std::unique_ptr<QgsLineString> curve( new QgsLineString );
if ( geometryType() == QgsWkbTypes::PolygonGeometry )
{
QgsPointSequence points = mPoints;
points.prepend( mFirstPolygonPoint );
curve->setPoints( points );
}
else
curve->setPoints( mPoints );

return curve.release();
}

QgsCurve *QgsMapToolCaptureRubberBand::createCircularString()
{
std::unique_ptr<QgsCircularString> curve( new QgsCircularString );
curve->setPoints( mPoints );
if ( geometryType() == QgsWkbTypes::PolygonGeometry )
{
// add a linear string to close the polygon
std::unique_ptr<QgsCompoundCurve> polygonCurve( new QgsCompoundCurve );
polygonCurve->addVertex( mFirstPolygonPoint );
if ( !mPoints.empty() )
polygonCurve->addVertex( mPoints.first() );
polygonCurve->addCurve( curve.release() );
return polygonCurve.release();
}
else
return curve.release();
}

///@endcond PRIVATE
@@ -23,7 +23,6 @@
#include "qgsgeometry.h"
#include "qobjectuniqueptr.h"
#include "qgssnappingutils.h"
#include "qgsgeometryrubberband.h"

#include <QPoint>
#include <QList>
@@ -37,84 +36,6 @@ class QgsGeometryValidator;
class QgsMapToolCaptureRubberBand;


#ifndef SIP_RUN

///@cond PRIVATE

/**
* Class that reprensents a rubber band that can be linear or circular.
*
* \since QGIS 3.16
*/
class QgsMapToolCaptureRubberBand: public QgsGeometryRubberBand
{
public:
//! Constructor
QgsMapToolCaptureRubberBand( QgsMapCanvas *mapCanvas, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::LineGeometry );

//! Returns the curve defined by the rubber band, the caller has to take the ownership, nullptr if no curve is defined.
QgsCurve *curve();

/**
* Returns if the curve defined by the rubber band is complete :
* has more than 2 points for circular string and more than 1 point for linear string
*/
bool curveIsComplete() const;

/**
* Resets the rubber band with the specified geometry type
* that must be line geometry or polygon geometry.
* \a firstPolygonPoint is the first point that will be used to render the polygon rubber band (if \a geomType is PolygonGeometry)
*/
void reset( QgsWkbTypes::GeometryType geomType = QgsWkbTypes::LineGeometry, QgsWkbTypes::Type stringType = QgsWkbTypes::LineString, const QgsPoint &firstPolygonPoint = QgsPoint() );

//! Sets the geometry type of the rubberband without removing already existing points
void setRubberBandGeometryType( QgsWkbTypes::GeometryType geomType );

//! Adds point to the rubber band
void addPoint( const QgsPoint &point, bool doUpdate = true );

//! Moves the last point to the \a point position
void movePoint( const QgsPoint &point );

//! Moves the point with \a index to the \a point position
void movePoint( int index, const QgsPoint &point );

//! Returns the points count in the rubber band (except the first point if polygon)
int pointsCount();

//! Returns the type of the curve (linear string or circular string)
QgsWkbTypes::Type stringType() const;

//! Sets the type of the curve (linear string or circular string)
void setStringType( const QgsWkbTypes::Type &type );

//! Returns the last point of the rubber band
QgsPoint lastPoint() const;

//! Returns the point of the rubber band at position from end
QgsPoint pointFromEnd( int posFromEnd ) const;

//! Removes the last point of the rrubber band
void removeLastPoint();

private:
QgsWkbTypes::Type mStringType = QgsWkbTypes::LineString;

void setGeometry( QgsAbstractGeometry *geom ) override;
void updateCurve();

QgsCurve *createLinearString();
QgsCurve *createCircularString();

QgsPointSequence mPoints;
QgsPoint mFirstPolygonPoint;
};

/// @endcond

#endif //SIP_RUN

/**
* \ingroup gui
* \class QgsMapToolCapture

0 comments on commit 795bb6a

Please sign in to comment.