mainwidget.cpp
461 lines
| 14.5 KiB
| text/x-c
|
CppLexer
|
r2548 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2013 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
|
r2574 | ** This file is part of the Qt Enterprise Charts Add-on. | ||
|
r2548 | ** | ||
** $QT_BEGIN_LICENSE$ | ||||
|
r2574 | ** Licensees holding valid Qt Enterprise licenses may use this file in | ||
** accordance with the Qt Enterprise License Agreement provided with the | ||||
|
r2548 | ** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
#include "mainwidget.h" | ||||
#include "customtablemodel.h" | ||||
|
r2566 | #include "pentool.h" | ||
|
r2548 | #include <QVBoxPlotModelMapper> | ||
#include <QTableView> | ||||
#include <QHeaderView> | ||||
#include <QChartView> | ||||
#include <QBoxPlotSeries> | ||||
#include <QBoxSet> | ||||
#include <QLegend> | ||||
#include <QBarCategoryAxis> | ||||
#include <QBrush> | ||||
#include <QColor> | ||||
#include <QPushButton> | ||||
#include <QComboBox> | ||||
#include <QSpinBox> | ||||
#include <QCheckBox> | ||||
#include <QGridLayout> | ||||
#include <QHBoxLayout> | ||||
#include <QLabel> | ||||
#include <QSpacerItem> | ||||
#include <QMessageBox> | ||||
#include <cmath> | ||||
#include <QDebug> | ||||
#include <QStandardItemModel> | ||||
#include <QBarCategoryAxis> | ||||
|
r2554 | #include <QLogValueAxis> | ||
|
r2548 | |||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
|
r2562 | static const QString allCategories[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; | ||
static const int maxCategories = 12; | ||||
|
r2548 | |||
MainWidget::MainWidget(QWidget *parent) : | ||||
QWidget(parent), | ||||
m_chart(0), | ||||
|
r2554 | m_axis(0), | ||
|
r2562 | m_rowPos(0), | ||
m_seriesCount(0) | ||||
|
r2548 | { | ||
m_chart = new QChart(); | ||||
|
r2566 | m_penTool = new PenTool("Whiskers pen", this); | ||
|
r2548 | // Grid layout for the controls for configuring the chart widget | ||
QGridLayout *grid = new QGridLayout(); | ||||
// Create add a series button | ||||
QPushButton *addSeriesButton = new QPushButton("Add a series"); | ||||
connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); | ||||
|
r2562 | grid->addWidget(addSeriesButton, m_rowPos++, 1); | ||
|
r2548 | |||
// Create remove a series button | ||||
QPushButton *removeSeriesButton = new QPushButton("Remove a series"); | ||||
connect(removeSeriesButton, SIGNAL(clicked()), this, SLOT(removeSeries())); | ||||
|
r2562 | grid->addWidget(removeSeriesButton, m_rowPos++, 1); | ||
|
r2548 | |||
// Create add a single box button | ||||
QPushButton *addBoxButton = new QPushButton("Add a box"); | ||||
connect(addBoxButton, SIGNAL(clicked()), this, SLOT(addBox())); | ||||
|
r2562 | grid->addWidget(addBoxButton, m_rowPos++, 1); | ||
|
r2548 | |||
// Create insert a box button | ||||
QPushButton *insertBoxButton = new QPushButton("Insert a box"); | ||||
connect(insertBoxButton, SIGNAL(clicked()), this, SLOT(insertBox())); | ||||
|
r2562 | grid->addWidget(insertBoxButton, m_rowPos++, 1); | ||
|
r2548 | |||
// Create add a single box button | ||||
QPushButton *removeBoxButton = new QPushButton("Remove a box"); | ||||
connect(removeBoxButton, SIGNAL(clicked()), this, SLOT(removeBox())); | ||||
|
r2562 | grid->addWidget(removeBoxButton, m_rowPos++, 1); | ||
|
r2548 | |||
// Create clear button | ||||
QPushButton *clearButton = new QPushButton("Clear"); | ||||
connect(clearButton, SIGNAL(clicked()), this, SLOT(clear())); | ||||
|
r2562 | grid->addWidget(clearButton, m_rowPos++, 1); | ||
|
r2548 | |||
// Create clear button | ||||
QPushButton *clearBoxButton = new QPushButton("ClearBox"); | ||||
connect(clearBoxButton, SIGNAL(clicked()), this, SLOT(clearBox())); | ||||
|
r2562 | grid->addWidget(clearBoxButton, m_rowPos++, 1); | ||
|
r2548 | |||
// Create set brush button | ||||
QPushButton *setBrushButton = new QPushButton("Set brush"); | ||||
connect(setBrushButton, SIGNAL(clicked()), this, SLOT(setBrush())); | ||||
|
r2562 | grid->addWidget(setBrushButton, m_rowPos++, 1); | ||
|
r2548 | |||
|
r2566 | // Create set whiskers pen button | ||
QPushButton *setWhiskersButton = new QPushButton("Whiskers pen"); | ||||
connect(setWhiskersButton, SIGNAL(clicked()), m_penTool, SLOT(show())); | ||||
connect(m_penTool, SIGNAL(changed()), this, SLOT(changePen())); | ||||
grid->addWidget(setWhiskersButton, m_rowPos++, 1); | ||||
|
r2548 | initThemeCombo(grid); | ||
initCheckboxes(grid); | ||||
|
r2559 | m_model = new CustomTableModel(); | ||
|
r2548 | QTableView *tableView = new QTableView; | ||
tableView->setModel(m_model); | ||||
tableView->setMaximumWidth(200); | ||||
|
r2562 | grid->addWidget(tableView, m_rowPos++, 0, 3, 2, Qt::AlignLeft); | ||
|
r2548 | #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) | ||
tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); | ||||
tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); | ||||
#else | ||||
tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch); | ||||
tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch); | ||||
#endif | ||||
// 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); | ||||
// Create chart view with the chart | ||||
m_chartView = new QChartView(m_chart, this); | ||||
|
r2566 | // As a default antialiasing is off | ||
m_chartView->setRenderHint(QPainter::Antialiasing, false); | ||||
|
r2548 | // Another grid layout as a main layout | ||
QGridLayout *mainLayout = new QGridLayout(); | ||||
mainLayout->addLayout(grid, 0, 0); | ||||
mainLayout->addWidget(m_chartView, 0, 1, 3, 1); | ||||
setLayout(mainLayout); | ||||
legendToggled(false); | ||||
animationToggled(false); | ||||
} | ||||
// Combo box for selecting theme | ||||
void MainWidget::initThemeCombo(QGridLayout *grid) | ||||
{ | ||||
QComboBox *chartTheme = new QComboBox(); | ||||
chartTheme->addItem("Default"); | ||||
chartTheme->addItem("Light"); | ||||
chartTheme->addItem("Blue Cerulean"); | ||||
chartTheme->addItem("Dark"); | ||||
chartTheme->addItem("Brown Sand"); | ||||
chartTheme->addItem("Blue NCS"); | ||||
chartTheme->addItem("High Contrast"); | ||||
chartTheme->addItem("Blue Icy"); | ||||
connect(chartTheme, SIGNAL(currentIndexChanged(int)), | ||||
this, SLOT(changeChartTheme(int))); | ||||
|
r2562 | grid->addWidget(new QLabel("Chart theme:"), m_rowPos, 0); | ||
grid->addWidget(chartTheme, m_rowPos++, 1); | ||||
|
r2548 | } | ||
// Different check boxes for customizing chart | ||||
void MainWidget::initCheckboxes(QGridLayout *grid) | ||||
{ | ||||
QCheckBox *animationCheckBox = new QCheckBox("Animation"); | ||||
connect(animationCheckBox, SIGNAL(toggled(bool)), this, SLOT(animationToggled(bool))); | ||||
animationCheckBox->setChecked(false); | ||||
|
r2562 | grid->addWidget(animationCheckBox, m_rowPos++, 0); | ||
|
r2548 | |||
QCheckBox *legendCheckBox = new QCheckBox("Legend"); | ||||
connect(legendCheckBox, SIGNAL(toggled(bool)), this, SLOT(legendToggled(bool))); | ||||
legendCheckBox->setChecked(false); | ||||
|
r2562 | grid->addWidget(legendCheckBox, m_rowPos++, 0); | ||
|
r2548 | |||
QCheckBox *titleCheckBox = new QCheckBox("Title"); | ||||
connect(titleCheckBox, SIGNAL(toggled(bool)), this, SLOT(titleToggled(bool))); | ||||
titleCheckBox->setChecked(false); | ||||
|
r2562 | grid->addWidget(titleCheckBox, m_rowPos++, 0); | ||
|
r2548 | |||
|
r2567 | QCheckBox *antialiasingCheckBox = new QCheckBox("Antialiasing"); | ||
connect(antialiasingCheckBox, SIGNAL(toggled(bool)), this, SLOT(antialiasingToggled(bool))); | ||||
antialiasingCheckBox->setChecked(false); | ||||
grid->addWidget(antialiasingCheckBox, m_rowPos++, 0); | ||||
|
r2566 | |||
|
r2548 | QCheckBox *modelMapperCheckBox = new QCheckBox("Use model mapper"); | ||
connect(modelMapperCheckBox, SIGNAL(toggled(bool)), this, SLOT(modelMapperToggled(bool))); | ||||
modelMapperCheckBox->setChecked(false); | ||||
|
r2562 | grid->addWidget(modelMapperCheckBox, m_rowPos++, 0); | ||
|
r2548 | |||
|
r2573 | m_boxOutlined = new QCheckBox("Box outlined"); | ||
connect(m_boxOutlined, SIGNAL(toggled(bool)), this, SLOT(boxOutlineToggled(bool))); | ||||
m_boxOutlined->setChecked(true); | ||||
grid->addWidget(m_boxOutlined, m_rowPos++, 0); | ||||
|
r2548 | } | ||
|
r2562 | void MainWidget::updateAxis(int categoryCount) | ||
{ | ||||
if (!m_axis) { | ||||
m_chart->createDefaultAxes(); | ||||
m_axis = new QBarCategoryAxis(); | ||||
} | ||||
QStringList categories; | ||||
for (int i = 0; i < categoryCount; i++) | ||||
categories << allCategories[i]; | ||||
m_axis->setCategories(categories); | ||||
} | ||||
|
r2548 | void MainWidget::addSeries() | ||
{ | ||||
qDebug() << "BoxPlotTester::MainWidget::addSeries()"; | ||||
|
r2562 | if (m_seriesCount > 9) | ||
|
r2548 | return; | ||
// Initial data | ||||
QBoxSet *set0 = new QBoxSet(); | ||||
QBoxSet *set1 = new QBoxSet(); | ||||
QBoxSet *set2 = new QBoxSet(); | ||||
QBoxSet *set3 = new QBoxSet(); | ||||
QBoxSet *set4 = new QBoxSet(); | ||||
QBoxSet *set5 = new QBoxSet(); | ||||
// low bot med top upp | ||||
|
r2554 | *set0 << -1 << 2 << 4 << 13 << 15; | ||
|
r2548 | *set1 << 5 << 6 << 7.5 << 8 << 12; | ||
*set2 << 3 << 5 << 5.7 << 8 << 9; | ||||
*set3 << 5 << 6 << 6.8 << 7 << 8; | ||||
*set4 << 4 << 5 << 5.2 << 6 << 7; | ||||
*set5 << 4 << 7 << 8.2 << 9 << 10; | ||||
|
r2562 | m_series[m_seriesCount] = new QBoxPlotSeries(); | ||
m_series[m_seriesCount]->append(set0); | ||||
m_series[m_seriesCount]->append(set1); | ||||
m_series[m_seriesCount]->append(set2); | ||||
m_series[m_seriesCount]->append(set3); | ||||
m_series[m_seriesCount]->append(set4); | ||||
m_series[m_seriesCount]->append(set5); | ||||
m_series[m_seriesCount]->setName("Box & Whiskers"); | ||||
connect(m_series[m_seriesCount], SIGNAL(clicked(QBoxSet*)), this, SLOT(boxClicked(QBoxSet*))); | ||||
connect(m_series[m_seriesCount], SIGNAL(hovered(bool, QBoxSet*)), this, SLOT(boxHovered(bool, QBoxSet*))); | ||||
|
r2548 | connect(set1, SIGNAL(clicked()), this, SLOT(singleBoxClicked())); | ||
connect(set2, SIGNAL(hovered(bool)), this, SLOT(singleBoxHovered(bool))); | ||||
|
r2573 | m_series[m_seriesCount]->setBoxOutlineVisible(m_boxOutlined->checkState()); | ||
|
r2562 | m_chart->addSeries(m_series[m_seriesCount]); | ||
|
r2548 | |||
|
r2562 | updateAxis(m_series[0]->count()); | ||
m_chart->setAxisX(m_axis, m_series[m_seriesCount]); | ||||
|
r2548 | |||
|
r2562 | m_seriesCount++; | ||
|
r2548 | } | ||
void MainWidget::removeSeries() | ||||
{ | ||||
qDebug() << "BoxPlotTester::MainWidget::removeSeries()"; | ||||
|
r2562 | if (m_seriesCount > 0) { | ||
m_seriesCount--; | ||||
m_chart->removeSeries(m_series[m_seriesCount]); | ||||
delete m_series[m_seriesCount]; | ||||
m_series[m_seriesCount] = 0; | ||||
|
r2548 | } else { | ||
qDebug() << "Create a series first"; | ||||
} | ||||
} | ||||
void MainWidget::addBox() | ||||
{ | ||||
qDebug() << "BoxPlotTester::MainWidget::addBox()"; | ||||
|
r2562 | if (m_seriesCount > 0 && m_series[0]->count() < maxCategories) { | ||
|
r2548 | QBoxSet *newSet = new QBoxSet(); | ||
newSet->setValue(QBoxSet::LowerExtreme, 5.0); | ||||
newSet->setValue(QBoxSet::LowerQuartile, 6.0); | ||||
newSet->setValue(QBoxSet::Median, 6.8); | ||||
newSet->setValue(QBoxSet::UpperQuartile, 7.0); | ||||
newSet->setValue(QBoxSet::UpperExtreme, 8.0); | ||||
|
r2562 | updateAxis(m_series[0]->count() + 1); | ||
|
r2548 | |||
|
r2562 | m_series[0]->append(newSet); | ||
|
r2548 | } | ||
} | ||||
void MainWidget::insertBox() | ||||
{ | ||||
qDebug() << "BoxPlotTester::MainWidget::insertBox()"; | ||||
|
r2562 | if (m_seriesCount > 0 && m_series[0]->count() < maxCategories) { | ||
updateAxis(m_series[0]->count() + 1); | ||||
for (int i = 0; i < m_seriesCount; i++) { | ||||
QBoxSet *newSet = new QBoxSet(); | ||||
*newSet << 2 << 6 << 6.8 << 7 << 10; | ||||
|
r2548 | m_series[i]->insert(1, newSet); | ||
|
r2562 | } | ||
|
r2548 | } | ||
} | ||||
void MainWidget::removeBox() | ||||
{ | ||||
qDebug() << "BoxPlotTester::MainWidget::removeBox"; | ||||
|
r2562 | if (m_seriesCount > 0) { | ||
for (int i = 0; i < m_seriesCount; i++) { | ||||
|
r2554 | qDebug() << "m_series[i]->count() = " << m_series[i]->count(); | ||
|
r2562 | if (m_series[i]->count()) { | ||
|
r2554 | QList<QBoxSet *> sets = m_series[i]->boxSets(); | ||
|
r2562 | m_series[i]->remove(sets.at(m_series[i]->count() - 1)); | ||
|
r2554 | } | ||
|
r2548 | } | ||
|
r2562 | updateAxis(m_series[0]->count()); | ||
|
r2548 | } else { | ||
qDebug() << "Create a series first"; | ||||
} | ||||
} | ||||
void MainWidget::clear() | ||||
{ | ||||
qDebug() << "BoxPlotTester::MainWidget::clear"; | ||||
|
r2562 | if (m_seriesCount > 0) | ||
|
r2548 | m_series[0]->clear(); | ||
else | ||||
qDebug() << "Create a series first"; | ||||
} | ||||
void MainWidget::clearBox() | ||||
{ | ||||
qDebug() << "BoxPlotTester::MainWidget::clearBox"; | ||||
|
r2562 | if (m_seriesCount > 0) { | ||
|
r2548 | QList<QBoxSet *> sets = m_series[0]->boxSets(); | ||
sets.at(1)->clear(); | ||||
} else { | ||||
qDebug() << "Create a series first"; | ||||
} | ||||
} | ||||
void MainWidget::setBrush() | ||||
{ | ||||
qDebug() << "BoxPlotTester::MainWidget::setBrush"; | ||||
|
r2562 | if (m_seriesCount > 0) { | ||
|
r2548 | QList<QBoxSet *> sets = m_series[0]->boxSets(); | ||
sets.at(1)->setBrush(QBrush(QColor(Qt::yellow))); | ||||
} else { | ||||
qDebug() << "Create a series first"; | ||||
} | ||||
} | ||||
void MainWidget::animationToggled(bool enabled) | ||||
{ | ||||
qDebug() << "BoxPlotTester::Animation toggled to " << enabled; | ||||
if (enabled) | ||||
m_chart->setAnimationOptions(QChart::SeriesAnimations); | ||||
else | ||||
m_chart->setAnimationOptions(QChart::NoAnimation); | ||||
} | ||||
void MainWidget::legendToggled(bool enabled) | ||||
{ | ||||
qDebug() << "BoxPlotTester::Legend toggled to " << enabled; | ||||
m_chart->legend()->setVisible(enabled); | ||||
if (enabled) | ||||
m_chart->legend()->setAlignment(Qt::AlignBottom); | ||||
} | ||||
void MainWidget::titleToggled(bool enabled) | ||||
{ | ||||
qDebug() << "BoxPlotTester::Title toggled to " << enabled; | ||||
if (enabled) | ||||
m_chart->setTitle("Simple boxplotchart example"); | ||||
else | ||||
m_chart->setTitle(""); | ||||
} | ||||
|
r2567 | void MainWidget::antialiasingToggled(bool enabled) | ||
|
r2566 | { | ||
|
r2567 | qDebug() << "BoxPlotTester::antialiasingToggled toggled to " << enabled; | ||
|
r2566 | m_chartView->setRenderHint(QPainter::Antialiasing, enabled); | ||
} | ||||
|
r2573 | void MainWidget::boxOutlineToggled(bool visible) | ||
{ | ||||
qDebug() << "BoxPlotTester::boxOutlineToggled toggled to " << visible; | ||||
for (int i = 0; i < m_seriesCount; i++) | ||||
m_series[i]->setBoxOutlineVisible(visible); | ||||
} | ||||
|
r2548 | void MainWidget::modelMapperToggled(bool enabled) | ||
{ | ||||
if (enabled) { | ||||
|
r2562 | m_series[m_seriesCount] = new QBoxPlotSeries(); | ||
|
r2548 | |||
int first = 0; | ||||
int count = 5; | ||||
QVBoxPlotModelMapper *mapper = new QVBoxPlotModelMapper(this); | ||||
mapper->setFirstBoxSetColumn(0); | ||||
mapper->setLastBoxSetColumn(5); | ||||
mapper->setFirstRow(first); | ||||
mapper->setRowCount(count); | ||||
|
r2562 | mapper->setSeries(m_series[m_seriesCount]); | ||
|
r2548 | mapper->setModel(m_model); | ||
|
r2562 | m_chart->addSeries(m_series[m_seriesCount]); | ||
|
r2548 | |||
|
r2562 | m_seriesCount++; | ||
|
r2548 | } else { | ||
removeSeries(); | ||||
} | ||||
} | ||||
void MainWidget::changeChartTheme(int themeIndex) | ||||
{ | ||||
qDebug() << "BoxPlotTester::changeChartTheme: " << themeIndex; | ||||
if (themeIndex == 0) | ||||
m_chart->setTheme(QChart::ChartThemeLight); | ||||
else | ||||
m_chart->setTheme((QChart::ChartTheme) (themeIndex - 1)); | ||||
} | ||||
void MainWidget::boxClicked(QBoxSet *set) | ||||
{ | ||||
qDebug() << "boxClicked, median = " << set->at(QBoxSet::Median); | ||||
} | ||||
void MainWidget::boxHovered(bool state, QBoxSet *set) | ||||
{ | ||||
if (state) | ||||
qDebug() << "box median " << set->at(QBoxSet::Median) << " hover started"; | ||||
else | ||||
qDebug() << "box median " << set->at(QBoxSet::Median) << " hover ended"; | ||||
} | ||||
void MainWidget::singleBoxClicked() | ||||
{ | ||||
qDebug() << "singleBoxClicked"; | ||||
} | ||||
void MainWidget::singleBoxHovered(bool state) | ||||
{ | ||||
if (state) | ||||
qDebug() << "single box hover started"; | ||||
else | ||||
qDebug() << "single box hover ended"; | ||||
} | ||||
|
r2566 | |||
void MainWidget::changePen() | ||||
{ | ||||
qDebug() << "changePen() = " << m_penTool->pen(); | ||||
for (int i = 0; i < m_seriesCount; i++) | ||||
m_series[i]->setPen(m_penTool->pen()); | ||||
} | ||||