QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsmodelviewrubberband.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmodelviewrubberband.cpp
3 ---------------------------
4 Date : March 2020
5 Copyright : (C) 2020 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
21
22#include <QGraphicsRectItem>
23
24#include "moc_qgsmodelviewrubberband.cpp"
25
27 : mView( view )
28{
29}
30
31QgsModelGraphicsView *QgsModelViewRubberBand::view() const
32{
33 return mView;
34}
35
36QRectF QgsModelViewRubberBand::updateRect( QPointF start, QPointF position, bool constrainSquare, bool fromCenter )
37{
38 double x = 0;
39 double y = 0;
40 double width = 0;
41 double height = 0;
42
43 const double dx = position.x() - start.x();
44 const double dy = position.y() - start.y();
45
46 if ( constrainSquare )
47 {
48 if ( std::fabs( dx ) > std::fabs( dy ) )
49 {
50 width = std::fabs( dx );
51 height = width;
52 }
53 else
54 {
55 height = std::fabs( dy );
56 width = height;
57 }
58
59 x = start.x() - ( ( dx < 0 ) ? width : 0 );
60 y = start.y() - ( ( dy < 0 ) ? height : 0 );
61 }
62 else
63 {
64 //not constraining
65 if ( dx < 0 )
66 {
67 x = position.x();
68 width = -dx;
69 }
70 else
71 {
72 x = start.x();
73 width = dx;
74 }
75
76 if ( dy < 0 )
77 {
78 y = position.y();
79 height = -dy;
80 }
81 else
82 {
83 y = start.y();
84 height = dy;
85 }
86 }
87
88 if ( fromCenter )
89 {
90 x = start.x() - width;
91 y = start.y() - height;
92 width *= 2.0;
93 height *= 2.0;
94 }
95
96 return QRectF( x, y, width, height );
97}
98
100{
101 return mPen;
102}
103
105{
106 mPen = pen;
107}
108
110{
111 return mBrush;
112}
113
115{
116 mBrush = brush;
117}
118
119
124
129
131{
132 if ( mRubberBandItem )
133 {
134 view()->scene()->removeItem( mRubberBandItem );
135 delete mRubberBandItem;
136 }
137}
138
139void QgsModelViewRectangularRubberBand::start( QPointF position, Qt::KeyboardModifiers )
140{
141 QTransform t;
142 mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
143 mRubberBandItem->setBrush( brush() );
144 mRubberBandItem->setPen( pen() );
145 mRubberBandStartPos = position;
146 t.translate( position.x(), position.y() );
147 mRubberBandItem->setTransform( t );
148 mRubberBandItem->setZValue( QgsModelGraphicsScene::RubberBand );
149 view()->scene()->addItem( mRubberBandItem );
150 view()->scene()->update();
151}
152
153void QgsModelViewRectangularRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
154{
155 if ( !mRubberBandItem )
156 {
157 return;
158 }
159
160 const bool constrainSquare = modifiers & Qt::ShiftModifier;
161 const bool fromCenter = modifiers & Qt::AltModifier;
162
163 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
164 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
165 QTransform t;
166 t.translate( newRect.x(), newRect.y() );
167 mRubberBandItem->setTransform( t );
168}
169
170QRectF QgsModelViewRectangularRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
171{
172 const bool constrainSquare = modifiers & Qt::ShiftModifier;
173 const bool fromCenter = modifiers & Qt::AltModifier;
174
175 if ( mRubberBandItem )
176 {
177 view()->scene()->removeItem( mRubberBandItem );
178 delete mRubberBandItem;
179 mRubberBandItem = nullptr;
180 }
181 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
182}
183
184
189
194
196{
197 if ( mRubberBandItem )
198 {
199 view()->scene()->removeItem( mRubberBandItem );
200 delete mRubberBandItem;
201 }
202}
203
204void QgsModelViewBezierRubberBand::start( QPointF position, Qt::KeyboardModifiers )
205{
206 // cppcheck-suppress publicAllocationError
207 mRubberBandItem = new QGraphicsPathItem();
208 mRubberBandItem->setBrush( Qt::NoBrush );
209 mRubberBandItem->setPen( pen() );
210 mRubberBandStartPos = position;
211 mRubberBandItem->setZValue( QgsModelGraphicsScene::RubberBand );
212 view()->scene()->addItem( mRubberBandItem );
213 view()->scene()->update();
214}
215
216void QgsModelViewBezierRubberBand::update( QPointF position, Qt::KeyboardModifiers )
217{
218 if ( !mRubberBandItem )
219 {
220 return;
221 }
222
223
224 // Change the offset
225 QList<QPointF> controlPoints;
226
227 int offsetX = ( position.x() - mRubberBandStartPos.x() > 0 ) ? 50 : -50;
228
229 controlPoints.append( mRubberBandStartPos );
230 controlPoints.append( mRubberBandStartPos + QPointF( offsetX, 0 ) );
231 controlPoints.append( position - QPointF( offsetX, 0 ) );
232 controlPoints.append( position );
233
234 QPainterPath path;
235
236 path.moveTo( controlPoints.at( 0 ) );
237 path.cubicTo( controlPoints.at( 1 ), controlPoints.at( 2 ), controlPoints.at( 3 ) );
238
239 mRubberBandItem->setPath( path );
240}
241
242QRectF QgsModelViewBezierRubberBand::finish( QPointF position, Qt::KeyboardModifiers )
243{
244 if ( mRubberBandItem )
245 {
246 view()->scene()->removeItem( mRubberBandItem );
247 delete mRubberBandItem;
248 mRubberBandItem = nullptr;
249 }
250 return updateRect( mRubberBandStartPos, position, false, false );
251}
void update(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be updated to reflect a temporary ending position (in model coordina...
QgsModelViewBezierRubberBand * create(QgsModelGraphicsView *view) const override
Creates a new instance of the QgsModelViewRubberBand subclass.
QgsModelViewBezierRubberBand(QgsModelGraphicsView *view=nullptr)
Constructor for QgsModelViewRectangularRubberBand.
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.
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in model coordinate s...
QgsModelViewRectangularRubberBand(QgsModelGraphicsView *view=nullptr)
Constructor for QgsModelViewRectangularRubberBand.
void update(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be updated to reflect a temporary ending position (in model coordina...
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in model coordinate s...
QgsModelViewRectangularRubberBand * create(QgsModelGraphicsView *view) const override
Creates a new instance of the QgsModelViewRubberBand subclass.
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.
QRectF updateRect(QPointF start, QPointF position, bool constrainSquare, bool fromCenter)
Calculates an updated bounding box rectangle from a original start position and new position.
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.
QgsModelViewRubberBand(QgsModelGraphicsView *view=nullptr)
Constructor for QgsModelViewRubberBand.
QPen pen() const
Returns the pen used for drawing the rubber band.
QgsModelGraphicsView * view() const
Returns the view associated with 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 model coordinate s...
QBrush brush() const
Returns the brush used for drawing the rubber band.