20#include <QGraphicsRectItem>
21#include <QGraphicsEllipseItem>
22#include <QGraphicsPolygonItem>
47 const double dx = position.x() -
start.x();
48 const double dy = position.y() -
start.y();
50 if ( constrainSquare )
52 if ( std::fabs( dx ) > std::fabs( dy ) )
54 width = std::fabs( dx );
59 height = std::fabs( dy );
63 x =
start.x() - ( ( dx < 0 ) ? width : 0 );
64 y =
start.y() - ( ( dy < 0 ) ? height : 0 );
94 x =
start.x() - width;
95 y =
start.y() - height;
100 return QRectF( x, y, width, height );
136 if ( mRubberBandItem )
138 layout()->removeItem( mRubberBandItem );
139 delete mRubberBandItem;
146 mRubberBandItem =
new QGraphicsRectItem( 0, 0, 0, 0 );
147 mRubberBandItem->setBrush(
brush() );
148 mRubberBandItem->setPen(
pen() );
149 mRubberBandStartPos = position;
150 t.translate( position.x(), position.y() );
151 mRubberBandItem->setTransform( t );
153 layout()->addItem( mRubberBandItem );
159 if ( !mRubberBandItem )
164 const bool constrainSquare = modifiers & Qt::ShiftModifier;
165 const bool fromCenter = modifiers & Qt::AltModifier;
167 const QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
168 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
170 t.translate( newRect.x(), newRect.y() );
171 mRubberBandItem->setTransform( t );
178 const bool constrainSquare = modifiers & Qt::ShiftModifier;
179 const bool fromCenter = modifiers & Qt::AltModifier;
181 if ( mRubberBandItem )
183 layout()->removeItem( mRubberBandItem );
184 delete mRubberBandItem;
185 mRubberBandItem =
nullptr;
187 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
203 if ( mRubberBandItem )
205 layout()->removeItem( mRubberBandItem );
206 delete mRubberBandItem;
213 mRubberBandItem =
new QGraphicsEllipseItem( 0, 0, 0, 0 );
214 mRubberBandItem->setBrush(
brush() );
215 mRubberBandItem->setPen(
pen() );
216 mRubberBandStartPos = position;
217 t.translate( position.x(), position.y() );
218 mRubberBandItem->setTransform( t );
220 layout()->addItem( mRubberBandItem );
226 if ( !mRubberBandItem )
231 const bool constrainSquare = modifiers & Qt::ShiftModifier;
232 const bool fromCenter = modifiers & Qt::AltModifier;
234 const QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
235 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
237 t.translate( newRect.x(), newRect.y() );
238 mRubberBandItem->setTransform( t );
245 const bool constrainSquare = modifiers & Qt::ShiftModifier;
246 const bool fromCenter = modifiers & Qt::AltModifier;
248 if ( mRubberBandItem )
250 layout()->removeItem( mRubberBandItem );
251 delete mRubberBandItem;
252 mRubberBandItem =
nullptr;
254 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
274 if ( mRubberBandItem )
276 layout()->removeItem( mRubberBandItem );
277 delete mRubberBandItem;
284 mRubberBandItem =
new QGraphicsPolygonItem();
285 mRubberBandItem->setBrush(
brush() );
286 mRubberBandItem->setPen(
pen() );
287 mRubberBandStartPos = position;
288 t.translate( position.x(), position.y() );
289 mRubberBandItem->setTransform( t );
291 layout()->addItem( mRubberBandItem );
297 if ( !mRubberBandItem )
302 const bool constrainSquare = modifiers & Qt::ShiftModifier;
303 const bool fromCenter = modifiers & Qt::AltModifier;
305 const QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
307 const QPolygonF shapePolygon = QPolygonF() << QPointF( 0, newRect.height() )
308 << QPointF( newRect.width(), newRect.height() )
309 << QPointF( newRect.width() / 2.0, 0 )
310 << QPointF( 0, newRect.height() );
312 mRubberBandItem->setPolygon( shapePolygon );
314 t.translate( newRect.x(), newRect.y() );
315 mRubberBandItem->setTransform( t );
322 const bool constrainSquare = modifiers & Qt::ShiftModifier;
323 const bool fromCenter = modifiers & Qt::AltModifier;
325 if ( mRubberBandItem )
327 layout()->removeItem( mRubberBandItem );
328 delete mRubberBandItem;
329 mRubberBandItem =
nullptr;
331 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(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.