qwt_interval_symbol.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
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 
18 {
19 public:
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  d_data = new PrivateData();
50  d_data->style = style;
51 }
52 
55 {
56  d_data = new PrivateData();
57  *d_data = *other.d_data;
58 }
59 
62 {
63  delete d_data;
64 }
65 
68  const QwtIntervalSymbol &other )
69 {
70  *d_data = *other.d_data;
71  return *this;
72 }
73 
76  const QwtIntervalSymbol &other ) const
77 {
78  return *d_data == *other.d_data;
79 }
80 
83  const QwtIntervalSymbol &other ) const
84 {
85  return !( *d_data == *other.d_data );
86 }
87 
95 {
96  d_data->style = style;
97 }
98 
104 {
105  return d_data->style;
106 }
107 
116 {
117  d_data->width = width;
118 }
119 
125 {
126  return d_data->width;
127 }
128 
137 void QwtIntervalSymbol::setBrush( const QBrush &brush )
138 {
139  d_data->brush = brush;
140 }
141 
146 const QBrush& QwtIntervalSymbol::brush() const
147 {
148  return d_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  d_data->pen = pen;
179 }
180 
185 const QPen& QwtIntervalSymbol::pen() const
186 {
187  return d_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 ( d_data->style )
214  {
216  {
217  QwtPainter::drawLine( painter, p1, p2 );
218  if ( d_data->width > pw )
219  {
220  if ( ( orientation == Qt::Horizontal )
221  && ( p1.y() == p2.y() ) )
222  {
223  const double sw = d_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 = d_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 = d_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 ( d_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 = d_data->width;
276 
277  const double y = p1.y() - d_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 = d_data->width;
285 
286  const double x = p1.x() - d_data->width / 2;
287  QwtPainter::drawRect( painter,
288  x, p1.y(), sw, p2.y() - p1.y() );
289  }
290  else
291  {
292  const double sw = d_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 }
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:152
#define M_PI_2
Definition: qwt_math.h:60
virtual ~QwtIntervalSymbol()
Destructor.
QwtIntervalSymbol & operator=(const QwtIntervalSymbol &)
Assignment operator.
static qreal effectivePenWidth(const QPen &)
Definition: qwt_painter.h:199
QwtIntervalSymbol::Style style
bool operator!=(const QwtIntervalSymbol &) const
Compare two symbols.
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
QwtIntervalSymbol(Style=NoSymbol)
const QBrush & brush() const
static void drawPolygon(QPainter *, const QPolygonF &)
Wrapper for QPainter::drawPolygon()
A drawing primitive for displaying an interval like an error bar.
static void drawRect(QPainter *, qreal x, qreal y, qreal w, qreal h)
Wrapper for QPainter::drawRect()
const QPen & pen() const
virtual void draw(QPainter *, Qt::Orientation, const QPointF &from, const QPointF &to) const
bool operator==(const PrivateData &other) const
No Style. The symbol cannot be drawn.
bool operator==(const QwtIntervalSymbol &) const
Compare two symbols.
void setBrush(const QBrush &)
Assign a brush.
static bool roundingAlignment()
Definition: qwt_painter.h:181


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10