diff --git a/src/gui/maptools/qgsmaptoolshapeabstract.cpp b/src/gui/maptools/qgsmaptoolshapeabstract.cpp new file mode 100644 index 00000000000..c369b05f682 --- /dev/null +++ b/src/gui/maptools/qgsmaptoolshapeabstract.cpp @@ -0,0 +1,49 @@ +/*************************************************************************** + qgsmaptoolshapeabstract.cpp + ---------------------- + begin : January 2022 + copyright : (C) 2022 by Denis Rouzaud + email : denis@opengis.ch + ***************************************************************************/ + +/*************************************************************************** + * * + * 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 "qgsmaptoolshapeabstract.h" +#include "qgsgeometryrubberband.h" + +#include + + +void QgsMapToolShapeAbstract::keyPressEvent(QKeyEvent *e) +{ + e->ignore(); +} + +void QgsMapToolShapeAbstract::keyReleaseEvent(QKeyEvent *e) +{ + e->ignore(); +} + +void QgsMapToolShapeAbstract::clean() +{ + if ( mTempRubberBand ) + { + delete mTempRubberBand; + mTempRubberBand = nullptr; + } + + mPoints.clear(); +} + +void QgsMapToolShapeAbstract::undo() +{ + if (mPoints.count() > 1) + mPoints.removeLast(); +} diff --git a/src/gui/maptools/qgsmaptoolshapeabstract.h b/src/gui/maptools/qgsmaptoolshapeabstract.h new file mode 100644 index 00000000000..c398ee2f802 --- /dev/null +++ b/src/gui/maptools/qgsmaptoolshapeabstract.h @@ -0,0 +1,116 @@ +/*************************************************************************** + qgsmaptoolshapeabstract.h - base class for map tools digitizing shapes + --------------------- + begin : January 2022 + copyright : (C) Denis Rouzaud + email : denis@opengis.ch + *************************************************************************** + * * + * 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 QGSMAPTOOLSHAPEABSTRACT_H +#define QGSMAPTOOLSHAPEABSTRACT_H + +// no bindings for now, not stable yet +#define SIP_NO_FILE + +#include "qgis_gui.h" +#include "qgsabstractgeometry.h" + +#include +#include + +class QgsMapToolCapture; +class QgsMapMouseEvent; +class QgsVectorLayer; +class QgsGeometryRubberBand; +class QKeyEvent; + + +/** + * \ingroup gui + * QgsMapToolShapeAbstract is a base class for shape map tools to be use in QgsMapToolCapture + * \since QGIS 3.24 + */ +class GUI_EXPORT QgsMapToolShapeAbstract + : public QObject +{ + Q_OBJECT + public: + //! List of different shapes + enum class ShapeCategory + { + Curve, //!< Curve + Circle,//!< Circle + Ellipse,//!< Ellipse + Rectangle,//!< Rectangle + RegularyPolygon,//!< RegularyPolygon + }; + Q_ENUM( ShapeCategory ) + + //! Constructor + QgsMapToolShapeAbstract( const QString &id, QgsMapToolCapture *parentTool ) + : mId( id ), mParentTool( parentTool ) + { + Q_ASSERT( !mId.isEmpty() ); + Q_ASSERT( parentTool ); + } + + virtual ~QgsMapToolShapeAbstract() = default; + + QString id() const {return mId;} + + /** + * Called for a mouse release event + * Must return true if the digitization has ended and the geometry is correctly set + */ + virtual bool cadCanvasReleaseEvent( QgsMapMouseEvent *e, const QgsVectorLayer *layer ) = 0; + + //! Called for a mouse move event + virtual void cadCanvasMoveEvent( QgsMapMouseEvent *e, const QgsVectorLayer *layer ) = 0; + + /** + * Eventually filters a key press event + * Ignores the event in default implementation + */ + virtual void keyPressEvent( QKeyEvent *e ); + + /** + * Eventually filters a key press event + * Ignores the event in default implementation + */ + virtual void keyReleaseEvent( QKeyEvent *e ); + + //! Activates the map tool with the last captured map point + virtual void activate( const QgsPoint &lastCapturedMapPoint ) {Q_UNUSED( lastCapturedMapPoint )} + + //! Deactivates the map tool + virtual void deactivate() {clean();} + + //! Called to clean the map tool (after canceling the operation or when the digitization has finished) + virtual void clean(); + + //! Called to undo last action (last point added) + virtual void undo(); + + private: + QString mId; + + protected: + QgsMapToolCapture *mParentTool = nullptr; + + //! points (in map coordinates) + QgsPointSequence mPoints; + + QgsGeometryRubberBand *mTempRubberBand = nullptr; + +}; + + + +#endif // QGSMAPTOOLSHAPEABSTRACT_H