QGIS API Documentation 3.41.0-Master (af5edcb665c)
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
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
129
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
195
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
265
270
272{
273 if ( mRubberBandItem )
274 {
275 layout()->removeItem( mRubberBandItem );
276 delete mRubberBandItem;
277 }
278}
279
280void QgsLayoutViewTriangleRubberBand::start( QPointF position, Qt::KeyboardModifiers )
281{
282 QTransform t;
283 mRubberBandItem = new QGraphicsPolygonItem();
284 mRubberBandItem->setBrush( brush() );
285 mRubberBandItem->setPen( pen() );
286 mRubberBandStartPos = position;
287 t.translate( position.x(), position.y() );
288 mRubberBandItem->setTransform( t );
289 mRubberBandItem->setZValue( QgsLayout::ZViewTool );
290 layout()->addItem( mRubberBandItem );
291 layout()->update();
292}
293
294void QgsLayoutViewTriangleRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
295{
296 if ( !mRubberBandItem )
297 {
298 return;
299 }
300
301 const bool constrainSquare = modifiers & Qt::ShiftModifier;
302 const bool fromCenter = modifiers & Qt::AltModifier;
303
304 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
305
306 const QPolygonF shapePolygon = QPolygonF() << QPointF( 0, newRect.height() )
307 << QPointF( newRect.width(), newRect.height() )
308 << QPointF( newRect.width() / 2.0, 0 )
309 << QPointF( 0, newRect.height() );
310
311 mRubberBandItem->setPolygon( shapePolygon );
312 QTransform t;
313 t.translate( newRect.x(), newRect.y() );
314 mRubberBandItem->setTransform( t );
315
316 emit sizeChanged( tr( "width: %1 %3 height: %2 %3" ).arg( newRect.width() ).arg( newRect.height() ).arg( QgsUnitTypes::toAbbreviatedString( layout()->units() ) ) );
317}
318
319QRectF QgsLayoutViewTriangleRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
320{
321 const bool constrainSquare = modifiers & Qt::ShiftModifier;
322 const bool fromCenter = modifiers & Qt::AltModifier;
323
324 if ( mRubberBandItem )
325 {
326 layout()->removeItem( mRubberBandItem );
327 delete mRubberBandItem;
328 mRubberBandItem = nullptr;
329 }
330 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
331}
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.