From 122f80368c30b143d6734ff24f14197562f75bfc 2012-05-14 14:13:03 From: Tero Ahola Date: 2012-05-14 14:13:03 Subject: [PATCH] QML area series to use models --- diff --git a/demos/qmlchart/qml/qmlchart/View1.qml b/demos/qmlchart/qml/qmlchart/View1.qml index 0cfbda2..d509572 100644 --- a/demos/qmlchart/qml/qmlchart/View1.qml +++ b/demos/qmlchart/qml/qmlchart/View1.qml @@ -26,7 +26,7 @@ Rectangle { ChartView { id: chart - title: "Car brand shares in Finland" + title: "Top-5 car brand shares in Finland" anchors.top: parent.top anchors.bottom: button.top anchors.left: parent.left diff --git a/demos/qmlchart/qml/qmlchart/View3.qml b/demos/qmlchart/qml/qmlchart/View3.qml index 857ad3a..b85e6fd 100644 --- a/demos/qmlchart/qml/qmlchart/View3.qml +++ b/demos/qmlchart/qml/qmlchart/View3.qml @@ -33,99 +33,83 @@ Rectangle { "6", "2006", "7", "2007", "8", "2008", "9", "2009", "10", "2010", "11", "2011"] AreaSeries { - name: "Swedish" - points: [ - XyPoint { x: 0; y: 1 }, - XyPoint { x: 1; y: 1 }, - XyPoint { x: 2; y: 3 }, - XyPoint { x: 3; y: 3 }, - XyPoint { x: 4; y: 2 }, - XyPoint { x: 5; y: 0 }, - XyPoint { x: 6; y: 2 }, - XyPoint { x: 7; y: 1 }, - XyPoint { x: 8; y: 2 }, - XyPoint { x: 9; y: 1 }, - XyPoint { x: 10; y: 3 }, - XyPoint { x: 11; y: 3 } - ] - lowerPoints: [ - XyPoint { x: 0; y: 0 }, - XyPoint { x: 1; y: 0 }, - XyPoint { x: 2; y: 0 }, - XyPoint { x: 3; y: 0 }, - XyPoint { x: 4; y: 0 }, - XyPoint { x: 5; y: 0 }, - XyPoint { x: 6; y: 0 }, - XyPoint { x: 7; y: 0 }, - XyPoint { x: 8; y: 0 }, - XyPoint { x: 9; y: 0 }, - XyPoint { x: 10; y: 0 }, - XyPoint { x: 11; y: 0 } - ] + name: "Russian" + upperModel: russianModel + lowerModel: zerosModel } - AreaSeries { - name: "Russian" - points: [ - XyPoint { x: 0; y: 1 }, - XyPoint { x: 1; y: 1 }, - XyPoint { x: 2; y: 1 }, - XyPoint { x: 3; y: 1 }, - XyPoint { x: 4; y: 1 }, - XyPoint { x: 5; y: 0 }, - XyPoint { x: 6; y: 1 }, - XyPoint { x: 7; y: 1 }, - XyPoint { x: 8; y: 4 }, - XyPoint { x: 9; y: 3 }, - XyPoint { x: 10; y: 2 }, - XyPoint { x: 11; y: 1 } - ] - lowerPoints: [ - XyPoint { x: 0; y: 0 }, - XyPoint { x: 1; y: 0 }, - XyPoint { x: 2; y: 0 }, - XyPoint { x: 3; y: 0 }, - XyPoint { x: 4; y: 0 }, - XyPoint { x: 5; y: 0 }, - XyPoint { x: 6; y: 0 }, - XyPoint { x: 7; y: 0 }, - XyPoint { x: 8; y: 0 }, - XyPoint { x: 9; y: 0 }, - XyPoint { x: 10; y: 0 }, - XyPoint { x: 11; y: 0 } - ] + name: "Swedish" + upperModel: swedishModel + lowerModel: zerosModel } - AreaSeries { name: "Finnish" - points: [ - XyPoint { x: 0; y: 0 }, - XyPoint { x: 1; y: 0 }, - XyPoint { x: 2; y: 0 }, - XyPoint { x: 3; y: 0 }, - XyPoint { x: 4; y: 0 }, - XyPoint { x: 5; y: 0 }, - XyPoint { x: 6; y: 1 }, - XyPoint { x: 7; y: 0 }, - XyPoint { x: 8; y: 0 }, - XyPoint { x: 9; y: 0 }, - XyPoint { x: 10; y: 0 }, - XyPoint { x: 11; y: 1 } - ] - lowerPoints: [ - XyPoint { x: 0; y: 0 }, - XyPoint { x: 1; y: 0 }, - XyPoint { x: 2; y: 0 }, - XyPoint { x: 3; y: 0 }, - XyPoint { x: 4; y: 0 }, - XyPoint { x: 5; y: 0 }, - XyPoint { x: 6; y: 0 }, - XyPoint { x: 7; y: 0 }, - XyPoint { x: 8; y: 0 }, - XyPoint { x: 9; y: 0 }, - XyPoint { x: 10; y: 0 }, - XyPoint { x: 11; y: 0 } - ] + upperModel: finnishModel + lowerModel: zerosModel } } + + XYModel { + id: zerosModel + XyPoint { x: 0; y: 0 } + XyPoint { x: 1; y: 0 } + XyPoint { x: 2; y: 0 } + XyPoint { x: 3; y: 0 } + XyPoint { x: 4; y: 0 } + XyPoint { x: 5; y: 0 } + XyPoint { x: 6; y: 0 } + XyPoint { x: 7; y: 0 } + XyPoint { x: 8; y: 0 } + XyPoint { x: 9; y: 0 } + XyPoint { x: 10; y: 0 } + XyPoint { x: 11; y: 0 } + } + + XYModel { + id: russianModel + XyPoint { x: 0; y: 1 } + XyPoint { x: 1; y: 1 } + XyPoint { x: 2; y: 1 } + XyPoint { x: 3; y: 1 } + XyPoint { x: 4; y: 1 } + XyPoint { x: 5; y: 0 } + XyPoint { x: 6; y: 1 } + XyPoint { x: 7; y: 1 } + XyPoint { x: 8; y: 4 } + XyPoint { x: 9; y: 3 } + XyPoint { x: 10; y: 2 } + XyPoint { x: 11; y: 1 } + } + + XYModel { + id: swedishModel + XyPoint { x: 0; y: 1 } + XyPoint { x: 1; y: 1 } + XyPoint { x: 2; y: 3 } + XyPoint { x: 3; y: 3 } + XyPoint { x: 4; y: 2 } + XyPoint { x: 5; y: 0 } + XyPoint { x: 6; y: 2 } + XyPoint { x: 7; y: 1 } + XyPoint { x: 8; y: 2 } + XyPoint { x: 9; y: 1 } + XyPoint { x: 10; y: 3 } + XyPoint { x: 11; y: 3 } + } + + XYModel { + id: finnishModel + XyPoint { x: 0; y: 0 } + XyPoint { x: 1; y: 0 } + XyPoint { x: 2; y: 0 } + XyPoint { x: 3; y: 0 } + XyPoint { x: 4; y: 0 } + XyPoint { x: 5; y: 0 } + XyPoint { x: 6; y: 1 } + XyPoint { x: 7; y: 0 } + XyPoint { x: 8; y: 0 } + XyPoint { x: 9; y: 0 } + XyPoint { x: 10; y: 0 } + XyPoint { x: 11; y: 1 } + } } diff --git a/qmlplugin/declarativeareaseries.cpp b/qmlplugin/declarativeareaseries.cpp index 86ca0c3..3330c07 100644 --- a/qmlplugin/declarativeareaseries.cpp +++ b/qmlplugin/declarativeareaseries.cpp @@ -21,7 +21,6 @@ #include "declarativeareaseries.h" #include "declarativechart.h" #include "qchart.h" -#include "qlineseries.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -30,34 +29,40 @@ DeclarativeAreaSeries::DeclarativeAreaSeries(QObject *parent) : { } -QDeclarativeListProperty DeclarativeAreaSeries::points() +bool DeclarativeAreaSeries::setDeclarativeUpperModel(DeclarativeXyModel *model) { - return QDeclarativeListProperty(this, 0, &DeclarativeAreaSeries::appendPoints); + QAbstractItemModel *m = qobject_cast(model); + bool value(false); + if (m) { + upperSeries()->setModel(m); + upperSeries()->setModelMapping(0, 1, Qt::Vertical); + } else { + qWarning("DeclarativeAreaSeries: Illegal model"); + } + return value; } -QDeclarativeListProperty DeclarativeAreaSeries::lowerPoints() +DeclarativeXyModel *DeclarativeAreaSeries::declarativeUpperModel() { - return QDeclarativeListProperty(this, 0, &DeclarativeAreaSeries::appendLowerPoints); + return qobject_cast(upperSeries()->model()); } -void DeclarativeAreaSeries::appendPoints(QDeclarativeListProperty *list, - DeclarativeXyPoint *element) +bool DeclarativeAreaSeries::setDeclarativeLowerModel(DeclarativeXyModel *model) { - QAreaSeries *series = qobject_cast(list->object); - if (series) { - QLineSeries *upper = series->upperSeries(); - upper->append(element->x(), element->y()); + QAbstractItemModel *m = qobject_cast(model); + bool value(false); + if (m) { + lowerSeries()->setModel(m); + lowerSeries()->setModelMapping(0, 1, Qt::Vertical); + } else { + qWarning("DeclarativeAreaSeries: Illegal model"); } + return value; } -void DeclarativeAreaSeries::appendLowerPoints(QDeclarativeListProperty *list, - DeclarativeXyPoint *element) +DeclarativeXyModel *DeclarativeAreaSeries::declarativeLowerModel() { - QAreaSeries *series = qobject_cast(list->object); - if (series) { - QLineSeries *lower = series->lowerSeries(); - lower->append(element->x(), element->y()); - } + return qobject_cast(lowerSeries()->model()); } #include "moc_declarativeareaseries.cpp" diff --git a/qmlplugin/declarativeareaseries.h b/qmlplugin/declarativeareaseries.h index f2a41ab..b2dd537 100644 --- a/qmlplugin/declarativeareaseries.h +++ b/qmlplugin/declarativeareaseries.h @@ -23,6 +23,7 @@ #include "qchartglobal.h" #include "qareaseries.h" +#include "declarativelineseries.h" #include "declarativexyseries.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -30,21 +31,17 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class DeclarativeAreaSeries : public QAreaSeries, public DeclarativeXySeries { Q_OBJECT - Q_PROPERTY(QDeclarativeListProperty points READ points) - Q_PROPERTY(QDeclarativeListProperty lowerPoints READ lowerPoints) + Q_PROPERTY(DeclarativeXyModel *upperModel READ declarativeUpperModel WRITE setDeclarativeUpperModel) + Q_PROPERTY(DeclarativeXyModel *lowerModel READ declarativeLowerModel WRITE setDeclarativeLowerModel) public: explicit DeclarativeAreaSeries(QObject *parent = 0); public: - QDeclarativeListProperty points(); - QDeclarativeListProperty lowerPoints(); - -public Q_SLOTS: - static void appendPoints(QDeclarativeListProperty *list, - DeclarativeXyPoint *element); - static void appendLowerPoints(QDeclarativeListProperty *list, - DeclarativeXyPoint *element); + bool setDeclarativeUpperModel(DeclarativeXyModel *model); + DeclarativeXyModel *declarativeUpperModel(); + bool setDeclarativeLowerModel(DeclarativeXyModel *model); + DeclarativeXyModel *declarativeLowerModel(); }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativemodel.cpp b/qmlplugin/declarativemodel.cpp index 3077d00..ab1b285 100644 --- a/qmlplugin/declarativemodel.cpp +++ b/qmlplugin/declarativemodel.cpp @@ -91,19 +91,16 @@ DeclarativePieModel::DeclarativePieModel(QObject *parent) : void DeclarativePieModel::append(QPieSlice* slice) { - qDebug() << "DeclarativePieModel::append:" << slice->label() << " " << slice->value(); - qDebug() << "rowCount:" << rowCount(); - qDebug() << "coolCount:" << columnCount(); +// qDebug() << "DeclarativePieModel::append:" << slice->label() << " " << slice->value(); insertRow(rowCount()); - qDebug() << "new rowCount:" << rowCount(); - qDebug() << setData(createIndex(rowCount() - 1, 0), slice->value()); - qDebug() << setData(createIndex(rowCount() - 1, 1), slice->label()); + setData(createIndex(rowCount() - 1, 0), slice->value()); + setData(createIndex(rowCount() - 1, 1), slice->label()); } void DeclarativePieModel::append(QVariantList slices) { - qDebug() << "append:" << slices; +// qDebug() << "append:" << slices; QString label = ""; for (int i(0); i < slices.count(); i++) { if (i % 2) {