mainwidget.cpp
359 lines
| 13.9 KiB
| text/x-c
|
CppLexer
|
r844 | /**************************************************************************** | ||
** | ||||
|
r2433 | ** Copyright (C) 2013 Digia Plc | ||
|
r844 | ** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
#include "mainwidget.h" | ||||
#include "customslice.h" | ||||
#include "pentool.h" | ||||
#include "brushtool.h" | ||||
#include <QPushButton> | ||||
#include <QComboBox> | ||||
#include <QCheckBox> | ||||
|
r1486 | #include <QLineEdit> | ||
|
r844 | #include <QGroupBox> | ||
#include <QDoubleSpinBox> | ||||
#include <QFormLayout> | ||||
#include <QFontDialog> | ||||
#include <QChartView> | ||||
#include <QPieSeries> | ||||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
|
r2099 | MainWidget::MainWidget(QWidget *parent) | ||
: QWidget(parent), | ||||
|
r1838 | m_slice(0) | ||
|
r844 | { | ||
// create chart | ||||
QChart *chart = new QChart; | ||||
chart->setTitle("Piechart customization"); | ||||
chart->setAnimationOptions(QChart::AllAnimations); | ||||
// create series | ||||
m_series = new QPieSeries(); | ||||
|
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); | ||||
|
r844 | m_series->setLabelsVisible(); | ||
chart->addSeries(m_series); | ||||
|
r1009 | connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*))); | ||
|
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); | ||||
m_aaCheckBox = new QCheckBox(); | ||||
m_animationsCheckBox = new QCheckBox(); | ||||
m_animationsCheckBox->setCheckState(Qt::Checked); | ||||
|
r1009 | m_legendCheckBox = new QCheckBox(); | ||
|
r2099 | QFormLayout *chartSettingsLayout = new QFormLayout(); | ||
|
r844 | chartSettingsLayout->addRow("Theme", m_themeComboBox); | ||
chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox); | ||||
chartSettingsLayout->addRow("Animations", m_animationsCheckBox); | ||||
|
r1009 | chartSettingsLayout->addRow("Legend", m_legendCheckBox); | ||
|
r2099 | QGroupBox *chartSettings = new QGroupBox("Chart"); | ||
|
r844 | chartSettings->setLayout(chartSettingsLayout); | ||
|
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())); | ||||
|
r844 | |||
// series settings | ||||
m_hPosition = new QDoubleSpinBox(); | ||||
m_hPosition->setMinimum(0.0); | ||||
m_hPosition->setMaximum(1.0); | ||||
m_hPosition->setSingleStep(0.1); | ||||
|
r884 | m_hPosition->setValue(m_series->horizontalPosition()); | ||
|
r844 | |||
m_vPosition = new QDoubleSpinBox(); | ||||
m_vPosition->setMinimum(0.0); | ||||
m_vPosition->setMaximum(1.0); | ||||
m_vPosition->setSingleStep(0.1); | ||||
|
r884 | m_vPosition->setValue(m_series->verticalPosition()); | ||
|
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(); | ||||
|
r1207 | m_startAngle->setMinimum(-720); | ||
m_startAngle->setMaximum(720); | ||||
|
r844 | m_startAngle->setValue(m_series->pieStartAngle()); | ||
m_startAngle->setSingleStep(1); | ||||
m_endAngle = new QDoubleSpinBox(); | ||||
|
r1207 | m_endAngle->setMinimum(-720); | ||
m_endAngle->setMaximum(720); | ||||
|
r844 | m_endAngle->setValue(m_series->pieEndAngle()); | ||
m_endAngle->setSingleStep(1); | ||||
|
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()); | ||||
|
r1758 | |||
|
r892 | QPushButton *appendSlice = new QPushButton("Append slice"); | ||
|
r844 | QPushButton *insertSlice = new QPushButton("Insert slice"); | ||
|
r1486 | QPushButton *removeSlice = new QPushButton("Remove selected slice"); | ||
|
r844 | |||
|
r2099 | QFormLayout *seriesSettingsLayout = new QFormLayout(); | ||
|
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); | ||||
|
r1838 | seriesSettingsLayout->addRow("Hole size", m_holeSize); | ||
|
r892 | seriesSettingsLayout->addRow(appendSlice); | ||
|
r844 | seriesSettingsLayout->addRow(insertSlice); | ||
|
r1486 | seriesSettingsLayout->addRow(removeSlice); | ||
|
r2099 | QGroupBox *seriesSettings = new QGroupBox("Series"); | ||
|
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())); | ||||
|
r1838 | connect(m_holeSize, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); | ||
|
r892 | connect(appendSlice, SIGNAL(clicked()), this, SLOT(appendSlice())); | ||
|
r844 | connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice())); | ||
|
r1486 | connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice())); | ||
|
r844 | |||
// slice settings | ||||
|
r1486 | m_sliceName = new QLineEdit("<click a slice>"); | ||
m_sliceName->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); | ||||
|
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(); | ||||
|
r1307 | m_labelBrush = new QPushButton(); | ||
m_labelBrushTool = new BrushTool("Label brush", this); | ||||
|
r1450 | m_labelPosition = new QComboBox(this); | ||
m_labelPosition->addItem("Outside", QPieSlice::LabelOutside); | ||||
|
r1759 | m_labelPosition->addItem("Inside horizontal", QPieSlice::LabelInsideHorizontal); | ||
|
r1712 | m_labelPosition->addItem("Inside tangential", QPieSlice::LabelInsideTangential); | ||
m_labelPosition->addItem("Inside normal", QPieSlice::LabelInsideNormal); | ||||
|
r844 | |||
|
r2099 | QFormLayout *sliceSettingsLayout = new QFormLayout(); | ||
|
r1486 | sliceSettingsLayout->addRow("Label", m_sliceName); | ||
|
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); | ||||
|
r1450 | sliceSettingsLayout->addRow("Label brush", m_labelBrush); | ||
sliceSettingsLayout->addRow("Label position", m_labelPosition); | ||||
|
r844 | sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor); | ||
sliceSettingsLayout->addRow("Exploded", m_sliceExploded); | ||||
sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor); | ||||
|
r2099 | QGroupBox *sliceSettings = new QGroupBox("Selected slice"); | ||
|
r844 | sliceSettings->setLayout(sliceSettingsLayout); | ||
|
r1486 | connect(m_sliceName, SIGNAL(textChanged(QString)), this, SLOT(updateSliceSettings())); | ||
|
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())); | ||||
|
r1307 | connect(m_labelBrush, SIGNAL(clicked()), m_labelBrushTool, SLOT(show())); | ||
connect(m_labelBrushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings())); | ||||
|
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())); | ||||
|
r1450 | connect(m_labelPosition, SIGNAL(currentIndexChanged(int)), this, SLOT(updateSliceSettings())); | ||
|
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(); | ||||
|
r2099 | QGridLayout *baseLayout = new QGridLayout(); | ||
|
r844 | baseLayout->addLayout(settingsLayout, 0, 0); | ||
baseLayout->addWidget(m_chartView, 0, 1); | ||||
setLayout(baseLayout); | ||||
updateSerieSettings(); | ||||
|
r1644 | updateChartSettings(); | ||
|
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); | ||||
|
r1009 | |||
if (m_legendCheckBox->checkState() == Qt::Checked) | ||||
m_chartView->chart()->legend()->show(); | ||||
else | ||||
m_chartView->chart()->legend()->hide(); | ||||
|
r844 | } | ||
void MainWidget::updateSerieSettings() | ||||
{ | ||||
|
r884 | m_series->setHorizontalPosition(m_hPosition->value()); | ||
m_series->setVerticalPosition(m_vPosition->value()); | ||||
|
r844 | m_series->setPieSize(m_sizeFactor->value()); | ||
|
r1838 | m_holeSize->setMaximum(m_sizeFactor->value()); | ||
|
r844 | m_series->setPieStartAngle(m_startAngle->value()); | ||
m_series->setPieEndAngle(m_endAngle->value()); | ||||
|
r1838 | m_series->setHoleSize(m_holeSize->value()); | ||
|
r844 | } | ||
void MainWidget::updateSliceSettings() | ||||
{ | ||||
if (!m_slice) | ||||
return; | ||||
|
r1486 | m_slice->setLabel(m_sliceName->text()); | ||
|
r844 | m_slice->setValue(m_sliceValue->value()); | ||
m_slice->setPen(m_penTool->pen()); | ||||
m_slice->setBrush(m_brushTool->brush()); | ||||
|
r1307 | m_slice->setLabelBrush(m_labelBrushTool->brush()); | ||
|
r844 | m_slice->setLabelVisible(m_sliceLabelVisible->isChecked()); | ||
m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value()); | ||||
|
r1450 | m_slice->setLabelPosition((QPieSlice::LabelPosition)m_labelPosition->currentIndex()); // assumes that index is in sync with the enum | ||
|
r844 | |||
m_slice->setExploded(m_sliceExploded->isChecked()); | ||||
m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value()); | ||||
} | ||||
|
r2099 | void MainWidget::handleSliceClicked(QPieSlice *slice) | ||
|
r844 | { | ||
|
r2099 | m_slice = static_cast<CustomSlice *>(slice); | ||
|
r844 | |||
// name | ||||
|
r1486 | m_sliceName->blockSignals(true); | ||
|
r844 | m_sliceName->setText(slice->label()); | ||
|
r1486 | m_sliceName->blockSignals(false); | ||
|
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 | ||||
|
r1307 | m_labelBrush->setText(BrushTool::name(m_slice->labelBrush())); | ||
m_labelBrushTool->setBrush(m_slice->labelBrush()); | ||||
|
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); | ||||
|
r1450 | m_labelPosition->blockSignals(true); | ||
m_labelPosition->setCurrentIndex(slice->labelPosition()); // assumes that index is in sync with the enum | ||||
m_labelPosition->blockSignals(false); | ||||
|
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()); | ||||
} | ||||
|
r892 | void MainWidget::appendSlice() | ||
|
r844 | { | ||
|
r2099 | *m_series << new CustomSlice("Slice " + QString::number(m_series->count() + 1), 10.0); | ||
|
r844 | } | ||
void MainWidget::insertSlice() | ||||
{ | ||||
if (!m_slice) | ||||
return; | ||||
int i = m_series->slices().indexOf(m_slice); | ||||
|
r2099 | m_series->insert(i, new CustomSlice("Slice " + QString::number(m_series->count() + 1), 10.0)); | ||
|
r844 | } | ||
void MainWidget::removeSlice() | ||||
{ | ||||
if (!m_slice) | ||||
return; | ||||
|
r892 | m_sliceName->setText("<click a slice>"); | ||
|
r844 | m_series->remove(m_slice); | ||
m_slice = 0; | ||||
} | ||||
#include "moc_mainwidget.cpp" | ||||