qwt_plot_shapeitem.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_shapeitem.h"
11 #include "qwt_scale_map.h"
12 #include "qwt_painter.h"
14 #include "qwt_clipper.h"
15 
16 static QPainterPath qwtTransformPath( const QwtScaleMap &xMap,
17  const QwtScaleMap &yMap, const QPainterPath &path, bool doAlign )
18 {
19  QPainterPath shape;
20  shape.setFillRule( path.fillRule() );
21 
22  for ( int i = 0; i < path.elementCount(); i++ )
23  {
24  const QPainterPath::Element &element = path.elementAt( i );
25 
26  double x = xMap.transform( element.x );
27  double y = yMap.transform( element.y );
28 
29  switch( element.type )
30  {
31  case QPainterPath::MoveToElement:
32  {
33  if ( doAlign )
34  {
35  x = qRound( x );
36  y = qRound( y );
37  }
38 
39  shape.moveTo( x, y );
40  break;
41  }
42  case QPainterPath::LineToElement:
43  {
44  if ( doAlign )
45  {
46  x = qRound( x );
47  y = qRound( y );
48  }
49 
50  shape.lineTo( x, y );
51  break;
52  }
53  case QPainterPath::CurveToElement:
54  {
55  const QPainterPath::Element& element1 = path.elementAt( ++i );
56  const double x1 = xMap.transform( element1.x );
57  const double y1 = yMap.transform( element1.y );
58 
59  const QPainterPath::Element& element2 = path.elementAt( ++i );
60  const double x2 = xMap.transform( element2.x );
61  const double y2 = yMap.transform( element2.y );
62 
63  shape.cubicTo( x, y, x1, y1, x2, y2 );
64  break;
65  }
66  case QPainterPath::CurveToDataElement:
67  {
68  break;
69  }
70  }
71  }
72 
73  return shape;
74 }
75 
76 
78 {
79 public:
82  renderTolerance( 0.0 )
83  {
84  }
85 
88 
90  QRectF boundingRect;
91 
92  QPen pen;
93  QBrush brush;
94  QPainterPath shape;
95 };
96 
107  QwtPlotItem( QwtText( title ) )
108 {
109  init();
110 }
111 
122  QwtPlotItem( title )
123 {
124  init();
125 }
126 
129 {
130  delete d_data;
131 }
132 
134 {
135  d_data = new PrivateData();
137 
140 
141  setZ( 8.0 );
142 }
143 
146 {
148 }
149 
158 {
159  if ( on )
160  d_data->paintAttributes |= attribute;
161  else
162  d_data->paintAttributes &= ~attribute;
163 }
164 
170 {
171  return ( d_data->paintAttributes & attribute );
172 }
173 
181 {
182  if ( mode != d_data->legendMode )
183  {
184  d_data->legendMode = mode;
185  legendChanged();
186  }
187 }
188 
194 {
195  return d_data->legendMode;
196 }
197 
200 {
201  return d_data->boundingRect;
202 }
203 
210 void QwtPlotShapeItem::setRect( const QRectF &rect )
211 {
212  QPainterPath path;
213  path.addRect( rect );
214 
215  setShape( path );
216 }
217 
224 void QwtPlotShapeItem::setPolygon( const QPolygonF &polygon )
225 {
226  QPainterPath shape;
227  shape.addPolygon( polygon );
228 
229  setShape( shape );
230 }
231 
238 void QwtPlotShapeItem::setShape( const QPainterPath &shape )
239 {
240  if ( shape != d_data->shape )
241  {
242  d_data->shape = shape;
243  if ( shape.isEmpty() )
244  {
246  }
247  else
248  {
249  d_data->boundingRect = shape.boundingRect();
250  }
251 
252  itemChanged();
253  }
254 }
255 
260 QPainterPath QwtPlotShapeItem::shape() const
261 {
262  return d_data->shape;
263 }
264 
278 void QwtPlotShapeItem::setPen( const QColor &color, qreal width, Qt::PenStyle style )
279 {
280  setPen( QPen( color, width, style ) );
281 }
282 
291 void QwtPlotShapeItem::setPen( const QPen &pen )
292 {
293  if ( pen != d_data->pen )
294  {
295  d_data->pen = pen;
296  itemChanged();
297  }
298 }
299 
305 {
306  return d_data->pen;
307 }
308 
317 void QwtPlotShapeItem::setBrush( const QBrush &brush )
318 {
319  if ( brush != d_data->brush )
320  {
321  d_data->brush = brush;
322  itemChanged();
323  }
324 }
325 
331 {
332  return d_data->brush;
333 }
334 
351 void QwtPlotShapeItem::setRenderTolerance( double tolerance )
352 {
353  tolerance = qMax( tolerance, 0.0 );
354 
355  if ( tolerance != d_data->renderTolerance )
356  {
357  d_data->renderTolerance = tolerance;
358  itemChanged();
359  }
360 }
361 
367 {
368  return d_data->renderTolerance;
369 }
370 
379 void QwtPlotShapeItem::draw( QPainter *painter,
380  const QwtScaleMap &xMap, const QwtScaleMap &yMap,
381  const QRectF &canvasRect ) const
382 {
383  if ( d_data->shape.isEmpty() )
384  return;
385 
386  if ( d_data->pen.style() == Qt::NoPen
387  && d_data->brush.style() == Qt::NoBrush )
388  {
389  return;
390  }
391 
392  const QRectF cr = QwtScaleMap::invTransform(
393  xMap, yMap, canvasRect.toRect() );
394 
395  const QRectF &br = d_data->boundingRect;
396 
397  if ( ( br.left() > cr.right() ) || ( br.right() < cr.left() )
398  || ( br.top() > cr.bottom() ) || ( br.bottom() < cr.top() ) )
399  {
400  // outside the visisble area
401  return;
402  }
403 
404  const bool doAlign = QwtPainter::roundingAlignment( painter );
405 
406  QPainterPath path = qwtTransformPath( xMap, yMap,
407  d_data->shape, doAlign );
408 
410  {
411  const qreal pw = qMax( qreal( 1.0 ), painter->pen().widthF());
412  const QRectF clipRect = canvasRect.adjusted( -pw, -pw, pw, pw );
413 
414  QPainterPath clippedPath;
415  clippedPath.setFillRule( path.fillRule() );
416 
417  QList<QPolygonF> polygons = path.toSubpathPolygons();
418  for ( int i = 0; i < polygons.size(); i++ )
419  {
420  QwtClipper::clipPolygonF( clipRect, polygons[i], true );
421  clippedPath.addPolygon( polygons[i] );
422 
423  }
424 
425  path = clippedPath;
426  }
427 
428  if ( d_data->renderTolerance > 0.0 )
429  {
431 
432  QPainterPath fittedPath;
433  fittedPath.setFillRule( path.fillRule() );
434 
435  const QList<QPolygonF> polygons = path.toSubpathPolygons();
436  for ( int i = 0; i < polygons.size(); i++ )
437  fittedPath.addPolygon( fitter.fitCurve( polygons[ i ] ) );
438 
439  path = fittedPath;
440  }
441 
442  painter->setPen( d_data->pen );
443  painter->setBrush( d_data->brush );
444 
445  painter->drawPath( path );
446 }
447 
458  const QSizeF &size ) const
459 {
460  Q_UNUSED( index );
461 
462  QwtGraphic icon;
463  icon.setDefaultSize( size );
464 
465  if ( size.isEmpty() )
466  return icon;
467 
469  {
470  const QRectF &br = d_data->boundingRect;
471 
472  QPainter painter( &icon );
473  painter.setRenderHint( QPainter::Antialiasing,
475 
476  painter.translate( -br.topLeft() );
477 
478  painter.setPen( d_data->pen );
479  painter.setBrush( d_data->brush );
480  painter.drawPath( d_data->shape );
481  }
482  else
483  {
484  QColor iconColor;
485  if ( d_data->brush.style() != Qt::NoBrush )
486  iconColor = d_data->brush.color();
487  else
488  iconColor = d_data->pen.color();
489 
490  icon = defaultIcon( iconColor, size );
491  }
492 
493  return icon;
494 }
495 
virtual void legendChanged()
Display a scaled down version of the shape.
Display a filled rectangle.
void setLegendMode(LegendMode)
QPainterPath shape() const
Enable antialiasing.
bool testPaintAttribute(PaintAttribute) const
virtual QwtGraphic legendIcon(int index, const QSizeF &) const
LegendMode
Mode how to display the item on the legend.
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
QwtGraphic defaultIcon(const QBrush &, const QSizeF &) const
Return a default icon from a brush.
TFSIMD_FORCE_INLINE const tfScalar & y() const
LegendMode legendMode() const
void setDefaultSize(const QSizeF &)
Set a default size.
The item is represented on the legend.
void setPaintAttribute(PaintAttribute, bool on=true)
bool testRenderHint(RenderHint) const
GraphId path[kMaxDeadlockPathLen]
virtual QRectF boundingRect() const
Bounding rectangle of the shape.
A curve fitter implementing Douglas and Peucker algorithm.
For QwtPlotShapeItem.
static void clipPolygonF(const QRectF &, QPolygonF &, bool closePolygon=false)
A class representing a text.
Definition: qwt_text.h:51
void setZ(double z)
Set the z value.
virtual QPolygonF fitCurve(const QPolygonF &) const
A paint device for scalable graphics.
Definition: qwt_graphic.h:74
TFSIMD_FORCE_INLINE const tfScalar & x() const
void setRenderTolerance(double)
Set the tolerance for the weeding optimization.
void setRect(const QRectF &)
Set a path built from a rectangle.
void setPolygon(const QPolygonF &)
Set a path built from a polygon.
A scale map.
Definition: qwt_scale_map.h:30
double invTransform(double p) const
QFlags< PaintAttribute > PaintAttributes
Paint attributes.
virtual void itemChanged()
A plot item, which displays any graphical shape, that can be defined by a QPainterPath.
QwtPlotShapeItem(const QString &title=QString())
Constructor.
QwtPlotShapeItem::PaintAttributes paintAttributes
PrivateData * d_data
virtual int rtti() const
QwtPlotShapeItem::LegendMode legendMode
double renderTolerance() const
static QPainterPath qwtTransformPath(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QPainterPath &path, bool doAlign)
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:64
void setItemAttribute(ItemAttribute, bool on=true)
double transform(double s) const
QBrush brush() const
int i
void setShape(const QPainterPath &)
Set the shape to be displayed.
void setBrush(const QBrush &)
virtual void draw(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &rect) const
static bool roundingAlignment()
Definition: qwt_painter.h:176
virtual ~QwtPlotShapeItem()
Destructor.
const QwtText & title() const
virtual QRectF boundingRect() const


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