QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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
20#include <QGraphicsRectItem>
21
23 : mView( view )
24{
25
26}
27
28QgsModelGraphicsView *QgsModelViewRubberBand::view() const
29{
30 return mView;
31}
32
33QRectF QgsModelViewRubberBand::updateRect( QPointF start, QPointF position, bool constrainSquare, bool fromCenter )
34{
35 double x = 0;
36 double y = 0;
37 double width = 0;
38 double height = 0;
39
40 const double dx = position.x() - start.x();
41 const double dy = position.y() - start.y();
42
43 if ( constrainSquare )
44 {
45 if ( std::fabs( dx ) > std::fabs( dy ) )
46 {
47 width = std::fabs( dx );
48 height = width;
49 }
50 else
51 {
52 height = std::fabs( dy );
53 width = height;
54 }
55
56 x = start.x() - ( ( dx < 0 ) ? width : 0 );
57 y = start.y() - ( ( dy < 0 ) ? height : 0 );
58 }
59 else
60 {
61 //not constraining
62 if ( dx < 0 )
63 {
64 x = position.x();
65 width = -dx;
66 }
67 else
68 {
69 x = start.x();
70 width = dx;
71 }
72
73 if ( dy < 0 )
74 {
75 y = position.y();
76 height = -dy;
77 }
78 else
79 {
80 y = start.y();
81 height = dy;
82 }
83 }
84
85 if ( fromCenter )
86 {
87 x = start.x() - width;
88 y = start.y() - height;
89 width *= 2.0;
90 height *= 2.0;
91 }
92
93 return QRectF( x, y, width, height );
94}
95
97{
98 return mPen;
99}
100
101void QgsModelViewRubberBand::setPen( const QPen &pen )
102{
103 mPen = pen;
104}
105
107{
108 return mBrush;
109}
110
111void QgsModelViewRubberBand::setBrush( const QBrush &brush )
112{
113 mBrush = brush;
114}
115
116
121
126
128{
129 if ( mRubberBandItem )
130 {
131 view()->scene()->removeItem( mRubberBandItem );
132 delete mRubberBandItem;
133 }
134}
135
136void QgsModelViewRectangularRubberBand::start( QPointF position, Qt::KeyboardModifiers )
137{
138 QTransform t;
139 mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
140 mRubberBandItem->setBrush( brush() );
141 mRubberBandItem->setPen( pen() );
142 mRubberBandStartPos = position;
143 t.translate( position.x(), position.y() );
144 mRubberBandItem->setTransform( t );
145 mRubberBandItem->setZValue( QgsModelGraphicsScene::RubberBand );
146 view()->scene()->addItem( mRubberBandItem );
147 view()->scene()->update();
148}
149
150void QgsModelViewRectangularRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
151{
152 if ( !mRubberBandItem )
153 {
154 return;
155 }
156
157 const bool constrainSquare = modifiers & Qt::ShiftModifier;
158 const bool fromCenter = modifiers & Qt::AltModifier;
159
160 const QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
161 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
162 QTransform t;
163 t.translate( newRect.x(), newRect.y() );
164 mRubberBandItem->setTransform( t );
165}
166
167QRectF QgsModelViewRectangularRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
168{
169 const bool constrainSquare = modifiers & Qt::ShiftModifier;
170 const bool fromCenter = modifiers & Qt::AltModifier;
171
172 if ( mRubberBandItem )
173 {
174 view()->scene()->removeItem( mRubberBandItem );
175 delete mRubberBandItem;
176 mRubberBandItem = nullptr;
177 }
178 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
179}
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.