##// END OF EJS Templates
Refactor animation to fit line,spline,scatter...
Michal Klocek -
r476:b6fb05e4e56f
parent child
Show More
@@ -0,0 +1,100
1 #ifndef XYCHARTANIMATIONITEM_P_H_
2 #define XYCHARTANIMATIONITEM_P_H_
3 #include "qchartglobal.h"
4 #include "xychartanimator_p.h"
5 #include <QPointF>
6 #include <QTimer>
7 #include <QGraphicsItem>
8
9 Q_DECLARE_METATYPE(QVector<QPointF>)
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
13
14 const static int duration = 1000;
15
16 template <class T , class U>
17 class XYChartAnimationItem : public T {
18
19 public:
20 XYChartAnimationItem(U *series, QGraphicsItem *parent = 0);
21 virtual ~XYChartAnimationItem();
22
23 void animationStarted();
24
25 protected:
26 virtual void updatePoints(QVector<QPointF>& newPoints);
27 virtual void updatePoint(QVector<QPointF>& newPoints);
28
29 private:
30 XYChartAnimator<T,U> *m_animation;
31 QVector<QPointF> m_points;
32 bool m_dirty;
33 };
34
35 template <class T, class U>
36 XYChartAnimationItem<T,U>::XYChartAnimationItem(U *series,QGraphicsItem *parent):
37 T(series,parent),
38 m_animation(new XYChartAnimator<T,U>(this,this)),
39 m_dirty(false)
40 {
41 }
42
43 template <class T, class U>
44 XYChartAnimationItem<T,U>::~XYChartAnimationItem()
45 {
46 }
47
48 template <class T, class U>
49 void XYChartAnimationItem<T,U>::updatePoints(QVector<QPointF>& newPoints)
50 {
51 QVector<QPointF> oldPoints = T::points();
52
53 if(newPoints.count()==0) return;
54 oldPoints.resize(newPoints.size());
55
56 if(m_animation->state()!=QAbstractAnimation::Stopped){
57 m_animation->stop();
58 }
59
60 m_animation->setDuration(duration);
61 m_animation->setEasingCurve(QEasingCurve::InOutBack);
62 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldPoints));
63 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
64 QTimer::singleShot(0,m_animation,SLOT(start()));
65
66 m_points = newPoints;
67 m_dirty=false;
68 }
69
70 template <class T, class U>
71 void XYChartAnimationItem<T,U>::updatePoint(QVector<QPointF>& newPoints)
72 {
73
74 if(m_animation->state()!=QAbstractAnimation::Stopped) {
75 m_animation->stop();
76 m_dirty=true;
77 }
78
79 if(m_dirty) {
80 m_points=newPoints;
81 m_dirty=false;
82 }
83
84 m_animation->setDuration(duration);
85 m_animation->setEasingCurve(QEasingCurve::InOutBack);
86 m_animation->setKeyValueAt(0.0, qVariantFromValue(m_points));
87 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
88
89 QTimer::singleShot(0,m_animation,SLOT(start()));
90 }
91
92 template <class T, class U>
93 void XYChartAnimationItem<T,U>::animationStarted()
94 {
95 m_dirty=true;
96 }
97
98 QTCOMMERCIALCHART_END_NAMESPACE
99
100 #endif
@@ -0,0 +1,96
1 #ifndef XYCHARTANIMATOR_P_H_
2 #define XYCHARTANIMATOR_P_H_
3 #include "qchartglobal.h"
4 #include <QVariantAnimation>
5 #include <QPointF>
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9 template <class T, class U>
10 class XYChartAnimationItem;
11
12
13 template <class T, class U>
14 class XYChartAnimator : public QVariantAnimation
15 {
16 public:
17 enum Animation { LineDrawAnimation, MoveDownAnimation, MoveUpAnimation };
18 XYChartAnimator(XYChartAnimationItem<T,U> *item, QObject *parent = 0 );
19 ~XYChartAnimator();
20
21 protected:
22 QVariant interpolated(const QVariant &start, const QVariant & end, qreal progress ) const;
23 void updateCurrentValue (const QVariant & value );
24 void updateState ( QAbstractAnimation::State newState, QAbstractAnimation::State oldState);
25
26 private:
27 XYChartAnimationItem<T,U> *m_item;
28 Animation m_type;
29 };
30
31 template <class T, class U>
32 XYChartAnimator<T,U>::XYChartAnimator(XYChartAnimationItem<T,U> *item , QObject *parent):QVariantAnimation(parent),
33 m_item(item),
34 m_type(MoveDownAnimation)
35 {
36 }
37
38 template <class T,class U>
39 XYChartAnimator<T,U>::~XYChartAnimator()
40 {
41 }
42
43 template <class T, class U>
44 QVariant XYChartAnimator<T,U>::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
45 {
46 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
47 QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end);
48 QVector<QPointF> result;
49
50 switch(m_type) {
51
52 case MoveDownAnimation: {
53
54 Q_ASSERT(startVector.count() == endVector.count());
55 for(int i =0;i< startVector.count();i++) {
56 qreal x = startVector[i].x() + ((endVector[i].x()- startVector[i].x()) * progress);
57 qreal y = startVector[i].y() + ((endVector[i].y()- startVector[i].y()) * progress);
58 result << QPointF(x,y);
59 }
60
61 }
62 break;
63 case LineDrawAnimation:{
64 for(int i =0;i< endVector.count()* qBound(0.0, progress, 1.0);i++) {
65 result << endVector[i];
66 }
67 }
68 break;
69 default:
70 qWarning()<<"Unknow type of animation";
71 break;
72 }
73
74 return qVariantFromValue(result);
75 }
76
77 template <class T, class U>
78 void XYChartAnimator<T,U>::updateCurrentValue (const QVariant & value )
79 {
80 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
81 if(state()!=QAbstractAnimation::Stopped){ //workaround
82 m_item->setGeometry(vector);
83 }
84 }
85
86 template <class T, class U>
87 void XYChartAnimator<T,U>::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
88 {
89 Q_UNUSED(oldState)
90 if (newState==QAbstractAnimation::Running) m_item->animationStarted();
91 QVariantAnimation::updateState(newState,oldState);
92 }
93
94 QTCOMMERCIALCHART_END_NAMESPACE
95
96 #endif /* XYCHARTANIMATOR_P_H_ */
@@ -1,10 +1,14
1 #include "chartview.h"
1 #include "chartview.h"
2 #include <qlineseries.h>
2 #include <qlineseries.h>
3 #include <qscatterseries.h>
4 #include <qsplineseries.h>
3 #include <QTime>
5 #include <QTime>
4
6
5 ChartView::ChartView(QWidget* parent):QChartView(parent),
7 ChartView::ChartView(QWidget* parent):QChartView(parent),
6 m_index(0)
8 m_index(0)
7 {
9 {
10 setChartTitle("Three random line charts");
11
8 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
12 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
9 m_timer.setInterval(3000);
13 m_timer.setInterval(3000);
10
14
@@ -15,11 +19,12 m_index(0)
15 QPen blue(Qt::blue);
19 QPen blue(Qt::blue);
16 blue.setWidth(3);
20 blue.setWidth(3);
17 series0->setPen(blue);
21 series0->setPen(blue);
18 QLineSeries* series1 = new QLineSeries(this);
22 QScatterSeries* series1 = new QScatterSeries(this);
19 QPen red(Qt::red);
23 QPen red(Qt::red);
20 red.setWidth(3);
24 red.setWidth(3);
21 series1->setPen(red);
25 series1->setPen(red);
22 QLineSeries* series2 = new QLineSeries(this);
26 series1->setBrush(Qt::white);
27 QSplineSeries* series2 = new QSplineSeries(this);
23 QPen green(Qt::green);
28 QPen green(Qt::green);
24 green.setWidth(3);
29 green.setWidth(3);
25 series2->setPen(green);
30 series2->setPen(green);
@@ -27,16 +32,21 m_index(0)
27 int numPoints = 10;
32 int numPoints = 10;
28
33
29 for (int x = 0; x <= numPoints; ++x) {
34 for (int x = 0; x <= numPoints; ++x) {
30 series0->add(x, qrand() % 100);
35 qreal y = qrand() % 100;
31 series1->add(x, qrand() % 100);
36 series0->add(x,y);
32 series2->add(x, qrand() % 100);
37 series1->add(x,y);
38 series2->add(x,y);
33 }
39 }
34
40
35 addSeries(series0);
36
37 m_series<<series0;
41 m_series<<series0;
42 m_titles<<chartTitle()+": LineChart";
38 m_series<<series1;
43 m_series<<series1;
44 m_titles<<chartTitle()+": ScatterChart";
39 m_series<<series2;
45 m_series<<series2;
46 m_titles<<chartTitle()+": SplineChart";
47
48 addSeries(series0);
49 setChartTitle(m_titles.at(0));
40
50
41 m_timer.start();
51 m_timer.start();
42 }
52 }
@@ -56,4 +66,5 void ChartView::handleTimeout()
56 m_index++;
66 m_index++;
57 m_index=m_index%m_series.size();
67 m_index=m_index%m_series.size();
58 addSeries(m_series.at(m_index));
68 addSeries(m_series.at(m_index));
69 setChartTitle(m_titles.at(m_index));
59 }
70 }
@@ -19,6 +19,7 public slots:
19 private:
19 private:
20 QTimer m_timer;
20 QTimer m_timer;
21 QList<QSeries*> m_series;
21 QList<QSeries*> m_series;
22 QStringList m_titles;
22 int m_index;
23 int m_index;
23 };
24 };
24
25
@@ -8,7 +8,6 int main(int argc, char *argv[])
8 QMainWindow window;
8 QMainWindow window;
9 ChartView chartView(&window);
9 ChartView chartView(&window);
10 chartView.setRenderHint(QPainter::Antialiasing);
10 chartView.setRenderHint(QPainter::Antialiasing);
11 chartView.setChartTitle("Three random line charts");
12 chartView.setAnimationOptions(QChart::AllAnimations);
11 chartView.setAnimationOptions(QChart::AllAnimations);
13 window.setCentralWidget(&chartView);
12 window.setCentralWidget(&chartView);
14 window.resize(400, 300);
13 window.resize(400, 300);
@@ -20,7 +20,6
20 #include "stackedbarpresenter_p.h"
20 #include "stackedbarpresenter_p.h"
21 #include "percentbarpresenter_p.h"
21 #include "percentbarpresenter_p.h"
22 #include "linechartitem_p.h"
22 #include "linechartitem_p.h"
23 #include "linechartanimationitem_p.h"
24 #include "piepresenter_p.h"
23 #include "piepresenter_p.h"
25 #include "scatterchartitem_p.h"
24 #include "scatterchartitem_p.h"
26 #include "splinechartitem_p.h"
25 #include "splinechartitem_p.h"
@@ -216,16 +215,24 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
216 }
215 }
217 case QSeries::SeriesTypeScatter: {
216 case QSeries::SeriesTypeScatter: {
218 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
217 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
219 ScatterChartItem *scatterPresenter = new ScatterChartItem(scatterSeries, m_chart);
218 ScatterChartItem *item;
219 if(m_options.testFlag(QChart::SeriesAnimations)) {
220 item = new ScatterChartAnimationItem(scatterSeries,m_chart);
221 } else {
222 item = new ScatterChartItem(scatterSeries, m_chart);
223 }
220 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
224 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
221 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
225 item, SLOT(handleGeometryChanged(const QRectF&)));
222 QObject::connect(domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),
226 QObject::connect(domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),
223 scatterPresenter, SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
227 item, SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
224 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
228 //initialize
225 m_chartItems.insert(scatterSeries, scatterPresenter);
226 if (m_rect.isValid())
229 if (m_rect.isValid())
227 scatterPresenter->handleGeometryChanged(m_rect);
230 item->handleGeometryChanged(m_rect);
228 scatterPresenter->handleDomainChanged(domain->minX(), domain->maxX(), domain->minY(), domain->maxY());
231 item->handleDomainChanged(domain->minX(), domain->maxX(), domain->minY(), domain->maxY());
232 //decorate
233 m_chartTheme->decorate(item, scatterSeries, m_chartItems.count());
234 m_chartItems.insert(scatterSeries, item);
235
229 break;
236 break;
230 }
237 }
231 case QSeries::SeriesTypePie: {
238 case QSeries::SeriesTypePie: {
@@ -254,14 +261,22 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
254 }
261 }
255
262
256 case QSeries::SeriesTypeSpline: {
263 case QSeries::SeriesTypeSpline: {
264
257 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(series);
265 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(series);
258 SplineChartItem* splinePresenter = new SplineChartItem(splineSeries, m_chart);
266 SplineChartItem* item;
259 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), splinePresenter, SLOT(handleGeometryChanged(const QRectF&)));
267 if(m_options.testFlag(QChart::SeriesAnimations)) {
260 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),splinePresenter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
268 item = new SplineChartAnimationItem(splineSeries, m_chart);
269 } else {
270 item = new SplineChartItem(splineSeries, m_chart);
271 }
272 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), item, SLOT(handleGeometryChanged(const QRectF&)));
273 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
261 //initialize
274 //initialize
262 splinePresenter->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
275 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
263 m_chartTheme->decorate(splinePresenter, splineSeries, m_chartItems.count());
276 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
264 m_chartItems.insert(splineSeries, splinePresenter);
277 //decorate
278 m_chartTheme->decorate(item, splineSeries, m_chartItems.count());
279 m_chartItems.insert(splineSeries, item);
265 break;
280 break;
266 }
281 }
267 default: {
282 default: {
@@ -165,12 +165,12 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count)
165 {
165 {
166 QPen pen;
166 QPen pen;
167 if(pen != series->pen()){
167 if(pen != series->pen()){
168 item->setPen(series->pen());
168 item->setLinePen(series->pen());
169 return;
169 return;
170 }
170 }
171 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
171 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
172 pen.setWidthF(2);
172 pen.setWidthF(2);
173 item->setPen(pen);
173 item->setLinePen(pen);
174 }
174 }
175
175
176 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count)
176 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count)
@@ -305,13 +305,14 void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int coun
305 Q_ASSERT(series);
305 Q_ASSERT(series);
306
306
307 QPen pen;
307 QPen pen;
308
308 if(pen != series->pen()){
309 if(pen != series->pen()){
309 item->setPen(series->pen());
310 item->setLinePen(series->pen());
310 return;
311 }else{
311 }
312 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
312 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
313 pen.setWidthF(series->pen().widthF());
313 pen.setWidthF(series->pen().widthF());
314 item->setPen(pen);
314 item->setLinePen(series->pen());
315 }
315
316
316 // QColor color = m_seriesColor.at(count % m_seriesColor.size());
317 // QColor color = m_seriesColor.at(count % m_seriesColor.size());
317 // TODO: define alpha in the theme? or in the series?
318 // TODO: define alpha in the theme? or in the series?
@@ -2,13 +2,11 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
2 DEPENDPATH += $$PWD
3
3
4 SOURCES += \
4 SOURCES += \
5 $$PWD/linechartanimationitem.cpp \
6 $$PWD/linechartitem.cpp \
5 $$PWD/linechartitem.cpp \
7 $$PWD/qlineseries.cpp
6 $$PWD/qlineseries.cpp
8
7
9 PRIVATE_HEADERS += \
8 PRIVATE_HEADERS += \
10 $$PWD/linechartitem_p.h \
9 $$PWD/linechartitem_p.h
11 $$PWD/linechartanimationitem_p.h
12
10
13 PUBLIC_HEADERS += \
11 PUBLIC_HEADERS += \
14 $$PWD/qlineseries.h No newline at end of file
12 $$PWD/qlineseries.h
@@ -38,7 +38,6 void LineChartAnimationItem::updatePoints(QVector<QPointF>& newPoints)
38 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
38 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
39 QTimer::singleShot(0,m_animation,SLOT(start()));
39 QTimer::singleShot(0,m_animation,SLOT(start()));
40
40
41
42 m_points = newPoints;
41 m_points = newPoints;
43 m_dirty=false;
42 m_dirty=false;
44
43
@@ -93,16 +93,15 void LineChartItem::setGeometry(QVector<QPointF>& points)
93 XYChartItem::setGeometry(points);
93 XYChartItem::setGeometry(points);
94 }
94 }
95
95
96 void LineChartItem::setPen(const QPen& pen)
96 void LineChartItem::setLinePen(const QPen& pen)
97 {
97 {
98 m_pen = pen;
98 m_pen = pen;
99 }
99 }
100
100
101
102 void LineChartItem::handleUpdated()
101 void LineChartItem::handleUpdated()
103 {
102 {
104 m_items.setVisible(m_series->pointsVisible());
103 m_items.setVisible(m_series->pointsVisible());
105 setPen(m_series->pen());
104 setLinePen(m_series->pen());
106 update();
105 update();
107 }
106 }
108
107
@@ -3,6 +3,7
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "xychartitem_p.h"
5 #include "xychartitem_p.h"
6 #include "xychartanimationitem_p.h"
6 #include <QPen>
7 #include <QPen>
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -22,7 +23,7 public:
22 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 QPainterPath shape() const;
24 QPainterPath shape() const;
24
25
25 void setPen(const QPen& pen);
26 void setLinePen(const QPen& pen);
26 void setPointsVisible(bool visible);
27 void setPointsVisible(bool visible);
27
28
28 public slots:
29 public slots:
@@ -42,10 +43,12 private:
42 QRectF m_rect;
43 QRectF m_rect;
43 QPen m_pen;
44 QPen m_pen;
44
45
45 friend class LineChartAnimatator;
46 template<class,class> friend class XYChartAnimator;
46
47
47 };
48 };
48
49
50 typedef XYChartAnimationItem<LineChartItem,QLineSeries> LineChartAnimationItem;
51
49 QTCOMMERCIALCHART_END_NAMESPACE
52 QTCOMMERCIALCHART_END_NAMESPACE
50
53
51 #endif
54 #endif
@@ -117,7 +117,18 void QChart::setChartBackgroundPen(const QPen& pen)
117 void QChart::setChartTitle(const QString& title)
117 void QChart::setChartTitle(const QString& title)
118 {
118 {
119 createChartTitleItem();
119 createChartTitleItem();
120 m_titleItem->setPlainText(title);
120 m_titleItem->setText(title);
121 }
122
123 /*!
124 Gets the chart \a title. The description text that is rendered above the chart.
125 */
126 QString QChart::chartTitle() const
127 {
128 if(m_titleItem)
129 return m_titleItem->text();
130 else
131 return QString();
121 }
132 }
122
133
123 /*!
134 /*!
@@ -141,7 +152,7 void QChart::createChartBackgroundItem()
141 void QChart::createChartTitleItem()
152 void QChart::createChartTitleItem()
142 {
153 {
143 if(!m_titleItem) {
154 if(!m_titleItem) {
144 m_titleItem = new QGraphicsTextItem(this);
155 m_titleItem = new QGraphicsSimpleTextItem(this);
145 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
156 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
146 }
157 }
147 }
158 }
@@ -57,6 +57,7 public:
57 QChart::ChartTheme chartTheme() const;
57 QChart::ChartTheme chartTheme() const;
58
58
59 void setChartTitle(const QString& title);
59 void setChartTitle(const QString& title);
60 QString chartTitle() const;
60 void setChartTitleFont(const QFont& font);
61 void setChartTitleFont(const QFont& font);
61 void setChartBackgroundBrush(const QBrush& brush);
62 void setChartBackgroundBrush(const QBrush& brush);
62 void setChartBackgroundPen(const QPen& pen);
63 void setChartBackgroundPen(const QPen& pen);
@@ -82,7 +83,7 private:
82 private:
83 private:
83 Q_DISABLE_COPY(QChart)
84 Q_DISABLE_COPY(QChart)
84 QGraphicsRectItem* m_backgroundItem;
85 QGraphicsRectItem* m_backgroundItem;
85 QGraphicsTextItem* m_titleItem;
86 QGraphicsSimpleTextItem* m_titleItem;
86 QRectF m_rect;
87 QRectF m_rect;
87 ChartDataSet *m_dataset;
88 ChartDataSet *m_dataset;
88 ChartPresenter *m_presenter;
89 ChartPresenter *m_presenter;
@@ -142,6 +142,14 void QChartView::setChartTitle(const QString& title)
142 }
142 }
143
143
144 /*!
144 /*!
145 Gets the chart \a title. A description text that is rendered above the chart.
146 */
147 QString QChartView::chartTitle() const
148 {
149 return m_chart->chartTitle();
150 }
151
152 /*!
145 Sets the \a font that is used for rendering the description text that is rendered above the chart.
153 Sets the \a font that is used for rendering the description text that is rendered above the chart.
146 */
154 */
147 void QChartView::setChartTitleFont(const QFont& font)
155 void QChartView::setChartTitleFont(const QFont& font)
@@ -30,6 +30,7 public:
30 int margin() const;
30 int margin() const;
31
31
32 void setChartTitle(const QString& title);
32 void setChartTitle(const QString& title);
33 QString chartTitle() const;
33 void setChartTitleFont(const QFont& font);
34 void setChartTitleFont(const QFont& font);
34 void setChartBackgroundBrush(const QBrush& brush);
35 void setChartBackgroundBrush(const QBrush& brush);
35 void setChartBackgroundPen(const QPen& pen);
36 void setChartBackgroundPen(const QPen& pen);
@@ -6,7 +6,7
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsObject *parent) :
9 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) :
10 XYChartItem(series,parent),
10 XYChartItem(series,parent),
11 m_series(series),
11 m_series(series),
12 m_items(this),
12 m_items(this),
@@ -3,7 +3,8
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "xychartitem_p.h"
5 #include "xychartitem_p.h"
6 #include <QObject>
6 #include "xychartanimationitem_p.h"
7 #include <QGraphicsItem>
7 #include <QPen>
8 #include <QPen>
8
9
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -14,7 +15,7 class ScatterChartItem : public XYChartItem
14 {
15 {
15 Q_OBJECT
16 Q_OBJECT
16 public:
17 public:
17 explicit ScatterChartItem(QScatterSeries *series, QGraphicsObject *parent = 0);
18 explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent = 0);
18
19
19 public:
20 public:
20 //from QGraphicsItem
21 //from QGraphicsItem
@@ -43,8 +44,12 private:
43 int m_shape;
44 int m_shape;
44 int m_size;
45 int m_size;
45 QRectF m_rect;
46 QRectF m_rect;
47
48 template<class,class> friend class XYChartAnimator;
46 };
49 };
47
50
51 typedef XYChartAnimationItem<ScatterChartItem,QScatterSeries> ScatterChartAnimationItem;
52
48 QTCOMMERCIALCHART_END_NAMESPACE
53 QTCOMMERCIALCHART_END_NAMESPACE
49
54
50 #endif // SCATTERPRESENTER_H
55 #endif // SCATTERPRESENTER_H
@@ -1,12 +1,16
1 #include "splinechartitem_p.h"
1 #include "splinechartitem_p.h"
2 #include "chartpresenter_p.h"
2 #include <QPainter>
3 #include <QPainter>
3
4
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
6
6 SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsObject *parent) :
7 SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsItem *parent) :
7 XYChartItem(series, parent),
8 XYChartItem(series, parent),
8 m_series(series)
9 m_series(series)
9 {
10 {
11 setZValue(ChartPresenter::LineChartZValue);
12 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
13 handleUpdated();
10 }
14 }
11
15
12 QRectF SplineChartItem::boundingRect() const
16 QRectF SplineChartItem::boundingRect() const
@@ -26,6 +30,7 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
26
30
27 void SplineChartItem::setGeometry(QVector<QPointF>& points)
31 void SplineChartItem::setGeometry(QVector<QPointF>& points)
28 {
32 {
33
29 if(points.size()==0) return;
34 if(points.size()==0) return;
30
35
31 QPainterPath splinePath;
36 QPainterPath splinePath;
@@ -44,11 +49,22 void SplineChartItem::setGeometry(QVector<QPointF>& points)
44 XYChartItem::setGeometry(points);
49 XYChartItem::setGeometry(points);
45 }
50 }
46
51
47 void SplineChartItem::setPen(const QPen& pen)
52 void SplineChartItem::setLinePen(const QPen& pen)
48 {
53 {
49 m_pen = pen;
54 m_pen = pen;
55 qDebug()<<pen;
50 }
56 }
51
57
58 //handlers
59
60 void SplineChartItem::handleUpdated()
61 {
62 //m_items.setVisible(m_series->pointsVisible());
63 setLinePen(m_series->pen());
64 update();
65 }
66
67 //painter
52
68
53 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
69 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
54 {
70 {
@@ -56,6 +72,7 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
56 Q_UNUSED(option);
72 Q_UNUSED(option);
57 painter->save();
73 painter->save();
58 painter->setClipRect(clipRect());
74 painter->setClipRect(clipRect());
75 painter->setPen(m_pen);
59 painter->drawPath(m_path);
76 painter->drawPath(m_path);
60
77
61 const QVector<QPointF> points = XYChartItem::points();
78 const QVector<QPointF> points = XYChartItem::points();
@@ -79,6 +96,8 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
79 painter->restore();
96 painter->restore();
80 }
97 }
81
98
99
100
82 #include "moc_splinechartitem_p.cpp"
101 #include "moc_splinechartitem_p.cpp"
83
102
84 QTCOMMERCIALCHART_END_NAMESPACE
103 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,10 +1,10
1 #ifndef SPLINECHARTITEM_P_H
1 #ifndef SPLINECHARTITEM_P_H
2 #define SPLINECHARTITEM_P_H
2 #define SPLINECHARTITEM_P_H
3
3
4 #include "chartitem_p.h"
5 #include <QObject>
6 #include "qsplineseries.h"
4 #include "qsplineseries.h"
7 #include "xychartitem_p.h"
5 #include "xychartitem_p.h"
6 #include "xychartanimationitem_p.h"
7 #include <QGraphicsItem>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
@@ -12,16 +12,19 class SplineChartItem : public XYChartItem
12 {
12 {
13 Q_OBJECT
13 Q_OBJECT
14 public:
14 public:
15 SplineChartItem(QSplineSeries* series, QGraphicsObject *parent = 0);
15 SplineChartItem(QSplineSeries* series, QGraphicsItem *parent = 0);
16
16
17 //from QGraphicsItem
17 //from QGraphicsItem
18 QRectF boundingRect() const;
18 QRectF boundingRect() const;
19 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
19 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
20 QPainterPath shape() const;
20 QPainterPath shape() const;
21
21
22 void setPen(const QPen& pen);
22 void setLinePen(const QPen& pen);
23 void setPointsVisible(bool visible);
23 void setPointsVisible(bool visible);
24
24
25 public slots:
26 void handleUpdated();
27
25 protected:
28 protected:
26 void setGeometry(QVector<QPointF>& points);
29 void setGeometry(QVector<QPointF>& points);
27
30
@@ -33,8 +36,12 private:
33 QPainterPath m_path;
36 QPainterPath m_path;
34 QRectF m_rect;
37 QRectF m_rect;
35 QPen m_pen;
38 QPen m_pen;
39
40 template<class,class> friend class XYChartAnimator;
36 };
41 };
37
42
43 typedef XYChartAnimationItem<SplineChartItem,QSplineSeries> SplineChartAnimationItem;
44
38 QTCOMMERCIALCHART_END_NAMESPACE
45 QTCOMMERCIALCHART_END_NAMESPACE
39
46
40 #endif // SPLINECHARTITEM_P_H
47 #endif // SPLINECHARTITEM_P_H
@@ -7,6 +7,8 SOURCES += \
7
7
8 PRIVATE_HEADERS += \
8 PRIVATE_HEADERS += \
9 $$PWD/xychartitem_p.h \
9 $$PWD/xychartitem_p.h \
10 $$PWD/xychartanimationitem_p.h \
11 $$PWD/xychartanimator_p.h
10
12
11 PUBLIC_HEADERS += \
13 PUBLIC_HEADERS += \
12 $$PWD/qxyseries.h No newline at end of file
14 $$PWD/qxyseries.h
@@ -55,16 +55,14 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
55 return points;
55 return points;
56 }
56 }
57
57
58
59 void XYChartItem::updatePoints(QVector<QPointF>& points)
58 void XYChartItem::updatePoints(QVector<QPointF>& points)
60 {
59 {
61 setGeometry(points);
60 setGeometry(points);
62 }
61 }
63
62
64 void XYChartItem::updatePoint(int index,QPointF& newPoint)
63 void XYChartItem::updatePoint(QVector<QPointF>& points)
65 {
64 {
66 m_points.replace(index,newPoint);
65 setGeometry(points);
67 setGeometry(m_points);
68 }
66 }
69
67
70 void XYChartItem::setGeometry(QVector<QPointF>& points)
68 void XYChartItem::setGeometry(QVector<QPointF>& points)
@@ -89,7 +87,6 void XYChartItem::handlePointRemoved(int index)
89 {
87 {
90 Q_ASSERT(index<m_series->count());
88 Q_ASSERT(index<m_series->count());
91 Q_ASSERT(index>=0);
89 Q_ASSERT(index>=0);
92
93 QPointF point = calculateGeometryPoint(index);
90 QPointF point = calculateGeometryPoint(index);
94 QVector<QPointF> points = m_points;
91 QVector<QPointF> points = m_points;
95 points.remove(index);
92 points.remove(index);
@@ -102,7 +99,9 void XYChartItem::handlePointReplaced(int index)
102 Q_ASSERT(index<m_series->count());
99 Q_ASSERT(index<m_series->count());
103 Q_ASSERT(index>=0);
100 Q_ASSERT(index>=0);
104 QPointF point = calculateGeometryPoint(index);
101 QPointF point = calculateGeometryPoint(index);
105 updatePoint(index,point);
102 QVector<QPointF> points = m_points;
103 m_points.replace(index,point);
104 updatePoint(points);
106 update();
105 update();
107 }
106 }
108
107
@@ -29,8 +29,9 public slots:
29
29
30 protected:
30 protected:
31 virtual void updatePoints(QVector<QPointF>& points);
31 virtual void updatePoints(QVector<QPointF>& points);
32 virtual void updatePoint(int index,QPointF& newPoint);
32 virtual void updatePoint(QVector<QPointF>& points);
33 virtual void setGeometry(QVector<QPointF>& points);
33 virtual void setGeometry(QVector<QPointF>& points);
34
34 QPointF calculateGeometryPoint(const QPointF& point) const;
35 QPointF calculateGeometryPoint(const QPointF& point) const;
35 QPointF calculateGeometryPoint(int index) const;
36 QPointF calculateGeometryPoint(int index) const;
36 QVector<QPointF> calculateGeometryPoints() const;
37 QVector<QPointF> calculateGeometryPoints() const;
@@ -48,6 +49,8 private:
48 QRectF m_clipRect;
49 QRectF m_clipRect;
49 QVector<QPointF> m_points;
50 QVector<QPointF> m_points;
50
51
52 template<class,class> friend class XYChartAnimator;
53
51 };
54 };
52
55
53 QTCOMMERCIALCHART_END_NAMESPACE
56 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now