From 4aded284df805c3e9fb12541a624079d3315555c 2012-03-26 15:03:07 From: Tero Ahola Date: 2012-03-26 15:03:07 Subject: [PATCH] Spline to QML API --- diff --git a/qmlplugin/declarativelineseries.cpp b/qmlplugin/declarativelineseries.cpp index 1ed200a..218dfc9 100644 --- a/qmlplugin/declarativelineseries.cpp +++ b/qmlplugin/declarativelineseries.cpp @@ -6,48 +6,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE DeclarativeLineSeries::DeclarativeLineSeries(QDeclarativeItem *parent) : - QDeclarativeItem(parent) + DeclarativeXySeries(QSeries::SeriesTypeLine, parent) { - setFlag(QGraphicsItem::ItemHasNoContents, false); - connect(this, SIGNAL(parentChanged()), - this, SLOT(setParentForSeries())); -} - -void DeclarativeLineSeries::setParentForSeries() -{ - if (!m_series) { - DeclarativeChart *declarativeChart = qobject_cast(parent()); - - if (declarativeChart) { - QChart *chart = qobject_cast(declarativeChart->m_chart); - qDebug() << "creating line series for chart: " << chart; - Q_ASSERT(chart); - - m_series = new QLineSeries(); - Q_ASSERT(m_series); - for (int i(0); i < m_data.count(); i++) { - DeclarativeXyPoint *element = m_data.at(i); - m_series->add(element->x(), element->y()); - } - chart->addSeries(m_series); - } - } -} - -QDeclarativeListProperty DeclarativeLineSeries::data() -{ - return QDeclarativeListProperty(this, 0, &DeclarativeLineSeries::appendData); -} - -void DeclarativeLineSeries::appendData(QDeclarativeListProperty *list, - DeclarativeXyPoint *element) -{ - DeclarativeLineSeries *series = qobject_cast(list->object); - if (series) { - series->m_data.append(element); - if (series->m_series) - series->m_series->add(element->x(), element->y()); - } } #include "moc_declarativelineseries.cpp" diff --git a/qmlplugin/declarativelineseries.h b/qmlplugin/declarativelineseries.h index 9ac67a8..b6e5100 100644 --- a/qmlplugin/declarativelineseries.h +++ b/qmlplugin/declarativelineseries.h @@ -2,36 +2,17 @@ #define DECLARATIVELINESERIES_H #include "qchartglobal.h" -#include "declarativexypoint.h" +#include "declarativexyseries.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class QChart; -class QLineSeries; - -class DeclarativeLineSeries : public QDeclarativeItem +class DeclarativeLineSeries : public DeclarativeXySeries { Q_OBJECT - Q_PROPERTY(QDeclarativeListProperty data READ data) public: explicit DeclarativeLineSeries(QDeclarativeItem *parent = 0); - QDeclarativeListProperty data(); - -signals: - -public slots: - static void appendData(QDeclarativeListProperty *list, - DeclarativeXyPoint *element); - -private slots: - void setParentForSeries(); - -private: - QChart *m_chart; - QLineSeries *m_series; - QList m_data; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativescatterseries.cpp b/qmlplugin/declarativescatterseries.cpp index 9dbd0f9..c0340b6 100644 --- a/qmlplugin/declarativescatterseries.cpp +++ b/qmlplugin/declarativescatterseries.cpp @@ -6,58 +6,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE DeclarativeScatterSeries::DeclarativeScatterSeries(QDeclarativeItem *parent) : - QDeclarativeItem(parent), - m_chart(0), - m_series(0) + DeclarativeXySeries(QSeries::SeriesTypeScatter, parent) { setFlag(QGraphicsItem::ItemHasNoContents, false); } -DeclarativeScatterSeries::~DeclarativeScatterSeries() -{ -} - -void DeclarativeScatterSeries::componentComplete() -{ - Q_ASSERT(!m_series); - DeclarativeChart *declarativeChart = qobject_cast(parent()); - - if (declarativeChart) { - m_chart = qobject_cast(declarativeChart->m_chart); - qDebug() << "creating scatter series for chart: " << m_chart; - Q_ASSERT(m_chart); - - m_series = new QScatterSeries(); - for (int i(0); i < m_points.count(); i++) { - DeclarativeXyPoint *element = m_points.at(i); - *m_series << QPointF(element->x(), element->y()); - } - m_chart->addSeries(m_series); - } -} - -QDeclarativeListProperty DeclarativeScatterSeries::points() -{ - return QDeclarativeListProperty(this, 0, - &DeclarativeScatterSeries::appendPoints); -} - -void DeclarativeScatterSeries::appendPoints(QDeclarativeListProperty *list, - DeclarativeXyPoint *element) -{ - DeclarativeScatterSeries *series = qobject_cast(list->object); - qDebug() << "appendPoints: " << series; - qDebug() << "appendPoints: " << element; - qDebug() << "appendPoints: " << element->x(); - qDebug() << "appendPoints: " << element->y(); - qDebug() << "appendPoints: " << series->m_series; - if (series) { - series->m_points.append(element); - if (series->m_series) - series->m_series->add(element->x(), element->y()); - } -} - #include "moc_declarativescatterseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativescatterseries.h b/qmlplugin/declarativescatterseries.h index 5d9511d..b0954a3 100644 --- a/qmlplugin/declarativescatterseries.h +++ b/qmlplugin/declarativescatterseries.h @@ -2,41 +2,17 @@ #define DECLARATIVESCATTERSERIES_H #include "qchartglobal.h" -#include "declarativexypoint.h" +#include "declarativexyseries.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class QChart; -class QScatterSeries; - -class DeclarativeScatterSeries : public QDeclarativeItem +class DeclarativeScatterSeries : public DeclarativeXySeries { Q_OBJECT - Q_PROPERTY(QDeclarativeListProperty points READ points) public: explicit DeclarativeScatterSeries(QDeclarativeItem *parent = 0); - ~DeclarativeScatterSeries(); - -public: // from QDeclarativeParserStatus - void componentComplete(); - -public: - QDeclarativeListProperty points(); - -signals: - -public slots: - static void appendPoints(QDeclarativeListProperty *list, - DeclarativeXyPoint *element); - -private slots: - -public: - QChart *m_chart; // not owned - QScatterSeries *m_series; // not owned - QList m_points; // not owned }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativesplineseries.h b/qmlplugin/declarativesplineseries.h new file mode 100644 index 0000000..76f4083 --- /dev/null +++ b/qmlplugin/declarativesplineseries.h @@ -0,0 +1,23 @@ +#ifndef DECLARATIVESPLINESERIES_H +#define DECLARATIVESPLINESERIES_H + +#include "qchartglobal.h" +#include "declarativexyseries.h" +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class DeclarativeSplineSeries : public DeclarativeXySeries +{ + Q_OBJECT + +public: + explicit DeclarativeSplineSeries(QDeclarativeItem *parent = 0) : + DeclarativeXySeries(QSeries::SeriesTypeSpline, parent) {} +}; + +#include "moc_declarativesplineseries.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // DECLARATIVESPLINESERIES_H diff --git a/qmlplugin/declarativexyseries.cpp b/qmlplugin/declarativexyseries.cpp new file mode 100644 index 0000000..210beec --- /dev/null +++ b/qmlplugin/declarativexyseries.cpp @@ -0,0 +1,85 @@ +#include "DeclarativeXySeries.h" +#include "declarativechart.h" +#include "qchart.h" +#include "qxyseries.h" +#include "qareaseries.h" +#include "qsplineseries.h" +#include "qscatterseries.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +DeclarativeXySeries::DeclarativeXySeries(QSeries::QSeriesType type, QDeclarativeItem *parent) : + QDeclarativeItem(parent), + m_seriesType(type), + m_chart(0), + m_series(0) +{ + setFlag(QGraphicsItem::ItemHasNoContents, false); +} + +DeclarativeXySeries::~DeclarativeXySeries() +{ +} + +void DeclarativeXySeries::componentComplete() +{ + Q_ASSERT(!m_series); + DeclarativeChart *declarativeChart = qobject_cast(parent()); + + if (declarativeChart) { + m_chart = qobject_cast(declarativeChart->m_chart); + Q_ASSERT(m_chart); + + switch (m_seriesType) { + case QSeries::SeriesTypeLine: + qDebug() << "creating line series for chart: " << m_chart; + m_series = new QLineSeries(); + break; + case QSeries::SeriesTypeArea: + qDebug() << "TODO: creating area series for chart: " << m_chart; +// m_series = new QAreaSeries(); + break; + case QSeries::SeriesTypeSpline: + qDebug() << "creating spline series for chart: " << m_chart; + m_series = new QSplineSeries(); + break; + case QSeries::SeriesTypeScatter: + qDebug() << "creating scatter series for chart: " << m_chart; + m_series = new QScatterSeries(); + break; + default: + Q_ASSERT(false); + } + + for (int i(0); i < m_points.count(); i++) { + DeclarativeXyPoint *element = m_points.at(i); + m_series->add(element->x(), element->y()); + } + m_chart->addSeries(m_series); + } +} + +QDeclarativeListProperty DeclarativeXySeries::points() +{ + return QDeclarativeListProperty(this, 0, &DeclarativeXySeries::appendPoints); +} + +void DeclarativeXySeries::appendPoints(QDeclarativeListProperty *list, + DeclarativeXyPoint *element) +{ + DeclarativeXySeries *series = qobject_cast(list->object); + qDebug() << "appendPoints: " << series; + qDebug() << "appendPoints: " << element; + qDebug() << "appendPoints: " << element->x(); + qDebug() << "appendPoints: " << element->y(); + qDebug() << "appendPoints: " << series->m_series; + if (series) { + series->m_points.append(element); + if (series->m_series) + series->m_series->add(element->x(), element->y()); + } +} + +#include "moc_declarativexyseries.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativexyseries.h b/qmlplugin/declarativexyseries.h new file mode 100644 index 0000000..adf7c3d --- /dev/null +++ b/qmlplugin/declarativexyseries.h @@ -0,0 +1,45 @@ +#ifndef DECLARATIVE_XY_SERIES_H +#define DECLARATIVE_XY_SERIES_H + +#include "qchartglobal.h" +#include "declarativexypoint.h" +#include "qxyseries.h" +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QChart; + +class DeclarativeXySeries : public QDeclarativeItem +{ + Q_OBJECT + Q_PROPERTY(QDeclarativeListProperty points READ points) + +public: + explicit DeclarativeXySeries(QSeries::QSeriesType type, QDeclarativeItem *parent = 0); + ~DeclarativeXySeries(); + +public: // from QDeclarativeParserStatus + void componentComplete(); + +public: + QDeclarativeListProperty points(); + +signals: + +public slots: + static void appendPoints(QDeclarativeListProperty *list, + DeclarativeXyPoint *element); + +private slots: + +public: + QSeries::QSeriesType m_seriesType; + QChart *m_chart; + QXYSeries *m_series; + QList m_points; +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // DECLARATIVE_XY_SERIES_H diff --git a/qmlplugin/plugin.cpp b/qmlplugin/plugin.cpp index 1c2a359..58bda3e 100644 --- a/qmlplugin/plugin.cpp +++ b/qmlplugin/plugin.cpp @@ -2,8 +2,9 @@ #include #include "declarativechart.h" #include "declarativexypoint.h" -#include "declarativescatterseries.h" #include "declarativelineseries.h" +#include "declarativesplineseries.h" +#include "declarativescatterseries.h" #include "declarativebarseries.h" #include "declarativepieseries.h" @@ -21,6 +22,7 @@ public: qmlRegisterType(uri, 1, 0, "XyPoint"); qmlRegisterType(uri, 1, 0, "ScatterSeries"); qmlRegisterType(uri, 1, 0, "LineSeries"); + qmlRegisterType(uri, 1, 0, "SplineSeries"); qmlRegisterType(uri, 1, 0, "BarSeries"); qmlRegisterType(uri, 1, 0, "PieSeries"); qmlRegisterType(uri, 1, 0, "PieSlice"); diff --git a/qmlplugin/qmlplugin.pro b/qmlplugin/qmlplugin.pro index 2e44d3a..dd60250 100644 --- a/qmlplugin/qmlplugin.pro +++ b/qmlplugin/qmlplugin.pro @@ -24,17 +24,20 @@ RCC_DIR = $$CHART_BUILD_DIR/plugin SOURCES += \ plugin.cpp \ declarativechart.cpp \ - declarativescatterseries.cpp \ declarativexypoint.cpp \ - declarativepieseries.cpp \ + declarativexyseries.cpp \ declarativelineseries.cpp \ + declarativescatterseries.cpp \ + declarativepieseries.cpp \ declarativebarseries.cpp HEADERS += \ declarativechart.h \ - declarativescatterseries.h \ declarativexypoint.h \ - declarativepieseries.h \ + declarativexyseries.h \ declarativelineseries.h \ + declarativesplineseries.h \ + declarativescatterseries.h \ + declarativepieseries.h \ declarativebarseries.h TARGETPATH = QtCommercial/Chart diff --git a/test/qmlchart/qml/qmlchart/main.qml b/test/qmlchart/qml/qmlchart/main.qml index ab5d515..015f9a9 100644 --- a/test/qmlchart/qml/qmlchart/main.qml +++ b/test/qmlchart/qml/qmlchart/main.qml @@ -63,7 +63,7 @@ Rectangle { theme: Chart.ThemeBrownSand LineSeries { - data: [ + points: [ XyPoint { x: 0.0; y: 0.0 }, XyPoint { x: 1.1; y: 2.1 }, XyPoint { x: 2.9; y: 4.9 }, @@ -71,12 +71,12 @@ Rectangle { ] } - ScatterSeries { + SplineSeries { id: scatter points: [ - XyPoint { x: 1.1; y: 1.1 }, - XyPoint { x: 1.1; y: 1.2 }, - XyPoint { x: 1.17; y: 1.15 } + XyPoint { x: 0.0; y: 0.3 }, + XyPoint { x: 1.1; y: 3.2 }, + XyPoint { x: 4.17; y: 3.15 } ] } ScatterSeries {