@@ -1,328 +1,329 | |||
|
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 <tst_definitions.h> |
|
27 | 27 | |
|
28 | 28 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
29 | 29 | |
|
30 | 30 | Q_DECLARE_METATYPE(QPieSlice*) |
|
31 | 31 | |
|
32 | 32 | class tst_qpieseries : public QObject |
|
33 | 33 | { |
|
34 | 34 | Q_OBJECT |
|
35 | 35 | |
|
36 | 36 | public slots: |
|
37 | 37 | void initTestCase(); |
|
38 | 38 | void cleanupTestCase(); |
|
39 | 39 | void init(); |
|
40 | 40 | void cleanup(); |
|
41 | 41 | |
|
42 | 42 | private slots: |
|
43 | 43 | void construction(); |
|
44 | 44 | void append(); |
|
45 | 45 | void insert(); |
|
46 | 46 | void remove(); |
|
47 | 47 | void calculatedValues(); |
|
48 | 48 | void clickedSignal(); |
|
49 | 49 | void hoverSignal(); |
|
50 | 50 | |
|
51 | 51 | private: |
|
52 | 52 | void verifyCalculatedData(const QPieSeries &series, bool *ok); |
|
53 | 53 | |
|
54 | 54 | private: |
|
55 | 55 | |
|
56 | 56 | }; |
|
57 | 57 | |
|
58 | 58 | void tst_qpieseries::initTestCase() |
|
59 | 59 | { |
|
60 | 60 | qRegisterMetaType<QPieSlice*>("QPieSlice*"); |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | void tst_qpieseries::cleanupTestCase() |
|
64 | 64 | { |
|
65 | 65 | } |
|
66 | 66 | |
|
67 | 67 | void tst_qpieseries::init() |
|
68 | 68 | { |
|
69 | 69 | |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | void tst_qpieseries::cleanup() |
|
73 | 73 | { |
|
74 | 74 | |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | void tst_qpieseries::construction() |
|
78 | 78 | { |
|
79 | 79 | // verify default values |
|
80 | 80 | QPieSeries s; |
|
81 | 81 | QVERIFY(s.type() == QAbstractSeries::SeriesTypePie); |
|
82 | 82 | QVERIFY(s.count() == 0); |
|
83 | 83 | QVERIFY(s.isEmpty()); |
|
84 | 84 | QCOMPARE(s.sum(), 0.0); |
|
85 | 85 | QCOMPARE(s.horizontalPosition(), 0.5); |
|
86 | 86 | QCOMPARE(s.verticalPosition(), 0.5); |
|
87 | 87 | QCOMPARE(s.pieSize(), 0.7); |
|
88 | 88 | QCOMPARE(s.pieStartAngle(), 0.0); |
|
89 | 89 | QCOMPARE(s.pieEndAngle(), 360.0); |
|
90 | 90 | } |
|
91 | 91 | |
|
92 | 92 | void tst_qpieseries::append() |
|
93 | 93 | { |
|
94 | 94 | QPieSeries s; |
|
95 | 95 | |
|
96 | 96 | // append pointer |
|
97 | 97 | QPieSlice *slice1 = 0; |
|
98 | 98 | QVERIFY(!s.append(slice1)); |
|
99 | 99 | slice1 = new QPieSlice(1, "slice 1"); |
|
100 | 100 | QVERIFY(s.append(slice1)); |
|
101 | 101 | QVERIFY(!s.append(slice1)); |
|
102 | 102 | QCOMPARE(s.count(), 1); |
|
103 | 103 | |
|
104 | 104 | // append pointer list |
|
105 | 105 | QList<QPieSlice *> list; |
|
106 | 106 | QVERIFY(!s.append(list)); |
|
107 | 107 | list << (QPieSlice *) 0; |
|
108 | 108 | QVERIFY(!s.append(list)); |
|
109 | 109 | list.clear(); |
|
110 | 110 | list << new QPieSlice(2, "slice 2"); |
|
111 | 111 | list << new QPieSlice(3, "slice 3"); |
|
112 | 112 | QVERIFY(s.append(list)); |
|
113 | 113 | QVERIFY(!s.append(list)); |
|
114 | 114 | QCOMPARE(s.count(), 3); |
|
115 | 115 | |
|
116 | 116 | // append operator |
|
117 | 117 | s << new QPieSlice(4, "slice 4"); |
|
118 | 118 | s << slice1; // fails because already added |
|
119 | 119 | QCOMPARE(s.count(), 4); |
|
120 | 120 | |
|
121 | 121 | // append with params |
|
122 | 122 | QPieSlice *slice5 = s.append(5, "slice 5"); |
|
123 | 123 | QVERIFY(slice5 != 0); |
|
124 | 124 | QCOMPARE(slice5->value(), 5.0); |
|
125 | 125 | QCOMPARE(slice5->label(), QString("slice 5")); |
|
126 | 126 | QCOMPARE(s.count(), 5); |
|
127 | 127 | |
|
128 | 128 | // check slices |
|
129 | 129 | QVERIFY(!s.isEmpty()); |
|
130 | 130 | for (int i=0; i<s.count(); i++) { |
|
131 | 131 | QCOMPARE(s.slices().at(i)->value(), (qreal) i+1); |
|
132 | 132 | QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1)); |
|
133 | 133 | } |
|
134 | 134 | } |
|
135 | 135 | |
|
136 | 136 | void tst_qpieseries::insert() |
|
137 | 137 | { |
|
138 | 138 | QPieSeries s; |
|
139 | 139 | |
|
140 | 140 | // insert one slice |
|
141 | 141 | QPieSlice *slice1 = 0; |
|
142 | 142 | QVERIFY(!s.insert(0, slice1)); |
|
143 | 143 | slice1 = new QPieSlice(1, "slice 1"); |
|
144 | 144 | QVERIFY(!s.insert(-1, slice1)); |
|
145 | 145 | QVERIFY(!s.insert(5, slice1)); |
|
146 | 146 | QVERIFY(s.insert(0, slice1)); |
|
147 | 147 | QVERIFY(!s.insert(0, slice1)); |
|
148 | 148 | QCOMPARE(s.count(), 1); |
|
149 | 149 | |
|
150 | 150 | // add some more slices |
|
151 | 151 | s.append(2, "slice 2"); |
|
152 | 152 | s.append(4, "slice 4"); |
|
153 | 153 | QCOMPARE(s.count(), 3); |
|
154 | 154 | |
|
155 | 155 | // insert between slices |
|
156 | 156 | s.insert(2, new QPieSlice(3, "slice 3")); |
|
157 | 157 | QCOMPARE(s.count(), 4); |
|
158 | 158 | |
|
159 | 159 | // check slices |
|
160 | 160 | for (int i=0; i<s.count(); i++) { |
|
161 | 161 | QCOMPARE(s.slices().at(i)->value(), (qreal) i+1); |
|
162 | 162 | QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1)); |
|
163 | 163 | } |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | void tst_qpieseries::remove() |
|
167 | 167 | { |
|
168 | 168 | QPieSeries s; |
|
169 | 169 | |
|
170 | 170 | // add some slices |
|
171 | 171 | QPieSlice *slice1 = s.append(1, "slice 1"); |
|
172 | 172 | QPieSlice *slice2 = s.append(2, "slice 2"); |
|
173 | 173 | QPieSlice *slice3 = s.append(3, "slice 3"); |
|
174 | 174 | QSignalSpy spy1(slice1, SIGNAL(destroyed())); |
|
175 | 175 | QSignalSpy spy2(slice2, SIGNAL(destroyed())); |
|
176 | 176 | QSignalSpy spy3(slice3, SIGNAL(destroyed())); |
|
177 | 177 | QCOMPARE(s.count(), 3); |
|
178 | 178 | |
|
179 | 179 | // null pointer remove |
|
180 | 180 | QVERIFY(!s.remove(0)); |
|
181 | 181 | |
|
182 | 182 | // remove first |
|
183 | 183 | QVERIFY(s.remove(slice1)); |
|
184 | 184 | QVERIFY(!s.remove(slice1)); |
|
185 | 185 | QCOMPARE(s.count(), 2); |
|
186 | 186 | QCOMPARE(s.slices().at(0)->label(), slice2->label()); |
|
187 | 187 | |
|
188 | 188 | // remove all |
|
189 | 189 | s.clear(); |
|
190 | 190 | QVERIFY(s.isEmpty()); |
|
191 | 191 | QVERIFY(s.slices().isEmpty()); |
|
192 | 192 | QCOMPARE(s.count(), 0); |
|
193 | 193 | |
|
194 | 194 | // check that slices were actually destroyed |
|
195 | 195 | TRY_COMPARE(spy1.count(), 1); |
|
196 | 196 | TRY_COMPARE(spy2.count(), 1); |
|
197 | 197 | TRY_COMPARE(spy3.count(), 1); |
|
198 | 198 | } |
|
199 | 199 | |
|
200 | 200 | void tst_qpieseries::calculatedValues() |
|
201 | 201 | { |
|
202 | 202 | bool ok; |
|
203 | 203 | QPieSeries s; |
|
204 | 204 | |
|
205 | 205 | // add a slice |
|
206 | 206 | QPieSlice *slice1 = s.append(1, "slice 1"); |
|
207 | 207 | verifyCalculatedData(s, &ok); |
|
208 | 208 | if (!ok) |
|
209 | 209 | return; |
|
210 | 210 | |
|
211 | 211 | // add some more slices |
|
212 | 212 | QList<QPieSlice *> list; |
|
213 | 213 | list << new QPieSlice(2, "slice 2"); |
|
214 | 214 | list << new QPieSlice(3, "slice 3"); |
|
215 | 215 | s.append(list); |
|
216 | 216 | verifyCalculatedData(s, &ok); |
|
217 | 217 | if (!ok) |
|
218 | 218 | return; |
|
219 | 219 | |
|
220 | 220 | // remove a slice |
|
221 | 221 | s.remove(slice1); |
|
222 | 222 | verifyCalculatedData(s, &ok); |
|
223 | 223 | if (!ok) |
|
224 | 224 | return; |
|
225 | 225 | |
|
226 | 226 | // insert a slice |
|
227 | 227 | s.insert(0, new QPieSlice(1, "Slice 4")); |
|
228 | 228 | verifyCalculatedData(s, &ok); |
|
229 | 229 | if (!ok) |
|
230 | 230 | return; |
|
231 | 231 | |
|
232 | 232 | // clear all |
|
233 | 233 | s.clear(); |
|
234 | 234 | verifyCalculatedData(s, &ok); |
|
235 | 235 | } |
|
236 | 236 | |
|
237 | 237 | void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok) |
|
238 | 238 | { |
|
239 | 239 | *ok = false; |
|
240 | 240 | |
|
241 | 241 | qreal sum = 0; |
|
242 | 242 | foreach (const QPieSlice *slice, series.slices()) |
|
243 | 243 | sum += slice->value(); |
|
244 | 244 | QCOMPARE(series.sum(), sum); |
|
245 | 245 | |
|
246 | 246 | qreal startAngle = series.pieStartAngle(); |
|
247 | 247 | qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle(); |
|
248 | 248 | foreach (const QPieSlice *slice, series.slices()) { |
|
249 | 249 | qreal ratio = slice->value() / sum; |
|
250 | 250 | qreal sliceSpan = pieAngleSpan * ratio; |
|
251 | 251 | QCOMPARE(slice->startAngle(), startAngle); |
|
252 | 252 | QCOMPARE(slice->endAngle(), startAngle + sliceSpan); |
|
253 | 253 | QCOMPARE(slice->percentage(), ratio); |
|
254 | 254 | startAngle += sliceSpan; |
|
255 | 255 | } |
|
256 | 256 | |
|
257 | 257 | if (!series.isEmpty()) |
|
258 | 258 | QCOMPARE(series.slices().last()->endAngle(), series.pieEndAngle()); |
|
259 | 259 | |
|
260 | 260 | *ok = true; |
|
261 | 261 | } |
|
262 | 262 | |
|
263 | 263 | |
|
264 | 264 | void tst_qpieseries::clickedSignal() |
|
265 | 265 | { |
|
266 | 266 | // create a pie series |
|
267 | 267 | QPieSeries *series = new QPieSeries(); |
|
268 | 268 | series->setPieSize(1.0); |
|
269 | 269 | QPieSlice *s1 = series->append(1, "slice 1"); |
|
270 | 270 | series->append(2, "slice 2"); |
|
271 | 271 | series->append(3, "slice 3"); |
|
272 | 272 | QSignalSpy clickSpy1(series, SIGNAL(clicked(QPieSlice*))); |
|
273 | 273 | |
|
274 | 274 | // add series to the chart |
|
275 | 275 | QChartView view(new QChart()); |
|
276 | 276 | view.resize(200, 200); |
|
277 | 277 | view.chart()->addSeries(series); |
|
278 | 278 | view.show(); |
|
279 | 279 | QTest::qWaitForWindowShown(&view); |
|
280 | 280 | |
|
281 | 281 | // simulate clicks |
|
282 | 282 | // pie rectangle: QRectF(60,60 121x121) |
|
283 | 283 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1 |
|
284 | 284 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
285 | 285 | QCOMPARE(clickSpy1.count(), 1); |
|
286 | 286 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy1.at(0).at(0)), s1); |
|
287 | 287 | } |
|
288 | 288 | |
|
289 | 289 | void tst_qpieseries::hoverSignal() |
|
290 | 290 | { |
|
291 | 291 | // create a pie series |
|
292 | 292 | QPieSeries *series = new QPieSeries(); |
|
293 | 293 | series->setPieSize(1.0); |
|
294 | 294 | QPieSlice *s1 = series->append(1, "slice 1"); |
|
295 | 295 | series->append(2, "slice 2"); |
|
296 | 296 | series->append(3, "slice 3"); |
|
297 | 297 | |
|
298 | 298 | // add series to the chart |
|
299 | 299 | QChartView view(new QChart()); |
|
300 | 300 | view.resize(200, 200); |
|
301 | 301 | view.chart()->addSeries(series); |
|
302 | 302 | view.show(); |
|
303 | 303 | QTest::qWaitForWindowShown(&view); |
|
304 | 304 | |
|
305 | 305 | // first move to right top corner |
|
306 | 306 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); |
|
307 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |
|
307 | 308 | |
|
308 | 309 | // move inside the slice |
|
309 | 310 | // pie rectangle: QRectF(60,60 121x121) |
|
310 | 311 | QSignalSpy hoverSpy(series, SIGNAL(hovered(QPieSlice*,bool))); |
|
311 | 312 | QTest::mouseMove(view.viewport(), QPoint(139, 85)); |
|
312 | 313 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
313 | 314 | QCOMPARE(hoverSpy.count(), 1); |
|
314 | 315 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1); |
|
315 | 316 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true); |
|
316 | 317 | |
|
317 | 318 | // move outside the slice |
|
318 | 319 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); |
|
319 | 320 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
320 | 321 | QCOMPARE(hoverSpy.count(), 2); |
|
321 | 322 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1); |
|
322 | 323 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false); |
|
323 | 324 | } |
|
324 | 325 | |
|
325 | 326 | QTEST_MAIN(tst_qpieseries) |
|
326 | 327 | |
|
327 | 328 | #include "tst_qpieseries.moc" |
|
328 | 329 |
@@ -1,270 +1,271 | |||
|
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 | 210 | view.resize(200, 200); |
|
211 | 211 | view.chart()->addSeries(series); |
|
212 | 212 | view.show(); |
|
213 | 213 | QTest::qWaitForWindowShown(&view); |
|
214 | 214 | |
|
215 | 215 | // simulate clicks |
|
216 | 216 | // pie rectangle: QRectF(60,60 121x121) |
|
217 | 217 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1 |
|
218 | 218 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 136)); // inside slice 2 |
|
219 | 219 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(91, 119)); // inside slice 3 |
|
220 | 220 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(70, 70)); // inside pie rectangle but not inside a slice |
|
221 | 221 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(170, 170)); // inside pie rectangle but not inside a slice |
|
222 | 222 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
223 | 223 | QCOMPARE(clickSpy1.count(), 1); |
|
224 | 224 | QCOMPARE(clickSpy2.count(), 1); |
|
225 | 225 | QCOMPARE(clickSpy3.count(), 1); |
|
226 | 226 | } |
|
227 | 227 | |
|
228 | 228 | void tst_qpieslice::mouseHover() |
|
229 | 229 | { |
|
230 | 230 | // create a pie series |
|
231 | 231 | QPieSeries *series = new QPieSeries(); |
|
232 | 232 | series->setPieSize(1.0); |
|
233 | 233 | QPieSlice *s1 = series->append(1, "slice 1"); |
|
234 | 234 | series->append(2, "slice 2"); |
|
235 | 235 | series->append(3, "slice 3"); |
|
236 | 236 | |
|
237 | 237 | // add series to the chart |
|
238 | 238 | QChartView view(new QChart()); |
|
239 | 239 | view.resize(200, 200); |
|
240 | 240 | view.chart()->addSeries(series); |
|
241 | 241 | view.show(); |
|
242 | 242 | QTest::qWaitForWindowShown(&view); |
|
243 | 243 | |
|
244 | 244 | // first move to right top corner |
|
245 | 245 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); |
|
246 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |
|
246 | 247 | |
|
247 | 248 | // move inside slice rectangle but NOT the actual slice |
|
248 | 249 | // pie rectangle: QRectF(60,60 121x121) |
|
249 | 250 | QSignalSpy hoverSpy(s1, SIGNAL(hovered(bool))); |
|
250 | 251 | QTest::mouseMove(view.viewport(), QPoint(170, 70)); |
|
251 | 252 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
252 | 253 | QCOMPARE(hoverSpy.count(), 0); |
|
253 | 254 | |
|
254 | 255 | // move inside the slice |
|
255 | 256 | QTest::mouseMove(view.viewport(), QPoint(139, 85)); |
|
256 | 257 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
257 | 258 | QCOMPARE(hoverSpy.count(), 1); |
|
258 | 259 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(0)), true); |
|
259 | 260 | |
|
260 | 261 | // move outside the slice |
|
261 | 262 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); |
|
262 | 263 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
263 | 264 | QCOMPARE(hoverSpy.count(), 2); |
|
264 | 265 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(0)), false); |
|
265 | 266 | } |
|
266 | 267 | |
|
267 | 268 | QTEST_MAIN(tst_qpieslice) |
|
268 | 269 | |
|
269 | 270 | #include "tst_qpieslice.moc" |
|
270 | 271 |
General Comments 0
You need to be logged in to leave comments.
Login now