@@ -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 | 15 | SOURCES += main.cpp \ |
|
16 | 16 | mainwidget.cpp \ |
|
17 | 17 | qscatterseries.cpp \ |
|
18 | qchartwidget.cpp | |
|
18 | qchartwidget.cpp \ | |
|
19 | qseriespointgraphicsitem.cpp | |
|
19 | 20 | |
|
20 | 21 | HEADERS += \ |
|
21 | 22 | mainwidget.h \ |
|
22 | 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 | 10 | #include <QLabel> |
|
11 | 11 | #include <QSpacerItem> |
|
12 | 12 | #include <QMessageBox> |
|
13 | #include <cmath> | |
|
13 | 14 | #include <QDebug> |
|
14 | 15 | |
|
15 | 16 | MainWidget::MainWidget(QWidget *parent) : |
@@ -96,6 +97,7 MainWidget::MainWidget(QWidget *parent) : | |||
|
96 | 97 | setLayout(hbox); |
|
97 | 98 | |
|
98 | 99 | m_autoScaleCheck->setChecked(true); |
|
100 | chartTypeChanged(4); | |
|
99 | 101 | } |
|
100 | 102 | |
|
101 | 103 | void MainWidget::chartTypeChanged(int itemIndex) |
@@ -103,13 +105,25 void MainWidget::chartTypeChanged(int itemIndex) | |||
|
103 | 105 | // TODO: change chart type |
|
104 | 106 | switch (itemIndex) { |
|
105 | 107 | case 4: { |
|
106 | m_chartWidget->setType(4); | |
|
107 | break; | |
|
108 | } | |
|
109 | default: | |
|
108 | QList<QChartDataPoint> data; | |
|
109 | for (int x = 0; x < 1000; ++x) { | |
|
110 | data.append(QChartDataPoint() << x -200 << 2 * (uint(sin(3.14159/50*x)*80) % 100)); | |
|
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 | 123 | m_chartWidget->setType(0); |
|
111 | 124 | break; |
|
112 | 125 | } |
|
126 | } | |
|
113 | 127 | } |
|
114 | 128 | |
|
115 | 129 | void MainWidget::dataChanged(QString itemText) |
@@ -4,17 +4,12 | |||
|
4 | 4 | #include <QGraphicsScene> |
|
5 | 5 | |
|
6 | 6 | QChartWidget::QChartWidget(QWidget *parent) : |
|
7 | QWidget(parent) | |
|
7 | QWidget(parent), | |
|
8 | m_scatter(0) | |
|
8 | 9 | { |
|
9 | // scatter | |
|
10 | 10 | m_scene = new QGraphicsScene(); |
|
11 | 11 | m_view = new QGraphicsView(m_scene, this); |
|
12 |
m_view->resize( |
|
|
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 | ||
|
12 | m_view->resize(490, 300); | |
|
18 | 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 | 7 | class QGraphicsScene; |
|
8 | 8 | class QScatterSeries; |
|
9 | 9 | |
|
10 | // TODO: | |
|
11 | #define QChartDataPoint QList<QVariant> | |
|
12 | ||
|
10 | 13 | class QChartWidget : public QWidget |
|
11 | 14 | { |
|
12 | 15 | Q_OBJECT |
@@ -14,6 +17,8 public: | |||
|
14 | 17 | explicit QChartWidget(QWidget *parent = 0); |
|
15 | 18 | ~QChartWidget(); |
|
16 | 19 | void setType(/*TODO QChart::Type*/ int type); |
|
20 | void setData(QList<QChartDataPoint> data); | |
|
21 | // void setData(QList<int> data); | |
|
17 | 22 | |
|
18 | 23 | signals: |
|
19 | 24 |
@@ -1,5 +1,7 | |||
|
1 | 1 | #include "qscatterseries.h" |
|
2 | #include "qseriespointgraphicsitem.h" | |
|
2 | 3 | #include <QPainter> |
|
4 | #include <QGraphicsScene> | |
|
3 | 5 | |
|
4 | 6 | QScatterSeries::QScatterSeries(QGraphicsItem *parent) : |
|
5 | 7 | QGraphicsItem(parent) |
@@ -8,22 +10,21 QScatterSeries::QScatterSeries(QGraphicsItem *parent) : | |||
|
8 | 10 | |
|
9 | 11 | QRectF QScatterSeries::boundingRect() const |
|
10 | 12 | { |
|
11 | // qreal penWidth = 1; | |
|
12 | // return QRectF(-10 - penWidth / 2, -10 - penWidth / 2, | |
|
13 | // 20 + penWidth, 20 + penWidth); | |
|
14 | return QRectF(0, 0, 100, 100); | |
|
13 | return QRectF(0, 50, 100, 100); | |
|
15 | 14 | } |
|
16 | 15 | |
|
17 | 16 | void QScatterSeries::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
18 | 17 | { |
|
19 | QPen pen = painter->pen(); | |
|
20 | QBrush brush = pen.brush(); | |
|
21 | brush.setColor(Qt::darkRed); | |
|
22 | pen.setBrush(brush); | |
|
23 | painter->setPen(pen); | |
|
24 | painter->drawRoundedRect(0, 0, 10, 10, 5, 5); | |
|
25 | painter->drawRoundedRect(5, 5, 10, 10, 5, 5); | |
|
26 | painter->drawRoundedRect(54, 25, 10, 10, 5, 5); | |
|
27 | painter->drawRoundedRect(34, 35, 10, 10, 5, 5); | |
|
28 | //painter->drawEllipse(QPoint(10, 10), 100, 100); | |
|
18 | // painter->drawRect(boundingRect()); | |
|
19 | } | |
|
20 | ||
|
21 | void QScatterSeries::setData(QList<QChartDataPoint> data) | |
|
22 | //void QScatterSeries::setData(QList<int> data) | |
|
23 | { | |
|
24 | foreach(QChartDataPoint point, data) { | |
|
25 | // TODO: ownership? | |
|
26 | // TODO: position | |
|
27 | QSeriesPointGraphicsItem *item = new QSeriesPointGraphicsItem( | |
|
28 | point[0].toReal(), 100-point[1].toReal(), this); | |
|
29 | } | |
|
29 | 30 | } |
@@ -3,16 +3,23 | |||
|
3 | 3 | |
|
4 | 4 | #include <QGraphicsItem> |
|
5 | 5 | |
|
6 | // TODO: | |
|
7 | #define QChartDataPoint QList<QVariant> | |
|
8 | ||
|
9 | class QSeriesPointGraphicsItem; | |
|
10 | ||
|
6 | 11 | class QScatterSeries : public QGraphicsItem |
|
7 | 12 | { |
|
8 | 13 | public: |
|
9 |
|
|
|
14 | QScatterSeries(QGraphicsItem *parent = 0); | |
|
15 | void setData(QList<QChartDataPoint> data); | |
|
16 | // void setData(QList<int> data); | |
|
10 | 17 | |
|
11 | 18 | signals: |
|
12 | 19 | |
|
13 | 20 | public slots: |
|
14 | 21 | |
|
15 | private: | |
|
22 | public: | |
|
16 | 23 | QRectF boundingRect() const; |
|
17 | 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