QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsplotrubberband.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsplotrubberband.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 "qgsplotrubberband.h"
19
20#include <cmath>
21
22#include "qgsplotcanvas.h"
23
24#include <QGraphicsRectItem>
25#include <QGraphicsScene>
26
27#include "moc_qgsplotrubberband.cpp"
28
33
35{
36 return mCanvas;
37}
38
39QRectF QgsPlotRubberBand::updateRect( QPointF start, QPointF position, bool constrainSquare, bool fromCenter )
40{
41 double x = 0;
42 double y = 0;
43 double width = 0;
44 double height = 0;
45
46 const double dx = position.x() - start.x();
47 const double dy = position.y() - start.y();
48
49 if ( constrainSquare )
50 {
51 if ( std::fabs( dx ) > std::fabs( dy ) )
52 {
53 width = std::fabs( dx );
54 height = width;
55 }
56 else
57 {
58 height = std::fabs( dy );
59 width = height;
60 }
61
62 x = start.x() - ( ( dx < 0 ) ? width : 0 );
63 y = start.y() - ( ( dy < 0 ) ? height : 0 );
64 }
65 else
66 {
67 //not constraining
68 if ( dx < 0 )
69 {
70 x = position.x();
71 width = -dx;
72 }
73 else
74 {
75 x = start.x();
76 width = dx;
77 }
78
79 if ( dy < 0 )
80 {
81 y = position.y();
82 height = -dy;
83 }
84 else
85 {
86 y = start.y();
87 height = dy;
88 }
89 }
90
91 if ( fromCenter )
92 {
93 x = start.x() - width;
94 y = start.y() - height;
95 width *= 2.0;
96 height *= 2.0;
97 }
98
99 return QRectF( x, y, width, height );
100}
101
103{
104 return mPen;
105}
106
108{
109 mPen = pen;
110}
111
113{
114 return mBrush;
115}
116
118{
119 mBrush = brush;
120}
121
122
127
129{
130 if ( mRubberBandItem )
131 {
132 canvas()->scene()->removeItem( mRubberBandItem );
133 delete mRubberBandItem;
134 }
135}
136
137void QgsPlotRectangularRubberBand::start( QPointF position, Qt::KeyboardModifiers )
138{
139 QTransform t;
140 mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
141 mRubberBandItem->setBrush( brush() );
142 mRubberBandItem->setPen( pen() );
143 mRubberBandStartPos = position;
144 t.translate( position.x(), position.y() );
145 mRubberBandItem->setTransform( t );
146 mRubberBandItem->setZValue( 1000 );
147 canvas()->scene()->addItem( mRubberBandItem );
148 canvas()->scene()->update();
149}
150
151void QgsPlotRectangularRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
152{
153 if ( !mRubberBandItem )
154 {
155 return;
156 }
157
158 const bool constrainSquare = modifiers & Qt::ShiftModifier;
159 const bool fromCenter = modifiers & Qt::AltModifier;
160
161 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
162 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
163 QTransform t;
164 t.translate( newRect.x(), newRect.y() );
165 mRubberBandItem->setTransform( t );
166}
167
168QRectF QgsPlotRectangularRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
169{
170 const bool constrainSquare = modifiers & Qt::ShiftModifier;
171 const bool fromCenter = modifiers & Qt::AltModifier;
172
173 if ( mRubberBandItem )
174 {
175 canvas()->scene()->removeItem( mRubberBandItem );
176 delete mRubberBandItem;
177 mRubberBandItem = nullptr;
178 }
179 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
180}
Plot canvas is a class for displaying interactive 2d charts and plots.
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in canvas coordinate ...
void update(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be updated to reflect a temporary ending position (in canvas coordin...
QgsPlotRectangularRubberBand(QgsPlotCanvas *canvas=nullptr)
Constructor for QgsPlotRectangularRubberBand.
QRectF finish(QPointF position=QPointF(), Qt::KeyboardModifiers modifiers=Qt::KeyboardModifiers()) override
Called when a rubber band use has finished and the rubber band is no longer required.
QBrush brush() const
Returns the brush used for drawing the rubber band.
void setBrush(const QBrush &brush)
Sets the brush used for drawing the rubber band.
QgsPlotRubberBand(QgsPlotCanvas *canvas=nullptr)
Constructor for QgsPlotRubberBand.
QgsPlotCanvas * canvas() const
Returns the canvas associated with the rubber band.
QRectF updateRect(QPointF start, QPointF position, bool constrainSquare, bool fromCenter)
Calculates an updated bounding box rectangle from a original start position and new position.
virtual void start(QPointF position, Qt::KeyboardModifiers modifiers)=0
Called when a rubber band should be created at the specified starting position (in canvas coordinate ...
void setPen(const QPen &pen)
Sets the pen used for drawing the rubber band.
QPen pen() const
Returns the pen used for drawing the rubber band.