QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
17 #include "qgsmodelviewrubberband.h"
18 #include "qgsmodelgraphicsview.h"
19 #include "qgsmodelgraphicsscene.h"
20 #include <QGraphicsRectItem>
21 
23  : mView( view )
24 {
25 
26 }
27 
28 QgsModelGraphicsView *QgsModelViewRubberBand::view() const
29 {
30  return mView;
31 }
32 
33 QRectF 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  double dx = position.x() - start.x();
41  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 
101 void QgsModelViewRubberBand::setPen( const QPen &pen )
102 {
103  mPen = pen;
104 }
105 
107 {
108  return mBrush;
109 }
110 
111 void QgsModelViewRubberBand::setBrush( const QBrush &brush )
112 {
113  mBrush = brush;
114 }
115 
116 
118  : QgsModelViewRubberBand( view )
119 {
120 }
121 
123 {
125 }
126 
128 {
129  if ( mRubberBandItem )
130  {
131  view()->scene()->removeItem( mRubberBandItem );
132  delete mRubberBandItem;
133  }
134 }
135 
136 void 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 
150 void QgsModelViewRectangularRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
151 {
152  if ( !mRubberBandItem )
153  {
154  return;
155  }
156 
157  bool constrainSquare = modifiers & Qt::ShiftModifier;
158  bool fromCenter = modifiers & Qt::AltModifier;
159 
160  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 
167 QRectF QgsModelViewRectangularRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
168 {
169  bool constrainSquare = modifiers & Qt::ShiftModifier;
170  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::update
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...
Definition: qgsmodelviewrubberband.cpp:150
QgsModelViewRectangularRubberBand::create
QgsModelViewRectangularRubberBand * create(QgsModelGraphicsView *view) const override
Creates a new instance of the QgsModelViewRubberBand subclass.
Definition: qgsmodelviewrubberband.cpp:122
QgsModelViewRubberBand::view
QgsModelGraphicsView * view() const
Returns the view associated with the rubber band.
Definition: qgsmodelviewrubberband.cpp:28
QgsModelViewRectangularRubberBand::QgsModelViewRectangularRubberBand
QgsModelViewRectangularRubberBand(QgsModelGraphicsView *view=nullptr)
Constructor for QgsModelViewRectangularRubberBand.
Definition: qgsmodelviewrubberband.cpp:117
QgsModelViewRubberBand::QgsModelViewRubberBand
QgsModelViewRubberBand(QgsModelGraphicsView *view=nullptr)
Constructor for QgsModelViewRubberBand.
Definition: qgsmodelviewrubberband.cpp:22
QgsModelViewRubberBand::start
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...
QgsModelViewRubberBand::brush
QBrush brush() const
Returns the brush used for drawing the rubber band.
Definition: qgsmodelviewrubberband.cpp:106
QgsModelViewRectangularRubberBand::start
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...
Definition: qgsmodelviewrubberband.cpp:136
QgsModelViewRectangularRubberBand::~QgsModelViewRectangularRubberBand
~QgsModelViewRectangularRubberBand() override
Definition: qgsmodelviewrubberband.cpp:127
QgsModelViewRectangularRubberBand::finish
QRectF finish(QPointF position=QPointF(), Qt::KeyboardModifiers modifiers=nullptr) override
Called when a rubber band use has finished and the rubber band is no longer required.
Definition: qgsmodelviewrubberband.cpp:167
qgsmodelgraphicsview.h
QgsModelViewRubberBand::setPen
void setPen(const QPen &pen)
Sets the pen used for drawing the rubber band.
Definition: qgsmodelviewrubberband.cpp:101
QgsModelViewRubberBand::setBrush
void setBrush(const QBrush &brush)
Sets the brush used for drawing the rubber band.
Definition: qgsmodelviewrubberband.cpp:111
qgsmodelgraphicsscene.h
QgsModelViewRubberBand::pen
QPen pen() const
Returns the pen used for drawing the rubber band.
Definition: qgsmodelviewrubberband.cpp:96
QgsModelViewRectangularRubberBand
Definition: qgsmodelviewrubberband.h:144
QgsModelViewRubberBand
Definition: qgsmodelviewrubberband.h:38
QgsModelViewRubberBand::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: qgsmodelviewrubberband.cpp:33
qgsmodelviewrubberband.h