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