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