QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
30QgsModelGraphicsView *QgsModelViewRubberBand::view() const
31{
32 return mView;
33}
34
35QRectF QgsModelViewRubberBand::updateRect( QPointF start, QPointF position, bool constrainSquare, bool fromCenter )
36{
37 double x = 0;
38 double y = 0;
39 double width = 0;
40 double height = 0;
41
42 const double dx = position.x() - start.x();
43 const double dy = position.y() - start.y();
44
45 if ( constrainSquare )
46 {
47 if ( std::fabs( dx ) > std::fabs( dy ) )
48 {
49 width = std::fabs( dx );
50 height = width;
51 }
52 else
53 {
54 height = std::fabs( dy );
55 width = height;
56 }
57
58 x = start.x() - ( ( dx < 0 ) ? width : 0 );
59 y = start.y() - ( ( dy < 0 ) ? height : 0 );
60 }
61 else
62 {
63 //not constraining
64 if ( dx < 0 )
65 {
66 x = position.x();
67 width = -dx;
68 }
69 else
70 {
71 x = start.x();
72 width = dx;
73 }
74
75 if ( dy < 0 )
76 {
77 y = position.y();
78 height = -dy;
79 }
80 else
81 {
82 y = start.y();
83 height = dy;
84 }
85 }
86
87 if ( fromCenter )
88 {
89 x = start.x() - width;
90 y = start.y() - height;
91 width *= 2.0;
92 height *= 2.0;
93 }
94
95 return QRectF( x, y, width, height );
96}
97
99{
100 return mPen;
101}
102
104{
105 mPen = pen;
106}
107
109{
110 return mBrush;
111}
112
114{
115 mBrush = brush;
116}
117
118
122
127
129{
130 if ( mRubberBandItem )
131 {
132 view()->scene()->removeItem( mRubberBandItem );
133 delete mRubberBandItem;
134 }
135}
136
137void QgsModelViewRectangularRubberBand::start( QPointF position, Qt::KeyboardModifiers )
138{
139 QTransform t;
140 mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
141 mRubberBandItem->setBrush( brush() );
142 mRubberBandItem->setPen( pen() );
143 mRubberBandStartPos = position;
144 t.translate( position.x(), position.y() );
145 mRubberBandItem->setTransform( t );
146 mRubberBandItem->setZValue( QgsModelGraphicsScene::RubberBand );
147 view()->scene()->addItem( mRubberBandItem );
148 view()->scene()->update();
149}
150
151void QgsModelViewRectangularRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
152{
153 if ( !mRubberBandItem )
154 {
155 return;
156 }
157
158 const bool constrainSquare = modifiers & Qt::ShiftModifier;
159 const bool fromCenter = modifiers & Qt::AltModifier;
160
161 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
162 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
163 QTransform t;
164 t.translate( newRect.x(), newRect.y() );
165 mRubberBandItem->setTransform( t );
166}
167
168QRectF QgsModelViewRectangularRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
169{
170 const bool constrainSquare = modifiers & Qt::ShiftModifier;
171 const bool fromCenter = modifiers & Qt::AltModifier;
172
173 if ( mRubberBandItem )
174 {
175 view()->scene()->removeItem( mRubberBandItem );
176 delete mRubberBandItem;
177 mRubberBandItem = nullptr;
178 }
179 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
180}
181
182
186
191
193{
194 if ( mRubberBandItem )
195 {
196 view()->scene()->removeItem( mRubberBandItem );
197 delete mRubberBandItem;
198 }
199}
200
201void QgsModelViewBezierRubberBand::start( QPointF position, Qt::KeyboardModifiers )
202{
203 // cppcheck-suppress publicAllocationError
204 mRubberBandItem = new QGraphicsPathItem();
205 mRubberBandItem->setBrush( Qt::NoBrush );
206 mRubberBandItem->setPen( pen() );
207 mRubberBandStartPos = position;
208 mRubberBandItem->setZValue( QgsModelGraphicsScene::RubberBand );
209 view()->scene()->addItem( mRubberBandItem );
210 view()->scene()->update();
211}
212
213void QgsModelViewBezierRubberBand::update( QPointF position, Qt::KeyboardModifiers )
214{
215 if ( !mRubberBandItem )
216 {
217 return;
218 }
219
220
221 // Change the offset
222 QList<QPointF> controlPoints;
223
224 int offsetX = ( position.x() - mRubberBandStartPos.x() > 0 ) ? 50 : -50;
225
226 controlPoints.append( mRubberBandStartPos );
227 controlPoints.append( mRubberBandStartPos + QPointF( offsetX, 0 ) );
228 controlPoints.append( position - QPointF( offsetX, 0 ) );
229 controlPoints.append( position );
230
231 QPainterPath path;
232
233 path.moveTo( controlPoints.at( 0 ) );
234 path.cubicTo( controlPoints.at( 1 ), controlPoints.at( 2 ), controlPoints.at( 3 ) );
235
236 mRubberBandItem->setPath( path );
237}
238
239QRectF QgsModelViewBezierRubberBand::finish( QPointF position, Qt::KeyboardModifiers )
240{
241 if ( mRubberBandItem )
242 {
243 view()->scene()->removeItem( mRubberBandItem );
244 delete mRubberBandItem;
245 mRubberBandItem = nullptr;
246 }
247 return updateRect( mRubberBandStartPos, position, false, false );
248}
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.