mainwidget.cpp
222 lines
| 6.8 KiB
| text/x-c
|
CppLexer
|
r5 | #include "mainwidget.h" | ||
|
r19 | #include "dataseriedialog.h" | ||
#include <qxyseries.h> | ||||
|
r5 | #include <QPushButton> | ||
#include <QComboBox> | ||||
#include <QSpinBox> | ||||
|
r6 | #include <QCheckBox> | ||
|
r5 | #include <QGridLayout> | ||
#include <QHBoxLayout> | ||||
#include <QLabel> | ||||
#include <QSpacerItem> | ||||
#include <QMessageBox> | ||||
|
r13 | #include <cmath> | ||
|
r5 | #include <QDebug> | ||
|
r19 | QCHART_USE_NAMESPACE | ||
|
r5 | MainWidget::MainWidget(QWidget *parent) : | ||
QWidget(parent) | ||||
{ | ||||
|
r8 | m_chartWidget = new QChartWidget(this); | ||
// m_chartWidget->resize(QSize(200,200)); | ||||
// m_chartWidget->setColor(Qt::red); | ||||
|
r19 | |||
QPushButton *addSeriesButton = new QPushButton("Add series"); | ||||
connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); | ||||
|
r5 | |||
// Chart background | ||||
QComboBox *backgroundCombo = new QComboBox(this); | ||||
|
r16 | backgroundCombo->addItem("None"); | ||
backgroundCombo->addItem("TODO Grid"); | ||||
backgroundCombo->addItem("TODO Image"); | ||||
|
r5 | connect(backgroundCombo, SIGNAL(currentIndexChanged(int)), | ||
this, SLOT(backgroundChanged(int))); | ||||
// Axis | ||||
|
r7 | // TODO: multiple axes? | ||
|
r8 | m_autoScaleCheck = new QCheckBox("Automatic scaling"); | ||
connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int))); | ||||
|
r5 | // Allow setting also non-sense values (like -2147483648 and 2147483647) | ||
|
r6 | m_xMinSpin = new QSpinBox(); | ||
m_xMinSpin->setMinimum(INT_MIN); | ||||
m_xMinSpin->setMaximum(INT_MAX); | ||||
m_xMinSpin->setValue(0); | ||||
connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int))); | ||||
m_xMaxSpin = new QSpinBox(); | ||||
m_xMaxSpin->setMinimum(INT_MIN); | ||||
m_xMaxSpin->setMaximum(INT_MAX); | ||||
m_xMaxSpin->setValue(10); | ||||
connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int))); | ||||
m_yMinSpin = new QSpinBox(); | ||||
m_yMinSpin->setMinimum(INT_MIN); | ||||
m_yMinSpin->setMaximum(INT_MAX); | ||||
m_yMinSpin->setValue(0); | ||||
connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int))); | ||||
m_yMaxSpin = new QSpinBox(); | ||||
m_yMaxSpin->setMinimum(INT_MIN); | ||||
m_yMaxSpin->setMaximum(INT_MAX); | ||||
m_yMaxSpin->setValue(10); | ||||
connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int))); | ||||
|
r5 | |||
QGridLayout *grid = new QGridLayout(); | ||||
QHBoxLayout *hbox = new QHBoxLayout(); | ||||
|
r19 | //grid->addWidget(new QLabel("Add series:"), 0, 0); | ||
grid->addWidget(addSeriesButton, 0, 1); | ||||
|
r5 | grid->addWidget(new QLabel("Background:"), 2, 0); | ||
|
r16 | grid->addWidget(backgroundCombo, 2, 1); | ||
|
r8 | grid->addWidget(m_autoScaleCheck, 3, 0); | ||
|
r6 | grid->addWidget(new QLabel("x min:"), 4, 0); | ||
|
r16 | grid->addWidget(m_xMinSpin, 4, 1); | ||
|
r6 | grid->addWidget(new QLabel("x max:"), 5, 0); | ||
|
r16 | grid->addWidget(m_xMaxSpin, 5, 1); | ||
|
r6 | grid->addWidget(new QLabel("y min:"), 6, 0); | ||
|
r16 | grid->addWidget(m_yMinSpin, 6, 1); | ||
|
r6 | grid->addWidget(new QLabel("y max:"), 7, 0); | ||
|
r16 | grid->addWidget(m_yMaxSpin, 7, 1); | ||
|
r5 | // add row with empty label to make all the other rows static | ||
|
r6 | grid->addWidget(new QLabel(""), 8, 0); | ||
grid->setRowStretch(8, 1); | ||||
|
r5 | |||
hbox->addLayout(grid); | ||||
hbox->addWidget(m_chartWidget); | ||||
hbox->setStretch(1, 1); | ||||
setLayout(hbox); | ||||
|
r6 | |||
|
r8 | m_autoScaleCheck->setChecked(true); | ||
|
r13 | chartTypeChanged(4); | ||
|
r16 | testDataChanged(0); | ||
|
r5 | } | ||
|
r19 | void MainWidget::addSeries() | ||
{ | ||||
DataSerieDialog dialog(this); | ||||
|
r20 | connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString))); | ||
|
r19 | dialog.exec(); | ||
} | ||||
|
r20 | void MainWidget::addSeries(QString series, QString data) | ||
|
r19 | { | ||
|
r20 | qDebug() << "addSeries: " << series << " data: " << data; | ||
QXYSeries* series0 = new QXYSeries(); | ||||
if (data == "linear") { | ||||
// TODO | ||||
} else if (data == "SIN") { | ||||
|
r19 | series0->setColor(Qt::blue); | ||
for (int x = 0; x < 100; x++) | ||||
series0->add(x, abs(sin(3.14159265358979 / 50 * x) * 100)); | ||||
QList<QXYSeries*> dataset; | ||||
dataset << series0; | ||||
|
r20 | } else if (data == "SIN + random") { | ||
series0->setColor(Qt::blue); | ||||
for (qreal x = 0; x < 100; x += 0.1) { | ||||
series0->add(x + (rand() % 5), | ||||
abs(sin(3.14159265358979 / 50 * x) * 100) + (rand() % 5)); | ||||
} | ||||
} else { | ||||
// TODO: check if data has a valid file name | ||||
} | ||||
QList<QXYSeries*> dataset; | ||||
dataset << series0; | ||||
if (series == "Scatter") { | ||||
|
r19 | m_chartWidget->addDataSeries(QChart::DataSeriesTypeScatter, dataset); | ||
|
r20 | } else if (series == "Line") { | ||
m_chartWidget->addDataSeries(QChart::DataSeriesTypeLine, dataset); | ||||
|
r19 | } else { | ||
// TODO | ||||
} | ||||
} | ||||
|
r5 | void MainWidget::chartTypeChanged(int itemIndex) | ||
{ | ||||
|
r8 | // TODO: change chart type | ||
switch (itemIndex) { | ||||
|
r16 | case 4: | ||
|
r19 | //m_chartWidget->setType(4); | ||
|
r13 | break; | ||
default: { | ||||
|
r19 | //m_chartWidget->setType(0); | ||
|
r8 | break; | ||
} | ||||
|
r13 | } | ||
|
r5 | } | ||
|
r16 | void MainWidget::testDataChanged(int itemIndex) | ||
|
r5 | { | ||
|
r16 | qDebug() << "testDataChanged: " << itemIndex; | ||
|
r19 | // switch (itemIndex) { | ||
// case 0: { | ||||
// QList<QChartDataPoint> data; | ||||
// for (int x = 0; x < 20; x++) { | ||||
// data.append(QChartDataPoint() << x << x / 2); | ||||
// } | ||||
// m_chartWidget->setData(data); | ||||
// break; | ||||
// } | ||||
// case 1: { | ||||
// QList<QChartDataPoint> data; | ||||
// for (int x = 0; x < 100; x++) { | ||||
// data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100)); | ||||
// } | ||||
// m_chartWidget->setData(data); | ||||
// break; | ||||
// } | ||||
// case 2: { | ||||
// QList<QChartDataPoint> data; | ||||
// for (int x = 0; x < 1000; x++) { | ||||
// data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); | ||||
// data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); | ||||
// data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); | ||||
// } | ||||
// m_chartWidget->setData(data); | ||||
// break; | ||||
// } | ||||
// default: | ||||
// break; | ||||
// } | ||||
|
r5 | } | ||
void MainWidget::backgroundChanged(int itemIndex) | ||||
{ | ||||
qDebug() << "backgroundChanged: " << itemIndex; | ||||
} | ||||
|
r6 | void MainWidget::autoScaleChanged(int value) | ||
{ | ||||
|
r8 | if (value) { | ||
// TODO: enable auto scaling | ||||
} else { | ||||
// TODO: set scaling manually (and disable auto scaling) | ||||
|
r6 | } | ||
|
r8 | m_xMinSpin->setEnabled(!value); | ||
m_xMaxSpin->setEnabled(!value); | ||||
m_yMinSpin->setEnabled(!value); | ||||
m_yMaxSpin->setEnabled(!value); | ||||
|
r6 | } | ||
|
r5 | void MainWidget::xMinChanged(int value) | ||
{ | ||||
qDebug() << "xMinChanged: " << value; | ||||
} | ||||
void MainWidget::xMaxChanged(int value) | ||||
{ | ||||
qDebug() << "xMaxChanged: " << value; | ||||
} | ||||
void MainWidget::yMinChanged(int value) | ||||
{ | ||||
qDebug() << "yMinChanged: " << value; | ||||
} | ||||
void MainWidget::yMaxChanged(int value) | ||||
{ | ||||
qDebug() << "yMaxChanged: " << value; | ||||
} | ||||