QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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 "qgsplotcanvas.h"
20
21#include <QGraphicsScene>
22#include <QGraphicsRectItem>
23#include <cmath>
24
26 : mCanvas( canvas )
27{
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
120
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.
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.