mainwidget.cpp
338 lines
| 11.1 KiB
| text/x-c
|
CppLexer
Tero Ahola
|
r5 | #include "mainwidget.h" | ||
Tero Ahola
|
r19 | #include "dataseriedialog.h" | ||
Tero Ahola
|
r60 | #include "qchartseries.h" | ||
#include "qpieseries.h" | ||||
Tero Ahola
|
r27 | #include <qxychartseries.h> | ||
sauimone
|
r62 | #include <barchartseries.h> | ||
Tero Ahola
|
r5 | #include <QPushButton> | ||
#include <QComboBox> | ||||
#include <QSpinBox> | ||||
Tero Ahola
|
r6 | #include <QCheckBox> | ||
Tero Ahola
|
r5 | #include <QGridLayout> | ||
#include <QHBoxLayout> | ||||
#include <QLabel> | ||||
#include <QSpacerItem> | ||||
#include <QMessageBox> | ||||
Tero Ahola
|
r13 | #include <cmath> | ||
Tero Ahola
|
r5 | #include <QDebug> | ||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_USE_NAMESPACE | ||
Tero Ahola
|
r19 | |||
Tero Ahola
|
r5 | MainWidget::MainWidget(QWidget *parent) : | ||
QWidget(parent) | ||||
{ | ||||
Tero Ahola
|
r77 | m_chartWidget = new QChartWidget(this); | ||
Tero Ahola
|
r19 | QPushButton *addSeriesButton = new QPushButton("Add series"); | ||
connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); | ||||
Tero Ahola
|
r5 | |||
// Chart background | ||||
QComboBox *backgroundCombo = new QComboBox(this); | ||||
Tero Ahola
|
r77 | backgroundCombo->addItem("Color"); | ||
backgroundCombo->addItem("Gradient"); | ||||
backgroundCombo->addItem("Image"); | ||||
Tero Ahola
|
r5 | connect(backgroundCombo, SIGNAL(currentIndexChanged(int)), | ||
this, SLOT(backgroundChanged(int))); | ||||
// Axis | ||||
Tero Ahola
|
r7 | // TODO: multiple axes? | ||
Tero Ahola
|
r8 | m_autoScaleCheck = new QCheckBox("Automatic scaling"); | ||
connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int))); | ||||
Tero Ahola
|
r5 | // Allow setting also non-sense values (like -2147483648 and 2147483647) | ||
Tero Ahola
|
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))); | ||||
Tero Ahola
|
r5 | |||
Tero Ahola
|
r64 | QComboBox *chartTheme = new QComboBox(); | ||
chartTheme->addItem("Vanilla"); | ||||
chartTheme->addItem("Icy"); | ||||
chartTheme->addItem("Grayscale"); | ||||
Tero Ahola
|
r77 | chartTheme->addItem("Unnamed1"); | ||
Tero Ahola
|
r64 | connect(chartTheme, SIGNAL(currentIndexChanged(int)), | ||
this, SLOT(changeChartTheme(int))); | ||||
Tero Ahola
|
r77 | QCheckBox *zoomCheckBox = new QCheckBox("Zoom enabled"); | ||
connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool))); | ||||
zoomCheckBox->setChecked(true); | ||||
Tero Ahola
|
r5 | QGridLayout *grid = new QGridLayout(); | ||
Tero Ahola
|
r60 | QGridLayout *mainLayout = new QGridLayout(); | ||
Tero Ahola
|
r19 | grid->addWidget(addSeriesButton, 0, 1); | ||
Tero Ahola
|
r5 | grid->addWidget(new QLabel("Background:"), 2, 0); | ||
Tero Ahola
|
r16 | grid->addWidget(backgroundCombo, 2, 1); | ||
Tero Ahola
|
r8 | grid->addWidget(m_autoScaleCheck, 3, 0); | ||
Tero Ahola
|
r6 | grid->addWidget(new QLabel("x min:"), 4, 0); | ||
Tero Ahola
|
r16 | grid->addWidget(m_xMinSpin, 4, 1); | ||
Tero Ahola
|
r6 | grid->addWidget(new QLabel("x max:"), 5, 0); | ||
Tero Ahola
|
r16 | grid->addWidget(m_xMaxSpin, 5, 1); | ||
Tero Ahola
|
r6 | grid->addWidget(new QLabel("y min:"), 6, 0); | ||
Tero Ahola
|
r16 | grid->addWidget(m_yMinSpin, 6, 1); | ||
Tero Ahola
|
r6 | grid->addWidget(new QLabel("y max:"), 7, 0); | ||
Tero Ahola
|
r16 | grid->addWidget(m_yMaxSpin, 7, 1); | ||
Tero Ahola
|
r64 | grid->addWidget(new QLabel("Chart theme:"), 8, 0); | ||
grid->addWidget(chartTheme, 8, 1); | ||||
Tero Ahola
|
r77 | grid->addWidget(zoomCheckBox, 9, 0); | ||
Tero Ahola
|
r5 | // add row with empty label to make all the other rows static | ||
Tero Ahola
|
r77 | grid->addWidget(new QLabel(""), 10, 0); | ||
grid->setRowStretch(10, 1); | ||||
Tero Ahola
|
r5 | |||
Tero Ahola
|
r60 | mainLayout->addLayout(grid, 0, 0); | ||
// Scatter specific settings | ||||
m_scatterLayout = new QGridLayout(); | ||||
m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0); | ||||
m_scatterLayout->setEnabled(false); | ||||
// Pie specific settings | ||||
Tero Ahola
|
r77 | // Pie size factory | ||
Tero Ahola
|
r60 | QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox(); | ||
pieSizeSpin->setMinimum(LONG_MIN); | ||||
pieSizeSpin->setMaximum(LONG_MAX); | ||||
pieSizeSpin->setValue(1.0); | ||||
pieSizeSpin->setSingleStep(0.1); | ||||
connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double))); | ||||
Tero Ahola
|
r77 | // Pie position | ||
QComboBox *piePosCombo = new QComboBox(this); | ||||
piePosCombo->addItem("Maximized"); | ||||
piePosCombo->addItem("Top left"); | ||||
piePosCombo->addItem("Top right"); | ||||
piePosCombo->addItem("Bottom left"); | ||||
piePosCombo->addItem("Bottom right"); | ||||
connect(piePosCombo, SIGNAL(currentIndexChanged(int)), | ||||
this, SLOT(setPiePosition(int))); | ||||
m_pieLayout = new QGridLayout(); | ||||
Tero Ahola
|
r60 | m_pieLayout->setEnabled(false); | ||
Tero Ahola
|
r77 | m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0); | ||
Tero Ahola
|
r60 | m_pieLayout->addWidget(pieSizeSpin, 0, 1); | ||
Tero Ahola
|
r77 | m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0); | ||
m_pieLayout->addWidget(piePosCombo, 1, 1); | ||||
Tero Ahola
|
r60 | |||
mainLayout->addLayout(m_scatterLayout, 1, 0); | ||||
mainLayout->addLayout(m_pieLayout, 2, 0); | ||||
Tero Ahola
|
r30 | |||
//m_chartWidget->setColor(Qt::red); | ||||
Tero Ahola
|
r60 | mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1); | ||
Tero Ahola
|
r30 | // hbox->setStretch(1, 1); | ||
Tero Ahola
|
r5 | |||
Tero Ahola
|
r60 | setLayout(mainLayout); | ||
Tero Ahola
|
r6 | |||
Tero Ahola
|
r8 | m_autoScaleCheck->setChecked(true); | ||
Tero Ahola
|
r16 | testDataChanged(0); | ||
Tero Ahola
|
r5 | } | ||
Tero Ahola
|
r19 | void MainWidget::addSeries() | ||
{ | ||||
Tero Ahola
|
r60 | DataSerieDialog dialog(m_defaultSeriesName, this); | ||
Tero Ahola
|
r20 | connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString))); | ||
Tero Ahola
|
r19 | dialog.exec(); | ||
} | ||||
Tero Ahola
|
r20 | void MainWidget::addSeries(QString series, QString data) | ||
Tero Ahola
|
r19 | { | ||
Tero Ahola
|
r20 | qDebug() << "addSeries: " << series << " data: " << data; | ||
Tero Ahola
|
r60 | m_defaultSeriesName = series; | ||
Tero Ahola
|
r48 | |||
Tero Ahola
|
r42 | // TODO: a dedicated data class for storing x and y values | ||
QList<qreal> x; | ||||
QList<qreal> y; | ||||
Tero Ahola
|
r20 | |||
if (data == "linear") { | ||||
Tero Ahola
|
r48 | for (int i = 0; i < 20; i++) { | ||
Tero Ahola
|
r42 | x.append(i); | ||
Tero Ahola
|
r48 | y.append(i); | ||
Tero Ahola
|
r42 | } | ||
Tero Ahola
|
r27 | } else if (data == "linear, 1M") { | ||
Tero Ahola
|
r42 | for (int i = 0; i < 10000; i++) { | ||
x.append(i); | ||||
y.append(20); | ||||
} | ||||
Tero Ahola
|
r20 | } else if (data == "SIN") { | ||
Tero Ahola
|
r42 | for (int i = 0; i < 100; i++) { | ||
x.append(i); | ||||
y.append(abs(sin(3.14159265358979 / 50 * i) * 100)); | ||||
} | ||||
Tero Ahola
|
r20 | } else if (data == "SIN + random") { | ||
Tero Ahola
|
r42 | for (qreal i = 0; i < 100; i += 0.1) { | ||
x.append(i + (rand() % 5)); | ||||
y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5)); | ||||
Tero Ahola
|
r20 | } | ||
} else { | ||||
// TODO: check if data has a valid file name | ||||
Tero Ahola
|
r61 | Q_ASSERT(false); | ||
Tero Ahola
|
r20 | } | ||
Tero Ahola
|
r42 | // TODO: color of the series | ||
Tero Ahola
|
r61 | QChartSeries *newSeries = 0; | ||
Tero Ahola
|
r42 | if (series == "Scatter") { | ||
Tero Ahola
|
r61 | newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter); | ||
Q_ASSERT(newSeries->setData(x, y)); | ||||
Tero Ahola
|
r51 | } else if (series == "Pie") { | ||
Tero Ahola
|
r61 | newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie); | ||
Q_ASSERT(newSeries->setData(y)); | ||||
Tero Ahola
|
r42 | } else if (series == "Line") { | ||
Tero Ahola
|
r65 | // TODO: adding data to an existing line series does not give any visuals for some reason | ||
// newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeLine); | ||||
// QXYChartSeries *lineSeries = static_cast<QXYChartSeries *>(newSeries); | ||||
// lineSeries->setColor(Qt::blue); | ||||
// for (int i(0); i < x.count() && i < y.count(); i++) { | ||||
// lineSeries->add(x.at(i), y.at(i)); | ||||
// } | ||||
//Q_ASSERT(newSeries->setData(x, y)); | ||||
QXYChartSeries* series0 = QXYChartSeries::create(); | ||||
for (int i(0); i < x.count() && i < y.count(); i++) | ||||
series0->add(x.at(i), y.at(i)); | ||||
m_chartWidget->addSeries(series0); | ||||
newSeries = series0; | ||||
Tero Ahola
|
r42 | } else { | ||
// TODO | ||||
} | ||||
Tero Ahola
|
r60 | |||
sauimone
|
r62 | // BarChart | ||
if (series == "Bar") { | ||||
sauimone
|
r63 | // This is the another way of creating series. Should we create test cases for both ways, if we support them? | ||
sauimone
|
r62 | qDebug() << "Bar chart series"; | ||
sauimone
|
r63 | newSeries = QChartSeries::create(QChartSeries::SeriesTypeBar, this); | ||
sauimone
|
r62 | QList<int> barData; | ||
barData << 1; | ||||
barData << 12; | ||||
barData << 5; | ||||
barData << 8; | ||||
barData << 17; | ||||
barData << 9; | ||||
sauimone
|
r63 | newSeries->setData(barData); | ||
m_chartWidget->addSeries(newSeries); | ||||
sauimone
|
r62 | } | ||
Tero Ahola
|
r60 | setCurrentSeries(newSeries); | ||
Tero Ahola
|
r19 | } | ||
Tero Ahola
|
r60 | void MainWidget::setCurrentSeries(QChartSeries *series) | ||
Tero Ahola
|
r5 | { | ||
Tero Ahola
|
r60 | m_currentSeries = series; | ||
switch (m_currentSeries->type()) { | ||||
case QChartSeries::SeriesTypeLine: | ||||
Tero Ahola
|
r13 | break; | ||
Tero Ahola
|
r60 | case QChartSeries::SeriesTypeScatter: | ||
break; | ||||
case QChartSeries::SeriesTypePie: | ||||
break; | ||||
sauimone
|
r63 | case QChartSeries::SeriesTypeBar: | ||
qDebug() << "setCurrentSeries (bar)"; | ||||
break; | ||||
Tero Ahola
|
r60 | default: | ||
Q_ASSERT(false); | ||||
Tero Ahola
|
r8 | break; | ||
Tero Ahola
|
r13 | } | ||
Tero Ahola
|
r5 | } | ||
Tero Ahola
|
r16 | void MainWidget::testDataChanged(int itemIndex) | ||
Tero Ahola
|
r5 | { | ||
Tero Ahola
|
r16 | qDebug() << "testDataChanged: " << itemIndex; | ||
Tero Ahola
|
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; | ||||
// } | ||||
Tero Ahola
|
r5 | } | ||
void MainWidget::backgroundChanged(int itemIndex) | ||||
{ | ||||
qDebug() << "backgroundChanged: " << itemIndex; | ||||
} | ||||
Tero Ahola
|
r6 | void MainWidget::autoScaleChanged(int value) | ||
{ | ||||
Tero Ahola
|
r8 | if (value) { | ||
// TODO: enable auto scaling | ||||
} else { | ||||
// TODO: set scaling manually (and disable auto scaling) | ||||
Tero Ahola
|
r6 | } | ||
Tero Ahola
|
r8 | m_xMinSpin->setEnabled(!value); | ||
m_xMaxSpin->setEnabled(!value); | ||||
m_yMinSpin->setEnabled(!value); | ||||
m_yMaxSpin->setEnabled(!value); | ||||
Tero Ahola
|
r6 | } | ||
Tero Ahola
|
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; | ||||
} | ||||
Tero Ahola
|
r60 | |||
Tero Ahola
|
r64 | void MainWidget::changeChartTheme(int themeIndex) | ||
{ | ||||
qDebug() << "changeChartTheme: " << themeIndex; | ||||
Tero Ahola
|
r75 | m_chartWidget->setTheme((QChart::ChartThemeId) themeIndex); | ||
Tero Ahola
|
r64 | } | ||
Tero Ahola
|
r60 | void MainWidget::setPieSizeFactor(double size) | ||
{ | ||||
QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); | ||||
Tero Ahola
|
r75 | if (pie) | ||
pie->setSizeFactor(qreal(size)); | ||||
Tero Ahola
|
r60 | } | ||
Tero Ahola
|
r77 | |||
void MainWidget::setPiePosition(int position) | ||||
{ | ||||
QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); | ||||
if (pie) | ||||
pie->setPosition((QPieSeries::PiePosition) position); | ||||
} | ||||