QGIS API Documentation 3.41.0-Master (3440c17df1d)
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#include "moc_qgsplotrubberband.cpp"
20#include "qgsplotcanvas.h"
21
22#include <QGraphicsScene>
23#include <QGraphicsRectItem>
24#include <cmath>
25
27 : mCanvas( canvas )
28{
29
30}
31
33{
34 return mCanvas;
35}
36
37QRectF QgsPlotRubberBand::updateRect( QPointF start, QPointF position, bool constrainSquare, bool fromCenter )
38{
39 double x = 0;
40 double y = 0;
41 double width = 0;
42 double height = 0;
43
44 const double dx = position.x() - start.x();
45 const double dy = position.y() - start.y();
46
47 if ( constrainSquare )
48 {
49 if ( std::fabs( dx ) > std::fabs( dy ) )
50 {
51 width = std::fabs( dx );
52 height = width;
53 }
54 else
55 {
56 height = std::fabs( dy );
57 width = height;
58 }
59
60 x = start.x() - ( ( dx < 0 ) ? width : 0 );
61 y = start.y() - ( ( dy < 0 ) ? height : 0 );
62 }
63 else
64 {
65 //not constraining
66 if ( dx < 0 )
67 {
68 x = position.x();
69 width = -dx;
70 }
71 else
72 {
73 x = start.x();
74 width = dx;
75 }
76
77 if ( dy < 0 )
78 {
79 y = position.y();
80 height = -dy;
81 }
82 else
83 {
84 y = start.y();
85 height = dy;
86 }
87 }
88
89 if ( fromCenter )
90 {
91 x = start.x() - width;
92 y = start.y() - height;
93 width *= 2.0;
94 height *= 2.0;
95 }
96
97 return QRectF( x, y, width, height );
98}
99
101{
102 return mPen;
103}
104
105void QgsPlotRubberBand::setPen( const QPen &pen )
106{
107 mPen = pen;
108}
109
111{
112 return mBrush;
113}
114
115void QgsPlotRubberBand::setBrush( const QBrush &brush )
116{
117 mBrush = brush;
118}
119
120
121
126
128{
129 if ( mRubberBandItem )
130 {
131 canvas()->scene()->removeItem( mRubberBandItem );
132 delete mRubberBandItem;
133 }
134}
135
136void QgsPlotRectangularRubberBand::start( QPointF position, Qt::KeyboardModifiers )
137{
138 QTransform t;
139 mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
140 mRubberBandItem->setBrush( brush() );
141 mRubberBandItem->setPen( pen() );
142 mRubberBandStartPos = position;
143 t.translate( position.x(), position.y() );
144 mRubberBandItem->setTransform( t );
145 mRubberBandItem->setZValue( 1000 );
146 canvas()->scene()->addItem( mRubberBandItem );
147 canvas()->scene()->update();
148}
149
150void QgsPlotRectangularRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
151{
152 if ( !mRubberBandItem )
153 {
154 return;
155 }
156
157 const bool constrainSquare = modifiers & Qt::ShiftModifier;
158 const bool fromCenter = modifiers & Qt::AltModifier;
159
160 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
161 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
162 QTransform t;
163 t.translate( newRect.x(), newRect.y() );
164 mRubberBandItem->setTransform( t );
165}
166
167QRectF QgsPlotRectangularRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
168{
169 const bool constrainSquare = modifiers & Qt::ShiftModifier;
170 const bool fromCenter = modifiers & Qt::AltModifier;
171
172 if ( mRubberBandItem )
173 {
174 canvas()->scene()->removeItem( mRubberBandItem );
175 delete mRubberBandItem;
176 mRubberBandItem = nullptr;
177 }
178 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
179}
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.
QgsPlotRubberBand is an abstract base class for temporary rubber band items in various shapes,...
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.