QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsplottoolxaxiszoom.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsplottoolxaxiszoom.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
19#include "moc_qgsplottoolxaxiszoom.cpp"
20#include "qgsplotcanvas.h"
22#include <QWheelEvent>
23
25 : QgsPlotToolZoom( canvas )
26 , mElevationCanvas( canvas )
27{
28 mToolName = tr( "Zoom X Axis" );
29}
30
32
33QPointF QgsPlotToolXAxisZoom::constrainStartPoint( QPointF scenePoint ) const
34{
35 if ( mElevationCanvas->lockAxisScales() )
36 {
37 return scenePoint;
38 }
39 else
40 {
41 // force the rubber band to take the whole vertical area
42 QRectF plotArea = mElevationCanvas->plotArea();
43 return QPointF( scenePoint.x(), plotArea.top() );
44 }
45}
46
47QPointF QgsPlotToolXAxisZoom::constrainMovePoint( QPointF scenePoint ) const
48{
49 const QRectF plotArea = mElevationCanvas->plotArea();
50 if ( mElevationCanvas->lockAxisScales() )
51 {
52 const double selectedDistanceRange = std::fabs( scenePoint.x() - mRubberBandStartPos.x() );
53 // ensure the same aspect ratio is retained
54 const double calculatedElevationRange = plotArea.height() / plotArea.width() * selectedDistanceRange;
55 return QPointF( scenePoint.x(), mRubberBandStartPos.y() + calculatedElevationRange );
56 }
57 else
58 {
59 // force the rubber band to take the whole vertical area
60 return QPointF( scenePoint.x(), plotArea.bottom() );
61 }
62}
63
64QRectF QgsPlotToolXAxisZoom::constrainBounds( const QRectF &sceneBounds ) const
65{
66 const QRectF plotArea = mElevationCanvas->plotArea();
67
68 if ( mElevationCanvas->lockAxisScales() )
69 {
70 // constraint has already been applied
71 return sceneBounds;
72 }
73 else
74 {
75 // retain current vertical rect
76 return QRectF( sceneBounds.left(), plotArea.top(), sceneBounds.width(), plotArea.height() );
77 }
78}
79
80void QgsPlotToolXAxisZoom::zoomOutClickOn( QPointF scenePoint )
81{
82 //just a click, so zoom to clicked point and recenter
83 const QRectF plotArea = mElevationCanvas->plotArea();
84 canvas()->centerPlotOn( scenePoint.x(), plotArea.center().y() );
85 mElevationCanvas->scalePlot( 0.5, 1 );
86}
87
88void QgsPlotToolXAxisZoom::zoomInClickOn( QPointF scenePoint )
89{
90 //just a click, so zoom to clicked point and recenter
91 const QRectF plotArea = mElevationCanvas->plotArea();
92 canvas()->centerPlotOn( scenePoint.x(), plotArea.center().y() );
93 mElevationCanvas->scalePlot( 2.0, 1 );
94}
A canvas for elevation profiles.
void scalePlot(double factor) override
Scales the plot by a specified scale factor.
bool lockAxisScales() const
Returns true if the distance and elevation scales are locked to each other.
QRectF plotArea() const
Returns the interior rectangle representing the surface of the plot, in canvas coordinates.
virtual void centerPlotOn(double x, double y)
Centers the plot on the plot point corresponding to x, y in canvas units.
QgsPlotToolXAxisZoom(QgsElevationProfileCanvas *canvas)
Constructor for QgsPlotToolXAxisZoom, with the associated canvas.
~QgsPlotToolXAxisZoom() override
void zoomOutClickOn(QPointF scenePoint) override
Handles a zoom out click on the given point.
QRectF constrainBounds(const QRectF &sceneBounds) const override
Applies constraints to the overall bounds of the rubber band.
QPointF constrainStartPoint(QPointF scenePoint) const override
Applies constraints to the start point of the zoom rubber band.
QPointF constrainMovePoint(QPointF scenePoint) const override
Applies constraints to a move point of the zoom rubber band.
void zoomInClickOn(QPointF scenePoint) override
Handles a zoom out click on the given point.
Plot tool for zooming into and out of the plot.
QPointF mRubberBandStartPos
Start position for drag, in scene coordinates.
QgsPlotCanvas * canvas() const
Returns the tool's plot canvas.
QString mToolName
Translated name of the map tool.