From 88d7235253d9c36f6d774502c35827bb3b076c89 2012-05-18 14:17:51 From: Tero Ahola Date: 2012-05-18 14:17:51 Subject: [PATCH] Removed usage of model in QML pie example --- diff --git a/demos/qmlchart/qml/qmlchart/View1.qml b/demos/qmlchart/qml/qmlchart/View1.qml index 2dfaf49..ec85deb 100644 --- a/demos/qmlchart/qml/qmlchart/View1.qml +++ b/demos/qmlchart/qml/qmlchart/View1.qml @@ -39,14 +39,15 @@ Rectangle { // If you have static data, you can simply use the PieSlice API PieSeries { id: pieSeries - PieSlice { label: "Volkswagen"; value: 13.5 } + PieSlice { id: volkswagenSlice; 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 } } - // For dynamic data you can use the ChartModel API. + // TODO: move ChartModel API into a demo application instead of making it a public API +// // For dynamic data you can use the ChartModel API. // ChartModel { // id: chartModel // ChartModelRow { values: ["Volkswagen", 13.5] } @@ -55,8 +56,7 @@ Rectangle { // ChartModelRow { values: ["Skoda", 8.2] } // ChartModelRow { values: ["Volvo", 6.8] } // } - - // In this case you need to define how the data maps to pie slices with the ModelMapper API of the pie series. +// // In this case you need to define how the data maps to pie slices with the ModelMapper API of the pie series. // PieSeries { // id: pieSeries // model: chartModel @@ -66,11 +66,15 @@ Rectangle { // modelMapper.count: -1 // "Undefined" = -1 by default // modelMapper.orientation: PieModelMapper.Vertical // } + + // TODO: you could also append to your model, for example: +// pieSeries.model.append(["Others", 52.0]); } Component.onCompleted: { - // You can also add data dynamically - pieSeries.model.append(["Others", 52.0]); + volkswagenSlice.exploded = true; + // You can also add slices dynamically + var newSlice = pieSeries.append("Others", 52.0); } Timer { @@ -80,11 +84,15 @@ Rectangle { onTriggered: { // Set all slices as not exploded for (var i = 0; i < pieSeries.count; i++) - pieSeries.slice(i).exploded = false; + pieSeries.at(i).exploded = false; // Explode one of the slices __explodedIndex = (__explodedIndex + 1) % pieSeries.count; - pieSeries.slice(__explodedIndex).exploded = true; + pieSeries.at(__explodedIndex).exploded = true; + + // TODO: implement for convenience +// pieSeries.find("Ford").exploded = true; +// pieSeries.removeAll("Ford") } } @@ -111,8 +119,6 @@ Rectangle { } else { pieSeries.modelMapper.count = 5; buttonText.text = "Show others"; - //pieModel.removeRow(pieModel.count - 1); - // TODO: removeAll("label") ? } } } diff --git a/demos/qmlchart/qml/qmlchart/View3.qml b/demos/qmlchart/qml/qmlchart/View3.qml index 35fca7b..7560f43 100644 --- a/demos/qmlchart/qml/qmlchart/View3.qml +++ b/demos/qmlchart/qml/qmlchart/View3.qml @@ -28,7 +28,6 @@ Rectangle { title: "NHL All-Star Team Players" anchors.fill: parent theme: ChartView.ChartThemeHighContrast - legend: ChartView.LegendTop axisXLabels: ["0", "2000", "1", "2001", "2", "2002", "3", "2003", "4", "2004", "5", "2005", "6", "2006", "7", "2007", "8", "2008", "9", "2009", "10", "2010", "11", "2011"] diff --git a/qmlplugin/declarativechart.cpp b/qmlplugin/declarativechart.cpp index af8b3f0..0c39bb8 100644 --- a/qmlplugin/declarativechart.cpp +++ b/qmlplugin/declarativechart.cpp @@ -26,7 +26,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) : QDeclarativeItem(parent), m_chart(new QChart(this)), - m_legend(LegendDisabled) + m_legend(LegendTop) { setFlag(QGraphicsItem::ItemHasNoContents, false); m_chart->axisX()->setNiceNumbersEnabled(false); diff --git a/qmlplugin/declarativepieseries.cpp b/qmlplugin/declarativepieseries.cpp index 657005a..8bb7915 100644 --- a/qmlplugin/declarativepieseries.cpp +++ b/qmlplugin/declarativepieseries.cpp @@ -30,9 +30,9 @@ DeclarativePieSeries::DeclarativePieSeries(QObject *parent) : QPieSeries(parent) { // TODO: set default model on init? - setModel(new DeclarativeTableModel()); +// setModel(new DeclarativeTableModel()); - // Set default mapper parameters to allow easy to use PieSeries api + // TODO: Set default mapper parameters to allow easy to use PieSeries api? QPieModelMapper *mapper = new QPieModelMapper(); mapper->setMapLabels(0); mapper->setMapValues(1); @@ -49,15 +49,8 @@ void DeclarativePieSeries::classBegin() void DeclarativePieSeries::componentComplete() { foreach(QObject *child, children()) { - qDebug() << "pie child: " << child; if (qobject_cast(child)) { - QPieSlice *slice = qobject_cast(child); - QVariantList values; - values.insert(modelMapper()->mapLabels(), slice->label()); - values.insert(modelMapper()->mapValues(), slice->value()); - DeclarativeTableModel *m = qobject_cast(model()); - Q_ASSERT(m); - m->append(values); + QPieSeries::append(qobject_cast(child)); } } } @@ -67,7 +60,7 @@ QDeclarativeListProperty DeclarativePieSeries::initialSlices() return QDeclarativeListProperty(this, 0, &DeclarativePieSeries::appendInitialSlices); } -QPieSlice *DeclarativePieSeries::slice(int index) +QPieSlice *DeclarativePieSeries::at(int index) { QList sliceList = slices(); if (index < sliceList.count()) @@ -76,6 +69,12 @@ QPieSlice *DeclarativePieSeries::slice(int index) return 0; } +QPieSlice* DeclarativePieSeries::append(QString name, qreal value) +{ + // TODO: parameter order is wrong, switch it: + return QPieSeries::append(value, name); +} + void DeclarativePieSeries::setPieModel(DeclarativeTableModel *model) { QAbstractItemModel *m = qobject_cast(model); diff --git a/qmlplugin/declarativepieseries.h b/qmlplugin/declarativepieseries.h index 0734d02..bcb03c5 100644 --- a/qmlplugin/declarativepieseries.h +++ b/qmlplugin/declarativepieseries.h @@ -47,7 +47,8 @@ public: QDeclarativeListProperty initialSlices(); DeclarativeTableModel *pieModel(); void setPieModel(DeclarativeTableModel *model); - Q_INVOKABLE QPieSlice *slice(int index); + Q_INVOKABLE QPieSlice *at(int index); + Q_INVOKABLE QPieSlice* append(QString name, qreal value); public: void classBegin();