18#include "moc_qgslayoutviewrubberband.cpp"
22#include <QGraphicsRectItem>
23#include <QGraphicsEllipseItem>
24#include <QGraphicsPolygonItem>
49 const double dx = position.x() -
start.x();
50 const double dy = position.y() -
start.y();
52 if ( constrainSquare )
54 if ( std::fabs( dx ) > std::fabs( dy ) )
56 width = std::fabs( dx );
61 height = std::fabs( dy );
65 x =
start.x() - ( ( dx < 0 ) ? width : 0 );
66 y =
start.y() - ( ( dy < 0 ) ? height : 0 );
96 x =
start.x() - width;
97 y =
start.y() - height;
102 return QRectF( x, y, width, height );
138 if ( mRubberBandItem )
140 layout()->removeItem( mRubberBandItem );
141 delete mRubberBandItem;
148 mRubberBandItem =
new QGraphicsRectItem( 0, 0, 0, 0 );
149 mRubberBandItem->setBrush(
brush() );
150 mRubberBandItem->setPen(
pen() );
151 mRubberBandStartPos = position;
152 t.translate( position.x(), position.y() );
153 mRubberBandItem->setTransform( t );
155 layout()->addItem( mRubberBandItem );
161 if ( !mRubberBandItem )
166 const bool constrainSquare = modifiers & Qt::ShiftModifier;
167 const bool fromCenter = modifiers & Qt::AltModifier;
169 const QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
170 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
172 t.translate( newRect.x(), newRect.y() );
173 mRubberBandItem->setTransform( t );
180 const bool constrainSquare = modifiers & Qt::ShiftModifier;
181 const bool fromCenter = modifiers & Qt::AltModifier;
183 if ( mRubberBandItem )
185 layout()->removeItem( mRubberBandItem );
186 delete mRubberBandItem;
187 mRubberBandItem =
nullptr;
189 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
205 if ( mRubberBandItem )
207 layout()->removeItem( mRubberBandItem );
208 delete mRubberBandItem;
215 mRubberBandItem =
new QGraphicsEllipseItem( 0, 0, 0, 0 );
216 mRubberBandItem->setBrush(
brush() );
217 mRubberBandItem->setPen(
pen() );
218 mRubberBandStartPos = position;
219 t.translate( position.x(), position.y() );
220 mRubberBandItem->setTransform( t );
222 layout()->addItem( mRubberBandItem );
228 if ( !mRubberBandItem )
233 const bool constrainSquare = modifiers & Qt::ShiftModifier;
234 const bool fromCenter = modifiers & Qt::AltModifier;
236 const QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
237 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
239 t.translate( newRect.x(), newRect.y() );
240 mRubberBandItem->setTransform( t );
247 const bool constrainSquare = modifiers & Qt::ShiftModifier;
248 const bool fromCenter = modifiers & Qt::AltModifier;
250 if ( mRubberBandItem )
252 layout()->removeItem( mRubberBandItem );
253 delete mRubberBandItem;
254 mRubberBandItem =
nullptr;
256 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
276 if ( mRubberBandItem )
278 layout()->removeItem( mRubberBandItem );
279 delete mRubberBandItem;
286 mRubberBandItem =
new QGraphicsPolygonItem();
287 mRubberBandItem->setBrush(
brush() );
288 mRubberBandItem->setPen(
pen() );
289 mRubberBandStartPos = position;
290 t.translate( position.x(), position.y() );
291 mRubberBandItem->setTransform( t );
293 layout()->addItem( mRubberBandItem );
299 if ( !mRubberBandItem )
304 const bool constrainSquare = modifiers & Qt::ShiftModifier;
305 const bool fromCenter = modifiers & Qt::AltModifier;
307 const QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
309 const QPolygonF shapePolygon = QPolygonF() << QPointF( 0, newRect.height() )
310 << QPointF( newRect.width(), newRect.height() )
311 << QPointF( newRect.width() / 2.0, 0 )
312 << QPointF( 0, newRect.height() );
314 mRubberBandItem->setPolygon( shapePolygon );
316 t.translate( newRect.x(), newRect.y() );
317 mRubberBandItem->setTransform( t );
324 const bool constrainSquare = modifiers & Qt::ShiftModifier;
325 const bool fromCenter = modifiers & Qt::AltModifier;
327 if ( mRubberBandItem )
329 layout()->removeItem( mRubberBandItem );
330 delete mRubberBandItem;
331 mRubberBandItem =
nullptr;
333 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
QgsLayoutViewEllipseRubberBand is elliptical rubber band for use within QgsLayoutView widgets.
QgsLayoutViewEllipticalRubberBand(QgsLayoutView *view=nullptr)
Constructor for QgsLayoutViewEllipticalRubberBand.
void update(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be updated to reflect a temporary ending position (in layout coordin...
QgsLayoutViewEllipticalRubberBand * create(QgsLayoutView *view) const override
Creates a new instance of the QgsLayoutViewRubberBand subclass.
~QgsLayoutViewEllipticalRubberBand() override
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in layout coordinate ...
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.
QgsLayoutViewRectangularRubberBand is rectangular rubber band for use within QgsLayoutView widgets.
QgsLayoutViewRectangularRubberBand * create(QgsLayoutView *view) const override
Creates a new instance of the QgsLayoutViewRubberBand subclass.
void update(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be updated to reflect a temporary ending position (in layout coordin...
QgsLayoutViewRectangularRubberBand(QgsLayoutView *view=nullptr)
Constructor for QgsLayoutViewRectangularRubberBand.
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in layout coordinate ...
~QgsLayoutViewRectangularRubberBand() 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.
QgsLayoutViewRubberBand is an abstract base class for temporary rubber band items in various shapes,...
QgsLayoutViewRubberBand(QgsLayoutView *view=nullptr)
Constructor for QgsLayoutViewRubberBand.
QgsLayoutView * view() const
Returns the view associated with the rubber band.
void sizeChanged(const QString &size)
Emitted when the size of the rubber band is changed.
QBrush brush() const
Returns the brush used for drawing 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 layout coordinate ...
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.
QgsLayout * layout() const
Returns the layout associated with the rubber band.
QPen pen() const
Returns the pen used for drawing 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.
QgsLayoutViewTriangleRubberBand is triangular rubber band for use within QgsLayoutView widgets.
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.
QgsLayoutViewTriangleRubberBand(QgsLayoutView *view=nullptr)
Constructor for QgsLayoutViewTriangleRubberBand.
QgsLayoutViewTriangleRubberBand * create(QgsLayoutView *view) const override
Creates a new instance of the QgsLayoutViewRubberBand subclass.
void update(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be updated to reflect a temporary ending position (in layout coordin...
~QgsLayoutViewTriangleRubberBand() override
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in layout coordinate ...
A graphical widget to display and interact with QgsLayouts.
QgsLayout * currentLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
@ ZViewTool
Z-value for temporary view tool items.
static Q_INVOKABLE QString toAbbreviatedString(Qgis::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.