QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
gui
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 : denis.rouzaud@gmail.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 "
qgsmapmouseevent.h
"
18
19
#include "
qgsmapcanvas.h
"
20
#include "
qgssnappingconfig.h
"
21
#include "
qgssnappingutils.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
QgsMapMouseEvent::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
41
QgsPointXY
QgsMapMouseEvent::snapPoint
()
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
66
void
QgsMapMouseEvent::setMapPoint
(
const
QgsPointXY
&point )
67
{
68
mMapPoint = point;
69
mPixelPoint = mapToPixelCoordinates( point );
70
}
71
72
void
QgsMapMouseEvent::snapToGrid
(
double
precision,
const
QgsCoordinateReferenceSystem
&crs )
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
86
pt = ct.
transform
( pt,
Qgis::TransformDirection::Reverse
);
87
88
setMapPoint
( pt );
89
}
90
catch
(
QgsCsException
&e )
91
{
92
Q_UNUSED( e )
93
}
94
}
95
96
QPoint 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
}
Qgis::TransformDirection::Reverse
@ Reverse
Reverse/inverse transform (from destination to source).
Definition
qgis.h:2884
QgsCoordinateReferenceSystem
Represents a coordinate reference system (CRS).
Definition
qgscoordinatereferencesystem.h:216
QgsCoordinateTransform
Handles coordinate transforms between two coordinate systems.
Definition
qgscoordinatetransform.h:63
QgsCoordinateTransform::transform
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transform the point from the source CRS to the destination CRS.
Definition
qgscoordinatetransform.cpp:304
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition
qgsexception.h:60
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition
qgsmapcanvas.h:96
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition
qgsmapcanvas.cpp:519
QgsMapMouseEvent::QgsMapMouseEvent
QgsMapMouseEvent(QgsMapCanvas *mapCanvas, QMouseEvent *event)
Creates a new QgsMapMouseEvent.
Definition
qgsmapmouseevent.cpp:23
QgsMapMouseEvent::setMapPoint
void setMapPoint(const QgsPointXY &point)
Set the (snapped) point this event points to in map coordinates.
Definition
qgsmapmouseevent.cpp:66
QgsMapMouseEvent::snapToGrid
void snapToGrid(double precision, const QgsCoordinateReferenceSystem &crs)
Snaps the mapPoint to a grid with the given precision.
Definition
qgsmapmouseevent.cpp:72
QgsMapMouseEvent::snapPoint
QgsPointXY snapPoint()
snapPoint will snap the points using the map canvas snapping utils configuration
Definition
qgsmapmouseevent.cpp:41
QgsMapSettings::mapToPixel
const QgsMapToPixel & mapToPixel() const
Definition
qgsmapsettings.h:536
QgsMapToPixel::transformInPlace
void transformInPlace(double &x, double &y) const
Transforms map coordinates to device coordinates.
Definition
qgsmaptopixel.h:124
QgsPointXY
Represents a 2D point.
Definition
qgspointxy.h:62
QgsPointXY::setY
void setY(double y)
Sets the y value of the point.
Definition
qgspointxy.h:132
QgsPointXY::y
double y
Definition
qgspointxy.h:66
QgsPointXY::x
double x
Definition
qgspointxy.h:65
QgsPointXY::setX
void setX(double x)
Sets the x value of the point.
Definition
qgspointxy.h:122
QgsSnappingUtils
Contains configuration of snapping and can return answers to snapping queries.
Definition
qgssnappingutils.h:50
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:265
qgsmapcanvas.h
qgsmapmouseevent.h
qgssnappingconfig.h
qgssnappingutils.h
Generated on
for QGIS API Documentation by
1.17.0