##// END OF EJS Templates
Unify naming setGeometry -> setLayout
Michal Klocek -
r557:6b65c67e3745
parent child
Show More
@@ -1,96 +1,96
1 #include "xyanimation_p.h"
1 #include "xyanimation_p.h"
2 #include "xychartitem_p.h"
2 #include "xychartitem_p.h"
3
3
4 Q_DECLARE_METATYPE(QVector<QPointF>)
4 Q_DECLARE_METATYPE(QVector<QPointF>)
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 XYAnimation::XYAnimation(XYChartItem *item):ChartAnimation(item),
8 XYAnimation::XYAnimation(XYChartItem *item):ChartAnimation(item),
9 m_item(item),
9 m_item(item),
10 m_type(MoveDownAnimation),
10 m_type(MoveDownAnimation),
11 m_dirty(false)
11 m_dirty(false)
12 {
12 {
13 }
13 }
14
14
15 XYAnimation::~XYAnimation()
15 XYAnimation::~XYAnimation()
16 {
16 {
17 }
17 }
18
18
19 void XYAnimation::setAnimationType(Animation type)
19 void XYAnimation::setAnimationType(Animation type)
20 {
20 {
21 m_type=type;
21 m_type=type;
22 }
22 }
23
23
24 void XYAnimation::setValues(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints)
24 void XYAnimation::setValues(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints)
25 {
25 {
26 setKeyValueAt(0.0, qVariantFromValue(oldPoints));
26 setKeyValueAt(0.0, qVariantFromValue(oldPoints));
27 setKeyValueAt(1.0, qVariantFromValue(newPoints));
27 setKeyValueAt(1.0, qVariantFromValue(newPoints));
28 m_points = newPoints;
28 m_points = newPoints;
29 m_dirty=false;
29 m_dirty=false;
30 }
30 }
31
31
32 void XYAnimation::updateValues(QVector<QPointF>& newPoints)
32 void XYAnimation::updateValues(QVector<QPointF>& newPoints)
33 {
33 {
34 if(state()!=QAbstractAnimation::Stopped) {
34 if(state()!=QAbstractAnimation::Stopped) {
35 stop();
35 stop();
36 m_dirty=true;
36 m_dirty=true;
37 }
37 }
38
38
39 if(m_dirty) {
39 if(m_dirty) {
40 m_points=newPoints;
40 m_points=newPoints;
41 m_dirty=false;
41 m_dirty=false;
42 }
42 }
43
43
44 setKeyValueAt(0.0, qVariantFromValue(m_points));
44 setKeyValueAt(0.0, qVariantFromValue(m_points));
45 setKeyValueAt(1.0, qVariantFromValue(newPoints));
45 setKeyValueAt(1.0, qVariantFromValue(newPoints));
46 }
46 }
47
47
48 QVariant XYAnimation::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
48 QVariant XYAnimation::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
49 {
49 {
50 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
50 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
51 QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end);
51 QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end);
52 QVector<QPointF> result;
52 QVector<QPointF> result;
53
53
54 switch(m_type) {
54 switch(m_type) {
55
55
56 case MoveDownAnimation: {
56 case MoveDownAnimation: {
57
57
58 Q_ASSERT(startVector.count() == endVector.count());
58 Q_ASSERT(startVector.count() == endVector.count());
59 for(int i =0;i< startVector.count();i++) {
59 for(int i =0;i< startVector.count();i++) {
60 qreal x = startVector[i].x() + ((endVector[i].x()- startVector[i].x()) * progress);
60 qreal x = startVector[i].x() + ((endVector[i].x()- startVector[i].x()) * progress);
61 qreal y = startVector[i].y() + ((endVector[i].y()- startVector[i].y()) * progress);
61 qreal y = startVector[i].y() + ((endVector[i].y()- startVector[i].y()) * progress);
62 result << QPointF(x,y);
62 result << QPointF(x,y);
63 }
63 }
64
64
65 }
65 }
66 break;
66 break;
67 case LineDrawAnimation:{
67 case LineDrawAnimation:{
68 for(int i =0;i< endVector.count()* qBound(0.0, progress, 1.0);i++) {
68 for(int i =0;i< endVector.count()* qBound(0.0, progress, 1.0);i++) {
69 result << endVector[i];
69 result << endVector[i];
70 }
70 }
71 }
71 }
72 break;
72 break;
73 default:
73 default:
74 qWarning()<<"Unknow type of animation";
74 qWarning()<<"Unknow type of animation";
75 break;
75 break;
76 }
76 }
77
77
78 return qVariantFromValue(result);
78 return qVariantFromValue(result);
79 }
79 }
80
80
81 void XYAnimation::updateCurrentValue (const QVariant & value )
81 void XYAnimation::updateCurrentValue (const QVariant & value )
82 {
82 {
83 if(state()!=QAbstractAnimation::Stopped){ //workaround
83 if(state()!=QAbstractAnimation::Stopped){ //workaround
84 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
84 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
85 m_item->setGeometry(vector);
85 m_item->setLayout(vector);
86 }
86 }
87 }
87 }
88
88
89 void XYAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
89 void XYAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
90 {
90 {
91 Q_UNUSED(oldState)
91 Q_UNUSED(oldState)
92 if (newState==QAbstractAnimation::Running) m_dirty=true;
92 if (newState==QAbstractAnimation::Running) m_dirty=true;
93 QVariantAnimation::updateState(newState,oldState);
93 QVariantAnimation::updateState(newState,oldState);
94 }
94 }
95
95
96 QTCOMMERCIALCHART_END_NAMESPACE
96 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,112 +1,102
1 #include "areachartitem_p.h"
1 #include "areachartitem_p.h"
2 #include "qareaseries.h"
2 #include "qareaseries.h"
3 #include "qlineseries.h"
3 #include "qlineseries.h"
4 #include <QPainter>
4 #include <QPainter>
5
5
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 //TODO: optimize : remove points which are not visible
9 //TODO: optimize : remove points which are not visible
10
10
11 AreaChartItem::AreaChartItem(QAreaSeries* areaSeries,QGraphicsItem *parent):ChartItem(parent),
11 AreaChartItem::AreaChartItem(QAreaSeries* areaSeries,QGraphicsItem *parent):ChartItem(parent),
12 m_series(areaSeries),
12 m_series(areaSeries),
13 m_upper(0),
13 m_upper(0),
14 m_lower(0)
14 m_lower(0)
15 {
15 {
16 //m_items.setZValue(ChartPresenter::LineChartZValue);
16 //m_items.setZValue(ChartPresenter::LineChartZValue);
17 m_upper = new AreaBoundItem(this,m_series->upperSeries());
17 m_upper = new AreaBoundItem(this,m_series->upperSeries());
18 if(m_series->lowerSeries()){
18 if(m_series->lowerSeries()){
19 m_lower = new AreaBoundItem(this,m_series->lowerSeries());
19 m_lower = new AreaBoundItem(this,m_series->lowerSeries());
20 }
20 }
21
21
22 QObject::connect(areaSeries,SIGNAL(updated()),this,SLOT(handleUpdated()));
22 QObject::connect(areaSeries,SIGNAL(updated()),this,SLOT(handleUpdated()));
23
23
24 handleUpdated();
24 handleUpdated();
25 }
25 }
26
26
27 AreaChartItem::~AreaChartItem()
27 AreaChartItem::~AreaChartItem()
28 {
28 {
29 delete m_upper;
29 delete m_upper;
30 delete m_lower;
30 delete m_lower;
31 };
31 };
32
32
33 QRectF AreaChartItem::boundingRect() const
33 QRectF AreaChartItem::boundingRect() const
34 {
34 {
35 return m_rect;
35 return m_rect;
36 }
36 }
37
37
38 QPainterPath AreaChartItem::shape() const
38 QPainterPath AreaChartItem::shape() const
39 {
39 {
40 return m_path;
40 return m_path;
41 }
41 }
42
42
43 void AreaChartItem::setPen(const QPen& pen)
44 {
45 m_pen = pen;
46 }
47
48 void AreaChartItem::setBrush(const QBrush& brush)
49 {
50 m_brush = brush;
51 }
52
53 void AreaChartItem::updatePath()
43 void AreaChartItem::updatePath()
54 {
44 {
55 QPainterPath path;
45 QPainterPath path;
56
46
57 path.connectPath(m_upper->shape());
47 path.connectPath(m_upper->shape());
58 if(m_lower){
48 if(m_lower){
59 path.connectPath(m_lower->shape().toReversed());
49 path.connectPath(m_lower->shape().toReversed());
60 }
50 }
61 else{
51 else{
62 QPointF first = path.pointAtPercent(0);
52 QPointF first = path.pointAtPercent(0);
63 QPointF last = path.pointAtPercent(1);
53 QPointF last = path.pointAtPercent(1);
64 path.lineTo(last.x(),m_clipRect.bottom());
54 path.lineTo(last.x(),m_clipRect.bottom());
65 path.lineTo(first.x(),m_clipRect.bottom());
55 path.lineTo(first.x(),m_clipRect.bottom());
66 }
56 }
67 path.closeSubpath();
57 path.closeSubpath();
68 prepareGeometryChange();
58 prepareGeometryChange();
69 m_path=path;
59 m_path=path;
70 m_rect=path.boundingRect();
60 m_rect=path.boundingRect();
71 update();
61 update();
72 }
62 }
73
63
74 void AreaChartItem::handleUpdated()
64 void AreaChartItem::handleUpdated()
75 {
65 {
76 setPen(m_series->pen());
66 m_pen = m_series->pen();
77 setBrush(m_series->brush());
67 m_brush = m_series->brush();
78 update();
68 update();
79 }
69 }
80
70
81 void AreaChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
71 void AreaChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
82 {
72 {
83 m_upper->handleDomainChanged(minX,maxX,minY,maxY);
73 m_upper->handleDomainChanged(minX,maxX,minY,maxY);
84 if(m_lower)
74 if(m_lower)
85 m_lower->handleDomainChanged(minX,maxX,minY,maxY);
75 m_lower->handleDomainChanged(minX,maxX,minY,maxY);
86 }
76 }
87
77
88 void AreaChartItem::handleGeometryChanged(const QRectF& rect)
78 void AreaChartItem::handleGeometryChanged(const QRectF& rect)
89 {
79 {
90 m_clipRect=rect.translated(-rect.topLeft());
80 m_clipRect=rect.translated(-rect.topLeft());
91 setPos(rect.topLeft());
81 setPos(rect.topLeft());
92 m_upper->handleGeometryChanged(rect);
82 m_upper->handleGeometryChanged(rect);
93 if(m_lower)
83 if(m_lower)
94 m_lower->handleGeometryChanged(rect);
84 m_lower->handleGeometryChanged(rect);
95 }
85 }
96 //painter
86 //painter
97
87
98 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
88 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
99 {
89 {
100 Q_UNUSED(widget);
90 Q_UNUSED(widget);
101 Q_UNUSED(option);
91 Q_UNUSED(option);
102 painter->save();
92 painter->save();
103 painter->setPen(m_pen);
93 painter->setPen(m_pen);
104 painter->setBrush(m_brush);
94 painter->setBrush(m_brush);
105 painter->setClipRect(m_clipRect);
95 painter->setClipRect(m_clipRect);
106 painter->drawPath(m_path);
96 painter->drawPath(m_path);
107 painter->restore();
97 painter->restore();
108 }
98 }
109
99
110 #include "moc_areachartitem_p.cpp"
100 #include "moc_areachartitem_p.cpp"
111
101
112 QTCOMMERCIALCHART_END_NAMESPACE
102 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,65 +1,63
1 #ifndef AREACHARTITEM_H
1 #ifndef AREACHARTITEM_H
2 #define AREACHARTITEM_H
2 #define AREACHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "linechartitem_p.h"
5 #include "linechartitem_p.h"
6 #include <QPen>
6 #include <QPen>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QAreaSeries;
10 class QAreaSeries;
11 class AreaChartItem;
11 class AreaChartItem;
12
12
13 class AreaChartItem : public QObject ,public ChartItem
13 class AreaChartItem : public QObject ,public ChartItem
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 public:
16 public:
17 AreaChartItem(QAreaSeries* areaSeries, QGraphicsItem *parent = 0);
17 AreaChartItem(QAreaSeries* areaSeries, QGraphicsItem *parent = 0);
18 ~ AreaChartItem();
18 ~ AreaChartItem();
19
19
20 //from QGraphicsItem
20 //from QGraphicsItem
21 QRectF boundingRect() const;
21 QRectF boundingRect() const;
22 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
22 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 QPainterPath shape() const;
23 QPainterPath shape() const;
24
24
25 void setPen(const QPen& pen);
26 void setBrush(const QBrush& brush);
27 void setPointsVisible(bool visible);
25 void setPointsVisible(bool visible);
28 void updatePath();
26 void updatePath();
29 public slots:
27 public slots:
30 void handleUpdated();
28 void handleUpdated();
31 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
29 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
32 void handleGeometryChanged(const QRectF& size);
30 void handleGeometryChanged(const QRectF& size);
33
31
34 private:
32 private:
35 QAreaSeries* m_series;
33 QAreaSeries* m_series;
36 LineChartItem* m_upper;
34 LineChartItem* m_upper;
37 LineChartItem* m_lower;
35 LineChartItem* m_lower;
38 QPainterPath m_path;
36 QPainterPath m_path;
39 QRectF m_rect;
37 QRectF m_rect;
40 QRectF m_clipRect;
38 QRectF m_clipRect;
41 QPen m_pen;
39 QPen m_pen;
42 QBrush m_brush;
40 QBrush m_brush;
43 };
41 };
44
42
45 class AreaBoundItem : public LineChartItem
43 class AreaBoundItem : public LineChartItem
46 {
44 {
47 public:
45 public:
48 AreaBoundItem(AreaChartItem* item,QLineSeries* lineSeries):LineChartItem(lineSeries),
46 AreaBoundItem(AreaChartItem* item,QLineSeries* lineSeries):LineChartItem(lineSeries),
49 m_item(item){};
47 m_item(item){};
50
48
51 ~AreaBoundItem(){};
49 ~AreaBoundItem(){};
52
50
53 void setGeometry(QVector<QPointF>& points){
51 void setLayout(QVector<QPointF>& points){
54 LineChartItem::setGeometry(points);
52 LineChartItem::setLayout(points);
55 m_item->updatePath();
53 m_item->updatePath();
56 }
54 }
57
55
58 private:
56 private:
59 AreaChartItem* m_item;
57 AreaChartItem* m_item;
60
58
61 };
59 };
62
60
63 QTCOMMERCIALCHART_END_NAMESPACE
61 QTCOMMERCIALCHART_END_NAMESPACE
64
62
65 #endif
63 #endif
@@ -1,333 +1,329
1 #include "charttheme_p.h"
1 #include "charttheme_p.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qlegend.h"
3 #include "qlegend.h"
4 #include "qchartaxis.h"
4 #include "qchartaxis.h"
5 #include <QTime>
5 #include <QTime>
6
6
7 //series
7 //series
8 #include "qbarset.h"
8 #include "qbarset.h"
9 #include "qbarseries.h"
9 #include "qbarseries.h"
10 #include "qstackedbarseries.h"
10 #include "qstackedbarseries.h"
11 #include "qpercentbarseries.h"
11 #include "qpercentbarseries.h"
12 #include "qlineseries.h"
12 #include "qlineseries.h"
13 #include "qareaseries.h"
13 #include "qareaseries.h"
14 #include "qscatterseries.h"
14 #include "qscatterseries.h"
15 #include "qpieseries.h"
15 #include "qpieseries.h"
16 #include "qpieslice.h"
16 #include "qpieslice.h"
17 #include "qsplineseries.h"
17 #include "qsplineseries.h"
18
18
19 //items
19 //items
20 #include "axisitem_p.h"
20 #include "axisitem_p.h"
21 #include "barpresenter_p.h"
21 #include "barpresenter_p.h"
22 #include "stackedbarpresenter_p.h"
22 #include "stackedbarpresenter_p.h"
23 #include "percentbarpresenter_p.h"
23 #include "percentbarpresenter_p.h"
24 #include "linechartitem_p.h"
24 #include "linechartitem_p.h"
25 #include "areachartitem_p.h"
25 #include "areachartitem_p.h"
26 #include "scatterchartitem_p.h"
26 #include "scatterchartitem_p.h"
27 #include "piepresenter_p.h"
27 #include "piepresenter_p.h"
28 #include "splinechartitem_p.h"
28 #include "splinechartitem_p.h"
29
29
30 //themes
30 //themes
31 #include "chartthemedefault_p.h"
31 #include "chartthemedefault_p.h"
32 #include "chartthemevanilla_p.h"
32 #include "chartthemevanilla_p.h"
33 #include "chartthemeicy_p.h"
33 #include "chartthemeicy_p.h"
34 #include "chartthemegrayscale_p.h"
34 #include "chartthemegrayscale_p.h"
35 #include "chartthemescientific_p.h"
35 #include "chartthemescientific_p.h"
36
36
37
37
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
39
40 ChartTheme::ChartTheme(QChart::ChartTheme id)
40 ChartTheme::ChartTheme(QChart::ChartTheme id)
41 {
41 {
42 m_id = id;
42 m_id = id;
43 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
43 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
44 }
44 }
45
45
46
46
47 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
47 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
48 {
48 {
49 switch(theme) {
49 switch(theme) {
50 case QChart::ChartThemeVanilla:
50 case QChart::ChartThemeVanilla:
51 return new ChartThemeVanilla();
51 return new ChartThemeVanilla();
52 case QChart::ChartThemeIcy:
52 case QChart::ChartThemeIcy:
53 return new ChartThemeIcy();
53 return new ChartThemeIcy();
54 case QChart::ChartThemeGrayscale:
54 case QChart::ChartThemeGrayscale:
55 return new ChartThemeGrayscale();
55 return new ChartThemeGrayscale();
56 case QChart::ChartThemeScientific:
56 case QChart::ChartThemeScientific:
57 return new ChartThemeScientific();
57 return new ChartThemeScientific();
58 default:
58 default:
59 return new ChartThemeDefault();
59 return new ChartThemeDefault();
60 }
60 }
61 }
61 }
62
62
63 void ChartTheme::decorate(QChart* chart)
63 void ChartTheme::decorate(QChart* chart)
64 {
64 {
65 chart->setChartBackgroundBrush(m_backgroundGradient);
65 chart->setChartBackgroundBrush(m_backgroundGradient);
66 chart->setChartTitleFont(m_masterFont);
66 chart->setChartTitleFont(m_masterFont);
67 }
67 }
68
68
69 void ChartTheme::decorate(QLegend* legend)
69 void ChartTheme::decorate(QLegend* legend)
70 {
70 {
71 legend->setBackgroundBrush(m_backgroundGradient);
71 legend->setBackgroundBrush(m_backgroundGradient);
72 }
72 }
73
73
74 void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series, int index)
74 void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series, int index)
75 {
75 {
76 QPen pen;
76 QPen pen;
77 QBrush brush;
77 QBrush brush;
78
78
79 if (pen != series->pen()){
79 if (pen != series->pen()){
80 item->setPen(series->pen());
81 } else {
82 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
80 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
83 pen.setWidthF(2);
81 pen.setWidthF(2);
84 item->setPen(pen);
82 series->setPen(pen);
85 }
83 }
86
84
87 if (brush != series->brush()) {
85 if (brush != series->brush()) {
88 item->setBrush(series->brush());
89 } else {
90 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
86 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
91 item->setBrush(brush);
87 series->setBrush(brush);
92 }
88 }
93 }
89 }
94
90
95
91
96 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int index)
92 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int index)
97 {
93 {
98 QPen pen;
94 QPen pen;
99 if(pen != series->pen()){
95 if(pen != series->pen()){
100 series->setPen(series->pen());
96 series->setPen(series->pen());
101 return;
97 return;
102 }else{
98 }else{
103 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
99 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
104 pen.setWidthF(2);
100 pen.setWidthF(2);
105 series->setPen(pen);
101 series->setPen(pen);
106 }
102 }
107 }
103 }
108
104
109 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index)
105 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index)
110 {
106 {
111 QList<QBarSet*> sets = series->barSets();
107 QList<QBarSet*> sets = series->barSets();
112 for (int i=0; i<sets.count(); i++) {
108 for (int i=0; i<sets.count(); i++) {
113 qreal pos = 0.5;
109 qreal pos = 0.5;
114 if (sets.count() > 1)
110 if (sets.count() > 1)
115 pos = (qreal) i / (qreal) (sets.count() - 1);
111 pos = (qreal) i / (qreal) (sets.count() - 1);
116 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
112 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
117 sets.at(i)->setBrush(QBrush(c));
113 sets.at(i)->setBrush(QBrush(c));
118
114
119 // Pick label color as far as possible from bar color (within gradient).
115 // Pick label color as far as possible from bar color (within gradient).
120 // 0.3 is magic number that was picked as value that gave enough contrast with icy theme gradient :)
116 // 0.3 is magic number that was picked as value that gave enough contrast with icy theme gradient :)
121 // TODO: better picking of label color?
117 // TODO: better picking of label color?
122 if (pos < 0.3) {
118 if (pos < 0.3) {
123 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
119 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
124 } else {
120 } else {
125 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
121 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
126 }
122 }
127 sets.at(i)->setFloatingValuePen(QPen(c));
123 sets.at(i)->setFloatingValuePen(QPen(c));
128 }
124 }
129 }
125 }
130
126
131 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int index)
127 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int index)
132 {
128 {
133 QList<QBarSet*> sets = series->barSets();
129 QList<QBarSet*> sets = series->barSets();
134 for (int i=0; i<sets.count(); i++) {
130 for (int i=0; i<sets.count(); i++) {
135 qreal pos = 0.5;
131 qreal pos = 0.5;
136 if (sets.count() > 1)
132 if (sets.count() > 1)
137 pos = (qreal) i / (qreal) (sets.count() - 1);
133 pos = (qreal) i / (qreal) (sets.count() - 1);
138 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
134 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
139 sets.at(i)->setBrush(QBrush(c));
135 sets.at(i)->setBrush(QBrush(c));
140
136
141 if (pos < 0.3) {
137 if (pos < 0.3) {
142 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
138 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
143 } else {
139 } else {
144 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
140 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
145 }
141 }
146 sets.at(i)->setFloatingValuePen(QPen(c));
142 sets.at(i)->setFloatingValuePen(QPen(c));
147 }
143 }
148 }
144 }
149
145
150 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int index)
146 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int index)
151 {
147 {
152 QList<QBarSet*> sets = series->barSets();
148 QList<QBarSet*> sets = series->barSets();
153 for (int i=0; i<sets.count(); i++) {
149 for (int i=0; i<sets.count(); i++) {
154 qreal pos = 0.5;
150 qreal pos = 0.5;
155 if (sets.count() > 1)
151 if (sets.count() > 1)
156 pos = (qreal) i / (qreal) (sets.count() - 1);
152 pos = (qreal) i / (qreal) (sets.count() - 1);
157 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
153 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
158 sets.at(i)->setBrush(QBrush(c));
154 sets.at(i)->setBrush(QBrush(c));
159
155
160 if (pos < 0.3) {
156 if (pos < 0.3) {
161 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
157 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
162 } else {
158 } else {
163 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
159 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
164 }
160 }
165 sets.at(i)->setFloatingValuePen(QPen(c));
161 sets.at(i)->setFloatingValuePen(QPen(c));
166 }
162 }
167 }
163 }
168
164
169 void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int index)
165 void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int index)
170 {
166 {
171 Q_ASSERT(item);
167 Q_ASSERT(item);
172 Q_ASSERT(series);
168 Q_ASSERT(series);
173
169
174 QPen pen;
170 QPen pen;
175 QBrush brush;
171 QBrush brush;
176
172
177 if (pen == series->pen()) {
173 if (pen == series->pen()) {
178 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
174 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
179 pen.setWidthF(2);
175 pen.setWidthF(2);
180 series->setPen(pen);
176 series->setPen(pen);
181 }
177 }
182
178
183 if (brush == series->brush()) {
179 if (brush == series->brush()) {
184 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
180 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
185 series->setBrush(brush);
181 series->setBrush(brush);
186 }
182 }
187 }
183 }
188
184
189 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int index)
185 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int index)
190 {
186 {
191 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
187 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
192 for (int i(0); i < series->slices().count(); i++) {
188 for (int i(0); i < series->slices().count(); i++) {
193 qreal pos = (qreal) i / (qreal) series->count();
189 qreal pos = (qreal) i / (qreal) series->count();
194 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1);
190 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1);
195 series->slices().at(i)->setSlicePen(penColor);
191 series->slices().at(i)->setSlicePen(penColor);
196 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
192 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
197 series->slices().at(i)->setSliceBrush(brushColor);
193 series->slices().at(i)->setSliceBrush(brushColor);
198 }
194 }
199 }
195 }
200
196
201 void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int index)
197 void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int index)
202 {
198 {
203 Q_ASSERT(item);
199 Q_ASSERT(item);
204 Q_ASSERT(series);
200 Q_ASSERT(series);
205
201
206 QPen pen;
202 QPen pen;
207
203
208 if(pen != series->pen()){
204 if(pen != series->pen()){
209 item->setLinePen(series->pen());
205 item->setLinePen(series->pen());
210 }else{
206 }else{
211 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
207 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
212 pen.setWidthF(series->pen().widthF());
208 pen.setWidthF(series->pen().widthF());
213 item->setLinePen(series->pen());
209 item->setLinePen(series->pen());
214 }
210 }
215
211
216 // QColor color = m_seriesColors.at(index % m_seriesColors.size());
212 // QColor color = m_seriesColors.at(index % m_seriesColors.size());
217 // TODO: define alpha in the theme? or in the series?
213 // TODO: define alpha in the theme? or in the series?
218 //color.setAlpha(120);
214 //color.setAlpha(120);
219
215
220 // QBrush brush(color, Qt::SolidPattern);
216 // QBrush brush(color, Qt::SolidPattern);
221 // presenter->m_markerBrush = brush;
217 // presenter->m_markerBrush = brush;
222
218
223 // QPen pen(brush, 3);
219 // QPen pen(brush, 3);
224 // pen.setColor(color);
220 // pen.setColor(color);
225 // presenter->m_markerPen = pen;
221 // presenter->m_markerPen = pen;
226 }
222 }
227
223
228 void ChartTheme::decorate(QChartAxis* axis, AxisItem* item)
224 void ChartTheme::decorate(QChartAxis* axis, AxisItem* item)
229 {
225 {
230 Q_ASSERT(axis);
226 Q_ASSERT(axis);
231 Q_ASSERT(item);
227 Q_ASSERT(item);
232
228
233 if (axis->isAxisVisible()) {
229 if (axis->isAxisVisible()) {
234 axis->setLabelsBrush(m_axisLabelBrush);
230 axis->setLabelsBrush(m_axisLabelBrush);
235 axis->setLabelsPen(m_axisLabelPen);
231 axis->setLabelsPen(m_axisLabelPen);
236 // TODO: check the axis type (x or y) should define whether to show the shades or not
232 // TODO: check the axis type (x or y) should define whether to show the shades or not
237 if (m_backgroundShades == BackgroundShadesBoth
233 if (m_backgroundShades == BackgroundShadesBoth
238 || m_backgroundShades == BackgroundShadesVertical /*&& x axis ?*/
234 || m_backgroundShades == BackgroundShadesVertical /*&& x axis ?*/
239 || m_backgroundShades == BackgroundShadesHorizontal /* && y axis*/) {
235 || m_backgroundShades == BackgroundShadesHorizontal /* && y axis*/) {
240 axis->setShadesPen(m_backgroundShadesPen);
236 axis->setShadesPen(m_backgroundShadesPen);
241 axis->setShadesBrush(m_backgroundShadesBrush);
237 axis->setShadesBrush(m_backgroundShadesBrush);
242 } else {
238 } else {
243 // The shades not supposed to be shown for this axis, clear possible brush and pen
239 // The shades not supposed to be shown for this axis, clear possible brush and pen
244 axis->setShadesPen(Qt::NoPen);
240 axis->setShadesPen(Qt::NoPen);
245 axis->setShadesBrush(Qt::NoBrush);
241 axis->setShadesBrush(Qt::NoBrush);
246 }
242 }
247 axis->setAxisPen(m_axisLinePen);
243 axis->setAxisPen(m_axisLinePen);
248 axis->setGridLinePen(m_gridLinePen);
244 axis->setGridLinePen(m_gridLinePen);
249 axis->setLabelsFont(m_masterFont);
245 axis->setLabelsFont(m_masterFont);
250 }
246 }
251 }
247 }
252
248
253 void ChartTheme::generateSeriesGradients()
249 void ChartTheme::generateSeriesGradients()
254 {
250 {
255 // Generate gradients in HSV color space
251 // Generate gradients in HSV color space
256 foreach (QColor color, m_seriesColors) {
252 foreach (QColor color, m_seriesColors) {
257 QLinearGradient g;
253 QLinearGradient g;
258 qreal h = color.hsvHueF();
254 qreal h = color.hsvHueF();
259 qreal s = color.hsvSaturationF();
255 qreal s = color.hsvSaturationF();
260
256
261 // TODO: tune the algorithm to give nice results with most base colors defined in
257 // TODO: tune the algorithm to give nice results with most base colors defined in
262 // most themes. The rest of the gradients we can define manually in theme specific
258 // most themes. The rest of the gradients we can define manually in theme specific
263 // implementation.
259 // implementation.
264 QColor start = color;
260 QColor start = color;
265 start.setHsvF(h, 0.05, 0.95);
261 start.setHsvF(h, 0.05, 0.95);
266 g.setColorAt(0.0, start);
262 g.setColorAt(0.0, start);
267
263
268 g.setColorAt(0.5, color);
264 g.setColorAt(0.5, color);
269
265
270 QColor end = color;
266 QColor end = color;
271 end.setHsvF(h, s, 0.25);
267 end.setHsvF(h, s, 0.25);
272 g.setColorAt(1.0, end);
268 g.setColorAt(1.0, end);
273
269
274 m_seriesGradients << g;
270 m_seriesGradients << g;
275 }
271 }
276 }
272 }
277
273
278
274
279 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
275 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
280 {
276 {
281 Q_ASSERT(pos >=0.0 && pos <= 1.0);
277 Q_ASSERT(pos >=0.0 && pos <= 1.0);
282 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
278 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
283 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
279 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
284 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
280 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
285 QColor c;
281 QColor c;
286 c.setRgbF(r, g, b);
282 c.setRgbF(r, g, b);
287 return c;
283 return c;
288 }
284 }
289
285
290 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
286 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
291 {
287 {
292 Q_ASSERT(pos >=0 && pos <= 1.0);
288 Q_ASSERT(pos >=0 && pos <= 1.0);
293
289
294 // another possibility:
290 // another possibility:
295 // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient
291 // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient
296
292
297 QGradientStops stops = gradient.stops();
293 QGradientStops stops = gradient.stops();
298 int count = stops.count();
294 int count = stops.count();
299
295
300 // find previous stop relative to position
296 // find previous stop relative to position
301 QGradientStop prev = stops.first();
297 QGradientStop prev = stops.first();
302 for (int i=0; i<count; i++) {
298 for (int i=0; i<count; i++) {
303 QGradientStop stop = stops.at(i);
299 QGradientStop stop = stops.at(i);
304 if (pos > stop.first)
300 if (pos > stop.first)
305 prev = stop;
301 prev = stop;
306
302
307 // given position is actually a stop position?
303 // given position is actually a stop position?
308 if (pos == stop.first) {
304 if (pos == stop.first) {
309 //qDebug() << "stop color" << pos;
305 //qDebug() << "stop color" << pos;
310 return stop.second;
306 return stop.second;
311 }
307 }
312 }
308 }
313
309
314 // find next stop relative to position
310 // find next stop relative to position
315 QGradientStop next = stops.last();
311 QGradientStop next = stops.last();
316 for (int i=count-1; i>=0; i--) {
312 for (int i=count-1; i>=0; i--) {
317 QGradientStop stop = stops.at(i);
313 QGradientStop stop = stops.at(i);
318 if (pos < stop.first)
314 if (pos < stop.first)
319 next = stop;
315 next = stop;
320 }
316 }
321
317
322 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
318 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
323
319
324 qreal range = next.first - prev.first;
320 qreal range = next.first - prev.first;
325 qreal posDelta = pos - prev.first;
321 qreal posDelta = pos - prev.first;
326 qreal relativePos = posDelta / range;
322 qreal relativePos = posDelta / range;
327
323
328 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
324 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
329
325
330 return colorAt(prev.second, next.second, relativePos);
326 return colorAt(prev.second, next.second, relativePos);
331 }
327 }
332
328
333 QTCOMMERCIALCHART_END_NAMESPACE
329 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,88 +1,88
1 #include "linechartitem_p.h"
1 #include "linechartitem_p.h"
2 #include "qlineseries.h"
2 #include "qlineseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QGraphicsSceneMouseEvent>
5 #include <QGraphicsSceneMouseEvent>
6
6
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 //TODO: optimize : remove points which are not visible
10 //TODO: optimize : remove points which are not visible
11
11
12 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent),
12 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent),
13 m_series(series),
13 m_series(series),
14 m_pointsVisible(false)
14 m_pointsVisible(false)
15 {
15 {
16 setZValue(ChartPresenter::LineChartZValue);
16 setZValue(ChartPresenter::LineChartZValue);
17 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
17 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
18 QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&)));
18 QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&)));
19 handleUpdated();
19 handleUpdated();
20 }
20 }
21
21
22 QRectF LineChartItem::boundingRect() const
22 QRectF LineChartItem::boundingRect() const
23 {
23 {
24 return m_rect;
24 return m_rect;
25 }
25 }
26
26
27 QPainterPath LineChartItem::shape() const
27 QPainterPath LineChartItem::shape() const
28 {
28 {
29 return m_path;
29 return m_path;
30 }
30 }
31
31
32 void LineChartItem::setGeometry(QVector<QPointF>& points)
32 void LineChartItem::setLayout(QVector<QPointF>& points)
33 {
33 {
34 if(points.size()==0)
34 if(points.size()==0)
35 {
35 {
36 XYChartItem::setGeometry(points);
36 XYChartItem::setLayout(points);
37 return;
37 return;
38 }
38 }
39
39
40 QList<QGraphicsItem*> items = m_items.childItems();
40 QList<QGraphicsItem*> items = m_items.childItems();
41
41
42 QPainterPath linePath(points.at(0));
42 QPainterPath linePath(points.at(0));
43
43
44 for(int i=1; i< points.size();i++) {
44 for(int i=1; i< points.size();i++) {
45 linePath.lineTo(points.at(i));
45 linePath.lineTo(points.at(i));
46 }
46 }
47
47
48 prepareGeometryChange();
48 prepareGeometryChange();
49 m_path = linePath;
49 m_path = linePath;
50 m_rect = linePath.boundingRect();
50 m_rect = linePath.boundingRect();
51
51
52 XYChartItem::setGeometry(points);
52 XYChartItem::setLayout(points);
53 }
53 }
54
54
55 void LineChartItem::handleUpdated()
55 void LineChartItem::handleUpdated()
56 {
56 {
57 m_pointsVisible = m_series->pointsVisible();
57 m_pointsVisible = m_series->pointsVisible();
58 m_linePen = m_series->pen();
58 m_linePen = m_series->pen();
59 m_pointPen = m_series->pen();
59 m_pointPen = m_series->pen();
60 m_pointPen.setWidthF(2*m_pointPen.width());
60 m_pointPen.setWidthF(2*m_pointPen.width());
61 update();
61 update();
62 }
62 }
63
63
64 //painter
64 //painter
65
65
66 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
66 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
67 {
67 {
68 Q_UNUSED(widget);
68 Q_UNUSED(widget);
69 Q_UNUSED(option);
69 Q_UNUSED(option);
70 painter->save();
70 painter->save();
71 painter->setPen(m_linePen);
71 painter->setPen(m_linePen);
72 painter->setClipRect(clipRect());
72 painter->setClipRect(clipRect());
73 painter->drawPath(m_path);
73 painter->drawPath(m_path);
74 if(m_pointsVisible){
74 if(m_pointsVisible){
75 painter->setPen(m_pointPen);
75 painter->setPen(m_pointPen);
76 painter->drawPoints(points());
76 painter->drawPoints(points());
77 }
77 }
78 painter->restore();
78 painter->restore();
79 }
79 }
80
80
81 void LineChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
81 void LineChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
82 {
82 {
83 emit clicked(calculateDomainPoint(event->pos()));
83 emit clicked(calculateDomainPoint(event->pos()));
84 }
84 }
85
85
86 #include "moc_linechartitem_p.cpp"
86 #include "moc_linechartitem_p.cpp"
87
87
88 QTCOMMERCIALCHART_END_NAMESPACE
88 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,47 +1,47
1 #ifndef LINECHARTITEM_H
1 #ifndef LINECHARTITEM_H
2 #define LINECHARTITEM_H
2 #define LINECHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "xychartitem_p.h"
5 #include "xychartitem_p.h"
6 #include <QPen>
6 #include <QPen>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QLineSeries;
10 class QLineSeries;
11
11
12 class LineChartItem : public XYChartItem
12 class LineChartItem : public XYChartItem
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 explicit LineChartItem(QLineSeries* series,QGraphicsItem *parent = 0);
16 explicit LineChartItem(QLineSeries* series,QGraphicsItem *parent = 0);
17 ~ LineChartItem(){};
17 ~ LineChartItem(){};
18
18
19 //from QGraphicsItem
19 //from QGraphicsItem
20 QRectF boundingRect() const;
20 QRectF boundingRect() const;
21 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
22 QPainterPath shape() const;
22 QPainterPath shape() const;
23
23
24 public slots:
24 public slots:
25 void handleUpdated();
25 void handleUpdated();
26
26
27 signals:
27 signals:
28 void clicked(const QPointF& point);
28 void clicked(const QPointF& point);
29
29
30 protected:
30 protected:
31 virtual void setGeometry(QVector<QPointF>& points);
31 void setLayout(QVector<QPointF>& points);
32 void mousePressEvent( QGraphicsSceneMouseEvent * event );
32 void mousePressEvent( QGraphicsSceneMouseEvent * event );
33
33
34 private:
34 private:
35 QLineSeries* m_series;
35 QLineSeries* m_series;
36 QGraphicsItemGroup m_items;
36 QGraphicsItemGroup m_items;
37 QPainterPath m_path;
37 QPainterPath m_path;
38 QRectF m_rect;
38 QRectF m_rect;
39 QPen m_linePen;
39 QPen m_linePen;
40 QPen m_pointPen;
40 QPen m_pointPen;
41 bool m_pointsVisible;
41 bool m_pointsVisible;
42
42
43 };
43 };
44
44
45 QTCOMMERCIALCHART_END_NAMESPACE
45 QTCOMMERCIALCHART_END_NAMESPACE
46
46
47 #endif
47 #endif
@@ -1,181 +1,181
1 #include "scatterchartitem_p.h"
1 #include "scatterchartitem_p.h"
2 #include "qscatterseries.h"
2 #include "qscatterseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) :
15 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) :
16 XYChartItem(series,parent),
16 XYChartItem(series,parent),
17 m_series(series),
17 m_series(series),
18 m_items(this),
18 m_items(this),
19 m_shape(QScatterSeries::MarkerShapeRectangle),
19 m_shape(QScatterSeries::MarkerShapeRectangle),
20 m_size(10)
20 m_size(10)
21
21
22 {
22 {
23 Q_ASSERT(parent);
23 Q_ASSERT(parent);
24 Q_ASSERT(series);
24 Q_ASSERT(series);
25
25
26 QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
26 QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
27 QObject::connect(this,SIGNAL(clicked(const QPointF&)), m_series, SIGNAL(clicked(const QPointF&)));
27 QObject::connect(this,SIGNAL(clicked(const QPointF&)), m_series, SIGNAL(clicked(const QPointF&)));
28
28
29 setZValue(ChartPresenter::ScatterSeriesZValue);
29 setZValue(ChartPresenter::ScatterSeriesZValue);
30 setFlags(QGraphicsItem::ItemHasNoContents);
30 setFlags(QGraphicsItem::ItemHasNoContents);
31 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
31 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
32
32
33 handleUpdated();
33 handleUpdated();
34
34
35 m_items.setHandlesChildEvents(false);
35 m_items.setHandlesChildEvents(false);
36
36
37 // TODO: how to draw a drop shadow?
37 // TODO: how to draw a drop shadow?
38 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
38 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
39 // dropShadow->setOffset(2.0);
39 // dropShadow->setOffset(2.0);
40 // dropShadow->setBlurRadius(2.0);
40 // dropShadow->setBlurRadius(2.0);
41 // setGraphicsEffect(dropShadow);
41 // setGraphicsEffect(dropShadow);
42 }
42 }
43
43
44
44
45 QRectF ScatterChartItem::boundingRect() const
45 QRectF ScatterChartItem::boundingRect() const
46 {
46 {
47 return m_rect;
47 return m_rect;
48 }
48 }
49
49
50 void ScatterChartItem::createPoints(int count)
50 void ScatterChartItem::createPoints(int count)
51 {
51 {
52 for (int i = 0; i < count; ++i) {
52 for (int i = 0; i < count; ++i) {
53
53
54 QGraphicsItem *item;
54 QGraphicsItem *item;
55
55
56 switch (m_shape) {
56 switch (m_shape) {
57 case QScatterSeries::MarkerShapeCircle:{
57 case QScatterSeries::MarkerShapeCircle:{
58 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
58 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
59 const QRectF& rect = i->boundingRect();
59 const QRectF& rect = i->boundingRect();
60 i->setPos(-rect.width()/2,-rect.height()/2);
60 i->setPos(-rect.width()/2,-rect.height()/2);
61 item = new Marker(i,this);
61 item = new Marker(i,this);
62 break;
62 break;
63 }
63 }
64 case QScatterSeries::MarkerShapeRectangle:{
64 case QScatterSeries::MarkerShapeRectangle:{
65 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
65 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
66 i->setPos(-m_size/2,-m_size/2);
66 i->setPos(-m_size/2,-m_size/2);
67 item = new Marker(i,this);
67 item = new Marker(i,this);
68 break;
68 break;
69 }
69 }
70 default:
70 default:
71 qWarning()<<"Unsupported marker type";
71 qWarning()<<"Unsupported marker type";
72 break;
72 break;
73
73
74 }
74 }
75 m_items.addToGroup(item);
75 m_items.addToGroup(item);
76 }
76 }
77 }
77 }
78
78
79 void ScatterChartItem::deletePoints(int count)
79 void ScatterChartItem::deletePoints(int count)
80 {
80 {
81 QList<QGraphicsItem *> items = m_items.childItems();
81 QList<QGraphicsItem *> items = m_items.childItems();
82
82
83 for (int i = 0; i < count; ++i) {
83 for (int i = 0; i < count; ++i) {
84 delete(items.takeLast());
84 delete(items.takeLast());
85 }
85 }
86 }
86 }
87
87
88 void ScatterChartItem::markerSelected(Marker* marker)
88 void ScatterChartItem::markerSelected(Marker* marker)
89 {
89 {
90 emit clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index())));
90 emit clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index())));
91 }
91 }
92
92
93 void ScatterChartItem::setGeometry(QVector<QPointF>& points)
93 void ScatterChartItem::setLayout(QVector<QPointF>& points)
94 {
94 {
95 if(points.size()==0)
95 if(points.size()==0)
96 {
96 {
97 XYChartItem::setGeometry(points);
97 XYChartItem::setLayout(points);
98 return;
98 return;
99 }
99 }
100
100
101 int diff = XYChartItem::points().size() - points.size();
101 int diff = XYChartItem::points().size() - points.size();
102
102
103 if(diff>0) {
103 if(diff>0) {
104 deletePoints(diff);
104 deletePoints(diff);
105 }
105 }
106 else if(diff<0) {
106 else if(diff<0) {
107 createPoints(-diff);
107 createPoints(-diff);
108 }
108 }
109
109
110 if(diff!=0) handleUpdated();
110 if(diff!=0) handleUpdated();
111
111
112 QList<QGraphicsItem*> items = m_items.childItems();
112 QList<QGraphicsItem*> items = m_items.childItems();
113
113
114 for(int i=0; i< points.size();i++) {
114 for(int i=0; i< points.size();i++) {
115 Marker* item = static_cast<Marker*>(items.at(i));
115 Marker* item = static_cast<Marker*>(items.at(i));
116 const QPointF& point = points.at(i);
116 const QPointF& point = points.at(i);
117 const QRectF& rect = item->boundingRect();
117 const QRectF& rect = item->boundingRect();
118 item->setIndex(i);
118 item->setIndex(i);
119 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
119 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
120 if(!clipRect().contains(point)) {
120 if(!clipRect().contains(point)) {
121 item->setVisible(false);
121 item->setVisible(false);
122 }
122 }
123 else {
123 else {
124 item->setVisible(true);
124 item->setVisible(true);
125 }
125 }
126 }
126 }
127
127
128 prepareGeometryChange();
128 prepareGeometryChange();
129 m_rect = clipRect();
129 m_rect = clipRect();
130 XYChartItem::setGeometry(points);
130 XYChartItem::setLayout(points);
131 }
131 }
132
132
133
133
134 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
134 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
135 {
135 {
136 Q_UNUSED(painter);
136 Q_UNUSED(painter);
137 Q_UNUSED(option);
137 Q_UNUSED(option);
138 Q_UNUSED(widget);
138 Q_UNUSED(widget);
139 }
139 }
140
140
141 void ScatterChartItem::setPen(const QPen& pen)
141 void ScatterChartItem::setPen(const QPen& pen)
142 {
142 {
143 foreach(QGraphicsItem* item , m_items.childItems()) {
143 foreach(QGraphicsItem* item , m_items.childItems()) {
144 static_cast<Marker*>(item)->setPen(pen);
144 static_cast<Marker*>(item)->setPen(pen);
145 }
145 }
146 }
146 }
147
147
148 void ScatterChartItem::setBrush(const QBrush& brush)
148 void ScatterChartItem::setBrush(const QBrush& brush)
149 {
149 {
150 foreach(QGraphicsItem* item , m_items.childItems()) {
150 foreach(QGraphicsItem* item , m_items.childItems()) {
151 static_cast<Marker*>(item)->setBrush(brush);
151 static_cast<Marker*>(item)->setBrush(brush);
152 }
152 }
153 }
153 }
154
154
155 void ScatterChartItem::handleUpdated()
155 void ScatterChartItem::handleUpdated()
156 {
156 {
157
157
158 int count = m_items.childItems().count();
158 int count = m_items.childItems().count();
159
159
160 if(count==0) return;
160 if(count==0) return;
161
161
162 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
162 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
163
163
164 //TODO: only rewrite on size change
164 //TODO: only rewrite on size change
165
165
166 m_size = m_series->size();
166 m_size = m_series->size();
167 m_shape = m_series->shape();
167 m_shape = m_series->shape();
168
168
169 if(recreate){
169 if(recreate){
170 deletePoints(count);
170 deletePoints(count);
171 createPoints(count);
171 createPoints(count);
172 }
172 }
173
173
174 setPen(m_series->pen());
174 setPen(m_series->pen());
175 setBrush(m_series->brush());
175 setBrush(m_series->brush());
176
176
177 }
177 }
178
178
179 #include "moc_scatterchartitem_p.cpp"
179 #include "moc_scatterchartitem_p.cpp"
180
180
181 QTCOMMERCIALCHART_END_NAMESPACE
181 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,122 +1,122
1 #ifndef SCATTERPRESENTER_H
1 #ifndef SCATTERPRESENTER_H
2 #define SCATTERPRESENTER_H
2 #define SCATTERPRESENTER_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "xychartitem_p.h"
5 #include "xychartitem_p.h"
6 #include <QGraphicsEllipseItem>
6 #include <QGraphicsEllipseItem>
7 #include <QPen>
7 #include <QPen>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class QScatterSeries;
11 class QScatterSeries;
12 class Marker;
12 class Marker;
13
13
14 class ScatterChartItem : public XYChartItem
14 class ScatterChartItem : public XYChartItem
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent = 0);
18 explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent = 0);
19
19
20 public:
20 public:
21 //from QGraphicsItem
21 //from QGraphicsItem
22 QRectF boundingRect() const;
22 QRectF boundingRect() const;
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24
24
25 void setPen(const QPen& pen);
25 void setPen(const QPen& pen);
26 void setBrush(const QBrush& brush);
26 void setBrush(const QBrush& brush);
27
27
28 void markerSelected(Marker* item);
28 void markerSelected(Marker* item);
29
29
30 signals:
30 signals:
31 void clicked(const QPointF& point);
31 void clicked(const QPointF& point);
32
32
33 public slots:
33 public slots:
34 void handleUpdated();
34 void handleUpdated();
35
35
36 private:
36 private:
37 void createPoints(int count);
37 void createPoints(int count);
38 void deletePoints(int count);
38 void deletePoints(int count);
39
39
40 protected:
40 protected:
41 virtual void setGeometry(QVector<QPointF>& points);
41 void setLayout(QVector<QPointF>& points);
42
42
43 private:
43 private:
44 QScatterSeries *m_series;
44 QScatterSeries *m_series;
45 QGraphicsItemGroup m_items;
45 QGraphicsItemGroup m_items;
46 int m_shape;
46 int m_shape;
47 int m_size;
47 int m_size;
48 QRectF m_rect;
48 QRectF m_rect;
49
49
50 };
50 };
51
51
52
52
53 class Marker: public QAbstractGraphicsShapeItem
53 class Marker: public QAbstractGraphicsShapeItem
54 {
54 {
55
55
56 public:
56 public:
57
57
58 Marker(QAbstractGraphicsShapeItem* item , ScatterChartItem* parent):QAbstractGraphicsShapeItem(0),m_item(item),m_parent(parent)
58 Marker(QAbstractGraphicsShapeItem* item , ScatterChartItem* parent):QAbstractGraphicsShapeItem(0),m_item(item),m_parent(parent)
59 {
59 {
60 };
60 };
61
61
62 ~Marker()
62 ~Marker()
63 {
63 {
64 delete m_item;
64 delete m_item;
65 }
65 }
66
66
67 void setIndex(int index)
67 void setIndex(int index)
68 {
68 {
69 m_index=index;
69 m_index=index;
70 }
70 }
71
71
72 int index() const
72 int index() const
73 {
73 {
74 return m_index;
74 return m_index;
75 }
75 }
76
76
77 QPainterPath shape() const
77 QPainterPath shape() const
78 {
78 {
79 return m_item->shape();
79 return m_item->shape();
80 }
80 }
81
81
82 QRectF boundingRect() const
82 QRectF boundingRect() const
83 {
83 {
84 return m_item->boundingRect();
84 return m_item->boundingRect();
85 }
85 }
86
86
87 bool contains(const QPointF &point) const
87 bool contains(const QPointF &point) const
88 {
88 {
89 return m_item->contains(point);
89 return m_item->contains(point);
90 }
90 }
91
91
92 void setPen(const QPen& pen)
92 void setPen(const QPen& pen)
93 {
93 {
94 m_item->setPen(pen);
94 m_item->setPen(pen);
95 }
95 }
96
96
97 void setBrush(const QBrush& brush)
97 void setBrush(const QBrush& brush)
98 {
98 {
99 m_item->setBrush(brush);
99 m_item->setBrush(brush);
100 }
100 }
101
101
102 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
102 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
103 {
103 {
104 m_item->paint(painter,option,widget);
104 m_item->paint(painter,option,widget);
105 }
105 }
106
106
107 protected:
107 protected:
108
108
109 void mousePressEvent( QGraphicsSceneMouseEvent * event )
109 void mousePressEvent( QGraphicsSceneMouseEvent * event )
110 {
110 {
111 m_parent->markerSelected(this);
111 m_parent->markerSelected(this);
112 }
112 }
113
113
114 private:
114 private:
115 QAbstractGraphicsShapeItem* m_item;
115 QAbstractGraphicsShapeItem* m_item;
116 ScatterChartItem* m_parent;
116 ScatterChartItem* m_parent;
117 int m_index;
117 int m_index;
118 };
118 };
119
119
120 QTCOMMERCIALCHART_END_NAMESPACE
120 QTCOMMERCIALCHART_END_NAMESPACE
121
121
122 #endif // SCATTERPRESENTER_H
122 #endif // SCATTERPRESENTER_H
@@ -1,106 +1,106
1 #include "splinechartitem_p.h"
1 #include "splinechartitem_p.h"
2 #include "chartpresenter_p.h"
2 #include "chartpresenter_p.h"
3 #include <QPainter>
3 #include <QPainter>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsItem *parent) :
7 SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsItem *parent) :
8 XYChartItem(series, parent),
8 XYChartItem(series, parent),
9 m_series(series)
9 m_series(series)
10 {
10 {
11 setZValue(ChartPresenter::LineChartZValue);
11 setZValue(ChartPresenter::LineChartZValue);
12 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
12 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
13 handleUpdated();
13 handleUpdated();
14 }
14 }
15
15
16 QRectF SplineChartItem::boundingRect() const
16 QRectF SplineChartItem::boundingRect() const
17 {
17 {
18 return m_rect;
18 return m_rect;
19 }
19 }
20
20
21 QPainterPath SplineChartItem::shape() const
21 QPainterPath SplineChartItem::shape() const
22 {
22 {
23 return m_path;
23 return m_path;
24 }
24 }
25
25
26 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
26 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
27 {
27 {
28 return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index));
28 return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index));
29 }
29 }
30
30
31 void SplineChartItem::setGeometry(QVector<QPointF>& points)
31 void SplineChartItem::setLayout(QVector<QPointF>& points)
32 {
32 {
33
33
34 if(points.size()==0)
34 if(points.size()==0)
35 {
35 {
36 XYChartItem::setGeometry(points);
36 XYChartItem::setLayout(points);
37 return;
37 return;
38 }
38 }
39
39
40 QPainterPath splinePath;
40 QPainterPath splinePath;
41 const QPointF& point = points.at(0);
41 const QPointF& point = points.at(0);
42 splinePath.moveTo(point);
42 splinePath.moveTo(point);
43
43
44 for (int i = 0; i < points.size() - 1; i++)
44 for (int i = 0; i < points.size() - 1; i++)
45 {
45 {
46 const QPointF& point = points.at(i + 1);
46 const QPointF& point = points.at(i + 1);
47 splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point);
47 splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point);
48 }
48 }
49
49
50 prepareGeometryChange();
50 prepareGeometryChange();
51 m_path = splinePath;
51 m_path = splinePath;
52 m_rect = splinePath.boundingRect();
52 m_rect = splinePath.boundingRect();
53 XYChartItem::setGeometry(points);
53 XYChartItem::setLayout(points);
54 }
54 }
55
55
56 void SplineChartItem::setLinePen(const QPen& pen)
56 void SplineChartItem::setLinePen(const QPen& pen)
57 {
57 {
58 m_pen = pen;
58 m_pen = pen;
59 }
59 }
60
60
61 //handlers
61 //handlers
62
62
63 void SplineChartItem::handleUpdated()
63 void SplineChartItem::handleUpdated()
64 {
64 {
65 //m_items.setVisible(m_series->pointsVisible());
65 //m_items.setVisible(m_series->pointsVisible());
66 setLinePen(m_series->pen());
66 setLinePen(m_series->pen());
67 update();
67 update();
68 }
68 }
69
69
70 //painter
70 //painter
71
71
72 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
72 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
73 {
73 {
74 Q_UNUSED(widget);
74 Q_UNUSED(widget);
75 Q_UNUSED(option);
75 Q_UNUSED(option);
76 painter->save();
76 painter->save();
77 painter->setClipRect(clipRect());
77 painter->setClipRect(clipRect());
78 painter->setPen(m_pen);
78 painter->setPen(m_pen);
79 painter->drawPath(m_path);
79 painter->drawPath(m_path);
80
80
81 const QVector<QPointF> points = XYChartItem::points();
81 const QVector<QPointF> points = XYChartItem::points();
82
82
83 for (int i = 0; i < points.size() - 1; i++)
83 for (int i = 0; i < points.size() - 1; i++)
84 {
84 {
85 painter->setPen(Qt::red);
85 painter->setPen(Qt::red);
86 painter->drawEllipse(points[i], 2, 2);
86 painter->drawEllipse(points[i], 2, 2);
87
87
88 painter->setPen(Qt::blue);
88 painter->setPen(Qt::blue);
89 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
89 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
90 // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1));
90 // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1));
91 // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
91 // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
92 // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
92 // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
93 }
93 }
94 if (points.count() > 0)
94 if (points.count() > 0)
95 {
95 {
96 painter->setPen(Qt::red);
96 painter->setPen(Qt::red);
97 painter->drawEllipse(points[points.count() - 1], 2, 2);
97 painter->drawEllipse(points[points.count() - 1], 2, 2);
98 }
98 }
99 painter->restore();
99 painter->restore();
100 }
100 }
101
101
102
102
103
103
104 #include "moc_splinechartitem_p.cpp"
104 #include "moc_splinechartitem_p.cpp"
105
105
106 QTCOMMERCIALCHART_END_NAMESPACE
106 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,43 +1,43
1 #ifndef SPLINECHARTITEM_P_H
1 #ifndef SPLINECHARTITEM_P_H
2 #define SPLINECHARTITEM_P_H
2 #define SPLINECHARTITEM_P_H
3
3
4 #include "qsplineseries.h"
4 #include "qsplineseries.h"
5 #include "xychartitem_p.h"
5 #include "xychartitem_p.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class SplineChartItem : public XYChartItem
10 class SplineChartItem : public XYChartItem
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 SplineChartItem(QSplineSeries* series, QGraphicsItem *parent = 0);
14 SplineChartItem(QSplineSeries* series, QGraphicsItem *parent = 0);
15
15
16 //from QGraphicsItem
16 //from QGraphicsItem
17 QRectF boundingRect() const;
17 QRectF boundingRect() const;
18 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
18 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
19 QPainterPath shape() const;
19 QPainterPath shape() const;
20
20
21 void setLinePen(const QPen& pen);
21 void setLinePen(const QPen& pen);
22 void setPointsVisible(bool visible);
22 void setPointsVisible(bool visible);
23
23
24 public slots:
24 public slots:
25 void handleUpdated();
25 void handleUpdated();
26
26
27 protected:
27 protected:
28 void setGeometry(QVector<QPointF>& points);
28 void setLayout(QVector<QPointF>& points);
29
29
30 private:
30 private:
31 QPointF calculateGeometryControlPoint(int index) const;
31 QPointF calculateGeometryControlPoint(int index) const;
32
32
33 private:
33 private:
34 QSplineSeries* m_series;
34 QSplineSeries* m_series;
35 QPainterPath m_path;
35 QPainterPath m_path;
36 QRectF m_rect;
36 QRectF m_rect;
37 QPen m_pen;
37 QPen m_pen;
38
38
39 };
39 };
40
40
41 QTCOMMERCIALCHART_END_NAMESPACE
41 QTCOMMERCIALCHART_END_NAMESPACE
42
42
43 #endif // SPLINECHARTITEM_P_H
43 #endif // SPLINECHARTITEM_P_H
@@ -1,156 +1,156
1 #include "xychartitem_p.h"
1 #include "xychartitem_p.h"
2 #include "qxyseries.h"
2 #include "qxyseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartanimator_p.h"
4 #include "chartanimator_p.h"
5 #include <QPainter>
5 #include <QPainter>
6
6
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 //TODO: optimize : remove points which are not visible
10 //TODO: optimize : remove points which are not visible
11
11
12 XYChartItem::XYChartItem(QXYSeries* series, QGraphicsItem *parent):ChartItem(parent),
12 XYChartItem::XYChartItem(QXYSeries* series, QGraphicsItem *parent):ChartItem(parent),
13 m_minX(0),
13 m_minX(0),
14 m_maxX(0),
14 m_maxX(0),
15 m_minY(0),
15 m_minY(0),
16 m_maxY(0),
16 m_maxY(0),
17 m_series(series)
17 m_series(series)
18 {
18 {
19 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
19 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
20 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
20 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
21 QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
21 QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
22
22
23 }
23 }
24
24
25 QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const
25 QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const
26 {
26 {
27 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
27 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
28 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
28 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
29 qreal x = (point.x() - m_minX)* deltaX;
29 qreal x = (point.x() - m_minX)* deltaX;
30 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
30 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
31 return QPointF(x,y);
31 return QPointF(x,y);
32 }
32 }
33
33
34
34
35 QPointF XYChartItem::calculateGeometryPoint(int index) const
35 QPointF XYChartItem::calculateGeometryPoint(int index) const
36 {
36 {
37 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
37 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
38 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
38 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
39 qreal x = (m_series->x(index) - m_minX)* deltaX;
39 qreal x = (m_series->x(index) - m_minX)* deltaX;
40 qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height();
40 qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height();
41 return QPointF(x,y);
41 return QPointF(x,y);
42 }
42 }
43
43
44 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
44 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
45 {
45 {
46 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
46 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
47 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
47 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
48
48
49 QVector<QPointF> points;
49 QVector<QPointF> points;
50 points.reserve(m_series->count());
50 points.reserve(m_series->count());
51 for (int i = 0; i < m_series->count(); ++i) {
51 for (int i = 0; i < m_series->count(); ++i) {
52 qreal x = (m_series->x(i) - m_minX)* deltaX;
52 qreal x = (m_series->x(i) - m_minX)* deltaX;
53 qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height();
53 qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height();
54 points << QPointF(x,y);
54 points << QPointF(x,y);
55 }
55 }
56 return points;
56 return points;
57 }
57 }
58
58
59 QPointF XYChartItem::calculateDomainPoint(const QPointF& point) const
59 QPointF XYChartItem::calculateDomainPoint(const QPointF& point) const
60 {
60 {
61 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
61 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
62 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
62 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
63 qreal x = point.x()/deltaX +m_minX;
63 qreal x = point.x()/deltaX +m_minX;
64 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
64 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
65 return QPointF(x,y);
65 return QPointF(x,y);
66 }
66 }
67
67
68 void XYChartItem::updatePoints(QVector<QPointF>& points)
68 void XYChartItem::applyLayout(QVector<QPointF>& points)
69 {
69 {
70 if(m_animator){
70 if(m_animator){
71 m_animator->applyLayout(this,points);
71 m_animator->applyLayout(this,points);
72 }
72 }
73 else setGeometry(points);
73 else setLayout(points);
74
74
75 }
75 }
76
76
77 void XYChartItem::updatePoint(QVector<QPointF>& points)
77 void XYChartItem::updateLayout(QVector<QPointF>& points)
78 {
78 {
79 setGeometry(points);
79 setLayout(points);
80 }
80 }
81
81
82 void XYChartItem::setGeometry(QVector<QPointF>& points)
82 void XYChartItem::setLayout(QVector<QPointF>& points)
83 {
83 {
84 m_points = points;
84 m_points = points;
85 }
85 }
86
86
87 //handlers
87 //handlers
88
88
89 void XYChartItem::handlePointAdded(int index)
89 void XYChartItem::handlePointAdded(int index)
90 {
90 {
91 Q_ASSERT(index<m_series->count());
91 Q_ASSERT(index<m_series->count());
92 Q_ASSERT(index>=0);
92 Q_ASSERT(index>=0);
93
93
94 QPointF point = calculateGeometryPoint(index);
94 QPointF point = calculateGeometryPoint(index);
95 QVector<QPointF> points = m_points;
95 QVector<QPointF> points = m_points;
96 points.insert(index,point);
96 points.insert(index,point);
97 updatePoints(points);
97 updateLayout(points);
98 update();
98 update();
99 }
99 }
100 void XYChartItem::handlePointRemoved(int index)
100 void XYChartItem::handlePointRemoved(int index)
101 {
101 {
102 Q_ASSERT(index<m_series->count() + 1);
102 Q_ASSERT(index<m_series->count() + 1);
103 Q_ASSERT(index>=0);
103 Q_ASSERT(index>=0);
104 // QPointF point = calculateGeometryPoint(index);
104 // QPointF point = calculateGeometryPoint(index);
105 QVector<QPointF> points = m_points;
105 QVector<QPointF> points = m_points;
106 points.remove(index);
106 points.remove(index);
107 updatePoints(points);
107 updateLayout(points);
108 update();
108 update();
109 }
109 }
110
110
111 void XYChartItem::handlePointReplaced(int index)
111 void XYChartItem::handlePointReplaced(int index)
112 {
112 {
113 Q_ASSERT(index<m_series->count());
113 Q_ASSERT(index<m_series->count());
114 Q_ASSERT(index>=0);
114 Q_ASSERT(index>=0);
115 QPointF point = calculateGeometryPoint(index);
115 QPointF point = calculateGeometryPoint(index);
116 QVector<QPointF> points = m_points;
116 QVector<QPointF> points = m_points;
117 points.replace(index,point);
117 points.replace(index,point);
118 updatePoint(points);
118 updateLayout(points);
119 update();
119 update();
120 }
120 }
121
121
122 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
122 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
123 {
123 {
124 m_minX=minX;
124 m_minX=minX;
125 m_maxX=maxX;
125 m_maxX=maxX;
126 m_minY=minY;
126 m_minY=minY;
127 m_maxY=maxY;
127 m_maxY=maxY;
128
128
129 if(isEmpty()) return;
129 if(isEmpty()) return;
130 QVector<QPointF> points = calculateGeometryPoints();
130 QVector<QPointF> points = calculateGeometryPoints();
131 updatePoints(points);
131 applyLayout(points);
132 update();
132 update();
133 }
133 }
134
134
135 void XYChartItem::handleGeometryChanged(const QRectF& rect)
135 void XYChartItem::handleGeometryChanged(const QRectF& rect)
136 {
136 {
137 Q_ASSERT(rect.isValid());
137 Q_ASSERT(rect.isValid());
138 m_size=rect.size();
138 m_size=rect.size();
139 m_clipRect=rect.translated(-rect.topLeft());
139 m_clipRect=rect.translated(-rect.topLeft());
140 setPos(rect.topLeft());
140 setPos(rect.topLeft());
141
141
142 if(isEmpty()) return;
142 if(isEmpty()) return;
143 QVector<QPointF> points = calculateGeometryPoints();
143 QVector<QPointF> points = calculateGeometryPoints();
144 updatePoints(points);
144 applyLayout(points);
145 update();
145 update();
146 }
146 }
147
147
148
148
149 bool XYChartItem::isEmpty()
149 bool XYChartItem::isEmpty()
150 {
150 {
151 return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ;
151 return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ;
152 }
152 }
153
153
154 #include "moc_xychartitem_p.cpp"
154 #include "moc_xychartitem_p.cpp"
155
155
156 QTCOMMERCIALCHART_END_NAMESPACE
156 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,58 +1,58
1 #ifndef XYCHARTITEM_H
1 #ifndef XYCHARTITEM_H
2 #define XYCHARTITEM_H
2 #define XYCHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "chartitem_p.h"
5 #include "chartitem_p.h"
6 #include <QPen>
6 #include <QPen>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class ChartPresenter;
10 class ChartPresenter;
11 class QXYSeries;
11 class QXYSeries;
12
12
13 class XYChartItem : public QObject , public ChartItem
13 class XYChartItem : public QObject , public ChartItem
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 public:
16 public:
17 explicit XYChartItem(QXYSeries* series, QGraphicsItem *parent = 0);
17 explicit XYChartItem(QXYSeries* series, QGraphicsItem *parent = 0);
18 ~ XYChartItem(){};
18 ~ XYChartItem(){};
19
19
20 QVector<QPointF> points() const {return m_points;}
20 QVector<QPointF> points() const {return m_points;}
21 QRectF clipRect() const { return m_clipRect;}
21 QRectF clipRect() const { return m_clipRect;}
22
22
23 public slots:
23 public slots:
24 void handlePointAdded(int index);
24 void handlePointAdded(int index);
25 void handlePointRemoved(int index);
25 void handlePointRemoved(int index);
26 void handlePointReplaced(int index);
26 void handlePointReplaced(int index);
27 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
27 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
28 void handleGeometryChanged(const QRectF& size);
28 void handleGeometryChanged(const QRectF& size);
29
29
30 protected:
30 protected:
31 virtual void setGeometry(QVector<QPointF>& points);
31 virtual void setLayout(QVector<QPointF>& points);
32 QPointF calculateGeometryPoint(const QPointF& point) const;
32 QPointF calculateGeometryPoint(const QPointF& point) const;
33 QPointF calculateGeometryPoint(int index) const;
33 QPointF calculateGeometryPoint(int index) const;
34 QPointF calculateDomainPoint(const QPointF& point) const;
34 QPointF calculateDomainPoint(const QPointF& point) const;
35 QVector<QPointF> calculateGeometryPoints() const;
35 QVector<QPointF> calculateGeometryPoints() const;
36
36
37 private:
37 private:
38 void updatePoints(QVector<QPointF>& points);
38 void applyLayout(QVector<QPointF>& points);
39 void updatePoint(QVector<QPointF>& points);
39 void updateLayout(QVector<QPointF>& points);
40 inline bool isEmpty();
40 inline bool isEmpty();
41
41
42 private:
42 private:
43 qreal m_minX;
43 qreal m_minX;
44 qreal m_maxX;
44 qreal m_maxX;
45 qreal m_minY;
45 qreal m_minY;
46 qreal m_maxY;
46 qreal m_maxY;
47 QXYSeries* m_series;
47 QXYSeries* m_series;
48 QSizeF m_size;
48 QSizeF m_size;
49 QRectF m_clipRect;
49 QRectF m_clipRect;
50 QVector<QPointF> m_points;
50 QVector<QPointF> m_points;
51
51
52 friend class XYAnimation;
52 friend class XYAnimation;
53
53
54 };
54 };
55
55
56 QTCOMMERCIALCHART_END_NAMESPACE
56 QTCOMMERCIALCHART_END_NAMESPACE
57
57
58 #endif
58 #endif
General Comments 0
You need to be logged in to leave comments. Login now