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