QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsplottransienttools.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsplottransienttools.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 "qgsplottransienttools.h"
19 #include "qgsplotcanvas.h"
20 #include "qgsplotmouseevent.h"
21 #include "qgsapplication.h"
22 
23 #include <QKeyEvent>
24 #include <QApplication>
25 
26 //
27 // QgsPlotToolTemporaryKeyPan
28 //
29 
31  : QgsPlotTool( canvas, tr( "Pan" ) )
32 {
33  setCursor( Qt::ClosedHandCursor );
34 }
35 
37 {
38  canvas()->panContentsBy( event->x() - mLastMousePos.x(),
39  event->y() - mLastMousePos.y() );
40  mLastMousePos = event->pos();
41 }
42 
44 {
45  if ( event->key() == Qt::Key_Space && !event->isAutoRepeat() )
46  {
47  canvas()->setTool( mPreviousTool );
48  }
49 }
50 
52 {
53  mLastMousePos = canvas()->mapFromGlobal( QCursor::pos() );
54  mPreviousTool = canvas()->tool();
56 }
57 
58 
59 //
60 // QgsPlotToolTemporaryMousePan
61 //
62 
64  : QgsPlotTool( canvas, tr( "Pan" ) )
65 {
66  setCursor( Qt::ClosedHandCursor );
67 }
68 
70 {
71  canvas()->panContentsBy( event->x() - mLastMousePos.x(),
72  event->y() - mLastMousePos.y() );
73  mLastMousePos = event->pos();
74 }
75 
77 {
78  if ( event->button() == Qt::MiddleButton )
79  {
80  canvas()->setTool( mPreviousTool );
81  }
82 }
83 
85 {
86  mLastMousePos = canvas()->mapFromGlobal( QCursor::pos() );
87  mPreviousTool = canvas()->tool();
89 }
90 
91 
92 //
93 // QgsPlotToolTemporaryKeyZoom
94 //
95 
97  : QgsPlotToolZoom( canvas )
98 {
99 }
100 
102 {
104  if ( !event->isAccepted() )
105  return;
106 
107  //end of temporary zoom tool
108  if ( mDeactivateOnMouseRelease )
109  canvas()->setTool( mPreviousViewTool );
110 }
111 
113 {
114  if ( event->isAutoRepeat() )
115  {
116  event->ignore();
117  return;
118  }
119 
120  //respond to changes in ctrl key status
121  if ( !( event->modifiers() & Qt::ControlModifier ) )
122  {
123  if ( !mMarqueeZoom )
124  {
125  //space pressed, but control key was released, end of temporary zoom tool
126  canvas()->setTool( mPreviousViewTool );
127  }
128  else
129  {
130  mDeactivateOnMouseRelease = true;
131  }
132  }
133  else
134  {
135  //both control and space pressed
136  //set cursor to zoom in/out depending on alt key status
137  updateCursor( event->modifiers() );
138  if ( event->key() == Qt::Key_Space )
139  {
140  mDeactivateOnMouseRelease = false;
141  }
142  }
143 }
144 
146 {
147  if ( event->isAutoRepeat() )
148  {
149  event->ignore();
150  return;
151  }
152 
153  if ( event->key() == Qt::Key_Space )
154  {
155  //temporary keyboard-based zoom tool is active and space key has been released
156  if ( !mMarqueeZoom )
157  {
158  //not mid-way through an operation, so immediately switch tool back
159  canvas()->setTool( mPreviousViewTool );
160  }
161  else
162  {
163  mDeactivateOnMouseRelease = true;
164  }
165  }
166  else
167  {
168  updateCursor( event->modifiers() );
169  event->ignore();
170  }
171 }
172 
174 {
175  mDeactivateOnMouseRelease = false;
176  mPreviousViewTool = canvas()->tool();
178  updateCursor( QApplication::keyboardModifiers() );
179 }
180 
181 void QgsPlotToolTemporaryKeyZoom::updateCursor( Qt::KeyboardModifiers modifiers )
182 {
183  canvas()->viewport()->setCursor( ( modifiers & Qt::AltModifier ) ?
184  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) :
185  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
186 }
qgsplotmouseevent.h
QgsPlotToolTemporaryMousePan::plotMoveEvent
void plotMoveEvent(QgsPlotMouseEvent *event) override
Mouse move event for overriding.
Definition: qgsplottransienttools.cpp:69
QgsPlotCanvas::tool
QgsPlotTool * tool()
Returns the currently active tool.
Definition: qgsplotcanvas.cpp:257
QgsPlotTool::setCursor
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
Definition: qgsplottool.cpp:162
QgsApplication::getThemeCursor
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
Definition: qgsapplication.cpp:762
qgsplotcanvas.h
QgsPlotToolZoom::mMarqueeZoom
bool mMarqueeZoom
Will be true will marquee zoom operation is in progress.
Definition: qgsplottoolzoom.h:92
QgsPlotToolTemporaryKeyZoom::QgsPlotToolTemporaryKeyZoom
QgsPlotToolTemporaryKeyZoom(QgsPlotCanvas *canvas)
Constructor for QgsPlotToolTemporaryKeyZoom.
Definition: qgsplottransienttools.cpp:96
QgsPlotToolTemporaryKeyPan::activate
void activate() override
Called when the tool is set as the currently active plot tool.
Definition: qgsplottransienttools.cpp:51
QgsPlotToolTemporaryMousePan::activate
void activate() override
Called when the tool is set as the currently active plot tool.
Definition: qgsplottransienttools.cpp:84
qgsapplication.h
QgsPlotToolTemporaryKeyZoom::activate
void activate() override
Called when the tool is set as the currently active plot tool.
Definition: qgsplottransienttools.cpp:173
QgsPlotToolZoom::plotReleaseEvent
void plotReleaseEvent(QgsPlotMouseEvent *event) override
Mouse release event for overriding.
Definition: qgsplottoolzoom.cpp:71
QgsPlotToolTemporaryKeyZoom::plotReleaseEvent
void plotReleaseEvent(QgsPlotMouseEvent *event) override
Mouse release event for overriding.
Definition: qgsplottransienttools.cpp:101
QgsPlotToolTemporaryKeyZoom::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *event) override
Key release event for overriding.
Definition: qgsplottransienttools.cpp:145
QgsPlotTool
Abstract base class for all interactive plot tools.
Definition: qgsplottool.h:57
QgsPlotCanvas::setTool
void setTool(QgsPlotTool *tool)
Sets the interactive tool currently being used on the canvas.
Definition: qgsplotcanvas.cpp:229
QgsPlotCanvas
Plot canvas is a class for displaying interactive 2d charts and plots.
Definition: qgsplotcanvas.h:53
QgsPlotToolTemporaryMousePan::QgsPlotToolTemporaryMousePan
QgsPlotToolTemporaryMousePan(QgsPlotCanvas *canvas)
Constructor for QgsPlotToolTemporaryMousePan.
Definition: qgsplottransienttools.cpp:63
QgsPlotTool::activate
virtual void activate()
Called when the tool is set as the currently active plot tool.
Definition: qgsplottool.cpp:79
qgsplottransienttools.h
QgsPlotToolTemporaryKeyPan::plotMoveEvent
void plotMoveEvent(QgsPlotMouseEvent *event) override
Mouse move event for overriding.
Definition: qgsplottransienttools.cpp:36
QgsPlotToolTemporaryKeyPan::QgsPlotToolTemporaryKeyPan
QgsPlotToolTemporaryKeyPan(QgsPlotCanvas *canvas)
Constructor for QgsPlotToolTemporaryKeyPan.
Definition: qgsplottransienttools.cpp:30
QgsPlotCanvas::panContentsBy
virtual void panContentsBy(double dx, double dy)
Pans the plot contents by dx, dy in canvas units.
Definition: qgsplotcanvas.cpp:277
QgsPlotToolZoom
Plot tool for zooming into and out of the plot.
Definition: qgsplottoolzoom.h:34
QgsPlotMouseEvent
A QgsPlotMouseEvent is the result of a user interaction with the mouse on a QgsPlotCanvas.
Definition: qgsplotmouseevent.h:39
QgsPlotToolTemporaryMousePan::plotReleaseEvent
void plotReleaseEvent(QgsPlotMouseEvent *event) override
Mouse release event for overriding.
Definition: qgsplottransienttools.cpp:76
QgsPlotTool::canvas
QgsPlotCanvas * canvas() const
Returns the tool's plot canvas.
Definition: qgsplottool.cpp:147
QgsPlotToolTemporaryKeyPan::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *event) override
Key release event for overriding.
Definition: qgsplottransienttools.cpp:43
QgsPlotToolTemporaryKeyZoom::keyPressEvent
void keyPressEvent(QKeyEvent *event) override
Key press event for overriding.
Definition: qgsplottransienttools.cpp:112