mainwidget.cpp
371 lines
| 13.1 KiB
| text/x-c
|
CppLexer
|
r5 | #include "mainwidget.h" | ||
|
r19 | #include "dataseriedialog.h" | ||
|
r60 | #include "qpieseries.h" | ||
|
r179 | #include "qscatterseries.h" | ||
|
r349 | #include <qlineseries.h> | ||
|
r171 | #include <qbarset.h> | ||
#include <qbarcategory.h> | ||||
|
r338 | #include <qbarseries.h> | ||
#include <qstackedbarseries.h> | ||||
#include <qpercentbarseries.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> | ||
|
r78 | #include <QStandardItemModel> | ||
|
r5 | |||
|
r30 | QTCOMMERCIALCHART_USE_NAMESPACE | ||
|
r19 | |||
|
r5 | MainWidget::MainWidget(QWidget *parent) : | ||
QWidget(parent) | ||||
{ | ||||
|
r136 | m_chartWidget = new QChartView(this); | ||
m_chartWidget->setRubberBandPolicy(QChartView::HorizonalRubberBand); | ||||
|
r77 | |||
|
r111 | // Grid layout for the controls for configuring the chart widget | ||
|
r110 | QGridLayout *grid = new QGridLayout(); | ||
|
r19 | QPushButton *addSeriesButton = new QPushButton("Add series"); | ||
connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); | ||||
|
r110 | grid->addWidget(addSeriesButton, 0, 1); | ||
initBackroundCombo(grid); | ||||
initScaleControls(grid); | ||||
initThemeCombo(grid); | ||||
|
r111 | QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom"); | ||
|
r110 | connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool))); | ||
zoomCheckBox->setChecked(true); | ||||
grid->addWidget(zoomCheckBox, grid->rowCount(), 0); | ||||
// add row with empty label to make all the other rows static | ||||
grid->addWidget(new QLabel(""), grid->rowCount(), 0); | ||||
grid->setRowStretch(grid->rowCount() - 1, 1); | ||||
|
r111 | |||
// Another grid layout as a main layout | ||||
QGridLayout *mainLayout = new QGridLayout(); | ||||
|
r110 | mainLayout->addLayout(grid, 0, 0); | ||
// Init series type specific controls | ||||
initPieControls(); | ||||
mainLayout->addLayout(m_pieLayout, 2, 0); | ||||
// Scatter series specific settings | ||||
// m_scatterLayout = new QGridLayout(); | ||||
// m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0); | ||||
// m_scatterLayout->setEnabled(false); | ||||
// mainLayout->addLayout(m_scatterLayout, 1, 0); | ||||
// Add layouts and the chart widget to the main layout | ||||
mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1); | ||||
setLayout(mainLayout); | ||||
} | ||||
|
r5 | |||
|
r110 | // Combo box for selecting the chart's background | ||
void MainWidget::initBackroundCombo(QGridLayout *grid) | ||||
{ | ||||
|
r5 | QComboBox *backgroundCombo = new QComboBox(this); | ||
|
r77 | backgroundCombo->addItem("Color"); | ||
backgroundCombo->addItem("Gradient"); | ||||
backgroundCombo->addItem("Image"); | ||||
|
r5 | connect(backgroundCombo, SIGNAL(currentIndexChanged(int)), | ||
this, SLOT(backgroundChanged(int))); | ||||
|
r110 | grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0); | ||
grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1); | ||||
} | ||||
// Scale related controls (auto-scale vs. manual min-max values) | ||||
void MainWidget::initScaleControls(QGridLayout *grid) | ||||
{ | ||||
|
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 | |||
|
r110 | grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0); | ||
grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0); | ||||
grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1); | ||||
grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0); | ||||
grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1); | ||||
grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0); | ||||
grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1); | ||||
grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0); | ||||
grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1); | ||||
m_autoScaleCheck->setChecked(true); | ||||
} | ||||
// Combo box for selecting theme | ||||
void MainWidget::initThemeCombo(QGridLayout *grid) | ||||
{ | ||||
|
r64 | QComboBox *chartTheme = new QComboBox(); | ||
|
r81 | chartTheme->addItem("Default"); | ||
|
r64 | chartTheme->addItem("Vanilla"); | ||
chartTheme->addItem("Icy"); | ||||
chartTheme->addItem("Grayscale"); | ||||
|
r125 | chartTheme->addItem("Scientific"); | ||
|
r77 | chartTheme->addItem("Unnamed1"); | ||
|
r64 | connect(chartTheme, SIGNAL(currentIndexChanged(int)), | ||
this, SLOT(changeChartTheme(int))); | ||||
grid->addWidget(new QLabel("Chart theme:"), 8, 0); | ||||
grid->addWidget(chartTheme, 8, 1); | ||||
|
r110 | } | ||
|
r60 | |||
|
r110 | void MainWidget::initPieControls() | ||
{ | ||||
// Pie series specific settings | ||||
|
r77 | // Pie size factory | ||
|
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))); | ||||
|
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(); | ||||
|
r60 | m_pieLayout->setEnabled(false); | ||
|
r77 | m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0); | ||
|
r60 | m_pieLayout->addWidget(pieSizeSpin, 0, 1); | ||
|
r77 | m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0); | ||
m_pieLayout->addWidget(piePosCombo, 1, 1); | ||||
|
r5 | } | ||
|
r19 | void MainWidget::addSeries() | ||
{ | ||||
|
r60 | DataSerieDialog dialog(m_defaultSeriesName, this); | ||
|
r278 | connect(&dialog, SIGNAL(accepted(QString, int, int, QString, bool)), | ||
this, SLOT(addSeries(QString, int, int, QString, bool))); | ||||
|
r19 | dialog.exec(); | ||
} | ||||
|
r278 | QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics) | ||
|
r19 | { | ||
|
r278 | // TODO: dataCharacteristics | ||
QList<RealList> testData; | ||||
for (int j(0); j < columnCount; j++) { | ||||
QList <qreal> newColumn; | ||||
for (int i(0); i < rowCount; i++) { | ||||
if (dataCharacteristics == "Sin") { | ||||
newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100)); | ||||
} else if (dataCharacteristics == "Sin + random") { | ||||
newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5)); | ||||
} else if (dataCharacteristics == "Random") { | ||||
newColumn.append(rand() % 5); | ||||
} else if (dataCharacteristics == "Linear") { | ||||
//newColumn.append(i * (j + 1.0)); | ||||
// TODO: temporary hack to make pie work; prevent zero values: | ||||
newColumn.append(i * (j + 1.0) + 0.1); | ||||
} else { // "constant" | ||||
newColumn.append((j + 1.0)); | ||||
} | ||||
} | ||||
testData.append(newColumn); | ||||
} | ||||
return testData; | ||||
} | ||||
|
r20 | |||
|
r278 | QStringList MainWidget::generateLabels(int count) | ||
{ | ||||
QStringList result; | ||||
for (int i(0); i < count; i++) | ||||
result.append("label" + QString::number(i)); | ||||
return result; | ||||
} | ||||
|
r165 | |||
|
r278 | void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled) | ||
{ | ||||
qDebug() << "addSeries: " << seriesName | ||||
<< " columnCount: " << columnCount | ||||
<< " rowCount: " << rowCount | ||||
<< " dataCharacteristics: " << dataCharacteristics | ||||
<< " labels enabled: " << labelsEnabled; | ||||
m_defaultSeriesName = seriesName; | ||||
QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics); | ||||
// Line series and scatter series use similar data | ||||
if (seriesName.contains("line", Qt::CaseInsensitive)) { | ||||
for (int j(0); j < data.count(); j ++) { | ||||
QList<qreal> column = data.at(j); | ||||
|
r349 | QLineSeries *series = new QLineSeries(); | ||
|
r278 | for (int i(0); i < column.count(); i++) { | ||
series->add(i, column.at(i)); | ||||
} | ||||
m_chartWidget->addSeries(series); | ||||
setCurrentSeries(series); | ||||
|
r42 | } | ||
|
r278 | } else if (seriesName.contains("scatter", Qt::CaseInsensitive)) { | ||
for (int j(0); j < data.count(); j++) { | ||||
QList<qreal> column = data.at(j); | ||||
QScatterSeries *series = new QScatterSeries(); | ||||
for (int i(0); i < column.count(); i++) { | ||||
(*series) << QPointF(i, column.at(i)); | ||||
} | ||||
m_chartWidget->addSeries(series); | ||||
setCurrentSeries(series); | ||||
|
r42 | } | ||
|
r278 | } else if (seriesName.contains("pie", Qt::CaseInsensitive)) { | ||
QStringList labels = generateLabels(rowCount); | ||||
for (int j(0); j < data.count(); j++) { | ||||
QPieSeries *series = new QPieSeries(); | ||||
QList<qreal> column = data.at(j); | ||||
for (int i(0); i < column.count(); i++) { | ||||
series->add(column.at(i), labels.at(i)); | ||||
} | ||||
m_chartWidget->addSeries(series); | ||||
setCurrentSeries(series); | ||||
|
r20 | } | ||
|
r284 | } else if (seriesName == "Bar" | ||
|| seriesName == "Stacked bar" | ||||
|| seriesName == "Percent bar") { | ||||
|
r279 | // TODO: replace QBarCategory with QStringList? | ||
QBarCategory *category = new QBarCategory; | ||||
QStringList labels = generateLabels(rowCount); | ||||
foreach(QString label, labels) | ||||
*category << label; | ||||
|
r338 | QBarSeries* series = 0; | ||
|
r284 | if (seriesName == "Bar") | ||
|
r338 | series = new QBarSeries(category, this); | ||
|
r284 | else if (seriesName == "Stacked bar") | ||
|
r338 | series = new QStackedBarSeries(category, this); | ||
|
r284 | else | ||
|
r338 | series = new QPercentBarSeries(category, this); | ||
|
r279 | |||
for (int j(0); j < data.count(); j++) { | ||||
QList<qreal> column = data.at(j); | ||||
|
r284 | QBarSet *set = new QBarSet("set" + QString::number(j)); | ||
|
r279 | for (int i(0); i < column.count(); i++) { | ||
*set << column.at(i); | ||||
} | ||||
series->addBarSet(set); | ||||
} | ||||
|
r364 | series->setFloatingValuesEnabled(true); | ||
|
r357 | series->setToolTipEnabled(); | ||
|
r364 | series->setSeparatorsEnabled(false); | ||
|
r279 | m_chartWidget->addSeries(series); | ||
setCurrentSeries(series); | ||||
|
r20 | } | ||
|
r279 | // TODO: spline and area | ||
|
r19 | } | ||
|
r360 | void MainWidget::setCurrentSeries(QSeries *series) | ||
|
r5 | { | ||
|
r180 | if (series) { | ||
m_currentSeries = series; | ||||
switch (m_currentSeries->type()) { | ||||
|
r360 | case QSeries::SeriesTypeLine: | ||
|
r180 | break; | ||
|
r360 | case QSeries::SeriesTypeScatter: | ||
|
r180 | break; | ||
|
r360 | case QSeries::SeriesTypePie: | ||
|
r180 | break; | ||
|
r360 | case QSeries::SeriesTypeBar: | ||
|
r180 | qDebug() << "setCurrentSeries (bar)"; | ||
break; | ||||
|
r360 | case QSeries::SeriesTypeStackedBar: | ||
|
r180 | qDebug() << "setCurrentSeries (Stackedbar)"; | ||
break; | ||||
|
r360 | case QSeries::SeriesTypePercentBar: | ||
|
r180 | qDebug() << "setCurrentSeries (Percentbar)"; | ||
break; | ||||
default: | ||||
Q_ASSERT(false); | ||||
break; | ||||
} | ||||
|
r13 | } | ||
|
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; | ||||
} | ||||
|
r60 | |||
|
r64 | void MainWidget::changeChartTheme(int themeIndex) | ||
{ | ||||
qDebug() << "changeChartTheme: " << themeIndex; | ||||
|
r154 | m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex); | ||
|
r167 | //TODO: remove this hack. This is just to make it so that theme change is seen immediately. | ||
QSize s = size(); | ||||
s.setWidth(s.width()+1); | ||||
resize(s); | ||||
|
r64 | } | ||
|
r60 | void MainWidget::setPieSizeFactor(double size) | ||
{ | ||||
QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); | ||||
|
r75 | if (pie) | ||
pie->setSizeFactor(qreal(size)); | ||||
|
r60 | } | ||
|
r77 | |||
void MainWidget::setPiePosition(int position) | ||||
{ | ||||
QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); | ||||
if (pie) | ||||
pie->setPosition((QPieSeries::PiePosition) position); | ||||
|
r60 | } | ||