Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
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
17class 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
35 int width;
36
37 QPen pen;
38 QBrush brush;
39};
40
48{
49 m_data = new PrivateData();
50 m_data->style = style;
51}
52
55{
56 m_data = new PrivateData();
57 *m_data = *other.m_data;
58}
59
62{
63 delete m_data;
64}
65
68 const QwtIntervalSymbol& other )
69{
70 *m_data = *other.m_data;
71 return *this;
72}
73
76 const QwtIntervalSymbol& other ) const
77{
78 return *m_data == *other.m_data;
79}
80
83 const QwtIntervalSymbol& other ) const
84{
85 return !( *m_data == *other.m_data );
86}
87
95{
96 m_data->style = style;
97}
98
104{
105 return m_data->style;
106}
107
116{
117 m_data->width = width;
118}
119
125{
126 return m_data->width;
127}
128
137void QwtIntervalSymbol::setBrush( const QBrush& brush )
138{
139 m_data->brush = brush;
140}
141
146const QBrush& QwtIntervalSymbol::brush() const
147{
148 return m_data->brush;
149}
150
164void QwtIntervalSymbol::setPen( const QColor& color,
165 qreal width, Qt::PenStyle style )
166{
167 setPen( QPen( color, width, style ) );
168}
169
176void QwtIntervalSymbol::setPen( const QPen& pen )
177{
178 m_data->pen = pen;
179}
180
185const QPen& QwtIntervalSymbol::pen() const
186{
187 return m_data->pen;
188}
189
200void 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 {
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 }
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}
A drawing primitive for displaying an interval like an error bar.
virtual void draw(QPainter *, Qt::Orientation, const QPointF &from, const QPointF &to) const
void setBrush(const QBrush &)
Assign a brush.
const QPen & pen() const
QwtIntervalSymbol(Style=NoSymbol)
QwtIntervalSymbol & operator=(const QwtIntervalSymbol &)
Assignment operator.
bool operator==(const QwtIntervalSymbol &) const
Compare two symbols.
@ NoSymbol
No Style. The symbol cannot be drawn.
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
bool operator!=(const QwtIntervalSymbol &) const
Compare two symbols.
const QBrush & brush() const
virtual ~QwtIntervalSymbol()
Destructor.
static void drawPolygon(QPainter *, const QPolygonF &)
Wrapper for QPainter::drawPolygon()
static qreal effectivePenWidth(const QPen &)
static void drawRect(QPainter *, qreal x, qreal y, qreal w, qreal h)
Wrapper for QPainter::drawRect()
static bool roundingAlignment()
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()