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