QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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 "qgsunittypes.h"
21#include <QGraphicsRectItem>
22#include <QGraphicsEllipseItem>
23#include <QGraphicsPolygonItem>
24
26 : mView( view )
27{
28
29}
30
32{
33 return mView;
34}
35
37{
38 return mView->currentLayout();
39}
40
41QRectF QgsLayoutViewRubberBand::updateRect( QPointF start, QPointF position, bool constrainSquare, bool fromCenter )
42{
43 double x = 0;
44 double y = 0;
45 double width = 0;
46 double height = 0;
47
48 const double dx = position.x() - start.x();
49 const double dy = position.y() - start.y();
50
51 if ( constrainSquare )
52 {
53 if ( std::fabs( dx ) > std::fabs( dy ) )
54 {
55 width = std::fabs( dx );
56 height = width;
57 }
58 else
59 {
60 height = std::fabs( dy );
61 width = height;
62 }
63
64 x = start.x() - ( ( dx < 0 ) ? width : 0 );
65 y = start.y() - ( ( dy < 0 ) ? height : 0 );
66 }
67 else
68 {
69 //not constraining
70 if ( dx < 0 )
71 {
72 x = position.x();
73 width = -dx;
74 }
75 else
76 {
77 x = start.x();
78 width = dx;
79 }
80
81 if ( dy < 0 )
82 {
83 y = position.y();
84 height = -dy;
85 }
86 else
87 {
88 y = start.y();
89 height = dy;
90 }
91 }
92
93 if ( fromCenter )
94 {
95 x = start.x() - width;
96 y = start.y() - height;
97 width *= 2.0;
98 height *= 2.0;
99 }
100
101 return QRectF( x, y, width, height );
102}
103
105{
106 return mPen;
107}
108
109void QgsLayoutViewRubberBand::setPen( const QPen &pen )
110{
111 mPen = pen;
112}
113
115{
116 return mBrush;
117}
118
119void QgsLayoutViewRubberBand::setBrush( const QBrush &brush )
120{
121 mBrush = brush;
122}
123
124
127{
128}
129
131{
133}
134
136{
137 if ( mRubberBandItem )
138 {
139 layout()->removeItem( mRubberBandItem );
140 delete mRubberBandItem;
141 }
142}
143
144void QgsLayoutViewRectangularRubberBand::start( QPointF position, Qt::KeyboardModifiers )
145{
146 QTransform t;
147 mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
148 mRubberBandItem->setBrush( brush() );
149 mRubberBandItem->setPen( pen() );
150 mRubberBandStartPos = position;
151 t.translate( position.x(), position.y() );
152 mRubberBandItem->setTransform( t );
153 mRubberBandItem->setZValue( QgsLayout::ZViewTool );
154 layout()->addItem( mRubberBandItem );
155 layout()->update();
156}
157
158void QgsLayoutViewRectangularRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
159{
160 if ( !mRubberBandItem )
161 {
162 return;
163 }
164
165 const bool constrainSquare = modifiers & Qt::ShiftModifier;
166 const bool fromCenter = modifiers & Qt::AltModifier;
167
168 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
169 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
170 QTransform t;
171 t.translate( newRect.x(), newRect.y() );
172 mRubberBandItem->setTransform( t );
173
174 emit sizeChanged( tr( "width: %1 %3 height: %2 %3" ).arg( newRect.width() ).arg( newRect.height() ).arg( QgsUnitTypes::toAbbreviatedString( layout()->units() ) ) );
175}
176
177QRectF QgsLayoutViewRectangularRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
178{
179 const bool constrainSquare = modifiers & Qt::ShiftModifier;
180 const bool fromCenter = modifiers & Qt::AltModifier;
181
182 if ( mRubberBandItem )
183 {
184 layout()->removeItem( mRubberBandItem );
185 delete mRubberBandItem;
186 mRubberBandItem = nullptr;
187 }
188 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
189}
190
193{
194
195}
196
198{
200}
201
203{
204 if ( mRubberBandItem )
205 {
206 layout()->removeItem( mRubberBandItem );
207 delete mRubberBandItem;
208 }
209}
210
211void QgsLayoutViewEllipticalRubberBand::start( QPointF position, Qt::KeyboardModifiers )
212{
213 QTransform t;
214 mRubberBandItem = new QGraphicsEllipseItem( 0, 0, 0, 0 );
215 mRubberBandItem->setBrush( brush() );
216 mRubberBandItem->setPen( pen() );
217 mRubberBandStartPos = position;
218 t.translate( position.x(), position.y() );
219 mRubberBandItem->setTransform( t );
220 mRubberBandItem->setZValue( QgsLayout::ZViewTool );
221 layout()->addItem( mRubberBandItem );
222 layout()->update();
223}
224
225void QgsLayoutViewEllipticalRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
226{
227 if ( !mRubberBandItem )
228 {
229 return;
230 }
231
232 const bool constrainSquare = modifiers & Qt::ShiftModifier;
233 const bool fromCenter = modifiers & Qt::AltModifier;
234
235 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
236 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
237 QTransform t;
238 t.translate( newRect.x(), newRect.y() );
239 mRubberBandItem->setTransform( t );
240
241 emit sizeChanged( tr( "width: %1 %3 height: %2 %3" ).arg( newRect.width() ).arg( newRect.height() ).arg( QgsUnitTypes::toAbbreviatedString( layout()->units() ) ) );
242}
243
244QRectF QgsLayoutViewEllipticalRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
245{
246 const bool constrainSquare = modifiers & Qt::ShiftModifier;
247 const bool fromCenter = modifiers & Qt::AltModifier;
248
249 if ( mRubberBandItem )
250 {
251 layout()->removeItem( mRubberBandItem );
252 delete mRubberBandItem;
253 mRubberBandItem = nullptr;
254 }
255 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
256}
257
258//
259// QgsLayoutViewTriangleRubberBand
260//
261
264{
265
266}
267
269{
271}
272
274{
275 if ( mRubberBandItem )
276 {
277 layout()->removeItem( mRubberBandItem );
278 delete mRubberBandItem;
279 }
280}
281
282void QgsLayoutViewTriangleRubberBand::start( QPointF position, Qt::KeyboardModifiers )
283{
284 QTransform t;
285 mRubberBandItem = new QGraphicsPolygonItem();
286 mRubberBandItem->setBrush( brush() );
287 mRubberBandItem->setPen( pen() );
288 mRubberBandStartPos = position;
289 t.translate( position.x(), position.y() );
290 mRubberBandItem->setTransform( t );
291 mRubberBandItem->setZValue( QgsLayout::ZViewTool );
292 layout()->addItem( mRubberBandItem );
293 layout()->update();
294}
295
296void QgsLayoutViewTriangleRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
297{
298 if ( !mRubberBandItem )
299 {
300 return;
301 }
302
303 const bool constrainSquare = modifiers & Qt::ShiftModifier;
304 const bool fromCenter = modifiers & Qt::AltModifier;
305
306 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
307
308 const QPolygonF shapePolygon = QPolygonF() << QPointF( 0, newRect.height() )
309 << QPointF( newRect.width(), newRect.height() )
310 << QPointF( newRect.width() / 2.0, 0 )
311 << QPointF( 0, newRect.height() );
312
313 mRubberBandItem->setPolygon( shapePolygon );
314 QTransform t;
315 t.translate( newRect.x(), newRect.y() );
316 mRubberBandItem->setTransform( t );
317
318 emit sizeChanged( tr( "width: %1 %3 height: %2 %3" ).arg( newRect.width() ).arg( newRect.height() ).arg( QgsUnitTypes::toAbbreviatedString( layout()->units() ) ) );
319}
320
321QRectF QgsLayoutViewTriangleRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
322{
323 const bool constrainSquare = modifiers & Qt::ShiftModifier;
324 const bool fromCenter = modifiers & Qt::AltModifier;
325
326 if ( mRubberBandItem )
327 {
328 layout()->removeItem( mRubberBandItem );
329 delete mRubberBandItem;
330 mRubberBandItem = nullptr;
331 }
332 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
333}
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:50
@ ZViewTool
Z-value for temporary view tool items.
Definition: qgslayout.h:64
static Q_INVOKABLE QString toAbbreviatedString(Qgis::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.