##// END OF EJS Templates
Fixed isVisible implementation in XY series
Tero Ahola -
r1346:4fe424a18505
parent child
Show More
@@ -1,146 +1,145
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "areachartitem_p.h"
22 22 #include "qareaseries.h"
23 23 #include "qareaseries_p.h"
24 24 #include "qlineseries.h"
25 25 #include "chartpresenter_p.h"
26 26 #include <QPainter>
27 27 #include <QGraphicsSceneMouseEvent>
28 28 #include <QDebug>
29 29
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32
33 33 //TODO: optimize : remove points which are not visible
34 34
35 35 AreaChartItem::AreaChartItem(QAreaSeries *areaSeries, ChartPresenter *presenter)
36 36 : ChartItem(presenter),
37 37 m_series(areaSeries),
38 38 m_upper(0),
39 39 m_lower(0),
40 40 m_pointsVisible(false)
41 41 {
42 42 setZValue(ChartPresenter::LineChartZValue);
43 43 m_upper = new AreaBoundItem(this,m_series->upperSeries(),presenter);
44 44 if (m_series->lowerSeries())
45 45 m_lower = new AreaBoundItem(this,m_series->lowerSeries(),presenter);
46 46
47 47 QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated()));
48 48 QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
49 49 QObject::connect(this,SIGNAL(clicked(QPointF)),areaSeries,SIGNAL(clicked(QPointF)));
50 50
51 51 handleUpdated();
52 52 }
53 53
54 54 AreaChartItem::~AreaChartItem()
55 55 {
56 56 delete m_upper;
57 57 delete m_lower;
58 58 }
59 59
60 60 QRectF AreaChartItem::boundingRect() const
61 61 {
62 62 return m_rect;
63 63 }
64 64
65 65 QPainterPath AreaChartItem::shape() const
66 66 {
67 67 return m_path;
68 68 }
69 69
70 70 void AreaChartItem::updatePath()
71 71 {
72 72 QPainterPath path;
73 73
74 74 path = m_upper->shape();
75 75
76 76 if (m_lower) {
77 77 path.connectPath(m_lower->shape().toReversed());
78 78 } else {
79 79 QPointF first = path.pointAtPercent(0);
80 80 QPointF last = path.pointAtPercent(1);
81 81 path.lineTo(last.x(),m_clipRect.bottom());
82 82 path.lineTo(first.x(),m_clipRect.bottom());
83 83 }
84 84 path.closeSubpath();
85 85 prepareGeometryChange();
86 86 m_path = path;
87 87 m_rect = path.boundingRect();
88 88 update();
89 89 }
90 90
91 91 void AreaChartItem::handleUpdated()
92 92 {
93 setVisible(m_series->isVisible());
93 94 m_pointsVisible = m_series->pointsVisible();
94 95 m_linePen = m_series->pen();
95 96 m_brush = m_series->brush();
96 97 m_pointPen = m_series->pen();
97 98 m_pointPen.setWidthF(2 * m_pointPen.width());
98 99
99 100 update();
100 101 }
101 102
102 103 void AreaChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
103 104 {
104 105 m_upper->handleDomainChanged(minX,maxX,minY,maxY);
105 106 if (m_lower)
106 107 m_lower->handleDomainChanged(minX,maxX,minY,maxY);
107 108 }
108 109
109 110 void AreaChartItem::handleGeometryChanged(const QRectF &rect)
110 111 {
111 112 m_clipRect=rect.translated(-rect.topLeft());
112 113 setPos(rect.topLeft());
113 114 m_upper->handleGeometryChanged(rect);
114 115 if (m_lower)
115 116 m_lower->handleGeometryChanged(rect);
116 117 }
117 118
118 119 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
119 120 {
120 121 Q_UNUSED(widget)
121 122 Q_UNUSED(option)
122 123
123 if (m_series->isVisible()) {
124 124 painter->save();
125 125 painter->setPen(m_linePen);
126 126 painter->setBrush(m_brush);
127 127 painter->setClipRect(m_clipRect);
128 128 painter->drawPath(m_path);
129 129 if (m_pointsVisible) {
130 130 painter->setPen(m_pointPen);
131 131 painter->drawPoints(m_upper->geometryPoints());
132 132 if (m_lower)
133 133 painter->drawPoints(m_lower->geometryPoints());
134 134 }
135 135 painter->restore();
136 136 }
137 }
138 137
139 138 void AreaChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
140 139 {
141 140 emit clicked(m_upper->calculateDomainPoint(event->pos()));
142 141 }
143 142
144 143 #include "moc_areachartitem_p.cpp"
145 144
146 145 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,114 +1,114
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "linechartitem_p.h"
22 22 #include "qlineseries.h"
23 23 #include "qlineseries_p.h"
24 24 #include "chartpresenter_p.h"
25 25 #include <QPainter>
26 26 #include <QGraphicsSceneMouseEvent>
27 27
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 //TODO: optimize : remove points which are not visible
32 32
33 LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter):XYChart(series,presenter),
33 LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter):
34 XYChart(series, presenter),
34 35 QGraphicsItem(presenter ? presenter->rootItem() : 0),
35 36 m_series(series),
36 37 m_pointsVisible(false)
37 38 {
38 39 setZValue(ChartPresenter::LineChartZValue);
39 40 QObject::connect(series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated()));
40 41 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
41 42 handleUpdated();
42 43 }
43 44
44 45 QRectF LineChartItem::boundingRect() const
45 46 {
46 47 return m_rect;
47 48 }
48 49
49 50 QPainterPath LineChartItem::shape() const
50 51 {
51 52 return m_path;
52 53 }
53 54
54 55 void LineChartItem::updateGeometry()
55 56 {
56 57 const QVector<QPointF>& points = geometryPoints();
57 58
58 59 if(points.size()==0)
59 60 {
60 61 prepareGeometryChange();
61 62 m_path = QPainterPath();
62 63 m_rect = QRect();
63 64 return;
64 65 }
65 66
66 67 QPainterPath linePath(points.at(0));
67 68
68 69 for(int i=1; i< points.size();i++) {
69 70 linePath.lineTo(points.at(i));
70 71 }
71 72
72 73 prepareGeometryChange();
73 74 m_path = linePath;
74 75 m_rect = linePath.boundingRect();
75 76 setPos(origin());
76 77 }
77 78
78 79 void LineChartItem::handleUpdated()
79 80 {
81 setVisible(m_series->isVisible());
80 82 m_pointsVisible = m_series->pointsVisible();
81 83 m_linePen = m_series->pen();
82 84 m_pointPen = m_series->pen();
83 85 m_pointPen.setWidthF(2*m_pointPen.width());
84 86 update();
85 87 }
86 88
87 89 //painter
88 90
89 91 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
90 92 {
91 93 Q_UNUSED(widget)
92 94 Q_UNUSED(option)
93 95
94 if (m_series->isVisible()) {
95 96 painter->save();
96 97 painter->setPen(m_linePen);
97 98 painter->setClipRect(clipRect());
98 99 painter->drawPath(m_path);
99 100 if(m_pointsVisible){
100 101 painter->setPen(m_pointPen);
101 102 painter->drawPoints(geometryPoints());
102 103 }
103 104 painter->restore();
104 105 }
105 }
106 106
107 107 void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
108 108 {
109 109 emit XYChart::clicked(calculateDomainPoint(event->pos()));
110 110 }
111 111
112 112 #include "moc_linechartitem_p.cpp"
113 113
114 114 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,201 +1,205
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "scatterchartitem_p.h"
22 22 #include "qscatterseries.h"
23 23 #include "qscatterseries_p.h"
24 24 #include "chartpresenter_p.h"
25 25 #include <QPainter>
26 26 #include <QGraphicsScene>
27 27 #include <QDebug>
28 28 #include <QGraphicsSceneMouseEvent>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) :
33 33 XYChart(series,presenter),
34 34 QGraphicsItem(presenter ? presenter->rootItem() : 0),
35 35 m_series(series),
36 36 m_items(this),
37 m_visible(true),
37 38 m_shape(QScatterSeries::MarkerShapeRectangle),
38 39 m_size(15)
39 40 {
40 41 QObject::connect(m_series->d_func(),SIGNAL(updated()), this, SLOT(handleUpdated()));
41 42 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
42 43
43 44 setZValue(ChartPresenter::ScatterSeriesZValue);
44 45 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
45 46
46 47 handleUpdated();
47 48
48 49 m_items.setHandlesChildEvents(false);
49 50
50 51 // TODO: how to draw a drop shadow?
51 52 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
52 53 // dropShadow->setOffset(2.0);
53 54 // dropShadow->setBlurRadius(2.0);
54 55 // setGraphicsEffect(dropShadow);
55 56 }
56 57
57 58 QRectF ScatterChartItem::boundingRect() const
58 59 {
59 60 return m_rect;
60 61 }
61 62
62 63 void ScatterChartItem::createPoints(int count)
63 64 {
64 65 for (int i = 0; i < count; ++i) {
65 66
66 67 QGraphicsItem *item = 0;
67 68
68 69 switch (m_shape) {
69 70 case QScatterSeries::MarkerShapeCircle: {
70 71 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
71 72 const QRectF& rect = i->boundingRect();
72 73 i->setPos(-rect.width()/2,-rect.height()/2);
73 74 item = new Marker(i,this);
74 75 break;
75 76 }
76 77 case QScatterSeries::MarkerShapeRectangle: {
77 78 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
78 79 i->setPos(-m_size/2,-m_size/2);
79 80 item = new Marker(i,this);
80 81 break;
81 82 }
82 83 default:
83 84 qWarning()<<"Unsupported marker type";
84 85 break;
85 86
86 87 }
87 88 m_items.addToGroup(item);
88 89 }
89 90 }
90 91
91 92 void ScatterChartItem::deletePoints(int count)
92 93 {
93 94 QList<QGraphicsItem *> items = m_items.childItems();
94 95
95 96 for (int i = 0; i < count; ++i) {
96 97 delete(items.takeLast());
97 98 }
98 99 }
99 100
100 101 void ScatterChartItem::markerSelected(Marker *marker)
101 102 {
102 103 emit XYChart::clicked(marker->point());
103 104 }
104 105
105 106 void ScatterChartItem::updateGeometry()
106 107 {
107 108
108 109 const QVector<QPointF>& points = geometryPoints();
109 110
110 111 if(points.size()==0)
111 112 {
112 113 deletePoints(m_items.childItems().count());
113 114 return;
114 115 }
115 116
116 117 int diff = m_items.childItems().size() - points.size();
117 118
118 119 if(diff>0) {
119 120 deletePoints(diff);
120 121 }
121 122 else if(diff<0) {
122 123 createPoints(-diff);
123 124 }
124 125
125 126 if(diff!=0) handleUpdated();
126 127
127 128 QList<QGraphicsItem*> items = m_items.childItems();
128 129
129 130 for (int i = 0; i < points.size(); i++) {
130 131 Marker* item = static_cast<Marker*>(items.at(i));
131 132 const QPointF& point = points.at(i);
132 133 const QRectF& rect = item->boundingRect();
133 134 item->setPoint(point);
134 135 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
135 if(!clipRect().contains(point)) {
136 if(!m_visible || !clipRect().contains(point)) {
136 137 item->setVisible(false);
137 138 }
138 139 else {
139 140 item->setVisible(true);
140 141 }
141 142 }
142 143
143 144 prepareGeometryChange();
144 145 m_rect = clipRect();
145 146 setPos(origin());
146 147 }
147 148
148 149 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
149 150 {
150 151 Q_UNUSED(painter)
151 152 Q_UNUSED(option)
152 153 Q_UNUSED(widget)
153 154 }
154 155
155 156 void ScatterChartItem::setPen(const QPen& pen)
156 157 {
157 158 foreach(QGraphicsItem* item , m_items.childItems()) {
158 159 static_cast<Marker*>(item)->setPen(pen);
159 160 }
160 161 }
161 162
162 163 void ScatterChartItem::setBrush(const QBrush& brush)
163 164 {
164 165 foreach(QGraphicsItem* item , m_items.childItems()) {
165 166 static_cast<Marker*>(item)->setBrush(brush);
166 167 }
167 168 }
168 169
169 170 void ScatterChartItem::handleUpdated()
170 171 {
171 172 int count = m_items.childItems().count();
172 173
173 174 if(count==0) return;
174 175
175 bool recreate = m_size != m_series->markerSize() || m_shape != m_series->markerShape();
176 bool recreate = m_visible != m_series->isVisible()
177 || m_size != m_series->markerSize()
178 || m_shape != m_series->markerShape();
176 179
180 m_visible = m_series->isVisible();
177 181 m_size = m_series->markerSize();
178 182 m_shape = m_series->markerShape();
179 183
180 184 if(recreate) {
181 185 // TODO: optimize handleUpdate to recreate points only in case shape changed
182 186 deletePoints(count);
183 187 createPoints(count);
184 188
185 189 // Updating geometry is now safe, because it won't call handleUpdated unless it creates/deletes points
186 190 updateGeometry();
187 191 }
188 192
189 193 setPen(m_series->pen());
190 194 setBrush(m_series->brush());
191 195 update();
192 196 }
193 197
194 198 void ScatterChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
195 199 {
196 200 emit XYChart::clicked(calculateDomainPoint(event->pos()));
197 201 }
198 202
199 203 #include "moc_scatterchartitem_p.cpp"
200 204
201 205 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,141 +1,142
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef SCATTERCHARTITEM_H
22 22 #define SCATTERCHARTITEM_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "xychart_p.h"
26 26 #include <QGraphicsEllipseItem>
27 27 #include <QPen>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class QScatterSeries;
32 32 class Marker;
33 33
34 34 class ScatterChartItem : public XYChart, public QGraphicsItem
35 35 {
36 36 Q_OBJECT
37 37 Q_INTERFACES(QGraphicsItem)
38 38 public:
39 39 explicit ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter);
40 40
41 41 public:
42 42 //from QGraphicsItem
43 43 QRectF boundingRect() const;
44 44 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
45 45
46 46 void setPen(const QPen &pen);
47 47 void setBrush(const QBrush &brush);
48 48
49 49 void markerSelected(Marker *item);
50 50
51 51 public Q_SLOTS:
52 52 void handleUpdated();
53 53
54 54 private:
55 55 void createPoints(int count);
56 56 void deletePoints(int count);
57 57
58 58 protected:
59 59 void updateGeometry();
60 60 void mousePressEvent(QGraphicsSceneMouseEvent *event);
61 61
62 62 private:
63 63 QScatterSeries *m_series;
64 64 QGraphicsItemGroup m_items;
65 bool m_visible;
65 66 int m_shape;
66 67 int m_size;
67 68 QRectF m_rect;
68 69 };
69 70
70 71
71 72 class Marker: public QAbstractGraphicsShapeItem
72 73 {
73 74
74 75 public:
75 76
76 77 Marker(QAbstractGraphicsShapeItem *item , ScatterChartItem *parent) : QAbstractGraphicsShapeItem(0) ,m_item(item), m_parent(parent)
77 78 {
78 79 }
79 80
80 81 ~Marker()
81 82 {
82 83 delete m_item;
83 84 }
84 85
85 86 void setPoint(const QPointF& point)
86 87 {
87 88 m_point=point;
88 89 }
89 90
90 91 QPointF point() const
91 92 {
92 93 return m_point;
93 94 }
94 95
95 96 QPainterPath shape() const
96 97 {
97 98 return m_item->shape();
98 99 }
99 100
100 101 QRectF boundingRect() const
101 102 {
102 103 return m_item->boundingRect();
103 104 }
104 105
105 106 bool contains(const QPointF &point) const
106 107 {
107 108 return m_item->contains(point);
108 109 }
109 110
110 111 void setPen(const QPen &pen)
111 112 {
112 113 m_item->setPen(pen);
113 114 }
114 115
115 116 void setBrush(const QBrush &brush)
116 117 {
117 118 m_item->setBrush(brush);
118 119 }
119 120
120 121 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
121 122 {
122 123 m_item->paint(painter,option,widget);
123 124 }
124 125
125 126 protected:
126 127
127 128 void mousePressEvent(QGraphicsSceneMouseEvent *event)
128 129 {
129 130 Q_UNUSED(event)
130 131 m_parent->markerSelected(this);
131 132 }
132 133
133 134 private:
134 135 QAbstractGraphicsShapeItem* m_item;
135 136 ScatterChartItem* m_parent;
136 137 QPointF m_point;
137 138 };
138 139
139 140 QTCOMMERCIALCHART_END_NAMESPACE
140 141
141 142 #endif // SCATTERPRESENTER_H
@@ -1,171 +1,171
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "splinechartitem_p.h"
22 22 #include "qsplineseries_p.h"
23 23 #include "chartpresenter_p.h"
24 24 #include "chartanimator_p.h"
25 25 #include <QPainter>
26 26 #include <QGraphicsSceneMouseEvent>
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) :
31 31 XYChart(series, presenter),
32 32 QGraphicsItem(presenter ? presenter->rootItem() : 0),
33 33 m_series(series),
34 34 m_pointsVisible(false),
35 35 m_animation(0)
36 36 {
37 37 setZValue(ChartPresenter::LineChartZValue);
38 38 QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated()));
39 39 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
40 40 handleUpdated();
41 41 }
42 42
43 43 QRectF SplineChartItem::boundingRect() const
44 44 {
45 45 return m_rect;
46 46 }
47 47
48 48 QPainterPath SplineChartItem::shape() const
49 49 {
50 50 return m_path;
51 51 }
52 52
53 53 void SplineChartItem::setAnimation(SplineAnimation* animation)
54 54 {
55 55 m_animation=animation;
56 56 XYChart::setAnimation(animation);
57 57 }
58 58
59 59 void SplineChartItem::setControlGeometryPoints(QVector<QPointF>& points)
60 60 {
61 61 m_controlPoints=points;
62 62 }
63 63
64 64 QVector<QPointF> SplineChartItem::controlGeometryPoints() const
65 65 {
66 66 return m_controlPoints;
67 67 }
68 68
69 69 void SplineChartItem::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
70 70 {
71 71 QVector<QPointF> controlPoints;
72 72
73 73 if(newPoints.count()>=2) {
74 74 controlPoints.resize(newPoints.count()*2-2);
75 75 }
76 76
77 77 for (int i = 0; i < newPoints.size() - 1; i++) {
78 78 controlPoints[2*i] = calculateGeometryControlPoint(2 * i);
79 79 controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1);
80 80 }
81 81
82 82 if (controlPoints.count()<2) {
83 83 setGeometryPoints(newPoints);
84 84 setControlGeometryPoints(controlPoints);
85 85 updateGeometry();
86 86 return;
87 87 }
88 88
89 89 if (m_animation) {
90 90 m_animation->setup(oldPoints,newPoints,m_controlPoints,controlPoints,index);
91 91 setGeometryPoints(newPoints);
92 92 setDirty(false);
93 93 presenter()->startAnimation(m_animation);
94 94 }
95 95 else {
96 96 setGeometryPoints(newPoints);
97 97 setControlGeometryPoints(controlPoints);
98 98 updateGeometry();
99 99 }
100 100 }
101 101
102 102 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
103 103 {
104 104 return XYChart::calculateGeometryPoint(m_series->d_func()->controlPoint(index));
105 105 }
106 106
107 107 void SplineChartItem::updateGeometry()
108 108 {
109 109 const QVector<QPointF> &points = geometryPoints();
110 110 const QVector<QPointF> &controlPoints = controlGeometryPoints();
111 111
112 112 if ((points.size()<2) || (controlPoints.size()<2)) {
113 113 prepareGeometryChange();
114 114 m_path = QPainterPath();
115 115 m_rect = QRect();
116 116 return;
117 117 }
118 118
119 119 Q_ASSERT(points.count()*2-2 == controlPoints.count());
120 120
121 121 QPainterPath splinePath(points.at(0));
122 122
123 123 for (int i = 0; i < points.size() - 1; i++) {
124 124 const QPointF& point = points.at(i + 1);
125 125 splinePath.cubicTo(controlPoints[2*i],controlPoints[2 * i + 1],point);
126 126 }
127 127
128 128 prepareGeometryChange();
129 129 m_path = splinePath;
130 130 m_rect = splinePath.boundingRect();
131 131 setPos(origin());
132 132 }
133 133
134 134 //handlers
135 135
136 136 void SplineChartItem::handleUpdated()
137 137 {
138 setVisible(m_series->isVisible());
138 139 m_pointsVisible = m_series->pointsVisible();
139 140 m_linePen = m_series->pen();
140 141 m_pointPen = m_series->pen();
141 142 m_pointPen.setWidthF(2*m_pointPen.width());
142 143 update();
143 144 }
144 145
145 146 //painter
146 147
147 148 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
148 149 {
149 150 Q_UNUSED(widget)
150 151 Q_UNUSED(option)
151 if (m_series->isVisible()) {
152
152 153 painter->save();
153 154 painter->setClipRect(clipRect());
154 155 painter->setPen(m_linePen);
155 156 painter->drawPath(m_path);
156 157 if (m_pointsVisible) {
157 158 painter->setPen(m_pointPen);
158 159 painter->drawPoints(geometryPoints());
159 160 }
160 161 painter->restore();
161 162 }
162 }
163 163
164 164 void SplineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
165 165 {
166 166 emit XYChart::clicked(calculateDomainPoint(event->pos()));
167 167 }
168 168
169 169 #include "moc_splinechartitem_p.cpp"
170 170
171 171 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now