##// END OF EJS Templates
Adds visiblePoints handling to area chart
Michal Klocek -
r563:c49efba21573
parent child
Show More
@@ -1,102 +1,112
1 1 #include "areachartitem_p.h"
2 2 #include "qareaseries.h"
3 3 #include "qlineseries.h"
4 4 #include <QPainter>
5 5
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 //TODO: optimize : remove points which are not visible
10 10
11 11 AreaChartItem::AreaChartItem(QAreaSeries* areaSeries,QGraphicsItem *parent):ChartItem(parent),
12 12 m_series(areaSeries),
13 13 m_upper(0),
14 m_lower(0)
14 m_lower(0),
15 m_pointsVisible(false)
15 16 {
16 17 //m_items.setZValue(ChartPresenter::LineChartZValue);
17 18 m_upper = new AreaBoundItem(this,m_series->upperSeries());
18 19 if(m_series->lowerSeries()){
19 20 m_lower = new AreaBoundItem(this,m_series->lowerSeries());
20 21 }
21 22
22 23 QObject::connect(areaSeries,SIGNAL(updated()),this,SLOT(handleUpdated()));
23 24
24 25 handleUpdated();
25 26 }
26 27
27 28 AreaChartItem::~AreaChartItem()
28 29 {
29 30 delete m_upper;
30 31 delete m_lower;
31 32 };
32 33
33 34 QRectF AreaChartItem::boundingRect() const
34 35 {
35 36 return m_rect;
36 37 }
37 38
38 39 QPainterPath AreaChartItem::shape() const
39 40 {
40 41 return m_path;
41 42 }
42 43
43 44 void AreaChartItem::updatePath()
44 45 {
45 46 QPainterPath path;
46 47
47 48 path.connectPath(m_upper->shape());
48 49 if(m_lower){
49 50 path.connectPath(m_lower->shape().toReversed());
50 51 }
51 52 else{
52 53 QPointF first = path.pointAtPercent(0);
53 54 QPointF last = path.pointAtPercent(1);
54 55 path.lineTo(last.x(),m_clipRect.bottom());
55 56 path.lineTo(first.x(),m_clipRect.bottom());
56 57 }
57 58 path.closeSubpath();
58 59 prepareGeometryChange();
59 60 m_path=path;
60 61 m_rect=path.boundingRect();
61 62 update();
62 63 }
63 64
64 65 void AreaChartItem::handleUpdated()
65 66 {
66 m_pen = m_series->pen();
67 m_pointsVisible = m_series->pointsVisible();
68 m_linePen = m_series->pen();
67 69 m_brush = m_series->brush();
70 m_pointPen = m_series->pen();
71 m_pointPen.setWidthF(2*m_pointPen.width());
72
68 73 update();
69 74 }
70 75
71 76 void AreaChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
72 77 {
73 78 m_upper->handleDomainChanged(minX,maxX,minY,maxY);
74 79 if(m_lower)
75 80 m_lower->handleDomainChanged(minX,maxX,minY,maxY);
76 81 }
77 82
78 83 void AreaChartItem::handleGeometryChanged(const QRectF& rect)
79 84 {
80 85 m_clipRect=rect.translated(-rect.topLeft());
81 86 setPos(rect.topLeft());
82 87 m_upper->handleGeometryChanged(rect);
83 88 if(m_lower)
84 89 m_lower->handleGeometryChanged(rect);
85 90 }
86 91 //painter
87 92
88 93 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
89 94 {
90 95 Q_UNUSED(widget);
91 96 Q_UNUSED(option);
92 97 painter->save();
93 painter->setPen(m_pen);
98 painter->setPen(m_linePen);
94 99 painter->setBrush(m_brush);
95 100 painter->setClipRect(m_clipRect);
96 101 painter->drawPath(m_path);
102 if(m_pointsVisible){
103 painter->setPen(m_pointPen);
104 painter->drawPoints(m_upper->points());
105 if(m_lower) painter->drawPoints(m_lower->points());
106 }
97 107 painter->restore();
98 108 }
99 109
100 110 #include "moc_areachartitem_p.cpp"
101 111
102 112 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,66 +1,68
1 1 #ifndef AREACHARTITEM_H
2 2 #define AREACHARTITEM_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "linechartitem_p.h"
6 6 #include <QPen>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QAreaSeries;
11 11 class AreaChartItem;
12 12
13 13 class AreaChartItem : public QObject ,public ChartItem
14 14 {
15 15 Q_OBJECT
16 16 public:
17 17 AreaChartItem(QAreaSeries* areaSeries, QGraphicsItem *parent = 0);
18 18 ~ AreaChartItem();
19 19
20 20 //from QGraphicsItem
21 21 QRectF boundingRect() const;
22 22 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 23 QPainterPath shape() const;
24 24
25 25 LineChartItem* upperLineItem() const { return m_upper ;}
26 26 LineChartItem* lowerLineItem() const { return m_lower ;}
27 27
28 void setPointsVisible(bool visible);
29 28 void updatePath();
30 29 public slots:
31 30 void handleUpdated();
32 31 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
33 32 void handleGeometryChanged(const QRectF& size);
34 33
35 34 private:
36 35 QAreaSeries* m_series;
37 36 LineChartItem* m_upper;
38 37 LineChartItem* m_lower;
39 38 QPainterPath m_path;
40 39 QRectF m_rect;
41 40 QRectF m_clipRect;
42 QPen m_pen;
41 QPen m_linePen;
42 QPen m_pointPen;
43 43 QBrush m_brush;
44 bool m_pointsVisible;
45
44 46 };
45 47
46 48 class AreaBoundItem : public LineChartItem
47 49 {
48 50 public:
49 51 AreaBoundItem(AreaChartItem* item,QLineSeries* lineSeries):LineChartItem(lineSeries),
50 52 m_item(item){};
51 53
52 54 ~AreaBoundItem(){};
53 55
54 56 void setLayout(QVector<QPointF>& points){
55 57 LineChartItem::setLayout(points);
56 58 m_item->updatePath();
57 59 }
58 60
59 61 private:
60 62 AreaChartItem* m_item;
61 63
62 64 };
63 65
64 66 QTCOMMERCIALCHART_END_NAMESPACE
65 67
66 68 #endif
General Comments 0
You need to be logged in to leave comments. Login now