QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsmaptoolextent.cpp
Go to the documentation of this file.
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 
17 
18 #include "qgsmaptoolextent.h"
19 #include "qgsmapcanvas.h"
20 #include "qgswkbtypes.h"
21 #include "qgsmapmouseevent.h"
22 
23 
25  : QgsMapTool( canvas )
26 {
28 }
29 
31 {
33 }
34 
36 {
39 }
40 
42 {
43  if ( !mDraw )
44  return;
45 
46 
47  QgsPointXY p = toMapCoordinates( e->pos() );
48  if ( mRatio.width() > 0 && mRatio.height() > 0 )
49  {
50  const double width = std::fabs( p.x() - mStartPoint.x() );
51  double height = width * ( mRatio.width() / mRatio.height() );
52  if ( p.y() - mStartPoint.y() < 0 )
53  height *= -1;
54  p.setY( mStartPoint.y() + height );
55  }
56 
57  mEndPoint = toMapCoordinates( e->pos() );
58  calculateEndPoint( mEndPoint );
59  drawExtent();
60 }
61 
63 {
64  mStartPoint = toMapCoordinates( e->pos() );
65  mEndPoint = mStartPoint;
66  drawExtent();
67 
68  mDraw = true;
69 }
70 
72 {
73  if ( !mDraw )
74  return;
75 
76  mEndPoint = toMapCoordinates( e->pos() );
77  calculateEndPoint( mEndPoint );
78  drawExtent();
79 
80  emit extentChanged( extent() );
81 
82  mDraw = false;
83 }
84 
86 {
87  if ( mStartPoint.x() != mEndPoint.x() && mStartPoint.y() != mEndPoint.y() )
88  {
89  return QgsRectangle( mStartPoint, mEndPoint );
90  }
91  else
92  {
93  return QgsRectangle();
94  }
95 }
96 
98 {
99  mRubberBand->reset( QgsWkbTypes::PolygonGeometry );
100 }
101 
102 void QgsMapToolExtent::calculateEndPoint( QgsPointXY &point )
103 {
104  if ( mRatio.width() > 0 && mRatio.height() > 0 )
105  {
106  const double width = std::fabs( point.x() - mStartPoint.x() );
107  double height = width * mRatio.height() / mRatio.width();
108  if ( point.y() - mStartPoint.y() < 0 )
109  height *= -1;
110  point.setY( mStartPoint.y() + height );
111  }
112 }
113 
114 void QgsMapToolExtent::drawExtent()
115 {
116  if ( qgsDoubleNear( mStartPoint.x(), mEndPoint.x() ) && qgsDoubleNear( mStartPoint.y(), mEndPoint.y() ) )
117  return;
118 
119  const QgsRectangle rect( mStartPoint, mEndPoint );
120 
121  mRubberBand->reset( QgsWkbTypes::PolygonGeometry );
122  mRubberBand->addPoint( QgsPointXY( rect.xMinimum(), rect.yMinimum() ), false );
123  mRubberBand->addPoint( QgsPointXY( rect.xMaximum(), rect.yMinimum() ), false );
124  mRubberBand->addPoint( QgsPointXY( rect.xMaximum(), rect.yMaximum() ), false );
125  mRubberBand->addPoint( QgsPointXY( rect.xMinimum(), rect.yMaximum() ), true );
126 
127  mRubberBand->show();
128 }
QgsMapToolExtent::extentChanged
void extentChanged(const QgsRectangle &extent)
signal emitted on extent change
QgsPointXY::y
double y
Definition: qgspointxy.h:63
QObjectUniquePtr::reset
void reset(T *p=nullptr)
Will reset the managed pointer to p.
Definition: qobjectuniqueptr.h:179
QgsMapToolExtent::extent
QgsRectangle extent() const
Returns the current extent drawn onto the canvas.
Definition: qgsmaptoolextent.cpp:85
qgsmapcanvas.h
QgsMapToolExtent::deactivate
void deactivate() override
called when map tool is being deactivated
Definition: qgsmaptoolextent.cpp:35
QgsRubberBand
A class for drawing transient features (e.g. digitizing lines) on the map.
Definition: qgsrubberband.h:51
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsMapTool::deactivate
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:110
QgsPointXY::setY
void setY(double y) SIP_HOLDGIL
Sets the y value of the point.
Definition: qgspointxy.h:132
QgsMapToolExtent::activate
void activate() override
called when set as currently active map tool
Definition: qgsmaptoolextent.cpp:30
QgsMapTool::canvas
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
Definition: qgsmaptool.cpp:215
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:144
QgsRubberBand::reset
void reset(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry)
Clears all the geometries in this rubberband.
Definition: qgsrubberband.cpp:110
QgsMapTool
Abstract base class for all map tools. Map tools are user interactive tools for manipulating the map ...
Definition: qgsmaptool.h:70
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2265
QgsMapToolExtent::canvasMoveEvent
void canvasMoveEvent(QgsMapMouseEvent *e) override
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptoolextent.cpp:41
QgsMapToolExtent::canvasReleaseEvent
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptoolextent.cpp:71
QgsRubberBand::addPoint
void addPoint(const QgsPointXY &p, bool doUpdate=true, int geometryIndex=0, int ringIndex=0)
Adds a vertex to the rubberband and update canvas.
Definition: qgsrubberband.cpp:118
QgsMapTool::activate
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:94
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsMapMouseEvent
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas....
Definition: qgsmapmouseevent.h:35
QgsPointXY::x
double x
Definition: qgspointxy.h:62
qgsmaptoolextent.h
QgsMapToolExtent::canvasPressEvent
void canvasPressEvent(QgsMapMouseEvent *e) override
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptoolextent.cpp:62
QgsMapToolExtent::QgsMapToolExtent
QgsMapToolExtent(QgsMapCanvas *canvas)
constructor
Definition: qgsmaptoolextent.cpp:24
qgsmapmouseevent.h
qgswkbtypes.h
QgsMapTool::toMapCoordinates
QgsPointXY toMapCoordinates(QPoint point)
Transforms a point from screen coordinates to map coordinates.
Definition: qgsmaptool.cpp:41
QgsMapToolExtent::clearRubberBand
void clearRubberBand()
Removes the tool's rubber band from the canvas.
Definition: qgsmaptoolextent.cpp:97