##// END OF EJS Templates
Refactoring QML impl for series
Tero Ahola -
r846:accf902f8269
parent child
Show More
@@ -1,65 +1,66
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "declarativechart.h"
22 22 #include <QPainter>
23 23
24 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 25
26 26 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
27 27 : QDeclarativeItem(parent),
28 28 m_chart(new QChart(this))
29 29 {
30 m_chart->setAnimationOptions(QChart::SeriesAnimations);
30 31 setFlag(QGraphicsItem::ItemHasNoContents, false);
31 32 }
32 33
33 34 DeclarativeChart::~DeclarativeChart()
34 35 {
35 36 delete m_chart;
36 37 }
37 38
38 39 QChart::ChartTheme DeclarativeChart::theme()
39 40 {
40 41 return m_chart->theme();
41 42 }
42 43
43 44 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
44 45 {
45 46 Q_UNUSED(oldGeometry)
46 47
47 48 if (newGeometry.isValid()) {
48 49 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
49 50 m_chart->resize(newGeometry.width(), newGeometry.height());
50 51 }
51 52 }
52 53 }
53 54
54 55 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
55 56 {
56 57 Q_UNUSED(option)
57 58 Q_UNUSED(widget)
58 59
59 60 // TODO: optimized?
60 61 painter->setRenderHint(QPainter::Antialiasing, true);
61 62 }
62 63
63 64 #include "moc_declarativechart.cpp"
64 65
65 66 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,51 +1,45
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "declarativelineseries.h"
22 22 #include "declarativechart.h"
23 23 #include "qchart.h"
24 24 #include "qlineseries.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 DeclarativeLineSeries::DeclarativeLineSeries(QObject *parent) :
29 29 QLineSeries(parent)
30 30 {
31 31 }
32 32
33 void DeclarativeLineSeries::componentComplete()
33 QSeries *DeclarativeLineSeries::series()
34 34 {
35 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
36
37 if (declarativeChart) {
38 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
39 Q_ASSERT(chart);
40 chart->addSeries(this);
41 }
35 return this;
42 36 }
43 37
44 38 QDeclarativeListProperty<DeclarativeXyPoint> DeclarativeLineSeries::points()
45 39 {
46 40 return QDeclarativeListProperty<DeclarativeXyPoint>(this, 0, &DeclarativeXySeries::appendPoints);
47 41 }
48 42
49 43 #include "moc_declarativelineseries.cpp"
50 44
51 45 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,46 +1,46
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef DECLARATIVELINESERIES_H
22 22 #define DECLARATIVELINESERIES_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "qlineseries.h"
26 26 #include "declarativexyseries.h"
27 27 #include <QDeclarativeParserStatus>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class DeclarativeLineSeries : public QLineSeries, public DeclarativeXySeries
32 32 {
33 33 Q_OBJECT
34 34 Q_PROPERTY(QDeclarativeListProperty<DeclarativeXyPoint> points READ points)
35 35
36 36 public:
37 37 explicit DeclarativeLineSeries(QObject *parent = 0);
38 38
39 39 public:
40 void componentComplete();
40 QSeries *series();
41 41 QDeclarativeListProperty<DeclarativeXyPoint> points();
42 42 };
43 43
44 44 QTCOMMERCIALCHART_END_NAMESPACE
45 45
46 46 #endif // DECLARATIVELINESERIES_H
@@ -1,52 +1,45
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "declarativescatterseries.h"
22 22 #include "declarativechart.h"
23 23 #include "qchart.h"
24 24 #include "qscatterseries.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 DeclarativeScatterSeries::DeclarativeScatterSeries(QObject *parent) :
29 29 QScatterSeries(parent)
30 30 {
31 31 }
32 32
33 void DeclarativeScatterSeries::componentComplete()
33 QSeries *DeclarativeScatterSeries::series()
34 34 {
35 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
36
37 if (declarativeChart) {
38 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
39 Q_ASSERT(chart);
40 qDebug() << "chart:" << chart;
41 chart->addSeries(this);
42 }
35 return this;
43 36 }
44 37
45 38 QDeclarativeListProperty<DeclarativeXyPoint> DeclarativeScatterSeries::points()
46 39 {
47 40 return QDeclarativeListProperty<DeclarativeXyPoint>(this, 0, &DeclarativeXySeries::appendPoints);
48 41 }
49 42
50 43 #include "moc_declarativescatterseries.cpp"
51 44
52 45 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,46 +1,46
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef DECLARATIVESCATTERSERIES_H
22 22 #define DECLARATIVESCATTERSERIES_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "qscatterseries.h"
26 26 #include "declarativexyseries.h"
27 27 #include <QDeclarativeParserStatus>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class DeclarativeScatterSeries : public QScatterSeries, public DeclarativeXySeries
32 32 {
33 33 Q_OBJECT
34 34 Q_PROPERTY(QDeclarativeListProperty<DeclarativeXyPoint> points READ points)
35 35
36 36 public:
37 37 explicit DeclarativeScatterSeries(QObject *parent = 0);
38 38
39 39 public:
40 void componentComplete();
40 QSeries *series();
41 41 QDeclarativeListProperty<DeclarativeXyPoint> points();
42 42 };
43 43
44 44 QTCOMMERCIALCHART_END_NAMESPACE
45 45
46 46 #endif // DECLARATIVESCATTERSERIES_H
@@ -1,50 +1,44
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "declarativesplineseries.h"
22 22 #include "declarativechart.h"
23 23 #include "qchart.h"
24 24
25 25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 26
27 27 DeclarativeSplineSeries::DeclarativeSplineSeries(QObject *parent) :
28 28 QSplineSeries(parent)
29 29 {
30 30 }
31 31
32 void DeclarativeSplineSeries::componentComplete()
32 QSeries *DeclarativeSplineSeries::series()
33 33 {
34 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
35
36 if (declarativeChart) {
37 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
38 Q_ASSERT(chart);
39 chart->addSeries(this);
40 }
34 return this;
41 35 }
42 36
43 37 QDeclarativeListProperty<DeclarativeXyPoint> DeclarativeSplineSeries::points()
44 38 {
45 39 return QDeclarativeListProperty<DeclarativeXyPoint>(this, 0, &DeclarativeXySeries::appendPoints);
46 40 }
47 41
48 42 #include "moc_declarativesplineseries.cpp"
49 43
50 44 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,46 +1,46
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef DECLARATIVESPLINESERIES_H
22 22 #define DECLARATIVESPLINESERIES_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "qsplineseries.h"
26 26 #include "declarativexyseries.h"
27 27 #include <QDeclarativeParserStatus>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class DeclarativeSplineSeries : public QSplineSeries, public DeclarativeXySeries
32 32 {
33 33 Q_OBJECT
34 34 Q_PROPERTY(QDeclarativeListProperty<DeclarativeXyPoint> points READ points)
35 35
36 36 public:
37 37 explicit DeclarativeSplineSeries(QObject *parent = 0);
38 38
39 39 public:
40 virtual void componentComplete();
40 QSeries *series();
41 41 QDeclarativeListProperty<DeclarativeXyPoint> points();
42 42 };
43 43
44 44 QTCOMMERCIALCHART_END_NAMESPACE
45 45
46 46 #endif // DECLARATIVESPLINESERIES_H
@@ -1,47 +1,63
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 //#include "DeclarativeXySeries.h"
22 22 #include "declarativexyseries.h"
23 23 #include "qxyseries.h"
24 #include "declarativechart.h"
25 #include <QObject>
24 26
25 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 28
27 29 DeclarativeXySeries::DeclarativeXySeries()
28 30 {
29 31 }
30 32
31 33 DeclarativeXySeries::~DeclarativeXySeries()
32 34 {
33 35 }
34 36
35 37 void DeclarativeXySeries::classBegin()
36 38 {
37 39 }
38 40
41 void DeclarativeXySeries::componentComplete()
42 {
43 QSeries *thisObj = reinterpret_cast<QSeries *>(series());
44 // QSeries *thisObj = qobject_cast<QSeries *>(this);
45 // Q_ASSERT(thisObj);
46 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(thisObj->parent());
47
48 if (declarativeChart) {
49 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
50 Q_ASSERT(chart);
51 chart->addSeries(thisObj);
52 }
53 }
54
39 55 void DeclarativeXySeries::appendPoints(QDeclarativeListProperty<DeclarativeXyPoint> *list,
40 56 DeclarativeXyPoint *element)
41 57 {
42 58 QXYSeries *series = qobject_cast<QXYSeries *>(list->object);
43 59 if (series)
44 series->append(QPointF(element->x(), element->y()));
60 series->append(element->x(), element->y());
45 61 }
46 62
47 63 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,55 +1,57
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef DECLARATIVE_XY_SERIES_H
22 22 #define DECLARATIVE_XY_SERIES_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "declarativexypoint.h"
26 26 #include <QDeclarativeParserStatus>
27 27 #include <QDeclarativeListProperty>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class QChart;
32 class QSeries;
32 33
33 34 class DeclarativeXySeries : public QDeclarativeParserStatus
34 35 {
35 36 Q_INTERFACES(QDeclarativeParserStatus)
36 37
37 38 public:
38 39 explicit DeclarativeXySeries();
39 40 ~DeclarativeXySeries();
40 41
41 42 public: // from QDeclarativeParserStatus
42 43 virtual void classBegin();
43 virtual void componentComplete() = 0;
44 virtual void componentComplete();
44 45
45 46 public:
47 virtual QSeries *series() = 0;
46 48 virtual QDeclarativeListProperty<DeclarativeXyPoint> points() = 0;
47 49
48 50 public Q_SLOTS:
49 51 static void appendPoints(QDeclarativeListProperty<DeclarativeXyPoint> *list,
50 52 DeclarativeXyPoint *element);
51 53 };
52 54
53 55 QTCOMMERCIALCHART_END_NAMESPACE
54 56
55 57 #endif // DECLARATIVE_XY_SERIES_H
@@ -1,103 +1,104
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "linechartitem_p.h"
22 22 #include "qlineseries.h"
23 23 #include "chartpresenter_p.h"
24 24 #include <QPainter>
25 25
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 //TODO: optimize : remove points which are not visible
30 30
31 31 LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter):XYChartItem(series,presenter),
32 32 m_series(series),
33 33 m_pointsVisible(false)
34 34 {
35 35 setZValue(ChartPresenter::LineChartZValue);
36 36 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
37 37 handleUpdated();
38 38 }
39 39
40 40 QRectF LineChartItem::boundingRect() const
41 41 {
42 42 return m_rect;
43 43 }
44 44
45 45 QPainterPath LineChartItem::shape() const
46 46 {
47 47 return m_path;
48 48 }
49 49
50 50 void LineChartItem::setLayout(QVector<QPointF>& points)
51 51 {
52 // qDebug() << "line: " << points.count();
53
52 54 if(points.size()==0)
53 55 {
54 56 XYChartItem::setLayout(points);
55 57 return;
56 58 }
57 59
58 60 QList<QGraphicsItem*> items = m_items.childItems();
59 61
60 62 QPainterPath linePath(points.at(0));
61 63
62 64 for(int i=1; i< points.size();i++) {
63 65 linePath.lineTo(points.at(i));
64 66 }
65 67
66 68 prepareGeometryChange();
67 69 m_path = linePath;
68 70 m_rect = linePath.boundingRect();
69 71
70 72 XYChartItem::setLayout(points);
71
72 73 }
73 74
74 75 void LineChartItem::handleUpdated()
75 76 {
76 77 m_pointsVisible = m_series->pointsVisible();
77 78 m_linePen = m_series->pen();
78 79 m_pointPen = m_series->pen();
79 80 m_pointPen.setWidthF(2*m_pointPen.width());
80 81 update();
81 82 }
82 83
83 84 //painter
84 85
85 86 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
86 87 {
87 88 Q_UNUSED(widget)
88 89 Q_UNUSED(option)
89 90
90 91 painter->save();
91 92 painter->setPen(m_linePen);
92 93 painter->setClipRect(clipRect());
93 94 painter->drawPath(m_path);
94 95 if(m_pointsVisible){
95 96 painter->setPen(m_pointPen);
96 97 painter->drawPoints(points());
97 98 }
98 99 painter->restore();
99 100 }
100 101
101 102 #include "moc_linechartitem_p.cpp"
102 103
103 104 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,190 +1,193
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "scatterchartitem_p.h"
22 22 #include "qscatterseries.h"
23 23 #include "chartpresenter_p.h"
24 24 #include <QPainter>
25 25 #include <QGraphicsScene>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) :
30 30 XYChartItem(series,presenter),
31 31 m_series(series),
32 32 m_items(this),
33 33 m_shape(QScatterSeries::MarkerShapeRectangle),
34 34 m_size(15)
35 35
36 36 {
37 37 QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
38 38
39 39 setZValue(ChartPresenter::ScatterSeriesZValue);
40 40 setFlags(QGraphicsItem::ItemHasNoContents);
41 41 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
42 42
43 43 handleUpdated();
44 44
45 45 m_items.setHandlesChildEvents(false);
46 46
47 47 // TODO: how to draw a drop shadow?
48 48 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
49 49 // dropShadow->setOffset(2.0);
50 50 // dropShadow->setBlurRadius(2.0);
51 51 // setGraphicsEffect(dropShadow);
52 52 }
53 53
54 54
55 55 QRectF ScatterChartItem::boundingRect() const
56 56 {
57 57 return m_rect;
58 58 }
59 59
60 60 void ScatterChartItem::createPoints(int count)
61 61 {
62 62 for (int i = 0; i < count; ++i) {
63 63
64 64 QGraphicsItem *item = 0;
65 65
66 66 switch (m_shape) {
67 67 case QScatterSeries::MarkerShapeCircle:{
68 68 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
69 69 const QRectF& rect = i->boundingRect();
70 70 i->setPos(-rect.width()/2,-rect.height()/2);
71 71 item = new Marker(i,this);
72 72 break;
73 73 }
74 74 case QScatterSeries::MarkerShapeRectangle:{
75 75 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
76 76 i->setPos(-m_size/2,-m_size/2);
77 77 item = new Marker(i,this);
78 78 break;
79 79 }
80 80 default:
81 81 qWarning()<<"Unsupported marker type";
82 82 break;
83 83
84 84 }
85 85 m_items.addToGroup(item);
86 86 }
87 87 }
88 88
89 89 void ScatterChartItem::deletePoints(int count)
90 90 {
91 91 QList<QGraphicsItem *> items = m_items.childItems();
92 92
93 93 for (int i = 0; i < count; ++i) {
94 94 delete(items.takeLast());
95 95 }
96 96 }
97 97
98 98 void ScatterChartItem::markerSelected(Marker *marker)
99 99 {
100 100 emit XYChartItem::clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index())));
101 101 }
102 102
103 103 void ScatterChartItem::setLayout(QVector<QPointF>& points)
104 104 {
105 // qDebug() << "scatter: " << points.count();
105 106 if(points.size()==0)
106 107 {
107 108 XYChartItem::setLayout(points);
108 109 return;
109 110 }
110 111
111 112 int diff = XYChartItem::points().size() - points.size();
112 113
113 114 if(diff>0) {
114 115 deletePoints(diff);
115 116 }
116 117 else if(diff<0) {
117 118 createPoints(-diff);
118 119 }
119 120
120 121 if(diff!=0) handleUpdated();
121 122
122 123 QList<QGraphicsItem*> items = m_items.childItems();
124 // qDebug() << "items count" << items.count();
123 125
124 for(int i=0; i< points.size();i++) {
126 for (int i = 0; i < points.size() && i < items.count(); i++) {
127 Q_ASSERT(i < items.count());
125 128 Marker* item = static_cast<Marker*>(items.at(i));
126 129 const QPointF& point = points.at(i);
127 130 const QRectF& rect = item->boundingRect();
128 131 item->setIndex(i);
129 132 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
130 133 if(!clipRect().contains(point)) {
131 134 item->setVisible(false);
132 135 }
133 136 else {
134 137 item->setVisible(true);
135 138 }
136 139 }
137 140
138 141 prepareGeometryChange();
139 142 m_rect = clipRect();
140 143 XYChartItem::setLayout(points);
141 144 }
142 145
143 146
144 147 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
145 148 {
146 149 Q_UNUSED(painter)
147 150 Q_UNUSED(option)
148 151 Q_UNUSED(widget)
149 152 }
150 153
151 154 void ScatterChartItem::setPen(const QPen& pen)
152 155 {
153 156 foreach(QGraphicsItem* item , m_items.childItems()) {
154 157 static_cast<Marker*>(item)->setPen(pen);
155 158 }
156 159 }
157 160
158 161 void ScatterChartItem::setBrush(const QBrush& brush)
159 162 {
160 163 foreach(QGraphicsItem* item , m_items.childItems()) {
161 164 static_cast<Marker*>(item)->setBrush(brush);
162 165 }
163 166 }
164 167
165 168 void ScatterChartItem::handleUpdated()
166 169 {
167 170
168 171 int count = m_items.childItems().count();
169 172
170 173 if(count==0) return;
171 174
172 175 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
173 176
174 177 //TODO: only rewrite on size change
175 178
176 179 m_size = m_series->size();
177 180 m_shape = m_series->shape();
178 181
179 182 if(recreate){
180 183 deletePoints(count);
181 184 createPoints(count);
182 185 }
183 186
184 187 setPen(m_series->pen());
185 188 setBrush(m_series->brush());
186 189 }
187 190
188 191 #include "moc_scatterchartitem_p.cpp"
189 192
190 193 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,123 +1,123
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 import QtQuick 1.0
22 22 import QtCommercial.Chart 1.0
23 23
24 24 Rectangle {
25 25 width: parent.width
26 26 height: parent.height
27 27
28 28 // Another option for QML data api:
29 29 // ListModel {
30 30 // id: listModelForPie
31 31 // // PieDataElement
32 32 // ListElement {
33 33 // label: "Apple"
34 34 // value: 4.3
35 35 // }
36 36 // ListElement {
37 37 // label: "Blackberry"
38 38 // value: 15.1
39 39 // }
40 40 // }
41 41
42 42 Component.onCompleted: {
43 43 // console.log("model:" + myModel.item(0));
44 44 // myModel.insert(1, {"time":1.4; "speed":41.1 });
45 45 // scatter.appendData();
46 46 // chart1.theme = Chart.ThemeHighContrast;
47 47 // chart2.theme = Chart.ThemeHighContrast;
48 48 }
49 49
50 50
51 51 Chart {
52 52 id: chart1
53 53 anchors.top: parent.top
54 54 anchors.left: parent.left
55 55 anchors.right: parent.right
56 56 height: parent.height / 2
57 57 theme: Chart.ChartThemeBlueCerulean
58 58
59 59 BarSeries {
60 60 barCategories: [ "2008", "2009", "2010", "2011", "2012" ]
61 61 }
62 62
63 63 PieSeries {
64 64 slices: [
65 65 PieSlice { label: "Volkswagen"; value: 13.5 },
66 66 PieSlice { label: "Toyota"; value: 10.9 },
67 67 PieSlice { label: "Ford"; value: 8.6 },
68 68 PieSlice { label: "Skoda"; value: 8.2 },
69 69 PieSlice { label: "Volvo"; value: 6.8 },
70 70 PieSlice { label: "Others"; value: 52.0 }
71 71 ]
72 72 }
73 73 }
74 74
75 75
76 76 Chart {
77 77 id: chart2
78 78 anchors.top: chart1.bottom
79 79 anchors.bottom: parent.bottom
80 80 anchors.left: parent.left
81 81 anchors.right: parent.right
82 82 theme: Chart.ChartThemeHighContrast
83 83
84 84 LineSeries {
85 85 points: [
86 86 XyPoint { x: 0.0; y: 0.0 },
87 87 XyPoint { x: 1.1; y: 2.1 },
88 88 XyPoint { x: 2.9; y: 4.9 },
89 89 XyPoint { x: 3.2; y: 3.0 }
90 90 ]
91 91 }
92 92
93 SplineSeries {
94 points: [
95 XyPoint { x: 0.0; y: 0.3 },
96 XyPoint { x: 1.1; y: 3.2 },
97 XyPoint { x: 4.17; y: 3.15 }
98 ]
99 }
93 // SplineSeries {
94 // points: [
95 // XyPoint { x: 0.0; y: 0.3 },
96 // XyPoint { x: 1.1; y: 3.2 },
97 // XyPoint { x: 4.17; y: 3.15 }
98 // ]
99 // }
100 100
101 101 ScatterSeries {
102 102 points: [
103 103 XyPoint { x: 1.5; y: 1.5 },
104 104 XyPoint { x: 1.5; y: 1.6 },
105 105 XyPoint { x: 1.57; y: 1.55 }
106 106 ]
107 107 }
108 ScatterSeries {
109 points: [
110 XyPoint { x: 2.0; y: 2.0 },
111 XyPoint { x: 2.0; y: 2.1 },
112 XyPoint { x: 2.07; y: 2.05 }
113 ]
114 }
115 ScatterSeries {
116 points: [
117 XyPoint { x: 2.6; y: 2.6 },
118 XyPoint { x: 2.6; y: 2.7 },
119 XyPoint { x: 2.67; y: 2.65 }
120 ]
121 }
108 // ScatterSeries {
109 // points: [
110 // XyPoint { x: 2.0; y: 2.0 },
111 // XyPoint { x: 2.0; y: 2.1 },
112 // XyPoint { x: 2.07; y: 2.05 }
113 // ]
114 // }
115 // ScatterSeries {
116 // points: [
117 // XyPoint { x: 2.6; y: 2.6 },
118 // XyPoint { x: 2.6; y: 2.7 },
119 // XyPoint { x: 2.67; y: 2.65 }
120 // ]
121 // }
122 122 }
123 123 }
General Comments 0
You need to be logged in to leave comments. Login now