themewidget.cpp
381 lines
| 13.3 KiB
| text/x-c
|
CppLexer
Miikka Heikkinen
|
r2854 | /**************************************************************************** | ||
Jani Honkonen
|
r830 | ** | ||
Miikka Heikkinen
|
r2854 | ** Copyright (C) 2016 The Qt Company Ltd. | ||
** Contact: https://www.qt.io/licensing/ | ||||
Jani Honkonen
|
r830 | ** | ||
Miikka Heikkinen
|
r2854 | ** This file is part of the Qt Charts module of the Qt Toolkit. | ||
Jani Honkonen
|
r830 | ** | ||
Miikka Heikkinen
|
r2854 | ** $QT_BEGIN_LICENSE:GPL$ | ||
Titta Heikkala
|
r2845 | ** Commercial License Usage | ||
** Licensees holding valid commercial Qt licenses may use this file in | ||||
** accordance with the commercial license agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and The Qt Company. For licensing terms | ||||
Miikka Heikkinen
|
r2854 | ** and conditions see https://www.qt.io/terms-conditions. For further | ||
** information use the contact form at https://www.qt.io/contact-us. | ||||
** | ||||
** GNU General Public License Usage | ||||
** Alternatively, this file may be used under the terms of the GNU | ||||
** General Public License version 3 or (at your option) any later version | ||||
** approved by the KDE Free Qt Foundation. The licenses are as published by | ||||
** the Free Software Foundation and appearing in the file LICENSE.GPL3 | ||||
** included in the packaging of this file. Please review the following | ||||
** information to ensure the GNU General Public License requirements will | ||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html. | ||||
Jani Honkonen
|
r830 | ** | ||
Titta Heikkala
|
r2845 | ** $QT_END_LICENSE$ | ||
** | ||||
Miikka Heikkinen
|
r2854 | ****************************************************************************/ | ||
Michal Klocek
|
r748 | |||
#include "themewidget.h" | ||||
Titta Heikkala
|
r2714 | #include <QtCharts/QChartView> | ||
#include <QtCharts/QPieSeries> | ||||
#include <QtCharts/QPieSlice> | ||||
#include <QtCharts/QAbstractBarSeries> | ||||
#include <QtCharts/QPercentBarSeries> | ||||
#include <QtCharts/QStackedBarSeries> | ||||
#include <QtCharts/QBarSeries> | ||||
#include <QtCharts/QBarSet> | ||||
#include <QtCharts/QLineSeries> | ||||
#include <QtCharts/QSplineSeries> | ||||
#include <QtCharts/QScatterSeries> | ||||
#include <QtCharts/QAreaSeries> | ||||
#include <QtCharts/QLegend> | ||||
#include <QtWidgets/QGridLayout> | ||||
#include <QtWidgets/QFormLayout> | ||||
#include <QtWidgets/QComboBox> | ||||
#include <QtWidgets/QSpinBox> | ||||
#include <QtWidgets/QCheckBox> | ||||
#include <QtWidgets/QGroupBox> | ||||
#include <QtWidgets/QLabel> | ||||
#include <QtCore/QTime> | ||||
#include <QtCharts/QBarCategoryAxis> | ||||
Michal Klocek
|
r748 | |||
Jani Honkonen
|
r2099 | ThemeWidget::ThemeWidget(QWidget *parent) : | ||
Michal Klocek
|
r748 | QWidget(parent), | ||
m_listCount(3), | ||||
Tero Ahola
|
r843 | m_valueMax(10), | ||
m_valueCount(7), | ||||
Jani Honkonen
|
r2099 | m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)), | ||
Michal Klocek
|
r748 | m_themeComboBox(createThemeBox()), | ||
Tero Ahola
|
r853 | m_antialiasCheckBox(new QCheckBox("Anti-aliasing")), | ||
sauimone
|
r803 | m_animatedComboBox(createAnimationBox()), | ||
m_legendComboBox(createLegendBox()) | ||||
Michal Klocek
|
r748 | { | ||
connectSignals(); | ||||
// create layout | ||||
Jani Honkonen
|
r2099 | QGridLayout *baseLayout = new QGridLayout(); | ||
Michal Klocek
|
r748 | QHBoxLayout *settingsLayout = new QHBoxLayout(); | ||
settingsLayout->addWidget(new QLabel("Theme:")); | ||||
settingsLayout->addWidget(m_themeComboBox); | ||||
settingsLayout->addWidget(new QLabel("Animation:")); | ||||
settingsLayout->addWidget(m_animatedComboBox); | ||||
Michal Klocek
|
r855 | settingsLayout->addWidget(new QLabel("Legend:")); | ||
sauimone
|
r803 | settingsLayout->addWidget(m_legendComboBox); | ||
Michal Klocek
|
r748 | settingsLayout->addWidget(m_antialiasCheckBox); | ||
settingsLayout->addStretch(); | ||||
baseLayout->addLayout(settingsLayout, 0, 0, 1, 3); | ||||
//create charts | ||||
QChartView *chartView; | ||||
chartView = new QChartView(createAreaChart()); | ||||
baseLayout->addWidget(chartView, 1, 0); | ||||
m_charts << chartView; | ||||
chartView = new QChartView(createBarChart(m_valueCount)); | ||||
baseLayout->addWidget(chartView, 1, 1); | ||||
m_charts << chartView; | ||||
chartView = new QChartView(createLineChart()); | ||||
baseLayout->addWidget(chartView, 1, 2); | ||||
m_charts << chartView; | ||||
chartView = new QChartView(createPieChart()); | ||||
chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen... | ||||
baseLayout->addWidget(chartView, 2, 0); | ||||
m_charts << chartView; | ||||
chartView = new QChartView(createSplineChart()); | ||||
baseLayout->addWidget(chartView, 2, 1); | ||||
m_charts << chartView; | ||||
Michal Klocek
|
r754 | chartView = new QChartView(createScatterChart()); | ||
Michal Klocek
|
r748 | baseLayout->addWidget(chartView, 2, 2); | ||
m_charts << chartView; | ||||
setLayout(baseLayout); | ||||
Tero Ahola
|
r843 | |||
// Set defaults | ||||
m_antialiasCheckBox->setChecked(true); | ||||
Michal Klocek
|
r855 | updateUI(); | ||
Michal Klocek
|
r748 | } | ||
ThemeWidget::~ThemeWidget() | ||||
{ | ||||
} | ||||
void ThemeWidget::connectSignals() | ||||
{ | ||||
connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); | ||||
connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); | ||||
connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); | ||||
sauimone
|
r803 | connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); | ||
Michal Klocek
|
r748 | } | ||
Jani Honkonen
|
r2099 | DataTable ThemeWidget::generateRandomData(int listCount, int valueMax, int valueCount) const | ||
Michal Klocek
|
r748 | { | ||
DataTable dataTable; | ||||
// set seed for random stuff | ||||
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); | ||||
// generate random data | ||||
for (int i(0); i < listCount; i++) { | ||||
DataList dataList; | ||||
Tero Ahola
|
r843 | qreal yValue(0); | ||
Michal Klocek
|
r748 | for (int j(0); j < valueCount; j++) { | ||
Jani Honkonen
|
r2099 | yValue = yValue + (qreal)(qrand() % valueMax) / (qreal) valueCount; | ||
Tero Ahola
|
r843 | QPointF value((j + (qreal) rand() / (qreal) RAND_MAX) * ((qreal) m_valueMax / (qreal) valueCount), | ||
yValue); | ||||
Tero Ahola
|
r945 | QString label = "Slice " + QString::number(i) + ":" + QString::number(j); | ||
Michal Klocek
|
r748 | dataList << Data(value, label); | ||
} | ||||
dataTable << dataList; | ||||
} | ||||
return dataTable; | ||||
} | ||||
Jani Honkonen
|
r2099 | QComboBox *ThemeWidget::createThemeBox() const | ||
Michal Klocek
|
r748 | { | ||
// settings layout | ||||
Jani Honkonen
|
r2099 | QComboBox *themeComboBox = new QComboBox(); | ||
Michal Klocek
|
r748 | themeComboBox->addItem("Light", QChart::ChartThemeLight); | ||
themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean); | ||||
themeComboBox->addItem("Dark", QChart::ChartThemeDark); | ||||
themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand); | ||||
themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs); | ||||
Tero Ahola
|
r757 | themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast); | ||
themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy); | ||||
Titta Heikkala
|
r2625 | themeComboBox->addItem("Qt", QChart::ChartThemeQt); | ||
Michal Klocek
|
r748 | return themeComboBox; | ||
} | ||||
Jani Honkonen
|
r2099 | QComboBox *ThemeWidget::createAnimationBox() const | ||
Michal Klocek
|
r748 | { | ||
// settings layout | ||||
Jani Honkonen
|
r2099 | QComboBox *animationComboBox = new QComboBox(); | ||
Michal Klocek
|
r748 | animationComboBox->addItem("No Animations", QChart::NoAnimation); | ||
animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations); | ||||
animationComboBox->addItem("Series Animations", QChart::SeriesAnimations); | ||||
animationComboBox->addItem("All Animations", QChart::AllAnimations); | ||||
return animationComboBox; | ||||
} | ||||
Jani Honkonen
|
r2099 | QComboBox *ThemeWidget::createLegendBox() const | ||
sauimone
|
r803 | { | ||
Jani Honkonen
|
r2099 | QComboBox *legendComboBox = new QComboBox(); | ||
Michal Klocek
|
r855 | legendComboBox->addItem("No Legend ", 0); | ||
Tero Ahola
|
r1357 | legendComboBox->addItem("Legend Top", Qt::AlignTop); | ||
legendComboBox->addItem("Legend Bottom", Qt::AlignBottom); | ||||
legendComboBox->addItem("Legend Left", Qt::AlignLeft); | ||||
legendComboBox->addItem("Legend Right", Qt::AlignRight); | ||||
sauimone
|
r803 | return legendComboBox; | ||
} | ||||
Jani Honkonen
|
r2099 | QChart *ThemeWidget::createAreaChart() const | ||
Michal Klocek
|
r748 | { | ||
QChart *chart = new QChart(); | ||||
chart->setTitle("Area chart"); | ||||
Tero Ahola
|
r843 | |||
// The lower series initialized to zero values | ||||
Tero Ahola
|
r845 | QLineSeries *lowerSeries = 0; | ||
Tero Ahola
|
r958 | QString name("Series "); | ||
int nameIndex = 0; | ||||
Tero Ahola
|
r843 | for (int i(0); i < m_dataTable.count(); i++) { | ||
QLineSeries *upperSeries = new QLineSeries(chart); | ||||
for (int j(0); j < m_dataTable[i].count(); j++) { | ||||
Data data = m_dataTable[i].at(j); | ||||
Jani Honkonen
|
r2099 | if (lowerSeries) { | ||
Miikka Heikkinen
|
r2820 | const QVector<QPointF>& points = lowerSeries->pointsVector(); | ||
Michal Klocek
|
r1057 | upperSeries->append(QPointF(j, points[i].y() + data.first.y())); | ||
Jani Honkonen
|
r2099 | } else { | ||
Tero Ahola
|
r845 | upperSeries->append(QPointF(j, data.first.y())); | ||
Jani Honkonen
|
r2099 | } | ||
Michal Klocek
|
r748 | } | ||
Tero Ahola
|
r843 | QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries); | ||
area->setName(name + QString::number(nameIndex)); | ||||
nameIndex++; | ||||
chart->addSeries(area); | ||||
Marek Rosa
|
r1582 | chart->createDefaultAxes(); | ||
Tero Ahola
|
r843 | lowerSeries = upperSeries; | ||
Michal Klocek
|
r748 | } | ||
Tero Ahola
|
r958 | |||
Michal Klocek
|
r748 | return chart; | ||
} | ||||
Jani Honkonen
|
r2099 | QChart *ThemeWidget::createBarChart(int valueCount) const | ||
Michal Klocek
|
r748 | { | ||
sauimone
|
r1675 | Q_UNUSED(valueCount); | ||
Jani Honkonen
|
r2099 | QChart *chart = new QChart(); | ||
Michal Klocek
|
r748 | chart->setTitle("Bar chart"); | ||
Tero Ahola
|
r958 | |||
Jani Honkonen
|
r2099 | QStackedBarSeries *series = new QStackedBarSeries(chart); | ||
Tero Ahola
|
r843 | for (int i(0); i < m_dataTable.count(); i++) { | ||
Tero Ahola
|
r945 | QBarSet *set = new QBarSet("Bar set " + QString::number(i)); | ||
Tero Ahola
|
r843 | foreach (Data data, m_dataTable[i]) | ||
*set << data.first.y(); | ||||
sauimone
|
r1194 | series->append(set); | ||
Michal Klocek
|
r748 | } | ||
Tero Ahola
|
r843 | chart->addSeries(series); | ||
Michal Klocek
|
r1577 | chart->createDefaultAxes(); | ||
Tero Ahola
|
r958 | |||
Michal Klocek
|
r748 | return chart; | ||
} | ||||
Jani Honkonen
|
r2099 | QChart *ThemeWidget::createLineChart() const | ||
Michal Klocek
|
r748 | { | ||
Jani Honkonen
|
r2099 | QChart *chart = new QChart(); | ||
Michal Klocek
|
r748 | chart->setTitle("Line chart"); | ||
Tero Ahola
|
r958 | |||
sauimone
|
r786 | QString name("Series "); | ||
int nameIndex = 0; | ||||
Michal Klocek
|
r748 | foreach (DataList list, m_dataTable) { | ||
QLineSeries *series = new QLineSeries(chart); | ||||
foreach (Data data, list) | ||||
Jani Honkonen
|
r796 | series->append(data.first); | ||
sauimone
|
r786 | series->setName(name + QString::number(nameIndex)); | ||
nameIndex++; | ||||
Michal Klocek
|
r748 | chart->addSeries(series); | ||
} | ||||
Michal Klocek
|
r1588 | chart->createDefaultAxes(); | ||
Tero Ahola
|
r958 | |||
Michal Klocek
|
r748 | return chart; | ||
} | ||||
Jani Honkonen
|
r2099 | QChart *ThemeWidget::createPieChart() const | ||
Michal Klocek
|
r748 | { | ||
Jani Honkonen
|
r2099 | QChart *chart = new QChart(); | ||
Michal Klocek
|
r748 | chart->setTitle("Pie chart"); | ||
Tero Ahola
|
r958 | |||
Michal Klocek
|
r748 | qreal pieSize = 1.0 / m_dataTable.count(); | ||
for (int i = 0; i < m_dataTable.count(); i++) { | ||||
QPieSeries *series = new QPieSeries(chart); | ||||
foreach (Data data, m_dataTable[i]) { | ||||
Jani Honkonen
|
r1206 | QPieSlice *slice = series->append(data.second, data.first.y()); | ||
Michal Klocek
|
r748 | if (data == m_dataTable[i].first()) { | ||
slice->setLabelVisible(); | ||||
slice->setExploded(); | ||||
} | ||||
} | ||||
qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count()); | ||||
series->setPieSize(pieSize); | ||||
Tero Ahola
|
r884 | series->setHorizontalPosition(hPos); | ||
series->setVerticalPosition(0.5); | ||||
Michal Klocek
|
r748 | chart->addSeries(series); | ||
} | ||||
return chart; | ||||
} | ||||
Jani Honkonen
|
r2099 | QChart *ThemeWidget::createSplineChart() const | ||
{ | ||||
// spine chart | ||||
QChart *chart = new QChart(); | ||||
Michal Klocek
|
r748 | chart->setTitle("Spline chart"); | ||
sauimone
|
r786 | QString name("Series "); | ||
int nameIndex = 0; | ||||
Michal Klocek
|
r748 | foreach (DataList list, m_dataTable) { | ||
QSplineSeries *series = new QSplineSeries(chart); | ||||
foreach (Data data, list) | ||||
Jani Honkonen
|
r796 | series->append(data.first); | ||
sauimone
|
r786 | series->setName(name + QString::number(nameIndex)); | ||
nameIndex++; | ||||
Michal Klocek
|
r748 | chart->addSeries(series); | ||
} | ||||
Michal Klocek
|
r1588 | chart->createDefaultAxes(); | ||
Michal Klocek
|
r748 | return chart; | ||
} | ||||
Jani Honkonen
|
r2099 | QChart *ThemeWidget::createScatterChart() const | ||
{ | ||||
// scatter chart | ||||
QChart *chart = new QChart(); | ||||
Michal Klocek
|
r748 | chart->setTitle("Scatter chart"); | ||
sauimone
|
r786 | QString name("Series "); | ||
int nameIndex = 0; | ||||
Michal Klocek
|
r748 | foreach (DataList list, m_dataTable) { | ||
QScatterSeries *series = new QScatterSeries(chart); | ||||
foreach (Data data, list) | ||||
Jani Honkonen
|
r796 | series->append(data.first); | ||
sauimone
|
r786 | series->setName(name + QString::number(nameIndex)); | ||
nameIndex++; | ||||
Michal Klocek
|
r748 | chart->addSeries(series); | ||
} | ||||
Michal Klocek
|
r1588 | chart->createDefaultAxes(); | ||
Michal Klocek
|
r748 | return chart; | ||
} | ||||
void ThemeWidget::updateUI() | ||||
{ | ||||
QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); | ||||
if (m_charts.at(0)->chart()->theme() != theme) { | ||||
foreach (QChartView *chartView, m_charts) | ||||
chartView->chart()->setTheme(theme); | ||||
QPalette pal = window()->palette(); | ||||
if (theme == QChart::ChartThemeLight) { | ||||
pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); | ||||
pal.setColor(QPalette::WindowText, QRgb(0x404044)); | ||||
Tero Ahola
|
r757 | } else if (theme == QChart::ChartThemeDark) { | ||
Michal Klocek
|
r748 | pal.setColor(QPalette::Window, QRgb(0x121218)); | ||
pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); | ||||
Tero Ahola
|
r757 | } else if (theme == QChart::ChartThemeBlueCerulean) { | ||
Michal Klocek
|
r748 | pal.setColor(QPalette::Window, QRgb(0x40434a)); | ||
pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); | ||||
Tero Ahola
|
r757 | } else if (theme == QChart::ChartThemeBrownSand) { | ||
Michal Klocek
|
r748 | pal.setColor(QPalette::Window, QRgb(0x9e8965)); | ||
pal.setColor(QPalette::WindowText, QRgb(0x404044)); | ||||
Tero Ahola
|
r757 | } else if (theme == QChart::ChartThemeBlueNcs) { | ||
Michal Klocek
|
r748 | pal.setColor(QPalette::Window, QRgb(0x018bba)); | ||
pal.setColor(QPalette::WindowText, QRgb(0x404044)); | ||||
Tero Ahola
|
r757 | } else if (theme == QChart::ChartThemeHighContrast) { | ||
pal.setColor(QPalette::Window, QRgb(0xffab03)); | ||||
pal.setColor(QPalette::WindowText, QRgb(0x181818)); | ||||
} else if (theme == QChart::ChartThemeBlueIcy) { | ||||
pal.setColor(QPalette::Window, QRgb(0xcee7f0)); | ||||
pal.setColor(QPalette::WindowText, QRgb(0x404044)); | ||||
} else { | ||||
Michal Klocek
|
r748 | pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); | ||
pal.setColor(QPalette::WindowText, QRgb(0x404044)); | ||||
} | ||||
window()->setPalette(pal); | ||||
} | ||||
bool checked = m_antialiasCheckBox->isChecked(); | ||||
foreach (QChartView *chart, m_charts) | ||||
chart->setRenderHint(QPainter::Antialiasing, checked); | ||||
QChart::AnimationOptions options(m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt()); | ||||
if (m_charts.at(0)->chart()->animationOptions() != options) { | ||||
foreach (QChartView *chartView, m_charts) | ||||
chartView->chart()->setAnimationOptions(options); | ||||
} | ||||
sauimone
|
r803 | |||
Tero Ahola
|
r1357 | Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt()); | ||
Michal Klocek
|
r855 | |||
if (!alignment) { | ||||
Jani Honkonen
|
r2099 | foreach (QChartView *chartView, m_charts) | ||
Michal Klocek
|
r855 | chartView->chart()->legend()->hide(); | ||
Tero Ahola
|
r958 | } else { | ||
Tero Ahola
|
r848 | foreach (QChartView *chartView, m_charts) { | ||
Jani Honkonen
|
r907 | chartView->chart()->legend()->setAlignment(alignment); | ||
Michal Klocek
|
r855 | chartView->chart()->legend()->show(); | ||
Tero Ahola
|
r848 | } | ||
Tero Ahola
|
r958 | } | ||
Michal Klocek
|
r748 | } | ||