@@ -1,642 +1,636 | |||
|
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 | Q_DECLARE_METATYPE(QList<QPieSlice*>) |
|
34 | 34 | |
|
35 | 35 | class tst_qpieseries : public QObject |
|
36 | 36 | { |
|
37 | 37 | Q_OBJECT |
|
38 | 38 | |
|
39 | 39 | public slots: |
|
40 | 40 | void initTestCase(); |
|
41 | 41 | void cleanupTestCase(); |
|
42 | 42 | void init(); |
|
43 | 43 | void cleanup(); |
|
44 | 44 | |
|
45 | 45 | private slots: |
|
46 | 46 | void properties(); |
|
47 | 47 | void append(); |
|
48 | 48 | void appendAnimated(); |
|
49 | 49 | void insert(); |
|
50 | 50 | void insertAnimated(); |
|
51 | 51 | void remove(); |
|
52 | 52 | void removeAnimated(); |
|
53 | 53 | void take(); |
|
54 | 54 | void takeAnimated(); |
|
55 | 55 | void calculatedValues(); |
|
56 | 56 | void clickedSignal(); |
|
57 | 57 | void hoverSignal(); |
|
58 | 58 | void sliceSeries(); |
|
59 | 59 | void destruction(); |
|
60 | 60 | |
|
61 | 61 | private: |
|
62 | 62 | void verifyCalculatedData(const QPieSeries &series, bool *ok); |
|
63 | QList<QPoint> slicePoints(QRectF rect); | |
|
63 | 64 | |
|
64 | 65 | private: |
|
65 | 66 | QChartView *m_view; |
|
66 | 67 | QPieSeries *m_series; |
|
67 | 68 | }; |
|
68 | 69 | |
|
69 | 70 | void tst_qpieseries::initTestCase() |
|
70 | 71 | { |
|
71 | 72 | qRegisterMetaType<QPieSlice*>("QPieSlice*"); |
|
72 | 73 | qRegisterMetaType<QList<QPieSlice*> >("QList<QPieSlice*>"); |
|
73 | 74 | } |
|
74 | 75 | |
|
75 | 76 | void tst_qpieseries::cleanupTestCase() |
|
76 | 77 | { |
|
77 | 78 | } |
|
78 | 79 | |
|
79 | 80 | void tst_qpieseries::init() |
|
80 | 81 | { |
|
81 | 82 | m_view = new QChartView(); |
|
82 | 83 | m_series = new QPieSeries(m_view); |
|
83 | 84 | m_view->show(); |
|
84 | 85 | QTest::qWaitForWindowShown(m_view); |
|
85 | 86 | |
|
86 | 87 | } |
|
87 | 88 | |
|
88 | 89 | void tst_qpieseries::cleanup() |
|
89 | 90 | { |
|
90 | 91 | delete m_view; |
|
91 | 92 | m_view = 0; |
|
92 | 93 | m_series = 0; |
|
93 | 94 | } |
|
94 | 95 | |
|
95 | 96 | void tst_qpieseries::properties() |
|
96 | 97 | { |
|
97 | 98 | QSignalSpy countSpy(m_series, SIGNAL(countChanged())); |
|
98 | 99 | QSignalSpy sumSpy(m_series, SIGNAL(sumChanged())); |
|
99 | 100 | |
|
100 | 101 | QVERIFY(m_series->type() == QAbstractSeries::SeriesTypePie); |
|
101 | 102 | QVERIFY(m_series->count() == 0); |
|
102 | 103 | QVERIFY(m_series->isEmpty()); |
|
103 | 104 | QCOMPARE(m_series->sum(), 0.0); |
|
104 | 105 | QCOMPARE(m_series->horizontalPosition(), 0.5); |
|
105 | 106 | QCOMPARE(m_series->verticalPosition(), 0.5); |
|
106 | 107 | QCOMPARE(m_series->pieSize(), 0.7); |
|
107 | 108 | QCOMPARE(m_series->pieStartAngle(), 0.0); |
|
108 | 109 | QCOMPARE(m_series->pieEndAngle(), 360.0); |
|
109 | 110 | |
|
110 | 111 | m_series->append("s1", 1); |
|
111 | 112 | m_series->append("s2", 1); |
|
112 | 113 | m_series->append("s3", 1); |
|
113 | 114 | m_series->insert(1, new QPieSlice("s4", 1)); |
|
114 | 115 | m_series->remove(m_series->slices().first()); |
|
115 | 116 | QCOMPARE(m_series->count(), 3); |
|
116 | 117 | QCOMPARE(m_series->sum(), 3.0); |
|
117 | 118 | m_series->clear(); |
|
118 | 119 | QCOMPARE(m_series->count(), 0); |
|
119 | 120 | QCOMPARE(m_series->sum(), 0.0); |
|
120 | 121 | QCOMPARE(countSpy.count(), 6); |
|
121 | 122 | QCOMPARE(sumSpy.count(), 6); |
|
122 | 123 | |
|
123 | 124 | m_series->setPieSize(-1.0); |
|
124 | 125 | QCOMPARE(m_series->pieSize(), 0.0); |
|
125 | 126 | m_series->setPieSize(0.0); |
|
126 | 127 | m_series->setPieSize(0.9); |
|
127 | 128 | m_series->setPieSize(2.0); |
|
128 | 129 | QCOMPARE(m_series->pieSize(), 1.0); |
|
129 | 130 | |
|
130 | 131 | m_series->setPieSize(0.7); |
|
131 | 132 | QCOMPARE(m_series->pieSize(), 0.7); |
|
132 | 133 | |
|
133 | 134 | m_series->setHoleSize(-1.0); |
|
134 | 135 | QCOMPARE(m_series->holeSize(), 0.0); |
|
135 | 136 | m_series->setHoleSize(0.5); |
|
136 | 137 | QCOMPARE(m_series->holeSize(), 0.5); |
|
137 | 138 | |
|
138 | 139 | m_series->setHoleSize(0.8); |
|
139 | 140 | QCOMPARE(m_series->holeSize(), 0.8); |
|
140 | 141 | QCOMPARE(m_series->pieSize(), 0.8); |
|
141 | 142 | |
|
142 | 143 | m_series->setPieSize(0.4); |
|
143 | 144 | QCOMPARE(m_series->pieSize(), 0.4); |
|
144 | 145 | QCOMPARE(m_series->holeSize(), 0.4); |
|
145 | 146 | |
|
146 | 147 | m_series->setPieStartAngle(0); |
|
147 | 148 | m_series->setPieStartAngle(-180); |
|
148 | 149 | m_series->setPieStartAngle(180); |
|
149 | 150 | QCOMPARE(m_series->pieStartAngle(), 180.0); |
|
150 | 151 | |
|
151 | 152 | m_series->setPieEndAngle(360); |
|
152 | 153 | m_series->setPieEndAngle(-180); |
|
153 | 154 | m_series->setPieEndAngle(180); |
|
154 | 155 | QCOMPARE(m_series->pieEndAngle(), 180.0); |
|
155 | 156 | |
|
156 | 157 | m_series->setHorizontalPosition(0.5); |
|
157 | 158 | m_series->setHorizontalPosition(-1.0); |
|
158 | 159 | QCOMPARE(m_series->horizontalPosition(), 0.0); |
|
159 | 160 | m_series->setHorizontalPosition(1.0); |
|
160 | 161 | m_series->setHorizontalPosition(2.0); |
|
161 | 162 | QCOMPARE(m_series->horizontalPosition(), 1.0); |
|
162 | 163 | |
|
163 | 164 | m_series->setVerticalPosition(0.5); |
|
164 | 165 | m_series->setVerticalPosition(-1.0); |
|
165 | 166 | QCOMPARE(m_series->verticalPosition(), 0.0); |
|
166 | 167 | m_series->setVerticalPosition(1.0); |
|
167 | 168 | m_series->setVerticalPosition(2.0); |
|
168 | 169 | QCOMPARE(m_series->verticalPosition(), 1.0); |
|
169 | 170 | } |
|
170 | 171 | |
|
171 | 172 | void tst_qpieseries::append() |
|
172 | 173 | { |
|
173 | 174 | m_view->chart()->addSeries(m_series); |
|
174 | 175 | QSignalSpy addedSpy(m_series, SIGNAL(added(QList<QPieSlice*>))); |
|
175 | 176 | |
|
176 | 177 | // append pointer |
|
177 | 178 | QPieSlice *slice1 = 0; |
|
178 | 179 | QVERIFY(!m_series->append(slice1)); |
|
179 | 180 | slice1 = new QPieSlice("slice 1", 1); |
|
180 | 181 | QVERIFY(m_series->append(slice1)); |
|
181 | 182 | QVERIFY(!m_series->append(slice1)); |
|
182 | 183 | QCOMPARE(m_series->count(), 1); |
|
183 | 184 | QCOMPARE(addedSpy.count(), 1); |
|
184 | 185 | QList<QPieSlice*> added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(0).at(0)); |
|
185 | 186 | QCOMPARE(added.count(), 1); |
|
186 | 187 | QCOMPARE(added.first(), slice1); |
|
187 | 188 | |
|
188 | 189 | // try to append same slice to another series |
|
189 | 190 | QPieSeries series2; |
|
190 | 191 | QVERIFY(!series2.append(slice1)); |
|
191 | 192 | |
|
192 | 193 | // append pointer list |
|
193 | 194 | QList<QPieSlice *> list; |
|
194 | 195 | QVERIFY(!m_series->append(list)); |
|
195 | 196 | list << (QPieSlice *) 0; |
|
196 | 197 | QVERIFY(!m_series->append(list)); |
|
197 | 198 | list.clear(); |
|
198 | 199 | list << new QPieSlice("slice 2", 2); |
|
199 | 200 | list << new QPieSlice("slice 3", 3); |
|
200 | 201 | QVERIFY(m_series->append(list)); |
|
201 | 202 | QVERIFY(!m_series->append(list)); |
|
202 | 203 | QCOMPARE(m_series->count(), 3); |
|
203 | 204 | QCOMPARE(addedSpy.count(), 2); |
|
204 | 205 | added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(1).at(0)); |
|
205 | 206 | QCOMPARE(added.count(), 2); |
|
206 | 207 | QCOMPARE(added, list); |
|
207 | 208 | |
|
208 | 209 | // append operator |
|
209 | 210 | QPieSlice *slice4 = new QPieSlice("slice 4", 4); |
|
210 | 211 | *m_series << slice4; |
|
211 | 212 | *m_series << slice1; // fails because already added |
|
212 | 213 | QCOMPARE(m_series->count(), 4); |
|
213 | 214 | QCOMPARE(addedSpy.count(), 3); |
|
214 | 215 | added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(2).at(0)); |
|
215 | 216 | QCOMPARE(added.count(), 1); |
|
216 | 217 | QCOMPARE(added.first(), slice4); |
|
217 | 218 | |
|
218 | 219 | // append with params |
|
219 | 220 | QPieSlice *slice5 = m_series->append("slice 5", 5); |
|
220 | 221 | QVERIFY(slice5 != 0); |
|
221 | 222 | QCOMPARE(slice5->value(), 5.0); |
|
222 | 223 | QCOMPARE(slice5->label(), QString("slice 5")); |
|
223 | 224 | QCOMPARE(m_series->count(), 5); |
|
224 | 225 | QCOMPARE(addedSpy.count(), 4); |
|
225 | 226 | added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(3).at(0)); |
|
226 | 227 | QCOMPARE(added.count(), 1); |
|
227 | 228 | QCOMPARE(added.first(), slice5); |
|
228 | 229 | |
|
229 | 230 | // check slices |
|
230 | 231 | QVERIFY(!m_series->isEmpty()); |
|
231 | 232 | for (int i=0; i<m_series->count(); i++) { |
|
232 | 233 | QCOMPARE(m_series->slices().at(i)->value(), (qreal) i+1); |
|
233 | 234 | QCOMPARE(m_series->slices().at(i)->label(), QString("slice ") + QString::number(i+1)); |
|
234 | 235 | } |
|
235 | 236 | } |
|
236 | 237 | |
|
237 | 238 | void tst_qpieseries::appendAnimated() |
|
238 | 239 | { |
|
239 | 240 | m_view->chart()->setAnimationOptions(QChart::AllAnimations); |
|
240 | 241 | append(); |
|
241 | 242 | } |
|
242 | 243 | |
|
243 | 244 | void tst_qpieseries::insert() |
|
244 | 245 | { |
|
245 | 246 | m_view->chart()->addSeries(m_series); |
|
246 | 247 | QSignalSpy addedSpy(m_series, SIGNAL(added(QList<QPieSlice*>))); |
|
247 | 248 | |
|
248 | 249 | // insert one slice |
|
249 | 250 | QPieSlice *slice1 = 0; |
|
250 | 251 | QVERIFY(!m_series->insert(0, slice1)); |
|
251 | 252 | slice1 = new QPieSlice("slice 1", 1); |
|
252 | 253 | QVERIFY(!m_series->insert(-1, slice1)); |
|
253 | 254 | QVERIFY(!m_series->insert(5, slice1)); |
|
254 | 255 | QVERIFY(m_series->insert(0, slice1)); |
|
255 | 256 | QVERIFY(!m_series->insert(0, slice1)); |
|
256 | 257 | QCOMPARE(m_series->count(), 1); |
|
257 | 258 | QCOMPARE(addedSpy.count(), 1); |
|
258 | 259 | QList<QPieSlice*> added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(0).at(0)); |
|
259 | 260 | QCOMPARE(added.count(), 1); |
|
260 | 261 | QCOMPARE(added.first(), slice1); |
|
261 | 262 | |
|
262 | 263 | // try to insert same slice to another series |
|
263 | 264 | QPieSeries series2; |
|
264 | 265 | QVERIFY(!series2.insert(0, slice1)); |
|
265 | 266 | |
|
266 | 267 | // add some more slices |
|
267 | 268 | QPieSlice *slice2 = m_series->append("slice 2", 2); |
|
268 | 269 | QPieSlice *slice4 = m_series->append("slice 4", 4); |
|
269 | 270 | QCOMPARE(m_series->count(), 3); |
|
270 | 271 | QCOMPARE(addedSpy.count(), 3); |
|
271 | 272 | added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(1).at(0)); |
|
272 | 273 | QCOMPARE(added.count(), 1); |
|
273 | 274 | QCOMPARE(added.first(), slice2); |
|
274 | 275 | added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(2).at(0)); |
|
275 | 276 | QCOMPARE(added.count(), 1); |
|
276 | 277 | QCOMPARE(added.first(), slice4); |
|
277 | 278 | |
|
278 | 279 | // insert between slices |
|
279 | 280 | QPieSlice *slice3 = new QPieSlice("slice 3", 3); |
|
280 | 281 | m_series->insert(2, slice3); |
|
281 | 282 | QCOMPARE(m_series->count(), 4); |
|
282 | 283 | QCOMPARE(addedSpy.count(), 4); |
|
283 | 284 | added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(3).at(0)); |
|
284 | 285 | QCOMPARE(added.count(), 1); |
|
285 | 286 | QCOMPARE(added.first(), slice3); |
|
286 | 287 | |
|
287 | 288 | // check slices |
|
288 | 289 | for (int i=0; i<m_series->count(); i++) { |
|
289 | 290 | QCOMPARE(m_series->slices().at(i)->value(), (qreal) i+1); |
|
290 | 291 | QCOMPARE(m_series->slices().at(i)->label(), QString("slice ") + QString::number(i+1)); |
|
291 | 292 | QVERIFY(m_series->slices().at(i)->parent() == m_series); |
|
292 | 293 | } |
|
293 | 294 | } |
|
294 | 295 | |
|
295 | 296 | void tst_qpieseries::insertAnimated() |
|
296 | 297 | { |
|
297 | 298 | m_view->chart()->setAnimationOptions(QChart::AllAnimations); |
|
298 | 299 | insert(); |
|
299 | 300 | } |
|
300 | 301 | |
|
301 | 302 | void tst_qpieseries::remove() |
|
302 | 303 | { |
|
303 | 304 | m_view->chart()->addSeries(m_series); |
|
304 | 305 | QSignalSpy removedSpy(m_series, SIGNAL(removed(QList<QPieSlice*>))); |
|
305 | 306 | |
|
306 | 307 | // add some slices |
|
307 | 308 | QPieSlice *slice1 = m_series->append("slice 1", 1); |
|
308 | 309 | QPieSlice *slice2 = m_series->append("slice 2", 2); |
|
309 | 310 | QPieSlice *slice3 = m_series->append("slice 3", 3); |
|
310 | 311 | QSignalSpy spy1(slice1, SIGNAL(destroyed())); |
|
311 | 312 | QSignalSpy spy2(slice2, SIGNAL(destroyed())); |
|
312 | 313 | QSignalSpy spy3(slice3, SIGNAL(destroyed())); |
|
313 | 314 | QCOMPARE(m_series->count(), 3); |
|
314 | 315 | |
|
315 | 316 | // null pointer remove |
|
316 | 317 | QVERIFY(!m_series->remove(0)); |
|
317 | 318 | |
|
318 | 319 | // remove first |
|
319 | 320 | QVERIFY(m_series->remove(slice1)); |
|
320 | 321 | QVERIFY(!m_series->remove(slice1)); |
|
321 | 322 | QCOMPARE(m_series->count(), 2); |
|
322 | 323 | QCOMPARE(m_series->slices().at(0)->label(), slice2->label()); |
|
323 | 324 | QCOMPARE(removedSpy.count(), 1); |
|
324 | 325 | QList<QPieSlice*> removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(0).at(0)); |
|
325 | 326 | QCOMPARE(removed.count(), 1); |
|
326 | 327 | QCOMPARE(removed.first(), slice1); |
|
327 | 328 | |
|
328 | 329 | // remove all |
|
329 | 330 | m_series->clear(); |
|
330 | 331 | QVERIFY(m_series->isEmpty()); |
|
331 | 332 | QVERIFY(m_series->slices().isEmpty()); |
|
332 | 333 | QCOMPARE(m_series->count(), 0); |
|
333 | 334 | QCOMPARE(removedSpy.count(), 2); |
|
334 | 335 | removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(1).at(0)); |
|
335 | 336 | QCOMPARE(removed.count(), 2); |
|
336 | 337 | QCOMPARE(removed.first(), slice2); |
|
337 | 338 | QCOMPARE(removed.last(), slice3); |
|
338 | 339 | |
|
339 | 340 | // check that slices were actually destroyed |
|
340 | 341 | TRY_COMPARE(spy1.count(), 1); |
|
341 | 342 | TRY_COMPARE(spy2.count(), 1); |
|
342 | 343 | TRY_COMPARE(spy3.count(), 1); |
|
343 | 344 | } |
|
344 | 345 | |
|
345 | 346 | void tst_qpieseries::removeAnimated() |
|
346 | 347 | { |
|
347 | 348 | m_view->chart()->setAnimationOptions(QChart::AllAnimations); |
|
348 | 349 | remove(); |
|
349 | 350 | } |
|
350 | 351 | |
|
351 | 352 | void tst_qpieseries::take() |
|
352 | 353 | { |
|
353 | 354 | m_view->chart()->addSeries(m_series); |
|
354 | 355 | QSignalSpy removedSpy(m_series, SIGNAL(removed(QList<QPieSlice*>))); |
|
355 | 356 | |
|
356 | 357 | // add some slices |
|
357 | 358 | QPieSlice *slice1 = m_series->append("slice 1", 1); |
|
358 | 359 | QPieSlice *slice2 = m_series->append("slice 2", 2); |
|
359 | 360 | m_series->append("slice 3", 3); |
|
360 | 361 | QSignalSpy spy1(slice1, SIGNAL(destroyed())); |
|
361 | 362 | QCOMPARE(m_series->count(), 3); |
|
362 | 363 | |
|
363 | 364 | // null pointer remove |
|
364 | 365 | QVERIFY(!m_series->take(0)); |
|
365 | 366 | |
|
366 | 367 | // take first |
|
367 | 368 | QVERIFY(m_series->take(slice1)); |
|
368 | 369 | TRY_COMPARE(spy1.count(), 0); |
|
369 | 370 | QVERIFY(slice1->parent() == m_series); // series is still the parent object |
|
370 | 371 | QVERIFY(!m_series->take(slice1)); |
|
371 | 372 | QCOMPARE(m_series->count(), 2); |
|
372 | 373 | QCOMPARE(m_series->slices().at(0)->label(), slice2->label()); |
|
373 | 374 | QCOMPARE(removedSpy.count(), 1); |
|
374 | 375 | QList<QPieSlice*> removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(0).at(0)); |
|
375 | 376 | QCOMPARE(removed.count(), 1); |
|
376 | 377 | QCOMPARE(removed.first(), slice1); |
|
377 | 378 | } |
|
378 | 379 | |
|
379 | 380 | void tst_qpieseries::takeAnimated() |
|
380 | 381 | { |
|
381 | 382 | m_view->chart()->setAnimationOptions(QChart::AllAnimations); |
|
382 | 383 | take(); |
|
383 | 384 | } |
|
384 | 385 | |
|
385 | 386 | void tst_qpieseries::calculatedValues() |
|
386 | 387 | { |
|
387 | 388 | m_view->chart()->addSeries(m_series); |
|
388 | 389 | |
|
389 | 390 | QPieSlice *slice1 = new QPieSlice("slice 1", 1); |
|
390 | 391 | QSignalSpy percentageSpy(slice1, SIGNAL(percentageChanged())); |
|
391 | 392 | QSignalSpy startAngleSpy(slice1, SIGNAL(startAngleChanged())); |
|
392 | 393 | QSignalSpy angleSpanSpy(slice1, SIGNAL(angleSpanChanged())); |
|
393 | 394 | |
|
394 | 395 | // add a slice |
|
395 | 396 | m_series->append(slice1); |
|
396 | 397 | bool ok; |
|
397 | 398 | verifyCalculatedData(*m_series, &ok); |
|
398 | 399 | if (!ok) |
|
399 | 400 | return; |
|
400 | 401 | QCOMPARE(percentageSpy.count(), 1); |
|
401 | 402 | QCOMPARE(startAngleSpy.count(), 0); |
|
402 | 403 | QCOMPARE(angleSpanSpy.count(), 1); |
|
403 | 404 | |
|
404 | 405 | // add some more slices |
|
405 | 406 | QList<QPieSlice *> list; |
|
406 | 407 | list << new QPieSlice("slice 2", 2); |
|
407 | 408 | list << new QPieSlice("slice 3", 3); |
|
408 | 409 | m_series->append(list); |
|
409 | 410 | verifyCalculatedData(*m_series, &ok); |
|
410 | 411 | if (!ok) |
|
411 | 412 | return; |
|
412 | 413 | QCOMPARE(percentageSpy.count(), 2); |
|
413 | 414 | QCOMPARE(startAngleSpy.count(), 0); |
|
414 | 415 | QCOMPARE(angleSpanSpy.count(), 2); |
|
415 | 416 | |
|
416 | 417 | // remove a slice |
|
417 | 418 | m_series->remove(list.first()); // remove slice 2 |
|
418 | 419 | verifyCalculatedData(*m_series, &ok); |
|
419 | 420 | if (!ok) |
|
420 | 421 | return; |
|
421 | 422 | QCOMPARE(percentageSpy.count(), 3); |
|
422 | 423 | QCOMPARE(startAngleSpy.count(), 0); |
|
423 | 424 | QCOMPARE(angleSpanSpy.count(), 3); |
|
424 | 425 | |
|
425 | 426 | // insert a slice |
|
426 | 427 | m_series->insert(0, new QPieSlice("Slice 4", 4)); |
|
427 | 428 | verifyCalculatedData(*m_series, &ok); |
|
428 | 429 | if (!ok) |
|
429 | 430 | return; |
|
430 | 431 | QCOMPARE(percentageSpy.count(), 4); |
|
431 | 432 | QCOMPARE(startAngleSpy.count(), 1); |
|
432 | 433 | QCOMPARE(angleSpanSpy.count(), 4); |
|
433 | 434 | |
|
434 | 435 | // modify pie angles |
|
435 | 436 | m_series->setPieStartAngle(-90); |
|
436 | 437 | m_series->setPieEndAngle(90); |
|
437 | 438 | verifyCalculatedData(*m_series, &ok); |
|
438 | 439 | if (!ok) |
|
439 | 440 | return; |
|
440 | 441 | QCOMPARE(percentageSpy.count(), 4); |
|
441 | 442 | QCOMPARE(startAngleSpy.count(), 3); |
|
442 | 443 | QCOMPARE(angleSpanSpy.count(), 6); |
|
443 | 444 | |
|
444 | 445 | // clear all |
|
445 | 446 | m_series->clear(); |
|
446 | 447 | verifyCalculatedData(*m_series, &ok); |
|
447 | 448 | if (!ok) |
|
448 | 449 | return; |
|
449 | 450 | QCOMPARE(percentageSpy.count(), 4); |
|
450 | 451 | QCOMPARE(startAngleSpy.count(), 3); |
|
451 | 452 | QCOMPARE(angleSpanSpy.count(), 6); |
|
452 | 453 | } |
|
453 | 454 | |
|
454 | 455 | void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok) |
|
455 | 456 | { |
|
456 | 457 | *ok = false; |
|
457 | 458 | |
|
458 | 459 | qreal sum = 0; |
|
459 | 460 | foreach (const QPieSlice *slice, series.slices()) |
|
460 | 461 | sum += slice->value(); |
|
461 | 462 | QCOMPARE(series.sum(), sum); |
|
462 | 463 | |
|
463 | 464 | qreal startAngle = series.pieStartAngle(); |
|
464 | 465 | qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle(); |
|
465 | 466 | foreach (const QPieSlice *slice, series.slices()) { |
|
466 | 467 | qreal ratio = slice->value() / sum; |
|
467 | 468 | qreal sliceSpan = pieAngleSpan * ratio; |
|
468 | 469 | QCOMPARE(slice->startAngle(), startAngle); |
|
469 | 470 | QCOMPARE(slice->angleSpan(), sliceSpan); |
|
470 | 471 | QCOMPARE(slice->percentage(), ratio); |
|
471 | 472 | startAngle += sliceSpan; |
|
472 | 473 | } |
|
473 | 474 | |
|
474 | 475 | if (!series.isEmpty()) |
|
475 | 476 | QCOMPARE(series.slices().last()->startAngle() + series.slices().last()->angleSpan(), series.pieEndAngle()); |
|
476 | 477 | |
|
477 | 478 | *ok = true; |
|
478 | 479 | } |
|
479 | 480 | |
|
480 | ||
|
481 | 481 | void tst_qpieseries::clickedSignal() |
|
482 | 482 | { |
|
483 | // NOTE: | |
|
484 | // This test is the same as tst_qpieslice::clickedSignal() | |
|
485 | // Just for different signals. | |
|
486 | ||
|
483 | 487 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
|
484 | 488 | |
|
485 | 489 | // add some slices |
|
486 | 490 | QPieSlice *s1 = m_series->append("slice 1", 1); |
|
487 | 491 | QPieSlice *s2 = m_series->append("slice 2", 1); |
|
488 | 492 | QPieSlice *s3 = m_series->append("slice 3", 1); |
|
489 | 493 | QPieSlice *s4 = m_series->append("slice 4", 1); |
|
490 | 494 | QSignalSpy clickSpy(m_series, SIGNAL(clicked(QPieSlice*))); |
|
491 | 495 | |
|
492 | 496 | // add series to the chart |
|
493 | 497 | m_view->chart()->legend()->setVisible(false); |
|
494 | m_view->resize(200, 200); | |
|
495 | 498 | m_view->chart()->addSeries(m_series); |
|
496 | 499 | m_view->show(); |
|
497 | 500 | QTest::qWaitForWindowShown(m_view); |
|
498 | 501 | |
|
499 | // if you divide the chart in four equal tiles these | |
|
500 | // are the center points of those tiles | |
|
501 | QPoint p1(90.25, 90); | |
|
502 | QPoint p2(150, 90); | |
|
503 | QPoint p3(90, 150); | |
|
504 | QPoint p4(150, 150); | |
|
505 | ||
|
506 | QPoint center(120, 120); | |
|
507 | ||
|
502 | // test maximum size | |
|
508 | 503 | m_series->setPieSize(1.0); |
|
509 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); | |
|
510 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); | |
|
511 |
QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p |
|
|
512 |
QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p |
|
|
513 |
QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, |
|
|
514 | TRY_COMPARE(clickSpy.count(), 5); // all hit | |
|
515 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(0).at(0)), s4); | |
|
516 |
QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at( |
|
|
504 | QRectF pieRect = m_view->chart()->plotArea(); | |
|
505 | QList<QPoint> points = slicePoints(pieRect); | |
|
506 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(0)); | |
|
507 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(1)); | |
|
508 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(2)); | |
|
509 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(3)); | |
|
510 | TRY_COMPARE(clickSpy.count(), 4); | |
|
511 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(0).at(0)), s1); | |
|
512 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(1).at(0)), s2); | |
|
517 | 513 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(2).at(0)), s3); |
|
518 |
QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(3).at(0)), s |
|
|
514 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(3).at(0)), s4); | |
|
519 | 515 | clickSpy.clear(); |
|
520 | 516 | |
|
517 | // test half size | |
|
521 | 518 | m_series->setPieSize(0.5); |
|
522 | 519 | m_series->setVerticalPosition(0.25); |
|
523 | 520 | m_series->setHorizontalPosition(0.25); |
|
524 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); // hits | |
|
525 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); | |
|
526 |
QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p |
|
|
527 |
QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p |
|
|
528 |
QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, |
|
|
529 | TRY_COMPARE(clickSpy.count(), 1); | |
|
530 | clickSpy.clear(); | |
|
531 | ||
|
532 | m_series->setVerticalPosition(0.25); | |
|
533 | m_series->setHorizontalPosition(0.75); | |
|
534 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); | |
|
535 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); // hits | |
|
536 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); | |
|
537 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); | |
|
538 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center); | |
|
539 | TRY_COMPARE(clickSpy.count(), 1); | |
|
540 | clickSpy.clear(); | |
|
541 | ||
|
542 | m_series->setVerticalPosition(0.75); | |
|
543 | m_series->setHorizontalPosition(0.25); | |
|
544 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); | |
|
545 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); | |
|
546 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); // hits | |
|
547 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); | |
|
548 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center); | |
|
549 | TRY_COMPARE(clickSpy.count(), 1); | |
|
550 | clickSpy.clear(); | |
|
551 | ||
|
552 | m_series->setVerticalPosition(0.75); | |
|
553 | m_series->setHorizontalPosition(0.75); | |
|
554 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); | |
|
555 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); | |
|
556 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); | |
|
557 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); // hits | |
|
558 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center); | |
|
559 | TRY_COMPARE(clickSpy.count(), 1); | |
|
560 | clickSpy.clear(); | |
|
521 | pieRect = QRectF(m_view->chart()->plotArea().topLeft(), m_view->chart()->plotArea().center()); | |
|
522 | points = slicePoints(pieRect); | |
|
523 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(0)); | |
|
524 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(1)); | |
|
525 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(2)); | |
|
526 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(3)); | |
|
527 | TRY_COMPARE(clickSpy.count(), 4); | |
|
528 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(0).at(0)), s1); | |
|
529 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(1).at(0)), s2); | |
|
530 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(2).at(0)), s3); | |
|
531 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(3).at(0)), s4); | |
|
561 | 532 | } |
|
562 | 533 | |
|
563 | 534 | void tst_qpieseries::hoverSignal() |
|
564 | 535 | { |
|
536 | // NOTE: | |
|
537 | // This test is the same as tst_qpieslice::hoverSignal() | |
|
538 | // Just for different signals. | |
|
539 | ||
|
565 | 540 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
|
566 | 541 | |
|
567 | 542 | // add some slices |
|
568 | m_series->setPieSize(1.0); | |
|
569 | 543 | QPieSlice *s1 = m_series->append("slice 1", 1); |
|
570 |
m_series->append("slice 2", |
|
|
571 |
m_series->append("slice 3", |
|
|
544 | QPieSlice *s2 = m_series->append("slice 2", 1); | |
|
545 | QPieSlice *s3 = m_series->append("slice 3", 1); | |
|
546 | QPieSlice *s4 = m_series->append("slice 4", 1); | |
|
572 | 547 | |
|
573 | 548 | // add series to the chart |
|
574 | 549 | m_view->chart()->legend()->setVisible(false); |
|
575 | m_view->resize(200, 200); | |
|
576 | 550 | m_view->chart()->addSeries(m_series); |
|
577 | 551 | m_view->show(); |
|
578 | 552 | QTest::qWaitForWindowShown(m_view); |
|
579 | 553 | |
|
580 | // first move to right top corner | |
|
581 | QTest::mouseMove(m_view->viewport(), QPoint(200, 0)); | |
|
582 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |
|
583 | ||
|
584 | // move inside the slice | |
|
585 | // pie rectangle: QRectF(60,60 121x121) | |
|
554 | // move inside the slices | |
|
555 | m_series->setPieSize(1.0); | |
|
556 | QRectF pieRect = m_view->chart()->plotArea(); | |
|
557 | QList<QPoint> points = slicePoints(pieRect); | |
|
558 | QTest::mouseMove(m_view->viewport(), pieRect.topRight().toPoint()); | |
|
586 | 559 | QSignalSpy hoverSpy(m_series, SIGNAL(hovered(QPieSlice*,bool))); |
|
587 |
QTest::mouseMove(m_view->viewport(), |
|
|
588 | TRY_COMPARE(hoverSpy.count(), 1); | |
|
589 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1); | |
|
590 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true); | |
|
591 | ||
|
592 | // move outside the slice | |
|
593 | QTest::mouseMove(m_view->viewport(), QPoint(200, 0)); | |
|
594 |
|
|
|
595 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1); | |
|
596 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false); | |
|
560 | QTest::mouseMove(m_view->viewport(), points.at(0)); | |
|
561 | QTest::mouseMove(m_view->viewport(), points.at(1)); | |
|
562 | QTest::mouseMove(m_view->viewport(), points.at(2)); | |
|
563 | QTest::mouseMove(m_view->viewport(), points.at(3)); | |
|
564 | QTest::mouseMove(m_view->viewport(), pieRect.topLeft().toPoint()); | |
|
565 | ||
|
566 | // check | |
|
567 | QCOMPARE(hoverSpy.count(), 8); | |
|
568 | int i = 0; | |
|
569 | foreach (QPieSlice *s, m_series->slices()) { | |
|
570 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(i).at(0)), s); | |
|
571 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(i).at(1)), true); | |
|
572 | i++; | |
|
573 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(i).at(0)), s); | |
|
574 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(i).at(1)), false); | |
|
575 | i++; | |
|
576 | } | |
|
597 | 577 | } |
|
598 | 578 | |
|
599 | 579 | void tst_qpieseries::sliceSeries() |
|
600 | 580 | { |
|
601 | 581 | QPieSlice *slice = new QPieSlice(); |
|
602 | 582 | QVERIFY(!slice->series()); |
|
603 | 583 | delete slice; |
|
604 | 584 | |
|
605 | 585 | slice = new QPieSlice(m_series); |
|
606 | 586 | QVERIFY(!slice->series()); |
|
607 | 587 | |
|
608 | 588 | m_series->append(slice); |
|
609 | 589 | QCOMPARE(slice->series(), m_series); |
|
610 | 590 | |
|
611 | 591 | slice = new QPieSlice(); |
|
612 | 592 | m_series->insert(0, slice); |
|
613 | 593 | QCOMPARE(slice->series(), m_series); |
|
614 | 594 | |
|
615 | 595 | m_series->take(slice); |
|
616 | 596 | QCOMPARE(slice->series(), (QPieSeries*) 0); |
|
617 | 597 | } |
|
618 | 598 | |
|
619 | 599 | void tst_qpieseries::destruction() |
|
620 | 600 | { |
|
621 | 601 | // add some slices |
|
622 | 602 | QPieSlice *slice1 = m_series->append("slice 1", 1); |
|
623 | 603 | QPieSlice *slice2 = m_series->append("slice 2", 2); |
|
624 | 604 | QPieSlice *slice3 = m_series->append("slice 3", 3); |
|
625 | 605 | QSignalSpy spy1(slice1, SIGNAL(destroyed())); |
|
626 | 606 | QSignalSpy spy2(slice2, SIGNAL(destroyed())); |
|
627 | 607 | QSignalSpy spy3(slice3, SIGNAL(destroyed())); |
|
628 | 608 | |
|
629 | 609 | // destroy series |
|
630 | 610 | delete m_series; |
|
631 | 611 | m_series = 0; |
|
632 | 612 | |
|
633 | 613 | // check that series has destroyed its slices |
|
634 | 614 | QCOMPARE(spy1.count(), 1); |
|
635 | 615 | QCOMPARE(spy2.count(), 1); |
|
636 | 616 | QCOMPARE(spy3.count(), 1); |
|
637 | 617 | } |
|
638 | 618 | |
|
619 | QList<QPoint> tst_qpieseries::slicePoints(QRectF rect) | |
|
620 | { | |
|
621 | qreal x1 = rect.topLeft().x() + (rect.width() / 4); | |
|
622 | qreal x2 = rect.topLeft().x() + (rect.width() / 4) * 3; | |
|
623 | qreal y1 = rect.topLeft().y() + (rect.height() / 4); | |
|
624 | qreal y2 = rect.topLeft().y() + (rect.height() / 4) * 3; | |
|
625 | QList<QPoint> points; | |
|
626 | points << QPoint(x2, y1); | |
|
627 | points << QPoint(x2, y2); | |
|
628 | points << QPoint(x1, y2); | |
|
629 | points << QPoint(x1, y1); | |
|
630 | return points; | |
|
631 | } | |
|
632 | ||
|
639 | 633 | QTEST_MAIN(tst_qpieseries) |
|
640 | 634 | |
|
641 | 635 | #include "tst_qpieseries.moc" |
|
642 | 636 |
@@ -1,296 +1,331 | |||
|
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 |
void |
|
|
45 |
void |
|
|
44 | void clickedSignal(); | |
|
45 | void hoverSignal(); | |
|
46 | 46 | |
|
47 | 47 | private: |
|
48 | ||
|
48 | QList<QPoint> slicePoints(QRectF rect); | |
|
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.labelBrush(), QBrush()); |
|
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.angleSpan(), 0.0); |
|
89 | 89 | |
|
90 | 90 | // value and label params |
|
91 | 91 | QPieSlice slice2("foobar", 1.0); |
|
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.labelBrush(), QBrush()); |
|
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.angleSpan(), 0.0); |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | void tst_qpieslice::changedSignals() |
|
108 | 108 | { |
|
109 | 109 | QPieSlice slice; |
|
110 | 110 | |
|
111 | 111 | QSignalSpy valueSpy(&slice, SIGNAL(valueChanged())); |
|
112 | 112 | QSignalSpy labelSpy(&slice, SIGNAL(labelChanged())); |
|
113 | 113 | QSignalSpy penSpy(&slice, SIGNAL(penChanged())); |
|
114 | 114 | QSignalSpy brushSpy(&slice, SIGNAL(brushChanged())); |
|
115 | 115 | QSignalSpy labelBrushSpy(&slice, SIGNAL(labelBrushChanged())); |
|
116 | 116 | QSignalSpy labelFontSpy(&slice, SIGNAL(labelFontChanged())); |
|
117 | 117 | QSignalSpy colorSpy(&slice, SIGNAL(colorChanged())); |
|
118 | 118 | QSignalSpy borderColorSpy(&slice, SIGNAL(borderColorChanged())); |
|
119 | 119 | QSignalSpy borderWidthSpy(&slice, SIGNAL(borderWidthChanged())); |
|
120 | 120 | QSignalSpy labelColorSpy(&slice, SIGNAL(labelColorChanged())); |
|
121 | 121 | |
|
122 | 122 | // percentageChanged(), startAngleChanged() and angleSpanChanged() signals tested at tst_qpieseries::calculatedValues() |
|
123 | 123 | |
|
124 | 124 | // set everything twice to see we do not get unnecessary signals |
|
125 | 125 | slice.setValue(1.0); |
|
126 | 126 | slice.setValue(-1.0); |
|
127 | 127 | QCOMPARE(slice.value(), 1.0); |
|
128 | 128 | slice.setLabel("foobar"); |
|
129 | 129 | slice.setLabel("foobar"); |
|
130 | 130 | slice.setLabelVisible(); |
|
131 | 131 | slice.setLabelVisible(); |
|
132 | 132 | slice.setExploded(); |
|
133 | 133 | slice.setExploded(); |
|
134 | 134 | slice.setPen(QPen(Qt::red)); |
|
135 | 135 | slice.setPen(QPen(QBrush(Qt::red), 3)); |
|
136 | 136 | slice.setBrush(QBrush(Qt::red)); |
|
137 | 137 | slice.setBrush(QBrush(Qt::red)); |
|
138 | 138 | slice.setLabelBrush(QBrush(Qt::green)); |
|
139 | 139 | slice.setLabelBrush(QBrush(Qt::green)); |
|
140 | 140 | slice.setLabelFont(QFont("Tahoma")); |
|
141 | 141 | slice.setLabelFont(QFont("Tahoma")); |
|
142 | 142 | slice.setLabelPosition(QPieSlice::LabelInsideHorizontal); |
|
143 | 143 | slice.setLabelPosition(QPieSlice::LabelInsideHorizontal); |
|
144 | 144 | slice.setLabelArmLengthFactor(0.1); |
|
145 | 145 | slice.setLabelArmLengthFactor(0.1); |
|
146 | 146 | slice.setExplodeDistanceFactor(0.1); |
|
147 | 147 | slice.setExplodeDistanceFactor(0.1); |
|
148 | 148 | |
|
149 | 149 | TRY_COMPARE(valueSpy.count(), 1); |
|
150 | 150 | TRY_COMPARE(labelSpy.count(), 1); |
|
151 | 151 | TRY_COMPARE(penSpy.count(), 2); |
|
152 | 152 | TRY_COMPARE(brushSpy.count(), 1); |
|
153 | 153 | TRY_COMPARE(labelBrushSpy.count(), 1); |
|
154 | 154 | TRY_COMPARE(labelFontSpy.count(), 1); |
|
155 | 155 | TRY_COMPARE(colorSpy.count(), 1); |
|
156 | 156 | TRY_COMPARE(borderColorSpy.count(), 1); |
|
157 | 157 | TRY_COMPARE(borderWidthSpy.count(), 1); |
|
158 | 158 | TRY_COMPARE(labelColorSpy.count(), 1); |
|
159 | 159 | } |
|
160 | 160 | |
|
161 | 161 | void tst_qpieslice::customize() |
|
162 | 162 | { |
|
163 | 163 | // create a pie series |
|
164 | 164 | QPieSeries *series = new QPieSeries(); |
|
165 | 165 | QPieSlice *s1 = series->append("slice 1", 1); |
|
166 | 166 | QPieSlice *s2 = series->append("slice 2", 2); |
|
167 | 167 | series->append("slice 3", 3); |
|
168 | 168 | |
|
169 | 169 | // customize a slice |
|
170 | 170 | QPen p1(Qt::red); |
|
171 | 171 | s1->setPen(p1); |
|
172 | 172 | QBrush b1(Qt::red); |
|
173 | 173 | s1->setBrush(b1); |
|
174 | 174 | s1->setLabelBrush(b1); |
|
175 | 175 | QFont f1("Consolas"); |
|
176 | 176 | s1->setLabelFont(f1); |
|
177 | 177 | |
|
178 | 178 | // add series to the chart |
|
179 | 179 | QChartView view(new QChart()); |
|
180 | 180 | view.resize(200, 200); |
|
181 | 181 | view.chart()->addSeries(series); |
|
182 | 182 | view.show(); |
|
183 | 183 | QTest::qWaitForWindowShown(&view); |
|
184 | 184 | //QTest::qWait(1000); |
|
185 | 185 | |
|
186 | 186 | // check that customizations persist |
|
187 | 187 | QCOMPARE(s1->pen(), p1); |
|
188 | 188 | QCOMPARE(s1->brush(), b1); |
|
189 | 189 | QCOMPARE(s1->labelBrush(), b1); |
|
190 | 190 | QCOMPARE(s1->labelFont(), f1); |
|
191 | 191 | |
|
192 | 192 | // remove a slice |
|
193 | 193 | series->remove(s2); |
|
194 | 194 | QCOMPARE(s1->pen(), p1); |
|
195 | 195 | QCOMPARE(s1->brush(), b1); |
|
196 | 196 | QCOMPARE(s1->labelBrush(), b1); |
|
197 | 197 | QCOMPARE(s1->labelFont(), f1); |
|
198 | 198 | |
|
199 | 199 | // add a slice |
|
200 | 200 | series->append("slice 4", 4); |
|
201 | 201 | QCOMPARE(s1->pen(), p1); |
|
202 | 202 | QCOMPARE(s1->brush(), b1); |
|
203 | 203 | QCOMPARE(s1->labelBrush(), b1); |
|
204 | 204 | QCOMPARE(s1->labelFont(), f1); |
|
205 | 205 | |
|
206 | 206 | // insert a slice |
|
207 | 207 | series->insert(0, new QPieSlice("slice 5", 5)); |
|
208 | 208 | QCOMPARE(s1->pen(), p1); |
|
209 | 209 | QCOMPARE(s1->brush(), b1); |
|
210 | 210 | QCOMPARE(s1->labelBrush(), b1); |
|
211 | 211 | QCOMPARE(s1->labelFont(), f1); |
|
212 | 212 | |
|
213 | 213 | // change theme |
|
214 | 214 | // theme will overwrite customizations |
|
215 | 215 | view.chart()->setTheme(QChart::ChartThemeHighContrast); |
|
216 | 216 | QVERIFY(s1->pen() != p1); |
|
217 | 217 | QVERIFY(s1->brush() != b1); |
|
218 | 218 | QVERIFY(s1->labelBrush() != b1); |
|
219 | 219 | QVERIFY(s1->labelFont() != f1); |
|
220 | 220 | } |
|
221 | 221 | |
|
222 |
void tst_qpieslice:: |
|
|
222 | void tst_qpieslice::clickedSignal() | |
|
223 | 223 | { |
|
224 | // NOTE: | |
|
225 | // This test is the same as tst_qpieseries::clickedSignal() | |
|
226 | // Just for different signals. | |
|
227 | ||
|
228 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); | |
|
229 | ||
|
224 | 230 | // create a pie series |
|
225 | 231 | QPieSeries *series = new QPieSeries(); |
|
226 | series->setPieSize(1.0); | |
|
227 | 232 | QPieSlice *s1 = series->append("slice 1", 1); |
|
228 |
QPieSlice *s2 = series->append("slice 2", |
|
|
229 |
QPieSlice *s3 = series->append("slice 3", |
|
|
233 | QPieSlice *s2 = series->append("slice 2", 1); | |
|
234 | QPieSlice *s3 = series->append("slice 3", 1); | |
|
235 | QPieSlice *s4 = series->append("slice 4", 1); | |
|
230 | 236 | QSignalSpy clickSpy1(s1, SIGNAL(clicked())); |
|
231 | 237 | QSignalSpy clickSpy2(s2, SIGNAL(clicked())); |
|
232 | 238 | QSignalSpy clickSpy3(s3, SIGNAL(clicked())); |
|
239 | QSignalSpy clickSpy4(s4, SIGNAL(clicked())); | |
|
233 | 240 | |
|
234 | 241 | // add series to the chart |
|
235 |
QChartView view |
|
|
242 | QChartView view; | |
|
236 | 243 | view.chart()->legend()->setVisible(false); |
|
237 | view.resize(200, 200); | |
|
238 | 244 | view.chart()->addSeries(series); |
|
239 | 245 | view.show(); |
|
240 | 246 | QTest::qWaitForWindowShown(&view); |
|
241 | 247 | |
|
242 | 248 | // simulate clicks |
|
243 | // pie rectangle: QRectF(60,60 121x121) | |
|
244 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1 | |
|
245 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 136)); // inside slice 2 | |
|
246 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, |
|
|
247 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, |
|
|
248 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, |
|
|
249 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |
|
249 | series->setPieSize(1.0); | |
|
250 | QRectF pieRect = view.chart()->plotArea(); | |
|
251 | QList<QPoint> points = slicePoints(pieRect); | |
|
252 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(0)); | |
|
253 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(1)); | |
|
254 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(2)); | |
|
255 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(3)); | |
|
250 | 256 | QCOMPARE(clickSpy1.count(), 1); |
|
251 | 257 | QCOMPARE(clickSpy2.count(), 1); |
|
252 | 258 | QCOMPARE(clickSpy3.count(), 1); |
|
259 | QCOMPARE(clickSpy4.count(), 1); | |
|
253 | 260 | } |
|
254 | 261 | |
|
255 |
void tst_qpieslice:: |
|
|
262 | void tst_qpieslice::hoverSignal() | |
|
256 | 263 | { |
|
257 | // create a pie series | |
|
264 | // NOTE: | |
|
265 | // This test is the same as tst_qpieseries::hoverSignal() | |
|
266 | // Just for different signals. | |
|
267 | ||
|
268 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); | |
|
269 | ||
|
270 | // add some slices | |
|
258 | 271 | QPieSeries *series = new QPieSeries(); |
|
259 | series->setPieSize(1.0); | |
|
260 | 272 | QPieSlice *s1 = series->append("slice 1", 1); |
|
261 |
series->append("slice 2", |
|
|
262 |
series->append("slice 3", |
|
|
273 | QPieSlice *s2 = series->append("slice 2", 1); | |
|
274 | QPieSlice *s3 = series->append("slice 3", 1); | |
|
275 | QPieSlice *s4 = series->append("slice 4", 1); | |
|
263 | 276 | |
|
264 | 277 | // add series to the chart |
|
265 |
QChartView view |
|
|
278 | QChartView view; | |
|
266 | 279 | view.chart()->legend()->setVisible(false); |
|
267 | view.resize(200, 200); | |
|
268 | 280 | view.chart()->addSeries(series); |
|
269 | 281 | view.show(); |
|
270 | 282 | QTest::qWaitForWindowShown(&view); |
|
271 | 283 | |
|
272 | // first move to right top corner | |
|
273 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); | |
|
274 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |
|
275 | ||
|
276 | // move inside slice rectangle but NOT the actual slice | |
|
277 | // pie rectangle: QRectF(60,60 121x121) | |
|
278 |
QSignalSpy hoverSpy(s |
|
|
279 | QTest::mouseMove(view.viewport(), QPoint(170, 70)); | |
|
280 | TRY_COMPARE(hoverSpy.count(), 0); | |
|
281 | ||
|
282 | // move inside the slice | |
|
283 |
QTest::mouseMove(view.viewport(), |
|
|
284 | TRY_COMPARE(hoverSpy.count(), 1); | |
|
285 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(0)), true); | |
|
286 | ||
|
287 | // move outside the slice | |
|
288 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); | |
|
289 | TRY_COMPARE(hoverSpy.count(), 2); | |
|
290 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(0)), false); | |
|
284 | // move inside the slices | |
|
285 | series->setPieSize(1.0); | |
|
286 | QRectF pieRect = view.chart()->plotArea(); | |
|
287 | QList<QPoint> points = slicePoints(pieRect); | |
|
288 | QTest::mouseMove(view.viewport(), pieRect.topRight().toPoint()); | |
|
289 | QSignalSpy hoverSpy1(s1, SIGNAL(hovered(bool))); | |
|
290 | QSignalSpy hoverSpy2(s2, SIGNAL(hovered(bool))); | |
|
291 | QSignalSpy hoverSpy3(s3, SIGNAL(hovered(bool))); | |
|
292 | QSignalSpy hoverSpy4(s4, SIGNAL(hovered(bool))); | |
|
293 | QTest::mouseMove(view.viewport(), points.at(0)); | |
|
294 | QTest::mouseMove(view.viewport(), points.at(1)); | |
|
295 | QTest::mouseMove(view.viewport(), points.at(2)); | |
|
296 | QTest::mouseMove(view.viewport(), points.at(3)); | |
|
297 | QTest::mouseMove(view.viewport(), pieRect.topLeft().toPoint()); | |
|
298 | ||
|
299 | // check | |
|
300 | QCOMPARE(hoverSpy1.count(), 2); | |
|
301 | QCOMPARE(qvariant_cast<bool>(hoverSpy1.at(0).at(0)), true); | |
|
302 | QCOMPARE(qvariant_cast<bool>(hoverSpy1.at(1).at(0)), false); | |
|
303 | QCOMPARE(hoverSpy2.count(), 2); | |
|
304 | QCOMPARE(qvariant_cast<bool>(hoverSpy2.at(0).at(0)), true); | |
|
305 | QCOMPARE(qvariant_cast<bool>(hoverSpy2.at(1).at(0)), false); | |
|
306 | QCOMPARE(hoverSpy3.count(), 2); | |
|
307 | QCOMPARE(qvariant_cast<bool>(hoverSpy3.at(0).at(0)), true); | |
|
308 | QCOMPARE(qvariant_cast<bool>(hoverSpy3.at(1).at(0)), false); | |
|
309 | QCOMPARE(hoverSpy4.count(), 2); | |
|
310 | QCOMPARE(qvariant_cast<bool>(hoverSpy4.at(0).at(0)), true); | |
|
311 | QCOMPARE(qvariant_cast<bool>(hoverSpy4.at(1).at(0)), false); | |
|
312 | } | |
|
313 | ||
|
314 | QList<QPoint> tst_qpieslice::slicePoints(QRectF rect) | |
|
315 | { | |
|
316 | qreal x1 = rect.topLeft().x() + (rect.width() / 4); | |
|
317 | qreal x2 = rect.topLeft().x() + (rect.width() / 4) * 3; | |
|
318 | qreal y1 = rect.topLeft().y() + (rect.height() / 4); | |
|
319 | qreal y2 = rect.topLeft().y() + (rect.height() / 4) * 3; | |
|
320 | QList<QPoint> points; | |
|
321 | points << QPoint(x2, y1); | |
|
322 | points << QPoint(x2, y2); | |
|
323 | points << QPoint(x1, y2); | |
|
324 | points << QPoint(x1, y1); | |
|
325 | return points; | |
|
291 | 326 | } |
|
292 | 327 | |
|
293 | 328 | QTEST_MAIN(tst_qpieslice) |
|
294 | 329 | |
|
295 | 330 | #include "tst_qpieslice.moc" |
|
296 | 331 |
General Comments 0
You need to be logged in to leave comments.
Login now