QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
33
35{
36 return mView;
37}
38
40{
41 return mView->currentLayout();
42}
43
44QRectF QgsLayoutViewRubberBand::updateRect( QPointF start, QPointF position, bool constrainSquare, bool fromCenter )
45{
46 double x = 0;
47 double y = 0;
48 double width = 0;
49 double height = 0;
50
51 const double dx = position.x() - start.x();
52 const double dy = position.y() - start.y();
53
54 if ( constrainSquare )
55 {
56 if ( std::fabs( dx ) > std::fabs( dy ) )
57 {
58 width = std::fabs( dx );
59 height = width;
60 }
61 else
62 {
63 height = std::fabs( dy );
64 width = height;
65 }
66
67 x = start.x() - ( ( dx < 0 ) ? width : 0 );
68 y = start.y() - ( ( dy < 0 ) ? height : 0 );
69 }
70 else
71 {
72 //not constraining
73 if ( dx < 0 )
74 {
75 x = position.x();
76 width = -dx;
77 }
78 else
79 {
80 x = start.x();
81 width = dx;
82 }
83
84 if ( dy < 0 )
85 {
86 y = position.y();
87 height = -dy;
88 }
89 else
90 {
91 y = start.y();
92 height = dy;
93 }
94 }
95
96 if ( fromCenter )
97 {
98 x = start.x() - width;
99 y = start.y() - height;
100 width *= 2.0;
101 height *= 2.0;
102 }
103
104 return QRectF( x, y, width, height );
105}
106
108{
109 return mPen;
110}
111
113{
114 mPen = pen;
115}
116
118{
119 return mBrush;
120}
121
123{
124 mBrush = brush;
125}
126
127
132
137
139{
140 if ( mRubberBandItem )
141 {
142 layout()->removeItem( mRubberBandItem );
143 delete mRubberBandItem;
144 }
145}
146
147void QgsLayoutViewRectangularRubberBand::start( QPointF position, Qt::KeyboardModifiers )
148{
149 QTransform t;
150 mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
151 mRubberBandItem->setBrush( brush() );
152 mRubberBandItem->setPen( pen() );
153 mRubberBandStartPos = position;
154 t.translate( position.x(), position.y() );
155 mRubberBandItem->setTransform( t );
156 mRubberBandItem->setZValue( QgsLayout::ZViewTool );
157 layout()->addItem( mRubberBandItem );
158 layout()->update();
159}
160
161void QgsLayoutViewRectangularRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
162{
163 if ( !mRubberBandItem )
164 {
165 return;
166 }
167
168 const bool constrainSquare = modifiers & Qt::ShiftModifier;
169 const bool fromCenter = modifiers & Qt::AltModifier;
170
171 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
172 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
173 QTransform t;
174 t.translate( newRect.x(), newRect.y() );
175 mRubberBandItem->setTransform( t );
176
177 emit sizeChanged( tr( "width: %1 %3 height: %2 %3" ).arg( newRect.width() ).arg( newRect.height() ).arg( QgsUnitTypes::toAbbreviatedString( layout()->units() ) ) );
178}
179
180QRectF QgsLayoutViewRectangularRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
181{
182 const bool constrainSquare = modifiers & Qt::ShiftModifier;
183 const bool fromCenter = modifiers & Qt::AltModifier;
184
185 if ( mRubberBandItem )
186 {
187 layout()->removeItem( mRubberBandItem );
188 delete mRubberBandItem;
189 mRubberBandItem = nullptr;
190 }
191 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
192}
193
198
203
205{
206 if ( mRubberBandItem )
207 {
208 layout()->removeItem( mRubberBandItem );
209 delete mRubberBandItem;
210 }
211}
212
213void QgsLayoutViewEllipticalRubberBand::start( QPointF position, Qt::KeyboardModifiers )
214{
215 QTransform t;
216 mRubberBandItem = new QGraphicsEllipseItem( 0, 0, 0, 0 );
217 mRubberBandItem->setBrush( brush() );
218 mRubberBandItem->setPen( pen() );
219 mRubberBandStartPos = position;
220 t.translate( position.x(), position.y() );
221 mRubberBandItem->setTransform( t );
222 mRubberBandItem->setZValue( QgsLayout::ZViewTool );
223 layout()->addItem( mRubberBandItem );
224 layout()->update();
225}
226
227void QgsLayoutViewEllipticalRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
228{
229 if ( !mRubberBandItem )
230 {
231 return;
232 }
233
234 const bool constrainSquare = modifiers & Qt::ShiftModifier;
235 const bool fromCenter = modifiers & Qt::AltModifier;
236
237 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
238 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
239 QTransform t;
240 t.translate( newRect.x(), newRect.y() );
241 mRubberBandItem->setTransform( t );
242
243 emit sizeChanged( tr( "width: %1 %3 height: %2 %3" ).arg( newRect.width() ).arg( newRect.height() ).arg( QgsUnitTypes::toAbbreviatedString( layout()->units() ) ) );
244}
245
246QRectF QgsLayoutViewEllipticalRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
247{
248 const bool constrainSquare = modifiers & Qt::ShiftModifier;
249 const bool fromCenter = modifiers & Qt::AltModifier;
250
251 if ( mRubberBandItem )
252 {
253 layout()->removeItem( mRubberBandItem );
254 delete mRubberBandItem;
255 mRubberBandItem = nullptr;
256 }
257 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
258}
259
260//
261// QgsLayoutViewTriangleRubberBand
262//
263
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}
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:64
static Q_INVOKABLE QString toAbbreviatedString(Qgis::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.