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