Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Factor common code of QgsMapToolAddCircle/Ellipse/Rectangle/RegularRe…
…ctangle

Those classes have identical implementations for a number of methods.
Add a QgsMapToolAddAbstract class for that, and make them derive for it.
  • Loading branch information
rouault authored and nyalldawson committed May 9, 2021
1 parent 3711fcf commit fffcf66
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 474 deletions.
1 change: 1 addition & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -150,6 +150,7 @@ set(QGIS_APP_SRCS
qgsundowidget.cpp
qgsmapthemes.cpp
qgshandlebadlayers.cpp
qgsmaptooladdabstract.cpp
qgsmaptooladdcircularstring.cpp
qgsmaptoolcircularstringcurvepoint.cpp
qgsmaptoolcircularstringradius.cpp
Expand Down
119 changes: 119 additions & 0 deletions src/app/qgsmaptooladdabstract.cpp
@@ -0,0 +1,119 @@
/***************************************************************************
qgsmaptooladdabstract.cpp - abstract class for map tools of the 'add' kind
---------------------
begin : July 2017
copyright : (C) 2017
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 "qgsmaptooladdabstract.h"
#include "qgsgeometryrubberband.h"
#include "qgsgeometryutils.h"
#include "qgsmapcanvas.h"
#include "qgspoint.h"
#include "qgisapp.h"
#include "qgssnapindicator.h"

QgsMapToolAddAbstract::QgsMapToolAddAbstract( QgsMapToolCapture *parentTool, QgsMapCanvas *canvas, CaptureMode mode )
: QgsMapToolCapture( canvas, QgisApp::instance()->cadDockWidget(), mode )
, mParentTool( parentTool )
, mSnapIndicator( std::make_unique< QgsSnapIndicator>( canvas ) )
{
QgsMapToolAddAbstract::clean();

connect( QgisApp::instance(), &QgisApp::newProject, this, &QgsMapToolAddAbstract::stopCapturing );
connect( QgisApp::instance(), &QgisApp::projectRead, this, &QgsMapToolAddAbstract::stopCapturing );
}

QgsMapToolAddAbstract::~QgsMapToolAddAbstract()
{
QgsMapToolAddAbstract::clean();
}

void QgsMapToolAddAbstract::keyPressEvent( QKeyEvent *e )
{
if ( e && e->isAutoRepeat() )
{
return;
}

if ( e && e->key() == Qt::Key_Escape )
{
clean();
if ( mParentTool )
mParentTool->keyPressEvent( e );
}

if ( e && e->key() == Qt::Key_Backspace )
{
if ( mPoints.size() == 1 )
{

if ( mTempRubberBand )
{
delete mTempRubberBand;
mTempRubberBand = nullptr;
}

mPoints.clear();
}
else if ( mPoints.size() > 1 )
{
mPoints.removeLast();

}
if ( mParentTool )
mParentTool->keyPressEvent( e );
}
}

void QgsMapToolAddAbstract::keyReleaseEvent( QKeyEvent *e )
{
if ( e && e->isAutoRepeat() )
{
return;
}
}

void QgsMapToolAddAbstract::activate()
{
clean();
QgsMapToolCapture::activate();
}

void QgsMapToolAddAbstract::clean()
{
if ( mTempRubberBand )
{
delete mTempRubberBand;
mTempRubberBand = nullptr;
}

mPoints.clear();

if ( mParentTool )
{
mParentTool->deleteTempRubberBand();
}

QgsVectorLayer *vLayer = static_cast<QgsVectorLayer *>( QgisApp::instance()->activeLayer() );
if ( vLayer )
mLayerType = vLayer->geometryType();
}

void QgsMapToolAddAbstract::release( QgsMapMouseEvent *e )
{
deactivate();
if ( mParentTool )
{
mParentTool->canvasReleaseEvent( e );
}
activate();
}
66 changes: 66 additions & 0 deletions src/app/qgsmaptooladdabstract.h
@@ -0,0 +1,66 @@
/***************************************************************************
qgsmaptooladdabstract.h - abstract class for map tools of the 'add' kind
---------------------
begin : May 2017
copyright : (C) 2017
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. *
* *
***************************************************************************/

#ifndef QGSMAPTOOLADDABSTRACT_H
#define QGSMAPTOOLADDABSTRACT_H

#include "qgsmaptoolcapture.h"
#include "qgsellipse.h"
#include "qgssettingsregistrycore.h"
#include "qgis_app.h"

class QgsGeometryRubberBand;
class QgsSnapIndicator;

class APP_EXPORT QgsMapToolAddAbstract: public QgsMapToolCapture
{
Q_OBJECT
public:
QgsMapToolAddAbstract( QgsMapToolCapture *parentTool, QgsMapCanvas *canvas, CaptureMode mode = CaptureLine );
~QgsMapToolAddAbstract() override;

void keyPressEvent( QKeyEvent *e ) override;
void keyReleaseEvent( QKeyEvent *e ) override;

void deactivate() override = 0;

void activate() override;
void clean() override;

protected:
explicit QgsMapToolAddAbstract( QgsMapCanvas *canvas ) = delete; //forbidden

//! Convenient method to release (activate/deactivate) tools
void release( QgsMapMouseEvent *e );

/**
* The parent map tool, e.g. the add feature tool.
* Completed geometry will be added to this tool by calling its toLineString() method.
*/
QPointer<QgsMapToolCapture> mParentTool;
//! Ellipse points (in map coordinates)
QgsPointSequence mPoints;
//! The rubberband to show the geometry currently working on
QgsGeometryRubberBand *mTempRubberBand = nullptr;

//! Layer type which will be used for rubberband
QgsWkbTypes::GeometryType mLayerType = QgsWkbTypes::LineGeometry;

//! Snapping indicators
std::unique_ptr<QgsSnapIndicator> mSnapIndicator;

};

#endif // QGSMAPTOOLADDABSTRACT_H
92 changes: 2 additions & 90 deletions src/app/qgsmaptooladdcircle.cpp
Expand Up @@ -25,65 +25,9 @@
#include "qgssnapindicator.h"

QgsMapToolAddCircle::QgsMapToolAddCircle( QgsMapToolCapture *parentTool, QgsMapCanvas *canvas, CaptureMode mode )
: QgsMapToolCapture( canvas, QgisApp::instance()->cadDockWidget(), mode )
, mParentTool( parentTool )
, mSnapIndicator( std::make_unique< QgsSnapIndicator>( canvas ) )
: QgsMapToolAddAbstract( parentTool, canvas, mode )
{
mToolName = tr( "Add circle" );

clean();
connect( QgisApp::instance(), &QgisApp::newProject, this, &QgsMapToolAddCircle::stopCapturing );
connect( QgisApp::instance(), &QgisApp::projectRead, this, &QgsMapToolAddCircle::stopCapturing );
}

QgsMapToolAddCircle::~QgsMapToolAddCircle()
{
clean();
}

void QgsMapToolAddCircle::keyPressEvent( QKeyEvent *e )
{
if ( e && e->isAutoRepeat() )
{
return;
}

if ( e && e->key() == Qt::Key_Escape )
{
clean();
if ( mParentTool )
mParentTool->keyPressEvent( e );
}

if ( e && e->key() == Qt::Key_Backspace )
{
if ( mPoints.size() == 1 )
{

if ( mTempRubberBand )
{
delete mTempRubberBand;
mTempRubberBand = nullptr;
}

mPoints.clear();
}
else if ( mPoints.size() > 1 )
{
mPoints.removeLast();

}
if ( mParentTool )
mParentTool->keyPressEvent( e );
}
}

void QgsMapToolAddCircle::keyReleaseEvent( QKeyEvent *e )
{
if ( e && e->isAutoRepeat() )
{
return;
}
}

void QgsMapToolAddCircle::deactivate()
Expand Down Expand Up @@ -114,40 +58,8 @@ void QgsMapToolAddCircle::deactivate()
QgsMapToolCapture::deactivate();
}

void QgsMapToolAddCircle::activate()
{
clean();
QgsMapToolCapture::activate();
}

void QgsMapToolAddCircle::clean()
{
if ( mTempRubberBand )
{
delete mTempRubberBand;
mTempRubberBand = nullptr;
}

mPoints.clear();

if ( mParentTool )
{
mParentTool->deleteTempRubberBand();
}

QgsMapToolAddAbstract::clean();
mCircle = QgsCircle();

QgsVectorLayer *vLayer = static_cast<QgsVectorLayer *>( QgisApp::instance()->activeLayer() );
if ( vLayer )
mLayerType = vLayer->geometryType();
}

void QgsMapToolAddCircle::release( QgsMapMouseEvent *e )
{
deactivate();
if ( mParentTool )
{
mParentTool->canvasReleaseEvent( e );
}
activate();
}
29 changes: 2 additions & 27 deletions src/app/qgsmaptooladdcircle.h
Expand Up @@ -16,55 +16,30 @@
#ifndef QGSMAPTOOLADDCIRCLE_H
#define QGSMAPTOOLADDCIRCLE_H

#include "qgsmaptoolcapture.h"
#include "qgsmaptooladdabstract.h"
#include "qgscircle.h"
#include "qgis_app.h"

class QgsGeometryRubberBand;
class QgsSnapIndicator;

struct EdgesOnlyFilter : public QgsPointLocator::MatchFilter
{
bool acceptMatch( const QgsPointLocator::Match &m ) override { return m.hasEdge(); }
};

class APP_EXPORT QgsMapToolAddCircle: public QgsMapToolCapture
class APP_EXPORT QgsMapToolAddCircle: public QgsMapToolAddAbstract
{
Q_OBJECT

public:
QgsMapToolAddCircle( QgsMapToolCapture *parentTool, QgsMapCanvas *canvas, CaptureMode mode = CaptureLine );
~QgsMapToolAddCircle() override;

void keyPressEvent( QKeyEvent *e ) override;
void keyReleaseEvent( QKeyEvent *e ) override;

void deactivate() override;
void activate() override;
void clean() override;

protected:
explicit QgsMapToolAddCircle( QgsMapCanvas *canvas ) = delete; //forbidden

//! Convenient method to release (activate/deactivate) tools
void release( QgsMapMouseEvent *e );

/**
* The parent map tool, e.g. the add feature tool.
* Completed circle will be added to this tool by calling its addCurve() method.
*/
QPointer<QgsMapToolCapture> mParentTool;
//! Circle points (in map coordinates)
QgsPointSequence mPoints;
//! The rubberband to show the circular string currently working on
QgsGeometryRubberBand *mTempRubberBand = nullptr;
//! Circle
QgsCircle mCircle;
//! Layer type which will be used for rubberband
QgsWkbTypes::GeometryType mLayerType = QgsWkbTypes::LineGeometry;

//! Snapping indicators
std::unique_ptr<QgsSnapIndicator> mSnapIndicator;

};

Expand Down

0 comments on commit fffcf66

Please sign in to comment.