@@ -1,477 +1,479 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include <QtTest/QtTest> |
|
22 | 22 | #include <qchartview.h> |
|
23 | 23 | #include <qchart.h> |
|
24 | 24 | #include <qpieseries.h> |
|
25 | 25 | #include <qpieslice.h> |
|
26 | 26 | #include <qpiemodelmapper.h> |
|
27 | 27 | #include <QStandardItemModel> |
|
28 | 28 | #include <tst_definitions.h> |
|
29 | 29 | |
|
30 | 30 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
31 | 31 | |
|
32 | 32 | Q_DECLARE_METATYPE(QPieSlice*) |
|
33 | 33 | |
|
34 | 34 | class tst_qpieseries : public QObject |
|
35 | 35 | { |
|
36 | 36 | Q_OBJECT |
|
37 | 37 | |
|
38 | 38 | public slots: |
|
39 | 39 | void initTestCase(); |
|
40 | 40 | void cleanupTestCase(); |
|
41 | 41 | void init(); |
|
42 | 42 | void cleanup(); |
|
43 | 43 | |
|
44 | 44 | private slots: |
|
45 | 45 | void construction(); |
|
46 | 46 | void append(); |
|
47 | 47 | void insert(); |
|
48 | 48 | void remove(); |
|
49 | 49 | void calculatedValues(); |
|
50 | 50 | void clickedSignal(); |
|
51 | 51 | void hoverSignal(); |
|
52 | 52 | void model(); |
|
53 | 53 | void modelCustomMap(); |
|
54 | 54 | void modelUpdate(); |
|
55 | 55 | |
|
56 | 56 | private: |
|
57 | 57 | void verifyCalculatedData(const QPieSeries &series, bool *ok); |
|
58 | 58 | |
|
59 | 59 | private: |
|
60 | 60 | |
|
61 | 61 | }; |
|
62 | 62 | |
|
63 | 63 | void tst_qpieseries::initTestCase() |
|
64 | 64 | { |
|
65 | 65 | qRegisterMetaType<QPieSlice*>("QPieSlice*"); |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | void tst_qpieseries::cleanupTestCase() |
|
69 | 69 | { |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | void tst_qpieseries::init() |
|
73 | 73 | { |
|
74 | 74 | |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | void tst_qpieseries::cleanup() |
|
78 | 78 | { |
|
79 | 79 | |
|
80 | 80 | } |
|
81 | 81 | |
|
82 | 82 | void tst_qpieseries::construction() |
|
83 | 83 | { |
|
84 | 84 | // verify default values |
|
85 | 85 | QPieSeries s; |
|
86 | 86 | QVERIFY(s.type() == QAbstractSeries::SeriesTypePie); |
|
87 | 87 | QVERIFY(s.count() == 0); |
|
88 | 88 | QVERIFY(s.isEmpty()); |
|
89 | 89 | QCOMPARE(s.sum(), 0.0); |
|
90 | 90 | QCOMPARE(s.horizontalPosition(), 0.5); |
|
91 | 91 | QCOMPARE(s.verticalPosition(), 0.5); |
|
92 | 92 | QCOMPARE(s.pieSize(), 0.7); |
|
93 | 93 | QCOMPARE(s.pieStartAngle(), 0.0); |
|
94 | 94 | QCOMPARE(s.pieEndAngle(), 360.0); |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | void tst_qpieseries::append() |
|
98 | 98 | { |
|
99 | 99 | QPieSeries s; |
|
100 | 100 | |
|
101 | 101 | // append pointer |
|
102 | 102 | QPieSlice *slice1 = 0; |
|
103 | 103 | QVERIFY(!s.append(slice1)); |
|
104 | 104 | slice1 = new QPieSlice(1, "slice 1"); |
|
105 | 105 | QVERIFY(s.append(slice1)); |
|
106 | 106 | QVERIFY(!s.append(slice1)); |
|
107 | 107 | QCOMPARE(s.count(), 1); |
|
108 | 108 | |
|
109 | 109 | // append pointer list |
|
110 | 110 | QList<QPieSlice *> list; |
|
111 | 111 | QVERIFY(!s.append(list)); |
|
112 | 112 | list << (QPieSlice *) 0; |
|
113 | 113 | QVERIFY(!s.append(list)); |
|
114 | 114 | list.clear(); |
|
115 | 115 | list << new QPieSlice(2, "slice 2"); |
|
116 | 116 | list << new QPieSlice(3, "slice 3"); |
|
117 | 117 | QVERIFY(s.append(list)); |
|
118 | 118 | QVERIFY(!s.append(list)); |
|
119 | 119 | QCOMPARE(s.count(), 3); |
|
120 | 120 | |
|
121 | 121 | // append operator |
|
122 | 122 | s << new QPieSlice(4, "slice 4"); |
|
123 | 123 | s << slice1; // fails because already added |
|
124 | 124 | QCOMPARE(s.count(), 4); |
|
125 | 125 | |
|
126 | 126 | // append with params |
|
127 | 127 | QPieSlice *slice5 = s.append(5, "slice 5"); |
|
128 | 128 | QVERIFY(slice5 != 0); |
|
129 | 129 | QCOMPARE(slice5->value(), 5.0); |
|
130 | 130 | QCOMPARE(slice5->label(), QString("slice 5")); |
|
131 | 131 | QCOMPARE(s.count(), 5); |
|
132 | 132 | |
|
133 | 133 | // check slices |
|
134 | 134 | QVERIFY(!s.isEmpty()); |
|
135 | 135 | for (int i=0; i<s.count(); i++) { |
|
136 | 136 | QCOMPARE(s.slices().at(i)->value(), (qreal) i+1); |
|
137 | 137 | QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1)); |
|
138 | 138 | } |
|
139 | 139 | } |
|
140 | 140 | |
|
141 | 141 | void tst_qpieseries::insert() |
|
142 | 142 | { |
|
143 | 143 | QPieSeries s; |
|
144 | 144 | |
|
145 | 145 | // insert one slice |
|
146 | 146 | QPieSlice *slice1 = 0; |
|
147 | 147 | QVERIFY(!s.insert(0, slice1)); |
|
148 | 148 | slice1 = new QPieSlice(1, "slice 1"); |
|
149 | 149 | QVERIFY(!s.insert(-1, slice1)); |
|
150 | 150 | QVERIFY(!s.insert(5, slice1)); |
|
151 | 151 | QVERIFY(s.insert(0, slice1)); |
|
152 | 152 | QVERIFY(!s.insert(0, slice1)); |
|
153 | 153 | QCOMPARE(s.count(), 1); |
|
154 | 154 | |
|
155 | 155 | // add some more slices |
|
156 | 156 | s.append(2, "slice 2"); |
|
157 | 157 | s.append(4, "slice 4"); |
|
158 | 158 | QCOMPARE(s.count(), 3); |
|
159 | 159 | |
|
160 | 160 | // insert between slices |
|
161 | 161 | s.insert(2, new QPieSlice(3, "slice 3")); |
|
162 | 162 | QCOMPARE(s.count(), 4); |
|
163 | 163 | |
|
164 | 164 | // check slices |
|
165 | 165 | for (int i=0; i<s.count(); i++) { |
|
166 | 166 | QCOMPARE(s.slices().at(i)->value(), (qreal) i+1); |
|
167 | 167 | QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1)); |
|
168 | 168 | } |
|
169 | 169 | } |
|
170 | 170 | |
|
171 | 171 | void tst_qpieseries::remove() |
|
172 | 172 | { |
|
173 | 173 | QPieSeries s; |
|
174 | 174 | |
|
175 | 175 | // add some slices |
|
176 | 176 | QPieSlice *slice1 = s.append(1, "slice 1"); |
|
177 | 177 | QPieSlice *slice2 = s.append(2, "slice 2"); |
|
178 | 178 | QPieSlice *slice3 = s.append(3, "slice 3"); |
|
179 | 179 | QSignalSpy spy1(slice1, SIGNAL(destroyed())); |
|
180 | 180 | QSignalSpy spy2(slice2, SIGNAL(destroyed())); |
|
181 | 181 | QSignalSpy spy3(slice3, SIGNAL(destroyed())); |
|
182 | 182 | QCOMPARE(s.count(), 3); |
|
183 | 183 | |
|
184 | 184 | // null pointer remove |
|
185 | 185 | QVERIFY(!s.remove(0)); |
|
186 | 186 | |
|
187 | 187 | // remove first |
|
188 | 188 | QVERIFY(s.remove(slice1)); |
|
189 | 189 | QVERIFY(!s.remove(slice1)); |
|
190 | 190 | QCOMPARE(s.count(), 2); |
|
191 | 191 | QCOMPARE(s.slices().at(0)->label(), slice2->label()); |
|
192 | 192 | |
|
193 | 193 | // remove all |
|
194 | 194 | s.clear(); |
|
195 | 195 | QVERIFY(s.isEmpty()); |
|
196 | 196 | QVERIFY(s.slices().isEmpty()); |
|
197 | 197 | QCOMPARE(s.count(), 0); |
|
198 | 198 | |
|
199 | 199 | // check that slices were actually destroyed |
|
200 | 200 | TRY_COMPARE(spy1.count(), 1); |
|
201 | 201 | TRY_COMPARE(spy2.count(), 1); |
|
202 | 202 | TRY_COMPARE(spy3.count(), 1); |
|
203 | 203 | } |
|
204 | 204 | |
|
205 | 205 | void tst_qpieseries::calculatedValues() |
|
206 | 206 | { |
|
207 | 207 | bool ok; |
|
208 | 208 | QPieSeries s; |
|
209 | 209 | |
|
210 | 210 | // add a slice |
|
211 | 211 | QPieSlice *slice1 = s.append(1, "slice 1"); |
|
212 | 212 | verifyCalculatedData(s, &ok); |
|
213 | 213 | if (!ok) |
|
214 | 214 | return; |
|
215 | 215 | |
|
216 | 216 | // add some more slices |
|
217 | 217 | QList<QPieSlice *> list; |
|
218 | 218 | list << new QPieSlice(2, "slice 2"); |
|
219 | 219 | list << new QPieSlice(3, "slice 3"); |
|
220 | 220 | s.append(list); |
|
221 | 221 | verifyCalculatedData(s, &ok); |
|
222 | 222 | if (!ok) |
|
223 | 223 | return; |
|
224 | 224 | |
|
225 | 225 | // remove a slice |
|
226 | 226 | s.remove(slice1); |
|
227 | 227 | verifyCalculatedData(s, &ok); |
|
228 | 228 | if (!ok) |
|
229 | 229 | return; |
|
230 | 230 | |
|
231 | 231 | // insert a slice |
|
232 | 232 | s.insert(0, new QPieSlice(1, "Slice 4")); |
|
233 | 233 | verifyCalculatedData(s, &ok); |
|
234 | 234 | if (!ok) |
|
235 | 235 | return; |
|
236 | 236 | |
|
237 | 237 | // clear all |
|
238 | 238 | s.clear(); |
|
239 | 239 | verifyCalculatedData(s, &ok); |
|
240 | 240 | } |
|
241 | 241 | |
|
242 | 242 | void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok) |
|
243 | 243 | { |
|
244 | 244 | *ok = false; |
|
245 | 245 | |
|
246 | 246 | qreal sum = 0; |
|
247 | 247 | foreach (const QPieSlice *slice, series.slices()) |
|
248 | 248 | sum += slice->value(); |
|
249 | 249 | QCOMPARE(series.sum(), sum); |
|
250 | 250 | |
|
251 | 251 | qreal startAngle = series.pieStartAngle(); |
|
252 | 252 | qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle(); |
|
253 | 253 | foreach (const QPieSlice *slice, series.slices()) { |
|
254 | 254 | qreal ratio = slice->value() / sum; |
|
255 | 255 | qreal sliceSpan = pieAngleSpan * ratio; |
|
256 | 256 | QCOMPARE(slice->startAngle(), startAngle); |
|
257 | 257 | QCOMPARE(slice->endAngle(), startAngle + sliceSpan); |
|
258 | 258 | QCOMPARE(slice->percentage(), ratio); |
|
259 | 259 | startAngle += sliceSpan; |
|
260 | 260 | } |
|
261 | 261 | |
|
262 | 262 | if (!series.isEmpty()) |
|
263 | 263 | QCOMPARE(series.slices().last()->endAngle(), series.pieEndAngle()); |
|
264 | 264 | |
|
265 | 265 | *ok = true; |
|
266 | 266 | } |
|
267 | 267 | |
|
268 | 268 | |
|
269 | 269 | void tst_qpieseries::clickedSignal() |
|
270 | 270 | { |
|
271 | 271 | // create a pie series |
|
272 | 272 | QPieSeries *series = new QPieSeries(); |
|
273 | 273 | series->setPieSize(1.0); |
|
274 | 274 | QPieSlice *s1 = series->append(1, "slice 1"); |
|
275 | 275 | series->append(2, "slice 2"); |
|
276 | 276 | series->append(3, "slice 3"); |
|
277 | 277 | QSignalSpy clickSpy1(series, SIGNAL(clicked(QPieSlice*))); |
|
278 | 278 | |
|
279 | 279 | // add series to the chart |
|
280 | 280 | QChartView view(new QChart()); |
|
281 | view.chart()->legend()->setVisible(false); | |
|
281 | 282 | view.resize(200, 200); |
|
282 | 283 | view.chart()->addSeries(series); |
|
283 | 284 | view.show(); |
|
284 | 285 | QTest::qWaitForWindowShown(&view); |
|
285 | 286 | |
|
286 | 287 | // simulate clicks |
|
287 | 288 | // pie rectangle: QRectF(60,60 121x121) |
|
288 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(1 |
|
|
289 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 90)); // inside slice 1 | |
|
289 | 290 | TRY_COMPARE(clickSpy1.count(), 1); |
|
290 | 291 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy1.at(0).at(0)), s1); |
|
291 | 292 | } |
|
292 | 293 | |
|
293 | 294 | void tst_qpieseries::hoverSignal() |
|
294 | 295 | { |
|
295 | 296 | // create a pie series |
|
296 | 297 | QPieSeries *series = new QPieSeries(); |
|
297 | 298 | series->setPieSize(1.0); |
|
298 | 299 | QPieSlice *s1 = series->append(1, "slice 1"); |
|
299 | 300 | series->append(2, "slice 2"); |
|
300 | 301 | series->append(3, "slice 3"); |
|
301 | 302 | |
|
302 | 303 | // add series to the chart |
|
303 | 304 | QChartView view(new QChart()); |
|
305 | view.chart()->legend()->setVisible(false); | |
|
304 | 306 | view.resize(200, 200); |
|
305 | 307 | view.chart()->addSeries(series); |
|
306 | 308 | view.show(); |
|
307 | 309 | QTest::qWaitForWindowShown(&view); |
|
308 | 310 | |
|
309 | 311 | // first move to right top corner |
|
310 | 312 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); |
|
311 | 313 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
312 | 314 | |
|
313 | 315 | // move inside the slice |
|
314 | 316 | // pie rectangle: QRectF(60,60 121x121) |
|
315 | 317 | QSignalSpy hoverSpy(series, SIGNAL(hovered(QPieSlice*,bool))); |
|
316 | 318 | QTest::mouseMove(view.viewport(), QPoint(139, 85)); |
|
317 | 319 | TRY_COMPARE(hoverSpy.count(), 1); |
|
318 | 320 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1); |
|
319 | 321 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true); |
|
320 | 322 | |
|
321 | 323 | // move outside the slice |
|
322 | 324 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); |
|
323 | 325 | TRY_COMPARE(hoverSpy.count(), 2); |
|
324 | 326 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1); |
|
325 | 327 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false); |
|
326 | 328 | } |
|
327 | 329 | |
|
328 | 330 | void tst_qpieseries::model() |
|
329 | 331 | { |
|
330 | 332 | QPieSeries *series = new QPieSeries; |
|
331 | 333 | QChart *chart = new QChart; |
|
332 | 334 | chart->addSeries(series); |
|
333 | 335 | QChartView *chartView = new QChartView(chart); |
|
334 | 336 | chartView->show(); |
|
335 | 337 | |
|
336 | 338 | QStandardItemModel *stdModel = new QStandardItemModel(0, 2); |
|
337 | 339 | series->setModel(stdModel); |
|
338 | 340 | QVERIFY2((series->model()) == stdModel, "Model should be stdModel"); |
|
339 | 341 | |
|
340 | 342 | int rowCount = 3; |
|
341 | 343 | for (int row = 0; row < rowCount; ++row) { |
|
342 | 344 | for (int column = 0; column < 2; column++) { |
|
343 | 345 | QStandardItem *item = new QStandardItem(row * column); |
|
344 | 346 | stdModel->setItem(row, column, item); |
|
345 | 347 | } |
|
346 | 348 | } |
|
347 | 349 | |
|
348 | 350 | // data has been added to the model, but mapper is not set the number of slices should still be 0 |
|
349 | 351 | QVERIFY2(series->slices().count() == 0, "Mapper has not been set, so the number of slices should be 0"); |
|
350 | 352 | |
|
351 | 353 | // set the mapper |
|
352 | 354 | QPieModelMapper *mapper = new QPieModelMapper; |
|
353 | 355 | mapper->setMapValues(0); |
|
354 | 356 | mapper->setMapLabels(0); |
|
355 | 357 | 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 |
|
356 | 358 | QCOMPARE(series->slices().count(), rowCount); |
|
357 | 359 | |
|
358 | 360 | // set the mappings to be outside of the model |
|
359 | 361 | mapper->setMapLabels(5); |
|
360 | 362 | mapper->setMapValues(4); |
|
361 | 363 | QCOMPARE(series->slices().count(), 0); // Mappings are invalid, so the number of slices should be 0 |
|
362 | 364 | |
|
363 | 365 | // set back to correct ones |
|
364 | 366 | mapper->setMapValues(0); |
|
365 | 367 | mapper->setMapLabels(0); |
|
366 | 368 | QCOMPARE(series->slices().count(), rowCount); |
|
367 | 369 | |
|
368 | 370 | // reset the mappings |
|
369 | 371 | mapper->reset(); |
|
370 | 372 | QCOMPARE(series->slices().count(), 0); // Mappings have been reset and are invalid, so the number of slices should be 0 |
|
371 | 373 | |
|
372 | 374 | // unset the model and the mapper |
|
373 | 375 | series->setModel(0); |
|
374 | 376 | series->setModelMapper(0); |
|
375 | 377 | QVERIFY(series->model() == 0); // Model should be unset |
|
376 | 378 | QVERIFY(series->modelMapper() == 0); // Model mapper should be unset |
|
377 | 379 | } |
|
378 | 380 | |
|
379 | 381 | void tst_qpieseries::modelCustomMap() |
|
380 | 382 | { |
|
381 | 383 | int rowCount = 12; |
|
382 | 384 | int columnCount = 3; |
|
383 | 385 | QStandardItemModel *stdModel = new QStandardItemModel(0, 3); |
|
384 | 386 | for (int row = 0; row < rowCount; ++row) { |
|
385 | 387 | for (int column = 0; column < 2; column++) { |
|
386 | 388 | QStandardItem *item = new QStandardItem(row * column); |
|
387 | 389 | stdModel->setItem(row, column, item); |
|
388 | 390 | } |
|
389 | 391 | } |
|
390 | 392 | |
|
391 | 393 | QPieSeries *series = new QPieSeries; |
|
392 | 394 | QChart *chart = new QChart; |
|
393 | 395 | chart->addSeries(series); |
|
394 | 396 | QChartView *chartView = new QChartView(chart); |
|
395 | 397 | chartView->show(); |
|
396 | 398 | series->setModel(stdModel); |
|
397 | 399 | |
|
398 | 400 | QPieModelMapper *mapper = new QPieModelMapper; |
|
399 | 401 | mapper->setMapValues(0); |
|
400 | 402 | mapper->setMapLabels(0); |
|
401 | 403 | series->setModelMapper(mapper); |
|
402 | 404 | QCOMPARE(series->slices().count(), rowCount); |
|
403 | 405 | |
|
404 | 406 | // lets change the orientation to horizontal |
|
405 | 407 | mapper->setOrientation(Qt::Horizontal); |
|
406 | 408 | QCOMPARE(series->slices().count(), columnCount); |
|
407 | 409 | |
|
408 | 410 | // change it back to vertical |
|
409 | 411 | mapper->setOrientation(Qt::Vertical); |
|
410 | 412 | QCOMPARE(series->slices().count(), rowCount); |
|
411 | 413 | |
|
412 | 414 | // lets customize the mapping |
|
413 | 415 | int first = 3; |
|
414 | 416 | mapper->setFirst(first); |
|
415 | 417 | QCOMPARE(series->slices().count(), rowCount - first); |
|
416 | 418 | int count = 7; |
|
417 | 419 | mapper->setCount(count); |
|
418 | 420 | QCOMPARE(series->slices().count(), count); |
|
419 | 421 | first = 9; |
|
420 | 422 | mapper->setFirst(first); |
|
421 | 423 | QCOMPARE(series->slices().count(), qMin(count, rowCount - first)); |
|
422 | 424 | } |
|
423 | 425 | |
|
424 | 426 | void tst_qpieseries::modelUpdate() |
|
425 | 427 | { |
|
426 | 428 | int rowCount = 12; |
|
427 | 429 | int columnCount = 7; |
|
428 | 430 | QStandardItemModel *stdModel = new QStandardItemModel(rowCount, columnCount); |
|
429 | 431 | for (int row = 0; row < rowCount; ++row) { |
|
430 | 432 | for (int column = 0; column < columnCount; column++) { |
|
431 | 433 | QStandardItem *item = new QStandardItem(row * column); |
|
432 | 434 | stdModel->setItem(row, column, item); |
|
433 | 435 | } |
|
434 | 436 | } |
|
435 | 437 | |
|
436 | 438 | QPieSeries *series = new QPieSeries; |
|
437 | 439 | QChart *chart = new QChart; |
|
438 | 440 | chart->addSeries(series); |
|
439 | 441 | QChartView *chartView = new QChartView(chart); |
|
440 | 442 | chartView->show(); |
|
441 | 443 | series->setModel(stdModel); |
|
442 | 444 | |
|
443 | 445 | QPieModelMapper *mapper = new QPieModelMapper; |
|
444 | 446 | mapper->setMapValues(0); |
|
445 | 447 | mapper->setMapLabels(0); |
|
446 | 448 | series->setModelMapper(mapper); |
|
447 | 449 | |
|
448 | 450 | stdModel->insertRows(3, 5); |
|
449 | 451 | QCOMPARE(series->slices().count(), rowCount + 5); |
|
450 | 452 | |
|
451 | 453 | stdModel->removeRows(10, 5); |
|
452 | 454 | QCOMPARE(series->slices().count(), rowCount); |
|
453 | 455 | |
|
454 | 456 | // limit the number of slices taken from the model to 12 |
|
455 | 457 | mapper->setCount(rowCount); |
|
456 | 458 | stdModel->insertRows(3, 5); |
|
457 | 459 | QCOMPARE(series->slices().count(), rowCount); |
|
458 | 460 | |
|
459 | 461 | stdModel->removeRows(0, 10); |
|
460 | 462 | QCOMPARE(series->slices().count(), rowCount - 5); |
|
461 | 463 | |
|
462 | 464 | // change the orientation to horizontal |
|
463 | 465 | mapper->setOrientation(Qt::Horizontal); |
|
464 | 466 | QCOMPARE(series->slices().count(), columnCount); |
|
465 | 467 | |
|
466 | 468 | stdModel->insertColumns(3, 10); |
|
467 | 469 | QCOMPARE(series->slices().count(), rowCount); // count is limited to rowCount (12) |
|
468 | 470 | |
|
469 | 471 | stdModel->removeColumns(5, 10); |
|
470 | 472 | QCOMPARE(series->slices().count(), columnCount); |
|
471 | 473 | |
|
472 | 474 | } |
|
473 | 475 | |
|
474 | 476 | QTEST_MAIN(tst_qpieseries) |
|
475 | 477 | |
|
476 | 478 | #include "tst_qpieseries.moc" |
|
477 | 479 |
@@ -1,268 +1,270 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include <QtTest/QtTest> |
|
22 | 22 | #include <tst_definitions.h> |
|
23 | 23 | #include <qchartview.h> |
|
24 | 24 | #include <qchart.h> |
|
25 | 25 | #include <qpieslice.h> |
|
26 | 26 | #include <qpieseries.h> |
|
27 | 27 | |
|
28 | 28 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
29 | 29 | |
|
30 | 30 | class tst_qpieslice : public QObject |
|
31 | 31 | { |
|
32 | 32 | Q_OBJECT |
|
33 | 33 | |
|
34 | 34 | public slots: |
|
35 | 35 | void initTestCase(); |
|
36 | 36 | void cleanupTestCase(); |
|
37 | 37 | void init(); |
|
38 | 38 | void cleanup(); |
|
39 | 39 | |
|
40 | 40 | private slots: |
|
41 | 41 | void construction(); |
|
42 | 42 | void changedSignals(); |
|
43 | 43 | void customize(); |
|
44 | 44 | void mouseClick(); |
|
45 | 45 | void mouseHover(); |
|
46 | 46 | |
|
47 | 47 | private: |
|
48 | 48 | |
|
49 | 49 | |
|
50 | 50 | private: |
|
51 | 51 | |
|
52 | 52 | }; |
|
53 | 53 | |
|
54 | 54 | void tst_qpieslice::initTestCase() |
|
55 | 55 | { |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | void tst_qpieslice::cleanupTestCase() |
|
59 | 59 | { |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | void tst_qpieslice::init() |
|
63 | 63 | { |
|
64 | 64 | |
|
65 | 65 | } |
|
66 | 66 | |
|
67 | 67 | void tst_qpieslice::cleanup() |
|
68 | 68 | { |
|
69 | 69 | |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | void tst_qpieslice::construction() |
|
73 | 73 | { |
|
74 | 74 | // no params |
|
75 | 75 | QPieSlice slice1; |
|
76 | 76 | QCOMPARE(slice1.value(), 0.0); |
|
77 | 77 | QVERIFY(slice1.label().isEmpty()); |
|
78 | 78 | QVERIFY(!slice1.isLabelVisible()); |
|
79 | 79 | QVERIFY(!slice1.isExploded()); |
|
80 | 80 | QCOMPARE(slice1.pen(), QPen()); |
|
81 | 81 | QCOMPARE(slice1.brush(), QBrush()); |
|
82 | 82 | QCOMPARE(slice1.labelPen(), QPen()); |
|
83 | 83 | QCOMPARE(slice1.labelFont(), QFont()); |
|
84 | 84 | QCOMPARE(slice1.labelArmLengthFactor(), 0.15); // default value |
|
85 | 85 | QCOMPARE(slice1.explodeDistanceFactor(), 0.15); // default value |
|
86 | 86 | QCOMPARE(slice1.percentage(), 0.0); |
|
87 | 87 | QCOMPARE(slice1.startAngle(), 0.0); |
|
88 | 88 | QCOMPARE(slice1.endAngle(), 0.0); |
|
89 | 89 | |
|
90 | 90 | // value and label params |
|
91 | 91 | QPieSlice slice2(1.0, "foobar"); |
|
92 | 92 | QCOMPARE(slice2.value(), 1.0); |
|
93 | 93 | QCOMPARE(slice2.label(), QString("foobar")); |
|
94 | 94 | QVERIFY(!slice2.isLabelVisible()); |
|
95 | 95 | QVERIFY(!slice2.isExploded()); |
|
96 | 96 | QCOMPARE(slice2.pen(), QPen()); |
|
97 | 97 | QCOMPARE(slice2.brush(), QBrush()); |
|
98 | 98 | QCOMPARE(slice2.labelPen(), QPen()); |
|
99 | 99 | QCOMPARE(slice2.labelFont(), QFont()); |
|
100 | 100 | QCOMPARE(slice2.labelArmLengthFactor(), 0.15); // default value |
|
101 | 101 | QCOMPARE(slice2.explodeDistanceFactor(), 0.15); // default value |
|
102 | 102 | QCOMPARE(slice2.percentage(), 0.0); |
|
103 | 103 | QCOMPARE(slice2.startAngle(), 0.0); |
|
104 | 104 | QCOMPARE(slice2.endAngle(), 0.0); |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | void tst_qpieslice::changedSignals() |
|
108 | 108 | { |
|
109 | 109 | // set everything twice to see we do not get unnecessary signals |
|
110 | 110 | QPieSlice slice; |
|
111 | 111 | QSignalSpy spy(&slice, SIGNAL(changed())); // TODO: this will be changed to something more refined |
|
112 | 112 | slice.setValue(1); |
|
113 | 113 | slice.setValue(1); |
|
114 | 114 | slice.setLabel("foobar"); |
|
115 | 115 | slice.setLabel("foobar"); |
|
116 | 116 | slice.setLabelVisible(); |
|
117 | 117 | slice.setLabelVisible(); |
|
118 | 118 | slice.setExploded(); |
|
119 | 119 | slice.setExploded(); |
|
120 | 120 | slice.setPen(QPen(Qt::red)); |
|
121 | 121 | slice.setPen(QPen(Qt::red)); |
|
122 | 122 | slice.setBrush(QBrush(Qt::red)); |
|
123 | 123 | slice.setBrush(QBrush(Qt::red)); |
|
124 | 124 | slice.setLabelPen(QPen(Qt::green)); |
|
125 | 125 | slice.setLabelPen(QPen(Qt::green)); |
|
126 | 126 | slice.setLabelFont(QFont("Tahoma")); |
|
127 | 127 | slice.setLabelFont(QFont("Tahoma")); |
|
128 | 128 | slice.setLabelArmLengthFactor(0.1); |
|
129 | 129 | slice.setLabelArmLengthFactor(0.1); |
|
130 | 130 | slice.setExplodeDistanceFactor(0.1); |
|
131 | 131 | slice.setExplodeDistanceFactor(0.1); |
|
132 | 132 | TRY_COMPARE(spy.count(), 10); |
|
133 | 133 | } |
|
134 | 134 | |
|
135 | 135 | void tst_qpieslice::customize() |
|
136 | 136 | { |
|
137 | 137 | // create a pie series |
|
138 | 138 | QPieSeries *series = new QPieSeries(); |
|
139 | 139 | QPieSlice *s1 = series->append(1, "slice 1"); |
|
140 | 140 | QPieSlice *s2 = series->append(2, "slice 2"); |
|
141 | 141 | series->append(3, "slice 3"); |
|
142 | 142 | |
|
143 | 143 | // customize a slice |
|
144 | 144 | QPen p1(Qt::red); |
|
145 | 145 | s1->setPen(p1); |
|
146 | 146 | QBrush b1(Qt::red); |
|
147 | 147 | s1->setBrush(b1); |
|
148 | 148 | s1->setLabelPen(p1); |
|
149 | 149 | QFont f1("Consolas"); |
|
150 | 150 | s1->setLabelFont(f1); |
|
151 | 151 | |
|
152 | 152 | // add series to the chart |
|
153 | 153 | QChartView view(new QChart()); |
|
154 | 154 | view.resize(200, 200); |
|
155 | 155 | view.chart()->addSeries(series); |
|
156 | 156 | view.show(); |
|
157 | 157 | QTest::qWaitForWindowShown(&view); |
|
158 | 158 | //QTest::qWait(1000); |
|
159 | 159 | |
|
160 | 160 | // check that customizations persist |
|
161 | 161 | QCOMPARE(s1->pen(), p1); |
|
162 | 162 | QCOMPARE(s1->brush(), b1); |
|
163 | 163 | QCOMPARE(s1->labelPen(), p1); |
|
164 | 164 | QCOMPARE(s1->labelFont(), f1); |
|
165 | 165 | |
|
166 | 166 | // remove a slice |
|
167 | 167 | series->remove(s2); |
|
168 | 168 | QCOMPARE(s1->pen(), p1); |
|
169 | 169 | QCOMPARE(s1->brush(), b1); |
|
170 | 170 | QCOMPARE(s1->labelPen(), p1); |
|
171 | 171 | QCOMPARE(s1->labelFont(), f1); |
|
172 | 172 | |
|
173 | 173 | // add a slice |
|
174 | 174 | series->append(4, "slice 4"); |
|
175 | 175 | QCOMPARE(s1->pen(), p1); |
|
176 | 176 | QCOMPARE(s1->brush(), b1); |
|
177 | 177 | QCOMPARE(s1->labelPen(), p1); |
|
178 | 178 | QCOMPARE(s1->labelFont(), f1); |
|
179 | 179 | |
|
180 | 180 | // insert a slice |
|
181 | 181 | series->insert(0, new QPieSlice(5, "slice 5")); |
|
182 | 182 | QCOMPARE(s1->pen(), p1); |
|
183 | 183 | QCOMPARE(s1->brush(), b1); |
|
184 | 184 | QCOMPARE(s1->labelPen(), p1); |
|
185 | 185 | QCOMPARE(s1->labelFont(), f1); |
|
186 | 186 | |
|
187 | 187 | // change theme |
|
188 | 188 | // theme will overwrite customizations |
|
189 | 189 | view.chart()->setTheme(QChart::ChartThemeHighContrast); |
|
190 | 190 | QVERIFY(s1->pen() != p1); |
|
191 | 191 | QVERIFY(s1->brush() != b1); |
|
192 | 192 | QVERIFY(s1->labelPen() != p1); |
|
193 | 193 | QVERIFY(s1->labelFont() != f1); |
|
194 | 194 | } |
|
195 | 195 | |
|
196 | 196 | void tst_qpieslice::mouseClick() |
|
197 | 197 | { |
|
198 | 198 | // create a pie series |
|
199 | 199 | QPieSeries *series = new QPieSeries(); |
|
200 | 200 | series->setPieSize(1.0); |
|
201 | 201 | QPieSlice *s1 = series->append(1, "slice 1"); |
|
202 | 202 | QPieSlice *s2 = series->append(2, "slice 2"); |
|
203 | 203 | QPieSlice *s3 = series->append(3, "slice 3"); |
|
204 | 204 | QSignalSpy clickSpy1(s1, SIGNAL(clicked())); |
|
205 | 205 | QSignalSpy clickSpy2(s2, SIGNAL(clicked())); |
|
206 | 206 | QSignalSpy clickSpy3(s3, SIGNAL(clicked())); |
|
207 | 207 | |
|
208 | 208 | // add series to the chart |
|
209 | 209 | QChartView view(new QChart()); |
|
210 | view.chart()->legend()->setVisible(false); | |
|
210 | 211 | view.resize(200, 200); |
|
211 | 212 | view.chart()->addSeries(series); |
|
212 | 213 | view.show(); |
|
213 | 214 | QTest::qWaitForWindowShown(&view); |
|
214 | 215 | |
|
215 | 216 | // simulate clicks |
|
216 | 217 | // pie rectangle: QRectF(60,60 121x121) |
|
217 | 218 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1 |
|
218 | 219 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 136)); // inside slice 2 |
|
219 | 220 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(91, 119)); // inside slice 3 |
|
220 | 221 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(70, 70)); // inside pie rectangle but not inside a slice |
|
221 | 222 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(170, 170)); // inside pie rectangle but not inside a slice |
|
222 | 223 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
223 | 224 | QCOMPARE(clickSpy1.count(), 1); |
|
224 | 225 | QCOMPARE(clickSpy2.count(), 1); |
|
225 | 226 | QCOMPARE(clickSpy3.count(), 1); |
|
226 | 227 | } |
|
227 | 228 | |
|
228 | 229 | void tst_qpieslice::mouseHover() |
|
229 | 230 | { |
|
230 | 231 | // create a pie series |
|
231 | 232 | QPieSeries *series = new QPieSeries(); |
|
232 | 233 | series->setPieSize(1.0); |
|
233 | 234 | QPieSlice *s1 = series->append(1, "slice 1"); |
|
234 | 235 | series->append(2, "slice 2"); |
|
235 | 236 | series->append(3, "slice 3"); |
|
236 | 237 | |
|
237 | 238 | // add series to the chart |
|
238 | 239 | QChartView view(new QChart()); |
|
240 | view.chart()->legend()->setVisible(false); | |
|
239 | 241 | view.resize(200, 200); |
|
240 | 242 | view.chart()->addSeries(series); |
|
241 | 243 | view.show(); |
|
242 | 244 | QTest::qWaitForWindowShown(&view); |
|
243 | 245 | |
|
244 | 246 | // first move to right top corner |
|
245 | 247 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); |
|
246 | 248 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
247 | 249 | |
|
248 | 250 | // move inside slice rectangle but NOT the actual slice |
|
249 | 251 | // pie rectangle: QRectF(60,60 121x121) |
|
250 | 252 | QSignalSpy hoverSpy(s1, SIGNAL(hovered(bool))); |
|
251 | 253 | QTest::mouseMove(view.viewport(), QPoint(170, 70)); |
|
252 | 254 | TRY_COMPARE(hoverSpy.count(), 0); |
|
253 | 255 | |
|
254 | 256 | // move inside the slice |
|
255 | 257 | QTest::mouseMove(view.viewport(), QPoint(139, 85)); |
|
256 | 258 | TRY_COMPARE(hoverSpy.count(), 1); |
|
257 | 259 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(0)), true); |
|
258 | 260 | |
|
259 | 261 | // move outside the slice |
|
260 | 262 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); |
|
261 | 263 | TRY_COMPARE(hoverSpy.count(), 2); |
|
262 | 264 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(0)), false); |
|
263 | 265 | } |
|
264 | 266 | |
|
265 | 267 | QTEST_MAIN(tst_qpieslice) |
|
266 | 268 | |
|
267 | 269 | #include "tst_qpieslice.moc" |
|
268 | 270 |
General Comments 0
You need to be logged in to leave comments.
Login now