QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 {
27  mRubberBand.reset( new QgsRubberBand( canvas, QgsWkbTypes::PolygonGeometry ) );
28 }
29 
31 {
33 }
34 
36 {
37  mRubberBand->reset( QgsWkbTypes::PolygonGeometry );
38 
40 }
41 
43 {
44  if ( !mDraw )
45  return;
46 
47 
48  QgsPointXY p = toMapCoordinates( e->pos() );
49  if ( mRatio.width() > 0 && mRatio.height() > 0 )
50  {
51  double width = std::fabs( p.x() - mStartPoint.x() );
52  double height = width * ( mRatio.width() / mRatio.height() );
53  if ( p.y() - mStartPoint.y() < 0 )
54  height *= -1;
55  p.setY( mStartPoint.y() + height );
56  }
57 
58  mEndPoint = toMapCoordinates( e->pos() );
59  calculateEndPoint( mEndPoint );
60  drawExtent();
61 }
62 
64 {
65  mStartPoint = toMapCoordinates( e->pos() );
66  mEndPoint = mStartPoint;
67  drawExtent();
68 
69  mDraw = true;
70 }
71 
73 {
74  if ( !mDraw )
75  return;
76 
77  mEndPoint = toMapCoordinates( e->pos() );
78  calculateEndPoint( mEndPoint );
79  drawExtent();
80 
81  emit extentChanged( extent() );
82 
83  mDraw = false;
84 }
85 
87 {
88  if ( mStartPoint.x() != mEndPoint.x() && mStartPoint.y() != mEndPoint.y() )
89  {
90  return QgsRectangle( mStartPoint, mEndPoint );
91  }
92  else
93  {
94  return QgsRectangle();
95  }
96 }
97 
98 void QgsMapToolExtent::calculateEndPoint( QgsPointXY &point )
99 {
100  if ( mRatio.width() > 0 && mRatio.height() > 0 )
101  {
102  double width = std::fabs( point.x() - mStartPoint.x() );
103  double height = width * mRatio.height() / mRatio.width();
104  if ( point.y() - mStartPoint.y() < 0 )
105  height *= -1;
106  point.setY( mStartPoint.y() + height );
107  }
108 }
109 
110 void QgsMapToolExtent::drawExtent()
111 {
112  if ( qgsDoubleNear( mStartPoint.x(), mEndPoint.x() ) && qgsDoubleNear( mStartPoint.y(), mEndPoint.y() ) )
113  return;
114 
115  mRubberBand->reset( QgsWkbTypes::PolygonGeometry );
116 
117  QgsRectangle rect( mStartPoint, mEndPoint );
118 
119  mRubberBand->reset( QgsWkbTypes::PolygonGeometry );
120  mRubberBand->addPoint( QgsPointXY( rect.xMinimum(), rect.yMinimum() ), false );
121  mRubberBand->addPoint( QgsPointXY( rect.xMaximum(), rect.yMinimum() ), false );
122  mRubberBand->addPoint( QgsPointXY( rect.xMaximum(), rect.yMaximum() ), false );
123  mRubberBand->addPoint( QgsPointXY( rect.xMinimum(), rect.yMaximum() ), true );
124 
125  mRubberBand->show();
126 }
A rectangle specified with double values.
Definition: qgsrectangle.h:40
QgsRectangle extent() const
Returns the current extent drawn onto the canvas.
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Mouse release event for overriding. Default implementation does nothing.
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:171
double y
Definition: qgspointxy.h:48
A class to represent a 2D point.
Definition: qgspointxy.h:43
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:278
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
QgsPointXY toMapCoordinates(QPoint point)
transformation from screen coordinates to map coordinates
Definition: qgsmaptool.cpp:42
QgsMapToolExtent(QgsMapCanvas *canvas)
constructor
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:83
void canvasPressEvent(QgsMapMouseEvent *e) override
Mouse press event for overriding. Default implementation does nothing.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:176
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:161
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:47
void extentChanged(const QgsRectangle &extent)
signal emitted on extent change
void setY(double y)
Sets the y value of the point.
Definition: qgspointxy.h:113
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:99
double x
Definition: qgspointxy.h:47
void activate() override
called when set as currently active map tool
Abstract base class for all map tools.
Definition: qgsmaptool.h:62
void deactivate() override
called when map tool is being deactivated
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:166
void canvasMoveEvent(QgsMapMouseEvent *e) override
Mouse move event for overriding. Default implementation does nothing.