20 #include <QGraphicsRectItem> 21 #include <QGraphicsEllipseItem> 22 #include <QGraphicsPolygonItem> 47 double dx = position.x() - start.x();
48 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 bool constrainSquare = modifiers & Qt::ShiftModifier;
165 bool fromCenter = modifiers & Qt::AltModifier;
167 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 bool constrainSquare = modifiers & Qt::ShiftModifier;
179 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 bool constrainSquare = modifiers & Qt::ShiftModifier;
232 bool fromCenter = modifiers & Qt::AltModifier;
234 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 bool constrainSquare = modifiers & Qt::ShiftModifier;
246 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 bool constrainSquare = modifiers & Qt::ShiftModifier;
303 bool fromCenter = modifiers & Qt::AltModifier;
305 QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
307 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 bool constrainSquare = modifiers & Qt::ShiftModifier;
323 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 );
~QgsLayoutViewRectangularRubberBand() override
QPen pen() const
Returns the pen used for drawing the rubber band.
QBrush brush() const
Returns the brush used for drawing the rubber band.
QgsLayout * layout() const
Returns the layout associated with the rubber band.
QgsLayoutView * view() const
Returns the view associated with the rubber band.
A graphical widget to display and interact with QgsLayouts.
QgsLayoutViewEllipseRubberBand is elliptical rubber band for use within QgsLayoutView widgets...
QRectF finish(QPointF position=QPointF(), Qt::KeyboardModifiers modifiers=nullptr) override
Called when a rubber band use has finished and the rubber band is no longer required.
QgsLayoutViewEllipticalRubberBand * create(QgsLayoutView *view) const override
Creates a new instance of the QgsLayoutViewRubberBand subclass.
QgsLayoutViewTriangleRubberBand is triangular rubber band for use within QgsLayoutView widgets...
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...
Z-value for temporary view tool items.
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in layout coordinate ...
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in layout coordinate ...
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 ...
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
QgsLayoutViewRubberBand(QgsLayoutView *view=nullptr)
Constructor for QgsLayoutViewRubberBand.
~QgsLayoutViewEllipticalRubberBand() override
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...
QRectF updateRect(QPointF start, QPointF position, bool constrainSquare, bool fromCenter)
Calculates an updated bounding box rectangle from a original start position and new position...
QgsLayoutViewRectangularRubberBand * create(QgsLayoutView *view) const override
Creates a new instance of the QgsLayoutViewRubberBand subclass.
void sizeChanged(const QString &size)
Emitted when the size of the rubber band is changed.
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...
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
QgsLayoutViewRectangularRubberBand is rectangular rubber band for use within QgsLayoutView widgets...
void setBrush(const QBrush &brush)
Sets the brush used for drawing the rubber band.
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in layout coordinate ...
QgsLayoutViewRubberBand is an abstract base class for temporary rubber band items in various shapes...
QRectF finish(QPointF position=QPointF(), Qt::KeyboardModifiers modifiers=nullptr) override
Called when a rubber band use has finished and the rubber band is no longer required.
~QgsLayoutViewTriangleRubberBand() override
QgsLayoutViewTriangleRubberBand * create(QgsLayoutView *view) const override
Creates a new instance of the QgsLayoutViewRubberBand subclass.
QgsLayoutViewRectangularRubberBand(QgsLayoutView *view=nullptr)
Constructor for QgsLayoutViewRectangularRubberBand.
QgsLayoutViewEllipticalRubberBand(QgsLayoutView *view=nullptr)
Constructor for QgsLayoutViewEllipticalRubberBand.
QgsLayoutViewTriangleRubberBand(QgsLayoutView *view=nullptr)
Constructor for QgsLayoutViewTriangleRubberBand.
QRectF finish(QPointF position=QPointF(), Qt::KeyboardModifiers modifiers=nullptr) override
Called when a rubber band use has finished and the rubber band is no longer required.
void setPen(const QPen &pen)
Sets the pen used for drawing the rubber band.