24#include <QGraphicsRectItem>
25#include <QGraphicsScene>
27#include "moc_qgsplotrubberband.cpp"
45 const double dx = position.x() -
start.x();
46 const double dy = position.y() -
start.y();
48 if ( constrainSquare )
50 if ( std::fabs( dx ) > std::fabs( dy ) )
52 width = std::fabs( dx );
57 height = std::fabs( dy );
61 x =
start.x() - ( ( dx < 0 ) ? width : 0 );
62 y =
start.y() - ( ( dy < 0 ) ? height : 0 );
92 x =
start.x() - width;
93 y =
start.y() - height;
98 return QRectF( x, y, width, height );
128 if ( mRubberBandItem )
130 canvas()->scene()->removeItem( mRubberBandItem );
131 delete mRubberBandItem;
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();
151 if ( !mRubberBandItem )
156 const bool constrainSquare = modifiers & Qt::ShiftModifier;
157 const bool fromCenter = modifiers & Qt::AltModifier;
159 const QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
160 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
162 t.translate( newRect.x(), newRect.y() );
163 mRubberBandItem->setTransform( t );
168 const bool constrainSquare = modifiers & Qt::ShiftModifier;
169 const bool fromCenter = modifiers & Qt::AltModifier;
171 if ( mRubberBandItem )
173 canvas()->scene()->removeItem( mRubberBandItem );
174 delete mRubberBandItem;
175 mRubberBandItem =
nullptr;
177 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.