diff --git a/demos/piechartcustomization/brushtool.cpp b/demos/piechartcustomization/brushtool.cpp new file mode 100644 index 0000000..e2bc169 --- /dev/null +++ b/demos/piechartcustomization/brushtool.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ +#include "brushtool.h" +#include +#include +#include +#include + +BrushTool::BrushTool(QString title, QWidget *parent) + :QWidget(parent) +{ + setWindowTitle(title); + setWindowFlags(Qt::Tool); + + m_colorButton = new QPushButton(); + m_styleCombo = new QComboBox(); + m_styleCombo->addItem("Nobrush", Qt::NoBrush); + m_styleCombo->addItem("Solidpattern", Qt::SolidPattern); + m_styleCombo->addItem("Dense1pattern", Qt::Dense1Pattern); + m_styleCombo->addItem("Dense2attern", Qt::Dense2Pattern); + m_styleCombo->addItem("Dense3Pattern", Qt::Dense3Pattern); + m_styleCombo->addItem("Dense4Pattern", Qt::Dense4Pattern); + m_styleCombo->addItem("Dense5Pattern", Qt::Dense5Pattern); + m_styleCombo->addItem("Dense6Pattern", Qt::Dense6Pattern); + m_styleCombo->addItem("Dense7Pattern", Qt::Dense7Pattern); + m_styleCombo->addItem("HorPattern", Qt::HorPattern); + m_styleCombo->addItem("VerPattern", Qt::VerPattern); + m_styleCombo->addItem("CrossPattern", Qt::CrossPattern); + m_styleCombo->addItem("BDiagPattern", Qt::BDiagPattern); + m_styleCombo->addItem("FDiagPattern", Qt::FDiagPattern); + m_styleCombo->addItem("DiagCrossPattern", Qt::DiagCrossPattern); + + QFormLayout *layout = new QFormLayout(); + layout->addRow("Color", m_colorButton); + layout->addRow("Style", m_styleCombo); + setLayout(layout); + + connect(m_colorButton, SIGNAL(clicked()), this, SLOT(showColorDialog())); + connect(m_styleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateStyle())); +} + +void BrushTool::setBrush(QBrush brush) +{ + m_brush = brush; + m_colorButton->setText(m_brush.color().name()); + m_styleCombo->setCurrentIndex(m_brush.style()); // index matches the enum +} + +QBrush BrushTool::brush() const +{ + return m_brush; +} + +QString BrushTool::name() +{ + return name(m_brush); +} + +QString BrushTool::name(const QBrush &brush) +{ + return brush.color().name(); +} + +void BrushTool::showColorDialog() +{ + QColorDialog dialog(m_brush.color()); + dialog.show(); + dialog.exec(); + m_brush.setColor(dialog.selectedColor()); + m_colorButton->setText(m_brush.color().name()); + emit changed(); +} + +void BrushTool::updateStyle() +{ + Qt::BrushStyle style = (Qt::BrushStyle) m_styleCombo->itemData(m_styleCombo->currentIndex()).toInt(); + if (m_brush.style() != style) { + m_brush.setStyle(style); + emit changed(); + } +} + +#include "moc_brushtool.cpp" diff --git a/demos/piechartcustomization/brushtool.h b/demos/piechartcustomization/brushtool.h new file mode 100644 index 0000000..eaa2244 --- /dev/null +++ b/demos/piechartcustomization/brushtool.h @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ +#ifndef BRUSHTOOL_H +#define BRUSHTOOL_H + +#include +#include + +class QPushButton; +class QComboBox; + +class BrushTool : public QWidget +{ + Q_OBJECT + +public: + explicit BrushTool(QString title, QWidget *parent = 0); + void setBrush(QBrush brush); + QBrush brush() const; + QString name(); + static QString name(const QBrush &brush); + +Q_SIGNALS: + void changed(); + +public Q_SLOTS: + void showColorDialog(); + void updateStyle(); + +private: + QBrush m_brush; + QPushButton *m_colorButton; + QComboBox *m_styleCombo; +}; + +#endif // BRUSHTOOL_H diff --git a/demos/piechartcustomization/customslice.cpp b/demos/piechartcustomization/customslice.cpp new file mode 100644 index 0000000..4b96c2d --- /dev/null +++ b/demos/piechartcustomization/customslice.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "customslice.h" + +QTCOMMERCIALCHART_USE_NAMESPACE + +CustomSlice::CustomSlice(qreal value, QString label) + :QPieSlice(value, label) +{ + connect(this, SIGNAL(hoverEnter()), this, SLOT(handleHoverEnter())); + connect(this, SIGNAL(hoverLeave()), this, SLOT(handleHoverLeave())); +} + +QBrush CustomSlice::originalBrush() +{ + return m_originalBrush; +} + +void CustomSlice::handleHoverEnter() +{ + QBrush brush = this->brush(); + m_originalBrush = brush; + brush.setColor(brush.color().lighter()); + setBrush(brush); +} + +void CustomSlice::handleHoverLeave() +{ + setBrush(m_originalBrush); +} + +#include "moc_customslice.cpp" diff --git a/demos/piechartcustomization/customslice.h b/demos/piechartcustomization/customslice.h new file mode 100644 index 0000000..1f855d3 --- /dev/null +++ b/demos/piechartcustomization/customslice.h @@ -0,0 +1,45 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ +#ifndef CUSTOMSLICE_H +#define CUSTOMSLICE_H + +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +class CustomSlice : public QPieSlice +{ + Q_OBJECT + +public: + CustomSlice(qreal value, QString label); + +public: + QBrush originalBrush(); + +public Q_SLOTS: + void handleHoverEnter(); + void handleHoverLeave(); + +private: + QBrush m_originalBrush; +}; + +#endif // CUSTOMSLICE_H diff --git a/demos/piechartcustomization/main.cpp b/demos/piechartcustomization/main.cpp index 689ae4b..a60b090 100644 --- a/demos/piechartcustomization/main.cpp +++ b/demos/piechartcustomization/main.cpp @@ -18,614 +18,17 @@ ** ****************************************************************************/ +#include "mainwidget.h" #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -QTCOMMERCIALCHART_USE_NAMESPACE - -class PenTool : public QWidget -{ - Q_OBJECT - -public: - explicit PenTool(QString title, QWidget *parent = 0) - :QWidget(parent) - { - setWindowTitle(title); - setWindowFlags(Qt::Tool); - - m_colorButton = new QPushButton(); - - m_widthSpinBox = new QDoubleSpinBox(); - - m_styleCombo = new QComboBox(); - m_styleCombo->addItem("NoPen"); - m_styleCombo->addItem("SolidLine"); - m_styleCombo->addItem("DashLine"); - m_styleCombo->addItem("DotLine"); - m_styleCombo->addItem("DashDotLine"); - m_styleCombo->addItem("DashDotDotLine"); - - m_capStyleCombo = new QComboBox(); - m_capStyleCombo->addItem("FlatCap", Qt::FlatCap); - m_capStyleCombo->addItem("SquareCap", Qt::SquareCap); - m_capStyleCombo->addItem("RoundCap", Qt::RoundCap); - - m_joinStyleCombo = new QComboBox(); - m_joinStyleCombo->addItem("MiterJoin", Qt::MiterJoin); - m_joinStyleCombo->addItem("BevelJoin", Qt::BevelJoin); - m_joinStyleCombo->addItem("RoundJoin", Qt::RoundJoin); - m_joinStyleCombo->addItem("SvgMiterJoin", Qt::SvgMiterJoin); - - QFormLayout *layout = new QFormLayout(); - layout->addRow("Color", m_colorButton); - layout->addRow("Width", m_widthSpinBox); - layout->addRow("Style", m_styleCombo); - layout->addRow("Cap style", m_capStyleCombo); - layout->addRow("Join style", m_joinStyleCombo); - setLayout(layout); - - connect(m_colorButton, SIGNAL(clicked()), this, SLOT(showColorDialog())); - connect(m_widthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateWidth(double))); - connect(m_styleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateStyle(int))); - connect(m_capStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateCapStyle(int))); - connect(m_joinStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateJoinStyle(int))); - } - - void setPen(QPen pen) - { - m_pen = pen; - m_colorButton->setText(m_pen.color().name()); - m_widthSpinBox->setValue(m_pen.widthF()); - m_styleCombo->setCurrentIndex(m_pen.style()); // index matches the enum - m_capStyleCombo->setCurrentIndex(m_capStyleCombo->findData(m_pen.capStyle())); - m_joinStyleCombo->setCurrentIndex(m_joinStyleCombo->findData(m_pen.joinStyle())); - } - - QPen pen() const - { - return m_pen; - } - - QString name() - { - return name(m_pen); - } - - static QString name(const QPen &pen) - { - return pen.color().name() + ":" + QString::number(pen.widthF()); - } - -Q_SIGNALS: - void changed(); - -public Q_SLOTS: - - void showColorDialog() - { - QColorDialog dialog(m_pen.color()); - dialog.show(); - dialog.exec(); - m_pen.setColor(dialog.selectedColor()); - m_colorButton->setText(m_pen.color().name()); - emit changed(); - } - - void updateWidth(double width) - { - if (!qFuzzyIsNull(width - m_pen.widthF())) { - m_pen.setWidthF(width); - emit changed(); - } - } - - void updateStyle(int style) - { - if (m_pen.style() != style) { - m_pen.setStyle((Qt::PenStyle) style); - emit changed(); - } - } - - void updateCapStyle(int index) - { - Qt::PenCapStyle capStyle = (Qt::PenCapStyle) m_capStyleCombo->itemData(index).toInt(); - if (m_pen.capStyle() != capStyle) { - m_pen.setCapStyle(capStyle); - emit changed(); - } - } - - void updateJoinStyle(int index) - { - Qt::PenJoinStyle joinStyle = (Qt::PenJoinStyle) m_joinStyleCombo->itemData(index).toInt(); - if (m_pen.joinStyle() != joinStyle) { - m_pen.setJoinStyle(joinStyle); - emit changed(); - } - } - -private: - QPen m_pen; - QPushButton *m_colorButton; - QDoubleSpinBox *m_widthSpinBox; - QComboBox *m_styleCombo; - QComboBox *m_capStyleCombo; - QComboBox *m_joinStyleCombo; -}; - -class BrushTool : public QWidget -{ - Q_OBJECT - -public: - explicit BrushTool(QString title, QWidget *parent = 0) - :QWidget(parent) - { - setWindowTitle(title); - setWindowFlags(Qt::Tool); - - m_colorButton = new QPushButton(); - m_styleCombo = new QComboBox(); - m_styleCombo->addItem("Nobrush", Qt::NoBrush); - m_styleCombo->addItem("Solidpattern", Qt::SolidPattern); - m_styleCombo->addItem("Dense1pattern", Qt::Dense1Pattern); - m_styleCombo->addItem("Dense2attern", Qt::Dense2Pattern); - m_styleCombo->addItem("Dense3Pattern", Qt::Dense3Pattern); - m_styleCombo->addItem("Dense4Pattern", Qt::Dense4Pattern); - m_styleCombo->addItem("Dense5Pattern", Qt::Dense5Pattern); - m_styleCombo->addItem("Dense6Pattern", Qt::Dense6Pattern); - m_styleCombo->addItem("Dense7Pattern", Qt::Dense7Pattern); - m_styleCombo->addItem("HorPattern", Qt::HorPattern); - m_styleCombo->addItem("VerPattern", Qt::VerPattern); - m_styleCombo->addItem("CrossPattern", Qt::CrossPattern); - m_styleCombo->addItem("BDiagPattern", Qt::BDiagPattern); - m_styleCombo->addItem("FDiagPattern", Qt::FDiagPattern); - m_styleCombo->addItem("DiagCrossPattern", Qt::DiagCrossPattern); - - QFormLayout *layout = new QFormLayout(); - layout->addRow("Color", m_colorButton); - layout->addRow("Style", m_styleCombo); - setLayout(layout); - - connect(m_colorButton, SIGNAL(clicked()), this, SLOT(showColorDialog())); - connect(m_styleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateStyle())); - } - - void setBrush(QBrush brush) - { - m_brush = brush; - m_colorButton->setText(m_brush.color().name()); - m_styleCombo->setCurrentIndex(m_brush.style()); // index matches the enum - } - - QBrush brush() const - { - return m_brush; - } - - QString name() - { - return name(m_brush); - } - - static QString name(const QBrush &brush) - { - return brush.color().name(); - } - -Q_SIGNALS: - void changed(); - -public Q_SLOTS: - - void showColorDialog() - { - QColorDialog dialog(m_brush.color()); - dialog.show(); - dialog.exec(); - m_brush.setColor(dialog.selectedColor()); - m_colorButton->setText(m_brush.color().name()); - emit changed(); - } - - void updateStyle() - { - Qt::BrushStyle style = (Qt::BrushStyle) m_styleCombo->itemData(m_styleCombo->currentIndex()).toInt(); - if (m_brush.style() != style) { - m_brush.setStyle(style); - emit changed(); - } - } - -private: - QBrush m_brush; - QPushButton *m_colorButton; - QComboBox *m_styleCombo; -}; - -class CustomSlice : public QPieSlice -{ - Q_OBJECT -public: - CustomSlice(qreal value, QString label) - :QPieSlice(value, label) - { - connect(this, SIGNAL(hoverEnter()), this, SLOT(handleHoverEnter())); - connect(this, SIGNAL(hoverLeave()), this, SLOT(handleHoverLeave())); - } - -public: - QBrush originalBrush() - { - return m_originalBrush; - } - -public Q_SLOTS: - - void handleHoverEnter() - { - QBrush brush = this->brush(); - m_originalBrush = brush; - brush.setColor(brush.color().lighter()); - setBrush(brush); - } - - void handleHoverLeave() - { - setBrush(m_originalBrush); - } - -private: - QBrush m_originalBrush; -}; - -class MainWidget : public QWidget -{ - Q_OBJECT - -public: - explicit MainWidget(QWidget* parent = 0) - :QWidget(parent), - m_slice(0) - { - // create chart - m_chartView = new QChartView(new QChart()); - m_chartView->chart()->setTitle("Piechart customization"); - m_chartView->chart()->setAnimationOptions(QChart::AllAnimations); - - // create series - m_series = new QPieSeries(); - *m_series << new CustomSlice(10.0, "Slice 1"); - *m_series << new CustomSlice(20.0, "Slice 2"); - *m_series << new CustomSlice(30.0, "Slice 3"); - *m_series << new CustomSlice(40.0, "Slice 4"); - *m_series << new CustomSlice(50.0, "Slice 5"); - m_series->setLabelsVisible(); - m_chartView->chart()->addSeries(m_series); - - connect(m_series, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), this, SLOT(handleSliceClicked(QPieSlice*, Qt::MouseButtons))); - - // chart settings - m_themeComboBox = new QComboBox(); - m_themeComboBox->addItem("Default", QChart::ChartThemeDefault); - 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); - - QFormLayout* chartSettingsLayout = new QFormLayout(); - chartSettingsLayout->addRow("Theme", m_themeComboBox); - chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox); - chartSettingsLayout->addRow("Animations", m_animationsCheckBox); - 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())); - connect(m_animationsCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings())); - - // series settings - m_hPosition = new QDoubleSpinBox(); - m_hPosition->setMinimum(0.0); - m_hPosition->setMaximum(1.0); - m_hPosition->setSingleStep(0.1); - m_hPosition->setValue(m_series->pieHorizontalPosition()); - - m_vPosition = new QDoubleSpinBox(); - m_vPosition->setMinimum(0.0); - m_vPosition->setMaximum(1.0); - m_vPosition->setSingleStep(0.1); - m_vPosition->setValue(m_series->pieVerticalPosition()); - - 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(); - m_startAngle->setMinimum(0.0); - m_startAngle->setMaximum(360); - m_startAngle->setValue(m_series->pieStartAngle()); - m_startAngle->setSingleStep(1); - - m_endAngle = new QDoubleSpinBox(); - m_endAngle->setMinimum(0.0); - m_endAngle->setMaximum(360); - m_endAngle->setValue(m_series->pieEndAngle()); - m_endAngle->setSingleStep(1); - - QPushButton *addSlice = new QPushButton("Add slice"); - QPushButton *insertSlice = new QPushButton("Insert slice"); - - QFormLayout* seriesSettingsLayout = new QFormLayout(); - 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); - seriesSettingsLayout->addRow(addSlice); - seriesSettingsLayout->addRow(insertSlice); - 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())); - connect(addSlice, SIGNAL(clicked()), this, SLOT(addSlice())); - connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice())); - - // slice settings - m_sliceName = new QLabel(""); - 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(); - m_labelPen = new QPushButton(); - m_labelPenTool = new PenTool("Label pen", this); - QPushButton *removeSlice = new QPushButton("Remove slice"); - - QFormLayout* sliceSettingsLayout = new QFormLayout(); - sliceSettingsLayout->addRow("Selected", m_sliceName); - 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); - sliceSettingsLayout->addRow("Label pen", m_labelPen); - sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor); - sliceSettingsLayout->addRow("Exploded", m_sliceExploded); - sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor); - sliceSettingsLayout->addRow(removeSlice); - QGroupBox* sliceSettings = new QGroupBox("Slice"); - sliceSettings->setLayout(sliceSettingsLayout); - - 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())); - connect(m_labelPen, SIGNAL(clicked()), m_labelPenTool, SLOT(show())); - connect(m_labelPenTool, SIGNAL(changed()), this, SLOT(updateSliceSettings())); - 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())); - connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice())); - - // 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->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); - } - - void updateSerieSettings() - { - m_series->setPiePosition(m_hPosition->value(), m_vPosition->value()); - m_series->setPieSize(m_sizeFactor->value()); - m_series->setPieStartAngle(m_startAngle->value()); - m_series->setPieEndAngle(m_endAngle->value()); - } - - void updateSliceSettings() - { - if (!m_slice) - return; - - m_slice->setValue(m_sliceValue->value()); - - m_slice->setPen(m_penTool->pen()); - m_slice->setBrush(m_brushTool->brush()); - - m_slice->setLabelPen(m_labelPenTool->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, Qt::MouseButtons buttons) - { - Q_UNUSED(buttons); - - m_slice = static_cast(slice); - - // name - m_sliceName->setText(slice->label()); - - // 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 - m_labelPen->setText(PenTool::name(m_slice->labelPen())); - m_labelPenTool->setPen(m_slice->labelPen()); - 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); - - // 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 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()); - } - - void addSlice() - { - *m_series << new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1)); - } - - void insertSlice() - { - if (!m_slice) - return; - - int i = m_series->slices().indexOf(m_slice); - - m_series->insert(i, new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1))); - } - - void removeSlice() - { - if (!m_slice) - return; - - m_series->remove(m_slice); - m_slice = 0; - } - -private: - QComboBox *m_themeComboBox; - QCheckBox *m_aaCheckBox; - QCheckBox *m_animationsCheckBox; - - QChartView* m_chartView; - QPieSeries* m_series; - CustomSlice* m_slice; - - QDoubleSpinBox* m_hPosition; - QDoubleSpinBox* m_vPosition; - QDoubleSpinBox* m_sizeFactor; - QDoubleSpinBox* m_startAngle; - QDoubleSpinBox* m_endAngle; - - QLabel* m_sliceName; - QDoubleSpinBox* m_sliceValue; - QCheckBox* m_sliceLabelVisible; - QDoubleSpinBox* m_sliceLabelArmFactor; - QCheckBox* m_sliceExploded; - QDoubleSpinBox* m_sliceExplodedFactor; - QPushButton *m_brush; - BrushTool *m_brushTool; - QPushButton *m_pen; - PenTool *m_penTool; - QPushButton *m_font; - QPushButton *m_labelPen; - PenTool *m_labelPenTool; -}; int main(int argc, char *argv[]) { QApplication a(argc, argv); - QMainWindow window; - - MainWidget* widget = new MainWidget(); - - window.setCentralWidget(widget); + MainWidget widget; + window.setCentralWidget(&widget); window.resize(900, 600); window.show(); - return a.exec(); } - -#include "main.moc" diff --git a/demos/piechartcustomization/mainwidget.cpp b/demos/piechartcustomization/mainwidget.cpp new file mode 100644 index 0000000..06b1418 --- /dev/null +++ b/demos/piechartcustomization/mainwidget.cpp @@ -0,0 +1,322 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ +#include "mainwidget.h" +#include "customslice.h" +#include "pentool.h" +#include "brushtool.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +MainWidget::MainWidget(QWidget* parent) + :QWidget(parent), + m_slice(0) +{ + // create chart + QChart *chart = new QChart; + chart->setTitle("Piechart customization"); + chart->setAnimationOptions(QChart::AllAnimations); + + // create series + m_series = new QPieSeries(); + *m_series << new CustomSlice(10.0, "Slice 1"); + *m_series << new CustomSlice(20.0, "Slice 2"); + *m_series << new CustomSlice(30.0, "Slice 3"); + *m_series << new CustomSlice(40.0, "Slice 4"); + *m_series << new CustomSlice(50.0, "Slice 5"); + m_series->setLabelsVisible(); + chart->addSeries(m_series); + + connect(m_series, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), this, SLOT(handleSliceClicked(QPieSlice*, Qt::MouseButtons))); + + // chart settings + m_themeComboBox = new QComboBox(); + m_themeComboBox->addItem("Default", QChart::ChartThemeDefault); + 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); + + QFormLayout* chartSettingsLayout = new QFormLayout(); + chartSettingsLayout->addRow("Theme", m_themeComboBox); + chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox); + chartSettingsLayout->addRow("Animations", m_animationsCheckBox); + 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())); + connect(m_animationsCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings())); + + // series settings + m_hPosition = new QDoubleSpinBox(); + m_hPosition->setMinimum(0.0); + m_hPosition->setMaximum(1.0); + m_hPosition->setSingleStep(0.1); + m_hPosition->setValue(m_series->pieHorizontalPosition()); + + m_vPosition = new QDoubleSpinBox(); + m_vPosition->setMinimum(0.0); + m_vPosition->setMaximum(1.0); + m_vPosition->setSingleStep(0.1); + m_vPosition->setValue(m_series->pieVerticalPosition()); + + 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(); + m_startAngle->setMinimum(0.0); + m_startAngle->setMaximum(360); + m_startAngle->setValue(m_series->pieStartAngle()); + m_startAngle->setSingleStep(1); + + m_endAngle = new QDoubleSpinBox(); + m_endAngle->setMinimum(0.0); + m_endAngle->setMaximum(360); + m_endAngle->setValue(m_series->pieEndAngle()); + m_endAngle->setSingleStep(1); + + QPushButton *addSlice = new QPushButton("Add slice"); + QPushButton *insertSlice = new QPushButton("Insert slice"); + + QFormLayout* seriesSettingsLayout = new QFormLayout(); + 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); + seriesSettingsLayout->addRow(addSlice); + seriesSettingsLayout->addRow(insertSlice); + 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())); + connect(addSlice, SIGNAL(clicked()), this, SLOT(addSlice())); + connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice())); + + // slice settings + m_sliceName = new QLabel(""); + 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(); + m_labelPen = new QPushButton(); + m_labelPenTool = new PenTool("Label pen", this); + QPushButton *removeSlice = new QPushButton("Remove slice"); + + QFormLayout* sliceSettingsLayout = new QFormLayout(); + sliceSettingsLayout->addRow("Selected", m_sliceName); + 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); + sliceSettingsLayout->addRow("Label pen", m_labelPen); + sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor); + sliceSettingsLayout->addRow("Exploded", m_sliceExploded); + sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor); + sliceSettingsLayout->addRow(removeSlice); + QGroupBox* sliceSettings = new QGroupBox("Slice"); + sliceSettings->setLayout(sliceSettingsLayout); + + 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())); + connect(m_labelPen, SIGNAL(clicked()), m_labelPenTool, SLOT(show())); + connect(m_labelPenTool, SIGNAL(changed()), this, SLOT(updateSliceSettings())); + 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())); + connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice())); + + // 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(); + + QGridLayout* baseLayout = new QGridLayout(); + baseLayout->addLayout(settingsLayout, 0, 0); + baseLayout->addWidget(m_chartView, 0, 1); + setLayout(baseLayout); + + updateSerieSettings(); +} + + +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); +} + +void MainWidget::updateSerieSettings() +{ + m_series->setPiePosition(m_hPosition->value(), m_vPosition->value()); + m_series->setPieSize(m_sizeFactor->value()); + m_series->setPieStartAngle(m_startAngle->value()); + m_series->setPieEndAngle(m_endAngle->value()); +} + +void MainWidget::updateSliceSettings() +{ + if (!m_slice) + return; + + m_slice->setValue(m_sliceValue->value()); + + m_slice->setPen(m_penTool->pen()); + m_slice->setBrush(m_brushTool->brush()); + + m_slice->setLabelPen(m_labelPenTool->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 MainWidget::handleSliceClicked(QPieSlice* slice, Qt::MouseButtons buttons) +{ + Q_UNUSED(buttons); + + m_slice = static_cast(slice); + + // name + m_sliceName->setText(slice->label()); + + // 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 + m_labelPen->setText(PenTool::name(m_slice->labelPen())); + m_labelPenTool->setPen(m_slice->labelPen()); + 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); + + // 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()); +} + +void MainWidget::addSlice() +{ + *m_series << new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1)); +} + +void MainWidget::insertSlice() +{ + if (!m_slice) + return; + + int i = m_series->slices().indexOf(m_slice); + + m_series->insert(i, new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1))); +} + +void MainWidget::removeSlice() +{ + if (!m_slice) + return; + + m_series->remove(m_slice); + m_slice = 0; +} + +#include "moc_mainwidget.cpp" diff --git a/demos/piechartcustomization/mainwidget.h b/demos/piechartcustomization/mainwidget.h new file mode 100644 index 0000000..b01fe95 --- /dev/null +++ b/demos/piechartcustomization/mainwidget.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ +#ifndef MAINWIDGET_H +#define MAINWIDGET_H + +#include +#include + +class QLabel; +class QPushButton; +class QCheckBox; +class QComboBox; +class QDoubleSpinBox; +class PenTool; +class BrushTool; +class CustomSlice; + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +class QChartView; +class QPieSeries; +class QPieSlice; +QTCOMMERCIALCHART_END_NAMESPACE + +QTCOMMERCIALCHART_USE_NAMESPACE + +class MainWidget : public QWidget +{ + Q_OBJECT + +public: + explicit MainWidget(QWidget* parent = 0); + +public Q_SLOTS: + void updateChartSettings(); + void updateSerieSettings(); + void updateSliceSettings(); + void handleSliceClicked(QPieSlice* slice, Qt::MouseButtons buttons); + void showFontDialog(); + void addSlice(); + void insertSlice(); + void removeSlice(); + +private: + QComboBox *m_themeComboBox; + QCheckBox *m_aaCheckBox; + QCheckBox *m_animationsCheckBox; + + QChartView* m_chartView; + QPieSeries* m_series; + CustomSlice* m_slice; + + QDoubleSpinBox* m_hPosition; + QDoubleSpinBox* m_vPosition; + QDoubleSpinBox* m_sizeFactor; + QDoubleSpinBox* m_startAngle; + QDoubleSpinBox* m_endAngle; + + QLabel* m_sliceName; + QDoubleSpinBox* m_sliceValue; + QCheckBox* m_sliceLabelVisible; + QDoubleSpinBox* m_sliceLabelArmFactor; + QCheckBox* m_sliceExploded; + QDoubleSpinBox* m_sliceExplodedFactor; + QPushButton *m_brush; + BrushTool *m_brushTool; + QPushButton *m_pen; + PenTool *m_penTool; + QPushButton *m_font; + QPushButton *m_labelPen; + PenTool *m_labelPenTool; +}; + +#endif // MAINWIDGET_H diff --git a/demos/piechartcustomization/pentool.cpp b/demos/piechartcustomization/pentool.cpp new file mode 100644 index 0000000..d79a458 --- /dev/null +++ b/demos/piechartcustomization/pentool.cpp @@ -0,0 +1,141 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "pentool.h" +#include +#include +#include +#include +#include + +PenTool::PenTool(QString title, QWidget *parent) + :QWidget(parent) +{ + setWindowTitle(title); + setWindowFlags(Qt::Tool); + + m_colorButton = new QPushButton(); + + m_widthSpinBox = new QDoubleSpinBox(); + + m_styleCombo = new QComboBox(); + m_styleCombo->addItem("NoPen"); + m_styleCombo->addItem("SolidLine"); + m_styleCombo->addItem("DashLine"); + m_styleCombo->addItem("DotLine"); + m_styleCombo->addItem("DashDotLine"); + m_styleCombo->addItem("DashDotDotLine"); + + m_capStyleCombo = new QComboBox(); + m_capStyleCombo->addItem("FlatCap", Qt::FlatCap); + m_capStyleCombo->addItem("SquareCap", Qt::SquareCap); + m_capStyleCombo->addItem("RoundCap", Qt::RoundCap); + + m_joinStyleCombo = new QComboBox(); + m_joinStyleCombo->addItem("MiterJoin", Qt::MiterJoin); + m_joinStyleCombo->addItem("BevelJoin", Qt::BevelJoin); + m_joinStyleCombo->addItem("RoundJoin", Qt::RoundJoin); + m_joinStyleCombo->addItem("SvgMiterJoin", Qt::SvgMiterJoin); + + QFormLayout *layout = new QFormLayout(); + layout->addRow("Color", m_colorButton); + layout->addRow("Width", m_widthSpinBox); + layout->addRow("Style", m_styleCombo); + layout->addRow("Cap style", m_capStyleCombo); + layout->addRow("Join style", m_joinStyleCombo); + setLayout(layout); + + connect(m_colorButton, SIGNAL(clicked()), this, SLOT(showColorDialog())); + connect(m_widthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateWidth(double))); + connect(m_styleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateStyle(int))); + connect(m_capStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateCapStyle(int))); + connect(m_joinStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateJoinStyle(int))); +} + +void PenTool::setPen(const QPen &pen) +{ + m_pen = pen; + m_colorButton->setText(m_pen.color().name()); + m_widthSpinBox->setValue(m_pen.widthF()); + m_styleCombo->setCurrentIndex(m_pen.style()); // index matches the enum + m_capStyleCombo->setCurrentIndex(m_capStyleCombo->findData(m_pen.capStyle())); + m_joinStyleCombo->setCurrentIndex(m_joinStyleCombo->findData(m_pen.joinStyle())); +} + +QPen PenTool::pen() const +{ + return m_pen; +} + +QString PenTool::name() +{ + return name(m_pen); +} + +QString PenTool::name(const QPen &pen) +{ + return pen.color().name() + ":" + QString::number(pen.widthF()); +} + +void PenTool::showColorDialog() +{ + QColorDialog dialog(m_pen.color()); + dialog.show(); + dialog.exec(); + m_pen.setColor(dialog.selectedColor()); + m_colorButton->setText(m_pen.color().name()); + emit changed(); +} + +void PenTool::updateWidth(double width) +{ + if (!qFuzzyIsNull(width - m_pen.widthF())) { + m_pen.setWidthF(width); + emit changed(); + } +} + +void PenTool::updateStyle(int style) +{ + if (m_pen.style() != style) { + m_pen.setStyle((Qt::PenStyle) style); + emit changed(); + } +} + +void PenTool::updateCapStyle(int index) +{ + Qt::PenCapStyle capStyle = (Qt::PenCapStyle) m_capStyleCombo->itemData(index).toInt(); + if (m_pen.capStyle() != capStyle) { + m_pen.setCapStyle(capStyle); + emit changed(); + } +} + +void PenTool::updateJoinStyle(int index) +{ + Qt::PenJoinStyle joinStyle = (Qt::PenJoinStyle) m_joinStyleCombo->itemData(index).toInt(); + if (m_pen.joinStyle() != joinStyle) { + m_pen.setJoinStyle(joinStyle); + emit changed(); + } +} + +#include "moc_pentool.cpp" diff --git a/demos/piechartcustomization/pentool.h b/demos/piechartcustomization/pentool.h new file mode 100644 index 0000000..0c2f700 --- /dev/null +++ b/demos/piechartcustomization/pentool.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ +#ifndef PENTOOL_H +#define PENTOOL_H + +#include +#include + +class QPushButton; +class QDoubleSpinBox; +class QComboBox; + +class PenTool : public QWidget +{ + Q_OBJECT + +public: + explicit PenTool(QString title, QWidget *parent = 0); + void setPen(const QPen &pen); + QPen pen() const; + QString name(); + static QString name(const QPen &pen); + +Q_SIGNALS: + void changed(); + +public Q_SLOTS: + void showColorDialog(); + void updateWidth(double width); + void updateStyle(int style); + void updateCapStyle(int index); + void updateJoinStyle(int index); + +private: + QPen m_pen; + QPushButton *m_colorButton; + QDoubleSpinBox *m_widthSpinBox; + QComboBox *m_styleCombo; + QComboBox *m_capStyleCombo; + QComboBox *m_joinStyleCombo; +}; + +#endif // PENTOOL_H diff --git a/demos/piechartcustomization/piechartcustomization.pro b/demos/piechartcustomization/piechartcustomization.pro index 352af7e..c2fac07 100644 --- a/demos/piechartcustomization/piechartcustomization.pro +++ b/demos/piechartcustomization/piechartcustomization.pro @@ -2,6 +2,16 @@ error( "Couldn't find the examples.pri file!" ) } TARGET = piechartcustomization -SOURCES += main.cpp +SOURCES += main.cpp \ + pentool.cpp \ + brushtool.cpp \ + customslice.cpp \ + mainwidget.cpp + +HEADERS += \ + pentool.h \ + brushtool.h \ + customslice.h \ + mainwidget.h