##// END OF EJS Templates
pie: change the order of parameters when creating slices to be more intuitive
Jani Honkonen -
r1206:080b090a9a03
parent child
Show More
@@ -1,376 +1,376
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 "themewidget.h"
21 #include "themewidget.h"
22
22
23 #include <QChartView>
23 #include <QChartView>
24 #include <QPieSeries>
24 #include <QPieSeries>
25 #include <QPieSlice>
25 #include <QPieSlice>
26 #include <QBarSeries>
26 #include <QBarSeries>
27 #include <QPercentBarSeries>
27 #include <QPercentBarSeries>
28 #include <QStackedBarSeries>
28 #include <QStackedBarSeries>
29 #include <QBarSet>
29 #include <QBarSet>
30 #include <QLineSeries>
30 #include <QLineSeries>
31 #include <QSplineSeries>
31 #include <QSplineSeries>
32 #include <QScatterSeries>
32 #include <QScatterSeries>
33 #include <QAreaSeries>
33 #include <QAreaSeries>
34 #include <QLegend>
34 #include <QLegend>
35 #include <QGridLayout>
35 #include <QGridLayout>
36 #include <QFormLayout>
36 #include <QFormLayout>
37 #include <QComboBox>
37 #include <QComboBox>
38 #include <QSpinBox>
38 #include <QSpinBox>
39 #include <QCheckBox>
39 #include <QCheckBox>
40 #include <QGroupBox>
40 #include <QGroupBox>
41 #include <QLabel>
41 #include <QLabel>
42 #include <QTime>
42 #include <QTime>
43
43
44 ThemeWidget::ThemeWidget(QWidget* parent) :
44 ThemeWidget::ThemeWidget(QWidget* parent) :
45 QWidget(parent),
45 QWidget(parent),
46 m_listCount(3),
46 m_listCount(3),
47 m_valueMax(10),
47 m_valueMax(10),
48 m_valueCount(7),
48 m_valueCount(7),
49 m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)),
49 m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)),
50 m_themeComboBox(createThemeBox()),
50 m_themeComboBox(createThemeBox()),
51 m_antialiasCheckBox(new QCheckBox("Anti-aliasing")),
51 m_antialiasCheckBox(new QCheckBox("Anti-aliasing")),
52 m_animatedComboBox(createAnimationBox()),
52 m_animatedComboBox(createAnimationBox()),
53 m_legendComboBox(createLegendBox())
53 m_legendComboBox(createLegendBox())
54 {
54 {
55 connectSignals();
55 connectSignals();
56 // create layout
56 // create layout
57 QGridLayout* baseLayout = new QGridLayout();
57 QGridLayout* baseLayout = new QGridLayout();
58 QHBoxLayout *settingsLayout = new QHBoxLayout();
58 QHBoxLayout *settingsLayout = new QHBoxLayout();
59 settingsLayout->addWidget(new QLabel("Theme:"));
59 settingsLayout->addWidget(new QLabel("Theme:"));
60 settingsLayout->addWidget(m_themeComboBox);
60 settingsLayout->addWidget(m_themeComboBox);
61 settingsLayout->addWidget(new QLabel("Animation:"));
61 settingsLayout->addWidget(new QLabel("Animation:"));
62 settingsLayout->addWidget(m_animatedComboBox);
62 settingsLayout->addWidget(m_animatedComboBox);
63 settingsLayout->addWidget(new QLabel("Legend:"));
63 settingsLayout->addWidget(new QLabel("Legend:"));
64 settingsLayout->addWidget(m_legendComboBox);
64 settingsLayout->addWidget(m_legendComboBox);
65 settingsLayout->addWidget(m_antialiasCheckBox);
65 settingsLayout->addWidget(m_antialiasCheckBox);
66 settingsLayout->addStretch();
66 settingsLayout->addStretch();
67 baseLayout->addLayout(settingsLayout, 0, 0, 1, 3);
67 baseLayout->addLayout(settingsLayout, 0, 0, 1, 3);
68
68
69 //create charts
69 //create charts
70
70
71 QChartView *chartView;
71 QChartView *chartView;
72
72
73 chartView = new QChartView(createAreaChart());
73 chartView = new QChartView(createAreaChart());
74 baseLayout->addWidget(chartView, 1, 0);
74 baseLayout->addWidget(chartView, 1, 0);
75 m_charts << chartView;
75 m_charts << chartView;
76
76
77 chartView = new QChartView(createBarChart(m_valueCount));
77 chartView = new QChartView(createBarChart(m_valueCount));
78 baseLayout->addWidget(chartView, 1, 1);
78 baseLayout->addWidget(chartView, 1, 1);
79 m_charts << chartView;
79 m_charts << chartView;
80
80
81 chartView = new QChartView(createLineChart());
81 chartView = new QChartView(createLineChart());
82 baseLayout->addWidget(chartView, 1, 2);
82 baseLayout->addWidget(chartView, 1, 2);
83 m_charts << chartView;
83 m_charts << chartView;
84
84
85 chartView = new QChartView(createPieChart());
85 chartView = new QChartView(createPieChart());
86 chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen...
86 chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen...
87 baseLayout->addWidget(chartView, 2, 0);
87 baseLayout->addWidget(chartView, 2, 0);
88 m_charts << chartView;
88 m_charts << chartView;
89
89
90 chartView = new QChartView(createSplineChart());
90 chartView = new QChartView(createSplineChart());
91 baseLayout->addWidget(chartView, 2, 1);
91 baseLayout->addWidget(chartView, 2, 1);
92 m_charts << chartView;
92 m_charts << chartView;
93
93
94 chartView = new QChartView(createScatterChart());
94 chartView = new QChartView(createScatterChart());
95 baseLayout->addWidget(chartView, 2, 2);
95 baseLayout->addWidget(chartView, 2, 2);
96 m_charts << chartView;
96 m_charts << chartView;
97
97
98 setLayout(baseLayout);
98 setLayout(baseLayout);
99
99
100 // Set defaults
100 // Set defaults
101 m_antialiasCheckBox->setChecked(true);
101 m_antialiasCheckBox->setChecked(true);
102 updateUI();
102 updateUI();
103 }
103 }
104
104
105 ThemeWidget::~ThemeWidget()
105 ThemeWidget::~ThemeWidget()
106 {
106 {
107 }
107 }
108
108
109 void ThemeWidget::connectSignals()
109 void ThemeWidget::connectSignals()
110 {
110 {
111 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
111 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
112 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
112 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
113 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
113 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
114 connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
114 connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
115 }
115 }
116
116
117 DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const
117 DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const
118 {
118 {
119 DataTable dataTable;
119 DataTable dataTable;
120
120
121 // set seed for random stuff
121 // set seed for random stuff
122 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
122 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
123
123
124 // generate random data
124 // generate random data
125 for (int i(0); i < listCount; i++) {
125 for (int i(0); i < listCount; i++) {
126 DataList dataList;
126 DataList dataList;
127 qreal yValue(0);
127 qreal yValue(0);
128 for (int j(0); j < valueCount; j++) {
128 for (int j(0); j < valueCount; j++) {
129 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
129 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
130 QPointF value((j + (qreal) rand() / (qreal) RAND_MAX) * ((qreal) m_valueMax / (qreal) valueCount),
130 QPointF value((j + (qreal) rand() / (qreal) RAND_MAX) * ((qreal) m_valueMax / (qreal) valueCount),
131 yValue);
131 yValue);
132 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
132 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
133 dataList << Data(value, label);
133 dataList << Data(value, label);
134 }
134 }
135 dataTable << dataList;
135 dataTable << dataList;
136 }
136 }
137
137
138 return dataTable;
138 return dataTable;
139 }
139 }
140
140
141 QComboBox* ThemeWidget::createThemeBox() const
141 QComboBox* ThemeWidget::createThemeBox() const
142 {
142 {
143 // settings layout
143 // settings layout
144 QComboBox* themeComboBox = new QComboBox();
144 QComboBox* themeComboBox = new QComboBox();
145 themeComboBox->addItem("Light", QChart::ChartThemeLight);
145 themeComboBox->addItem("Light", QChart::ChartThemeLight);
146 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
146 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
147 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
147 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
148 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
148 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
149 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
149 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
150 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
150 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
151 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
151 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
152 return themeComboBox;
152 return themeComboBox;
153 }
153 }
154
154
155 QComboBox* ThemeWidget::createAnimationBox() const
155 QComboBox* ThemeWidget::createAnimationBox() const
156 {
156 {
157 // settings layout
157 // settings layout
158 QComboBox* animationComboBox = new QComboBox();
158 QComboBox* animationComboBox = new QComboBox();
159 animationComboBox->addItem("No Animations", QChart::NoAnimation);
159 animationComboBox->addItem("No Animations", QChart::NoAnimation);
160 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
160 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
161 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
161 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
162 animationComboBox->addItem("All Animations", QChart::AllAnimations);
162 animationComboBox->addItem("All Animations", QChart::AllAnimations);
163 return animationComboBox;
163 return animationComboBox;
164 }
164 }
165
165
166 QComboBox* ThemeWidget::createLegendBox() const
166 QComboBox* ThemeWidget::createLegendBox() const
167 {
167 {
168 QComboBox* legendComboBox = new QComboBox();
168 QComboBox* legendComboBox = new QComboBox();
169 legendComboBox->addItem("No Legend ", 0);
169 legendComboBox->addItem("No Legend ", 0);
170 legendComboBox->addItem("Legend Top", QLegend::AlignmentTop);
170 legendComboBox->addItem("Legend Top", QLegend::AlignmentTop);
171 legendComboBox->addItem("Legend Bottom", QLegend::AlignmentBottom);
171 legendComboBox->addItem("Legend Bottom", QLegend::AlignmentBottom);
172 legendComboBox->addItem("Legend Left", QLegend::AlignmentLeft);
172 legendComboBox->addItem("Legend Left", QLegend::AlignmentLeft);
173 legendComboBox->addItem("Legend Right", QLegend::AlignmentRight);
173 legendComboBox->addItem("Legend Right", QLegend::AlignmentRight);
174 return legendComboBox;
174 return legendComboBox;
175 }
175 }
176
176
177 QChart* ThemeWidget::createAreaChart() const
177 QChart* ThemeWidget::createAreaChart() const
178 {
178 {
179 QChart *chart = new QChart();
179 QChart *chart = new QChart();
180 chart->axisX()->setNiceNumbersEnabled(true);
180 chart->axisX()->setNiceNumbersEnabled(true);
181 chart->axisY()->setNiceNumbersEnabled(true);
181 chart->axisY()->setNiceNumbersEnabled(true);
182 chart->setTitle("Area chart");
182 chart->setTitle("Area chart");
183
183
184 // The lower series initialized to zero values
184 // The lower series initialized to zero values
185 QLineSeries *lowerSeries = 0;
185 QLineSeries *lowerSeries = 0;
186 QString name("Series ");
186 QString name("Series ");
187 int nameIndex = 0;
187 int nameIndex = 0;
188 for (int i(0); i < m_dataTable.count(); i++) {
188 for (int i(0); i < m_dataTable.count(); i++) {
189 QLineSeries *upperSeries = new QLineSeries(chart);
189 QLineSeries *upperSeries = new QLineSeries(chart);
190 for (int j(0); j < m_dataTable[i].count(); j++) {
190 for (int j(0); j < m_dataTable[i].count(); j++) {
191 Data data = m_dataTable[i].at(j);
191 Data data = m_dataTable[i].at(j);
192 if (lowerSeries){
192 if (lowerSeries){
193 const QList<QPointF>& points = lowerSeries->points();
193 const QList<QPointF>& points = lowerSeries->points();
194 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
194 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
195 }else
195 }else
196 upperSeries->append(QPointF(j, data.first.y()));
196 upperSeries->append(QPointF(j, data.first.y()));
197 }
197 }
198 QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
198 QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
199 area->setName(name + QString::number(nameIndex));
199 area->setName(name + QString::number(nameIndex));
200 nameIndex++;
200 nameIndex++;
201 chart->addSeries(area);
201 chart->addSeries(area);
202 lowerSeries = upperSeries;
202 lowerSeries = upperSeries;
203 }
203 }
204
204
205 return chart;
205 return chart;
206 }
206 }
207
207
208 QChart* ThemeWidget::createBarChart(int valueCount) const
208 QChart* ThemeWidget::createBarChart(int valueCount) const
209 {
209 {
210 QChart* chart = new QChart();
210 QChart* chart = new QChart();
211 chart->axisX()->setNiceNumbersEnabled(true);
211 chart->axisX()->setNiceNumbersEnabled(true);
212 chart->axisY()->setNiceNumbersEnabled(true);
212 chart->axisY()->setNiceNumbersEnabled(true);
213 chart->setTitle("Bar chart");
213 chart->setTitle("Bar chart");
214
214
215 QBarCategories categories;
215 QBarCategories categories;
216 for (int i(0); i < valueCount; i++)
216 for (int i(0); i < valueCount; i++)
217 categories << QString::number(i);
217 categories << QString::number(i);
218
218
219 QStackedBarSeries* series = new QStackedBarSeries(chart);
219 QStackedBarSeries* series = new QStackedBarSeries(chart);
220 series->setCategories(categories);
220 series->setCategories(categories);
221 for (int i(0); i < m_dataTable.count(); i++) {
221 for (int i(0); i < m_dataTable.count(); i++) {
222 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
222 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
223 foreach (Data data, m_dataTable[i])
223 foreach (Data data, m_dataTable[i])
224 *set << data.first.y();
224 *set << data.first.y();
225 series->append(set);
225 series->append(set);
226 }
226 }
227 chart->addSeries(series);
227 chart->addSeries(series);
228
228
229 return chart;
229 return chart;
230 }
230 }
231
231
232 QChart* ThemeWidget::createLineChart() const
232 QChart* ThemeWidget::createLineChart() const
233 {
233 {
234 QChart* chart = new QChart();
234 QChart* chart = new QChart();
235 chart->axisX()->setNiceNumbersEnabled(true);
235 chart->axisX()->setNiceNumbersEnabled(true);
236 chart->axisY()->setNiceNumbersEnabled(true);
236 chart->axisY()->setNiceNumbersEnabled(true);
237 chart->setTitle("Line chart");
237 chart->setTitle("Line chart");
238
238
239 QString name("Series ");
239 QString name("Series ");
240 int nameIndex = 0;
240 int nameIndex = 0;
241 foreach (DataList list, m_dataTable) {
241 foreach (DataList list, m_dataTable) {
242 QLineSeries *series = new QLineSeries(chart);
242 QLineSeries *series = new QLineSeries(chart);
243 foreach (Data data, list)
243 foreach (Data data, list)
244 series->append(data.first);
244 series->append(data.first);
245 series->setName(name + QString::number(nameIndex));
245 series->setName(name + QString::number(nameIndex));
246 nameIndex++;
246 nameIndex++;
247 chart->addSeries(series);
247 chart->addSeries(series);
248 }
248 }
249
249
250 return chart;
250 return chart;
251 }
251 }
252
252
253 QChart* ThemeWidget::createPieChart() const
253 QChart* ThemeWidget::createPieChart() const
254 {
254 {
255 QChart* chart = new QChart();
255 QChart* chart = new QChart();
256 chart->setTitle("Pie chart");
256 chart->setTitle("Pie chart");
257
257
258 qreal pieSize = 1.0 / m_dataTable.count();
258 qreal pieSize = 1.0 / m_dataTable.count();
259 for (int i = 0; i < m_dataTable.count(); i++) {
259 for (int i = 0; i < m_dataTable.count(); i++) {
260 QPieSeries *series = new QPieSeries(chart);
260 QPieSeries *series = new QPieSeries(chart);
261 foreach (Data data, m_dataTable[i]) {
261 foreach (Data data, m_dataTable[i]) {
262 QPieSlice *slice = series->append(data.first.y(), data.second);
262 QPieSlice *slice = series->append(data.second, data.first.y());
263 if (data == m_dataTable[i].first()) {
263 if (data == m_dataTable[i].first()) {
264 slice->setLabelVisible();
264 slice->setLabelVisible();
265 slice->setExploded();
265 slice->setExploded();
266 }
266 }
267 }
267 }
268 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
268 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
269 series->setPieSize(pieSize);
269 series->setPieSize(pieSize);
270 series->setHorizontalPosition(hPos);
270 series->setHorizontalPosition(hPos);
271 series->setVerticalPosition(0.5);
271 series->setVerticalPosition(0.5);
272 chart->addSeries(series);
272 chart->addSeries(series);
273 }
273 }
274
274
275 return chart;
275 return chart;
276 }
276 }
277
277
278 QChart* ThemeWidget::createSplineChart() const
278 QChart* ThemeWidget::createSplineChart() const
279 { // spine chart
279 { // spine chart
280 QChart* chart = new QChart();
280 QChart* chart = new QChart();
281 chart->axisX()->setNiceNumbersEnabled(true);
281 chart->axisX()->setNiceNumbersEnabled(true);
282 chart->axisY()->setNiceNumbersEnabled(true);
282 chart->axisY()->setNiceNumbersEnabled(true);
283 chart->setTitle("Spline chart");
283 chart->setTitle("Spline chart");
284 QString name("Series ");
284 QString name("Series ");
285 int nameIndex = 0;
285 int nameIndex = 0;
286 foreach (DataList list, m_dataTable) {
286 foreach (DataList list, m_dataTable) {
287 QSplineSeries *series = new QSplineSeries(chart);
287 QSplineSeries *series = new QSplineSeries(chart);
288 foreach (Data data, list)
288 foreach (Data data, list)
289 series->append(data.first);
289 series->append(data.first);
290 series->setName(name + QString::number(nameIndex));
290 series->setName(name + QString::number(nameIndex));
291 nameIndex++;
291 nameIndex++;
292 chart->addSeries(series);
292 chart->addSeries(series);
293 }
293 }
294 return chart;
294 return chart;
295 }
295 }
296
296
297 QChart* ThemeWidget::createScatterChart() const
297 QChart* ThemeWidget::createScatterChart() const
298 { // scatter chart
298 { // scatter chart
299 QChart* chart = new QChart();
299 QChart* chart = new QChart();
300 chart->axisX()->setNiceNumbersEnabled(true);
300 chart->axisX()->setNiceNumbersEnabled(true);
301 chart->axisY()->setNiceNumbersEnabled(true);
301 chart->axisY()->setNiceNumbersEnabled(true);
302 chart->setTitle("Scatter chart");
302 chart->setTitle("Scatter chart");
303 QString name("Series ");
303 QString name("Series ");
304 int nameIndex = 0;
304 int nameIndex = 0;
305 foreach (DataList list, m_dataTable) {
305 foreach (DataList list, m_dataTable) {
306 QScatterSeries *series = new QScatterSeries(chart);
306 QScatterSeries *series = new QScatterSeries(chart);
307 foreach (Data data, list)
307 foreach (Data data, list)
308 series->append(data.first);
308 series->append(data.first);
309 series->setName(name + QString::number(nameIndex));
309 series->setName(name + QString::number(nameIndex));
310 nameIndex++;
310 nameIndex++;
311 chart->addSeries(series);
311 chart->addSeries(series);
312 }
312 }
313 return chart;
313 return chart;
314 }
314 }
315
315
316 void ThemeWidget::updateUI()
316 void ThemeWidget::updateUI()
317 {
317 {
318 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
318 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
319
319
320 if (m_charts.at(0)->chart()->theme() != theme) {
320 if (m_charts.at(0)->chart()->theme() != theme) {
321 foreach (QChartView *chartView, m_charts)
321 foreach (QChartView *chartView, m_charts)
322 chartView->chart()->setTheme(theme);
322 chartView->chart()->setTheme(theme);
323
323
324 QPalette pal = window()->palette();
324 QPalette pal = window()->palette();
325 if (theme == QChart::ChartThemeLight) {
325 if (theme == QChart::ChartThemeLight) {
326 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
326 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
327 pal.setColor(QPalette::WindowText, QRgb(0x404044));
327 pal.setColor(QPalette::WindowText, QRgb(0x404044));
328 } else if (theme == QChart::ChartThemeDark) {
328 } else if (theme == QChart::ChartThemeDark) {
329 pal.setColor(QPalette::Window, QRgb(0x121218));
329 pal.setColor(QPalette::Window, QRgb(0x121218));
330 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
330 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
331 } else if (theme == QChart::ChartThemeBlueCerulean) {
331 } else if (theme == QChart::ChartThemeBlueCerulean) {
332 pal.setColor(QPalette::Window, QRgb(0x40434a));
332 pal.setColor(QPalette::Window, QRgb(0x40434a));
333 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
333 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
334 } else if (theme == QChart::ChartThemeBrownSand) {
334 } else if (theme == QChart::ChartThemeBrownSand) {
335 pal.setColor(QPalette::Window, QRgb(0x9e8965));
335 pal.setColor(QPalette::Window, QRgb(0x9e8965));
336 pal.setColor(QPalette::WindowText, QRgb(0x404044));
336 pal.setColor(QPalette::WindowText, QRgb(0x404044));
337 } else if (theme == QChart::ChartThemeBlueNcs) {
337 } else if (theme == QChart::ChartThemeBlueNcs) {
338 pal.setColor(QPalette::Window, QRgb(0x018bba));
338 pal.setColor(QPalette::Window, QRgb(0x018bba));
339 pal.setColor(QPalette::WindowText, QRgb(0x404044));
339 pal.setColor(QPalette::WindowText, QRgb(0x404044));
340 } else if (theme == QChart::ChartThemeHighContrast) {
340 } else if (theme == QChart::ChartThemeHighContrast) {
341 pal.setColor(QPalette::Window, QRgb(0xffab03));
341 pal.setColor(QPalette::Window, QRgb(0xffab03));
342 pal.setColor(QPalette::WindowText, QRgb(0x181818));
342 pal.setColor(QPalette::WindowText, QRgb(0x181818));
343 } else if (theme == QChart::ChartThemeBlueIcy) {
343 } else if (theme == QChart::ChartThemeBlueIcy) {
344 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
344 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
345 pal.setColor(QPalette::WindowText, QRgb(0x404044));
345 pal.setColor(QPalette::WindowText, QRgb(0x404044));
346 } else {
346 } else {
347 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
347 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
348 pal.setColor(QPalette::WindowText, QRgb(0x404044));
348 pal.setColor(QPalette::WindowText, QRgb(0x404044));
349 }
349 }
350 window()->setPalette(pal);
350 window()->setPalette(pal);
351 }
351 }
352
352
353 bool checked = m_antialiasCheckBox->isChecked();
353 bool checked = m_antialiasCheckBox->isChecked();
354 foreach (QChartView *chart, m_charts)
354 foreach (QChartView *chart, m_charts)
355 chart->setRenderHint(QPainter::Antialiasing, checked);
355 chart->setRenderHint(QPainter::Antialiasing, checked);
356
356
357 QChart::AnimationOptions options(m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
357 QChart::AnimationOptions options(m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
358 if (m_charts.at(0)->chart()->animationOptions() != options) {
358 if (m_charts.at(0)->chart()->animationOptions() != options) {
359 foreach (QChartView *chartView, m_charts)
359 foreach (QChartView *chartView, m_charts)
360 chartView->chart()->setAnimationOptions(options);
360 chartView->chart()->setAnimationOptions(options);
361 }
361 }
362
362
363 QLegend::Alignments alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
363 QLegend::Alignments alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
364
364
365 if (!alignment) {
365 if (!alignment) {
366 foreach (QChartView *chartView, m_charts) {
366 foreach (QChartView *chartView, m_charts) {
367 chartView->chart()->legend()->hide();
367 chartView->chart()->legend()->hide();
368 }
368 }
369 } else {
369 } else {
370 foreach (QChartView *chartView, m_charts) {
370 foreach (QChartView *chartView, m_charts) {
371 chartView->chart()->legend()->setAlignment(alignment);
371 chartView->chart()->legend()->setAlignment(alignment);
372 chartView->chart()->legend()->show();
372 chartView->chart()->legend()->show();
373 }
373 }
374 }
374 }
375 }
375 }
376
376
@@ -1,48 +1,48
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 "customslice.h"
21 #include "customslice.h"
22
22
23 QTCOMMERCIALCHART_USE_NAMESPACE
23 QTCOMMERCIALCHART_USE_NAMESPACE
24
24
25 CustomSlice::CustomSlice(qreal value, QString label)
25 CustomSlice::CustomSlice(QString label, qreal value)
26 :QPieSlice(value, label)
26 :QPieSlice(label, value)
27 {
27 {
28 connect(this, SIGNAL(hovered(bool)), this, SLOT(showHighlight(bool)));
28 connect(this, SIGNAL(hovered(bool)), this, SLOT(showHighlight(bool)));
29 }
29 }
30
30
31 QBrush CustomSlice::originalBrush()
31 QBrush CustomSlice::originalBrush()
32 {
32 {
33 return m_originalBrush;
33 return m_originalBrush;
34 }
34 }
35
35
36 void CustomSlice::showHighlight(bool show)
36 void CustomSlice::showHighlight(bool show)
37 {
37 {
38 if (show) {
38 if (show) {
39 QBrush brush = this->brush();
39 QBrush brush = this->brush();
40 m_originalBrush = brush;
40 m_originalBrush = brush;
41 brush.setColor(brush.color().lighter());
41 brush.setColor(brush.color().lighter());
42 setBrush(brush);
42 setBrush(brush);
43 } else {
43 } else {
44 setBrush(m_originalBrush);
44 setBrush(m_originalBrush);
45 }
45 }
46 }
46 }
47
47
48 #include "moc_customslice.cpp"
48 #include "moc_customslice.cpp"
@@ -1,44 +1,44
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 CUSTOMSLICE_H
20 #ifndef CUSTOMSLICE_H
21 #define CUSTOMSLICE_H
21 #define CUSTOMSLICE_H
22
22
23 #include <QPieSlice>
23 #include <QPieSlice>
24
24
25 QTCOMMERCIALCHART_USE_NAMESPACE
25 QTCOMMERCIALCHART_USE_NAMESPACE
26
26
27 class CustomSlice : public QPieSlice
27 class CustomSlice : public QPieSlice
28 {
28 {
29 Q_OBJECT
29 Q_OBJECT
30
30
31 public:
31 public:
32 CustomSlice(qreal value, QString label);
32 CustomSlice(QString label, qreal value);
33
33
34 public:
34 public:
35 QBrush originalBrush();
35 QBrush originalBrush();
36
36
37 public Q_SLOTS:
37 public Q_SLOTS:
38 void showHighlight(bool show);
38 void showHighlight(bool show);
39
39
40 private:
40 private:
41 QBrush m_originalBrush;
41 QBrush m_originalBrush;
42 };
42 };
43
43
44 #endif // CUSTOMSLICE_H
44 #endif // CUSTOMSLICE_H
@@ -1,331 +1,331
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(10.0, "Slice 1");
48 *m_series << new CustomSlice("Slice 1", 10.0);
49 *m_series << new CustomSlice(20.0, "Slice 2");
49 *m_series << new CustomSlice("Slice 2", 20.0);
50 *m_series << new CustomSlice(30.0, "Slice 3");
50 *m_series << new CustomSlice("Slice 3", 30.0);
51 *m_series << new CustomSlice(40.0, "Slice 4");
51 *m_series << new CustomSlice("Slice 4", 40.0);
52 *m_series << new CustomSlice(50.0, "Slice 5");
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(0.0);
107 m_startAngle->setMinimum(0.0);
108 m_startAngle->setMaximum(360);
108 m_startAngle->setMaximum(360);
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(0.0);
113 m_endAngle->setMinimum(0.0);
114 m_endAngle->setMaximum(360);
114 m_endAngle->setMaximum(360);
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_labelPen = new QPushButton();
155 m_labelPen = new QPushButton();
156 m_labelPenTool = new PenTool("Label pen", this);
156 m_labelPenTool = new PenTool("Label pen", this);
157 QPushButton *removeSlice = new QPushButton("Remove slice");
157 QPushButton *removeSlice = new QPushButton("Remove slice");
158
158
159 QFormLayout* sliceSettingsLayout = new QFormLayout();
159 QFormLayout* sliceSettingsLayout = new QFormLayout();
160 sliceSettingsLayout->addRow("Selected", m_sliceName);
160 sliceSettingsLayout->addRow("Selected", m_sliceName);
161 sliceSettingsLayout->addRow("Value", m_sliceValue);
161 sliceSettingsLayout->addRow("Value", m_sliceValue);
162 sliceSettingsLayout->addRow("Pen", m_pen);
162 sliceSettingsLayout->addRow("Pen", m_pen);
163 sliceSettingsLayout->addRow("Brush", m_brush);
163 sliceSettingsLayout->addRow("Brush", m_brush);
164 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
164 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
165 sliceSettingsLayout->addRow("Label font", m_font);
165 sliceSettingsLayout->addRow("Label font", m_font);
166 sliceSettingsLayout->addRow("Label pen", m_labelPen);
166 sliceSettingsLayout->addRow("Label pen", m_labelPen);
167 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
167 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
168 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
168 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
169 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
169 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
170 sliceSettingsLayout->addRow(removeSlice);
170 sliceSettingsLayout->addRow(removeSlice);
171 QGroupBox* sliceSettings = new QGroupBox("Slice");
171 QGroupBox* sliceSettings = new QGroupBox("Slice");
172 sliceSettings->setLayout(sliceSettingsLayout);
172 sliceSettings->setLayout(sliceSettingsLayout);
173
173
174 connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
174 connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
175 connect(m_pen, SIGNAL(clicked()), m_penTool, SLOT(show()));
175 connect(m_pen, SIGNAL(clicked()), m_penTool, SLOT(show()));
176 connect(m_penTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
176 connect(m_penTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
177 connect(m_brush, SIGNAL(clicked()), m_brushTool, SLOT(show()));
177 connect(m_brush, SIGNAL(clicked()), m_brushTool, SLOT(show()));
178 connect(m_brushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
178 connect(m_brushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
179 connect(m_font, SIGNAL(clicked()), this, SLOT(showFontDialog()));
179 connect(m_font, SIGNAL(clicked()), this, SLOT(showFontDialog()));
180 connect(m_labelPen, SIGNAL(clicked()), m_labelPenTool, SLOT(show()));
180 connect(m_labelPen, SIGNAL(clicked()), m_labelPenTool, SLOT(show()));
181 connect(m_labelPenTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
181 connect(m_labelPenTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
182 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
182 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
183 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
183 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
184 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
184 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
185 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
185 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
186 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
186 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
187 connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice()));
187 connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice()));
188
188
189 // create chart view
189 // create chart view
190 m_chartView = new QChartView(chart);
190 m_chartView = new QChartView(chart);
191
191
192 // create main layout
192 // create main layout
193 QVBoxLayout *settingsLayout = new QVBoxLayout();
193 QVBoxLayout *settingsLayout = new QVBoxLayout();
194 settingsLayout->addWidget(chartSettings);
194 settingsLayout->addWidget(chartSettings);
195 settingsLayout->addWidget(seriesSettings);
195 settingsLayout->addWidget(seriesSettings);
196 settingsLayout->addWidget(sliceSettings);
196 settingsLayout->addWidget(sliceSettings);
197 settingsLayout->addStretch();
197 settingsLayout->addStretch();
198
198
199 QGridLayout* baseLayout = new QGridLayout();
199 QGridLayout* baseLayout = new QGridLayout();
200 baseLayout->addLayout(settingsLayout, 0, 0);
200 baseLayout->addLayout(settingsLayout, 0, 0);
201 baseLayout->addWidget(m_chartView, 0, 1);
201 baseLayout->addWidget(m_chartView, 0, 1);
202 setLayout(baseLayout);
202 setLayout(baseLayout);
203
203
204 updateSerieSettings();
204 updateSerieSettings();
205 }
205 }
206
206
207
207
208 void MainWidget::updateChartSettings()
208 void MainWidget::updateChartSettings()
209 {
209 {
210 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
210 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
211 m_chartView->chart()->setTheme(theme);
211 m_chartView->chart()->setTheme(theme);
212 m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked());
212 m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked());
213
213
214 if (m_animationsCheckBox->checkState() == Qt::Checked)
214 if (m_animationsCheckBox->checkState() == Qt::Checked)
215 m_chartView->chart()->setAnimationOptions(QChart::AllAnimations);
215 m_chartView->chart()->setAnimationOptions(QChart::AllAnimations);
216 else
216 else
217 m_chartView->chart()->setAnimationOptions(QChart::NoAnimation);
217 m_chartView->chart()->setAnimationOptions(QChart::NoAnimation);
218
218
219 if (m_legendCheckBox->checkState() == Qt::Checked)
219 if (m_legendCheckBox->checkState() == Qt::Checked)
220 m_chartView->chart()->legend()->show();
220 m_chartView->chart()->legend()->show();
221 else
221 else
222 m_chartView->chart()->legend()->hide();
222 m_chartView->chart()->legend()->hide();
223 }
223 }
224
224
225 void MainWidget::updateSerieSettings()
225 void MainWidget::updateSerieSettings()
226 {
226 {
227 m_series->setHorizontalPosition(m_hPosition->value());
227 m_series->setHorizontalPosition(m_hPosition->value());
228 m_series->setVerticalPosition(m_vPosition->value());
228 m_series->setVerticalPosition(m_vPosition->value());
229 m_series->setPieSize(m_sizeFactor->value());
229 m_series->setPieSize(m_sizeFactor->value());
230 m_series->setPieStartAngle(m_startAngle->value());
230 m_series->setPieStartAngle(m_startAngle->value());
231 m_series->setPieEndAngle(m_endAngle->value());
231 m_series->setPieEndAngle(m_endAngle->value());
232 }
232 }
233
233
234 void MainWidget::updateSliceSettings()
234 void MainWidget::updateSliceSettings()
235 {
235 {
236 if (!m_slice)
236 if (!m_slice)
237 return;
237 return;
238
238
239 m_slice->setValue(m_sliceValue->value());
239 m_slice->setValue(m_sliceValue->value());
240
240
241 m_slice->setPen(m_penTool->pen());
241 m_slice->setPen(m_penTool->pen());
242 m_slice->setBrush(m_brushTool->brush());
242 m_slice->setBrush(m_brushTool->brush());
243
243
244 m_slice->setLabelPen(m_labelPenTool->pen());
244 m_slice->setLabelPen(m_labelPenTool->pen());
245 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
245 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
246 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
246 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
247
247
248 m_slice->setExploded(m_sliceExploded->isChecked());
248 m_slice->setExploded(m_sliceExploded->isChecked());
249 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
249 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
250 }
250 }
251
251
252 void MainWidget::handleSliceClicked(QPieSlice* slice)
252 void MainWidget::handleSliceClicked(QPieSlice* slice)
253 {
253 {
254 m_slice = static_cast<CustomSlice*>(slice);
254 m_slice = static_cast<CustomSlice*>(slice);
255
255
256 // name
256 // name
257 m_sliceName->setText(slice->label());
257 m_sliceName->setText(slice->label());
258
258
259 // value
259 // value
260 m_sliceValue->blockSignals(true);
260 m_sliceValue->blockSignals(true);
261 m_sliceValue->setValue(slice->value());
261 m_sliceValue->setValue(slice->value());
262 m_sliceValue->blockSignals(false);
262 m_sliceValue->blockSignals(false);
263
263
264 // pen
264 // pen
265 m_pen->setText(PenTool::name(m_slice->pen()));
265 m_pen->setText(PenTool::name(m_slice->pen()));
266 m_penTool->setPen(m_slice->pen());
266 m_penTool->setPen(m_slice->pen());
267
267
268 // brush
268 // brush
269 m_brush->setText(m_slice->originalBrush().color().name());
269 m_brush->setText(m_slice->originalBrush().color().name());
270 m_brushTool->setBrush(m_slice->originalBrush());
270 m_brushTool->setBrush(m_slice->originalBrush());
271
271
272 // label
272 // label
273 m_labelPen->setText(PenTool::name(m_slice->labelPen()));
273 m_labelPen->setText(PenTool::name(m_slice->labelPen()));
274 m_labelPenTool->setPen(m_slice->labelPen());
274 m_labelPenTool->setPen(m_slice->labelPen());
275 m_font->setText(slice->labelFont().toString());
275 m_font->setText(slice->labelFont().toString());
276 m_sliceLabelVisible->blockSignals(true);
276 m_sliceLabelVisible->blockSignals(true);
277 m_sliceLabelVisible->setChecked(slice->isLabelVisible());
277 m_sliceLabelVisible->setChecked(slice->isLabelVisible());
278 m_sliceLabelVisible->blockSignals(false);
278 m_sliceLabelVisible->blockSignals(false);
279 m_sliceLabelArmFactor->blockSignals(true);
279 m_sliceLabelArmFactor->blockSignals(true);
280 m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
280 m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
281 m_sliceLabelArmFactor->blockSignals(false);
281 m_sliceLabelArmFactor->blockSignals(false);
282
282
283 // exploded
283 // exploded
284 m_sliceExploded->blockSignals(true);
284 m_sliceExploded->blockSignals(true);
285 m_sliceExploded->setChecked(slice->isExploded());
285 m_sliceExploded->setChecked(slice->isExploded());
286 m_sliceExploded->blockSignals(false);
286 m_sliceExploded->blockSignals(false);
287 m_sliceExplodedFactor->blockSignals(true);
287 m_sliceExplodedFactor->blockSignals(true);
288 m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
288 m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
289 m_sliceExplodedFactor->blockSignals(false);
289 m_sliceExplodedFactor->blockSignals(false);
290 }
290 }
291
291
292 void MainWidget::showFontDialog()
292 void MainWidget::showFontDialog()
293 {
293 {
294 if (!m_slice)
294 if (!m_slice)
295 return;
295 return;
296
296
297 QFontDialog dialog(m_slice->labelFont());
297 QFontDialog dialog(m_slice->labelFont());
298 dialog.show();
298 dialog.show();
299 dialog.exec();
299 dialog.exec();
300
300
301 m_slice->setLabelFont(dialog.currentFont());
301 m_slice->setLabelFont(dialog.currentFont());
302 m_font->setText(dialog.currentFont().toString());
302 m_font->setText(dialog.currentFont().toString());
303 }
303 }
304
304
305 void MainWidget::appendSlice()
305 void MainWidget::appendSlice()
306 {
306 {
307 *m_series << new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1));
307 *m_series << new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0);
308 }
308 }
309
309
310 void MainWidget::insertSlice()
310 void MainWidget::insertSlice()
311 {
311 {
312 if (!m_slice)
312 if (!m_slice)
313 return;
313 return;
314
314
315 int i = m_series->slices().indexOf(m_slice);
315 int i = m_series->slices().indexOf(m_slice);
316
316
317 m_series->insert(i, new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1)));
317 m_series->insert(i, new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0));
318 }
318 }
319
319
320 void MainWidget::removeSlice()
320 void MainWidget::removeSlice()
321 {
321 {
322 if (!m_slice)
322 if (!m_slice)
323 return;
323 return;
324
324
325 m_sliceName->setText("<click a slice>");
325 m_sliceName->setText("<click a slice>");
326
326
327 m_series->remove(m_slice);
327 m_series->remove(m_slice);
328 m_slice = 0;
328 m_slice = 0;
329 }
329 }
330
330
331 #include "moc_mainwidget.cpp"
331 #include "moc_mainwidget.cpp"
@@ -1,69 +1,69
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 <QApplication>
21 #include <QApplication>
22 #include <QMainWindow>
22 #include <QMainWindow>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QPieSeries>
24 #include <QPieSeries>
25 #include <QPieSlice>
25 #include <QPieSlice>
26
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
28
29 int main(int argc, char *argv[])
29 int main(int argc, char *argv[])
30 {
30 {
31 QApplication a(argc, argv);
31 QApplication a(argc, argv);
32
32
33 //![1]
33 //![1]
34 QPieSeries *series = new QPieSeries();
34 QPieSeries *series = new QPieSeries();
35 series->append(1, "Jane");
35 series->append("Jane", 1);
36 series->append(2, "Joe");
36 series->append("Joe", 2);
37 series->append(3, "Andy");
37 series->append("Andy", 3);
38 series->append(4, "Barbara");
38 series->append("Barbara", 4);
39 series->append(5, "Axel");
39 series->append("Axel", 5);
40 //![1]
40 //![1]
41
41
42 //![2]
42 //![2]
43 QPieSlice *slice = series->slices().at(1);
43 QPieSlice *slice = series->slices().at(1);
44 slice->setExploded();
44 slice->setExploded();
45 slice->setLabelVisible();
45 slice->setLabelVisible();
46 slice->setPen(QPen(Qt::darkGreen, 2));
46 slice->setPen(QPen(Qt::darkGreen, 2));
47 slice->setBrush(Qt::green);
47 slice->setBrush(Qt::green);
48 //![2]
48 //![2]
49
49
50 //![3]
50 //![3]
51 QChart* chart = new QChart();
51 QChart* chart = new QChart();
52 chart->addSeries(series);
52 chart->addSeries(series);
53 chart->setTitle("Simple piechart example");
53 chart->setTitle("Simple piechart example");
54 //![3]
54 //![3]
55
55
56 //![4]
56 //![4]
57 QChartView* chartView = new QChartView(chart);
57 QChartView* chartView = new QChartView(chart);
58 chartView->setRenderHint(QPainter::Antialiasing);
58 chartView->setRenderHint(QPainter::Antialiasing);
59 //![4]
59 //![4]
60
60
61 //![5]
61 //![5]
62 QMainWindow window;
62 QMainWindow window;
63 window.setCentralWidget(chartView);
63 window.setCentralWidget(chartView);
64 window.resize(400, 300);
64 window.resize(400, 300);
65 window.show();
65 window.show();
66 //![5]
66 //![5]
67
67
68 return a.exec();
68 return a.exec();
69 }
69 }
@@ -1,104 +1,104
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 "declarativepieseries.h"
21 #include "declarativepieseries.h"
22 #include "declarativechart.h"
22 #include "declarativechart.h"
23 #include "qchart.h"
23 #include "qchart.h"
24 #include <qdeclarativelist.h>
24 #include <qdeclarativelist.h>
25 #include "qpiemodelmapper.h"
25 #include "qpiemodelmapper.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
29 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
30 QPieSeries(parent)
30 QPieSeries(parent)
31 {
31 {
32 // TODO: set default model on init?
32 // TODO: set default model on init?
33 // setModel(new DeclarativeTableModel());
33 // setModel(new DeclarativeTableModel());
34
34
35 // TODO: Set default mapper parameters to allow easy to use PieSeries api?
35 // TODO: Set default mapper parameters to allow easy to use PieSeries api?
36 QPieModelMapper *mapper = modelMapper();//new QPieModelMapper();
36 QPieModelMapper *mapper = modelMapper();//new QPieModelMapper();
37 mapper->setMapLabels(0);
37 mapper->setMapLabels(0);
38 mapper->setMapValues(1);
38 mapper->setMapValues(1);
39 mapper->setOrientation(Qt::Vertical);
39 mapper->setOrientation(Qt::Vertical);
40 mapper->setFirst(0);
40 mapper->setFirst(0);
41 mapper->setCount(-1);
41 mapper->setCount(-1);
42 // setModelMapper(mapper);
42 // setModelMapper(mapper);
43 }
43 }
44
44
45 void DeclarativePieSeries::classBegin()
45 void DeclarativePieSeries::classBegin()
46 {
46 {
47 }
47 }
48
48
49 void DeclarativePieSeries::componentComplete()
49 void DeclarativePieSeries::componentComplete()
50 {
50 {
51 foreach(QObject *child, children()) {
51 foreach(QObject *child, children()) {
52 if (qobject_cast<QPieSlice *>(child)) {
52 if (qobject_cast<QPieSlice *>(child)) {
53 QPieSeries::append(qobject_cast<QPieSlice *>(child));
53 QPieSeries::append(qobject_cast<QPieSlice *>(child));
54 }
54 }
55 }
55 }
56 }
56 }
57
57
58 QDeclarativeListProperty<QPieSlice> DeclarativePieSeries::initialSlices()
58 QDeclarativeListProperty<QPieSlice> DeclarativePieSeries::initialSlices()
59 {
59 {
60 return QDeclarativeListProperty<QPieSlice>(this, 0, &DeclarativePieSeries::appendInitialSlices);
60 return QDeclarativeListProperty<QPieSlice>(this, 0, &DeclarativePieSeries::appendInitialSlices);
61 }
61 }
62
62
63 QPieSlice *DeclarativePieSeries::at(int index)
63 QPieSlice *DeclarativePieSeries::at(int index)
64 {
64 {
65 QList<QPieSlice*> sliceList = slices();
65 QList<QPieSlice*> sliceList = slices();
66 if (index < sliceList.count())
66 if (index < sliceList.count())
67 return sliceList[index];
67 return sliceList[index];
68
68
69 return 0;
69 return 0;
70 }
70 }
71
71
72 QPieSlice* DeclarativePieSeries::find(QString label)
72 QPieSlice* DeclarativePieSeries::find(QString label)
73 {
73 {
74 foreach (QPieSlice *slice, slices()) {
74 foreach (QPieSlice *slice, slices()) {
75 if (slice->label() == label)
75 if (slice->label() == label)
76 return slice;
76 return slice;
77 }
77 }
78 return 0;
78 return 0;
79 }
79 }
80
80
81 QPieSlice* DeclarativePieSeries::append(QString name, qreal value)
81 QPieSlice* DeclarativePieSeries::append(QString name, qreal value)
82 {
82 {
83 // TODO: parameter order is wrong, switch it:
83 // TODO: parameter order is wrong, switch it:
84 return QPieSeries::append(value, name);
84 return QPieSeries::append(name, value);
85 }
85 }
86
86
87 void DeclarativePieSeries::setPieModel(DeclarativeTableModel *model)
87 void DeclarativePieSeries::setPieModel(DeclarativeTableModel *model)
88 {
88 {
89 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
89 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
90 if (m) {
90 if (m) {
91 QPieSeries::setModel(m);
91 QPieSeries::setModel(m);
92 } else {
92 } else {
93 qWarning("DeclarativePieSeries: Illegal model");
93 qWarning("DeclarativePieSeries: Illegal model");
94 }
94 }
95 }
95 }
96
96
97 DeclarativeTableModel *DeclarativePieSeries::pieModel()
97 DeclarativeTableModel *DeclarativePieSeries::pieModel()
98 {
98 {
99 return qobject_cast<DeclarativeTableModel *>(model());
99 return qobject_cast<DeclarativeTableModel *>(model());
100 }
100 }
101
101
102 #include "moc_declarativepieseries.cpp"
102 #include "moc_declarativepieseries.cpp"
103
103
104 QTCOMMERCIALCHART_END_NAMESPACE
104 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,813 +1,813
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 "qpieseries.h"
21 #include "qpieseries.h"
22 #include "qpieseries_p.h"
22 #include "qpieseries_p.h"
23 #include "qpieslice.h"
23 #include "qpieslice.h"
24 #include "pieslicedata_p.h"
24 #include "pieslicedata_p.h"
25 #include "chartdataset_p.h"
25 #include "chartdataset_p.h"
26 #include "charttheme_p.h"
26 #include "charttheme_p.h"
27 #include "chartanimator_p.h"
27 #include "chartanimator_p.h"
28 #include "legendmarker_p.h"
28 #include "legendmarker_p.h"
29 #include <QAbstractItemModel>
29 #include <QAbstractItemModel>
30 #include "qpiemodelmapper.h"
30 #include "qpiemodelmapper.h"
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 /*!
34 /*!
35 \class QPieSeries
35 \class QPieSeries
36 \brief Pie series API for QtCommercial Charts
36 \brief Pie series API for QtCommercial Charts
37
37
38 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
38 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
39 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
39 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
40 The actual slice size is determined by that relative value.
40 The actual slice size is determined by that relative value.
41
41
42 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
42 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
43 These relate to the actual chart rectangle.
43 These relate to the actual chart rectangle.
44
44
45 By default the pie is defined as a full pie but it can also be a partial pie.
45 By default the pie is defined as a full pie but it can also be a partial pie.
46 This can be done by setting a starting angle and angle span to the series.
46 This can be done by setting a starting angle and angle span to the series.
47 Full pie is 360 degrees where 0 is at 12 a'clock.
47 Full pie is 360 degrees where 0 is at 12 a'clock.
48
48
49 See the \l {PieChart Example} {pie chart example} to learn how to create a simple pie chart.
49 See the \l {PieChart Example} {pie chart example} to learn how to create a simple pie chart.
50 \image examples_piechart.png
50 \image examples_piechart.png
51 */
51 */
52
52
53 /*!
53 /*!
54 \property QPieSeries::horizontalPosition
54 \property QPieSeries::horizontalPosition
55 \brief Defines the horizontal position of the pie.
55 \brief Defines the horizontal position of the pie.
56
56
57 The value is a relative value to the chart rectangle where:
57 The value is a relative value to the chart rectangle where:
58
58
59 \list
59 \list
60 \o 0.0 is the absolute left.
60 \o 0.0 is the absolute left.
61 \o 1.0 is the absolute right.
61 \o 1.0 is the absolute right.
62 \endlist
62 \endlist
63
63
64 Default value is 0.5 (center).
64 Default value is 0.5 (center).
65 */
65 */
66
66
67 /*!
67 /*!
68 \property QPieSeries::verticalPosition
68 \property QPieSeries::verticalPosition
69 \brief Defines the vertical position of the pie.
69 \brief Defines the vertical position of the pie.
70
70
71 The value is a relative value to the chart rectangle where:
71 The value is a relative value to the chart rectangle where:
72
72
73 \list
73 \list
74 \o 0.0 is the absolute top.
74 \o 0.0 is the absolute top.
75 \o 1.0 is the absolute bottom.
75 \o 1.0 is the absolute bottom.
76 \endlist
76 \endlist
77
77
78 Default value is 0.5 (center).
78 Default value is 0.5 (center).
79 */
79 */
80
80
81 /*!
81 /*!
82 \property QPieSeries::size
82 \property QPieSeries::size
83 \brief Defines the pie size.
83 \brief Defines the pie size.
84
84
85 The value is a relative value to the chart rectangle where:
85 The value is a relative value to the chart rectangle where:
86
86
87 \list
87 \list
88 \o 0.0 is the minimum size (pie not drawn).
88 \o 0.0 is the minimum size (pie not drawn).
89 \o 1.0 is the maximum size that can fit the chart.
89 \o 1.0 is the maximum size that can fit the chart.
90 \endlist
90 \endlist
91
91
92 Default value is 0.7.
92 Default value is 0.7.
93 */
93 */
94
94
95 /*!
95 /*!
96 \property QPieSeries::startAngle
96 \property QPieSeries::startAngle
97 \brief Defines the starting angle of the pie.
97 \brief Defines the starting angle of the pie.
98
98
99 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
99 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
100
100
101 Default is value is 0.
101 Default is value is 0.
102 */
102 */
103
103
104 /*!
104 /*!
105 \property QPieSeries::endAngle
105 \property QPieSeries::endAngle
106 \brief Defines the ending angle of the pie.
106 \brief Defines the ending angle of the pie.
107
107
108 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
108 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
109
109
110 Default is value is 360.
110 Default is value is 360.
111 */
111 */
112
112
113
113
114 /*!
114 /*!
115 Constructs a series object which is a child of \a parent.
115 Constructs a series object which is a child of \a parent.
116 */
116 */
117 QPieSeries::QPieSeries(QObject *parent) :
117 QPieSeries::QPieSeries(QObject *parent) :
118 QAbstractSeries(*new QPieSeriesPrivate(this),parent)
118 QAbstractSeries(*new QPieSeriesPrivate(this),parent)
119 {
119 {
120
120
121 }
121 }
122
122
123 /*!
123 /*!
124 Destroys the series and its slices.
124 Destroys the series and its slices.
125 */
125 */
126 QPieSeries::~QPieSeries()
126 QPieSeries::~QPieSeries()
127 {
127 {
128 // NOTE: d_prt destroyed by QObject
128 // NOTE: d_prt destroyed by QObject
129 }
129 }
130
130
131 /*!
131 /*!
132 Returns QChartSeries::SeriesTypePie.
132 Returns QChartSeries::SeriesTypePie.
133 */
133 */
134 QAbstractSeries::SeriesType QPieSeries::type() const
134 QAbstractSeries::SeriesType QPieSeries::type() const
135 {
135 {
136 return QAbstractSeries::SeriesTypePie;
136 return QAbstractSeries::SeriesTypePie;
137 }
137 }
138
138
139 /*!
139 /*!
140 Appends an array of \a slices to the series.
140 Appends an array of \a slices to the series.
141 Slice ownership is passed to the series.
141 Slice ownership is passed to the series.
142 */
142 */
143 bool QPieSeries::append(QList<QPieSlice*> slices)
143 bool QPieSeries::append(QList<QPieSlice*> slices)
144 {
144 {
145 Q_D(QPieSeries);
145 Q_D(QPieSeries);
146
146
147 if (slices.count() == 0)
147 if (slices.count() == 0)
148 return false;
148 return false;
149
149
150 foreach (QPieSlice* s, slices) {
150 foreach (QPieSlice* s, slices) {
151 if (!s || d->m_slices.contains(s))
151 if (!s || d->m_slices.contains(s))
152 return false;
152 return false;
153 }
153 }
154
154
155 foreach (QPieSlice* s, slices) {
155 foreach (QPieSlice* s, slices) {
156 s->setParent(this);
156 s->setParent(this);
157 d->m_slices << s;
157 d->m_slices << s;
158 }
158 }
159
159
160 d->updateDerivativeData();
160 d->updateDerivativeData();
161
161
162 foreach (QPieSlice* s, slices) {
162 foreach (QPieSlice* s, slices) {
163 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
163 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
164 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
164 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
165 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
165 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
166 }
166 }
167
167
168 emit d->added(slices);
168 emit d->added(slices);
169
169
170 return true;
170 return true;
171 }
171 }
172
172
173 /*!
173 /*!
174 Appends a single \a slice to the series.
174 Appends a single \a slice to the series.
175 Slice ownership is passed to the series.
175 Slice ownership is passed to the series.
176 */
176 */
177 bool QPieSeries::append(QPieSlice* slice)
177 bool QPieSeries::append(QPieSlice* slice)
178 {
178 {
179 return append(QList<QPieSlice*>() << slice);
179 return append(QList<QPieSlice*>() << slice);
180 }
180 }
181
181
182 /*!
182 /*!
183 Appends a single \a slice to the series and returns a reference to the series.
183 Appends a single \a slice to the series and returns a reference to the series.
184 Slice ownership is passed to the series.
184 Slice ownership is passed to the series.
185 */
185 */
186 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
186 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
187 {
187 {
188 append(slice);
188 append(slice);
189 return *this;
189 return *this;
190 }
190 }
191
191
192
192
193 /*!
193 /*!
194 Appends a single slice to the series with give \a value and \a name.
194 Appends a single slice to the series with give \a value and \a label.
195 Slice ownership is passed to the series.
195 Slice ownership is passed to the series.
196 */
196 */
197 QPieSlice* QPieSeries::append(qreal value, QString name)
197 QPieSlice* QPieSeries::append(QString label, qreal value)
198 {
198 {
199 QPieSlice* slice = new QPieSlice(value, name);
199 QPieSlice* slice = new QPieSlice(label, value);
200 append(slice);
200 append(slice);
201 return slice;
201 return slice;
202 }
202 }
203
203
204 /*!
204 /*!
205 Inserts a single \a slice to the series before the slice at \a index position.
205 Inserts a single \a slice to the series before the slice at \a index position.
206 Slice ownership is passed to the series.
206 Slice ownership is passed to the series.
207 */
207 */
208 bool QPieSeries::insert(int index, QPieSlice* slice)
208 bool QPieSeries::insert(int index, QPieSlice* slice)
209 {
209 {
210 Q_D(QPieSeries);
210 Q_D(QPieSeries);
211
211
212 if (index < 0 || index > d->m_slices.count())
212 if (index < 0 || index > d->m_slices.count())
213 return false;
213 return false;
214
214
215 if (!slice || d->m_slices.contains(slice))
215 if (!slice || d->m_slices.contains(slice))
216 return false;
216 return false;
217
217
218 slice->setParent(this);
218 slice->setParent(this);
219 d->m_slices.insert(index, slice);
219 d->m_slices.insert(index, slice);
220
220
221 d->updateDerivativeData();
221 d->updateDerivativeData();
222
222
223 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
223 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
224 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
224 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
225 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
225 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
226
226
227 emit d->added(QList<QPieSlice*>() << slice);
227 emit d->added(QList<QPieSlice*>() << slice);
228
228
229 return true;
229 return true;
230 }
230 }
231
231
232 /*!
232 /*!
233 Removes a single \a slice from the series and deletes the slice.
233 Removes a single \a slice from the series and deletes the slice.
234
234
235 Do not reference the pointer after this call.
235 Do not reference the pointer after this call.
236 */
236 */
237 bool QPieSeries::remove(QPieSlice* slice)
237 bool QPieSeries::remove(QPieSlice* slice)
238 {
238 {
239 Q_D(QPieSeries);
239 Q_D(QPieSeries);
240
240
241 if (!d->m_slices.removeOne(slice))
241 if (!d->m_slices.removeOne(slice))
242 return false;
242 return false;
243
243
244 d->updateDerivativeData();
244 d->updateDerivativeData();
245
245
246 emit d->removed(QList<QPieSlice*>() << slice);
246 emit d->removed(QList<QPieSlice*>() << slice);
247
247
248 delete slice;
248 delete slice;
249 slice = 0;
249 slice = 0;
250
250
251 return true;
251 return true;
252 }
252 }
253
253
254 /*!
254 /*!
255 Clears all slices from the series.
255 Clears all slices from the series.
256 */
256 */
257 void QPieSeries::clear()
257 void QPieSeries::clear()
258 {
258 {
259 Q_D(QPieSeries);
259 Q_D(QPieSeries);
260 if (d->m_slices.count() == 0)
260 if (d->m_slices.count() == 0)
261 return;
261 return;
262
262
263 QList<QPieSlice*> slices = d->m_slices;
263 QList<QPieSlice*> slices = d->m_slices;
264 foreach (QPieSlice* s, d->m_slices) {
264 foreach (QPieSlice* s, d->m_slices) {
265 d->m_slices.removeOne(s);
265 d->m_slices.removeOne(s);
266 delete s;
266 delete s;
267 }
267 }
268
268
269 d->updateDerivativeData();
269 d->updateDerivativeData();
270
270
271 emit d->removed(slices);
271 emit d->removed(slices);
272 }
272 }
273
273
274 /*!
274 /*!
275 returns the number of the slices in this series.
275 returns the number of the slices in this series.
276 */
276 */
277 int QPieSeries::count() const
277 int QPieSeries::count() const
278 {
278 {
279 Q_D(const QPieSeries);
279 Q_D(const QPieSeries);
280 return d->m_slices.count();
280 return d->m_slices.count();
281 }
281 }
282
282
283 /*!
283 /*!
284 Returns true is the series is empty.
284 Returns true is the series is empty.
285 */
285 */
286 bool QPieSeries::isEmpty() const
286 bool QPieSeries::isEmpty() const
287 {
287 {
288 Q_D(const QPieSeries);
288 Q_D(const QPieSeries);
289 return d->m_slices.isEmpty();
289 return d->m_slices.isEmpty();
290 }
290 }
291
291
292 /*!
292 /*!
293 Returns a list of slices that belong to this series.
293 Returns a list of slices that belong to this series.
294 */
294 */
295 QList<QPieSlice*> QPieSeries::slices() const
295 QList<QPieSlice*> QPieSeries::slices() const
296 {
296 {
297 Q_D(const QPieSeries);
297 Q_D(const QPieSeries);
298 return d->m_slices;
298 return d->m_slices;
299 }
299 }
300
300
301 void QPieSeries::setHorizontalPosition(qreal relativePosition)
301 void QPieSeries::setHorizontalPosition(qreal relativePosition)
302 {
302 {
303 Q_D(QPieSeries);
303 Q_D(QPieSeries);
304 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
304 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
305 emit d->piePositionChanged();
305 emit d->piePositionChanged();
306 }
306 }
307
307
308 void QPieSeries::setVerticalPosition(qreal relativePosition)
308 void QPieSeries::setVerticalPosition(qreal relativePosition)
309 {
309 {
310 Q_D(QPieSeries);
310 Q_D(QPieSeries);
311 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
311 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
312 emit d->piePositionChanged();
312 emit d->piePositionChanged();
313 }
313 }
314
314
315 qreal QPieSeries::horizontalPosition() const
315 qreal QPieSeries::horizontalPosition() const
316 {
316 {
317 Q_D(const QPieSeries);
317 Q_D(const QPieSeries);
318 return d->m_pieRelativeHorPos;
318 return d->m_pieRelativeHorPos;
319 }
319 }
320
320
321 qreal QPieSeries::verticalPosition() const
321 qreal QPieSeries::verticalPosition() const
322 {
322 {
323 Q_D(const QPieSeries);
323 Q_D(const QPieSeries);
324 return d->m_pieRelativeVerPos;
324 return d->m_pieRelativeVerPos;
325 }
325 }
326
326
327 void QPieSeries::setPieSize(qreal relativeSize)
327 void QPieSeries::setPieSize(qreal relativeSize)
328 {
328 {
329 Q_D(QPieSeries);
329 Q_D(QPieSeries);
330 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
330 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
331 emit d->pieSizeChanged();
331 emit d->pieSizeChanged();
332 }
332 }
333
333
334 qreal QPieSeries::pieSize() const
334 qreal QPieSeries::pieSize() const
335 {
335 {
336 Q_D(const QPieSeries);
336 Q_D(const QPieSeries);
337 return d->m_pieRelativeSize;
337 return d->m_pieRelativeSize;
338 }
338 }
339
339
340
340
341 void QPieSeries::setPieStartAngle(qreal angle)
341 void QPieSeries::setPieStartAngle(qreal angle)
342 {
342 {
343 Q_D(QPieSeries);
343 Q_D(QPieSeries);
344 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
344 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
345 d->updateDerivativeData();
345 d->updateDerivativeData();
346 }
346 }
347
347
348 qreal QPieSeries::pieStartAngle() const
348 qreal QPieSeries::pieStartAngle() const
349 {
349 {
350 Q_D(const QPieSeries);
350 Q_D(const QPieSeries);
351 return d->m_pieStartAngle;
351 return d->m_pieStartAngle;
352 }
352 }
353
353
354 /*!
354 /*!
355 Sets the end angle of the pie.
355 Sets the end angle of the pie.
356
356
357 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
357 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
358
358
359 \a angle must be greater than start angle.
359 \a angle must be greater than start angle.
360
360
361 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
361 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
362 */
362 */
363 void QPieSeries::setPieEndAngle(qreal angle)
363 void QPieSeries::setPieEndAngle(qreal angle)
364 {
364 {
365 Q_D(QPieSeries);
365 Q_D(QPieSeries);
366
366
367 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
367 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
368 d->updateDerivativeData();
368 d->updateDerivativeData();
369 }
369 }
370
370
371 /*!
371 /*!
372 Returns the end angle of the pie.
372 Returns the end angle of the pie.
373
373
374 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
374 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
375
375
376 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
376 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
377 */
377 */
378 qreal QPieSeries::pieEndAngle() const
378 qreal QPieSeries::pieEndAngle() const
379 {
379 {
380 Q_D(const QPieSeries);
380 Q_D(const QPieSeries);
381 return d->m_pieEndAngle;
381 return d->m_pieEndAngle;
382 }
382 }
383
383
384 /*!
384 /*!
385 Sets the all the slice labels \a visible or invisible.
385 Sets the all the slice labels \a visible or invisible.
386
386
387 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
387 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
388 */
388 */
389 void QPieSeries::setLabelsVisible(bool visible)
389 void QPieSeries::setLabelsVisible(bool visible)
390 {
390 {
391 Q_D(QPieSeries);
391 Q_D(QPieSeries);
392 foreach (QPieSlice* s, d->m_slices)
392 foreach (QPieSlice* s, d->m_slices)
393 s->setLabelVisible(visible);
393 s->setLabelVisible(visible);
394 }
394 }
395
395
396 /*!
396 /*!
397 Returns the sum of all slice values in this series.
397 Returns the sum of all slice values in this series.
398
398
399 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
399 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
400 */
400 */
401 qreal QPieSeries::sum() const
401 qreal QPieSeries::sum() const
402 {
402 {
403 Q_D(const QPieSeries);
403 Q_D(const QPieSeries);
404 return d->m_sum;
404 return d->m_sum;
405 }
405 }
406
406
407 /*!
407 /*!
408 \fn void QPieSeries::clicked(QPieSlice* slice)
408 \fn void QPieSeries::clicked(QPieSlice* slice)
409
409
410 This signal is emitted when a \a slice has been clicked.
410 This signal is emitted when a \a slice has been clicked.
411
411
412 \sa QPieSlice::clicked()
412 \sa QPieSlice::clicked()
413 */
413 */
414
414
415 /*!
415 /*!
416 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
416 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
417
417
418 This signal is emitted when user has hovered over or away from the \a slice.
418 This signal is emitted when user has hovered over or away from the \a slice.
419
419
420 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
420 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
421
421
422 \sa QPieSlice::hovered()
422 \sa QPieSlice::hovered()
423 */
423 */
424
424
425 /*!
425 /*!
426 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
426 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
427 Sets the \a model to be used as a data source
427 Sets the \a model to be used as a data source
428 */
428 */
429 void QPieSeries::setModel(QAbstractItemModel* model)
429 void QPieSeries::setModel(QAbstractItemModel* model)
430 {
430 {
431 Q_D(QPieSeries);
431 Q_D(QPieSeries);
432 // disconnect signals from old model
432 // disconnect signals from old model
433 if(d->m_model)
433 if(d->m_model)
434 {
434 {
435 disconnect(d->m_model, 0, this, 0);
435 disconnect(d->m_model, 0, this, 0);
436 }
436 }
437
437
438 // set new model
438 // set new model
439 if(model)
439 if(model)
440 {
440 {
441 d->m_model = model;
441 d->m_model = model;
442 // connect signals from the model
442 // connect signals from the model
443 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
443 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
444 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
444 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
445 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
445 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
446 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
446 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
447 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
447 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
448
448
449 if (d->m_mapper)
449 if (d->m_mapper)
450 d->initializePieFromModel();
450 d->initializePieFromModel();
451 }
451 }
452 else
452 else
453 {
453 {
454 d->m_model = 0;
454 d->m_model = 0;
455 }
455 }
456 }
456 }
457
457
458 void QPieSeries::setModelMapper(QPieModelMapper *mapper)
458 void QPieSeries::setModelMapper(QPieModelMapper *mapper)
459 {
459 {
460 Q_D(QPieSeries);
460 Q_D(QPieSeries);
461 // disconnect signals from old mapper
461 // disconnect signals from old mapper
462 if (d->m_mapper) {
462 if (d->m_mapper) {
463 QObject::disconnect(d->m_mapper, 0, this, 0);
463 QObject::disconnect(d->m_mapper, 0, this, 0);
464 }
464 }
465
465
466 if (mapper) {
466 if (mapper) {
467 d->m_mapper = mapper;
467 d->m_mapper = mapper;
468 // connect the signal from the mapper
468 // connect the signal from the mapper
469 connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializePieFromModel()));
469 connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializePieFromModel()));
470
470
471 if (d->m_model)
471 if (d->m_model)
472 d->initializePieFromModel();
472 d->initializePieFromModel();
473 } else {
473 } else {
474 d->m_mapper = 0;
474 d->m_mapper = 0;
475 }
475 }
476 }
476 }
477
477
478 QPieModelMapper* QPieSeries::modelMapper() const
478 QPieModelMapper* QPieSeries::modelMapper() const
479 {
479 {
480 Q_D(const QPieSeries);
480 Q_D(const QPieSeries);
481 return d->m_mapper;
481 return d->m_mapper;
482 }
482 }
483
483
484 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
484 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
485
485
486
486
487 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
487 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
488 QAbstractSeriesPrivate(parent),
488 QAbstractSeriesPrivate(parent),
489 m_pieRelativeHorPos(0.5),
489 m_pieRelativeHorPos(0.5),
490 m_pieRelativeVerPos(0.5),
490 m_pieRelativeVerPos(0.5),
491 m_pieRelativeSize(0.7),
491 m_pieRelativeSize(0.7),
492 m_pieStartAngle(0),
492 m_pieStartAngle(0),
493 m_pieEndAngle(360),
493 m_pieEndAngle(360),
494 m_sum(0),
494 m_sum(0),
495 m_mapper(0)
495 m_mapper(0)
496 {
496 {
497
497
498 }
498 }
499
499
500 QPieSeriesPrivate::~QPieSeriesPrivate()
500 QPieSeriesPrivate::~QPieSeriesPrivate()
501 {
501 {
502
502
503 }
503 }
504
504
505 void QPieSeriesPrivate::updateDerivativeData()
505 void QPieSeriesPrivate::updateDerivativeData()
506 {
506 {
507 m_sum = 0;
507 m_sum = 0;
508
508
509 // nothing to do?
509 // nothing to do?
510 if (m_slices.count() == 0)
510 if (m_slices.count() == 0)
511 return;
511 return;
512
512
513 // calculate sum of all slices
513 // calculate sum of all slices
514 foreach (QPieSlice* s, m_slices)
514 foreach (QPieSlice* s, m_slices)
515 m_sum += s->value();
515 m_sum += s->value();
516
516
517 // nothing to show..
517 // nothing to show..
518 if (qFuzzyIsNull(m_sum))
518 if (qFuzzyIsNull(m_sum))
519 return;
519 return;
520
520
521 // update slice attributes
521 // update slice attributes
522 qreal sliceAngle = m_pieStartAngle;
522 qreal sliceAngle = m_pieStartAngle;
523 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
523 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
524 QVector<QPieSlice*> changed;
524 QVector<QPieSlice*> changed;
525 foreach (QPieSlice* s, m_slices) {
525 foreach (QPieSlice* s, m_slices) {
526
526
527 PieSliceData data = PieSliceData::data(s);
527 PieSliceData data = PieSliceData::data(s);
528 data.m_percentage = s->value() / m_sum;
528 data.m_percentage = s->value() / m_sum;
529 data.m_angleSpan = pieSpan * data.m_percentage;
529 data.m_angleSpan = pieSpan * data.m_percentage;
530 data.m_startAngle = sliceAngle;
530 data.m_startAngle = sliceAngle;
531 sliceAngle += data.m_angleSpan;
531 sliceAngle += data.m_angleSpan;
532
532
533 if (PieSliceData::data(s) != data) {
533 if (PieSliceData::data(s) != data) {
534 PieSliceData::data(s) = data;
534 PieSliceData::data(s) = data;
535 changed << s;
535 changed << s;
536 }
536 }
537 }
537 }
538
538
539 // emit signals
539 // emit signals
540 foreach (QPieSlice* s, changed)
540 foreach (QPieSlice* s, changed)
541 PieSliceData::data(s).emitChangedSignal(s);
541 PieSliceData::data(s).emitChangedSignal(s);
542 }
542 }
543
543
544 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
544 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
545 {
545 {
546 return series.d_func();
546 return series.d_func();
547 }
547 }
548
548
549 void QPieSeriesPrivate::sliceChanged()
549 void QPieSeriesPrivate::sliceChanged()
550 {
550 {
551 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
551 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
552 updateDerivativeData();
552 updateDerivativeData();
553 }
553 }
554
554
555 void QPieSeriesPrivate::sliceClicked()
555 void QPieSeriesPrivate::sliceClicked()
556 {
556 {
557 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
557 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
558 Q_ASSERT(m_slices.contains(slice));
558 Q_ASSERT(m_slices.contains(slice));
559 Q_Q(QPieSeries);
559 Q_Q(QPieSeries);
560 emit q->clicked(slice);
560 emit q->clicked(slice);
561 }
561 }
562
562
563 void QPieSeriesPrivate::sliceHovered(bool state)
563 void QPieSeriesPrivate::sliceHovered(bool state)
564 {
564 {
565 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
565 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
566 Q_ASSERT(m_slices.contains(slice));
566 Q_ASSERT(m_slices.contains(slice));
567 Q_Q(QPieSeries);
567 Q_Q(QPieSeries);
568 emit q->hovered(slice, state);
568 emit q->hovered(slice, state);
569 }
569 }
570
570
571 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
571 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
572 {
572 {
573 if (m_mapper) {
573 if (m_mapper) {
574 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
574 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
575 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
575 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
576 if (m_mapper->orientation() == Qt::Vertical)
576 if (m_mapper->orientation() == Qt::Vertical)
577 {
577 {
578 if ( topLeft.row() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.row() < m_mapper->first() + m_mapper->count())) {
578 if ( topLeft.row() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.row() < m_mapper->first() + m_mapper->count())) {
579 if (topLeft.column() == m_mapper->mapValues())
579 if (topLeft.column() == m_mapper->mapValues())
580 m_slices.at(topLeft.row() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
580 m_slices.at(topLeft.row() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
581 if (topLeft.column() == m_mapper->mapLabels())
581 if (topLeft.column() == m_mapper->mapLabels())
582 m_slices.at(topLeft.row() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
582 m_slices.at(topLeft.row() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
583 }
583 }
584 }
584 }
585 else
585 else
586 {
586 {
587 if (topLeft.column() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.column() < m_mapper->first() + m_mapper->count())) {
587 if (topLeft.column() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.column() < m_mapper->first() + m_mapper->count())) {
588 if (topLeft.row() == m_mapper->mapValues())
588 if (topLeft.row() == m_mapper->mapValues())
589 m_slices.at(topLeft.column() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
589 m_slices.at(topLeft.column() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
590 if (topLeft.row() == m_mapper->mapLabels())
590 if (topLeft.row() == m_mapper->mapLabels())
591 m_slices.at(topLeft.column() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
591 m_slices.at(topLeft.column() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
592 }
592 }
593 }
593 }
594 }
594 }
595 }
595 }
596 }
596 }
597 }
597 }
598
598
599
599
600 void QPieSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
600 void QPieSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
601 {
601 {
602 Q_UNUSED(parent);
602 Q_UNUSED(parent);
603 if (m_mapper) {
603 if (m_mapper) {
604 if (m_mapper->orientation() == Qt::Vertical)
604 if (m_mapper->orientation() == Qt::Vertical)
605 insertData(start, end);
605 insertData(start, end);
606 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
606 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
607 initializePieFromModel();
607 initializePieFromModel();
608 }
608 }
609 }
609 }
610
610
611 void QPieSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
611 void QPieSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
612 {
612 {
613 Q_UNUSED(parent);
613 Q_UNUSED(parent);
614 if (m_mapper) {
614 if (m_mapper) {
615 if (m_mapper->orientation() == Qt::Vertical)
615 if (m_mapper->orientation() == Qt::Vertical)
616 removeData(start, end);
616 removeData(start, end);
617 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
617 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
618 initializePieFromModel();
618 initializePieFromModel();
619 }
619 }
620 }
620 }
621
621
622 void QPieSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
622 void QPieSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
623 {
623 {
624 Q_UNUSED(parent);
624 Q_UNUSED(parent);
625 if (m_mapper) {
625 if (m_mapper) {
626 if (m_mapper->orientation() == Qt::Horizontal)
626 if (m_mapper->orientation() == Qt::Horizontal)
627 insertData(start, end);
627 insertData(start, end);
628 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
628 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
629 initializePieFromModel();
629 initializePieFromModel();
630 }
630 }
631 }
631 }
632
632
633 void QPieSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
633 void QPieSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
634 {
634 {
635 Q_UNUSED(parent);
635 Q_UNUSED(parent);
636 if (m_mapper) {
636 if (m_mapper) {
637 if (m_mapper->orientation() == Qt::Horizontal)
637 if (m_mapper->orientation() == Qt::Horizontal)
638 removeData(start, end);
638 removeData(start, end);
639 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
639 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
640 initializePieFromModel();
640 initializePieFromModel();
641 }
641 }
642 }
642 }
643
643
644 void QPieSeriesPrivate::insertData(int start, int end)
644 void QPieSeriesPrivate::insertData(int start, int end)
645 {
645 {
646 Q_Q(QPieSeries);
646 Q_Q(QPieSeries);
647 if (m_mapper) {
647 if (m_mapper) {
648 if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) {
648 if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) {
649 return;
649 return;
650 } else {
650 } else {
651 int addedCount = end - start + 1;
651 int addedCount = end - start + 1;
652 if (m_mapper->count() != -1 && addedCount > m_mapper->count())
652 if (m_mapper->count() != -1 && addedCount > m_mapper->count())
653 addedCount = m_mapper->count();
653 addedCount = m_mapper->count();
654 int first = qMax(start, m_mapper->first());
654 int first = qMax(start, m_mapper->first());
655 int last = qMin(first + addedCount - 1, m_mapper->orientation() == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
655 int last = qMin(first + addedCount - 1, m_mapper->orientation() == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
656 for (int i = first; i <= last; i++) {
656 for (int i = first; i <= last; i++) {
657 QPieSlice *slice = new QPieSlice;
657 QPieSlice *slice = new QPieSlice;
658 if (m_mapper->orientation() == Qt::Vertical) {
658 if (m_mapper->orientation() == Qt::Vertical) {
659 slice->setValue(m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble());
659 slice->setValue(m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble());
660 slice->setLabel(m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString());
660 slice->setLabel(m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString());
661 } else {
661 } else {
662 slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble());
662 slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble());
663 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString());
663 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString());
664 }
664 }
665 slice->setLabelVisible();
665 slice->setLabelVisible();
666 q->insert(i - m_mapper->first(), slice);
666 q->insert(i - m_mapper->first(), slice);
667 }
667 }
668 if (m_mapper->count() != -1 && m_slices.size() > m_mapper->count())
668 if (m_mapper->count() != -1 && m_slices.size() > m_mapper->count())
669 for (int i = m_slices.size() - 1; i >= m_mapper->count(); i--)
669 for (int i = m_slices.size() - 1; i >= m_mapper->count(); i--)
670 q->remove(q->slices().at(i));
670 q->remove(q->slices().at(i));
671 }
671 }
672 }
672 }
673 }
673 }
674
674
675 void QPieSeriesPrivate::removeData(int start, int end)
675 void QPieSeriesPrivate::removeData(int start, int end)
676 {
676 {
677 Q_Q(QPieSeries);
677 Q_Q(QPieSeries);
678 if (m_mapper) {
678 if (m_mapper) {
679 int removedCount = end - start + 1;
679 int removedCount = end - start + 1;
680 if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) {
680 if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) {
681 return;
681 return;
682 } else {
682 } else {
683 int toRemove = qMin(m_slices.size(), removedCount); // first find how many items can actually be removed
683 int toRemove = qMin(m_slices.size(), removedCount); // first find how many items can actually be removed
684 int first = qMax(start, m_mapper->first()); // get the index of the first item that will be removed.
684 int first = qMax(start, m_mapper->first()); // get the index of the first item that will be removed.
685 int last = qMin(first + toRemove - 1, m_slices.size() + m_mapper->first() - 1); // get the index of the last item that will be removed.
685 int last = qMin(first + toRemove - 1, m_slices.size() + m_mapper->first() - 1); // get the index of the last item that will be removed.
686 for (int i = last; i >= first; i--)
686 for (int i = last; i >= first; i--)
687 q->remove(q->slices().at(i - m_mapper->first()));
687 q->remove(q->slices().at(i - m_mapper->first()));
688
688
689 if (m_mapper->count() != -1) {
689 if (m_mapper->count() != -1) {
690 int itemsAvailable; // check how many are available to be added
690 int itemsAvailable; // check how many are available to be added
691 if (m_mapper->orientation() == Qt::Vertical)
691 if (m_mapper->orientation() == Qt::Vertical)
692 itemsAvailable = m_model->rowCount() - m_mapper->first() - m_slices.size();
692 itemsAvailable = m_model->rowCount() - m_mapper->first() - m_slices.size();
693 else
693 else
694 itemsAvailable = m_model->columnCount() - m_mapper->first() - m_slices.size();
694 itemsAvailable = m_model->columnCount() - m_mapper->first() - m_slices.size();
695 int toBeAdded = qMin(itemsAvailable, m_mapper->count() - m_slices.size()); // add not more items than there is space left to be filled.
695 int toBeAdded = qMin(itemsAvailable, m_mapper->count() - m_slices.size()); // add not more items than there is space left to be filled.
696 int currentSize = m_slices.size();
696 int currentSize = m_slices.size();
697 if (toBeAdded > 0)
697 if (toBeAdded > 0)
698 for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) {
698 for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) {
699 QPieSlice *slice = new QPieSlice;
699 QPieSlice *slice = new QPieSlice;
700 if (m_mapper->orientation() == Qt::Vertical) {
700 if (m_mapper->orientation() == Qt::Vertical) {
701 slice->setValue(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapValues()), Qt::DisplayRole).toDouble());
701 slice->setValue(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapValues()), Qt::DisplayRole).toDouble());
702 slice->setLabel(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapLabels()), Qt::DisplayRole).toString());
702 slice->setLabel(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapLabels()), Qt::DisplayRole).toString());
703 } else {
703 } else {
704 slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i + m_mapper->first()), Qt::DisplayRole).toDouble());
704 slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i + m_mapper->first()), Qt::DisplayRole).toDouble());
705 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i + m_mapper->first()), Qt::DisplayRole).toString());
705 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i + m_mapper->first()), Qt::DisplayRole).toString());
706 }
706 }
707 slice->setLabelVisible();
707 slice->setLabelVisible();
708 q->insert(i, slice);
708 q->insert(i, slice);
709 }
709 }
710 }
710 }
711 }
711 }
712 }
712 }
713 }
713 }
714
714
715 void QPieSeriesPrivate::initializePieFromModel()
715 void QPieSeriesPrivate::initializePieFromModel()
716 {
716 {
717 Q_Q(QPieSeries);
717 Q_Q(QPieSeries);
718
718
719 // clear current content
719 // clear current content
720 q->clear();
720 q->clear();
721
721
722 if (m_model == 0 || m_mapper == 0)
722 if (m_model == 0 || m_mapper == 0)
723 return;
723 return;
724
724
725 // check if mappings are set
725 // check if mappings are set
726 if (m_mapper->mapValues() == -1 || m_mapper->mapLabels() == -1)
726 if (m_mapper->mapValues() == -1 || m_mapper->mapLabels() == -1)
727 return;
727 return;
728
728
729 // create the initial slices set
729 // create the initial slices set
730 if (m_mapper->orientation() == Qt::Vertical) {
730 if (m_mapper->orientation() == Qt::Vertical) {
731 if (m_mapper->mapValues() >= m_model->columnCount() || m_mapper->mapLabels() >= m_model->columnCount())
731 if (m_mapper->mapValues() >= m_model->columnCount() || m_mapper->mapLabels() >= m_model->columnCount())
732 return; // mapped columns are not existing
732 return; // mapped columns are not existing
733
733
734 int sliceCount = 0;
734 int sliceCount = 0;
735 if(m_mapper->count() == -1)
735 if(m_mapper->count() == -1)
736 sliceCount = m_model->rowCount() - m_mapper->first();
736 sliceCount = m_model->rowCount() - m_mapper->first();
737 else
737 else
738 sliceCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first());
738 sliceCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first());
739 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
739 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
740 q->append(m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString());
740 q->append(m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString(), m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble());
741 } else {
741 } else {
742 if (m_mapper->mapValues() >= m_model->rowCount() || m_mapper->mapLabels() >= m_model->rowCount())
742 if (m_mapper->mapValues() >= m_model->rowCount() || m_mapper->mapLabels() >= m_model->rowCount())
743 return; // mapped columns are not existing
743 return; // mapped columns are not existing
744
744
745 int sliceCount = 0;
745 int sliceCount = 0;
746 if(m_mapper->count() == -1)
746 if(m_mapper->count() == -1)
747 sliceCount = m_model->columnCount() - m_mapper->first();
747 sliceCount = m_model->columnCount() - m_mapper->first();
748 else
748 else
749 sliceCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first());
749 sliceCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first());
750 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
750 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
751 q->append(m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString());
751 q->append(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString(), m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble());
752 }
752 }
753 q->setLabelsVisible(true);
753 q->setLabelsVisible(true);
754 }
754 }
755
755
756 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
756 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
757 {
757 {
758 // Remove rounding errors
758 // Remove rounding errors
759 qreal roundedValue = newValue;
759 qreal roundedValue = newValue;
760 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
760 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
761 roundedValue = 0.0;
761 roundedValue = 0.0;
762 else if (qFuzzyCompare(newValue, max))
762 else if (qFuzzyCompare(newValue, max))
763 roundedValue = max;
763 roundedValue = max;
764 else if (qFuzzyCompare(newValue, min))
764 else if (qFuzzyCompare(newValue, min))
765 roundedValue = min;
765 roundedValue = min;
766
766
767 // Check if the position is valid after removing the rounding errors
767 // Check if the position is valid after removing the rounding errors
768 if (roundedValue < min || roundedValue > max) {
768 if (roundedValue < min || roundedValue > max) {
769 qWarning("QPieSeries: Illegal value");
769 qWarning("QPieSeries: Illegal value");
770 return false;
770 return false;
771 }
771 }
772
772
773 if (!qFuzzyIsNull(value - roundedValue)) {
773 if (!qFuzzyIsNull(value - roundedValue)) {
774 value = roundedValue;
774 value = roundedValue;
775 return true;
775 return true;
776 }
776 }
777
777
778 // The change was so small it is considered a rounding error
778 // The change was so small it is considered a rounding error
779 return false;
779 return false;
780 }
780 }
781
781
782 void QPieSeriesPrivate::scaleDomain(Domain& domain)
782 void QPieSeriesPrivate::scaleDomain(Domain& domain)
783 {
783 {
784 Q_UNUSED(domain);
784 Q_UNUSED(domain);
785 // does not apply to pie
785 // does not apply to pie
786 }
786 }
787
787
788 Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
788 Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
789 {
789 {
790 Q_Q(QPieSeries);
790 Q_Q(QPieSeries);
791 PieChartItem* pie = new PieChartItem(q,presenter);
791 PieChartItem* pie = new PieChartItem(q,presenter);
792 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
792 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
793 presenter->animator()->addAnimation(pie);
793 presenter->animator()->addAnimation(pie);
794 }
794 }
795 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
795 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
796 return pie;
796 return pie;
797 }
797 }
798
798
799 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
799 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
800 {
800 {
801 Q_Q(QPieSeries);
801 Q_Q(QPieSeries);
802 QList<LegendMarker*> markers;
802 QList<LegendMarker*> markers;
803 foreach(QPieSlice* slice, q->slices()) {
803 foreach(QPieSlice* slice, q->slices()) {
804 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
804 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
805 markers << marker;
805 markers << marker;
806 }
806 }
807 return markers;
807 return markers;
808 }
808 }
809
809
810 #include "moc_qpieseries.cpp"
810 #include "moc_qpieseries.cpp"
811 #include "moc_qpieseries_p.cpp"
811 #include "moc_qpieseries_p.cpp"
812
812
813 QTCOMMERCIALCHART_END_NAMESPACE
813 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,92 +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
20
21 #ifndef PIESERIES_H
21 #ifndef PIESERIES_H
22 #define PIESERIES_H
22 #define PIESERIES_H
23
23
24 #include <qabstractseries.h>
24 #include <qabstractseries.h>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 class QPieSeriesPrivate;
27 class QPieSeriesPrivate;
28 class QPieSlice;
28 class QPieSlice;
29 class QPieModelMapper;
29 class QPieModelMapper;
30
30
31 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
31 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34 Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition)
34 Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition)
35 Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition)
35 Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition)
36 Q_PROPERTY(qreal size READ pieSize WRITE setPieSize)
36 Q_PROPERTY(qreal size READ pieSize WRITE setPieSize)
37 Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle)
37 Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle)
38 Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle)
38 Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle)
39 Q_PROPERTY(int count READ count)
39 Q_PROPERTY(int count READ count)
40 Q_PROPERTY(QPieModelMapper *modelMapper READ modelMapper)
40 Q_PROPERTY(QPieModelMapper *modelMapper READ modelMapper)
41
41
42 public:
42 public:
43 explicit QPieSeries(QObject *parent = 0);
43 explicit QPieSeries(QObject *parent = 0);
44 virtual ~QPieSeries();
44 virtual ~QPieSeries();
45
45
46 QAbstractSeries::SeriesType type() const;
46 QAbstractSeries::SeriesType type() const;
47
47
48 bool append(QPieSlice* slice);
48 bool append(QPieSlice* slice);
49 bool append(QList<QPieSlice*> slices);
49 bool append(QList<QPieSlice*> slices);
50 QPieSeries& operator << (QPieSlice* slice);
50 QPieSeries& operator << (QPieSlice* slice);
51 QPieSlice* append(qreal value, QString name);
51 QPieSlice* append(QString label, qreal value);
52 bool insert(int index, QPieSlice* slice);
52 bool insert(int index, QPieSlice* slice);
53 bool remove(QPieSlice* slice);
53 bool remove(QPieSlice* slice);
54 void clear();
54 void clear();
55
55
56 QList<QPieSlice*> slices() const;
56 QList<QPieSlice*> slices() const;
57 int count() const;
57 int count() const;
58 bool isEmpty() const;
58 bool isEmpty() const;
59
59
60 qreal sum() const;
60 qreal sum() const;
61
61
62 void setHorizontalPosition(qreal relativePosition);
62 void setHorizontalPosition(qreal relativePosition);
63 qreal horizontalPosition() const;
63 qreal horizontalPosition() const;
64 void setVerticalPosition(qreal relativePosition);
64 void setVerticalPosition(qreal relativePosition);
65 qreal verticalPosition() const;
65 qreal verticalPosition() const;
66
66
67 void setPieSize(qreal relativeSize);
67 void setPieSize(qreal relativeSize);
68 qreal pieSize() const;
68 qreal pieSize() const;
69
69
70 void setPieStartAngle(qreal startAngle);
70 void setPieStartAngle(qreal startAngle);
71 qreal pieStartAngle() const;
71 qreal pieStartAngle() const;
72 void setPieEndAngle(qreal endAngle);
72 void setPieEndAngle(qreal endAngle);
73 qreal pieEndAngle() const;
73 qreal pieEndAngle() const;
74
74
75 void setLabelsVisible(bool visible = true);
75 void setLabelsVisible(bool visible = true);
76
76
77 void setModel(QAbstractItemModel* model);
77 void setModel(QAbstractItemModel* model);
78 void setModelMapper(QPieModelMapper *mapper);
78 void setModelMapper(QPieModelMapper *mapper);
79 QPieModelMapper* modelMapper() const;
79 QPieModelMapper* modelMapper() const;
80
80
81 Q_SIGNALS:
81 Q_SIGNALS:
82 void clicked(QPieSlice* slice);
82 void clicked(QPieSlice* slice);
83 void hovered(QPieSlice* slice, bool state);
83 void hovered(QPieSlice* slice, bool state);
84
84
85 private:
85 private:
86 Q_DECLARE_PRIVATE(QPieSeries)
86 Q_DECLARE_PRIVATE(QPieSeries)
87 Q_DISABLE_COPY(QPieSeries)
87 Q_DISABLE_COPY(QPieSeries)
88 };
88 };
89
89
90 QTCOMMERCIALCHART_END_NAMESPACE
90 QTCOMMERCIALCHART_END_NAMESPACE
91
91
92 #endif // PIESERIES_H
92 #endif // PIESERIES_H
@@ -1,411 +1,411
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 "pieslicedata_p.h"
22 #include "pieslicedata_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. Even if the theme is
34 the user is overriding the default appearance set by the theme. Even if the theme is
35 changed users settings will persist.
35 changed users settings will persist.
36
36
37 To enable user interaction customization with the slices some basic signals
37 To enable user interaction customization with the slices some basic signals
38 are provided about clicking and hovering.
38 are provided about clicking and hovering.
39 */
39 */
40
40
41 /*!
41 /*!
42 \property QPieSlice::label
42 \property QPieSlice::label
43
43
44 Label of the slice.
44 Label of the slice.
45 */
45 */
46
46
47 /*!
47 /*!
48 \property QPieSlice::value
48 \property QPieSlice::value
49
49
50 Value of the slice.
50 Value of the slice.
51
51
52 \sa percentage(), QPieSeries::sum()
52 \sa percentage(), QPieSeries::sum()
53 */
53 */
54
54
55 /*!
55 /*!
56 Constructs an empty slice with a \a parent.
56 Constructs an empty slice with a \a parent.
57
57
58 \sa QPieSeries::append(), QPieSeries::insert()
58 \sa QPieSeries::append(), QPieSeries::insert()
59 */
59 */
60 QPieSlice::QPieSlice(QObject *parent)
60 QPieSlice::QPieSlice(QObject *parent)
61 :QObject(parent),
61 :QObject(parent),
62 d(new PieSliceData())
62 d(new PieSliceData())
63 {
63 {
64
64
65 }
65 }
66
66
67 /*!
67 /*!
68 Constructs an empty slice with given \a value, \a label and a \a parent.
68 Constructs an empty slice with given \a value, \a label and a \a parent.
69 \sa QPieSeries::append(), QPieSeries::insert()
69 \sa QPieSeries::append(), QPieSeries::insert()
70 */
70 */
71 QPieSlice::QPieSlice(qreal value, QString label, QObject *parent)
71 QPieSlice::QPieSlice(QString label, qreal value, QObject *parent)
72 :QObject(parent),
72 :QObject(parent),
73 d(new PieSliceData())
73 d(new PieSliceData())
74 {
74 {
75 d->m_value = value;
75 d->m_value = value;
76 d->m_labelText = label;
76 d->m_labelText = label;
77 }
77 }
78
78
79 /*!
79 /*!
80 Destroys the slice.
80 Destroys the slice.
81 User should not delete the slice if it has been added to the series.
81 User should not delete the slice if it has been added to the series.
82 */
82 */
83 QPieSlice::~QPieSlice()
83 QPieSlice::~QPieSlice()
84 {
84 {
85 delete d;
85 delete d;
86 }
86 }
87
87
88 /*!
88 /*!
89 Gets the value of the slice.
89 Gets the value of the slice.
90 Note that all values in the series
90 Note that all values in the series
91 \sa setValue()
91 \sa setValue()
92 */
92 */
93 qreal QPieSlice::value() const
93 qreal QPieSlice::value() const
94 {
94 {
95 return d->m_value;
95 return d->m_value;
96 }
96 }
97
97
98 /*!
98 /*!
99 Gets the label of the slice.
99 Gets the label of the slice.
100 \sa setLabel()
100 \sa setLabel()
101 */
101 */
102 QString QPieSlice::label() const
102 QString QPieSlice::label() const
103 {
103 {
104 return d->m_labelText;
104 return d->m_labelText;
105 }
105 }
106
106
107 /*!
107 /*!
108 Returns true if label is set as visible.
108 Returns true if label is set as visible.
109 \sa setLabelVisible()
109 \sa setLabelVisible()
110 */
110 */
111 bool QPieSlice::isLabelVisible() const
111 bool QPieSlice::isLabelVisible() const
112 {
112 {
113 return d->m_isLabelVisible;
113 return d->m_isLabelVisible;
114 }
114 }
115
115
116 /*!
116 /*!
117 Returns true if slice is exloded from the pie.
117 Returns true if slice is exloded from the pie.
118 \sa setExploded(), explodeDistanceFactor(), setExplodeDistanceFactor()
118 \sa setExploded(), explodeDistanceFactor(), setExplodeDistanceFactor()
119 */
119 */
120 bool QPieSlice::isExploded() const
120 bool QPieSlice::isExploded() const
121 {
121 {
122 return d->m_isExploded;
122 return d->m_isExploded;
123 }
123 }
124
124
125 /*!
125 /*!
126 Returns the explode distance factor.
126 Returns the explode distance factor.
127
127
128 The factor is relative to pie radius. For example:
128 The factor is relative to pie radius. For example:
129 1.0 means the distance is the same as the radius.
129 1.0 means the distance is the same as the radius.
130 0.5 means the distance is half of the radius.
130 0.5 means the distance is half of the radius.
131
131
132 Default value is 0.15.
132 Default value is 0.15.
133
133
134 \sa setExplodeDistanceFactor(), isExploded(), setExploded()
134 \sa setExplodeDistanceFactor(), isExploded(), setExploded()
135 */
135 */
136 qreal QPieSlice::explodeDistanceFactor() const
136 qreal QPieSlice::explodeDistanceFactor() const
137 {
137 {
138 return d->m_explodeDistanceFactor;
138 return d->m_explodeDistanceFactor;
139 }
139 }
140
140
141 /*!
141 /*!
142 Returns the percentage of this slice compared to the sum of all slices in the same series.
142 Returns the percentage of this slice compared to the sum of all slices in the same series.
143 The returned value ranges from 0 to 1.0.
143 The returned value ranges from 0 to 1.0.
144
144
145 Updated internally after the slice is added to the series.
145 Updated internally after the slice is added to the series.
146
146
147 \sa QPieSeries::sum()
147 \sa QPieSeries::sum()
148 */
148 */
149 qreal QPieSlice::percentage() const
149 qreal QPieSlice::percentage() const
150 {
150 {
151 return d->m_percentage;
151 return d->m_percentage;
152 }
152 }
153
153
154 /*!
154 /*!
155 Returns the starting angle of this slice in the series it belongs to.
155 Returns the starting angle of this slice in the series it belongs to.
156
156
157 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
157 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
158
158
159 Updated internally after the slice is added to the series.
159 Updated internally after the slice is added to the series.
160 */
160 */
161 qreal QPieSlice::startAngle() const
161 qreal QPieSlice::startAngle() const
162 {
162 {
163 return d->m_startAngle;
163 return d->m_startAngle;
164 }
164 }
165
165
166 /*!
166 /*!
167 Returns the end angle of this slice in the series it belongs to.
167 Returns the end angle of this slice in the series it belongs to.
168
168
169 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
169 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
170
170
171 Updated internally after the slice is added to the series.
171 Updated internally after the slice is added to the series.
172 */
172 */
173 qreal QPieSlice::endAngle() const
173 qreal QPieSlice::endAngle() const
174 {
174 {
175 return d->m_startAngle + d->m_angleSpan;
175 return d->m_startAngle + d->m_angleSpan;
176 }
176 }
177
177
178 /*!
178 /*!
179 Returns the pen used to draw this slice.
179 Returns the pen used to draw this slice.
180 \sa setPen()
180 \sa setPen()
181 */
181 */
182 QPen QPieSlice::pen() const
182 QPen QPieSlice::pen() const
183 {
183 {
184 return d->m_slicePen;
184 return d->m_slicePen;
185 }
185 }
186
186
187 /*!
187 /*!
188 Returns the brush used to draw this slice.
188 Returns the brush used to draw this slice.
189 \sa setBrush()
189 \sa setBrush()
190 */
190 */
191 QBrush QPieSlice::brush() const
191 QBrush QPieSlice::brush() const
192 {
192 {
193 return d->m_sliceBrush;
193 return d->m_sliceBrush;
194 }
194 }
195
195
196 /*!
196 /*!
197 Returns the pen used to draw the label in this slice.
197 Returns the pen used to draw the label in this slice.
198 \sa setLabelPen()
198 \sa setLabelPen()
199 */
199 */
200 QPen QPieSlice::labelPen() const
200 QPen QPieSlice::labelPen() const
201 {
201 {
202 return d->m_labelPen;
202 return d->m_labelPen;
203 }
203 }
204
204
205 /*!
205 /*!
206 Returns the font used to draw label in this slice.
206 Returns the font used to draw label in this slice.
207 \sa setLabelFont()
207 \sa setLabelFont()
208 */
208 */
209 QFont QPieSlice::labelFont() const
209 QFont QPieSlice::labelFont() const
210 {
210 {
211 return d->m_labelFont;
211 return d->m_labelFont;
212 }
212 }
213
213
214 /*!
214 /*!
215 Gets the label arm length factor.
215 Gets the label arm length factor.
216
216
217 The factor is relative to pie radius. For example:
217 The factor is relative to pie radius. For example:
218 1.0 means the length is the same as the radius.
218 1.0 means the length is the same as the radius.
219 0.5 means the length is half of the radius.
219 0.5 means the length is half of the radius.
220
220
221 Default value is 0.15
221 Default value is 0.15
222
222
223 \sa setLabelArmLengthFactor()
223 \sa setLabelArmLengthFactor()
224 */
224 */
225 qreal QPieSlice::labelArmLengthFactor() const
225 qreal QPieSlice::labelArmLengthFactor() const
226 {
226 {
227 return d->m_labelArmLengthFactor;
227 return d->m_labelArmLengthFactor;
228 }
228 }
229
229
230 /*!
230 /*!
231 \fn void QPieSlice::clicked()
231 \fn void QPieSlice::clicked()
232
232
233 This signal is emitted when user has clicked the slice.
233 This signal is emitted when user has clicked the slice.
234
234
235 \sa QPieSeries::clicked()
235 \sa QPieSeries::clicked()
236 */
236 */
237
237
238 /*!
238 /*!
239 \fn void QPieSlice::hovered(bool state)
239 \fn void QPieSlice::hovered(bool state)
240
240
241 This signal is emitted when user has hovered over or away from the slice.
241 This signal is emitted when user has hovered over or away from the slice.
242
242
243 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
243 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
244
244
245 \sa QPieSeries::hovered()
245 \sa QPieSeries::hovered()
246 */
246 */
247
247
248 /*!
248 /*!
249 \fn void QPieSlice::changed()
249 \fn void QPieSlice::changed()
250
250
251 This signal emitted when something has changed in the slice.
251 This signal emitted when something has changed in the slice.
252 */
252 */
253
253
254 /*!
254 /*!
255 Sets the \a value of this slice.
255 Sets the \a value of this slice.
256 \sa value()
256 \sa value()
257 */
257 */
258 void QPieSlice::setValue(qreal value)
258 void QPieSlice::setValue(qreal value)
259 {
259 {
260 if (!qFuzzyIsNull(d->m_value - value)) {
260 if (!qFuzzyIsNull(d->m_value - value)) {
261 d->m_value = value;
261 d->m_value = value;
262 emit changed();
262 emit changed();
263 }
263 }
264 }
264 }
265
265
266 /*!
266 /*!
267 Sets the \a label of the slice.
267 Sets the \a label of the slice.
268 \sa label()
268 \sa label()
269 */
269 */
270 void QPieSlice::setLabel(QString label)
270 void QPieSlice::setLabel(QString label)
271 {
271 {
272 if (d->m_labelText != label) {
272 if (d->m_labelText != label) {
273 d->m_labelText = label;
273 d->m_labelText = label;
274
274
275 emit changed();
275 emit changed();
276 }
276 }
277 }
277 }
278
278
279 /*!
279 /*!
280 Sets the label \a visible in this slice.
280 Sets the label \a visible in this slice.
281 \sa isLabelVisible(), QPieSeries::setLabelsVisible()
281 \sa isLabelVisible(), QPieSeries::setLabelsVisible()
282 */
282 */
283 void QPieSlice::setLabelVisible(bool visible)
283 void QPieSlice::setLabelVisible(bool visible)
284 {
284 {
285 if (d->m_isLabelVisible != visible) {
285 if (d->m_isLabelVisible != visible) {
286 d->m_isLabelVisible = visible;
286 d->m_isLabelVisible = visible;
287 emit changed();
287 emit changed();
288 }
288 }
289 }
289 }
290
290
291 /*!
291 /*!
292 Sets this slices \a exploded state.
292 Sets this slices \a exploded state.
293
293
294 If the slice is exploded it is moved away from the pie center. The distance is defined by the explode distance factor.
294 If the slice is exploded it is moved away from the pie center. The distance is defined by the explode distance factor.
295
295
296 \sa isExploded(), explodeDistanceFactor(), setExplodeDistanceFactor()
296 \sa isExploded(), explodeDistanceFactor(), setExplodeDistanceFactor()
297 */
297 */
298 void QPieSlice::setExploded(bool exploded)
298 void QPieSlice::setExploded(bool exploded)
299 {
299 {
300 if (d->m_isExploded != exploded) {
300 if (d->m_isExploded != exploded) {
301 d->m_isExploded = exploded;
301 d->m_isExploded = exploded;
302 emit changed();
302 emit changed();
303 }
303 }
304 }
304 }
305
305
306 /*!
306 /*!
307 Sets the explode distance \a factor.
307 Sets the explode distance \a factor.
308
308
309 The factor is relative to pie radius. For example:
309 The factor is relative to pie radius. For example:
310 1.0 means the distance is the same as the radius.
310 1.0 means the distance is the same as the radius.
311 0.5 means the distance is half of the radius.
311 0.5 means the distance is half of the radius.
312
312
313 Default value is 0.15
313 Default value is 0.15
314
314
315 \sa explodeDistanceFactor(), isExploded(), setExploded()
315 \sa explodeDistanceFactor(), isExploded(), setExploded()
316 */
316 */
317 void QPieSlice::setExplodeDistanceFactor(qreal factor)
317 void QPieSlice::setExplodeDistanceFactor(qreal factor)
318 {
318 {
319 if (!qFuzzyIsNull(d->m_explodeDistanceFactor - factor)) {
319 if (!qFuzzyIsNull(d->m_explodeDistanceFactor - factor)) {
320 d->m_explodeDistanceFactor = factor;
320 d->m_explodeDistanceFactor = factor;
321 emit changed();
321 emit changed();
322 }
322 }
323 }
323 }
324
324
325 /*!
325 /*!
326 Sets the \a pen used to draw this slice.
326 Sets the \a pen used to draw this slice.
327
327
328 Overrides the pen set by the theme.
328 Overrides the pen set by the theme.
329
329
330 \sa pen()
330 \sa pen()
331 */
331 */
332 void QPieSlice::setPen(const QPen &pen)
332 void QPieSlice::setPen(const QPen &pen)
333 {
333 {
334 if (d->m_slicePen != pen) {
334 if (d->m_slicePen != pen) {
335 d->m_slicePen = pen;
335 d->m_slicePen = pen;
336 d->m_slicePen.setThemed(false);
336 d->m_slicePen.setThemed(false);
337 emit changed();
337 emit changed();
338 }
338 }
339 }
339 }
340
340
341 /*!
341 /*!
342 Sets the \a brush used to draw this slice.
342 Sets the \a brush used to draw this slice.
343
343
344 Overrides the brush set by the theme.
344 Overrides the brush set by the theme.
345
345
346 \sa brush()
346 \sa brush()
347 */
347 */
348 void QPieSlice::setBrush(const QBrush &brush)
348 void QPieSlice::setBrush(const QBrush &brush)
349 {
349 {
350 if (d->m_sliceBrush != brush) {
350 if (d->m_sliceBrush != brush) {
351 d->m_sliceBrush = brush;
351 d->m_sliceBrush = brush;
352 d->m_sliceBrush.setThemed(false);
352 d->m_sliceBrush.setThemed(false);
353 emit changed();
353 emit changed();
354 }
354 }
355 }
355 }
356
356
357 /*!
357 /*!
358 Sets the \a pen used to draw the label in this slice.
358 Sets the \a pen used to draw the label in this slice.
359
359
360 Overrides the pen set by the theme.
360 Overrides the pen set by the theme.
361
361
362 \sa labelPen()
362 \sa labelPen()
363 */
363 */
364 void QPieSlice::setLabelPen(const QPen &pen)
364 void QPieSlice::setLabelPen(const QPen &pen)
365 {
365 {
366 if (d->m_labelPen != pen) {
366 if (d->m_labelPen != pen) {
367 d->m_labelPen = pen;
367 d->m_labelPen = pen;
368 d->m_labelPen.setThemed(false);
368 d->m_labelPen.setThemed(false);
369 emit changed();
369 emit changed();
370 }
370 }
371 }
371 }
372
372
373 /*!
373 /*!
374 Sets the \a font used to draw the label in this slice.
374 Sets the \a font used to draw the label in this slice.
375
375
376 Overrides the font set by the theme.
376 Overrides the font set by the theme.
377
377
378 \sa labelFont()
378 \sa labelFont()
379 */
379 */
380 void QPieSlice::setLabelFont(const QFont &font)
380 void QPieSlice::setLabelFont(const QFont &font)
381 {
381 {
382 if (d->m_labelFont != font) {
382 if (d->m_labelFont != font) {
383 d->m_labelFont = font;
383 d->m_labelFont = font;
384 d->m_labelFont.setThemed(false);
384 d->m_labelFont.setThemed(false);
385 emit changed();
385 emit changed();
386 }
386 }
387 }
387 }
388
388
389 /*!
389 /*!
390 Sets the label arm length \a factor.
390 Sets the label arm length \a factor.
391
391
392 The factor is relative to pie radius. For example:
392 The factor is relative to pie radius. For example:
393 1.0 means the length is the same as the radius.
393 1.0 means the length is the same as the radius.
394 0.5 means the length is half of the radius.
394 0.5 means the length is half of the radius.
395
395
396 Default value is 0.15
396 Default value is 0.15
397
397
398 \sa labelArmLengthFactor()
398 \sa labelArmLengthFactor()
399 */
399 */
400 void QPieSlice::setLabelArmLengthFactor(qreal factor)
400 void QPieSlice::setLabelArmLengthFactor(qreal factor)
401 {
401 {
402 if (!qFuzzyIsNull(d->m_labelArmLengthFactor - factor)) {
402 if (!qFuzzyIsNull(d->m_labelArmLengthFactor - factor)) {
403 d->m_labelArmLengthFactor = factor;
403 d->m_labelArmLengthFactor = factor;
404 emit changed();
404 emit changed();
405 }
405 }
406 }
406 }
407
407
408 QTCOMMERCIALCHART_END_NAMESPACE
408 QTCOMMERCIALCHART_END_NAMESPACE
409
409
410 QTCOMMERCIALCHART_USE_NAMESPACE
410 QTCOMMERCIALCHART_USE_NAMESPACE
411 #include "moc_qpieslice.cpp"
411 #include "moc_qpieslice.cpp"
@@ -1,85 +1,85
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 PieSliceData;
31 class PieSliceData;
32
32
33 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
33 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(QString label READ label WRITE setLabel)
36 Q_PROPERTY(QString label READ label WRITE setLabel)
37 Q_PROPERTY(qreal value READ value WRITE setValue)
37 Q_PROPERTY(qreal value READ value WRITE setValue)
38 Q_PROPERTY(bool exploded READ isExploded WRITE setExploded)
38 Q_PROPERTY(bool exploded READ isExploded WRITE setExploded)
39
39
40 public:
40 public:
41 explicit QPieSlice(QObject *parent = 0);
41 explicit QPieSlice(QObject *parent = 0);
42 QPieSlice(qreal value, QString label, QObject *parent = 0);
42 QPieSlice(QString label, qreal value, QObject *parent = 0);
43 virtual ~QPieSlice();
43 virtual ~QPieSlice();
44
44
45 void setValue(qreal value);
45 void setValue(qreal value);
46 qreal value() const;
46 qreal value() const;
47 void setLabel(QString label);
47 void setLabel(QString label);
48 QString label() const;
48 QString label() const;
49 void setLabelVisible(bool visible = true);
49 void setLabelVisible(bool visible = true);
50 bool isLabelVisible() const;
50 bool isLabelVisible() const;
51 void setExploded(bool exploded = true);
51 void setExploded(bool exploded = true);
52 bool isExploded() const;
52 bool isExploded() const;
53
53
54 void setPen(const QPen &pen);
54 void setPen(const QPen &pen);
55 QPen pen() const;
55 QPen pen() const;
56 void setBrush(const QBrush &brush);
56 void setBrush(const QBrush &brush);
57 QBrush brush() const;
57 QBrush brush() const;
58 void setLabelPen(const QPen &pen);
58 void setLabelPen(const QPen &pen);
59 QPen labelPen() const;
59 QPen labelPen() const;
60 void setLabelFont(const QFont &font);
60 void setLabelFont(const QFont &font);
61 QFont labelFont() const;
61 QFont labelFont() const;
62
62
63 void setLabelArmLengthFactor(qreal factor);
63 void setLabelArmLengthFactor(qreal factor);
64 qreal labelArmLengthFactor() const;
64 qreal labelArmLengthFactor() const;
65 void setExplodeDistanceFactor(qreal factor);
65 void setExplodeDistanceFactor(qreal factor);
66 qreal explodeDistanceFactor() const;
66 qreal explodeDistanceFactor() const;
67
67
68 qreal percentage() const;
68 qreal percentage() const;
69 qreal startAngle() const;
69 qreal startAngle() const;
70 qreal endAngle() const;
70 qreal endAngle() const;
71
71
72 Q_SIGNALS:
72 Q_SIGNALS:
73 void clicked();
73 void clicked();
74 void hovered(bool state);
74 void hovered(bool state);
75 void changed();
75 void changed();
76
76
77 private:
77 private:
78 friend class PieSliceData;
78 friend class PieSliceData;
79 PieSliceData * const d;
79 PieSliceData * const d;
80 Q_DISABLE_COPY(QPieSlice)
80 Q_DISABLE_COPY(QPieSlice)
81 };
81 };
82
82
83 QTCOMMERCIALCHART_END_NAMESPACE
83 QTCOMMERCIALCHART_END_NAMESPACE
84
84
85 #endif // QPIESLICE_H
85 #endif // QPIESLICE_H
@@ -1,484 +1,484
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 <qchartview.h>
22 #include <qchartview.h>
23 #include <qchart.h>
23 #include <qchart.h>
24 #include <qpieseries.h>
24 #include <qpieseries.h>
25 #include <qpieslice.h>
25 #include <qpieslice.h>
26 #include <qpiemodelmapper.h>
26 #include <qpiemodelmapper.h>
27 #include <QStandardItemModel>
27 #include <QStandardItemModel>
28 #include <tst_definitions.h>
28 #include <tst_definitions.h>
29
29
30 QTCOMMERCIALCHART_USE_NAMESPACE
30 QTCOMMERCIALCHART_USE_NAMESPACE
31
31
32 Q_DECLARE_METATYPE(QPieSlice*)
32 Q_DECLARE_METATYPE(QPieSlice*)
33
33
34 class tst_qpieseries : public QObject
34 class tst_qpieseries : public QObject
35 {
35 {
36 Q_OBJECT
36 Q_OBJECT
37
37
38 public slots:
38 public slots:
39 void initTestCase();
39 void initTestCase();
40 void cleanupTestCase();
40 void cleanupTestCase();
41 void init();
41 void init();
42 void cleanup();
42 void cleanup();
43
43
44 private slots:
44 private slots:
45 void construction();
45 void construction();
46 void append();
46 void append();
47 void insert();
47 void insert();
48 void remove();
48 void remove();
49 void calculatedValues();
49 void calculatedValues();
50 void clickedSignal();
50 void clickedSignal();
51 void hoverSignal();
51 void hoverSignal();
52 void model();
52 void model();
53 void modelCustomMap();
53 void modelCustomMap();
54 void modelUpdate();
54 void modelUpdate();
55
55
56 private:
56 private:
57 void verifyCalculatedData(const QPieSeries &series, bool *ok);
57 void verifyCalculatedData(const QPieSeries &series, bool *ok);
58
58
59 private:
59 private:
60
60
61 };
61 };
62
62
63 void tst_qpieseries::initTestCase()
63 void tst_qpieseries::initTestCase()
64 {
64 {
65 qRegisterMetaType<QPieSlice*>("QPieSlice*");
65 qRegisterMetaType<QPieSlice*>("QPieSlice*");
66 }
66 }
67
67
68 void tst_qpieseries::cleanupTestCase()
68 void tst_qpieseries::cleanupTestCase()
69 {
69 {
70 }
70 }
71
71
72 void tst_qpieseries::init()
72 void tst_qpieseries::init()
73 {
73 {
74
74
75 }
75 }
76
76
77 void tst_qpieseries::cleanup()
77 void tst_qpieseries::cleanup()
78 {
78 {
79
79
80 }
80 }
81
81
82 void tst_qpieseries::construction()
82 void tst_qpieseries::construction()
83 {
83 {
84 // verify default values
84 // verify default values
85 QPieSeries s;
85 QPieSeries s;
86 QVERIFY(s.type() == QAbstractSeries::SeriesTypePie);
86 QVERIFY(s.type() == QAbstractSeries::SeriesTypePie);
87 QVERIFY(s.count() == 0);
87 QVERIFY(s.count() == 0);
88 QVERIFY(s.isEmpty());
88 QVERIFY(s.isEmpty());
89 QCOMPARE(s.sum(), 0.0);
89 QCOMPARE(s.sum(), 0.0);
90 QCOMPARE(s.horizontalPosition(), 0.5);
90 QCOMPARE(s.horizontalPosition(), 0.5);
91 QCOMPARE(s.verticalPosition(), 0.5);
91 QCOMPARE(s.verticalPosition(), 0.5);
92 QCOMPARE(s.pieSize(), 0.7);
92 QCOMPARE(s.pieSize(), 0.7);
93 QCOMPARE(s.pieStartAngle(), 0.0);
93 QCOMPARE(s.pieStartAngle(), 0.0);
94 QCOMPARE(s.pieEndAngle(), 360.0);
94 QCOMPARE(s.pieEndAngle(), 360.0);
95 }
95 }
96
96
97 void tst_qpieseries::append()
97 void tst_qpieseries::append()
98 {
98 {
99 QPieSeries s;
99 QPieSeries s;
100
100
101 // append pointer
101 // append pointer
102 QPieSlice *slice1 = 0;
102 QPieSlice *slice1 = 0;
103 QVERIFY(!s.append(slice1));
103 QVERIFY(!s.append(slice1));
104 slice1 = new QPieSlice(1, "slice 1");
104 slice1 = new QPieSlice("slice 1", 1);
105 QVERIFY(s.append(slice1));
105 QVERIFY(s.append(slice1));
106 QVERIFY(!s.append(slice1));
106 QVERIFY(!s.append(slice1));
107 QCOMPARE(s.count(), 1);
107 QCOMPARE(s.count(), 1);
108
108
109 // append pointer list
109 // append pointer list
110 QList<QPieSlice *> list;
110 QList<QPieSlice *> list;
111 QVERIFY(!s.append(list));
111 QVERIFY(!s.append(list));
112 list << (QPieSlice *) 0;
112 list << (QPieSlice *) 0;
113 QVERIFY(!s.append(list));
113 QVERIFY(!s.append(list));
114 list.clear();
114 list.clear();
115 list << new QPieSlice(2, "slice 2");
115 list << new QPieSlice("slice 2", 2);
116 list << new QPieSlice(3, "slice 3");
116 list << new QPieSlice("slice 3", 3);
117 QVERIFY(s.append(list));
117 QVERIFY(s.append(list));
118 QVERIFY(!s.append(list));
118 QVERIFY(!s.append(list));
119 QCOMPARE(s.count(), 3);
119 QCOMPARE(s.count(), 3);
120
120
121 // append operator
121 // append operator
122 s << new QPieSlice(4, "slice 4");
122 s << new QPieSlice("slice 4", 4);
123 s << slice1; // fails because already added
123 s << slice1; // fails because already added
124 QCOMPARE(s.count(), 4);
124 QCOMPARE(s.count(), 4);
125
125
126 // append with params
126 // append with params
127 QPieSlice *slice5 = s.append(5, "slice 5");
127 QPieSlice *slice5 = s.append("slice 5", 5);
128 QVERIFY(slice5 != 0);
128 QVERIFY(slice5 != 0);
129 QCOMPARE(slice5->value(), 5.0);
129 QCOMPARE(slice5->value(), 5.0);
130 QCOMPARE(slice5->label(), QString("slice 5"));
130 QCOMPARE(slice5->label(), QString("slice 5"));
131 QCOMPARE(s.count(), 5);
131 QCOMPARE(s.count(), 5);
132
132
133 // check slices
133 // check slices
134 QVERIFY(!s.isEmpty());
134 QVERIFY(!s.isEmpty());
135 for (int i=0; i<s.count(); i++) {
135 for (int i=0; i<s.count(); i++) {
136 QCOMPARE(s.slices().at(i)->value(), (qreal) i+1);
136 QCOMPARE(s.slices().at(i)->value(), (qreal) i+1);
137 QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1));
137 QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1));
138 }
138 }
139 }
139 }
140
140
141 void tst_qpieseries::insert()
141 void tst_qpieseries::insert()
142 {
142 {
143 QPieSeries s;
143 QPieSeries s;
144
144
145 // insert one slice
145 // insert one slice
146 QPieSlice *slice1 = 0;
146 QPieSlice *slice1 = 0;
147 QVERIFY(!s.insert(0, slice1));
147 QVERIFY(!s.insert(0, slice1));
148 slice1 = new QPieSlice(1, "slice 1");
148 slice1 = new QPieSlice("slice 1", 1);
149 QVERIFY(!s.insert(-1, slice1));
149 QVERIFY(!s.insert(-1, slice1));
150 QVERIFY(!s.insert(5, slice1));
150 QVERIFY(!s.insert(5, slice1));
151 QVERIFY(s.insert(0, slice1));
151 QVERIFY(s.insert(0, slice1));
152 QVERIFY(!s.insert(0, slice1));
152 QVERIFY(!s.insert(0, slice1));
153 QCOMPARE(s.count(), 1);
153 QCOMPARE(s.count(), 1);
154
154
155 // add some more slices
155 // add some more slices
156 s.append(2, "slice 2");
156 s.append("slice 2", 2);
157 s.append(4, "slice 4");
157 s.append("slice 4", 4);
158 QCOMPARE(s.count(), 3);
158 QCOMPARE(s.count(), 3);
159
159
160 // insert between slices
160 // insert between slices
161 s.insert(2, new QPieSlice(3, "slice 3"));
161 s.insert(2, new QPieSlice("slice 3", 3));
162 QCOMPARE(s.count(), 4);
162 QCOMPARE(s.count(), 4);
163
163
164 // check slices
164 // check slices
165 for (int i=0; i<s.count(); i++) {
165 for (int i=0; i<s.count(); i++) {
166 QCOMPARE(s.slices().at(i)->value(), (qreal) i+1);
166 QCOMPARE(s.slices().at(i)->value(), (qreal) i+1);
167 QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1));
167 QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1));
168 }
168 }
169 }
169 }
170
170
171 void tst_qpieseries::remove()
171 void tst_qpieseries::remove()
172 {
172 {
173 QPieSeries s;
173 QPieSeries s;
174
174
175 // add some slices
175 // add some slices
176 QPieSlice *slice1 = s.append(1, "slice 1");
176 QPieSlice *slice1 = s.append("slice 1", 1);
177 QPieSlice *slice2 = s.append(2, "slice 2");
177 QPieSlice *slice2 = s.append("slice 2", 2);
178 QPieSlice *slice3 = s.append(3, "slice 3");
178 QPieSlice *slice3 = s.append("slice 3", 3);
179 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
179 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
180 QSignalSpy spy2(slice2, SIGNAL(destroyed()));
180 QSignalSpy spy2(slice2, SIGNAL(destroyed()));
181 QSignalSpy spy3(slice3, SIGNAL(destroyed()));
181 QSignalSpy spy3(slice3, SIGNAL(destroyed()));
182 QCOMPARE(s.count(), 3);
182 QCOMPARE(s.count(), 3);
183
183
184 // null pointer remove
184 // null pointer remove
185 QVERIFY(!s.remove(0));
185 QVERIFY(!s.remove(0));
186
186
187 // remove first
187 // remove first
188 QVERIFY(s.remove(slice1));
188 QVERIFY(s.remove(slice1));
189 QVERIFY(!s.remove(slice1));
189 QVERIFY(!s.remove(slice1));
190 QCOMPARE(s.count(), 2);
190 QCOMPARE(s.count(), 2);
191 QCOMPARE(s.slices().at(0)->label(), slice2->label());
191 QCOMPARE(s.slices().at(0)->label(), slice2->label());
192
192
193 // remove all
193 // remove all
194 s.clear();
194 s.clear();
195 QVERIFY(s.isEmpty());
195 QVERIFY(s.isEmpty());
196 QVERIFY(s.slices().isEmpty());
196 QVERIFY(s.slices().isEmpty());
197 QCOMPARE(s.count(), 0);
197 QCOMPARE(s.count(), 0);
198
198
199 // check that slices were actually destroyed
199 // check that slices were actually destroyed
200 TRY_COMPARE(spy1.count(), 1);
200 TRY_COMPARE(spy1.count(), 1);
201 TRY_COMPARE(spy2.count(), 1);
201 TRY_COMPARE(spy2.count(), 1);
202 TRY_COMPARE(spy3.count(), 1);
202 TRY_COMPARE(spy3.count(), 1);
203 }
203 }
204
204
205 void tst_qpieseries::calculatedValues()
205 void tst_qpieseries::calculatedValues()
206 {
206 {
207 bool ok;
207 bool ok;
208 QPieSeries s;
208 QPieSeries s;
209
209
210 // add a slice
210 // add a slice
211 QPieSlice *slice1 = s.append(1, "slice 1");
211 QPieSlice *slice1 = s.append("slice 1", 1);
212 verifyCalculatedData(s, &ok);
212 verifyCalculatedData(s, &ok);
213 if (!ok)
213 if (!ok)
214 return;
214 return;
215
215
216 // add some more slices
216 // add some more slices
217 QList<QPieSlice *> list;
217 QList<QPieSlice *> list;
218 list << new QPieSlice(2, "slice 2");
218 list << new QPieSlice("slice 2", 2);
219 list << new QPieSlice(3, "slice 3");
219 list << new QPieSlice("slice 3", 3);
220 s.append(list);
220 s.append(list);
221 verifyCalculatedData(s, &ok);
221 verifyCalculatedData(s, &ok);
222 if (!ok)
222 if (!ok)
223 return;
223 return;
224
224
225 // remove a slice
225 // remove a slice
226 s.remove(slice1);
226 s.remove(slice1);
227 verifyCalculatedData(s, &ok);
227 verifyCalculatedData(s, &ok);
228 if (!ok)
228 if (!ok)
229 return;
229 return;
230
230
231 // insert a slice
231 // insert a slice
232 s.insert(0, new QPieSlice(1, "Slice 4"));
232 s.insert(0, new QPieSlice("Slice 4", 4));
233 verifyCalculatedData(s, &ok);
233 verifyCalculatedData(s, &ok);
234 if (!ok)
234 if (!ok)
235 return;
235 return;
236
236
237 // clear all
237 // clear all
238 s.clear();
238 s.clear();
239 verifyCalculatedData(s, &ok);
239 verifyCalculatedData(s, &ok);
240 }
240 }
241
241
242 void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok)
242 void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok)
243 {
243 {
244 *ok = false;
244 *ok = false;
245
245
246 qreal sum = 0;
246 qreal sum = 0;
247 foreach (const QPieSlice *slice, series.slices())
247 foreach (const QPieSlice *slice, series.slices())
248 sum += slice->value();
248 sum += slice->value();
249 QCOMPARE(series.sum(), sum);
249 QCOMPARE(series.sum(), sum);
250
250
251 qreal startAngle = series.pieStartAngle();
251 qreal startAngle = series.pieStartAngle();
252 qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle();
252 qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle();
253 foreach (const QPieSlice *slice, series.slices()) {
253 foreach (const QPieSlice *slice, series.slices()) {
254 qreal ratio = slice->value() / sum;
254 qreal ratio = slice->value() / sum;
255 qreal sliceSpan = pieAngleSpan * ratio;
255 qreal sliceSpan = pieAngleSpan * ratio;
256 QCOMPARE(slice->startAngle(), startAngle);
256 QCOMPARE(slice->startAngle(), startAngle);
257 QCOMPARE(slice->endAngle(), startAngle + sliceSpan);
257 QCOMPARE(slice->endAngle(), startAngle + sliceSpan);
258 QCOMPARE(slice->percentage(), ratio);
258 QCOMPARE(slice->percentage(), ratio);
259 startAngle += sliceSpan;
259 startAngle += sliceSpan;
260 }
260 }
261
261
262 if (!series.isEmpty())
262 if (!series.isEmpty())
263 QCOMPARE(series.slices().last()->endAngle(), series.pieEndAngle());
263 QCOMPARE(series.slices().last()->endAngle(), series.pieEndAngle());
264
264
265 *ok = true;
265 *ok = true;
266 }
266 }
267
267
268
268
269 void tst_qpieseries::clickedSignal()
269 void tst_qpieseries::clickedSignal()
270 {
270 {
271 // create a pie series
271 // create a pie series
272 QPieSeries *series = new QPieSeries();
272 QPieSeries *series = new QPieSeries();
273 series->setPieSize(1.0);
273 series->setPieSize(1.0);
274 QPieSlice *s1 = series->append(1, "slice 1");
274 QPieSlice *s1 = series->append("slice 1", 1);
275 series->append(2, "slice 2");
275 series->append("slice 2", 2);
276 series->append(3, "slice 3");
276 series->append("slice 3", 3);
277 QSignalSpy clickSpy1(series, SIGNAL(clicked(QPieSlice*)));
277 QSignalSpy clickSpy1(series, SIGNAL(clicked(QPieSlice*)));
278
278
279 // add series to the chart
279 // add series to the chart
280 QChartView view(new QChart());
280 QChartView view(new QChart());
281 view.chart()->legend()->setVisible(false);
281 view.chart()->legend()->setVisible(false);
282 view.resize(200, 200);
282 view.resize(200, 200);
283 view.chart()->addSeries(series);
283 view.chart()->addSeries(series);
284 view.show();
284 view.show();
285 QTest::qWaitForWindowShown(&view);
285 QTest::qWaitForWindowShown(&view);
286
286
287 // simulate clicks
287 // simulate clicks
288 // pie rectangle: QRectF(60,60 121x121)
288 // pie rectangle: QRectF(60,60 121x121)
289 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 90)); // inside slice 1
289 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 90)); // inside slice 1
290 TRY_COMPARE(clickSpy1.count(), 1);
290 TRY_COMPARE(clickSpy1.count(), 1);
291 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy1.at(0).at(0)), s1);
291 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy1.at(0).at(0)), s1);
292 }
292 }
293
293
294 void tst_qpieseries::hoverSignal()
294 void tst_qpieseries::hoverSignal()
295 {
295 {
296 // create a pie series
296 // create a pie series
297 QPieSeries *series = new QPieSeries();
297 QPieSeries *series = new QPieSeries();
298 series->setPieSize(1.0);
298 series->setPieSize(1.0);
299 QPieSlice *s1 = series->append(1, "slice 1");
299 QPieSlice *s1 = series->append("slice 1", 1);
300 series->append(2, "slice 2");
300 series->append("slice 2", 2);
301 series->append(3, "slice 3");
301 series->append("slice 3", 3);
302
302
303 // add series to the chart
303 // add series to the chart
304 QChartView view(new QChart());
304 QChartView view(new QChart());
305 view.chart()->legend()->setVisible(false);
305 view.chart()->legend()->setVisible(false);
306 view.resize(200, 200);
306 view.resize(200, 200);
307 view.chart()->addSeries(series);
307 view.chart()->addSeries(series);
308 view.show();
308 view.show();
309 QTest::qWaitForWindowShown(&view);
309 QTest::qWaitForWindowShown(&view);
310
310
311 // first move to right top corner
311 // first move to right top corner
312 QTest::mouseMove(view.viewport(), QPoint(200, 0));
312 QTest::mouseMove(view.viewport(), QPoint(200, 0));
313 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
313 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
314
314
315 // move inside the slice
315 // move inside the slice
316 // pie rectangle: QRectF(60,60 121x121)
316 // pie rectangle: QRectF(60,60 121x121)
317 QSignalSpy hoverSpy(series, SIGNAL(hovered(QPieSlice*,bool)));
317 QSignalSpy hoverSpy(series, SIGNAL(hovered(QPieSlice*,bool)));
318 QTest::mouseMove(view.viewport(), QPoint(139, 85));
318 QTest::mouseMove(view.viewport(), QPoint(139, 85));
319 TRY_COMPARE(hoverSpy.count(), 1);
319 TRY_COMPARE(hoverSpy.count(), 1);
320 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1);
320 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1);
321 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true);
321 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true);
322
322
323 // move outside the slice
323 // move outside the slice
324 QTest::mouseMove(view.viewport(), QPoint(200, 0));
324 QTest::mouseMove(view.viewport(), QPoint(200, 0));
325 TRY_COMPARE(hoverSpy.count(), 2);
325 TRY_COMPARE(hoverSpy.count(), 2);
326 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1);
326 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1);
327 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false);
327 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false);
328 }
328 }
329
329
330 void tst_qpieseries::model()
330 void tst_qpieseries::model()
331 {
331 {
332 QSKIP("Needs to be checked again. Model implementation changed", SkipAll);
332 QSKIP("Needs to be checked again. Model implementation changed", SkipAll);
333
333
334 QPieSeries *series = new QPieSeries;
334 QPieSeries *series = new QPieSeries;
335 QChart *chart = new QChart;
335 QChart *chart = new QChart;
336 chart->addSeries(series);
336 chart->addSeries(series);
337 QChartView *chartView = new QChartView(chart);
337 QChartView *chartView = new QChartView(chart);
338 chartView->show();
338 chartView->show();
339
339
340 QStandardItemModel *stdModel = new QStandardItemModel(0, 2);
340 QStandardItemModel *stdModel = new QStandardItemModel(0, 2);
341 series->setModel(stdModel);
341 series->setModel(stdModel);
342
342
343 int rowCount = 3;
343 int rowCount = 3;
344 for (int row = 0; row < rowCount; ++row) {
344 for (int row = 0; row < rowCount; ++row) {
345 for (int column = 0; column < 2; column++) {
345 for (int column = 0; column < 2; column++) {
346 QStandardItem *item = new QStandardItem(row * column);
346 QStandardItem *item = new QStandardItem(row * column);
347 stdModel->setItem(row, column, item);
347 stdModel->setItem(row, column, item);
348 }
348 }
349 }
349 }
350
350
351 // data has been added to the model, but mapper is not set the number of slices should still be 0
351 // data has been added to the model, but mapper is not set the number of slices should still be 0
352 QVERIFY2(series->slices().count() == 0, "Mapper has not been set, so the number of slices should be 0");
352 QVERIFY2(series->slices().count() == 0, "Mapper has not been set, so the number of slices should be 0");
353
353
354 // set the mapper
354 // set the mapper
355 QPieModelMapper *mapper = series->modelMapper();//new QPieModelMapper;
355 QPieModelMapper *mapper = series->modelMapper();//new QPieModelMapper;
356 mapper->setMapValues(0);
356 mapper->setMapValues(0);
357 mapper->setMapLabels(0);
357 mapper->setMapLabels(0);
358 // series->setModelMapper(mapper); // this should cause the Pie to get initialized from the model, since there is now both the model and the mapper defined
358 // series->setModelMapper(mapper); // this should cause the Pie to get initialized from the model, since there is now both the model and the mapper defined
359 QCOMPARE(series->slices().count(), rowCount);
359 QCOMPARE(series->slices().count(), rowCount);
360
360
361 // set the mappings to be outside of the model
361 // set the mappings to be outside of the model
362 mapper->setMapLabels(5);
362 mapper->setMapLabels(5);
363 mapper->setMapValues(4);
363 mapper->setMapValues(4);
364 QCOMPARE(series->slices().count(), 0); // Mappings are invalid, so the number of slices should be 0
364 QCOMPARE(series->slices().count(), 0); // Mappings are invalid, so the number of slices should be 0
365
365
366 // set back to correct ones
366 // set back to correct ones
367 mapper->setMapValues(0);
367 mapper->setMapValues(0);
368 mapper->setMapLabels(0);
368 mapper->setMapLabels(0);
369 QCOMPARE(series->slices().count(), rowCount);
369 QCOMPARE(series->slices().count(), rowCount);
370
370
371 // reset the mappings
371 // reset the mappings
372 mapper->reset();
372 mapper->reset();
373 QCOMPARE(series->slices().count(), 0); // Mappings have been reset and are invalid, so the number of slices should be 0
373 QCOMPARE(series->slices().count(), 0); // Mappings have been reset and are invalid, so the number of slices should be 0
374
374
375 // unset the model and the mapper
375 // unset the model and the mapper
376 series->setModel(0);
376 series->setModel(0);
377 // series->setModelMapper(0);
377 // series->setModelMapper(0);
378 QVERIFY(series->model() == 0); // Model should be unset
378 QVERIFY(series->model() == 0); // Model should be unset
379 // QVERIFY(series->modelMapper() == 0); // Model mapper should be unset
379 // QVERIFY(series->modelMapper() == 0); // Model mapper should be unset
380 }
380 }
381
381
382 void tst_qpieseries::modelCustomMap()
382 void tst_qpieseries::modelCustomMap()
383 {
383 {
384 QSKIP("Needs to be checked again. Model implementation changed", SkipAll);
384 QSKIP("Needs to be checked again. Model implementation changed", SkipAll);
385
385
386 int rowCount = 12;
386 int rowCount = 12;
387 int columnCount = 3;
387 int columnCount = 3;
388 QStandardItemModel *stdModel = new QStandardItemModel(0, 3);
388 QStandardItemModel *stdModel = new QStandardItemModel(0, 3);
389 for (int row = 0; row < rowCount; ++row) {
389 for (int row = 0; row < rowCount; ++row) {
390 for (int column = 0; column < 2; column++) {
390 for (int column = 0; column < 2; column++) {
391 QStandardItem *item = new QStandardItem(row * column);
391 QStandardItem *item = new QStandardItem(row * column);
392 stdModel->setItem(row, column, item);
392 stdModel->setItem(row, column, item);
393 }
393 }
394 }
394 }
395
395
396 QPieSeries *series = new QPieSeries;
396 QPieSeries *series = new QPieSeries;
397 QChart *chart = new QChart;
397 QChart *chart = new QChart;
398 chart->addSeries(series);
398 chart->addSeries(series);
399 QChartView *chartView = new QChartView(chart);
399 QChartView *chartView = new QChartView(chart);
400 chartView->show();
400 chartView->show();
401 series->setModel(stdModel);
401 series->setModel(stdModel);
402
402
403 QPieModelMapper *mapper = series->modelMapper();//new QPieModelMapper;
403 QPieModelMapper *mapper = series->modelMapper();//new QPieModelMapper;
404 mapper->setMapValues(0);
404 mapper->setMapValues(0);
405 mapper->setMapLabels(0);
405 mapper->setMapLabels(0);
406 // series->setModelMapper(mapper);
406 // series->setModelMapper(mapper);
407 QCOMPARE(series->slices().count(), rowCount);
407 QCOMPARE(series->slices().count(), rowCount);
408
408
409 // lets change the orientation to horizontal
409 // lets change the orientation to horizontal
410 mapper->setOrientation(Qt::Horizontal);
410 mapper->setOrientation(Qt::Horizontal);
411 QCOMPARE(series->slices().count(), columnCount);
411 QCOMPARE(series->slices().count(), columnCount);
412
412
413 // change it back to vertical
413 // change it back to vertical
414 mapper->setOrientation(Qt::Vertical);
414 mapper->setOrientation(Qt::Vertical);
415 QCOMPARE(series->slices().count(), rowCount);
415 QCOMPARE(series->slices().count(), rowCount);
416
416
417 // lets customize the mapping
417 // lets customize the mapping
418 int first = 3;
418 int first = 3;
419 mapper->setFirst(first);
419 mapper->setFirst(first);
420 QCOMPARE(series->slices().count(), rowCount - first);
420 QCOMPARE(series->slices().count(), rowCount - first);
421 int count = 7;
421 int count = 7;
422 mapper->setCount(count);
422 mapper->setCount(count);
423 QCOMPARE(series->slices().count(), count);
423 QCOMPARE(series->slices().count(), count);
424 first = 9;
424 first = 9;
425 mapper->setFirst(first);
425 mapper->setFirst(first);
426 QCOMPARE(series->slices().count(), qMin(count, rowCount - first));
426 QCOMPARE(series->slices().count(), qMin(count, rowCount - first));
427 }
427 }
428
428
429 void tst_qpieseries::modelUpdate()
429 void tst_qpieseries::modelUpdate()
430 {
430 {
431 QSKIP("Needs to be checked again. Model implementation changed", SkipAll);
431 QSKIP("Needs to be checked again. Model implementation changed", SkipAll);
432
432
433 int rowCount = 12;
433 int rowCount = 12;
434 int columnCount = 7;
434 int columnCount = 7;
435 QStandardItemModel *stdModel = new QStandardItemModel(rowCount, columnCount);
435 QStandardItemModel *stdModel = new QStandardItemModel(rowCount, columnCount);
436 for (int row = 0; row < rowCount; ++row) {
436 for (int row = 0; row < rowCount; ++row) {
437 for (int column = 0; column < columnCount; column++) {
437 for (int column = 0; column < columnCount; column++) {
438 QStandardItem *item = new QStandardItem(row * column);
438 QStandardItem *item = new QStandardItem(row * column);
439 stdModel->setItem(row, column, item);
439 stdModel->setItem(row, column, item);
440 }
440 }
441 }
441 }
442
442
443 QPieSeries *series = new QPieSeries;
443 QPieSeries *series = new QPieSeries;
444 QChart *chart = new QChart;
444 QChart *chart = new QChart;
445 chart->addSeries(series);
445 chart->addSeries(series);
446 QChartView *chartView = new QChartView(chart);
446 QChartView *chartView = new QChartView(chart);
447 chartView->show();
447 chartView->show();
448 series->setModel(stdModel);
448 series->setModel(stdModel);
449
449
450 QPieModelMapper *mapper = series->modelMapper();//new QPieModelMapper;
450 QPieModelMapper *mapper = series->modelMapper();//new QPieModelMapper;
451 mapper->setMapValues(0);
451 mapper->setMapValues(0);
452 mapper->setMapLabels(0);
452 mapper->setMapLabels(0);
453 // series->setModelMapper(mapper);
453 // series->setModelMapper(mapper);
454
454
455 stdModel->insertRows(3, 5);
455 stdModel->insertRows(3, 5);
456 QCOMPARE(series->slices().count(), rowCount + 5);
456 QCOMPARE(series->slices().count(), rowCount + 5);
457
457
458 stdModel->removeRows(10, 5);
458 stdModel->removeRows(10, 5);
459 QCOMPARE(series->slices().count(), rowCount);
459 QCOMPARE(series->slices().count(), rowCount);
460
460
461 // limit the number of slices taken from the model to 12
461 // limit the number of slices taken from the model to 12
462 mapper->setCount(rowCount);
462 mapper->setCount(rowCount);
463 stdModel->insertRows(3, 5);
463 stdModel->insertRows(3, 5);
464 QCOMPARE(series->slices().count(), rowCount);
464 QCOMPARE(series->slices().count(), rowCount);
465
465
466 stdModel->removeRows(0, 10);
466 stdModel->removeRows(0, 10);
467 QCOMPARE(series->slices().count(), rowCount - 5);
467 QCOMPARE(series->slices().count(), rowCount - 5);
468
468
469 // change the orientation to horizontal
469 // change the orientation to horizontal
470 mapper->setOrientation(Qt::Horizontal);
470 mapper->setOrientation(Qt::Horizontal);
471 QCOMPARE(series->slices().count(), columnCount);
471 QCOMPARE(series->slices().count(), columnCount);
472
472
473 stdModel->insertColumns(3, 10);
473 stdModel->insertColumns(3, 10);
474 QCOMPARE(series->slices().count(), rowCount); // count is limited to rowCount (12)
474 QCOMPARE(series->slices().count(), rowCount); // count is limited to rowCount (12)
475
475
476 stdModel->removeColumns(5, 10);
476 stdModel->removeColumns(5, 10);
477 QCOMPARE(series->slices().count(), columnCount);
477 QCOMPARE(series->slices().count(), columnCount);
478
478
479 }
479 }
480
480
481 QTEST_MAIN(tst_qpieseries)
481 QTEST_MAIN(tst_qpieseries)
482
482
483 #include "tst_qpieseries.moc"
483 #include "tst_qpieseries.moc"
484
484
@@ -1,270 +1,270
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.labelPen(), QPen());
82 QCOMPARE(slice1.labelPen(), QPen());
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.endAngle(), 0.0);
88 QCOMPARE(slice1.endAngle(), 0.0);
89
89
90 // value and label params
90 // value and label params
91 QPieSlice slice2(1.0, "foobar");
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.labelPen(), QPen());
98 QCOMPARE(slice2.labelPen(), QPen());
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.endAngle(), 0.0);
104 QCOMPARE(slice2.endAngle(), 0.0);
105 }
105 }
106
106
107 void tst_qpieslice::changedSignals()
107 void tst_qpieslice::changedSignals()
108 {
108 {
109 // set everything twice to see we do not get unnecessary signals
109 // set everything twice to see we do not get unnecessary signals
110 QPieSlice slice;
110 QPieSlice slice;
111 QSignalSpy spy(&slice, SIGNAL(changed())); // TODO: this will be changed to something more refined
111 QSignalSpy spy(&slice, SIGNAL(changed())); // TODO: this will be changed to something more refined
112 slice.setValue(1);
112 slice.setValue(1);
113 slice.setValue(1);
113 slice.setValue(1);
114 slice.setLabel("foobar");
114 slice.setLabel("foobar");
115 slice.setLabel("foobar");
115 slice.setLabel("foobar");
116 slice.setLabelVisible();
116 slice.setLabelVisible();
117 slice.setLabelVisible();
117 slice.setLabelVisible();
118 slice.setExploded();
118 slice.setExploded();
119 slice.setExploded();
119 slice.setExploded();
120 slice.setPen(QPen(Qt::red));
120 slice.setPen(QPen(Qt::red));
121 slice.setPen(QPen(Qt::red));
121 slice.setPen(QPen(Qt::red));
122 slice.setBrush(QBrush(Qt::red));
122 slice.setBrush(QBrush(Qt::red));
123 slice.setBrush(QBrush(Qt::red));
123 slice.setBrush(QBrush(Qt::red));
124 slice.setLabelPen(QPen(Qt::green));
124 slice.setLabelPen(QPen(Qt::green));
125 slice.setLabelPen(QPen(Qt::green));
125 slice.setLabelPen(QPen(Qt::green));
126 slice.setLabelFont(QFont("Tahoma"));
126 slice.setLabelFont(QFont("Tahoma"));
127 slice.setLabelFont(QFont("Tahoma"));
127 slice.setLabelFont(QFont("Tahoma"));
128 slice.setLabelArmLengthFactor(0.1);
128 slice.setLabelArmLengthFactor(0.1);
129 slice.setLabelArmLengthFactor(0.1);
129 slice.setLabelArmLengthFactor(0.1);
130 slice.setExplodeDistanceFactor(0.1);
130 slice.setExplodeDistanceFactor(0.1);
131 slice.setExplodeDistanceFactor(0.1);
131 slice.setExplodeDistanceFactor(0.1);
132 TRY_COMPARE(spy.count(), 10);
132 TRY_COMPARE(spy.count(), 10);
133 }
133 }
134
134
135 void tst_qpieslice::customize()
135 void tst_qpieslice::customize()
136 {
136 {
137 // create a pie series
137 // create a pie series
138 QPieSeries *series = new QPieSeries();
138 QPieSeries *series = new QPieSeries();
139 QPieSlice *s1 = series->append(1, "slice 1");
139 QPieSlice *s1 = series->append("slice 1", 1);
140 QPieSlice *s2 = series->append(2, "slice 2");
140 QPieSlice *s2 = series->append("slice 2", 2);
141 series->append(3, "slice 3");
141 series->append("slice 3", 3);
142
142
143 // customize a slice
143 // customize a slice
144 QPen p1(Qt::red);
144 QPen p1(Qt::red);
145 s1->setPen(p1);
145 s1->setPen(p1);
146 QBrush b1(Qt::red);
146 QBrush b1(Qt::red);
147 s1->setBrush(b1);
147 s1->setBrush(b1);
148 s1->setLabelPen(p1);
148 s1->setLabelPen(p1);
149 QFont f1("Consolas");
149 QFont f1("Consolas");
150 s1->setLabelFont(f1);
150 s1->setLabelFont(f1);
151
151
152 // add series to the chart
152 // add series to the chart
153 QChartView view(new QChart());
153 QChartView view(new QChart());
154 view.resize(200, 200);
154 view.resize(200, 200);
155 view.chart()->addSeries(series);
155 view.chart()->addSeries(series);
156 view.show();
156 view.show();
157 QTest::qWaitForWindowShown(&view);
157 QTest::qWaitForWindowShown(&view);
158 //QTest::qWait(1000);
158 //QTest::qWait(1000);
159
159
160 // check that customizations persist
160 // check that customizations persist
161 QCOMPARE(s1->pen(), p1);
161 QCOMPARE(s1->pen(), p1);
162 QCOMPARE(s1->brush(), b1);
162 QCOMPARE(s1->brush(), b1);
163 QCOMPARE(s1->labelPen(), p1);
163 QCOMPARE(s1->labelPen(), p1);
164 QCOMPARE(s1->labelFont(), f1);
164 QCOMPARE(s1->labelFont(), f1);
165
165
166 // remove a slice
166 // remove a slice
167 series->remove(s2);
167 series->remove(s2);
168 QCOMPARE(s1->pen(), p1);
168 QCOMPARE(s1->pen(), p1);
169 QCOMPARE(s1->brush(), b1);
169 QCOMPARE(s1->brush(), b1);
170 QCOMPARE(s1->labelPen(), p1);
170 QCOMPARE(s1->labelPen(), p1);
171 QCOMPARE(s1->labelFont(), f1);
171 QCOMPARE(s1->labelFont(), f1);
172
172
173 // add a slice
173 // add a slice
174 series->append(4, "slice 4");
174 series->append("slice 4", 4);
175 QCOMPARE(s1->pen(), p1);
175 QCOMPARE(s1->pen(), p1);
176 QCOMPARE(s1->brush(), b1);
176 QCOMPARE(s1->brush(), b1);
177 QCOMPARE(s1->labelPen(), p1);
177 QCOMPARE(s1->labelPen(), p1);
178 QCOMPARE(s1->labelFont(), f1);
178 QCOMPARE(s1->labelFont(), f1);
179
179
180 // insert a slice
180 // insert a slice
181 series->insert(0, new QPieSlice(5, "slice 5"));
181 series->insert(0, new QPieSlice("slice 5", 5));
182 QCOMPARE(s1->pen(), p1);
182 QCOMPARE(s1->pen(), p1);
183 QCOMPARE(s1->brush(), b1);
183 QCOMPARE(s1->brush(), b1);
184 QCOMPARE(s1->labelPen(), p1);
184 QCOMPARE(s1->labelPen(), p1);
185 QCOMPARE(s1->labelFont(), f1);
185 QCOMPARE(s1->labelFont(), f1);
186
186
187 // change theme
187 // change theme
188 // theme will overwrite customizations
188 // theme will overwrite customizations
189 view.chart()->setTheme(QChart::ChartThemeHighContrast);
189 view.chart()->setTheme(QChart::ChartThemeHighContrast);
190 QVERIFY(s1->pen() != p1);
190 QVERIFY(s1->pen() != p1);
191 QVERIFY(s1->brush() != b1);
191 QVERIFY(s1->brush() != b1);
192 QVERIFY(s1->labelPen() != p1);
192 QVERIFY(s1->labelPen() != p1);
193 QVERIFY(s1->labelFont() != f1);
193 QVERIFY(s1->labelFont() != f1);
194 }
194 }
195
195
196 void tst_qpieslice::mouseClick()
196 void tst_qpieslice::mouseClick()
197 {
197 {
198 // create a pie series
198 // create a pie series
199 QPieSeries *series = new QPieSeries();
199 QPieSeries *series = new QPieSeries();
200 series->setPieSize(1.0);
200 series->setPieSize(1.0);
201 QPieSlice *s1 = series->append(1, "slice 1");
201 QPieSlice *s1 = series->append("slice 1", 1);
202 QPieSlice *s2 = series->append(2, "slice 2");
202 QPieSlice *s2 = series->append("slice 2", 2);
203 QPieSlice *s3 = series->append(3, "slice 3");
203 QPieSlice *s3 = series->append("slice 3", 3);
204 QSignalSpy clickSpy1(s1, SIGNAL(clicked()));
204 QSignalSpy clickSpy1(s1, SIGNAL(clicked()));
205 QSignalSpy clickSpy2(s2, SIGNAL(clicked()));
205 QSignalSpy clickSpy2(s2, SIGNAL(clicked()));
206 QSignalSpy clickSpy3(s3, SIGNAL(clicked()));
206 QSignalSpy clickSpy3(s3, SIGNAL(clicked()));
207
207
208 // add series to the chart
208 // add series to the chart
209 QChartView view(new QChart());
209 QChartView view(new QChart());
210 view.chart()->legend()->setVisible(false);
210 view.chart()->legend()->setVisible(false);
211 view.resize(200, 200);
211 view.resize(200, 200);
212 view.chart()->addSeries(series);
212 view.chart()->addSeries(series);
213 view.show();
213 view.show();
214 QTest::qWaitForWindowShown(&view);
214 QTest::qWaitForWindowShown(&view);
215
215
216 // simulate clicks
216 // simulate clicks
217 // pie rectangle: QRectF(60,60 121x121)
217 // pie rectangle: QRectF(60,60 121x121)
218 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1
218 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1
219 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 136)); // inside slice 2
219 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 136)); // inside slice 2
220 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(91, 119)); // inside slice 3
220 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(91, 119)); // inside slice 3
221 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(70, 70)); // inside pie rectangle but not inside a slice
221 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(70, 70)); // inside pie rectangle but not inside a slice
222 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(170, 170)); // inside pie rectangle but not inside a slice
222 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(170, 170)); // inside pie rectangle but not inside a slice
223 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
223 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
224 QCOMPARE(clickSpy1.count(), 1);
224 QCOMPARE(clickSpy1.count(), 1);
225 QCOMPARE(clickSpy2.count(), 1);
225 QCOMPARE(clickSpy2.count(), 1);
226 QCOMPARE(clickSpy3.count(), 1);
226 QCOMPARE(clickSpy3.count(), 1);
227 }
227 }
228
228
229 void tst_qpieslice::mouseHover()
229 void tst_qpieslice::mouseHover()
230 {
230 {
231 // create a pie series
231 // create a pie series
232 QPieSeries *series = new QPieSeries();
232 QPieSeries *series = new QPieSeries();
233 series->setPieSize(1.0);
233 series->setPieSize(1.0);
234 QPieSlice *s1 = series->append(1, "slice 1");
234 QPieSlice *s1 = series->append("slice 1", 1);
235 series->append(2, "slice 2");
235 series->append("slice 2", 2);
236 series->append(3, "slice 3");
236 series->append("slice 3", 3);
237
237
238 // add series to the chart
238 // add series to the chart
239 QChartView view(new QChart());
239 QChartView view(new QChart());
240 view.chart()->legend()->setVisible(false);
240 view.chart()->legend()->setVisible(false);
241 view.resize(200, 200);
241 view.resize(200, 200);
242 view.chart()->addSeries(series);
242 view.chart()->addSeries(series);
243 view.show();
243 view.show();
244 QTest::qWaitForWindowShown(&view);
244 QTest::qWaitForWindowShown(&view);
245
245
246 // first move to right top corner
246 // first move to right top corner
247 QTest::mouseMove(view.viewport(), QPoint(200, 0));
247 QTest::mouseMove(view.viewport(), QPoint(200, 0));
248 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
248 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
249
249
250 // move inside slice rectangle but NOT the actual slice
250 // move inside slice rectangle but NOT the actual slice
251 // pie rectangle: QRectF(60,60 121x121)
251 // pie rectangle: QRectF(60,60 121x121)
252 QSignalSpy hoverSpy(s1, SIGNAL(hovered(bool)));
252 QSignalSpy hoverSpy(s1, SIGNAL(hovered(bool)));
253 QTest::mouseMove(view.viewport(), QPoint(170, 70));
253 QTest::mouseMove(view.viewport(), QPoint(170, 70));
254 TRY_COMPARE(hoverSpy.count(), 0);
254 TRY_COMPARE(hoverSpy.count(), 0);
255
255
256 // move inside the slice
256 // move inside the slice
257 QTest::mouseMove(view.viewport(), QPoint(139, 85));
257 QTest::mouseMove(view.viewport(), QPoint(139, 85));
258 TRY_COMPARE(hoverSpy.count(), 1);
258 TRY_COMPARE(hoverSpy.count(), 1);
259 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(0)), true);
259 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(0)), true);
260
260
261 // move outside the slice
261 // move outside the slice
262 QTest::mouseMove(view.viewport(), QPoint(200, 0));
262 QTest::mouseMove(view.viewport(), QPoint(200, 0));
263 TRY_COMPARE(hoverSpy.count(), 2);
263 TRY_COMPARE(hoverSpy.count(), 2);
264 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(0)), false);
264 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(0)), false);
265 }
265 }
266
266
267 QTEST_MAIN(tst_qpieslice)
267 QTEST_MAIN(tst_qpieslice)
268
268
269 #include "tst_qpieslice.moc"
269 #include "tst_qpieslice.moc"
270
270
@@ -1,349 +1,349
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 "mainwidget.h"
21 #include "mainwidget.h"
22 #include "dataseriedialog.h"
22 #include "dataseriedialog.h"
23 #include "qchartview.h"
23 #include "qchartview.h"
24 #include "qpieseries.h"
24 #include "qpieseries.h"
25 #include "qscatterseries.h"
25 #include "qscatterseries.h"
26 #include "qlineseries.h"
26 #include "qlineseries.h"
27 #include <qareaseries.h>
27 #include <qareaseries.h>
28 #include <qsplineseries.h>
28 #include <qsplineseries.h>
29 #include <qbarset.h>
29 #include <qbarset.h>
30 #include <qbarseries.h>
30 #include <qbarseries.h>
31 #include <qstackedbarseries.h>
31 #include <qstackedbarseries.h>
32 #include <qpercentbarseries.h>
32 #include <qpercentbarseries.h>
33 #include <QPushButton>
33 #include <QPushButton>
34 #include <QComboBox>
34 #include <QComboBox>
35 #include <QSpinBox>
35 #include <QSpinBox>
36 #include <QCheckBox>
36 #include <QCheckBox>
37 #include <QGridLayout>
37 #include <QGridLayout>
38 #include <QHBoxLayout>
38 #include <QHBoxLayout>
39 #include <QLabel>
39 #include <QLabel>
40 #include <QSpacerItem>
40 #include <QSpacerItem>
41 #include <QMessageBox>
41 #include <QMessageBox>
42 #include <cmath>
42 #include <cmath>
43 #include <QDebug>
43 #include <QDebug>
44 #include <QStandardItemModel>
44 #include <QStandardItemModel>
45
45
46
46
47 QTCOMMERCIALCHART_USE_NAMESPACE
47 QTCOMMERCIALCHART_USE_NAMESPACE
48
48
49 MainWidget::MainWidget(QWidget *parent) :
49 MainWidget::MainWidget(QWidget *parent) :
50 QWidget(parent),
50 QWidget(parent),
51 m_addSerieDialog(0),
51 m_addSerieDialog(0),
52 m_chart(0)
52 m_chart(0)
53 {
53 {
54 m_chart = new QChart();
54 m_chart = new QChart();
55
55
56 // Grid layout for the controls for configuring the chart widget
56 // Grid layout for the controls for configuring the chart widget
57 QGridLayout *grid = new QGridLayout();
57 QGridLayout *grid = new QGridLayout();
58 QPushButton *addSeriesButton = new QPushButton("Add series");
58 QPushButton *addSeriesButton = new QPushButton("Add series");
59 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
59 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
60 grid->addWidget(addSeriesButton, 0, 1);
60 grid->addWidget(addSeriesButton, 0, 1);
61 initBackroundCombo(grid);
61 initBackroundCombo(grid);
62 initScaleControls(grid);
62 initScaleControls(grid);
63 initThemeCombo(grid);
63 initThemeCombo(grid);
64 initCheckboxes(grid);
64 initCheckboxes(grid);
65
65
66 // add row with empty label to make all the other rows static
66 // add row with empty label to make all the other rows static
67 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
67 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
68 grid->setRowStretch(grid->rowCount() - 1, 1);
68 grid->setRowStretch(grid->rowCount() - 1, 1);
69
69
70 // Create chart view with the chart
70 // Create chart view with the chart
71 m_chartView = new QChartView(m_chart, this);
71 m_chartView = new QChartView(m_chart, this);
72 m_chartView->setRubberBand(QChartView::HorizonalRubberBand);
72 m_chartView->setRubberBand(QChartView::HorizonalRubberBand);
73
73
74 // Another grid layout as a main layout
74 // Another grid layout as a main layout
75 QGridLayout *mainLayout = new QGridLayout();
75 QGridLayout *mainLayout = new QGridLayout();
76 mainLayout->addLayout(grid, 0, 0);
76 mainLayout->addLayout(grid, 0, 0);
77 mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
77 mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
78 setLayout(mainLayout);
78 setLayout(mainLayout);
79 }
79 }
80
80
81 // Combo box for selecting the chart's background
81 // Combo box for selecting the chart's background
82 void MainWidget::initBackroundCombo(QGridLayout *grid)
82 void MainWidget::initBackroundCombo(QGridLayout *grid)
83 {
83 {
84 QComboBox *backgroundCombo = new QComboBox(this);
84 QComboBox *backgroundCombo = new QComboBox(this);
85 backgroundCombo->addItem("Color");
85 backgroundCombo->addItem("Color");
86 backgroundCombo->addItem("Gradient");
86 backgroundCombo->addItem("Gradient");
87 backgroundCombo->addItem("Image");
87 backgroundCombo->addItem("Image");
88 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
88 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
89 this, SLOT(backgroundChanged(int)));
89 this, SLOT(backgroundChanged(int)));
90
90
91 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
91 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
92 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
92 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
93 }
93 }
94
94
95 // Scale related controls (auto-scale vs. manual min-max values)
95 // Scale related controls (auto-scale vs. manual min-max values)
96 void MainWidget::initScaleControls(QGridLayout *grid)
96 void MainWidget::initScaleControls(QGridLayout *grid)
97 {
97 {
98 m_autoScaleCheck = new QCheckBox("Automatic scaling");
98 m_autoScaleCheck = new QCheckBox("Automatic scaling");
99 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
99 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
100 // Allow setting also non-sense values (like -2147483648 and 2147483647)
100 // Allow setting also non-sense values (like -2147483648 and 2147483647)
101 m_xMinSpin = new QSpinBox();
101 m_xMinSpin = new QSpinBox();
102 m_xMinSpin->setMinimum(INT_MIN);
102 m_xMinSpin->setMinimum(INT_MIN);
103 m_xMinSpin->setMaximum(INT_MAX);
103 m_xMinSpin->setMaximum(INT_MAX);
104 m_xMinSpin->setValue(0);
104 m_xMinSpin->setValue(0);
105 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
105 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
106 m_xMaxSpin = new QSpinBox();
106 m_xMaxSpin = new QSpinBox();
107 m_xMaxSpin->setMinimum(INT_MIN);
107 m_xMaxSpin->setMinimum(INT_MIN);
108 m_xMaxSpin->setMaximum(INT_MAX);
108 m_xMaxSpin->setMaximum(INT_MAX);
109 m_xMaxSpin->setValue(10);
109 m_xMaxSpin->setValue(10);
110 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
110 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
111 m_yMinSpin = new QSpinBox();
111 m_yMinSpin = new QSpinBox();
112 m_yMinSpin->setMinimum(INT_MIN);
112 m_yMinSpin->setMinimum(INT_MIN);
113 m_yMinSpin->setMaximum(INT_MAX);
113 m_yMinSpin->setMaximum(INT_MAX);
114 m_yMinSpin->setValue(0);
114 m_yMinSpin->setValue(0);
115 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
115 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
116 m_yMaxSpin = new QSpinBox();
116 m_yMaxSpin = new QSpinBox();
117 m_yMaxSpin->setMinimum(INT_MIN);
117 m_yMaxSpin->setMinimum(INT_MIN);
118 m_yMaxSpin->setMaximum(INT_MAX);
118 m_yMaxSpin->setMaximum(INT_MAX);
119 m_yMaxSpin->setValue(10);
119 m_yMaxSpin->setValue(10);
120 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
120 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
121
121
122 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
122 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
123 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
123 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
124 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
124 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
125 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
125 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
126 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
126 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
127 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
127 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
128 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
128 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
129 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
129 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
130 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
130 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
131
131
132 m_autoScaleCheck->setChecked(true);
132 m_autoScaleCheck->setChecked(true);
133 }
133 }
134
134
135 // Combo box for selecting theme
135 // Combo box for selecting theme
136 void MainWidget::initThemeCombo(QGridLayout *grid)
136 void MainWidget::initThemeCombo(QGridLayout *grid)
137 {
137 {
138 QComboBox *chartTheme = new QComboBox();
138 QComboBox *chartTheme = new QComboBox();
139 chartTheme->addItem("Default");
139 chartTheme->addItem("Default");
140 chartTheme->addItem("Light");
140 chartTheme->addItem("Light");
141 chartTheme->addItem("Blue Cerulean");
141 chartTheme->addItem("Blue Cerulean");
142 chartTheme->addItem("Dark");
142 chartTheme->addItem("Dark");
143 chartTheme->addItem("Brown Sand");
143 chartTheme->addItem("Brown Sand");
144 chartTheme->addItem("Blue NCS");
144 chartTheme->addItem("Blue NCS");
145 chartTheme->addItem("High Contrast");
145 chartTheme->addItem("High Contrast");
146 chartTheme->addItem("Blue Icy");
146 chartTheme->addItem("Blue Icy");
147 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
147 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
148 this, SLOT(changeChartTheme(int)));
148 this, SLOT(changeChartTheme(int)));
149 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
149 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
150 grid->addWidget(chartTheme, 8, 1);
150 grid->addWidget(chartTheme, 8, 1);
151 }
151 }
152
152
153 // Different check boxes for customizing chart
153 // Different check boxes for customizing chart
154 void MainWidget::initCheckboxes(QGridLayout *grid)
154 void MainWidget::initCheckboxes(QGridLayout *grid)
155 {
155 {
156 // TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off
156 // TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off
157 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
157 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
158 // connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool)));
158 // connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool)));
159 zoomCheckBox->setChecked(true);
159 zoomCheckBox->setChecked(true);
160 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
160 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
161
161
162 QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias");
162 QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias");
163 connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool)));
163 connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool)));
164 aliasCheckBox->setChecked(false);
164 aliasCheckBox->setChecked(false);
165 grid->addWidget(aliasCheckBox, grid->rowCount(), 0);
165 grid->addWidget(aliasCheckBox, grid->rowCount(), 0);
166 }
166 }
167
167
168 void MainWidget::antiAliasToggled(bool enabled)
168 void MainWidget::antiAliasToggled(bool enabled)
169 {
169 {
170 m_chartView->setRenderHint(QPainter::Antialiasing, enabled);
170 m_chartView->setRenderHint(QPainter::Antialiasing, enabled);
171 }
171 }
172
172
173 void MainWidget::addSeries()
173 void MainWidget::addSeries()
174 {
174 {
175 if (!m_addSerieDialog) {
175 if (!m_addSerieDialog) {
176 m_addSerieDialog = new DataSerieDialog(this);
176 m_addSerieDialog = new DataSerieDialog(this);
177 connect(m_addSerieDialog, SIGNAL(accepted(QString,int,int,QString,bool)),
177 connect(m_addSerieDialog, SIGNAL(accepted(QString,int,int,QString,bool)),
178 this, SLOT(addSeries(QString,int,int,QString,bool)));
178 this, SLOT(addSeries(QString,int,int,QString,bool)));
179 }
179 }
180 m_addSerieDialog->exec();
180 m_addSerieDialog->exec();
181 }
181 }
182
182
183 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
183 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
184 {
184 {
185 // TODO: dataCharacteristics
185 // TODO: dataCharacteristics
186 QList<RealList> testData;
186 QList<RealList> testData;
187 for (int j(0); j < columnCount; j++) {
187 for (int j(0); j < columnCount; j++) {
188 QList <qreal> newColumn;
188 QList <qreal> newColumn;
189 for (int i(0); i < rowCount; i++) {
189 for (int i(0); i < rowCount; i++) {
190 if (dataCharacteristics == "Sin") {
190 if (dataCharacteristics == "Sin") {
191 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
191 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
192 } else if (dataCharacteristics == "Sin + random") {
192 } else if (dataCharacteristics == "Sin + random") {
193 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
193 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
194 } else if (dataCharacteristics == "Random") {
194 } else if (dataCharacteristics == "Random") {
195 newColumn.append(rand() % 10 + (qreal) rand() / (qreal) RAND_MAX);
195 newColumn.append(rand() % 10 + (qreal) rand() / (qreal) RAND_MAX);
196 } else if (dataCharacteristics == "Linear") {
196 } else if (dataCharacteristics == "Linear") {
197 //newColumn.append(i * (j + 1.0));
197 //newColumn.append(i * (j + 1.0));
198 // TODO: temporary hack to make pie work; prevent zero values:
198 // TODO: temporary hack to make pie work; prevent zero values:
199 newColumn.append(i * (j + 1.0) + 0.1);
199 newColumn.append(i * (j + 1.0) + 0.1);
200 } else { // "constant"
200 } else { // "constant"
201 newColumn.append((j + 1.0));
201 newColumn.append((j + 1.0));
202 }
202 }
203 }
203 }
204 testData.append(newColumn);
204 testData.append(newColumn);
205 }
205 }
206 return testData;
206 return testData;
207 }
207 }
208
208
209 QStringList MainWidget::generateLabels(int count)
209 QStringList MainWidget::generateLabels(int count)
210 {
210 {
211 QStringList result;
211 QStringList result;
212 for (int i(0); i < count; i++)
212 for (int i(0); i < count; i++)
213 result.append("label" + QString::number(i));
213 result.append("label" + QString::number(i));
214 return result;
214 return result;
215 }
215 }
216
216
217 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
217 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
218 {
218 {
219 qDebug() << "addSeries: " << seriesName
219 qDebug() << "addSeries: " << seriesName
220 << " columnCount: " << columnCount
220 << " columnCount: " << columnCount
221 << " rowCount: " << rowCount
221 << " rowCount: " << rowCount
222 << " dataCharacteristics: " << dataCharacteristics
222 << " dataCharacteristics: " << dataCharacteristics
223 << " labels enabled: " << labelsEnabled;
223 << " labels enabled: " << labelsEnabled;
224 m_defaultSeriesName = seriesName;
224 m_defaultSeriesName = seriesName;
225
225
226 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
226 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
227
227
228 // Line series and scatter series use similar data
228 // Line series and scatter series use similar data
229 if (seriesName == "Line") {
229 if (seriesName == "Line") {
230 for (int j(0); j < data.count(); j ++) {
230 for (int j(0); j < data.count(); j ++) {
231 QList<qreal> column = data.at(j);
231 QList<qreal> column = data.at(j);
232 QLineSeries *series = new QLineSeries();
232 QLineSeries *series = new QLineSeries();
233 series->setName("line" + QString::number(j));
233 series->setName("line" + QString::number(j));
234 for (int i(0); i < column.count(); i++)
234 for (int i(0); i < column.count(); i++)
235 series->append(i, column.at(i));
235 series->append(i, column.at(i));
236 m_chart->addSeries(series);
236 m_chart->addSeries(series);
237 }
237 }
238 } else if (seriesName == "Area") {
238 } else if (seriesName == "Area") {
239 // TODO: lower series for the area?
239 // TODO: lower series for the area?
240 for (int j(0); j < data.count(); j ++) {
240 for (int j(0); j < data.count(); j ++) {
241 QList<qreal> column = data.at(j);
241 QList<qreal> column = data.at(j);
242 QLineSeries *lineSeries = new QLineSeries();
242 QLineSeries *lineSeries = new QLineSeries();
243 for (int i(0); i < column.count(); i++)
243 for (int i(0); i < column.count(); i++)
244 lineSeries->append(i, column.at(i));
244 lineSeries->append(i, column.at(i));
245 QAreaSeries *areaSeries = new QAreaSeries(lineSeries);
245 QAreaSeries *areaSeries = new QAreaSeries(lineSeries);
246 areaSeries->setName("area" + QString::number(j));
246 areaSeries->setName("area" + QString::number(j));
247 m_chart->addSeries(areaSeries);
247 m_chart->addSeries(areaSeries);
248 }
248 }
249 } else if (seriesName == "Scatter") {
249 } else if (seriesName == "Scatter") {
250 for (int j(0); j < data.count(); j++) {
250 for (int j(0); j < data.count(); j++) {
251 QList<qreal> column = data.at(j);
251 QList<qreal> column = data.at(j);
252 QScatterSeries *series = new QScatterSeries();
252 QScatterSeries *series = new QScatterSeries();
253 series->setName("scatter" + QString::number(j));
253 series->setName("scatter" + QString::number(j));
254 for (int i(0); i < column.count(); i++)
254 for (int i(0); i < column.count(); i++)
255 series->append(i, column.at(i));
255 series->append(i, column.at(i));
256 m_chart->addSeries(series);
256 m_chart->addSeries(series);
257 }
257 }
258 } else if (seriesName == "Pie") {
258 } else if (seriesName == "Pie") {
259 QStringList labels = generateLabels(rowCount);
259 QStringList labels = generateLabels(rowCount);
260 for (int j(0); j < data.count(); j++) {
260 for (int j(0); j < data.count(); j++) {
261 QPieSeries *series = new QPieSeries();
261 QPieSeries *series = new QPieSeries();
262 QList<qreal> column = data.at(j);
262 QList<qreal> column = data.at(j);
263 for (int i(0); i < column.count(); i++)
263 for (int i(0); i < column.count(); i++)
264 series->append(column.at(i), labels.at(i));
264 series->append(labels.at(i), column.at(i));
265 m_chart->addSeries(series);
265 m_chart->addSeries(series);
266 }
266 }
267 } else if (seriesName == "Bar"
267 } else if (seriesName == "Bar"
268 || seriesName == "Stacked bar"
268 || seriesName == "Stacked bar"
269 || seriesName == "Percent bar") {
269 || seriesName == "Percent bar") {
270 QStringList category;
270 QStringList category;
271 QStringList labels = generateLabels(rowCount);
271 QStringList labels = generateLabels(rowCount);
272 foreach(QString label, labels)
272 foreach(QString label, labels)
273 category << label;
273 category << label;
274 QBarSeries* series = 0;
274 QBarSeries* series = 0;
275 if (seriesName == "Bar") {
275 if (seriesName == "Bar") {
276 series = new QBarSeries(this);
276 series = new QBarSeries(this);
277 series->setCategories(category);
277 series->setCategories(category);
278 } else if (seriesName == "Stacked bar") {
278 } else if (seriesName == "Stacked bar") {
279 series = new QStackedBarSeries(this);
279 series = new QStackedBarSeries(this);
280 series->setCategories(category);
280 series->setCategories(category);
281 } else {
281 } else {
282 series = new QPercentBarSeries(this);
282 series = new QPercentBarSeries(this);
283 series->setCategories(category);
283 series->setCategories(category);
284 }
284 }
285
285
286 for (int j(0); j < data.count(); j++) {
286 for (int j(0); j < data.count(); j++) {
287 QList<qreal> column = data.at(j);
287 QList<qreal> column = data.at(j);
288 QBarSet *set = new QBarSet("set" + QString::number(j));
288 QBarSet *set = new QBarSet("set" + QString::number(j));
289 for (int i(0); i < column.count(); i++)
289 for (int i(0); i < column.count(); i++)
290 *set << column.at(i);
290 *set << column.at(i);
291 series->append(set);
291 series->append(set);
292 }
292 }
293
293
294 m_chart->addSeries(series);
294 m_chart->addSeries(series);
295 } else if (seriesName == "Spline") {
295 } else if (seriesName == "Spline") {
296 for (int j(0); j < data.count(); j ++) {
296 for (int j(0); j < data.count(); j ++) {
297 QList<qreal> column = data.at(j);
297 QList<qreal> column = data.at(j);
298 QSplineSeries *series = new QSplineSeries();
298 QSplineSeries *series = new QSplineSeries();
299 for (int i(0); i < column.count(); i++)
299 for (int i(0); i < column.count(); i++)
300 series->append(i, column.at(i));
300 series->append(i, column.at(i));
301 m_chart->addSeries(series);
301 m_chart->addSeries(series);
302 }
302 }
303 }
303 }
304 }
304 }
305
305
306 void MainWidget::backgroundChanged(int itemIndex)
306 void MainWidget::backgroundChanged(int itemIndex)
307 {
307 {
308 qDebug() << "backgroundChanged: " << itemIndex;
308 qDebug() << "backgroundChanged: " << itemIndex;
309 }
309 }
310
310
311 void MainWidget::autoScaleChanged(int value)
311 void MainWidget::autoScaleChanged(int value)
312 {
312 {
313 if (value) {
313 if (value) {
314 // TODO: enable auto scaling
314 // TODO: enable auto scaling
315 } else {
315 } else {
316 // TODO: set scaling manually (and disable auto scaling)
316 // TODO: set scaling manually (and disable auto scaling)
317 }
317 }
318
318
319 m_xMinSpin->setEnabled(!value);
319 m_xMinSpin->setEnabled(!value);
320 m_xMaxSpin->setEnabled(!value);
320 m_xMaxSpin->setEnabled(!value);
321 m_yMinSpin->setEnabled(!value);
321 m_yMinSpin->setEnabled(!value);
322 m_yMaxSpin->setEnabled(!value);
322 m_yMaxSpin->setEnabled(!value);
323 }
323 }
324
324
325 void MainWidget::xMinChanged(int value)
325 void MainWidget::xMinChanged(int value)
326 {
326 {
327 qDebug() << "xMinChanged: " << value;
327 qDebug() << "xMinChanged: " << value;
328 }
328 }
329
329
330 void MainWidget::xMaxChanged(int value)
330 void MainWidget::xMaxChanged(int value)
331 {
331 {
332 qDebug() << "xMaxChanged: " << value;
332 qDebug() << "xMaxChanged: " << value;
333 }
333 }
334
334
335 void MainWidget::yMinChanged(int value)
335 void MainWidget::yMinChanged(int value)
336 {
336 {
337 qDebug() << "yMinChanged: " << value;
337 qDebug() << "yMinChanged: " << value;
338 }
338 }
339
339
340 void MainWidget::yMaxChanged(int value)
340 void MainWidget::yMaxChanged(int value)
341 {
341 {
342 qDebug() << "yMaxChanged: " << value;
342 qDebug() << "yMaxChanged: " << value;
343 }
343 }
344
344
345 void MainWidget::changeChartTheme(int themeIndex)
345 void MainWidget::changeChartTheme(int themeIndex)
346 {
346 {
347 qDebug() << "changeChartTheme: " << themeIndex;
347 qDebug() << "changeChartTheme: " << themeIndex;
348 m_chart->setTheme((QChart::ChartTheme) themeIndex);
348 m_chart->setTheme((QChart::ChartTheme) themeIndex);
349 }
349 }
General Comments 0
You need to be logged in to leave comments. Login now