From 86b61f579398c7be3e7e3cc7a4434e3427a3d71e 2012-05-29 12:49:06 From: Tero Ahola Date: 2012-05-29 12:49:06 Subject: [PATCH] XY model mappers to QML api --- diff --git a/demos/qmlchart/qml/qmlchart/View4.qml b/demos/qmlchart/qml/qmlchart/View4.qml index 2c54a88..04f3f6c 100644 --- a/demos/qmlchart/qml/qmlchart/View4.qml +++ b/demos/qmlchart/qml/qmlchart/View4.qml @@ -28,6 +28,8 @@ Rectangle { title: "Scatters" anchors.fill: parent theme: ChartView.ChartThemeBlueCerulean + axisX.max: 4 + axisY.max: 4 ScatterSeries { id: scatter1 diff --git a/demos/qmlcustommodel/qml/qmlcustommodel/main.qml b/demos/qmlcustommodel/qml/qmlcustommodel/main.qml index 3a13382..fe79e06 100644 --- a/demos/qmlcustommodel/qml/qmlcustommodel/main.qml +++ b/demos/qmlcustommodel/qml/qmlcustommodel/main.qml @@ -31,28 +31,39 @@ Rectangle { title: "Custom model example" anchors.fill: parent theme: ChartView.ChartThemeLight + axisX.max: 20 + axisY.max: 20 // For dynamic data we use a custom data model derived from QAbstractiItemModel CustomModel { id: customModel - CustomModelElement { values: ["Volkswagen", 13.5, 4.4] } - CustomModelElement { values: ["Toyota", 10.9, 4.2] } - CustomModelElement { values: ["Ford", 8.6, 3.0] } - CustomModelElement { values: ["Skoda", 8.2, 1.9] } - CustomModelElement { values: ["Volvo", 6.8, 1.5] } + CustomModelElement { values: [index, "Manufacturer", 1, 2] } + CustomModelElement { values: [1, "Volkswagen", 13.5, 12.5] } + CustomModelElement { values: [2, "Toyota", 10.9, 9.9] } + CustomModelElement { values: [3, "Ford", 8.6, 7.6] } + CustomModelElement { values: [4, "Skoda", 8.2, 7.2] } + CustomModelElement { values: [5, "Volvo", 6.8, 5.8] } } LineSeries { - name: "line" - - // TODO: the new mapper api -// VXYModelMapper { -// model: customModel -// xColumn: 0 -// yColumn: 1 -// } + name: "Volkswagen" + HXYModelMapper { + model: customModel + xRow: 0 + yRow: 1 + first: 2 + } } + LineSeries { + name: "Toyota" + HXYModelMapper { + model: customModel + xRow: 0 + yRow: 2 + first: 2 + } + } PieSeries { id: pieSeries @@ -64,15 +75,22 @@ Rectangle { VPieModelMapper { series: pieSeries model: customModel - labelsColumn: 0 - valuesColumn: 1 + labelsColumn: 1 + valuesColumn: 2 + first: 1 } -// AreaSeries { -// name: "area" -// upperSeries: LineSeries {} -// lowerSeries: LineSeries {} -// } + AreaSeries { + name: "Ford" + upperSeries: LineSeries { + HXYModelMapper { + model: customModel + xRow: 0 + yRow: 3 + first: 2 + } + } + } // BarSeries { // model: customModel diff --git a/plugins/declarative/declarativechart.cpp b/plugins/declarative/declarativechart.cpp index 4745afc..5a31684 100644 --- a/plugins/declarative/declarativechart.cpp +++ b/plugins/declarative/declarativechart.cpp @@ -35,7 +35,7 @@ DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) m_legend(LegendTop) { setFlag(QGraphicsItem::ItemHasNoContents, false); - m_chart->axisX()->setNiceNumbersEnabled(false); +// m_chart->axisX()->setNiceNumbersEnabled(false); } DeclarativeChart::~DeclarativeChart() diff --git a/plugins/declarative/declarativelineseries.h b/plugins/declarative/declarativelineseries.h index 869d63e..68c79d7 100644 --- a/plugins/declarative/declarativelineseries.h +++ b/plugins/declarative/declarativelineseries.h @@ -26,12 +26,14 @@ #include "declarativexyseries.h" #include #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class DeclarativeLineSeries : public QLineSeries, public DeclarativeXySeries +class DeclarativeLineSeries : public QLineSeries, public DeclarativeXySeries, public QDeclarativeParserStatus { Q_OBJECT + Q_INTERFACES(QDeclarativeParserStatus) Q_PROPERTY(QColor color READ penColor WRITE setPenColor) Q_PROPERTY(QDeclarativeListProperty declarativeChildren READ declarativeChildren) Q_CLASSINFO("DefaultProperty", "declarativeChildren") @@ -40,6 +42,10 @@ public: explicit DeclarativeLineSeries(QObject *parent = 0); QDeclarativeListProperty declarativeChildren(); +public: // from QDeclarativeParserStatus + void classBegin() { DeclarativeXySeries::classBegin(); } + void componentComplete() { DeclarativeXySeries::componentComplete(); } + public: // from QLineSeries Q_INVOKABLE void append(qreal x, qreal y) { QLineSeries::append(x, y); } Q_INVOKABLE void remove(qreal x, qreal y) { QLineSeries::remove(x, y); } diff --git a/plugins/declarative/declarativescatterseries.h b/plugins/declarative/declarativescatterseries.h index 2ea0ed5..27e0dce 100644 --- a/plugins/declarative/declarativescatterseries.h +++ b/plugins/declarative/declarativescatterseries.h @@ -25,12 +25,14 @@ #include "qscatterseries.h" #include "declarativexyseries.h" #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class DeclarativeScatterSeries : public QScatterSeries, public DeclarativeXySeries +class DeclarativeScatterSeries : public QScatterSeries, public DeclarativeXySeries, public QDeclarativeParserStatus { Q_OBJECT + Q_INTERFACES(QDeclarativeParserStatus) Q_PROPERTY(QColor color READ brushColor WRITE setBrushColor) Q_PROPERTY(QColor borderColor READ penColor WRITE setPenColor) Q_PROPERTY(QDeclarativeListProperty declarativeChildren READ declarativeChildren) @@ -42,6 +44,10 @@ public: QColor brushColor(); void setBrushColor(QColor color); +public: // from QDeclarativeParserStatus + void classBegin() { DeclarativeXySeries::classBegin(); } + void componentComplete() { DeclarativeXySeries::componentComplete(); } + public: // from QScatterSeries Q_INVOKABLE void append(qreal x, qreal y) { QScatterSeries::append(x, y); } Q_INVOKABLE void remove(qreal x, qreal y) { QScatterSeries::remove(x, y); } diff --git a/plugins/declarative/declarativesplineseries.h b/plugins/declarative/declarativesplineseries.h index 2800b0a..f5ca91d 100644 --- a/plugins/declarative/declarativesplineseries.h +++ b/plugins/declarative/declarativesplineseries.h @@ -26,12 +26,14 @@ #include "declarativexyseries.h" #include #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class DeclarativeSplineSeries : public QSplineSeries, public DeclarativeXySeries +class DeclarativeSplineSeries : public QSplineSeries, public DeclarativeXySeries, public QDeclarativeParserStatus { Q_OBJECT + Q_INTERFACES(QDeclarativeParserStatus) Q_PROPERTY(QColor color READ penColor WRITE setPenColor) Q_PROPERTY(QDeclarativeListProperty declarativeChildren READ declarativeChildren) Q_CLASSINFO("DefaultProperty", "declarativeChildren") @@ -40,6 +42,10 @@ public: explicit DeclarativeSplineSeries(QObject *parent = 0); QDeclarativeListProperty declarativeChildren(); +public: // from QDeclarativeParserStatus + void classBegin() { DeclarativeXySeries::classBegin(); } + void componentComplete() { DeclarativeXySeries::componentComplete(); } + public: // from QSplineSeries Q_INVOKABLE void append(qreal x, qreal y) { QSplineSeries::append(x, y); } Q_INVOKABLE void remove(qreal x, qreal y) { QSplineSeries::remove(x, y); } diff --git a/plugins/declarative/declarativexyseries.cpp b/plugins/declarative/declarativexyseries.cpp index 00af2e3..ac3d436 100644 --- a/plugins/declarative/declarativexyseries.cpp +++ b/plugins/declarative/declarativexyseries.cpp @@ -22,7 +22,8 @@ #include "declarativexyseries.h" #include "declarativechart.h" #include -#include +#include +#include #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -35,6 +36,29 @@ DeclarativeXySeries::~DeclarativeXySeries() { } +void DeclarativeXySeries::classBegin() +{ +} + +void DeclarativeXySeries::componentComplete() +{ + // All the inherited objects must be of type QXYSeries, so it is safe to cast + QXYSeries *series = reinterpret_cast(this); + foreach(QObject *child, series->children()) { + if (qobject_cast(child)) { + // TODO: +// series->append(qobject_cast(child)); + } else if(qobject_cast(child)) { + QVXYModelMapper *mapper = qobject_cast(child); + mapper->setSeries(series); + } else if(qobject_cast(child)) { + QHXYModelMapper *mapper = qobject_cast(child); + mapper->setSeries(series); + } + } +} + + QColor DeclarativeXySeries::penColor() { // All the inherited objects must be of type QXYSeries, so it is safe to cast diff --git a/plugins/declarative/declarativexyseries.h b/plugins/declarative/declarativexyseries.h index 8b7d761..e699a1f 100644 --- a/plugins/declarative/declarativexyseries.h +++ b/plugins/declarative/declarativexyseries.h @@ -37,6 +37,8 @@ public: ~DeclarativeXySeries(); public: + void classBegin(); + void componentComplete(); QColor penColor(); void setPenColor(QColor color); DeclarativeXyPoint *at(int index); diff --git a/plugins/declarative/plugin.cpp b/plugins/declarative/plugin.cpp index 6dfb54e..5c93250 100644 --- a/plugins/declarative/plugin.cpp +++ b/plugins/declarative/plugin.cpp @@ -30,9 +30,10 @@ #include "declarativescatterseries.h" #include "declarativebarseries.h" #include "declarativepieseries.h" +#include +#include #include #include -#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -55,6 +56,10 @@ public: qmlRegisterType(uri, 1, 0, "PieSeries"); qmlRegisterType(uri, 1, 0, "PieSlice"); qmlRegisterType(uri, 1, 0, "BarSet"); + qmlRegisterType(uri, 1, 0, "HXYModelMapper"); + qmlRegisterType(uri, 1, 0, "VXYModelMapper"); + qmlRegisterType(uri, 1, 0, "HPieModelMapper"); + qmlRegisterType(uri, 1, 0, "VPieModelMapper"); qmlRegisterUncreatableType(uri, 1, 0, "QScatterSeries", @@ -63,11 +68,10 @@ public: QLatin1String("Trying to create uncreatable: QPieSeries.")); qmlRegisterUncreatableType(uri, 1, 0, "AbstractItemModel", QLatin1String("Trying to create uncreatable: AbstractItemModel.")); + qmlRegisterUncreatableType(uri, 1, 0, "XYModelMapper", + QLatin1String("Trying to create uncreatable: XYModelMapper.")); qmlRegisterUncreatableType(uri, 1, 0, "PieModelMapper", QLatin1String("Trying to create uncreatable: PieModelMapper.")); - qmlRegisterType(uri, 1, 0, "HPieModelMapper"); - qmlRegisterType(uri, 1, 0, "VPieModelMapper"); - qmlRegisterUncreatableType(uri, 1, 0, "AbstractSeries", QLatin1String("Trying to create uncreatable: AbstractSeries.")); qmlRegisterUncreatableType(uri, 1, 0, "Axis", diff --git a/src/xychart/qxymodelmapper.cpp b/src/xychart/qxymodelmapper.cpp index 030231e..4b12f3b 100644 --- a/src/xychart/qxymodelmapper.cpp +++ b/src/xychart/qxymodelmapper.cpp @@ -233,6 +233,9 @@ void QXYModelMapperPrivate::handlePointReplaced(int pointPos) void QXYModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) { + if (m_model == 0 || m_series == 0) + return; + if (m_modelSignalsBlock) return;