QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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 "qgsplotcanvas.h"
20#include "qgsplotmouseevent.h"
21
22#include <QWheelEvent>
23#include <QAction>
24
25QgsPlotTool::QgsPlotTool( QgsPlotCanvas *canvas, const QString &name )
26 : QObject( canvas )
27 , mCanvas( canvas )
28 , mToolName( name )
29{
30 connect( mCanvas, &QgsPlotCanvas::willBeDeleted, this, [ = ]
31 {
32 mCanvas = nullptr;
33 } );
34}
35
37{
38 return mCanvas->toMapCoordinates( point );
39}
40
42{
43 return mCanvas->toCanvasCoordinates( point );
44}
45
46bool QgsPlotTool::isClickAndDrag( QPoint startViewPoint, QPoint endViewPoint ) const
47{
48 const int diffX = endViewPoint.x() - startViewPoint.x();
49 const int diffY = endViewPoint.y() - startViewPoint.y();
50 return std::abs( diffX ) >= 2 || std::abs( diffY ) >= 2;
51}
52
53QPointF QgsPlotTool::constrainPointToRect( QPointF point, const QRectF &rect )
54{
55 if ( point.x() < rect.left() )
56 point.setX( rect.left() );
57 else if ( point.x() > rect.right() )
58 point.setX( rect.right() );
59
60 if ( point.y() < rect.top() )
61 point.setY( rect.top() );
62 else if ( point.y() > rect.bottom() )
63 point.setY( rect.bottom() );
64
65 return point;
66}
67
69{
70 if ( mCanvas )
71 mCanvas->unsetTool( this );
72}
73
74Qgis::PlotToolFlags QgsPlotTool::flags() const
75{
76 return Qgis::PlotToolFlags();
77}
78
80{
81 // make action and/or button active
82 if ( mAction )
83 mAction->setChecked( true );
84
85 mCanvas->viewport()->setCursor( mCursor );
86 emit activated();
87}
88
90{
91 if ( mAction )
92 mAction->setChecked( false );
93
94 emit deactivated();
95}
96
98{
99 return mCanvas && mCanvas->tool() == this;
100}
101
103{
104 event->ignore();
105}
106
108{
109 event->ignore();
110}
111
113{
114 event->ignore();
115}
116
118{
119 event->ignore();
120}
121
122void QgsPlotTool::wheelEvent( QWheelEvent *event )
123{
124 event->ignore();
125}
126
127void QgsPlotTool::keyPressEvent( QKeyEvent *event )
128{
129 event->ignore();
130}
131
132void QgsPlotTool::keyReleaseEvent( QKeyEvent *event )
133{
134 event->ignore();
135}
136
137bool QgsPlotTool::gestureEvent( QGestureEvent * )
138{
139 return false;
140}
141
143{
144 return false;
145}
146
148{
149 return mCanvas;
150}
151
152void QgsPlotTool::setAction( QAction *action )
153{
154 mAction = action;
155}
156
158{
159 return mAction;
160}
161
162void QgsPlotTool::setCursor( const QCursor &cursor )
163{
164 mCursor = cursor;
165}
166
168{
169 return false;
170}
Plot canvas is a class for displaying interactive 2d charts and plots.
Definition: qgsplotcanvas.h:54
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.
Definition: qgsplottool.cpp:41
virtual bool gestureEvent(QGestureEvent *event)
Gesture event for overriding.
QCursor mCursor
Cursor used by tool.
Definition: qgsplottool.h:276
bool isActive() const
Returns true if this tool is the current tool active on the plot canvas.
Definition: qgsplottool.cpp:97
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.
Definition: qgsplottool.cpp:89
QgsPlotCanvas * mCanvas
The pointer to the canvas.
Definition: qgsplottool.h:267
QgsPlotTool(QgsPlotCanvas *canvas, const QString &name)
Constructor takes a plot canvas as a parameter.
Definition: qgsplottool.cpp:25
~QgsPlotTool() override
Definition: qgsplottool.cpp:68
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...
Definition: qgsplottool.cpp:46
QgsPoint toMapCoordinates(const QgsPointXY &point) const
Converts a point on the canvas to the associated map coordinate.
Definition: qgsplottool.cpp:36
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.
Definition: qgsplottool.h:273
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.
Definition: qgsplottool.cpp:79
static QPointF constrainPointToRect(QPointF point, const QRectF &rect)
Constrains a point to force it to fall within the specified rectangle.
Definition: qgsplottool.cpp:53
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.
Definition: qgsplottool.cpp:74
A class to represent a 2D point.
Definition: qgspointxy.h:59
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49