From a47b376c6f77a283b9c11bdded58f84b270c2e28 2012-03-19 11:01:35 From: Tero Ahola Date: 2012-03-19 11:01:35 Subject: [PATCH] Added QML api for bar series --- diff --git a/qmlplugin/declarativebarseries.cpp b/qmlplugin/declarativebarseries.cpp new file mode 100644 index 0000000..bdae134 --- /dev/null +++ b/qmlplugin/declarativebarseries.cpp @@ -0,0 +1,50 @@ +#include "declarativebarseries.h" +#include "declarativechart.h" +#include "qchart.h" +#include "qbarseries.h" +#include "qbarset.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +DeclarativeBarSeries::DeclarativeBarSeries(QDeclarativeItem *parent) : + QDeclarativeItem(parent) +{ + setFlag(QGraphicsItem::ItemHasNoContents, false); + connect(this, SIGNAL(parentChanged()), + this, SLOT(setParentForSeries())); +} + +void DeclarativeBarSeries::setParentForSeries() +{ + if (!m_series) { + DeclarativeChart *declarativeChart = qobject_cast(parent()); + + if (declarativeChart) { + QChart *chart = qobject_cast(declarativeChart->m_chart); + Q_ASSERT(chart); + + QStringList categories; + categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; + m_series = new QBarSeries(categories); + + // TODO: use data from model + QBarSet *set0 = new QBarSet("Bub"); + QBarSet *set1 = new QBarSet("Bob"); + QBarSet *set2 = new QBarSet("Guybrush"); + + *set0 << 1 << 2 << 3 << 4 << 5 << 6; + *set1 << 5 << 0 << 0 << 4 << 0 << 7; + *set2 << 3 << 5 << 8 << 13 << 8 << 5; + + m_series->addBarSet(set0); + m_series->addBarSet(set1); + m_series->addBarSet(set2); + + chart->addSeries(m_series); + } + } +} + +#include "moc_declarativebarseries.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativebarseries.h b/qmlplugin/declarativebarseries.h new file mode 100644 index 0000000..ec5e0f3 --- /dev/null +++ b/qmlplugin/declarativebarseries.h @@ -0,0 +1,34 @@ +#ifndef DECLARATIVEBARSERIES_H +#define DECLARATIVEBARSERIES_H + +#include "qchartglobal.h" +#include "scatterelement.h" // TODO: rename header +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QChart; +class QBarSeries; + +class DeclarativeBarSeries : public QDeclarativeItem +{ + Q_OBJECT + +public: + explicit DeclarativeBarSeries(QDeclarativeItem *parent = 0); + +signals: + +public slots: + +private slots: + void setParentForSeries(); + +private: + QChart *m_chart; + QBarSeries *m_series; +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // DECLARATIVEBARSERIES_H diff --git a/qmlplugin/declarativechart.cpp b/qmlplugin/declarativechart.cpp index eef3ac1..0fd36a8 100644 --- a/qmlplugin/declarativechart.cpp +++ b/qmlplugin/declarativechart.cpp @@ -1,4 +1,5 @@ #include "declarativechart.h" +#include s QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -16,6 +17,8 @@ DeclarativeChart::ChartTheme DeclarativeChart::theme() void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) { + Q_UNUSED(oldGeometry) + if (newGeometry.isValid()) { if (newGeometry.width() > 0 && newGeometry.height() > 0) { m_chart->resize(newGeometry.width(), newGeometry.height()); @@ -23,6 +26,15 @@ void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF & } } +void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(option) + Q_UNUSED(widget) + + // TODO: optimized? + painter->setRenderHint(QPainter::Antialiasing, true); +} + #include "moc_declarativechart.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativechart.h b/qmlplugin/declarativechart.h index 99678a8..c882289 100644 --- a/qmlplugin/declarativechart.h +++ b/qmlplugin/declarativechart.h @@ -22,12 +22,14 @@ public: ThemeIcy, ThemeGrayscale, ThemeScientific, - ThemeUnnamed1 + ThemeBlueCerulean, + ThemeLight }; DeclarativeChart(QDeclarativeItem *parent = 0); public: // From QDeclarativeItem/QGraphicsItem void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry); + void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); public: void setTheme(ChartTheme theme) {m_chart->setChartTheme((QChart::ChartTheme) theme);} diff --git a/qmlplugin/plugin.cpp b/qmlplugin/plugin.cpp index b4d6cfa..a540317 100644 --- a/qmlplugin/plugin.cpp +++ b/qmlplugin/plugin.cpp @@ -4,6 +4,7 @@ #include "scatterelement.h" #include "declarativescatterseries.h" #include "declarativelineseries.h" +#include "declarativebarseries.h" #include "declarativepieseries.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -19,6 +20,7 @@ public: qmlRegisterType(uri, 1, 0, "Chart"); qmlRegisterType(uri, 1, 0, "ScatterSeries"); qmlRegisterType(uri, 1, 0, "LineSeries"); + qmlRegisterType(uri, 1, 0, "BarSeries"); qmlRegisterType(uri, 1, 0, "PieSeries"); qmlRegisterType(uri, 1, 0, "ChartPieElement"); // TODO: rename ScatterElement class to something like "PointElement" diff --git a/qmlplugin/qmlplugin.pro b/qmlplugin/qmlplugin.pro index 1496ff7..ed2ed41 100644 --- a/qmlplugin/qmlplugin.pro +++ b/qmlplugin/qmlplugin.pro @@ -27,13 +27,15 @@ SOURCES += \ declarativescatterseries.cpp \ scatterelement.cpp \ declarativepieseries.cpp \ - declarativelineseries.cpp + declarativelineseries.cpp \ + declarativebarseries.cpp HEADERS += \ declarativechart.h \ declarativescatterseries.h \ scatterelement.h \ declarativepieseries.h \ - declarativelineseries.h + declarativelineseries.h \ + declarativebarseries.h TARGETPATH = QtCommercial/Chart target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH diff --git a/test/qmlchart/qml/qmlchart/main.qml b/test/qmlchart/qml/qmlchart/main.qml index 4cecfa2..63354f3 100644 --- a/test/qmlchart/qml/qmlchart/main.qml +++ b/test/qmlchart/qml/qmlchart/main.qml @@ -20,44 +20,35 @@ Rectangle { // } Component.onCompleted: { -// console.log("Component.onCompleted: " + ChartPointElement.x); -// console.log("Component.onCompleted: " + ChartPointElement.y); -// console.log("Component.onCompleted: " + ChartPointElement.dataX); -// console.log("Component.onCompleted: " + ChartPointElement.dataY); - //console.log("Component.onCompleted: " + chartModel.get(0).x); - //console.log("Component.onCompleted: " + chartModel.ChartPointElements); -// console.log("Component.onCompleted: " + elementt.dataX); // console.log("Component.onCompleted: " + chartModel.get(0).dataX); - //ChartPointElement { x: 0.3; y: 0.3 } } +// ChartModel { +// id: dynamicData +// } + Chart { id: chart1 anchors.top: parent.top anchors.left: parent.left anchors.right: parent.right height: parent.height / 2 - theme: Chart.ThemeVanilla + theme: Chart.ThemeBlueCerulean - PieSeries { - data: [ - // TODO: "NnElement" matches the naming convention of for example ListModel... - // But PieSlice would match the naming of QtCommercial Charts C++ api - ChartPieElement { label: "Volkswagen"; value: 13.5 }, - ChartPieElement { label: "Toyota"; value: 10.9 }, - ChartPieElement { label: "Ford"; value: 8.6 }, - ChartPieElement { label: "Skoda"; value: 8.2 }, - ChartPieElement { label: "Volvo"; value: 6.8 }, - ChartPieElement { label: "Others"; value: 52.0 } - ] + BarSeries { } -// Series { -// seriesType: Series.SeriesTypeLine -// } - // TODO: -// Series { -// seriesType: Series.SeriesTypeBar +// PieSeries { +// data: [ +// // TODO: "NnElement" matches the naming convention of for example ListModel... +// // But PieSlice would match the naming of QtCommercial Charts C++ api +// ChartPieElement { label: "Volkswagen"; value: 13.5 }, +// ChartPieElement { label: "Toyota"; value: 10.9 }, +// ChartPieElement { label: "Ford"; value: 8.6 }, +// ChartPieElement { label: "Skoda"; value: 8.2 }, +// ChartPieElement { label: "Volvo"; value: 6.8 }, +// ChartPieElement { label: "Others"; value: 52.0 } +// ] // } } @@ -101,7 +92,6 @@ Rectangle { ] } ScatterSeries { - id: scatter4 data: [ ChartPointElement { x: 2.6; y: 2.6 }, ChartPointElement { x: 2.6; y: 2.7 },