diff --git a/demos/piechartcustomization/main.cpp b/demos/piechartcustomization/main.cpp index ab13c65..dee429d 100644 --- a/demos/piechartcustomization/main.cpp +++ b/demos/piechartcustomization/main.cpp @@ -11,6 +11,8 @@ #include #include #include +#include +#include QTCOMMERCIALCHART_USE_NAMESPACE @@ -25,6 +27,12 @@ public: connect(this, SIGNAL(hoverLeave()), this, SLOT(handleHoverLeave())); } +public: + QBrush originalBrush() + { + return m_originalBrush; + } + public Q_SLOTS: void handleHoverEnter() @@ -53,11 +61,11 @@ public: :QWidget(parent), m_slice(0) { + // create chart m_chartView = new QChartView(); m_chartView->setChartTitle("Piechart customization"); - m_chartView->setRenderHint(QPainter::Antialiasing); - m_chartView->setChartTheme(QChart::ChartThemeIcy); + // create series m_series = new QPieSeries(); *m_series << new CustomSlice(10.0, "Slice 1"); *m_series << new CustomSlice(20.0, "Slice 2"); @@ -66,6 +74,28 @@ public: *m_series << new CustomSlice(50.0, "Slice 5"); m_chartView->addSeries(m_series); + connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*))); + + // chart settings + m_themeComboBox = new QComboBox(); + m_themeComboBox->addItem("Default", QChart::ChartThemeDefault); + m_themeComboBox->addItem("Vanilla", QChart::ChartThemeVanilla); + m_themeComboBox->addItem("Icy", QChart::ChartThemeIcy); + m_themeComboBox->addItem("Grayscale", QChart::ChartThemeGrayscale); + m_themeComboBox->addItem("Scientific", QChart::ChartThemeScientific); + + m_aaCheckBox = new QCheckBox(); + + QFormLayout* chartSettingsLayout = new QFormLayout(); + chartSettingsLayout->addRow("Theme", m_themeComboBox); + chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox); + QGroupBox* chartSettings = new QGroupBox("Chart"); + chartSettings->setLayout(chartSettingsLayout); + + connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateChartSettings())); + connect(m_aaCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings())); + + // series settings m_hPosition = new QDoubleSpinBox(); m_hPosition->setMinimum(0.0); m_hPosition->setMaximum(1.0); @@ -105,6 +135,13 @@ public: QGroupBox* seriesSettings = new QGroupBox("Series"); 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())); + + // slice settings m_sliceName = new QLabel(""); m_sliceValue = new QDoubleSpinBox(); m_sliceValue->setMaximum(1000); @@ -114,43 +151,58 @@ public: m_sliceExploded = new QCheckBox(); m_sliceExplodedFactor = new QDoubleSpinBox(); m_sliceExplodedFactor->setSingleStep(0.01); + m_penWidth = new QDoubleSpinBox(); + m_penColor = new QPushButton(); + m_brushColor = new QPushButton(); QFormLayout* sliceSettingsLayout = new QFormLayout(); sliceSettingsLayout->addRow("Selected", m_sliceName); sliceSettingsLayout->addRow("Value", m_sliceValue); + sliceSettingsLayout->addRow("Pen color", m_penColor); + sliceSettingsLayout->addRow("Pen width", m_penWidth); + sliceSettingsLayout->addRow("Brush color", m_brushColor); sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible); sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor); sliceSettingsLayout->addRow("Exploded", m_sliceExploded); sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor); + QGroupBox* sliceSettings = new QGroupBox("Slice"); sliceSettings->setLayout(sliceSettingsLayout); - QGridLayout* baseLayout = new QGridLayout(); - baseLayout->addWidget(seriesSettings, 0, 0); - baseLayout->addWidget(sliceSettings, 1, 0); - baseLayout->addWidget(m_chartView, 0, 1, 2, 1); - setLayout(baseLayout); - - 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())); - connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); + connect(m_penColor, SIGNAL(clicked()), this, SLOT(showPenColorDialog())); + connect(m_penWidth, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); + connect(m_brushColor, SIGNAL(clicked()), this, SLOT(showBrushColorDialog())); 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())); - connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*))); + // create main layout + QVBoxLayout *settingsLayout = new QVBoxLayout(); + settingsLayout->addWidget(chartSettings); + settingsLayout->addWidget(seriesSettings); + settingsLayout->addWidget(sliceSettings); + settingsLayout->addStretch(); + + QGridLayout* baseLayout = new QGridLayout(); + baseLayout->addLayout(settingsLayout, 0, 0); + baseLayout->addWidget(m_chartView, 0, 1); + setLayout(baseLayout); updateSerieSettings(); } public Q_SLOTS: + void updateChartSettings() + { + QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); + m_chartView->setChartTheme(theme); + m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked()); + } + void updateSerieSettings() { m_series->setPiePosition(m_vPosition->value(), m_hPosition->value()); @@ -165,21 +217,36 @@ public Q_SLOTS: return; m_slice->setValue(m_sliceValue->value()); + + QPen pen = m_slice->slicePen(); + pen.setWidthF(m_penWidth->value()); + m_slice->setSlicePen(pen); + m_slice->setLabelVisible(m_sliceLabelVisible->isChecked()); m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value()); + m_slice->setExploded(m_sliceExploded->isChecked()); m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value()); } void handleSliceClicked(QPieSlice* slice) { - m_slice = slice; + m_slice = static_cast(slice); + m_sliceName->setText(slice->label()); m_sliceValue->blockSignals(true); m_sliceValue->setValue(slice->value()); m_sliceValue->blockSignals(false); + m_penColor->setText(m_slice->slicePen().color().name()); + + m_sliceValue->blockSignals(true); + m_penWidth->setValue(slice->slicePen().widthF()); + m_sliceValue->blockSignals(false); + + m_brushColor->setText(m_slice->originalBrush().color().name()); + m_sliceLabelVisible->blockSignals(true); m_sliceLabelVisible->setChecked(slice->isLabelVisible()); m_sliceLabelVisible->blockSignals(false); @@ -197,10 +264,45 @@ public Q_SLOTS: m_sliceExplodedFactor->blockSignals(false); } + void showBrushColorDialog() + { + if (!m_slice) + return; + + QColorDialog dialog(m_slice->originalBrush().color()); + dialog.show(); + dialog.exec(); + + QBrush brush = m_slice->originalBrush(); + brush.setColor(dialog.currentColor()); + m_slice->setSliceBrush(brush); + + m_brushColor->setText(dialog.currentColor().name()); + } + + void showPenColorDialog() + { + if (!m_slice) + return; + + QColorDialog dialog(m_slice->slicePen().color()); + dialog.show(); + dialog.exec(); + + QPen pen = m_slice->slicePen(); + pen.setColor(dialog.currentColor()); + m_slice->setSlicePen(pen); + + m_penColor->setText(dialog.currentColor().name()); + } + private: + QComboBox *m_themeComboBox; + QCheckBox *m_aaCheckBox; + QChartView* m_chartView; QPieSeries* m_series; - QPieSlice* m_slice; + CustomSlice* m_slice; QDoubleSpinBox* m_hPosition; QDoubleSpinBox* m_vPosition; @@ -214,6 +316,9 @@ private: QDoubleSpinBox* m_sliceLabelArmFactor; QCheckBox* m_sliceExploded; QDoubleSpinBox* m_sliceExplodedFactor; + QPushButton *m_brushColor; + QPushButton *m_penColor; + QDoubleSpinBox* m_penWidth; }; int main(int argc, char *argv[])