mainwidget.cpp
380 lines
| 13.9 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r830 | /**************************************************************************** | ||
** | ||||
Titta Heikkala
|
r2688 | ** Copyright (C) 2014 Digia Plc | ||
Jani Honkonen
|
r830 | ** All rights reserved. | ||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
Miikka Heikkinen
|
r2574 | ** This file is part of the Qt Enterprise Charts Add-on. | ||
Jani Honkonen
|
r830 | ** | ||
** $QT_BEGIN_LICENSE$ | ||||
Miikka Heikkinen
|
r2574 | ** Licensees holding valid Qt Enterprise licenses may use this file in | ||
** accordance with the Qt Enterprise License Agreement provided with the | ||||
Jani Honkonen
|
r830 | ** Software or, alternatively, in accordance with the terms contained in | ||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
Tero Ahola
|
r5 | #include "mainwidget.h" | ||
Tero Ahola
|
r19 | #include "dataseriedialog.h" | ||
Titta Heikkala
|
r2714 | #include <QtCharts/QChartView> | ||
#include <QtCharts/QPieSeries> | ||||
#include <QtCharts/QScatterSeries> | ||||
#include <QtCharts/QLineSeries> | ||||
#include <QtCharts/QAreaSeries> | ||||
#include <QtCharts/QSplineSeries> | ||||
#include <QtCharts/QBarSet> | ||||
#include <QtCharts/QBarSeries> | ||||
#include <QtCharts/QStackedBarSeries> | ||||
#include <QtCharts/QPercentBarSeries> | ||||
#include <QtWidgets/QPushButton> | ||||
#include <QtWidgets/QComboBox> | ||||
#include <QtWidgets/QSpinBox> | ||||
#include <QtWidgets/QCheckBox> | ||||
#include <QtWidgets/QGridLayout> | ||||
#include <QtWidgets/QHBoxLayout> | ||||
#include <QtWidgets/QLabel> | ||||
#include <QtWidgets/QSpacerItem> | ||||
#include <QtWidgets/QMessageBox> | ||||
Tero Ahola
|
r13 | #include <cmath> | ||
Titta Heikkala
|
r2714 | #include <QtCore/QDebug> | ||
#include <QtGui/QStandardItemModel> | ||||
#include <QtCharts/QBarCategoryAxis> | ||||
#include <QtOpenGL/QGLWidget> | ||||
Tero Ahola
|
r5 | |||
Titta Heikkala
|
r2712 | QT_CHARTS_USE_NAMESPACE | ||
Tero Ahola
|
r19 | |||
Tero Ahola
|
r5 | MainWidget::MainWidget(QWidget *parent) : | ||
Tero Ahola
|
r376 | QWidget(parent), | ||
m_addSerieDialog(0), | ||||
Tero Ahola
|
r815 | m_chart(0) | ||
Tero Ahola
|
r5 | { | ||
Tero Ahola
|
r815 | m_chart = new QChart(); | ||
Tero Ahola
|
r77 | |||
Tero Ahola
|
r111 | // Grid layout for the controls for configuring the chart widget | ||
Tero Ahola
|
r110 | QGridLayout *grid = new QGridLayout(); | ||
Tero Ahola
|
r19 | QPushButton *addSeriesButton = new QPushButton("Add series"); | ||
connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); | ||||
Tero Ahola
|
r110 | grid->addWidget(addSeriesButton, 0, 1); | ||
initBackroundCombo(grid); | ||||
initScaleControls(grid); | ||||
initThemeCombo(grid); | ||||
Tero Ahola
|
r379 | initCheckboxes(grid); | ||
Tero Ahola
|
r110 | // 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); | ||||
Tero Ahola
|
r111 | |||
Tero Ahola
|
r815 | // Create chart view with the chart | ||
m_chartView = new QChartView(m_chart, this); | ||||
m_chartView->setRubberBand(QChartView::HorizonalRubberBand); | ||||
Tero Ahola
|
r111 | // Another grid layout as a main layout | ||
QGridLayout *mainLayout = new QGridLayout(); | ||||
Tero Ahola
|
r110 | mainLayout->addLayout(grid, 0, 0); | ||
Tero Ahola
|
r379 | mainLayout->addWidget(m_chartView, 0, 1, 3, 1); | ||
Tero Ahola
|
r110 | setLayout(mainLayout); | ||
} | ||||
Tero Ahola
|
r5 | |||
Tero Ahola
|
r110 | // Combo box for selecting the chart's background | ||
void MainWidget::initBackroundCombo(QGridLayout *grid) | ||||
{ | ||||
Tero Ahola
|
r5 | 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))); | ||||
Tero Ahola
|
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) | ||||
{ | ||||
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
|
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) | ||||
{ | ||||
Tero Ahola
|
r64 | QComboBox *chartTheme = new QComboBox(); | ||
Tero Ahola
|
r81 | chartTheme->addItem("Default"); | ||
Tero Ahola
|
r692 | chartTheme->addItem("Light"); | ||
chartTheme->addItem("Blue Cerulean"); | ||||
chartTheme->addItem("Dark"); | ||||
chartTheme->addItem("Brown Sand"); | ||||
chartTheme->addItem("Blue NCS"); | ||||
Tero Ahola
|
r757 | chartTheme->addItem("High Contrast"); | ||
chartTheme->addItem("Blue Icy"); | ||||
Titta Heikkala
|
r2631 | chartTheme->addItem("Qt"); | ||
Tero Ahola
|
r64 | connect(chartTheme, SIGNAL(currentIndexChanged(int)), | ||
this, SLOT(changeChartTheme(int))); | ||||
grid->addWidget(new QLabel("Chart theme:"), 8, 0); | ||||
grid->addWidget(chartTheme, 8, 1); | ||||
Tero Ahola
|
r110 | } | ||
Tero Ahola
|
r60 | |||
Tero Ahola
|
r379 | // Different check boxes for customizing chart | ||
void MainWidget::initCheckboxes(QGridLayout *grid) | ||||
{ | ||||
// TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off | ||||
QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom"); | ||||
Tero Ahola
|
r815 | // connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool))); | ||
Tero Ahola
|
r379 | zoomCheckBox->setChecked(true); | ||
grid->addWidget(zoomCheckBox, grid->rowCount(), 0); | ||||
QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias"); | ||||
connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool))); | ||||
aliasCheckBox->setChecked(false); | ||||
grid->addWidget(aliasCheckBox, grid->rowCount(), 0); | ||||
Tero Ahola
|
r1778 | |||
QCheckBox *openGLCheckBox = new QCheckBox("Use QGLWidget"); | ||||
connect(openGLCheckBox, SIGNAL(toggled(bool)), this, SLOT(openGLToggled(bool))); | ||||
openGLCheckBox->setChecked(false); | ||||
grid->addWidget(openGLCheckBox, grid->rowCount(), 0); | ||||
Tero Ahola
|
r379 | } | ||
void MainWidget::antiAliasToggled(bool enabled) | ||||
{ | ||||
m_chartView->setRenderHint(QPainter::Antialiasing, enabled); | ||||
} | ||||
Tero Ahola
|
r1778 | void MainWidget::openGLToggled(bool enabled) | ||
{ | ||||
if (enabled) { | ||||
QGLFormat f = QGLFormat::defaultFormat(); | ||||
f.setSampleBuffers(true); | ||||
f.setSamples(4); | ||||
QGLFormat::setDefaultFormat(f); | ||||
QGLWidget *g = new QGLWidget(); | ||||
m_chartView->setViewport(g); | ||||
} else { | ||||
m_chartView->setViewport(0); | ||||
} | ||||
} | ||||
Tero Ahola
|
r19 | void MainWidget::addSeries() | ||
{ | ||||
Tero Ahola
|
r376 | if (!m_addSerieDialog) { | ||
m_addSerieDialog = new DataSerieDialog(this); | ||||
Michal Klocek
|
r967 | connect(m_addSerieDialog, SIGNAL(accepted(QString,int,int,QString,bool)), | ||
this, SLOT(addSeries(QString,int,int,QString,bool))); | ||||
Tero Ahola
|
r376 | } | ||
m_addSerieDialog->exec(); | ||||
Tero Ahola
|
r19 | } | ||
Tero Ahola
|
r278 | QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics) | ||
Tero Ahola
|
r19 | { | ||
Tero Ahola
|
r278 | 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") { | ||||
Tero Ahola
|
r510 | newColumn.append(rand() % 10 + (qreal) rand() / (qreal) RAND_MAX); | ||
Tero Ahola
|
r278 | } 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; | ||||
} | ||||
Tero Ahola
|
r20 | |||
Tero Ahola
|
r278 | QStringList MainWidget::generateLabels(int count) | ||
{ | ||||
QStringList result; | ||||
for (int i(0); i < count; i++) | ||||
result.append("label" + QString::number(i)); | ||||
return result; | ||||
} | ||||
sauimone
|
r165 | |||
Tero Ahola
|
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 | ||||
Tero Ahola
|
r468 | if (seriesName == "Line") { | ||
Tero Ahola
|
r278 | for (int j(0); j < data.count(); j ++) { | ||
QList<qreal> column = data.at(j); | ||||
Michal Klocek
|
r349 | QLineSeries *series = new QLineSeries(); | ||
Michal Klocek
|
r912 | series->setName("line" + QString::number(j)); | ||
Tero Ahola
|
r815 | for (int i(0); i < column.count(); i++) | ||
series->append(i, column.at(i)); | ||||
m_chart->addSeries(series); | ||||
Tero Ahola
|
r42 | } | ||
Tero Ahola
|
r517 | } else if (seriesName == "Area") { | ||
Tero Ahola
|
r468 | // TODO: lower series for the area? | ||
for (int j(0); j < data.count(); j ++) { | ||||
QList<qreal> column = data.at(j); | ||||
QLineSeries *lineSeries = new QLineSeries(); | ||||
Tero Ahola
|
r815 | for (int i(0); i < column.count(); i++) | ||
lineSeries->append(i, column.at(i)); | ||||
Tero Ahola
|
r468 | QAreaSeries *areaSeries = new QAreaSeries(lineSeries); | ||
Michal Klocek
|
r912 | areaSeries->setName("area" + QString::number(j)); | ||
Tero Ahola
|
r815 | m_chart->addSeries(areaSeries); | ||
Tero Ahola
|
r468 | } | ||
} else if (seriesName == "Scatter") { | ||||
Tero Ahola
|
r278 | for (int j(0); j < data.count(); j++) { | ||
QList<qreal> column = data.at(j); | ||||
QScatterSeries *series = new QScatterSeries(); | ||||
Michal Klocek
|
r912 | series->setName("scatter" + QString::number(j)); | ||
Tero Ahola
|
r815 | for (int i(0); i < column.count(); i++) | ||
series->append(i, column.at(i)); | ||||
m_chart->addSeries(series); | ||||
Tero Ahola
|
r42 | } | ||
Tero Ahola
|
r468 | } else if (seriesName == "Pie") { | ||
Tero Ahola
|
r278 | QStringList labels = generateLabels(rowCount); | ||
for (int j(0); j < data.count(); j++) { | ||||
QPieSeries *series = new QPieSeries(); | ||||
QList<qreal> column = data.at(j); | ||||
Tero Ahola
|
r815 | for (int i(0); i < column.count(); i++) | ||
Jani Honkonen
|
r1206 | series->append(labels.at(i), column.at(i)); | ||
Tero Ahola
|
r815 | m_chart->addSeries(series); | ||
Tero Ahola
|
r20 | } | ||
sauimone
|
r1594 | } else if (seriesName == "Bar" | ||
Tero Ahola
|
r284 | || seriesName == "Stacked bar" | ||
|| seriesName == "Percent bar") { | ||||
sauimone
|
r377 | QStringList category; | ||
Tero Ahola
|
r279 | QStringList labels = generateLabels(rowCount); | ||
Jani Honkonen
|
r2100 | foreach (QString label, labels) | ||
sauimone
|
r377 | category << label; | ||
sauimone
|
r1584 | QAbstractBarSeries* series = 0; | ||
sauimone
|
r1594 | if (seriesName == "Bar") { | ||
series = new QBarSeries(this); | ||||
Marek Rosa
|
r1808 | QBarCategoryAxis* axis = new QBarCategoryAxis(); | ||
Michal Klocek
|
r1541 | axis->append(category); | ||
Michal Klocek
|
r1577 | m_chart->setAxisX(axis,series); | ||
sauimone
|
r1114 | } else if (seriesName == "Stacked bar") { | ||
series = new QStackedBarSeries(this); | ||||
Marek Rosa
|
r1808 | QBarCategoryAxis* axis = new QBarCategoryAxis(); | ||
Michal Klocek
|
r1541 | axis->append(category); | ||
Michal Klocek
|
r1577 | m_chart->setAxisX(axis,series); | ||
sauimone
|
r1114 | } else { | ||
series = new QPercentBarSeries(this); | ||||
Marek Rosa
|
r1808 | QBarCategoryAxis* axis = new QBarCategoryAxis(); | ||
Michal Klocek
|
r1541 | axis->append(category); | ||
Michal Klocek
|
r1577 | m_chart->setAxisX(axis,series); | ||
sauimone
|
r1114 | } | ||
Tero Ahola
|
r279 | |||
for (int j(0); j < data.count(); j++) { | ||||
QList<qreal> column = data.at(j); | ||||
Tero Ahola
|
r284 | QBarSet *set = new QBarSet("set" + QString::number(j)); | ||
Tero Ahola
|
r815 | for (int i(0); i < column.count(); i++) | ||
Tero Ahola
|
r279 | *set << column.at(i); | ||
sauimone
|
r1194 | series->append(set); | ||
Tero Ahola
|
r279 | } | ||
Marek Rosa
|
r455 | |||
Tero Ahola
|
r815 | m_chart->addSeries(series); | ||
Tero Ahola
|
r468 | } else if (seriesName == "Spline") { | ||
Marek Rosa
|
r455 | for (int j(0); j < data.count(); j ++) { | ||
QList<qreal> column = data.at(j); | ||||
QSplineSeries *series = new QSplineSeries(); | ||||
Titta Heikkala
|
r2712 | series->setName("spline" + QString::number(j)); | ||
Tero Ahola
|
r815 | for (int i(0); i < column.count(); i++) | ||
series->append(i, column.at(i)); | ||||
m_chart->addSeries(series); | ||||
Tero Ahola
|
r180 | } | ||
Tero Ahola
|
r13 | } | ||
Marek Rosa
|
r1583 | m_chart->createDefaultAxes(); | ||
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; | ||||
Titta Heikkala
|
r2631 | if (themeIndex == 0) | ||
m_chart->setTheme(QChart::ChartThemeLight); | ||||
else | ||||
m_chart->setTheme((QChart::ChartTheme) (themeIndex - 1)); | ||||
Tero Ahola
|
r64 | } | ||