##// END OF EJS Templates
Drafting scatter type plotting...
Tero Ahola -
r8:ca68423caa77
parent child
Show More
@@ -0,0 +1,41
1 #include "qchartwidget.h"
2 #include "qscatterseries.h"
3 #include <QGraphicsView>
4 #include <QGraphicsScene>
5
6 QChartWidget::QChartWidget(QWidget *parent) :
7 QWidget(parent)
8 {
9 // scatter
10 m_scene = new QGraphicsScene();
11 m_view = new QGraphicsView(m_scene, this);
12 m_view->resize(300, 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();
19 }
20
21 QChartWidget::~QChartWidget()
22 {
23 delete m_view;
24 delete m_scene;
25 }
26
27 void QChartWidget::setType(/*TODO QChart::Type*/ int type)
28 {
29 if (type == 4) {
30 if (!m_scatter) {
31 m_scatter = new QScatterSeries();
32 m_scene->addItem(m_scatter);
33 }
34 } else {
35 if (m_scatter) {
36 m_scene->removeItem(m_scatter);
37 delete m_scatter;
38 m_scatter = 0;
39 }
40 }
41 }
@@ -0,0 +1,28
1 #ifndef QCHARTWIDGET_H
2 #define QCHARTWIDGET_H
3
4 #include <QWidget>
5
6 class QGraphicsView;
7 class QGraphicsScene;
8 class QScatterSeries;
9
10 class QChartWidget : public QWidget
11 {
12 Q_OBJECT
13 public:
14 explicit QChartWidget(QWidget *parent = 0);
15 ~QChartWidget();
16 void setType(/*TODO QChart::Type*/ int type);
17
18 signals:
19
20 public slots:
21
22 private:
23 QGraphicsView *m_view;
24 QGraphicsScene *m_scene;
25 QScatterSeries *m_scatter;
26 };
27
28 #endif // QCHARTWIDGET_H
@@ -0,0 +1,29
1 #include "qscatterseries.h"
2 #include <QPainter>
3
4 QScatterSeries::QScatterSeries(QGraphicsItem *parent) :
5 QGraphicsItem(parent)
6 {
7 }
8
9 QRectF QScatterSeries::boundingRect() const
10 {
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);
15 }
16
17 void QScatterSeries::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
18 {
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);
29 }
@@ -0,0 +1,20
1 #ifndef QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
3
4 #include <QGraphicsItem>
5
6 class QScatterSeries : public QGraphicsItem
7 {
8 public:
9 explicit QScatterSeries(QGraphicsItem *parent = 0);
10
11 signals:
12
13 public slots:
14
15 private:
16 QRectF boundingRect() const;
17 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
18 };
19
20 #endif // QSCATTERSERIES_H
@@ -1,22 +1,28
1 TARGET = chartwidgettest
1 TARGET = chartwidgettest
2 TEMPLATE = app
2 TEMPLATE = app
3
3
4 QT += core gui
4 QT += core gui
5 contains(QT_MAJOR_VERSION, 5) {
5 contains(QT_MAJOR_VERSION, 5) {
6 QT += widgets
6 QT += widgets
7 }
7 }
8
8
9 CONFIG += charts
9 CONFIG += charts
10 CHARTS += widget
10 CHARTS += widget
11
11
12 OBJECTS_DIR = tmp
12 OBJECTS_DIR = tmp
13 MOC_DIR = tmp
13 MOC_DIR = tmp
14
14
15 SOURCES += main.cpp \
15 SOURCES += main.cpp \
16 mainwidget.cpp
16 mainwidget.cpp \
17 qscatterseries.cpp \
18 qchartwidget.cpp
17
19
18 HEADERS += \
20 HEADERS += \
19 mainwidget.h
21 mainwidget.h \
22 qscatterseries.h \
23 qchartwidget.h
24
25
20
26
21
27
22
28
@@ -1,155 +1,157
1 #include "mainwidget.h"
1 #include "mainwidget.h"
2 #include <chartwidget.h>
2 #include "qchartwidget.h"
3 //#include <chartwidget.h>
3 #include <QPushButton>
4 #include <QPushButton>
4 #include <QComboBox>
5 #include <QComboBox>
5 #include <QSpinBox>
6 #include <QSpinBox>
6 #include <QCheckBox>
7 #include <QCheckBox>
7 #include <QGridLayout>
8 #include <QGridLayout>
8 #include <QHBoxLayout>
9 #include <QHBoxLayout>
9 #include <QLabel>
10 #include <QLabel>
10 #include <QSpacerItem>
11 #include <QSpacerItem>
11 #include <QMessageBox>
12 #include <QMessageBox>
12 #include <QDebug>
13 #include <QDebug>
13
14
14 MainWidget::MainWidget(QWidget *parent) :
15 MainWidget::MainWidget(QWidget *parent) :
15 QWidget(parent)
16 QWidget(parent)
16 {
17 {
17 m_chartWidget = new ChartWidget(this);
18 m_chartWidget = new QChartWidget(this);
18 m_chartWidget->resize(QSize(200,200));
19 // m_chartWidget->resize(QSize(200,200));
19 m_chartWidget->setColor(Qt::red);
20 // m_chartWidget->setColor(Qt::red);
20
21 // Chart type
21 // Chart type
22 // TODO: How about multiple types?
22 // TODO: How about multiple types?
23 // Should the type be a property of a graph instead of the chart?
23 // Should the type be a property of a graph instead of the chart?
24 QComboBox *chartTypeCombo = new QComboBox(this);
24 QComboBox *chartTypeCombo = new QComboBox(this);
25 chartTypeCombo->addItem("Line");
25 chartTypeCombo->addItem("Line");
26 chartTypeCombo->addItem("Area");
26 chartTypeCombo->addItem("Area");
27 chartTypeCombo->addItem("Bar");
27 chartTypeCombo->addItem("Bar");
28 chartTypeCombo->addItem("Pie");
28 chartTypeCombo->addItem("Pie");
29 chartTypeCombo->addItem("Scatter");
29 chartTypeCombo->addItem("Scatter");
30 chartTypeCombo->addItem("Spline");
30 chartTypeCombo->addItem("Spline");
31 connect(chartTypeCombo, SIGNAL(currentIndexChanged(int)),
31 connect(chartTypeCombo, SIGNAL(currentIndexChanged(int)),
32 this, SLOT(chartTypeChanged(int)));
32 this, SLOT(chartTypeChanged(int)));
33
33
34 // Test data selector
34 // Test data selector
35 QComboBox *dataCombo = new QComboBox(this);
35 QPushButton *fileButton = new QPushButton("From file");
36 dataCombo->addItem("todo: add test data");
36 QPushButton *urlButton = new QPushButton("From URL");
37 connect(dataCombo, SIGNAL(currentIndexChanged(QString)),
38 this, SLOT(dataChanged(QString)));
39
37
40 // Chart background
38 // Chart background
41 QComboBox *backgroundCombo = new QComboBox(this);
39 QComboBox *backgroundCombo = new QComboBox(this);
42 backgroundCombo->addItem("todo: add background types");
40 backgroundCombo->addItem("todo: add background types");
43 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
41 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
44 this, SLOT(backgroundChanged(int)));
42 this, SLOT(backgroundChanged(int)));
45
43
46 // Axis
44 // Axis
47 // TODO: multiple axes?
45 // TODO: multiple axes?
48 QCheckBox *autoScaleCheck = new QCheckBox("Automatic scaling");
46 m_autoScaleCheck = new QCheckBox("Automatic scaling");
49 connect(autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
47 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
50 // Allow setting also non-sense values (like -2147483648 and 2147483647)
48 // Allow setting also non-sense values (like -2147483648 and 2147483647)
51 m_xMinSpin = new QSpinBox();
49 m_xMinSpin = new QSpinBox();
52 m_xMinSpin->setMinimum(INT_MIN);
50 m_xMinSpin->setMinimum(INT_MIN);
53 m_xMinSpin->setMaximum(INT_MAX);
51 m_xMinSpin->setMaximum(INT_MAX);
54 m_xMinSpin->setValue(0);
52 m_xMinSpin->setValue(0);
55 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
53 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
56 m_xMaxSpin = new QSpinBox();
54 m_xMaxSpin = new QSpinBox();
57 m_xMaxSpin->setMinimum(INT_MIN);
55 m_xMaxSpin->setMinimum(INT_MIN);
58 m_xMaxSpin->setMaximum(INT_MAX);
56 m_xMaxSpin->setMaximum(INT_MAX);
59 m_xMaxSpin->setValue(10);
57 m_xMaxSpin->setValue(10);
60 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
58 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
61 m_yMinSpin = new QSpinBox();
59 m_yMinSpin = new QSpinBox();
62 m_yMinSpin->setMinimum(INT_MIN);
60 m_yMinSpin->setMinimum(INT_MIN);
63 m_yMinSpin->setMaximum(INT_MAX);
61 m_yMinSpin->setMaximum(INT_MAX);
64 m_yMinSpin->setValue(0);
62 m_yMinSpin->setValue(0);
65 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
63 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
66 m_yMaxSpin = new QSpinBox();
64 m_yMaxSpin = new QSpinBox();
67 m_yMaxSpin->setMinimum(INT_MIN);
65 m_yMaxSpin->setMinimum(INT_MIN);
68 m_yMaxSpin->setMaximum(INT_MAX);
66 m_yMaxSpin->setMaximum(INT_MAX);
69 m_yMaxSpin->setValue(10);
67 m_yMaxSpin->setValue(10);
70 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
68 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
71
69
72 QGridLayout *grid = new QGridLayout();
70 QGridLayout *grid = new QGridLayout();
73 QHBoxLayout *hbox = new QHBoxLayout();
71 QHBoxLayout *hbox = new QHBoxLayout();
74 grid->addWidget(new QLabel("Chart type:"), 0, 0);
72 grid->addWidget(new QLabel("Chart type:"), 0, 0);
75 grid->addWidget(chartTypeCombo, 0, 1);
73 grid->addWidget(chartTypeCombo, 0, 1, 1, 2);
76 grid->addWidget(new QLabel("Data:"), 1, 0);
74 grid->addWidget(new QLabel("Data:"), 1, 0);
77 grid->addWidget(dataCombo, 1, 1);
75 grid->addWidget(fileButton, 1, 1);
76 grid->addWidget(urlButton, 1, 2);
78 grid->addWidget(new QLabel("Background:"), 2, 0);
77 grid->addWidget(new QLabel("Background:"), 2, 0);
79 grid->addWidget(backgroundCombo, 2, 1);
78 grid->addWidget(backgroundCombo, 2, 1, 1, 2);
80 grid->addWidget(autoScaleCheck, 3, 0);
79 grid->addWidget(m_autoScaleCheck, 3, 0);
81 grid->addWidget(new QLabel("x min:"), 4, 0);
80 grid->addWidget(new QLabel("x min:"), 4, 0);
82 grid->addWidget(m_xMinSpin, 4, 1);
81 grid->addWidget(m_xMinSpin, 4, 1, 1, 2);
83 grid->addWidget(new QLabel("x max:"), 5, 0);
82 grid->addWidget(new QLabel("x max:"), 5, 0);
84 grid->addWidget(m_xMaxSpin, 5, 1);
83 grid->addWidget(m_xMaxSpin, 5, 1, 1, 2);
85 grid->addWidget(new QLabel("y min:"), 6, 0);
84 grid->addWidget(new QLabel("y min:"), 6, 0);
86 grid->addWidget(m_yMinSpin, 6, 1);
85 grid->addWidget(m_yMinSpin, 6, 1, 1, 2);
87 grid->addWidget(new QLabel("y max:"), 7, 0);
86 grid->addWidget(new QLabel("y max:"), 7, 0);
88 grid->addWidget(m_yMaxSpin, 7, 1);
87 grid->addWidget(m_yMaxSpin, 7, 1, 1, 2);
89 // add row with empty label to make all the other rows static
88 // add row with empty label to make all the other rows static
90 grid->addWidget(new QLabel(""), 8, 0);
89 grid->addWidget(new QLabel(""), 8, 0);
91 grid->setRowStretch(8, 1);
90 grid->setRowStretch(8, 1);
92
91
93 hbox->addLayout(grid);
92 hbox->addLayout(grid);
94 hbox->addWidget(m_chartWidget);
93 hbox->addWidget(m_chartWidget);
95 hbox->setStretch(1, 1);
94 hbox->setStretch(1, 1);
96
95
97 setLayout(hbox);
96 setLayout(hbox);
98
97
99 // Setting auto scale affects min/max value spin boxes, so it needs to be done after they have
98 m_autoScaleCheck->setChecked(true);
100 // been constructed
101 autoScaleCheck->setChecked(true);
102 }
99 }
103
100
104 void MainWidget::chartTypeChanged(int itemIndex)
101 void MainWidget::chartTypeChanged(int itemIndex)
105 {
102 {
106 qDebug() << "chartTypeChanged: " << itemIndex;
103 // TODO: change chart type
107 QMessageBox msg;
104 switch (itemIndex) {
108 msg.setText("TODO: API for setting chart type");
105 case 4: {
109 msg.exec();
106 m_chartWidget->setType(4);
107 break;
108 }
109 default:
110 m_chartWidget->setType(0);
111 break;
112 }
110 }
113 }
111
114
112 void MainWidget::dataChanged(QString itemText)
115 void MainWidget::dataChanged(QString itemText)
113 {
116 {
114 qDebug() << "dataChanged: " << itemText;
117 qDebug() << "dataChanged: " << itemText;
115 }
118 }
116
119
117 void MainWidget::backgroundChanged(int itemIndex)
120 void MainWidget::backgroundChanged(int itemIndex)
118 {
121 {
119 qDebug() << "backgroundChanged: " << itemIndex;
122 qDebug() << "backgroundChanged: " << itemIndex;
120 }
123 }
121
124
122 void MainWidget::autoScaleChanged(int value)
125 void MainWidget::autoScaleChanged(int value)
123 {
126 {
124 if (this->isVisible()) {
127 if (value) {
125 QMessageBox msg;
128 // TODO: enable auto scaling
126 msg.setText("TODO: APIs for using either auto scaling or explicit values");
129 } else {
127 msg.exec();
130 // TODO: set scaling manually (and disable auto scaling)
128 }
131 }
129
132
130 // TODO: get initial spin box axis values from charts widget
133 m_xMinSpin->setEnabled(!value);
131 m_xMinSpin->setEnabled(value == Qt::Unchecked);
134 m_xMaxSpin->setEnabled(!value);
132 m_xMaxSpin->setEnabled(value == Qt::Unchecked);
135 m_yMinSpin->setEnabled(!value);
133 m_yMinSpin->setEnabled(value == Qt::Unchecked);
136 m_yMaxSpin->setEnabled(!value);
134 m_yMaxSpin->setEnabled(value == Qt::Unchecked);
135 }
137 }
136
138
137 void MainWidget::xMinChanged(int value)
139 void MainWidget::xMinChanged(int value)
138 {
140 {
139 qDebug() << "xMinChanged: " << value;
141 qDebug() << "xMinChanged: " << value;
140 }
142 }
141
143
142 void MainWidget::xMaxChanged(int value)
144 void MainWidget::xMaxChanged(int value)
143 {
145 {
144 qDebug() << "xMaxChanged: " << value;
146 qDebug() << "xMaxChanged: " << value;
145 }
147 }
146
148
147 void MainWidget::yMinChanged(int value)
149 void MainWidget::yMinChanged(int value)
148 {
150 {
149 qDebug() << "yMinChanged: " << value;
151 qDebug() << "yMinChanged: " << value;
150 }
152 }
151
153
152 void MainWidget::yMaxChanged(int value)
154 void MainWidget::yMaxChanged(int value)
153 {
155 {
154 qDebug() << "yMaxChanged: " << value;
156 qDebug() << "yMaxChanged: " << value;
155 }
157 }
@@ -1,35 +1,37
1 #ifndef MAINWIDGET_H
1 #ifndef MAINWIDGET_H
2 #define MAINWIDGET_H
2 #define MAINWIDGET_H
3
3
4 #include <QWidget>
4 #include <QWidget>
5
5
6 class ChartWidget;
6 class QChartWidget;
7 class QSpinBox;
7 class QSpinBox;
8 class QCheckBox;
8
9
9 class MainWidget : public QWidget
10 class MainWidget : public QWidget
10 {
11 {
11 Q_OBJECT
12 Q_OBJECT
12 public:
13 public:
13 explicit MainWidget(QWidget *parent = 0);
14 explicit MainWidget(QWidget *parent = 0);
14
15
15 signals:
16 signals:
16
17
17 private slots:
18 private slots:
18 void chartTypeChanged(int itemIndex);
19 void chartTypeChanged(int itemIndex);
19 void dataChanged(QString itemText);
20 void dataChanged(QString itemText);
20 void backgroundChanged(int itemIndex);
21 void backgroundChanged(int itemIndex);
21 void autoScaleChanged(int value);
22 void autoScaleChanged(int value);
22 void xMinChanged(int value);
23 void xMinChanged(int value);
23 void xMaxChanged(int value);
24 void xMaxChanged(int value);
24 void yMinChanged(int value);
25 void yMinChanged(int value);
25 void yMaxChanged(int value);
26 void yMaxChanged(int value);
26
27
27 private:
28 private:
28 ChartWidget *m_chartWidget;
29 QChartWidget *m_chartWidget;
30 QCheckBox *m_autoScaleCheck;
29 QSpinBox *m_xMinSpin;
31 QSpinBox *m_xMinSpin;
30 QSpinBox *m_xMaxSpin;
32 QSpinBox *m_xMaxSpin;
31 QSpinBox *m_yMinSpin;
33 QSpinBox *m_yMinSpin;
32 QSpinBox *m_yMaxSpin;
34 QSpinBox *m_yMaxSpin;
33 };
35 };
34
36
35 #endif // MAINWIDGET_H
37 #endif // MAINWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now