1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QtTest/QtTest> |
31 | #include <QtCharts/QPercentBarSeries> |
32 | #include <QtCharts/QBarSet> |
33 | #include <QtCharts/QChartView> |
34 | #include <QtCharts/QChart> |
35 | #include "tst_definitions.h" |
36 | |
37 | QT_CHARTS_USE_NAMESPACE |
38 | |
39 | Q_DECLARE_METATYPE(QBarSet*) |
40 | Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition) |
41 | |
42 | class tst_QPercentBarSeries : public QObject |
43 | { |
44 | Q_OBJECT |
45 | |
46 | public slots: |
47 | void initTestCase(); |
48 | void cleanupTestCase(); |
49 | void init(); |
50 | void cleanup(); |
51 | |
52 | private slots: |
53 | void qpercentbarseries_data(); |
54 | void qpercentbarseries(); |
55 | void type_data(); |
56 | void type(); |
57 | void setLabelsFormat(); |
58 | void setLabelsPosition(); |
59 | void setLabelsAngle(); |
60 | void mouseclicked_data(); |
61 | void mouseclicked(); |
62 | void mousehovered_data(); |
63 | void mousehovered(); |
64 | void zeroValuesInSeries(); |
65 | void mousePressed(); |
66 | void mouseReleased(); |
67 | void mouseDoubleClicked(); |
68 | |
69 | private: |
70 | QPercentBarSeries* m_barseries; |
71 | }; |
72 | |
73 | void tst_QPercentBarSeries::initTestCase() |
74 | { |
75 | qRegisterMetaType<QBarSet*>(typeName: "QBarSet*" ); |
76 | qRegisterMetaType<QAbstractBarSeries::LabelsPosition>(typeName: "QAbstractBarSeries::LabelsPosition" ); |
77 | } |
78 | |
79 | void tst_QPercentBarSeries::cleanupTestCase() |
80 | { |
81 | QTest::qWait(ms: 1); // Allow final deleteLaters to run |
82 | } |
83 | |
84 | void tst_QPercentBarSeries::init() |
85 | { |
86 | m_barseries = new QPercentBarSeries(); |
87 | } |
88 | |
89 | void tst_QPercentBarSeries::cleanup() |
90 | { |
91 | delete m_barseries; |
92 | m_barseries = 0; |
93 | } |
94 | |
95 | void tst_QPercentBarSeries::qpercentbarseries_data() |
96 | { |
97 | } |
98 | |
99 | void tst_QPercentBarSeries::qpercentbarseries() |
100 | { |
101 | QPercentBarSeries *barseries = new QPercentBarSeries(); |
102 | QVERIFY(barseries != 0); |
103 | delete barseries; |
104 | } |
105 | |
106 | void tst_QPercentBarSeries::type_data() |
107 | { |
108 | |
109 | } |
110 | |
111 | void tst_QPercentBarSeries::type() |
112 | { |
113 | QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypePercentBar); |
114 | } |
115 | |
116 | void tst_QPercentBarSeries::mouseclicked_data() |
117 | { |
118 | |
119 | } |
120 | |
121 | void tst_QPercentBarSeries::setLabelsFormat() |
122 | { |
123 | QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString))); |
124 | QCOMPARE(m_barseries->labelsFormat(), QString()); |
125 | |
126 | QString format("(@value)" ); |
127 | m_barseries->setLabelsFormat(format); |
128 | TRY_COMPARE(labelsFormatSpy.count(), 1); |
129 | QList<QVariant> arguments = labelsFormatSpy.takeFirst(); |
130 | QVERIFY(arguments.at(0).toString() == format); |
131 | QCOMPARE(m_barseries->labelsFormat(), format); |
132 | |
133 | m_barseries->setLabelsFormat(QString()); |
134 | TRY_COMPARE(labelsFormatSpy.count(), 1); |
135 | arguments = labelsFormatSpy.takeFirst(); |
136 | QVERIFY(arguments.at(0).toString() == QString()); |
137 | QCOMPARE(m_barseries->labelsFormat(), QString()); |
138 | } |
139 | |
140 | void tst_QPercentBarSeries::setLabelsPosition() |
141 | { |
142 | QSignalSpy labelsPositionSpy(m_barseries, |
143 | SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition))); |
144 | QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsCenter); |
145 | |
146 | m_barseries->setLabelsPosition(QPercentBarSeries::LabelsInsideEnd); |
147 | TRY_COMPARE(labelsPositionSpy.count(), 1); |
148 | QList<QVariant> arguments = labelsPositionSpy.takeFirst(); |
149 | QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>() |
150 | == QPercentBarSeries::LabelsInsideEnd); |
151 | QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsInsideEnd); |
152 | |
153 | m_barseries->setLabelsPosition(QPercentBarSeries::LabelsInsideBase); |
154 | TRY_COMPARE(labelsPositionSpy.count(), 1); |
155 | arguments = labelsPositionSpy.takeFirst(); |
156 | QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>() |
157 | == QPercentBarSeries::LabelsInsideBase); |
158 | QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsInsideBase); |
159 | |
160 | m_barseries->setLabelsPosition(QPercentBarSeries::LabelsOutsideEnd); |
161 | TRY_COMPARE(labelsPositionSpy.count(), 1); |
162 | arguments = labelsPositionSpy.takeFirst(); |
163 | QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>() |
164 | == QPercentBarSeries::LabelsOutsideEnd); |
165 | QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsOutsideEnd); |
166 | |
167 | m_barseries->setLabelsPosition(QPercentBarSeries::LabelsCenter); |
168 | TRY_COMPARE(labelsPositionSpy.count(), 1); |
169 | arguments = labelsPositionSpy.takeFirst(); |
170 | QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>() |
171 | == QPercentBarSeries::LabelsCenter); |
172 | QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsCenter); |
173 | } |
174 | |
175 | void tst_QPercentBarSeries::setLabelsAngle() |
176 | { |
177 | QSignalSpy labelsAngleSpy(m_barseries, |
178 | SIGNAL(labelsAngleChanged(qreal))); |
179 | QCOMPARE(m_barseries->labelsAngle(), 0.0); |
180 | |
181 | m_barseries->setLabelsAngle(55.0); |
182 | TRY_COMPARE(labelsAngleSpy.count(), 1); |
183 | QList<QVariant> arguments = labelsAngleSpy.takeFirst(); |
184 | QVERIFY(arguments.at(0).value<qreal>() == 55.0); |
185 | QCOMPARE(m_barseries->labelsAngle(), 55.0); |
186 | } |
187 | |
188 | void tst_QPercentBarSeries::mouseclicked() |
189 | { |
190 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
191 | |
192 | QPercentBarSeries* series = new QPercentBarSeries(); |
193 | |
194 | QBarSet* set1 = new QBarSet(QString("set 1" )); |
195 | *set1 << 10 << 10 << 10; |
196 | series->append(set: set1); |
197 | |
198 | QBarSet* set2 = new QBarSet(QString("set 2" )); |
199 | *set2 << 10 << 10 << 10; |
200 | series->append(set: set2); |
201 | |
202 | QList<QBarSet*> barSets = series->barSets(); |
203 | |
204 | QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*))); |
205 | |
206 | QChartView view(new QChart()); |
207 | view.resize(w: 400,h: 300); |
208 | view.chart()->addSeries(series); |
209 | view.show(); |
210 | QVERIFY(QTest::qWaitForWindowExposed(&view)); |
211 | |
212 | // Calculate expected layout for bars |
213 | QRectF plotArea = view.chart()->plotArea(); |
214 | qreal width = plotArea.width(); |
215 | qreal height = plotArea.height(); |
216 | qreal rangeY = 100; // From 0 to 100 because range is scaled to 100% |
217 | qreal rangeX = 3; // 3 values per set |
218 | qreal scaleY = (height / rangeY); |
219 | qreal scaleX = (width / rangeX); |
220 | |
221 | qreal setCount = series->count(); |
222 | qreal domainMinY = 0; // These come from internal domain used by barseries. |
223 | qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values. |
224 | qreal rectWidth = scaleX * series->barWidth(); |
225 | |
226 | QVector<QRectF> layout; |
227 | |
228 | // 3 = count of values in set |
229 | // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.) |
230 | for (int i = 0; i < 3; i++) { |
231 | qreal colSum = 20; // Sum of values in column (10 + 10 in our test case) |
232 | qreal percentage = (100 / colSum); |
233 | qreal yPos = height + scaleY * domainMinY + plotArea.top(); |
234 | |
235 | for (int set = 0; set < setCount; set++) { |
236 | qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2; |
237 | qreal rectHeigth = barSets.at(i: set)->at(index: i) * percentage * scaleY; |
238 | |
239 | QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth); |
240 | layout.append(t: rect); |
241 | yPos -= rectHeigth; |
242 | } |
243 | } |
244 | |
245 | //==================================================================================== |
246 | // barset 1, bar 0 |
247 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 0).center().toPoint()); |
248 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
249 | |
250 | QCOMPARE(seriesSpy.count(), 1); |
251 | |
252 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
253 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
254 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
255 | QVERIFY(seriesSpyArg.at(0).toInt() == 0); |
256 | |
257 | //==================================================================================== |
258 | // barset 1, bar 1 |
259 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 2).center().toPoint()); |
260 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
261 | |
262 | QCOMPARE(seriesSpy.count(), 1); |
263 | |
264 | seriesSpyArg = seriesSpy.takeFirst(); |
265 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
266 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
267 | QVERIFY(seriesSpyArg.at(0).toInt() == 1); |
268 | |
269 | //==================================================================================== |
270 | // barset 1, bar 2 |
271 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 4).center().toPoint()); |
272 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
273 | |
274 | QCOMPARE(seriesSpy.count(), 1); |
275 | |
276 | seriesSpyArg = seriesSpy.takeFirst(); |
277 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
278 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
279 | QVERIFY(seriesSpyArg.at(0).toInt() == 2); |
280 | |
281 | //==================================================================================== |
282 | // barset 2, bar 0 |
283 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 1).center().toPoint()); |
284 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
285 | |
286 | QCOMPARE(seriesSpy.count(), 1); |
287 | |
288 | seriesSpyArg = seriesSpy.takeFirst(); |
289 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
290 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
291 | QVERIFY(seriesSpyArg.at(0).toInt() == 0); |
292 | |
293 | //==================================================================================== |
294 | // barset 2, bar 1 |
295 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 3).center().toPoint()); |
296 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
297 | |
298 | QCOMPARE(seriesSpy.count(), 1); |
299 | |
300 | seriesSpyArg = seriesSpy.takeFirst(); |
301 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
302 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
303 | QVERIFY(seriesSpyArg.at(0).toInt() == 1); |
304 | |
305 | //==================================================================================== |
306 | // barset 2, bar 2 |
307 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 5).center().toPoint()); |
308 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
309 | |
310 | QCOMPARE(seriesSpy.count(), 1); |
311 | |
312 | seriesSpyArg = seriesSpy.takeFirst(); |
313 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
314 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
315 | QVERIFY(seriesSpyArg.at(0).toInt() == 2); |
316 | } |
317 | |
318 | void tst_QPercentBarSeries::mousehovered_data() |
319 | { |
320 | |
321 | } |
322 | |
323 | void tst_QPercentBarSeries::mousehovered() |
324 | { |
325 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
326 | SKIP_IF_FLAKY_MOUSE_MOVE(); |
327 | |
328 | QPercentBarSeries* series = new QPercentBarSeries(); |
329 | |
330 | QBarSet* set1 = new QBarSet(QString("set 1" )); |
331 | *set1 << 10 << 10 << 10; |
332 | series->append(set: set1); |
333 | |
334 | QBarSet* set2 = new QBarSet(QString("set 2" )); |
335 | *set2 << 10 << 10 << 10; |
336 | series->append(set: set2); |
337 | |
338 | QList<QBarSet*> barSets = series->barSets(); |
339 | |
340 | QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*))); |
341 | QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int))); |
342 | QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int))); |
343 | |
344 | QChartView view(new QChart()); |
345 | view.resize(w: 400,h: 300); |
346 | view.chart()->addSeries(series); |
347 | view.show(); |
348 | QVERIFY(QTest::qWaitForWindowExposed(&view)); |
349 | |
350 | //this is hack since view does not get events otherwise |
351 | view.setMouseTracking(true); |
352 | |
353 | // Calculate expected layout for bars |
354 | QRectF plotArea = view.chart()->plotArea(); |
355 | qreal width = plotArea.width(); |
356 | qreal height = plotArea.height(); |
357 | qreal rangeY = 100; // From 0 to 100 because range is scaled to 100% |
358 | qreal rangeX = 3; // 3 values per set |
359 | qreal scaleY = (height / rangeY); |
360 | qreal scaleX = (width / rangeX); |
361 | |
362 | qreal setCount = series->count(); |
363 | qreal domainMinY = 0; // These come from internal domain used by barseries. |
364 | qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values. |
365 | qreal rectWidth = scaleX * series->barWidth(); |
366 | |
367 | QVector<QRectF> layout; |
368 | |
369 | // 3 = count of values in set |
370 | // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.) |
371 | for (int i = 0; i < 3; i++) { |
372 | qreal colSum = 20; // Sum of values in column (10 + 10 in our test case) |
373 | qreal percentage = (100 / colSum); |
374 | qreal yPos = height + scaleY * domainMinY + plotArea.top(); |
375 | |
376 | for (int set = 0; set < setCount; set++) { |
377 | qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2; |
378 | qreal rectHeight = barSets.at(i: set)->at(index: i) * percentage * scaleY; |
379 | |
380 | QRectF rect(xPos, yPos-rectHeight, rectWidth, rectHeight); |
381 | layout.append(t: rect); |
382 | yPos -= rectHeight; |
383 | } |
384 | } |
385 | |
386 | //======================================================================= |
387 | // move mouse to left border |
388 | QTest::mouseMove(widget: view.viewport(), pos: QPoint(0, layout.at(i: 0).center().y())); |
389 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 10000); |
390 | TRY_COMPARE(seriesIndexSpy.count(), 0); |
391 | |
392 | //======================================================================= |
393 | // move mouse on top of set1 |
394 | QTest::mouseMove(widget: view.viewport(), pos: layout.at(i: 0).center().toPoint()); |
395 | TRY_COMPARE(seriesIndexSpy.count(), 1); |
396 | TRY_COMPARE(setIndexSpy1.count(), 1); |
397 | TRY_COMPARE(setIndexSpy2.count(), 0); |
398 | |
399 | QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst(); |
400 | QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1); |
401 | QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool); |
402 | QVERIFY(seriesIndexSpyArg.at(0).toBool() == true); |
403 | |
404 | QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst(); |
405 | QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool); |
406 | QVERIFY(setIndexSpyArg.at(0).toBool() == true); |
407 | |
408 | //======================================================================= |
409 | // move mouse from top of set1 to top of set2 |
410 | QTest::mouseMove(widget: view.viewport(), pos: layout.at(i: 1).center().toPoint()); |
411 | TRY_COMPARE(seriesIndexSpy.count(), 2); |
412 | TRY_COMPARE(setIndexSpy1.count(), 1); |
413 | TRY_COMPARE(setIndexSpy2.count(), 1); |
414 | |
415 | // should leave set1 |
416 | seriesIndexSpyArg = seriesIndexSpy.takeFirst(); |
417 | QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1); |
418 | QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool); |
419 | QVERIFY(seriesIndexSpyArg.at(0).toBool() == false); |
420 | |
421 | setIndexSpyArg = setIndexSpy1.takeFirst(); |
422 | QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool); |
423 | QVERIFY(setIndexSpyArg.at(0).toBool() == false); |
424 | |
425 | // should enter set2 |
426 | seriesIndexSpyArg = seriesIndexSpy.takeFirst(); |
427 | QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2); |
428 | QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool); |
429 | QVERIFY(seriesIndexSpyArg.at(0).toBool() == true); |
430 | |
431 | setIndexSpyArg = setIndexSpy2.takeFirst(); |
432 | QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool); |
433 | QVERIFY(setIndexSpyArg.at(0).toBool() == true); |
434 | |
435 | //======================================================================= |
436 | // move mouse from top of set2 to background |
437 | QTest::mouseMove(widget: view.viewport(), pos: QPoint(layout.at(i: 1).center().x(), 0)); |
438 | TRY_COMPARE(seriesIndexSpy.count(), 1); |
439 | TRY_COMPARE(setIndexSpy1.count(), 0); |
440 | TRY_COMPARE(setIndexSpy2.count(), 1); |
441 | |
442 | // should leave set2 |
443 | seriesIndexSpyArg = seriesIndexSpy.takeFirst(); |
444 | QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2); |
445 | QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool); |
446 | QVERIFY(seriesIndexSpyArg.at(0).toBool() == false); |
447 | |
448 | setIndexSpyArg = setIndexSpy2.takeFirst(); |
449 | QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool); |
450 | QVERIFY(setIndexSpyArg.at(0).toBool() == false); |
451 | |
452 | //======================================================================= |
453 | // move mouse on top of set1, bar0 to check the index (hover into set1) |
454 | QTest::mouseMove(widget: view.viewport(), pos: layout.at(i: 0).center().toPoint()); |
455 | |
456 | TRY_COMPARE(seriesIndexSpy.count(), 1); |
457 | TRY_COMPARE(setIndexSpy1.count(), 1); |
458 | TRY_COMPARE(setIndexSpy2.count(), 0); |
459 | |
460 | //should enter set1, bar0 |
461 | seriesIndexSpyArg = seriesIndexSpy.takeFirst(); |
462 | QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1); |
463 | QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool); |
464 | QVERIFY(seriesIndexSpyArg.at(0).toBool() == true); |
465 | QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int); |
466 | QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0); |
467 | |
468 | setIndexSpyArg = setIndexSpy1.takeFirst(); |
469 | QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool); |
470 | QVERIFY(setIndexSpyArg.at(0).toBool() == true); |
471 | QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int); |
472 | QVERIFY(setIndexSpyArg.at(1).toInt() == 0); |
473 | |
474 | //======================================================================= |
475 | // move mouse on top of set2, bar0 to check the index (hover out set1, |
476 | // hover in set2) |
477 | QTest::mouseMove(widget: view.viewport(), pos: layout.at(i: 1).center().toPoint()); |
478 | |
479 | TRY_COMPARE(seriesIndexSpy.count(), 2); |
480 | TRY_COMPARE(setIndexSpy1.count(), 1); |
481 | TRY_COMPARE(setIndexSpy2.count(), 1); |
482 | |
483 | //should leave set1, bar0 |
484 | seriesIndexSpyArg = seriesIndexSpy.takeFirst(); |
485 | QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1); |
486 | QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool); |
487 | QVERIFY(seriesIndexSpyArg.at(0).toBool() == false); |
488 | QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int); |
489 | QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0); |
490 | |
491 | setIndexSpyArg = setIndexSpy1.takeFirst(); |
492 | QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool); |
493 | QVERIFY(setIndexSpyArg.at(0).toBool() == false); |
494 | QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int); |
495 | QVERIFY(setIndexSpyArg.at(1).toInt() == 0); |
496 | |
497 | //should enter set2, bar0 |
498 | seriesIndexSpyArg = seriesIndexSpy.takeFirst(); |
499 | QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2); |
500 | QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool); |
501 | QVERIFY(seriesIndexSpyArg.at(0).toBool() == true); |
502 | QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int); |
503 | QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0); |
504 | |
505 | setIndexSpyArg = setIndexSpy2.takeFirst(); |
506 | QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool); |
507 | QVERIFY(setIndexSpyArg.at(0).toBool() == true); |
508 | QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int); |
509 | QVERIFY(setIndexSpyArg.at(1).toInt() == 0); |
510 | |
511 | //======================================================================= |
512 | // move mouse on top of set1, bar1 to check the index (hover out set2, |
513 | // hover in set1) |
514 | QTest::mouseMove(widget: view.viewport(), pos: layout.at(i: 2).center().toPoint()); |
515 | |
516 | TRY_COMPARE(seriesIndexSpy.count(), 2); |
517 | TRY_COMPARE(setIndexSpy1.count(), 1); |
518 | TRY_COMPARE(setIndexSpy2.count(), 1); |
519 | |
520 | //should leave set2, bar0 |
521 | seriesIndexSpyArg = seriesIndexSpy.takeFirst(); |
522 | QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2); |
523 | QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool); |
524 | QVERIFY(seriesIndexSpyArg.at(0).toBool() == false); |
525 | QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int); |
526 | QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0); |
527 | |
528 | setIndexSpyArg = setIndexSpy2.takeFirst(); |
529 | QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool); |
530 | QVERIFY(setIndexSpyArg.at(0).toBool() == false); |
531 | QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int); |
532 | QVERIFY(setIndexSpyArg.at(1).toInt() == 0); |
533 | |
534 | //should enter set1, bar1 |
535 | seriesIndexSpyArg = seriesIndexSpy.takeFirst(); |
536 | QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1); |
537 | QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool); |
538 | QVERIFY(seriesIndexSpyArg.at(0).toBool() == true); |
539 | QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int); |
540 | QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1); |
541 | |
542 | setIndexSpyArg = setIndexSpy1.takeFirst(); |
543 | QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool); |
544 | QVERIFY(setIndexSpyArg.at(0).toBool() == true); |
545 | QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int); |
546 | QVERIFY(setIndexSpyArg.at(1).toInt() == 1); |
547 | |
548 | //======================================================================= |
549 | // move mouse between set1 and set2 (hover out set1) |
550 | QTest::mouseMove(widget: view.viewport(), pos: QPoint((layout.at(i: 3).right() + layout.at(i: 4).left()) /2, |
551 | layout.at(i: 2).top())); |
552 | |
553 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
554 | TRY_COMPARE(seriesIndexSpy.count(), 1); |
555 | TRY_COMPARE(setIndexSpy1.count(), 1); |
556 | TRY_COMPARE(setIndexSpy2.count(), 0); |
557 | |
558 | //should leave set1, bar1 |
559 | seriesIndexSpyArg = seriesIndexSpy.takeFirst(); |
560 | QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1); |
561 | QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool); |
562 | QVERIFY(seriesIndexSpyArg.at(0).toBool() == false); |
563 | QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int); |
564 | QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1); |
565 | |
566 | setIndexSpyArg = setIndexSpy1.takeFirst(); |
567 | QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool); |
568 | QVERIFY(setIndexSpyArg.at(0).toBool() == false); |
569 | QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int); |
570 | QVERIFY(setIndexSpyArg.at(1).toInt() == 1); |
571 | |
572 | //======================================================================= |
573 | // move mouse on top of set2, bar1 to check the index (hover in set2) |
574 | QTest::mouseMove(widget: view.viewport(), pos: layout.at(i: 3).center().toPoint()); |
575 | |
576 | TRY_COMPARE(seriesIndexSpy.count(), 1); |
577 | TRY_COMPARE(setIndexSpy1.count(), 0); |
578 | TRY_COMPARE(setIndexSpy2.count(), 1); |
579 | |
580 | //should enter set2, bar1 |
581 | seriesIndexSpyArg = seriesIndexSpy.takeFirst(); |
582 | QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2); |
583 | QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool); |
584 | QVERIFY(seriesIndexSpyArg.at(0).toBool() == true); |
585 | QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int); |
586 | QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1); |
587 | |
588 | setIndexSpyArg = setIndexSpy2.takeFirst(); |
589 | QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool); |
590 | QVERIFY(setIndexSpyArg.at(0).toBool() == true); |
591 | QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int); |
592 | QVERIFY(setIndexSpyArg.at(1).toInt() == 1); |
593 | |
594 | //======================================================================= |
595 | // move mouse between set1 and set2 (hover out set2) |
596 | QTest::mouseMove(widget: view.viewport(), pos: QPoint((layout.at(i: 3).right() + layout.at(i: 4).left()) /2, |
597 | layout.at(i: 2).top())); |
598 | |
599 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
600 | TRY_COMPARE(seriesIndexSpy.count(), 1); |
601 | TRY_COMPARE(setIndexSpy1.count(), 0); |
602 | TRY_COMPARE(setIndexSpy2.count(), 1); |
603 | |
604 | //should leave set1, bar1 |
605 | seriesIndexSpyArg = seriesIndexSpy.takeFirst(); |
606 | QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2); |
607 | QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool); |
608 | QVERIFY(seriesIndexSpyArg.at(0).toBool() == false); |
609 | QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int); |
610 | QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1); |
611 | |
612 | setIndexSpyArg = setIndexSpy2.takeFirst(); |
613 | QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool); |
614 | QVERIFY(setIndexSpyArg.at(0).toBool() == false); |
615 | QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int); |
616 | QVERIFY(setIndexSpyArg.at(1).toInt() == 1); |
617 | } |
618 | |
619 | void tst_QPercentBarSeries::zeroValuesInSeries() |
620 | { |
621 | QPercentBarSeries *series = new QPercentBarSeries(); |
622 | QBarSet *set1 = new QBarSet(QString("set 1" )); |
623 | *set1 << 100 << 0.0 << 10; |
624 | series->append(set: set1); |
625 | |
626 | QBarSet *set2 = new QBarSet(QString("set 2" )); |
627 | *set2 << 0.0 << 0.0 << 70; |
628 | series->append(set: set2); |
629 | |
630 | QChartView view(new QChart()); |
631 | view.resize(w: 400, h: 300); |
632 | view.chart()->addSeries(series); |
633 | view.chart()->createDefaultAxes(); |
634 | view.show(); |
635 | |
636 | QVERIFY(QTest::qWaitForWindowExposed(&view)); |
637 | } |
638 | |
639 | void tst_QPercentBarSeries::mousePressed() |
640 | { |
641 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
642 | |
643 | QPercentBarSeries* series = new QPercentBarSeries(); |
644 | |
645 | QBarSet* set1 = new QBarSet(QString("set 1" )); |
646 | *set1 << 10 << 10 << 10; |
647 | series->append(set: set1); |
648 | |
649 | QBarSet* set2 = new QBarSet(QString("set 2" )); |
650 | *set2 << 10 << 10 << 10; |
651 | series->append(set: set2); |
652 | QList<QBarSet*> barSets = series->barSets(); |
653 | |
654 | QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*))); |
655 | QSignalSpy setSpy1(set1, SIGNAL(pressed(int))); |
656 | QSignalSpy setSpy2(set2, SIGNAL(pressed(int))); |
657 | |
658 | QChartView view(new QChart()); |
659 | view.resize(w: 400,h: 300); |
660 | view.chart()->addSeries(series); |
661 | view.show(); |
662 | QVERIFY(QTest::qWaitForWindowExposed(&view)); |
663 | |
664 | // Calculate expected layout for bars |
665 | QRectF plotArea = view.chart()->plotArea(); |
666 | qreal width = plotArea.width(); |
667 | qreal height = plotArea.height(); |
668 | qreal rangeY = 100; // From 0 to 100 because range is scaled to 100% |
669 | qreal rangeX = 3; // 3 values per set |
670 | qreal scaleY = (height / rangeY); |
671 | qreal scaleX = (width / rangeX); |
672 | |
673 | qreal setCount = series->count(); |
674 | qreal domainMinY = 0; // These come from internal domain used by barseries. |
675 | qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values. |
676 | qreal rectWidth = scaleX * series->barWidth(); |
677 | |
678 | QVector<QRectF> layout; |
679 | |
680 | // 3 = count of values in set |
681 | // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.) |
682 | for (int i = 0; i < 3; i++) { |
683 | qreal colSum = 20; // Sum of values in column (10 + 10 in our test case) |
684 | qreal percentage = (100 / colSum); |
685 | qreal yPos = height + scaleY * domainMinY + plotArea.top(); |
686 | |
687 | for (int set = 0; set < setCount; set++) { |
688 | qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2; |
689 | qreal rectHeigth = barSets.at(i: set)->at(index: i) * percentage * scaleY; |
690 | |
691 | QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth); |
692 | layout.append(t: rect); |
693 | yPos -= rectHeigth; |
694 | } |
695 | } |
696 | |
697 | //==================================================================================== |
698 | // barset 1, bar 0 |
699 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 0).center().toPoint()); |
700 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
701 | |
702 | QCOMPARE(seriesSpy.count(), 1); |
703 | QCOMPARE(setSpy1.count(), 1); |
704 | QCOMPARE(setSpy2.count(), 0); |
705 | |
706 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
707 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
708 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
709 | QVERIFY(seriesSpyArg.at(0).toInt() == 0); |
710 | |
711 | QList<QVariant> setSpyArg = setSpy1.takeFirst(); |
712 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
713 | QVERIFY(setSpyArg.at(0).toInt() == 0); |
714 | |
715 | //==================================================================================== |
716 | // barset 1, bar 1 |
717 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 2).center().toPoint()); |
718 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
719 | |
720 | QCOMPARE(seriesSpy.count(), 1); |
721 | QCOMPARE(setSpy1.count(), 1); |
722 | QCOMPARE(setSpy2.count(), 0); |
723 | |
724 | seriesSpyArg = seriesSpy.takeFirst(); |
725 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
726 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
727 | QVERIFY(seriesSpyArg.at(0).toInt() == 1); |
728 | |
729 | setSpyArg = setSpy1.takeFirst(); |
730 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
731 | QVERIFY(setSpyArg.at(0).toInt() == 1); |
732 | |
733 | //==================================================================================== |
734 | // barset 1, bar 2 |
735 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 4).center().toPoint()); |
736 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
737 | |
738 | QCOMPARE(seriesSpy.count(), 1); |
739 | QCOMPARE(setSpy1.count(), 1); |
740 | QCOMPARE(setSpy2.count(), 0); |
741 | |
742 | seriesSpyArg = seriesSpy.takeFirst(); |
743 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
744 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
745 | QVERIFY(seriesSpyArg.at(0).toInt() == 2); |
746 | |
747 | setSpyArg = setSpy1.takeFirst(); |
748 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
749 | QVERIFY(setSpyArg.at(0).toInt() == 2); |
750 | |
751 | //==================================================================================== |
752 | // barset 2, bar 0 |
753 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 1).center().toPoint()); |
754 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
755 | |
756 | QCOMPARE(seriesSpy.count(), 1); |
757 | QCOMPARE(setSpy1.count(), 0); |
758 | QCOMPARE(setSpy2.count(), 1); |
759 | |
760 | seriesSpyArg = seriesSpy.takeFirst(); |
761 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
762 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
763 | QVERIFY(seriesSpyArg.at(0).toInt() == 0); |
764 | |
765 | setSpyArg = setSpy2.takeFirst(); |
766 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
767 | QVERIFY(setSpyArg.at(0).toInt() == 0); |
768 | |
769 | //==================================================================================== |
770 | // barset 2, bar 1 |
771 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 3).center().toPoint()); |
772 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
773 | |
774 | QCOMPARE(seriesSpy.count(), 1); |
775 | QCOMPARE(setSpy1.count(), 0); |
776 | QCOMPARE(setSpy2.count(), 1); |
777 | |
778 | seriesSpyArg = seriesSpy.takeFirst(); |
779 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
780 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
781 | QVERIFY(seriesSpyArg.at(0).toInt() == 1); |
782 | |
783 | setSpyArg = setSpy2.takeFirst(); |
784 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
785 | QVERIFY(setSpyArg.at(0).toInt() == 1); |
786 | |
787 | //==================================================================================== |
788 | // barset 2, bar 2 |
789 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 5).center().toPoint()); |
790 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
791 | |
792 | QCOMPARE(seriesSpy.count(), 1); |
793 | QCOMPARE(setSpy1.count(), 0); |
794 | QCOMPARE(setSpy2.count(), 1); |
795 | |
796 | seriesSpyArg = seriesSpy.takeFirst(); |
797 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
798 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
799 | QVERIFY(seriesSpyArg.at(0).toInt() == 2); |
800 | |
801 | setSpyArg = setSpy2.takeFirst(); |
802 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
803 | QVERIFY(setSpyArg.at(0).toInt() == 2); |
804 | } |
805 | |
806 | void tst_QPercentBarSeries::mouseReleased() |
807 | { |
808 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
809 | |
810 | QPercentBarSeries* series = new QPercentBarSeries(); |
811 | |
812 | QBarSet* set1 = new QBarSet(QString("set 1" )); |
813 | *set1 << 10 << 10 << 10; |
814 | series->append(set: set1); |
815 | |
816 | QBarSet* set2 = new QBarSet(QString("set 2" )); |
817 | *set2 << 10 << 10 << 10; |
818 | series->append(set: set2); |
819 | QList<QBarSet*> barSets = series->barSets(); |
820 | |
821 | QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*))); |
822 | QSignalSpy setSpy1(set1, SIGNAL(released(int))); |
823 | QSignalSpy setSpy2(set2, SIGNAL(released(int))); |
824 | |
825 | QChartView view(new QChart()); |
826 | view.resize(w: 400,h: 300); |
827 | view.chart()->addSeries(series); |
828 | view.show(); |
829 | QVERIFY(QTest::qWaitForWindowExposed(&view)); |
830 | |
831 | // Calculate expected layout for bars |
832 | QRectF plotArea = view.chart()->plotArea(); |
833 | qreal width = plotArea.width(); |
834 | qreal height = plotArea.height(); |
835 | qreal rangeY = 100; // From 0 to 100 because range is scaled to 100% |
836 | qreal rangeX = 3; // 3 values per set |
837 | qreal scaleY = (height / rangeY); |
838 | qreal scaleX = (width / rangeX); |
839 | |
840 | qreal setCount = series->count(); |
841 | qreal domainMinY = 0; // These come from internal domain used by barseries. |
842 | qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values. |
843 | qreal rectWidth = scaleX * series->barWidth(); |
844 | |
845 | QVector<QRectF> layout; |
846 | |
847 | // 3 = count of values in set |
848 | // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.) |
849 | for (int i = 0; i < 3; i++) { |
850 | qreal colSum = 20; // Sum of values in column (10 + 10 in our test case) |
851 | qreal percentage = (100 / colSum); |
852 | qreal yPos = height + scaleY * domainMinY + plotArea.top(); |
853 | |
854 | for (int set = 0; set < setCount; set++) { |
855 | qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2; |
856 | qreal rectHeigth = barSets.at(i: set)->at(index: i) * percentage * scaleY; |
857 | |
858 | QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth); |
859 | layout.append(t: rect); |
860 | yPos -= rectHeigth; |
861 | } |
862 | } |
863 | |
864 | //==================================================================================== |
865 | // barset 1, bar 0 |
866 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 0).center().toPoint()); |
867 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
868 | |
869 | QCOMPARE(seriesSpy.count(), 1); |
870 | QCOMPARE(setSpy1.count(), 1); |
871 | QCOMPARE(setSpy2.count(), 0); |
872 | |
873 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
874 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
875 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
876 | QVERIFY(seriesSpyArg.at(0).toInt() == 0); |
877 | |
878 | QList<QVariant> setSpyArg = setSpy1.takeFirst(); |
879 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
880 | QVERIFY(setSpyArg.at(0).toInt() == 0); |
881 | |
882 | //==================================================================================== |
883 | // barset 1, bar 1 |
884 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 2).center().toPoint()); |
885 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
886 | |
887 | QCOMPARE(seriesSpy.count(), 1); |
888 | QCOMPARE(setSpy1.count(), 1); |
889 | QCOMPARE(setSpy2.count(), 0); |
890 | |
891 | seriesSpyArg = seriesSpy.takeFirst(); |
892 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
893 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
894 | QVERIFY(seriesSpyArg.at(0).toInt() == 1); |
895 | |
896 | setSpyArg = setSpy1.takeFirst(); |
897 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
898 | QVERIFY(setSpyArg.at(0).toInt() == 1); |
899 | |
900 | //==================================================================================== |
901 | // barset 1, bar 2 |
902 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 4).center().toPoint()); |
903 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
904 | |
905 | QCOMPARE(seriesSpy.count(), 1); |
906 | QCOMPARE(setSpy1.count(), 1); |
907 | QCOMPARE(setSpy2.count(), 0); |
908 | |
909 | seriesSpyArg = seriesSpy.takeFirst(); |
910 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
911 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
912 | QVERIFY(seriesSpyArg.at(0).toInt() == 2); |
913 | |
914 | setSpyArg = setSpy1.takeFirst(); |
915 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
916 | QVERIFY(setSpyArg.at(0).toInt() == 2); |
917 | |
918 | //==================================================================================== |
919 | // barset 2, bar 0 |
920 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 1).center().toPoint()); |
921 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
922 | |
923 | QCOMPARE(seriesSpy.count(), 1); |
924 | QCOMPARE(setSpy1.count(), 0); |
925 | QCOMPARE(setSpy2.count(), 1); |
926 | |
927 | seriesSpyArg = seriesSpy.takeFirst(); |
928 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
929 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
930 | QVERIFY(seriesSpyArg.at(0).toInt() == 0); |
931 | |
932 | setSpyArg = setSpy2.takeFirst(); |
933 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
934 | QVERIFY(setSpyArg.at(0).toInt() == 0); |
935 | |
936 | //==================================================================================== |
937 | // barset 2, bar 1 |
938 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 3).center().toPoint()); |
939 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
940 | |
941 | QCOMPARE(seriesSpy.count(), 1); |
942 | QCOMPARE(setSpy1.count(), 0); |
943 | QCOMPARE(setSpy2.count(), 1); |
944 | |
945 | seriesSpyArg = seriesSpy.takeFirst(); |
946 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
947 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
948 | QVERIFY(seriesSpyArg.at(0).toInt() == 1); |
949 | |
950 | setSpyArg = setSpy2.takeFirst(); |
951 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
952 | QVERIFY(setSpyArg.at(0).toInt() == 1); |
953 | |
954 | //==================================================================================== |
955 | // barset 2, bar 2 |
956 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 5).center().toPoint()); |
957 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
958 | |
959 | QCOMPARE(seriesSpy.count(), 1); |
960 | QCOMPARE(setSpy1.count(), 0); |
961 | QCOMPARE(setSpy2.count(), 1); |
962 | |
963 | seriesSpyArg = seriesSpy.takeFirst(); |
964 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
965 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
966 | QVERIFY(seriesSpyArg.at(0).toInt() == 2); |
967 | |
968 | setSpyArg = setSpy2.takeFirst(); |
969 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
970 | QVERIFY(setSpyArg.at(0).toInt() == 2); |
971 | } |
972 | |
973 | void tst_QPercentBarSeries::mouseDoubleClicked() |
974 | { |
975 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
976 | |
977 | QPercentBarSeries* series = new QPercentBarSeries(); |
978 | |
979 | QBarSet* set1 = new QBarSet(QString("set 1" )); |
980 | *set1 << 10 << 10 << 10; |
981 | series->append(set: set1); |
982 | |
983 | QBarSet* set2 = new QBarSet(QString("set 2" )); |
984 | *set2 << 10 << 10 << 10; |
985 | series->append(set: set2); |
986 | QList<QBarSet*> barSets = series->barSets(); |
987 | |
988 | QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*))); |
989 | QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int))); |
990 | QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int))); |
991 | |
992 | QChartView view(new QChart()); |
993 | view.resize(w: 400,h: 300); |
994 | view.chart()->addSeries(series); |
995 | view.show(); |
996 | QVERIFY(QTest::qWaitForWindowExposed(&view)); |
997 | |
998 | // Calculate expected layout for bars |
999 | QRectF plotArea = view.chart()->plotArea(); |
1000 | qreal width = plotArea.width(); |
1001 | qreal height = plotArea.height(); |
1002 | qreal rangeY = 100; // From 0 to 100 because range is scaled to 100% |
1003 | qreal rangeX = 3; // 3 values per set |
1004 | qreal scaleY = (height / rangeY); |
1005 | qreal scaleX = (width / rangeX); |
1006 | |
1007 | qreal setCount = series->count(); |
1008 | qreal domainMinY = 0; // These come from internal domain used by barseries. |
1009 | qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values. |
1010 | qreal rectWidth = scaleX * series->barWidth(); |
1011 | |
1012 | QVector<QRectF> layout; |
1013 | |
1014 | // 3 = count of values in set |
1015 | // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.) |
1016 | for (int i = 0; i < 3; i++) { |
1017 | qreal colSum = 20; // Sum of values in column (10 + 10 in our test case) |
1018 | qreal percentage = (100 / colSum); |
1019 | qreal yPos = height + scaleY * domainMinY + plotArea.top(); |
1020 | |
1021 | for (int set = 0; set < setCount; set++) { |
1022 | qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2; |
1023 | qreal rectHeigth = barSets.at(i: set)->at(index: i) * percentage * scaleY; |
1024 | |
1025 | QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth); |
1026 | layout.append(t: rect); |
1027 | yPos -= rectHeigth; |
1028 | } |
1029 | } |
1030 | |
1031 | // barset 1, bar 0 |
1032 | QTest::mouseDClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 0).center().toPoint()); |
1033 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
1034 | |
1035 | QCOMPARE(seriesSpy.count(), 1); |
1036 | QCOMPARE(setSpy1.count(), 1); |
1037 | QCOMPARE(setSpy2.count(), 0); |
1038 | |
1039 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
1040 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
1041 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
1042 | QVERIFY(seriesSpyArg.at(0).toInt() == 0); |
1043 | |
1044 | QList<QVariant> setSpyArg = setSpy1.takeFirst(); |
1045 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
1046 | QVERIFY(setSpyArg.at(0).toInt() == 0); |
1047 | } |
1048 | |
1049 | QTEST_MAIN(tst_QPercentBarSeries) |
1050 | |
1051 | #include "tst_qpercentbarseries.moc" |
1052 | |
1053 | |