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