##// END OF EJS Templates
Scatter data point now shown using a picture
Tero Ahola -
r13:335e8906dfcb
parent child
Show More
@@ -0,0 +1,37
1 #include "qseriespointgraphicsitem.h"
2 #include <QPainter>
3 #include <QPicture>
4 #include <QPixmap>
5 #include <QDebug>
6
7 QSeriesPointGraphicsItem::QSeriesPointGraphicsItem(qreal dx, qreal dy, QGraphicsItem *parent)
8 : QGraphicsItem(parent),
9 m_dx(dx),
10 m_dy(dy)
11 {
12 }
13
14 QRectF QSeriesPointGraphicsItem::boundingRect() const
15 {
16 // return parentItem()->boundingRect();
17 return QRectF(0, 0, 10, 10);
18 }
19
20 void QSeriesPointGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
21 {
22 QTransform transform = painter->transform();
23 transform.translate(m_dx, m_dy);
24 painter->setTransform(transform);
25
26 QPen pen = painter->pen();
27 QBrush brush = pen.brush();
28 brush.setColor(Qt::darkRed);
29 pen.setBrush(brush);
30 painter->setPen(pen);
31
32 QPixmap pixmap;
33 pixmap.load("scatter.png");
34 painter->drawPixmap(boundingRect().toRect(), pixmap);
35
36 // painter->drawRect(boundingRect());
37 }
@@ -0,0 +1,18
1 #ifndef QSERIESPOINTGRAPHICSITEM_H
2 #define QSERIESPOINTGRAPHICSITEM_H
3
4 #include <QGraphicsItem>
5
6 class QSeriesPointGraphicsItem : public QGraphicsItem
7 {
8 public:
9 QSeriesPointGraphicsItem(qreal dx, qreal dy, QGraphicsItem *parent = 0);
10
11 private:
12 QRectF boundingRect() const;
13 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
14 qreal m_dx;
15 qreal m_dy;
16 };
17
18 #endif // QSERIESPOINTGRAPHICSITEM_H
@@ -15,12 +15,16 MOC_DIR = tmp
15 SOURCES += main.cpp \
15 SOURCES += main.cpp \
16 mainwidget.cpp \
16 mainwidget.cpp \
17 qscatterseries.cpp \
17 qscatterseries.cpp \
18 qchartwidget.cpp
18 qchartwidget.cpp \
19 qseriespointgraphicsitem.cpp
19
20
20 HEADERS += \
21 HEADERS += \
21 mainwidget.h \
22 mainwidget.h \
22 qscatterseries.h \
23 qscatterseries.h \
23 qchartwidget.h
24 qchartwidget.h \
25 qseriespointgraphicsitem.h
26
27
24
28
25
29
26
30
@@ -10,6 +10,7
10 #include <QLabel>
10 #include <QLabel>
11 #include <QSpacerItem>
11 #include <QSpacerItem>
12 #include <QMessageBox>
12 #include <QMessageBox>
13 #include <cmath>
13 #include <QDebug>
14 #include <QDebug>
14
15
15 MainWidget::MainWidget(QWidget *parent) :
16 MainWidget::MainWidget(QWidget *parent) :
@@ -96,6 +97,7 MainWidget::MainWidget(QWidget *parent) :
96 setLayout(hbox);
97 setLayout(hbox);
97
98
98 m_autoScaleCheck->setChecked(true);
99 m_autoScaleCheck->setChecked(true);
100 chartTypeChanged(4);
99 }
101 }
100
102
101 void MainWidget::chartTypeChanged(int itemIndex)
103 void MainWidget::chartTypeChanged(int itemIndex)
@@ -103,13 +105,25 void MainWidget::chartTypeChanged(int itemIndex)
103 // TODO: change chart type
105 // TODO: change chart type
104 switch (itemIndex) {
106 switch (itemIndex) {
105 case 4: {
107 case 4: {
106 m_chartWidget->setType(4);
108 QList<QChartDataPoint> data;
107 break;
109 for (int x = 0; x < 1000; ++x) {
108 }
110 data.append(QChartDataPoint() << x -200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
109 default:
111 }
112 // data.append(QChartDataPoint() << 0 << 0);
113 // data.append(QChartDataPoint() << 2 << 2);
114 // data.append(QChartDataPoint() << 4 << 5);
115 // data.append(QChartDataPoint() << 5 << 9);
116 // data.append(QChartDataPoint() << 20 << 20);
117 // QList<int> data;
118 m_chartWidget->setType(4);
119 m_chartWidget->setData(data);
120 break;
121 }
122 default: {
110 m_chartWidget->setType(0);
123 m_chartWidget->setType(0);
111 break;
124 break;
112 }
125 }
126 }
113 }
127 }
114
128
115 void MainWidget::dataChanged(QString itemText)
129 void MainWidget::dataChanged(QString itemText)
@@ -4,17 +4,12
4 #include <QGraphicsScene>
4 #include <QGraphicsScene>
5
5
6 QChartWidget::QChartWidget(QWidget *parent) :
6 QChartWidget::QChartWidget(QWidget *parent) :
7 QWidget(parent)
7 QWidget(parent),
8 m_scatter(0)
8 {
9 {
9 // scatter
10 m_scene = new QGraphicsScene();
10 m_scene = new QGraphicsScene();
11 m_view = new QGraphicsView(m_scene, this);
11 m_view = new QGraphicsView(m_scene, this);
12 m_view->resize(300, 300);
12 m_view->resize(490, 300);
13
14 // TODO: implement graphics item for axis
15 m_scene->addLine(0, 0, 0, 100);
16 m_scene->addLine(0, 100, 100, 100);
17
18 m_view->show();
13 m_view->show();
19 }
14 }
20
15
@@ -39,3 +34,11 void QChartWidget::setType(/*TODO QChart::Type*/ int type)
39 }
34 }
40 }
35 }
41 }
36 }
37
38 void QChartWidget::setData(QList<QChartDataPoint> data)
39 //void QChartWidget::setData(QList<int> data)
40 {
41 // TODO: other chart types... this looks ugly
42 if (m_scatter)
43 m_scatter->setData(data);
44 }
@@ -7,6 +7,9 class QGraphicsView;
7 class QGraphicsScene;
7 class QGraphicsScene;
8 class QScatterSeries;
8 class QScatterSeries;
9
9
10 // TODO:
11 #define QChartDataPoint QList<QVariant>
12
10 class QChartWidget : public QWidget
13 class QChartWidget : public QWidget
11 {
14 {
12 Q_OBJECT
15 Q_OBJECT
@@ -14,6 +17,8 public:
14 explicit QChartWidget(QWidget *parent = 0);
17 explicit QChartWidget(QWidget *parent = 0);
15 ~QChartWidget();
18 ~QChartWidget();
16 void setType(/*TODO QChart::Type*/ int type);
19 void setType(/*TODO QChart::Type*/ int type);
20 void setData(QList<QChartDataPoint> data);
21 // void setData(QList<int> data);
17
22
18 signals:
23 signals:
19
24
@@ -1,5 +1,7
1 #include "qscatterseries.h"
1 #include "qscatterseries.h"
2 #include "qseriespointgraphicsitem.h"
2 #include <QPainter>
3 #include <QPainter>
4 #include <QGraphicsScene>
3
5
4 QScatterSeries::QScatterSeries(QGraphicsItem *parent) :
6 QScatterSeries::QScatterSeries(QGraphicsItem *parent) :
5 QGraphicsItem(parent)
7 QGraphicsItem(parent)
@@ -8,22 +10,21 QScatterSeries::QScatterSeries(QGraphicsItem *parent) :
8
10
9 QRectF QScatterSeries::boundingRect() const
11 QRectF QScatterSeries::boundingRect() const
10 {
12 {
11 // qreal penWidth = 1;
13 return QRectF(0, 50, 100, 100);
12 // return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
13 // 20 + penWidth, 20 + penWidth);
14 return QRectF(0, 0, 100, 100);
15 }
14 }
16
15
17 void QScatterSeries::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
16 void QScatterSeries::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
18 {
17 {
19 QPen pen = painter->pen();
18 // painter->drawRect(boundingRect());
20 QBrush brush = pen.brush();
19 }
21 brush.setColor(Qt::darkRed);
20
22 pen.setBrush(brush);
21 void QScatterSeries::setData(QList<QChartDataPoint> data)
23 painter->setPen(pen);
22 //void QScatterSeries::setData(QList<int> data)
24 painter->drawRoundedRect(0, 0, 10, 10, 5, 5);
23 {
25 painter->drawRoundedRect(5, 5, 10, 10, 5, 5);
24 foreach(QChartDataPoint point, data) {
26 painter->drawRoundedRect(54, 25, 10, 10, 5, 5);
25 // TODO: ownership?
27 painter->drawRoundedRect(34, 35, 10, 10, 5, 5);
26 // TODO: position
28 //painter->drawEllipse(QPoint(10, 10), 100, 100);
27 QSeriesPointGraphicsItem *item = new QSeriesPointGraphicsItem(
28 point[0].toReal(), 100-point[1].toReal(), this);
29 }
29 }
30 }
@@ -3,16 +3,23
3
3
4 #include <QGraphicsItem>
4 #include <QGraphicsItem>
5
5
6 // TODO:
7 #define QChartDataPoint QList<QVariant>
8
9 class QSeriesPointGraphicsItem;
10
6 class QScatterSeries : public QGraphicsItem
11 class QScatterSeries : public QGraphicsItem
7 {
12 {
8 public:
13 public:
9 explicit QScatterSeries(QGraphicsItem *parent = 0);
14 QScatterSeries(QGraphicsItem *parent = 0);
15 void setData(QList<QChartDataPoint> data);
16 // void setData(QList<int> data);
10
17
11 signals:
18 signals:
12
19
13 public slots:
20 public slots:
14
21
15 private:
22 public:
16 QRectF boundingRect() const;
23 QRectF boundingRect() const;
17 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
24 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
18 };
25 };
General Comments 0
You need to be logged in to leave comments. Login now