qwt_plot_shapeitem.cpp
Go to the documentation of this file.
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_plot_shapeitem.h"
11 #include "qwt_scale_map.h"
12 #include "qwt_text.h"
13 #include "qwt_graphic.h"
14 #include "qwt_painter.h"
16 #include "qwt_clipper.h"
17 #include "qwt_math.h"
18 
19 #include <qpainter.h>
20 #include <qpainterpath.h>
21 
22 static QPainterPath qwtTransformPath( const QwtScaleMap& xMap,
23  const QwtScaleMap& yMap, const QPainterPath& path, bool doAlign )
24 {
25  QPainterPath shape;
26  shape.setFillRule( path.fillRule() );
27 
28  for ( int i = 0; i < path.elementCount(); i++ )
29  {
30  const QPainterPath::Element element = path.elementAt( i );
31 
32  double x = xMap.transform( element.x );
33  double y = yMap.transform( element.y );
34 
35  switch( element.type )
36  {
37  case QPainterPath::MoveToElement:
38  {
39  if ( doAlign )
40  {
41  x = qRound( x );
42  y = qRound( y );
43  }
44 
45  shape.moveTo( x, y );
46  break;
47  }
48  case QPainterPath::LineToElement:
49  {
50  if ( doAlign )
51  {
52  x = qRound( x );
53  y = qRound( y );
54  }
55 
56  shape.lineTo( x, y );
57  break;
58  }
59  case QPainterPath::CurveToElement:
60  {
61  const QPainterPath::Element element1 = path.elementAt( ++i );
62  const double x1 = xMap.transform( element1.x );
63  const double y1 = yMap.transform( element1.y );
64 
65  const QPainterPath::Element element2 = path.elementAt( ++i );
66  const double x2 = xMap.transform( element2.x );
67  const double y2 = yMap.transform( element2.y );
68 
69  shape.cubicTo( x, y, x1, y1, x2, y2 );
70  break;
71  }
72  case QPainterPath::CurveToDataElement:
73  {
74  break;
75  }
76  }
77  }
78 
79  return shape;
80 }
81 
82 
84 {
85  public:
88  , renderTolerance( 0.0 )
89  {
90  }
91 
92  QwtPlotShapeItem::PaintAttributes paintAttributes;
94 
96  QRectF boundingRect;
97 
98  QPen pen;
99  QBrush brush;
100  QPainterPath shape;
101 };
102 
112 QwtPlotShapeItem::QwtPlotShapeItem( const QString& title )
113  : QwtPlotItem( QwtText( title ) )
114 {
115  init();
116 }
117 
128  : QwtPlotItem( title )
129 {
130  init();
131 }
132 
135 {
136  delete m_data;
137 }
138 
140 {
141  m_data = new PrivateData();
143 
146 
147  setZ( 8.0 );
148 }
149 
152 {
154 }
155 
164 {
165  if ( on )
166  m_data->paintAttributes |= attribute;
167  else
168  m_data->paintAttributes &= ~attribute;
169 }
170 
176 {
177  return ( m_data->paintAttributes & attribute );
178 }
179 
187 {
188  if ( mode != m_data->legendMode )
189  {
190  m_data->legendMode = mode;
191  legendChanged();
192  }
193 }
194 
200 {
201  return m_data->legendMode;
202 }
203 
206 {
207  return m_data->boundingRect;
208 }
209 
216 void QwtPlotShapeItem::setRect( const QRectF& rect )
217 {
218  QPainterPath path;
219  path.addRect( rect );
220 
221  setShape( path );
222 }
223 
230 void QwtPlotShapeItem::setPolygon( const QPolygonF& polygon )
231 {
232  QPainterPath shape;
233  shape.addPolygon( polygon );
234 
235  setShape( shape );
236 }
237 
244 void QwtPlotShapeItem::setShape( const QPainterPath& shape )
245 {
246  if ( shape != m_data->shape )
247  {
248  m_data->shape = shape;
249  if ( shape.isEmpty() )
250  {
252  }
253  else
254  {
255  m_data->boundingRect = shape.boundingRect();
256  }
257 
258  itemChanged();
259  }
260 }
261 
266 QPainterPath QwtPlotShapeItem::shape() const
267 {
268  return m_data->shape;
269 }
270 
284 void QwtPlotShapeItem::setPen( const QColor& color, qreal width, Qt::PenStyle style )
285 {
286  setPen( QPen( color, width, style ) );
287 }
288 
297 void QwtPlotShapeItem::setPen( const QPen& pen )
298 {
299  if ( pen != m_data->pen )
300  {
301  m_data->pen = pen;
302  itemChanged();
303  }
304 }
305 
311 {
312  return m_data->pen;
313 }
314 
323 void QwtPlotShapeItem::setBrush( const QBrush& brush )
324 {
325  if ( brush != m_data->brush )
326  {
327  m_data->brush = brush;
328  itemChanged();
329  }
330 }
331 
337 {
338  return m_data->brush;
339 }
340 
357 void QwtPlotShapeItem::setRenderTolerance( double tolerance )
358 {
359  tolerance = qwtMaxF( tolerance, 0.0 );
360 
361  if ( tolerance != m_data->renderTolerance )
362  {
363  m_data->renderTolerance = tolerance;
364  itemChanged();
365  }
366 }
367 
373 {
374  return m_data->renderTolerance;
375 }
376 
385 void QwtPlotShapeItem::draw( QPainter* painter,
386  const QwtScaleMap& xMap, const QwtScaleMap& yMap,
387  const QRectF& canvasRect ) const
388 {
389  if ( m_data->shape.isEmpty() )
390  return;
391 
392  if ( m_data->pen.style() == Qt::NoPen
393  && m_data->brush.style() == Qt::NoBrush )
394  {
395  return;
396  }
397 
398  const QRectF cr = QwtScaleMap::invTransform(
399  xMap, yMap, canvasRect.toRect() );
400 
401  const QRectF& br = m_data->boundingRect;
402 
403  if ( ( br.left() > cr.right() ) || ( br.right() < cr.left() )
404  || ( br.top() > cr.bottom() ) || ( br.bottom() < cr.top() ) )
405  {
406  // outside the visible area
407  return;
408  }
409 
410  const bool doAlign = QwtPainter::roundingAlignment( painter );
411 
412  QPainterPath path = qwtTransformPath( xMap, yMap,
413  m_data->shape, doAlign );
414 
416  {
417  const qreal pw = QwtPainter::effectivePenWidth( painter->pen() );
418  const QRectF clipRect = canvasRect.adjusted( -pw, -pw, pw, pw );
419 
420  QPainterPath clippedPath;
421  clippedPath.setFillRule( path.fillRule() );
422 
423  QList< QPolygonF > polygons = path.toSubpathPolygons();
424  for ( int i = 0; i < polygons.size(); i++ )
425  {
426  QwtClipper::clipPolygonF( clipRect, polygons[i], true );
427  clippedPath.addPolygon( polygons[i] );
428 
429  }
430 
431  path = clippedPath;
432  }
433 
434  if ( m_data->renderTolerance > 0.0 )
435  {
437 
438  QPainterPath fittedPath;
439  fittedPath.setFillRule( path.fillRule() );
440 
441  const QList< QPolygonF > polygons = path.toSubpathPolygons();
442  for ( int i = 0; i < polygons.size(); i++ )
443  fittedPath.addPolygon( fitter.fitCurve( polygons[ i ] ) );
444 
445  path = fittedPath;
446  }
447 
448  painter->setPen( m_data->pen );
449  painter->setBrush( m_data->brush );
450 
451  painter->drawPath( path );
452 }
453 
464  const QSizeF& size ) const
465 {
466  Q_UNUSED( index );
467 
468  QwtGraphic icon;
469  icon.setDefaultSize( size );
470 
471  if ( size.isEmpty() )
472  return icon;
473 
475  {
476  const QRectF& br = m_data->boundingRect;
477 
478  QPainter painter( &icon );
479  painter.setRenderHint( QPainter::Antialiasing,
481 
482  painter.translate( -br.topLeft() );
483 
484  painter.setPen( m_data->pen );
485  painter.setBrush( m_data->brush );
486  painter.drawPath( m_data->shape );
487  }
488  else
489  {
490  QColor iconColor;
491  if ( m_data->brush.style() != Qt::NoBrush )
492  iconColor = m_data->brush.color();
493  else
494  iconColor = m_data->pen.color();
495 
496  icon = defaultIcon( iconColor, size );
497  }
498 
499  return icon;
500 }
501 
color
color
Definition: color.h:16
qwt_graphic.h
QwtPlotShapeItem
A plot item, which displays any graphical shape, that can be defined by a QPainterPath.
Definition: qwt_plot_shapeitem.h:38
QwtScaleMap::invTransform
double invTransform(double p) const
Definition: qwt_scale_map.h:154
QwtPlotShapeItem::setPolygon
void setPolygon(const QPolygonF &)
Set a path built from a polygon.
Definition: qwt_plot_shapeitem.cpp:230
QwtPlotShapeItem::LegendShape
@ LegendShape
Display a scaled down version of the shape.
Definition: qwt_plot_shapeitem.h:67
QwtPlotShapeItem::draw
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect) const QWT_OVERRIDE
Definition: qwt_plot_shapeitem.cpp:385
QwtPlotShapeItem::PrivateData
Definition: qwt_plot_shapeitem.cpp:83
QwtPlotShapeItem::setRenderTolerance
void setRenderTolerance(double)
Set the tolerance for the weeding optimization.
Definition: qwt_plot_shapeitem.cpp:357
QwtPlotShapeItem::LegendColor
@ LegendColor
Display a filled rectangle.
Definition: qwt_plot_shapeitem.h:70
QwtPlotShapeItem::legendMode
LegendMode legendMode() const
Definition: qwt_plot_shapeitem.cpp:199
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition: qwt_plot_item.cpp:491
QwtGraphic
A paint device for scalable graphics.
Definition: qwt_graphic.h:75
QwtPlotShapeItem::m_data
PrivateData * m_data
Definition: qwt_plot_shapeitem.h:114
QwtPlotShapeItem::setRect
void setRect(const QRectF &)
Set a path built from a rectangle.
Definition: qwt_plot_shapeitem.cpp:216
QwtPlotShapeItem::PrivateData::brush
QBrush brush
Definition: qwt_plot_shapeitem.cpp:99
QwtPlotShapeItem::legendIcon
virtual QwtGraphic legendIcon(int index, const QSizeF &) const QWT_OVERRIDE
Definition: qwt_plot_shapeitem.cpp:463
QwtPlotShapeItem::QwtPlotShapeItem
QwtPlotShapeItem(const QString &title=QString())
Constructor.
Definition: qwt_plot_shapeitem.cpp:112
QwtPlotShapeItem::PrivateData::shape
QPainterPath shape
Definition: qwt_plot_shapeitem.cpp:100
QwtPlotShapeItem::shape
QPainterPath shape() const
Definition: qwt_plot_shapeitem.cpp:266
QwtPlotItem::defaultIcon
QwtGraphic defaultIcon(const QBrush &, const QSizeF &) const
Return a default icon from a brush.
Definition: qwt_plot_item.cpp:422
mqtt_test_proto.x
x
Definition: mqtt_test_proto.py:34
QList
Definition: qwt_abstract_legend.h:17
qwt_math.h
QwtPlotItem::Legend
@ Legend
The item is represented on the legend.
Definition: qwt_plot_item.h:150
QwtPlotShapeItem::pen
QPen pen() const
Definition: qwt_plot_shapeitem.cpp:310
QwtWeedingCurveFitter
A curve fitter implementing Douglas and Peucker algorithm.
Definition: qwt_weeding_curve_fitter.h:38
QwtPainter::roundingAlignment
static bool roundingAlignment()
Definition: qwt_painter.h:183
QwtPlotShapeItem::PrivateData::PrivateData
PrivateData()
Definition: qwt_plot_shapeitem.cpp:86
mqtt_test_proto.y
y
Definition: mqtt_test_proto.py:35
QwtPlotShapeItem::setShape
void setShape(const QPainterPath &)
Set the shape to be displayed.
Definition: qwt_plot_shapeitem.cpp:244
QwtPlotShapeItem::setLegendMode
void setLegendMode(LegendMode)
Definition: qwt_plot_shapeitem.cpp:186
QwtPlotItem::boundingRect
virtual QRectF boundingRect() const
Definition: qwt_plot_item.cpp:568
QwtPlotShapeItem::setBrush
void setBrush(const QBrush &)
Definition: qwt_plot_shapeitem.cpp:323
QwtPlotShapeItem::PrivateData::pen
QPen pen
Definition: qwt_plot_shapeitem.cpp:98
QwtPlotItem::AutoScale
@ AutoScale
Definition: qwt_plot_item.h:157
nonstd::span_lite::size
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1554
qwt_clipper.h
QwtPlotShapeItem::PrivateData::paintAttributes
QwtPlotShapeItem::PaintAttributes paintAttributes
Definition: qwt_plot_shapeitem.cpp:92
qwt_scale_map.h
QwtText
A class representing a text.
Definition: qwt_text.h:51
QwtPlotShapeItem::renderTolerance
double renderTolerance() const
Definition: qwt_plot_shapeitem.cpp:372
qwtMaxF
QWT_CONSTEXPR float qwtMaxF(float a, float b)
Definition: qwt_math.h:127
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition: qwt_plot_item.cpp:165
QwtPlotShapeItem::boundingRect
virtual QRectF boundingRect() const QWT_OVERRIDE
Bounding rectangle of the shape.
Definition: qwt_plot_shapeitem.cpp:205
QwtPlotItem::Rtti_PlotShape
@ Rtti_PlotShape
For QwtPlotShapeItem.
Definition: qwt_plot_item.h:120
QwtPlotShapeItem::~QwtPlotShapeItem
virtual ~QwtPlotShapeItem()
Destructor.
Definition: qwt_plot_shapeitem.cpp:134
QwtPlotShapeItem::PrivateData::boundingRect
QRectF boundingRect
Definition: qwt_plot_shapeitem.cpp:96
QwtPlotItem
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:66
QwtClipper::clipPolygonF
QWT_EXPORT void clipPolygonF(const QRectF &, QPolygonF &, bool closePolygon=false)
Definition: qwt_clipper.cpp:404
qwt_weeding_curve_fitter.h
QwtWeedingCurveFitter::fitCurve
virtual QPolygonF fitCurve(const QPolygonF &) const QWT_OVERRIDE
Definition: qwt_weeding_curve_fitter.cpp:124
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:26
QwtPlotShapeItem::PrivateData::renderTolerance
double renderTolerance
Definition: qwt_plot_shapeitem.cpp:95
QwtPlotShapeItem::PrivateData::legendMode
QwtPlotShapeItem::LegendMode legendMode
Definition: qwt_plot_shapeitem.cpp:93
qwt_painter.h
QwtPlotItem::setItemAttribute
void setItemAttribute(ItemAttribute, bool on=true)
Definition: qwt_plot_item.cpp:228
QwtPlotShapeItem::LegendMode
LegendMode
Mode how to display the item on the legend.
Definition: qwt_plot_shapeitem.h:64
QwtPainter::effectivePenWidth
static qreal effectivePenWidth(const QPen &)
Definition: qwt_painter.h:201
QwtPlotShapeItem::rtti
virtual int rtti() const QWT_OVERRIDE
Definition: qwt_plot_shapeitem.cpp:151
QwtPlotShapeItem::PaintAttribute
PaintAttribute
Definition: qwt_plot_shapeitem.h:47
QwtPlotShapeItem::init
void init()
Definition: qwt_plot_shapeitem.cpp:139
QwtPlotShapeItem::setPaintAttribute
void setPaintAttribute(PaintAttribute, bool on=true)
Definition: qwt_plot_shapeitem.cpp:163
qwt_plot_shapeitem.h
qwtTransformPath
static QPainterPath qwtTransformPath(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QPainterPath &path, bool doAlign)
Definition: qwt_plot_shapeitem.cpp:22
QwtPlotShapeItem::setPen
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
Definition: qwt_plot_shapeitem.cpp:284
QwtPlotShapeItem::brush
QBrush brush() const
Definition: qwt_plot_shapeitem.cpp:336
QwtPlotShapeItem::testPaintAttribute
bool testPaintAttribute(PaintAttribute) const
Definition: qwt_plot_shapeitem.cpp:175
QwtPlotItem::testRenderHint
bool testRenderHint(RenderHint) const
Definition: qwt_plot_item.cpp:332
qwt_text.h
QwtPlotItem::RenderAntialiased
@ RenderAntialiased
Enable antialiasing.
Definition: qwt_plot_item.h:206
QwtGraphic::setDefaultSize
void setDefaultSize(const QSizeF &)
Set a default size.
Definition: qwt_graphic.cpp:553
QwtPlotShapeItem::ClipPolygons
@ ClipPolygons
Definition: qwt_plot_shapeitem.h:58


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:46