QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
19#include "qgslayout.h"
20#include "qgslayoutview.h"
21#include "qgsunittypes.h"
22
23#include <QGraphicsEllipseItem>
24#include <QGraphicsPolygonItem>
25#include <QGraphicsRectItem>
26
27#include "moc_qgslayoutviewrubberband.cpp"
28
32
34{
35 return mView;
36}
37
39{
40 return mView->currentLayout();
41}
42
43QRectF QgsLayoutViewRubberBand::updateRect( QPointF start, QPointF position, bool constrainSquare, bool fromCenter )
44{
45 double x = 0;
46 double y = 0;
47 double width = 0;
48 double height = 0;
49
50 const double dx = position.x() - start.x();
51 const double dy = position.y() - start.y();
52
53 if ( constrainSquare )
54 {
55 if ( std::fabs( dx ) > std::fabs( dy ) )
56 {
57 width = std::fabs( dx );
58 height = width;
59 }
60 else
61 {
62 height = std::fabs( dy );
63 width = height;
64 }
65
66 x = start.x() - ( ( dx < 0 ) ? width : 0 );
67 y = start.y() - ( ( dy < 0 ) ? height : 0 );
68 }
69 else
70 {
71 //not constraining
72 if ( dx < 0 )
73 {
74 x = position.x();
75 width = -dx;
76 }
77 else
78 {
79 x = start.x();
80 width = dx;
81 }
82
83 if ( dy < 0 )
84 {
85 y = position.y();
86 height = -dy;
87 }
88 else
89 {
90 y = start.y();
91 height = dy;
92 }
93 }
94
95 if ( fromCenter )
96 {
97 x = start.x() - width;
98 y = start.y() - height;
99 width *= 2.0;
100 height *= 2.0;
101 }
102
103 return QRectF( x, y, width, height );
104}
105
107{
108 return mPen;
109}
110
112{
113 mPen = pen;
114}
115
117{
118 return mBrush;
119}
120
122{
123 mBrush = brush;
124}
125
126
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
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
264
269
271{
272 if ( mRubberBandItem )
273 {
274 layout()->removeItem( mRubberBandItem );
275 delete mRubberBandItem;
276 }
277}
278
279void QgsLayoutViewTriangleRubberBand::start( QPointF position, Qt::KeyboardModifiers )
280{
281 QTransform t;
282 mRubberBandItem = new QGraphicsPolygonItem();
283 mRubberBandItem->setBrush( brush() );
284 mRubberBandItem->setPen( pen() );
285 mRubberBandStartPos = position;
286 t.translate( position.x(), position.y() );
287 mRubberBandItem->setTransform( t );
288 mRubberBandItem->setZValue( QgsLayout::ZViewTool );
289 layout()->addItem( mRubberBandItem );
290 layout()->update();
291}
292
293void QgsLayoutViewTriangleRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
294{
295 if ( !mRubberBandItem )
296 {
297 return;
298 }
299
300 const bool constrainSquare = modifiers & Qt::ShiftModifier;
301 const bool fromCenter = modifiers & Qt::AltModifier;
302
303 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
304
305 const QPolygonF shapePolygon = QPolygonF() << QPointF( 0, newRect.height() ) << QPointF( newRect.width(), newRect.height() ) << QPointF( newRect.width() / 2.0, 0 ) << QPointF( 0, newRect.height() );
306
307 mRubberBandItem->setPolygon( shapePolygon );
308 QTransform t;
309 t.translate( newRect.x(), newRect.y() );
310 mRubberBandItem->setTransform( t );
311
312 emit sizeChanged( tr( "width: %1 %3 height: %2 %3" ).arg( newRect.width() ).arg( newRect.height() ).arg( QgsUnitTypes::toAbbreviatedString( layout()->units() ) ) );
313}
314
315QRectF QgsLayoutViewTriangleRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
316{
317 const bool constrainSquare = modifiers & Qt::ShiftModifier;
318 const bool fromCenter = modifiers & Qt::AltModifier;
319
320 if ( mRubberBandItem )
321 {
322 layout()->removeItem( mRubberBandItem );
323 delete mRubberBandItem;
324 mRubberBandItem = nullptr;
325 }
326 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
327}
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 * 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(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.
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.
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:63
static Q_INVOKABLE QString toAbbreviatedString(Qgis::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.