18#include "moc_qgsmodelviewrubberband.cpp"
21#include <QGraphicsRectItem>
40 const double dx = position.x() -
start.x();
41 const double dy = position.y() -
start.y();
43 if ( constrainSquare )
45 if ( std::fabs( dx ) > std::fabs( dy ) )
47 width = std::fabs( dx );
52 height = std::fabs( dy );
56 x =
start.x() - ( ( dx < 0 ) ? width : 0 );
57 y =
start.y() - ( ( dy < 0 ) ? height : 0 );
87 x =
start.x() - width;
88 y =
start.y() - height;
93 return QRectF( x, y, width, height );
129 if ( mRubberBandItem )
131 view()->scene()->removeItem( mRubberBandItem );
132 delete mRubberBandItem;
139 mRubberBandItem =
new QGraphicsRectItem( 0, 0, 0, 0 );
140 mRubberBandItem->setBrush(
brush() );
141 mRubberBandItem->setPen(
pen() );
142 mRubberBandStartPos = position;
143 t.translate( position.x(), position.y() );
144 mRubberBandItem->setTransform( t );
145 mRubberBandItem->setZValue( QgsModelGraphicsScene::RubberBand );
146 view()->scene()->addItem( mRubberBandItem );
147 view()->scene()->update();
152 if ( !mRubberBandItem )
157 const bool constrainSquare = modifiers & Qt::ShiftModifier;
158 const bool fromCenter = modifiers & Qt::AltModifier;
160 const QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
161 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
163 t.translate( newRect.x(), newRect.y() );
164 mRubberBandItem->setTransform( t );
169 const bool constrainSquare = modifiers & Qt::ShiftModifier;
170 const bool fromCenter = modifiers & Qt::AltModifier;
172 if ( mRubberBandItem )
174 view()->scene()->removeItem( mRubberBandItem );
175 delete mRubberBandItem;
176 mRubberBandItem =
nullptr;
178 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
194 if ( mRubberBandItem )
196 view()->scene()->removeItem( mRubberBandItem );
197 delete mRubberBandItem;
204 mRubberBandItem =
new QGraphicsPathItem();
205 mRubberBandItem->setBrush( Qt::NoBrush );
206 mRubberBandItem->setPen(
pen() );
207 mRubberBandStartPos = position;
208 mRubberBandItem->setZValue( QgsModelGraphicsScene::RubberBand );
209 view()->scene()->addItem( mRubberBandItem );
210 view()->scene()->update();
215 if ( !mRubberBandItem )
222 QList<QPointF> controlPoints;
224 int offsetX = ( position.x() - mRubberBandStartPos.x() > 0 ) ? 50 : -50;
226 controlPoints.append( mRubberBandStartPos );
227 controlPoints.append( mRubberBandStartPos + QPointF( offsetX, 0 ) );
228 controlPoints.append( position - QPointF( offsetX, 0 ) );
229 controlPoints.append( position );
233 path.moveTo( controlPoints.at( 0 ) );
234 path.cubicTo( controlPoints.at( 1 ), controlPoints.at( 2 ), controlPoints.at( 3 ) );
236 mRubberBandItem->setPath( path );
241 if ( mRubberBandItem )
243 view()->scene()->removeItem( mRubberBandItem );
244 delete mRubberBandItem;
245 mRubberBandItem =
nullptr;
247 return updateRect( mRubberBandStartPos, position,
false,
false );
A bezier curve rubber band for use within QgsModelGraphicsView widgets.
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...
A rectangular rubber band for use within QgsModelGraphicsView widgets.
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
An abstract base class for temporary rubber band items in various shapes, for use within QgsModelGrap...
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.