From afaf49bc7dc078e8c457ce9697568643b73420e1 2012-05-15 13:57:07 From: Tero Ahola Date: 2012-05-15 13:57:07 Subject: [PATCH] Now using only one declarative model --- diff --git a/demos/qmlchart/qml/qmlchart/View1.qml b/demos/qmlchart/qml/qmlchart/View1.qml index 4031919..1dd35e7 100644 --- a/demos/qmlchart/qml/qmlchart/View1.qml +++ b/demos/qmlchart/qml/qmlchart/View1.qml @@ -25,6 +25,15 @@ Rectangle { anchors.fill: parent property int __explodedIndex: -1 + ChartModel { + id: chartModel + ChartModelRow { values: ["Volkswagen", 13.5] } + ChartModelRow { values: ["Toyota", 10.9] } + ChartModelRow { values: ["Ford", 8.6] } + ChartModelRow { values: ["Skoda", 8.2] } + ChartModelRow { values: ["Volvo", 6.8] } + } + ChartView { id: chart title: "Top-5 car brand shares in Finland" @@ -38,33 +47,34 @@ Rectangle { PieSeries { id: pieSeries - model: PieModel { - id: pieModel - // TODO: initializing properties does not work at the moment, see DeclarativePieModel::append - // TODO: explode range, color, border color, border thickness, font, .. - PieSlice { exploded: true; 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 } - } + model: chartModel + modelMapper.mapLabels: 0 + modelMapper.mapValues: 1 + modelMapper.first: 0 + modelMapper.count: -1 // "Undefined" = -1 by default + modelMapper.orientation: PieModelMapper.Vertical + + // TODO: PieSlice to append the data directly into the mapped columns + //PieSlice { label: "Toyota"; value: 10.9 } } } + Component.onCompleted: { + chartModel.append(["Others", 52.0]); + } + Timer { repeat: true interval: 2000 running: true onTriggered: { - changeSliceExploded(__explodedIndex); - __explodedIndex = (__explodedIndex + 1) % pieModel.count; - changeSliceExploded(__explodedIndex); - } - } + // Set all slices as not exploded + for (var i = 0; i < pieSeries.count; i++) + pieSeries.slice(i).exploded = false; - function changeSliceExploded(index) { - if (index >= 0 && index < pieModel.count) { - pieSeries.slice(index).exploded = !pieSeries.slice(index).exploded; + // Explode one of the slices + __explodedIndex = (__explodedIndex + 1) % pieSeries.count; + pieSeries.slice(__explodedIndex).exploded = true; } } @@ -80,46 +90,24 @@ Rectangle { Text { id: buttonText anchors.centerIn: parent - text: button.state == "" ? "Show others" : "Hide others" + text: "Hide others" } MouseArea { anchors.fill: parent onClicked: { - if (button.state == "") { - // The share of "others" was enabled -> append the data into the model - // TODO: this should also be doable by redefining the range inside the model - button.state = "show"; - pieModel.append(["Others", 52.0]); + if (buttonText.text == "Show others") { + pieSeries.modelMapper.count = -1; + buttonText.text = "Hide others"; } else { - // The share of "others" was disabled -> remove the data from the model - // TODO: this should also be doable by redefining the range inside the model - button.state = ""; - pieModel.removeRow(pieModel.count - 1); + pieSeries.modelMapper.count = 5; + buttonText.text = "Show others"; + //pieModel.removeRow(pieModel.count - 1); // TODO: removeAll("label") ? } } } } - - // TODO: Optional syntax for defining models for different series. Is this really needed? -// ChartModel { -// id: chartModel -// ChartElement { column1: "Volkswagen"; column2: 13.5; column3: 1.2 } -// ChartElement { column1: "Toyota"; column2: 10.9; column3: 2.5 } -// } -// // column3 not used by pie series -// PieSeries { -// model: chartModel -// modelMapping: PieMapping { -// labels: 0 // undefined by default -// values: 1 // undefined by default -// first: 0 // 0 by default -// count: 10 // "Undefined" by default -// orientation: PieMapping.Vertical // Vertical by default -// } -// } - // TODO: show how to use data from a list model in a chart view // i.e. copy the data into a chart model // ListModel { diff --git a/demos/qmlchart/qml/qmlchart/View2.qml b/demos/qmlchart/qml/qmlchart/View2.qml index e3d797b..f2724e0 100644 --- a/demos/qmlchart/qml/qmlchart/View2.qml +++ b/demos/qmlchart/qml/qmlchart/View2.qml @@ -32,27 +32,31 @@ Rectangle { LineSeries { name: "Line" - model: XYModel { - XyPoint { x: 0.0; y: 0.0 } - XyPoint { x: 1.1; y: 2.1 } - XyPoint { x: 1.9; y: 3.3 } - XyPoint { x: 2.9; y: 4.9 } - XyPoint { x: 3.2; y: 3.0 } - XyPoint { x: 4.0; y: 3.3 } - } + model: chartModel + modelMapper.mapX: 0 + modelMapper.mapY: 1 + modelMapper.first: 0 + modelMapper.count: -1 + modelMapper.orientation: XYModelMapper.Vertical } SplineSeries { name: "Spline" - model: XYModel { - XyPoint { x: 0.0; y: 0.3 } - XyPoint { x: 1.1; y: 3.2 } - XyPoint { x: 1.7; y: 2.4 } - XyPoint { x: 2.1; y: 2.1 } - XyPoint { x: 2.9; y: 2.6 } - XyPoint { x: 3.4; y: 2.3 } - XyPoint { x: 4.1; y: 3.1 } - } + model: chartModel + modelMapper.mapX: 0 + modelMapper.mapY: 2 } } + + ChartModel { + id: chartModel + ChartModelRow { values: [0.0, 0.0, 0.3] } + ChartModelRow { values: [1.1, 2.1, 3.2] } + ChartModelRow { values: [1.9, 3.3, 2.4] } + ChartModelRow { values: [2.1, 2.1, 2.1] } + ChartModelRow { values: [2.9, 4.9, 2.6] } + ChartModelRow { values: [3.4, 3.0, 2.3] } + ChartModelRow { values: [4.1, 3.3, 3.1] } + } + } diff --git a/demos/qmlchart/qml/qmlchart/View3.qml b/demos/qmlchart/qml/qmlchart/View3.qml index 4c31afd..35fca7b 100644 --- a/demos/qmlchart/qml/qmlchart/View3.qml +++ b/demos/qmlchart/qml/qmlchart/View3.qml @@ -34,95 +34,46 @@ Rectangle { AreaSeries { name: "Russian" - upperModel: russianModel - lowerModel: zerosModel + upperModel: chartModel + upperModelMapper.mapX: 0 + upperModelMapper.mapY: 2 + lowerModel: chartModel + lowerModelMapper.mapX: 0 + lowerModelMapper.mapY: 1 } AreaSeries { name: "Swedish" - upperModel: swedishModel - lowerModel: zerosModel + upperModel: chartModel + upperModelMapper.mapX: 0 + upperModelMapper.mapY: 3 + lowerModel: chartModel + lowerModelMapper.mapX: 0 + lowerModelMapper.mapY: 1 } AreaSeries { name: "Finnish" - upperModel: finnishModel - lowerModel: zerosModel + upperModel: chartModel + upperModelMapper.mapX: 0 + upperModelMapper.mapY: 4 + lowerModel: chartModel + lowerModelMapper.mapX: 0 + lowerModelMapper.mapY: 1 } } - // TODO: optional implementation with generic ChartModel -// AreaSeries { -// model: chartModel -// modelMapping: XyMapping { -// xValues: 0 // undefined by default -// yValues: 1 // undefined by default -// first: 0 // 0 by default -// count: 10 // "Undefined" by default -// orientation: XyMapping.Vertical // Vertical by default -// } -// } - - - 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 } + ChartModel { + id: chartModel + ChartModelRow { values: [0, 0, 1, 1, 0] } + ChartModelRow { values: [1, 0, 1, 1, 0] } + ChartModelRow { values: [2, 0, 1, 3, 0] } + ChartModelRow { values: [3, 0, 1, 3, 0] } + ChartModelRow { values: [4, 0, 1, 2, 0] } + ChartModelRow { values: [5, 0, 0, 0, 0] } + ChartModelRow { values: [6, 0, 1, 2, 1] } + ChartModelRow { values: [7, 0, 1, 1, 0] } + ChartModelRow { values: [8, 0, 4, 2, 0] } + ChartModelRow { values: [9, 0, 3, 1, 0] } + ChartModelRow { values: [10, 0, 2, 3, 0] } + ChartModelRow { values: [11, 0, 1, 3, 1] } } } diff --git a/demos/qmlchart/qml/qmlchart/View4.qml b/demos/qmlchart/qml/qmlchart/View4.qml index 171f039..3cc8a4e 100644 --- a/demos/qmlchart/qml/qmlchart/View4.qml +++ b/demos/qmlchart/qml/qmlchart/View4.qml @@ -32,7 +32,7 @@ Rectangle { ScatterSeries { id: scatter1 name: "Scatter1" - model: XYModel { + model: ChartModel { XyPoint { x: 1.5; y: 1.5 } XyPoint { x: 1.5; y: 1.6 } XyPoint { x: 1.57; y: 1.55 } @@ -44,7 +44,7 @@ Rectangle { } ScatterSeries { name: "Scatter2" - model: XYModel { + model: ChartModel { XyPoint { x: 2.0; y: 2.0 } XyPoint { x: 2.0; y: 2.1 } XyPoint { x: 2.07; y: 2.05 } diff --git a/demos/qmlweather/qml/qmlweather/main.qml b/demos/qmlweather/qml/qmlweather/main.qml index 9636e02..1e49803 100644 --- a/demos/qmlweather/qml/qmlweather/main.qml +++ b/demos/qmlweather/qml/qmlweather/main.qml @@ -69,11 +69,11 @@ Rectangle { // id: chartModel // } - XYModel { + ChartModel { id: maxModel } - XYModel { + ChartModel { id: minModel } diff --git a/qmlplugin/declarativeareaseries.cpp b/qmlplugin/declarativeareaseries.cpp index 53619b8..939858b 100644 --- a/qmlplugin/declarativeareaseries.cpp +++ b/qmlplugin/declarativeareaseries.cpp @@ -28,48 +28,65 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE DeclarativeAreaSeries::DeclarativeAreaSeries(QObject *parent) : QAreaSeries(new QLineSeries(parent), new QLineSeries(parent)) { + QXYModelMapper *upperMapper = new QXYModelMapper(upperSeries()); + upperMapper->setMapX(0); + upperMapper->setMapY(1); + upperMapper->setFirst(0); + upperMapper->setCount(-1); + upperMapper->setOrientation(Qt::Vertical); + upperSeries()->setModelMapper(upperMapper); + + QXYModelMapper *lowerMapper = new QXYModelMapper(lowerSeries()); + lowerMapper->setMapX(2); + lowerMapper->setMapY(3); + lowerMapper->setFirst(0); + lowerMapper->setCount(-1); + lowerMapper->setOrientation(Qt::Vertical); + lowerSeries()->setModelMapper(lowerMapper); } -bool DeclarativeAreaSeries::setDeclarativeUpperModel(DeclarativeXyModel *model) +bool DeclarativeAreaSeries::setDeclarativeUpperModel(DeclarativeTableModel *model) { QAbstractItemModel *m = qobject_cast(model); bool value(false); if (m) { upperSeries()->setModel(m); - QXYModelMapper *mapper = new QXYModelMapper; - mapper->setMapX(0); - mapper->setMapY(1); - upperSeries()->setModelMapper(mapper); } else { qWarning("DeclarativeAreaSeries: Illegal model"); } return value; } -DeclarativeXyModel *DeclarativeAreaSeries::declarativeUpperModel() +DeclarativeTableModel *DeclarativeAreaSeries::declarativeUpperModel() { - return qobject_cast(upperSeries()->model()); + return qobject_cast(upperSeries()->model()); } -bool DeclarativeAreaSeries::setDeclarativeLowerModel(DeclarativeXyModel *model) +bool DeclarativeAreaSeries::setDeclarativeLowerModel(DeclarativeTableModel *model) { QAbstractItemModel *m = qobject_cast(model); bool value(false); if (m) { lowerSeries()->setModel(m); - QXYModelMapper *mapper = new QXYModelMapper; - mapper->setMapX(0); - mapper->setMapY(1); - lowerSeries()->setModelMapper(mapper); } else { qWarning("DeclarativeAreaSeries: Illegal model"); } return value; } -DeclarativeXyModel *DeclarativeAreaSeries::declarativeLowerModel() +DeclarativeTableModel *DeclarativeAreaSeries::declarativeLowerModel() +{ + return qobject_cast(lowerSeries()->model()); +} + +QXYModelMapper* DeclarativeAreaSeries::upperModelMapper() const +{ + return upperSeries()->modelMapper(); +} + +QXYModelMapper* DeclarativeAreaSeries::lowerModelMapper() const { - return qobject_cast(lowerSeries()->model()); + return lowerSeries()->modelMapper(); } #include "moc_declarativeareaseries.cpp" diff --git a/qmlplugin/declarativeareaseries.h b/qmlplugin/declarativeareaseries.h index b2dd537..2773ece 100644 --- a/qmlplugin/declarativeareaseries.h +++ b/qmlplugin/declarativeareaseries.h @@ -24,24 +24,27 @@ #include "qchartglobal.h" #include "qareaseries.h" #include "declarativelineseries.h" -#include "declarativexyseries.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE -class DeclarativeAreaSeries : public QAreaSeries, public DeclarativeXySeries +class DeclarativeAreaSeries : public QAreaSeries { Q_OBJECT - Q_PROPERTY(DeclarativeXyModel *upperModel READ declarativeUpperModel WRITE setDeclarativeUpperModel) - Q_PROPERTY(DeclarativeXyModel *lowerModel READ declarativeLowerModel WRITE setDeclarativeLowerModel) + Q_PROPERTY(DeclarativeTableModel *upperModel READ declarativeUpperModel WRITE setDeclarativeUpperModel) + Q_PROPERTY(DeclarativeTableModel *lowerModel READ declarativeLowerModel WRITE setDeclarativeLowerModel) + Q_PROPERTY(QXYModelMapper *upperModelMapper READ upperModelMapper) + Q_PROPERTY(QXYModelMapper *lowerModelMapper READ lowerModelMapper) public: explicit DeclarativeAreaSeries(QObject *parent = 0); public: - bool setDeclarativeUpperModel(DeclarativeXyModel *model); - DeclarativeXyModel *declarativeUpperModel(); - bool setDeclarativeLowerModel(DeclarativeXyModel *model); - DeclarativeXyModel *declarativeLowerModel(); + bool setDeclarativeUpperModel(DeclarativeTableModel *model); + DeclarativeTableModel *declarativeUpperModel(); + bool setDeclarativeLowerModel(DeclarativeTableModel *model); + DeclarativeTableModel *declarativeLowerModel(); + QXYModelMapper* upperModelMapper() const; + QXYModelMapper* lowerModelMapper() const; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativelineseries.h b/qmlplugin/declarativelineseries.h index 2e97273..3e5cc23 100644 --- a/qmlplugin/declarativelineseries.h +++ b/qmlplugin/declarativelineseries.h @@ -31,7 +31,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class DeclarativeLineSeries : public QLineSeries, public DeclarativeXySeries { Q_OBJECT - Q_PROPERTY(DeclarativeXyModel *model READ declarativeModel WRITE setDeclarativeModel) + Q_PROPERTY(DeclarativeTableModel *model READ declarativeModel WRITE setDeclarativeModel) public: explicit DeclarativeLineSeries(QObject *parent = 0); diff --git a/qmlplugin/declarativemodel.cpp b/qmlplugin/declarativemodel.cpp index 5362543..58a62cd 100644 --- a/qmlplugin/declarativemodel.cpp +++ b/qmlplugin/declarativemodel.cpp @@ -24,7 +24,25 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -////////////// Table model (base) /////////////////// + +////////////// Table model row /////////////////// + +DeclarativeTableModelRow::DeclarativeTableModelRow(QObject *parent) + : QObject(parent) +{ +} + +QVariantList DeclarativeTableModelRow::values() +{ + return m_values; +} + +void DeclarativeTableModelRow::setValues(QVariantList values) +{ + m_values = values; +} + +////////////// Table model /////////////////// DeclarativeTableModel::DeclarativeTableModel(QObject *parent) : ChartTableModel(parent) @@ -54,39 +72,45 @@ void DeclarativeTableModel::appendModelChild(QDeclarativeListProperty * Q_UNUSED(child) } -void DeclarativeTableModel::appendToModel(QObject *object) +void DeclarativeTableModel::append(QVariantList values) { - if (qobject_cast(this)) { - DeclarativeBarModel *model = qobject_cast(this); - model->append(qobject_cast(object)); - } else if (qobject_cast(this)) { - DeclarativePieModel *model = qobject_cast(this); - model->append(qobject_cast(object)); - } else if (qobject_cast(this)) { - DeclarativeXyModel *model = qobject_cast(this); - model->append(qobject_cast(object)); - } -} +// qDebug() << "DeclarativeTableModel::append:" << values; -////////////// XY model /////////////////////// + while (columnCount() < values.count()) + insertColumn(columnCount()); -DeclarativeXyModel::DeclarativeXyModel(QObject *parent) : - DeclarativeTableModel(parent) -{ + insertRow(rowCount()); + + QModelIndex beginIndex = QModelIndex(); + QModelIndex endIndex = QModelIndex(); + for (int i(0); i < values.count(); i++) { + QModelIndex modelIndex = createIndex(rowCount() - 1, i); + if (i == 0) + beginIndex = modelIndex; + if (i == (values.count() - 1)) + endIndex = modelIndex; + setData(modelIndex, values.at(i)); + } + dataChanged(beginIndex, endIndex); } -void DeclarativeXyModel::append(DeclarativeXyPoint* point) +void DeclarativeTableModel::appendToModel(QObject *object) { -// qDebug() << "DeclarativeXyModel::append:" << point->x() << " " << point->y(); - insertRow(rowCount()); - QModelIndex xModelIndex = createIndex(rowCount() - 1, 0); - QModelIndex yModelIndex = createIndex(rowCount() - 1, 1); - setData(xModelIndex, point->x()); - setData(yModelIndex, point->y()); - dataChanged(xModelIndex, yModelIndex); + if (qobject_cast(object)) { + DeclarativeBarModel *model = qobject_cast(this); + Q_ASSERT(model); + model->append(qobject_cast(object)); + } else if (qobject_cast(object)) { + // TODO + } else if (qobject_cast(object)) { + // TODO + appendPoint(qobject_cast(object)); + } else if (qobject_cast(this)) { + append(qobject_cast(object)->values()); + } } -void DeclarativeXyModel::append(QVariantList points) +void DeclarativeTableModel::appendPoints(QVariantList points) { qreal x = 0.0; for (int i(0); i < points.count(); i++) { @@ -97,7 +121,7 @@ void DeclarativeXyModel::append(QVariantList points) DeclarativeXyPoint *point= new DeclarativeXyPoint(); point->setX(x); point->setY(y); - append(point); + appendPoint(point); } else { qWarning() << "Illegal y value"; } @@ -111,44 +135,15 @@ void DeclarativeXyModel::append(QVariantList points) } } -////////////// Pie model /////////////////////// - -DeclarativePieModel::DeclarativePieModel(QObject *parent) : - DeclarativeTableModel(parent) +void DeclarativeTableModel::appendPoint(DeclarativeXyPoint* point) { -} - -void DeclarativePieModel::append(QPieSlice* slice) -{ -// qDebug() << "DeclarativePieModel::append:" << slice->label() << " " << slice->value(); +// qDebug() << "DeclarativeTableModel::append:" << point->x() << " " << point->y(); insertRow(rowCount()); - - setData(createIndex(rowCount() - 1, 0), slice->value()); - setData(createIndex(rowCount() - 1, 1), slice->label()); -} - -void DeclarativePieModel::append(QVariantList slices) -{ -// qDebug() << "append:" << slices; - QString label = ""; - for (int i(0); i < slices.count(); i++) { - if (i % 2) { - bool ok(false); - qreal value = slices.at(i).toReal(&ok); - if (ok) { - QPieSlice *slice = new QPieSlice(value, label); - append(slice); - // TODO: how to copy the properties to the newly added slice? - // (DeclarativePieModel::append only copies the label and value to the model) -// QPieSlice *addedSlice = append(slice); -// addedSlice->setExploded(slice->isExploded()); - } else { - qWarning() << "Illegal slice item"; - } - } else { - label = slices.at(i).toString(); - } - } + QModelIndex xModelIndex = createIndex(rowCount() - 1, 0); + QModelIndex yModelIndex = createIndex(rowCount() - 1, 1); + setData(xModelIndex, point->x()); + setData(yModelIndex, point->y()); + dataChanged(xModelIndex, yModelIndex); } ////////////// Bar model /////////////////////// diff --git a/qmlplugin/declarativemodel.h b/qmlplugin/declarativemodel.h index 806c52d..fcf3e08 100644 --- a/qmlplugin/declarativemodel.h +++ b/qmlplugin/declarativemodel.h @@ -32,6 +32,19 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE +class DeclarativeTableModelRow : public QObject +{ + Q_OBJECT + Q_PROPERTY(QVariantList values READ values WRITE setValues) + +public: + explicit DeclarativeTableModelRow(QObject *parent = 0); + QVariantList values(); + void setValues(QVariantList values); +private: + QVariantList m_values; +}; + class DeclarativeTableModel : public ChartTableModel, public QDeclarativeParserStatus { Q_OBJECT @@ -48,36 +61,15 @@ public: // from QDeclarativeParserStatus void componentComplete(); public Q_SLOTS: + void append(QVariantList slices); + void appendPoints(QVariantList points); + void appendPoint(DeclarativeXyPoint* point); static void appendModelChild(QDeclarativeListProperty *list, QObject *element); private: void appendToModel(QObject *object); }; -class DeclarativeXyModel : public DeclarativeTableModel -{ - Q_OBJECT - -public: - explicit DeclarativeXyModel(QObject *parent = 0); - -public Q_SLOTS: - void append(DeclarativeXyPoint* point); - void append(QVariantList points); -}; - -class DeclarativePieModel : public DeclarativeTableModel -{ - Q_OBJECT - -public: - explicit DeclarativePieModel(QObject *parent = 0); - -public Q_SLOTS: - void append(QPieSlice* slice); - void append(QVariantList slices); -}; - class DeclarativeBarModel : public DeclarativeTableModel { Q_OBJECT diff --git a/qmlplugin/declarativepieseries.cpp b/qmlplugin/declarativepieseries.cpp index cf2d5f8..19069c6 100644 --- a/qmlplugin/declarativepieseries.cpp +++ b/qmlplugin/declarativepieseries.cpp @@ -29,6 +29,17 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE DeclarativePieSeries::DeclarativePieSeries(QObject *parent) : QPieSeries(parent) { + // TODO: set default model on init? +// setModel(new DeclarativeTableModel()); + + // Set default mapper parameters to allow easy to use PieSeries api + QPieModelMapper *mapper = new QPieModelMapper(); + mapper->setMapLabels(0); + mapper->setMapValues(1); + mapper->setOrientation(Qt::Vertical); + mapper->setFirst(0); + mapper->setCount(-1); + setModelMapper(mapper); } QPieSlice *DeclarativePieSeries::slice(int index) @@ -40,24 +51,19 @@ QPieSlice *DeclarativePieSeries::slice(int index) return 0; } - -void DeclarativePieSeries::setPieModel(DeclarativePieModel *model) +void DeclarativePieSeries::setPieModel(DeclarativeTableModel *model) { QAbstractItemModel *m = qobject_cast(model); if (m) { QPieSeries::setModel(m); - QPieModelMapper *mapper = new QPieModelMapper; - mapper->setMapValues(0); - mapper->setMapLabels(1); - QPieSeries::setModelMapper(mapper); } else { qWarning("DeclarativePieSeries: Illegal model"); } } -DeclarativePieModel *DeclarativePieSeries::pieModel() +DeclarativeTableModel *DeclarativePieSeries::pieModel() { - return qobject_cast(model()); + return qobject_cast(model()); } #include "moc_declarativepieseries.cpp" diff --git a/qmlplugin/declarativepieseries.h b/qmlplugin/declarativepieseries.h index c4f4d2f..a3c9a51 100644 --- a/qmlplugin/declarativepieseries.h +++ b/qmlplugin/declarativepieseries.h @@ -36,7 +36,7 @@ class QChart; class DeclarativePieSeries : public QPieSeries { Q_OBJECT - Q_PROPERTY(DeclarativePieModel *model READ pieModel WRITE setPieModel) + Q_PROPERTY(DeclarativeTableModel *model READ pieModel WRITE setPieModel) public: explicit DeclarativePieSeries(QObject *parent = 0); @@ -44,11 +44,9 @@ public: public: Q_INVOKABLE QPieSlice *slice(int index); -public Q_SLOTS: - public: - void setPieModel(DeclarativePieModel *model); - DeclarativePieModel *pieModel(); + void setPieModel(DeclarativeTableModel *model); + DeclarativeTableModel *pieModel(); }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativescatterseries.h b/qmlplugin/declarativescatterseries.h index f04a721..0b42721 100644 --- a/qmlplugin/declarativescatterseries.h +++ b/qmlplugin/declarativescatterseries.h @@ -31,7 +31,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class DeclarativeScatterSeries : public QScatterSeries, public DeclarativeXySeries { Q_OBJECT - Q_PROPERTY(DeclarativeXyModel *model READ declarativeModel WRITE setDeclarativeModel) + Q_PROPERTY(DeclarativeTableModel *model READ declarativeModel WRITE setDeclarativeModel) public: explicit DeclarativeScatterSeries(QObject *parent = 0); diff --git a/qmlplugin/declarativesplineseries.cpp b/qmlplugin/declarativesplineseries.cpp index 954e1af..2ba89e3 100644 --- a/qmlplugin/declarativesplineseries.cpp +++ b/qmlplugin/declarativesplineseries.cpp @@ -20,7 +20,7 @@ #include "declarativesplineseries.h" #include "declarativechart.h" -#include "qchart.h" +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE diff --git a/qmlplugin/declarativesplineseries.h b/qmlplugin/declarativesplineseries.h index e4e5049..cc9f5b4 100644 --- a/qmlplugin/declarativesplineseries.h +++ b/qmlplugin/declarativesplineseries.h @@ -31,7 +31,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class DeclarativeSplineSeries : public QSplineSeries, public DeclarativeXySeries { Q_OBJECT - Q_PROPERTY(DeclarativeXyModel *model READ declarativeModel WRITE setDeclarativeModel) + Q_PROPERTY(DeclarativeTableModel *model READ declarativeModel WRITE setDeclarativeModel) public: explicit DeclarativeSplineSeries(QObject *parent = 0); diff --git a/qmlplugin/declarativexyseries.cpp b/qmlplugin/declarativexyseries.cpp index 9af0881..6e216eb 100644 --- a/qmlplugin/declarativexyseries.cpp +++ b/qmlplugin/declarativexyseries.cpp @@ -20,21 +20,30 @@ //#include "DeclarativeXySeries.h" #include "declarativexyseries.h" -#include "qxyseries.h" -#include "qxymodelmapper.h" +#include +#include #include "declarativechart.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE DeclarativeXySeries::DeclarativeXySeries() { + // All the inherited objects must be of type QXYSeries, so it is safe to cast + QXYSeries *series = reinterpret_cast(this); + QXYModelMapper *mapper = new QXYModelMapper(series); + mapper->setMapX(0); + mapper->setMapY(1); + mapper->setFirst(0); + mapper->setCount(-1); + mapper->setOrientation(Qt::Vertical); + series->setModelMapper(mapper); } DeclarativeXySeries::~DeclarativeXySeries() { } -bool DeclarativeXySeries::setDeclarativeModel(DeclarativeXyModel *model) +bool DeclarativeXySeries::setDeclarativeModel(DeclarativeTableModel *model) { QAbstractItemModel *m = qobject_cast(model); bool value(false); @@ -42,22 +51,21 @@ bool DeclarativeXySeries::setDeclarativeModel(DeclarativeXyModel *model) // All the inherited objects must be of type QXYSeries, so it is safe to cast QXYSeries *series = reinterpret_cast(this); series->setModel(m); - QXYModelMapper *mapper = new QXYModelMapper; - mapper->setMapX(0); - mapper->setMapY(1); - series->setModelMapper(mapper); +// QXYModelMapper *mapper = new QXYModelMapper; +// mapper->setMapX(0); +// mapper->setMapY(1); +// series->setModelMapper(mapper); } else { qWarning("DeclarativeXySeries: Illegal model"); } return value; } -DeclarativeXyModel *DeclarativeXySeries::declarativeModel() +DeclarativeTableModel *DeclarativeXySeries::declarativeModel() { // All the inherited objects must be of type QXYSeries, so it is safe to cast QXYSeries *series = reinterpret_cast(this); - Q_ASSERT(series); - return qobject_cast(series->model()); + return qobject_cast(series->model()); } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativexyseries.h b/qmlplugin/declarativexyseries.h index f4a4c9f..a546e5b 100644 --- a/qmlplugin/declarativexyseries.h +++ b/qmlplugin/declarativexyseries.h @@ -39,8 +39,8 @@ public: ~DeclarativeXySeries(); public: - bool setDeclarativeModel(DeclarativeXyModel *model); - DeclarativeXyModel *declarativeModel(); + bool setDeclarativeModel(DeclarativeTableModel *model); + DeclarativeTableModel *declarativeModel(); }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/plugin.cpp b/qmlplugin/plugin.cpp index 927ec5f..c991097 100644 --- a/qmlplugin/plugin.cpp +++ b/qmlplugin/plugin.cpp @@ -30,6 +30,9 @@ #include "declarativescatterseries.h" #include "declarativebarseries.h" #include "declarativepieseries.h" +#include "declarativemodel.h" +#include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -43,7 +46,7 @@ public: qmlRegisterType(uri, 1, 0, "ChartView"); qmlRegisterUncreatableType(uri, 1, 0, "Axis", - QLatin1String("Trying to create uncreatable type: Axis.")); + QLatin1String("Trying to create uncreatable: Axis.")); //qmlRegisterType(uri, 1, 0, "AxisCategory"); qmlRegisterType(uri, 1, 0, "XyPoint"); qmlRegisterType(uri, 1, 0, "ScatterSeries"); @@ -53,8 +56,15 @@ public: qmlRegisterType(uri, 1, 0, "BarSeries"); qmlRegisterType(uri, 1, 0, "PieSeries"); qmlRegisterType(uri, 1, 0, "PieSlice"); - qmlRegisterType(uri, 1, 0, "PieModel"); - qmlRegisterType(uri, 1, 0, "XYModel"); + qmlRegisterType(uri, 1, 0, "ChartModel"); + qmlRegisterType(uri, 1, 0, "ChartModelRow"); + //qmlRegisterType(uri, 1, 0, "PieMapping"); + //qmlRegisterType(uri, 1, 0, "PieModelMapper"); + qmlRegisterUncreatableType(uri, 1, 0, "PieModelMapper", + QLatin1String("Trying to create uncreatable: PieModelMapper.")); + qmlRegisterUncreatableType(uri, 1, 0, "XYModelMapper", + QLatin1String("Trying to create uncreatable: PieModelMapper.")); + qmlRegisterType(uri, 1, 0, "BarModel"); qmlRegisterType(uri, 1, 0, "BarSet"); } diff --git a/src/charttablemodel.cpp b/src/charttablemodel.cpp index c6f0be8..26193b1 100644 --- a/src/charttablemodel.cpp +++ b/src/charttablemodel.cpp @@ -94,11 +94,16 @@ bool ChartTableModel::setData(const QModelIndex &index, const QVariant &value, i return false; } -void ChartTableModel::insertRow(int row, const QModelIndex &parent) +void ChartTableModel::insertColumn(int column, const QModelIndex &parent) { - Q_UNUSED(parent) + beginInsertColumns(parent, column, column); + m_columnCount++; + endInsertColumns(); +} - beginInsertRows(QModelIndex(), row, row); +void ChartTableModel::insertRow(int row, const QModelIndex &parent) +{ + beginInsertRows(parent, row, row); QVector* dataVec = new QVector(m_columnCount); m_data.insert(row, dataVec); endInsertRows(); diff --git a/src/charttablemodel.h b/src/charttablemodel.h index e13cd80..4d7dd78 100644 --- a/src/charttablemodel.h +++ b/src/charttablemodel.h @@ -42,6 +42,7 @@ public: 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; + void insertColumn(int column, const QModelIndex &parent = QModelIndex()); void insertRow(int row, const QModelIndex &parent = QModelIndex()); /*Q_INVOKABLE*/ //bool removeRow(int row, const QModelIndex &parent = QModelIndex()); Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex()); diff --git a/src/piechart/qpiemodelmapper.h b/src/piechart/qpiemodelmapper.h index 0cd44cb..e816c66 100644 --- a/src/piechart/qpiemodelmapper.h +++ b/src/piechart/qpiemodelmapper.h @@ -9,9 +9,16 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QTCOMMERCIALCHART_EXPORT QPieModelMapper : public QObject { Q_OBJECT + Q_PROPERTY(int mapValues READ mapValues WRITE setMapValues) + Q_PROPERTY(int mapLabels READ mapLabels WRITE setMapLabels) + Q_PROPERTY(int first READ first WRITE setFirst) + Q_PROPERTY(int count READ count WRITE setCount) + Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation) + Q_ENUMS(Qt::Orientation) + public: explicit QPieModelMapper(QObject *parent = 0); - + int first() const; void setFirst(int first); diff --git a/src/piechart/qpieseries.h b/src/piechart/qpieseries.h index 2375281..1d8dd26 100644 --- a/src/piechart/qpieseries.h +++ b/src/piechart/qpieseries.h @@ -36,6 +36,8 @@ class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries Q_PROPERTY(qreal size READ pieSize WRITE setPieSize) Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle) Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle) + Q_PROPERTY(int count READ count) + Q_PROPERTY(QPieModelMapper *modelMapper READ modelMapper) public: explicit QPieSeries(QObject *parent = 0); diff --git a/src/xychart/qxymodelmapper.h b/src/xychart/qxymodelmapper.h index f1ccaff..db35de7 100644 --- a/src/xychart/qxymodelmapper.h +++ b/src/xychart/qxymodelmapper.h @@ -3,13 +3,18 @@ #include "qchartglobal.h" #include -#include QTCOMMERCIALCHART_BEGIN_NAMESPACE class QTCOMMERCIALCHART_EXPORT QXYModelMapper : public QObject { Q_OBJECT + Q_PROPERTY(int mapX READ mapX WRITE setMapX) + Q_PROPERTY(int mapY READ mapY WRITE setMapY) + Q_PROPERTY(int first READ first WRITE setFirst) + Q_PROPERTY(int count READ count WRITE setCount) + Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation) + Q_ENUMS(Qt::Orientation) public: explicit QXYModelMapper(QObject *parent = 0); diff --git a/src/xychart/qxyseries.h b/src/xychart/qxyseries.h index 40d4389..0f2cc94 100644 --- a/src/xychart/qxyseries.h +++ b/src/xychart/qxyseries.h @@ -36,6 +36,8 @@ class QXYModelMapper; class QTCOMMERCIALCHART_EXPORT QXYSeries : public QAbstractSeries { Q_OBJECT + Q_PROPERTY(QXYModelMapper *modelMapper READ modelMapper) + protected: explicit QXYSeries(QXYSeriesPrivate &d,QObject *parent = 0); ~QXYSeries();