From a31eb092569c055461c0c2fc8d944abbd7c57bfe 2012-03-29 10:31:46 From: Tero Ahola Date: 2012-03-29 10:31:46 Subject: [PATCH] Declarative series classed now derived from QSeries childs --- diff --git a/qmlplugin/declarativelineseries.cpp b/qmlplugin/declarativelineseries.cpp index 218dfc9..ccfcba9 100644 --- a/qmlplugin/declarativelineseries.cpp +++ b/qmlplugin/declarativelineseries.cpp @@ -5,11 +5,38 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -DeclarativeLineSeries::DeclarativeLineSeries(QDeclarativeItem *parent) : - DeclarativeXySeries(QSeries::SeriesTypeLine, parent) +DeclarativeLineSeries::DeclarativeLineSeries(QObject *parent) : + QLineSeries(parent) { } +void DeclarativeLineSeries::seriesComplete() +{ + DeclarativeChart *declarativeChart = qobject_cast(parent()); + + if (declarativeChart) { + QChart *chart = qobject_cast(declarativeChart->m_chart); + Q_ASSERT(chart); + chart->addSeries(this); + } +} + +QObject *DeclarativeLineSeries::seriesObject() +{ + return this; +} + +QDeclarativeListProperty DeclarativeLineSeries::points() +{ + return DeclarativeXySeries::points(); +} + +void DeclarativeLineSeries::appendPoints(QDeclarativeListProperty *list, + DeclarativeXyPoint *element) +{ + DeclarativeXySeries::appendPoints(list, element); +} + #include "moc_declarativelineseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativelineseries.h b/qmlplugin/declarativelineseries.h index b6e5100..9329fb6 100644 --- a/qmlplugin/declarativelineseries.h +++ b/qmlplugin/declarativelineseries.h @@ -2,17 +2,30 @@ #define DECLARATIVELINESERIES_H #include "qchartglobal.h" +#include "qlineseries.h" #include "declarativexyseries.h" -#include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class DeclarativeLineSeries : public DeclarativeXySeries +class DeclarativeLineSeries : public QLineSeries, public DeclarativeXySeries { Q_OBJECT + Q_PROPERTY(QDeclarativeListProperty points READ points) public: - explicit DeclarativeLineSeries(QDeclarativeItem *parent = 0); + explicit DeclarativeLineSeries(QObject *parent = 0); + +public: + void seriesComplete(); + QObject *seriesObject(); + +public: + QDeclarativeListProperty points(); + +public slots: + static void appendPoints(QDeclarativeListProperty *list, + DeclarativeXyPoint *element); }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativepieseries.cpp b/qmlplugin/declarativepieseries.cpp index 3270387..42926dd 100644 --- a/qmlplugin/declarativepieseries.cpp +++ b/qmlplugin/declarativepieseries.cpp @@ -1,56 +1,41 @@ #include "declarativepieseries.h" #include "declarativechart.h" #include "qchart.h" -#include "qpieseries.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE -DeclarativePieSeries::DeclarativePieSeries(QDeclarativeItem *parent) : - QDeclarativeItem(parent), - m_chart(0), - m_series(0) +DeclarativePieSeries::DeclarativePieSeries(QObject *parent) : + QPieSeries(parent), + m_chart(0) { - setFlag(QGraphicsItem::ItemHasNoContents, false); - connect(this, SIGNAL(parentChanged()), - this, SLOT(setParentForSeries())); } -void DeclarativePieSeries::setParentForSeries() +void DeclarativePieSeries::classBegin() { - if (!m_series) { - DeclarativeChart *declarativeChart = qobject_cast(parent()); - if (declarativeChart) { - QChart *chart = qobject_cast(declarativeChart->m_chart); - qDebug() << "creating pie series for chart: " << chart; - Q_ASSERT(chart); - - m_series = new QPieSeries(); - Q_ASSERT(m_series); - foreach (QPieSlice* slice, m_data) { - // Have to duplicate the data - m_series->add(slice->value(), slice->label()); - } - chart->addSeries(m_series); - //chart->axisY(); - } +} + +void DeclarativePieSeries::componentComplete() +{ + DeclarativeChart *declarativeChart = qobject_cast(parent()); + if (declarativeChart) { + QChart *chart = qobject_cast(declarativeChart->m_chart); + Q_ASSERT(chart); + qDebug() << "parent for pie:" << chart; + chart->addSeries(this); } } -QDeclarativeListProperty DeclarativePieSeries::data() +QDeclarativeListProperty DeclarativePieSeries::slices() { - return QDeclarativeListProperty(this, 0, &DeclarativePieSeries::appendData); + return QDeclarativeListProperty(this, 0, &DeclarativePieSeries::appendSlice); } -void DeclarativePieSeries::appendData(QDeclarativeListProperty *list, +void DeclarativePieSeries::appendSlice(QDeclarativeListProperty *list, QPieSlice *slice) { DeclarativePieSeries *series = qobject_cast(list->object); - if (series) { - series->m_data.append(slice); - // Have to duplicate the data - if (series->m_series) - series->m_series->add(slice->value(), slice->label()); - } + if (series) + series->add(slice->value(), slice->label()); } #include "moc_declarativepieseries.cpp" diff --git a/qmlplugin/declarativepieseries.h b/qmlplugin/declarativepieseries.h index 41177ce..2048547 100644 --- a/qmlplugin/declarativepieseries.h +++ b/qmlplugin/declarativepieseries.h @@ -3,35 +3,33 @@ #include "qchartglobal.h" #include "qpieslice.h" +#include "qpieseries.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class QPieSeries; class QChart; -class DeclarativePieSeries : public QDeclarativeItem +class DeclarativePieSeries : public QPieSeries, public QDeclarativeParserStatus { Q_OBJECT - Q_PROPERTY(QDeclarativeListProperty data READ data) + Q_INTERFACES(QDeclarativeParserStatus) + Q_PROPERTY(QDeclarativeListProperty slices READ slices) public: - explicit DeclarativePieSeries(QDeclarativeItem *parent = 0); - QDeclarativeListProperty data(); + explicit DeclarativePieSeries(QObject *parent = 0); + QDeclarativeListProperty slices(); -Q_SIGNALS: +public: // from QDeclarativeParserStatus + void classBegin(); + void componentComplete(); public Q_SLOTS: - static void appendData(QDeclarativeListProperty *list, - QPieSlice *element); - -private Q_SLOTS: - void setParentForSeries(); + static void appendSlice(QDeclarativeListProperty *list, + QPieSlice *element); private: QChart *m_chart; - QPieSeries *m_series; - QList m_data; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativescatterseries.cpp b/qmlplugin/declarativescatterseries.cpp index c0340b6..c614b38 100644 --- a/qmlplugin/declarativescatterseries.cpp +++ b/qmlplugin/declarativescatterseries.cpp @@ -5,10 +5,37 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -DeclarativeScatterSeries::DeclarativeScatterSeries(QDeclarativeItem *parent) : - DeclarativeXySeries(QSeries::SeriesTypeScatter, parent) +DeclarativeScatterSeries::DeclarativeScatterSeries(QObject *parent) : + QScatterSeries(parent) { - setFlag(QGraphicsItem::ItemHasNoContents, false); +} + +void DeclarativeScatterSeries::seriesComplete() +{ + DeclarativeChart *declarativeChart = qobject_cast(parent()); + + if (declarativeChart) { + QChart *chart = qobject_cast(declarativeChart->m_chart); + Q_ASSERT(chart); + qDebug() << "chart:" << chart; + chart->addSeries(this); + } +} + +QObject *DeclarativeScatterSeries::seriesObject() +{ + return this; +} + +QDeclarativeListProperty DeclarativeScatterSeries::points() +{ + return DeclarativeXySeries::points(); +} + +void DeclarativeScatterSeries::appendPoints(QDeclarativeListProperty *list, + DeclarativeXyPoint *element) +{ + DeclarativeXySeries::appendPoints(list, element); } #include "moc_declarativescatterseries.cpp" diff --git a/qmlplugin/declarativescatterseries.h b/qmlplugin/declarativescatterseries.h index b0954a3..a2fd044 100644 --- a/qmlplugin/declarativescatterseries.h +++ b/qmlplugin/declarativescatterseries.h @@ -2,17 +2,30 @@ #define DECLARATIVESCATTERSERIES_H #include "qchartglobal.h" +#include "qscatterseries.h" #include "declarativexyseries.h" -#include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class DeclarativeScatterSeries : public DeclarativeXySeries +class DeclarativeScatterSeries : public QScatterSeries, public DeclarativeXySeries { Q_OBJECT + Q_PROPERTY(QDeclarativeListProperty points READ points) public: - explicit DeclarativeScatterSeries(QDeclarativeItem *parent = 0); + explicit DeclarativeScatterSeries(QObject *parent = 0); + +public: + void seriesComplete(); + QObject *seriesObject(); + +public: + QDeclarativeListProperty points(); + +public slots: + static void appendPoints(QDeclarativeListProperty *list, + DeclarativeXyPoint *element); }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativesplineseries.cpp b/qmlplugin/declarativesplineseries.cpp new file mode 100644 index 0000000..b5c23f3 --- /dev/null +++ b/qmlplugin/declarativesplineseries.cpp @@ -0,0 +1,41 @@ +#include "declarativesplineseries.h" +#include "declarativechart.h" +#include "qchart.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +DeclarativeSplineSeries::DeclarativeSplineSeries(QObject *parent) : + QSplineSeries(parent) +{ +} + +void DeclarativeSplineSeries::seriesComplete() +{ + DeclarativeChart *declarativeChart = qobject_cast(parent()); + + if (declarativeChart) { + QChart *chart = qobject_cast(declarativeChart->m_chart); + Q_ASSERT(chart); + chart->addSeries(this); + } +} + +QObject *DeclarativeSplineSeries::seriesObject() +{ + return this; +} + +QDeclarativeListProperty DeclarativeSplineSeries::points() +{ + return DeclarativeXySeries::points(); +} + +void DeclarativeSplineSeries::appendPoints(QDeclarativeListProperty *list, + DeclarativeXyPoint *element) +{ + DeclarativeXySeries::appendPoints(list, element); +} + +#include "moc_declarativesplineseries.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativesplineseries.h b/qmlplugin/declarativesplineseries.h index 76f4083..8bc022a 100644 --- a/qmlplugin/declarativesplineseries.h +++ b/qmlplugin/declarativesplineseries.h @@ -2,21 +2,31 @@ #define DECLARATIVESPLINESERIES_H #include "qchartglobal.h" +#include "qsplineseries.h" #include "declarativexyseries.h" -#include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class DeclarativeSplineSeries : public DeclarativeXySeries +class DeclarativeSplineSeries : public QSplineSeries, public DeclarativeXySeries { Q_OBJECT + Q_PROPERTY(QDeclarativeListProperty points READ points) public: - explicit DeclarativeSplineSeries(QDeclarativeItem *parent = 0) : - DeclarativeXySeries(QSeries::SeriesTypeSpline, parent) {} -}; + explicit DeclarativeSplineSeries(QObject *parent = 0); + +public: + void seriesComplete(); + QObject *seriesObject(); -#include "moc_declarativesplineseries.cpp" +public: + QDeclarativeListProperty points(); + +public slots: + static void appendPoints(QDeclarativeListProperty *list, + DeclarativeXyPoint *element); +}; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativexypoint.cpp b/qmlplugin/declarativexypoint.cpp index c5b2393..f5dca0c 100644 --- a/qmlplugin/declarativexypoint.cpp +++ b/qmlplugin/declarativexypoint.cpp @@ -5,6 +5,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE DeclarativeXyPoint::DeclarativeXyPoint(QObject *parent) : QObject(parent) { + setX(0.0); + setY(0.0); } #include "moc_declarativexypoint.cpp" diff --git a/qmlplugin/declarativexypoint.h b/qmlplugin/declarativexypoint.h index 02e9df0..c72dc8d 100644 --- a/qmlplugin/declarativexypoint.h +++ b/qmlplugin/declarativexypoint.h @@ -3,10 +3,11 @@ #include "qchartglobal.h" #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class DeclarativeXyPoint : public QObject +class DeclarativeXyPoint : public QObject, public QPointF { Q_OBJECT Q_PROPERTY(qreal x READ x WRITE setX /*NOTIFY dataXChanged*/) @@ -14,15 +15,6 @@ class DeclarativeXyPoint : public QObject public: explicit DeclarativeXyPoint(QObject *parent = 0); - - void setX(qreal x) {m_x = x;} - qreal x() {return m_x;} - void setY(qreal y) {m_y = y;} - qreal y() {return m_y;} - -public: - qreal m_x; - qreal m_y; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativexyseries.cpp b/qmlplugin/declarativexyseries.cpp index bb78033..b7a0b78 100644 --- a/qmlplugin/declarativexyseries.cpp +++ b/qmlplugin/declarativexyseries.cpp @@ -1,86 +1,37 @@ //#include "DeclarativeXySeries.h" #include "declarativexyseries.h" -#include "declarativechart.h" -#include "qchart.h" #include "qxyseries.h" -#include "qareaseries.h" -#include "qsplineseries.h" -#include "qscatterseries.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE -DeclarativeXySeries::DeclarativeXySeries(QSeries::QSeriesType type, QDeclarativeItem *parent) : - QDeclarativeItem(parent), - m_seriesType(type), - m_chart(0), - m_series(0) +DeclarativeXySeries::DeclarativeXySeries() { - setFlag(QGraphicsItem::ItemHasNoContents, false); } DeclarativeXySeries::~DeclarativeXySeries() { } -void DeclarativeXySeries::componentComplete() +void DeclarativeXySeries::classBegin() { - Q_ASSERT(!m_series); - DeclarativeChart *declarativeChart = qobject_cast(parent()); - - if (declarativeChart) { - m_chart = qobject_cast(declarativeChart->m_chart); - Q_ASSERT(m_chart); - - switch (m_seriesType) { - case QSeries::SeriesTypeLine: - qDebug() << "creating line series for chart: " << m_chart; - m_series = new QLineSeries(); - break; - case QSeries::SeriesTypeArea: - qDebug() << "TODO: creating area series for chart: " << m_chart; -// m_series = new QAreaSeries(); - break; - case QSeries::SeriesTypeSpline: - qDebug() << "creating spline series for chart: " << m_chart; - m_series = new QSplineSeries(); - break; - case QSeries::SeriesTypeScatter: - qDebug() << "creating scatter series for chart: " << m_chart; - m_series = new QScatterSeries(); - break; - default: - Q_ASSERT(false); - } +} - for (int i(0); i < m_points.count(); i++) { - DeclarativeXyPoint *element = m_points.at(i); - m_series->add(element->x(), element->y()); - } - m_chart->addSeries(m_series); - } +void DeclarativeXySeries::componentComplete() +{ + seriesComplete(); } QDeclarativeListProperty DeclarativeXySeries::points() { - return QDeclarativeListProperty(this, 0, &DeclarativeXySeries::appendPoints); + return QDeclarativeListProperty(seriesObject(), 0, &DeclarativeXySeries::appendPoints); } void DeclarativeXySeries::appendPoints(QDeclarativeListProperty *list, DeclarativeXyPoint *element) { - DeclarativeXySeries *series = qobject_cast(list->object); - qDebug() << "appendPoints: " << series; - qDebug() << "appendPoints: " << element; - qDebug() << "appendPoints: " << element->x(); - qDebug() << "appendPoints: " << element->y(); - qDebug() << "appendPoints: " << series->m_series; - if (series) { - series->m_points.append(element); - if (series->m_series) - series->m_series->add(element->x(), element->y()); - } + QXYSeries *series = qobject_cast(list->object); + if (series) + series->add(QPointF(element->x(), element->y())); } -#include "moc_declarativexyseries.cpp" - QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativexyseries.h b/qmlplugin/declarativexyseries.h index ddd4f35..a981db1 100644 --- a/qmlplugin/declarativexyseries.h +++ b/qmlplugin/declarativexyseries.h @@ -3,41 +3,35 @@ #include "qchartglobal.h" #include "declarativexypoint.h" -#include "qxyseries.h" -#include +#include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE class QChart; -class DeclarativeXySeries : public QDeclarativeItem +class DeclarativeXySeries : public QDeclarativeParserStatus { - Q_OBJECT - Q_PROPERTY(QDeclarativeListProperty points READ points) + Q_INTERFACES(QDeclarativeParserStatus) public: - explicit DeclarativeXySeries(QSeries::QSeriesType type, QDeclarativeItem *parent = 0); + explicit DeclarativeXySeries(); ~DeclarativeXySeries(); public: // from QDeclarativeParserStatus + void classBegin(); void componentComplete(); public: QDeclarativeListProperty points(); -Q_SIGNALS: - public Q_SLOTS: static void appendPoints(QDeclarativeListProperty *list, - DeclarativeXyPoint *element); - -private Q_SLOTS: + DeclarativeXyPoint *element); -public: - QSeries::QSeriesType m_seriesType; - QChart *m_chart; - QXYSeries *m_series; - QList m_points; +protected: + virtual void seriesComplete() = 0; + virtual QObject *seriesObject() = 0; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/qmlplugin.pro b/qmlplugin/qmlplugin.pro index dd60250..51f4927 100644 --- a/qmlplugin/qmlplugin.pro +++ b/qmlplugin/qmlplugin.pro @@ -24,16 +24,17 @@ RCC_DIR = $$CHART_BUILD_DIR/plugin SOURCES += \ plugin.cpp \ declarativechart.cpp \ - declarativexypoint.cpp \ declarativexyseries.cpp \ + declarativexypoint.cpp \ declarativelineseries.cpp \ + declarativesplineseries.cpp \ declarativescatterseries.cpp \ declarativepieseries.cpp \ declarativebarseries.cpp HEADERS += \ declarativechart.h \ - declarativexypoint.h \ declarativexyseries.h \ + declarativexypoint.h \ declarativelineseries.h \ declarativesplineseries.h \ declarativescatterseries.h \ diff --git a/src/piechart/piesliceitem.cpp b/src/piechart/piesliceitem.cpp index fe5cc9c..0e0a5c8 100644 --- a/src/piechart/piesliceitem.cpp +++ b/src/piechart/piesliceitem.cpp @@ -47,9 +47,8 @@ QPainterPath PieSliceItem::shape() const void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) { - painter->setClipRect(parentItem()->boundingRect()); - painter->save(); + painter->setClipRect(parentItem()->boundingRect()); painter->setPen(m_data.m_slicePen); painter->setBrush(m_data.m_sliceBrush); painter->drawPath(m_slicePath); diff --git a/test/qmlchart/qml/qmlchart/main.qml b/test/qmlchart/qml/qmlchart/main.qml index 015f9a9..c541957 100644 --- a/test/qmlchart/qml/qmlchart/main.qml +++ b/test/qmlchart/qml/qmlchart/main.qml @@ -23,6 +23,8 @@ Rectangle { // console.log("model:" + myModel.item(0)); // myModel.insert(1, {"time":1.4; "speed":41.1 }); // scatter.appendData(); +// chart1.theme = Chart.ThemeHighContrast; +// chart2.theme = Chart.ThemeHighContrast; } @@ -36,21 +38,18 @@ Rectangle { BarSeries { barCategories: [ "2008", "2009", "2010", "2011", "2012" ] -// data: [ -// BarSet { } -// ] } -// PieSeries { -// data: [ -// 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 } -// ] -// } + PieSeries { + slices: [ + 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 } + ] + } } @@ -72,13 +71,13 @@ Rectangle { } SplineSeries { - id: scatter points: [ XyPoint { x: 0.0; y: 0.3 }, XyPoint { x: 1.1; y: 3.2 }, XyPoint { x: 4.17; y: 3.15 } ] } + ScatterSeries { points: [ XyPoint { x: 1.5; y: 1.5 },