Qwt User's Guide: qwt_interval_symbol.cpp Source File

Qwt User's Guide 6.2.0

qwt_interval_symbol.cpp
1 /******************************************************************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10 #include "qwt_interval_symbol.h"
11 #include "qwt_painter.h"
12 #include "qwt_math.h"
13
14 #include <qpainter.h>
15 #include <qmath.h>
16
17 class QwtIntervalSymbol::PrivateData
18 {
19 public:
20 PrivateData()
21 : style( QwtIntervalSymbol::NoSymbol )
22 , width( 6 )
23 {
24 }
25
26 bool operator==( const PrivateData& other ) const
27 {
28 return ( style == other.style )
29 && ( width == other.width )
30 && ( brush == other.brush )
31 && ( pen == other.pen );
32 }
33
34 QwtIntervalSymbol::Style style;
35 int width;
36
37 QPen pen;
38 QBrush brush;
39 };
40
47 QwtIntervalSymbol::QwtIntervalSymbol( Style style )
48 {
49 m_data = new PrivateData();
50 m_data->style = style;
51 }
52
54 QwtIntervalSymbol::QwtIntervalSymbol( const QwtIntervalSymbol& other )
55 {
56 m_data = new PrivateData();
57 *m_data = *other.m_data;
58 }
59
61 QwtIntervalSymbol::~QwtIntervalSymbol()
62 {
63 delete m_data;
64 }
65
67 QwtIntervalSymbol& QwtIntervalSymbol::operator=(
68 const QwtIntervalSymbol& other )
69 {
70 *m_data = *other.m_data;
71 return *this;
72 }
73
75 bool QwtIntervalSymbol::operator==(
76 const QwtIntervalSymbol& other ) const
77 {
78 return *m_data == *other.m_data;
79 }
80
82 bool QwtIntervalSymbol::operator!=(
83 const QwtIntervalSymbol& other ) const
84 {
85 return !( *m_data == *other.m_data );
86 }
87
94 void QwtIntervalSymbol::setStyle( Style style )
95 {
96 m_data->style = style;
97 }
98
103 QwtIntervalSymbol::Style QwtIntervalSymbol::style() const
104 {
105 return m_data->style;
106 }
107
115 void QwtIntervalSymbol::setWidth( int width )
116 {
117 m_data->width = width;
118 }
119
124 int QwtIntervalSymbol::width() const
125 {
126 return m_data->width;
127 }
128
137 void QwtIntervalSymbol::setBrush( const QBrush& brush )
138 {
139 m_data->brush = brush;
140 }
141
146 const QBrush& QwtIntervalSymbol::brush() const
147 {
148 return m_data->brush;
149 }
150
164 void QwtIntervalSymbol::setPen( const QColor& color,
165 qreal width, Qt::PenStyle style )
166 {
167 setPen( QPen( color, width, style ) );
168 }
169
176 void QwtIntervalSymbol::setPen( const QPen& pen )
177 {
178 m_data->pen = pen;
179 }
180
185 const QPen& QwtIntervalSymbol::pen() const
186 {
187 return m_data->pen;
188 }
189
200 void QwtIntervalSymbol::draw( QPainter* painter, Qt::Orientation orientation,
201 const QPointF& from, const QPointF& to ) const
202 {
203 const qreal pw = QwtPainter::effectivePenWidth( painter->pen() );
204
205 QPointF p1 = from;
206 QPointF p2 = to;
207 if ( QwtPainter::roundingAlignment( painter ) )
208 {
209 p1 = p1.toPoint();
210 p2 = p2.toPoint();
211 }
212
213 switch ( m_data->style )
214 {
215 case QwtIntervalSymbol::Bar:
216 {
217 QwtPainter::drawLine( painter, p1, p2 );
218 if ( m_data->width > pw )
219 {
220 if ( ( orientation == Qt::Horizontal )
221 && ( p1.y() == p2.y() ) )
222 {
223 const double sw = m_data->width;
224
225 const double y = p1.y() - sw / 2;
226 QwtPainter::drawLine( painter,
227 p1.x(), y, p1.x(), y + sw );
228 QwtPainter::drawLine( painter,
229 p2.x(), y, p2.x(), y + sw );
230 }
231 else if ( ( orientation == Qt::Vertical )
232 && ( p1.x() == p2.x() ) )
233 {
234 const double sw = m_data->width;
235
236 const double x = p1.x() - sw / 2;
237 QwtPainter::drawLine( painter,
238 x, p1.y(), x + sw, p1.y() );
239 QwtPainter::drawLine( painter,
240 x, p2.y(), x + sw, p2.y() );
241 }
242 else
243 {
244 const double sw = m_data->width;
245
246 const double dx = p2.x() - p1.x();
247 const double dy = p2.y() - p1.y();
248 const double angle = std::atan2( dy, dx ) + M_PI_2;
249 double dw2 = sw / 2.0;
250
251 const double cx = qFastCos( angle ) * dw2;
252 const double sy = qFastSin( angle ) * dw2;
253
254 QwtPainter::drawLine( painter,
255 p1.x() - cx, p1.y() - sy,
256 p1.x() + cx, p1.y() + sy );
257 QwtPainter::drawLine( painter,
258 p2.x() - cx, p2.y() - sy,
259 p2.x() + cx, p2.y() + sy );
260 }
261 }
262 break;
263 }
264 case QwtIntervalSymbol::Box:
265 {
266 if ( m_data->width <= pw )
267 {
268 QwtPainter::drawLine( painter, p1, p2 );
269 }
270 else
271 {
272 if ( ( orientation == Qt::Horizontal )
273 && ( p1.y() == p2.y() ) )
274 {
275 const double sw = m_data->width;
276
277 const double y = p1.y() - m_data->width / 2;
278 QwtPainter::drawRect( painter,
279 p1.x(), y, p2.x() - p1.x(), sw );
280 }
281 else if ( ( orientation == Qt::Vertical )
282 && ( p1.x() == p2.x() ) )
283 {
284 const double sw = m_data->width;
285
286 const double x = p1.x() - m_data->width / 2;
287 QwtPainter::drawRect( painter,
288 x, p1.y(), sw, p2.y() - p1.y() );
289 }
290 else
291 {
292 const double sw = m_data->width;
293
294 const double dx = p2.x() - p1.x();
295 const double dy = p2.y() - p1.y();
296 const double angle = std::atan2( dy, dx ) + M_PI_2;
297 double dw2 = sw / 2.0;
298
299 const double cx = qFastCos( angle ) * dw2;
300 const double sy = qFastSin( angle ) * dw2;
301
302 QPolygonF polygon;
303 polygon += QPointF( p1.x() - cx, p1.y() - sy );
304 polygon += QPointF( p1.x() + cx, p1.y() + sy );
305 polygon += QPointF( p2.x() + cx, p2.y() + sy );
306 polygon += QPointF( p2.x() - cx, p2.y() - sy );
307
308 QwtPainter::drawPolygon( painter, polygon );
309 }
310 }
311 break;
312 }
313 default:;
314 }
315 }
QwtIntervalSymbol
A drawing primitive for displaying an interval like an error bar.
Definition: qwt_interval_symbol.h:28
QwtIntervalSymbol::width
int width() const
Definition: qwt_interval_symbol.cpp:124
QwtIntervalSymbol::setStyle
void setStyle(Style)
Definition: qwt_interval_symbol.cpp:94
QwtIntervalSymbol::draw
virtual void draw(QPainter *, Qt::Orientation, const QPointF &from, const QPointF &to) const
Definition: qwt_interval_symbol.cpp:200
QwtIntervalSymbol::setBrush
void setBrush(const QBrush &)
Assign a brush.
Definition: qwt_interval_symbol.cpp:137
QwtIntervalSymbol::pen
const QPen & pen() const
Definition: qwt_interval_symbol.cpp:185
QwtIntervalSymbol::QwtIntervalSymbol
QwtIntervalSymbol(Style=NoSymbol)
Definition: qwt_interval_symbol.cpp:47
QwtIntervalSymbol::operator=
QwtIntervalSymbol & operator=(const QwtIntervalSymbol &)
Assignment operator.
Definition: qwt_interval_symbol.cpp:67
QwtIntervalSymbol::operator==
bool operator==(const QwtIntervalSymbol &) const
Compare two symbols.
Definition: qwt_interval_symbol.cpp:75
QwtIntervalSymbol::Style
Style
Symbol style.
Definition: qwt_interval_symbol.h:32
QwtIntervalSymbol::Bar
@ Bar
Definition: qwt_interval_symbol.h:40
QwtIntervalSymbol::NoSymbol
@ NoSymbol
No Style. The symbol cannot be drawn.
Definition: qwt_interval_symbol.h:34
QwtIntervalSymbol::Box
@ Box
Definition: qwt_interval_symbol.h:47
QwtIntervalSymbol::style
Style style() const
Definition: qwt_interval_symbol.cpp:103
QwtIntervalSymbol::setPen
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
Definition: qwt_interval_symbol.cpp:164
QwtIntervalSymbol::operator!=
bool operator!=(const QwtIntervalSymbol &) const
Compare two symbols.
Definition: qwt_interval_symbol.cpp:82
QwtIntervalSymbol::brush
const QBrush & brush() const
Definition: qwt_interval_symbol.cpp:146
QwtIntervalSymbol::~QwtIntervalSymbol
virtual ~QwtIntervalSymbol()
Destructor.
Definition: qwt_interval_symbol.cpp:61
QwtIntervalSymbol::setWidth
void setWidth(int)
Definition: qwt_interval_symbol.cpp:115
QwtPainter::drawPolygon
static void drawPolygon(QPainter *, const QPolygonF &)
Wrapper for QPainter::drawPolygon()
Definition: qwt_painter.cpp:561
QwtPainter::effectivePenWidth
static qreal effectivePenWidth(const QPen &)
Definition: qwt_painter.h:201
QwtPainter::drawRect
static void drawRect(QPainter *, qreal x, qreal y, qreal w, qreal h)
Wrapper for QPainter::drawRect()
Definition: qwt_painter.cpp:347
QwtPainter::roundingAlignment
static bool roundingAlignment()
Definition: qwt_painter.h:183
QwtPainter::drawLine
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:154