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