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