From 36c146599567a1283644023c1fb2feb5fafdf63a 2012-02-17 07:06:51 From: Tero Ahola Date: 2012-02-17 07:06:51 Subject: [PATCH] Proposal for QML data API --- diff --git a/qmlplugin/declarativescatterseries.cpp b/qmlplugin/declarativescatterseries.cpp new file mode 100644 index 0000000..50925ff --- /dev/null +++ b/qmlplugin/declarativescatterseries.cpp @@ -0,0 +1,69 @@ +#include "declarativescatterseries.h" +#include "declarativechart.h" +#include "qchart.h" +#include "qscatterseries.h" +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +DeclarativeScatterSeries::DeclarativeScatterSeries(QDeclarativeItem *parent) : + QDeclarativeItem(parent), + m_chart(0), + m_series(0) +{ + setFlag(QGraphicsItem::ItemHasNoContents, false); + connect(this, SIGNAL(parentChanged()), + this, SLOT(setParentForSeries())); +} + +void DeclarativeScatterSeries::setParentForSeries() +{ + if (!m_series) + initSeries(); +} + +void DeclarativeScatterSeries::initSeries() +{ + Q_ASSERT(!m_series); + DeclarativeChart *declarativeChart = qobject_cast(parent()); + + if (declarativeChart) { + QChart *chart = qobject_cast(declarativeChart->m_chart); + qDebug() << "creating scatter series for chart: " << chart; + Q_ASSERT(chart); + + m_series = new QScatterSeries(); + Q_ASSERT(m_series); + for (int i(0); i < m_data.count(); i++) { + ScatterElement *element = m_data.at(i); + *m_series << QPointF(element->x(), element->y()); + } + chart->addSeries(m_series); + } +} + +QDeclarativeListProperty DeclarativeScatterSeries::data() +{ + return QDeclarativeListProperty(this, 0, + &DeclarativeScatterSeries::appendData); +} + +void DeclarativeScatterSeries::appendData(QDeclarativeListProperty *list, + ScatterElement *element) +{ + DeclarativeScatterSeries *series = qobject_cast(list->object); + qDebug() << "appendData: " << series; + qDebug() << "appendData: " << element; + qDebug() << "appendData: " << element->x(); + qDebug() << "appendData: " << element->y(); + qDebug() << "appendData: " << series->m_series; + if (series) { + series->m_data.append(element); + if (series->m_series) + series->m_series->addData(QPointF(element->x(), element->y())); + } +} + +#include "moc_declarativescatterseries.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativescatterseries.h b/qmlplugin/declarativescatterseries.h new file mode 100644 index 0000000..fda58c0 --- /dev/null +++ b/qmlplugin/declarativescatterseries.h @@ -0,0 +1,42 @@ +#ifndef DECLARATIVESCATTERSERIES_H +#define DECLARATIVESCATTERSERIES_H + +#include "qchartglobal.h" +#include "scatterelement.h" +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QChart; +class QScatterSeries; + +class DeclarativeScatterSeries : public QDeclarativeItem +{ + Q_OBJECT + Q_PROPERTY(QDeclarativeListProperty data READ data) + +public: + explicit DeclarativeScatterSeries(QDeclarativeItem *parent = 0); + QDeclarativeListProperty data(); + +signals: + +public slots: + static void appendData(QDeclarativeListProperty *list, + ScatterElement *element); +// void append(ScatterElement element); + +private slots: + void setParentForSeries(); + +public: + void initSeries(); + + QChart *m_chart; + QScatterSeries *m_series; + QList m_data; +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // DECLARATIVESCATTERSERIES_H diff --git a/qmlplugin/plugin.cpp b/qmlplugin/plugin.cpp index d9140f3..545952c 100644 --- a/qmlplugin/plugin.cpp +++ b/qmlplugin/plugin.cpp @@ -2,6 +2,8 @@ #include #include "declarativechart.h" #include "declarativeseries.h" +#include "scatterelement.h" +#include "declarativescatterseries.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -14,7 +16,8 @@ public: Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart")); qmlRegisterType(uri, 1, 0, "Chart"); qmlRegisterType(uri, 1, 0, "Series"); - //qmlRegisterUncreatableType(uri, 1, 0, "Series.Se", QLatin1String("Do not create objects of this type.")); + qmlRegisterType(uri, 1, 0, "ScatterSeries"); + qmlRegisterType(uri, 1, 0, "ScatterElement"); } }; diff --git a/qmlplugin/qmlplugin.pro b/qmlplugin/qmlplugin.pro index cde7f93..6a452a8 100644 --- a/qmlplugin/qmlplugin.pro +++ b/qmlplugin/qmlplugin.pro @@ -24,10 +24,14 @@ RCC_DIR = $$CHART_BUILD_DIR/lib SOURCES += \ plugin.cpp \ declarativechart.cpp \ - declarativeseries.cpp + declarativeseries.cpp \ + declarativescatterseries.cpp \ + scatterelement.cpp HEADERS += \ declarativechart.h \ - declarativeseries.h + declarativeseries.h \ + declarativescatterseries.h \ + scatterelement.h TARGETPATH = QtCommercial/Chart target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH @@ -37,3 +41,17 @@ qmldir.path += $$[QT_INSTALL_IMPORTS]/$$TARGETPATH INSTALLS += target qmldir + + + + + + + + + + + + + + diff --git a/qmlplugin/scatterelement.cpp b/qmlplugin/scatterelement.cpp new file mode 100644 index 0000000..f1c3aa4 --- /dev/null +++ b/qmlplugin/scatterelement.cpp @@ -0,0 +1,12 @@ +#include "scatterelement.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +ScatterElement::ScatterElement(QObject *parent) : + QObject(parent) +{ +} + +#include "moc_scatterelement.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/scatterelement.h b/qmlplugin/scatterelement.h new file mode 100644 index 0000000..ad91c92 --- /dev/null +++ b/qmlplugin/scatterelement.h @@ -0,0 +1,30 @@ +#ifndef SCATTERELEMENT_H +#define SCATTERELEMENT_H + +#include "qchartglobal.h" +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class ScatterElement : public QObject +{ + Q_OBJECT + Q_PROPERTY(qreal x READ x WRITE setX /*NOTIFY dataXChanged*/) + Q_PROPERTY(qreal y READ y WRITE setY /*NOTIFY dataYChanged*/) + +public: + explicit ScatterElement(QObject *parent = 0); + + void setX(qreal x) {m_x = x;} + qreal x() {return m_x;} + void setY(qreal y) {m_y = y;} + qreal y() {return m_y;} + +public: + qreal m_x; + qreal m_y; +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // SCATTERELEMENT_H diff --git a/test/qmlchart/qml/qmlchart/main.qml b/test/qmlchart/qml/qmlchart/main.qml index 74d4e33..84647b2 100644 --- a/test/qmlchart/qml/qmlchart/main.qml +++ b/test/qmlchart/qml/qmlchart/main.qml @@ -9,106 +9,64 @@ Rectangle { anchors.centerIn: parent } + // Another option for QML data api: // ListModel { // id: listModelForPie // // PieDataElement // ListElement { // label: "Apple" -// value: 40.3 +// value: 4.3 // } // ListElement { -// label: "Pumpkin" -// value: 10.1 -// } -// ListElement { -// label: "Raspberry" +// label: "Blackberry" // value: 15.1 // } -// ListElement { -// label: "Strawberry" -// value: 29.9 -// } // } -// ChartModel { -// id: chartModel -// ListElement { -// label: "dada" -// x: 1.1 -// y: 3.2 -// } -// } - -// ChartModel { -// ScatterElement {x: 1.1; y: 1.2} -// ScatterElement {x: 1.3; y: 1.9} -// ScatterElement {x: 1.1; y: 1.2} -// } - - ListModel { - id: listModelScatter - ListElement { - height: 154 - weight: 54 - } - ListElement { - height: 166 - weight: 64 - } - ListElement { - height: 199 - weight: 97 - } + Component.onCompleted: { +// console.log("Component.onCompleted: " + scatterElement.x); +// console.log("Component.onCompleted: " + scatterElement.y); +// console.log("Component.onCompleted: " + scatterElement.dataX); +// console.log("Component.onCompleted: " + scatterElement.dataY); + //console.log("Component.onCompleted: " + chartModel.get(0).x); + //console.log("Component.onCompleted: " + chartModel.scatterElements); +// console.log("Component.onCompleted: " + elementt.dataX); +// console.log("Component.onCompleted: " + chartModel.get(0).dataX); } -// Chart { -// anchors.fill: parent -// theme: Chart.ThemeIcy -// ScatterSeries { -// model: listModelScatter -// name: "scatter" -// xValue: x -// yValue: y -// } -// } - Chart { anchors.fill: parent theme: Chart.ThemeIcy -// PieSeries { -// labels: ["point1", "point2", "point3", "point4", "point5"] -// datax: [2, 1.5, 3, 3, 3] -// } -// PieSeries { -// name: "raspberry pie" -// seriesLabels: ["point1", "point2", "point3", "point4", "point5"] -// seriesData: [2, 1.5, 3, 3, 3] -// } -// ScatterSeries { -// name: "scatter1" -// datax: [2, 1.5, 3, 3, 3] -// datay: [2, 1.5, 3, 3, 3] -// } + ScatterSeries { + id: scatterSeries + data: [ + ScatterElement { x: 1.1; y: 2.1 }, + ScatterElement { x: 1.2; y: 2.0 }, + ScatterElement { x: 1.4; y: 2.3 }, + ScatterElement { x: 1.9; y: 2.5 }, + ScatterElement { x: 1.9; y: 3.4 }, + ScatterElement { x: 2.9; y: 1.4 }, + ScatterElement { x: 2.9; y: 2.4 }, + ScatterElement { x: 3.1; y: 5.3 }, + ScatterElement { x: 4.1; y: 3.7 } + ] + Component.onCompleted: { + console.log("onCompleted " + data); +// console.log("onCompleted " + data.get(0)); +// console.log("onCompleted " + data.get(0).x); +// var element = {"x": 9.9, "y": 8.5}; +// data.append(element); + } + } + // Series { -// labels: ["point1", "point2", "point3", "point4", "point5"] -// datax: [2, 1.5, 3, 3, 3] // seriesType: Series.SeriesTypePie // } - Series { - seriesType: Series.SeriesTypePie - //model: listModelForPie - //seriesData: {11.0, 6.4, 12.6, 22.4} - //seriesLabels: {"Strawberry", "Blackberry", "Apple", "Pumpkin"} - } - Series { -// data: {[1.2], "y":2.2 } - seriesType: Series.SeriesTypeScatter - } - Series { - seriesType: Series.SeriesTypeLine - } +// Series { +// seriesType: Series.SeriesTypeLine +// } // TODO: // Series { // seriesType: Series.SeriesTypeBar