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