QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
gui
plot
qgsplotrubberband.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgsplotrubberband.cpp
3
---------------
4
begin : March 2022
5
copyright : (C) 2022 by Nyall Dawson
6
email : nyall dot dawson at gmail dot com
7
***************************************************************************/
8
9
/***************************************************************************
10
* *
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
15
* *
16
***************************************************************************/
17
18
#include "
qgsplotrubberband.h
"
19
20
#include <cmath>
21
22
#include "
qgsplotcanvas.h
"
23
24
#include <QGraphicsRectItem>
25
#include <QGraphicsScene>
26
27
#include "moc_qgsplotrubberband.cpp"
28
29
QgsPlotRubberBand::QgsPlotRubberBand
(
QgsPlotCanvas
*
canvas
)
30
: mCanvas(
canvas
)
31
{}
32
33
QgsPlotCanvas
*
QgsPlotRubberBand::canvas
()
const
34
{
35
return
mCanvas;
36
}
37
38
QRectF
QgsPlotRubberBand::updateRect
( QPointF
start
, QPointF position,
bool
constrainSquare,
bool
fromCenter )
39
{
40
double
x = 0;
41
double
y = 0;
42
double
width = 0;
43
double
height = 0;
44
45
const
double
dx = position.x() -
start
.x();
46
const
double
dy = position.y() -
start
.y();
47
48
if
( constrainSquare )
49
{
50
if
( std::fabs( dx ) > std::fabs( dy ) )
51
{
52
width = std::fabs( dx );
53
height = width;
54
}
55
else
56
{
57
height = std::fabs( dy );
58
width = height;
59
}
60
61
x =
start
.x() - ( ( dx < 0 ) ? width : 0 );
62
y =
start
.y() - ( ( dy < 0 ) ? height : 0 );
63
}
64
else
65
{
66
//not constraining
67
if
( dx < 0 )
68
{
69
x = position.x();
70
width = -dx;
71
}
72
else
73
{
74
x =
start
.x();
75
width = dx;
76
}
77
78
if
( dy < 0 )
79
{
80
y = position.y();
81
height = -dy;
82
}
83
else
84
{
85
y =
start
.y();
86
height = dy;
87
}
88
}
89
90
if
( fromCenter )
91
{
92
x =
start
.x() - width;
93
y =
start
.y() - height;
94
width *= 2.0;
95
height *= 2.0;
96
}
97
98
return
QRectF( x, y, width, height );
99
}
100
101
QPen
QgsPlotRubberBand::pen
()
const
102
{
103
return
mPen;
104
}
105
106
void
QgsPlotRubberBand::setPen
(
const
QPen &
pen
)
107
{
108
mPen =
pen
;
109
}
110
111
QBrush
QgsPlotRubberBand::brush
()
const
112
{
113
return
mBrush;
114
}
115
116
void
QgsPlotRubberBand::setBrush
(
const
QBrush &
brush
)
117
{
118
mBrush =
brush
;
119
}
120
121
122
QgsPlotRectangularRubberBand::QgsPlotRectangularRubberBand
(
QgsPlotCanvas
*
canvas
)
123
:
QgsPlotRubberBand
(
canvas
)
124
{}
125
126
QgsPlotRectangularRubberBand::~QgsPlotRectangularRubberBand
()
127
{
128
if
( mRubberBandItem )
129
{
130
canvas
()->scene()->removeItem( mRubberBandItem );
131
delete
mRubberBandItem;
132
}
133
}
134
135
void
QgsPlotRectangularRubberBand::start
( QPointF position, Qt::KeyboardModifiers )
136
{
137
QTransform t;
138
mRubberBandItem =
new
QGraphicsRectItem( 0, 0, 0, 0 );
139
mRubberBandItem->setBrush(
brush
() );
140
mRubberBandItem->setPen(
pen
() );
141
mRubberBandStartPos = position;
142
t.translate( position.x(), position.y() );
143
mRubberBandItem->setTransform( t );
144
mRubberBandItem->setZValue( 1000 );
145
canvas
()->scene()->addItem( mRubberBandItem );
146
canvas
()->scene()->update();
147
}
148
149
void
QgsPlotRectangularRubberBand::update
( QPointF position, Qt::KeyboardModifiers modifiers )
150
{
151
if
( !mRubberBandItem )
152
{
153
return
;
154
}
155
156
const
bool
constrainSquare = modifiers & Qt::ShiftModifier;
157
const
bool
fromCenter = modifiers & Qt::AltModifier;
158
159
const
QRectF newRect =
updateRect
( mRubberBandStartPos, position, constrainSquare, fromCenter );
160
mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
161
QTransform t;
162
t.translate( newRect.x(), newRect.y() );
163
mRubberBandItem->setTransform( t );
164
}
165
166
QRectF
QgsPlotRectangularRubberBand::finish
( QPointF position, Qt::KeyboardModifiers modifiers )
167
{
168
const
bool
constrainSquare = modifiers & Qt::ShiftModifier;
169
const
bool
fromCenter = modifiers & Qt::AltModifier;
170
171
if
( mRubberBandItem )
172
{
173
canvas
()->scene()->removeItem( mRubberBandItem );
174
delete
mRubberBandItem;
175
mRubberBandItem =
nullptr
;
176
}
177
return
updateRect
( mRubberBandStartPos, position, constrainSquare, fromCenter );
178
}
QgsPlotCanvas
Plot canvas is a class for displaying interactive 2d charts and plots.
Definition
qgsplotcanvas.h:55
QgsPlotRectangularRubberBand::start
void start(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be created at the specified starting position (in canvas coordinate ...
Definition
qgsplotrubberband.cpp:135
QgsPlotRectangularRubberBand::update
void update(QPointF position, Qt::KeyboardModifiers modifiers) override
Called when a rubber band should be updated to reflect a temporary ending position (in canvas coordin...
Definition
qgsplotrubberband.cpp:149
QgsPlotRectangularRubberBand::QgsPlotRectangularRubberBand
QgsPlotRectangularRubberBand(QgsPlotCanvas *canvas=nullptr)
Constructor for QgsPlotRectangularRubberBand.
Definition
qgsplotrubberband.cpp:122
QgsPlotRectangularRubberBand::~QgsPlotRectangularRubberBand
~QgsPlotRectangularRubberBand() override
Definition
qgsplotrubberband.cpp:126
QgsPlotRectangularRubberBand::finish
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.
Definition
qgsplotrubberband.cpp:166
QgsPlotRubberBand::brush
QBrush brush() const
Returns the brush used for drawing the rubber band.
Definition
qgsplotrubberband.cpp:111
QgsPlotRubberBand::setBrush
void setBrush(const QBrush &brush)
Sets the brush used for drawing the rubber band.
Definition
qgsplotrubberband.cpp:116
QgsPlotRubberBand::QgsPlotRubberBand
QgsPlotRubberBand(QgsPlotCanvas *canvas=nullptr)
Constructor for QgsPlotRubberBand.
Definition
qgsplotrubberband.cpp:29
QgsPlotRubberBand::canvas
QgsPlotCanvas * canvas() const
Returns the canvas associated with the rubber band.
Definition
qgsplotrubberband.cpp:33
QgsPlotRubberBand::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
qgsplotrubberband.cpp:38
QgsPlotRubberBand::start
virtual void start(QPointF position, Qt::KeyboardModifiers modifiers)=0
Called when a rubber band should be created at the specified starting position (in canvas coordinate ...
QgsPlotRubberBand::setPen
void setPen(const QPen &pen)
Sets the pen used for drawing the rubber band.
Definition
qgsplotrubberband.cpp:106
QgsPlotRubberBand::pen
QPen pen() const
Returns the pen used for drawing the rubber band.
Definition
qgsplotrubberband.cpp:101
qgsplotcanvas.h
qgsplotrubberband.h
Generated on
for QGIS API Documentation by
1.17.0