##// END OF EJS Templates
Adding more controls to piechartcustomization demo
Adding more controls to piechartcustomization demo

File last commit:

r511:6c936a9996d6
r511:6c936a9996d6
Show More
main.cpp
339 lines | 11.4 KiB | text/x-c | CppLexer
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 #include <QtGui/QApplication>
#include <QMainWindow>
#include <qchartglobal.h>
#include <qchartview.h>
#include <qpieseries.h>
#include <qpieslice.h>
#include <QGridLayout>
#include <QFormLayout>
#include <QComboBox>
#include <QSpinBox>
#include <QCheckBox>
#include <QGroupBox>
#include <QLabel>
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 #include <QPushButton>
#include <QColorDialog>
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437
QTCOMMERCIALCHART_USE_NAMESPACE
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()));
}
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 public:
QBrush originalBrush()
{
return m_originalBrush;
}
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 public Q_SLOTS:
void handleHoverEnter()
{
Jani Honkonen
Renaming pen & brush functions for pie and adding const
r469 QBrush brush = this->sliceBrush();
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 m_originalBrush = brush;
brush.setColor(brush.color().lighter());
Jani Honkonen
Renaming pen & brush functions for pie and adding const
r469 setSliceBrush(brush);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 }
void handleHoverLeave()
{
Jani Honkonen
Renaming pen & brush functions for pie and adding const
r469 setSliceBrush(m_originalBrush);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 }
private:
QBrush m_originalBrush;
};
class MainWidget : public QWidget
{
Q_OBJECT
public:
explicit MainWidget(QWidget* parent = 0)
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 :QWidget(parent),
m_slice(0)
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 {
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 // create chart
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 m_chartView = new QChartView();
m_chartView->setChartTitle("Piechart customization");
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 // create series
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 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_chartView->addSeries(m_series);
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 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
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 m_hPosition = new QDoubleSpinBox();
m_hPosition->setMinimum(0.0);
m_hPosition->setMaximum(1.0);
m_hPosition->setSingleStep(0.1);
Jani Honkonen
Renamed the "factor" stuff from pie series API.
r498 m_hPosition->setValue(m_series->pieHorizontalPosition());
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 m_vPosition = new QDoubleSpinBox();
m_vPosition->setMinimum(0.0);
m_vPosition->setMaximum(1.0);
m_vPosition->setSingleStep(0.1);
Jani Honkonen
Renamed the "factor" stuff from pie series API.
r498 m_vPosition->setValue(m_series->pieVerticalPosition());
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437
m_sizeFactor = new QDoubleSpinBox();
m_sizeFactor->setMinimum(0.0);
m_sizeFactor->setMaximum(1.0);
m_sizeFactor->setSingleStep(0.1);
Jani Honkonen
Renamed the "factor" stuff from pie series API.
r498 m_sizeFactor->setValue(m_series->pieSize());
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437
m_startAngle = new QDoubleSpinBox();
m_startAngle->setMinimum(0.0);
m_startAngle->setMaximum(360);
Jani Honkonen
Renamed the "factor" stuff from pie series API.
r498 m_startAngle->setValue(m_series->pieStartAngle());
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 m_startAngle->setSingleStep(1);
m_endAngle = new QDoubleSpinBox();
m_endAngle->setMinimum(0.0);
m_endAngle->setMaximum(360);
Jani Honkonen
Renamed the "factor" stuff from pie series API.
r498 m_endAngle->setValue(m_series->pieEndAngle());
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 m_endAngle->setSingleStep(1);
QFormLayout* seriesSettingsLayout = new QFormLayout();
seriesSettingsLayout->addRow("Horizontal position", m_hPosition);
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 seriesSettingsLayout->addRow("Vertical position", m_vPosition);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 seriesSettingsLayout->addRow("Size factor", m_sizeFactor);
seriesSettingsLayout->addRow("Start angle", m_startAngle);
seriesSettingsLayout->addRow("End angle", m_endAngle);
QGroupBox* seriesSettings = new QGroupBox("Series");
seriesSettings->setLayout(seriesSettingsLayout);
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 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
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 m_sliceName = new QLabel("<click a slice>");
m_sliceValue = new QDoubleSpinBox();
m_sliceValue->setMaximum(1000);
m_sliceLabelVisible = new QCheckBox();
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 m_sliceLabelArmFactor = new QDoubleSpinBox();
m_sliceLabelArmFactor->setSingleStep(0.01);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 m_sliceExploded = new QCheckBox();
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 m_sliceExplodedFactor = new QDoubleSpinBox();
m_sliceExplodedFactor->setSingleStep(0.01);
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 m_penWidth = new QDoubleSpinBox();
m_penColor = new QPushButton();
m_brushColor = new QPushButton();
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437
QFormLayout* sliceSettingsLayout = new QFormLayout();
sliceSettingsLayout->addRow("Selected", m_sliceName);
sliceSettingsLayout->addRow("Value", m_sliceValue);
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 sliceSettingsLayout->addRow("Pen color", m_penColor);
sliceSettingsLayout->addRow("Pen width", m_penWidth);
sliceSettingsLayout->addRow("Brush color", m_brushColor);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
Jani Honkonen
Adding more controls to piechartcustomization demo
r511
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 QGroupBox* sliceSettings = new QGroupBox("Slice");
sliceSettings->setLayout(sliceSettingsLayout);
connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 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()));
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 // 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);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437
updateSerieSettings();
}
public Q_SLOTS:
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 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());
}
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 void updateSerieSettings()
{
Jani Honkonen
Renamed the "factor" stuff from pie series API.
r498 m_series->setPiePosition(m_vPosition->value(), m_hPosition->value());
m_series->setPieSize(m_sizeFactor->value());
m_series->setPieStartAngle(m_startAngle->value());
m_series->setPieEndAngle(m_endAngle->value());
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 }
void updateSliceSettings()
{
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 if (!m_slice)
return;
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 m_slice->setValue(m_sliceValue->value());
Jani Honkonen
Adding more controls to piechartcustomization demo
r511
QPen pen = m_slice->slicePen();
pen.setWidthF(m_penWidth->value());
m_slice->setSlicePen(pen);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
Jani Honkonen
Adding more controls to piechartcustomization demo
r511
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 m_slice->setExploded(m_sliceExploded->isChecked());
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 }
void handleSliceClicked(QPieSlice* slice)
{
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 m_slice = static_cast<CustomSlice*>(slice);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 m_sliceName->setText(slice->label());
m_sliceValue->blockSignals(true);
m_sliceValue->setValue(slice->value());
m_sliceValue->blockSignals(false);
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 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());
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 m_sliceLabelVisible->blockSignals(true);
m_sliceLabelVisible->setChecked(slice->isLabelVisible());
m_sliceLabelVisible->blockSignals(false);
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 m_sliceLabelArmFactor->blockSignals(true);
m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
m_sliceLabelArmFactor->blockSignals(false);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 m_sliceExploded->blockSignals(true);
m_sliceExploded->setChecked(slice->isExploded());
m_sliceExploded->blockSignals(false);
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454
m_sliceExplodedFactor->blockSignals(true);
m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
m_sliceExplodedFactor->blockSignals(false);
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 }
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 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());
}
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 private:
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 QComboBox *m_themeComboBox;
QCheckBox *m_aaCheckBox;
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 QChartView* m_chartView;
QPieSeries* m_series;
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 CustomSlice* m_slice;
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 QDoubleSpinBox* m_hPosition;
QDoubleSpinBox* m_vPosition;
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 QDoubleSpinBox* m_sizeFactor;
QDoubleSpinBox* m_startAngle;
QDoubleSpinBox* m_endAngle;
QLabel* m_sliceName;
QDoubleSpinBox* m_sliceValue;
QCheckBox* m_sliceLabelVisible;
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 QDoubleSpinBox* m_sliceLabelArmFactor;
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 QCheckBox* m_sliceExploded;
Jani Honkonen
Introducing vertical and horizontal factors to control the position of the pie.
r454 QDoubleSpinBox* m_sliceExplodedFactor;
Jani Honkonen
Adding more controls to piechartcustomization demo
r511 QPushButton *m_brushColor;
QPushButton *m_penColor;
QDoubleSpinBox* m_penWidth;
Jani Honkonen
Added a pie chart customization example and refactoring the pie interface.
r437 };
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow window;
MainWidget* widget = new MainWidget();
window.setCentralWidget(widget);
window.resize(900, 600);
window.show();
return a.exec();
}
#include "main.moc"