22#include <QGraphicsRectItem>
24#include "moc_qgsmodelviewrubberband.cpp"
43 const double dx = position.x() -
start.x();
44 const double dy = position.y() -
start.y();
46 if ( constrainSquare )
48 if ( std::fabs( dx ) > std::fabs( dy ) )
50 width = std::fabs( dx );
55 height = std::fabs( dy );
59 x =
start.x() - ( ( dx < 0 ) ? width : 0 );
60 y =
start.y() - ( ( dy < 0 ) ? height : 0 );
90 x =
start.x() - width;
91 y =
start.y() - height;
96 return QRectF( x, y, width, height );
132 if ( mRubberBandItem )
134 view()->scene()->removeItem( mRubberBandItem );
135 delete mRubberBandItem;
142 mRubberBandItem =
new QGraphicsRectItem( 0, 0, 0, 0 );
143 mRubberBandItem->setBrush(
brush() );
144 mRubberBandItem->setPen(
pen() );
145 mRubberBandStartPos = position;
146 t.translate( position.x(), position.y() );
147 mRubberBandItem->setTransform( t );
148 mRubberBandItem->setZValue( QgsModelGraphicsScene::RubberBand );
149 view()->scene()->addItem( mRubberBandItem );
150 view()->scene()->update();
155 if ( !mRubberBandItem )
160 const bool constrainSquare = modifiers & Qt::ShiftModifier;
161 const bool fromCenter = modifiers & Qt::AltModifier;
163 const QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
164 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
166 t.translate( newRect.x(), newRect.y() );
167 mRubberBandItem->setTransform( t );
172 const bool constrainSquare = modifiers & Qt::ShiftModifier;
173 const bool fromCenter = modifiers & Qt::AltModifier;
175 if ( mRubberBandItem )
177 view()->scene()->removeItem( mRubberBandItem );
178 delete mRubberBandItem;
179 mRubberBandItem =
nullptr;
181 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
197 if ( mRubberBandItem )
199 view()->scene()->removeItem( mRubberBandItem );
200 delete mRubberBandItem;
207 mRubberBandItem =
new QGraphicsPathItem();
208 mRubberBandItem->setBrush( Qt::NoBrush );
209 mRubberBandItem->setPen(
pen() );
210 mRubberBandStartPos = position;
211 mRubberBandItem->setZValue( QgsModelGraphicsScene::RubberBand );
212 view()->scene()->addItem( mRubberBandItem );
213 view()->scene()->update();
218 if ( !mRubberBandItem )
225 QList<QPointF> controlPoints;
227 int offsetX = ( position.x() - mRubberBandStartPos.x() > 0 ) ? 50 : -50;
229 controlPoints.append( mRubberBandStartPos );
230 controlPoints.append( mRubberBandStartPos + QPointF( offsetX, 0 ) );
231 controlPoints.append( position - QPointF( offsetX, 0 ) );
232 controlPoints.append( position );
236 path.moveTo( controlPoints.at( 0 ) );
237 path.cubicTo( controlPoints.at( 1 ), controlPoints.at( 2 ), controlPoints.at( 3 ) );
239 mRubberBandItem->setPath( path );
244 if ( mRubberBandItem )
246 view()->scene()->removeItem( mRubberBandItem );
247 delete mRubberBandItem;
248 mRubberBandItem =
nullptr;
250 return updateRect( mRubberBandStartPos, position,
false,
false );
void update(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be updated to reflect a temporary ending position (in model coordina...
~QgsModelViewBezierRubberBand() override
QgsModelViewBezierRubberBand * create(QgsModelGraphicsView *view) const override
Creates a new instance of the QgsModelViewRubberBand subclass.
QgsModelViewBezierRubberBand(QgsModelGraphicsView *view=nullptr)
Constructor for QgsModelViewRectangularRubberBand.
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.
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in model coordinate s...
QgsModelViewRectangularRubberBand(QgsModelGraphicsView *view=nullptr)
Constructor for QgsModelViewRectangularRubberBand.
void update(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be updated to reflect a temporary ending position (in model coordina...
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in model coordinate s...
QgsModelViewRectangularRubberBand * create(QgsModelGraphicsView *view) const override
Creates a new instance of the QgsModelViewRubberBand subclass.
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.
~QgsModelViewRectangularRubberBand() override
QRectF updateRect(QPointF start, QPointF position, bool constrainSquare, bool fromCenter)
Calculates an updated bounding box rectangle from a original start position and new position.
void setPen(const QPen &pen)
Sets the pen used for drawing the rubber band.
void setBrush(const QBrush &brush)
Sets the brush used for drawing the rubber band.
QgsModelViewRubberBand(QgsModelGraphicsView *view=nullptr)
Constructor for QgsModelViewRubberBand.
QPen pen() const
Returns the pen used for drawing the rubber band.
QgsModelGraphicsView * view() const
Returns the view associated with the rubber band.
virtual void start(QPointF position, Qt::KeyboardModifiers modifiers)=0
Called when a rubber band should be created at the specified starting position (in model coordinate s...
QBrush brush() const
Returns the brush used for drawing the rubber band.