24#include <QGraphicsRectItem>
25#include <QGraphicsScene>
27#include "moc_qgsplotrubberband.cpp"
46 const double dx = position.x() -
start.x();
47 const double dy = position.y() -
start.y();
49 if ( constrainSquare )
51 if ( std::fabs( dx ) > std::fabs( dy ) )
53 width = std::fabs( dx );
58 height = std::fabs( dy );
62 x =
start.x() - ( ( dx < 0 ) ? width : 0 );
63 y =
start.y() - ( ( dy < 0 ) ? height : 0 );
93 x =
start.x() - width;
94 y =
start.y() - height;
99 return QRectF( x, y, width, height );
130 if ( mRubberBandItem )
132 canvas()->scene()->removeItem( mRubberBandItem );
133 delete mRubberBandItem;
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();
153 if ( !mRubberBandItem )
158 const bool constrainSquare = modifiers & Qt::ShiftModifier;
159 const bool fromCenter = modifiers & Qt::AltModifier;
161 const QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
162 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
164 t.translate( newRect.x(), newRect.y() );
165 mRubberBandItem->setTransform( t );
170 const bool constrainSquare = modifiers & Qt::ShiftModifier;
171 const bool fromCenter = modifiers & Qt::AltModifier;
173 if ( mRubberBandItem )
175 canvas()->scene()->removeItem( mRubberBandItem );
176 delete mRubberBandItem;
177 mRubberBandItem =
nullptr;
179 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
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.
~QgsPlotRectangularRubberBand() override
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.