23#include <QGraphicsEllipseItem>
24#include <QGraphicsPolygonItem>
25#include <QGraphicsRectItem>
27#include "moc_qgslayoutviewrubberband.cpp"
41 return mView->currentLayout();
51 const double dx = position.x() -
start.x();
52 const double dy = position.y() -
start.y();
54 if ( constrainSquare )
56 if ( std::fabs( dx ) > std::fabs( dy ) )
58 width = std::fabs( dx );
63 height = std::fabs( dy );
67 x =
start.x() - ( ( dx < 0 ) ? width : 0 );
68 y =
start.y() - ( ( dy < 0 ) ? height : 0 );
98 x =
start.x() - width;
99 y =
start.y() - height;
104 return QRectF( x, y, width, height );
140 if ( mRubberBandItem )
142 layout()->removeItem( mRubberBandItem );
143 delete mRubberBandItem;
150 mRubberBandItem =
new QGraphicsRectItem( 0, 0, 0, 0 );
151 mRubberBandItem->setBrush(
brush() );
152 mRubberBandItem->setPen(
pen() );
153 mRubberBandStartPos = position;
154 t.translate( position.x(), position.y() );
155 mRubberBandItem->setTransform( t );
157 layout()->addItem( mRubberBandItem );
163 if ( !mRubberBandItem )
168 const bool constrainSquare = modifiers & Qt::ShiftModifier;
169 const bool fromCenter = modifiers & Qt::AltModifier;
171 const QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
172 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
174 t.translate( newRect.x(), newRect.y() );
175 mRubberBandItem->setTransform( t );
182 const bool constrainSquare = modifiers & Qt::ShiftModifier;
183 const bool fromCenter = modifiers & Qt::AltModifier;
185 if ( mRubberBandItem )
187 layout()->removeItem( mRubberBandItem );
188 delete mRubberBandItem;
189 mRubberBandItem =
nullptr;
191 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
206 if ( mRubberBandItem )
208 layout()->removeItem( mRubberBandItem );
209 delete mRubberBandItem;
216 mRubberBandItem =
new QGraphicsEllipseItem( 0, 0, 0, 0 );
217 mRubberBandItem->setBrush(
brush() );
218 mRubberBandItem->setPen(
pen() );
219 mRubberBandStartPos = position;
220 t.translate( position.x(), position.y() );
221 mRubberBandItem->setTransform( t );
223 layout()->addItem( mRubberBandItem );
229 if ( !mRubberBandItem )
234 const bool constrainSquare = modifiers & Qt::ShiftModifier;
235 const bool fromCenter = modifiers & Qt::AltModifier;
237 const QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
238 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
240 t.translate( newRect.x(), newRect.y() );
241 mRubberBandItem->setTransform( t );
248 const bool constrainSquare = modifiers & Qt::ShiftModifier;
249 const bool fromCenter = modifiers & Qt::AltModifier;
251 if ( mRubberBandItem )
253 layout()->removeItem( mRubberBandItem );
254 delete mRubberBandItem;
255 mRubberBandItem =
nullptr;
257 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 );
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 * 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(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.
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.
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.