QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgsmodelviewrubberband.cpp"
21#include <QGraphicsRectItem>
22
24 : mView( view )
25{
26
27}
28
29QgsModelGraphicsView *QgsModelViewRubberBand::view() const
30{
31 return mView;
32}
33
34QRectF QgsModelViewRubberBand::updateRect( QPointF start, QPointF position, bool constrainSquare, bool fromCenter )
35{
36 double x = 0;
37 double y = 0;
38 double width = 0;
39 double height = 0;
40
41 const double dx = position.x() - start.x();
42 const double dy = position.y() - start.y();
43
44 if ( constrainSquare )
45 {
46 if ( std::fabs( dx ) > std::fabs( dy ) )
47 {
48 width = std::fabs( dx );
49 height = width;
50 }
51 else
52 {
53 height = std::fabs( dy );
54 width = height;
55 }
56
57 x = start.x() - ( ( dx < 0 ) ? width : 0 );
58 y = start.y() - ( ( dy < 0 ) ? height : 0 );
59 }
60 else
61 {
62 //not constraining
63 if ( dx < 0 )
64 {
65 x = position.x();
66 width = -dx;
67 }
68 else
69 {
70 x = start.x();
71 width = dx;
72 }
73
74 if ( dy < 0 )
75 {
76 y = position.y();
77 height = -dy;
78 }
79 else
80 {
81 y = start.y();
82 height = dy;
83 }
84 }
85
86 if ( fromCenter )
87 {
88 x = start.x() - width;
89 y = start.y() - height;
90 width *= 2.0;
91 height *= 2.0;
92 }
93
94 return QRectF( x, y, width, height );
95}
96
98{
99 return mPen;
100}
101
102void QgsModelViewRubberBand::setPen( const QPen &pen )
103{
104 mPen = pen;
105}
106
108{
109 return mBrush;
110}
111
112void QgsModelViewRubberBand::setBrush( const QBrush &brush )
113{
114 mBrush = brush;
115}
116
117
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}
QgsModelViewRectangularRubberBand is rectangular rubber band for use within QgsModelGraphicsView widg...
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.
QgsModelViewRubberBand is an abstract base class for temporary rubber band items in various shapes,...
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.