mainwidget.cpp
358 lines
| 14.0 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r844 | /**************************************************************************** | ||
** | ||||
Titta Heikkala
|
r2688 | ** Copyright (C) 2014 Digia Plc | ||
Jani Honkonen
|
r844 | ** All rights reserved. | ||
Titta Heikkala
|
r2740 | ** For any questions to Digia, please use contact form at http://qt.io | ||
Jani Honkonen
|
r844 | ** | ||
Titta Heikkala
|
r2740 | ** This file is part of the Qt Charts module. | ||
Jani Honkonen
|
r844 | ** | ||
Titta Heikkala
|
r2740 | ** Licensees holding valid commercial license for Qt may use this file in | ||
** accordance with the Qt License Agreement provided with the Software | ||||
** or, alternatively, in accordance with the terms contained in a written | ||||
** agreement between you and Digia. | ||||
Jani Honkonen
|
r844 | ** | ||
** If you have questions regarding the use of this file, please use | ||||
Titta Heikkala
|
r2740 | ** contact form at http://qt.io | ||
Jani Honkonen
|
r844 | ** | ||
****************************************************************************/ | ||||
#include "mainwidget.h" | ||||
#include "customslice.h" | ||||
#include "pentool.h" | ||||
#include "brushtool.h" | ||||
Titta Heikkala
|
r2714 | #include <QtWidgets/QPushButton> | ||
#include <QtWidgets/QComboBox> | ||||
#include <QtWidgets/QCheckBox> | ||||
#include <QtWidgets/QLineEdit> | ||||
#include <QtWidgets/QGroupBox> | ||||
#include <QtWidgets/QDoubleSpinBox> | ||||
#include <QtWidgets/QFormLayout> | ||||
#include <QtWidgets/QFontDialog> | ||||
#include <QtCharts/QChartView> | ||||
#include <QtCharts/QPieSeries> | ||||
Jani Honkonen
|
r844 | |||
Titta Heikkala
|
r2712 | QT_CHARTS_USE_NAMESPACE | ||
Jani Honkonen
|
r844 | |||
Jani Honkonen
|
r2099 | MainWidget::MainWidget(QWidget *parent) | ||
: QWidget(parent), | ||||
Marek Rosa
|
r1838 | m_slice(0) | ||
Jani Honkonen
|
r844 | { | ||
// create chart | ||||
QChart *chart = new QChart; | ||||
chart->setTitle("Piechart customization"); | ||||
chart->setAnimationOptions(QChart::AllAnimations); | ||||
// create series | ||||
m_series = new QPieSeries(); | ||||
Jani Honkonen
|
r1206 | *m_series << new CustomSlice("Slice 1", 10.0); | ||
*m_series << new CustomSlice("Slice 2", 20.0); | ||||
*m_series << new CustomSlice("Slice 3", 30.0); | ||||
*m_series << new CustomSlice("Slice 4", 40.0); | ||||
*m_series << new CustomSlice("Slice 5", 50.0); | ||||
Jani Honkonen
|
r844 | m_series->setLabelsVisible(); | ||
chart->addSeries(m_series); | ||||
Jani Honkonen
|
r1009 | connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*))); | ||
Jani Honkonen
|
r844 | |||
// chart settings | ||||
m_themeComboBox = new QComboBox(); | ||||
m_themeComboBox->addItem("Light", QChart::ChartThemeLight); | ||||
m_themeComboBox->addItem("BlueCerulean", QChart::ChartThemeBlueCerulean); | ||||
m_themeComboBox->addItem("Dark", QChart::ChartThemeDark); | ||||
m_themeComboBox->addItem("BrownSand", QChart::ChartThemeBrownSand); | ||||
m_themeComboBox->addItem("BlueNcs", QChart::ChartThemeBlueNcs); | ||||
m_themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast); | ||||
m_themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy); | ||||
Titta Heikkala
|
r2631 | m_themeComboBox->addItem("Qt", QChart::ChartThemeQt); | ||
Jani Honkonen
|
r844 | |||
m_aaCheckBox = new QCheckBox(); | ||||
m_animationsCheckBox = new QCheckBox(); | ||||
m_animationsCheckBox->setCheckState(Qt::Checked); | ||||
Jani Honkonen
|
r1009 | m_legendCheckBox = new QCheckBox(); | ||
Jani Honkonen
|
r2099 | QFormLayout *chartSettingsLayout = new QFormLayout(); | ||
Jani Honkonen
|
r844 | chartSettingsLayout->addRow("Theme", m_themeComboBox); | ||
chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox); | ||||
chartSettingsLayout->addRow("Animations", m_animationsCheckBox); | ||||
Jani Honkonen
|
r1009 | chartSettingsLayout->addRow("Legend", m_legendCheckBox); | ||
Jani Honkonen
|
r2099 | QGroupBox *chartSettings = new QGroupBox("Chart"); | ||
Jani Honkonen
|
r844 | chartSettings->setLayout(chartSettingsLayout); | ||
Jani Honkonen
|
r2099 | connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateChartSettings())); | ||
connect(m_aaCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateChartSettings())); | ||||
connect(m_animationsCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateChartSettings())); | ||||
connect(m_legendCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateChartSettings())); | ||||
Jani Honkonen
|
r844 | |||
// series settings | ||||
m_hPosition = new QDoubleSpinBox(); | ||||
m_hPosition->setMinimum(0.0); | ||||
m_hPosition->setMaximum(1.0); | ||||
m_hPosition->setSingleStep(0.1); | ||||
Tero Ahola
|
r884 | m_hPosition->setValue(m_series->horizontalPosition()); | ||
Jani Honkonen
|
r844 | |||
m_vPosition = new QDoubleSpinBox(); | ||||
m_vPosition->setMinimum(0.0); | ||||
m_vPosition->setMaximum(1.0); | ||||
m_vPosition->setSingleStep(0.1); | ||||
Tero Ahola
|
r884 | m_vPosition->setValue(m_series->verticalPosition()); | ||
Jani Honkonen
|
r844 | |||
m_sizeFactor = new QDoubleSpinBox(); | ||||
m_sizeFactor->setMinimum(0.0); | ||||
m_sizeFactor->setMaximum(1.0); | ||||
m_sizeFactor->setSingleStep(0.1); | ||||
m_sizeFactor->setValue(m_series->pieSize()); | ||||
m_startAngle = new QDoubleSpinBox(); | ||||
Jani Honkonen
|
r1207 | m_startAngle->setMinimum(-720); | ||
m_startAngle->setMaximum(720); | ||||
Jani Honkonen
|
r844 | m_startAngle->setValue(m_series->pieStartAngle()); | ||
m_startAngle->setSingleStep(1); | ||||
m_endAngle = new QDoubleSpinBox(); | ||||
Jani Honkonen
|
r1207 | m_endAngle->setMinimum(-720); | ||
m_endAngle->setMaximum(720); | ||||
Jani Honkonen
|
r844 | m_endAngle->setValue(m_series->pieEndAngle()); | ||
m_endAngle->setSingleStep(1); | ||||
Marek Rosa
|
r1838 | m_holeSize = new QDoubleSpinBox(); | ||
m_holeSize->setMinimum(0.0); | ||||
m_holeSize->setMaximum(1.0); | ||||
m_holeSize->setSingleStep(0.1); | ||||
m_holeSize->setValue(m_series->holeSize()); | ||||
Jani Honkonen
|
r1758 | |||
Jani Honkonen
|
r892 | QPushButton *appendSlice = new QPushButton("Append slice"); | ||
Jani Honkonen
|
r844 | QPushButton *insertSlice = new QPushButton("Insert slice"); | ||
Jani Honkonen
|
r1486 | QPushButton *removeSlice = new QPushButton("Remove selected slice"); | ||
Jani Honkonen
|
r844 | |||
Jani Honkonen
|
r2099 | QFormLayout *seriesSettingsLayout = new QFormLayout(); | ||
Jani Honkonen
|
r844 | seriesSettingsLayout->addRow("Horizontal position", m_hPosition); | ||
seriesSettingsLayout->addRow("Vertical position", m_vPosition); | ||||
seriesSettingsLayout->addRow("Size factor", m_sizeFactor); | ||||
seriesSettingsLayout->addRow("Start angle", m_startAngle); | ||||
seriesSettingsLayout->addRow("End angle", m_endAngle); | ||||
Marek Rosa
|
r1838 | seriesSettingsLayout->addRow("Hole size", m_holeSize); | ||
Jani Honkonen
|
r892 | seriesSettingsLayout->addRow(appendSlice); | ||
Jani Honkonen
|
r844 | seriesSettingsLayout->addRow(insertSlice); | ||
Jani Honkonen
|
r1486 | seriesSettingsLayout->addRow(removeSlice); | ||
Jani Honkonen
|
r2099 | QGroupBox *seriesSettings = new QGroupBox("Series"); | ||
Jani Honkonen
|
r844 | seriesSettings->setLayout(seriesSettingsLayout); | ||
connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); | ||||
connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); | ||||
connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); | ||||
connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); | ||||
connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); | ||||
Marek Rosa
|
r1838 | connect(m_holeSize, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); | ||
Jani Honkonen
|
r892 | connect(appendSlice, SIGNAL(clicked()), this, SLOT(appendSlice())); | ||
Jani Honkonen
|
r844 | connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice())); | ||
Jani Honkonen
|
r1486 | connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice())); | ||
Jani Honkonen
|
r844 | |||
// slice settings | ||||
Jani Honkonen
|
r1486 | m_sliceName = new QLineEdit("<click a slice>"); | ||
m_sliceName->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); | ||||
Jani Honkonen
|
r844 | m_sliceValue = new QDoubleSpinBox(); | ||
m_sliceValue->setMaximum(1000); | ||||
m_sliceLabelVisible = new QCheckBox(); | ||||
m_sliceLabelArmFactor = new QDoubleSpinBox(); | ||||
m_sliceLabelArmFactor->setSingleStep(0.01); | ||||
m_sliceExploded = new QCheckBox(); | ||||
m_sliceExplodedFactor = new QDoubleSpinBox(); | ||||
m_sliceExplodedFactor->setSingleStep(0.01); | ||||
m_pen = new QPushButton(); | ||||
m_penTool = new PenTool("Slice pen", this); | ||||
m_brush = new QPushButton(); | ||||
m_brushTool = new BrushTool("Slice brush", this); | ||||
m_font = new QPushButton(); | ||||
Tero Ahola
|
r1307 | m_labelBrush = new QPushButton(); | ||
m_labelBrushTool = new BrushTool("Label brush", this); | ||||
Jani Honkonen
|
r1450 | m_labelPosition = new QComboBox(this); | ||
m_labelPosition->addItem("Outside", QPieSlice::LabelOutside); | ||||
Jani Honkonen
|
r1759 | m_labelPosition->addItem("Inside horizontal", QPieSlice::LabelInsideHorizontal); | ||
Marek Rosa
|
r1712 | m_labelPosition->addItem("Inside tangential", QPieSlice::LabelInsideTangential); | ||
m_labelPosition->addItem("Inside normal", QPieSlice::LabelInsideNormal); | ||||
Jani Honkonen
|
r844 | |||
Jani Honkonen
|
r2099 | QFormLayout *sliceSettingsLayout = new QFormLayout(); | ||
Jani Honkonen
|
r1486 | sliceSettingsLayout->addRow("Label", m_sliceName); | ||
Jani Honkonen
|
r844 | sliceSettingsLayout->addRow("Value", m_sliceValue); | ||
sliceSettingsLayout->addRow("Pen", m_pen); | ||||
sliceSettingsLayout->addRow("Brush", m_brush); | ||||
sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible); | ||||
sliceSettingsLayout->addRow("Label font", m_font); | ||||
Jani Honkonen
|
r1450 | sliceSettingsLayout->addRow("Label brush", m_labelBrush); | ||
sliceSettingsLayout->addRow("Label position", m_labelPosition); | ||||
Jani Honkonen
|
r844 | sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor); | ||
sliceSettingsLayout->addRow("Exploded", m_sliceExploded); | ||||
sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor); | ||||
Jani Honkonen
|
r2099 | QGroupBox *sliceSettings = new QGroupBox("Selected slice"); | ||
Jani Honkonen
|
r844 | sliceSettings->setLayout(sliceSettingsLayout); | ||
Jani Honkonen
|
r1486 | connect(m_sliceName, SIGNAL(textChanged(QString)), this, SLOT(updateSliceSettings())); | ||
Jani Honkonen
|
r844 | connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); | ||
connect(m_pen, SIGNAL(clicked()), m_penTool, SLOT(show())); | ||||
connect(m_penTool, SIGNAL(changed()), this, SLOT(updateSliceSettings())); | ||||
connect(m_brush, SIGNAL(clicked()), m_brushTool, SLOT(show())); | ||||
connect(m_brushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings())); | ||||
connect(m_font, SIGNAL(clicked()), this, SLOT(showFontDialog())); | ||||
Tero Ahola
|
r1307 | connect(m_labelBrush, SIGNAL(clicked()), m_labelBrushTool, SLOT(show())); | ||
connect(m_labelBrushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings())); | ||||
Jani Honkonen
|
r844 | connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings())); | ||
connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings())); | ||||
connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); | ||||
connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings())); | ||||
connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); | ||||
Jani Honkonen
|
r1450 | connect(m_labelPosition, SIGNAL(currentIndexChanged(int)), this, SLOT(updateSliceSettings())); | ||
Jani Honkonen
|
r844 | |||
// create chart view | ||||
m_chartView = new QChartView(chart); | ||||
// create main layout | ||||
QVBoxLayout *settingsLayout = new QVBoxLayout(); | ||||
settingsLayout->addWidget(chartSettings); | ||||
settingsLayout->addWidget(seriesSettings); | ||||
settingsLayout->addWidget(sliceSettings); | ||||
settingsLayout->addStretch(); | ||||
Jani Honkonen
|
r2099 | QGridLayout *baseLayout = new QGridLayout(); | ||
Jani Honkonen
|
r844 | baseLayout->addLayout(settingsLayout, 0, 0); | ||
baseLayout->addWidget(m_chartView, 0, 1); | ||||
setLayout(baseLayout); | ||||
updateSerieSettings(); | ||||
Michal Klocek
|
r1644 | updateChartSettings(); | ||
Jani Honkonen
|
r844 | } | ||
void MainWidget::updateChartSettings() | ||||
{ | ||||
QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); | ||||
m_chartView->chart()->setTheme(theme); | ||||
m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked()); | ||||
if (m_animationsCheckBox->checkState() == Qt::Checked) | ||||
m_chartView->chart()->setAnimationOptions(QChart::AllAnimations); | ||||
else | ||||
m_chartView->chart()->setAnimationOptions(QChart::NoAnimation); | ||||
Jani Honkonen
|
r1009 | |||
if (m_legendCheckBox->checkState() == Qt::Checked) | ||||
m_chartView->chart()->legend()->show(); | ||||
else | ||||
m_chartView->chart()->legend()->hide(); | ||||
Jani Honkonen
|
r844 | } | ||
void MainWidget::updateSerieSettings() | ||||
{ | ||||
Tero Ahola
|
r884 | m_series->setHorizontalPosition(m_hPosition->value()); | ||
m_series->setVerticalPosition(m_vPosition->value()); | ||||
Jani Honkonen
|
r844 | m_series->setPieSize(m_sizeFactor->value()); | ||
Marek Rosa
|
r1838 | m_holeSize->setMaximum(m_sizeFactor->value()); | ||
Jani Honkonen
|
r844 | m_series->setPieStartAngle(m_startAngle->value()); | ||
m_series->setPieEndAngle(m_endAngle->value()); | ||||
Marek Rosa
|
r1838 | m_series->setHoleSize(m_holeSize->value()); | ||
Jani Honkonen
|
r844 | } | ||
void MainWidget::updateSliceSettings() | ||||
{ | ||||
if (!m_slice) | ||||
return; | ||||
Jani Honkonen
|
r1486 | m_slice->setLabel(m_sliceName->text()); | ||
Jani Honkonen
|
r844 | m_slice->setValue(m_sliceValue->value()); | ||
m_slice->setPen(m_penTool->pen()); | ||||
m_slice->setBrush(m_brushTool->brush()); | ||||
Tero Ahola
|
r1307 | m_slice->setLabelBrush(m_labelBrushTool->brush()); | ||
Jani Honkonen
|
r844 | m_slice->setLabelVisible(m_sliceLabelVisible->isChecked()); | ||
m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value()); | ||||
Jani Honkonen
|
r1450 | m_slice->setLabelPosition((QPieSlice::LabelPosition)m_labelPosition->currentIndex()); // assumes that index is in sync with the enum | ||
Jani Honkonen
|
r844 | |||
m_slice->setExploded(m_sliceExploded->isChecked()); | ||||
m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value()); | ||||
} | ||||
Jani Honkonen
|
r2099 | void MainWidget::handleSliceClicked(QPieSlice *slice) | ||
Jani Honkonen
|
r844 | { | ||
Jani Honkonen
|
r2099 | m_slice = static_cast<CustomSlice *>(slice); | ||
Jani Honkonen
|
r844 | |||
// name | ||||
Jani Honkonen
|
r1486 | m_sliceName->blockSignals(true); | ||
Jani Honkonen
|
r844 | m_sliceName->setText(slice->label()); | ||
Jani Honkonen
|
r1486 | m_sliceName->blockSignals(false); | ||
Jani Honkonen
|
r844 | |||
// value | ||||
m_sliceValue->blockSignals(true); | ||||
m_sliceValue->setValue(slice->value()); | ||||
m_sliceValue->blockSignals(false); | ||||
// pen | ||||
m_pen->setText(PenTool::name(m_slice->pen())); | ||||
m_penTool->setPen(m_slice->pen()); | ||||
// brush | ||||
m_brush->setText(m_slice->originalBrush().color().name()); | ||||
m_brushTool->setBrush(m_slice->originalBrush()); | ||||
// label | ||||
Tero Ahola
|
r1307 | m_labelBrush->setText(BrushTool::name(m_slice->labelBrush())); | ||
m_labelBrushTool->setBrush(m_slice->labelBrush()); | ||||
Jani Honkonen
|
r844 | m_font->setText(slice->labelFont().toString()); | ||
m_sliceLabelVisible->blockSignals(true); | ||||
m_sliceLabelVisible->setChecked(slice->isLabelVisible()); | ||||
m_sliceLabelVisible->blockSignals(false); | ||||
m_sliceLabelArmFactor->blockSignals(true); | ||||
m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor()); | ||||
m_sliceLabelArmFactor->blockSignals(false); | ||||
Jani Honkonen
|
r1450 | m_labelPosition->blockSignals(true); | ||
m_labelPosition->setCurrentIndex(slice->labelPosition()); // assumes that index is in sync with the enum | ||||
m_labelPosition->blockSignals(false); | ||||
Jani Honkonen
|
r844 | |||
// exploded | ||||
m_sliceExploded->blockSignals(true); | ||||
m_sliceExploded->setChecked(slice->isExploded()); | ||||
m_sliceExploded->blockSignals(false); | ||||
m_sliceExplodedFactor->blockSignals(true); | ||||
m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor()); | ||||
m_sliceExplodedFactor->blockSignals(false); | ||||
} | ||||
void MainWidget::showFontDialog() | ||||
{ | ||||
if (!m_slice) | ||||
return; | ||||
QFontDialog dialog(m_slice->labelFont()); | ||||
dialog.show(); | ||||
dialog.exec(); | ||||
m_slice->setLabelFont(dialog.currentFont()); | ||||
m_font->setText(dialog.currentFont().toString()); | ||||
} | ||||
Jani Honkonen
|
r892 | void MainWidget::appendSlice() | ||
Jani Honkonen
|
r844 | { | ||
Jani Honkonen
|
r2099 | *m_series << new CustomSlice("Slice " + QString::number(m_series->count() + 1), 10.0); | ||
Jani Honkonen
|
r844 | } | ||
void MainWidget::insertSlice() | ||||
{ | ||||
if (!m_slice) | ||||
return; | ||||
int i = m_series->slices().indexOf(m_slice); | ||||
Jani Honkonen
|
r2099 | m_series->insert(i, new CustomSlice("Slice " + QString::number(m_series->count() + 1), 10.0)); | ||
Jani Honkonen
|
r844 | } | ||
void MainWidget::removeSlice() | ||||
{ | ||||
if (!m_slice) | ||||
return; | ||||
Jani Honkonen
|
r892 | m_sliceName->setText("<click a slice>"); | ||
Jani Honkonen
|
r844 | m_series->remove(m_slice); | ||
m_slice = 0; | ||||
} | ||||
#include "moc_mainwidget.cpp" | ||||