##// END OF EJS Templates
Renamed appendBarSet to append, removeBarSet to remove
sauimone -
r1194:d962cd819f79
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->appendBarSet(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.first.y(), data.second);
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,89 +1,89
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 <QBarSeries>
24 #include <QBarSeries>
25 #include <QBarSet>
25 #include <QBarSet>
26 #include <QLegend>
26 #include <QLegend>
27
27
28 QTCOMMERCIALCHART_USE_NAMESPACE
28 QTCOMMERCIALCHART_USE_NAMESPACE
29
29
30 int main(int argc, char *argv[])
30 int main(int argc, char *argv[])
31 {
31 {
32 QApplication a(argc, argv);
32 QApplication a(argc, argv);
33
33
34 //![1]
34 //![1]
35 QBarCategories categories;
35 QBarCategories categories;
36 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
36 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
37 //![1]
37 //![1]
38
38
39 //![2]
39 //![2]
40 QBarSet *set0 = new QBarSet("Jane");
40 QBarSet *set0 = new QBarSet("Jane");
41 QBarSet *set1 = new QBarSet("John");
41 QBarSet *set1 = new QBarSet("John");
42 QBarSet *set2 = new QBarSet("Axel");
42 QBarSet *set2 = new QBarSet("Axel");
43 QBarSet *set3 = new QBarSet("Mary");
43 QBarSet *set3 = new QBarSet("Mary");
44 QBarSet *set4 = new QBarSet("Samantha");
44 QBarSet *set4 = new QBarSet("Samantha");
45
45
46 *set0 << QPointF(0.0, 1) << QPointF(1.0, 2) << QPointF(2.4, 3) << QPointF(3.0, 4) << QPointF(4.0, 5) << QPointF(5.0, 6);
46 *set0 << QPointF(0.0, 1) << QPointF(1.0, 2) << QPointF(2.4, 3) << QPointF(3.0, 4) << QPointF(4.0, 5) << QPointF(5.0, 6);
47 *set1 << QPointF(0.1, 2) << QPointF(1.2, 3) << QPointF(2.45, 4) << QPointF(3.2, 5) << QPointF(4.2, 6) << QPointF(5.2, 7);
47 *set1 << QPointF(0.1, 2) << QPointF(1.2, 3) << QPointF(2.45, 4) << QPointF(3.2, 5) << QPointF(4.2, 6) << QPointF(5.2, 7);
48 *set2 << QPointF(0.2, 3) << QPointF(1.4, 4) << QPointF(2.50, 5) << QPointF(3.4, 6) << QPointF(4.4, 7) << QPointF(5.4, 8);
48 *set2 << QPointF(0.2, 3) << QPointF(1.4, 4) << QPointF(2.50, 5) << QPointF(3.4, 6) << QPointF(4.4, 7) << QPointF(5.4, 8);
49 *set3 << QPointF(0.3, 4) << QPointF(1.6, 5) << QPointF(2.55, 6) << QPointF(3.6, 7) << QPointF(4.6, 8) << QPointF(5.6, 9);
49 *set3 << QPointF(0.3, 4) << QPointF(1.6, 5) << QPointF(2.55, 6) << QPointF(3.6, 7) << QPointF(4.6, 8) << QPointF(5.6, 9);
50 *set4 << QPointF(0.4, 5) << QPointF(1.8, 6) << QPointF(2.6, 7) << QPointF(3.8, 8) << QPointF(4.8, 9) << QPointF(5.8, 10);
50 *set4 << QPointF(0.4, 5) << QPointF(1.8, 6) << QPointF(2.6, 7) << QPointF(3.8, 8) << QPointF(4.8, 9) << QPointF(5.8, 10);
51 //![2]
51 //![2]
52
52
53 //![3]
53 //![3]
54 QBarSeries* series = new QBarSeries();
54 QBarSeries* series = new QBarSeries();
55 series->setCategories(categories);
55 series->setCategories(categories);
56 series->appendBarSet(set0);
56 series->append(set0);
57 series->appendBarSet(set1);
57 series->append(set1);
58 series->appendBarSet(set2);
58 series->append(set2);
59 series->appendBarSet(set3);
59 series->append(set3);
60 series->appendBarSet(set4);
60 series->append(set4);
61
61
62 //![3]
62 //![3]
63
63
64 //![4]
64 //![4]
65 QChart* chart = new QChart();
65 QChart* chart = new QChart();
66 chart->addSeries(series);
66 chart->addSeries(series);
67 chart->setTitle("Simple barchart example");
67 chart->setTitle("Simple barchart example");
68 //![4]
68 //![4]
69
69
70 //![5]
70 //![5]
71 chart->legend()->setVisible(true);
71 chart->legend()->setVisible(true);
72 chart->legend()->setAlignment(QLegend::AlignmentBottom);
72 chart->legend()->setAlignment(QLegend::AlignmentBottom);
73 chart->axisY()->setNiceNumbersEnabled(true);
73 chart->axisY()->setNiceNumbersEnabled(true);
74 //![5]
74 //![5]
75
75
76 //![6]
76 //![6]
77 QChartView* chartView = new QChartView(chart);
77 QChartView* chartView = new QChartView(chart);
78 chartView->setRenderHint(QPainter::Antialiasing);
78 chartView->setRenderHint(QPainter::Antialiasing);
79 //![6]
79 //![6]
80
80
81 //![7]
81 //![7]
82 QMainWindow window;
82 QMainWindow window;
83 window.setCentralWidget(chartView);
83 window.setCentralWidget(chartView);
84 window.resize(400, 300);
84 window.resize(400, 300);
85 window.show();
85 window.show();
86 //![7]
86 //![7]
87
87
88 return a.exec();
88 return a.exec();
89 }
89 }
@@ -1,89 +1,89
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 <QGroupedBarSeries>
24 #include <QGroupedBarSeries>
25 #include <QBarSet>
25 #include <QBarSet>
26 #include <QLegend>
26 #include <QLegend>
27
27
28 QTCOMMERCIALCHART_USE_NAMESPACE
28 QTCOMMERCIALCHART_USE_NAMESPACE
29
29
30 int main(int argc, char *argv[])
30 int main(int argc, char *argv[])
31 {
31 {
32 QApplication a(argc, argv);
32 QApplication a(argc, argv);
33
33
34 //![1]
34 //![1]
35 QBarCategories categories;
35 QBarCategories categories;
36 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
36 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
37 //![1]
37 //![1]
38
38
39 //![2]
39 //![2]
40 QBarSet *set0 = new QBarSet("Jane");
40 QBarSet *set0 = new QBarSet("Jane");
41 QBarSet *set1 = new QBarSet("John");
41 QBarSet *set1 = new QBarSet("John");
42 QBarSet *set2 = new QBarSet("Axel");
42 QBarSet *set2 = new QBarSet("Axel");
43 QBarSet *set3 = new QBarSet("Mary");
43 QBarSet *set3 = new QBarSet("Mary");
44 QBarSet *set4 = new QBarSet("Samantha");
44 QBarSet *set4 = new QBarSet("Samantha");
45
45
46 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
46 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
47 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
47 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
48 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
48 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
49 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
49 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
50 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
50 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
51 //![2]
51 //![2]
52
52
53 //![3]
53 //![3]
54 QGroupedBarSeries* series = new QGroupedBarSeries();
54 QGroupedBarSeries* series = new QGroupedBarSeries();
55 series->setCategories(categories);
55 series->setCategories(categories);
56 series->appendBarSet(set0);
56 series->append(set0);
57 series->appendBarSet(set1);
57 series->append(set1);
58 series->appendBarSet(set2);
58 series->append(set2);
59 series->appendBarSet(set3);
59 series->append(set3);
60 series->appendBarSet(set4);
60 series->append(set4);
61
61
62 //![3]
62 //![3]
63
63
64 //![4]
64 //![4]
65 QChart* chart = new QChart();
65 QChart* chart = new QChart();
66 chart->addSeries(series);
66 chart->addSeries(series);
67 chart->setTitle("Simple grouped barchart example");
67 chart->setTitle("Simple grouped barchart example");
68 //![4]
68 //![4]
69
69
70 //![5]
70 //![5]
71 chart->legend()->setVisible(true);
71 chart->legend()->setVisible(true);
72 chart->legend()->setAlignment(QLegend::AlignmentBottom);
72 chart->legend()->setAlignment(QLegend::AlignmentBottom);
73 chart->axisY()->setNiceNumbersEnabled(true);
73 chart->axisY()->setNiceNumbersEnabled(true);
74 //![5]
74 //![5]
75
75
76 //![6]
76 //![6]
77 QChartView* chartView = new QChartView(chart);
77 QChartView* chartView = new QChartView(chart);
78 chartView->setRenderHint(QPainter::Antialiasing);
78 chartView->setRenderHint(QPainter::Antialiasing);
79 //![6]
79 //![6]
80
80
81 //![7]
81 //![7]
82 QMainWindow window;
82 QMainWindow window;
83 window.setCentralWidget(chartView);
83 window.setCentralWidget(chartView);
84 window.resize(400, 300);
84 window.resize(400, 300);
85 window.show();
85 window.show();
86 //![7]
86 //![7]
87
87
88 return a.exec();
88 return a.exec();
89 }
89 }
@@ -1,88 +1,88
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QApplication>
21 #include <QApplication>
22 #include <QMainWindow>
22 #include <QMainWindow>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QPercentBarSeries>
24 #include <QPercentBarSeries>
25 #include <QBarSet>
25 #include <QBarSet>
26 #include <QLegend>
26 #include <QLegend>
27
27
28 QTCOMMERCIALCHART_USE_NAMESPACE
28 QTCOMMERCIALCHART_USE_NAMESPACE
29
29
30 int main(int argc, char *argv[])
30 int main(int argc, char *argv[])
31 {
31 {
32 QApplication a(argc, argv);
32 QApplication a(argc, argv);
33
33
34 //![1]
34 //![1]
35 QBarCategories categories;
35 QBarCategories categories;
36 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
36 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
37 //![1]
37 //![1]
38
38
39 //![2]
39 //![2]
40 QBarSet *set0 = new QBarSet("Jane");
40 QBarSet *set0 = new QBarSet("Jane");
41 QBarSet *set1 = new QBarSet("John");
41 QBarSet *set1 = new QBarSet("John");
42 QBarSet *set2 = new QBarSet("Axel");
42 QBarSet *set2 = new QBarSet("Axel");
43 QBarSet *set3 = new QBarSet("Mary");
43 QBarSet *set3 = new QBarSet("Mary");
44 QBarSet *set4 = new QBarSet("Samantha");
44 QBarSet *set4 = new QBarSet("Samantha");
45
45
46 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
46 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
47 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
47 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
48 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
48 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
49 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
49 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
50 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
50 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
51 //![2]
51 //![2]
52
52
53 //![3]
53 //![3]
54 QPercentBarSeries* series = new QPercentBarSeries();
54 QPercentBarSeries* series = new QPercentBarSeries();
55 series->setCategories(categories);
55 series->setCategories(categories);
56 series->appendBarSet(set0);
56 series->append(set0);
57 series->appendBarSet(set1);
57 series->append(set1);
58 series->appendBarSet(set2);
58 series->append(set2);
59 series->appendBarSet(set3);
59 series->append(set3);
60 series->appendBarSet(set4);
60 series->append(set4);
61 //![3]
61 //![3]
62
62
63 //![4]
63 //![4]
64 QChart* chart = new QChart();
64 QChart* chart = new QChart();
65 chart->addSeries(series);
65 chart->addSeries(series);
66 chart->setTitle("Simple percentbarchart example");
66 chart->setTitle("Simple percentbarchart example");
67 //![4]
67 //![4]
68
68
69 //![5]
69 //![5]
70 chart->legend()->setVisible(true);
70 chart->legend()->setVisible(true);
71 chart->legend()->setAlignment(QLegend::AlignmentBottom);
71 chart->legend()->setAlignment(QLegend::AlignmentBottom);
72 //![5]
72 //![5]
73
73
74 //![6]
74 //![6]
75 QChartView* chartView = new QChartView(chart);
75 QChartView* chartView = new QChartView(chart);
76 chartView->setRenderHint(QPainter::Antialiasing);
76 chartView->setRenderHint(QPainter::Antialiasing);
77 //![6]
77 //![6]
78
78
79 //![7]
79 //![7]
80 QMainWindow window;
80 QMainWindow window;
81 window.setCentralWidget(chartView);
81 window.setCentralWidget(chartView);
82 window.resize(400, 300);
82 window.resize(400, 300);
83 window.show();
83 window.show();
84 //![7]
84 //![7]
85
85
86 return a.exec();
86 return a.exec();
87 }
87 }
88
88
@@ -1,89 +1,89
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 <QStackedBarSeries>
24 #include <QStackedBarSeries>
25 #include <QBarSet>
25 #include <QBarSet>
26 #include <QLegend>
26 #include <QLegend>
27
27
28 QTCOMMERCIALCHART_USE_NAMESPACE
28 QTCOMMERCIALCHART_USE_NAMESPACE
29
29
30 int main(int argc, char *argv[])
30 int main(int argc, char *argv[])
31 {
31 {
32 QApplication a(argc, argv);
32 QApplication a(argc, argv);
33
33
34 //![1]
34 //![1]
35 QBarCategories categories;
35 QBarCategories categories;
36 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
36 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
37 //![1]
37 //![1]
38
38
39 //![2]
39 //![2]
40 QBarSet *set0 = new QBarSet("Jane");
40 QBarSet *set0 = new QBarSet("Jane");
41 QBarSet *set1 = new QBarSet("John");
41 QBarSet *set1 = new QBarSet("John");
42 QBarSet *set2 = new QBarSet("Axel");
42 QBarSet *set2 = new QBarSet("Axel");
43 QBarSet *set3 = new QBarSet("Mary");
43 QBarSet *set3 = new QBarSet("Mary");
44 QBarSet *set4 = new QBarSet("Samantha");
44 QBarSet *set4 = new QBarSet("Samantha");
45
45
46 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
46 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
47 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
47 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
48 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
48 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
49 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
49 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
50 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
50 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
51 //![2]
51 //![2]
52
52
53 //![3]
53 //![3]
54 QStackedBarSeries* series = new QStackedBarSeries();
54 QStackedBarSeries* series = new QStackedBarSeries();
55 series->setCategories(categories);
55 series->setCategories(categories);
56 series->appendBarSet(set0);
56 series->append(set0);
57 series->appendBarSet(set1);
57 series->append(set1);
58 series->appendBarSet(set2);
58 series->append(set2);
59 series->appendBarSet(set3);
59 series->append(set3);
60 series->appendBarSet(set4);
60 series->append(set4);
61 //![3]
61 //![3]
62
62
63 //![4]
63 //![4]
64 QChart* chart = new QChart();
64 QChart* chart = new QChart();
65 chart->addSeries(series);
65 chart->addSeries(series);
66 chart->setTitle("Simple stackedbarchart example");
66 chart->setTitle("Simple stackedbarchart example");
67 //![4]
67 //![4]
68
68
69 //![5]
69 //![5]
70 chart->legend()->setVisible(true);
70 chart->legend()->setVisible(true);
71 chart->legend()->setAlignment(QLegend::AlignmentBottom);
71 chart->legend()->setAlignment(QLegend::AlignmentBottom);
72 chart->axisY()->setNiceNumbersEnabled(true);
72 chart->axisY()->setNiceNumbersEnabled(true);
73 //![5]
73 //![5]
74
74
75 //![6]
75 //![6]
76 QChartView* chartView = new QChartView(chart);
76 QChartView* chartView = new QChartView(chart);
77 chartView->setRenderHint(QPainter::Antialiasing);
77 chartView->setRenderHint(QPainter::Antialiasing);
78 //![6]
78 //![6]
79
79
80 //![7]
80 //![7]
81 QMainWindow window;
81 QMainWindow window;
82 window.setCentralWidget(chartView);
82 window.setCentralWidget(chartView);
83 window.resize(400, 300);
83 window.resize(400, 300);
84 window.show();
84 window.show();
85 //![7]
85 //![7]
86
86
87 return a.exec();
87 return a.exec();
88 }
88 }
89
89
@@ -1,116 +1,116
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 <QtGui/QApplication>
21 #include <QtGui/QApplication>
22 #include <QMainWindow>
22 #include <QMainWindow>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QBarSet>
24 #include <QBarSet>
25 #include <QLegend>
25 #include <QLegend>
26 #include "drilldownseries.h"
26 #include "drilldownseries.h"
27 #include "drilldownchart.h"
27 #include "drilldownchart.h"
28
28
29 QTCOMMERCIALCHART_USE_NAMESPACE
29 QTCOMMERCIALCHART_USE_NAMESPACE
30
30
31 int main(int argc, char *argv[])
31 int main(int argc, char *argv[])
32 {
32 {
33 //! [1]
33 //! [1]
34 QApplication a(argc, argv);
34 QApplication a(argc, argv);
35 QMainWindow window;
35 QMainWindow window;
36 //! [1]
36 //! [1]
37
37
38 //! [2]
38 //! [2]
39 DrilldownChart* drilldownChart = new DrilldownChart();
39 DrilldownChart* drilldownChart = new DrilldownChart();
40 drilldownChart->setTheme(QChart::ChartThemeBlueIcy);
40 drilldownChart->setTheme(QChart::ChartThemeBlueIcy);
41 drilldownChart->setAnimationOptions(QChart::SeriesAnimations);
41 drilldownChart->setAnimationOptions(QChart::SeriesAnimations);
42 //! [2]
42 //! [2]
43
43
44 //! [3]
44 //! [3]
45 // Define categories
45 // Define categories
46 QStringList months;
46 QStringList months;
47 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
47 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
48 QStringList weeks;
48 QStringList weeks;
49 weeks << "week 1" << "week 2" << "week 3" << "week 4";
49 weeks << "week 1" << "week 2" << "week 3" << "week 4";
50 QStringList plants;
50 QStringList plants;
51 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
51 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
52 //! [3]
52 //! [3]
53
53
54 //! [4]
54 //! [4]
55 // Create drilldown structure
55 // Create drilldown structure
56 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
56 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
57 seasonSeries->setName("Crop by month - Season");
57 seasonSeries->setName("Crop by month - Season");
58
58
59 // Each month in season series has drilldown series for weekly data
59 // Each month in season series has drilldown series for weekly data
60 foreach (QString month, months) {
60 foreach (QString month, months) {
61
61
62 // Create drilldown series for every week
62 // Create drilldown series for every week
63 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
63 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
64 seasonSeries->mapDrilldownSeries(month, weeklySeries);
64 seasonSeries->mapDrilldownSeries(month, weeklySeries);
65
65
66 // Drilling down from weekly data brings us back to season data.
66 // Drilling down from weekly data brings us back to season data.
67 foreach (QString week, weeks) {
67 foreach (QString week, weeks) {
68 weeklySeries->mapDrilldownSeries(week, seasonSeries);
68 weeklySeries->mapDrilldownSeries(week, seasonSeries);
69 weeklySeries->setName(QString("Crop by week - " + month));
69 weeklySeries->setName(QString("Crop by week - " + month));
70 }
70 }
71
71
72 // Use right click signal to implement drilldown
72 // Use right click signal to implement drilldown
73 QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,QString)), drilldownChart, SLOT(handleClicked(QBarSet*,QString)));
73 QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,QString)), drilldownChart, SLOT(handleClicked(QBarSet*,QString)));
74 }
74 }
75
75
76 // Enable drilldown from season series using right click.
76 // Enable drilldown from season series using right click.
77 QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,QString)), drilldownChart, SLOT(handleClicked(QBarSet*,QString)));
77 QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,QString)), drilldownChart, SLOT(handleClicked(QBarSet*,QString)));
78 //! [4]
78 //! [4]
79
79
80 //! [5]
80 //! [5]
81 // Fill monthly and weekly series with data
81 // Fill monthly and weekly series with data
82 foreach (QString plant, plants) {
82 foreach (QString plant, plants) {
83 QBarSet* monthlyCrop = new QBarSet(plant);
83 QBarSet* monthlyCrop = new QBarSet(plant);
84 foreach (QString month, months) {
84 foreach (QString month, months) {
85 QBarSet* weeklyCrop = new QBarSet(plant);
85 QBarSet* weeklyCrop = new QBarSet(plant);
86 foreach (QString week, weeks )
86 foreach (QString week, weeks )
87 *weeklyCrop << (qrand() % 20);
87 *weeklyCrop << (qrand() % 20);
88 // Get the drilldown series from season series and add crop to it.
88 // Get the drilldown series from season series and add crop to it.
89 seasonSeries->drilldownSeries(month)->appendBarSet(weeklyCrop);
89 seasonSeries->drilldownSeries(month)->append(weeklyCrop);
90 *monthlyCrop << weeklyCrop->sum();
90 *monthlyCrop << weeklyCrop->sum();
91 }
91 }
92 seasonSeries->appendBarSet(monthlyCrop);
92 seasonSeries->append(monthlyCrop);
93 }
93 }
94 //! [5]
94 //! [5]
95
95
96 //! [6]
96 //! [6]
97 // Show season series in initial view
97 // Show season series in initial view
98 drilldownChart->changeSeries(seasonSeries);
98 drilldownChart->changeSeries(seasonSeries);
99 drilldownChart->setTitle(seasonSeries->name());
99 drilldownChart->setTitle(seasonSeries->name());
100 //! [6]
100 //! [6]
101
101
102 //! [7]
102 //! [7]
103 drilldownChart->axisX()->setGridLineVisible(false);
103 drilldownChart->axisX()->setGridLineVisible(false);
104 drilldownChart->axisY()->setNiceNumbersEnabled(true);
104 drilldownChart->axisY()->setNiceNumbersEnabled(true);
105 drilldownChart->legend()->setVisible(true);
105 drilldownChart->legend()->setVisible(true);
106 drilldownChart->legend()->setAlignment(QLegend::AlignmentBottom);
106 drilldownChart->legend()->setAlignment(QLegend::AlignmentBottom);
107 //! [7]
107 //! [7]
108
108
109 QChartView *chartView = new QChartView(drilldownChart);
109 QChartView *chartView = new QChartView(drilldownChart);
110 window.setCentralWidget(chartView);
110 window.setCentralWidget(chartView);
111 window.resize(400, 300);
111 window.resize(400, 300);
112 window.show();
112 window.show();
113
113
114 return a.exec();
114 return a.exec();
115 }
115 }
116
116
@@ -1,608 +1,608
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 "qbarseries.h"
21 #include "qbarseries.h"
22 #include "qbarseries_p.h"
22 #include "qbarseries_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "domain_p.h"
25 #include "domain_p.h"
26 #include "legendmarker_p.h"
26 #include "legendmarker_p.h"
27 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
28 #include "charttheme_p.h"
28 #include "charttheme_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30
30
31 #include <QAbstractItemModel>
31 #include <QAbstractItemModel>
32 #include <QModelIndex>
32 #include <QModelIndex>
33 #include <QBarModelMapper>
33 #include <QBarModelMapper>
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 /*!
37 /*!
38 \class QBarSeries
38 \class QBarSeries
39 \brief part of QtCommercial chart API.
39 \brief part of QtCommercial chart API.
40 \mainclass
40 \mainclass
41
41
42 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multiple
42 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multiple
43 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
43 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
44 by QStringList.
44 by QStringList.
45
45
46 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
46 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
47 \image examples_barchart.png
47 \image examples_barchart.png
48
48
49 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
49 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
50 */
50 */
51
51
52 /*!
52 /*!
53 \fn void QBarSeries::clicked(QBarSet *barset, QString category)
53 \fn void QBarSeries::clicked(QBarSet *barset, QString category)
54
54
55 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset of category \a category
55 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset of category \a category
56 contained by the series.
56 contained by the series.
57 */
57 */
58
58
59 /*!
59 /*!
60 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
60 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
61
61
62 The signal is emitted if mouse is hovered on top of series.
62 The signal is emitted if mouse is hovered on top of series.
63 Parameter \a barset is the pointer of barset, where hover happened.
63 Parameter \a barset is the pointer of barset, where hover happened.
64 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
64 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
65 */
65 */
66
66
67 /*!
67 /*!
68 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
68 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
69 QBarSeries is QObject which is a child of a \a parent.
69 QBarSeries is QObject which is a child of a \a parent.
70 */
70 */
71 QBarSeries::QBarSeries(/*QBarCategories categories,*/ QObject *parent) :
71 QBarSeries::QBarSeries(/*QBarCategories categories,*/ QObject *parent) :
72 QAbstractSeries(*new QBarSeriesPrivate(/*categories,*/ this),parent)
72 QAbstractSeries(*new QBarSeriesPrivate(/*categories,*/ this),parent)
73 {
73 {
74 }
74 }
75
75
76 /*!
76 /*!
77 Destructs barseries and owned barsets.
77 Destructs barseries and owned barsets.
78 */
78 */
79 QBarSeries::~QBarSeries()
79 QBarSeries::~QBarSeries()
80 {
80 {
81 // NOTE: d_ptr destroyed by QObject
81 // NOTE: d_ptr destroyed by QObject
82 }
82 }
83
83
84 /*!
84 /*!
85 \internal
85 \internal
86 */
86 */
87 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
87 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
88 QAbstractSeries(d,parent)
88 QAbstractSeries(d,parent)
89 {
89 {
90 }
90 }
91
91
92 /*!
92 /*!
93 Returns the type of series. Derived classes override this.
93 Returns the type of series. Derived classes override this.
94 */
94 */
95 QAbstractSeries::SeriesType QBarSeries::type() const
95 QAbstractSeries::SeriesType QBarSeries::type() const
96 {
96 {
97 return QAbstractSeries::SeriesTypeBar;
97 return QAbstractSeries::SeriesTypeBar;
98 }
98 }
99
99
100 void QBarSeries::setCategories(QBarCategories categories)
100 void QBarSeries::setCategories(QBarCategories categories)
101 {
101 {
102 Q_D(QBarSeries);
102 Q_D(QBarSeries);
103 d->setCategories(categories);
103 d->setCategories(categories);
104 emit d->categoriesUpdated();
104 emit d->categoriesUpdated();
105 }
105 }
106
106
107 /*!
107 /*!
108 Adds a set of bars to series. Takes ownership of \a set.
108 Adds a set of bars to series. Takes ownership of \a set.
109 */
109 */
110 bool QBarSeries::appendBarSet(QBarSet *set)
110 bool QBarSeries::append(QBarSet *set)
111 {
111 {
112 Q_D(QBarSeries);
112 Q_D(QBarSeries);
113 if ((d->m_barSets.contains(set)) || (set == 0)) {
113 if ((d->m_barSets.contains(set)) || (set == 0)) {
114 // Fail if set is already in list or set is null.
114 // Fail if set is already in list or set is null.
115 return false;
115 return false;
116 }
116 }
117 d->m_barSets.append(set);
117 d->m_barSets.append(set);
118 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
118 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
119 emit d->restructuredBars();
119 emit d->restructuredBars();
120 return true;
120 return true;
121 }
121 }
122
122
123 /*!
123 /*!
124 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
124 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
125 */
125 */
126 bool QBarSeries::removeBarSet(QBarSet *set)
126 bool QBarSeries::remove(QBarSet *set)
127 {
127 {
128 Q_D(QBarSeries);
128 Q_D(QBarSeries);
129 if (!d->m_barSets.contains(set)) {
129 if (!d->m_barSets.contains(set)) {
130 // Fail if set is not in list
130 // Fail if set is not in list
131 return false;
131 return false;
132 }
132 }
133 d->m_barSets.removeOne(set);
133 d->m_barSets.removeOne(set);
134 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
134 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
135 emit d->restructuredBars();
135 emit d->restructuredBars();
136 return true;
136 return true;
137 }
137 }
138
138
139 /*!
139 /*!
140 Adds a list of barsets to series. Takes ownership of \a sets.
140 Adds a list of barsets to series. Takes ownership of \a sets.
141 */
141 */
142 bool QBarSeries::appendBarSets(QList<QBarSet* > sets)
142 bool QBarSeries::append(QList<QBarSet* > sets)
143 {
143 {
144 Q_D(QBarSeries);
144 Q_D(QBarSeries);
145 foreach (QBarSet* set, sets) {
145 foreach (QBarSet* set, sets) {
146 if ((set == 0) || (d->m_barSets.contains(set))) {
146 if ((set == 0) || (d->m_barSets.contains(set))) {
147 // Fail if any of the sets is null or is already appended.
147 // Fail if any of the sets is null or is already appended.
148 return false;
148 return false;
149 }
149 }
150 if (sets.count(set) != 1) {
150 if (sets.count(set) != 1) {
151 // Also fail if same set is more than once in given list.
151 // Also fail if same set is more than once in given list.
152 return false;
152 return false;
153 }
153 }
154 }
154 }
155
155
156 foreach (QBarSet* set, sets) {
156 foreach (QBarSet* set, sets) {
157 d->m_barSets.append(set);
157 d->m_barSets.append(set);
158 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
158 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
159 }
159 }
160 emit d->restructuredBars();
160 emit d->restructuredBars();
161 return true;
161 return true;
162 }
162 }
163
163
164 /*!
164 /*!
165 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
165 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
166 */
166 */
167 bool QBarSeries::removeBarSets(QList<QBarSet* > sets)
167 bool QBarSeries::remove(QList<QBarSet* > sets)
168 {
168 {
169 Q_D(QBarSeries);
169 Q_D(QBarSeries);
170
170
171 bool setsRemoved = false;
171 bool setsRemoved = false;
172 foreach (QBarSet* set, sets) {
172 foreach (QBarSet* set, sets) {
173 if (d->m_barSets.contains(set)) {
173 if (d->m_barSets.contains(set)) {
174 d->m_barSets.removeOne(set);
174 d->m_barSets.removeOne(set);
175 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
175 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
176 setsRemoved = true;
176 setsRemoved = true;
177 }
177 }
178 }
178 }
179
179
180 if (setsRemoved) {
180 if (setsRemoved) {
181 emit d->restructuredBars();
181 emit d->restructuredBars();
182 }
182 }
183 return setsRemoved;
183 return setsRemoved;
184 }
184 }
185
185
186 /*!
186 /*!
187 Returns number of sets in series.
187 Returns number of sets in series.
188 */
188 */
189 int QBarSeries::barsetCount() const
189 int QBarSeries::barsetCount() const
190 {
190 {
191 Q_D(const QBarSeries);
191 Q_D(const QBarSeries);
192 return d->m_barSets.count();
192 return d->m_barSets.count();
193 }
193 }
194
194
195 /*!
195 /*!
196 Returns number of categories in series
196 Returns number of categories in series
197 */
197 */
198 int QBarSeries::categoryCount() const
198 int QBarSeries::categoryCount() const
199 {
199 {
200 Q_D(const QBarSeries);
200 Q_D(const QBarSeries);
201 return d->m_categories.count();
201 return d->m_categories.count();
202 }
202 }
203
203
204 /*!
204 /*!
205 Returns a list of sets in series. Keeps ownership of sets.
205 Returns a list of sets in series. Keeps ownership of sets.
206 */
206 */
207 QList<QBarSet*> QBarSeries::barSets() const
207 QList<QBarSet*> QBarSeries::barSets() const
208 {
208 {
209 Q_D(const QBarSeries);
209 Q_D(const QBarSeries);
210 return d->m_barSets;
210 return d->m_barSets;
211 }
211 }
212
212
213 /*!
213 /*!
214 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
214 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
215 Sets the \a model to be used as a data source
215 Sets the \a model to be used as a data source
216 */
216 */
217 void QBarSeries::setModel(QAbstractItemModel *model)
217 void QBarSeries::setModel(QAbstractItemModel *model)
218 {
218 {
219 Q_D(QBarSeries);
219 Q_D(QBarSeries);
220 // disconnect signals from old model
220 // disconnect signals from old model
221 if(d->m_model)
221 if(d->m_model)
222 {
222 {
223 disconnect(d->m_model, 0, this, 0);
223 disconnect(d->m_model, 0, this, 0);
224 }
224 }
225
225
226 // set new model
226 // set new model
227 if(model)
227 if(model)
228 {
228 {
229 d->m_model = model;
229 d->m_model = model;
230
230
231 // connect the signals
231 // connect the signals
232 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
232 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
233 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
233 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
234 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
234 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
235 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
235 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
236 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
236 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
237
237
238 if (d->m_mapper)
238 if (d->m_mapper)
239 d->initializeDataFromModel();
239 d->initializeDataFromModel();
240 }
240 }
241 else
241 else
242 {
242 {
243 d->m_model = 0;
243 d->m_model = 0;
244 }
244 }
245 }
245 }
246
246
247 void QBarSeries::setModelMapper(QBarModelMapper *mapper)
247 void QBarSeries::setModelMapper(QBarModelMapper *mapper)
248 {
248 {
249 Q_D(QBarSeries);
249 Q_D(QBarSeries);
250 // disconnect signals from old mapper
250 // disconnect signals from old mapper
251 if (d->m_mapper) {
251 if (d->m_mapper) {
252 QObject::disconnect(d->m_mapper, 0, this, 0);
252 QObject::disconnect(d->m_mapper, 0, this, 0);
253 }
253 }
254
254
255 if (mapper) {
255 if (mapper) {
256 d->m_mapper = mapper;
256 d->m_mapper = mapper;
257 // connect the signal from the mapper
257 // connect the signal from the mapper
258 connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializeDataFromModel()));
258 connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializeDataFromModel()));
259
259
260 if (d->m_model)
260 if (d->m_model)
261 d->initializeDataFromModel();
261 d->initializeDataFromModel();
262 } else {
262 } else {
263 d->m_mapper = 0;
263 d->m_mapper = 0;
264 }
264 }
265 }
265 }
266
266
267 QBarModelMapper* QBarSeries::modelMapper() const
267 QBarModelMapper* QBarSeries::modelMapper() const
268 {
268 {
269 Q_D(const QBarSeries);
269 Q_D(const QBarSeries);
270 return d->m_mapper;
270 return d->m_mapper;
271 }
271 }
272
272
273 /*!
273 /*!
274 Returns the bar categories of the series.
274 Returns the bar categories of the series.
275 */
275 */
276 QBarCategories QBarSeries::categories() const
276 QBarCategories QBarSeries::categories() const
277 {
277 {
278 Q_D(const QBarSeries);
278 Q_D(const QBarSeries);
279 return d->m_categories;
279 return d->m_categories;
280 }
280 }
281
281
282 /*!
282 /*!
283 Sets the visibility of labels in series to \a visible
283 Sets the visibility of labels in series to \a visible
284 */
284 */
285 void QBarSeries::setLabelsVisible(bool visible)
285 void QBarSeries::setLabelsVisible(bool visible)
286 {
286 {
287 foreach (QBarSet* s, barSets()) {
287 foreach (QBarSet* s, barSets()) {
288 s->setLabelsVisible(visible);
288 s->setLabelsVisible(visible);
289 }
289 }
290 }
290 }
291
291
292 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
292 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
293
293
294 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
294 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
295 QAbstractSeriesPrivate(q),
295 QAbstractSeriesPrivate(q),
296 m_barMargin(0.05), // Default value is 5% of category width
296 m_barMargin(0.05), // Default value is 5% of category width
297 m_mapper(0)
297 m_mapper(0)
298 // m_categories(categories),
298 // m_categories(categories),
299 {
299 {
300 }
300 }
301
301
302 void QBarSeriesPrivate::setCategories(QBarCategories categories)
302 void QBarSeriesPrivate::setCategories(QBarCategories categories)
303 {
303 {
304 m_categories = categories;
304 m_categories = categories;
305 }
305 }
306
306
307 void QBarSeriesPrivate::setBarMargin(qreal margin)
307 void QBarSeriesPrivate::setBarMargin(qreal margin)
308 {
308 {
309 if (margin > 1.0) {
309 if (margin > 1.0) {
310 margin = 1.0;
310 margin = 1.0;
311 } else if (margin < 0.0) {
311 } else if (margin < 0.0) {
312 margin = 0.0;
312 margin = 0.0;
313 }
313 }
314
314
315 m_barMargin = margin;
315 m_barMargin = margin;
316 emit updatedBars();
316 emit updatedBars();
317 }
317 }
318
318
319 qreal QBarSeriesPrivate::barMargin()
319 qreal QBarSeriesPrivate::barMargin()
320 {
320 {
321 return m_barMargin;
321 return m_barMargin;
322 }
322 }
323
323
324 QBarSet* QBarSeriesPrivate::barsetAt(int index)
324 QBarSet* QBarSeriesPrivate::barsetAt(int index)
325 {
325 {
326 return m_barSets.at(index);
326 return m_barSets.at(index);
327 }
327 }
328
328
329 QString QBarSeriesPrivate::categoryName(int category)
329 QString QBarSeriesPrivate::categoryName(int category)
330 {
330 {
331 return m_categories.at(category);
331 return m_categories.at(category);
332 }
332 }
333
333
334 qreal QBarSeriesPrivate::min()
334 qreal QBarSeriesPrivate::min()
335 {
335 {
336 if (m_barSets.count() <= 0) {
336 if (m_barSets.count() <= 0) {
337 return 0;
337 return 0;
338 }
338 }
339 qreal min = INT_MAX;
339 qreal min = INT_MAX;
340
340
341 for (int i = 0; i < m_barSets.count(); i++) {
341 for (int i = 0; i < m_barSets.count(); i++) {
342 int categoryCount = m_barSets.at(i)->count();
342 int categoryCount = m_barSets.at(i)->count();
343 for (int j = 0; j < categoryCount; j++) {
343 for (int j = 0; j < categoryCount; j++) {
344 qreal temp = m_barSets.at(i)->at(j).y();
344 qreal temp = m_barSets.at(i)->at(j).y();
345 if (temp < min)
345 if (temp < min)
346 min = temp;
346 min = temp;
347 }
347 }
348 }
348 }
349 return min;
349 return min;
350 }
350 }
351
351
352 qreal QBarSeriesPrivate::max()
352 qreal QBarSeriesPrivate::max()
353 {
353 {
354 if (m_barSets.count() <= 0) {
354 if (m_barSets.count() <= 0) {
355 return 0;
355 return 0;
356 }
356 }
357 qreal max = INT_MIN;
357 qreal max = INT_MIN;
358
358
359 for (int i = 0; i < m_barSets.count(); i++) {
359 for (int i = 0; i < m_barSets.count(); i++) {
360 int categoryCount = m_barSets.at(i)->count();
360 int categoryCount = m_barSets.at(i)->count();
361 for (int j = 0; j < categoryCount; j++) {
361 for (int j = 0; j < categoryCount; j++) {
362 qreal temp = m_barSets.at(i)->at(j).y();
362 qreal temp = m_barSets.at(i)->at(j).y();
363 if (temp > max)
363 if (temp > max)
364 max = temp;
364 max = temp;
365 }
365 }
366 }
366 }
367
367
368 return max;
368 return max;
369 }
369 }
370
370
371 qreal QBarSeriesPrivate::valueAt(int set, int category)
371 qreal QBarSeriesPrivate::valueAt(int set, int category)
372 {
372 {
373 if ((set < 0) || (set >= m_barSets.count())) {
373 if ((set < 0) || (set >= m_barSets.count())) {
374 // No set, no value.
374 // No set, no value.
375 return 0;
375 return 0;
376 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
376 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
377 // No category, no value.
377 // No category, no value.
378 return 0;
378 return 0;
379 }
379 }
380
380
381 return m_barSets.at(set)->at(category).y();
381 return m_barSets.at(set)->at(category).y();
382 }
382 }
383
383
384 qreal QBarSeriesPrivate::percentageAt(int set, int category)
384 qreal QBarSeriesPrivate::percentageAt(int set, int category)
385 {
385 {
386 if ((set < 0) || (set >= m_barSets.count())) {
386 if ((set < 0) || (set >= m_barSets.count())) {
387 // No set, no value.
387 // No set, no value.
388 return 0;
388 return 0;
389 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
389 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
390 // No category, no value.
390 // No category, no value.
391 return 0;
391 return 0;
392 }
392 }
393
393
394 qreal value = m_barSets.at(set)->at(category).y();
394 qreal value = m_barSets.at(set)->at(category).y();
395 qreal sum = categorySum(category);
395 qreal sum = categorySum(category);
396 if ( qFuzzyIsNull(sum) ) {
396 if ( qFuzzyIsNull(sum) ) {
397 return 0;
397 return 0;
398 }
398 }
399
399
400 return value / sum;
400 return value / sum;
401 }
401 }
402
402
403 qreal QBarSeriesPrivate::categorySum(int category)
403 qreal QBarSeriesPrivate::categorySum(int category)
404 {
404 {
405 qreal sum(0);
405 qreal sum(0);
406 int count = m_barSets.count(); // Count sets
406 int count = m_barSets.count(); // Count sets
407 for (int set = 0; set < count; set++) {
407 for (int set = 0; set < count; set++) {
408 if (category < m_barSets.at(set)->count())
408 if (category < m_barSets.at(set)->count())
409 sum += m_barSets.at(set)->at(category).y();
409 sum += m_barSets.at(set)->at(category).y();
410 }
410 }
411 return sum;
411 return sum;
412 }
412 }
413
413
414 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
414 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
415 {
415 {
416 qreal sum(0);
416 qreal sum(0);
417 int count = m_barSets.count(); // Count sets
417 int count = m_barSets.count(); // Count sets
418 for (int set = 0; set < count; set++) {
418 for (int set = 0; set < count; set++) {
419 if (category < m_barSets.at(set)->count())
419 if (category < m_barSets.at(set)->count())
420 sum += qAbs(m_barSets.at(set)->at(category).y());
420 sum += qAbs(m_barSets.at(set)->at(category).y());
421 }
421 }
422 return sum;
422 return sum;
423 }
423 }
424
424
425 qreal QBarSeriesPrivate::maxCategorySum()
425 qreal QBarSeriesPrivate::maxCategorySum()
426 {
426 {
427 qreal max = INT_MIN;
427 qreal max = INT_MIN;
428 int count = m_categories.count();
428 int count = m_categories.count();
429 for (int i = 0; i < count; i++) {
429 for (int i = 0; i < count; i++) {
430 qreal sum = categorySum(i);
430 qreal sum = categorySum(i);
431 if (sum > max)
431 if (sum > max)
432 max = sum;
432 max = sum;
433 }
433 }
434 return max;
434 return max;
435 }
435 }
436
436
437 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
437 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
438 {
438 {
439 if (m_model == 0 || m_mapper == 0)
439 if (m_model == 0 || m_mapper == 0)
440 return;
440 return;
441
441
442 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
442 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
443 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
443 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
444 if (m_mapper->orientation() == Qt::Vertical)
444 if (m_mapper->orientation() == Qt::Vertical)
445 {
445 {
446 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
446 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
447 if ( row >= m_mapper->first() && (m_mapper->count() == - 1 || row < m_mapper->first() + m_mapper->count())) {
447 if ( row >= m_mapper->first() && (m_mapper->count() == - 1 || row < m_mapper->first() + m_mapper->count())) {
448 if (column >= m_mapper->mapBarBottom() && column <= m_mapper->mapBarTop())
448 if (column >= m_mapper->mapBarBottom() && column <= m_mapper->mapBarTop())
449 barsetAt(column - m_mapper->mapBarBottom())->replace(row - m_mapper->first(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
449 barsetAt(column - m_mapper->mapBarBottom())->replace(row - m_mapper->first(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
450 // if (column == m_mapper->mapCategories());// TODO:
450 // if (column == m_mapper->mapCategories());// TODO:
451 }
451 }
452 }
452 }
453 else
453 else
454 {
454 {
455 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
455 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
456 if (column >= m_mapper->first() && (m_mapper->count() == - 1 || column < m_mapper->first() + m_mapper->count())) {
456 if (column >= m_mapper->first() && (m_mapper->count() == - 1 || column < m_mapper->first() + m_mapper->count())) {
457 if (row >= m_mapper->mapBarBottom() && row <= m_mapper->mapBarTop())
457 if (row >= m_mapper->mapBarBottom() && row <= m_mapper->mapBarTop())
458 barsetAt(row - m_mapper->mapBarBottom())->replace(column - m_mapper->first(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
458 barsetAt(row - m_mapper->mapBarBottom())->replace(column - m_mapper->first(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
459 // if (row == m_mapper->mapCategories());// TODO:
459 // if (row == m_mapper->mapCategories());// TODO:
460 }
460 }
461 }
461 }
462 }
462 }
463 }
463 }
464 }
464 }
465
465
466 void QBarSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
466 void QBarSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
467 {
467 {
468 Q_UNUSED(parent);
468 Q_UNUSED(parent);
469 Q_UNUSED(start);
469 Q_UNUSED(start);
470 Q_UNUSED(end);
470 Q_UNUSED(end);
471 initializeDataFromModel();
471 initializeDataFromModel();
472 }
472 }
473
473
474 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
474 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
475 {
475 {
476 Q_UNUSED(parent);
476 Q_UNUSED(parent);
477 Q_UNUSED(start);
477 Q_UNUSED(start);
478 Q_UNUSED(end);
478 Q_UNUSED(end);
479 initializeDataFromModel();
479 initializeDataFromModel();
480 }
480 }
481
481
482 void QBarSeriesPrivate::initializeDataFromModel()
482 void QBarSeriesPrivate::initializeDataFromModel()
483 {
483 {
484 Q_Q(QBarSeries);
484 Q_Q(QBarSeries);
485
485
486 // create the initial bars
486 // create the initial bars
487 m_categories.clear();
487 m_categories.clear();
488 m_barSets.clear();
488 m_barSets.clear();
489
489
490 if (m_model == 0 || m_mapper == 0)
490 if (m_model == 0 || m_mapper == 0)
491 return;
491 return;
492
492
493 // check if mappings are set
493 // check if mappings are set
494 if (m_mapper->mapBarBottom() == -1 || m_mapper->mapBarTop() == -1 || m_mapper->mapCategories() == -1)
494 if (m_mapper->mapBarBottom() == -1 || m_mapper->mapBarTop() == -1 || m_mapper->mapCategories() == -1)
495 return;
495 return;
496
496
497 // emit restructuredBars();
497 // emit restructuredBars();
498 if (m_mapper->orientation() == Qt::Vertical) {
498 if (m_mapper->orientation() == Qt::Vertical) {
499 if (m_mapCategories >= m_model->columnCount())
499 if (m_mapCategories >= m_model->columnCount())
500 return;
500 return;
501 int rowCount = 0;
501 int rowCount = 0;
502 if(m_mapper->count() == -1)
502 if(m_mapper->count() == -1)
503 rowCount = m_model->rowCount() - m_mapper->first();
503 rowCount = m_model->rowCount() - m_mapper->first();
504 else
504 else
505 rowCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first());
505 rowCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first());
506 for (int k = m_mapper->first(); k < m_mapper->first() + rowCount; k++) {
506 for (int k = m_mapper->first(); k < m_mapper->first() + rowCount; k++) {
507 m_categories << m_model->data(m_model->index(k, m_mapper->mapCategories()), Qt::DisplayRole).toString();
507 m_categories << m_model->data(m_model->index(k, m_mapper->mapCategories()), Qt::DisplayRole).toString();
508 }
508 }
509
509
510 int lastAvailableBarSet = qMin(m_model->columnCount() - 1, m_mapper->mapBarTop());
510 int lastAvailableBarSet = qMin(m_model->columnCount() - 1, m_mapper->mapBarTop());
511 for (int i = m_mapper->mapBarBottom(); i <= lastAvailableBarSet; i++) {
511 for (int i = m_mapper->mapBarBottom(); i <= lastAvailableBarSet; i++) {
512 // for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) {
512 // for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) {
513 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
513 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
514 for(int m = m_mapper->first(); m < m_mapper->first() + rowCount; m++)
514 for(int m = m_mapper->first(); m < m_mapper->first() + rowCount; m++)
515 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
515 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
516 q->appendBarSet(barSet);
516 q->append(barSet);
517 }
517 }
518 } else {
518 } else {
519 if (m_mapCategories >= m_model->rowCount())
519 if (m_mapCategories >= m_model->rowCount())
520 return;
520 return;
521 int columnCount = 0;
521 int columnCount = 0;
522 if(m_mapper->count() == -1)
522 if(m_mapper->count() == -1)
523 columnCount = m_model->columnCount() - m_mapper->first();
523 columnCount = m_model->columnCount() - m_mapper->first();
524 else
524 else
525 columnCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first());
525 columnCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first());
526 for (int k = m_mapper->first(); k < m_mapper->first() + columnCount; k++) {
526 for (int k = m_mapper->first(); k < m_mapper->first() + columnCount; k++) {
527 m_categories << m_model->data(m_model->index(m_mapper->mapCategories(), k), Qt::DisplayRole).toString();
527 m_categories << m_model->data(m_model->index(m_mapper->mapCategories(), k), Qt::DisplayRole).toString();
528 }
528 }
529
529
530 int lastAvailableBarSet = qMin(m_model->rowCount() - 1, m_mapper->mapBarTop());
530 int lastAvailableBarSet = qMin(m_model->rowCount() - 1, m_mapper->mapBarTop());
531 for (int i = m_mapper->mapBarBottom(); i <= lastAvailableBarSet; i++) {
531 for (int i = m_mapper->mapBarBottom(); i <= lastAvailableBarSet; i++) {
532 // for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) {
532 // for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) {
533 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
533 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
534 for(int m = m_mapper->first(); m < m_mapper->first() + columnCount; m++)
534 for(int m = m_mapper->first(); m < m_mapper->first() + columnCount; m++)
535 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
535 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
536 q->appendBarSet(barSet);
536 q->append(barSet);
537 }
537 }
538 }
538 }
539 emit restructuredBars();
539 emit restructuredBars();
540 // emit updatedBars();
540 // emit updatedBars();
541 }
541 }
542
542
543 void QBarSeriesPrivate::insertCategory(int index, const QString category)
543 void QBarSeriesPrivate::insertCategory(int index, const QString category)
544 {
544 {
545 m_categories.insert(index, category);
545 m_categories.insert(index, category);
546 emit categoriesUpdated();
546 emit categoriesUpdated();
547 }
547 }
548
548
549 void QBarSeriesPrivate::removeCategory(int index)
549 void QBarSeriesPrivate::removeCategory(int index)
550 {
550 {
551 m_categories.removeAt(index);
551 m_categories.removeAt(index);
552 emit categoriesUpdated();
552 emit categoriesUpdated();
553 }
553 }
554
554
555 void QBarSeriesPrivate::barsetChanged()
555 void QBarSeriesPrivate::barsetChanged()
556 {
556 {
557 emit updatedBars();
557 emit updatedBars();
558 }
558 }
559
559
560 void QBarSeriesPrivate::scaleDomain(Domain& domain)
560 void QBarSeriesPrivate::scaleDomain(Domain& domain)
561 {
561 {
562 qreal minX(domain.minX());
562 qreal minX(domain.minX());
563 qreal minY(domain.minY());
563 qreal minY(domain.minY());
564 qreal maxX(domain.maxX());
564 qreal maxX(domain.maxX());
565 qreal maxY(domain.maxY());
565 qreal maxY(domain.maxY());
566 int tickXCount(domain.tickXCount());
566 int tickXCount(domain.tickXCount());
567 int tickYCount(domain.tickYCount());
567 int tickYCount(domain.tickYCount());
568
568
569 qreal x = m_categories.count();
569 qreal x = m_categories.count();
570 qreal y = max();
570 qreal y = max();
571 minX = qMin(minX, x);
571 minX = qMin(minX, x);
572 minY = qMin(minY, y);
572 minY = qMin(minY, y);
573 maxX = qMax(maxX, x);
573 maxX = qMax(maxX, x);
574 maxY = qMax(maxY, y);
574 maxY = qMax(maxY, y);
575 tickXCount = x+1;
575 tickXCount = x+1;
576
576
577 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
577 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
578 }
578 }
579
579
580 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
580 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
581 {
581 {
582 Q_Q(QBarSeries);
582 Q_Q(QBarSeries);
583
583
584 BarChartItem* bar = new BarChartItem(q,presenter);
584 BarChartItem* bar = new BarChartItem(q,presenter);
585 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
585 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
586 presenter->animator()->addAnimation(bar);
586 presenter->animator()->addAnimation(bar);
587 }
587 }
588 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
588 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
589 return bar;
589 return bar;
590
590
591 }
591 }
592
592
593 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
593 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
594 {
594 {
595 Q_Q(QBarSeries);
595 Q_Q(QBarSeries);
596 QList<LegendMarker*> markers;
596 QList<LegendMarker*> markers;
597 foreach(QBarSet* set, q->barSets()) {
597 foreach(QBarSet* set, q->barSets()) {
598 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
598 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
599 markers << marker;
599 markers << marker;
600 }
600 }
601
601
602 return markers;
602 return markers;
603 }
603 }
604
604
605 #include "moc_qbarseries.cpp"
605 #include "moc_qbarseries.cpp"
606 #include "moc_qbarseries_p.cpp"
606 #include "moc_qbarseries_p.cpp"
607
607
608 QTCOMMERCIALCHART_END_NAMESPACE
608 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,81 +1,81
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 BARSERIES_H
21 #ifndef BARSERIES_H
22 #define BARSERIES_H
22 #define BARSERIES_H
23
23
24 #include <qabstractseries.h>
24 #include <qabstractseries.h>
25 #include <QStringList>
25 #include <QStringList>
26
26
27 class QModelIndex;
27 class QModelIndex;
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 typedef QStringList QBarCategories;
31 typedef QStringList QBarCategories;
32
32
33 class QBarSet;
33 class QBarSet;
34 class BarCategory;
34 class BarCategory;
35 class QBarSeriesPrivate;
35 class QBarSeriesPrivate;
36 class QBarModelMapper;
36 class QBarModelMapper;
37
37
38 // Container for series
38 // Container for series
39 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
39 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
40 {
40 {
41 Q_OBJECT
41 Q_OBJECT
42 public:
42 public:
43 explicit QBarSeries(QObject *parent = 0);
43 explicit QBarSeries(QObject *parent = 0);
44 virtual ~QBarSeries();
44 virtual ~QBarSeries();
45
45
46 QAbstractSeries::SeriesType type() const;
46 QAbstractSeries::SeriesType type() const;
47 void setCategories(QBarCategories categories);
47 void setCategories(QBarCategories categories);
48
48
49 bool appendBarSet(QBarSet *set); // Takes ownership of set
49 bool append(QBarSet *set); // Takes ownership of set
50 bool removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
50 bool remove(QBarSet *set); // Releases ownership, doesn't delete set
51 bool appendBarSets(QList<QBarSet* > sets);
51 bool append(QList<QBarSet* > sets);
52 bool removeBarSets(QList<QBarSet* > sets);
52 bool remove(QList<QBarSet* > sets);
53 int barsetCount() const;
53 int barsetCount() const;
54 int categoryCount() const;
54 int categoryCount() const;
55 QList<QBarSet*> barSets() const;
55 QList<QBarSet*> barSets() const;
56 QBarCategories categories() const;
56 QBarCategories categories() const;
57
57
58 void setLabelsVisible(bool visible = true);
58 void setLabelsVisible(bool visible = true);
59
59
60 void setModel(QAbstractItemModel *model);
60 void setModel(QAbstractItemModel *model);
61 void setModelMapper(QBarModelMapper *mapper);
61 void setModelMapper(QBarModelMapper *mapper);
62 QBarModelMapper* modelMapper() const;
62 QBarModelMapper* modelMapper() const;
63
63
64 protected:
64 protected:
65 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
65 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
66
66
67 Q_SIGNALS:
67 Q_SIGNALS:
68 void clicked(QBarSet *barset, QString category);
68 void clicked(QBarSet *barset, QString category);
69 void hovered(QBarSet* barset, bool status);
69 void hovered(QBarSet* barset, bool status);
70
70
71 protected:
71 protected:
72 Q_DECLARE_PRIVATE(QBarSeries)
72 Q_DECLARE_PRIVATE(QBarSeries)
73 friend class BarChartItem;
73 friend class BarChartItem;
74 friend class PercentBarChartItem;
74 friend class PercentBarChartItem;
75 friend class StackedBarChartItem;
75 friend class StackedBarChartItem;
76 friend class GroupedBarChartItem;
76 friend class GroupedBarChartItem;
77 };
77 };
78
78
79 QTCOMMERCIALCHART_END_NAMESPACE
79 QTCOMMERCIALCHART_END_NAMESPACE
80
80
81 #endif // BARSERIES_H
81 #endif // BARSERIES_H
@@ -1,677 +1,677
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 <qbarseries.h>
22 #include <qbarseries.h>
23 #include <qbarset.h>
23 #include <qbarset.h>
24 #include <qchartview.h>
24 #include <qchartview.h>
25 #include <qchart.h>
25 #include <qchart.h>
26
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
28
29 Q_DECLARE_METATYPE(QBarSet*)
29 Q_DECLARE_METATYPE(QBarSet*)
30
30
31 class tst_QBarSeries : public QObject
31 class tst_QBarSeries : public QObject
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public slots:
35 public slots:
36 void initTestCase();
36 void initTestCase();
37 void cleanupTestCase();
37 void cleanupTestCase();
38 void init();
38 void init();
39 void cleanup();
39 void cleanup();
40
40
41 private slots:
41 private slots:
42 void qbarseries_data();
42 void qbarseries_data();
43 void qbarseries();
43 void qbarseries();
44 void type_data();
44 void type_data();
45 void type();
45 void type();
46 void setCategories_data();
46 void setCategories_data();
47 void setCategories();
47 void setCategories();
48 void appendBarSet_data();
48 void append_data();
49 void appendBarSet();
49 void append();
50 void removeBarSet_data();
50 void remove_data();
51 void removeBarSet();
51 void remove();
52 void appendBarSets_data();
52 void appendList_data();
53 void appendBarSets();
53 void appendList();
54 void removeBarSets_data();
54 void removeList_data();
55 void removeBarSets();
55 void removeList();
56 void barsetCount_data();
56 void barsetCount_data();
57 void barsetCount();
57 void barsetCount();
58 void categoryCount_data();
58 void categoryCount_data();
59 void categoryCount();
59 void categoryCount();
60 void barSets_data();
60 void barSets_data();
61 void barSets();
61 void barSets();
62 void categories_data();
62 void categories_data();
63 void categories();
63 void categories();
64 void setLabelsVisible_data();
64 void setLabelsVisible_data();
65 void setLabelsVisible();
65 void setLabelsVisible();
66 void mouseclicked_data();
66 void mouseclicked_data();
67 void mouseclicked();
67 void mouseclicked();
68 void mousehovered_data();
68 void mousehovered_data();
69 void mousehovered();
69 void mousehovered();
70
70
71 private:
71 private:
72 QBarSeries* m_barseries;
72 QBarSeries* m_barseries;
73 QBarSeries* m_barseries_with_sets;
73 QBarSeries* m_barseries_with_sets;
74
74
75 QList<QBarSet*> m_testSets;
75 QList<QBarSet*> m_testSets;
76
76
77 QBarCategories m_categories;
77 QBarCategories m_categories;
78 };
78 };
79
79
80 void tst_QBarSeries::initTestCase()
80 void tst_QBarSeries::initTestCase()
81 {
81 {
82 qRegisterMetaType<QBarSet*>("QBarSet*");
82 qRegisterMetaType<QBarSet*>("QBarSet*");
83 }
83 }
84
84
85 void tst_QBarSeries::cleanupTestCase()
85 void tst_QBarSeries::cleanupTestCase()
86 {
86 {
87 }
87 }
88
88
89 void tst_QBarSeries::init()
89 void tst_QBarSeries::init()
90 {
90 {
91 m_categories << "category0" << "category1" << "category2";
91 m_categories << "category0" << "category1" << "category2";
92 m_barseries = new QBarSeries();
92 m_barseries = new QBarSeries();
93 m_barseries->setCategories(m_categories);
93 m_barseries->setCategories(m_categories);
94 m_barseries_with_sets = new QBarSeries();
94 m_barseries_with_sets = new QBarSeries();
95 m_barseries_with_sets->setCategories(m_categories);
95 m_barseries_with_sets->setCategories(m_categories);
96
96
97 for (int i=0; i<5; i++) {
97 for (int i=0; i<5; i++) {
98 m_testSets.append(new QBarSet("testset"));
98 m_testSets.append(new QBarSet("testset"));
99 m_barseries_with_sets->appendBarSet(m_testSets.at(i));
99 m_barseries_with_sets->append(m_testSets.at(i));
100 }
100 }
101 }
101 }
102
102
103 void tst_QBarSeries::cleanup()
103 void tst_QBarSeries::cleanup()
104 {
104 {
105 foreach(QBarSet* s, m_testSets) {
105 foreach(QBarSet* s, m_testSets) {
106 m_barseries_with_sets->removeBarSet(s);
106 m_barseries_with_sets->remove(s);
107 delete s;
107 delete s;
108 }
108 }
109 m_testSets.clear();
109 m_testSets.clear();
110
110
111 delete m_barseries;
111 delete m_barseries;
112 m_barseries = 0;
112 m_barseries = 0;
113 delete m_barseries_with_sets;
113 delete m_barseries_with_sets;
114 m_barseries_with_sets = 0;
114 m_barseries_with_sets = 0;
115 m_categories.clear();
115 m_categories.clear();
116 }
116 }
117
117
118 void tst_QBarSeries::qbarseries_data()
118 void tst_QBarSeries::qbarseries_data()
119 {
119 {
120 QTest::addColumn<QBarCategories> ("categories");
120 QTest::addColumn<QBarCategories> ("categories");
121 QBarCategories c;
121 QBarCategories c;
122 c << "category0" << "category1" << "category2";
122 c << "category0" << "category1" << "category2";
123 QTest::newRow("categories") << c;
123 QTest::newRow("categories") << c;
124 }
124 }
125
125
126 void tst_QBarSeries::qbarseries()
126 void tst_QBarSeries::qbarseries()
127 {
127 {
128 QFETCH(QBarCategories, categories);
128 QFETCH(QBarCategories, categories);
129 QBarSeries *barseries = new QBarSeries();
129 QBarSeries *barseries = new QBarSeries();
130 QVERIFY(barseries != 0);
130 QVERIFY(barseries != 0);
131 barseries->setCategories(categories);
131 barseries->setCategories(categories);
132 QBarCategories verifyCategories = barseries->categories();
132 QBarCategories verifyCategories = barseries->categories();
133
133
134 QVERIFY(verifyCategories.count() == categories.count());
134 QVERIFY(verifyCategories.count() == categories.count());
135 for (int i=0; i<categories.count(); i++) {
135 for (int i=0; i<categories.count(); i++) {
136 QVERIFY(verifyCategories.at(i).compare(categories.at(i)) == 0);
136 QVERIFY(verifyCategories.at(i).compare(categories.at(i)) == 0);
137 }
137 }
138 }
138 }
139
139
140 void tst_QBarSeries::type_data()
140 void tst_QBarSeries::type_data()
141 {
141 {
142
142
143 }
143 }
144
144
145 void tst_QBarSeries::type()
145 void tst_QBarSeries::type()
146 {
146 {
147 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar);
147 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar);
148 }
148 }
149
149
150 void tst_QBarSeries::setCategories_data()
150 void tst_QBarSeries::setCategories_data()
151 {
151 {
152 QTest::addColumn<QBarCategories> ("categories");
152 QTest::addColumn<QBarCategories> ("categories");
153 QBarCategories categories;
153 QBarCategories categories;
154 categories << "c1" << "c2" << "c3" << "c4" << "c5" << "c6";
154 categories << "c1" << "c2" << "c3" << "c4" << "c5" << "c6";
155 QTest::newRow("cat") << categories;
155 QTest::newRow("cat") << categories;
156 }
156 }
157
157
158 void tst_QBarSeries::setCategories()
158 void tst_QBarSeries::setCategories()
159 {
159 {
160 QVERIFY(m_barseries->categories().count() == m_categories.count());
160 QVERIFY(m_barseries->categories().count() == m_categories.count());
161
161
162 QFETCH(QBarCategories, categories);
162 QFETCH(QBarCategories, categories);
163 m_barseries->setCategories(categories);
163 m_barseries->setCategories(categories);
164
164
165 QVERIFY(m_barseries->categories().count() == categories.count());
165 QVERIFY(m_barseries->categories().count() == categories.count());
166 for (int i=0; i<categories.count(); i++) {
166 for (int i=0; i<categories.count(); i++) {
167 QVERIFY(m_barseries->categories().at(i).compare(categories.at(i)) == 0);
167 QVERIFY(m_barseries->categories().at(i).compare(categories.at(i)) == 0);
168 }
168 }
169 }
169 }
170
170
171 void tst_QBarSeries::appendBarSet_data()
171 void tst_QBarSeries::append_data()
172 {
172 {
173 }
173 }
174
174
175 void tst_QBarSeries::appendBarSet()
175 void tst_QBarSeries::append()
176 {
176 {
177 QVERIFY(m_barseries->barsetCount() == 0);
177 QVERIFY(m_barseries->barsetCount() == 0);
178
178
179 bool ret = false;
179 bool ret = false;
180
180
181 // Try adding barset
181 // Try adding barset
182 QBarSet *barset = new QBarSet("testset");
182 QBarSet *barset = new QBarSet("testset");
183 ret = m_barseries->appendBarSet(barset);
183 ret = m_barseries->append(barset);
184
184
185 QVERIFY(ret == true);
185 QVERIFY(ret == true);
186 QVERIFY(m_barseries->barsetCount() == 1);
186 QVERIFY(m_barseries->barsetCount() == 1);
187
187
188 // Try adding another set
188 // Try adding another set
189 QBarSet *barset2 = new QBarSet("testset2");
189 QBarSet *barset2 = new QBarSet("testset2");
190 ret = m_barseries->appendBarSet(barset2);
190 ret = m_barseries->append(barset2);
191
191
192 QVERIFY(ret == true);
192 QVERIFY(ret == true);
193 QVERIFY(m_barseries->barsetCount() == 2);
193 QVERIFY(m_barseries->barsetCount() == 2);
194
194
195 // Try adding same set again
195 // Try adding same set again
196 ret = m_barseries->appendBarSet(barset2);
196 ret = m_barseries->append(barset2);
197 QVERIFY(ret == false);
197 QVERIFY(ret == false);
198 QVERIFY(m_barseries->barsetCount() == 2);
198 QVERIFY(m_barseries->barsetCount() == 2);
199
199
200 // Try adding null set
200 // Try adding null set
201 ret = m_barseries->appendBarSet(0);
201 ret = m_barseries->append(0);
202 QVERIFY(ret == false);
202 QVERIFY(ret == false);
203 QVERIFY(m_barseries->barsetCount() == 2);
203 QVERIFY(m_barseries->barsetCount() == 2);
204
204
205 }
205 }
206
206
207 void tst_QBarSeries::removeBarSet_data()
207 void tst_QBarSeries::remove_data()
208 {
208 {
209 }
209 }
210
210
211 void tst_QBarSeries::removeBarSet()
211 void tst_QBarSeries::remove()
212 {
212 {
213 int count = m_testSets.count();
213 int count = m_testSets.count();
214 QVERIFY(m_barseries_with_sets->barsetCount() == count);
214 QVERIFY(m_barseries_with_sets->barsetCount() == count);
215
215
216 // Try to remove null pointer (should not remove, should not crash)
216 // Try to remove null pointer (should not remove, should not crash)
217 bool ret = false;
217 bool ret = false;
218 ret = m_barseries_with_sets->removeBarSet(0);
218 ret = m_barseries_with_sets->remove(0);
219 QVERIFY(ret == false);
219 QVERIFY(ret == false);
220 QVERIFY(m_barseries_with_sets->barsetCount() == count);
220 QVERIFY(m_barseries_with_sets->barsetCount() == count);
221
221
222 // Try to remove invalid pointer (should not remove, should not crash)
222 // Try to remove invalid pointer (should not remove, should not crash)
223 ret = m_barseries_with_sets->removeBarSet((QBarSet*) (m_testSets.at(0) + 1) );
223 ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) );
224 QVERIFY(ret == false);
224 QVERIFY(ret == false);
225 QVERIFY(m_barseries_with_sets->barsetCount() == count);
225 QVERIFY(m_barseries_with_sets->barsetCount() == count);
226
226
227 // remove some sets
227 // remove some sets
228 ret = m_barseries_with_sets->removeBarSet(m_testSets.at(2));
228 ret = m_barseries_with_sets->remove(m_testSets.at(2));
229 QVERIFY(ret == true);
229 QVERIFY(ret == true);
230 ret = m_barseries_with_sets->removeBarSet(m_testSets.at(3));
230 ret = m_barseries_with_sets->remove(m_testSets.at(3));
231 QVERIFY(ret == true);
231 QVERIFY(ret == true);
232 ret = m_barseries_with_sets->removeBarSet(m_testSets.at(4));
232 ret = m_barseries_with_sets->remove(m_testSets.at(4));
233 QVERIFY(ret == true);
233 QVERIFY(ret == true);
234
234
235 QVERIFY(m_barseries_with_sets->barsetCount() == 2);
235 QVERIFY(m_barseries_with_sets->barsetCount() == 2);
236
236
237 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
237 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
238
238
239 QVERIFY(verifysets.at(0) == m_testSets.at(0));
239 QVERIFY(verifysets.at(0) == m_testSets.at(0));
240 QVERIFY(verifysets.at(1) == m_testSets.at(1));
240 QVERIFY(verifysets.at(1) == m_testSets.at(1));
241
241
242 // Try removing all sets again (should be ok, even if some sets have already been removed)
242 // Try removing all sets again (should be ok, even if some sets have already been removed)
243 ret = false;
243 ret = false;
244 for (int i=0; i<count; i++) {
244 for (int i=0; i<count; i++) {
245 ret |= m_barseries_with_sets->removeBarSet(m_testSets.at(i));
245 ret |= m_barseries_with_sets->remove(m_testSets.at(i));
246 }
246 }
247
247
248 QVERIFY(ret == true);
248 QVERIFY(ret == true);
249 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
249 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
250 }
250 }
251
251
252 void tst_QBarSeries::appendBarSets_data()
252 void tst_QBarSeries::appendList_data()
253 {
253 {
254
254
255 }
255 }
256
256
257 void tst_QBarSeries::appendBarSets()
257 void tst_QBarSeries::appendList()
258 {
258 {
259 int count = 5;
259 int count = 5;
260 QVERIFY(m_barseries->barsetCount() == 0);
260 QVERIFY(m_barseries->barsetCount() == 0);
261
261
262 QList<QBarSet*> sets;
262 QList<QBarSet*> sets;
263 for (int i=0; i<count; i++) {
263 for (int i=0; i<count; i++) {
264 sets.append(new QBarSet("testset"));
264 sets.append(new QBarSet("testset"));
265 }
265 }
266
266
267 // Append new sets (should succeed, count should match the count of sets)
267 // Append new sets (should succeed, count should match the count of sets)
268 bool ret = false;
268 bool ret = false;
269 ret = m_barseries->appendBarSets(sets);
269 ret = m_barseries->append(sets);
270 QVERIFY(ret == true);
270 QVERIFY(ret == true);
271 QVERIFY(m_barseries->barsetCount() == count);
271 QVERIFY(m_barseries->barsetCount() == count);
272
272
273 // Append same sets again (should fail, count should remain same)
273 // Append same sets again (should fail, count should remain same)
274 ret = m_barseries->appendBarSets(sets);
274 ret = m_barseries->append(sets);
275 QVERIFY(ret == false);
275 QVERIFY(ret == false);
276 QVERIFY(m_barseries->barsetCount() == count);
276 QVERIFY(m_barseries->barsetCount() == count);
277
277
278 // Try append empty list (should succeed, but count should remain same)
278 // Try append empty list (should succeed, but count should remain same)
279 QList<QBarSet*> invalidList;
279 QList<QBarSet*> invalidList;
280 ret = m_barseries->appendBarSets(invalidList);
280 ret = m_barseries->append(invalidList);
281 QVERIFY(ret == true);
281 QVERIFY(ret == true);
282 QVERIFY(m_barseries->barsetCount() == count);
282 QVERIFY(m_barseries->barsetCount() == count);
283
283
284 // Try append list with one new and one existing set (should fail, count remains same)
284 // Try append list with one new and one existing set (should fail, count remains same)
285 invalidList.append(new QBarSet("ok set"));
285 invalidList.append(new QBarSet("ok set"));
286 invalidList.append(sets.at(0));
286 invalidList.append(sets.at(0));
287 ret = m_barseries->appendBarSets(invalidList);
287 ret = m_barseries->append(invalidList);
288 QVERIFY(ret == false);
288 QVERIFY(ret == false);
289 QVERIFY(m_barseries->barsetCount() == count);
289 QVERIFY(m_barseries->barsetCount() == count);
290
290
291 // Try append list with null pointers (should fail, count remains same)
291 // Try append list with null pointers (should fail, count remains same)
292 QList<QBarSet*> invalidList2;
292 QList<QBarSet*> invalidList2;
293 invalidList2.append(0);
293 invalidList2.append(0);
294 invalidList2.append(0);
294 invalidList2.append(0);
295 invalidList2.append(0);
295 invalidList2.append(0);
296 ret = m_barseries->appendBarSets(invalidList2);
296 ret = m_barseries->append(invalidList2);
297 QVERIFY(ret == false);
297 QVERIFY(ret == false);
298 QVERIFY(m_barseries->barsetCount() == count);
298 QVERIFY(m_barseries->barsetCount() == count);
299 }
299 }
300
300
301 void tst_QBarSeries::removeBarSets_data()
301 void tst_QBarSeries::removeList_data()
302 {
302 {
303
303
304 }
304 }
305
305
306 void tst_QBarSeries::removeBarSets()
306 void tst_QBarSeries::removeList()
307 {
307 {
308 int count = m_testSets.count();
308 int count = m_testSets.count();
309 QVERIFY(m_barseries_with_sets->barsetCount() == count);
309 QVERIFY(m_barseries_with_sets->barsetCount() == count);
310
310
311 // Try removing empty list of sets (should return false, since no barsets were removed)
311 // Try removing empty list of sets (should return false, since no barsets were removed)
312 bool ret = false;
312 bool ret = false;
313 QList<QBarSet*> invalidList;
313 QList<QBarSet*> invalidList;
314 ret = m_barseries_with_sets->removeBarSets(invalidList);
314 ret = m_barseries_with_sets->remove(invalidList);
315 QVERIFY(ret == false);
315 QVERIFY(ret == false);
316 QVERIFY(m_barseries_with_sets->barsetCount() == count);
316 QVERIFY(m_barseries_with_sets->barsetCount() == count);
317
317
318 // Add some null pointers to list
318 // Add some null pointers to list
319 invalidList.append(0);
319 invalidList.append(0);
320 invalidList.append(0);
320 invalidList.append(0);
321 invalidList.append(0);
321 invalidList.append(0);
322
322
323 // Try removing null pointers from list (should return false, should not crash, should not remove anything)
323 // Try removing null pointers from list (should return false, should not crash, should not remove anything)
324 ret = m_barseries_with_sets->removeBarSets(invalidList);
324 ret = m_barseries_with_sets->remove(invalidList);
325 QVERIFY(ret == false);
325 QVERIFY(ret == false);
326 QVERIFY(m_barseries_with_sets->barsetCount() == count);
326 QVERIFY(m_barseries_with_sets->barsetCount() == count);
327
327
328 // remove all sets (should return true, since sets were removed)
328 // remove all sets (should return true, since sets were removed)
329 ret = m_barseries_with_sets->removeBarSets(m_testSets);
329 ret = m_barseries_with_sets->remove(m_testSets);
330 QVERIFY(ret == true);
330 QVERIFY(ret == true);
331 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
331 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
332
332
333 // Try removing invalid list again (should return false, since no barsets were removed)
333 // Try removing invalid list again (should return false, since no barsets were removed)
334 ret = m_barseries_with_sets->removeBarSets(invalidList);
334 ret = m_barseries_with_sets->remove(invalidList);
335 QVERIFY(ret == false);
335 QVERIFY(ret == false);
336 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
336 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
337
337
338 // remove all sets again (should return false, since barsets were already removed)
338 // remove all sets again (should return false, since barsets were already removed)
339 ret = m_barseries_with_sets->removeBarSets(m_testSets);
339 ret = m_barseries_with_sets->remove(m_testSets);
340 QVERIFY(ret == false);
340 QVERIFY(ret == false);
341 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
341 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
342 }
342 }
343
343
344 void tst_QBarSeries::barsetCount_data()
344 void tst_QBarSeries::barsetCount_data()
345 {
345 {
346
346
347 }
347 }
348
348
349 void tst_QBarSeries::barsetCount()
349 void tst_QBarSeries::barsetCount()
350 {
350 {
351 QVERIFY(m_barseries->barsetCount() == 0);
351 QVERIFY(m_barseries->barsetCount() == 0);
352 QVERIFY(m_barseries_with_sets->barsetCount() == m_testSets.count());
352 QVERIFY(m_barseries_with_sets->barsetCount() == m_testSets.count());
353 }
353 }
354
354
355 void tst_QBarSeries::categoryCount_data()
355 void tst_QBarSeries::categoryCount_data()
356 {
356 {
357
357
358 }
358 }
359
359
360 void tst_QBarSeries::categoryCount()
360 void tst_QBarSeries::categoryCount()
361 {
361 {
362 QVERIFY(m_barseries->categoryCount() == m_categories.count());
362 QVERIFY(m_barseries->categoryCount() == m_categories.count());
363 QVERIFY(m_barseries_with_sets->categoryCount() == m_categories.count());
363 QVERIFY(m_barseries_with_sets->categoryCount() == m_categories.count());
364 }
364 }
365
365
366 void tst_QBarSeries::barSets_data()
366 void tst_QBarSeries::barSets_data()
367 {
367 {
368
368
369 }
369 }
370
370
371 void tst_QBarSeries::barSets()
371 void tst_QBarSeries::barSets()
372 {
372 {
373 QVERIFY(m_barseries->barSets().count() == 0);
373 QVERIFY(m_barseries->barSets().count() == 0);
374
374
375 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
375 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
376 QVERIFY(sets.count() == m_testSets.count());
376 QVERIFY(sets.count() == m_testSets.count());
377
377
378 for (int i=0; i<m_testSets.count(); i++) {
378 for (int i=0; i<m_testSets.count(); i++) {
379 QVERIFY(sets.at(i) == m_testSets.at(i));
379 QVERIFY(sets.at(i) == m_testSets.at(i));
380 }
380 }
381 }
381 }
382
382
383 void tst_QBarSeries::categories_data()
383 void tst_QBarSeries::categories_data()
384 {
384 {
385
385
386 }
386 }
387
387
388 void tst_QBarSeries::categories()
388 void tst_QBarSeries::categories()
389 {
389 {
390 QBarCategories categories = m_barseries->categories();
390 QBarCategories categories = m_barseries->categories();
391
391
392 QVERIFY(categories.count() == m_categories.count());
392 QVERIFY(categories.count() == m_categories.count());
393 for (int i=0; i<m_categories.count(); i++) {
393 for (int i=0; i<m_categories.count(); i++) {
394 QVERIFY(categories.at(i).compare(m_categories.at(i)) == 0);
394 QVERIFY(categories.at(i).compare(m_categories.at(i)) == 0);
395 }
395 }
396 }
396 }
397
397
398 void tst_QBarSeries::setLabelsVisible_data()
398 void tst_QBarSeries::setLabelsVisible_data()
399 {
399 {
400
400
401 }
401 }
402
402
403 void tst_QBarSeries::setLabelsVisible()
403 void tst_QBarSeries::setLabelsVisible()
404 {
404 {
405 // labels should be invisible by default
405 // labels should be invisible by default
406 foreach (QBarSet* s, m_testSets) {
406 foreach (QBarSet* s, m_testSets) {
407 QVERIFY(s->labelsVisible() == false);
407 QVERIFY(s->labelsVisible() == false);
408 }
408 }
409
409
410 // turn labels to visible
410 // turn labels to visible
411 m_barseries_with_sets->setLabelsVisible(true);
411 m_barseries_with_sets->setLabelsVisible(true);
412 foreach (QBarSet* s, m_testSets) {
412 foreach (QBarSet* s, m_testSets) {
413 QVERIFY(s->labelsVisible() == true);
413 QVERIFY(s->labelsVisible() == true);
414 }
414 }
415
415
416 // turn labels to invisible
416 // turn labels to invisible
417 m_barseries_with_sets->setLabelsVisible(false);
417 m_barseries_with_sets->setLabelsVisible(false);
418 foreach (QBarSet* s, m_testSets) {
418 foreach (QBarSet* s, m_testSets) {
419 QVERIFY(s->labelsVisible() == false);
419 QVERIFY(s->labelsVisible() == false);
420 }
420 }
421
421
422 // without parameter, should turn labels to visible
422 // without parameter, should turn labels to visible
423 m_barseries_with_sets->setLabelsVisible();
423 m_barseries_with_sets->setLabelsVisible();
424 foreach (QBarSet* s, m_testSets) {
424 foreach (QBarSet* s, m_testSets) {
425 QVERIFY(s->labelsVisible() == true);
425 QVERIFY(s->labelsVisible() == true);
426 }
426 }
427 }
427 }
428
428
429 void tst_QBarSeries::mouseclicked_data()
429 void tst_QBarSeries::mouseclicked_data()
430 {
430 {
431
431
432 }
432 }
433
433
434 void tst_QBarSeries::mouseclicked()
434 void tst_QBarSeries::mouseclicked()
435 {
435 {
436 QBarSeries* series = new QBarSeries();
436 QBarSeries* series = new QBarSeries();
437 QBarCategories categories;
437 QBarCategories categories;
438 categories << "test1" << "test2" << "test3";
438 categories << "test1" << "test2" << "test3";
439 series->setCategories(categories);
439 series->setCategories(categories);
440
440
441 QBarSet* set1 = new QBarSet(QString("set 1"));
441 QBarSet* set1 = new QBarSet(QString("set 1"));
442 *set1 << QPointF(0,10) << QPointF(1,10) << QPointF(2,10);
442 *set1 << QPointF(0,10) << QPointF(1,10) << QPointF(2,10);
443 series->appendBarSet(set1);
443 series->append(set1);
444
444
445 QBarSet* set2 = new QBarSet(QString("set 2"));
445 QBarSet* set2 = new QBarSet(QString("set 2"));
446 *set2 << QPointF(0.5,10) << QPointF(1.5,10) << QPointF(2.5,10);
446 *set2 << QPointF(0.5,10) << QPointF(1.5,10) << QPointF(2.5,10);
447 series->appendBarSet(set2);
447 series->append(set2);
448
448
449 QSignalSpy setSpy1(set1, SIGNAL(clicked(QString)));
449 QSignalSpy setSpy1(set1, SIGNAL(clicked(QString)));
450 QSignalSpy setSpy2(set2, SIGNAL(clicked(QString)));
450 QSignalSpy setSpy2(set2, SIGNAL(clicked(QString)));
451 QSignalSpy seriesSpy(series,SIGNAL(clicked(QBarSet*,QString)));
451 QSignalSpy seriesSpy(series,SIGNAL(clicked(QBarSet*,QString)));
452
452
453 QChartView view(new QChart());
453 QChartView view(new QChart());
454 view.resize(400,300);
454 view.resize(400,300);
455 view.chart()->addSeries(series);
455 view.chart()->addSeries(series);
456 view.show();
456 view.show();
457 QTest::qWaitForWindowShown(&view);
457 QTest::qWaitForWindowShown(&view);
458
458
459 //====================================================================================
459 //====================================================================================
460 // barset 1, category test1
460 // barset 1, category test1
461 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(75,180));
461 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(75,180));
462 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
462 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
463
463
464 QCOMPARE(setSpy1.count(), 1);
464 QCOMPARE(setSpy1.count(), 1);
465 QCOMPARE(setSpy2.count(), 0);
465 QCOMPARE(setSpy2.count(), 0);
466 QCOMPARE(seriesSpy.count(), 1);
466 QCOMPARE(seriesSpy.count(), 1);
467 QList<QVariant> setSpyArg = setSpy1.takeFirst();
467 QList<QVariant> setSpyArg = setSpy1.takeFirst();
468 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
468 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
469 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
469 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
470
470
471 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
471 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
472 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
472 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
473
473
474 //====================================================================================
474 //====================================================================================
475 // barset 1, category test2
475 // barset 1, category test2
476 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(160,180));
476 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(160,180));
477 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
477 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
478
478
479 QCOMPARE(setSpy1.count(), 1);
479 QCOMPARE(setSpy1.count(), 1);
480 QCOMPARE(setSpy2.count(), 0);
480 QCOMPARE(setSpy2.count(), 0);
481 QCOMPARE(seriesSpy.count(), 1);
481 QCOMPARE(seriesSpy.count(), 1);
482 setSpyArg = setSpy1.takeFirst();
482 setSpyArg = setSpy1.takeFirst();
483 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
483 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
484 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
484 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
485
485
486 seriesSpyArg = seriesSpy.takeFirst();
486 seriesSpyArg = seriesSpy.takeFirst();
487 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
487 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
488
488
489 //====================================================================================
489 //====================================================================================
490 // barset 1, category test3
490 // barset 1, category test3
491 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(255,180));
491 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(255,180));
492 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
492 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
493
493
494 QCOMPARE(setSpy1.count(), 1);
494 QCOMPARE(setSpy1.count(), 1);
495 QCOMPARE(setSpy2.count(), 0);
495 QCOMPARE(setSpy2.count(), 0);
496 QCOMPARE(seriesSpy.count(), 1);
496 QCOMPARE(seriesSpy.count(), 1);
497 setSpyArg = setSpy1.takeFirst();
497 setSpyArg = setSpy1.takeFirst();
498 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
498 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
499 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
499 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
500
500
501 seriesSpyArg = seriesSpy.takeFirst();
501 seriesSpyArg = seriesSpy.takeFirst();
502 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
502 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
503
503
504 //====================================================================================
504 //====================================================================================
505 // barset 2, category test1
505 // barset 2, category test1
506 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(115,180));
506 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(115,180));
507 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
507 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
508
508
509 QCOMPARE(setSpy1.count(), 0);
509 QCOMPARE(setSpy1.count(), 0);
510 QCOMPARE(setSpy2.count(), 1);
510 QCOMPARE(setSpy2.count(), 1);
511 QCOMPARE(seriesSpy.count(), 1);
511 QCOMPARE(seriesSpy.count(), 1);
512 setSpyArg = setSpy2.takeFirst();
512 setSpyArg = setSpy2.takeFirst();
513 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
513 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
514 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
514 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
515
515
516 seriesSpyArg = seriesSpy.takeFirst();
516 seriesSpyArg = seriesSpy.takeFirst();
517 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
517 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
518
518
519 //====================================================================================
519 //====================================================================================
520 // barset 2, category test2
520 // barset 2, category test2
521 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(205,180));
521 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(205,180));
522 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
522 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
523
523
524 QCOMPARE(setSpy1.count(), 0);
524 QCOMPARE(setSpy1.count(), 0);
525 QCOMPARE(setSpy2.count(), 1);
525 QCOMPARE(setSpy2.count(), 1);
526 QCOMPARE(seriesSpy.count(), 1);
526 QCOMPARE(seriesSpy.count(), 1);
527 setSpyArg = setSpy2.takeFirst();
527 setSpyArg = setSpy2.takeFirst();
528 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
528 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
529 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
529 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
530
530
531 seriesSpyArg = seriesSpy.takeFirst();
531 seriesSpyArg = seriesSpy.takeFirst();
532 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
532 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
533
533
534 //====================================================================================
534 //====================================================================================
535 // barset 2, category test3
535 // barset 2, category test3
536 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(300,180));
536 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(300,180));
537 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
537 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
538
538
539 QCOMPARE(setSpy1.count(), 0);
539 QCOMPARE(setSpy1.count(), 0);
540 QCOMPARE(setSpy2.count(), 1);
540 QCOMPARE(setSpy2.count(), 1);
541 QCOMPARE(seriesSpy.count(), 1);
541 QCOMPARE(seriesSpy.count(), 1);
542 setSpyArg = setSpy2.takeFirst();
542 setSpyArg = setSpy2.takeFirst();
543 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
543 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
544 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
544 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
545
545
546 seriesSpyArg = seriesSpy.takeFirst();
546 seriesSpyArg = seriesSpy.takeFirst();
547 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
547 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
548
548
549 //====================================================================================
549 //====================================================================================
550 // no event cases
550 // no event cases
551 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(1,1)); // Outside of both sets
551 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(1,1)); // Outside of both sets
552 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(1,1)); // Right mouse button outside and inside sets
552 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(1,1)); // Right mouse button outside and inside sets
553 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(75,180)); // barset 1, category test1
553 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(75,180)); // barset 1, category test1
554 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(160,180)); // barset 1, category test2
554 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(160,180)); // barset 1, category test2
555 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(255,180)); // barset 1, category test3
555 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(255,180)); // barset 1, category test3
556 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(115,180)); // barset 2, category test1
556 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(115,180)); // barset 2, category test1
557 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(205,180)); // barset 2, category test2
557 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(205,180)); // barset 2, category test2
558 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(300,180)); // barset 2, category test3
558 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(300,180)); // barset 2, category test3
559
559
560 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
560 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
561 QCOMPARE(setSpy1.count(), 0);
561 QCOMPARE(setSpy1.count(), 0);
562 QCOMPARE(setSpy2.count(), 0);
562 QCOMPARE(setSpy2.count(), 0);
563 QCOMPARE(seriesSpy.count(), 0);
563 QCOMPARE(seriesSpy.count(), 0);
564 }
564 }
565
565
566 void tst_QBarSeries::mousehovered_data()
566 void tst_QBarSeries::mousehovered_data()
567 {
567 {
568
568
569 }
569 }
570
570
571 void tst_QBarSeries::mousehovered()
571 void tst_QBarSeries::mousehovered()
572 {
572 {
573 QBarSeries* series = new QBarSeries();
573 QBarSeries* series = new QBarSeries();
574 QBarCategories categories;
574 QBarCategories categories;
575 categories << "test1" << "test2" << "test3";
575 categories << "test1" << "test2" << "test3";
576 series->setCategories(categories);
576 series->setCategories(categories);
577
577
578 QBarSet* set1 = new QBarSet(QString("set 1"));
578 QBarSet* set1 = new QBarSet(QString("set 1"));
579 *set1 << QPointF(0.1,10) << QPointF(1.1,10) << QPointF(2.1,10);
579 *set1 << QPointF(0.1,10) << QPointF(1.1,10) << QPointF(2.1,10);
580 series->appendBarSet(set1);
580 series->append(set1);
581
581
582 QBarSet* set2 = new QBarSet(QString("set 2"));
582 QBarSet* set2 = new QBarSet(QString("set 2"));
583 *set2 << QPointF(0.4,10) << QPointF(1.4,10) << QPointF(2.4,10);
583 *set2 << QPointF(0.4,10) << QPointF(1.4,10) << QPointF(2.4,10);
584 series->appendBarSet(set2);
584 series->append(set2);
585
585
586 QSignalSpy setSpy1(set1, SIGNAL(hovered(bool)));
586 QSignalSpy setSpy1(set1, SIGNAL(hovered(bool)));
587 QSignalSpy setSpy2(set2, SIGNAL(hovered(bool)));
587 QSignalSpy setSpy2(set2, SIGNAL(hovered(bool)));
588 QSignalSpy seriesSpy(series,SIGNAL(hovered(QBarSet*,bool)));
588 QSignalSpy seriesSpy(series,SIGNAL(hovered(QBarSet*,bool)));
589
589
590 QChartView view(new QChart());
590 QChartView view(new QChart());
591 view.resize(400,300);
591 view.resize(400,300);
592 view.chart()->addSeries(series);
592 view.chart()->addSeries(series);
593 view.show();
593 view.show();
594 QTest::qWaitForWindowShown(&view);
594 QTest::qWaitForWindowShown(&view);
595
595
596 //=======================================================================
596 //=======================================================================
597 // move mouse to left border
597 // move mouse to left border
598 QTest::mouseMove(view.viewport(), QPoint(0, 180));
598 QTest::mouseMove(view.viewport(), QPoint(0, 180));
599
599
600 QVERIFY(setSpy1.count() == 0);
600 QVERIFY(setSpy1.count() == 0);
601 QVERIFY(setSpy2.count() == 0);
601 QVERIFY(setSpy2.count() == 0);
602 QVERIFY(seriesSpy.count() == 0);
602 QVERIFY(seriesSpy.count() == 0);
603
603
604 //=======================================================================
604 //=======================================================================
605 // move mouse on top of set1
605 // move mouse on top of set1
606 QTest::mouseMove(view.viewport(), QPoint(75,180));
606 QTest::mouseMove(view.viewport(), QPoint(75,180));
607
607
608 QVERIFY(setSpy1.count() == 1);
608 QVERIFY(setSpy1.count() == 1);
609 QVERIFY(setSpy2.count() == 0);
609 QVERIFY(setSpy2.count() == 0);
610 QVERIFY(seriesSpy.count() == 1);
610 QVERIFY(seriesSpy.count() == 1);
611
611
612 QList<QVariant> setSpyArg = setSpy1.takeFirst();
612 QList<QVariant> setSpyArg = setSpy1.takeFirst();
613 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
613 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
614 QVERIFY(setSpyArg.at(0).toBool() == true);
614 QVERIFY(setSpyArg.at(0).toBool() == true);
615
615
616 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
616 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
617 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
617 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
618
618
619 //=======================================================================
619 //=======================================================================
620 // move mouse from top of set1 to top of set2
620 // move mouse from top of set1 to top of set2
621 QTest::mouseMove(view.viewport(), QPoint(105,180));
621 QTest::mouseMove(view.viewport(), QPoint(105,180));
622
622
623 QVERIFY(setSpy1.count() == 1);
623 QVERIFY(setSpy1.count() == 1);
624 QVERIFY(setSpy2.count() == 1);
624 QVERIFY(setSpy2.count() == 1);
625 QVERIFY(seriesSpy.count() == 2);
625 QVERIFY(seriesSpy.count() == 2);
626
626
627 // should leave set1
627 // should leave set1
628 setSpyArg = setSpy1.takeFirst();
628 setSpyArg = setSpy1.takeFirst();
629 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
629 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
630 QVERIFY(setSpyArg.at(0).toBool() == false);
630 QVERIFY(setSpyArg.at(0).toBool() == false);
631
631
632 // should enter set2
632 // should enter set2
633 setSpyArg = setSpy2.takeFirst();
633 setSpyArg = setSpy2.takeFirst();
634 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
634 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
635 QVERIFY(setSpyArg.at(0).toBool() == true);
635 QVERIFY(setSpyArg.at(0).toBool() == true);
636
636
637 // should leave set1
637 // should leave set1
638 seriesSpyArg = seriesSpy.takeFirst();
638 seriesSpyArg = seriesSpy.takeFirst();
639 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
639 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
640 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
640 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
641 QVERIFY(seriesSpyArg.at(1).toBool() == false);
641 QVERIFY(seriesSpyArg.at(1).toBool() == false);
642
642
643 // should enter set2
643 // should enter set2
644 seriesSpyArg = seriesSpy.takeFirst();
644 seriesSpyArg = seriesSpy.takeFirst();
645 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
645 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
646 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
646 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
647 QVERIFY(seriesSpyArg.at(1).toBool() == true);
647 QVERIFY(seriesSpyArg.at(1).toBool() == true);
648
648
649 //=======================================================================
649 //=======================================================================
650 // move mouse from top of set2 to background
650 // move mouse from top of set2 to background
651 QTest::mouseMove(view.viewport(), QPoint(135,180));
651 QTest::mouseMove(view.viewport(), QPoint(135,180));
652
652
653 QVERIFY(setSpy1.count() == 0);
653 QVERIFY(setSpy1.count() == 0);
654 QVERIFY(setSpy2.count() == 1);
654 QVERIFY(setSpy2.count() == 1);
655 QVERIFY(seriesSpy.count() == 1);
655 QVERIFY(seriesSpy.count() == 1);
656
656
657 // should leave set2 (event via set)
657 // should leave set2 (event via set)
658 setSpyArg = setSpy2.takeFirst();
658 setSpyArg = setSpy2.takeFirst();
659 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
659 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
660 QVERIFY(setSpyArg.at(0).toBool() == false);
660 QVERIFY(setSpyArg.at(0).toBool() == false);
661
661
662 // should leave set2 (event via series)
662 // should leave set2 (event via series)
663 seriesSpyArg = seriesSpy.takeFirst();
663 seriesSpyArg = seriesSpy.takeFirst();
664 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
664 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
665 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
665 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
666 QVERIFY(seriesSpyArg.at(1).toBool() == false);
666 QVERIFY(seriesSpyArg.at(1).toBool() == false);
667 }
667 }
668
668
669 /*
669 /*
670 bool setModel(QAbstractItemModel *model);
670 bool setModel(QAbstractItemModel *model);
671 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
671 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
672 void setModelMappingRange(int first, int count = -1);
672 void setModelMappingRange(int first, int count = -1);
673 */
673 */
674 QTEST_MAIN(tst_QBarSeries)
674 QTEST_MAIN(tst_QBarSeries)
675
675
676 #include "tst_qbarseries.moc"
676 #include "tst_qbarseries.moc"
677
677
@@ -1,403 +1,403
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 <qgroupedbarseries.h>
22 #include <qgroupedbarseries.h>
23 #include <qbarset.h>
23 #include <qbarset.h>
24 #include <qchartview.h>
24 #include <qchartview.h>
25 #include <qchart.h>
25 #include <qchart.h>
26 #include <QBarModelMapper>
26 #include <QBarModelMapper>
27 #include <QStandardItemModel>
27 #include <QStandardItemModel>
28
28
29 QTCOMMERCIALCHART_USE_NAMESPACE
29 QTCOMMERCIALCHART_USE_NAMESPACE
30
30
31 Q_DECLARE_METATYPE(QBarSet*)
31 Q_DECLARE_METATYPE(QBarSet*)
32
32
33 class tst_QGroupedBarSeries : public QObject
33 class tst_QGroupedBarSeries : public QObject
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36
36
37 public slots:
37 public slots:
38 void initTestCase();
38 void initTestCase();
39 void cleanupTestCase();
39 void cleanupTestCase();
40 void init();
40 void init();
41 void cleanup();
41 void cleanup();
42
42
43 private slots:
43 private slots:
44 void qgroupedbarseries_data();
44 void qgroupedbarseries_data();
45 void qgroupedbarseries();
45 void qgroupedbarseries();
46 void type_data();
46 void type_data();
47 void type();
47 void type();
48 void mouseclicked_data();
48 void mouseclicked_data();
49 void mouseclicked();
49 void mouseclicked();
50 void mousehovered_data();
50 void mousehovered_data();
51 void mousehovered();
51 void mousehovered();
52 void model();
52 void model();
53
53
54 private:
54 private:
55 QGroupedBarSeries* m_barseries;
55 QGroupedBarSeries* m_barseries;
56 };
56 };
57
57
58 void tst_QGroupedBarSeries::initTestCase()
58 void tst_QGroupedBarSeries::initTestCase()
59 {
59 {
60 qRegisterMetaType<QBarSet*>("QBarSet*");
60 qRegisterMetaType<QBarSet*>("QBarSet*");
61 }
61 }
62
62
63 void tst_QGroupedBarSeries::cleanupTestCase()
63 void tst_QGroupedBarSeries::cleanupTestCase()
64 {
64 {
65 }
65 }
66
66
67 void tst_QGroupedBarSeries::init()
67 void tst_QGroupedBarSeries::init()
68 {
68 {
69 m_barseries = new QGroupedBarSeries();
69 m_barseries = new QGroupedBarSeries();
70 }
70 }
71
71
72 void tst_QGroupedBarSeries::cleanup()
72 void tst_QGroupedBarSeries::cleanup()
73 {
73 {
74 delete m_barseries;
74 delete m_barseries;
75 m_barseries = 0;
75 m_barseries = 0;
76 }
76 }
77
77
78 void tst_QGroupedBarSeries::qgroupedbarseries_data()
78 void tst_QGroupedBarSeries::qgroupedbarseries_data()
79 {
79 {
80 }
80 }
81
81
82 void tst_QGroupedBarSeries::qgroupedbarseries()
82 void tst_QGroupedBarSeries::qgroupedbarseries()
83 {
83 {
84 QGroupedBarSeries *barseries = new QGroupedBarSeries();
84 QGroupedBarSeries *barseries = new QGroupedBarSeries();
85 QVERIFY(barseries != 0);
85 QVERIFY(barseries != 0);
86 }
86 }
87
87
88 void tst_QGroupedBarSeries::type_data()
88 void tst_QGroupedBarSeries::type_data()
89 {
89 {
90
90
91 }
91 }
92
92
93 void tst_QGroupedBarSeries::type()
93 void tst_QGroupedBarSeries::type()
94 {
94 {
95 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeGroupedBar);
95 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeGroupedBar);
96 }
96 }
97
97
98 void tst_QGroupedBarSeries::mouseclicked_data()
98 void tst_QGroupedBarSeries::mouseclicked_data()
99 {
99 {
100
100
101 }
101 }
102
102
103 void tst_QGroupedBarSeries::mouseclicked()
103 void tst_QGroupedBarSeries::mouseclicked()
104 {
104 {
105 QGroupedBarSeries* series = new QGroupedBarSeries();
105 QGroupedBarSeries* series = new QGroupedBarSeries();
106 QBarCategories categories;
106 QBarCategories categories;
107 categories << "test1" << "test2" << "test3";
107 categories << "test1" << "test2" << "test3";
108 series->setCategories(categories);
108 series->setCategories(categories);
109
109
110 QBarSet* set1 = new QBarSet(QString("set 1"));
110 QBarSet* set1 = new QBarSet(QString("set 1"));
111 *set1 << 10 << 10 << 10;
111 *set1 << 10 << 10 << 10;
112 series->appendBarSet(set1);
112 series->append(set1);
113
113
114 QBarSet* set2 = new QBarSet(QString("set 2"));
114 QBarSet* set2 = new QBarSet(QString("set 2"));
115 *set2 << 10 << 10 << 10;
115 *set2 << 10 << 10 << 10;
116 series->appendBarSet(set2);
116 series->append(set2);
117
117
118 QSignalSpy setSpy1(set1, SIGNAL(clicked(QString)));
118 QSignalSpy setSpy1(set1, SIGNAL(clicked(QString)));
119 QSignalSpy setSpy2(set2, SIGNAL(clicked(QString)));
119 QSignalSpy setSpy2(set2, SIGNAL(clicked(QString)));
120 QSignalSpy seriesSpy(series,SIGNAL(clicked(QBarSet*,QString)));
120 QSignalSpy seriesSpy(series,SIGNAL(clicked(QBarSet*,QString)));
121
121
122 QChartView view(new QChart());
122 QChartView view(new QChart());
123 view.resize(400,300);
123 view.resize(400,300);
124 view.chart()->addSeries(series);
124 view.chart()->addSeries(series);
125 view.show();
125 view.show();
126 QTest::qWaitForWindowShown(&view);
126 QTest::qWaitForWindowShown(&view);
127
127
128 //====================================================================================
128 //====================================================================================
129 // barset 1, category test1
129 // barset 1, category test1
130 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(100,180));
130 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(100,180));
131 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
131 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
132
132
133 QCOMPARE(setSpy1.count(), 1);
133 QCOMPARE(setSpy1.count(), 1);
134 QCOMPARE(setSpy2.count(), 0);
134 QCOMPARE(setSpy2.count(), 0);
135 QCOMPARE(seriesSpy.count(), 1);
135 QCOMPARE(seriesSpy.count(), 1);
136 QList<QVariant> setSpyArg = setSpy1.takeFirst();
136 QList<QVariant> setSpyArg = setSpy1.takeFirst();
137 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
137 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
138 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
138 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
139
139
140 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
140 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
141 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
141 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
142
142
143 //====================================================================================
143 //====================================================================================
144 // barset 1, category test2
144 // barset 1, category test2
145 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(190,180));
145 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(190,180));
146 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
146 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
147
147
148 QCOMPARE(setSpy1.count(), 1);
148 QCOMPARE(setSpy1.count(), 1);
149 QCOMPARE(setSpy2.count(), 0);
149 QCOMPARE(setSpy2.count(), 0);
150 QCOMPARE(seriesSpy.count(), 1);
150 QCOMPARE(seriesSpy.count(), 1);
151 setSpyArg = setSpy1.takeFirst();
151 setSpyArg = setSpy1.takeFirst();
152 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
152 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
153 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
153 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
154
154
155 seriesSpyArg = seriesSpy.takeFirst();
155 seriesSpyArg = seriesSpy.takeFirst();
156 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
156 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
157
157
158 //====================================================================================
158 //====================================================================================
159 // barset 1, category test3
159 // barset 1, category test3
160 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(280,180));
160 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(280,180));
161 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
161 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
162
162
163 QCOMPARE(setSpy1.count(), 1);
163 QCOMPARE(setSpy1.count(), 1);
164 QCOMPARE(setSpy2.count(), 0);
164 QCOMPARE(setSpy2.count(), 0);
165 QCOMPARE(seriesSpy.count(), 1);
165 QCOMPARE(seriesSpy.count(), 1);
166 setSpyArg = setSpy1.takeFirst();
166 setSpyArg = setSpy1.takeFirst();
167 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
167 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
168 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
168 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
169
169
170 seriesSpyArg = seriesSpy.takeFirst();
170 seriesSpyArg = seriesSpy.takeFirst();
171 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
171 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
172
172
173 //====================================================================================
173 //====================================================================================
174 // barset 2, category test1
174 // barset 2, category test1
175 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(130,180));
175 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(130,180));
176 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
176 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
177
177
178 QCOMPARE(setSpy1.count(), 0);
178 QCOMPARE(setSpy1.count(), 0);
179 QCOMPARE(setSpy2.count(), 1);
179 QCOMPARE(setSpy2.count(), 1);
180 QCOMPARE(seriesSpy.count(), 1);
180 QCOMPARE(seriesSpy.count(), 1);
181 setSpyArg = setSpy2.takeFirst();
181 setSpyArg = setSpy2.takeFirst();
182 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
182 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
183 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
183 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
184
184
185 seriesSpyArg = seriesSpy.takeFirst();
185 seriesSpyArg = seriesSpy.takeFirst();
186 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
186 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
187
187
188 //====================================================================================
188 //====================================================================================
189 // barset 2, category test2
189 // barset 2, category test2
190 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(220,180));
190 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(220,180));
191 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
191 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
192
192
193 QCOMPARE(setSpy1.count(), 0);
193 QCOMPARE(setSpy1.count(), 0);
194 QCOMPARE(setSpy2.count(), 1);
194 QCOMPARE(setSpy2.count(), 1);
195 QCOMPARE(seriesSpy.count(), 1);
195 QCOMPARE(seriesSpy.count(), 1);
196 setSpyArg = setSpy2.takeFirst();
196 setSpyArg = setSpy2.takeFirst();
197 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
197 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
198 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
198 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
199
199
200 seriesSpyArg = seriesSpy.takeFirst();
200 seriesSpyArg = seriesSpy.takeFirst();
201 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
201 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
202
202
203 //====================================================================================
203 //====================================================================================
204 // barset 2, category test3
204 // barset 2, category test3
205 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(310,180));
205 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(310,180));
206 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
206 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
207
207
208 QCOMPARE(setSpy1.count(), 0);
208 QCOMPARE(setSpy1.count(), 0);
209 QCOMPARE(setSpy2.count(), 1);
209 QCOMPARE(setSpy2.count(), 1);
210 QCOMPARE(seriesSpy.count(), 1);
210 QCOMPARE(seriesSpy.count(), 1);
211 setSpyArg = setSpy2.takeFirst();
211 setSpyArg = setSpy2.takeFirst();
212 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
212 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
213 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
213 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
214
214
215 seriesSpyArg = seriesSpy.takeFirst();
215 seriesSpyArg = seriesSpy.takeFirst();
216 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
216 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
217
217
218 //====================================================================================
218 //====================================================================================
219 // no event cases
219 // no event cases
220 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(1,1)); // Outside of both sets
220 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(1,1)); // Outside of both sets
221 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(1,1)); // Right mouse button outside and inside sets
221 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(1,1)); // Right mouse button outside and inside sets
222 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(100,180)); // barset 1, category test1
222 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(100,180)); // barset 1, category test1
223 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(190,180)); // barset 1, category test2
223 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(190,180)); // barset 1, category test2
224 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(280,180)); // barset 1, category test3
224 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(280,180)); // barset 1, category test3
225 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(130,180)); // barset 2, category test1
225 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(130,180)); // barset 2, category test1
226 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(220,180)); // barset 2, category test2
226 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(220,180)); // barset 2, category test2
227 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(310,180)); // barset 2, category test3
227 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(310,180)); // barset 2, category test3
228
228
229 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
229 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
230 QCOMPARE(setSpy1.count(), 0);
230 QCOMPARE(setSpy1.count(), 0);
231 QCOMPARE(setSpy2.count(), 0);
231 QCOMPARE(setSpy2.count(), 0);
232 QCOMPARE(seriesSpy.count(), 0);
232 QCOMPARE(seriesSpy.count(), 0);
233 }
233 }
234
234
235 void tst_QGroupedBarSeries::mousehovered_data()
235 void tst_QGroupedBarSeries::mousehovered_data()
236 {
236 {
237
237
238 }
238 }
239
239
240 void tst_QGroupedBarSeries::mousehovered()
240 void tst_QGroupedBarSeries::mousehovered()
241 {
241 {
242 QGroupedBarSeries* series = new QGroupedBarSeries();
242 QGroupedBarSeries* series = new QGroupedBarSeries();
243 QBarCategories categories;
243 QBarCategories categories;
244 categories << "test1" << "test2" << "test3";
244 categories << "test1" << "test2" << "test3";
245 series->setCategories(categories);
245 series->setCategories(categories);
246
246
247 QBarSet* set1 = new QBarSet(QString("set 1"));
247 QBarSet* set1 = new QBarSet(QString("set 1"));
248 *set1 << 10 << 10 << 10;
248 *set1 << 10 << 10 << 10;
249 series->appendBarSet(set1);
249 series->append(set1);
250
250
251 QBarSet* set2 = new QBarSet(QString("set 2"));
251 QBarSet* set2 = new QBarSet(QString("set 2"));
252 *set2 << 10 << 10 << 10;
252 *set2 << 10 << 10 << 10;
253 series->appendBarSet(set2);
253 series->append(set2);
254
254
255 QSignalSpy setSpy1(set1, SIGNAL(hovered(bool)));
255 QSignalSpy setSpy1(set1, SIGNAL(hovered(bool)));
256 QSignalSpy setSpy2(set2, SIGNAL(hovered(bool)));
256 QSignalSpy setSpy2(set2, SIGNAL(hovered(bool)));
257 QSignalSpy seriesSpy(series,SIGNAL(hovered(QBarSet*,bool)));
257 QSignalSpy seriesSpy(series,SIGNAL(hovered(QBarSet*,bool)));
258
258
259 QChartView view(new QChart());
259 QChartView view(new QChart());
260 view.resize(400,300);
260 view.resize(400,300);
261 view.chart()->addSeries(series);
261 view.chart()->addSeries(series);
262 view.show();
262 view.show();
263 QTest::qWaitForWindowShown(&view);
263 QTest::qWaitForWindowShown(&view);
264
264
265 //this is hack since view does not get events otherwise
265 //this is hack since view does not get events otherwise
266 view.setMouseTracking(true);
266 view.setMouseTracking(true);
267
267
268 //=======================================================================
268 //=======================================================================
269 // move mouse to left border
269 // move mouse to left border
270 QTest::mouseMove(view.viewport(), QPoint(0, 180));
270 QTest::mouseMove(view.viewport(), QPoint(0, 180));
271
271
272 QVERIFY(setSpy1.count() == 0);
272 QVERIFY(setSpy1.count() == 0);
273 QVERIFY(setSpy2.count() == 0);
273 QVERIFY(setSpy2.count() == 0);
274 QVERIFY(seriesSpy.count() == 0);
274 QVERIFY(seriesSpy.count() == 0);
275
275
276 //=======================================================================
276 //=======================================================================
277 // move mouse on top of set1
277 // move mouse on top of set1
278 QTest::mouseMove(view.viewport(), QPoint(100,180));
278 QTest::mouseMove(view.viewport(), QPoint(100,180));
279
279
280 QVERIFY(setSpy1.count() == 1);
280 QVERIFY(setSpy1.count() == 1);
281 QVERIFY(setSpy2.count() == 0);
281 QVERIFY(setSpy2.count() == 0);
282 QVERIFY(seriesSpy.count() == 1);
282 QVERIFY(seriesSpy.count() == 1);
283
283
284 QList<QVariant> setSpyArg = setSpy1.takeFirst();
284 QList<QVariant> setSpyArg = setSpy1.takeFirst();
285 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
285 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
286 QVERIFY(setSpyArg.at(0).toBool() == true);
286 QVERIFY(setSpyArg.at(0).toBool() == true);
287
287
288 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
288 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
289 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
289 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
290
290
291 //=======================================================================
291 //=======================================================================
292 // move mouse from top of set1 to top of set2
292 // move mouse from top of set1 to top of set2
293 QTest::mouseMove(view.viewport(), QPoint(130,180));
293 QTest::mouseMove(view.viewport(), QPoint(130,180));
294
294
295 QVERIFY(setSpy1.count() == 1);
295 QVERIFY(setSpy1.count() == 1);
296 QVERIFY(setSpy2.count() == 1);
296 QVERIFY(setSpy2.count() == 1);
297 QVERIFY(seriesSpy.count() == 2);
297 QVERIFY(seriesSpy.count() == 2);
298
298
299 // should leave set1
299 // should leave set1
300 setSpyArg = setSpy1.takeFirst();
300 setSpyArg = setSpy1.takeFirst();
301 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
301 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
302 QVERIFY(setSpyArg.at(0).toBool() == false);
302 QVERIFY(setSpyArg.at(0).toBool() == false);
303
303
304 // should enter set2
304 // should enter set2
305 setSpyArg = setSpy2.takeFirst();
305 setSpyArg = setSpy2.takeFirst();
306 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
306 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
307 QVERIFY(setSpyArg.at(0).toBool() == true);
307 QVERIFY(setSpyArg.at(0).toBool() == true);
308
308
309 // should leave set1
309 // should leave set1
310 seriesSpyArg = seriesSpy.takeFirst();
310 seriesSpyArg = seriesSpy.takeFirst();
311 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
311 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
312 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
312 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
313 QVERIFY(seriesSpyArg.at(1).toBool() == false);
313 QVERIFY(seriesSpyArg.at(1).toBool() == false);
314
314
315 // should enter set2
315 // should enter set2
316 seriesSpyArg = seriesSpy.takeFirst();
316 seriesSpyArg = seriesSpy.takeFirst();
317 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
317 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
318 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
318 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
319 QVERIFY(seriesSpyArg.at(1).toBool() == true);
319 QVERIFY(seriesSpyArg.at(1).toBool() == true);
320
320
321 //=======================================================================
321 //=======================================================================
322 // move mouse from top of set2 to background
322 // move mouse from top of set2 to background
323 QTest::mouseMove(view.viewport(), QPoint(160,180));
323 QTest::mouseMove(view.viewport(), QPoint(160,180));
324
324
325 QVERIFY(setSpy1.count() == 0);
325 QVERIFY(setSpy1.count() == 0);
326 QVERIFY(setSpy2.count() == 1);
326 QVERIFY(setSpy2.count() == 1);
327 QVERIFY(seriesSpy.count() == 1);
327 QVERIFY(seriesSpy.count() == 1);
328
328
329 // should leave set2 (event via set)
329 // should leave set2 (event via set)
330 setSpyArg = setSpy2.takeFirst();
330 setSpyArg = setSpy2.takeFirst();
331 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
331 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
332 QVERIFY(setSpyArg.at(0).toBool() == false);
332 QVERIFY(setSpyArg.at(0).toBool() == false);
333
333
334 // should leave set2 (event via series)
334 // should leave set2 (event via series)
335 seriesSpyArg = seriesSpy.takeFirst();
335 seriesSpyArg = seriesSpy.takeFirst();
336 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
336 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
337 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
337 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
338 QVERIFY(seriesSpyArg.at(1).toBool() == false);
338 QVERIFY(seriesSpyArg.at(1).toBool() == false);
339 }
339 }
340
340
341 void tst_QGroupedBarSeries::model()
341 void tst_QGroupedBarSeries::model()
342 {
342 {
343 QGroupedBarSeries *series = new QGroupedBarSeries;
343 QGroupedBarSeries *series = new QGroupedBarSeries;
344 QChart *chart = new QChart;
344 QChart *chart = new QChart;
345 chart->addSeries(series);
345 chart->addSeries(series);
346 QChartView *chartView = new QChartView(chart);
346 QChartView *chartView = new QChartView(chart);
347 chartView->show();
347 chartView->show();
348
348
349 int rowCount = 12;
349 int rowCount = 12;
350 int columnCount = 5;
350 int columnCount = 5;
351 QStandardItemModel *stdModel = new QStandardItemModel(rowCount, columnCount);
351 QStandardItemModel *stdModel = new QStandardItemModel(rowCount, columnCount);
352 series->setModel(stdModel);
352 series->setModel(stdModel);
353 QVERIFY2((series->model()) == stdModel, "Model should be stdModel");
353 QVERIFY2((series->model()) == stdModel, "Model should be stdModel");
354
354
355
355
356 for (int row = 0; row < rowCount; ++row) {
356 for (int row = 0; row < rowCount; ++row) {
357 for (int column = 0; column < columnCount; column++) {
357 for (int column = 0; column < columnCount; column++) {
358 QStandardItem *item = new QStandardItem(row * column);
358 QStandardItem *item = new QStandardItem(row * column);
359 stdModel->setItem(row, column, item);
359 stdModel->setItem(row, column, item);
360 }
360 }
361 }
361 }
362
362
363 // data has been added to the model, but mapper is not set the number of slices should still be 0
363 // data has been added to the model, but mapper is not set the number of slices should still be 0
364 QVERIFY2(series->barsetCount() == 0, "Mapper has not been set, so the number of slices should be 0");
364 QVERIFY2(series->barsetCount() == 0, "Mapper has not been set, so the number of slices should be 0");
365
365
366 // set the mapper
366 // set the mapper
367 QBarModelMapper *mapper = new QBarModelMapper;
367 QBarModelMapper *mapper = new QBarModelMapper;
368 mapper->setMapCategories(0);
368 mapper->setMapCategories(0);
369 mapper->setMapBarBottom(1);
369 mapper->setMapBarBottom(1);
370 mapper->setMapBarTop(3);
370 mapper->setMapBarTop(3);
371 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
371 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
372 QCOMPARE(series->barsetCount(), 3);
372 QCOMPARE(series->barsetCount(), 3);
373
373
374 // set the mappings to be outside of the model
374 // set the mappings to be outside of the model
375 mapper->setMapBarBottom(6);
375 mapper->setMapBarBottom(6);
376 mapper->setMapBarTop(7);
376 mapper->setMapBarTop(7);
377 QCOMPARE(series->barsetCount(), 0); // Mappings are invalid, so the number of slices should be 0
377 QCOMPARE(series->barsetCount(), 0); // Mappings are invalid, so the number of slices should be 0
378
378
379 // set back to correct ones
379 // set back to correct ones
380 mapper->setMapBarBottom(1);
380 mapper->setMapBarBottom(1);
381 mapper->setMapBarTop(3);
381 mapper->setMapBarTop(3);
382 QCOMPARE(series->barsetCount(), 3);
382 QCOMPARE(series->barsetCount(), 3);
383
383
384 // reset the mappings
384 // reset the mappings
385 mapper->reset();
385 mapper->reset();
386 QCOMPARE(series->barsetCount(), 0); // Mappings have been reset and are invalid, so the number of slices should be 0
386 QCOMPARE(series->barsetCount(), 0); // Mappings have been reset and are invalid, so the number of slices should be 0
387
387
388 // unset the model and the mapper
388 // unset the model and the mapper
389 series->setModel(0);
389 series->setModel(0);
390 series->setModelMapper(0);
390 series->setModelMapper(0);
391 QVERIFY(series->model() == 0); // Model should be unset
391 QVERIFY(series->model() == 0); // Model should be unset
392 QVERIFY(series->modelMapper() == 0); // Model mapper should be unset
392 QVERIFY(series->modelMapper() == 0); // Model mapper should be unset
393 }
393 }
394
394
395 /*
395 /*
396 bool setModel(QAbstractItemModel *model);
396 bool setModel(QAbstractItemModel *model);
397 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
397 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
398 void setModelMappingRange(int first, int count = -1);
398 void setModelMappingRange(int first, int count = -1);
399 */
399 */
400 QTEST_MAIN(tst_QGroupedBarSeries)
400 QTEST_MAIN(tst_QGroupedBarSeries)
401
401
402 #include "tst_qgroupedbarseries.moc"
402 #include "tst_qgroupedbarseries.moc"
403
403
@@ -1,344 +1,344
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 <qpercentbarseries.h>
22 #include <qpercentbarseries.h>
23 #include <qbarset.h>
23 #include <qbarset.h>
24 #include <qchartview.h>
24 #include <qchartview.h>
25 #include <qchart.h>
25 #include <qchart.h>
26
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
28
29 Q_DECLARE_METATYPE(QBarSet*)
29 Q_DECLARE_METATYPE(QBarSet*)
30
30
31 class tst_QPercentBarSeries : public QObject
31 class tst_QPercentBarSeries : public QObject
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public slots:
35 public slots:
36 void initTestCase();
36 void initTestCase();
37 void cleanupTestCase();
37 void cleanupTestCase();
38 void init();
38 void init();
39 void cleanup();
39 void cleanup();
40
40
41 private slots:
41 private slots:
42 void qpercentbarseries_data();
42 void qpercentbarseries_data();
43 void qpercentbarseries();
43 void qpercentbarseries();
44 void type_data();
44 void type_data();
45 void type();
45 void type();
46 void mouseclicked_data();
46 void mouseclicked_data();
47 void mouseclicked();
47 void mouseclicked();
48 void mousehovered_data();
48 void mousehovered_data();
49 void mousehovered();
49 void mousehovered();
50
50
51 private:
51 private:
52 QPercentBarSeries* m_barseries;
52 QPercentBarSeries* m_barseries;
53 };
53 };
54
54
55 void tst_QPercentBarSeries::initTestCase()
55 void tst_QPercentBarSeries::initTestCase()
56 {
56 {
57 qRegisterMetaType<QBarSet*>("QBarSet*");
57 qRegisterMetaType<QBarSet*>("QBarSet*");
58 }
58 }
59
59
60 void tst_QPercentBarSeries::cleanupTestCase()
60 void tst_QPercentBarSeries::cleanupTestCase()
61 {
61 {
62 }
62 }
63
63
64 void tst_QPercentBarSeries::init()
64 void tst_QPercentBarSeries::init()
65 {
65 {
66 m_barseries = new QPercentBarSeries();
66 m_barseries = new QPercentBarSeries();
67 }
67 }
68
68
69 void tst_QPercentBarSeries::cleanup()
69 void tst_QPercentBarSeries::cleanup()
70 {
70 {
71 delete m_barseries;
71 delete m_barseries;
72 m_barseries = 0;
72 m_barseries = 0;
73 }
73 }
74
74
75 void tst_QPercentBarSeries::qpercentbarseries_data()
75 void tst_QPercentBarSeries::qpercentbarseries_data()
76 {
76 {
77 }
77 }
78
78
79 void tst_QPercentBarSeries::qpercentbarseries()
79 void tst_QPercentBarSeries::qpercentbarseries()
80 {
80 {
81 QPercentBarSeries *barseries = new QPercentBarSeries();
81 QPercentBarSeries *barseries = new QPercentBarSeries();
82 QVERIFY(barseries != 0);
82 QVERIFY(barseries != 0);
83 }
83 }
84
84
85 void tst_QPercentBarSeries::type_data()
85 void tst_QPercentBarSeries::type_data()
86 {
86 {
87
87
88 }
88 }
89
89
90 void tst_QPercentBarSeries::type()
90 void tst_QPercentBarSeries::type()
91 {
91 {
92 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypePercentBar);
92 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypePercentBar);
93 }
93 }
94
94
95 void tst_QPercentBarSeries::mouseclicked_data()
95 void tst_QPercentBarSeries::mouseclicked_data()
96 {
96 {
97
97
98 }
98 }
99
99
100 void tst_QPercentBarSeries::mouseclicked()
100 void tst_QPercentBarSeries::mouseclicked()
101 {
101 {
102 QPercentBarSeries* series = new QPercentBarSeries();
102 QPercentBarSeries* series = new QPercentBarSeries();
103 QBarCategories categories;
103 QBarCategories categories;
104 categories << "test1" << "test2" << "test3";
104 categories << "test1" << "test2" << "test3";
105 series->setCategories(categories);
105 series->setCategories(categories);
106
106
107 QBarSet* set1 = new QBarSet(QString("set 1"));
107 QBarSet* set1 = new QBarSet(QString("set 1"));
108 *set1 << 10 << 10 << 10;
108 *set1 << 10 << 10 << 10;
109 series->appendBarSet(set1);
109 series->append(set1);
110
110
111 QBarSet* set2 = new QBarSet(QString("set 2"));
111 QBarSet* set2 = new QBarSet(QString("set 2"));
112 *set2 << 10 << 10 << 10;
112 *set2 << 10 << 10 << 10;
113 series->appendBarSet(set2);
113 series->append(set2);
114
114
115 QSignalSpy setSpy1(set1, SIGNAL(clicked(QString)));
115 QSignalSpy setSpy1(set1, SIGNAL(clicked(QString)));
116 QSignalSpy setSpy2(set2, SIGNAL(clicked(QString)));
116 QSignalSpy setSpy2(set2, SIGNAL(clicked(QString)));
117 QSignalSpy seriesSpy(series,SIGNAL(clicked(QBarSet*,QString)));
117 QSignalSpy seriesSpy(series,SIGNAL(clicked(QBarSet*,QString)));
118
118
119 QChartView view(new QChart());
119 QChartView view(new QChart());
120 view.resize(400,300);
120 view.resize(400,300);
121 view.chart()->addSeries(series);
121 view.chart()->addSeries(series);
122 view.show();
122 view.show();
123 QTest::qWaitForWindowShown(&view);
123 QTest::qWaitForWindowShown(&view);
124
124
125 //====================================================================================
125 //====================================================================================
126 // barset 1, category test1
126 // barset 1, category test1
127 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(115,230));
127 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(115,230));
128 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
128 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
129
129
130 QCOMPARE(setSpy1.count(), 1);
130 QCOMPARE(setSpy1.count(), 1);
131 QCOMPARE(setSpy2.count(), 0);
131 QCOMPARE(setSpy2.count(), 0);
132 QCOMPARE(seriesSpy.count(), 1);
132 QCOMPARE(seriesSpy.count(), 1);
133 QList<QVariant> setSpyArg = setSpy1.takeFirst();
133 QList<QVariant> setSpyArg = setSpy1.takeFirst();
134 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
134 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
135 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
135 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
136
136
137 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
137 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
138 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
138 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
139
139
140 //====================================================================================
140 //====================================================================================
141 // barset 1, category test2
141 // barset 1, category test2
142 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(205,230));
142 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(205,230));
143 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
143 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
144
144
145 QCOMPARE(setSpy1.count(), 1);
145 QCOMPARE(setSpy1.count(), 1);
146 QCOMPARE(setSpy2.count(), 0);
146 QCOMPARE(setSpy2.count(), 0);
147 QCOMPARE(seriesSpy.count(), 1);
147 QCOMPARE(seriesSpy.count(), 1);
148 setSpyArg = setSpy1.takeFirst();
148 setSpyArg = setSpy1.takeFirst();
149 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
149 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
150 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
150 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
151
151
152 seriesSpyArg = seriesSpy.takeFirst();
152 seriesSpyArg = seriesSpy.takeFirst();
153 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
153 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
154
154
155 //====================================================================================
155 //====================================================================================
156 // barset 1, category test3
156 // barset 1, category test3
157 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(300,230));
157 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(300,230));
158 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
158 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
159
159
160 QCOMPARE(setSpy1.count(), 1);
160 QCOMPARE(setSpy1.count(), 1);
161 QCOMPARE(setSpy2.count(), 0);
161 QCOMPARE(setSpy2.count(), 0);
162 QCOMPARE(seriesSpy.count(), 1);
162 QCOMPARE(seriesSpy.count(), 1);
163 setSpyArg = setSpy1.takeFirst();
163 setSpyArg = setSpy1.takeFirst();
164 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
164 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
165 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
165 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
166
166
167 seriesSpyArg = seriesSpy.takeFirst();
167 seriesSpyArg = seriesSpy.takeFirst();
168 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
168 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
169
169
170 //====================================================================================
170 //====================================================================================
171 // barset 2, category test1
171 // barset 2, category test1
172 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(115,140));
172 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(115,140));
173 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
173 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
174
174
175 QCOMPARE(setSpy1.count(), 0);
175 QCOMPARE(setSpy1.count(), 0);
176 QCOMPARE(setSpy2.count(), 1);
176 QCOMPARE(setSpy2.count(), 1);
177 QCOMPARE(seriesSpy.count(), 1);
177 QCOMPARE(seriesSpy.count(), 1);
178 setSpyArg = setSpy2.takeFirst();
178 setSpyArg = setSpy2.takeFirst();
179 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
179 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
180 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
180 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
181
181
182 seriesSpyArg = seriesSpy.takeFirst();
182 seriesSpyArg = seriesSpy.takeFirst();
183 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
183 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
184
184
185 //====================================================================================
185 //====================================================================================
186 // barset 2, category test2
186 // barset 2, category test2
187 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(205,140));
187 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(205,140));
188 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
188 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
189
189
190 QCOMPARE(setSpy1.count(), 0);
190 QCOMPARE(setSpy1.count(), 0);
191 QCOMPARE(setSpy2.count(), 1);
191 QCOMPARE(setSpy2.count(), 1);
192 QCOMPARE(seriesSpy.count(), 1);
192 QCOMPARE(seriesSpy.count(), 1);
193 setSpyArg = setSpy2.takeFirst();
193 setSpyArg = setSpy2.takeFirst();
194 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
194 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
195 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
195 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
196
196
197 seriesSpyArg = seriesSpy.takeFirst();
197 seriesSpyArg = seriesSpy.takeFirst();
198 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
198 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
199
199
200 //====================================================================================
200 //====================================================================================
201 // barset 2, category test3
201 // barset 2, category test3
202 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(300,140));
202 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(300,140));
203 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
203 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
204
204
205 QCOMPARE(setSpy1.count(), 0);
205 QCOMPARE(setSpy1.count(), 0);
206 QCOMPARE(setSpy2.count(), 1);
206 QCOMPARE(setSpy2.count(), 1);
207 QCOMPARE(seriesSpy.count(), 1);
207 QCOMPARE(seriesSpy.count(), 1);
208 setSpyArg = setSpy2.takeFirst();
208 setSpyArg = setSpy2.takeFirst();
209 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
209 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
210 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
210 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
211
211
212 seriesSpyArg = seriesSpy.takeFirst();
212 seriesSpyArg = seriesSpy.takeFirst();
213 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
213 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
214
214
215 //====================================================================================
215 //====================================================================================
216 // no event cases
216 // no event cases
217 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(1,1)); // Outside of both sets
217 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(1,1)); // Outside of both sets
218 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(1,1)); // Right mouse button outside and inside sets
218 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(1,1)); // Right mouse button outside and inside sets
219 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(115,230)); // barset 1, category test1
219 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(115,230)); // barset 1, category test1
220 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(205,230)); // barset 1, category test2
220 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(205,230)); // barset 1, category test2
221 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(300,230)); // barset 1, category test3
221 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(300,230)); // barset 1, category test3
222 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(115,140)); // barset 2, category test1
222 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(115,140)); // barset 2, category test1
223 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(205,140)); // barset 2, category test2
223 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(205,140)); // barset 2, category test2
224 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(300,140)); // barset 2, category test3
224 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(300,140)); // barset 2, category test3
225
225
226 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
226 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
227 QCOMPARE(setSpy1.count(), 0);
227 QCOMPARE(setSpy1.count(), 0);
228 QCOMPARE(setSpy2.count(), 0);
228 QCOMPARE(setSpy2.count(), 0);
229 QCOMPARE(seriesSpy.count(), 0);
229 QCOMPARE(seriesSpy.count(), 0);
230 }
230 }
231
231
232 void tst_QPercentBarSeries::mousehovered_data()
232 void tst_QPercentBarSeries::mousehovered_data()
233 {
233 {
234
234
235 }
235 }
236
236
237 void tst_QPercentBarSeries::mousehovered()
237 void tst_QPercentBarSeries::mousehovered()
238 {
238 {
239 QPercentBarSeries* series = new QPercentBarSeries();
239 QPercentBarSeries* series = new QPercentBarSeries();
240 QBarCategories categories;
240 QBarCategories categories;
241 categories << "test1" << "test2" << "test3";
241 categories << "test1" << "test2" << "test3";
242 series->setCategories(categories);
242 series->setCategories(categories);
243
243
244 QBarSet* set1 = new QBarSet(QString("set 1"));
244 QBarSet* set1 = new QBarSet(QString("set 1"));
245 *set1 << 10 << 10 << 10;
245 *set1 << 10 << 10 << 10;
246 series->appendBarSet(set1);
246 series->append(set1);
247
247
248 QBarSet* set2 = new QBarSet(QString("set 2"));
248 QBarSet* set2 = new QBarSet(QString("set 2"));
249 *set2 << 10 << 10 << 10;
249 *set2 << 10 << 10 << 10;
250 series->appendBarSet(set2);
250 series->append(set2);
251
251
252 QSignalSpy setSpy1(set1, SIGNAL(hovered(bool)));
252 QSignalSpy setSpy1(set1, SIGNAL(hovered(bool)));
253 QSignalSpy setSpy2(set2, SIGNAL(hovered(bool)));
253 QSignalSpy setSpy2(set2, SIGNAL(hovered(bool)));
254 QSignalSpy seriesSpy(series,SIGNAL(hovered(QBarSet*,bool)));
254 QSignalSpy seriesSpy(series,SIGNAL(hovered(QBarSet*,bool)));
255
255
256 QChartView view(new QChart());
256 QChartView view(new QChart());
257 view.resize(400,300);
257 view.resize(400,300);
258 view.chart()->addSeries(series);
258 view.chart()->addSeries(series);
259 view.show();
259 view.show();
260 QTest::qWaitForWindowShown(&view);
260 QTest::qWaitForWindowShown(&view);
261
261
262 //=======================================================================
262 //=======================================================================
263 // move mouse to left border
263 // move mouse to left border
264 QTest::mouseMove(view.viewport(), QPoint(0, 230));
264 QTest::mouseMove(view.viewport(), QPoint(0, 230));
265
265
266
266
267 QVERIFY(setSpy1.count() == 0);
267 QVERIFY(setSpy1.count() == 0);
268 QVERIFY(setSpy2.count() == 0);
268 QVERIFY(setSpy2.count() == 0);
269 QVERIFY(seriesSpy.count() == 0);
269 QVERIFY(seriesSpy.count() == 0);
270
270
271 //=======================================================================
271 //=======================================================================
272 // move mouse on top of set1
272 // move mouse on top of set1
273 QTest::mouseMove(view.viewport(), QPoint(115,230));
273 QTest::mouseMove(view.viewport(), QPoint(115,230));
274
274
275 QVERIFY(setSpy1.count() == 1);
275 QVERIFY(setSpy1.count() == 1);
276 QVERIFY(setSpy2.count() == 0);
276 QVERIFY(setSpy2.count() == 0);
277 QVERIFY(seriesSpy.count() == 1);
277 QVERIFY(seriesSpy.count() == 1);
278
278
279 QList<QVariant> setSpyArg = setSpy1.takeFirst();
279 QList<QVariant> setSpyArg = setSpy1.takeFirst();
280 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
280 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
281 QVERIFY(setSpyArg.at(0).toBool() == true);
281 QVERIFY(setSpyArg.at(0).toBool() == true);
282
282
283 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
283 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
284 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
284 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
285
285
286 //=======================================================================
286 //=======================================================================
287 // move mouse from top of set1 to top of set2
287 // move mouse from top of set1 to top of set2
288 QTest::mouseMove(view.viewport(), QPoint(115,140));
288 QTest::mouseMove(view.viewport(), QPoint(115,140));
289
289
290 QVERIFY(setSpy1.count() == 1);
290 QVERIFY(setSpy1.count() == 1);
291 QVERIFY(setSpy2.count() == 1);
291 QVERIFY(setSpy2.count() == 1);
292 QVERIFY(seriesSpy.count() == 2);
292 QVERIFY(seriesSpy.count() == 2);
293
293
294 // should leave set1
294 // should leave set1
295 setSpyArg = setSpy1.takeFirst();
295 setSpyArg = setSpy1.takeFirst();
296 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
296 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
297 QVERIFY(setSpyArg.at(0).toBool() == false);
297 QVERIFY(setSpyArg.at(0).toBool() == false);
298
298
299 // should enter set2
299 // should enter set2
300 setSpyArg = setSpy2.takeFirst();
300 setSpyArg = setSpy2.takeFirst();
301 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
301 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
302 QVERIFY(setSpyArg.at(0).toBool() == true);
302 QVERIFY(setSpyArg.at(0).toBool() == true);
303
303
304 // should leave set1
304 // should leave set1
305 seriesSpyArg = seriesSpy.takeFirst();
305 seriesSpyArg = seriesSpy.takeFirst();
306 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
306 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
307 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
307 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
308 QVERIFY(seriesSpyArg.at(1).toBool() == false);
308 QVERIFY(seriesSpyArg.at(1).toBool() == false);
309
309
310 // should enter set2
310 // should enter set2
311 seriesSpyArg = seriesSpy.takeFirst();
311 seriesSpyArg = seriesSpy.takeFirst();
312 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
312 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
313 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
313 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
314 QVERIFY(seriesSpyArg.at(1).toBool() == true);
314 QVERIFY(seriesSpyArg.at(1).toBool() == true);
315
315
316 //=======================================================================
316 //=======================================================================
317 // move mouse from top of set2 to background
317 // move mouse from top of set2 to background
318 QTest::mouseMove(view.viewport(), QPoint(0,140));
318 QTest::mouseMove(view.viewport(), QPoint(0,140));
319
319
320 QVERIFY(setSpy1.count() == 0);
320 QVERIFY(setSpy1.count() == 0);
321 QVERIFY(setSpy2.count() == 1);
321 QVERIFY(setSpy2.count() == 1);
322 QVERIFY(seriesSpy.count() == 1);
322 QVERIFY(seriesSpy.count() == 1);
323
323
324 // should leave set2 (event via set)
324 // should leave set2 (event via set)
325 setSpyArg = setSpy2.takeFirst();
325 setSpyArg = setSpy2.takeFirst();
326 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
326 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
327 QVERIFY(setSpyArg.at(0).toBool() == false);
327 QVERIFY(setSpyArg.at(0).toBool() == false);
328
328
329 // should leave set2 (event via series)
329 // should leave set2 (event via series)
330 seriesSpyArg = seriesSpy.takeFirst();
330 seriesSpyArg = seriesSpy.takeFirst();
331 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
331 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
332 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
332 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
333 QVERIFY(seriesSpyArg.at(1).toBool() == false);
333 QVERIFY(seriesSpyArg.at(1).toBool() == false);
334 }
334 }
335
335
336 /*
336 /*
337 bool setModel(QAbstractItemModel *model);
337 bool setModel(QAbstractItemModel *model);
338 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
338 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
339 void setModelMappingRange(int first, int count = -1);
339 void setModelMappingRange(int first, int count = -1);
340 */
340 */
341 QTEST_MAIN(tst_QPercentBarSeries)
341 QTEST_MAIN(tst_QPercentBarSeries)
342
342
343 #include "tst_qpercentbarseries.moc"
343 #include "tst_qpercentbarseries.moc"
344
344
@@ -1,344 +1,344
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 <qstackedbarseries.h>
22 #include <qstackedbarseries.h>
23 #include <qbarset.h>
23 #include <qbarset.h>
24 #include <qchartview.h>
24 #include <qchartview.h>
25 #include <qchart.h>
25 #include <qchart.h>
26
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
28
29 Q_DECLARE_METATYPE(QBarSet*)
29 Q_DECLARE_METATYPE(QBarSet*)
30
30
31 class tst_QStackedBarSeries : public QObject
31 class tst_QStackedBarSeries : public QObject
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public slots:
35 public slots:
36 void initTestCase();
36 void initTestCase();
37 void cleanupTestCase();
37 void cleanupTestCase();
38 void init();
38 void init();
39 void cleanup();
39 void cleanup();
40
40
41 private slots:
41 private slots:
42 void qstackedbarseries_data();
42 void qstackedbarseries_data();
43 void qstackedbarseries();
43 void qstackedbarseries();
44 void type_data();
44 void type_data();
45 void type();
45 void type();
46 void mouseclicked_data();
46 void mouseclicked_data();
47 void mouseclicked();
47 void mouseclicked();
48 void mousehovered_data();
48 void mousehovered_data();
49 void mousehovered();
49 void mousehovered();
50
50
51 private:
51 private:
52 QStackedBarSeries* m_barseries;
52 QStackedBarSeries* m_barseries;
53 };
53 };
54
54
55 void tst_QStackedBarSeries::initTestCase()
55 void tst_QStackedBarSeries::initTestCase()
56 {
56 {
57 qRegisterMetaType<QBarSet*>("QBarSet*");
57 qRegisterMetaType<QBarSet*>("QBarSet*");
58 }
58 }
59
59
60 void tst_QStackedBarSeries::cleanupTestCase()
60 void tst_QStackedBarSeries::cleanupTestCase()
61 {
61 {
62 }
62 }
63
63
64 void tst_QStackedBarSeries::init()
64 void tst_QStackedBarSeries::init()
65 {
65 {
66 m_barseries = new QStackedBarSeries();
66 m_barseries = new QStackedBarSeries();
67 }
67 }
68
68
69 void tst_QStackedBarSeries::cleanup()
69 void tst_QStackedBarSeries::cleanup()
70 {
70 {
71 delete m_barseries;
71 delete m_barseries;
72 m_barseries = 0;
72 m_barseries = 0;
73 }
73 }
74
74
75 void tst_QStackedBarSeries::qstackedbarseries_data()
75 void tst_QStackedBarSeries::qstackedbarseries_data()
76 {
76 {
77 }
77 }
78
78
79 void tst_QStackedBarSeries::qstackedbarseries()
79 void tst_QStackedBarSeries::qstackedbarseries()
80 {
80 {
81 QStackedBarSeries *barseries = new QStackedBarSeries();
81 QStackedBarSeries *barseries = new QStackedBarSeries();
82 QVERIFY(barseries != 0);
82 QVERIFY(barseries != 0);
83 }
83 }
84
84
85 void tst_QStackedBarSeries::type_data()
85 void tst_QStackedBarSeries::type_data()
86 {
86 {
87
87
88 }
88 }
89
89
90 void tst_QStackedBarSeries::type()
90 void tst_QStackedBarSeries::type()
91 {
91 {
92 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeStackedBar);
92 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeStackedBar);
93 }
93 }
94
94
95 void tst_QStackedBarSeries::mouseclicked_data()
95 void tst_QStackedBarSeries::mouseclicked_data()
96 {
96 {
97
97
98 }
98 }
99
99
100 void tst_QStackedBarSeries::mouseclicked()
100 void tst_QStackedBarSeries::mouseclicked()
101 {
101 {
102 QStackedBarSeries* series = new QStackedBarSeries();
102 QStackedBarSeries* series = new QStackedBarSeries();
103 QBarCategories categories;
103 QBarCategories categories;
104 categories << "test1" << "test2" << "test3";
104 categories << "test1" << "test2" << "test3";
105 series->setCategories(categories);
105 series->setCategories(categories);
106
106
107 QBarSet* set1 = new QBarSet(QString("set 1"));
107 QBarSet* set1 = new QBarSet(QString("set 1"));
108 *set1 << 10 << 10 << 10;
108 *set1 << 10 << 10 << 10;
109 series->appendBarSet(set1);
109 series->append(set1);
110
110
111 QBarSet* set2 = new QBarSet(QString("set 2"));
111 QBarSet* set2 = new QBarSet(QString("set 2"));
112 *set2 << 10 << 10 << 10;
112 *set2 << 10 << 10 << 10;
113 series->appendBarSet(set2);
113 series->append(set2);
114
114
115 QSignalSpy setSpy1(set1, SIGNAL(clicked(QString)));
115 QSignalSpy setSpy1(set1, SIGNAL(clicked(QString)));
116 QSignalSpy setSpy2(set2, SIGNAL(clicked(QString)));
116 QSignalSpy setSpy2(set2, SIGNAL(clicked(QString)));
117 QSignalSpy seriesSpy(series,SIGNAL(clicked(QBarSet*,QString)));
117 QSignalSpy seriesSpy(series,SIGNAL(clicked(QBarSet*,QString)));
118
118
119 QChartView view(new QChart());
119 QChartView view(new QChart());
120 view.resize(400,300);
120 view.resize(400,300);
121 view.chart()->addSeries(series);
121 view.chart()->addSeries(series);
122 view.show();
122 view.show();
123 QTest::qWaitForWindowShown(&view);
123 QTest::qWaitForWindowShown(&view);
124
124
125 //====================================================================================
125 //====================================================================================
126 // barset 1, category test1
126 // barset 1, category test1
127 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(115,230));
127 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(115,230));
128 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
128 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
129
129
130 QCOMPARE(setSpy1.count(), 1);
130 QCOMPARE(setSpy1.count(), 1);
131 QCOMPARE(setSpy2.count(), 0);
131 QCOMPARE(setSpy2.count(), 0);
132 QCOMPARE(seriesSpy.count(), 1);
132 QCOMPARE(seriesSpy.count(), 1);
133 QList<QVariant> setSpyArg = setSpy1.takeFirst();
133 QList<QVariant> setSpyArg = setSpy1.takeFirst();
134 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
134 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
135 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
135 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
136
136
137 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
137 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
138 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
138 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
139
139
140 //====================================================================================
140 //====================================================================================
141 // barset 1, category test2
141 // barset 1, category test2
142 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(205,230));
142 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(205,230));
143 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
143 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
144
144
145 QCOMPARE(setSpy1.count(), 1);
145 QCOMPARE(setSpy1.count(), 1);
146 QCOMPARE(setSpy2.count(), 0);
146 QCOMPARE(setSpy2.count(), 0);
147 QCOMPARE(seriesSpy.count(), 1);
147 QCOMPARE(seriesSpy.count(), 1);
148 setSpyArg = setSpy1.takeFirst();
148 setSpyArg = setSpy1.takeFirst();
149 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
149 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
150 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
150 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
151
151
152 seriesSpyArg = seriesSpy.takeFirst();
152 seriesSpyArg = seriesSpy.takeFirst();
153 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
153 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
154
154
155 //====================================================================================
155 //====================================================================================
156 // barset 1, category test3
156 // barset 1, category test3
157 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(300,230));
157 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(300,230));
158 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
158 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
159
159
160 QCOMPARE(setSpy1.count(), 1);
160 QCOMPARE(setSpy1.count(), 1);
161 QCOMPARE(setSpy2.count(), 0);
161 QCOMPARE(setSpy2.count(), 0);
162 QCOMPARE(seriesSpy.count(), 1);
162 QCOMPARE(seriesSpy.count(), 1);
163 setSpyArg = setSpy1.takeFirst();
163 setSpyArg = setSpy1.takeFirst();
164 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
164 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
165 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
165 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
166
166
167 seriesSpyArg = seriesSpy.takeFirst();
167 seriesSpyArg = seriesSpy.takeFirst();
168 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
168 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
169
169
170 //====================================================================================
170 //====================================================================================
171 // barset 2, category test1
171 // barset 2, category test1
172 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(115,140));
172 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(115,140));
173 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
173 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
174
174
175 QCOMPARE(setSpy1.count(), 0);
175 QCOMPARE(setSpy1.count(), 0);
176 QCOMPARE(setSpy2.count(), 1);
176 QCOMPARE(setSpy2.count(), 1);
177 QCOMPARE(seriesSpy.count(), 1);
177 QCOMPARE(seriesSpy.count(), 1);
178 setSpyArg = setSpy2.takeFirst();
178 setSpyArg = setSpy2.takeFirst();
179 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
179 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
180 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
180 QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0);
181
181
182 seriesSpyArg = seriesSpy.takeFirst();
182 seriesSpyArg = seriesSpy.takeFirst();
183 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
183 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
184
184
185 //====================================================================================
185 //====================================================================================
186 // barset 2, category test2
186 // barset 2, category test2
187 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(205,140));
187 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(205,140));
188 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
188 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
189
189
190 QCOMPARE(setSpy1.count(), 0);
190 QCOMPARE(setSpy1.count(), 0);
191 QCOMPARE(setSpy2.count(), 1);
191 QCOMPARE(setSpy2.count(), 1);
192 QCOMPARE(seriesSpy.count(), 1);
192 QCOMPARE(seriesSpy.count(), 1);
193 setSpyArg = setSpy2.takeFirst();
193 setSpyArg = setSpy2.takeFirst();
194 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
194 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
195 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
195 QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0);
196
196
197 seriesSpyArg = seriesSpy.takeFirst();
197 seriesSpyArg = seriesSpy.takeFirst();
198 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
198 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
199
199
200 //====================================================================================
200 //====================================================================================
201 // barset 2, category test3
201 // barset 2, category test3
202 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(300,140));
202 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(300,140));
203 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
203 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
204
204
205 QCOMPARE(setSpy1.count(), 0);
205 QCOMPARE(setSpy1.count(), 0);
206 QCOMPARE(setSpy2.count(), 1);
206 QCOMPARE(setSpy2.count(), 1);
207 QCOMPARE(seriesSpy.count(), 1);
207 QCOMPARE(seriesSpy.count(), 1);
208 setSpyArg = setSpy2.takeFirst();
208 setSpyArg = setSpy2.takeFirst();
209 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
209 QVERIFY(setSpyArg.at(0).type() == QVariant::String);
210 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
210 QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0);
211
211
212 seriesSpyArg = seriesSpy.takeFirst();
212 seriesSpyArg = seriesSpy.takeFirst();
213 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
213 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
214
214
215 //====================================================================================
215 //====================================================================================
216 // no event cases
216 // no event cases
217 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(1,1)); // Outside of both sets
217 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(1,1)); // Outside of both sets
218 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(1,1)); // Right mouse button outside and inside sets
218 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(1,1)); // Right mouse button outside and inside sets
219 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(115,230)); // barset 1, category test1
219 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(115,230)); // barset 1, category test1
220 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(205,230)); // barset 1, category test2
220 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(205,230)); // barset 1, category test2
221 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(300,230)); // barset 1, category test3
221 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(300,230)); // barset 1, category test3
222 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(115,140)); // barset 2, category test1
222 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(115,140)); // barset 2, category test1
223 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(205,140)); // barset 2, category test2
223 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(205,140)); // barset 2, category test2
224 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(300,140)); // barset 2, category test3
224 QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(300,140)); // barset 2, category test3
225
225
226 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
226 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
227 QCOMPARE(setSpy1.count(), 0);
227 QCOMPARE(setSpy1.count(), 0);
228 QCOMPARE(setSpy2.count(), 0);
228 QCOMPARE(setSpy2.count(), 0);
229 QCOMPARE(seriesSpy.count(), 0);
229 QCOMPARE(seriesSpy.count(), 0);
230 }
230 }
231
231
232 void tst_QStackedBarSeries::mousehovered_data()
232 void tst_QStackedBarSeries::mousehovered_data()
233 {
233 {
234
234
235 }
235 }
236
236
237 void tst_QStackedBarSeries::mousehovered()
237 void tst_QStackedBarSeries::mousehovered()
238 {
238 {
239 QStackedBarSeries* series = new QStackedBarSeries();
239 QStackedBarSeries* series = new QStackedBarSeries();
240 QBarCategories categories;
240 QBarCategories categories;
241 categories << "test1" << "test2" << "test3";
241 categories << "test1" << "test2" << "test3";
242 series->setCategories(categories);
242 series->setCategories(categories);
243
243
244 QBarSet* set1 = new QBarSet(QString("set 1"));
244 QBarSet* set1 = new QBarSet(QString("set 1"));
245 *set1 << 10 << 10 << 10;
245 *set1 << 10 << 10 << 10;
246 series->appendBarSet(set1);
246 series->append(set1);
247
247
248 QBarSet* set2 = new QBarSet(QString("set 2"));
248 QBarSet* set2 = new QBarSet(QString("set 2"));
249 *set2 << 10 << 10 << 10;
249 *set2 << 10 << 10 << 10;
250 series->appendBarSet(set2);
250 series->append(set2);
251
251
252 QSignalSpy setSpy1(set1, SIGNAL(hovered(bool)));
252 QSignalSpy setSpy1(set1, SIGNAL(hovered(bool)));
253 QSignalSpy setSpy2(set2, SIGNAL(hovered(bool)));
253 QSignalSpy setSpy2(set2, SIGNAL(hovered(bool)));
254 QSignalSpy seriesSpy(series,SIGNAL(hovered(QBarSet*,bool)));
254 QSignalSpy seriesSpy(series,SIGNAL(hovered(QBarSet*,bool)));
255
255
256 QChartView view(new QChart());
256 QChartView view(new QChart());
257 view.resize(400,300);
257 view.resize(400,300);
258 view.chart()->addSeries(series);
258 view.chart()->addSeries(series);
259 view.show();
259 view.show();
260 QTest::qWaitForWindowShown(&view);
260 QTest::qWaitForWindowShown(&view);
261
261
262 //=======================================================================
262 //=======================================================================
263 // move mouse to left border
263 // move mouse to left border
264 QTest::mouseMove(view.viewport(), QPoint(0, 230));
264 QTest::mouseMove(view.viewport(), QPoint(0, 230));
265
265
266 QVERIFY(setSpy1.count() == 0);
266 QVERIFY(setSpy1.count() == 0);
267 QVERIFY(setSpy2.count() == 0);
267 QVERIFY(setSpy2.count() == 0);
268 QVERIFY(seriesSpy.count() == 0);
268 QVERIFY(seriesSpy.count() == 0);
269
269
270 //=======================================================================
270 //=======================================================================
271 // move mouse on top of set1
271 // move mouse on top of set1
272 QTest::mouseMove(view.viewport(), QPoint(115,230));
272 QTest::mouseMove(view.viewport(), QPoint(115,230));
273
273
274 QVERIFY(setSpy1.count() == 1);
274 QVERIFY(setSpy1.count() == 1);
275 QVERIFY(setSpy2.count() == 0);
275 QVERIFY(setSpy2.count() == 0);
276 QVERIFY(seriesSpy.count() == 1);
276 QVERIFY(seriesSpy.count() == 1);
277
277
278 QList<QVariant> setSpyArg = setSpy1.takeFirst();
278 QList<QVariant> setSpyArg = setSpy1.takeFirst();
279 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
279 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
280 QVERIFY(setSpyArg.at(0).toBool() == true);
280 QVERIFY(setSpyArg.at(0).toBool() == true);
281
281
282 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
282 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
283 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
283 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
284
284
285 //=======================================================================
285 //=======================================================================
286 // move mouse from top of set1 to top of set2
286 // move mouse from top of set1 to top of set2
287 QTest::mouseMove(view.viewport(), QPoint(115,140));
287 QTest::mouseMove(view.viewport(), QPoint(115,140));
288
288
289 QVERIFY(setSpy1.count() == 1);
289 QVERIFY(setSpy1.count() == 1);
290 QVERIFY(setSpy2.count() == 1);
290 QVERIFY(setSpy2.count() == 1);
291 QVERIFY(seriesSpy.count() == 2);
291 QVERIFY(seriesSpy.count() == 2);
292
292
293 // should leave set1
293 // should leave set1
294 setSpyArg = setSpy1.takeFirst();
294 setSpyArg = setSpy1.takeFirst();
295 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
295 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
296 QVERIFY(setSpyArg.at(0).toBool() == false);
296 QVERIFY(setSpyArg.at(0).toBool() == false);
297
297
298 // should enter set2
298 // should enter set2
299 setSpyArg = setSpy2.takeFirst();
299 setSpyArg = setSpy2.takeFirst();
300 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
300 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
301 QVERIFY(setSpyArg.at(0).toBool() == true);
301 QVERIFY(setSpyArg.at(0).toBool() == true);
302
302
303 // should leave set1
303 // should leave set1
304 seriesSpyArg = seriesSpy.takeFirst();
304 seriesSpyArg = seriesSpy.takeFirst();
305 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
305 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
306 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
306 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
307 QVERIFY(seriesSpyArg.at(1).toBool() == false);
307 QVERIFY(seriesSpyArg.at(1).toBool() == false);
308
308
309 // should enter set2
309 // should enter set2
310 seriesSpyArg = seriesSpy.takeFirst();
310 seriesSpyArg = seriesSpy.takeFirst();
311 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
311 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
312 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
312 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
313 QVERIFY(seriesSpyArg.at(1).toBool() == true);
313 QVERIFY(seriesSpyArg.at(1).toBool() == true);
314
314
315 //=======================================================================
315 //=======================================================================
316 // move mouse from top of set2 to background
316 // move mouse from top of set2 to background
317 QTest::mouseMove(view.viewport(), QPoint(0,140));
317 QTest::mouseMove(view.viewport(), QPoint(0,140));
318
318
319 QVERIFY(setSpy1.count() == 0);
319 QVERIFY(setSpy1.count() == 0);
320 QVERIFY(setSpy2.count() == 1);
320 QVERIFY(setSpy2.count() == 1);
321 QVERIFY(seriesSpy.count() == 1);
321 QVERIFY(seriesSpy.count() == 1);
322
322
323 // should leave set2 (event via set)
323 // should leave set2 (event via set)
324 setSpyArg = setSpy2.takeFirst();
324 setSpyArg = setSpy2.takeFirst();
325 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
325 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
326 QVERIFY(setSpyArg.at(0).toBool() == false);
326 QVERIFY(setSpyArg.at(0).toBool() == false);
327
327
328 // should leave set2 (event via series)
328 // should leave set2 (event via series)
329 seriesSpyArg = seriesSpy.takeFirst();
329 seriesSpyArg = seriesSpy.takeFirst();
330 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
330 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
331 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
331 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
332 QVERIFY(seriesSpyArg.at(1).toBool() == false);
332 QVERIFY(seriesSpyArg.at(1).toBool() == false);
333 }
333 }
334
334
335
335
336 /*
336 /*
337 bool setModel(QAbstractItemModel *model);
337 bool setModel(QAbstractItemModel *model);
338 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
338 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
339 void setModelMappingRange(int first, int count = -1);
339 void setModelMappingRange(int first, int count = -1);
340 */
340 */
341 QTEST_MAIN(tst_QStackedBarSeries)
341 QTEST_MAIN(tst_QStackedBarSeries)
342
342
343 #include "tst_qstackedbarseries.moc"
343 #include "tst_qstackedbarseries.moc"
344
344
@@ -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(column.at(i), labels.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->appendBarSet(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 }
@@ -1,245 +1,245
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 "widget.h"
21 #include "widget.h"
22
22
23 #include <QChart>
23 #include <QChart>
24 #include <QScatterSeries>
24 #include <QScatterSeries>
25 #include <QAxis>
25 #include <QAxis>
26 #include <QBarSet>
26 #include <QBarSet>
27 #include <QBarSeries>
27 #include <QBarSeries>
28 #include <QLegend>
28 #include <QLegend>
29
29
30 #include <QGridLayout>
30 #include <QGridLayout>
31 #include <QPushButton>
31 #include <QPushButton>
32 #include <QLabel>
32 #include <QLabel>
33 #include <QListWidget>
33 #include <QListWidget>
34 #include <QPrinter>
34 #include <QPrinter>
35 #include <QPrintDialog>
35 #include <QPrintDialog>
36 #include <QRadioButton>
36 #include <QRadioButton>
37 #include <QStringList>
37 #include <QStringList>
38 #include <QSqlQuery>
38 #include <QSqlQuery>
39 #include <QDebug>
39 #include <QDebug>
40
40
41 QTCOMMERCIALCHART_USE_NAMESPACE
41 QTCOMMERCIALCHART_USE_NAMESPACE
42
42
43 Widget::Widget(QWidget *parent)
43 Widget::Widget(QWidget *parent)
44 : QWidget(parent)
44 : QWidget(parent)
45 {
45 {
46 setGeometry(100, 100, 1000, 600);
46 setGeometry(100, 100, 1000, 600);
47
47
48 // right panel layout
48 // right panel layout
49 barChartRadioButton = new QRadioButton(tr("Bar chart"));
49 barChartRadioButton = new QRadioButton(tr("Bar chart"));
50 barChartRadioButton->setChecked(true);
50 barChartRadioButton->setChecked(true);
51 scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
51 scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
52 scatterChartRadioButton->setChecked(false);
52 scatterChartRadioButton->setChecked(false);
53 countrieslist = new QListWidget;
53 countrieslist = new QListWidget;
54 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
54 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
55
55
56 //list of years widget
56 //list of years widget
57 yearslist = new QListWidget;
57 yearslist = new QListWidget;
58 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
58 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
59 for (int i = 1990; i < 2011; i++)
59 for (int i = 1990; i < 2011; i++)
60 yearslist->addItem(QString("%1").arg(i));
60 yearslist->addItem(QString("%1").arg(i));
61
61
62 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
62 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
63 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
63 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
64
64
65 QPushButton* printButton = new QPushButton(tr("Print to pdf"));
65 QPushButton* printButton = new QPushButton(tr("Print to pdf"));
66 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
66 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
67
67
68 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
68 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
69 rightPanelLayout->addWidget(barChartRadioButton);
69 rightPanelLayout->addWidget(barChartRadioButton);
70 rightPanelLayout->addWidget(scatterChartRadioButton);
70 rightPanelLayout->addWidget(scatterChartRadioButton);
71 rightPanelLayout->addWidget(countrieslist);
71 rightPanelLayout->addWidget(countrieslist);
72 rightPanelLayout->addWidget(yearslist);
72 rightPanelLayout->addWidget(yearslist);
73 rightPanelLayout->addWidget(refreshButton);
73 rightPanelLayout->addWidget(refreshButton);
74 rightPanelLayout->addWidget(printButton);
74 rightPanelLayout->addWidget(printButton);
75 rightPanelLayout->setStretch(0, 1);
75 rightPanelLayout->setStretch(0, 1);
76 rightPanelLayout->setStretch(1, 0);
76 rightPanelLayout->setStretch(1, 0);
77
77
78 QChart *chart = new QChart();
78 QChart *chart = new QChart();
79 chart->setTitle("GDP by country");
79 chart->setTitle("GDP by country");
80 chart->legend()->setVisible(true);
80 chart->legend()->setVisible(true);
81
81
82 // main layout
82 // main layout
83 chartView = new QChartView(chart);
83 chartView = new QChartView(chart);
84 QGridLayout* mainLayout = new QGridLayout;
84 QGridLayout* mainLayout = new QGridLayout;
85 mainLayout->addWidget(chartView, 0, 0);
85 mainLayout->addWidget(chartView, 0, 0);
86 mainLayout->addLayout(rightPanelLayout, 0, 1);
86 mainLayout->addLayout(rightPanelLayout, 0, 1);
87 mainLayout->setColumnStretch(0,1);
87 mainLayout->setColumnStretch(0,1);
88 setLayout(mainLayout);
88 setLayout(mainLayout);
89
89
90 // connect to the database
90 // connect to the database
91 db = QSqlDatabase::addDatabase("QSQLITE");
91 db = QSqlDatabase::addDatabase("QSQLITE");
92 db.setDatabaseName("gdpData");
92 db.setDatabaseName("gdpData");
93 if(!db.open())
93 if(!db.open())
94 {
94 {
95 qDebug() << "could not open database. SQLite db file missing (?)";
95 qDebug() << "could not open database. SQLite db file missing (?)";
96 return;
96 return;
97 }
97 }
98
98
99 // get the list of all countires and regions.
99 // get the list of all countires and regions.
100 QSqlQuery query;
100 QSqlQuery query;
101 query.exec("SELECT DISTINCT country FROM gdp2");
101 query.exec("SELECT DISTINCT country FROM gdp2");
102
102
103 // add the countries to the country filter
103 // add the countries to the country filter
104 while (query.next()) {
104 while (query.next()) {
105 countrieslist->addItem(query.value(0).toString());
105 countrieslist->addItem(query.value(0).toString());
106 }
106 }
107 }
107 }
108
108
109 Widget::~Widget()
109 Widget::~Widget()
110 {
110 {
111 //
111 //
112 db.close();
112 db.close();
113 }
113 }
114
114
115 /*!
115 /*!
116 refreshes the chart
116 refreshes the chart
117 */
117 */
118 void Widget::refreshChart()
118 void Widget::refreshChart()
119 {
119 {
120 chartView->chart()->removeAllSeries();
120 chartView->chart()->removeAllSeries();
121
121
122 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
122 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
123 QStringList selectedCountriesStrings;
123 QStringList selectedCountriesStrings;
124 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
124 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
125 for (int i = 0; i < selectedCountriesItems.size(); i++)
125 for (int i = 0; i < selectedCountriesItems.size(); i++)
126 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
126 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
127 selectedCountriesStrings.sort();
127 selectedCountriesStrings.sort();
128
128
129 QSqlQuery query;
129 QSqlQuery query;
130 // selected years items list is not sorted. copy the values to QList<int> and sort them.
130 // selected years items list is not sorted. copy the values to QList<int> and sort them.
131 QList<int> selectedYearsInts;
131 QList<int> selectedYearsInts;
132 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
132 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
133 for (int i = 0; i < selectedYearsItems.size(); i++)
133 for (int i = 0; i < selectedYearsItems.size(); i++)
134 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
134 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
135 qSort(selectedYearsInts.begin(), selectedYearsInts.end());
135 qSort(selectedYearsInts.begin(), selectedYearsInts.end());
136
136
137 if (barChartRadioButton->isChecked())
137 if (barChartRadioButton->isChecked())
138 {
138 {
139 // use the sorted selected coutries list to initialize BarCategory
139 // use the sorted selected coutries list to initialize BarCategory
140 QStringList category;
140 QStringList category;
141 for (int i = 0; i < selectedCountriesStrings.size(); i++)
141 for (int i = 0; i < selectedCountriesStrings.size(); i++)
142 category << selectedCountriesStrings[i];
142 category << selectedCountriesStrings[i];
143 QBarSeries* series0 = new QBarSeries();
143 QBarSeries* series0 = new QBarSeries();
144 series0->setCategories(category);
144 series0->setCategories(category);
145 // series0 = new QBarSeries(category);
145 // series0 = new QBarSeries(category);
146
146
147 // prepare the selected counries SQL query
147 // prepare the selected counries SQL query
148 QString countriesQuery = "country IN (";
148 QString countriesQuery = "country IN (";
149 for (int i = 0; i < selectedCountriesStrings.size(); i++)
149 for (int i = 0; i < selectedCountriesStrings.size(); i++)
150 {
150 {
151 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
151 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
152 if ( i < selectedCountriesStrings.size() - 1)
152 if ( i < selectedCountriesStrings.size() - 1)
153 countriesQuery.append(",");
153 countriesQuery.append(",");
154 else
154 else
155 countriesQuery.append(")");
155 countriesQuery.append(")");
156 }
156 }
157
157
158 // perform a query for each selected year
158 // perform a query for each selected year
159 for (int i = 0; i < selectedYearsInts.size(); i++)
159 for (int i = 0; i < selectedYearsInts.size(); i++)
160 {
160 {
161 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
161 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
162 QBarSet* barSet = new QBarSet(QString::number(selectedYearsInts[i]));
162 QBarSet* barSet = new QBarSet(QString::number(selectedYearsInts[i]));
163
163
164 // while (query.next()) {
164 // while (query.next()) {
165 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
165 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
166 // }
166 // }
167 query.first();
167 query.first();
168
168
169 // the data for some of the coutries for some years might be missing.
169 // the data for some of the coutries for some years might be missing.
170 // QBarChart needs bars to have same size
170 // QBarChart needs bars to have same size
171 for (int k = 0; k < selectedCountriesStrings.size(); k++)
171 for (int k = 0; k < selectedCountriesStrings.size(); k++)
172 {
172 {
173 if (selectedCountriesStrings[k] == query.value(0).toString())
173 if (selectedCountriesStrings[k] == query.value(0).toString())
174 {
174 {
175 *barSet << query.value(1).toReal();
175 *barSet << query.value(1).toReal();
176 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
176 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
177 query.next();
177 query.next();
178 }
178 }
179 else
179 else
180 {
180 {
181 // data missing, put 0
181 // data missing, put 0
182 *barSet << 0.0f;
182 *barSet << 0.0f;
183 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]);
183 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]);
184 }
184 }
185 }
185 }
186 series0->appendBarSet(barSet);
186 series0->append(barSet);
187 }
187 }
188 // add the serie to the chart
188 // add the serie to the chart
189 chartView->chart()->addSeries(series0);
189 chartView->chart()->addSeries(series0);
190 }
190 }
191 else if (scatterChartRadioButton->isChecked())
191 else if (scatterChartRadioButton->isChecked())
192 {
192 {
193 QString yearsQuery = "year IN (";
193 QString yearsQuery = "year IN (";
194 for (int i = 0; i < selectedYearsInts.size(); i++)
194 for (int i = 0; i < selectedYearsInts.size(); i++)
195 {
195 {
196 yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
196 yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
197 if ( i < selectedYearsInts.size() - 1)
197 if ( i < selectedYearsInts.size() - 1)
198 yearsQuery.append(",");
198 yearsQuery.append(",");
199 else
199 else
200 yearsQuery.append(")");
200 yearsQuery.append(")");
201 }
201 }
202
202
203 // perform a query for each selected country
203 // perform a query for each selected country
204 for (int i = 0; i < selectedCountriesStrings.size(); i++)
204 for (int i = 0; i < selectedCountriesStrings.size(); i++)
205 {
205 {
206 query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
206 query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
207 query.first();
207 query.first();
208
208
209 QScatterSeries* series = new QScatterSeries;
209 QScatterSeries* series = new QScatterSeries;
210 series->setName(selectedCountriesStrings[i]);
210 series->setName(selectedCountriesStrings[i]);
211 // the data for some of the coutries for some years might be missing.
211 // the data for some of the coutries for some years might be missing.
212 for (int k = 0; k < selectedYearsInts.size(); k++)
212 for (int k = 0; k < selectedYearsInts.size(); k++)
213 {
213 {
214 if (selectedYearsInts[k] == query.value(0).toInt())
214 if (selectedYearsInts[k] == query.value(0).toInt())
215 {
215 {
216 *series << QPointF(query.value(0).toInt() , query.value(1).toReal());
216 *series << QPointF(query.value(0).toInt() , query.value(1).toReal());
217 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
217 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
218 query.next();
218 query.next();
219 }
219 }
220 else
220 else
221 {
221 {
222 // data missing, put 0
222 // data missing, put 0
223 *series << QPointF(selectedYearsInts[k] , 0.0f);
223 *series << QPointF(selectedYearsInts[k] , 0.0f);
224 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
224 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
225 }
225 }
226 }
226 }
227 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
227 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
228 chartView->chart()->addSeries(series);
228 chartView->chart()->addSeries(series);
229 }
229 }
230 chartView->chart()->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1);
230 chartView->chart()->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1);
231 }
231 }
232 }
232 }
233
233
234 void Widget::printChart()
234 void Widget::printChart()
235 {
235 {
236 QPrinter printer;
236 QPrinter printer;
237 // QPrinter printer(QPrinter::HighResolution);
237 // QPrinter printer(QPrinter::HighResolution);
238 printer.setOutputFormat(QPrinter::PdfFormat);
238 printer.setOutputFormat(QPrinter::PdfFormat);
239 printer.setOrientation(QPrinter::Landscape);
239 printer.setOrientation(QPrinter::Landscape);
240 printer.setOutputFileName("print.pdf");
240 printer.setOutputFileName("print.pdf");
241
241
242 QPainter painter;
242 QPainter painter;
243 painter.begin(&printer);
243 painter.begin(&printer);
244 chartView->render(&painter);
244 chartView->render(&painter);
245 }
245 }
General Comments 0
You need to be logged in to leave comments. Login now