From accf902f8269e23859241617511466634b5b6a13 2012-04-03 12:43:41 From: Tero Ahola Date: 2012-04-03 12:43:41 Subject: [PATCH] Refactoring QML impl for series --- diff --git a/qmlplugin/declarativechart.cpp b/qmlplugin/declarativechart.cpp index e7a1477..eb6f2ff 100644 --- a/qmlplugin/declarativechart.cpp +++ b/qmlplugin/declarativechart.cpp @@ -27,6 +27,7 @@ DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) : QDeclarativeItem(parent), m_chart(new QChart(this)) { + m_chart->setAnimationOptions(QChart::SeriesAnimations); setFlag(QGraphicsItem::ItemHasNoContents, false); } diff --git a/qmlplugin/declarativelineseries.cpp b/qmlplugin/declarativelineseries.cpp index fbf6c74..8d50d8f 100644 --- a/qmlplugin/declarativelineseries.cpp +++ b/qmlplugin/declarativelineseries.cpp @@ -30,15 +30,9 @@ DeclarativeLineSeries::DeclarativeLineSeries(QObject *parent) : { } -void DeclarativeLineSeries::componentComplete() +QSeries *DeclarativeLineSeries::series() { - DeclarativeChart *declarativeChart = qobject_cast(parent()); - - if (declarativeChart) { - QChart *chart = qobject_cast(declarativeChart->m_chart); - Q_ASSERT(chart); - chart->addSeries(this); - } + return this; } QDeclarativeListProperty DeclarativeLineSeries::points() diff --git a/qmlplugin/declarativelineseries.h b/qmlplugin/declarativelineseries.h index 3935a19..056d918 100644 --- a/qmlplugin/declarativelineseries.h +++ b/qmlplugin/declarativelineseries.h @@ -37,7 +37,7 @@ public: explicit DeclarativeLineSeries(QObject *parent = 0); public: - void componentComplete(); + QSeries *series(); QDeclarativeListProperty points(); }; diff --git a/qmlplugin/declarativescatterseries.cpp b/qmlplugin/declarativescatterseries.cpp index d5f67ba..ea7419c 100644 --- a/qmlplugin/declarativescatterseries.cpp +++ b/qmlplugin/declarativescatterseries.cpp @@ -30,16 +30,9 @@ DeclarativeScatterSeries::DeclarativeScatterSeries(QObject *parent) : { } -void DeclarativeScatterSeries::componentComplete() +QSeries *DeclarativeScatterSeries::series() { - DeclarativeChart *declarativeChart = qobject_cast(parent()); - - if (declarativeChart) { - QChart *chart = qobject_cast(declarativeChart->m_chart); - Q_ASSERT(chart); - qDebug() << "chart:" << chart; - chart->addSeries(this); - } + return this; } QDeclarativeListProperty DeclarativeScatterSeries::points() diff --git a/qmlplugin/declarativescatterseries.h b/qmlplugin/declarativescatterseries.h index 69165f9..d0fe8cb 100644 --- a/qmlplugin/declarativescatterseries.h +++ b/qmlplugin/declarativescatterseries.h @@ -37,7 +37,7 @@ public: explicit DeclarativeScatterSeries(QObject *parent = 0); public: - void componentComplete(); + QSeries *series(); QDeclarativeListProperty points(); }; diff --git a/qmlplugin/declarativesplineseries.cpp b/qmlplugin/declarativesplineseries.cpp index 03675f6..b81f459 100644 --- a/qmlplugin/declarativesplineseries.cpp +++ b/qmlplugin/declarativesplineseries.cpp @@ -29,15 +29,9 @@ DeclarativeSplineSeries::DeclarativeSplineSeries(QObject *parent) : { } -void DeclarativeSplineSeries::componentComplete() +QSeries *DeclarativeSplineSeries::series() { - DeclarativeChart *declarativeChart = qobject_cast(parent()); - - if (declarativeChart) { - QChart *chart = qobject_cast(declarativeChart->m_chart); - Q_ASSERT(chart); - chart->addSeries(this); - } + return this; } QDeclarativeListProperty DeclarativeSplineSeries::points() diff --git a/qmlplugin/declarativesplineseries.h b/qmlplugin/declarativesplineseries.h index df64068..8fe90d6 100644 --- a/qmlplugin/declarativesplineseries.h +++ b/qmlplugin/declarativesplineseries.h @@ -37,7 +37,7 @@ public: explicit DeclarativeSplineSeries(QObject *parent = 0); public: - virtual void componentComplete(); + QSeries *series(); QDeclarativeListProperty points(); }; diff --git a/qmlplugin/declarativexyseries.cpp b/qmlplugin/declarativexyseries.cpp index 2c49fb4..42b7369 100644 --- a/qmlplugin/declarativexyseries.cpp +++ b/qmlplugin/declarativexyseries.cpp @@ -21,6 +21,8 @@ //#include "DeclarativeXySeries.h" #include "declarativexyseries.h" #include "qxyseries.h" +#include "declarativechart.h" +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -36,12 +38,26 @@ void DeclarativeXySeries::classBegin() { } +void DeclarativeXySeries::componentComplete() +{ + QSeries *thisObj = reinterpret_cast(series()); +// QSeries *thisObj = qobject_cast(this); +// Q_ASSERT(thisObj); + DeclarativeChart *declarativeChart = qobject_cast(thisObj->parent()); + + if (declarativeChart) { + QChart *chart = qobject_cast(declarativeChart->m_chart); + Q_ASSERT(chart); + chart->addSeries(thisObj); + } +} + void DeclarativeXySeries::appendPoints(QDeclarativeListProperty *list, DeclarativeXyPoint *element) { QXYSeries *series = qobject_cast(list->object); if (series) - series->append(QPointF(element->x(), element->y())); + series->append(element->x(), element->y()); } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativexyseries.h b/qmlplugin/declarativexyseries.h index 2df09a8..54c7068 100644 --- a/qmlplugin/declarativexyseries.h +++ b/qmlplugin/declarativexyseries.h @@ -29,6 +29,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QChart; +class QSeries; class DeclarativeXySeries : public QDeclarativeParserStatus { @@ -40,9 +41,10 @@ public: public: // from QDeclarativeParserStatus virtual void classBegin(); - virtual void componentComplete() = 0; + virtual void componentComplete(); public: + virtual QSeries *series() = 0; virtual QDeclarativeListProperty points() = 0; public Q_SLOTS: diff --git a/src/linechart/linechartitem.cpp b/src/linechart/linechartitem.cpp index a059329..1f669f5 100644 --- a/src/linechart/linechartitem.cpp +++ b/src/linechart/linechartitem.cpp @@ -49,6 +49,8 @@ QPainterPath LineChartItem::shape() const void LineChartItem::setLayout(QVector& points) { +// qDebug() << "line: " << points.count(); + if(points.size()==0) { XYChartItem::setLayout(points); @@ -68,7 +70,6 @@ void LineChartItem::setLayout(QVector& points) m_rect = linePath.boundingRect(); XYChartItem::setLayout(points); - } void LineChartItem::handleUpdated() diff --git a/src/scatterseries/scatterchartitem.cpp b/src/scatterseries/scatterchartitem.cpp index c3ca08a..e5cc99c 100644 --- a/src/scatterseries/scatterchartitem.cpp +++ b/src/scatterseries/scatterchartitem.cpp @@ -102,6 +102,7 @@ void ScatterChartItem::markerSelected(Marker *marker) void ScatterChartItem::setLayout(QVector& points) { +// qDebug() << "scatter: " << points.count(); if(points.size()==0) { XYChartItem::setLayout(points); @@ -120,8 +121,10 @@ void ScatterChartItem::setLayout(QVector& points) if(diff!=0) handleUpdated(); QList items = m_items.childItems(); +// qDebug() << "items count" << items.count(); - for(int i=0; i< points.size();i++) { + for (int i = 0; i < points.size() && i < items.count(); i++) { + Q_ASSERT(i < items.count()); Marker* item = static_cast(items.at(i)); const QPointF& point = points.at(i); const QRectF& rect = item->boundingRect(); diff --git a/test/qmlchart/qml/qmlchart/main.qml b/test/qmlchart/qml/qmlchart/main.qml index 8b57854..42426ea 100644 --- a/test/qmlchart/qml/qmlchart/main.qml +++ b/test/qmlchart/qml/qmlchart/main.qml @@ -90,13 +90,13 @@ Rectangle { ] } - SplineSeries { - points: [ - XyPoint { x: 0.0; y: 0.3 }, - XyPoint { x: 1.1; y: 3.2 }, - XyPoint { x: 4.17; y: 3.15 } - ] - } +// SplineSeries { +// points: [ +// XyPoint { x: 0.0; y: 0.3 }, +// XyPoint { x: 1.1; y: 3.2 }, +// XyPoint { x: 4.17; y: 3.15 } +// ] +// } ScatterSeries { points: [ @@ -105,19 +105,19 @@ Rectangle { XyPoint { x: 1.57; y: 1.55 } ] } - ScatterSeries { - points: [ - XyPoint { x: 2.0; y: 2.0 }, - XyPoint { x: 2.0; y: 2.1 }, - XyPoint { x: 2.07; y: 2.05 } - ] - } - ScatterSeries { - points: [ - XyPoint { x: 2.6; y: 2.6 }, - XyPoint { x: 2.6; y: 2.7 }, - XyPoint { x: 2.67; y: 2.65 } - ] - } +// ScatterSeries { +// points: [ +// XyPoint { x: 2.0; y: 2.0 }, +// XyPoint { x: 2.0; y: 2.1 }, +// XyPoint { x: 2.07; y: 2.05 } +// ] +// } +// ScatterSeries { +// points: [ +// XyPoint { x: 2.6; y: 2.6 }, +// XyPoint { x: 2.6; y: 2.7 }, +// XyPoint { x: 2.67; y: 2.65 } +// ] +// } } }