QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
25 QgsPlotTool::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 
46 bool 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 
53 QPointF 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 
74 Qgis::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 
122 void QgsPlotTool::wheelEvent( QWheelEvent *event )
123 {
124  event->ignore();
125 }
126 
127 void QgsPlotTool::keyPressEvent( QKeyEvent *event )
128 {
129  event->ignore();
130 }
131 
132 void QgsPlotTool::keyReleaseEvent( QKeyEvent *event )
133 {
134  event->ignore();
135 }
136 
137 bool QgsPlotTool::gestureEvent( QGestureEvent * )
138 {
139  return false;
140 }
141 
143 {
144  return false;
145 }
146 
148 {
149  return mCanvas;
150 }
151 
152 void QgsPlotTool::setAction( QAction *action )
153 {
154  mAction = action;
155 }
156 
158 {
159  return mAction;
160 }
161 
162 void QgsPlotTool::setCursor( const QCursor &cursor )
163 {
164  mCursor = cursor;
165 }
166 
168 {
169  return false;
170 }
qgsplotmouseevent.h
QgsPlotTool::populateContextMenuWithEvent
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-...
Definition: qgsplottool.cpp:167
QgsPlotTool::toCanvasCoordinates
QgsPointXY toCanvasCoordinates(const QgsPoint &point) const
Converts a point in map coordinates to the associated canvas point.
Definition: qgsplottool.cpp:41
QgsPlotTool::mCursor
QCursor mCursor
Cursor used by tool.
Definition: qgsplottool.h:276
QgsPlotCanvas::tool
QgsPlotTool * tool()
Returns the currently active tool.
Definition: qgsplotcanvas.cpp:257
QgsPlotTool::activated
void activated()
Emitted when the tool is activated.
QgsPlotTool::plotMoveEvent
virtual void plotMoveEvent(QgsPlotMouseEvent *event)
Mouse move event for overriding.
Definition: qgsplottool.cpp:102
QgsPlotTool::setCursor
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
Definition: qgsplottool.cpp:162
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
qgsplotcanvas.h
QgsPlotTool::deactivate
virtual void deactivate()
Called when the tool is being deactivated.
Definition: qgsplottool.cpp:89
QgsPlotCanvas::toCanvasCoordinates
virtual QgsPointXY toCanvasCoordinates(const QgsPoint &point) const
Converts a point in map coordinates to the associated canvas point.
Definition: qgsplotcanvas.cpp:272
QgsPlotCanvas::willBeDeleted
void willBeDeleted()
Emitted in the destructor when the canvas is about to be deleted, but is still in a perfectly valid s...
QgsPlotTool::keyPressEvent
virtual void keyPressEvent(QKeyEvent *event)
Key press event for overriding.
Definition: qgsplottool.cpp:127
QgsPlotTool::gestureEvent
virtual bool gestureEvent(QGestureEvent *event)
Gesture event for overriding.
Definition: qgsplottool.cpp:137
QgsPlotTool::flags
virtual Qgis::PlotToolFlags flags() const
Returns the flags for the plot tool.
Definition: qgsplottool.cpp:74
QgsPlotTool::plotDoubleClickEvent
virtual void plotDoubleClickEvent(QgsPlotMouseEvent *event)
Mouse double-click event for overriding.
Definition: qgsplottool.cpp:107
QgsPlotTool::mCanvas
QgsPlotCanvas * mCanvas
The pointer to the canvas.
Definition: qgsplottool.h:267
QgsPlotTool::keyReleaseEvent
virtual void keyReleaseEvent(QKeyEvent *event)
Key release event for overriding.
Definition: qgsplottool.cpp:132
QgsPlotTool::canvasToolTipEvent
virtual bool canvasToolTipEvent(QHelpEvent *event)
Tooltip event for overriding.
Definition: qgsplottool.cpp:142
qgsplottool.h
QgsPlotCanvas
Plot canvas is a class for displaying interactive 2d charts and plots.
Definition: qgsplotcanvas.h:53
QgsPlotTool::toMapCoordinates
QgsPoint toMapCoordinates(const QgsPointXY &point) const
Converts a point on the canvas to the associated map coordinate.
Definition: qgsplottool.cpp:36
QgsPlotTool::mAction
QPointer< QAction > mAction
Optional action associated with tool.
Definition: qgsplottool.h:273
QgsPlotTool::activate
virtual void activate()
Called when the tool is set as the currently active plot tool.
Definition: qgsplottool.cpp:79
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsPlotCanvas::unsetTool
void unsetTool(QgsPlotTool *tool)
Unset the current tool.
Definition: qgsplotcanvas.cpp:247
QgsPlotTool::setAction
void setAction(QAction *action)
Associates an action with this tool.
Definition: qgsplottool.cpp:152
QgsPlotTool::QgsPlotTool
QgsPlotTool(QgsPlotCanvas *canvas, const QString &name)
Constructor takes a plot canvas as a parameter.
Definition: qgsplottool.cpp:25
QgsPlotTool::action
QAction * action()
Returns the action associated with the tool or nullptr if no action is associated.
Definition: qgsplottool.cpp:157
QgsPlotCanvas::toMapCoordinates
virtual QgsPoint toMapCoordinates(const QgsPointXY &point) const
Converts a point on the canvas to the associated map coordinate.
Definition: qgsplotcanvas.cpp:267
QgsPlotTool::plotReleaseEvent
virtual void plotReleaseEvent(QgsPlotMouseEvent *event)
Mouse release event for overriding.
Definition: qgsplottool.cpp:117
QgsPlotMouseEvent
A QgsPlotMouseEvent is the result of a user interaction with the mouse on a QgsPlotCanvas.
Definition: qgsplotmouseevent.h:39
QgsPlotTool::~QgsPlotTool
~QgsPlotTool() override
Definition: qgsplottool.cpp:68
QgsPlotTool::canvas
QgsPlotCanvas * canvas() const
Returns the tool's plot canvas.
Definition: qgsplottool.cpp:147
QgsPlotTool::wheelEvent
virtual void wheelEvent(QWheelEvent *event)
Mouse wheel event for overriding.
Definition: qgsplottool.cpp:122
QgsPlotTool::constrainPointToRect
static QPointF constrainPointToRect(QPointF point, const QRectF &rect)
Constrains a point to force it to fall within the specified rectangle.
Definition: qgsplottool.cpp:53
QgsPlotTool::deactivated
void deactivated()
Emitted when the tool is deactivated.
QgsPlotTool::isClickAndDrag
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
QgsPlotTool::plotPressEvent
virtual void plotPressEvent(QgsPlotMouseEvent *event)
Mouse press event for overriding.
Definition: qgsplottool.cpp:112
QgsPlotTool::isActive
bool isActive() const
Returns true if this tool is the current tool active on the plot canvas.
Definition: qgsplottool.cpp:97