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