QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 <QGraphicsRectItem>
21 #include <QGraphicsEllipseItem>
22 #include <QGraphicsPolygonItem>
23 
25  : mView( view )
26 {
27 
28 }
29 
31 {
32  return mView;
33 }
34 
36 {
37  return mView->currentLayout();
38 }
39 
40 QRectF QgsLayoutViewRubberBand::updateRect( QPointF start, QPointF position, bool constrainSquare, bool fromCenter )
41 {
42  double x = 0;
43  double y = 0;
44  double width = 0;
45  double height = 0;
46 
47  double dx = position.x() - start.x();
48  double dy = position.y() - start.y();
49 
50  if ( constrainSquare )
51  {
52  if ( std::fabs( dx ) > std::fabs( dy ) )
53  {
54  width = std::fabs( dx );
55  height = width;
56  }
57  else
58  {
59  height = std::fabs( dy );
60  width = height;
61  }
62 
63  x = start.x() - ( ( dx < 0 ) ? width : 0 );
64  y = start.y() - ( ( dy < 0 ) ? height : 0 );
65  }
66  else
67  {
68  //not constraining
69  if ( dx < 0 )
70  {
71  x = position.x();
72  width = -dx;
73  }
74  else
75  {
76  x = start.x();
77  width = dx;
78  }
79 
80  if ( dy < 0 )
81  {
82  y = position.y();
83  height = -dy;
84  }
85  else
86  {
87  y = start.y();
88  height = dy;
89  }
90  }
91 
92  if ( fromCenter )
93  {
94  x = start.x() - width;
95  y = start.y() - height;
96  width *= 2.0;
97  height *= 2.0;
98  }
99 
100  return QRectF( x, y, width, height );
101 }
102 
104 {
105  return mPen;
106 }
107 
108 void QgsLayoutViewRubberBand::setPen( const QPen &pen )
109 {
110  mPen = pen;
111 }
112 
114 {
115  return mBrush;
116 }
117 
118 void QgsLayoutViewRubberBand::setBrush( const QBrush &brush )
119 {
120  mBrush = brush;
121 }
122 
123 
125  : QgsLayoutViewRubberBand( view )
126 {
127 }
128 
130 {
132 }
133 
135 {
136  if ( mRubberBandItem )
137  {
138  layout()->removeItem( mRubberBandItem );
139  delete mRubberBandItem;
140  }
141 }
142 
143 void QgsLayoutViewRectangularRubberBand::start( QPointF position, Qt::KeyboardModifiers )
144 {
145  QTransform t;
146  mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
147  mRubberBandItem->setBrush( brush() );
148  mRubberBandItem->setPen( pen() );
149  mRubberBandStartPos = position;
150  t.translate( position.x(), position.y() );
151  mRubberBandItem->setTransform( t );
152  mRubberBandItem->setZValue( QgsLayout::ZViewTool );
153  layout()->addItem( mRubberBandItem );
154  layout()->update();
155 }
156 
157 void QgsLayoutViewRectangularRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
158 {
159  if ( !mRubberBandItem )
160  {
161  return;
162  }
163 
164  bool constrainSquare = modifiers & Qt::ShiftModifier;
165  bool fromCenter = modifiers & Qt::AltModifier;
166 
167  QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
168  mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
169  QTransform t;
170  t.translate( newRect.x(), newRect.y() );
171  mRubberBandItem->setTransform( t );
172 
173  emit sizeChanged( tr( "width: %1 %3 height: %2 %3" ).arg( newRect.width() ).arg( newRect.height() ).arg( QgsUnitTypes::toAbbreviatedString( layout()->units() ) ) );
174 }
175 
176 QRectF QgsLayoutViewRectangularRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
177 {
178  bool constrainSquare = modifiers & Qt::ShiftModifier;
179  bool fromCenter = modifiers & Qt::AltModifier;
180 
181  if ( mRubberBandItem )
182  {
183  layout()->removeItem( mRubberBandItem );
184  delete mRubberBandItem;
185  mRubberBandItem = nullptr;
186  }
187  return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
188 }
189 
191  : QgsLayoutViewRubberBand( view )
192 {
193 
194 }
195 
197 {
199 }
200 
202 {
203  if ( mRubberBandItem )
204  {
205  layout()->removeItem( mRubberBandItem );
206  delete mRubberBandItem;
207  }
208 }
209 
210 void 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 
224 void QgsLayoutViewEllipticalRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
225 {
226  if ( !mRubberBandItem )
227  {
228  return;
229  }
230 
231  bool constrainSquare = modifiers & Qt::ShiftModifier;
232  bool fromCenter = modifiers & Qt::AltModifier;
233 
234  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 
243 QRectF QgsLayoutViewEllipticalRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
244 {
245  bool constrainSquare = modifiers & Qt::ShiftModifier;
246  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 
262  : QgsLayoutViewRubberBand( view )
263 {
264 
265 }
266 
268 {
270 }
271 
273 {
274  if ( mRubberBandItem )
275  {
276  layout()->removeItem( mRubberBandItem );
277  delete mRubberBandItem;
278  }
279 }
280 
281 void QgsLayoutViewTriangleRubberBand::start( QPointF position, Qt::KeyboardModifiers )
282 {
283  QTransform t;
284  mRubberBandItem = new QGraphicsPolygonItem();
285  mRubberBandItem->setBrush( brush() );
286  mRubberBandItem->setPen( pen() );
287  mRubberBandStartPos = position;
288  t.translate( position.x(), position.y() );
289  mRubberBandItem->setTransform( t );
290  mRubberBandItem->setZValue( QgsLayout::ZViewTool );
291  layout()->addItem( mRubberBandItem );
292  layout()->update();
293 }
294 
295 void QgsLayoutViewTriangleRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
296 {
297  if ( !mRubberBandItem )
298  {
299  return;
300  }
301 
302  bool constrainSquare = modifiers & Qt::ShiftModifier;
303  bool fromCenter = modifiers & Qt::AltModifier;
304 
305  QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
306 
307  QPolygonF shapePolygon = QPolygonF() << QPointF( 0, newRect.height() )
308  << QPointF( newRect.width(), newRect.height() )
309  << QPointF( newRect.width() / 2.0, 0 )
310  << QPointF( 0, newRect.height() );
311 
312  mRubberBandItem->setPolygon( shapePolygon );
313  QTransform t;
314  t.translate( newRect.x(), newRect.y() );
315  mRubberBandItem->setTransform( t );
316 
317  emit sizeChanged( tr( "width: %1 %3 height: %2 %3" ).arg( newRect.width() ).arg( newRect.height() ).arg( QgsUnitTypes::toAbbreviatedString( layout()->units() ) ) );
318 }
319 
320 QRectF QgsLayoutViewTriangleRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
321 {
322  bool constrainSquare = modifiers & Qt::ShiftModifier;
323  bool fromCenter = modifiers & Qt::AltModifier;
324 
325  if ( mRubberBandItem )
326  {
327  layout()->removeItem( mRubberBandItem );
328  delete mRubberBandItem;
329  mRubberBandItem = nullptr;
330  }
331  return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
332 }
QgsLayoutViewTriangleRubberBand::create
QgsLayoutViewTriangleRubberBand * create(QgsLayoutView *view) const override
Creates a new instance of the QgsLayoutViewRubberBand subclass.
Definition: qgslayoutviewrubberband.cpp:267
QgsLayoutViewRectangularRubberBand::create
QgsLayoutViewRectangularRubberBand * create(QgsLayoutView *view) const override
Creates a new instance of the QgsLayoutViewRubberBand subclass.
Definition: qgslayoutviewrubberband.cpp:129
QgsLayoutViewRubberBand::start
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 ...
QgsLayoutViewRubberBand::pen
QPen pen() const
Returns the pen used for drawing the rubber band.
Definition: qgslayoutviewrubberband.cpp:103
QgsLayoutViewRectangularRubberBand::QgsLayoutViewRectangularRubberBand
QgsLayoutViewRectangularRubberBand(QgsLayoutView *view=nullptr)
Constructor for QgsLayoutViewRectangularRubberBand.
Definition: qgslayoutviewrubberband.cpp:124
QgsUnitTypes::toAbbreviatedString
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
Definition: qgsunittypes.cpp:269
qgslayoutview.h
QgsLayoutViewRubberBand::layout
QgsLayout * layout() const
Returns the layout associated with the rubber band.
Definition: qgslayoutviewrubberband.cpp:35
QgsLayoutViewRectangularRubberBand::update
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...
Definition: qgslayoutviewrubberband.cpp:157
QgsLayoutViewRectangularRubberBand
QgsLayoutViewRectangularRubberBand is rectangular rubber band for use within QgsLayoutView widgets.
Definition: qgslayoutviewrubberband.h:160
QgsLayoutViewRubberBand::QgsLayoutViewRubberBand
QgsLayoutViewRubberBand(QgsLayoutView *view=nullptr)
Constructor for QgsLayoutViewRubberBand.
Definition: qgslayoutviewrubberband.cpp:24
QgsLayoutViewTriangleRubberBand
QgsLayoutViewTriangleRubberBand is triangular rubber band for use within QgsLayoutView widgets.
Definition: qgslayoutviewrubberband.h:226
QgsLayoutViewRubberBand::brush
QBrush brush() const
Returns the brush used for drawing the rubber band.
Definition: qgslayoutviewrubberband.cpp:113
QgsLayoutViewRubberBand::sizeChanged
void sizeChanged(const QString &size)
Emitted when the size of the rubber band is changed.
QgsLayoutViewEllipticalRubberBand::create
QgsLayoutViewEllipticalRubberBand * create(QgsLayoutView *view) const override
Creates a new instance of the QgsLayoutViewRubberBand subclass.
Definition: qgslayoutviewrubberband.cpp:196
QgsLayoutViewRubberBand::updateRect
QRectF updateRect(QPointF start, QPointF position, bool constrainSquare, bool fromCenter)
Calculates an updated bounding box rectangle from a original start position and new position.
Definition: qgslayoutviewrubberband.cpp:40
QgsLayoutViewRectangularRubberBand::~QgsLayoutViewRectangularRubberBand
~QgsLayoutViewRectangularRubberBand() override
Definition: qgslayoutviewrubberband.cpp:134
qgslayoutviewrubberband.h
QgsLayoutViewTriangleRubberBand::update
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...
Definition: qgslayoutviewrubberband.cpp:295
QgsLayoutViewEllipticalRubberBand::QgsLayoutViewEllipticalRubberBand
QgsLayoutViewEllipticalRubberBand(QgsLayoutView *view=nullptr)
Constructor for QgsLayoutViewEllipticalRubberBand.
Definition: qgslayoutviewrubberband.cpp:190
QgsLayoutViewRubberBand::setBrush
void setBrush(const QBrush &brush)
Sets the brush used for drawing the rubber band.
Definition: qgslayoutviewrubberband.cpp:118
QgsLayoutView::currentLayout
QgsLayout * currentLayout
Definition: qgslayoutview.h:63
QgsLayoutViewEllipticalRubberBand::update
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...
Definition: qgslayoutviewrubberband.cpp:224
qgslayout.h
QgsLayout::ZViewTool
@ ZViewTool
Z-value for temporary view tool items.
Definition: qgslayout.h:64
QgsLayoutViewTriangleRubberBand::~QgsLayoutViewTriangleRubberBand
~QgsLayoutViewTriangleRubberBand() override
Definition: qgslayoutviewrubberband.cpp:272
QgsLayoutViewEllipticalRubberBand::start
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in layout coordinate ...
Definition: qgslayoutviewrubberband.cpp:210
QgsLayoutViewRectangularRubberBand::start
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in layout coordinate ...
Definition: qgslayoutviewrubberband.cpp:143
QgsLayoutViewRubberBand::setPen
void setPen(const QPen &pen)
Sets the pen used for drawing the rubber band.
Definition: qgslayoutviewrubberband.cpp:108
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsLayoutView
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:50
QgsLayoutViewRubberBand::view
QgsLayoutView * view() const
Returns the view associated with the rubber band.
Definition: qgslayoutviewrubberband.cpp:30
QgsLayoutViewRectangularRubberBand::finish
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.
Definition: qgslayoutviewrubberband.cpp:176
QgsLayoutViewRubberBand
QgsLayoutViewRubberBand is an abstract base class for temporary rubber band items in various shapes,...
Definition: qgslayoutviewrubberband.h:38
QgsLayoutViewEllipticalRubberBand::~QgsLayoutViewEllipticalRubberBand
~QgsLayoutViewEllipticalRubberBand() override
Definition: qgslayoutviewrubberband.cpp:201
QgsLayoutViewTriangleRubberBand::start
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in layout coordinate ...
Definition: qgslayoutviewrubberband.cpp:281
QgsLayoutViewTriangleRubberBand::QgsLayoutViewTriangleRubberBand
QgsLayoutViewTriangleRubberBand(QgsLayoutView *view=nullptr)
Constructor for QgsLayoutViewTriangleRubberBand.
Definition: qgslayoutviewrubberband.cpp:261
QgsLayoutViewTriangleRubberBand::finish
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.
Definition: qgslayoutviewrubberband.cpp:320
QgsLayoutViewEllipticalRubberBand
QgsLayoutViewEllipseRubberBand is elliptical rubber band for use within QgsLayoutView widgets.
Definition: qgslayoutviewrubberband.h:193
QgsLayoutViewEllipticalRubberBand::finish
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.
Definition: qgslayoutviewrubberband.cpp:243