qwt_plot_intervalcurve.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_plot_intervalcurve.h"
11 #include "qwt_interval_symbol.h"
12 #include "qwt_scale_map.h"
13 #include "qwt_clipper.h"
14 #include "qwt_painter.h"
15 #include <string.h>
16 
17 #include <qpainter.h>
18 
19 static inline bool qwtIsHSampleInside( const QwtIntervalSample &sample,
20  double xMin, double xMax, double yMin, double yMax )
21 {
22  const double y = sample.value;
23  const double x1 = sample.interval.minValue();
24  const double x2 = sample.interval.maxValue();
25 
26  const bool isOffScreen = ( y < yMin ) || ( y > yMax )
27  || ( x1 < xMin && x2 < xMin ) || ( x1 > xMax && x2 > xMax );
28 
29  return !isOffScreen;
30 }
31 
32 static inline bool qwtIsVSampleInside( const QwtIntervalSample &sample,
33  double xMin, double xMax, double yMin, double yMax )
34 {
35  const double x = sample.value;
36  const double y1 = sample.interval.minValue();
37  const double y2 = sample.interval.maxValue();
38 
39  const bool isOffScreen = ( x < xMin ) || ( x > xMax )
40  || ( y1 < yMin && y2 < yMin ) || ( y1 > yMax && y2 > yMax );
41 
42  return !isOffScreen;
43 }
44 
46 {
47 public:
50  symbol( NULL ),
51  pen( Qt::black ),
52  brush( Qt::white )
53  {
56 
57  pen.setCapStyle( Qt::FlatCap );
58  }
59 
61  {
62  delete symbol;
63  }
64 
67 
68  QPen pen;
69  QBrush brush;
70 
72 };
73 
79  QwtPlotSeriesItem( title )
80 {
81  init();
82 }
83 
89  QwtPlotSeriesItem( QwtText( title ) )
90 {
91  init();
92 }
93 
96 {
97  delete d_data;
98 }
99 
102 {
105 
106  d_data = new PrivateData;
108 
109  setZ( 19.0 );
110 }
111 
114 {
116 }
117 
126  PaintAttribute attribute, bool on )
127 {
128  if ( on )
129  d_data->paintAttributes |= attribute;
130  else
131  d_data->paintAttributes &= ~attribute;
132 }
133 
139  PaintAttribute attribute ) const
140 {
141  return ( d_data->paintAttributes & attribute );
142 }
143 
149  const QVector<QwtIntervalSample> &samples )
150 {
151  setData( new QwtIntervalSeriesData( samples ) );
152 }
153 
166 {
167  setData( data );
168 }
169 
177 {
178  if ( style != d_data->style )
179  {
180  d_data->style = style;
181 
182  legendChanged();
183  itemChanged();
184  }
185 }
186 
192 {
193  return d_data->style;
194 }
195 
203 {
204  if ( symbol != d_data->symbol )
205  {
206  delete d_data->symbol;
207  d_data->symbol = symbol;
208 
209  legendChanged();
210  itemChanged();
211  }
212 }
213 
219 {
220  return d_data->symbol;
221 }
222 
236 void QwtPlotIntervalCurve::setPen( const QColor &color, qreal width, Qt::PenStyle style )
237 {
238  setPen( QPen( color, width, style ) );
239 }
240 
247 {
248  if ( pen != d_data->pen )
249  {
250  d_data->pen = pen;
251 
252  legendChanged();
253  itemChanged();
254  }
255 }
256 
261 const QPen& QwtPlotIntervalCurve::pen() const
262 {
263  return d_data->pen;
264 }
265 
275 {
276  if ( brush != d_data->brush )
277  {
278  d_data->brush = brush;
279 
280  legendChanged();
281  itemChanged();
282  }
283 }
284 
289 const QBrush& QwtPlotIntervalCurve::brush() const
290 {
291  return d_data->brush;
292 }
293 
299 {
300  QRectF rect = QwtPlotSeriesItem::boundingRect();
301  if ( rect.isValid() && orientation() == Qt::Vertical )
302  rect.setRect( rect.y(), rect.x(), rect.height(), rect.width() );
303 
304  return rect;
305 }
306 
320 void QwtPlotIntervalCurve::drawSeries( QPainter *painter,
321  const QwtScaleMap &xMap, const QwtScaleMap &yMap,
322  const QRectF &canvasRect, int from, int to ) const
323 {
324  if ( to < 0 )
325  to = dataSize() - 1;
326 
327  if ( from < 0 )
328  from = 0;
329 
330  if ( from > to )
331  return;
332 
333  switch ( d_data->style )
334  {
335  case Tube:
336  drawTube( painter, xMap, yMap, canvasRect, from, to );
337  break;
338 
339  case NoCurve:
340  default:
341  break;
342  }
343 
344  if ( d_data->symbol &&
346  {
347  drawSymbols( painter, *d_data->symbol,
348  xMap, yMap, canvasRect, from, to );
349  }
350 }
351 
369 void QwtPlotIntervalCurve::drawTube( QPainter *painter,
370  const QwtScaleMap &xMap, const QwtScaleMap &yMap,
371  const QRectF &canvasRect, int from, int to ) const
372 {
373  const bool doAlign = QwtPainter::roundingAlignment( painter );
374 
375  painter->save();
376 
377  const size_t size = to - from + 1;
378  QPolygonF polygon( 2 * size );
379  QPointF *points = polygon.data();
380 
381  for ( uint i = 0; i < size; i++ )
382  {
383  QPointF &minValue = points[i];
384  QPointF &maxValue = points[2 * size - 1 - i];
385 
386  const QwtIntervalSample intervalSample = sample( from + i );
387  if ( orientation() == Qt::Vertical )
388  {
389  double x = xMap.transform( intervalSample.value );
390  double y1 = yMap.transform( intervalSample.interval.minValue() );
391  double y2 = yMap.transform( intervalSample.interval.maxValue() );
392  if ( doAlign )
393  {
394  x = qRound( x );
395  y1 = qRound( y1 );
396  y2 = qRound( y2 );
397  }
398 
399  minValue.rx() = x;
400  minValue.ry() = y1;
401  maxValue.rx() = x;
402  maxValue.ry() = y2;
403  }
404  else
405  {
406  double y = yMap.transform( intervalSample.value );
407  double x1 = xMap.transform( intervalSample.interval.minValue() );
408  double x2 = xMap.transform( intervalSample.interval.maxValue() );
409  if ( doAlign )
410  {
411  y = qRound( y );
412  x1 = qRound( x1 );
413  x2 = qRound( x2 );
414  }
415 
416  minValue.rx() = x1;
417  minValue.ry() = y;
418  maxValue.rx() = x2;
419  maxValue.ry() = y;
420  }
421  }
422 
423  if ( d_data->brush.style() != Qt::NoBrush )
424  {
425  painter->setPen( QPen( Qt::NoPen ) );
426  painter->setBrush( d_data->brush );
427 
429  {
430  const qreal m = 1.0;
431  const QPolygonF p = QwtClipper::clippedPolygonF(
432  canvasRect.adjusted( -m, -m, m, m ), polygon, true );
433 
434  QwtPainter::drawPolygon( painter, p );
435  }
436  else
437  {
438  QwtPainter::drawPolygon( painter, polygon );
439  }
440  }
441 
442  if ( d_data->pen.style() != Qt::NoPen )
443  {
444  painter->setPen( d_data->pen );
445  painter->setBrush( Qt::NoBrush );
446 
448  {
449  qreal pw = qMax( qreal( 1.0 ), painter->pen().widthF() );
450  const QRectF clipRect = canvasRect.adjusted( -pw, -pw, pw, pw );
451 
452  QPolygonF p( size );
453 
454  ::memcpy( p.data(), points, size * sizeof( QPointF ) );
455  QwtPainter::drawPolyline( painter,
456  QwtClipper::clippedPolygonF( clipRect, p ) );
457 
458  ::memcpy( p.data(), points + size, size * sizeof( QPointF ) );
459  QwtPainter::drawPolyline( painter,
460  QwtClipper::clippedPolygonF( clipRect, p ) );
461  }
462  else
463  {
464  QwtPainter::drawPolyline( painter, points, size );
465  QwtPainter::drawPolyline( painter, points + size, size );
466  }
467  }
468 
469  painter->restore();
470 }
471 
486  QPainter *painter, const QwtIntervalSymbol &symbol,
487  const QwtScaleMap &xMap, const QwtScaleMap &yMap,
488  const QRectF &canvasRect, int from, int to ) const
489 {
490  painter->save();
491 
492  QPen pen = symbol.pen();
493  pen.setCapStyle( Qt::FlatCap );
494 
495  painter->setPen( pen );
496  painter->setBrush( symbol.brush() );
497 
498  const QRectF tr = QwtScaleMap::invTransform( xMap, yMap, canvasRect );
499 
500  const double xMin = tr.left();
501  const double xMax = tr.right();
502  const double yMin = tr.top();
503  const double yMax = tr.bottom();
504 
505  const bool doClip = d_data->paintAttributes & ClipSymbol;
506 
507  for ( int i = from; i <= to; i++ )
508  {
509  const QwtIntervalSample s = sample( i );
510 
511  if ( orientation() == Qt::Vertical )
512  {
513  if ( !doClip || qwtIsVSampleInside( s, xMin, xMax, yMin, yMax ) )
514  {
515  const double x = xMap.transform( s.value );
516  const double y1 = yMap.transform( s.interval.minValue() );
517  const double y2 = yMap.transform( s.interval.maxValue() );
518 
519  symbol.draw( painter, orientation(),
520  QPointF( x, y1 ), QPointF( x, y2 ) );
521  }
522  }
523  else
524  {
525  if ( !doClip || qwtIsHSampleInside( s, xMin, xMax, yMin, yMax ) )
526  {
527  const double y = yMap.transform( s.value );
528  const double x1 = xMap.transform( s.interval.minValue() );
529  const double x2 = xMap.transform( s.interval.maxValue() );
530 
531  symbol.draw( painter, orientation(),
532  QPointF( x1, y ), QPointF( x2, y ) );
533  }
534  }
535  }
536 
537  painter->restore();
538 }
539 
553  int index, const QSizeF &size ) const
554 {
555  Q_UNUSED( index );
556 
557  if ( size.isEmpty() )
558  return QwtGraphic();
559 
560  QwtGraphic icon;
561  icon.setDefaultSize( size );
563 
564  QPainter painter( &icon );
565  painter.setRenderHint( QPainter::Antialiasing,
567 
568  if ( d_data->style == Tube )
569  {
570  QRectF r( 0, 0, size.width(), size.height() );
571  painter.fillRect( r, d_data->brush );
572  }
573 
574  if ( d_data->symbol &&
576  {
577  QPen pen = d_data->symbol->pen();
578  pen.setWidthF( pen.widthF() );
579  pen.setCapStyle( Qt::FlatCap );
580 
581  painter.setPen( pen );
582  painter.setBrush( d_data->symbol->brush() );
583 
584  if ( orientation() == Qt::Vertical )
585  {
586  const double x = 0.5 * size.width();
587 
588  d_data->symbol->draw( &painter, orientation(),
589  QPointF( x, 0 ), QPointF( x, size.height() - 1.0 ) );
590  }
591  else
592  {
593  const double y = 0.5 * size.height();
594 
595  d_data->symbol->draw( &painter, orientation(),
596  QPointF( 0.0, y ), QPointF( size.width() - 1.0, y ) );
597  }
598  }
599 
600  return icon;
601 }
virtual void legendChanged()
virtual size_t dataSize() const
Qt::Orientation orientation() const
Enable antialiasing.
virtual void drawSymbols(QPainter *, const QwtIntervalSymbol &, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const
void setSamples(const QVector< QwtIntervalSample > &)
double minValue() const
Definition: qwt_interval.h:193
virtual ~QwtPlotIntervalCurve()
Destructor.
QwtPlotIntervalCurve(const QString &title=QString())
XmlRpcServer s
QwtPlotIntervalCurve::PaintAttributes paintAttributes
const QPen & pen() const
static bool qwtIsHSampleInside(const QwtIntervalSample &sample, double xMin, double xMax, double yMin, double yMax)
void setData(QwtSeriesData< QwtIntervalSample > *series)
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
QwtSeriesData< QwtIntervalSample > * data()
TFSIMD_FORCE_INLINE const tfScalar & y() const
static QPolygonF clippedPolygonF(const QRectF &, const QPolygonF &, bool closePolygon=false)
double maxValue() const
Definition: qwt_interval.h:199
void setRenderHint(RenderHint, bool on=true)
void setDefaultSize(const QSizeF &)
Set a default size.
void setPaintAttribute(PaintAttribute, bool on=true)
The item is represented on the legend.
static bool qwtIsVSampleInside(const QwtIntervalSample &sample, double xMin, double xMax, double yMin, double yMax)
void setStyle(CurveStyle style)
const QBrush & brush() const
static void drawPolygon(QPainter *, const QPolygonF &)
Wrapper for QPainter::drawPolygon()
bool testRenderHint(RenderHint) const
A sample of the types (x1-x2, y) or (x, y1-y2)
Definition: qwt_samples.h:19
const QBrush & brush() const
A class representing a text.
Definition: qwt_text.h:51
A drawing primitive for displaying an interval like an error bar.
virtual void drawTube(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const
void setZ(double z)
Set the z value.
virtual void drawSeries(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const
bool testPaintAttribute(PaintAttribute) const
A paint device for scalable graphics.
Definition: qwt_graphic.h:74
TFSIMD_FORCE_INLINE const tfScalar & x() const
virtual QRectF boundingRect() const
For QwtPlotIntervalCurve.
Definition: qwt_plot_item.h:97
A scale map.
Definition: qwt_scale_map.h:30
double invTransform(double p) const
QwtIntervalSample sample(int index) const
virtual void itemChanged()
uintptr_t size
Interface for iterating over an array of intervals.
static void drawPolyline(QPainter *, const QPolygonF &)
Wrapper for QPainter::drawPolyline()
const QPen & pen() const
Check if a symbol is on the plot canvas before painting it.
void setBrush(const QBrush &)
double value
Value.
Definition: qwt_samples.h:30
QwtPlotIntervalCurve::CurveStyle style
void init()
Initialize internal members.
Base class for plot items representing a series of samples.
void setItemAttribute(ItemAttribute, bool on=true)
double transform(double s) const
void setSymbol(const QwtIntervalSymbol *)
virtual QwtGraphic legendIcon(int index, const QSizeF &) const
int i
virtual QRectF boundingRect() const
virtual void draw(QPainter *, Qt::Orientation, const QPointF &from, const QPointF &to) const
QFlags< PaintAttribute > PaintAttributes
Paint attributes.
const QwtIntervalSymbol * symbol() const
No Style. The symbol cannot be drawn.
QwtPlotIntervalCurve represents a series of samples, where each value is associated with an interval ...
static bool roundingAlignment()
Definition: qwt_painter.h:176
const QwtText & title() const
QwtInterval interval
Interval.
Definition: qwt_samples.h:33
CurveStyle
Curve styles. The default setting is QwtPlotIntervalCurve::Tube.


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17