QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsmapmouseevent.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmapmouseevent.cpp - mouse event in map coordinates and ability to snap
3  ----------------------
4  begin : October 2014
5  copyright : (C) Denis Rouzaud
6  email : [email protected]
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 "qgsmapmouseevent.h"
18 #include "qgsmapcanvas.h"
19 
20 #include "qgssnappingutils.h"
21 #include "qgssnappingconfig.h"
22 
23 QgsMapMouseEvent::QgsMapMouseEvent( QgsMapCanvas *mapCanvas, QMouseEvent *event )
24  : QMouseEvent( event->type(), event->pos(), event->button(), event->buttons(), event->modifiers() )
25  , mHasCachedSnapResult( false )
26  , mOriginalMapPoint( mapCanvas ? mapCanvas->mapSettings().mapToPixel().toMapCoordinates( event->pos() ) : QgsPointXY() )
27  , mMapPoint( mOriginalMapPoint )
28  , mPixelPoint( event->pos() )
29  , mMapCanvas( mapCanvas )
30 {
31 }
32 
33 QgsMapMouseEvent::QgsMapMouseEvent( QgsMapCanvas *mapCanvas, QEvent::Type type, QPoint pos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers )
34  : QMouseEvent( type, pos, button, buttons, modifiers )
35  , mHasCachedSnapResult( false )
36  , mOriginalMapPoint( mapCanvas ? mapCanvas->mapSettings().mapToPixel().toMapCoordinates( pos ) : QgsPointXY() )
37  , mMapPoint( mOriginalMapPoint )
38  , mPixelPoint( pos )
39  , mMapCanvas( mapCanvas )
40 {
41 }
42 
44 {
45  // Use cached result
46  if ( mHasCachedSnapResult )
47  return mMapPoint;
48 
49  mHasCachedSnapResult = true;
50 
51  QgsSnappingUtils *snappingUtils = mMapCanvas->snappingUtils();
52  mSnapMatch = snappingUtils->snapToMap( mMapPoint, nullptr, true );
53 
54  if ( mSnapMatch.isValid() )
55  {
56  mMapPoint = mSnapMatch.point();
57  mPixelPoint = mapToPixelCoordinates( mMapPoint );
58  }
59  else
60  {
61  mMapPoint = mOriginalMapPoint;
62  mPixelPoint = pos();
63  }
64 
65  return mMapPoint;
66 }
67 
69 {
70  mMapPoint = point;
71  mPixelPoint = mapToPixelCoordinates( point );
72 }
73 
75 {
76  if ( precision <= 0 )
77  return;
78 
79  try
80  {
81  QgsCoordinateTransform ct( mMapCanvas->mapSettings().destinationCrs(), crs, mMapCanvas->mapSettings().transformContext() );
82 
83  QgsPointXY pt = ct.transform( mMapPoint );
84 
85  pt.setX( std::round( pt.x() / precision ) * precision );
86  pt.setY( std::round( pt.y() / precision ) * precision );
87 
89 
90  setMapPoint( pt );
91  }
92  catch ( QgsCsException &e )
93  {
94  Q_UNUSED( e )
95  }
96 }
97 
98 QPoint QgsMapMouseEvent::mapToPixelCoordinates( const QgsPointXY &point )
99 {
100  double x = point.x(), y = point.y();
101 
102  mMapCanvas->mapSettings().mapToPixel().transformInPlace( x, y );
103 
104  return QPoint( std::round( x ), std::round( y ) );
105 }
QgsPointXY::y
double y
Definition: qgspointxy.h:48
qgsmapcanvas.h
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:391
QgsPointXY::x
Q_GADGET double x
Definition: qgspointxy.h:47
QgsMapMouseEvent::snapPoint
QgsPointXY snapPoint()
snapPoint will snap the points using the map canvas snapping utils configuration
Definition: qgsmapmouseevent.cpp:43
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:51
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsPointXY::setY
void setY(double y) SIP_HOLDGIL
Sets the y value of the point.
Definition: qgspointxy.h:117
QgsPointLocator::Match::point
QgsPointXY point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords,...
Definition: qgspointlocator.h:228
QgsCoordinateTransform::transform
QgsPointXY transform(const QgsPointXY &point, TransformDirection direction=ForwardTransform) const SIP_THROW(QgsCsException)
Transform the point from the source CRS to the destination CRS.
Definition: qgscoordinatetransform.cpp:239
QgsPointXY::setX
void setX(double x) SIP_HOLDGIL
Sets the x value of the point.
Definition: qgspointxy.h:107
QgsCoordinateTransform::ReverseTransform
@ ReverseTransform
Transform from destination to source CRS.
Definition: qgscoordinatetransform.h:61
QgsMapCanvas::snappingUtils
QgsSnappingUtils * snappingUtils() const
Returns snapping utility class that is associated with map canvas.
Definition: qgsmapcanvas.cpp:2419
qgssnappingconfig.h
QgsSnappingUtils::snapToMap
QgsPointLocator::Match snapToMap(QPoint point, QgsPointLocator::MatchFilter *filter=nullptr, bool relaxed=false)
Snap to map according to the current configuration.
Definition: qgssnappingutils.cpp:243
precision
int precision
Definition: qgswfsgetfeature.cpp:49
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
QgsMapSettings::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
Definition: qgsmapsettings.cpp:401
QgsMapMouseEvent::snapToGrid
void snapToGrid(double precision, const QgsCoordinateReferenceSystem &crs)
Snaps the mapPoint to a grid with the given precision.
Definition: qgsmapmouseevent.cpp:74
QgsPointLocator::Match::isValid
bool isValid() const
Definition: qgspointlocator.h:206
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
QgsSnappingUtils
This class has all the configuration of snapping and can return answers to snapping queries.
Definition: qgssnappingutils.h:50
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsMapSettings::destinationCrs
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
Definition: qgsmapsettings.cpp:318
QgsMapToPixel::transformInPlace
void transformInPlace(double &x, double &y) const
Transform device coordinates to map coordinates.
Definition: qgsmaptopixel.cpp:233
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:53
qgsmapmouseevent.h
QgsMapMouseEvent::QgsMapMouseEvent
QgsMapMouseEvent(QgsMapCanvas *mapCanvas, QMouseEvent *event)
Creates a new QgsMapMouseEvent.
Definition: qgsmapmouseevent.cpp:23
QgsMapSettings::mapToPixel
const QgsMapToPixel & mapToPixel() const
Definition: qgsmapsettings.h:436
QgsMapMouseEvent::setMapPoint
void setMapPoint(const QgsPointXY &point)
Set the (snapped) point this event points to in map coordinates.
Definition: qgsmapmouseevent.cpp:68
qgssnappingutils.h