QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgslayoutviewrubberband.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutviewrubberband.cpp
3 ---------------------------
4 Date : July 2017
5 Copyright : (C) 2017 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16
18#include "qgslayout.h"
19#include "qgslayoutview.h"
20#include <QGraphicsRectItem>
21#include <QGraphicsEllipseItem>
22#include <QGraphicsPolygonItem>
23
25 : mView( view )
26{
27
28}
29
31{
32 return mView;
33}
34
36{
37 return mView->currentLayout();
38}
39
40QRectF QgsLayoutViewRubberBand::updateRect( QPointF start, QPointF position, bool constrainSquare, bool fromCenter )
41{
42 double x = 0;
43 double y = 0;
44 double width = 0;
45 double height = 0;
46
47 const double dx = position.x() - start.x();
48 const double dy = position.y() - start.y();
49
50 if ( constrainSquare )
51 {
52 if ( std::fabs( dx ) > std::fabs( dy ) )
53 {
54 width = std::fabs( dx );
55 height = width;
56 }
57 else
58 {
59 height = std::fabs( dy );
60 width = height;
61 }
62
63 x = start.x() - ( ( dx < 0 ) ? width : 0 );
64 y = start.y() - ( ( dy < 0 ) ? height : 0 );
65 }
66 else
67 {
68 //not constraining
69 if ( dx < 0 )
70 {
71 x = position.x();
72 width = -dx;
73 }
74 else
75 {
76 x = start.x();
77 width = dx;
78 }
79
80 if ( dy < 0 )
81 {
82 y = position.y();
83 height = -dy;
84 }
85 else
86 {
87 y = start.y();
88 height = dy;
89 }
90 }
91
92 if ( fromCenter )
93 {
94 x = start.x() - width;
95 y = start.y() - height;
96 width *= 2.0;
97 height *= 2.0;
98 }
99
100 return QRectF( x, y, width, height );
101}
102
104{
105 return mPen;
106}
107
108void QgsLayoutViewRubberBand::setPen( const QPen &pen )
109{
110 mPen = pen;
111}
112
114{
115 return mBrush;
116}
117
118void QgsLayoutViewRubberBand::setBrush( const QBrush &brush )
119{
120 mBrush = brush;
121}
122
123
126{
127}
128
130{
132}
133
135{
136 if ( mRubberBandItem )
137 {
138 layout()->removeItem( mRubberBandItem );
139 delete mRubberBandItem;
140 }
141}
142
143void QgsLayoutViewRectangularRubberBand::start( QPointF position, Qt::KeyboardModifiers )
144{
145 QTransform t;
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 );
152 mRubberBandItem->setZValue( QgsLayout::ZViewTool );
153 layout()->addItem( mRubberBandItem );
154 layout()->update();
155}
156
157void QgsLayoutViewRectangularRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
158{
159 if ( !mRubberBandItem )
160 {
161 return;
162 }
163
164 const bool constrainSquare = modifiers & Qt::ShiftModifier;
165 const bool fromCenter = modifiers & Qt::AltModifier;
166
167 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
168 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
169 QTransform t;
170 t.translate( newRect.x(), newRect.y() );
171 mRubberBandItem->setTransform( t );
172
173 emit sizeChanged( tr( "width: %1 %3 height: %2 %3" ).arg( newRect.width() ).arg( newRect.height() ).arg( QgsUnitTypes::toAbbreviatedString( layout()->units() ) ) );
174}
175
176QRectF QgsLayoutViewRectangularRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
177{
178 const bool constrainSquare = modifiers & Qt::ShiftModifier;
179 const bool fromCenter = modifiers & Qt::AltModifier;
180
181 if ( mRubberBandItem )
182 {
183 layout()->removeItem( mRubberBandItem );
184 delete mRubberBandItem;
185 mRubberBandItem = nullptr;
186 }
187 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
188}
189
192{
193
194}
195
197{
199}
200
202{
203 if ( mRubberBandItem )
204 {
205 layout()->removeItem( mRubberBandItem );
206 delete mRubberBandItem;
207 }
208}
209
210void QgsLayoutViewEllipticalRubberBand::start( QPointF position, Qt::KeyboardModifiers )
211{
212 QTransform t;
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 );
219 mRubberBandItem->setZValue( QgsLayout::ZViewTool );
220 layout()->addItem( mRubberBandItem );
221 layout()->update();
222}
223
224void QgsLayoutViewEllipticalRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
225{
226 if ( !mRubberBandItem )
227 {
228 return;
229 }
230
231 const bool constrainSquare = modifiers & Qt::ShiftModifier;
232 const bool fromCenter = modifiers & Qt::AltModifier;
233
234 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
235 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
236 QTransform t;
237 t.translate( newRect.x(), newRect.y() );
238 mRubberBandItem->setTransform( t );
239
240 emit sizeChanged( tr( "width: %1 %3 height: %2 %3" ).arg( newRect.width() ).arg( newRect.height() ).arg( QgsUnitTypes::toAbbreviatedString( layout()->units() ) ) );
241}
242
243QRectF QgsLayoutViewEllipticalRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
244{
245 const bool constrainSquare = modifiers & Qt::ShiftModifier;
246 const bool fromCenter = modifiers & Qt::AltModifier;
247
248 if ( mRubberBandItem )
249 {
250 layout()->removeItem( mRubberBandItem );
251 delete mRubberBandItem;
252 mRubberBandItem = nullptr;
253 }
254 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
255}
256
257//
258// QgsLayoutViewTriangleRubberBand
259//
260
263{
264
265}
266
268{
270}
271
273{
274 if ( mRubberBandItem )
275 {
276 layout()->removeItem( mRubberBandItem );
277 delete mRubberBandItem;
278 }
279}
280
281void QgsLayoutViewTriangleRubberBand::start( QPointF position, Qt::KeyboardModifiers )
282{
283 QTransform t;
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 );
290 mRubberBandItem->setZValue( QgsLayout::ZViewTool );
291 layout()->addItem( mRubberBandItem );
292 layout()->update();
293}
294
295void QgsLayoutViewTriangleRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
296{
297 if ( !mRubberBandItem )
298 {
299 return;
300 }
301
302 const bool constrainSquare = modifiers & Qt::ShiftModifier;
303 const bool fromCenter = modifiers & Qt::AltModifier;
304
305 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
306
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() );
311
312 mRubberBandItem->setPolygon( shapePolygon );
313 QTransform t;
314 t.translate( newRect.x(), newRect.y() );
315 mRubberBandItem->setTransform( t );
316
317 emit sizeChanged( tr( "width: %1 %3 height: %2 %3" ).arg( newRect.width() ).arg( newRect.height() ).arg( QgsUnitTypes::toAbbreviatedString( layout()->units() ) ) );
318}
319
320QRectF QgsLayoutViewTriangleRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
321{
322 const bool constrainSquare = modifiers & Qt::ShiftModifier;
323 const bool fromCenter = modifiers & Qt::AltModifier;
324
325 if ( mRubberBandItem )
326 {
327 layout()->removeItem( mRubberBandItem );
328 delete mRubberBandItem;
329 mRubberBandItem = nullptr;
330 }
331 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
332}
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.
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 ...
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...
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.
Definition: qgslayoutview.h:51
QgsLayout * currentLayout
Definition: qgslayoutview.h:64
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:51
@ ZViewTool
Z-value for temporary view tool items.
Definition: qgslayout.h:65
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.