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