##// END OF EJS Templates
Improving pie examples
Jani Honkonen -
r892:7f4e2249f988
parent child
Show More
@@ -1,322 +1,324
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20 #include "mainwidget.h"
20 #include "mainwidget.h"
21 #include "customslice.h"
21 #include "customslice.h"
22 #include "pentool.h"
22 #include "pentool.h"
23 #include "brushtool.h"
23 #include "brushtool.h"
24 #include <QPushButton>
24 #include <QPushButton>
25 #include <QComboBox>
25 #include <QComboBox>
26 #include <QCheckBox>
26 #include <QCheckBox>
27 #include <QLabel>
27 #include <QLabel>
28 #include <QGroupBox>
28 #include <QGroupBox>
29 #include <QDoubleSpinBox>
29 #include <QDoubleSpinBox>
30 #include <QFormLayout>
30 #include <QFormLayout>
31 #include <QFontDialog>
31 #include <QFontDialog>
32 #include <QChartView>
32 #include <QChartView>
33 #include <QPieSeries>
33 #include <QPieSeries>
34
34
35 QTCOMMERCIALCHART_USE_NAMESPACE
35 QTCOMMERCIALCHART_USE_NAMESPACE
36
36
37 MainWidget::MainWidget(QWidget* parent)
37 MainWidget::MainWidget(QWidget* parent)
38 :QWidget(parent),
38 :QWidget(parent),
39 m_slice(0)
39 m_slice(0)
40 {
40 {
41 // create chart
41 // create chart
42 QChart *chart = new QChart;
42 QChart *chart = new QChart;
43 chart->setTitle("Piechart customization");
43 chart->setTitle("Piechart customization");
44 chart->setAnimationOptions(QChart::AllAnimations);
44 chart->setAnimationOptions(QChart::AllAnimations);
45
45
46 // create series
46 // create series
47 m_series = new QPieSeries();
47 m_series = new QPieSeries();
48 *m_series << new CustomSlice(10.0, "Slice 1");
48 *m_series << new CustomSlice(10.0, "Slice 1");
49 *m_series << new CustomSlice(20.0, "Slice 2");
49 *m_series << new CustomSlice(20.0, "Slice 2");
50 *m_series << new CustomSlice(30.0, "Slice 3");
50 *m_series << new CustomSlice(30.0, "Slice 3");
51 *m_series << new CustomSlice(40.0, "Slice 4");
51 *m_series << new CustomSlice(40.0, "Slice 4");
52 *m_series << new CustomSlice(50.0, "Slice 5");
52 *m_series << new CustomSlice(50.0, "Slice 5");
53 m_series->setLabelsVisible();
53 m_series->setLabelsVisible();
54 chart->addSeries(m_series);
54 chart->addSeries(m_series);
55
55
56 connect(m_series, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), this, SLOT(handleSliceClicked(QPieSlice*, Qt::MouseButtons)));
56 connect(m_series, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), this, SLOT(handleSliceClicked(QPieSlice*, Qt::MouseButtons)));
57
57
58 // chart settings
58 // chart settings
59 m_themeComboBox = new QComboBox();
59 m_themeComboBox = new QComboBox();
60 m_themeComboBox->addItem("Light", QChart::ChartThemeLight);
60 m_themeComboBox->addItem("Light", QChart::ChartThemeLight);
61 m_themeComboBox->addItem("BlueCerulean", QChart::ChartThemeBlueCerulean);
61 m_themeComboBox->addItem("BlueCerulean", QChart::ChartThemeBlueCerulean);
62 m_themeComboBox->addItem("Dark", QChart::ChartThemeDark);
62 m_themeComboBox->addItem("Dark", QChart::ChartThemeDark);
63 m_themeComboBox->addItem("BrownSand", QChart::ChartThemeBrownSand);
63 m_themeComboBox->addItem("BrownSand", QChart::ChartThemeBrownSand);
64 m_themeComboBox->addItem("BlueNcs", QChart::ChartThemeBlueNcs);
64 m_themeComboBox->addItem("BlueNcs", QChart::ChartThemeBlueNcs);
65 m_themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
65 m_themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
66 m_themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
66 m_themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
67
67
68 m_aaCheckBox = new QCheckBox();
68 m_aaCheckBox = new QCheckBox();
69 m_animationsCheckBox = new QCheckBox();
69 m_animationsCheckBox = new QCheckBox();
70 m_animationsCheckBox->setCheckState(Qt::Checked);
70 m_animationsCheckBox->setCheckState(Qt::Checked);
71
71
72 QFormLayout* chartSettingsLayout = new QFormLayout();
72 QFormLayout* chartSettingsLayout = new QFormLayout();
73 chartSettingsLayout->addRow("Theme", m_themeComboBox);
73 chartSettingsLayout->addRow("Theme", m_themeComboBox);
74 chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox);
74 chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox);
75 chartSettingsLayout->addRow("Animations", m_animationsCheckBox);
75 chartSettingsLayout->addRow("Animations", m_animationsCheckBox);
76 QGroupBox* chartSettings = new QGroupBox("Chart");
76 QGroupBox* chartSettings = new QGroupBox("Chart");
77 chartSettings->setLayout(chartSettingsLayout);
77 chartSettings->setLayout(chartSettingsLayout);
78
78
79 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateChartSettings()));
79 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateChartSettings()));
80 connect(m_aaCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
80 connect(m_aaCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
81 connect(m_animationsCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
81 connect(m_animationsCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
82
82
83 // series settings
83 // series settings
84 m_hPosition = new QDoubleSpinBox();
84 m_hPosition = new QDoubleSpinBox();
85 m_hPosition->setMinimum(0.0);
85 m_hPosition->setMinimum(0.0);
86 m_hPosition->setMaximum(1.0);
86 m_hPosition->setMaximum(1.0);
87 m_hPosition->setSingleStep(0.1);
87 m_hPosition->setSingleStep(0.1);
88 m_hPosition->setValue(m_series->horizontalPosition());
88 m_hPosition->setValue(m_series->horizontalPosition());
89
89
90 m_vPosition = new QDoubleSpinBox();
90 m_vPosition = new QDoubleSpinBox();
91 m_vPosition->setMinimum(0.0);
91 m_vPosition->setMinimum(0.0);
92 m_vPosition->setMaximum(1.0);
92 m_vPosition->setMaximum(1.0);
93 m_vPosition->setSingleStep(0.1);
93 m_vPosition->setSingleStep(0.1);
94 m_vPosition->setValue(m_series->verticalPosition());
94 m_vPosition->setValue(m_series->verticalPosition());
95
95
96 m_sizeFactor = new QDoubleSpinBox();
96 m_sizeFactor = new QDoubleSpinBox();
97 m_sizeFactor->setMinimum(0.0);
97 m_sizeFactor->setMinimum(0.0);
98 m_sizeFactor->setMaximum(1.0);
98 m_sizeFactor->setMaximum(1.0);
99 m_sizeFactor->setSingleStep(0.1);
99 m_sizeFactor->setSingleStep(0.1);
100 m_sizeFactor->setValue(m_series->pieSize());
100 m_sizeFactor->setValue(m_series->pieSize());
101
101
102 m_startAngle = new QDoubleSpinBox();
102 m_startAngle = new QDoubleSpinBox();
103 m_startAngle->setMinimum(0.0);
103 m_startAngle->setMinimum(0.0);
104 m_startAngle->setMaximum(360);
104 m_startAngle->setMaximum(360);
105 m_startAngle->setValue(m_series->pieStartAngle());
105 m_startAngle->setValue(m_series->pieStartAngle());
106 m_startAngle->setSingleStep(1);
106 m_startAngle->setSingleStep(1);
107
107
108 m_endAngle = new QDoubleSpinBox();
108 m_endAngle = new QDoubleSpinBox();
109 m_endAngle->setMinimum(0.0);
109 m_endAngle->setMinimum(0.0);
110 m_endAngle->setMaximum(360);
110 m_endAngle->setMaximum(360);
111 m_endAngle->setValue(m_series->pieEndAngle());
111 m_endAngle->setValue(m_series->pieEndAngle());
112 m_endAngle->setSingleStep(1);
112 m_endAngle->setSingleStep(1);
113
113
114 QPushButton *addSlice = new QPushButton("Add slice");
114 QPushButton *appendSlice = new QPushButton("Append slice");
115 QPushButton *insertSlice = new QPushButton("Insert slice");
115 QPushButton *insertSlice = new QPushButton("Insert slice");
116
116
117 QFormLayout* seriesSettingsLayout = new QFormLayout();
117 QFormLayout* seriesSettingsLayout = new QFormLayout();
118 seriesSettingsLayout->addRow("Horizontal position", m_hPosition);
118 seriesSettingsLayout->addRow("Horizontal position", m_hPosition);
119 seriesSettingsLayout->addRow("Vertical position", m_vPosition);
119 seriesSettingsLayout->addRow("Vertical position", m_vPosition);
120 seriesSettingsLayout->addRow("Size factor", m_sizeFactor);
120 seriesSettingsLayout->addRow("Size factor", m_sizeFactor);
121 seriesSettingsLayout->addRow("Start angle", m_startAngle);
121 seriesSettingsLayout->addRow("Start angle", m_startAngle);
122 seriesSettingsLayout->addRow("End angle", m_endAngle);
122 seriesSettingsLayout->addRow("End angle", m_endAngle);
123 seriesSettingsLayout->addRow(addSlice);
123 seriesSettingsLayout->addRow(appendSlice);
124 seriesSettingsLayout->addRow(insertSlice);
124 seriesSettingsLayout->addRow(insertSlice);
125 QGroupBox* seriesSettings = new QGroupBox("Series");
125 QGroupBox* seriesSettings = new QGroupBox("Series");
126 seriesSettings->setLayout(seriesSettingsLayout);
126 seriesSettings->setLayout(seriesSettingsLayout);
127
127
128 connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
128 connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
129 connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
129 connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
130 connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
130 connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
131 connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
131 connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
132 connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
132 connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
133 connect(addSlice, SIGNAL(clicked()), this, SLOT(addSlice()));
133 connect(appendSlice, SIGNAL(clicked()), this, SLOT(appendSlice()));
134 connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice()));
134 connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice()));
135
135
136 // slice settings
136 // slice settings
137 m_sliceName = new QLabel("<click a slice>");
137 m_sliceName = new QLabel("<click a slice>");
138 m_sliceValue = new QDoubleSpinBox();
138 m_sliceValue = new QDoubleSpinBox();
139 m_sliceValue->setMaximum(1000);
139 m_sliceValue->setMaximum(1000);
140 m_sliceLabelVisible = new QCheckBox();
140 m_sliceLabelVisible = new QCheckBox();
141 m_sliceLabelArmFactor = new QDoubleSpinBox();
141 m_sliceLabelArmFactor = new QDoubleSpinBox();
142 m_sliceLabelArmFactor->setSingleStep(0.01);
142 m_sliceLabelArmFactor->setSingleStep(0.01);
143 m_sliceExploded = new QCheckBox();
143 m_sliceExploded = new QCheckBox();
144 m_sliceExplodedFactor = new QDoubleSpinBox();
144 m_sliceExplodedFactor = new QDoubleSpinBox();
145 m_sliceExplodedFactor->setSingleStep(0.01);
145 m_sliceExplodedFactor->setSingleStep(0.01);
146 m_pen = new QPushButton();
146 m_pen = new QPushButton();
147 m_penTool = new PenTool("Slice pen", this);
147 m_penTool = new PenTool("Slice pen", this);
148 m_brush = new QPushButton();
148 m_brush = new QPushButton();
149 m_brushTool = new BrushTool("Slice brush", this);
149 m_brushTool = new BrushTool("Slice brush", this);
150 m_font = new QPushButton();
150 m_font = new QPushButton();
151 m_labelPen = new QPushButton();
151 m_labelPen = new QPushButton();
152 m_labelPenTool = new PenTool("Label pen", this);
152 m_labelPenTool = new PenTool("Label pen", this);
153 QPushButton *removeSlice = new QPushButton("Remove slice");
153 QPushButton *removeSlice = new QPushButton("Remove slice");
154
154
155 QFormLayout* sliceSettingsLayout = new QFormLayout();
155 QFormLayout* sliceSettingsLayout = new QFormLayout();
156 sliceSettingsLayout->addRow("Selected", m_sliceName);
156 sliceSettingsLayout->addRow("Selected", m_sliceName);
157 sliceSettingsLayout->addRow("Value", m_sliceValue);
157 sliceSettingsLayout->addRow("Value", m_sliceValue);
158 sliceSettingsLayout->addRow("Pen", m_pen);
158 sliceSettingsLayout->addRow("Pen", m_pen);
159 sliceSettingsLayout->addRow("Brush", m_brush);
159 sliceSettingsLayout->addRow("Brush", m_brush);
160 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
160 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
161 sliceSettingsLayout->addRow("Label font", m_font);
161 sliceSettingsLayout->addRow("Label font", m_font);
162 sliceSettingsLayout->addRow("Label pen", m_labelPen);
162 sliceSettingsLayout->addRow("Label pen", m_labelPen);
163 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
163 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
164 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
164 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
165 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
165 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
166 sliceSettingsLayout->addRow(removeSlice);
166 sliceSettingsLayout->addRow(removeSlice);
167 QGroupBox* sliceSettings = new QGroupBox("Slice");
167 QGroupBox* sliceSettings = new QGroupBox("Slice");
168 sliceSettings->setLayout(sliceSettingsLayout);
168 sliceSettings->setLayout(sliceSettingsLayout);
169
169
170 connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
170 connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
171 connect(m_pen, SIGNAL(clicked()), m_penTool, SLOT(show()));
171 connect(m_pen, SIGNAL(clicked()), m_penTool, SLOT(show()));
172 connect(m_penTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
172 connect(m_penTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
173 connect(m_brush, SIGNAL(clicked()), m_brushTool, SLOT(show()));
173 connect(m_brush, SIGNAL(clicked()), m_brushTool, SLOT(show()));
174 connect(m_brushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
174 connect(m_brushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
175 connect(m_font, SIGNAL(clicked()), this, SLOT(showFontDialog()));
175 connect(m_font, SIGNAL(clicked()), this, SLOT(showFontDialog()));
176 connect(m_labelPen, SIGNAL(clicked()), m_labelPenTool, SLOT(show()));
176 connect(m_labelPen, SIGNAL(clicked()), m_labelPenTool, SLOT(show()));
177 connect(m_labelPenTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
177 connect(m_labelPenTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
178 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
178 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
179 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
179 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
180 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
180 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
181 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
181 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
182 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
182 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
183 connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice()));
183 connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice()));
184
184
185 // create chart view
185 // create chart view
186 m_chartView = new QChartView(chart);
186 m_chartView = new QChartView(chart);
187
187
188 // create main layout
188 // create main layout
189 QVBoxLayout *settingsLayout = new QVBoxLayout();
189 QVBoxLayout *settingsLayout = new QVBoxLayout();
190 settingsLayout->addWidget(chartSettings);
190 settingsLayout->addWidget(chartSettings);
191 settingsLayout->addWidget(seriesSettings);
191 settingsLayout->addWidget(seriesSettings);
192 settingsLayout->addWidget(sliceSettings);
192 settingsLayout->addWidget(sliceSettings);
193 settingsLayout->addStretch();
193 settingsLayout->addStretch();
194
194
195 QGridLayout* baseLayout = new QGridLayout();
195 QGridLayout* baseLayout = new QGridLayout();
196 baseLayout->addLayout(settingsLayout, 0, 0);
196 baseLayout->addLayout(settingsLayout, 0, 0);
197 baseLayout->addWidget(m_chartView, 0, 1);
197 baseLayout->addWidget(m_chartView, 0, 1);
198 setLayout(baseLayout);
198 setLayout(baseLayout);
199
199
200 updateSerieSettings();
200 updateSerieSettings();
201 }
201 }
202
202
203
203
204 void MainWidget::updateChartSettings()
204 void MainWidget::updateChartSettings()
205 {
205 {
206 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
206 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
207 m_chartView->chart()->setTheme(theme);
207 m_chartView->chart()->setTheme(theme);
208 m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked());
208 m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked());
209
209
210 if (m_animationsCheckBox->checkState() == Qt::Checked)
210 if (m_animationsCheckBox->checkState() == Qt::Checked)
211 m_chartView->chart()->setAnimationOptions(QChart::AllAnimations);
211 m_chartView->chart()->setAnimationOptions(QChart::AllAnimations);
212 else
212 else
213 m_chartView->chart()->setAnimationOptions(QChart::NoAnimation);
213 m_chartView->chart()->setAnimationOptions(QChart::NoAnimation);
214 }
214 }
215
215
216 void MainWidget::updateSerieSettings()
216 void MainWidget::updateSerieSettings()
217 {
217 {
218 m_series->setHorizontalPosition(m_hPosition->value());
218 m_series->setHorizontalPosition(m_hPosition->value());
219 m_series->setVerticalPosition(m_vPosition->value());
219 m_series->setVerticalPosition(m_vPosition->value());
220 m_series->setPieSize(m_sizeFactor->value());
220 m_series->setPieSize(m_sizeFactor->value());
221 m_series->setPieStartAngle(m_startAngle->value());
221 m_series->setPieStartAngle(m_startAngle->value());
222 m_series->setPieEndAngle(m_endAngle->value());
222 m_series->setPieEndAngle(m_endAngle->value());
223 }
223 }
224
224
225 void MainWidget::updateSliceSettings()
225 void MainWidget::updateSliceSettings()
226 {
226 {
227 if (!m_slice)
227 if (!m_slice)
228 return;
228 return;
229
229
230 m_slice->setValue(m_sliceValue->value());
230 m_slice->setValue(m_sliceValue->value());
231
231
232 m_slice->setPen(m_penTool->pen());
232 m_slice->setPen(m_penTool->pen());
233 m_slice->setBrush(m_brushTool->brush());
233 m_slice->setBrush(m_brushTool->brush());
234
234
235 m_slice->setLabelPen(m_labelPenTool->pen());
235 m_slice->setLabelPen(m_labelPenTool->pen());
236 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
236 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
237 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
237 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
238
238
239 m_slice->setExploded(m_sliceExploded->isChecked());
239 m_slice->setExploded(m_sliceExploded->isChecked());
240 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
240 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
241 }
241 }
242
242
243 void MainWidget::handleSliceClicked(QPieSlice* slice, Qt::MouseButtons buttons)
243 void MainWidget::handleSliceClicked(QPieSlice* slice, Qt::MouseButtons buttons)
244 {
244 {
245 Q_UNUSED(buttons);
245 Q_UNUSED(buttons);
246
246
247 m_slice = static_cast<CustomSlice*>(slice);
247 m_slice = static_cast<CustomSlice*>(slice);
248
248
249 // name
249 // name
250 m_sliceName->setText(slice->label());
250 m_sliceName->setText(slice->label());
251
251
252 // value
252 // value
253 m_sliceValue->blockSignals(true);
253 m_sliceValue->blockSignals(true);
254 m_sliceValue->setValue(slice->value());
254 m_sliceValue->setValue(slice->value());
255 m_sliceValue->blockSignals(false);
255 m_sliceValue->blockSignals(false);
256
256
257 // pen
257 // pen
258 m_pen->setText(PenTool::name(m_slice->pen()));
258 m_pen->setText(PenTool::name(m_slice->pen()));
259 m_penTool->setPen(m_slice->pen());
259 m_penTool->setPen(m_slice->pen());
260
260
261 // brush
261 // brush
262 m_brush->setText(m_slice->originalBrush().color().name());
262 m_brush->setText(m_slice->originalBrush().color().name());
263 m_brushTool->setBrush(m_slice->originalBrush());
263 m_brushTool->setBrush(m_slice->originalBrush());
264
264
265 // label
265 // label
266 m_labelPen->setText(PenTool::name(m_slice->labelPen()));
266 m_labelPen->setText(PenTool::name(m_slice->labelPen()));
267 m_labelPenTool->setPen(m_slice->labelPen());
267 m_labelPenTool->setPen(m_slice->labelPen());
268 m_font->setText(slice->labelFont().toString());
268 m_font->setText(slice->labelFont().toString());
269 m_sliceLabelVisible->blockSignals(true);
269 m_sliceLabelVisible->blockSignals(true);
270 m_sliceLabelVisible->setChecked(slice->isLabelVisible());
270 m_sliceLabelVisible->setChecked(slice->isLabelVisible());
271 m_sliceLabelVisible->blockSignals(false);
271 m_sliceLabelVisible->blockSignals(false);
272 m_sliceLabelArmFactor->blockSignals(true);
272 m_sliceLabelArmFactor->blockSignals(true);
273 m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
273 m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
274 m_sliceLabelArmFactor->blockSignals(false);
274 m_sliceLabelArmFactor->blockSignals(false);
275
275
276 // exploded
276 // exploded
277 m_sliceExploded->blockSignals(true);
277 m_sliceExploded->blockSignals(true);
278 m_sliceExploded->setChecked(slice->isExploded());
278 m_sliceExploded->setChecked(slice->isExploded());
279 m_sliceExploded->blockSignals(false);
279 m_sliceExploded->blockSignals(false);
280 m_sliceExplodedFactor->blockSignals(true);
280 m_sliceExplodedFactor->blockSignals(true);
281 m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
281 m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
282 m_sliceExplodedFactor->blockSignals(false);
282 m_sliceExplodedFactor->blockSignals(false);
283 }
283 }
284
284
285 void MainWidget::showFontDialog()
285 void MainWidget::showFontDialog()
286 {
286 {
287 if (!m_slice)
287 if (!m_slice)
288 return;
288 return;
289
289
290 QFontDialog dialog(m_slice->labelFont());
290 QFontDialog dialog(m_slice->labelFont());
291 dialog.show();
291 dialog.show();
292 dialog.exec();
292 dialog.exec();
293
293
294 m_slice->setLabelFont(dialog.currentFont());
294 m_slice->setLabelFont(dialog.currentFont());
295 m_font->setText(dialog.currentFont().toString());
295 m_font->setText(dialog.currentFont().toString());
296 }
296 }
297
297
298 void MainWidget::addSlice()
298 void MainWidget::appendSlice()
299 {
299 {
300 *m_series << new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1));
300 *m_series << new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1));
301 }
301 }
302
302
303 void MainWidget::insertSlice()
303 void MainWidget::insertSlice()
304 {
304 {
305 if (!m_slice)
305 if (!m_slice)
306 return;
306 return;
307
307
308 int i = m_series->slices().indexOf(m_slice);
308 int i = m_series->slices().indexOf(m_slice);
309
309
310 m_series->insert(i, new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1)));
310 m_series->insert(i, new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1)));
311 }
311 }
312
312
313 void MainWidget::removeSlice()
313 void MainWidget::removeSlice()
314 {
314 {
315 if (!m_slice)
315 if (!m_slice)
316 return;
316 return;
317
317
318 m_sliceName->setText("<click a slice>");
319
318 m_series->remove(m_slice);
320 m_series->remove(m_slice);
319 m_slice = 0;
321 m_slice = 0;
320 }
322 }
321
323
322 #include "moc_mainwidget.cpp"
324 #include "moc_mainwidget.cpp"
@@ -1,90 +1,90
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20 #ifndef MAINWIDGET_H
20 #ifndef MAINWIDGET_H
21 #define MAINWIDGET_H
21 #define MAINWIDGET_H
22
22
23 #include <QWidget>
23 #include <QWidget>
24 #include <QChartGlobal>
24 #include <QChartGlobal>
25
25
26 class QLabel;
26 class QLabel;
27 class QPushButton;
27 class QPushButton;
28 class QCheckBox;
28 class QCheckBox;
29 class QComboBox;
29 class QComboBox;
30 class QDoubleSpinBox;
30 class QDoubleSpinBox;
31 class PenTool;
31 class PenTool;
32 class BrushTool;
32 class BrushTool;
33 class CustomSlice;
33 class CustomSlice;
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 class QChartView;
36 class QChartView;
37 class QPieSeries;
37 class QPieSeries;
38 class QPieSlice;
38 class QPieSlice;
39 QTCOMMERCIALCHART_END_NAMESPACE
39 QTCOMMERCIALCHART_END_NAMESPACE
40
40
41 QTCOMMERCIALCHART_USE_NAMESPACE
41 QTCOMMERCIALCHART_USE_NAMESPACE
42
42
43 class MainWidget : public QWidget
43 class MainWidget : public QWidget
44 {
44 {
45 Q_OBJECT
45 Q_OBJECT
46
46
47 public:
47 public:
48 explicit MainWidget(QWidget* parent = 0);
48 explicit MainWidget(QWidget* parent = 0);
49
49
50 public Q_SLOTS:
50 public Q_SLOTS:
51 void updateChartSettings();
51 void updateChartSettings();
52 void updateSerieSettings();
52 void updateSerieSettings();
53 void updateSliceSettings();
53 void updateSliceSettings();
54 void handleSliceClicked(QPieSlice* slice, Qt::MouseButtons buttons);
54 void handleSliceClicked(QPieSlice* slice, Qt::MouseButtons buttons);
55 void showFontDialog();
55 void showFontDialog();
56 void addSlice();
56 void appendSlice();
57 void insertSlice();
57 void insertSlice();
58 void removeSlice();
58 void removeSlice();
59
59
60 private:
60 private:
61 QComboBox *m_themeComboBox;
61 QComboBox *m_themeComboBox;
62 QCheckBox *m_aaCheckBox;
62 QCheckBox *m_aaCheckBox;
63 QCheckBox *m_animationsCheckBox;
63 QCheckBox *m_animationsCheckBox;
64
64
65 QChartView* m_chartView;
65 QChartView* m_chartView;
66 QPieSeries* m_series;
66 QPieSeries* m_series;
67 CustomSlice* m_slice;
67 CustomSlice* m_slice;
68
68
69 QDoubleSpinBox* m_hPosition;
69 QDoubleSpinBox* m_hPosition;
70 QDoubleSpinBox* m_vPosition;
70 QDoubleSpinBox* m_vPosition;
71 QDoubleSpinBox* m_sizeFactor;
71 QDoubleSpinBox* m_sizeFactor;
72 QDoubleSpinBox* m_startAngle;
72 QDoubleSpinBox* m_startAngle;
73 QDoubleSpinBox* m_endAngle;
73 QDoubleSpinBox* m_endAngle;
74
74
75 QLabel* m_sliceName;
75 QLabel* m_sliceName;
76 QDoubleSpinBox* m_sliceValue;
76 QDoubleSpinBox* m_sliceValue;
77 QCheckBox* m_sliceLabelVisible;
77 QCheckBox* m_sliceLabelVisible;
78 QDoubleSpinBox* m_sliceLabelArmFactor;
78 QDoubleSpinBox* m_sliceLabelArmFactor;
79 QCheckBox* m_sliceExploded;
79 QCheckBox* m_sliceExploded;
80 QDoubleSpinBox* m_sliceExplodedFactor;
80 QDoubleSpinBox* m_sliceExplodedFactor;
81 QPushButton *m_brush;
81 QPushButton *m_brush;
82 BrushTool *m_brushTool;
82 BrushTool *m_brushTool;
83 QPushButton *m_pen;
83 QPushButton *m_pen;
84 PenTool *m_penTool;
84 PenTool *m_penTool;
85 QPushButton *m_font;
85 QPushButton *m_font;
86 QPushButton *m_labelPen;
86 QPushButton *m_labelPen;
87 PenTool *m_labelPenTool;
87 PenTool *m_labelPenTool;
88 };
88 };
89
89
90 #endif // MAINWIDGET_H
90 #endif // MAINWIDGET_H
@@ -1,69 +1,69
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QApplication>
21 #include <QApplication>
22 #include <QMainWindow>
22 #include <QMainWindow>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QPieSeries>
24 #include <QPieSeries>
25 #include <QPieSlice>
25 #include <QPieSlice>
26
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
28
29 int main(int argc, char *argv[])
29 int main(int argc, char *argv[])
30 {
30 {
31 QApplication a(argc, argv);
31 QApplication a(argc, argv);
32
32
33 //![1]
33 //![1]
34 QPieSeries *series = new QPieSeries();
34 QPieSeries *series = new QPieSeries();
35 series->append(1, "Slice 1");
35 series->append(1, "Jane");
36 series->append(2, "Slice 2");
36 series->append(2, "Joe");
37 series->append(3, "Slice 3");
37 series->append(3, "Andy");
38 series->append(4, "Slice 4");
38 series->append(4, "Barbara");
39 series->append(5, "Slice 5");
39 series->append(5, "Axel");
40 //![1]
40 //![1]
41
41
42 //![2]
42 //![2]
43 QPieSlice *slice = series->slices().first();
43 QPieSlice *slice = series->slices().at(1);
44 slice->setExploded();
44 slice->setExploded();
45 slice->setLabelVisible();
45 slice->setLabelVisible();
46 slice->setPen(QPen(Qt::darkGreen, 2));
46 slice->setPen(QPen(Qt::darkGreen, 2));
47 slice->setBrush(Qt::green);
47 slice->setBrush(Qt::green);
48 //![2]
48 //![2]
49
49
50 //![3]
50 //![3]
51 QChart* chart = new QChart();
51 QChart* chart = new QChart();
52 chart->addSeries(series);
52 chart->addSeries(series);
53 chart->setTitle("Simple piechart example");
53 chart->setTitle("Simple piechart example");
54 //![3]
54 //![3]
55
55
56 //![4]
56 //![4]
57 QChartView* chartView = new QChartView(chart);
57 QChartView* chartView = new QChartView(chart);
58 chartView->setRenderHint(QPainter::Antialiasing);
58 chartView->setRenderHint(QPainter::Antialiasing);
59 //![4]
59 //![4]
60
60
61 //![5]
61 //![5]
62 QMainWindow window;
62 QMainWindow window;
63 window.setCentralWidget(chartView);
63 window.setCentralWidget(chartView);
64 window.resize(400, 300);
64 window.resize(400, 300);
65 window.show();
65 window.show();
66 //![5]
66 //![5]
67
67
68 return a.exec();
68 return a.exec();
69 }
69 }
@@ -1,56 +1,69
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "drilldownslice.h"
21 #include "drilldownslice.h"
22
22
23 QTCOMMERCIALCHART_USE_NAMESPACE
23 QTCOMMERCIALCHART_USE_NAMESPACE
24
24
25 DrilldownSlice::DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries)
25 DrilldownSlice::DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries)
26 :m_drilldownSeries(drilldownSeries),
26 :m_drilldownSeries(drilldownSeries),
27 m_prefix(prefix)
27 m_prefix(prefix)
28 {
28 {
29 setValue(value);
29 setValue(value);
30 setLabelVisible(true);
31 updateLabel();
30 updateLabel();
32 connect(this, SIGNAL(changed()), this, SLOT(updateLabel()));
31 connect(this, SIGNAL(changed()), this, SLOT(updateLabel()));
32 connect(this, SIGNAL(hoverEnter()), this, SLOT(showHighlight()));
33 connect(this, SIGNAL(hoverLeave()), this, SLOT(hideHighlight()));
33 }
34 }
34
35
35 DrilldownSlice::~DrilldownSlice()
36 DrilldownSlice::~DrilldownSlice()
36 {
37 {
37
38
38 }
39 }
39
40
40 QSeries* DrilldownSlice::drilldownSeries() const
41 QSeries* DrilldownSlice::drilldownSeries() const
41 {
42 {
42 return m_drilldownSeries;
43 return m_drilldownSeries;
43 }
44 }
44
45
45 void DrilldownSlice::updateLabel()
46 void DrilldownSlice::updateLabel()
46 {
47 {
47 QString label = m_prefix;
48 QString label = m_prefix;
48 label += " ";
49 label += " $";
49 label += QString::number(this->value());
50 label += QString::number(this->value());
50 label += "e, ";
51 label += ", ";
51 label += QString::number(this->percentage()*100, 'f', 1);
52 label += QString::number(this->percentage()*100, 'f', 1);
52 label += "%";
53 label += "%";
53 setLabel(label);
54 setLabel(label);
54 }
55 }
55
56
57 void DrilldownSlice::showHighlight()
58 {
59 setExploded();
60 setLabelVisible();
61 }
62
63 void DrilldownSlice::hideHighlight()
64 {
65 setExploded(false);
66 setLabelVisible(false);
67 }
68
56 #include "moc_drilldownslice.cpp"
69 #include "moc_drilldownslice.cpp"
@@ -1,48 +1,50
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20 #ifndef DRILLDOWNSLICE_H
20 #ifndef DRILLDOWNSLICE_H
21 #define DRILLDOWNSLICE_H
21 #define DRILLDOWNSLICE_H
22
22
23 #include <QPieSlice>
23 #include <QPieSlice>
24
24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 class QSeries;
26 class QSeries;
27 QTCOMMERCIALCHART_END_NAMESPACE
27 QTCOMMERCIALCHART_END_NAMESPACE
28
28
29 QTCOMMERCIALCHART_USE_NAMESPACE
29 QTCOMMERCIALCHART_USE_NAMESPACE
30
30
31 class DrilldownSlice : public QPieSlice
31 class DrilldownSlice : public QPieSlice
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public:
35 public:
36 DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries);
36 DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries);
37 virtual ~DrilldownSlice();
37 virtual ~DrilldownSlice();
38 QSeries* drilldownSeries() const;
38 QSeries* drilldownSeries() const;
39
39
40 public Q_SLOTS:
40 public Q_SLOTS:
41 void updateLabel();
41 void updateLabel();
42 void showHighlight();
43 void hideHighlight();
42
44
43 private:
45 private:
44 QSeries* m_drilldownSeries;
46 QSeries* m_drilldownSeries;
45 QString m_prefix;
47 QString m_prefix;
46 };
48 };
47
49
48 #endif // DRILLDOWNSLICE_H
50 #endif // DRILLDOWNSLICE_H
@@ -1,74 +1,77
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "drilldownchart.h"
21 #include "drilldownchart.h"
22 #include "drilldownslice.h"
22 #include "drilldownslice.h"
23 #include <QtGui/QApplication>
23 #include <QtGui/QApplication>
24 #include <QMainWindow>
24 #include <QMainWindow>
25 #include <QTime>
25 #include <QTime>
26 #include <QChartView>
26 #include <QChartView>
27 #include <QLegend>
27 #include <QPieSeries>
28 #include <QPieSeries>
28
29
29 QTCOMMERCIALCHART_USE_NAMESPACE
30 QTCOMMERCIALCHART_USE_NAMESPACE
30
31
31 int main(int argc, char *argv[])
32 int main(int argc, char *argv[])
32 {
33 {
33 QApplication a(argc, argv);
34 QApplication a(argc, argv);
34
35
35 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
36 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
36
37
37 QMainWindow window;
38 QMainWindow window;
38
39
39 DrilldownChart* chart = new DrilldownChart();
40 DrilldownChart* chart = new DrilldownChart();
40 chart->setTheme(QChart::ChartThemeLight);
41 chart->setTheme(QChart::ChartThemeLight);
41 chart->setAnimationOptions(QChart::AllAnimations);
42 chart->setAnimationOptions(QChart::AllAnimations);
43 chart->legend()->setVisible(true);
44 chart->legend()->setAlignmnent(QLegend::AlignmentRight);
42
45
43 QPieSeries* yearSeries = new QPieSeries(&window);
46 QPieSeries* yearSeries = new QPieSeries(&window);
44 yearSeries->setName("Sales by year - All");
47 yearSeries->setName("Sales by year - All");
45
48
46 QList<QString> months;
49 QList<QString> months;
47 months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
50 months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
48 QList<QString> names;
51 QList<QString> names;
49 names << "Jane" << "John" << "Axel" << "Mary" << "Samantha" << "Bob";
52 names << "Jane" << "John" << "Axel" << "Mary" << "Samantha" << "Bob";
50
53
51 foreach (QString name, names) {
54 foreach (QString name, names) {
52 QPieSeries* series = new QPieSeries(&window);
55 QPieSeries* series = new QPieSeries(&window);
53 series->setName("Sales by month - " + name);
56 series->setName("Sales by month - " + name);
54
57
55 foreach (QString month, months)
58 foreach (QString month, months)
56 *series << new DrilldownSlice(qrand() % 1000, month, yearSeries);
59 *series << new DrilldownSlice(qrand() % 1000, month, yearSeries);
57
60
58 QObject::connect(series, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), chart, SLOT(handleSliceClicked(QPieSlice*)));
61 QObject::connect(series, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), chart, SLOT(handleSliceClicked(QPieSlice*)));
59
62
60 *yearSeries << new DrilldownSlice(series->total(), name, series);
63 *yearSeries << new DrilldownSlice(series->total(), name, series);
61 }
64 }
62
65
63 QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), chart, SLOT(handleSliceClicked(QPieSlice*)));
66 QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), chart, SLOT(handleSliceClicked(QPieSlice*)));
64
67
65 chart->changeSeries(yearSeries);
68 chart->changeSeries(yearSeries);
66
69
67 QChartView* chartView = new QChartView(chart);
70 QChartView* chartView = new QChartView(chart);
68 chartView->setRenderHint(QPainter::Antialiasing);
71 chartView->setRenderHint(QPainter::Antialiasing);
69 window.setCentralWidget(chartView);
72 window.setCentralWidget(chartView);
70 window.resize(800, 600);
73 window.resize(800, 600);
71 window.show();
74 window.show();
72
75
73 return a.exec();
76 return a.exec();
74 }
77 }
@@ -1,212 +1,215
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "piechartitem_p.h"
21 #include "piechartitem_p.h"
22 #include "piesliceitem_p.h"
22 #include "piesliceitem_p.h"
23 #include "qpieslice.h"
23 #include "qpieslice.h"
24 #include "qpieseries.h"
24 #include "qpieseries.h"
25 #include "chartpresenter_p.h"
25 #include "chartpresenter_p.h"
26 #include "chartdataset_p.h"
26 #include "chartdataset_p.h"
27 #include "chartanimator_p.h"
27 #include "chartanimator_p.h"
28 #include <QDebug>
28 #include <QDebug>
29 #include <QPainter>
29 #include <QPainter>
30 #include <QTimer>
30 #include <QTimer>
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
34 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
35 :ChartItem(presenter),
35 :ChartItem(presenter),
36 m_series(series)
36 m_series(series)
37 {
37 {
38 Q_ASSERT(series);
38 Q_ASSERT(series);
39 connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
39 connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
40 connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>)));
40 connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>)));
41 connect(series, SIGNAL(piePositionChanged()), this, SLOT(handlePieLayoutChanged()));
41 connect(series, SIGNAL(piePositionChanged()), this, SLOT(handlePieLayoutChanged()));
42 connect(series, SIGNAL(pieSizeChanged()), this, SLOT(handlePieLayoutChanged()));
42 connect(series, SIGNAL(pieSizeChanged()), this, SLOT(handlePieLayoutChanged()));
43
43
44 QTimer::singleShot(0, this, SLOT(initialize()));
44 QTimer::singleShot(0, this, SLOT(initialize()));
45
45
46 // Note: the following does not affect as long as the item does not have anything to paint
46 // Note: the following does not affect as long as the item does not have anything to paint
47 setZValue(ChartPresenter::PieSeriesZValue);
47 setZValue(ChartPresenter::PieSeriesZValue);
48 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
48
49 // If enabled slice boundingrect() is called instead of shape().
50 // And this causes severe issues with mouse click & hover decection.
51 //setFlags(QGraphicsItem::ItemClipsChildrenToShape);
49 }
52 }
50
53
51 PieChartItem::~PieChartItem()
54 PieChartItem::~PieChartItem()
52 {
55 {
53 // slices deleted automatically through QGraphicsItem
56 // slices deleted automatically through QGraphicsItem
54 }
57 }
55
58
56 void PieChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
59 void PieChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
57 {
60 {
58 Q_UNUSED(painter)
61 Q_UNUSED(painter)
59 // TODO: paint shadows for all components
62 // TODO: paint shadows for all components
60 // - get paths from items & merge & offset and draw with shadow color?
63 // - get paths from items & merge & offset and draw with shadow color?
61 //painter->setBrush(QBrush(Qt::red));
64 //painter->setBrush(QBrush(Qt::red));
62 //painter->drawRect(m_debugRect);
65 //painter->drawRect(m_debugRect);
63 }
66 }
64
67
65 void PieChartItem::initialize()
68 void PieChartItem::initialize()
66 {
69 {
67 handleSlicesAdded(m_series->slices());
70 handleSlicesAdded(m_series->slices());
68 }
71 }
69
72
70 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
73 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
71 {
74 {
72 bool isEmpty = m_slices.isEmpty();
75 bool isEmpty = m_slices.isEmpty();
73
76
74 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series), false);
77 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series), false);
75
78
76 foreach (QPieSlice *s, slices) {
79 foreach (QPieSlice *s, slices) {
77 PieSliceItem* item = new PieSliceItem(this);
80 PieSliceItem* item = new PieSliceItem(this);
78 m_slices.insert(s, item);
81 m_slices.insert(s, item);
79 connect(s, SIGNAL(changed()), this, SLOT(handleSliceChanged()));
82 connect(s, SIGNAL(changed()), this, SLOT(handleSliceChanged()));
80 connect(item, SIGNAL(clicked(Qt::MouseButtons)), s, SIGNAL(clicked(Qt::MouseButtons)));
83 connect(item, SIGNAL(clicked(Qt::MouseButtons)), s, SIGNAL(clicked(Qt::MouseButtons)));
81 connect(item, SIGNAL(hoverEnter()), s, SIGNAL(hoverEnter()));
84 connect(item, SIGNAL(hoverEnter()), s, SIGNAL(hoverEnter()));
82 connect(item, SIGNAL(hoverLeave()), s, SIGNAL(hoverLeave()));
85 connect(item, SIGNAL(hoverLeave()), s, SIGNAL(hoverLeave()));
83
86
84 PieSliceData data = sliceData(s);
87 PieSliceData data = sliceData(s);
85
88
86 if (animator())
89 if (animator())
87 animator()->addAnimation(this, s, data, isEmpty);
90 animator()->addAnimation(this, s, data, isEmpty);
88 else
91 else
89 setLayout(s, data);
92 setLayout(s, data);
90 }
93 }
91 }
94 }
92
95
93 void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices)
96 void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices)
94 {
97 {
95 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series), false);
98 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series), false);
96
99
97 foreach (QPieSlice *s, slices) {
100 foreach (QPieSlice *s, slices) {
98 if (animator())
101 if (animator())
99 animator()->removeAnimation(this, s);
102 animator()->removeAnimation(this, s);
100 else
103 else
101 destroySlice(s);
104 destroySlice(s);
102 }
105 }
103 }
106 }
104
107
105 void PieChartItem::handlePieLayoutChanged()
108 void PieChartItem::handlePieLayoutChanged()
106 {
109 {
107 PieLayout layout = calculateLayout();
110 PieLayout layout = calculateLayout();
108 applyLayout(layout);
111 applyLayout(layout);
109 update();
112 update();
110 }
113 }
111
114
112 void PieChartItem::handleSliceChanged()
115 void PieChartItem::handleSliceChanged()
113 {
116 {
114 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
117 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
115 Q_ASSERT(m_slices.contains(slice));
118 Q_ASSERT(m_slices.contains(slice));
116 PieSliceData data = sliceData(slice);
119 PieSliceData data = sliceData(slice);
117 updateLayout(slice, data);
120 updateLayout(slice, data);
118 update();
121 update();
119 }
122 }
120
123
121 void PieChartItem::handleDomainChanged(qreal, qreal, qreal, qreal)
124 void PieChartItem::handleDomainChanged(qreal, qreal, qreal, qreal)
122 {
125 {
123 // TODO
126 // TODO
124 }
127 }
125
128
126 void PieChartItem::handleGeometryChanged(const QRectF& rect)
129 void PieChartItem::handleGeometryChanged(const QRectF& rect)
127 {
130 {
128 prepareGeometryChange();
131 prepareGeometryChange();
129 m_rect = rect;
132 m_rect = rect;
130 handlePieLayoutChanged();
133 handlePieLayoutChanged();
131 }
134 }
132
135
133 void PieChartItem::calculatePieLayout()
136 void PieChartItem::calculatePieLayout()
134 {
137 {
135 // find pie center coordinates
138 // find pie center coordinates
136 m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->horizontalPosition()));
139 m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->horizontalPosition()));
137 m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->verticalPosition()));
140 m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->verticalPosition()));
138
141
139 // find maximum radius for pie
142 // find maximum radius for pie
140 m_pieRadius = m_rect.height() / 2;
143 m_pieRadius = m_rect.height() / 2;
141 if (m_rect.width() < m_rect.height())
144 if (m_rect.width() < m_rect.height())
142 m_pieRadius = m_rect.width() / 2;
145 m_pieRadius = m_rect.width() / 2;
143
146
144 // apply size factor
147 // apply size factor
145 m_pieRadius *= m_series->pieSize();
148 m_pieRadius *= m_series->pieSize();
146 }
149 }
147
150
148 PieSliceData PieChartItem::sliceData(QPieSlice *slice)
151 PieSliceData PieChartItem::sliceData(QPieSlice *slice)
149 {
152 {
150 // TODO: This function is kid of useless now. Refactor.
153 // TODO: This function is kid of useless now. Refactor.
151 PieSliceData sliceData = PieSliceData::data(slice);
154 PieSliceData sliceData = PieSliceData::data(slice);
152 sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice);
155 sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice);
153 sliceData.m_radius = m_pieRadius;
156 sliceData.m_radius = m_pieRadius;
154 return sliceData;
157 return sliceData;
155 }
158 }
156
159
157 PieLayout PieChartItem::calculateLayout()
160 PieLayout PieChartItem::calculateLayout()
158 {
161 {
159 calculatePieLayout();
162 calculatePieLayout();
160 PieLayout layout;
163 PieLayout layout;
161 foreach (QPieSlice* s, m_series->slices()) {
164 foreach (QPieSlice* s, m_series->slices()) {
162 if (m_slices.contains(s)) // calculate layout only for those slices that are already visible
165 if (m_slices.contains(s)) // calculate layout only for those slices that are already visible
163 layout.insert(s, sliceData(s));
166 layout.insert(s, sliceData(s));
164 }
167 }
165 return layout;
168 return layout;
166 }
169 }
167
170
168 void PieChartItem::applyLayout(const PieLayout &layout)
171 void PieChartItem::applyLayout(const PieLayout &layout)
169 {
172 {
170 if (animator())
173 if (animator())
171 animator()->updateLayout(this, layout);
174 animator()->updateLayout(this, layout);
172 else
175 else
173 setLayout(layout);
176 setLayout(layout);
174 }
177 }
175
178
176 void PieChartItem::updateLayout(QPieSlice *slice, const PieSliceData &sliceData)
179 void PieChartItem::updateLayout(QPieSlice *slice, const PieSliceData &sliceData)
177 {
180 {
178 if (animator())
181 if (animator())
179 animator()->updateLayout(this, slice, sliceData);
182 animator()->updateLayout(this, slice, sliceData);
180 else
183 else
181 setLayout(slice, sliceData);
184 setLayout(slice, sliceData);
182 }
185 }
183
186
184 void PieChartItem::setLayout(const PieLayout &layout)
187 void PieChartItem::setLayout(const PieLayout &layout)
185 {
188 {
186 foreach (QPieSlice *slice, layout.keys()) {
189 foreach (QPieSlice *slice, layout.keys()) {
187 PieSliceItem *item = m_slices.value(slice);
190 PieSliceItem *item = m_slices.value(slice);
188 Q_ASSERT(item);
191 Q_ASSERT(item);
189 item->setSliceData(layout.value(slice));
192 item->setSliceData(layout.value(slice));
190 item->updateGeometry();
193 item->updateGeometry();
191 item->update();
194 item->update();
192 }
195 }
193 }
196 }
194
197
195 void PieChartItem::setLayout(QPieSlice *slice, const PieSliceData &sliceData)
198 void PieChartItem::setLayout(QPieSlice *slice, const PieSliceData &sliceData)
196 {
199 {
197 // find slice
200 // find slice
198 PieSliceItem *item = m_slices.value(slice);
201 PieSliceItem *item = m_slices.value(slice);
199 Q_ASSERT(item);
202 Q_ASSERT(item);
200 item->setSliceData(sliceData);
203 item->setSliceData(sliceData);
201 item->updateGeometry();
204 item->updateGeometry();
202 item->update();
205 item->update();
203 }
206 }
204
207
205 void PieChartItem::destroySlice(QPieSlice *slice)
208 void PieChartItem::destroySlice(QPieSlice *slice)
206 {
209 {
207 delete m_slices.take(slice);
210 delete m_slices.take(slice);
208 }
211 }
209
212
210 #include "moc_piechartitem_p.cpp"
213 #include "moc_piechartitem_p.cpp"
211
214
212 QTCOMMERCIALCHART_END_NAMESPACE
215 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now