@@ -1,349 +1,363 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | #include "mainwidget.h" |
|
21 | 21 | #include "customslice.h" |
|
22 | 22 | #include "pentool.h" |
|
23 | 23 | #include "brushtool.h" |
|
24 | 24 | #include <QPushButton> |
|
25 | 25 | #include <QComboBox> |
|
26 | 26 | #include <QCheckBox> |
|
27 | 27 | #include <QLineEdit> |
|
28 | 28 | #include <QGroupBox> |
|
29 | 29 | #include <QDoubleSpinBox> |
|
30 | 30 | #include <QFormLayout> |
|
31 | 31 | #include <QFontDialog> |
|
32 | 32 | #include <QChartView> |
|
33 | 33 | #include <QPieSeries> |
|
34 | 34 | |
|
35 | 35 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
36 | 36 | |
|
37 | 37 | MainWidget::MainWidget(QWidget* parent) |
|
38 | 38 | :QWidget(parent), |
|
39 | 39 | m_slice(0) |
|
40 | 40 | { |
|
41 | 41 | // create chart |
|
42 | 42 | QChart *chart = new QChart; |
|
43 | 43 | chart->setTitle("Piechart customization"); |
|
44 | 44 | chart->setAnimationOptions(QChart::AllAnimations); |
|
45 | 45 | |
|
46 | 46 | // create series |
|
47 | 47 | m_series = new QPieSeries(); |
|
48 | 48 | *m_series << new CustomSlice("Slice 1", 10.0); |
|
49 | 49 | *m_series << new CustomSlice("Slice 2", 20.0); |
|
50 | 50 | *m_series << new CustomSlice("Slice 3", 30.0); |
|
51 | 51 | *m_series << new CustomSlice("Slice 4", 40.0); |
|
52 | 52 | *m_series << new CustomSlice("Slice 5", 50.0); |
|
53 | 53 | m_series->setLabelsVisible(); |
|
54 | 54 | chart->addSeries(m_series); |
|
55 | 55 | |
|
56 | 56 | connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*))); |
|
57 | 57 | |
|
58 | 58 | // chart settings |
|
59 | 59 | m_themeComboBox = new QComboBox(); |
|
60 | 60 | m_themeComboBox->addItem("Light", QChart::ChartThemeLight); |
|
61 | 61 | m_themeComboBox->addItem("BlueCerulean", QChart::ChartThemeBlueCerulean); |
|
62 | 62 | m_themeComboBox->addItem("Dark", QChart::ChartThemeDark); |
|
63 | 63 | m_themeComboBox->addItem("BrownSand", QChart::ChartThemeBrownSand); |
|
64 | 64 | m_themeComboBox->addItem("BlueNcs", QChart::ChartThemeBlueNcs); |
|
65 | 65 | m_themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast); |
|
66 | 66 | m_themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy); |
|
67 | 67 | |
|
68 | 68 | m_aaCheckBox = new QCheckBox(); |
|
69 | 69 | m_animationsCheckBox = new QCheckBox(); |
|
70 | 70 | m_animationsCheckBox->setCheckState(Qt::Checked); |
|
71 | 71 | |
|
72 | 72 | m_legendCheckBox = new QCheckBox(); |
|
73 | 73 | |
|
74 | 74 | QFormLayout* chartSettingsLayout = new QFormLayout(); |
|
75 | 75 | chartSettingsLayout->addRow("Theme", m_themeComboBox); |
|
76 | 76 | chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox); |
|
77 | 77 | chartSettingsLayout->addRow("Animations", m_animationsCheckBox); |
|
78 | 78 | chartSettingsLayout->addRow("Legend", m_legendCheckBox); |
|
79 | 79 | QGroupBox* chartSettings = new QGroupBox("Chart"); |
|
80 | 80 | chartSettings->setLayout(chartSettingsLayout); |
|
81 | 81 | |
|
82 | 82 | connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateChartSettings())); |
|
83 | 83 | connect(m_aaCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings())); |
|
84 | 84 | connect(m_animationsCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings())); |
|
85 | 85 | connect(m_legendCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings())); |
|
86 | 86 | |
|
87 | 87 | // series settings |
|
88 | 88 | m_hPosition = new QDoubleSpinBox(); |
|
89 | 89 | m_hPosition->setMinimum(0.0); |
|
90 | 90 | m_hPosition->setMaximum(1.0); |
|
91 | 91 | m_hPosition->setSingleStep(0.1); |
|
92 | 92 | m_hPosition->setValue(m_series->horizontalPosition()); |
|
93 | 93 | |
|
94 | 94 | m_vPosition = new QDoubleSpinBox(); |
|
95 | 95 | m_vPosition->setMinimum(0.0); |
|
96 | 96 | m_vPosition->setMaximum(1.0); |
|
97 | 97 | m_vPosition->setSingleStep(0.1); |
|
98 | 98 | m_vPosition->setValue(m_series->verticalPosition()); |
|
99 | 99 | |
|
100 | 100 | m_sizeFactor = new QDoubleSpinBox(); |
|
101 | 101 | m_sizeFactor->setMinimum(0.0); |
|
102 | 102 | m_sizeFactor->setMaximum(1.0); |
|
103 | 103 | m_sizeFactor->setSingleStep(0.1); |
|
104 | 104 | m_sizeFactor->setValue(m_series->pieSize()); |
|
105 | 105 | |
|
106 | 106 | m_startAngle = new QDoubleSpinBox(); |
|
107 | 107 | m_startAngle->setMinimum(-720); |
|
108 | 108 | m_startAngle->setMaximum(720); |
|
109 | 109 | m_startAngle->setValue(m_series->pieStartAngle()); |
|
110 | 110 | m_startAngle->setSingleStep(1); |
|
111 | 111 | |
|
112 | 112 | m_endAngle = new QDoubleSpinBox(); |
|
113 | 113 | m_endAngle->setMinimum(-720); |
|
114 | 114 | m_endAngle->setMaximum(720); |
|
115 | 115 | m_endAngle->setValue(m_series->pieEndAngle()); |
|
116 | 116 | m_endAngle->setSingleStep(1); |
|
117 | 117 | |
|
118 | m_isDonut = new QCheckBox(); | |
|
119 | ||
|
120 | m_donutInnerSize = new QDoubleSpinBox(); | |
|
121 | m_donutInnerSize->setMinimum(0.0); | |
|
122 | m_donutInnerSize->setMaximum(1.0); | |
|
123 | m_donutInnerSize->setSingleStep(0.1); | |
|
124 | m_donutInnerSize->setValue(m_series->donutInnerSize()); | |
|
125 | ||
|
118 | 126 | QPushButton *appendSlice = new QPushButton("Append slice"); |
|
119 | 127 | QPushButton *insertSlice = new QPushButton("Insert slice"); |
|
120 | 128 | QPushButton *removeSlice = new QPushButton("Remove selected slice"); |
|
121 | 129 | |
|
122 | 130 | QFormLayout* seriesSettingsLayout = new QFormLayout(); |
|
123 | 131 | seriesSettingsLayout->addRow("Horizontal position", m_hPosition); |
|
124 | 132 | seriesSettingsLayout->addRow("Vertical position", m_vPosition); |
|
125 | 133 | seriesSettingsLayout->addRow("Size factor", m_sizeFactor); |
|
126 | 134 | seriesSettingsLayout->addRow("Start angle", m_startAngle); |
|
127 | 135 | seriesSettingsLayout->addRow("End angle", m_endAngle); |
|
136 | seriesSettingsLayout->addRow("Is donut", m_isDonut); | |
|
137 | seriesSettingsLayout->addRow("Donut inner size", m_donutInnerSize); | |
|
128 | 138 | seriesSettingsLayout->addRow(appendSlice); |
|
129 | 139 | seriesSettingsLayout->addRow(insertSlice); |
|
130 | 140 | seriesSettingsLayout->addRow(removeSlice); |
|
131 | 141 | QGroupBox* seriesSettings = new QGroupBox("Series"); |
|
132 | 142 | seriesSettings->setLayout(seriesSettingsLayout); |
|
133 | 143 | |
|
134 | 144 | connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); |
|
135 | 145 | connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); |
|
136 | 146 | connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); |
|
137 | 147 | connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); |
|
138 | 148 | connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); |
|
149 | connect(m_isDonut, SIGNAL(toggled(bool)), this, SLOT(updateSerieSettings())); | |
|
150 | connect(m_donutInnerSize, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); | |
|
139 | 151 | connect(appendSlice, SIGNAL(clicked()), this, SLOT(appendSlice())); |
|
140 | 152 | connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice())); |
|
141 | 153 | connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice())); |
|
142 | 154 | |
|
143 | 155 | // slice settings |
|
144 | 156 | m_sliceName = new QLineEdit("<click a slice>"); |
|
145 | 157 | m_sliceName->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); |
|
146 | 158 | m_sliceValue = new QDoubleSpinBox(); |
|
147 | 159 | m_sliceValue->setMaximum(1000); |
|
148 | 160 | m_sliceLabelVisible = new QCheckBox(); |
|
149 | 161 | m_sliceLabelArmFactor = new QDoubleSpinBox(); |
|
150 | 162 | m_sliceLabelArmFactor->setSingleStep(0.01); |
|
151 | 163 | m_sliceExploded = new QCheckBox(); |
|
152 | 164 | m_sliceExplodedFactor = new QDoubleSpinBox(); |
|
153 | 165 | m_sliceExplodedFactor->setSingleStep(0.01); |
|
154 | 166 | m_pen = new QPushButton(); |
|
155 | 167 | m_penTool = new PenTool("Slice pen", this); |
|
156 | 168 | m_brush = new QPushButton(); |
|
157 | 169 | m_brushTool = new BrushTool("Slice brush", this); |
|
158 | 170 | m_font = new QPushButton(); |
|
159 | 171 | m_labelBrush = new QPushButton(); |
|
160 | 172 | m_labelBrushTool = new BrushTool("Label brush", this); |
|
161 | 173 | m_labelPosition = new QComboBox(this); |
|
162 | 174 | m_labelPosition->addItem("Outside", QPieSlice::LabelOutside); |
|
163 | 175 | m_labelPosition->addItem("Inside", QPieSlice::LabelInside); |
|
164 | 176 | m_labelPosition->addItem("Inside tangential", QPieSlice::LabelInsideTangential); |
|
165 | 177 | m_labelPosition->addItem("Inside normal", QPieSlice::LabelInsideNormal); |
|
166 | 178 | |
|
167 | 179 | QFormLayout* sliceSettingsLayout = new QFormLayout(); |
|
168 | 180 | sliceSettingsLayout->addRow("Label", m_sliceName); |
|
169 | 181 | sliceSettingsLayout->addRow("Value", m_sliceValue); |
|
170 | 182 | sliceSettingsLayout->addRow("Pen", m_pen); |
|
171 | 183 | sliceSettingsLayout->addRow("Brush", m_brush); |
|
172 | 184 | sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible); |
|
173 | 185 | sliceSettingsLayout->addRow("Label font", m_font); |
|
174 | 186 | sliceSettingsLayout->addRow("Label brush", m_labelBrush); |
|
175 | 187 | sliceSettingsLayout->addRow("Label position", m_labelPosition); |
|
176 | 188 | sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor); |
|
177 | 189 | sliceSettingsLayout->addRow("Exploded", m_sliceExploded); |
|
178 | 190 | sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor); |
|
179 | 191 | QGroupBox* sliceSettings = new QGroupBox("Selected slice"); |
|
180 | 192 | sliceSettings->setLayout(sliceSettingsLayout); |
|
181 | 193 | |
|
182 | 194 | connect(m_sliceName, SIGNAL(textChanged(QString)), this, SLOT(updateSliceSettings())); |
|
183 | 195 | connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); |
|
184 | 196 | connect(m_pen, SIGNAL(clicked()), m_penTool, SLOT(show())); |
|
185 | 197 | connect(m_penTool, SIGNAL(changed()), this, SLOT(updateSliceSettings())); |
|
186 | 198 | connect(m_brush, SIGNAL(clicked()), m_brushTool, SLOT(show())); |
|
187 | 199 | connect(m_brushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings())); |
|
188 | 200 | connect(m_font, SIGNAL(clicked()), this, SLOT(showFontDialog())); |
|
189 | 201 | connect(m_labelBrush, SIGNAL(clicked()), m_labelBrushTool, SLOT(show())); |
|
190 | 202 | connect(m_labelBrushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings())); |
|
191 | 203 | connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings())); |
|
192 | 204 | connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings())); |
|
193 | 205 | connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); |
|
194 | 206 | connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings())); |
|
195 | 207 | connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); |
|
196 | 208 | connect(m_labelPosition, SIGNAL(currentIndexChanged(int)), this, SLOT(updateSliceSettings())); |
|
197 | 209 | |
|
198 | 210 | // create chart view |
|
199 | 211 | m_chartView = new QChartView(chart); |
|
200 | 212 | |
|
201 | 213 | // create main layout |
|
202 | 214 | QVBoxLayout *settingsLayout = new QVBoxLayout(); |
|
203 | 215 | settingsLayout->addWidget(chartSettings); |
|
204 | 216 | settingsLayout->addWidget(seriesSettings); |
|
205 | 217 | settingsLayout->addWidget(sliceSettings); |
|
206 | 218 | settingsLayout->addStretch(); |
|
207 | 219 | |
|
208 | 220 | QGridLayout* baseLayout = new QGridLayout(); |
|
209 | 221 | baseLayout->addLayout(settingsLayout, 0, 0); |
|
210 | 222 | baseLayout->addWidget(m_chartView, 0, 1); |
|
211 | 223 | setLayout(baseLayout); |
|
212 | 224 | |
|
213 | 225 | updateSerieSettings(); |
|
214 | 226 | updateChartSettings(); |
|
215 | 227 | } |
|
216 | 228 | |
|
217 | 229 | |
|
218 | 230 | void MainWidget::updateChartSettings() |
|
219 | 231 | { |
|
220 | 232 | QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); |
|
221 | 233 | m_chartView->chart()->setTheme(theme); |
|
222 | 234 | m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked()); |
|
223 | 235 | |
|
224 | 236 | if (m_animationsCheckBox->checkState() == Qt::Checked) |
|
225 | 237 | m_chartView->chart()->setAnimationOptions(QChart::AllAnimations); |
|
226 | 238 | else |
|
227 | 239 | m_chartView->chart()->setAnimationOptions(QChart::NoAnimation); |
|
228 | 240 | |
|
229 | 241 | if (m_legendCheckBox->checkState() == Qt::Checked) |
|
230 | 242 | m_chartView->chart()->legend()->show(); |
|
231 | 243 | else |
|
232 | 244 | m_chartView->chart()->legend()->hide(); |
|
233 | 245 | } |
|
234 | 246 | |
|
235 | 247 | void MainWidget::updateSerieSettings() |
|
236 | 248 | { |
|
237 | 249 | m_series->setHorizontalPosition(m_hPosition->value()); |
|
238 | 250 | m_series->setVerticalPosition(m_vPosition->value()); |
|
239 | 251 | m_series->setPieSize(m_sizeFactor->value()); |
|
240 | 252 | m_series->setPieStartAngle(m_startAngle->value()); |
|
241 | 253 | m_series->setPieEndAngle(m_endAngle->value()); |
|
254 | m_series->setDonut(m_isDonut->isChecked()); | |
|
255 | m_series->setDonutInnerSize(m_donutInnerSize->value()); | |
|
242 | 256 | } |
|
243 | 257 | |
|
244 | 258 | void MainWidget::updateSliceSettings() |
|
245 | 259 | { |
|
246 | 260 | if (!m_slice) |
|
247 | 261 | return; |
|
248 | 262 | |
|
249 | 263 | m_slice->setLabel(m_sliceName->text()); |
|
250 | 264 | |
|
251 | 265 | m_slice->setValue(m_sliceValue->value()); |
|
252 | 266 | |
|
253 | 267 | m_slice->setPen(m_penTool->pen()); |
|
254 | 268 | m_slice->setBrush(m_brushTool->brush()); |
|
255 | 269 | |
|
256 | 270 | m_slice->setLabelBrush(m_labelBrushTool->brush()); |
|
257 | 271 | m_slice->setLabelVisible(m_sliceLabelVisible->isChecked()); |
|
258 | 272 | m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value()); |
|
259 | 273 | m_slice->setLabelPosition((QPieSlice::LabelPosition)m_labelPosition->currentIndex()); // assumes that index is in sync with the enum |
|
260 | 274 | |
|
261 | 275 | m_slice->setExploded(m_sliceExploded->isChecked()); |
|
262 | 276 | m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value()); |
|
263 | 277 | } |
|
264 | 278 | |
|
265 | 279 | void MainWidget::handleSliceClicked(QPieSlice* slice) |
|
266 | 280 | { |
|
267 | 281 | m_slice = static_cast<CustomSlice*>(slice); |
|
268 | 282 | |
|
269 | 283 | // name |
|
270 | 284 | m_sliceName->blockSignals(true); |
|
271 | 285 | m_sliceName->setText(slice->label()); |
|
272 | 286 | m_sliceName->blockSignals(false); |
|
273 | 287 | |
|
274 | 288 | // value |
|
275 | 289 | m_sliceValue->blockSignals(true); |
|
276 | 290 | m_sliceValue->setValue(slice->value()); |
|
277 | 291 | m_sliceValue->blockSignals(false); |
|
278 | 292 | |
|
279 | 293 | // pen |
|
280 | 294 | m_pen->setText(PenTool::name(m_slice->pen())); |
|
281 | 295 | m_penTool->setPen(m_slice->pen()); |
|
282 | 296 | |
|
283 | 297 | // brush |
|
284 | 298 | m_brush->setText(m_slice->originalBrush().color().name()); |
|
285 | 299 | m_brushTool->setBrush(m_slice->originalBrush()); |
|
286 | 300 | |
|
287 | 301 | // label |
|
288 | 302 | m_labelBrush->setText(BrushTool::name(m_slice->labelBrush())); |
|
289 | 303 | m_labelBrushTool->setBrush(m_slice->labelBrush()); |
|
290 | 304 | m_font->setText(slice->labelFont().toString()); |
|
291 | 305 | m_sliceLabelVisible->blockSignals(true); |
|
292 | 306 | m_sliceLabelVisible->setChecked(slice->isLabelVisible()); |
|
293 | 307 | m_sliceLabelVisible->blockSignals(false); |
|
294 | 308 | m_sliceLabelArmFactor->blockSignals(true); |
|
295 | 309 | m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor()); |
|
296 | 310 | m_sliceLabelArmFactor->blockSignals(false); |
|
297 | 311 | m_labelPosition->blockSignals(true); |
|
298 | 312 | m_labelPosition->setCurrentIndex(slice->labelPosition()); // assumes that index is in sync with the enum |
|
299 | 313 | m_labelPosition->blockSignals(false); |
|
300 | 314 | |
|
301 | 315 | // exploded |
|
302 | 316 | m_sliceExploded->blockSignals(true); |
|
303 | 317 | m_sliceExploded->setChecked(slice->isExploded()); |
|
304 | 318 | m_sliceExploded->blockSignals(false); |
|
305 | 319 | m_sliceExplodedFactor->blockSignals(true); |
|
306 | 320 | m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor()); |
|
307 | 321 | m_sliceExplodedFactor->blockSignals(false); |
|
308 | 322 | } |
|
309 | 323 | |
|
310 | 324 | void MainWidget::showFontDialog() |
|
311 | 325 | { |
|
312 | 326 | if (!m_slice) |
|
313 | 327 | return; |
|
314 | 328 | |
|
315 | 329 | QFontDialog dialog(m_slice->labelFont()); |
|
316 | 330 | dialog.show(); |
|
317 | 331 | dialog.exec(); |
|
318 | 332 | |
|
319 | 333 | m_slice->setLabelFont(dialog.currentFont()); |
|
320 | 334 | m_font->setText(dialog.currentFont().toString()); |
|
321 | 335 | } |
|
322 | 336 | |
|
323 | 337 | void MainWidget::appendSlice() |
|
324 | 338 | { |
|
325 | 339 | *m_series << new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0); |
|
326 | 340 | } |
|
327 | 341 | |
|
328 | 342 | void MainWidget::insertSlice() |
|
329 | 343 | { |
|
330 | 344 | if (!m_slice) |
|
331 | 345 | return; |
|
332 | 346 | |
|
333 | 347 | int i = m_series->slices().indexOf(m_slice); |
|
334 | 348 | |
|
335 | 349 | m_series->insert(i, new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0)); |
|
336 | 350 | } |
|
337 | 351 | |
|
338 | 352 | void MainWidget::removeSlice() |
|
339 | 353 | { |
|
340 | 354 | if (!m_slice) |
|
341 | 355 | return; |
|
342 | 356 | |
|
343 | 357 | m_sliceName->setText("<click a slice>"); |
|
344 | 358 | |
|
345 | 359 | m_series->remove(m_slice); |
|
346 | 360 | m_slice = 0; |
|
347 | 361 | } |
|
348 | 362 | |
|
349 | 363 | #include "moc_mainwidget.cpp" |
@@ -1,92 +1,94 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | #ifndef MAINWIDGET_H |
|
21 | 21 | #define MAINWIDGET_H |
|
22 | 22 | |
|
23 | 23 | #include <QWidget> |
|
24 | 24 | #include <QChartGlobal> |
|
25 | 25 | |
|
26 | 26 | class QLineEdit; |
|
27 | 27 | class QPushButton; |
|
28 | 28 | class QCheckBox; |
|
29 | 29 | class QComboBox; |
|
30 | 30 | class QDoubleSpinBox; |
|
31 | 31 | class PenTool; |
|
32 | 32 | class BrushTool; |
|
33 | 33 | class CustomSlice; |
|
34 | 34 | |
|
35 | 35 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
36 | 36 | class QChartView; |
|
37 | 37 | class QPieSeries; |
|
38 | 38 | class QPieSlice; |
|
39 | 39 | QTCOMMERCIALCHART_END_NAMESPACE |
|
40 | 40 | |
|
41 | 41 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
42 | 42 | |
|
43 | 43 | class MainWidget : public QWidget |
|
44 | 44 | { |
|
45 | 45 | Q_OBJECT |
|
46 | 46 | |
|
47 | 47 | public: |
|
48 | 48 | explicit MainWidget(QWidget* parent = 0); |
|
49 | 49 | |
|
50 | 50 | public Q_SLOTS: |
|
51 | 51 | void updateChartSettings(); |
|
52 | 52 | void updateSerieSettings(); |
|
53 | 53 | void updateSliceSettings(); |
|
54 | 54 | void handleSliceClicked(QPieSlice* slice); |
|
55 | 55 | void showFontDialog(); |
|
56 | 56 | void appendSlice(); |
|
57 | 57 | void insertSlice(); |
|
58 | 58 | void removeSlice(); |
|
59 | 59 | |
|
60 | 60 | private: |
|
61 | 61 | QComboBox *m_themeComboBox; |
|
62 | 62 | QCheckBox *m_aaCheckBox; |
|
63 | 63 | QCheckBox *m_animationsCheckBox; |
|
64 | 64 | QCheckBox *m_legendCheckBox; |
|
65 | 65 | |
|
66 | 66 | QChartView* m_chartView; |
|
67 | 67 | QPieSeries* m_series; |
|
68 | 68 | CustomSlice* m_slice; |
|
69 | 69 | |
|
70 | 70 | QDoubleSpinBox* m_hPosition; |
|
71 | 71 | QDoubleSpinBox* m_vPosition; |
|
72 | 72 | QDoubleSpinBox* m_sizeFactor; |
|
73 | 73 | QDoubleSpinBox* m_startAngle; |
|
74 | 74 | QDoubleSpinBox* m_endAngle; |
|
75 | QCheckBox* m_isDonut; | |
|
76 | QDoubleSpinBox* m_donutInnerSize; | |
|
75 | 77 | |
|
76 | 78 | QLineEdit* m_sliceName; |
|
77 | 79 | QDoubleSpinBox* m_sliceValue; |
|
78 | 80 | QCheckBox* m_sliceLabelVisible; |
|
79 | 81 | QDoubleSpinBox* m_sliceLabelArmFactor; |
|
80 | 82 | QCheckBox* m_sliceExploded; |
|
81 | 83 | QDoubleSpinBox* m_sliceExplodedFactor; |
|
82 | 84 | QPushButton *m_brush; |
|
83 | 85 | BrushTool *m_brushTool; |
|
84 | 86 | QPushButton *m_pen; |
|
85 | 87 | PenTool *m_penTool; |
|
86 | 88 | QPushButton *m_font; |
|
87 | 89 | QPushButton *m_labelBrush; |
|
88 | 90 | QComboBox *m_labelPosition; |
|
89 | 91 | BrushTool *m_labelBrushTool; |
|
90 | 92 | }; |
|
91 | 93 | |
|
92 | 94 | #endif // MAINWIDGET_H |
@@ -1,101 +1,107 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "pieanimation_p.h" |
|
22 | 22 | #include "piesliceanimation_p.h" |
|
23 | 23 | #include "piechartitem_p.h" |
|
24 | 24 | #include <QTimer> |
|
25 | 25 | |
|
26 | 26 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
27 | 27 | |
|
28 | 28 | PieAnimation::PieAnimation(PieChartItem *item) |
|
29 | 29 | :ChartAnimation(item), |
|
30 | 30 | m_item(item) |
|
31 | 31 | { |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | PieAnimation::~PieAnimation() |
|
35 | 35 | { |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | ChartAnimation* PieAnimation::updateValue(PieSliceItem *sliceItem, const PieSliceData &sliceData) |
|
39 | 39 | { |
|
40 | 40 | PieSliceAnimation *animation = m_animations.value(sliceItem); |
|
41 | 41 | Q_ASSERT(animation); |
|
42 | 42 | animation->stop(); |
|
43 | 43 | |
|
44 | 44 | animation->updateValue(sliceData); |
|
45 | 45 | animation->setDuration(ChartAnimationDuration); |
|
46 | 46 | animation->setEasingCurve(QEasingCurve::OutQuart); |
|
47 | 47 | |
|
48 | 48 | return animation; |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | ChartAnimation* PieAnimation::addSlice(PieSliceItem *sliceItem, const PieSliceData &sliceData, bool startupAnimation) |
|
52 | 52 | { |
|
53 | 53 | PieSliceAnimation *animation = new PieSliceAnimation(sliceItem); |
|
54 | 54 | m_animations.insert(sliceItem, animation); |
|
55 | 55 | |
|
56 | 56 | PieSliceData startValue = sliceData; |
|
57 | 57 | startValue.m_radius = 0; |
|
58 | 58 | if (startupAnimation) |
|
59 | 59 | startValue.m_startAngle = 0; |
|
60 | 60 | else |
|
61 | 61 | startValue.m_startAngle = sliceData.m_startAngle + (sliceData.m_angleSpan / 2); |
|
62 | 62 | startValue.m_angleSpan = 0; |
|
63 | animation->setValue(startValue, sliceData); | |
|
64 | 63 | |
|
64 | if (sliceData.m_donut) | |
|
65 | startValue.m_radius = sliceData.m_innerRadius; | |
|
66 | ||
|
67 | animation->setValue(startValue, sliceData); | |
|
65 | 68 | animation->setDuration(ChartAnimationDuration); |
|
66 | 69 | animation->setEasingCurve(QEasingCurve::OutQuart); |
|
67 | 70 | |
|
68 | 71 | return animation; |
|
69 | 72 | } |
|
70 | 73 | |
|
71 | 74 | ChartAnimation* PieAnimation::removeSlice(PieSliceItem *sliceItem) |
|
72 | 75 | { |
|
73 | 76 | PieSliceAnimation *animation = m_animations.value(sliceItem); |
|
74 | 77 | Q_ASSERT(animation); |
|
75 | 78 | animation->stop(); |
|
76 | 79 | |
|
77 | 80 | PieSliceData endValue = animation->currentSliceValue(); |
|
78 |
endValue.m_ |
|
|
81 | if (endValue.m_donut) | |
|
82 | endValue.m_radius = endValue.m_innerRadius; | |
|
83 | else | |
|
84 | endValue.m_radius = 0; | |
|
79 | 85 | endValue.m_startAngle = endValue.m_startAngle + endValue.m_angleSpan; |
|
80 | 86 | endValue.m_angleSpan = 0; |
|
81 | 87 | endValue.m_isLabelVisible = false; |
|
82 | 88 | |
|
83 | 89 | animation->updateValue(endValue); |
|
84 | 90 | animation->setDuration(ChartAnimationDuration); |
|
85 | 91 | animation->setEasingCurve(QEasingCurve::OutQuart); |
|
86 | 92 | |
|
87 | 93 | // PieSliceItem is the parent of PieSliceAnimation so the animation will be deleted as well.. |
|
88 | 94 | connect(animation, SIGNAL(finished()), sliceItem, SLOT(deleteLater())); |
|
89 | 95 | m_animations.remove(sliceItem); |
|
90 | 96 | |
|
91 | 97 | return animation; |
|
92 | 98 | } |
|
93 | 99 | |
|
94 | 100 | void PieAnimation::updateCurrentValue(const QVariant &) |
|
95 | 101 | { |
|
96 | 102 | // nothing to do... |
|
97 | 103 | } |
|
98 | 104 | |
|
99 | 105 | #include "moc_pieanimation_p.cpp" |
|
100 | 106 | |
|
101 | 107 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,125 +1,126 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "piesliceanimation_p.h" |
|
22 | 22 | #include "piechartitem_p.h" |
|
23 | 23 | |
|
24 | 24 | Q_DECLARE_METATYPE(QTCOMMERCIALCHART_NAMESPACE::PieSliceData) |
|
25 | 25 | |
|
26 | 26 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
27 | 27 | |
|
28 | 28 | qreal linearPos(qreal start, qreal end, qreal pos) |
|
29 | 29 | { |
|
30 | 30 | return start + ((end - start) * pos); |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | 33 | QPointF linearPos(QPointF start, QPointF end, qreal pos) |
|
34 | 34 | { |
|
35 | 35 | qreal x = linearPos(start.x(), end.x(), pos); |
|
36 | 36 | qreal y = linearPos(start.y(), end.y(), pos); |
|
37 | 37 | return QPointF(x, y); |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | QPen linearPos(QPen start, QPen end, qreal pos) |
|
41 | 41 | { |
|
42 | 42 | QColor c; |
|
43 | 43 | c.setRedF(linearPos(start.color().redF(), end.color().redF(), pos)); |
|
44 | 44 | c.setGreenF(linearPos(start.color().greenF(), end.color().greenF(), pos)); |
|
45 | 45 | c.setBlueF(linearPos(start.color().blueF(), end.color().blueF(), pos)); |
|
46 | 46 | end.setColor(c); |
|
47 | 47 | return end; |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | QBrush linearPos(QBrush start, QBrush end, qreal pos) |
|
51 | 51 | { |
|
52 | 52 | QColor c; |
|
53 | 53 | c.setRedF(linearPos(start.color().redF(), end.color().redF(), pos)); |
|
54 | 54 | c.setGreenF(linearPos(start.color().greenF(), end.color().greenF(), pos)); |
|
55 | 55 | c.setBlueF(linearPos(start.color().blueF(), end.color().blueF(), pos)); |
|
56 | 56 | end.setColor(c); |
|
57 | 57 | return end; |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | PieSliceAnimation::PieSliceAnimation(PieSliceItem *sliceItem) |
|
61 | 61 | :ChartAnimation(sliceItem), |
|
62 | 62 | m_sliceItem(sliceItem) |
|
63 | 63 | { |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | PieSliceAnimation::~PieSliceAnimation() |
|
67 | 67 | { |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | void PieSliceAnimation::setValue(const PieSliceData &startValue, const PieSliceData &endValue) |
|
71 | 71 | { |
|
72 | 72 | if (state() != QAbstractAnimation::Stopped) |
|
73 | 73 | stop(); |
|
74 | 74 | |
|
75 | 75 | m_currentValue = startValue; |
|
76 | 76 | |
|
77 | 77 | setKeyValueAt(0.0, qVariantFromValue(startValue)); |
|
78 | 78 | setKeyValueAt(1.0, qVariantFromValue(endValue)); |
|
79 | 79 | } |
|
80 | 80 | |
|
81 | 81 | void PieSliceAnimation::updateValue(const PieSliceData &endValue) |
|
82 | 82 | { |
|
83 | 83 | if (state() != QAbstractAnimation::Stopped) |
|
84 | 84 | stop(); |
|
85 | 85 | |
|
86 | 86 | setKeyValueAt(0.0, qVariantFromValue(m_currentValue)); |
|
87 | 87 | setKeyValueAt(1.0, qVariantFromValue(endValue)); |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | PieSliceData PieSliceAnimation::currentSliceValue() |
|
91 | 91 | { |
|
92 | 92 | // NOTE: |
|
93 | 93 | // We must use an internal current value because QVariantAnimation::currentValue() is updated |
|
94 | 94 | // before the animation is actually started. So if we get 2 updateValue() calls in a row the currentValue() |
|
95 | 95 | // will have the end value set from the first call and the second call will interpolate that instead of |
|
96 | 96 | // the original current value as it was before the first call. |
|
97 | 97 | return m_currentValue; |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | QVariant PieSliceAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const |
|
101 | 101 | { |
|
102 | 102 | PieSliceData startValue = qVariantValue<PieSliceData>(start); |
|
103 | 103 | PieSliceData endValue = qVariantValue<PieSliceData>(end); |
|
104 | 104 | |
|
105 | 105 | PieSliceData result; |
|
106 | 106 | result = endValue; |
|
107 | 107 | result.m_center = linearPos(startValue.m_center, endValue.m_center, progress); |
|
108 | 108 | result.m_radius = linearPos(startValue.m_radius, endValue.m_radius, progress); |
|
109 | 109 | result.m_startAngle = linearPos(startValue.m_startAngle, endValue.m_startAngle, progress); |
|
110 | 110 | result.m_angleSpan = linearPos(startValue.m_angleSpan, endValue.m_angleSpan, progress); |
|
111 | 111 | result.m_slicePen = linearPos(startValue.m_slicePen, endValue.m_slicePen, progress); |
|
112 | 112 | result.m_sliceBrush = linearPos(startValue.m_sliceBrush, endValue.m_sliceBrush, progress); |
|
113 | result.m_innerRadius = linearPos(startValue.m_innerRadius, endValue.m_innerRadius, progress); | |
|
113 | 114 | |
|
114 | 115 | return qVariantFromValue(result); |
|
115 | 116 | } |
|
116 | 117 | |
|
117 | 118 | void PieSliceAnimation::updateCurrentValue(const QVariant &value) |
|
118 | 119 | { |
|
119 | 120 | if (state() != QAbstractAnimation::Stopped) { //workaround |
|
120 | 121 | m_currentValue = qVariantValue<PieSliceData>(value); |
|
121 | 122 | m_sliceItem->setLayout(m_currentValue); |
|
122 | 123 | } |
|
123 | 124 | } |
|
124 | 125 | |
|
125 | 126 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now