1 /*************************************************************************** 2 qgsmaptoolextent.h - map tool that emits an extent 3 --------------------- 4 begin : July 2017 5 copyright : (C) 2017 by Mathieu Pellerin 6 email : nirvn dot asia at gmail dot com 7 *************************************************************************** 8 * * 9 * This program is free software; you can redistribute it and/or modify * 10 * it under the terms of the GNU General Public License as published by * 11 * the Free Software Foundation; either version 2 of the License, or * 12 * (at your option) any later version. * 13 * * 14 ***************************************************************************/ 15 16 #ifndef QGSMAPTOOLEXTENT_H 17 #define QGSMAPTOOLEXTENT_H 18 19 #include "qgsmaptool.h" 20 #include "qgspointxy.h" 21 #include "qgsrubberband.h" 22 #include "qgis_gui.h" 23 #include "qobjectuniqueptr.h" 24 25 class QgsMapCanvas; 26 27 28 /** 29 * \ingroup gui 30 * \brief A map tool that emits an extent from a rectangle drawn onto the map canvas. 31 * \since QGIS 3.0 32 */ 33 class GUI_EXPORT QgsMapToolExtent : public QgsMapTool 34 { 35 Q_OBJECT 36 37 public: 38 39 //! constructor 40 QgsMapToolExtent( QgsMapCanvas *canvas ); 41 flags()42 Flags flags() const override { return QgsMapTool::AllowZoomRect; } 43 void canvasMoveEvent( QgsMapMouseEvent *e ) override; 44 void canvasPressEvent( QgsMapMouseEvent *e ) override; 45 void canvasReleaseEvent( QgsMapMouseEvent *e ) override; 46 void activate() override; 47 void deactivate() override; 48 49 /** 50 * Sets a fixed aspect ratio to be used when dragging extent onto the canvas. 51 * To unset a fixed aspect ratio, set the width and height to zero. 52 * \param ratio aspect ratio's width and height 53 */ setRatio(QSize ratio)54 void setRatio( QSize ratio ) { mRatio = ratio; } 55 56 /** 57 * Returns the current fixed aspect ratio to be used when dragging extent onto the canvas. 58 * If the aspect ratio isn't fixed, the width and height will be set to zero. 59 */ ratio()60 QSize ratio() const { return mRatio; } 61 62 /** 63 * Returns the current extent drawn onto the canvas. 64 */ 65 QgsRectangle extent() const; 66 67 /** 68 * Removes the tool's rubber band from the canvas. 69 * 70 * \since QGIS 3.20 71 */ 72 void clearRubberBand(); 73 74 signals: 75 76 //! signal emitted on extent change 77 void extentChanged( const QgsRectangle &extent ); 78 79 private: 80 81 void calculateEndPoint( QgsPointXY &point ); 82 83 void drawExtent(); 84 85 QObjectUniquePtr< QgsRubberBand > mRubberBand; 86 87 QgsPointXY mStartPoint; 88 QgsPointXY mEndPoint; 89 90 bool mDraw = false; 91 92 QSize mRatio; 93 94 }; 95 96 #endif 97