##// END OF EJS Templates
pie: add label position to slice
Jani Honkonen -
r1450:7bf1a442df6a
parent child
Show More
@@ -1,331 +1,340
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20 #include "mainwidget.h"
20 #include "mainwidget.h"
21 #include "customslice.h"
21 #include "customslice.h"
22 #include "pentool.h"
22 #include "pentool.h"
23 #include "brushtool.h"
23 #include "brushtool.h"
24 #include <QPushButton>
24 #include <QPushButton>
25 #include <QComboBox>
25 #include <QComboBox>
26 #include <QCheckBox>
26 #include <QCheckBox>
27 #include <QLabel>
27 #include <QLabel>
28 #include <QGroupBox>
28 #include <QGroupBox>
29 #include <QDoubleSpinBox>
29 #include <QDoubleSpinBox>
30 #include <QFormLayout>
30 #include <QFormLayout>
31 #include <QFontDialog>
31 #include <QFontDialog>
32 #include <QChartView>
32 #include <QChartView>
33 #include <QPieSeries>
33 #include <QPieSeries>
34
34
35 QTCOMMERCIALCHART_USE_NAMESPACE
35 QTCOMMERCIALCHART_USE_NAMESPACE
36
36
37 MainWidget::MainWidget(QWidget* parent)
37 MainWidget::MainWidget(QWidget* parent)
38 :QWidget(parent),
38 :QWidget(parent),
39 m_slice(0)
39 m_slice(0)
40 {
40 {
41 // create chart
41 // create chart
42 QChart *chart = new QChart;
42 QChart *chart = new QChart;
43 chart->setTitle("Piechart customization");
43 chart->setTitle("Piechart customization");
44 chart->setAnimationOptions(QChart::AllAnimations);
44 chart->setAnimationOptions(QChart::AllAnimations);
45
45
46 // create series
46 // create series
47 m_series = new QPieSeries();
47 m_series = new QPieSeries();
48 *m_series << new CustomSlice("Slice 1", 10.0);
48 *m_series << new CustomSlice("Slice 1", 10.0);
49 *m_series << new CustomSlice("Slice 2", 20.0);
49 *m_series << new CustomSlice("Slice 2", 20.0);
50 *m_series << new CustomSlice("Slice 3", 30.0);
50 *m_series << new CustomSlice("Slice 3", 30.0);
51 *m_series << new CustomSlice("Slice 4", 40.0);
51 *m_series << new CustomSlice("Slice 4", 40.0);
52 *m_series << new CustomSlice("Slice 5", 50.0);
52 *m_series << new CustomSlice("Slice 5", 50.0);
53 m_series->setLabelsVisible();
53 m_series->setLabelsVisible();
54 chart->addSeries(m_series);
54 chart->addSeries(m_series);
55
55
56 connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*)));
56 connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*)));
57
57
58 // chart settings
58 // chart settings
59 m_themeComboBox = new QComboBox();
59 m_themeComboBox = new QComboBox();
60 m_themeComboBox->addItem("Light", QChart::ChartThemeLight);
60 m_themeComboBox->addItem("Light", QChart::ChartThemeLight);
61 m_themeComboBox->addItem("BlueCerulean", QChart::ChartThemeBlueCerulean);
61 m_themeComboBox->addItem("BlueCerulean", QChart::ChartThemeBlueCerulean);
62 m_themeComboBox->addItem("Dark", QChart::ChartThemeDark);
62 m_themeComboBox->addItem("Dark", QChart::ChartThemeDark);
63 m_themeComboBox->addItem("BrownSand", QChart::ChartThemeBrownSand);
63 m_themeComboBox->addItem("BrownSand", QChart::ChartThemeBrownSand);
64 m_themeComboBox->addItem("BlueNcs", QChart::ChartThemeBlueNcs);
64 m_themeComboBox->addItem("BlueNcs", QChart::ChartThemeBlueNcs);
65 m_themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
65 m_themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
66 m_themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
66 m_themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
67
67
68 m_aaCheckBox = new QCheckBox();
68 m_aaCheckBox = new QCheckBox();
69 m_animationsCheckBox = new QCheckBox();
69 m_animationsCheckBox = new QCheckBox();
70 m_animationsCheckBox->setCheckState(Qt::Checked);
70 m_animationsCheckBox->setCheckState(Qt::Checked);
71
71
72 m_legendCheckBox = new QCheckBox();
72 m_legendCheckBox = new QCheckBox();
73
73
74 QFormLayout* chartSettingsLayout = new QFormLayout();
74 QFormLayout* chartSettingsLayout = new QFormLayout();
75 chartSettingsLayout->addRow("Theme", m_themeComboBox);
75 chartSettingsLayout->addRow("Theme", m_themeComboBox);
76 chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox);
76 chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox);
77 chartSettingsLayout->addRow("Animations", m_animationsCheckBox);
77 chartSettingsLayout->addRow("Animations", m_animationsCheckBox);
78 chartSettingsLayout->addRow("Legend", m_legendCheckBox);
78 chartSettingsLayout->addRow("Legend", m_legendCheckBox);
79 QGroupBox* chartSettings = new QGroupBox("Chart");
79 QGroupBox* chartSettings = new QGroupBox("Chart");
80 chartSettings->setLayout(chartSettingsLayout);
80 chartSettings->setLayout(chartSettingsLayout);
81
81
82 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateChartSettings()));
82 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateChartSettings()));
83 connect(m_aaCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
83 connect(m_aaCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
84 connect(m_animationsCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
84 connect(m_animationsCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
85 connect(m_legendCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
85 connect(m_legendCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
86
86
87 // series settings
87 // series settings
88 m_hPosition = new QDoubleSpinBox();
88 m_hPosition = new QDoubleSpinBox();
89 m_hPosition->setMinimum(0.0);
89 m_hPosition->setMinimum(0.0);
90 m_hPosition->setMaximum(1.0);
90 m_hPosition->setMaximum(1.0);
91 m_hPosition->setSingleStep(0.1);
91 m_hPosition->setSingleStep(0.1);
92 m_hPosition->setValue(m_series->horizontalPosition());
92 m_hPosition->setValue(m_series->horizontalPosition());
93
93
94 m_vPosition = new QDoubleSpinBox();
94 m_vPosition = new QDoubleSpinBox();
95 m_vPosition->setMinimum(0.0);
95 m_vPosition->setMinimum(0.0);
96 m_vPosition->setMaximum(1.0);
96 m_vPosition->setMaximum(1.0);
97 m_vPosition->setSingleStep(0.1);
97 m_vPosition->setSingleStep(0.1);
98 m_vPosition->setValue(m_series->verticalPosition());
98 m_vPosition->setValue(m_series->verticalPosition());
99
99
100 m_sizeFactor = new QDoubleSpinBox();
100 m_sizeFactor = new QDoubleSpinBox();
101 m_sizeFactor->setMinimum(0.0);
101 m_sizeFactor->setMinimum(0.0);
102 m_sizeFactor->setMaximum(1.0);
102 m_sizeFactor->setMaximum(1.0);
103 m_sizeFactor->setSingleStep(0.1);
103 m_sizeFactor->setSingleStep(0.1);
104 m_sizeFactor->setValue(m_series->pieSize());
104 m_sizeFactor->setValue(m_series->pieSize());
105
105
106 m_startAngle = new QDoubleSpinBox();
106 m_startAngle = new QDoubleSpinBox();
107 m_startAngle->setMinimum(-720);
107 m_startAngle->setMinimum(-720);
108 m_startAngle->setMaximum(720);
108 m_startAngle->setMaximum(720);
109 m_startAngle->setValue(m_series->pieStartAngle());
109 m_startAngle->setValue(m_series->pieStartAngle());
110 m_startAngle->setSingleStep(1);
110 m_startAngle->setSingleStep(1);
111
111
112 m_endAngle = new QDoubleSpinBox();
112 m_endAngle = new QDoubleSpinBox();
113 m_endAngle->setMinimum(-720);
113 m_endAngle->setMinimum(-720);
114 m_endAngle->setMaximum(720);
114 m_endAngle->setMaximum(720);
115 m_endAngle->setValue(m_series->pieEndAngle());
115 m_endAngle->setValue(m_series->pieEndAngle());
116 m_endAngle->setSingleStep(1);
116 m_endAngle->setSingleStep(1);
117
117
118 QPushButton *appendSlice = new QPushButton("Append slice");
118 QPushButton *appendSlice = new QPushButton("Append slice");
119 QPushButton *insertSlice = new QPushButton("Insert slice");
119 QPushButton *insertSlice = new QPushButton("Insert slice");
120
120
121 QFormLayout* seriesSettingsLayout = new QFormLayout();
121 QFormLayout* seriesSettingsLayout = new QFormLayout();
122 seriesSettingsLayout->addRow("Horizontal position", m_hPosition);
122 seriesSettingsLayout->addRow("Horizontal position", m_hPosition);
123 seriesSettingsLayout->addRow("Vertical position", m_vPosition);
123 seriesSettingsLayout->addRow("Vertical position", m_vPosition);
124 seriesSettingsLayout->addRow("Size factor", m_sizeFactor);
124 seriesSettingsLayout->addRow("Size factor", m_sizeFactor);
125 seriesSettingsLayout->addRow("Start angle", m_startAngle);
125 seriesSettingsLayout->addRow("Start angle", m_startAngle);
126 seriesSettingsLayout->addRow("End angle", m_endAngle);
126 seriesSettingsLayout->addRow("End angle", m_endAngle);
127 seriesSettingsLayout->addRow(appendSlice);
127 seriesSettingsLayout->addRow(appendSlice);
128 seriesSettingsLayout->addRow(insertSlice);
128 seriesSettingsLayout->addRow(insertSlice);
129 QGroupBox* seriesSettings = new QGroupBox("Series");
129 QGroupBox* seriesSettings = new QGroupBox("Series");
130 seriesSettings->setLayout(seriesSettingsLayout);
130 seriesSettings->setLayout(seriesSettingsLayout);
131
131
132 connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
132 connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
133 connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
133 connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
134 connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
134 connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
135 connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
135 connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
136 connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
136 connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
137 connect(appendSlice, SIGNAL(clicked()), this, SLOT(appendSlice()));
137 connect(appendSlice, SIGNAL(clicked()), this, SLOT(appendSlice()));
138 connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice()));
138 connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice()));
139
139
140 // slice settings
140 // slice settings
141 m_sliceName = new QLabel("<click a slice>");
141 m_sliceName = new QLabel("<click a slice>");
142 m_sliceValue = new QDoubleSpinBox();
142 m_sliceValue = new QDoubleSpinBox();
143 m_sliceValue->setMaximum(1000);
143 m_sliceValue->setMaximum(1000);
144 m_sliceLabelVisible = new QCheckBox();
144 m_sliceLabelVisible = new QCheckBox();
145 m_sliceLabelArmFactor = new QDoubleSpinBox();
145 m_sliceLabelArmFactor = new QDoubleSpinBox();
146 m_sliceLabelArmFactor->setSingleStep(0.01);
146 m_sliceLabelArmFactor->setSingleStep(0.01);
147 m_sliceExploded = new QCheckBox();
147 m_sliceExploded = new QCheckBox();
148 m_sliceExplodedFactor = new QDoubleSpinBox();
148 m_sliceExplodedFactor = new QDoubleSpinBox();
149 m_sliceExplodedFactor->setSingleStep(0.01);
149 m_sliceExplodedFactor->setSingleStep(0.01);
150 m_pen = new QPushButton();
150 m_pen = new QPushButton();
151 m_penTool = new PenTool("Slice pen", this);
151 m_penTool = new PenTool("Slice pen", this);
152 m_brush = new QPushButton();
152 m_brush = new QPushButton();
153 m_brushTool = new BrushTool("Slice brush", this);
153 m_brushTool = new BrushTool("Slice brush", this);
154 m_font = new QPushButton();
154 m_font = new QPushButton();
155 m_labelBrush = new QPushButton();
155 m_labelBrush = new QPushButton();
156 m_labelBrushTool = new BrushTool("Label brush", this);
156 m_labelBrushTool = new BrushTool("Label brush", this);
157 m_labelPosition = new QComboBox(this);
158 m_labelPosition->addItem("Outside", QPieSlice::LabelOutside);
159 m_labelPosition->addItem("Inside", QPieSlice::LabelInside);
157 QPushButton *removeSlice = new QPushButton("Remove slice");
160 QPushButton *removeSlice = new QPushButton("Remove slice");
158
161
159 QFormLayout* sliceSettingsLayout = new QFormLayout();
162 QFormLayout* sliceSettingsLayout = new QFormLayout();
160 sliceSettingsLayout->addRow("Selected", m_sliceName);
163 sliceSettingsLayout->addRow("Selected", m_sliceName);
161 sliceSettingsLayout->addRow("Value", m_sliceValue);
164 sliceSettingsLayout->addRow("Value", m_sliceValue);
162 sliceSettingsLayout->addRow("Pen", m_pen);
165 sliceSettingsLayout->addRow("Pen", m_pen);
163 sliceSettingsLayout->addRow("Brush", m_brush);
166 sliceSettingsLayout->addRow("Brush", m_brush);
164 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
167 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
165 sliceSettingsLayout->addRow("Label font", m_font);
168 sliceSettingsLayout->addRow("Label font", m_font);
166 sliceSettingsLayout->addRow("Label pen", m_labelBrush);
169 sliceSettingsLayout->addRow("Label brush", m_labelBrush);
170 sliceSettingsLayout->addRow("Label position", m_labelPosition);
167 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
171 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
168 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
172 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
169 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
173 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
170 sliceSettingsLayout->addRow(removeSlice);
174 sliceSettingsLayout->addRow(removeSlice);
171 QGroupBox* sliceSettings = new QGroupBox("Slice");
175 QGroupBox* sliceSettings = new QGroupBox("Slice");
172 sliceSettings->setLayout(sliceSettingsLayout);
176 sliceSettings->setLayout(sliceSettingsLayout);
173
177
174 connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
178 connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
175 connect(m_pen, SIGNAL(clicked()), m_penTool, SLOT(show()));
179 connect(m_pen, SIGNAL(clicked()), m_penTool, SLOT(show()));
176 connect(m_penTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
180 connect(m_penTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
177 connect(m_brush, SIGNAL(clicked()), m_brushTool, SLOT(show()));
181 connect(m_brush, SIGNAL(clicked()), m_brushTool, SLOT(show()));
178 connect(m_brushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
182 connect(m_brushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
179 connect(m_font, SIGNAL(clicked()), this, SLOT(showFontDialog()));
183 connect(m_font, SIGNAL(clicked()), this, SLOT(showFontDialog()));
180 connect(m_labelBrush, SIGNAL(clicked()), m_labelBrushTool, SLOT(show()));
184 connect(m_labelBrush, SIGNAL(clicked()), m_labelBrushTool, SLOT(show()));
181 connect(m_labelBrushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
185 connect(m_labelBrushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
182 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
186 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
183 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
187 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
184 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
188 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
185 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
189 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
186 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
190 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
191 connect(m_labelPosition, SIGNAL(currentIndexChanged(int)), this, SLOT(updateSliceSettings()));
187 connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice()));
192 connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice()));
188
193
189 // create chart view
194 // create chart view
190 m_chartView = new QChartView(chart);
195 m_chartView = new QChartView(chart);
191
196
192 // create main layout
197 // create main layout
193 QVBoxLayout *settingsLayout = new QVBoxLayout();
198 QVBoxLayout *settingsLayout = new QVBoxLayout();
194 settingsLayout->addWidget(chartSettings);
199 settingsLayout->addWidget(chartSettings);
195 settingsLayout->addWidget(seriesSettings);
200 settingsLayout->addWidget(seriesSettings);
196 settingsLayout->addWidget(sliceSettings);
201 settingsLayout->addWidget(sliceSettings);
197 settingsLayout->addStretch();
202 settingsLayout->addStretch();
198
203
199 QGridLayout* baseLayout = new QGridLayout();
204 QGridLayout* baseLayout = new QGridLayout();
200 baseLayout->addLayout(settingsLayout, 0, 0);
205 baseLayout->addLayout(settingsLayout, 0, 0);
201 baseLayout->addWidget(m_chartView, 0, 1);
206 baseLayout->addWidget(m_chartView, 0, 1);
202 setLayout(baseLayout);
207 setLayout(baseLayout);
203
208
204 updateSerieSettings();
209 updateSerieSettings();
205 }
210 }
206
211
207
212
208 void MainWidget::updateChartSettings()
213 void MainWidget::updateChartSettings()
209 {
214 {
210 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
215 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
211 m_chartView->chart()->setTheme(theme);
216 m_chartView->chart()->setTheme(theme);
212 m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked());
217 m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked());
213
218
214 if (m_animationsCheckBox->checkState() == Qt::Checked)
219 if (m_animationsCheckBox->checkState() == Qt::Checked)
215 m_chartView->chart()->setAnimationOptions(QChart::AllAnimations);
220 m_chartView->chart()->setAnimationOptions(QChart::AllAnimations);
216 else
221 else
217 m_chartView->chart()->setAnimationOptions(QChart::NoAnimation);
222 m_chartView->chart()->setAnimationOptions(QChart::NoAnimation);
218
223
219 if (m_legendCheckBox->checkState() == Qt::Checked)
224 if (m_legendCheckBox->checkState() == Qt::Checked)
220 m_chartView->chart()->legend()->show();
225 m_chartView->chart()->legend()->show();
221 else
226 else
222 m_chartView->chart()->legend()->hide();
227 m_chartView->chart()->legend()->hide();
223 }
228 }
224
229
225 void MainWidget::updateSerieSettings()
230 void MainWidget::updateSerieSettings()
226 {
231 {
227 m_series->setHorizontalPosition(m_hPosition->value());
232 m_series->setHorizontalPosition(m_hPosition->value());
228 m_series->setVerticalPosition(m_vPosition->value());
233 m_series->setVerticalPosition(m_vPosition->value());
229 m_series->setPieSize(m_sizeFactor->value());
234 m_series->setPieSize(m_sizeFactor->value());
230 m_series->setPieStartAngle(m_startAngle->value());
235 m_series->setPieStartAngle(m_startAngle->value());
231 m_series->setPieEndAngle(m_endAngle->value());
236 m_series->setPieEndAngle(m_endAngle->value());
232 }
237 }
233
238
234 void MainWidget::updateSliceSettings()
239 void MainWidget::updateSliceSettings()
235 {
240 {
236 if (!m_slice)
241 if (!m_slice)
237 return;
242 return;
238
243
239 m_slice->setValue(m_sliceValue->value());
244 m_slice->setValue(m_sliceValue->value());
240
245
241 m_slice->setPen(m_penTool->pen());
246 m_slice->setPen(m_penTool->pen());
242 m_slice->setBrush(m_brushTool->brush());
247 m_slice->setBrush(m_brushTool->brush());
243
248
244 m_slice->setLabelBrush(m_labelBrushTool->brush());
249 m_slice->setLabelBrush(m_labelBrushTool->brush());
245 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
250 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
246 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
251 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
252 m_slice->setLabelPosition((QPieSlice::LabelPosition)m_labelPosition->currentIndex()); // assumes that index is in sync with the enum
247
253
248 m_slice->setExploded(m_sliceExploded->isChecked());
254 m_slice->setExploded(m_sliceExploded->isChecked());
249 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
255 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
250 }
256 }
251
257
252 void MainWidget::handleSliceClicked(QPieSlice* slice)
258 void MainWidget::handleSliceClicked(QPieSlice* slice)
253 {
259 {
254 m_slice = static_cast<CustomSlice*>(slice);
260 m_slice = static_cast<CustomSlice*>(slice);
255
261
256 // name
262 // name
257 m_sliceName->setText(slice->label());
263 m_sliceName->setText(slice->label());
258
264
259 // value
265 // value
260 m_sliceValue->blockSignals(true);
266 m_sliceValue->blockSignals(true);
261 m_sliceValue->setValue(slice->value());
267 m_sliceValue->setValue(slice->value());
262 m_sliceValue->blockSignals(false);
268 m_sliceValue->blockSignals(false);
263
269
264 // pen
270 // pen
265 m_pen->setText(PenTool::name(m_slice->pen()));
271 m_pen->setText(PenTool::name(m_slice->pen()));
266 m_penTool->setPen(m_slice->pen());
272 m_penTool->setPen(m_slice->pen());
267
273
268 // brush
274 // brush
269 m_brush->setText(m_slice->originalBrush().color().name());
275 m_brush->setText(m_slice->originalBrush().color().name());
270 m_brushTool->setBrush(m_slice->originalBrush());
276 m_brushTool->setBrush(m_slice->originalBrush());
271
277
272 // label
278 // label
273 m_labelBrush->setText(BrushTool::name(m_slice->labelBrush()));
279 m_labelBrush->setText(BrushTool::name(m_slice->labelBrush()));
274 m_labelBrushTool->setBrush(m_slice->labelBrush());
280 m_labelBrushTool->setBrush(m_slice->labelBrush());
275 m_font->setText(slice->labelFont().toString());
281 m_font->setText(slice->labelFont().toString());
276 m_sliceLabelVisible->blockSignals(true);
282 m_sliceLabelVisible->blockSignals(true);
277 m_sliceLabelVisible->setChecked(slice->isLabelVisible());
283 m_sliceLabelVisible->setChecked(slice->isLabelVisible());
278 m_sliceLabelVisible->blockSignals(false);
284 m_sliceLabelVisible->blockSignals(false);
279 m_sliceLabelArmFactor->blockSignals(true);
285 m_sliceLabelArmFactor->blockSignals(true);
280 m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
286 m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
281 m_sliceLabelArmFactor->blockSignals(false);
287 m_sliceLabelArmFactor->blockSignals(false);
288 m_labelPosition->blockSignals(true);
289 m_labelPosition->setCurrentIndex(slice->labelPosition()); // assumes that index is in sync with the enum
290 m_labelPosition->blockSignals(false);
282
291
283 // exploded
292 // exploded
284 m_sliceExploded->blockSignals(true);
293 m_sliceExploded->blockSignals(true);
285 m_sliceExploded->setChecked(slice->isExploded());
294 m_sliceExploded->setChecked(slice->isExploded());
286 m_sliceExploded->blockSignals(false);
295 m_sliceExploded->blockSignals(false);
287 m_sliceExplodedFactor->blockSignals(true);
296 m_sliceExplodedFactor->blockSignals(true);
288 m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
297 m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
289 m_sliceExplodedFactor->blockSignals(false);
298 m_sliceExplodedFactor->blockSignals(false);
290 }
299 }
291
300
292 void MainWidget::showFontDialog()
301 void MainWidget::showFontDialog()
293 {
302 {
294 if (!m_slice)
303 if (!m_slice)
295 return;
304 return;
296
305
297 QFontDialog dialog(m_slice->labelFont());
306 QFontDialog dialog(m_slice->labelFont());
298 dialog.show();
307 dialog.show();
299 dialog.exec();
308 dialog.exec();
300
309
301 m_slice->setLabelFont(dialog.currentFont());
310 m_slice->setLabelFont(dialog.currentFont());
302 m_font->setText(dialog.currentFont().toString());
311 m_font->setText(dialog.currentFont().toString());
303 }
312 }
304
313
305 void MainWidget::appendSlice()
314 void MainWidget::appendSlice()
306 {
315 {
307 *m_series << new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0);
316 *m_series << new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0);
308 }
317 }
309
318
310 void MainWidget::insertSlice()
319 void MainWidget::insertSlice()
311 {
320 {
312 if (!m_slice)
321 if (!m_slice)
313 return;
322 return;
314
323
315 int i = m_series->slices().indexOf(m_slice);
324 int i = m_series->slices().indexOf(m_slice);
316
325
317 m_series->insert(i, new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0));
326 m_series->insert(i, new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0));
318 }
327 }
319
328
320 void MainWidget::removeSlice()
329 void MainWidget::removeSlice()
321 {
330 {
322 if (!m_slice)
331 if (!m_slice)
323 return;
332 return;
324
333
325 m_sliceName->setText("<click a slice>");
334 m_sliceName->setText("<click a slice>");
326
335
327 m_series->remove(m_slice);
336 m_series->remove(m_slice);
328 m_slice = 0;
337 m_slice = 0;
329 }
338 }
330
339
331 #include "moc_mainwidget.cpp"
340 #include "moc_mainwidget.cpp"
@@ -1,91 +1,92
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20 #ifndef MAINWIDGET_H
20 #ifndef MAINWIDGET_H
21 #define MAINWIDGET_H
21 #define MAINWIDGET_H
22
22
23 #include <QWidget>
23 #include <QWidget>
24 #include <QChartGlobal>
24 #include <QChartGlobal>
25
25
26 class QLabel;
26 class QLabel;
27 class QPushButton;
27 class QPushButton;
28 class QCheckBox;
28 class QCheckBox;
29 class QComboBox;
29 class QComboBox;
30 class QDoubleSpinBox;
30 class QDoubleSpinBox;
31 class PenTool;
31 class PenTool;
32 class BrushTool;
32 class BrushTool;
33 class CustomSlice;
33 class CustomSlice;
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 class QChartView;
36 class QChartView;
37 class QPieSeries;
37 class QPieSeries;
38 class QPieSlice;
38 class QPieSlice;
39 QTCOMMERCIALCHART_END_NAMESPACE
39 QTCOMMERCIALCHART_END_NAMESPACE
40
40
41 QTCOMMERCIALCHART_USE_NAMESPACE
41 QTCOMMERCIALCHART_USE_NAMESPACE
42
42
43 class MainWidget : public QWidget
43 class MainWidget : public QWidget
44 {
44 {
45 Q_OBJECT
45 Q_OBJECT
46
46
47 public:
47 public:
48 explicit MainWidget(QWidget* parent = 0);
48 explicit MainWidget(QWidget* parent = 0);
49
49
50 public Q_SLOTS:
50 public Q_SLOTS:
51 void updateChartSettings();
51 void updateChartSettings();
52 void updateSerieSettings();
52 void updateSerieSettings();
53 void updateSliceSettings();
53 void updateSliceSettings();
54 void handleSliceClicked(QPieSlice* slice);
54 void handleSliceClicked(QPieSlice* slice);
55 void showFontDialog();
55 void showFontDialog();
56 void appendSlice();
56 void appendSlice();
57 void insertSlice();
57 void insertSlice();
58 void removeSlice();
58 void removeSlice();
59
59
60 private:
60 private:
61 QComboBox *m_themeComboBox;
61 QComboBox *m_themeComboBox;
62 QCheckBox *m_aaCheckBox;
62 QCheckBox *m_aaCheckBox;
63 QCheckBox *m_animationsCheckBox;
63 QCheckBox *m_animationsCheckBox;
64 QCheckBox *m_legendCheckBox;
64 QCheckBox *m_legendCheckBox;
65
65
66 QChartView* m_chartView;
66 QChartView* m_chartView;
67 QPieSeries* m_series;
67 QPieSeries* m_series;
68 CustomSlice* m_slice;
68 CustomSlice* m_slice;
69
69
70 QDoubleSpinBox* m_hPosition;
70 QDoubleSpinBox* m_hPosition;
71 QDoubleSpinBox* m_vPosition;
71 QDoubleSpinBox* m_vPosition;
72 QDoubleSpinBox* m_sizeFactor;
72 QDoubleSpinBox* m_sizeFactor;
73 QDoubleSpinBox* m_startAngle;
73 QDoubleSpinBox* m_startAngle;
74 QDoubleSpinBox* m_endAngle;
74 QDoubleSpinBox* m_endAngle;
75
75
76 QLabel* m_sliceName;
76 QLabel* m_sliceName;
77 QDoubleSpinBox* m_sliceValue;
77 QDoubleSpinBox* m_sliceValue;
78 QCheckBox* m_sliceLabelVisible;
78 QCheckBox* m_sliceLabelVisible;
79 QDoubleSpinBox* m_sliceLabelArmFactor;
79 QDoubleSpinBox* m_sliceLabelArmFactor;
80 QCheckBox* m_sliceExploded;
80 QCheckBox* m_sliceExploded;
81 QDoubleSpinBox* m_sliceExplodedFactor;
81 QDoubleSpinBox* m_sliceExplodedFactor;
82 QPushButton *m_brush;
82 QPushButton *m_brush;
83 BrushTool *m_brushTool;
83 BrushTool *m_brushTool;
84 QPushButton *m_pen;
84 QPushButton *m_pen;
85 PenTool *m_penTool;
85 PenTool *m_penTool;
86 QPushButton *m_font;
86 QPushButton *m_font;
87 QPushButton *m_labelBrush;
87 QPushButton *m_labelBrush;
88 QComboBox *m_labelPosition;
88 BrushTool *m_labelBrushTool;
89 BrushTool *m_labelBrushTool;
89 };
90 };
90
91
91 #endif // MAINWIDGET_H
92 #endif // MAINWIDGET_H
@@ -1,208 +1,209
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "piechartitem_p.h"
21 #include "piechartitem_p.h"
22 #include "piesliceitem_p.h"
22 #include "piesliceitem_p.h"
23 #include "qpieslice.h"
23 #include "qpieslice.h"
24 #include "qpieslice_p.h"
24 #include "qpieslice_p.h"
25 #include "qpieseries.h"
25 #include "qpieseries.h"
26 #include "qpieseries_p.h"
26 #include "qpieseries_p.h"
27 #include "chartpresenter_p.h"
27 #include "chartpresenter_p.h"
28 #include "chartdataset_p.h"
28 #include "chartdataset_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30 #include <QPainter>
30 #include <QPainter>
31 #include <QTimer>
31 #include <QTimer>
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
35 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
36 :ChartItem(presenter),
36 :ChartItem(presenter),
37 m_series(series)
37 m_series(series)
38 {
38 {
39 Q_ASSERT(series);
39 Q_ASSERT(series);
40
40
41 connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
41 connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
42 connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>)));
42 connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>)));
43 connect(series, SIGNAL(horizontalPositionChanged()), this, SLOT(updateLayout()));
43 connect(series, SIGNAL(horizontalPositionChanged()), this, SLOT(updateLayout()));
44 connect(series, SIGNAL(verticalPositionChanged()), this, SLOT(updateLayout()));
44 connect(series, SIGNAL(verticalPositionChanged()), this, SLOT(updateLayout()));
45 connect(series, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout()));
45 connect(series, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout()));
46 connect(QPieSeriesPrivate::fromSeries(series), SIGNAL(calculatedDataChanged()), this, SLOT(updateLayout()));
46 connect(QPieSeriesPrivate::fromSeries(series), SIGNAL(calculatedDataChanged()), this, SLOT(updateLayout()));
47
47
48 // Note: the following does not affect as long as the item does not have anything to paint
48 // Note: the following does not affect as long as the item does not have anything to paint
49 setZValue(ChartPresenter::PieSeriesZValue);
49 setZValue(ChartPresenter::PieSeriesZValue);
50
50
51 // Note: will not create slice items until we have a proper rectangle to draw on.
51 // Note: will not create slice items until we have a proper rectangle to draw on.
52 }
52 }
53
53
54 PieChartItem::~PieChartItem()
54 PieChartItem::~PieChartItem()
55 {
55 {
56 // slices deleted automatically through QGraphicsItem
56 // slices deleted automatically through QGraphicsItem
57 }
57 }
58
58
59 void PieChartItem::handleGeometryChanged(const QRectF& rect)
59 void PieChartItem::handleGeometryChanged(const QRectF& rect)
60 {
60 {
61 prepareGeometryChange();
61 prepareGeometryChange();
62 m_rect = rect;
62 m_rect = rect;
63 updateLayout();
63 updateLayout();
64
64
65 // This is for delayed initialization of the slice items during startup.
65 // This is for delayed initialization of the slice items during startup.
66 // It ensures that startup animation originates from the correct position.
66 // It ensures that startup animation originates from the correct position.
67 if (m_sliceItems.isEmpty())
67 if (m_sliceItems.isEmpty())
68 handleSlicesAdded(m_series->slices());
68 handleSlicesAdded(m_series->slices());
69 }
69 }
70
70
71 void PieChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
71 void PieChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
72 {
72 {
73 Q_UNUSED(minX);
73 Q_UNUSED(minX);
74 Q_UNUSED(maxX);
74 Q_UNUSED(maxX);
75 Q_UNUSED(minY);
75 Q_UNUSED(minY);
76 Q_UNUSED(maxY);
76 Q_UNUSED(maxY);
77 // does not apply to pie
77 // does not apply to pie
78 }
78 }
79
79
80 void PieChartItem::rangeXChanged(qreal min, qreal max, int tickXCount)
80 void PieChartItem::rangeXChanged(qreal min, qreal max, int tickXCount)
81 {
81 {
82 Q_UNUSED(min);
82 Q_UNUSED(min);
83 Q_UNUSED(max);
83 Q_UNUSED(max);
84 Q_UNUSED(tickXCount);
84 Q_UNUSED(tickXCount);
85 // does not apply to pie
85 // does not apply to pie
86 }
86 }
87
87
88 void PieChartItem::rangeYChanged(qreal min, qreal max, int tickYCount)
88 void PieChartItem::rangeYChanged(qreal min, qreal max, int tickYCount)
89 {
89 {
90 Q_UNUSED(min);
90 Q_UNUSED(min);
91 Q_UNUSED(max);
91 Q_UNUSED(max);
92 Q_UNUSED(tickYCount);
92 Q_UNUSED(tickYCount);
93 // does not apply to pie
93 // does not apply to pie
94 }
94 }
95
95
96 void PieChartItem::updateLayout()
96 void PieChartItem::updateLayout()
97 {
97 {
98 // find pie center coordinates
98 // find pie center coordinates
99 m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->horizontalPosition()));
99 m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->horizontalPosition()));
100 m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->verticalPosition()));
100 m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->verticalPosition()));
101
101
102 // find maximum radius for pie
102 // find maximum radius for pie
103 m_pieRadius = m_rect.height() / 2;
103 m_pieRadius = m_rect.height() / 2;
104 if (m_rect.width() < m_rect.height())
104 if (m_rect.width() < m_rect.height())
105 m_pieRadius = m_rect.width() / 2;
105 m_pieRadius = m_rect.width() / 2;
106
106
107 // apply size factor
107 // apply size factor
108 m_pieRadius *= m_series->pieSize();
108 m_pieRadius *= m_series->pieSize();
109
109
110 // set layouts for existing slice items
110 // set layouts for existing slice items
111 foreach (QPieSlice* slice, m_series->slices()) {
111 foreach (QPieSlice* slice, m_series->slices()) {
112 PieSliceItem *sliceItem = m_sliceItems.value(slice);
112 PieSliceItem *sliceItem = m_sliceItems.value(slice);
113 if (sliceItem) {
113 if (sliceItem) {
114 PieSliceData sliceData = updateSliceGeometry(slice);
114 PieSliceData sliceData = updateSliceGeometry(slice);
115 if (animator())
115 if (animator())
116 animator()->updateAnimation(this, sliceItem, sliceData);
116 animator()->updateAnimation(this, sliceItem, sliceData);
117 else
117 else
118 sliceItem->setLayout(sliceData);
118 sliceItem->setLayout(sliceData);
119 }
119 }
120 }
120 }
121
121
122 update();
122 update();
123 }
123 }
124
124
125 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
125 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
126 {
126 {
127 // delay creating slice items until there is a proper rectangle
127 // delay creating slice items until there is a proper rectangle
128 if (!m_rect.isValid() && m_sliceItems.isEmpty())
128 if (!m_rect.isValid() && m_sliceItems.isEmpty())
129 return;
129 return;
130
130
131 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
131 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
132
132
133 bool startupAnimation = m_sliceItems.isEmpty();
133 bool startupAnimation = m_sliceItems.isEmpty();
134
134
135 foreach (QPieSlice *slice, slices) {
135 foreach (QPieSlice *slice, slices) {
136 PieSliceItem* sliceItem = new PieSliceItem(this);
136 PieSliceItem* sliceItem = new PieSliceItem(this);
137 m_sliceItems.insert(slice, sliceItem);
137 m_sliceItems.insert(slice, sliceItem);
138
138
139 // Note: do need to connect to slice valueChanged() etc.
139 // Note: do need to connect to slice valueChanged() etc.
140 // This is handled through calculatedDataChanged signal.
140 // This is handled through calculatedDataChanged signal.
141 connect(slice, SIGNAL(labelChanged()), this, SLOT(handleSliceChanged()));
141 connect(slice, SIGNAL(labelChanged()), this, SLOT(handleSliceChanged()));
142 connect(slice, SIGNAL(labelVisibleChanged()), this, SLOT(handleSliceChanged()));
142 connect(slice, SIGNAL(labelVisibleChanged()), this, SLOT(handleSliceChanged()));
143 connect(slice, SIGNAL(explodedChanged()), this, SLOT(handleSliceChanged()));
143 connect(slice, SIGNAL(explodedChanged()), this, SLOT(handleSliceChanged()));
144 connect(slice, SIGNAL(penChanged()), this, SLOT(handleSliceChanged()));
144 connect(slice, SIGNAL(penChanged()), this, SLOT(handleSliceChanged()));
145 connect(slice, SIGNAL(brushChanged()), this, SLOT(handleSliceChanged()));
145 connect(slice, SIGNAL(brushChanged()), this, SLOT(handleSliceChanged()));
146 connect(slice, SIGNAL(labelBrushChanged()), this, SLOT(handleSliceChanged()));
146 connect(slice, SIGNAL(labelBrushChanged()), this, SLOT(handleSliceChanged()));
147 connect(slice, SIGNAL(labelFontChanged()), this, SLOT(handleSliceChanged()));
147 connect(slice, SIGNAL(labelFontChanged()), this, SLOT(handleSliceChanged()));
148 connect(slice, SIGNAL(labelPositionChanged()), this, SLOT(handleSliceChanged()));
148 connect(slice, SIGNAL(labelArmLengthFactorChanged()), this, SLOT(handleSliceChanged()));
149 connect(slice, SIGNAL(labelArmLengthFactorChanged()), this, SLOT(handleSliceChanged()));
149 connect(slice, SIGNAL(explodeDistanceFactorChanged()), this, SLOT(handleSliceChanged()));
150 connect(slice, SIGNAL(explodeDistanceFactorChanged()), this, SLOT(handleSliceChanged()));
150
151
151 connect(sliceItem, SIGNAL(clicked(Qt::MouseButtons)), slice, SIGNAL(clicked()));
152 connect(sliceItem, SIGNAL(clicked(Qt::MouseButtons)), slice, SIGNAL(clicked()));
152 connect(sliceItem, SIGNAL(hovered(bool)), slice, SIGNAL(hovered(bool)));
153 connect(sliceItem, SIGNAL(hovered(bool)), slice, SIGNAL(hovered(bool)));
153
154
154 PieSliceData sliceData = updateSliceGeometry(slice);
155 PieSliceData sliceData = updateSliceGeometry(slice);
155 if (animator())
156 if (animator())
156 animator()->addAnimation(this, sliceItem, sliceData, startupAnimation);
157 animator()->addAnimation(this, sliceItem, sliceData, startupAnimation);
157 else
158 else
158 sliceItem->setLayout(sliceData);
159 sliceItem->setLayout(sliceData);
159 }
160 }
160 }
161 }
161
162
162 void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices)
163 void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices)
163 {
164 {
164 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
165 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
165
166
166 foreach (QPieSlice *slice, slices) {
167 foreach (QPieSlice *slice, slices) {
167
168
168 PieSliceItem *sliceItem = m_sliceItems.value(slice);
169 PieSliceItem *sliceItem = m_sliceItems.value(slice);
169
170
170 // this can happen if you call append() & remove() in a row so that PieSliceItem is not even created
171 // this can happen if you call append() & remove() in a row so that PieSliceItem is not even created
171 if (!sliceItem)
172 if (!sliceItem)
172 continue;
173 continue;
173
174
174 m_sliceItems.remove(slice);
175 m_sliceItems.remove(slice);
175
176
176 if (animator())
177 if (animator())
177 animator()->removeAnimation(this, sliceItem); // animator deletes the PieSliceItem
178 animator()->removeAnimation(this, sliceItem); // animator deletes the PieSliceItem
178 else
179 else
179 delete sliceItem;
180 delete sliceItem;
180 }
181 }
181 }
182 }
182
183
183 void PieChartItem::handleSliceChanged()
184 void PieChartItem::handleSliceChanged()
184 {
185 {
185 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
186 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
186 Q_ASSERT(m_sliceItems.contains(slice));
187 Q_ASSERT(m_sliceItems.contains(slice));
187
188
188 PieSliceItem *sliceItem = m_sliceItems.value(slice);
189 PieSliceItem *sliceItem = m_sliceItems.value(slice);
189 PieSliceData sliceData = updateSliceGeometry(slice);
190 PieSliceData sliceData = updateSliceGeometry(slice);
190 if (animator())
191 if (animator())
191 animator()->updateAnimation(this, sliceItem, sliceData);
192 animator()->updateAnimation(this, sliceItem, sliceData);
192 else
193 else
193 sliceItem->setLayout(sliceData);
194 sliceItem->setLayout(sliceData);
194
195
195 update();
196 update();
196 }
197 }
197
198
198 PieSliceData PieChartItem::updateSliceGeometry(QPieSlice *slice)
199 PieSliceData PieChartItem::updateSliceGeometry(QPieSlice *slice)
199 {
200 {
200 PieSliceData &sliceData = QPieSlicePrivate::fromSlice(slice)->m_data;
201 PieSliceData &sliceData = QPieSlicePrivate::fromSlice(slice)->m_data;
201 sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice);
202 sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice);
202 sliceData.m_radius = m_pieRadius;
203 sliceData.m_radius = m_pieRadius;
203 return sliceData;
204 return sliceData;
204 }
205 }
205
206
206 #include "moc_piechartitem_p.cpp"
207 #include "moc_piechartitem_p.cpp"
207
208
208 QTCOMMERCIALCHART_END_NAMESPACE
209 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,139 +1,142
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef PIESLICEDATA_P_H
30 #ifndef PIESLICEDATA_P_H
31 #define PIESLICEDATA_P_H
31 #define PIESLICEDATA_P_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include "qpieslice.h"
34 #include "qpieslice.h"
35 #include <QPen>
35 #include <QPen>
36 #include <QBrush>
36 #include <QBrush>
37
37
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
39
40 template <class T>
40 template <class T>
41 class Themed : public T
41 class Themed : public T
42 {
42 {
43 public:
43 public:
44 Themed():m_isThemed(true) {}
44 Themed():m_isThemed(true) {}
45
45
46 inline T &operator=(const T &other) { return T::operator =(other); }
46 inline T &operator=(const T &other) { return T::operator =(other); }
47
47
48 inline bool operator!=(const T &other) const { return T::operator !=(other); }
48 inline bool operator!=(const T &other) const { return T::operator !=(other); }
49 inline bool operator!=(const Themed &other) const
49 inline bool operator!=(const Themed &other) const
50 {
50 {
51 if (T::operator !=(other))
51 if (T::operator !=(other))
52 return true;
52 return true;
53
53
54 if (m_isThemed != other.m_isThemed)
54 if (m_isThemed != other.m_isThemed)
55 return true;
55 return true;
56
56
57 return false;
57 return false;
58 }
58 }
59
59
60 inline void setThemed(bool state) { m_isThemed = state; }
60 inline void setThemed(bool state) { m_isThemed = state; }
61 inline bool isThemed() const { return m_isThemed; }
61 inline bool isThemed() const { return m_isThemed; }
62
62
63 private:
63 private:
64 bool m_isThemed;
64 bool m_isThemed;
65 };
65 };
66
66
67 class PieSliceData
67 class PieSliceData
68 {
68 {
69 public:
69 public:
70 PieSliceData()
70 PieSliceData()
71 {
71 {
72 m_value = 0;
72 m_value = 0;
73
73
74 m_isExploded = false;
74 m_isExploded = false;
75 m_explodeDistanceFactor = 0.15;
75 m_explodeDistanceFactor = 0.15;
76
76
77 m_isLabelVisible = false;
77 m_isLabelVisible = false;
78 m_labelPosition = QPieSlice::LabelOutside;
78 m_labelArmLengthFactor = 0.15;
79 m_labelArmLengthFactor = 0.15;
79
80
80 m_percentage = 0;
81 m_percentage = 0;
81 m_radius = 0;
82 m_radius = 0;
82 m_startAngle = 0;
83 m_startAngle = 0;
83 m_angleSpan = 0;
84 m_angleSpan = 0;
84 }
85 }
85
86
86 bool operator!=(const PieSliceData &other) const
87 bool operator!=(const PieSliceData &other) const
87 {
88 {
88 if (!qFuzzyIsNull(m_value - other.m_value))
89 if (!qFuzzyIsNull(m_value - other.m_value))
89 return true;
90 return true;
90
91
91 if (m_slicePen != other.m_slicePen ||
92 if (m_slicePen != other.m_slicePen ||
92 m_sliceBrush != other.m_sliceBrush)
93 m_sliceBrush != other.m_sliceBrush)
93 return true;
94 return true;
94
95
95 if (m_isExploded != other.m_isExploded ||
96 if (m_isExploded != other.m_isExploded ||
96 !qFuzzyIsNull(m_explodeDistanceFactor - other.m_explodeDistanceFactor))
97 !qFuzzyIsNull(m_explodeDistanceFactor - other.m_explodeDistanceFactor))
97 return true;
98 return true;
98
99
99 if (m_isLabelVisible != other.m_isLabelVisible ||
100 if (m_isLabelVisible != other.m_isLabelVisible ||
100 m_labelText != other.m_labelText ||
101 m_labelText != other.m_labelText ||
101 m_labelFont != other.m_labelFont ||
102 m_labelFont != other.m_labelFont ||
103 m_labelPosition != other.m_labelPosition ||
102 !qFuzzyIsNull(m_labelArmLengthFactor - other.m_labelArmLengthFactor) ||
104 !qFuzzyIsNull(m_labelArmLengthFactor - other.m_labelArmLengthFactor) ||
103 m_labelBrush != other.m_labelBrush)
105 m_labelBrush != other.m_labelBrush)
104 return true;
106 return true;
105
107
106 if (!qFuzzyIsNull(m_percentage - other.m_percentage) ||
108 if (!qFuzzyIsNull(m_percentage - other.m_percentage) ||
107 m_center != other.m_center ||
109 m_center != other.m_center ||
108 !qFuzzyIsNull(m_radius - other.m_radius) ||
110 !qFuzzyIsNull(m_radius - other.m_radius) ||
109 !qFuzzyIsNull(m_startAngle - other.m_startAngle) ||
111 !qFuzzyIsNull(m_startAngle - other.m_startAngle) ||
110 !qFuzzyIsNull(m_angleSpan - other.m_angleSpan))
112 !qFuzzyIsNull(m_angleSpan - other.m_angleSpan))
111 return true;
113 return true;
112
114
113 return false;
115 return false;
114 }
116 }
115
117
116 qreal m_value;
118 qreal m_value;
117
119
118 Themed<QPen> m_slicePen;
120 Themed<QPen> m_slicePen;
119 Themed<QBrush> m_sliceBrush;
121 Themed<QBrush> m_sliceBrush;
120
122
121 bool m_isExploded;
123 bool m_isExploded;
122 qreal m_explodeDistanceFactor;
124 qreal m_explodeDistanceFactor;
123
125
124 bool m_isLabelVisible;
126 bool m_isLabelVisible;
125 QString m_labelText;
127 QString m_labelText;
126 Themed<QFont> m_labelFont;
128 Themed<QFont> m_labelFont;
129 QPieSlice::LabelPosition m_labelPosition;
127 qreal m_labelArmLengthFactor;
130 qreal m_labelArmLengthFactor;
128 Themed<QBrush> m_labelBrush;
131 Themed<QBrush> m_labelBrush;
129
132
130 qreal m_percentage;
133 qreal m_percentage;
131 QPointF m_center;
134 QPointF m_center;
132 qreal m_radius;
135 qreal m_radius;
133 qreal m_startAngle;
136 qreal m_startAngle;
134 qreal m_angleSpan;
137 qreal m_angleSpan;
135 };
138 };
136
139
137 QTCOMMERCIALCHART_END_NAMESPACE
140 QTCOMMERCIALCHART_END_NAMESPACE
138
141
139 #endif // PIESLICEDATA_P_H
142 #endif // PIESLICEDATA_P_H
@@ -1,230 +1,234
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "piesliceitem_p.h"
21 #include "piesliceitem_p.h"
22 #include "piechartitem_p.h"
22 #include "piechartitem_p.h"
23 #include "qpieseries.h"
23 #include "qpieseries.h"
24 #include "qpieslice.h"
24 #include "qpieslice.h"
25 #include "chartpresenter_p.h"
25 #include "chartpresenter_p.h"
26 #include <QPainter>
26 #include <QPainter>
27 #include <qmath.h>
27 #include <qmath.h>
28 #include <QGraphicsSceneEvent>
28 #include <QGraphicsSceneEvent>
29 #include <QTime>
29 #include <QTime>
30 #include <QDebug>
30 #include <QDebug>
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 QPointF offset(qreal angle, qreal length)
34 QPointF offset(qreal angle, qreal length)
35 {
35 {
36 qreal dx = qSin(angle*(M_PI/180)) * length;
36 qreal dx = qSin(angle*(M_PI/180)) * length;
37 qreal dy = qCos(angle*(M_PI/180)) * length;
37 qreal dy = qCos(angle*(M_PI/180)) * length;
38 return QPointF(dx, -dy);
38 return QPointF(dx, -dy);
39 }
39 }
40
40
41 PieSliceItem::PieSliceItem(QGraphicsItem* parent)
41 PieSliceItem::PieSliceItem(QGraphicsItem* parent)
42 :QGraphicsObject(parent),
42 :QGraphicsObject(parent),
43 m_hovered(false)
43 m_hovered(false)
44 {
44 {
45 setAcceptHoverEvents(true);
45 setAcceptHoverEvents(true);
46 setAcceptedMouseButtons(Qt::MouseButtonMask);
46 setAcceptedMouseButtons(Qt::MouseButtonMask);
47 setZValue(ChartPresenter::PieSeriesZValue);
47 setZValue(ChartPresenter::PieSeriesZValue);
48 }
48 }
49
49
50 PieSliceItem::~PieSliceItem()
50 PieSliceItem::~PieSliceItem()
51 {
51 {
52 // If user is hovering over the slice and it gets destroyed we do
52 // If user is hovering over the slice and it gets destroyed we do
53 // not get a hover leave event. So we must emit the signal here.
53 // not get a hover leave event. So we must emit the signal here.
54 if (m_hovered)
54 if (m_hovered)
55 emit hovered(false);
55 emit hovered(false);
56 }
56 }
57
57
58 QRectF PieSliceItem::boundingRect() const
58 QRectF PieSliceItem::boundingRect() const
59 {
59 {
60 return m_boundingRect;
60 return m_boundingRect;
61 }
61 }
62
62
63 QPainterPath PieSliceItem::shape() const
63 QPainterPath PieSliceItem::shape() const
64 {
64 {
65 // Don't include the label and label arm.
65 // Don't include the label and label arm.
66 // This is used to detect a mouse clicks. We do not want clicks from label.
66 // This is used to detect a mouse clicks. We do not want clicks from label.
67 return m_slicePath;
67 return m_slicePath;
68 }
68 }
69
69
70 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
70 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
71 {
71 {
72 painter->save();
72 painter->save();
73 painter->setClipRect(parentItem()->boundingRect());
73 painter->setClipRect(parentItem()->boundingRect());
74 painter->setPen(m_data.m_slicePen);
74 painter->setPen(m_data.m_slicePen);
75 painter->setBrush(m_data.m_sliceBrush);
75 painter->setBrush(m_data.m_sliceBrush);
76 painter->drawPath(m_slicePath);
76 painter->drawPath(m_slicePath);
77 painter->restore();
77 painter->restore();
78
78
79 if (m_data.m_isLabelVisible) {
79 if (m_data.m_isLabelVisible) {
80 painter->save();
80 painter->save();
81 painter->setClipRect(parentItem()->boundingRect());
81
82 // Pen for label arm not defined in the QPieSeries api, let's use brush's color instead
82 // Pen for label arm not defined in the QPieSeries api, let's use brush's color instead
83 // Also, the drawText actually uses the pen color for the text color (unlike QGraphicsSimpleTextItem)
83 // Also, the drawText actually uses the pen color for the text color (unlike QGraphicsSimpleTextItem)
84 painter->setPen(m_data.m_labelBrush.color());
84 painter->setPen(m_data.m_labelBrush.color());
85 painter->drawPath(m_labelArmPath);
85 painter->setBrush(m_data.m_labelBrush);
86 painter->setFont(m_data.m_labelFont);
86 painter->setFont(m_data.m_labelFont);
87 painter->drawText(m_labelTextRect.bottomLeft(), m_data.m_labelText);
87 if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
88 painter->setClipRect(parentItem()->boundingRect());
89 painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
90 } else { // QPieSlice::LabelInside
91 painter->setClipPath(m_slicePath);
92 }
93 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
94
88 painter->restore();
95 painter->restore();
89 }
96 }
90 }
97 }
91
98
92 void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
99 void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
93 {
100 {
94 m_hovered = true;
101 m_hovered = true;
95 emit hovered(true);
102 emit hovered(true);
96 }
103 }
97
104
98 void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
105 void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
99 {
106 {
100 m_hovered = false;
107 m_hovered = false;
101 emit hovered(false);
108 emit hovered(false);
102 }
109 }
103
110
104 void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
111 void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
105 {
112 {
106 emit clicked(event->buttons());
113 emit clicked(event->buttons());
107 }
114 }
108
115
109 void PieSliceItem::setLayout(const PieSliceData &sliceData)
116 void PieSliceItem::setLayout(const PieSliceData &sliceData)
110 {
117 {
111 m_data = sliceData;
118 m_data = sliceData;
112 updateGeometry();
119 updateGeometry();
113 update();
120 update();
114 }
121 }
115
122
116 void PieSliceItem::updateGeometry()
123 void PieSliceItem::updateGeometry()
117 {
124 {
118 if (m_data.m_radius <= 0)
125 if (m_data.m_radius <= 0)
119 return;
126 return;
120
127
121 prepareGeometryChange();
128 prepareGeometryChange();
122
129
123 // update slice path
130 // slice path
124 qreal centerAngle;
131 qreal centerAngle;
125 QPointF armStart;
132 QPointF armStart;
126 m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);
133 m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);
127
134
128 // update text rect
135 // text rect
129 m_labelTextRect = labelTextRect(m_data.m_labelFont, m_data.m_labelText);
136 QFontMetricsF fm(m_data.m_labelFont);
137 m_labelTextRect = QRectF(0, 0, fm.width(m_data.m_labelText), fm.height());
130
138
131 // update label arm path
139 // label arm path
132 QPointF labelTextStart;
140 QPointF labelTextStart;
133 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
141 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
134
142
135 // update text position
143 // text position
136 m_labelTextRect.moveBottomLeft(labelTextStart);
144 if (m_data.m_labelPosition == QPieSlice::LabelOutside)
145 m_labelTextRect.moveBottomLeft(labelTextStart);
146 else {// QPieSlice::LabelInside
147 QPointF sliceCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
148 m_labelTextRect.moveCenter(sliceCenter);
149 }
137
150
138 // update bounding rect
151 // bounding rect
139 if (m_data.m_isLabelVisible)
152 if (m_data.m_isLabelVisible)
140 m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
153 m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
141 else
154 else
142 m_boundingRect = m_slicePath.boundingRect();
155 m_boundingRect = m_slicePath.boundingRect();
143 }
156 }
144
157
145 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
158 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
146 {
159 {
147 if (slice->isExploded()) {
160 if (slice->isExploded()) {
148 qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2);
161 qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2);
149 qreal len = radius * slice->explodeDistanceFactor();
162 qreal len = radius * slice->explodeDistanceFactor();
150 point += offset(centerAngle, len);
163 point += offset(centerAngle, len);
151 }
164 }
152 return point;
165 return point;
153 }
166 }
154
167
155 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart)
168 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart)
156 {
169 {
157 // calculate center angle
170 // calculate center angle
158 *centerAngle = startAngle + (angleSpan/2);
171 *centerAngle = startAngle + (angleSpan/2);
159
172
160 // calculate slice rectangle
173 // calculate slice rectangle
161 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
174 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
162
175
163 // slice path
176 // slice path
164 // TODO: draw the shape so that it might have a hole in the center
177 // TODO: draw the shape so that it might have a hole in the center
165 QPainterPath path;
178 QPainterPath path;
166 path.moveTo(rect.center());
179 path.moveTo(rect.center());
167 path.arcTo(rect, -startAngle + 90, -angleSpan);
180 path.arcTo(rect, -startAngle + 90, -angleSpan);
168 path.closeSubpath();
181 path.closeSubpath();
169
182
170 // calculate label arm start point
183 // calculate label arm start point
171 *armStart = center;
184 *armStart = center;
172 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
185 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
173
186
174 return path;
187 return path;
175 }
188 }
176
189
177 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart)
190 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart)
178 {
191 {
179 // Normalize the angle to 0-360 range
192 // Normalize the angle to 0-360 range
180 // NOTE: We are using int here on purpose. Depenging on platform and hardware
193 // NOTE: We are using int here on purpose. Depenging on platform and hardware
181 // qreal can be a double, float or something the user gives to the Qt configure
194 // qreal can be a double, float or something the user gives to the Qt configure
182 // (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float
195 // (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float
183 // but there are fmod() and fmodf() functions for that. So instead of some #ifdef
196 // but there are fmod() and fmodf() functions for that. So instead of some #ifdef
184 // that might break we just use int. Precision for this is just fine for our needs.
197 // that might break we just use int. Precision for this is just fine for our needs.
185 int normalized = angle * 10.0;
198 int normalized = angle * 10.0;
186 normalized = normalized % 3600;
199 normalized = normalized % 3600;
187 if (normalized < 0)
200 if (normalized < 0)
188 normalized += 3600;
201 normalized += 3600;
189 angle = (qreal) normalized / 10.0;
202 angle = (qreal) normalized / 10.0;
190
203
191 // prevent label arm pointing straight down because it will look bad
204 // prevent label arm pointing straight down because it will look bad
192 if (angle < 180 && angle > 170)
205 if (angle < 180 && angle > 170)
193 angle = 170;
206 angle = 170;
194 if (angle > 180 && angle < 190)
207 if (angle > 180 && angle < 190)
195 angle = 190;
208 angle = 190;
196
209
197 // line from slice to label
210 // line from slice to label
198 QPointF parm1 = start + offset(angle, length);
211 QPointF parm1 = start + offset(angle, length);
199
212
200 // line to underline the label
213 // line to underline the label
201 QPointF parm2 = parm1;
214 QPointF parm2 = parm1;
202 if (angle < 180) { // arm swings the other way on the left side
215 if (angle < 180) { // arm swings the other way on the left side
203 parm2 += QPointF(textWidth, 0);
216 parm2 += QPointF(textWidth, 0);
204 *textStart = parm1;
217 *textStart = parm1;
205 }
218 }
206 else {
219 else {
207 parm2 += QPointF(-textWidth,0);
220 parm2 += QPointF(-textWidth,0);
208 *textStart = parm2;
221 *textStart = parm2;
209 }
222 }
210
223
211 // elevate the text position a bit so that it does not hit the line
212 *textStart += QPointF(0, -3);
213
214 QPainterPath path;
224 QPainterPath path;
215 path.moveTo(start);
225 path.moveTo(start);
216 path.lineTo(parm1);
226 path.lineTo(parm1);
217 path.lineTo(parm2);
227 path.lineTo(parm2);
218
228
219 return path;
229 return path;
220 }
230 }
221
231
222 QRectF PieSliceItem::labelTextRect(QFont font, QString text)
223 {
224 QFontMetricsF fm(font);
225 return fm.boundingRect(text);
226 }
227
228 #include "moc_piesliceitem_p.cpp"
232 #include "moc_piesliceitem_p.cpp"
229
233
230 QTCOMMERCIALCHART_END_NAMESPACE
234 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,89 +1,88
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef PIESLICEITEM_H
30 #ifndef PIESLICEITEM_H
31 #define PIESLICEITEM_H
31 #define PIESLICEITEM_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include "charttheme_p.h"
34 #include "charttheme_p.h"
35 #include "qpieseries.h"
35 #include "qpieseries.h"
36 #include "pieslicedata_p.h"
36 #include "pieslicedata_p.h"
37 #include <QGraphicsItem>
37 #include <QGraphicsItem>
38 #include <QRectF>
38 #include <QRectF>
39 #include <QColor>
39 #include <QColor>
40 #include <QPen>
40 #include <QPen>
41
41
42 #define PIESLICE_LABEL_GAP 5
42 #define PIESLICE_LABEL_GAP 5
43
43
44 QTCOMMERCIALCHART_BEGIN_NAMESPACE
44 QTCOMMERCIALCHART_BEGIN_NAMESPACE
45 class PieChartItem;
45 class PieChartItem;
46 class PieSliceLabel;
46 class PieSliceLabel;
47 class QPieSlice;
47 class QPieSlice;
48
48
49 class PieSliceItem : public QGraphicsObject
49 class PieSliceItem : public QGraphicsObject
50 {
50 {
51 Q_OBJECT
51 Q_OBJECT
52
52
53 public:
53 public:
54 PieSliceItem(QGraphicsItem* parent = 0);
54 PieSliceItem(QGraphicsItem* parent = 0);
55 ~PieSliceItem();
55 ~PieSliceItem();
56
56
57 // from QGraphicsItem
57 // from QGraphicsItem
58 QRectF boundingRect() const;
58 QRectF boundingRect() const;
59 QPainterPath shape() const;
59 QPainterPath shape() const;
60 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
60 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
61 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
61 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
62 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
62 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
63 void mousePressEvent(QGraphicsSceneMouseEvent *event);
63 void mousePressEvent(QGraphicsSceneMouseEvent *event);
64
64
65 void setLayout(const PieSliceData &sliceData);
65 void setLayout(const PieSliceData &sliceData);
66 static QPointF sliceCenter(QPointF point, qreal radius, QPieSlice *slice);
66 static QPointF sliceCenter(QPointF point, qreal radius, QPieSlice *slice);
67
67
68 Q_SIGNALS:
68 Q_SIGNALS:
69 void clicked(Qt::MouseButtons buttons);
69 void clicked(Qt::MouseButtons buttons);
70 void hovered(bool state);
70 void hovered(bool state);
71
71
72 private:
72 private:
73 void updateGeometry();
73 void updateGeometry();
74 QPainterPath slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF *armStart);
74 QPainterPath slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF *armStart);
75 QPainterPath labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart);
75 QPainterPath labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart);
76 QRectF labelTextRect(QFont font, QString text);
77
76
78 private:
77 private:
79 PieSliceData m_data;
78 PieSliceData m_data;
80 QRectF m_boundingRect;
79 QRectF m_boundingRect;
81 QPainterPath m_slicePath;
80 QPainterPath m_slicePath;
82 QPainterPath m_labelArmPath;
81 QPainterPath m_labelArmPath;
83 QRectF m_labelTextRect;
82 QRectF m_labelTextRect;
84 bool m_hovered;
83 bool m_hovered;
85 };
84 };
86
85
87 QTCOMMERCIALCHART_END_NAMESPACE
86 QTCOMMERCIALCHART_END_NAMESPACE
88
87
89 #endif // PIESLICEITEM_H
88 #endif // PIESLICEITEM_H
@@ -1,695 +1,734
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qpieslice.h"
21 #include "qpieslice.h"
22 #include "qpieslice_p.h"
22 #include "qpieslice_p.h"
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 /*!
26 /*!
27 \class QPieSlice
27 \class QPieSlice
28 \brief Defines a slice in pie series.
28 \brief Defines a slice in pie series.
29
29
30 This object defines the properties of a single slice in a QPieSeries.
30 This object defines the properties of a single slice in a QPieSeries.
31
31
32 In addition to the obvious value and label properties the user can also control
32 In addition to the obvious value and label properties the user can also control
33 the visual appearance of a slice. By modifying the visual appearance also means that
33 the visual appearance of a slice. By modifying the visual appearance also means that
34 the user is overriding the default appearance set by the theme.
34 the user is overriding the default appearance set by the theme.
35
35
36 Note that if the user has customized slices and theme is changed all customizations will be lost.
36 Note that if the user has customized slices and theme is changed all customizations will be lost.
37
37
38 To enable user interaction with the pie some basic signals are provided about clicking and hovering.
38 To enable user interaction with the pie some basic signals are provided about clicking and hovering.
39 */
39 */
40
40
41 /*!
41 /*!
42 \enum QPieSlice::LabelPosition
43
44 This enum describes the position of the slice label.
45
46 \value LabelOutside Label is outside the slice with an arm.
47 \value LabelInside Label is centered inside the slice.
48
49 */
50
51 /*!
42 \property QPieSlice::label
52 \property QPieSlice::label
43
53
44 Label of the slice.
54 Label of the slice.
45
55
46 \sa labelVisible, labelBrush, labelFont, labelArmLengthFactor
56 \sa labelVisible, labelBrush, labelFont, labelArmLengthFactor
47 */
57 */
48
58
49 /*!
59 /*!
50 \fn void QPieSlice::labelChanged()
60 \fn void QPieSlice::labelChanged()
51
61
52 This signal emitted when the slice label has been changed.
62 This signal emitted when the slice label has been changed.
53
63
54 \sa label
64 \sa label
55 */
65 */
56
66
57 /*!
67 /*!
58 \property QPieSlice::value
68 \property QPieSlice::value
59
69
60 Value of the slice.
70 Value of the slice.
61
71
62 Note that if users sets a negative value it is converted to a positive value.
72 Note that if users sets a negative value it is converted to a positive value.
63
73
64 \sa percentage(), QPieSeries::sum()
74 \sa percentage(), QPieSeries::sum()
65 */
75 */
66
76
67 /*!
77 /*!
68 \fn void QPieSlice::valueChanged()
78 \fn void QPieSlice::valueChanged()
69
79
70 This signal is emitted when the slice value changes.
80 This signal is emitted when the slice value changes.
71
81
72 \sa value
82 \sa value
73 */
83 */
74
84
75 /*!
85 /*!
76 \property QPieSlice::labelVisible
86 \property QPieSlice::labelVisible
77
87
78 Defienes the visibility of the slice label.
88 Defienes the visibility of the slice label.
79
89
80 Default is not visible.
90 Default is not visible.
81
91
82 \sa label, labelBrush, labelFont, labelArmLengthFactor
92 \sa label, labelBrush, labelFont, labelArmLengthFactor
83 */
93 */
84
94
85 /*!
95 /*!
86 \fn void QPieSlice::labelVisibleChanged()
96 \fn void QPieSlice::labelVisibleChanged()
87
97
88 This signal emitted when visibility of the slice label has changed.
98 This signal emitted when visibility of the slice label has changed.
89
99
90 \sa labelVisible
100 \sa labelVisible
91 */
101 */
92
102
93 /*!
103 /*!
94 \property QPieSlice::exploded
104 \property QPieSlice::exploded
95
105
96 Defines if the slice is exploded from the pie.
106 Defines if the slice is exploded from the pie.
97
107
98 \sa explodeDistanceFactor
108 \sa explodeDistanceFactor
99 */
109 */
100
110
101 /*!
111 /*!
102 \fn void QPieSlice::explodedChanged()
112 \fn void QPieSlice::explodedChanged()
103
113
104 This signal is emitted the the slice has been exploded from the pie or is returned back to the pie.
114 This signal is emitted the the slice has been exploded from the pie or is returned back to the pie.
105
115
106 \sa exploded
116 \sa exploded
107 */
117 */
108
118
109 /*!
119 /*!
110 \property QPieSlice::pen
120 \property QPieSlice::pen
111
121
112 Pen used to draw the slice border.
122 Pen used to draw the slice border.
113 */
123 */
114
124
115 /*!
125 /*!
116 \fn void QPieSlice::penChanged()
126 \fn void QPieSlice::penChanged()
117
127
118 This signal is emitted when the pen of the slice has changed.
128 This signal is emitted when the pen of the slice has changed.
119
129
120 \sa pen
130 \sa pen
121 */
131 */
122
132
123 /*!
133 /*!
124 \property QPieSlice::borderColor
134 \property QPieSlice::borderColor
125
135
126 Color used to draw the slice border.
136 Color used to draw the slice border.
127
137
128 This is a convenience property for modifying the slice pen.
138 This is a convenience property for modifying the slice pen.
129
139
130 \sa pen, borderWidth
140 \sa pen, borderWidth
131 */
141 */
132
142
133 /*!
143 /*!
134 \fn void QPieSlice::borderColorChanged()
144 \fn void QPieSlice::borderColorChanged()
135
145
136 This signal is emitted when slice border color changes.
146 This signal is emitted when slice border color changes.
137
147
138 \sa pen, borderColor
148 \sa pen, borderColor
139 */
149 */
140
150
141 /*!
151 /*!
142 \property QPieSlice::borderWidth
152 \property QPieSlice::borderWidth
143
153
144 Width of the slice border.
154 Width of the slice border.
145
155
146 This is a convenience property for modifying the slice pen.
156 This is a convenience property for modifying the slice pen.
147
157
148 \sa pen, borderColor
158 \sa pen, borderColor
149 */
159 */
150
160
151 /*!
161 /*!
152 \fn void QPieSlice::borderWidthChanged()
162 \fn void QPieSlice::borderWidthChanged()
153
163
154 This signal is emitted when slice border width changes.
164 This signal is emitted when slice border width changes.
155
165
156 \sa pen, borderWidth
166 \sa pen, borderWidth
157 */
167 */
158
168
159 /*!
169 /*!
160 \property QPieSlice::brush
170 \property QPieSlice::brush
161
171
162 Brush used to draw the slice.
172 Brush used to draw the slice.
163 */
173 */
164
174
165 /*!
175 /*!
166 \fn void QPieSlice::brushChanged()
176 \fn void QPieSlice::brushChanged()
167
177
168 This signal is emitted when the brush of the slice has changed.
178 This signal is emitted when the brush of the slice has changed.
169
179
170 \sa brush
180 \sa brush
171 */
181 */
172
182
173 /*!
183 /*!
174 \property QPieSlice::color
184 \property QPieSlice::color
175
185
176 Color used to draw the slice.
186 Color used to draw the slice.
177
187
178 This is a convenience property for modifying the slice brush.
188 This is a convenience property for modifying the slice brush.
179
189
180 \sa brush
190 \sa brush
181 */
191 */
182
192
183 /*!
193 /*!
184 \fn void QPieSlice::colorChanged()
194 \fn void QPieSlice::colorChanged()
185
195
186 This signal is emitted when slice color changes.
196 This signal is emitted when slice color changes.
187
197
188 \sa brush
198 \sa brush
189 */
199 */
190
200
191 /*!
201 /*!
192 \property QPieSlice::labelBrush
202 \property QPieSlice::labelBrush
193
203
194 Pen used to draw label and label arm of the slice.
204 Pen used to draw label and label arm of the slice.
195
205
196 \sa label, labelVisible, labelFont, labelArmLengthFactor
206 \sa label, labelVisible, labelFont, labelArmLengthFactor
197 */
207 */
198
208
199 /*!
209 /*!
200 \fn void QPieSlice::labelBrushChanged()
210 \fn void QPieSlice::labelBrushChanged()
201
211
202 This signal is emitted when the label pen of the slice has changed.
212 This signal is emitted when the label pen of the slice has changed.
203
213
204 \sa labelBrush
214 \sa labelBrush
205 */
215 */
206
216
207 /*!
217 /*!
208 \property QPieSlice::labelColor
218 \property QPieSlice::labelColor
209
219
210 Color used to draw the slice label.
220 Color used to draw the slice label.
211
221
212 This is a convenience property for modifying the slice label brush.
222 This is a convenience property for modifying the slice label brush.
213
223
214 \sa labelBrush
224 \sa labelBrush
215 */
225 */
216
226
217 /*!
227 /*!
218 \fn void QPieSlice::labelColorChanged()
228 \fn void QPieSlice::labelColorChanged()
219
229
220 This signal is emitted when slice label color changes.
230 This signal is emitted when slice label color changes.
221
231
222 \sa labelColor
232 \sa labelColor
223 */
233 */
224
234
225 /*!
235 /*!
226 \property QPieSlice::labelFont
236 \property QPieSlice::labelFont
227
237
228 Font used for drawing label text.
238 Font used for drawing label text.
229
239
230 \sa label, labelVisible, labelArmLengthFactor
240 \sa label, labelVisible, labelArmLengthFactor
231 */
241 */
232
242
233 /*!
243 /*!
234 \fn void QPieSlice::labelFontChanged()
244 \fn void QPieSlice::labelFontChanged()
235
245
236 This signal is emitted when the label font of the slice has changed.
246 This signal is emitted when the label font of the slice has changed.
237
247
238 \sa labelFont
248 \sa labelFont
239 */
249 */
240
250
241 /*!
251 /*!
252 \property QPieSlice::labelPosition
253
254 Position of the slice label.
255
256 \sa label, labelVisible
257 */
258
259 /*!
260 \fn void QPieSlice::labelPositionChanged()
261
262 This signal is emitted when the label position of the slice has changed.
263
264 \sa labelPosition
265 */
266
267 /*!
242 \property QPieSlice::labelArmLengthFactor
268 \property QPieSlice::labelArmLengthFactor
243
269
244 Defines the length of the label arm.
270 Defines the length of the label arm.
245
271
246 The factor is relative to pie radius. For example:
272 The factor is relative to pie radius. For example:
247 1.0 means the length is the same as the radius.
273 1.0 means the length is the same as the radius.
248 0.5 means the length is half of the radius.
274 0.5 means the length is half of the radius.
249
275
250 Default value is 0.15
276 Default value is 0.15
251
277
252 \sa label, labelVisible, labelBrush, labelFont
278 \sa label, labelVisible, labelBrush, labelFont
253 */
279 */
254
280
255 /*!
281 /*!
256 \fn void QPieSlice::labelArmLengthFactorChanged()
282 \fn void QPieSlice::labelArmLengthFactorChanged()
257
283
258 This signal is emitted when the label arm factor of the slice has changed.
284 This signal is emitted when the label arm factor of the slice has changed.
259
285
260 \sa labelArmLengthFactor
286 \sa labelArmLengthFactor
261 */
287 */
262
288
263 /*!
289 /*!
264 \property QPieSlice::explodeDistanceFactor
290 \property QPieSlice::explodeDistanceFactor
265
291
266 When the slice is exploded this factor defines how far the slice is exploded away from the pie.
292 When the slice is exploded this factor defines how far the slice is exploded away from the pie.
267
293
268 The factor is relative to pie radius. For example:
294 The factor is relative to pie radius. For example:
269 1.0 means the distance is the same as the radius.
295 1.0 means the distance is the same as the radius.
270 0.5 means the distance is half of the radius.
296 0.5 means the distance is half of the radius.
271
297
272 Default value is 0.15
298 Default value is 0.15
273
299
274 \sa exploded
300 \sa exploded
275 */
301 */
276
302
277 /*!
303 /*!
278 \fn void QPieSlice::explodeDistanceFactorChanged()
304 \fn void QPieSlice::explodeDistanceFactorChanged()
279
305
280 This signal is emitted when the explode distance factor of the slice has changed.
306 This signal is emitted when the explode distance factor of the slice has changed.
281
307
282 \sa explodeDistanceFactor
308 \sa explodeDistanceFactor
283 */
309 */
284
310
285 /*!
311 /*!
286 \property QPieSlice::percentage
312 \property QPieSlice::percentage
287
313
288 Percentage of the slice compared to the sum of all slices in the series.
314 Percentage of the slice compared to the sum of all slices in the series.
289
315
290 The actual value ranges from 0.0 to 1.0.
316 The actual value ranges from 0.0 to 1.0.
291
317
292 Updated automatically once the slice is added to the series.
318 Updated automatically once the slice is added to the series.
293
319
294 \sa value, QPieSeries::sum
320 \sa value, QPieSeries::sum
295 */
321 */
296
322
297 /*!
323 /*!
298 \fn void QPieSlice::percentageChanged()
324 \fn void QPieSlice::percentageChanged()
299
325
300 This signal is emitted when the percentage of the slice has changed.
326 This signal is emitted when the percentage of the slice has changed.
301
327
302 \sa percentage
328 \sa percentage
303 */
329 */
304
330
305 /*!
331 /*!
306 \property QPieSlice::startAngle
332 \property QPieSlice::startAngle
307
333
308 Defines the starting angle of this slice in the series it belongs to.
334 Defines the starting angle of this slice in the series it belongs to.
309
335
310 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
336 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
311
337
312 Updated automatically once the slice is added to the series.
338 Updated automatically once the slice is added to the series.
313 */
339 */
314
340
315 /*!
341 /*!
316 \fn void QPieSlice::startAngleChanged()
342 \fn void QPieSlice::startAngleChanged()
317
343
318 This signal is emitted when the starting angle f the slice has changed.
344 This signal is emitted when the starting angle f the slice has changed.
319
345
320 \sa startAngle
346 \sa startAngle
321 */
347 */
322
348
323 /*!
349 /*!
324 \property QPieSlice::angleSpan
350 \property QPieSlice::angleSpan
325
351
326 Span of the slice in degrees.
352 Span of the slice in degrees.
327
353
328 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
354 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
329
355
330 Updated automatically once the slice is added to the series.
356 Updated automatically once the slice is added to the series.
331 */
357 */
332
358
333 /*!
359 /*!
334 \fn void QPieSlice::angleSpanChanged()
360 \fn void QPieSlice::angleSpanChanged()
335
361
336 This signal is emitted when the angle span of the slice has changed.
362 This signal is emitted when the angle span of the slice has changed.
337
363
338 \sa angleSpan
364 \sa angleSpan
339 */
365 */
340
366
341
367
342 /*!
368 /*!
343 \fn void QPieSlice::clicked()
369 \fn void QPieSlice::clicked()
344
370
345 This signal is emitted when user has clicked the slice.
371 This signal is emitted when user has clicked the slice.
346
372
347 \sa QPieSeries::clicked()
373 \sa QPieSeries::clicked()
348 */
374 */
349
375
350 /*!
376 /*!
351 \fn void QPieSlice::hovered(bool state)
377 \fn void QPieSlice::hovered(bool state)
352
378
353 This signal is emitted when user has hovered over or away from the slice.
379 This signal is emitted when user has hovered over or away from the slice.
354
380
355 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
381 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
356
382
357 \sa QPieSeries::hovered()
383 \sa QPieSeries::hovered()
358 */
384 */
359
385
360 /*!
386 /*!
361 Constructs an empty slice with a \a parent.
387 Constructs an empty slice with a \a parent.
362
388
363 \sa QPieSeries::append(), QPieSeries::insert()
389 \sa QPieSeries::append(), QPieSeries::insert()
364 */
390 */
365 QPieSlice::QPieSlice(QObject *parent)
391 QPieSlice::QPieSlice(QObject *parent)
366 :QObject(parent),
392 :QObject(parent),
367 d_ptr(new QPieSlicePrivate(this))
393 d_ptr(new QPieSlicePrivate(this))
368 {
394 {
369
395
370 }
396 }
371
397
372 /*!
398 /*!
373 Constructs an empty slice with given \a value, \a label and a \a parent.
399 Constructs an empty slice with given \a value, \a label and a \a parent.
374 \sa QPieSeries::append(), QPieSeries::insert()
400 \sa QPieSeries::append(), QPieSeries::insert()
375 */
401 */
376 QPieSlice::QPieSlice(QString label, qreal value, QObject *parent)
402 QPieSlice::QPieSlice(QString label, qreal value, QObject *parent)
377 :QObject(parent),
403 :QObject(parent),
378 d_ptr(new QPieSlicePrivate(this))
404 d_ptr(new QPieSlicePrivate(this))
379 {
405 {
380 setValue(value);
406 setValue(value);
381 setLabel(label);
407 setLabel(label);
382 }
408 }
383
409
384 /*!
410 /*!
385 Destroys the slice.
411 Destroys the slice.
386 User should not delete the slice if it has been added to the series.
412 User should not delete the slice if it has been added to the series.
387 */
413 */
388 QPieSlice::~QPieSlice()
414 QPieSlice::~QPieSlice()
389 {
415 {
390
416
391 }
417 }
392
418
393 void QPieSlice::setLabel(QString label)
419 void QPieSlice::setLabel(QString label)
394 {
420 {
395 if (d_ptr->m_data.m_labelText != label) {
421 if (d_ptr->m_data.m_labelText != label) {
396 d_ptr->m_data.m_labelText = label;
422 d_ptr->m_data.m_labelText = label;
397 emit labelChanged();
423 emit labelChanged();
398 }
424 }
399 }
425 }
400
426
401 QString QPieSlice::label() const
427 QString QPieSlice::label() const
402 {
428 {
403 return d_ptr->m_data.m_labelText;
429 return d_ptr->m_data.m_labelText;
404 }
430 }
405
431
406 void QPieSlice::setValue(qreal value)
432 void QPieSlice::setValue(qreal value)
407 {
433 {
408 value = qAbs(value); // negative values not allowed
434 value = qAbs(value); // negative values not allowed
409 if (!qFuzzyIsNull(d_ptr->m_data.m_value - value)) {
435 if (!qFuzzyIsNull(d_ptr->m_data.m_value - value)) {
410 d_ptr->m_data.m_value = value;
436 d_ptr->m_data.m_value = value;
411 emit valueChanged();
437 emit valueChanged();
412 }
438 }
413 }
439 }
414
440
415 qreal QPieSlice::value() const
441 qreal QPieSlice::value() const
416 {
442 {
417 return d_ptr->m_data.m_value;
443 return d_ptr->m_data.m_value;
418 }
444 }
419
445
420 void QPieSlice::setLabelVisible(bool visible)
446 void QPieSlice::setLabelVisible(bool visible)
421 {
447 {
422 if (d_ptr->m_data.m_isLabelVisible != visible) {
448 if (d_ptr->m_data.m_isLabelVisible != visible) {
423 d_ptr->m_data.m_isLabelVisible = visible;
449 d_ptr->m_data.m_isLabelVisible = visible;
424 emit labelVisibleChanged();
450 emit labelVisibleChanged();
425 }
451 }
426 }
452 }
427
453
428 bool QPieSlice::isLabelVisible() const
454 bool QPieSlice::isLabelVisible() const
429 {
455 {
430 return d_ptr->m_data.m_isLabelVisible;
456 return d_ptr->m_data.m_isLabelVisible;
431 }
457 }
432
458
433 void QPieSlice::setExploded(bool exploded)
459 void QPieSlice::setExploded(bool exploded)
434 {
460 {
435 if (d_ptr->m_data.m_isExploded != exploded) {
461 if (d_ptr->m_data.m_isExploded != exploded) {
436 d_ptr->m_data.m_isExploded = exploded;
462 d_ptr->m_data.m_isExploded = exploded;
437 emit explodedChanged();
463 emit explodedChanged();
438 }
464 }
439 }
465 }
440
466
467 QPieSlice::LabelPosition QPieSlice::labelPosition()
468 {
469 return d_ptr->m_data.m_labelPosition;
470 }
471
472 void QPieSlice::setLabelPosition(LabelPosition position)
473 {
474 if (d_ptr->m_data.m_labelPosition != position) {
475 d_ptr->m_data.m_labelPosition = position;
476 emit labelPositionChanged();
477 }
478 }
479
441 bool QPieSlice::isExploded() const
480 bool QPieSlice::isExploded() const
442 {
481 {
443 return d_ptr->m_data.m_isExploded;
482 return d_ptr->m_data.m_isExploded;
444 }
483 }
445
484
446 void QPieSlice::setPen(const QPen &pen)
485 void QPieSlice::setPen(const QPen &pen)
447 {
486 {
448 d_ptr->setPen(pen, false);
487 d_ptr->setPen(pen, false);
449 }
488 }
450
489
451 QPen QPieSlice::pen() const
490 QPen QPieSlice::pen() const
452 {
491 {
453 return d_ptr->m_data.m_slicePen;
492 return d_ptr->m_data.m_slicePen;
454 }
493 }
455
494
456 QColor QPieSlice::borderColor()
495 QColor QPieSlice::borderColor()
457 {
496 {
458 return pen().color();
497 return pen().color();
459 }
498 }
460
499
461 void QPieSlice::setBorderColor(QColor color)
500 void QPieSlice::setBorderColor(QColor color)
462 {
501 {
463 QPen p = pen();
502 QPen p = pen();
464 if (color != p.color()) {
503 if (color != p.color()) {
465 p.setColor(color);
504 p.setColor(color);
466 setPen(p);
505 setPen(p);
467 }
506 }
468 }
507 }
469
508
470 int QPieSlice::borderWidth()
509 int QPieSlice::borderWidth()
471 {
510 {
472 return pen().width();
511 return pen().width();
473 }
512 }
474
513
475 void QPieSlice::setBorderWidth(int width)
514 void QPieSlice::setBorderWidth(int width)
476 {
515 {
477 QPen p = pen();
516 QPen p = pen();
478 if (width != p.width()) {
517 if (width != p.width()) {
479 p.setWidth(width);
518 p.setWidth(width);
480 setPen(p);
519 setPen(p);
481 }
520 }
482 }
521 }
483
522
484 void QPieSlice::setBrush(const QBrush &brush)
523 void QPieSlice::setBrush(const QBrush &brush)
485 {
524 {
486 d_ptr->setBrush(brush, false);
525 d_ptr->setBrush(brush, false);
487 }
526 }
488
527
489 QBrush QPieSlice::brush() const
528 QBrush QPieSlice::brush() const
490 {
529 {
491 return d_ptr->m_data.m_sliceBrush;
530 return d_ptr->m_data.m_sliceBrush;
492 }
531 }
493
532
494 QColor QPieSlice::color()
533 QColor QPieSlice::color()
495 {
534 {
496 return brush().color();
535 return brush().color();
497 }
536 }
498
537
499 void QPieSlice::setColor(QColor color)
538 void QPieSlice::setColor(QColor color)
500 {
539 {
501 QBrush b = brush();
540 QBrush b = brush();
502 if (color != b.color()) {
541 if (color != b.color()) {
503 b.setColor(color);
542 b.setColor(color);
504 setBrush(b);
543 setBrush(b);
505 }
544 }
506 }
545 }
507
546
508 void QPieSlice::setLabelBrush(const QBrush &brush)
547 void QPieSlice::setLabelBrush(const QBrush &brush)
509 {
548 {
510 d_ptr->setLabelBrush(brush, false);
549 d_ptr->setLabelBrush(brush, false);
511 }
550 }
512
551
513 QBrush QPieSlice::labelBrush() const
552 QBrush QPieSlice::labelBrush() const
514 {
553 {
515 return d_ptr->m_data.m_labelBrush;
554 return d_ptr->m_data.m_labelBrush;
516 }
555 }
517
556
518 QColor QPieSlice::labelColor()
557 QColor QPieSlice::labelColor()
519 {
558 {
520 return labelBrush().color();
559 return labelBrush().color();
521 }
560 }
522
561
523 void QPieSlice::setLabelColor(QColor color)
562 void QPieSlice::setLabelColor(QColor color)
524 {
563 {
525 QBrush b = labelBrush();
564 QBrush b = labelBrush();
526 if (color != b.color()) {
565 if (color != b.color()) {
527 b.setColor(color);
566 b.setColor(color);
528 setLabelBrush(b);
567 setLabelBrush(b);
529 }
568 }
530 }
569 }
531
570
532 void QPieSlice::setLabelFont(const QFont &font)
571 void QPieSlice::setLabelFont(const QFont &font)
533 {
572 {
534 d_ptr->setLabelFont(font, false);
573 d_ptr->setLabelFont(font, false);
535 }
574 }
536
575
537 QFont QPieSlice::labelFont() const
576 QFont QPieSlice::labelFont() const
538 {
577 {
539 return d_ptr->m_data.m_labelFont;
578 return d_ptr->m_data.m_labelFont;
540 }
579 }
541
580
542 void QPieSlice::setLabelArmLengthFactor(qreal factor)
581 void QPieSlice::setLabelArmLengthFactor(qreal factor)
543 {
582 {
544 if (!qFuzzyIsNull(d_ptr->m_data.m_labelArmLengthFactor - factor)) {
583 if (!qFuzzyIsNull(d_ptr->m_data.m_labelArmLengthFactor - factor)) {
545 d_ptr->m_data.m_labelArmLengthFactor = factor;
584 d_ptr->m_data.m_labelArmLengthFactor = factor;
546 emit labelArmLengthFactorChanged();
585 emit labelArmLengthFactorChanged();
547 }
586 }
548 }
587 }
549
588
550 qreal QPieSlice::labelArmLengthFactor() const
589 qreal QPieSlice::labelArmLengthFactor() const
551 {
590 {
552 return d_ptr->m_data.m_labelArmLengthFactor;
591 return d_ptr->m_data.m_labelArmLengthFactor;
553 }
592 }
554
593
555 void QPieSlice::setExplodeDistanceFactor(qreal factor)
594 void QPieSlice::setExplodeDistanceFactor(qreal factor)
556 {
595 {
557 if (!qFuzzyIsNull(d_ptr->m_data.m_explodeDistanceFactor - factor)) {
596 if (!qFuzzyIsNull(d_ptr->m_data.m_explodeDistanceFactor - factor)) {
558 d_ptr->m_data.m_explodeDistanceFactor = factor;
597 d_ptr->m_data.m_explodeDistanceFactor = factor;
559 emit explodeDistanceFactorChanged();
598 emit explodeDistanceFactorChanged();
560 }
599 }
561 }
600 }
562
601
563 qreal QPieSlice::explodeDistanceFactor() const
602 qreal QPieSlice::explodeDistanceFactor() const
564 {
603 {
565 return d_ptr->m_data.m_explodeDistanceFactor;
604 return d_ptr->m_data.m_explodeDistanceFactor;
566 }
605 }
567
606
568 qreal QPieSlice::percentage() const
607 qreal QPieSlice::percentage() const
569 {
608 {
570 return d_ptr->m_data.m_percentage;
609 return d_ptr->m_data.m_percentage;
571 }
610 }
572
611
573 qreal QPieSlice::startAngle() const
612 qreal QPieSlice::startAngle() const
574 {
613 {
575 return d_ptr->m_data.m_startAngle;
614 return d_ptr->m_data.m_startAngle;
576 }
615 }
577
616
578 qreal QPieSlice::angleSpan() const
617 qreal QPieSlice::angleSpan() const
579 {
618 {
580 return d_ptr->m_data.m_angleSpan;
619 return d_ptr->m_data.m_angleSpan;
581 }
620 }
582
621
583 /*!
622 /*!
584 Returns the series that this slice belongs to.
623 Returns the series that this slice belongs to.
585
624
586 \sa QPieSeries::append()
625 \sa QPieSeries::append()
587 */
626 */
588 QPieSeries *QPieSlice::series() const
627 QPieSeries *QPieSlice::series() const
589 {
628 {
590 return d_ptr->m_series;
629 return d_ptr->m_series;
591 }
630 }
592
631
593 QPieSlicePrivate::QPieSlicePrivate(QPieSlice *parent)
632 QPieSlicePrivate::QPieSlicePrivate(QPieSlice *parent)
594 :QObject(parent),
633 :QObject(parent),
595 q_ptr(parent),
634 q_ptr(parent),
596 m_series(0)
635 m_series(0)
597 {
636 {
598
637
599 }
638 }
600
639
601 QPieSlicePrivate::~QPieSlicePrivate()
640 QPieSlicePrivate::~QPieSlicePrivate()
602 {
641 {
603
642
604 }
643 }
605
644
606 QPieSlicePrivate *QPieSlicePrivate::fromSlice(QPieSlice *slice)
645 QPieSlicePrivate *QPieSlicePrivate::fromSlice(QPieSlice *slice)
607 {
646 {
608 return slice->d_func();
647 return slice->d_func();
609 }
648 }
610
649
611 void QPieSlicePrivate::setPen(const QPen &pen, bool themed)
650 void QPieSlicePrivate::setPen(const QPen &pen, bool themed)
612 {
651 {
613 if (m_data.m_slicePen != pen) {
652 if (m_data.m_slicePen != pen) {
614
653
615 QPen oldPen = m_data.m_slicePen;
654 QPen oldPen = m_data.m_slicePen;
616
655
617 m_data.m_slicePen = pen;
656 m_data.m_slicePen = pen;
618 m_data.m_slicePen.setThemed(themed);
657 m_data.m_slicePen.setThemed(themed);
619
658
620 emit q_ptr->penChanged();
659 emit q_ptr->penChanged();
621 if (oldPen.color() != pen.color())
660 if (oldPen.color() != pen.color())
622 emit q_ptr->borderColorChanged();
661 emit q_ptr->borderColorChanged();
623 if (oldPen.width() != pen.width())
662 if (oldPen.width() != pen.width())
624 emit q_ptr->borderWidthChanged();
663 emit q_ptr->borderWidthChanged();
625 }
664 }
626 }
665 }
627
666
628 void QPieSlicePrivate::setBrush(const QBrush &brush, bool themed)
667 void QPieSlicePrivate::setBrush(const QBrush &brush, bool themed)
629 {
668 {
630 if (m_data.m_sliceBrush != brush) {
669 if (m_data.m_sliceBrush != brush) {
631
670
632 QBrush oldBrush = m_data.m_sliceBrush;
671 QBrush oldBrush = m_data.m_sliceBrush;
633
672
634 m_data.m_sliceBrush = brush;
673 m_data.m_sliceBrush = brush;
635 m_data.m_sliceBrush.setThemed(themed);
674 m_data.m_sliceBrush.setThemed(themed);
636
675
637 emit q_ptr->brushChanged();
676 emit q_ptr->brushChanged();
638 if (oldBrush.color() != brush.color())
677 if (oldBrush.color() != brush.color())
639 emit q_ptr->colorChanged();
678 emit q_ptr->colorChanged();
640 }
679 }
641 }
680 }
642
681
643 void QPieSlicePrivate::setLabelBrush(const QBrush &brush, bool themed)
682 void QPieSlicePrivate::setLabelBrush(const QBrush &brush, bool themed)
644 {
683 {
645 if (m_data.m_labelBrush != brush) {
684 if (m_data.m_labelBrush != brush) {
646
685
647 QBrush oldBrush = m_data.m_labelBrush;
686 QBrush oldBrush = m_data.m_labelBrush;
648
687
649 m_data.m_labelBrush = brush;
688 m_data.m_labelBrush = brush;
650 m_data.m_labelBrush.setThemed(themed);
689 m_data.m_labelBrush.setThemed(themed);
651
690
652 emit q_ptr->labelBrushChanged();
691 emit q_ptr->labelBrushChanged();
653 if (oldBrush.color() != brush.color())
692 if (oldBrush.color() != brush.color())
654 emit q_ptr->labelColorChanged();
693 emit q_ptr->labelColorChanged();
655 }
694 }
656 }
695 }
657
696
658 void QPieSlicePrivate::setLabelFont(const QFont &font, bool themed)
697 void QPieSlicePrivate::setLabelFont(const QFont &font, bool themed)
659 {
698 {
660 if (m_data.m_labelFont != font) {
699 if (m_data.m_labelFont != font) {
661 m_data.m_labelFont = font;
700 m_data.m_labelFont = font;
662 m_data.m_labelFont.setThemed(themed);
701 m_data.m_labelFont.setThemed(themed);
663 emit q_ptr->labelFontChanged();
702 emit q_ptr->labelFontChanged();
664 }
703 }
665 }
704 }
666
705
667 void QPieSlicePrivate::setPercentage(qreal percentage)
706 void QPieSlicePrivate::setPercentage(qreal percentage)
668 {
707 {
669 if (!qFuzzyIsNull(m_data.m_percentage - percentage)) {
708 if (!qFuzzyIsNull(m_data.m_percentage - percentage)) {
670 m_data.m_percentage = percentage;
709 m_data.m_percentage = percentage;
671 emit q_ptr->percentageChanged();
710 emit q_ptr->percentageChanged();
672 }
711 }
673 }
712 }
674
713
675 void QPieSlicePrivate::setStartAngle(qreal angle)
714 void QPieSlicePrivate::setStartAngle(qreal angle)
676 {
715 {
677 if (!qFuzzyIsNull(m_data.m_startAngle - angle)) {
716 if (!qFuzzyIsNull(m_data.m_startAngle - angle)) {
678 m_data.m_startAngle = angle;
717 m_data.m_startAngle = angle;
679 emit q_ptr->startAngleChanged();
718 emit q_ptr->startAngleChanged();
680 }
719 }
681 }
720 }
682
721
683 void QPieSlicePrivate::setAngleSpan(qreal span)
722 void QPieSlicePrivate::setAngleSpan(qreal span)
684 {
723 {
685 if (!qFuzzyIsNull(m_data.m_angleSpan - span)) {
724 if (!qFuzzyIsNull(m_data.m_angleSpan - span)) {
686 m_data.m_angleSpan = span;
725 m_data.m_angleSpan = span;
687 emit q_ptr->angleSpanChanged();
726 emit q_ptr->angleSpanChanged();
688 }
727 }
689 }
728 }
690
729
691 QTCOMMERCIALCHART_END_NAMESPACE
730 QTCOMMERCIALCHART_END_NAMESPACE
692
731
693 QTCOMMERCIALCHART_USE_NAMESPACE
732 QTCOMMERCIALCHART_USE_NAMESPACE
694 #include "moc_qpieslice.cpp"
733 #include "moc_qpieslice.cpp"
695 #include "moc_qpieslice_p.cpp"
734 #include "moc_qpieslice_p.cpp"
@@ -1,137 +1,149
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QPIESLICE_H
21 #ifndef QPIESLICE_H
22 #define QPIESLICE_H
22 #define QPIESLICE_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <QObject>
25 #include <QObject>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28 #include <QFont>
28 #include <QFont>
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 class QPieSlicePrivate;
31 class QPieSlicePrivate;
32 class QPieSeries;
32 class QPieSeries;
33
33
34 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
34 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
35 {
35 {
36 Q_OBJECT
36 Q_OBJECT
37 Q_ENUMS(LabelPosition)
37 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
38 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
38 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
39 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
39 Q_PROPERTY(bool labelVisible READ isLabelVisible WRITE setLabelVisible NOTIFY labelVisibleChanged)
40 Q_PROPERTY(bool labelVisible READ isLabelVisible WRITE setLabelVisible NOTIFY labelVisibleChanged)
41 Q_PROPERTY(LabelPosition labelPosition READ labelPosition WRITE setLabelPosition NOTIFY labelPositionChanged)
40 Q_PROPERTY(bool exploded READ isExploded WRITE setExploded NOTIFY explodedChanged)
42 Q_PROPERTY(bool exploded READ isExploded WRITE setExploded NOTIFY explodedChanged)
41 Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged)
43 Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged)
42 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
44 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
43 Q_PROPERTY(int borderWidth READ borderWidth WRITE setBorderWidth NOTIFY borderWidthChanged)
45 Q_PROPERTY(int borderWidth READ borderWidth WRITE setBorderWidth NOTIFY borderWidthChanged)
44 Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged)
46 Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged)
45 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
47 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
46 Q_PROPERTY(QBrush labelBrush READ labelBrush WRITE setLabelBrush NOTIFY labelBrushChanged)
48 Q_PROPERTY(QBrush labelBrush READ labelBrush WRITE setLabelBrush NOTIFY labelBrushChanged)
47 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
49 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
48 Q_PROPERTY(QFont labelFont READ labelFont WRITE setLabelFont NOTIFY labelFontChanged)
50 Q_PROPERTY(QFont labelFont READ labelFont WRITE setLabelFont NOTIFY labelFontChanged)
49 Q_PROPERTY(qreal labelArmLengthFactor READ labelArmLengthFactor WRITE setLabelArmLengthFactor)
51 Q_PROPERTY(qreal labelArmLengthFactor READ labelArmLengthFactor WRITE setLabelArmLengthFactor)
50 Q_PROPERTY(qreal explodeDistanceFactor READ explodeDistanceFactor WRITE setExplodeDistanceFactor)
52 Q_PROPERTY(qreal explodeDistanceFactor READ explodeDistanceFactor WRITE setExplodeDistanceFactor)
51 Q_PROPERTY(qreal percentage READ percentage NOTIFY percentageChanged)
53 Q_PROPERTY(qreal percentage READ percentage NOTIFY percentageChanged)
52 Q_PROPERTY(qreal startAngle READ startAngle NOTIFY startAngleChanged)
54 Q_PROPERTY(qreal startAngle READ startAngle NOTIFY startAngleChanged)
53 Q_PROPERTY(qreal angleSpan READ angleSpan NOTIFY angleSpanChanged)
55 Q_PROPERTY(qreal angleSpan READ angleSpan NOTIFY angleSpanChanged)
54
56
55 public:
57 public:
58 enum LabelPosition {
59 LabelOutside,
60 LabelInside
61 };
62
63 public:
56 explicit QPieSlice(QObject *parent = 0);
64 explicit QPieSlice(QObject *parent = 0);
57 QPieSlice(QString label, qreal value, QObject *parent = 0);
65 QPieSlice(QString label, qreal value, QObject *parent = 0);
58 virtual ~QPieSlice();
66 virtual ~QPieSlice();
59
67
60 void setLabel(QString label);
68 void setLabel(QString label);
61 QString label() const;
69 QString label() const;
62
70
63 void setValue(qreal value);
71 void setValue(qreal value);
64 qreal value() const;
72 qreal value() const;
65
73
66 void setLabelVisible(bool visible = true);
74 void setLabelVisible(bool visible = true);
67 bool isLabelVisible() const;
75 bool isLabelVisible() const;
68
76
77 LabelPosition labelPosition();
78 void setLabelPosition(LabelPosition position);
79
69 void setExploded(bool exploded = true);
80 void setExploded(bool exploded = true);
70 bool isExploded() const;
81 bool isExploded() const;
71
82
72 void setPen(const QPen &pen);
83 void setPen(const QPen &pen);
73 QPen pen() const;
84 QPen pen() const;
74
85
75 QColor borderColor();
86 QColor borderColor();
76 void setBorderColor(QColor color);
87 void setBorderColor(QColor color);
77
88
78 int borderWidth();
89 int borderWidth();
79 void setBorderWidth(int width);
90 void setBorderWidth(int width);
80
91
81 void setBrush(const QBrush &brush);
92 void setBrush(const QBrush &brush);
82 QBrush brush() const;
93 QBrush brush() const;
83
94
84 QColor color();
95 QColor color();
85 void setColor(QColor color);
96 void setColor(QColor color);
86
97
87 void setLabelBrush(const QBrush &brush);
98 void setLabelBrush(const QBrush &brush);
88 QBrush labelBrush() const;
99 QBrush labelBrush() const;
89
100
90 QColor labelColor();
101 QColor labelColor();
91 void setLabelColor(QColor color);
102 void setLabelColor(QColor color);
92
103
93 void setLabelFont(const QFont &font);
104 void setLabelFont(const QFont &font);
94 QFont labelFont() const;
105 QFont labelFont() const;
95
106
96 void setLabelArmLengthFactor(qreal factor);
107 void setLabelArmLengthFactor(qreal factor);
97 qreal labelArmLengthFactor() const;
108 qreal labelArmLengthFactor() const;
98
109
99 void setExplodeDistanceFactor(qreal factor);
110 void setExplodeDistanceFactor(qreal factor);
100 qreal explodeDistanceFactor() const;
111 qreal explodeDistanceFactor() const;
101
112
102 qreal percentage() const;
113 qreal percentage() const;
103 qreal startAngle() const;
114 qreal startAngle() const;
104 qreal angleSpan() const;
115 qreal angleSpan() const;
105
116
106 QPieSeries *series() const;
117 QPieSeries *series() const;
107
118
108 Q_SIGNALS:
119 Q_SIGNALS:
109 void labelChanged();
120 void labelChanged();
110 void valueChanged();
121 void valueChanged();
111 void labelVisibleChanged();
122 void labelVisibleChanged();
123 void labelPositionChanged();
112 void explodedChanged();
124 void explodedChanged();
113 void penChanged();
125 void penChanged();
114 void brushChanged();
126 void brushChanged();
115 void labelBrushChanged();
127 void labelBrushChanged();
116 void labelFontChanged();
128 void labelFontChanged();
117 void labelArmLengthFactorChanged();
129 void labelArmLengthFactorChanged();
118 void explodeDistanceFactorChanged();
130 void explodeDistanceFactorChanged();
119 void percentageChanged();
131 void percentageChanged();
120 void startAngleChanged();
132 void startAngleChanged();
121 void angleSpanChanged();
133 void angleSpanChanged();
122 void colorChanged();
134 void colorChanged();
123 void borderColorChanged();
135 void borderColorChanged();
124 void borderWidthChanged();
136 void borderWidthChanged();
125 void labelColorChanged();
137 void labelColorChanged();
126 void clicked();
138 void clicked();
127 void hovered(bool state);
139 void hovered(bool state);
128
140
129 private:
141 private:
130 QPieSlicePrivate * const d_ptr;
142 QPieSlicePrivate * const d_ptr;
131 Q_DECLARE_PRIVATE(QPieSlice)
143 Q_DECLARE_PRIVATE(QPieSlice)
132 Q_DISABLE_COPY(QPieSlice)
144 Q_DISABLE_COPY(QPieSlice)
133 };
145 };
134
146
135 QTCOMMERCIALCHART_END_NAMESPACE
147 QTCOMMERCIALCHART_END_NAMESPACE
136
148
137 #endif // QPIESLICE_H
149 #endif // QPIESLICE_H
@@ -1,300 +1,304
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QtTest/QtTest>
21 #include <QtTest/QtTest>
22 #include <tst_definitions.h>
22 #include <tst_definitions.h>
23 #include <qchartview.h>
23 #include <qchartview.h>
24 #include <qchart.h>
24 #include <qchart.h>
25 #include <qpieslice.h>
25 #include <qpieslice.h>
26 #include <qpieseries.h>
26 #include <qpieseries.h>
27
27
28 QTCOMMERCIALCHART_USE_NAMESPACE
28 QTCOMMERCIALCHART_USE_NAMESPACE
29
29
30 class tst_qpieslice : public QObject
30 class tst_qpieslice : public QObject
31 {
31 {
32 Q_OBJECT
32 Q_OBJECT
33
33
34 public slots:
34 public slots:
35 void initTestCase();
35 void initTestCase();
36 void cleanupTestCase();
36 void cleanupTestCase();
37 void init();
37 void init();
38 void cleanup();
38 void cleanup();
39
39
40 private slots:
40 private slots:
41 void construction();
41 void construction();
42 void changedSignals();
42 void changedSignals();
43 void customize();
43 void customize();
44 void mouseClick();
44 void mouseClick();
45 void mouseHover();
45 void mouseHover();
46
46
47 private:
47 private:
48
48
49
49
50 private:
50 private:
51
51
52 };
52 };
53
53
54 void tst_qpieslice::initTestCase()
54 void tst_qpieslice::initTestCase()
55 {
55 {
56 }
56 }
57
57
58 void tst_qpieslice::cleanupTestCase()
58 void tst_qpieslice::cleanupTestCase()
59 {
59 {
60 }
60 }
61
61
62 void tst_qpieslice::init()
62 void tst_qpieslice::init()
63 {
63 {
64
64
65 }
65 }
66
66
67 void tst_qpieslice::cleanup()
67 void tst_qpieslice::cleanup()
68 {
68 {
69
69
70 }
70 }
71
71
72 void tst_qpieslice::construction()
72 void tst_qpieslice::construction()
73 {
73 {
74 // no params
74 // no params
75 QPieSlice slice1;
75 QPieSlice slice1;
76 QCOMPARE(slice1.value(), 0.0);
76 QCOMPARE(slice1.value(), 0.0);
77 QVERIFY(slice1.label().isEmpty());
77 QVERIFY(slice1.label().isEmpty());
78 QVERIFY(!slice1.isLabelVisible());
78 QVERIFY(!slice1.isLabelVisible());
79 QVERIFY(!slice1.isExploded());
79 QVERIFY(!slice1.isExploded());
80 QCOMPARE(slice1.pen(), QPen());
80 QCOMPARE(slice1.pen(), QPen());
81 QCOMPARE(slice1.brush(), QBrush());
81 QCOMPARE(slice1.brush(), QBrush());
82 QCOMPARE(slice1.labelBrush(), QBrush());
82 QCOMPARE(slice1.labelBrush(), QBrush());
83 QCOMPARE(slice1.labelFont(), QFont());
83 QCOMPARE(slice1.labelFont(), QFont());
84 QCOMPARE(slice1.labelArmLengthFactor(), 0.15); // default value
84 QCOMPARE(slice1.labelArmLengthFactor(), 0.15); // default value
85 QCOMPARE(slice1.explodeDistanceFactor(), 0.15); // default value
85 QCOMPARE(slice1.explodeDistanceFactor(), 0.15); // default value
86 QCOMPARE(slice1.percentage(), 0.0);
86 QCOMPARE(slice1.percentage(), 0.0);
87 QCOMPARE(slice1.startAngle(), 0.0);
87 QCOMPARE(slice1.startAngle(), 0.0);
88 QCOMPARE(slice1.angleSpan(), 0.0);
88 QCOMPARE(slice1.angleSpan(), 0.0);
89
89
90 // value and label params
90 // value and label params
91 QPieSlice slice2("foobar", 1.0);
91 QPieSlice slice2("foobar", 1.0);
92 QCOMPARE(slice2.value(), 1.0);
92 QCOMPARE(slice2.value(), 1.0);
93 QCOMPARE(slice2.label(), QString("foobar"));
93 QCOMPARE(slice2.label(), QString("foobar"));
94 QVERIFY(!slice2.isLabelVisible());
94 QVERIFY(!slice2.isLabelVisible());
95 QVERIFY(!slice2.isExploded());
95 QVERIFY(!slice2.isExploded());
96 QCOMPARE(slice2.pen(), QPen());
96 QCOMPARE(slice2.pen(), QPen());
97 QCOMPARE(slice2.brush(), QBrush());
97 QCOMPARE(slice2.brush(), QBrush());
98 QCOMPARE(slice2.labelBrush(), QBrush());
98 QCOMPARE(slice2.labelBrush(), QBrush());
99 QCOMPARE(slice2.labelFont(), QFont());
99 QCOMPARE(slice2.labelFont(), QFont());
100 QCOMPARE(slice2.labelArmLengthFactor(), 0.15); // default value
100 QCOMPARE(slice2.labelArmLengthFactor(), 0.15); // default value
101 QCOMPARE(slice2.explodeDistanceFactor(), 0.15); // default value
101 QCOMPARE(slice2.explodeDistanceFactor(), 0.15); // default value
102 QCOMPARE(slice2.percentage(), 0.0);
102 QCOMPARE(slice2.percentage(), 0.0);
103 QCOMPARE(slice2.startAngle(), 0.0);
103 QCOMPARE(slice2.startAngle(), 0.0);
104 QCOMPARE(slice2.angleSpan(), 0.0);
104 QCOMPARE(slice2.angleSpan(), 0.0);
105 }
105 }
106
106
107 void tst_qpieslice::changedSignals()
107 void tst_qpieslice::changedSignals()
108 {
108 {
109 QPieSlice slice;
109 QPieSlice slice;
110
110
111 QSignalSpy valueSpy(&slice, SIGNAL(valueChanged()));
111 QSignalSpy valueSpy(&slice, SIGNAL(valueChanged()));
112 QSignalSpy labelSpy(&slice, SIGNAL(labelChanged()));
112 QSignalSpy labelSpy(&slice, SIGNAL(labelChanged()));
113 QSignalSpy explodedSpy(&slice, SIGNAL(explodedChanged()));
113 QSignalSpy explodedSpy(&slice, SIGNAL(explodedChanged()));
114 QSignalSpy penSpy(&slice, SIGNAL(penChanged()));
114 QSignalSpy penSpy(&slice, SIGNAL(penChanged()));
115 QSignalSpy brushSpy(&slice, SIGNAL(brushChanged()));
115 QSignalSpy brushSpy(&slice, SIGNAL(brushChanged()));
116 QSignalSpy labelBrushSpy(&slice, SIGNAL(labelBrushChanged()));
116 QSignalSpy labelBrushSpy(&slice, SIGNAL(labelBrushChanged()));
117 QSignalSpy labelFontSpy(&slice, SIGNAL(labelFontChanged()));
117 QSignalSpy labelFontSpy(&slice, SIGNAL(labelFontChanged()));
118 QSignalSpy labelPositionSpy(&slice, SIGNAL(labelPositionChanged()));
118 QSignalSpy labelArmLengthFactorSpy(&slice, SIGNAL(labelArmLengthFactorChanged()));
119 QSignalSpy labelArmLengthFactorSpy(&slice, SIGNAL(labelArmLengthFactorChanged()));
119 QSignalSpy explodeDistanceFactorSpy(&slice, SIGNAL(explodeDistanceFactorChanged()));
120 QSignalSpy explodeDistanceFactorSpy(&slice, SIGNAL(explodeDistanceFactorChanged()));
120 QSignalSpy colorSpy(&slice, SIGNAL(colorChanged()));
121 QSignalSpy colorSpy(&slice, SIGNAL(colorChanged()));
121 QSignalSpy borderColorSpy(&slice, SIGNAL(borderColorChanged()));
122 QSignalSpy borderColorSpy(&slice, SIGNAL(borderColorChanged()));
122 QSignalSpy borderWidthSpy(&slice, SIGNAL(borderWidthChanged()));
123 QSignalSpy borderWidthSpy(&slice, SIGNAL(borderWidthChanged()));
123 QSignalSpy labelColorSpy(&slice, SIGNAL(labelColorChanged()));
124 QSignalSpy labelColorSpy(&slice, SIGNAL(labelColorChanged()));
124
125
125 // percentageChanged(), startAngleChanged() and angleSpanChanged() signals tested at tst_qpieseries::calculatedValues()
126 // percentageChanged(), startAngleChanged() and angleSpanChanged() signals tested at tst_qpieseries::calculatedValues()
126
127
127 // set everything twice to see we do not get unnecessary signals
128 // set everything twice to see we do not get unnecessary signals
128 slice.setValue(1.0);
129 slice.setValue(1.0);
129 slice.setValue(-1.0);
130 slice.setValue(-1.0);
130 QCOMPARE(slice.value(), 1.0);
131 QCOMPARE(slice.value(), 1.0);
131 slice.setLabel("foobar");
132 slice.setLabel("foobar");
132 slice.setLabel("foobar");
133 slice.setLabel("foobar");
133 slice.setLabelVisible();
134 slice.setLabelVisible();
134 slice.setLabelVisible();
135 slice.setLabelVisible();
135 slice.setExploded();
136 slice.setExploded();
136 slice.setExploded();
137 slice.setExploded();
137 slice.setPen(QPen(Qt::red));
138 slice.setPen(QPen(Qt::red));
138 slice.setPen(QPen(QBrush(Qt::red), 3));
139 slice.setPen(QPen(QBrush(Qt::red), 3));
139 slice.setBrush(QBrush(Qt::red));
140 slice.setBrush(QBrush(Qt::red));
140 slice.setBrush(QBrush(Qt::red));
141 slice.setBrush(QBrush(Qt::red));
141 slice.setLabelBrush(QBrush(Qt::green));
142 slice.setLabelBrush(QBrush(Qt::green));
142 slice.setLabelBrush(QBrush(Qt::green));
143 slice.setLabelBrush(QBrush(Qt::green));
143 slice.setLabelFont(QFont("Tahoma"));
144 slice.setLabelFont(QFont("Tahoma"));
144 slice.setLabelFont(QFont("Tahoma"));
145 slice.setLabelFont(QFont("Tahoma"));
146 slice.setLabelPosition(QPieSlice::LabelInside);
147 slice.setLabelPosition(QPieSlice::LabelInside);
145 slice.setLabelArmLengthFactor(0.1);
148 slice.setLabelArmLengthFactor(0.1);
146 slice.setLabelArmLengthFactor(0.1);
149 slice.setLabelArmLengthFactor(0.1);
147 slice.setExplodeDistanceFactor(0.1);
150 slice.setExplodeDistanceFactor(0.1);
148 slice.setExplodeDistanceFactor(0.1);
151 slice.setExplodeDistanceFactor(0.1);
149
152
150 TRY_COMPARE(valueSpy.count(), 1);
153 TRY_COMPARE(valueSpy.count(), 1);
151 TRY_COMPARE(labelSpy.count(), 1);
154 TRY_COMPARE(labelSpy.count(), 1);
152 TRY_COMPARE(explodedSpy.count(), 1);
155 TRY_COMPARE(explodedSpy.count(), 1);
153 TRY_COMPARE(penSpy.count(), 2);
156 TRY_COMPARE(penSpy.count(), 2);
154 TRY_COMPARE(brushSpy.count(), 1);
157 TRY_COMPARE(brushSpy.count(), 1);
155 TRY_COMPARE(labelBrushSpy.count(), 1);
158 TRY_COMPARE(labelBrushSpy.count(), 1);
156 TRY_COMPARE(labelFontSpy.count(), 1);
159 TRY_COMPARE(labelFontSpy.count(), 1);
160 TRY_COMPARE(labelPositionSpy.count(), 1);
157 TRY_COMPARE(labelArmLengthFactorSpy.count(), 1);
161 TRY_COMPARE(labelArmLengthFactorSpy.count(), 1);
158 TRY_COMPARE(explodeDistanceFactorSpy.count(), 1);
162 TRY_COMPARE(explodeDistanceFactorSpy.count(), 1);
159 TRY_COMPARE(colorSpy.count(), 1);
163 TRY_COMPARE(colorSpy.count(), 1);
160 TRY_COMPARE(borderColorSpy.count(), 1);
164 TRY_COMPARE(borderColorSpy.count(), 1);
161 TRY_COMPARE(borderWidthSpy.count(), 1);
165 TRY_COMPARE(borderWidthSpy.count(), 1);
162 TRY_COMPARE(labelColorSpy.count(), 1);
166 TRY_COMPARE(labelColorSpy.count(), 1);
163 }
167 }
164
168
165 void tst_qpieslice::customize()
169 void tst_qpieslice::customize()
166 {
170 {
167 // create a pie series
171 // create a pie series
168 QPieSeries *series = new QPieSeries();
172 QPieSeries *series = new QPieSeries();
169 QPieSlice *s1 = series->append("slice 1", 1);
173 QPieSlice *s1 = series->append("slice 1", 1);
170 QPieSlice *s2 = series->append("slice 2", 2);
174 QPieSlice *s2 = series->append("slice 2", 2);
171 series->append("slice 3", 3);
175 series->append("slice 3", 3);
172
176
173 // customize a slice
177 // customize a slice
174 QPen p1(Qt::red);
178 QPen p1(Qt::red);
175 s1->setPen(p1);
179 s1->setPen(p1);
176 QBrush b1(Qt::red);
180 QBrush b1(Qt::red);
177 s1->setBrush(b1);
181 s1->setBrush(b1);
178 s1->setLabelBrush(b1);
182 s1->setLabelBrush(b1);
179 QFont f1("Consolas");
183 QFont f1("Consolas");
180 s1->setLabelFont(f1);
184 s1->setLabelFont(f1);
181
185
182 // add series to the chart
186 // add series to the chart
183 QChartView view(new QChart());
187 QChartView view(new QChart());
184 view.resize(200, 200);
188 view.resize(200, 200);
185 view.chart()->addSeries(series);
189 view.chart()->addSeries(series);
186 view.show();
190 view.show();
187 QTest::qWaitForWindowShown(&view);
191 QTest::qWaitForWindowShown(&view);
188 //QTest::qWait(1000);
192 //QTest::qWait(1000);
189
193
190 // check that customizations persist
194 // check that customizations persist
191 QCOMPARE(s1->pen(), p1);
195 QCOMPARE(s1->pen(), p1);
192 QCOMPARE(s1->brush(), b1);
196 QCOMPARE(s1->brush(), b1);
193 QCOMPARE(s1->labelBrush(), b1);
197 QCOMPARE(s1->labelBrush(), b1);
194 QCOMPARE(s1->labelFont(), f1);
198 QCOMPARE(s1->labelFont(), f1);
195
199
196 // remove a slice
200 // remove a slice
197 series->remove(s2);
201 series->remove(s2);
198 QCOMPARE(s1->pen(), p1);
202 QCOMPARE(s1->pen(), p1);
199 QCOMPARE(s1->brush(), b1);
203 QCOMPARE(s1->brush(), b1);
200 QCOMPARE(s1->labelBrush(), b1);
204 QCOMPARE(s1->labelBrush(), b1);
201 QCOMPARE(s1->labelFont(), f1);
205 QCOMPARE(s1->labelFont(), f1);
202
206
203 // add a slice
207 // add a slice
204 series->append("slice 4", 4);
208 series->append("slice 4", 4);
205 QCOMPARE(s1->pen(), p1);
209 QCOMPARE(s1->pen(), p1);
206 QCOMPARE(s1->brush(), b1);
210 QCOMPARE(s1->brush(), b1);
207 QCOMPARE(s1->labelBrush(), b1);
211 QCOMPARE(s1->labelBrush(), b1);
208 QCOMPARE(s1->labelFont(), f1);
212 QCOMPARE(s1->labelFont(), f1);
209
213
210 // insert a slice
214 // insert a slice
211 series->insert(0, new QPieSlice("slice 5", 5));
215 series->insert(0, new QPieSlice("slice 5", 5));
212 QCOMPARE(s1->pen(), p1);
216 QCOMPARE(s1->pen(), p1);
213 QCOMPARE(s1->brush(), b1);
217 QCOMPARE(s1->brush(), b1);
214 QCOMPARE(s1->labelBrush(), b1);
218 QCOMPARE(s1->labelBrush(), b1);
215 QCOMPARE(s1->labelFont(), f1);
219 QCOMPARE(s1->labelFont(), f1);
216
220
217 // change theme
221 // change theme
218 // theme will overwrite customizations
222 // theme will overwrite customizations
219 view.chart()->setTheme(QChart::ChartThemeHighContrast);
223 view.chart()->setTheme(QChart::ChartThemeHighContrast);
220 QVERIFY(s1->pen() != p1);
224 QVERIFY(s1->pen() != p1);
221 QVERIFY(s1->brush() != b1);
225 QVERIFY(s1->brush() != b1);
222 QVERIFY(s1->labelBrush() != b1);
226 QVERIFY(s1->labelBrush() != b1);
223 QVERIFY(s1->labelFont() != f1);
227 QVERIFY(s1->labelFont() != f1);
224 }
228 }
225
229
226 void tst_qpieslice::mouseClick()
230 void tst_qpieslice::mouseClick()
227 {
231 {
228 // create a pie series
232 // create a pie series
229 QPieSeries *series = new QPieSeries();
233 QPieSeries *series = new QPieSeries();
230 series->setPieSize(1.0);
234 series->setPieSize(1.0);
231 QPieSlice *s1 = series->append("slice 1", 1);
235 QPieSlice *s1 = series->append("slice 1", 1);
232 QPieSlice *s2 = series->append("slice 2", 2);
236 QPieSlice *s2 = series->append("slice 2", 2);
233 QPieSlice *s3 = series->append("slice 3", 3);
237 QPieSlice *s3 = series->append("slice 3", 3);
234 QSignalSpy clickSpy1(s1, SIGNAL(clicked()));
238 QSignalSpy clickSpy1(s1, SIGNAL(clicked()));
235 QSignalSpy clickSpy2(s2, SIGNAL(clicked()));
239 QSignalSpy clickSpy2(s2, SIGNAL(clicked()));
236 QSignalSpy clickSpy3(s3, SIGNAL(clicked()));
240 QSignalSpy clickSpy3(s3, SIGNAL(clicked()));
237
241
238 // add series to the chart
242 // add series to the chart
239 QChartView view(new QChart());
243 QChartView view(new QChart());
240 view.chart()->legend()->setVisible(false);
244 view.chart()->legend()->setVisible(false);
241 view.resize(200, 200);
245 view.resize(200, 200);
242 view.chart()->addSeries(series);
246 view.chart()->addSeries(series);
243 view.show();
247 view.show();
244 QTest::qWaitForWindowShown(&view);
248 QTest::qWaitForWindowShown(&view);
245
249
246 // simulate clicks
250 // simulate clicks
247 // pie rectangle: QRectF(60,60 121x121)
251 // pie rectangle: QRectF(60,60 121x121)
248 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1
252 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1
249 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 136)); // inside slice 2
253 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 136)); // inside slice 2
250 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(91, 119)); // inside slice 3
254 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(91, 119)); // inside slice 3
251 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(70, 70)); // inside pie rectangle but not inside a slice
255 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(70, 70)); // inside pie rectangle but not inside a slice
252 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(170, 170)); // inside pie rectangle but not inside a slice
256 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(170, 170)); // inside pie rectangle but not inside a slice
253 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
257 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
254 QCOMPARE(clickSpy1.count(), 1);
258 QCOMPARE(clickSpy1.count(), 1);
255 QCOMPARE(clickSpy2.count(), 1);
259 QCOMPARE(clickSpy2.count(), 1);
256 QCOMPARE(clickSpy3.count(), 1);
260 QCOMPARE(clickSpy3.count(), 1);
257 }
261 }
258
262
259 void tst_qpieslice::mouseHover()
263 void tst_qpieslice::mouseHover()
260 {
264 {
261 // create a pie series
265 // create a pie series
262 QPieSeries *series = new QPieSeries();
266 QPieSeries *series = new QPieSeries();
263 series->setPieSize(1.0);
267 series->setPieSize(1.0);
264 QPieSlice *s1 = series->append("slice 1", 1);
268 QPieSlice *s1 = series->append("slice 1", 1);
265 series->append("slice 2", 2);
269 series->append("slice 2", 2);
266 series->append("slice 3", 3);
270 series->append("slice 3", 3);
267
271
268 // add series to the chart
272 // add series to the chart
269 QChartView view(new QChart());
273 QChartView view(new QChart());
270 view.chart()->legend()->setVisible(false);
274 view.chart()->legend()->setVisible(false);
271 view.resize(200, 200);
275 view.resize(200, 200);
272 view.chart()->addSeries(series);
276 view.chart()->addSeries(series);
273 view.show();
277 view.show();
274 QTest::qWaitForWindowShown(&view);
278 QTest::qWaitForWindowShown(&view);
275
279
276 // first move to right top corner
280 // first move to right top corner
277 QTest::mouseMove(view.viewport(), QPoint(200, 0));
281 QTest::mouseMove(view.viewport(), QPoint(200, 0));
278 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
282 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
279
283
280 // move inside slice rectangle but NOT the actual slice
284 // move inside slice rectangle but NOT the actual slice
281 // pie rectangle: QRectF(60,60 121x121)
285 // pie rectangle: QRectF(60,60 121x121)
282 QSignalSpy hoverSpy(s1, SIGNAL(hovered(bool)));
286 QSignalSpy hoverSpy(s1, SIGNAL(hovered(bool)));
283 QTest::mouseMove(view.viewport(), QPoint(170, 70));
287 QTest::mouseMove(view.viewport(), QPoint(170, 70));
284 TRY_COMPARE(hoverSpy.count(), 0);
288 TRY_COMPARE(hoverSpy.count(), 0);
285
289
286 // move inside the slice
290 // move inside the slice
287 QTest::mouseMove(view.viewport(), QPoint(139, 85));
291 QTest::mouseMove(view.viewport(), QPoint(139, 85));
288 TRY_COMPARE(hoverSpy.count(), 1);
292 TRY_COMPARE(hoverSpy.count(), 1);
289 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(0)), true);
293 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(0)), true);
290
294
291 // move outside the slice
295 // move outside the slice
292 QTest::mouseMove(view.viewport(), QPoint(200, 0));
296 QTest::mouseMove(view.viewport(), QPoint(200, 0));
293 TRY_COMPARE(hoverSpy.count(), 2);
297 TRY_COMPARE(hoverSpy.count(), 2);
294 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(0)), false);
298 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(0)), false);
295 }
299 }
296
300
297 QTEST_MAIN(tst_qpieslice)
301 QTEST_MAIN(tst_qpieslice)
298
302
299 #include "tst_qpieslice.moc"
303 #include "tst_qpieslice.moc"
300
304
@@ -1,157 +1,166
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 import QtQuick 1.0
21 import QtQuick 1.0
22 import QtCommercial.Chart 1.0
22 import QtCommercial.Chart 1.0
23
23
24 Flow {
24 Flow {
25 id: flow
25 id: flow
26 spacing: 5
26 spacing: 5
27 flow: Flow.TopToBottom
27 flow: Flow.TopToBottom
28 property variant series
28 property variant series
29
29
30 onSeriesChanged: {
30 onSeriesChanged: {
31 seriesConnections.target = series;
31 seriesConnections.target = series;
32 sliceConnections.target = series.at(0);
32 sliceConnections.target = series.at(0);
33 }
33 }
34
34
35 Connections {
35 Connections {
36 id: seriesConnections
36 id: seriesConnections
37 ignoreUnknownSignals: true
37 ignoreUnknownSignals: true
38 onVisibleChanged: console.log("series.onVisibleChanged: " + series.visible);
38 onVisibleChanged: console.log("series.onVisibleChanged: " + series.visible);
39 onHorizontalPositionChanged:console.log("series.onHorizontalPositionChanged: " + series.horizontalPosition);
39 onHorizontalPositionChanged:console.log("series.onHorizontalPositionChanged: " + series.horizontalPosition);
40 onVerticalPositionChanged: console.log("series.onVerticalPositionChanged: " + series.verticalPosition);
40 onVerticalPositionChanged: console.log("series.onVerticalPositionChanged: " + series.verticalPosition);
41 onSizeChanged: console.log("series.onSizeChanged: " + series.size);
41 onSizeChanged: console.log("series.onSizeChanged: " + series.size);
42 onStartAngleChanged: console.log("series.onStartAngleChanged: " + series.startAngle);
42 onStartAngleChanged: console.log("series.onStartAngleChanged: " + series.startAngle);
43 onEndAngleChanged: console.log("series.onEndAngleChanged: " + series.endAngle);
43 onEndAngleChanged: console.log("series.onEndAngleChanged: " + series.endAngle);
44 onCountChanged: console.log("series.onCountChanged: " + series.count);
44 onCountChanged: console.log("series.onCountChanged: " + series.count);
45 onSumChanged: console.log("series.onSumChanged: " + series.sum);
45 onSumChanged: console.log("series.onSumChanged: " + series.sum);
46 }
46 }
47
47
48 Connections {
48 Connections {
49 id: sliceConnections
49 id: sliceConnections
50 ignoreUnknownSignals: true
50 ignoreUnknownSignals: true
51 onValueChanged: console.log("slice.onValueChanged: " + series.at(0).value);
51 onValueChanged: console.log("slice.onValueChanged: " + series.at(0).value);
52 onLabelVisibleChanged: console.log("slice.onLabelVisibleChanged: " + series.at(0).labelVisible);
52 onLabelVisibleChanged: console.log("slice.onLabelVisibleChanged: " + series.at(0).labelVisible);
53 onLabelPositionChanged: console.log("slice.onLabelPositionChanged: " + series.at(0).labelPosition);
53 onExplodedChanged: console.log("slice.onExplodedChanged: " + series.at(0).exploded);
54 onExplodedChanged: console.log("slice.onExplodedChanged: " + series.at(0).exploded);
54 onPenChanged: console.log("slice.onPenChanged: " + series.at(0).pen);
55 onPenChanged: console.log("slice.onPenChanged: " + series.at(0).pen);
55 onBorderColorChanged: console.log("slice.onBorderColorChanged: " + series.at(0).borderColor);
56 onBorderColorChanged: console.log("slice.onBorderColorChanged: " + series.at(0).borderColor);
56 onBorderWidthChanged: console.log("slice.onBorderWidthChanged: " + series.at(0).borderWidth);
57 onBorderWidthChanged: console.log("slice.onBorderWidthChanged: " + series.at(0).borderWidth);
57 onBrushChanged: console.log("slice.onBrushChanged: " + series.at(0).brush);
58 onBrushChanged: console.log("slice.onBrushChanged: " + series.at(0).brush);
58 onColorChanged: console.log("slice.onColorChanged: " + series.at(0).color);
59 onColorChanged: console.log("slice.onColorChanged: " + series.at(0).color);
59 onLabelColorChanged: console.log("slice.onLabelColorChanged: " + series.at(0).labelColor);
60 onLabelColorChanged: console.log("slice.onLabelColorChanged: " + series.at(0).labelColor);
60 onLabelBrushChanged: console.log("slice.onLabelBrushChanged: " + series.at(0).labelBrush);
61 onLabelBrushChanged: console.log("slice.onLabelBrushChanged: " + series.at(0).labelBrush);
61 onLabelFontChanged: console.log("slice.onLabelFontChanged: " + series.at(0).labelFont);
62 onLabelFontChanged: console.log("slice.onLabelFontChanged: " + series.at(0).labelFont);
62 onLabelArmLengthFactorChanged: console.log("slice.onLabelArmLengthFactorChanged: " + series.at(0).labelArmLengthFactor);
63 onLabelArmLengthFactorChanged: console.log("slice.onLabelArmLengthFactorChanged: " + series.at(0).labelArmLengthFactor);
63 onExplodeDistanceFactorChanged: console.log("slice.onExplodeDistanceFactorChanged: " + series.at(0).explodeDistanceFactor);
64 onExplodeDistanceFactorChanged: console.log("slice.onExplodeDistanceFactorChanged: " + series.at(0).explodeDistanceFactor);
64 onPercentageChanged: console.log("slice.onPercentageChanged: " + series.at(0).percentage);
65 onPercentageChanged: console.log("slice.onPercentageChanged: " + series.at(0).percentage);
65 onStartAngleChanged: console.log("slice.onStartAngleChanged: " + series.at(0).startAngle);
66 onStartAngleChanged: console.log("slice.onStartAngleChanged: " + series.at(0).startAngle);
66 onAngleSpanChanged: console.log("slice.onAngleSpanChanged: " + series.at(0).angleSpan);
67 onAngleSpanChanged: console.log("slice.onAngleSpanChanged: " + series.at(0).angleSpan);
67 }
68 }
68
69
69 Button {
70 Button {
70 text: "visible"
71 text: "visible"
71 onClicked: series.visible = !series.visible;
72 onClicked: series.visible = !series.visible;
72 }
73 }
73 Button {
74 Button {
74 text: "series hpos +"
75 text: "series hpos +"
75 onClicked: series.horizontalPosition += 0.1;
76 onClicked: series.horizontalPosition += 0.1;
76 }
77 }
77 Button {
78 Button {
78 text: "series hpos -"
79 text: "series hpos -"
79 onClicked: series.horizontalPosition -= 0.1;
80 onClicked: series.horizontalPosition -= 0.1;
80 }
81 }
81 Button {
82 Button {
82 text: "series vpos +"
83 text: "series vpos +"
83 onClicked: series.verticalPosition += 0.1;
84 onClicked: series.verticalPosition += 0.1;
84 }
85 }
85 Button {
86 Button {
86 text: "series vpos -"
87 text: "series vpos -"
87 onClicked: series.verticalPosition -= 0.1;
88 onClicked: series.verticalPosition -= 0.1;
88 }
89 }
89 Button {
90 Button {
90 text: "series size +"
91 text: "series size +"
91 onClicked: series.size += 0.1;
92 onClicked: series.size += 0.1;
92 }
93 }
93 Button {
94 Button {
94 text: "series size -"
95 text: "series size -"
95 onClicked: series.size -= 0.1;
96 onClicked: series.size -= 0.1;
96 }
97 }
97 Button {
98 Button {
98 text: "series start angle +"
99 text: "series start angle +"
99 onClicked: series.startAngle += 1.1;
100 onClicked: series.startAngle += 1.1;
100 }
101 }
101 Button {
102 Button {
102 text: "series start angle -"
103 text: "series start angle -"
103 onClicked: series.startAngle -= 1.1;
104 onClicked: series.startAngle -= 1.1;
104 }
105 }
105 Button {
106 Button {
106 text: "series end angle +"
107 text: "series end angle +"
107 onClicked: series.endAngle += 1.1;
108 onClicked: series.endAngle += 1.1;
108 }
109 }
109 Button {
110 Button {
110 text: "series end angle -"
111 text: "series end angle -"
111 onClicked: series.endAngle -= 1.1;
112 onClicked: series.endAngle -= 1.1;
112 }
113 }
113 Button {
114 Button {
114 text: "slice color"
115 text: "slice color"
115 onClicked: series.at(0).color = main.nextColor();
116 onClicked: series.at(0).color = main.nextColor();
116 }
117 }
117 Button {
118 Button {
118 text: "slice border color"
119 text: "slice border color"
119 onClicked: series.at(0).borderColor = main.nextColor();
120 onClicked: series.at(0).borderColor = main.nextColor();
120 }
121 }
121 Button {
122 Button {
122 text: "slice border width +"
123 text: "slice border width +"
123 onClicked: series.at(0).borderWidth++;
124 onClicked: series.at(0).borderWidth++;
124 }
125 }
125 Button {
126 Button {
126 text: "slice border width -"
127 text: "slice border width -"
127 onClicked: series.at(0).borderWidth--;
128 onClicked: series.at(0).borderWidth--;
128 }
129 }
129 Button {
130 Button {
130 text: "slice label visible"
131 text: "slice label visible"
131 onClicked: series.at(0).labelVisible = !series.at(0).labelVisible;
132 onClicked: series.at(0).labelVisible = !series.at(0).labelVisible;
132 }
133 }
133 Button {
134 Button {
135 text: "slice label position inside"
136 onClicked: series.at(0).labelPosition = PieSlice.LabelInside;
137 }
138 Button {
139 text: "slice label position outside"
140 onClicked: series.at(0).labelPosition = PieSlice.LabelOutside;
141 }
142 Button {
134 text: "slice label arm len +"
143 text: "slice label arm len +"
135 onClicked: series.at(0).labelArmLengthFactor += 0.1;
144 onClicked: series.at(0).labelArmLengthFactor += 0.1;
136 }
145 }
137 Button {
146 Button {
138 text: "slice label arm len -"
147 text: "slice label arm len -"
139 onClicked: series.at(0).labelArmLengthFactor -= 0.1;
148 onClicked: series.at(0).labelArmLengthFactor -= 0.1;
140 }
149 }
141 Button {
150 Button {
142 text: "slice label color"
151 text: "slice label color"
143 onClicked: series.at(0).labelColor = main.nextColor();
152 onClicked: series.at(0).labelColor = main.nextColor();
144 }
153 }
145 Button {
154 Button {
146 text: "slice exploded"
155 text: "slice exploded"
147 onClicked: series.at(0).exploded = !series.at(0).exploded;
156 onClicked: series.at(0).exploded = !series.at(0).exploded;
148 }
157 }
149 Button {
158 Button {
150 text: "slice explode dist +"
159 text: "slice explode dist +"
151 onClicked: series.at(0).explodeDistanceFactor += 0.1;
160 onClicked: series.at(0).explodeDistanceFactor += 0.1;
152 }
161 }
153 Button {
162 Button {
154 text: "slice explode dist -"
163 text: "slice explode dist -"
155 onClicked: series.at(0).explodeDistanceFactor -= 0.1;
164 onClicked: series.at(0).explodeDistanceFactor -= 0.1;
156 }
165 }
157 }
166 }
General Comments 0
You need to be logged in to leave comments. Login now