QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
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
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
19#include "qgsmapcanvas.h"
20#include "qgssnappingconfig.h"
21#include "qgssnappingutils.h"
22
23QgsMapMouseEvent::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
32QgsMapMouseEvent::QgsMapMouseEvent( QgsMapCanvas *mapCanvas, QEvent::Type type, QPoint pos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers )
33 : QMouseEvent( type, pos, button, buttons, modifiers )
34 , mHasCachedSnapResult( false )
35 , mOriginalMapPoint( mapCanvas ? mapCanvas->mapSettings().mapToPixel().toMapCoordinates( pos ) : QgsPointXY() )
36 , mMapPoint( mOriginalMapPoint )
37 , mPixelPoint( pos )
38 , mMapCanvas( mapCanvas )
39{}
40
42{
43 // Use cached result
44 if ( mHasCachedSnapResult )
45 return mMapPoint;
46
47 mHasCachedSnapResult = true;
48
49 QgsSnappingUtils *snappingUtils = mMapCanvas->snappingUtils();
50 mSnapMatch = snappingUtils->snapToMap( mMapPoint, nullptr, true );
51
52 if ( mSnapMatch.isValid() )
53 {
54 mMapPoint = mSnapMatch.point();
55 mPixelPoint = mapToPixelCoordinates( mMapPoint );
56 }
57 else
58 {
59 mMapPoint = mOriginalMapPoint;
60 mPixelPoint = pos();
61 }
62
63 return mMapPoint;
64}
65
67{
68 mMapPoint = point;
69 mPixelPoint = mapToPixelCoordinates( point );
70}
71
73{
74 if ( precision <= 0 )
75 return;
76
77 try
78 {
79 const QgsCoordinateTransform ct( mMapCanvas->mapSettings().destinationCrs(), crs, mMapCanvas->mapSettings().transformContext() );
80
81 QgsPointXY pt = ct.transform( mMapPoint );
82
83 pt.setX( std::round( pt.x() / precision ) * precision );
84 pt.setY( std::round( pt.y() / precision ) * precision );
85
87
88 setMapPoint( pt );
89 }
90 catch ( QgsCsException &e )
91 {
92 Q_UNUSED( e )
93 }
94}
95
96QPoint QgsMapMouseEvent::mapToPixelCoordinates( const QgsPointXY &point )
97{
98 double x = point.x(), y = point.y();
99
100 mMapCanvas->mapSettings().mapToPixel().transformInPlace( x, y );
101
102 return QPoint( std::round( x ), std::round( y ) );
103}
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2766
Represents a coordinate reference system (CRS).
Handles coordinate transforms between two coordinate systems.
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transform the point from the source CRS to the destination CRS.
Custom exception class for Coordinate Reference System related exceptions.
Map canvas is a class for displaying all GIS data types on a canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QgsMapMouseEvent(QgsMapCanvas *mapCanvas, QMouseEvent *event)
Creates a new QgsMapMouseEvent.
void setMapPoint(const QgsPointXY &point)
Set the (snapped) point this event points to in map coordinates.
void snapToGrid(double precision, const QgsCoordinateReferenceSystem &crs)
Snaps the mapPoint to a grid with the given precision.
QgsPointXY snapPoint()
snapPoint will snap the points using the map canvas snapping utils configuration
const QgsMapToPixel & mapToPixel() const
void transformInPlace(double &x, double &y) const
Transforms map coordinates to device coordinates.
Represents a 2D point.
Definition qgspointxy.h:62
void setY(double y)
Sets the y value of the point.
Definition qgspointxy.h:132
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
void setX(double x)
Sets the x value of the point.
Definition qgspointxy.h:122
Contains configuration of snapping and can return answers to snapping queries.
QgsPointLocator::Match snapToMap(QPoint point, QgsPointLocator::MatchFilter *filter=nullptr, bool relaxed=false)
Snap to map according to the current configuration.