From 335e8906dfcb4772bd383526911ca918cb2d89ca 2012-01-16 09:07:36 From: Tero Ahola Date: 2012-01-16 09:07:36 Subject: [PATCH] Scatter data point now shown using a picture --- diff --git a/test/chartwidgettest/chartwidgettest.pro b/test/chartwidgettest/chartwidgettest.pro index 25b59ad..fce9129 100644 --- a/test/chartwidgettest/chartwidgettest.pro +++ b/test/chartwidgettest/chartwidgettest.pro @@ -15,12 +15,16 @@ MOC_DIR = tmp SOURCES += main.cpp \ mainwidget.cpp \ qscatterseries.cpp \ - qchartwidget.cpp + qchartwidget.cpp \ + qseriespointgraphicsitem.cpp HEADERS += \ mainwidget.h \ qscatterseries.h \ - qchartwidget.h + qchartwidget.h \ + qseriespointgraphicsitem.h + + diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index 549cf44..7556044 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include MainWidget::MainWidget(QWidget *parent) : @@ -96,6 +97,7 @@ MainWidget::MainWidget(QWidget *parent) : setLayout(hbox); m_autoScaleCheck->setChecked(true); + chartTypeChanged(4); } void MainWidget::chartTypeChanged(int itemIndex) @@ -103,13 +105,25 @@ void MainWidget::chartTypeChanged(int itemIndex) // TODO: change chart type switch (itemIndex) { case 4: { - m_chartWidget->setType(4); - break; - } - default: + QList data; + for (int x = 0; x < 1000; ++x) { + data.append(QChartDataPoint() << x -200 << 2 * (uint(sin(3.14159/50*x)*80) % 100)); + } +// data.append(QChartDataPoint() << 0 << 0); +// data.append(QChartDataPoint() << 2 << 2); +// data.append(QChartDataPoint() << 4 << 5); +// data.append(QChartDataPoint() << 5 << 9); +// data.append(QChartDataPoint() << 20 << 20); +// QList data; + m_chartWidget->setType(4); + m_chartWidget->setData(data); + break; + } + default: { m_chartWidget->setType(0); break; } + } } void MainWidget::dataChanged(QString itemText) diff --git a/test/chartwidgettest/qchartwidget.cpp b/test/chartwidgettest/qchartwidget.cpp index 9e479f4..d145942 100644 --- a/test/chartwidgettest/qchartwidget.cpp +++ b/test/chartwidgettest/qchartwidget.cpp @@ -4,17 +4,12 @@ #include QChartWidget::QChartWidget(QWidget *parent) : - QWidget(parent) + QWidget(parent), + m_scatter(0) { - // scatter m_scene = new QGraphicsScene(); m_view = new QGraphicsView(m_scene, this); - m_view->resize(300, 300); - - // TODO: implement graphics item for axis - m_scene->addLine(0, 0, 0, 100); - m_scene->addLine(0, 100, 100, 100); - + m_view->resize(490, 300); m_view->show(); } @@ -39,3 +34,11 @@ void QChartWidget::setType(/*TODO QChart::Type*/ int type) } } } + +void QChartWidget::setData(QList data) +//void QChartWidget::setData(QList data) +{ + // TODO: other chart types... this looks ugly + if (m_scatter) + m_scatter->setData(data); +} diff --git a/test/chartwidgettest/qchartwidget.h b/test/chartwidgettest/qchartwidget.h index b574248..29261d3 100644 --- a/test/chartwidgettest/qchartwidget.h +++ b/test/chartwidgettest/qchartwidget.h @@ -7,6 +7,9 @@ class QGraphicsView; class QGraphicsScene; class QScatterSeries; +// TODO: +#define QChartDataPoint QList + class QChartWidget : public QWidget { Q_OBJECT @@ -14,6 +17,8 @@ public: explicit QChartWidget(QWidget *parent = 0); ~QChartWidget(); void setType(/*TODO QChart::Type*/ int type); + void setData(QList data); +// void setData(QList data); signals: diff --git a/test/chartwidgettest/qscatterseries.cpp b/test/chartwidgettest/qscatterseries.cpp index a008e84..0d88a4d 100644 --- a/test/chartwidgettest/qscatterseries.cpp +++ b/test/chartwidgettest/qscatterseries.cpp @@ -1,5 +1,7 @@ #include "qscatterseries.h" +#include "qseriespointgraphicsitem.h" #include +#include QScatterSeries::QScatterSeries(QGraphicsItem *parent) : QGraphicsItem(parent) @@ -8,22 +10,21 @@ QScatterSeries::QScatterSeries(QGraphicsItem *parent) : QRectF QScatterSeries::boundingRect() const { -// qreal penWidth = 1; -// return QRectF(-10 - penWidth / 2, -10 - penWidth / 2, -// 20 + penWidth, 20 + penWidth); - return QRectF(0, 0, 100, 100); + return QRectF(0, 50, 100, 100); } void QScatterSeries::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { - QPen pen = painter->pen(); - QBrush brush = pen.brush(); - brush.setColor(Qt::darkRed); - pen.setBrush(brush); - painter->setPen(pen); - painter->drawRoundedRect(0, 0, 10, 10, 5, 5); - painter->drawRoundedRect(5, 5, 10, 10, 5, 5); - painter->drawRoundedRect(54, 25, 10, 10, 5, 5); - painter->drawRoundedRect(34, 35, 10, 10, 5, 5); - //painter->drawEllipse(QPoint(10, 10), 100, 100); +// painter->drawRect(boundingRect()); +} + +void QScatterSeries::setData(QList data) +//void QScatterSeries::setData(QList data) +{ + foreach(QChartDataPoint point, data) { + // TODO: ownership? + // TODO: position + QSeriesPointGraphicsItem *item = new QSeriesPointGraphicsItem( + point[0].toReal(), 100-point[1].toReal(), this); + } } diff --git a/test/chartwidgettest/qscatterseries.h b/test/chartwidgettest/qscatterseries.h index 56d570a..e6e1b3b 100644 --- a/test/chartwidgettest/qscatterseries.h +++ b/test/chartwidgettest/qscatterseries.h @@ -3,16 +3,23 @@ #include +// TODO: +#define QChartDataPoint QList + +class QSeriesPointGraphicsItem; + class QScatterSeries : public QGraphicsItem { public: - explicit QScatterSeries(QGraphicsItem *parent = 0); + QScatterSeries(QGraphicsItem *parent = 0); + void setData(QList data); +// void setData(QList data); signals: public slots: -private: +public: QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); }; diff --git a/test/chartwidgettest/qseriespointgraphicsitem.cpp b/test/chartwidgettest/qseriespointgraphicsitem.cpp new file mode 100644 index 0000000..6ecfb28 --- /dev/null +++ b/test/chartwidgettest/qseriespointgraphicsitem.cpp @@ -0,0 +1,37 @@ +#include "qseriespointgraphicsitem.h" +#include +#include +#include +#include + +QSeriesPointGraphicsItem::QSeriesPointGraphicsItem(qreal dx, qreal dy, QGraphicsItem *parent) + : QGraphicsItem(parent), + m_dx(dx), + m_dy(dy) +{ +} + +QRectF QSeriesPointGraphicsItem::boundingRect() const +{ +// return parentItem()->boundingRect(); + return QRectF(0, 0, 10, 10); +} + +void QSeriesPointGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + QTransform transform = painter->transform(); + transform.translate(m_dx, m_dy); + painter->setTransform(transform); + + QPen pen = painter->pen(); + QBrush brush = pen.brush(); + brush.setColor(Qt::darkRed); + pen.setBrush(brush); + painter->setPen(pen); + + QPixmap pixmap; + pixmap.load("scatter.png"); + painter->drawPixmap(boundingRect().toRect(), pixmap); + +// painter->drawRect(boundingRect()); +} diff --git a/test/chartwidgettest/qseriespointgraphicsitem.h b/test/chartwidgettest/qseriespointgraphicsitem.h new file mode 100644 index 0000000..29f94ef --- /dev/null +++ b/test/chartwidgettest/qseriespointgraphicsitem.h @@ -0,0 +1,18 @@ +#ifndef QSERIESPOINTGRAPHICSITEM_H +#define QSERIESPOINTGRAPHICSITEM_H + +#include + +class QSeriesPointGraphicsItem : public QGraphicsItem +{ +public: + QSeriesPointGraphicsItem(qreal dx, qreal dy, QGraphicsItem *parent = 0); + +private: + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + qreal m_dx; + qreal m_dy; +}; + +#endif // QSERIESPOINTGRAPHICSITEM_H