diff --git a/qmlplugin/declarativebarseries.h b/qmlplugin/declarativebarseries.h index ec5e0f3..50fbc55 100644 --- a/qmlplugin/declarativebarseries.h +++ b/qmlplugin/declarativebarseries.h @@ -2,7 +2,6 @@ #define DECLARATIVEBARSERIES_H #include "qchartglobal.h" -#include "scatterelement.h" // TODO: rename header #include QTCOMMERCIALCHART_BEGIN_NAMESPACE diff --git a/qmlplugin/declarativechart.cpp b/qmlplugin/declarativechart.cpp index 77b4ede..983263b 100644 --- a/qmlplugin/declarativechart.cpp +++ b/qmlplugin/declarativechart.cpp @@ -10,6 +10,11 @@ DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) setFlag(QGraphicsItem::ItemHasNoContents, false); } +DeclarativeChart::~DeclarativeChart() +{ + delete m_chart; +} + DeclarativeChart::ChartTheme DeclarativeChart::theme() { return (ChartTheme) m_chart->chartTheme(); diff --git a/qmlplugin/declarativechart.h b/qmlplugin/declarativechart.h index d558a9f..24afe2f 100644 --- a/qmlplugin/declarativechart.h +++ b/qmlplugin/declarativechart.h @@ -27,6 +27,7 @@ public: ThemeScientific }; DeclarativeChart(QDeclarativeItem *parent = 0); + ~DeclarativeChart(); public: // From QDeclarativeItem/QGraphicsItem void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry); diff --git a/qmlplugin/declarativelineseries.cpp b/qmlplugin/declarativelineseries.cpp index 558aa23..1ed200a 100644 --- a/qmlplugin/declarativelineseries.cpp +++ b/qmlplugin/declarativelineseries.cpp @@ -26,7 +26,7 @@ void DeclarativeLineSeries::setParentForSeries() m_series = new QLineSeries(); Q_ASSERT(m_series); for (int i(0); i < m_data.count(); i++) { - ScatterElement *element = m_data.at(i); + DeclarativeXyPoint *element = m_data.at(i); m_series->add(element->x(), element->y()); } chart->addSeries(m_series); @@ -34,13 +34,13 @@ void DeclarativeLineSeries::setParentForSeries() } } -QDeclarativeListProperty DeclarativeLineSeries::data() +QDeclarativeListProperty DeclarativeLineSeries::data() { - return QDeclarativeListProperty(this, 0, &DeclarativeLineSeries::appendData); + return QDeclarativeListProperty(this, 0, &DeclarativeLineSeries::appendData); } -void DeclarativeLineSeries::appendData(QDeclarativeListProperty *list, - ScatterElement *element) +void DeclarativeLineSeries::appendData(QDeclarativeListProperty *list, + DeclarativeXyPoint *element) { DeclarativeLineSeries *series = qobject_cast(list->object); if (series) { diff --git a/qmlplugin/declarativelineseries.h b/qmlplugin/declarativelineseries.h index 92bb1b1..9ac67a8 100644 --- a/qmlplugin/declarativelineseries.h +++ b/qmlplugin/declarativelineseries.h @@ -2,7 +2,7 @@ #define DECLARATIVELINESERIES_H #include "qchartglobal.h" -#include "scatterelement.h" // TODO: rename header +#include "declarativexypoint.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -13,17 +13,17 @@ class QLineSeries; class DeclarativeLineSeries : public QDeclarativeItem { Q_OBJECT - Q_PROPERTY(QDeclarativeListProperty data READ data) + Q_PROPERTY(QDeclarativeListProperty data READ data) public: explicit DeclarativeLineSeries(QDeclarativeItem *parent = 0); - QDeclarativeListProperty data(); + QDeclarativeListProperty data(); signals: public slots: - static void appendData(QDeclarativeListProperty *list, - ScatterElement *element); + static void appendData(QDeclarativeListProperty *list, + DeclarativeXyPoint *element); private slots: void setParentForSeries(); @@ -31,7 +31,7 @@ private slots: private: QChart *m_chart; QLineSeries *m_series; - QList m_data; + QList m_data; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativescatterseries.cpp b/qmlplugin/declarativescatterseries.cpp index 5780cca..ef72461 100644 --- a/qmlplugin/declarativescatterseries.cpp +++ b/qmlplugin/declarativescatterseries.cpp @@ -1,5 +1,6 @@ #include "declarativescatterseries.h" #include "declarativechart.h" +#include "declarativetablemodel.h" #include "qchart.h" #include "qscatterseries.h" @@ -8,47 +9,51 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE DeclarativeScatterSeries::DeclarativeScatterSeries(QDeclarativeItem *parent) : QDeclarativeItem(parent), m_chart(0), - m_series(0) + m_series(0), + m_model(0), + m_xColumn(0), + m_yColumn(1) { setFlag(QGraphicsItem::ItemHasNoContents, false); - connect(this, SIGNAL(parentChanged()), - this, SLOT(setParentForSeries())); } -void DeclarativeScatterSeries::setParentForSeries() +DeclarativeScatterSeries::~DeclarativeScatterSeries() { - if (!m_series) - initSeries(); } -void DeclarativeScatterSeries::initSeries() +void DeclarativeScatterSeries::componentComplete() { 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_chart = qobject_cast(declarativeChart->m_chart); + qDebug() << "creating scatter series for chart: " << m_chart; + Q_ASSERT(m_chart); m_series = new QScatterSeries(); - Q_ASSERT(m_series); +// if (!m_model) +// m_model = new DeclarativeTableModel(); + if (m_model) { + m_series->setModel(m_model); + m_series->setModelMapping(m_xColumn, m_yColumn); + } for (int i(0); i < m_data.count(); i++) { - ScatterElement *element = m_data.at(i); + DeclarativeXyPoint *element = m_data.at(i); *m_series << QPointF(element->x(), element->y()); } - chart->addSeries(m_series); + m_chart->addSeries(m_series); } } -QDeclarativeListProperty DeclarativeScatterSeries::data() +QDeclarativeListProperty DeclarativeScatterSeries::data() { - return QDeclarativeListProperty(this, 0, + return QDeclarativeListProperty(this, 0, &DeclarativeScatterSeries::appendData); } -void DeclarativeScatterSeries::appendData(QDeclarativeListProperty *list, - ScatterElement *element) +void DeclarativeScatterSeries::appendData(QDeclarativeListProperty *list, + DeclarativeXyPoint *element) { DeclarativeScatterSeries *series = qobject_cast(list->object); qDebug() << "appendData: " << series; @@ -63,6 +68,41 @@ void DeclarativeScatterSeries::appendData(QDeclarativeListPropertymodel(); + else + return m_model; +} + +void DeclarativeScatterSeries::setModel(DeclarativeTableModel *model) +{ + m_model = model; + if (m_chart && m_series) { + // Hack: remove and add the series to force an update for the chart range + m_chart->removeSeries(m_series); + m_series = new QScatterSeries(); + m_series->setModel(m_model); + m_series->setModelMapping(m_xColumn, m_yColumn); + m_chart->addSeries(m_series); + } +} + +void DeclarativeScatterSeries::setXColumn(int xColumn) +{ + m_xColumn = xColumn; + if (m_series && m_series->model()) + m_series->setModelMapping(m_xColumn, m_yColumn); +} + +void DeclarativeScatterSeries::setYColumn(int yColumn) +{ + m_yColumn = yColumn; + if (m_series && m_series->model()) + m_series->setModelMapping(m_xColumn, m_yColumn); +} + #include "moc_declarativescatterseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativescatterseries.h b/qmlplugin/declarativescatterseries.h index 1ae0af2..385e255 100644 --- a/qmlplugin/declarativescatterseries.h +++ b/qmlplugin/declarativescatterseries.h @@ -2,38 +2,58 @@ #define DECLARATIVESCATTERSERIES_H #include "qchartglobal.h" -#include "scatterelement.h" +#include "declarativetablemodel.h" +#include "declarativexypoint.h" #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE class QChart; class QScatterSeries; -class DeclarativeScatterSeries : public QDeclarativeItem +class DeclarativeScatterSeries : public QDeclarativeItem//, public QDeclarativeParserStatus { Q_OBJECT - Q_PROPERTY(QDeclarativeListProperty data READ data) + Q_PROPERTY(QDeclarativeListProperty data READ data) + Q_PROPERTY(DeclarativeTableModel *model READ model WRITE setModel) +// Q_PROPERTY(QObject *listModel READ listModel WRITE setListModel) + Q_PROPERTY(int xColumn READ xColumn WRITE setXColumn) + Q_PROPERTY(int yColumn READ yColumn WRITE setYColumn) public: explicit DeclarativeScatterSeries(QDeclarativeItem *parent = 0); - QDeclarativeListProperty data(); + ~DeclarativeScatterSeries(); + +public: // from QDeclarativeParserStatus + void componentComplete(); + +public: + QDeclarativeListProperty data(); + DeclarativeTableModel *model(); + void setModel(DeclarativeTableModel *model); +//QObject *listModel(); +//void setListModel(QObject *model); + int xColumn() { return m_xColumn; } + void setXColumn(int xColumn); + int yColumn() { return m_yColumn; } + void setYColumn(int yColumn); signals: public slots: - static void appendData(QDeclarativeListProperty *list, - ScatterElement *element); + static void appendData(QDeclarativeListProperty *list, + DeclarativeXyPoint *element); private slots: - void setParentForSeries(); public: - void initSeries(); - - QChart *m_chart; - QScatterSeries *m_series; - QList m_data; + QChart *m_chart; // not owned + QScatterSeries *m_series; // not owned + DeclarativeTableModel *m_model; // not owned + QList m_data; + int m_xColumn; + int m_yColumn; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativetablemodel.cpp b/qmlplugin/declarativetablemodel.cpp new file mode 100644 index 0000000..74abfbb --- /dev/null +++ b/qmlplugin/declarativetablemodel.cpp @@ -0,0 +1,52 @@ +#include "declarativetablemodel.h" +#include + +DeclarativeTableModel::DeclarativeTableModel(QObject *parent) : + QAbstractTableModel(parent) +{ +} + +int DeclarativeTableModel::rowCount(const QModelIndex &/*parent*/) const +{ + return 5; +} + +int DeclarativeTableModel::columnCount(const QModelIndex &/*parent*/) const +{ + return 4; +} + +QVariant DeclarativeTableModel::headerData(int /*section*/, Qt::Orientation /*orientation*/, int /*role*/) const +{ + return QString("headerData"); +} + +QVariant DeclarativeTableModel::data(const QModelIndex &index, int /*role*/) const +{ +// return QString("data") + QString::number(index.row()) + QString::number(index.column()); + QObject *element = this->findChild("ListElement"); + if (element) + qDebug() << "property: " << element->property("time"); +// element->setProperty("time", "0.1"); + return index.row() + index.column(); +} + +bool DeclarativeTableModel::setData(const QModelIndex &/*index*/, const QVariant &/*value*/, int /*role*/) +{ + return true; +} + +Qt::ItemFlags DeclarativeTableModel::flags(const QModelIndex &/*index*/) const +{ + return 0; +} + +bool DeclarativeTableModel::insertRows(int /*row*/, int /*count*/, const QModelIndex &/*parent*/) +{ + return true; +} + +bool DeclarativeTableModel::removeRows(int /*row*/, int /*count*/, const QModelIndex &/*parent*/) +{ + return true; +} diff --git a/qmlplugin/declarativetablemodel.h b/qmlplugin/declarativetablemodel.h new file mode 100644 index 0000000..20b98ae --- /dev/null +++ b/qmlplugin/declarativetablemodel.h @@ -0,0 +1,28 @@ +#ifndef DECLARATIVETABLEMODEL_H +#define DECLARATIVETABLEMODEL_H + +#include + +class DeclarativeTableModel : public QAbstractTableModel +{ + Q_OBJECT +public: + explicit DeclarativeTableModel(QObject *parent = 0); + +public: + int rowCount ( const QModelIndex & parent = QModelIndex() ) const; + int columnCount ( const QModelIndex & parent = QModelIndex() ) const; + QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole ); + Qt::ItemFlags flags ( const QModelIndex & index ) const; + bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() ); + bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() ); + +signals: + +public slots: + +}; + +#endif // DECLARATIVETABLEMODEL_H diff --git a/qmlplugin/scatterelement.cpp b/qmlplugin/declarativexypoint.cpp similarity index 62% rename from qmlplugin/scatterelement.cpp rename to qmlplugin/declarativexypoint.cpp index f1c3aa4..c5b2393 100644 --- a/qmlplugin/scatterelement.cpp +++ b/qmlplugin/declarativexypoint.cpp @@ -1,12 +1,12 @@ -#include "scatterelement.h" +#include "declarativexypoint.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE -ScatterElement::ScatterElement(QObject *parent) : +DeclarativeXyPoint::DeclarativeXyPoint(QObject *parent) : QObject(parent) { } -#include "moc_scatterelement.cpp" +#include "moc_declarativexypoint.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/scatterelement.h b/qmlplugin/declarativexypoint.h similarity index 77% rename from qmlplugin/scatterelement.h rename to qmlplugin/declarativexypoint.h index ad91c92..02e9df0 100644 --- a/qmlplugin/scatterelement.h +++ b/qmlplugin/declarativexypoint.h @@ -1,19 +1,19 @@ -#ifndef SCATTERELEMENT_H -#define SCATTERELEMENT_H +#ifndef DECLARATIVE_XY_POINT_H +#define DECLARATIVE_XY_POINT_H #include "qchartglobal.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class ScatterElement : public QObject +class DeclarativeXyPoint : 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); + explicit DeclarativeXyPoint(QObject *parent = 0); void setX(qreal x) {m_x = x;} qreal x() {return m_x;} @@ -27,4 +27,4 @@ public: QTCOMMERCIALCHART_END_NAMESPACE -#endif // SCATTERELEMENT_H +#endif // DECLARATIVE_XY_POINT_H diff --git a/qmlplugin/plugin.cpp b/qmlplugin/plugin.cpp index a540317..0638b7d 100644 --- a/qmlplugin/plugin.cpp +++ b/qmlplugin/plugin.cpp @@ -1,7 +1,8 @@ #include #include #include "declarativechart.h" -#include "scatterelement.h" +#include "declarativetablemodel.h" +#include "declarativexypoint.h" #include "declarativescatterseries.h" #include "declarativelineseries.h" #include "declarativebarseries.h" @@ -18,13 +19,13 @@ public: Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart")); qmlRegisterType(uri, 1, 0, "Chart"); + qmlRegisterType(uri, 1, 0, "ChartTableModel"); + qmlRegisterType(uri, 1, 0, "XyPoint"); qmlRegisterType(uri, 1, 0, "ScatterSeries"); qmlRegisterType(uri, 1, 0, "LineSeries"); qmlRegisterType(uri, 1, 0, "BarSeries"); qmlRegisterType(uri, 1, 0, "PieSeries"); - qmlRegisterType(uri, 1, 0, "ChartPieElement"); - // TODO: rename ScatterElement class to something like "PointElement" - qmlRegisterType(uri, 1, 0, "ChartPointElement"); + qmlRegisterType(uri, 1, 0, "PieSlice"); } }; diff --git a/qmlplugin/qmlplugin.pro b/qmlplugin/qmlplugin.pro index ed2ed41..53e2720 100644 --- a/qmlplugin/qmlplugin.pro +++ b/qmlplugin/qmlplugin.pro @@ -25,17 +25,19 @@ SOURCES += \ plugin.cpp \ declarativechart.cpp \ declarativescatterseries.cpp \ - scatterelement.cpp \ + declarativexypoint.cpp \ declarativepieseries.cpp \ declarativelineseries.cpp \ - declarativebarseries.cpp + declarativebarseries.cpp \ + declarativetablemodel.cpp HEADERS += \ declarativechart.h \ declarativescatterseries.h \ - scatterelement.h \ + declarativexypoint.h \ declarativepieseries.h \ declarativelineseries.h \ - declarativebarseries.h + declarativebarseries.h \ + declarativetablemodel.h TARGETPATH = QtCommercial/Chart target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH diff --git a/test/qmlchart/qml/qmlchart/main.qml b/test/qmlchart/qml/qmlchart/main.qml index 63354f3..374c1a0 100644 --- a/test/qmlchart/qml/qmlchart/main.qml +++ b/test/qmlchart/qml/qmlchart/main.qml @@ -20,12 +20,47 @@ Rectangle { // } Component.onCompleted: { -// console.log("Component.onCompleted: " + chartModel.get(0).dataX); + console.log("model: " + myModel); +// console.log("model:" + myModel.item(0)); +// myModel.insert(1, {"time":1.4; "speed":41.1 }); +// scatter.appendData(); } -// ChartModel { -// id: dynamicData -// } + ListModel { + ListElement { + time: 0.0 + speed: 45.2 + } + } + ChartTableModel { + id: myModel + +// ListElement { +// time: 0.0 +// speed: 45.2 +// } +// ListElement { +// time: 0.5 +// speed: 48.9 +// } +// ListElement { +// time: 1.1 +// speed: 42.6 +// } + +// ChartTableElement { +// time: 0.0 +// speed: 45.2 +// } +// ChartTableElement { +// time: 0.5 +// speed: 48.9 +// } +// ChartTableElement { +// time: 1.1 +// speed: 42.6 +// } + } Chart { id: chart1 @@ -35,19 +70,23 @@ Rectangle { height: parent.height / 2 theme: Chart.ThemeBlueCerulean - BarSeries { +// BarSeries { +// } + + ScatterSeries { + model: myModel +// xColumn: time +// yColumn: speed } // PieSeries { // data: [ -// // TODO: "NnElement" matches the naming convention of for example ListModel... -// // But PieSlice would match the naming of QtCommercial Charts C++ api -// ChartPieElement { label: "Volkswagen"; value: 13.5 }, -// ChartPieElement { label: "Toyota"; value: 10.9 }, -// ChartPieElement { label: "Ford"; value: 8.6 }, -// ChartPieElement { label: "Skoda"; value: 8.2 }, -// ChartPieElement { label: "Volvo"; value: 6.8 }, -// ChartPieElement { label: "Others"; value: 52.0 } +// PieSlice { label: "Volkswagen"; value: 13.5 }, +// PieSlice { label: "Toyota"; value: 10.9 }, +// PieSlice { label: "Ford"; value: 8.6 }, +// PieSlice { label: "Skoda"; value: 8.2 }, +// PieSlice { label: "Volvo"; value: 6.8 }, +// PieSlice { label: "Others"; value: 52.0 } // ] // } } @@ -63,40 +102,41 @@ Rectangle { LineSeries { data: [ - ChartPointElement { x: 0.0; y: 0.0 }, - ChartPointElement { x: 1.1; y: 2.1 }, - ChartPointElement { x: 2.9; y: 4.9 }, - ChartPointElement { x: 3.2; y: 3.0 } + XyPoint { x: 0.0; y: 0.0 }, + XyPoint { x: 1.1; y: 2.1 }, + XyPoint { x: 2.9; y: 4.9 }, + XyPoint { x: 3.2; y: 3.0 } ] } - ScatterSeries { - data: [ - ChartPointElement { x: 1.1; y: 1.1 }, - ChartPointElement { x: 1.1; y: 1.2 }, - ChartPointElement { x: 1.17; y: 1.15 } - ] - } - ScatterSeries { - data: [ - ChartPointElement { x: 1.5; y: 1.5 }, - ChartPointElement { x: 1.5; y: 1.6 }, - ChartPointElement { x: 1.57; y: 1.55 } - ] - } - ScatterSeries { - data: [ - ChartPointElement { x: 2.0; y: 2.0 }, - ChartPointElement { x: 2.0; y: 2.1 }, - ChartPointElement { x: 2.07; y: 2.05 } - ] - } - ScatterSeries { - data: [ - ChartPointElement { x: 2.6; y: 2.6 }, - ChartPointElement { x: 2.6; y: 2.7 }, - ChartPointElement { x: 2.67; y: 2.65 } - ] - } +// ScatterSeries { +// id: scatter +// data: [ +// XyPoint { x: 1.1; y: 1.1 }, +// XyPoint { x: 1.1; y: 1.2 }, +// XyPoint { x: 1.17; y: 1.15 } +// ] +// } +// ScatterSeries { +// data: [ +// XyPoint { x: 1.5; y: 1.5 }, +// XyPoint { x: 1.5; y: 1.6 }, +// XyPoint { x: 1.57; y: 1.55 } +// ] +// } +// ScatterSeries { +// data: [ +// XyPoint { x: 2.0; y: 2.0 }, +// XyPoint { x: 2.0; y: 2.1 }, +// XyPoint { x: 2.07; y: 2.05 } +// ] +// } +// ScatterSeries { +// data: [ +// XyPoint { x: 2.6; y: 2.6 }, +// XyPoint { x: 2.6; y: 2.7 }, +// XyPoint { x: 2.67; y: 2.65 } +// ] +// } } }