QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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#include "moc_qgsplottool.cpp"
20#include "qgsplotcanvas.h"
21#include "qgsplotmouseevent.h"
22
23#include <QWheelEvent>
24#include <QAction>
25
26QgsPlotTool::QgsPlotTool( QgsPlotCanvas *canvas, const QString &name )
27 : QObject( canvas )
28 , mCanvas( canvas )
29 , mToolName( name )
30{
31 connect( mCanvas, &QgsPlotCanvas::willBeDeleted, this, [ = ]
32 {
33 mCanvas = nullptr;
34 } );
35}
36
38{
39 return mCanvas->toMapCoordinates( point );
40}
41
43{
44 return mCanvas->toCanvasCoordinates( point );
45}
46
47bool QgsPlotTool::isClickAndDrag( QPoint startViewPoint, QPoint endViewPoint ) const
48{
49 const int diffX = endViewPoint.x() - startViewPoint.x();
50 const int diffY = endViewPoint.y() - startViewPoint.y();
51 return std::abs( diffX ) >= 2 || std::abs( diffY ) >= 2;
52}
53
54QPointF QgsPlotTool::constrainPointToRect( QPointF point, const QRectF &rect )
55{
56 if ( point.x() < rect.left() )
57 point.setX( rect.left() );
58 else if ( point.x() > rect.right() )
59 point.setX( rect.right() );
60
61 if ( point.y() < rect.top() )
62 point.setY( rect.top() );
63 else if ( point.y() > rect.bottom() )
64 point.setY( rect.bottom() );
65
66 return point;
67}
68
70{
71 if ( mCanvas )
72 mCanvas->unsetTool( this );
73}
74
79
81{
82 // make action and/or button active
83 if ( mAction )
84 mAction->setChecked( true );
85
86 mCanvas->viewport()->setCursor( mCursor );
87 emit activated();
88}
89
91{
92 if ( mAction )
93 mAction->setChecked( false );
94
95 emit deactivated();
96}
97
99{
100 return mCanvas && mCanvas->tool() == this;
101}
102
104{
105 event->ignore();
106}
107
109{
110 event->ignore();
111}
112
114{
115 event->ignore();
116}
117
119{
120 event->ignore();
121}
122
123void QgsPlotTool::wheelEvent( QWheelEvent *event )
124{
125 event->ignore();
126}
127
128void QgsPlotTool::keyPressEvent( QKeyEvent *event )
129{
130 event->ignore();
131}
132
133void QgsPlotTool::keyReleaseEvent( QKeyEvent *event )
134{
135 event->ignore();
136}
137
138bool QgsPlotTool::gestureEvent( QGestureEvent * )
139{
140 return false;
141}
142
144{
145 return false;
146}
147
149{
150 return mCanvas;
151}
152
153void QgsPlotTool::setAction( QAction *action )
154{
155 mAction = action;
156}
157
159{
160 return mAction;
161}
162
163void QgsPlotTool::setCursor( const QCursor &cursor )
164{
165 mCursor = cursor;
166}
167
169{
170 return false;
171}
QFlags< PlotToolFlag > PlotToolFlags
Definition qgis.h:3821
Plot canvas is a class for displaying interactive 2d charts and plots.
virtual QgsPointXY toCanvasCoordinates(const QgsPoint &point) const
Converts a point in map coordinates to the associated canvas point.
QgsPlotTool * tool()
Returns the currently active tool.
void unsetTool(QgsPlotTool *tool)
Unset the current tool.
void willBeDeleted()
Emitted in the destructor when the canvas is about to be deleted, but is still in a perfectly valid s...
virtual QgsPoint toMapCoordinates(const QgsPointXY &point) const
Converts a point on the canvas to the associated map coordinate.
A QgsPlotMouseEvent is the result of a user interaction with the mouse on 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.
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.
A class to represent a 2D point.
Definition qgspointxy.h:60
double x
Definition qgspointxy.h:63
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49