##// END OF EJS Templates
Fix piechartcustomization example
Michal Klocek -
r1644:188b08492c42
parent child
Show More
@@ -1,346 +1,347
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 118 QPushButton *appendSlice = new QPushButton("Append slice");
119 119 QPushButton *insertSlice = new QPushButton("Insert slice");
120 120 QPushButton *removeSlice = new QPushButton("Remove selected slice");
121 121
122 122 QFormLayout* seriesSettingsLayout = new QFormLayout();
123 123 seriesSettingsLayout->addRow("Horizontal position", m_hPosition);
124 124 seriesSettingsLayout->addRow("Vertical position", m_vPosition);
125 125 seriesSettingsLayout->addRow("Size factor", m_sizeFactor);
126 126 seriesSettingsLayout->addRow("Start angle", m_startAngle);
127 127 seriesSettingsLayout->addRow("End angle", m_endAngle);
128 128 seriesSettingsLayout->addRow(appendSlice);
129 129 seriesSettingsLayout->addRow(insertSlice);
130 130 seriesSettingsLayout->addRow(removeSlice);
131 131 QGroupBox* seriesSettings = new QGroupBox("Series");
132 132 seriesSettings->setLayout(seriesSettingsLayout);
133 133
134 134 connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
135 135 connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
136 136 connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
137 137 connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
138 138 connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
139 139 connect(appendSlice, SIGNAL(clicked()), this, SLOT(appendSlice()));
140 140 connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice()));
141 141 connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice()));
142 142
143 143 // slice settings
144 144 m_sliceName = new QLineEdit("<click a slice>");
145 145 m_sliceName->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
146 146 m_sliceValue = new QDoubleSpinBox();
147 147 m_sliceValue->setMaximum(1000);
148 148 m_sliceLabelVisible = new QCheckBox();
149 149 m_sliceLabelArmFactor = new QDoubleSpinBox();
150 150 m_sliceLabelArmFactor->setSingleStep(0.01);
151 151 m_sliceExploded = new QCheckBox();
152 152 m_sliceExplodedFactor = new QDoubleSpinBox();
153 153 m_sliceExplodedFactor->setSingleStep(0.01);
154 154 m_pen = new QPushButton();
155 155 m_penTool = new PenTool("Slice pen", this);
156 156 m_brush = new QPushButton();
157 157 m_brushTool = new BrushTool("Slice brush", this);
158 158 m_font = new QPushButton();
159 159 m_labelBrush = new QPushButton();
160 160 m_labelBrushTool = new BrushTool("Label brush", this);
161 161 m_labelPosition = new QComboBox(this);
162 162 m_labelPosition->addItem("Outside", QPieSlice::LabelOutside);
163 163 m_labelPosition->addItem("Inside", QPieSlice::LabelInside);
164 164
165 165 QFormLayout* sliceSettingsLayout = new QFormLayout();
166 166 sliceSettingsLayout->addRow("Label", m_sliceName);
167 167 sliceSettingsLayout->addRow("Value", m_sliceValue);
168 168 sliceSettingsLayout->addRow("Pen", m_pen);
169 169 sliceSettingsLayout->addRow("Brush", m_brush);
170 170 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
171 171 sliceSettingsLayout->addRow("Label font", m_font);
172 172 sliceSettingsLayout->addRow("Label brush", m_labelBrush);
173 173 sliceSettingsLayout->addRow("Label position", m_labelPosition);
174 174 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
175 175 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
176 176 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
177 177 QGroupBox* sliceSettings = new QGroupBox("Selected slice");
178 178 sliceSettings->setLayout(sliceSettingsLayout);
179 179
180 180 connect(m_sliceName, SIGNAL(textChanged(QString)), this, SLOT(updateSliceSettings()));
181 181 connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
182 182 connect(m_pen, SIGNAL(clicked()), m_penTool, SLOT(show()));
183 183 connect(m_penTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
184 184 connect(m_brush, SIGNAL(clicked()), m_brushTool, SLOT(show()));
185 185 connect(m_brushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
186 186 connect(m_font, SIGNAL(clicked()), this, SLOT(showFontDialog()));
187 187 connect(m_labelBrush, SIGNAL(clicked()), m_labelBrushTool, SLOT(show()));
188 188 connect(m_labelBrushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
189 189 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
190 190 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
191 191 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
192 192 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
193 193 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
194 194 connect(m_labelPosition, SIGNAL(currentIndexChanged(int)), this, SLOT(updateSliceSettings()));
195 195
196 196 // create chart view
197 197 m_chartView = new QChartView(chart);
198 198
199 199 // create main layout
200 200 QVBoxLayout *settingsLayout = new QVBoxLayout();
201 201 settingsLayout->addWidget(chartSettings);
202 202 settingsLayout->addWidget(seriesSettings);
203 203 settingsLayout->addWidget(sliceSettings);
204 204 settingsLayout->addStretch();
205 205
206 206 QGridLayout* baseLayout = new QGridLayout();
207 207 baseLayout->addLayout(settingsLayout, 0, 0);
208 208 baseLayout->addWidget(m_chartView, 0, 1);
209 209 setLayout(baseLayout);
210 210
211 211 updateSerieSettings();
212 updateChartSettings();
212 213 }
213 214
214 215
215 216 void MainWidget::updateChartSettings()
216 217 {
217 218 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
218 219 m_chartView->chart()->setTheme(theme);
219 220 m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked());
220 221
221 222 if (m_animationsCheckBox->checkState() == Qt::Checked)
222 223 m_chartView->chart()->setAnimationOptions(QChart::AllAnimations);
223 224 else
224 225 m_chartView->chart()->setAnimationOptions(QChart::NoAnimation);
225 226
226 227 if (m_legendCheckBox->checkState() == Qt::Checked)
227 228 m_chartView->chart()->legend()->show();
228 229 else
229 230 m_chartView->chart()->legend()->hide();
230 231 }
231 232
232 233 void MainWidget::updateSerieSettings()
233 234 {
234 235 m_series->setHorizontalPosition(m_hPosition->value());
235 236 m_series->setVerticalPosition(m_vPosition->value());
236 237 m_series->setPieSize(m_sizeFactor->value());
237 238 m_series->setPieStartAngle(m_startAngle->value());
238 239 m_series->setPieEndAngle(m_endAngle->value());
239 240 }
240 241
241 242 void MainWidget::updateSliceSettings()
242 243 {
243 244 if (!m_slice)
244 245 return;
245 246
246 247 m_slice->setLabel(m_sliceName->text());
247 248
248 249 m_slice->setValue(m_sliceValue->value());
249 250
250 251 m_slice->setPen(m_penTool->pen());
251 252 m_slice->setBrush(m_brushTool->brush());
252 253
253 254 m_slice->setLabelBrush(m_labelBrushTool->brush());
254 255 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
255 256 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
256 257 m_slice->setLabelPosition((QPieSlice::LabelPosition)m_labelPosition->currentIndex()); // assumes that index is in sync with the enum
257 258
258 259 m_slice->setExploded(m_sliceExploded->isChecked());
259 260 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
260 261 }
261 262
262 263 void MainWidget::handleSliceClicked(QPieSlice* slice)
263 264 {
264 265 m_slice = static_cast<CustomSlice*>(slice);
265 266
266 267 // name
267 268 m_sliceName->blockSignals(true);
268 269 m_sliceName->setText(slice->label());
269 270 m_sliceName->blockSignals(false);
270 271
271 272 // value
272 273 m_sliceValue->blockSignals(true);
273 274 m_sliceValue->setValue(slice->value());
274 275 m_sliceValue->blockSignals(false);
275 276
276 277 // pen
277 278 m_pen->setText(PenTool::name(m_slice->pen()));
278 279 m_penTool->setPen(m_slice->pen());
279 280
280 281 // brush
281 282 m_brush->setText(m_slice->originalBrush().color().name());
282 283 m_brushTool->setBrush(m_slice->originalBrush());
283 284
284 285 // label
285 286 m_labelBrush->setText(BrushTool::name(m_slice->labelBrush()));
286 287 m_labelBrushTool->setBrush(m_slice->labelBrush());
287 288 m_font->setText(slice->labelFont().toString());
288 289 m_sliceLabelVisible->blockSignals(true);
289 290 m_sliceLabelVisible->setChecked(slice->isLabelVisible());
290 291 m_sliceLabelVisible->blockSignals(false);
291 292 m_sliceLabelArmFactor->blockSignals(true);
292 293 m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
293 294 m_sliceLabelArmFactor->blockSignals(false);
294 295 m_labelPosition->blockSignals(true);
295 296 m_labelPosition->setCurrentIndex(slice->labelPosition()); // assumes that index is in sync with the enum
296 297 m_labelPosition->blockSignals(false);
297 298
298 299 // exploded
299 300 m_sliceExploded->blockSignals(true);
300 301 m_sliceExploded->setChecked(slice->isExploded());
301 302 m_sliceExploded->blockSignals(false);
302 303 m_sliceExplodedFactor->blockSignals(true);
303 304 m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
304 305 m_sliceExplodedFactor->blockSignals(false);
305 306 }
306 307
307 308 void MainWidget::showFontDialog()
308 309 {
309 310 if (!m_slice)
310 311 return;
311 312
312 313 QFontDialog dialog(m_slice->labelFont());
313 314 dialog.show();
314 315 dialog.exec();
315 316
316 317 m_slice->setLabelFont(dialog.currentFont());
317 318 m_font->setText(dialog.currentFont().toString());
318 319 }
319 320
320 321 void MainWidget::appendSlice()
321 322 {
322 323 *m_series << new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0);
323 324 }
324 325
325 326 void MainWidget::insertSlice()
326 327 {
327 328 if (!m_slice)
328 329 return;
329 330
330 331 int i = m_series->slices().indexOf(m_slice);
331 332
332 333 m_series->insert(i, new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0));
333 334 }
334 335
335 336 void MainWidget::removeSlice()
336 337 {
337 338 if (!m_slice)
338 339 return;
339 340
340 341 m_sliceName->setText("<click a slice>");
341 342
342 343 m_series->remove(m_slice);
343 344 m_slice = 0;
344 345 }
345 346
346 347 #include "moc_mainwidget.cpp"
General Comments 0
You need to be logged in to leave comments. Login now