themewidget.cpp
377 lines
| 12.9 KiB
| text/x-c
|
CppLexer
|
r748 | /**************************************************************************** | ||
|
r830 | ** | ||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r748 | |||
#include "themewidget.h" | ||||
#include <QChartView> | ||||
#include <QPieSeries> | ||||
#include <QPieSlice> | ||||
#include <QBarSeries> | ||||
#include <QPercentBarSeries> | ||||
#include <QStackedBarSeries> | ||||
#include <QBarSet> | ||||
#include <QLineSeries> | ||||
#include <QSplineSeries> | ||||
#include <QScatterSeries> | ||||
#include <QAreaSeries> | ||||
|
r803 | #include <QLegend> | ||
|
r748 | #include <QGridLayout> | ||
#include <QFormLayout> | ||||
#include <QComboBox> | ||||
#include <QSpinBox> | ||||
#include <QCheckBox> | ||||
#include <QGroupBox> | ||||
#include <QLabel> | ||||
#include <QTime> | ||||
ThemeWidget::ThemeWidget(QWidget* parent) : | ||||
QWidget(parent), | ||||
m_listCount(3), | ||||
|
r843 | m_valueMax(10), | ||
m_valueCount(7), | ||||
|
r748 | m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)), | ||
m_themeComboBox(createThemeBox()), | ||||
|
r853 | m_antialiasCheckBox(new QCheckBox("Anti-aliasing")), | ||
|
r803 | m_animatedComboBox(createAnimationBox()), | ||
m_legendComboBox(createLegendBox()) | ||||
|
r748 | { | ||
connectSignals(); | ||||
// create layout | ||||
QGridLayout* baseLayout = new QGridLayout(); | ||||
QHBoxLayout *settingsLayout = new QHBoxLayout(); | ||||
settingsLayout->addWidget(new QLabel("Theme:")); | ||||
settingsLayout->addWidget(m_themeComboBox); | ||||
settingsLayout->addWidget(new QLabel("Animation:")); | ||||
settingsLayout->addWidget(m_animatedComboBox); | ||||
|
r855 | settingsLayout->addWidget(new QLabel("Legend:")); | ||
|
r803 | settingsLayout->addWidget(m_legendComboBox); | ||
|
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; | ||||
|
r754 | chartView = new QChartView(createScatterChart()); | ||
|
r748 | baseLayout->addWidget(chartView, 2, 2); | ||
m_charts << chartView; | ||||
setLayout(baseLayout); | ||||
|
r843 | |||
// Set defaults | ||||
m_antialiasCheckBox->setChecked(true); | ||||
|
r855 | updateUI(); | ||
|
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())); | ||||
|
r803 | connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); | ||
|
r748 | } | ||
DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const | ||||
{ | ||||
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; | ||||
|
r843 | qreal yValue(0); | ||
|
r748 | for (int j(0); j < valueCount; j++) { | ||
|
r843 | yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount; | ||
QPointF value((j + (qreal) rand() / (qreal) RAND_MAX) * ((qreal) m_valueMax / (qreal) valueCount), | ||||
yValue); | ||||
|
r945 | QString label = "Slice " + QString::number(i) + ":" + QString::number(j); | ||
|
r748 | dataList << Data(value, label); | ||
} | ||||
dataTable << dataList; | ||||
} | ||||
return dataTable; | ||||
} | ||||
QComboBox* ThemeWidget::createThemeBox() const | ||||
{ | ||||
// settings layout | ||||
QComboBox* themeComboBox = new QComboBox(); | ||||
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); | ||||
|
r757 | themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast); | ||
themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy); | ||||
|
r748 | return themeComboBox; | ||
} | ||||
QComboBox* ThemeWidget::createAnimationBox() const | ||||
{ | ||||
// settings layout | ||||
QComboBox* animationComboBox = new QComboBox(); | ||||
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; | ||||
} | ||||
|
r803 | QComboBox* ThemeWidget::createLegendBox() const | ||
{ | ||||
QComboBox* legendComboBox = new QComboBox(); | ||||
|
r855 | legendComboBox->addItem("No Legend ", 0); | ||
|
r803 | legendComboBox->addItem("Legend Top", QLegend::AlignmentTop); | ||
legendComboBox->addItem("Legend Bottom", QLegend::AlignmentBottom); | ||||
legendComboBox->addItem("Legend Left", QLegend::AlignmentLeft); | ||||
legendComboBox->addItem("Legend Right", QLegend::AlignmentRight); | ||||
return legendComboBox; | ||||
} | ||||
|
r748 | QChart* ThemeWidget::createAreaChart() const | ||
{ | ||||
QChart *chart = new QChart(); | ||||
|
r987 | chart->axisX()->setNiceNumbersEnabled(true); | ||
chart->axisY()->setNiceNumbersEnabled(true); | ||||
|
r748 | chart->setTitle("Area chart"); | ||
|
r843 | |||
// The lower series initialized to zero values | ||||
|
r845 | QLineSeries *lowerSeries = 0; | ||
|
r958 | QString name("Series "); | ||
int nameIndex = 0; | ||||
|
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); | ||||
|
r1057 | if (lowerSeries){ | ||
const QList<QPointF>& points = lowerSeries->points(); | ||||
upperSeries->append(QPointF(j, points[i].y() + data.first.y())); | ||||
}else | ||||
|
r845 | upperSeries->append(QPointF(j, data.first.y())); | ||
|
r748 | } | ||
|
r843 | QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries); | ||
area->setName(name + QString::number(nameIndex)); | ||||
nameIndex++; | ||||
chart->addSeries(area); | ||||
lowerSeries = upperSeries; | ||||
|
r748 | } | ||
|
r958 | |||
|
r748 | return chart; | ||
} | ||||
QChart* ThemeWidget::createBarChart(int valueCount) const | ||||
{ | ||||
QChart* chart = new QChart(); | ||||
|
r987 | chart->axisX()->setNiceNumbersEnabled(true); | ||
chart->axisY()->setNiceNumbersEnabled(true); | ||||
|
r748 | chart->setTitle("Bar chart"); | ||
|
r958 | |||
|
r1321 | QStringList categories; | ||
|
r843 | for (int i(0); i < valueCount; i++) | ||
categories << QString::number(i); | ||||
|
r958 | |||
|
r1321 | chart->axisX()->categories()->insert(categories); | ||
|
r1113 | QStackedBarSeries* series = new QStackedBarSeries(chart); | ||
|
r843 | for (int i(0); i < m_dataTable.count(); i++) { | ||
|
r945 | QBarSet *set = new QBarSet("Bar set " + QString::number(i)); | ||
|
r843 | foreach (Data data, m_dataTable[i]) | ||
*set << data.first.y(); | ||||
|
r1194 | series->append(set); | ||
|
r748 | } | ||
|
r843 | chart->addSeries(series); | ||
|
r958 | |||
|
r748 | return chart; | ||
} | ||||
QChart* ThemeWidget::createLineChart() const | ||||
{ | ||||
QChart* chart = new QChart(); | ||||
|
r987 | chart->axisX()->setNiceNumbersEnabled(true); | ||
chart->axisY()->setNiceNumbersEnabled(true); | ||||
|
r748 | chart->setTitle("Line chart"); | ||
|
r958 | |||
|
r786 | QString name("Series "); | ||
int nameIndex = 0; | ||||
|
r748 | foreach (DataList list, m_dataTable) { | ||
QLineSeries *series = new QLineSeries(chart); | ||||
foreach (Data data, list) | ||||
|
r796 | series->append(data.first); | ||
|
r786 | series->setName(name + QString::number(nameIndex)); | ||
nameIndex++; | ||||
|
r748 | chart->addSeries(series); | ||
} | ||||
|
r958 | |||
|
r748 | return chart; | ||
} | ||||
QChart* ThemeWidget::createPieChart() const | ||||
{ | ||||
QChart* chart = new QChart(); | ||||
chart->setTitle("Pie chart"); | ||||
|
r958 | |||
|
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]) { | ||||
|
r1206 | QPieSlice *slice = series->append(data.second, data.first.y()); | ||
|
r748 | if (data == m_dataTable[i].first()) { | ||
slice->setLabelVisible(); | ||||
slice->setExploded(); | ||||
} | ||||
} | ||||
qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count()); | ||||
series->setPieSize(pieSize); | ||||
|
r884 | series->setHorizontalPosition(hPos); | ||
series->setVerticalPosition(0.5); | ||||
|
r748 | chart->addSeries(series); | ||
} | ||||
return chart; | ||||
} | ||||
QChart* ThemeWidget::createSplineChart() const | ||||
{ // spine chart | ||||
QChart* chart = new QChart(); | ||||
|
r987 | chart->axisX()->setNiceNumbersEnabled(true); | ||
chart->axisY()->setNiceNumbersEnabled(true); | ||||
|
r748 | chart->setTitle("Spline chart"); | ||
|
r786 | QString name("Series "); | ||
int nameIndex = 0; | ||||
|
r748 | foreach (DataList list, m_dataTable) { | ||
QSplineSeries *series = new QSplineSeries(chart); | ||||
foreach (Data data, list) | ||||
|
r796 | series->append(data.first); | ||
|
r786 | series->setName(name + QString::number(nameIndex)); | ||
nameIndex++; | ||||
|
r748 | chart->addSeries(series); | ||
} | ||||
return chart; | ||||
} | ||||
QChart* ThemeWidget::createScatterChart() const | ||||
{ // scatter chart | ||||
QChart* chart = new QChart(); | ||||
|
r987 | chart->axisX()->setNiceNumbersEnabled(true); | ||
chart->axisY()->setNiceNumbersEnabled(true); | ||||
|
r748 | chart->setTitle("Scatter chart"); | ||
|
r786 | QString name("Series "); | ||
int nameIndex = 0; | ||||
|
r748 | foreach (DataList list, m_dataTable) { | ||
QScatterSeries *series = new QScatterSeries(chart); | ||||
foreach (Data data, list) | ||||
|
r796 | series->append(data.first); | ||
|
r786 | series->setName(name + QString::number(nameIndex)); | ||
nameIndex++; | ||||
|
r748 | chart->addSeries(series); | ||
} | ||||
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)); | ||||
|
r757 | } else if (theme == QChart::ChartThemeDark) { | ||
|
r748 | pal.setColor(QPalette::Window, QRgb(0x121218)); | ||
pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); | ||||
|
r757 | } else if (theme == QChart::ChartThemeBlueCerulean) { | ||
|
r748 | pal.setColor(QPalette::Window, QRgb(0x40434a)); | ||
pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); | ||||
|
r757 | } else if (theme == QChart::ChartThemeBrownSand) { | ||
|
r748 | pal.setColor(QPalette::Window, QRgb(0x9e8965)); | ||
pal.setColor(QPalette::WindowText, QRgb(0x404044)); | ||||
|
r757 | } else if (theme == QChart::ChartThemeBlueNcs) { | ||
|
r748 | pal.setColor(QPalette::Window, QRgb(0x018bba)); | ||
pal.setColor(QPalette::WindowText, QRgb(0x404044)); | ||||
|
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 { | ||||
|
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); | ||||
} | ||||
|
r803 | |||
|
r855 | QLegend::Alignments alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt()); | ||
if (!alignment) { | ||||
|
r848 | foreach (QChartView *chartView, m_charts) { | ||
|
r855 | chartView->chart()->legend()->hide(); | ||
|
r848 | } | ||
|
r958 | } else { | ||
|
r848 | foreach (QChartView *chartView, m_charts) { | ||
|
r907 | chartView->chart()->legend()->setAlignment(alignment); | ||
|
r855 | chartView->chart()->legend()->show(); | ||
|
r848 | } | ||
|
r958 | } | ||
|
r748 | } | ||