QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
qgsplottool.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsplottool.cpp
3 ---------------
4 begin : March 2022
5 copyright : (C) 2022 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsplottool.h"
19
20#include "qgsplotcanvas.h"
21#include "qgsplotmouseevent.h"
22
23#include <QAction>
24#include <QWheelEvent>
25
26#include "moc_qgsplottool.cpp"
27
29 : QObject( canvas )
30 , mCanvas( canvas )
31 , mToolName( name )
32{
33 connect( mCanvas, &QgsPlotCanvas::willBeDeleted, this, [this] {
34 mCanvas = nullptr;
35 } );
36}
37
39{
40 return mCanvas->toMapCoordinates( point );
41}
42
44{
45 return mCanvas->toCanvasCoordinates( point );
46}
47
48bool QgsPlotTool::isClickAndDrag( QPoint startViewPoint, QPoint endViewPoint ) const
49{
50 const int diffX = endViewPoint.x() - startViewPoint.x();
51 const int diffY = endViewPoint.y() - startViewPoint.y();
52 return std::abs( diffX ) >= 2 || std::abs( diffY ) >= 2;
53}
54
55QPointF QgsPlotTool::constrainPointToRect( QPointF point, const QRectF &rect )
56{
57 if ( point.x() < rect.left() )
58 point.setX( rect.left() );
59 else if ( point.x() > rect.right() )
60 point.setX( rect.right() );
61
62 if ( point.y() < rect.top() )
63 point.setY( rect.top() );
64 else if ( point.y() > rect.bottom() )
65 point.setY( rect.bottom() );
66
67 return point;
68}
69
71{
72 if ( mCanvas )
73 mCanvas->unsetTool( this );
74}
75
80
82{
83 // make action and/or button active
84 if ( mAction )
85 mAction->setChecked( true );
86
87 mCanvas->viewport()->setCursor( mCursor );
88 emit activated();
89}
90
92{
93 if ( mAction )
94 mAction->setChecked( false );
95
96 emit deactivated();
97}
98
100{
101 return mCanvas && mCanvas->tool() == this;
102}
103
105{
106 event->ignore();
107}
108
110{
111 event->ignore();
112}
113
115{
116 event->ignore();
117}
118
120{
121 event->ignore();
122}
123
124void QgsPlotTool::wheelEvent( QWheelEvent *event )
125{
126 event->ignore();
127}
128
129void QgsPlotTool::keyPressEvent( QKeyEvent *event )
130{
131 event->ignore();
132}
133
134void QgsPlotTool::keyReleaseEvent( QKeyEvent *event )
135{
136 event->ignore();
137}
138
139bool QgsPlotTool::gestureEvent( QGestureEvent * )
140{
141 return false;
142}
143
145{
146 return false;
147}
148
150{
151 return mCanvas;
152}
153
155{
156 mAction = action;
157}
158
160{
161 return mAction;
162}
163
164void QgsPlotTool::setCursor( const QCursor &cursor )
165{
166 mCursor = cursor;
167}
168
170{
171 return false;
172}
QFlags< PlotToolFlag > PlotToolFlags
Definition qgis.h:4112
Plot canvas is a class for displaying interactive 2d charts and plots.
void willBeDeleted()
Emitted in the destructor when the canvas is about to be deleted, but is still in a perfectly valid s...
A mouse event which is the result of a user interaction with a QgsPlotCanvas.
QgsPointXY toCanvasCoordinates(const QgsPoint &point) const
Converts a point in map coordinates to the associated canvas point.
virtual bool gestureEvent(QGestureEvent *event)
Gesture event for overriding.
QCursor mCursor
Cursor used by tool.
bool isActive() const
Returns true if this tool is the current tool active on the plot canvas.
virtual bool populateContextMenuWithEvent(QMenu *menu, QgsPlotMouseEvent *event)
Allows the tool to populate and customize the given menu, prior to showing it in response to a right-...
QgsPlotCanvas * canvas() const
Returns the tool's plot canvas.
virtual void plotDoubleClickEvent(QgsPlotMouseEvent *event)
Mouse double-click event for overriding.
virtual void deactivate()
Called when the tool is being deactivated.
QgsPlotCanvas * mCanvas
The pointer to the canvas.
QgsPlotTool(QgsPlotCanvas *canvas, const QString &name)
Constructor takes a plot canvas as a parameter.
~QgsPlotTool() override
void activated()
Emitted when the tool is activated.
virtual void wheelEvent(QWheelEvent *event)
Mouse wheel event for overriding.
void setAction(QAction *action)
Associates an action with this tool.
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
bool isClickAndDrag(QPoint startViewPoint, QPoint endViewPoint) const
Returns true if a mouse press/release operation which started at startViewPoint and ended at endViewP...
QgsPoint toMapCoordinates(const QgsPointXY &point) const
Converts a point on the canvas to the associated map coordinate.
virtual void plotMoveEvent(QgsPlotMouseEvent *event)
Mouse move event for overriding.
void deactivated()
Emitted when the tool is deactivated.
QPointer< QAction > mAction
Optional action associated with tool.
QAction * action()
Returns the action associated with the tool or nullptr if no action is associated.
QString mToolName
Translated name of the map tool.
virtual bool canvasToolTipEvent(QHelpEvent *event)
Tooltip event for overriding.
virtual void activate()
Called when the tool is set as the currently active plot tool.
static QPointF constrainPointToRect(QPointF point, const QRectF &rect)
Constrains a point to force it to fall within the specified rectangle.
virtual void keyReleaseEvent(QKeyEvent *event)
Key release event for overriding.
virtual void keyPressEvent(QKeyEvent *event)
Key press event for overriding.
virtual void plotPressEvent(QgsPlotMouseEvent *event)
Mouse press event for overriding.
virtual void plotReleaseEvent(QgsPlotMouseEvent *event)
Mouse release event for overriding.
virtual Qgis::PlotToolFlags flags() const
Returns the flags for the plot tool.
Represents a 2D point.
Definition qgspointxy.h:60
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49