@@ -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 |
@@ -1,28 +1,32 | |||
|
1 | 1 | TARGET = chartwidgettest |
|
2 | 2 | TEMPLATE = app |
|
3 | 3 | |
|
4 | 4 | QT += core gui |
|
5 | 5 | contains(QT_MAJOR_VERSION, 5) { |
|
6 | 6 | QT += widgets |
|
7 | 7 | } |
|
8 | 8 | |
|
9 | 9 | CONFIG += charts |
|
10 | 10 | CHARTS += widget |
|
11 | 11 | |
|
12 | 12 | OBJECTS_DIR = tmp |
|
13 | 13 | MOC_DIR = tmp |
|
14 | 14 | |
|
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 | |
|
27 | 31 | |
|
28 | 32 |
@@ -1,157 +1,171 | |||
|
1 | 1 | #include "mainwidget.h" |
|
2 | 2 | #include "qchartwidget.h" |
|
3 | 3 | //#include <chartwidget.h> |
|
4 | 4 | #include <QPushButton> |
|
5 | 5 | #include <QComboBox> |
|
6 | 6 | #include <QSpinBox> |
|
7 | 7 | #include <QCheckBox> |
|
8 | 8 | #include <QGridLayout> |
|
9 | 9 | #include <QHBoxLayout> |
|
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) : |
|
16 | 17 | QWidget(parent) |
|
17 | 18 | { |
|
18 | 19 | m_chartWidget = new QChartWidget(this); |
|
19 | 20 | // m_chartWidget->resize(QSize(200,200)); |
|
20 | 21 | // m_chartWidget->setColor(Qt::red); |
|
21 | 22 | // Chart type |
|
22 | 23 | // TODO: How about multiple types? |
|
23 | 24 | // Should the type be a property of a graph instead of the chart? |
|
24 | 25 | QComboBox *chartTypeCombo = new QComboBox(this); |
|
25 | 26 | chartTypeCombo->addItem("Line"); |
|
26 | 27 | chartTypeCombo->addItem("Area"); |
|
27 | 28 | chartTypeCombo->addItem("Bar"); |
|
28 | 29 | chartTypeCombo->addItem("Pie"); |
|
29 | 30 | chartTypeCombo->addItem("Scatter"); |
|
30 | 31 | chartTypeCombo->addItem("Spline"); |
|
31 | 32 | connect(chartTypeCombo, SIGNAL(currentIndexChanged(int)), |
|
32 | 33 | this, SLOT(chartTypeChanged(int))); |
|
33 | 34 | |
|
34 | 35 | // Test data selector |
|
35 | 36 | QPushButton *fileButton = new QPushButton("From file"); |
|
36 | 37 | QPushButton *urlButton = new QPushButton("From URL"); |
|
37 | 38 | |
|
38 | 39 | // Chart background |
|
39 | 40 | QComboBox *backgroundCombo = new QComboBox(this); |
|
40 | 41 | backgroundCombo->addItem("todo: add background types"); |
|
41 | 42 | connect(backgroundCombo, SIGNAL(currentIndexChanged(int)), |
|
42 | 43 | this, SLOT(backgroundChanged(int))); |
|
43 | 44 | |
|
44 | 45 | // Axis |
|
45 | 46 | // TODO: multiple axes? |
|
46 | 47 | m_autoScaleCheck = new QCheckBox("Automatic scaling"); |
|
47 | 48 | connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int))); |
|
48 | 49 | // Allow setting also non-sense values (like -2147483648 and 2147483647) |
|
49 | 50 | m_xMinSpin = new QSpinBox(); |
|
50 | 51 | m_xMinSpin->setMinimum(INT_MIN); |
|
51 | 52 | m_xMinSpin->setMaximum(INT_MAX); |
|
52 | 53 | m_xMinSpin->setValue(0); |
|
53 | 54 | connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int))); |
|
54 | 55 | m_xMaxSpin = new QSpinBox(); |
|
55 | 56 | m_xMaxSpin->setMinimum(INT_MIN); |
|
56 | 57 | m_xMaxSpin->setMaximum(INT_MAX); |
|
57 | 58 | m_xMaxSpin->setValue(10); |
|
58 | 59 | connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int))); |
|
59 | 60 | m_yMinSpin = new QSpinBox(); |
|
60 | 61 | m_yMinSpin->setMinimum(INT_MIN); |
|
61 | 62 | m_yMinSpin->setMaximum(INT_MAX); |
|
62 | 63 | m_yMinSpin->setValue(0); |
|
63 | 64 | connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int))); |
|
64 | 65 | m_yMaxSpin = new QSpinBox(); |
|
65 | 66 | m_yMaxSpin->setMinimum(INT_MIN); |
|
66 | 67 | m_yMaxSpin->setMaximum(INT_MAX); |
|
67 | 68 | m_yMaxSpin->setValue(10); |
|
68 | 69 | connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int))); |
|
69 | 70 | |
|
70 | 71 | QGridLayout *grid = new QGridLayout(); |
|
71 | 72 | QHBoxLayout *hbox = new QHBoxLayout(); |
|
72 | 73 | grid->addWidget(new QLabel("Chart type:"), 0, 0); |
|
73 | 74 | grid->addWidget(chartTypeCombo, 0, 1, 1, 2); |
|
74 | 75 | grid->addWidget(new QLabel("Data:"), 1, 0); |
|
75 | 76 | grid->addWidget(fileButton, 1, 1); |
|
76 | 77 | grid->addWidget(urlButton, 1, 2); |
|
77 | 78 | grid->addWidget(new QLabel("Background:"), 2, 0); |
|
78 | 79 | grid->addWidget(backgroundCombo, 2, 1, 1, 2); |
|
79 | 80 | grid->addWidget(m_autoScaleCheck, 3, 0); |
|
80 | 81 | grid->addWidget(new QLabel("x min:"), 4, 0); |
|
81 | 82 | grid->addWidget(m_xMinSpin, 4, 1, 1, 2); |
|
82 | 83 | grid->addWidget(new QLabel("x max:"), 5, 0); |
|
83 | 84 | grid->addWidget(m_xMaxSpin, 5, 1, 1, 2); |
|
84 | 85 | grid->addWidget(new QLabel("y min:"), 6, 0); |
|
85 | 86 | grid->addWidget(m_yMinSpin, 6, 1, 1, 2); |
|
86 | 87 | grid->addWidget(new QLabel("y max:"), 7, 0); |
|
87 | 88 | grid->addWidget(m_yMaxSpin, 7, 1, 1, 2); |
|
88 | 89 | // add row with empty label to make all the other rows static |
|
89 | 90 | grid->addWidget(new QLabel(""), 8, 0); |
|
90 | 91 | grid->setRowStretch(8, 1); |
|
91 | 92 | |
|
92 | 93 | hbox->addLayout(grid); |
|
93 | 94 | hbox->addWidget(m_chartWidget); |
|
94 | 95 | hbox->setStretch(1, 1); |
|
95 | 96 | |
|
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) |
|
102 | 104 | { |
|
103 | 105 | // TODO: change chart type |
|
104 | 106 | switch (itemIndex) { |
|
105 | 107 | case 4: { |
|
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; | |
|
106 | 118 |
|
|
119 | m_chartWidget->setData(data); | |
|
107 | 120 |
|
|
108 | 121 |
|
|
109 | default: | |
|
122 | default: { | |
|
110 | 123 | m_chartWidget->setType(0); |
|
111 | 124 | break; |
|
112 | 125 | } |
|
113 | 126 | } |
|
127 | } | |
|
114 | 128 | |
|
115 | 129 | void MainWidget::dataChanged(QString itemText) |
|
116 | 130 | { |
|
117 | 131 | qDebug() << "dataChanged: " << itemText; |
|
118 | 132 | } |
|
119 | 133 | |
|
120 | 134 | void MainWidget::backgroundChanged(int itemIndex) |
|
121 | 135 | { |
|
122 | 136 | qDebug() << "backgroundChanged: " << itemIndex; |
|
123 | 137 | } |
|
124 | 138 | |
|
125 | 139 | void MainWidget::autoScaleChanged(int value) |
|
126 | 140 | { |
|
127 | 141 | if (value) { |
|
128 | 142 | // TODO: enable auto scaling |
|
129 | 143 | } else { |
|
130 | 144 | // TODO: set scaling manually (and disable auto scaling) |
|
131 | 145 | } |
|
132 | 146 | |
|
133 | 147 | m_xMinSpin->setEnabled(!value); |
|
134 | 148 | m_xMaxSpin->setEnabled(!value); |
|
135 | 149 | m_yMinSpin->setEnabled(!value); |
|
136 | 150 | m_yMaxSpin->setEnabled(!value); |
|
137 | 151 | } |
|
138 | 152 | |
|
139 | 153 | void MainWidget::xMinChanged(int value) |
|
140 | 154 | { |
|
141 | 155 | qDebug() << "xMinChanged: " << value; |
|
142 | 156 | } |
|
143 | 157 | |
|
144 | 158 | void MainWidget::xMaxChanged(int value) |
|
145 | 159 | { |
|
146 | 160 | qDebug() << "xMaxChanged: " << value; |
|
147 | 161 | } |
|
148 | 162 | |
|
149 | 163 | void MainWidget::yMinChanged(int value) |
|
150 | 164 | { |
|
151 | 165 | qDebug() << "yMinChanged: " << value; |
|
152 | 166 | } |
|
153 | 167 | |
|
154 | 168 | void MainWidget::yMaxChanged(int value) |
|
155 | 169 | { |
|
156 | 170 | qDebug() << "yMaxChanged: " << value; |
|
157 | 171 | } |
@@ -1,41 +1,44 | |||
|
1 | 1 | #include "qchartwidget.h" |
|
2 | 2 | #include "qscatterseries.h" |
|
3 | 3 | #include <QGraphicsView> |
|
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 | |
|
21 | 16 | QChartWidget::~QChartWidget() |
|
22 | 17 | { |
|
23 | 18 | delete m_view; |
|
24 | 19 | delete m_scene; |
|
25 | 20 | } |
|
26 | 21 | |
|
27 | 22 | void QChartWidget::setType(/*TODO QChart::Type*/ int type) |
|
28 | 23 | { |
|
29 | 24 | if (type == 4) { |
|
30 | 25 | if (!m_scatter) { |
|
31 | 26 | m_scatter = new QScatterSeries(); |
|
32 | 27 | m_scene->addItem(m_scatter); |
|
33 | 28 | } |
|
34 | 29 | } else { |
|
35 | 30 | if (m_scatter) { |
|
36 | 31 | m_scene->removeItem(m_scatter); |
|
37 | 32 | delete m_scatter; |
|
38 | 33 | m_scatter = 0; |
|
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 | } |
@@ -1,28 +1,33 | |||
|
1 | 1 | #ifndef QCHARTWIDGET_H |
|
2 | 2 | #define QCHARTWIDGET_H |
|
3 | 3 | |
|
4 | 4 | #include <QWidget> |
|
5 | 5 | |
|
6 | 6 | 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 |
|
13 | 16 | 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 | |
|
20 | 25 | public slots: |
|
21 | 26 | |
|
22 | 27 | private: |
|
23 | 28 | QGraphicsView *m_view; |
|
24 | 29 | QGraphicsScene *m_scene; |
|
25 | 30 | QScatterSeries *m_scatter; |
|
26 | 31 | }; |
|
27 | 32 | |
|
28 | 33 | #endif // QCHARTWIDGET_H |
@@ -1,29 +1,30 | |||
|
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) |
|
6 | 8 | { |
|
7 | 9 | } |
|
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 | } |
@@ -1,20 +1,27 | |||
|
1 | 1 | #ifndef QSCATTERSERIES_H |
|
2 | 2 | #define QSCATTERSERIES_H |
|
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 | }; |
|
19 | 26 | |
|
20 | 27 | #endif // QSCATTERSERIES_H |
General Comments 0
You need to be logged in to leave comments.
Login now