@@ -19,12 +19,16 | |||
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include <QtTest/QtTest> |
|
22 | #include <qchartview.h> | |
|
23 | #include <qchart.h> | |
|
22 | 24 | #include <qpieseries.h> |
|
23 | 25 | #include <qpieslice.h> |
|
24 | 26 | #include <tst_definitions.h> |
|
25 | 27 | |
|
26 | 28 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
27 | 29 | |
|
30 | Q_DECLARE_METATYPE(QPieSlice*) | |
|
31 | ||
|
28 | 32 | class tst_qpieseries : public QObject |
|
29 | 33 | { |
|
30 | 34 | Q_OBJECT |
@@ -40,13 +44,12 private slots: | |||
|
40 | 44 | void append(); |
|
41 | 45 | void insert(); |
|
42 | 46 | void remove(); |
|
43 |
|
|
|
44 | //void themes(); | |
|
45 |
|
|
|
46 | //void hoverSignal(); | |
|
47 | void calculatedValues(); | |
|
48 | void clickedSignal(); | |
|
49 | void hoverSignal(); | |
|
47 | 50 | |
|
48 | 51 | private: |
|
49 | ||
|
52 | void verifyCalculatedData(const QPieSeries &series, bool *ok); | |
|
50 | 53 | |
|
51 | 54 | private: |
|
52 | 55 | |
@@ -54,6 +57,7 private: | |||
|
54 | 57 | |
|
55 | 58 | void tst_qpieseries::initTestCase() |
|
56 | 59 | { |
|
60 | qRegisterMetaType<QPieSlice*>("QPieSlice*"); | |
|
57 | 61 | } |
|
58 | 62 | |
|
59 | 63 | void tst_qpieseries::cleanupTestCase() |
@@ -193,27 +197,130 void tst_qpieseries::remove() | |||
|
193 | 197 | TRY_COMPARE(spy3.count(), 1); |
|
194 | 198 | } |
|
195 | 199 | |
|
196 | /* | |
|
197 | 200 | void tst_qpieseries::calculatedValues() |
|
198 | 201 | { |
|
202 | bool ok; | |
|
203 | QPieSeries s; | |
|
204 | ||
|
205 | // add a slice | |
|
206 | QPieSlice *slice1 = s.append(1, "slice 1"); | |
|
207 | verifyCalculatedData(s, &ok); | |
|
208 | if (!ok) | |
|
209 | return; | |
|
199 | 210 | |
|
211 | // add some more slices | |
|
212 | QList<QPieSlice *> list; | |
|
213 | list << new QPieSlice(2, "slice 2"); | |
|
214 | list << new QPieSlice(3, "slice 3"); | |
|
215 | s.append(list); | |
|
216 | verifyCalculatedData(s, &ok); | |
|
217 | if (!ok) | |
|
218 | return; | |
|
219 | ||
|
220 | // remove a slice | |
|
221 | s.remove(slice1); | |
|
222 | verifyCalculatedData(s, &ok); | |
|
223 | if (!ok) | |
|
224 | return; | |
|
225 | ||
|
226 | // insert a slice | |
|
227 | s.insert(0, new QPieSlice(1, "Slice 4")); | |
|
228 | verifyCalculatedData(s, &ok); | |
|
229 | if (!ok) | |
|
230 | return; | |
|
231 | ||
|
232 | // clear all | |
|
233 | s.clear(); | |
|
234 | verifyCalculatedData(s, &ok); | |
|
200 | 235 | } |
|
201 | 236 | |
|
202 | void tst_qpieseries::themes() | |
|
237 | void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok) | |
|
203 | 238 | { |
|
239 | *ok = false; | |
|
240 | ||
|
241 | qreal sum = 0; | |
|
242 | foreach (const QPieSlice *slice, series.slices()) | |
|
243 | sum += slice->value(); | |
|
244 | QCOMPARE(series.sum(), sum); | |
|
245 | ||
|
246 | qreal startAngle = series.pieStartAngle(); | |
|
247 | qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle(); | |
|
248 | foreach (const QPieSlice *slice, series.slices()) { | |
|
249 | qreal ratio = slice->value() / sum; | |
|
250 | qreal sliceSpan = pieAngleSpan * ratio; | |
|
251 | QCOMPARE(slice->startAngle(), startAngle); | |
|
252 | QCOMPARE(slice->endAngle(), startAngle + sliceSpan); | |
|
253 | QCOMPARE(slice->percentage(), ratio); | |
|
254 | startAngle += sliceSpan; | |
|
255 | } | |
|
256 | ||
|
257 | if (!series.isEmpty()) | |
|
258 | QCOMPARE(series.slices().last()->endAngle(), series.pieEndAngle()); | |
|
204 | 259 | |
|
260 | *ok = true; | |
|
205 | 261 | } |
|
206 | 262 | |
|
263 | ||
|
207 | 264 | void tst_qpieseries::clickedSignal() |
|
208 | 265 | { |
|
209 | ||
|
266 | // create a pie series | |
|
267 | QPieSeries *series = new QPieSeries(); | |
|
268 | series->setPieSize(1.0); | |
|
269 | QPieSlice *s1 = series->append(1, "slice 1"); | |
|
270 | series->append(2, "slice 2"); | |
|
271 | series->append(3, "slice 3"); | |
|
272 | QSignalSpy clickSpy1(series, SIGNAL(clicked(QPieSlice*))); | |
|
273 | ||
|
274 | // add series to the chart | |
|
275 | QChartView view(new QChart()); | |
|
276 | view.resize(200, 200); | |
|
277 | view.chart()->addSeries(series); | |
|
278 | view.show(); | |
|
279 | QTest::qWaitForWindowShown(&view); | |
|
280 | ||
|
281 | // simulate clicks | |
|
282 | // pie rectangle: QRectF(60,60 121x121) | |
|
283 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1 | |
|
284 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |
|
285 | QCOMPARE(clickSpy1.count(), 1); | |
|
286 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy1.at(0).at(0)), s1); | |
|
210 | 287 | } |
|
211 | 288 | |
|
212 | 289 | void tst_qpieseries::hoverSignal() |
|
213 | 290 | { |
|
214 | ||
|
291 | // create a pie series | |
|
292 | QPieSeries *series = new QPieSeries(); | |
|
293 | series->setPieSize(1.0); | |
|
294 | QPieSlice *s1 = series->append(1, "slice 1"); | |
|
295 | series->append(2, "slice 2"); | |
|
296 | series->append(3, "slice 3"); | |
|
297 | ||
|
298 | // add series to the chart | |
|
299 | QChartView view(new QChart()); | |
|
300 | view.resize(200, 200); | |
|
301 | view.chart()->addSeries(series); | |
|
302 | view.show(); | |
|
303 | QTest::qWaitForWindowShown(&view); | |
|
304 | ||
|
305 | // first move to right top corner | |
|
306 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); | |
|
307 | ||
|
308 | // move inside the slice | |
|
309 | // pie rectangle: QRectF(60,60 121x121) | |
|
310 | QSignalSpy hoverSpy(series, SIGNAL(hovered(QPieSlice*,bool))); | |
|
311 | QTest::mouseMove(view.viewport(), QPoint(139, 85)); | |
|
312 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |
|
313 | QCOMPARE(hoverSpy.count(), 1); | |
|
314 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1); | |
|
315 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true); | |
|
316 | ||
|
317 | // move outside the slice | |
|
318 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); | |
|
319 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |
|
320 | QCOMPARE(hoverSpy.count(), 2); | |
|
321 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1); | |
|
322 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false); | |
|
215 | 323 | } |
|
216 | */ | |
|
217 | 324 | |
|
218 | 325 | QTEST_MAIN(tst_qpieseries) |
|
219 | 326 |
@@ -19,8 +19,11 | |||
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include <QtTest/QtTest> |
|
22 | #include <qpieslice.h> | |
|
23 | 22 | #include <tst_definitions.h> |
|
23 | #include <qchartview.h> | |
|
24 | #include <qchart.h> | |
|
25 | #include <qpieslice.h> | |
|
26 | #include <qpieseries.h> | |
|
24 | 27 | |
|
25 | 28 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
26 | 29 | |
@@ -37,6 +40,9 public slots: | |||
|
37 | 40 | private slots: |
|
38 | 41 | void construction(); |
|
39 | 42 | void changedSignals(); |
|
43 | void customize(); | |
|
44 | void mouseClick(); | |
|
45 | void mouseHover(); | |
|
40 | 46 | |
|
41 | 47 | private: |
|
42 | 48 | |
@@ -126,6 +132,138 void tst_qpieslice::changedSignals() | |||
|
126 | 132 | TRY_COMPARE(spy.count(), 10); |
|
127 | 133 | } |
|
128 | 134 | |
|
135 | void tst_qpieslice::customize() | |
|
136 | { | |
|
137 | // create a pie series | |
|
138 | QPieSeries *series = new QPieSeries(); | |
|
139 | QPieSlice *s1 = series->append(1, "slice 1"); | |
|
140 | QPieSlice *s2 = series->append(2, "slice 2"); | |
|
141 | series->append(3, "slice 3"); | |
|
142 | ||
|
143 | // customize a slice | |
|
144 | QPen p1(Qt::red); | |
|
145 | s1->setPen(p1); | |
|
146 | QBrush b1(Qt::red); | |
|
147 | s1->setBrush(b1); | |
|
148 | s1->setLabelPen(p1); | |
|
149 | QFont f1("Consolas"); | |
|
150 | s1->setLabelFont(f1); | |
|
151 | ||
|
152 | // add series to the chart | |
|
153 | QChartView view(new QChart()); | |
|
154 | view.resize(200, 200); | |
|
155 | view.chart()->addSeries(series); | |
|
156 | view.show(); | |
|
157 | QTest::qWaitForWindowShown(&view); | |
|
158 | //QTest::qWait(1000); | |
|
159 | ||
|
160 | // check that customizations persist | |
|
161 | QCOMPARE(s1->pen(), p1); | |
|
162 | QCOMPARE(s1->brush(), b1); | |
|
163 | QCOMPARE(s1->labelPen(), p1); | |
|
164 | QCOMPARE(s1->labelFont(), f1); | |
|
165 | ||
|
166 | // remove a slice | |
|
167 | series->remove(s2); | |
|
168 | QCOMPARE(s1->pen(), p1); | |
|
169 | QCOMPARE(s1->brush(), b1); | |
|
170 | QCOMPARE(s1->labelPen(), p1); | |
|
171 | QCOMPARE(s1->labelFont(), f1); | |
|
172 | ||
|
173 | // add a slice | |
|
174 | series->append(4, "slice 4"); | |
|
175 | QCOMPARE(s1->pen(), p1); | |
|
176 | QCOMPARE(s1->brush(), b1); | |
|
177 | QCOMPARE(s1->labelPen(), p1); | |
|
178 | QCOMPARE(s1->labelFont(), f1); | |
|
179 | ||
|
180 | // insert a slice | |
|
181 | series->insert(0, new QPieSlice(5, "slice 5")); | |
|
182 | QCOMPARE(s1->pen(), p1); | |
|
183 | QCOMPARE(s1->brush(), b1); | |
|
184 | QCOMPARE(s1->labelPen(), p1); | |
|
185 | QCOMPARE(s1->labelFont(), f1); | |
|
186 | ||
|
187 | // change theme | |
|
188 | // theme will overwrite customizations | |
|
189 | view.chart()->setTheme(QChart::ChartThemeHighContrast); | |
|
190 | QVERIFY(s1->pen() != p1); | |
|
191 | QVERIFY(s1->brush() != b1); | |
|
192 | QVERIFY(s1->labelPen() != p1); | |
|
193 | QVERIFY(s1->labelFont() != f1); | |
|
194 | } | |
|
195 | ||
|
196 | void tst_qpieslice::mouseClick() | |
|
197 | { | |
|
198 | // create a pie series | |
|
199 | QPieSeries *series = new QPieSeries(); | |
|
200 | series->setPieSize(1.0); | |
|
201 | QPieSlice *s1 = series->append(1, "slice 1"); | |
|
202 | QPieSlice *s2 = series->append(2, "slice 2"); | |
|
203 | QPieSlice *s3 = series->append(3, "slice 3"); | |
|
204 | QSignalSpy clickSpy1(s1, SIGNAL(clicked())); | |
|
205 | QSignalSpy clickSpy2(s2, SIGNAL(clicked())); | |
|
206 | QSignalSpy clickSpy3(s3, SIGNAL(clicked())); | |
|
207 | ||
|
208 | // add series to the chart | |
|
209 | QChartView view(new QChart()); | |
|
210 | view.resize(200, 200); | |
|
211 | view.chart()->addSeries(series); | |
|
212 | view.show(); | |
|
213 | QTest::qWaitForWindowShown(&view); | |
|
214 | ||
|
215 | // simulate clicks | |
|
216 | // 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(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(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 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |
|
223 | QCOMPARE(clickSpy1.count(), 1); | |
|
224 | QCOMPARE(clickSpy2.count(), 1); | |
|
225 | QCOMPARE(clickSpy3.count(), 1); | |
|
226 | } | |
|
227 | ||
|
228 | void tst_qpieslice::mouseHover() | |
|
229 | { | |
|
230 | // create a pie series | |
|
231 | QPieSeries *series = new QPieSeries(); | |
|
232 | series->setPieSize(1.0); | |
|
233 | QPieSlice *s1 = series->append(1, "slice 1"); | |
|
234 | series->append(2, "slice 2"); | |
|
235 | series->append(3, "slice 3"); | |
|
236 | ||
|
237 | // add series to the chart | |
|
238 | QChartView view(new QChart()); | |
|
239 | view.resize(200, 200); | |
|
240 | view.chart()->addSeries(series); | |
|
241 | view.show(); | |
|
242 | QTest::qWaitForWindowShown(&view); | |
|
243 | ||
|
244 | // first move to right top corner | |
|
245 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); | |
|
246 | ||
|
247 | // move inside slice rectangle but NOT the actual slice | |
|
248 | // pie rectangle: QRectF(60,60 121x121) | |
|
249 | QSignalSpy hoverSpy(s1, SIGNAL(hovered(bool))); | |
|
250 | QTest::mouseMove(view.viewport(), QPoint(170, 70)); | |
|
251 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |
|
252 | QCOMPARE(hoverSpy.count(), 0); | |
|
253 | ||
|
254 | // move inside the slice | |
|
255 | QTest::mouseMove(view.viewport(), QPoint(139, 85)); | |
|
256 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |
|
257 | QCOMPARE(hoverSpy.count(), 1); | |
|
258 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(0)), true); | |
|
259 | ||
|
260 | // move outside the slice | |
|
261 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); | |
|
262 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |
|
263 | QCOMPARE(hoverSpy.count(), 2); | |
|
264 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(0)), false); | |
|
265 | } | |
|
266 | ||
|
129 | 267 | QTEST_MAIN(tst_qpieslice) |
|
130 | 268 | |
|
131 | 269 | #include "tst_qpieslice.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now