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/QHorizontalStackedBarSeries>
32#include <QtCharts/QBarSet>
33#include <QtCharts/QChartView>
34#include <QtCharts/QChart>
35#include "tst_definitions.h"
36
37QT_CHARTS_USE_NAMESPACE
38
39Q_DECLARE_METATYPE(QBarSet*)
40Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
41
42class tst_QHorizontalStackedBarSeries : public QObject
43{
44 Q_OBJECT
45
46public slots:
47 void initTestCase();
48 void cleanupTestCase();
49 void init();
50 void cleanup();
51
52private slots:
53 void qhorizontalstackedbarseries_data();
54 void qhorizontalstackedbarseries();
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
68private:
69 QHorizontalStackedBarSeries* m_barseries;
70};
71
72void tst_QHorizontalStackedBarSeries::initTestCase()
73{
74 qRegisterMetaType<QBarSet*>(typeName: "QBarSet*");
75 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>(typeName: "QAbstractBarSeries::LabelsPosition");
76}
77
78void tst_QHorizontalStackedBarSeries::cleanupTestCase()
79{
80 QTest::qWait(ms: 1); // Allow final deleteLaters to run
81}
82
83void tst_QHorizontalStackedBarSeries::init()
84{
85 m_barseries = new QHorizontalStackedBarSeries();
86}
87
88void tst_QHorizontalStackedBarSeries::cleanup()
89{
90 delete m_barseries;
91 m_barseries = 0;
92}
93
94void tst_QHorizontalStackedBarSeries::qhorizontalstackedbarseries_data()
95{
96}
97
98void tst_QHorizontalStackedBarSeries::qhorizontalstackedbarseries()
99{
100 QHorizontalStackedBarSeries *barseries = new QHorizontalStackedBarSeries();
101 QVERIFY(barseries != 0);
102 delete barseries;
103}
104
105void tst_QHorizontalStackedBarSeries::type_data()
106{
107
108}
109
110void tst_QHorizontalStackedBarSeries::type()
111{
112 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeHorizontalStackedBar);
113}
114
115void tst_QHorizontalStackedBarSeries::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
134void tst_QHorizontalStackedBarSeries::setLabelsPosition()
135{
136 QSignalSpy labelsPositionSpy(m_barseries,
137 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
138 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsCenter);
139
140 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsInsideEnd);
141 TRY_COMPARE(labelsPositionSpy.count(), 1);
142 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
143 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
144 == QHorizontalStackedBarSeries::LabelsInsideEnd);
145 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsInsideEnd);
146
147 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsInsideBase);
148 TRY_COMPARE(labelsPositionSpy.count(), 1);
149 arguments = labelsPositionSpy.takeFirst();
150 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
151 == QHorizontalStackedBarSeries::LabelsInsideBase);
152 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsInsideBase);
153
154 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsOutsideEnd);
155 TRY_COMPARE(labelsPositionSpy.count(), 1);
156 arguments = labelsPositionSpy.takeFirst();
157 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
158 == QHorizontalStackedBarSeries::LabelsOutsideEnd);
159 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsOutsideEnd);
160
161 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsCenter);
162 TRY_COMPARE(labelsPositionSpy.count(), 1);
163 arguments = labelsPositionSpy.takeFirst();
164 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
165 == QHorizontalStackedBarSeries::LabelsCenter);
166 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsCenter);
167}
168
169void tst_QHorizontalStackedBarSeries::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
182void tst_QHorizontalStackedBarSeries::mouseclicked_data()
183{
184
185}
186
187void tst_QHorizontalStackedBarSeries::mouseclicked()
188{
189 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
190
191 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
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 = 3; // 3 values per set
216 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
217 qreal scaleY = (height / rangeY);
218 qreal scaleX = (width / rangeX);
219
220 qreal setCount = series->count();
221 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
222 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
223 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
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 xMax = -scaleX * domainMinX + plotArea.left();
231 qreal xMin = -scaleX * domainMinX + plotArea.left();
232 for (int set = 0; set < setCount; set++) {
233 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
234 qreal rectWidth = barSets.at(i: set)->at(index: i) * scaleX;
235 if (rectWidth > 0) {
236 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
237 layout.append(t: rect);
238 xMax += rectWidth;
239 } else {
240 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
241 layout.append(t: rect);
242 xMin += rectWidth;
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
320void tst_QHorizontalStackedBarSeries::mousehovered_data()
321{
322
323}
324
325void tst_QHorizontalStackedBarSeries::mousehovered()
326{
327 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
328 SKIP_IF_FLAKY_MOUSE_MOVE();
329
330 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
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 = 3; // 3 values per set
360 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
361 qreal scaleY = (height / rangeY);
362 qreal scaleX = (width / rangeX);
363
364 qreal setCount = series->count();
365 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
366 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
367 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
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 xMax = -scaleX * domainMinX + plotArea.left();
375 qreal xMin = -scaleX * domainMinX + plotArea.left();
376 for (int set = 0; set < setCount; set++) {
377 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
378 qreal rectWidth = barSets.at(i: set)->at(index: i) * scaleX;
379 if (rectWidth > 0) {
380 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
381 layout.append(t: rect);
382 xMax += rectWidth;
383 } else {
384 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
385 layout.append(t: rect);
386 xMin += rectWidth;
387 }
388 }
389 }
390
391//=======================================================================
392// move mouse to left border
393 QTest::mouseMove(widget: view.viewport(), pos: QPoint(0, layout.at(i: 4).center().y()));
394 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 10000);
395 TRY_COMPARE(seriesIndexSpy.count(), 0);
396
397//=======================================================================
398// move mouse on top of set1
399 QTest::mouseMove(widget: view.viewport(), pos: layout.at(i: 4).center().toPoint());
400 TRY_COMPARE(seriesIndexSpy.count(), 1);
401 TRY_COMPARE(setIndexSpy1.count(), 1);
402 TRY_COMPARE(setIndexSpy2.count(), 0);
403
404 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
405 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
406 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
407 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
408
409 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
410 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
411 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
412
413//=======================================================================
414// move mouse from top of set1 to top of set2
415 QTest::mouseMove(widget: view.viewport(), pos: layout.at(i: 5).center().toPoint());
416 TRY_COMPARE(seriesIndexSpy.count(), 2);
417 TRY_COMPARE(setIndexSpy1.count(), 1);
418 TRY_COMPARE(setIndexSpy2.count(), 1);
419
420 // should leave set1
421 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
422 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
423 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
424 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
425
426 setIndexSpyArg = setIndexSpy1.takeFirst();
427 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
428 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
429
430 // should enter set2
431 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
432 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
433 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
434 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
435
436 setIndexSpyArg = setIndexSpy2.takeFirst();
437 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
438 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
439
440//=======================================================================
441// move mouse from top of set2 to background
442 QTest::mouseMove(widget: view.viewport(), pos: QPoint(layout.at(i: 5).center().y(), 0));
443 TRY_COMPARE(seriesIndexSpy.count(), 1);
444 TRY_COMPARE(setIndexSpy1.count(), 0);
445 TRY_COMPARE(setIndexSpy2.count(), 1);
446
447 // should leave set2
448 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
449 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
450 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
451 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
452
453 setIndexSpyArg = setIndexSpy2.takeFirst();
454 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
455 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
456
457//=======================================================================
458// move mouse on top of set1, bar0 to check the index (hover into set1)
459 QTest::mouseMove(widget: view.viewport(), pos: layout.at(i: 0).center().toPoint());
460
461 TRY_COMPARE(seriesIndexSpy.count(), 1);
462 TRY_COMPARE(setIndexSpy1.count(), 1);
463 TRY_COMPARE(setIndexSpy2.count(), 0);
464
465 //should enter set1, bar0
466 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
467 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
468 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
469 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
470 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
471 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
472
473 setIndexSpyArg = setIndexSpy1.takeFirst();
474 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
475 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
476 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
477 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
478
479//=======================================================================
480// move mouse on top of set2, bar0 to check the index (hover out set1,
481// hover in set2)
482 QTest::mouseMove(widget: view.viewport(), pos: layout.at(i: 1).center().toPoint());
483 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 10000);
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).left(),
557 (layout.at(i: 3).top() + layout.at(i: 4).bottom()) / 2));
558
559 TRY_COMPARE(seriesIndexSpy.count(), 1);
560 TRY_COMPARE(setIndexSpy1.count(), 1);
561 TRY_COMPARE(setIndexSpy2.count(), 0);
562
563 //should leave set1, bar1
564 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
565 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
566 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
567 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
568 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
569 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
570
571 setIndexSpyArg = setIndexSpy1.takeFirst();
572 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
573 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
574 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
575 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
576
577//=======================================================================
578// move mouse on top of set2, bar1 to check the index (hover in set2)
579 QTest::mouseMove(widget: view.viewport(), pos: layout.at(i: 3).center().toPoint());
580
581 TRY_COMPARE(seriesIndexSpy.count(), 1);
582 TRY_COMPARE(setIndexSpy1.count(), 0);
583 TRY_COMPARE(setIndexSpy2.count(), 1);
584
585 //should enter set2, bar1
586 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
587 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
588 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
589 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
590 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
591 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
592
593 setIndexSpyArg = setIndexSpy2.takeFirst();
594 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
595 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
596 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
597 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
598
599//=======================================================================
600// move mouse between set1 and set2 (hover out set2)
601 QTest::mouseMove(widget: view.viewport(), pos: QPoint(layout.at(i: 3).left(),
602 (layout.at(i: 3).top() + layout.at(i: 4).bottom()) / 2));
603
604 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
605 TRY_COMPARE(seriesIndexSpy.count(), 1);
606 TRY_COMPARE(setIndexSpy1.count(), 0);
607 TRY_COMPARE(setIndexSpy2.count(), 1);
608
609 //should leave set1, bar1
610 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
611 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
612 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
613 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
614 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
615 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
616
617 setIndexSpyArg = setIndexSpy2.takeFirst();
618 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
619 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
620 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
621 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
622}
623
624void tst_QHorizontalStackedBarSeries::mousePressed()
625{
626 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
627
628 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
629
630 QBarSet* set1 = new QBarSet(QString("set 1"));
631 *set1 << 10 << 10 << 10;
632 series->append(set: set1);
633
634 QBarSet* set2 = new QBarSet(QString("set 2"));
635 *set2 << 10 << 10 << 10;
636 series->append(set: set2);
637 QList<QBarSet*> barSets = series->barSets();
638
639 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
640 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
641 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
642
643 QChartView view(new QChart());
644 view.resize(w: 400,h: 300);
645 view.chart()->addSeries(series);
646 view.show();
647 QVERIFY(QTest::qWaitForWindowExposed(&view));
648
649 // Calculate expected layout for bars
650 QRectF plotArea = view.chart()->plotArea();
651 qreal width = plotArea.width();
652 qreal height = plotArea.height();
653 qreal rangeY = 3; // 3 values per set
654 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
655 qreal scaleY = (height / rangeY);
656 qreal scaleX = (width / rangeX);
657
658 qreal setCount = series->count();
659 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
660 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
661 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
662
663 QVector<QRectF> layout;
664
665 // 3 = count of values in set
666 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
667 for (int i = 0; i < 3; i++) {
668 qreal xMax = -scaleX * domainMinX + plotArea.left();
669 qreal xMin = -scaleX * domainMinX + plotArea.left();
670 for (int set = 0; set < setCount; set++) {
671 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
672 qreal rectWidth = barSets.at(i: set)->at(index: i) * scaleX;
673 if (rectWidth > 0) {
674 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
675 layout.append(t: rect);
676 xMax += rectWidth;
677 } else {
678 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
679 layout.append(t: rect);
680 xMin += rectWidth;
681 }
682 }
683 }
684
685//====================================================================================
686// barset 1, bar 0
687 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 0).center().toPoint());
688 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
689
690 QCOMPARE(seriesSpy.count(), 1);
691 QCOMPARE(setSpy1.count(), 1);
692 QCOMPARE(setSpy2.count(), 0);
693
694 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
695 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
696 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
697 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
698
699 QList<QVariant> setSpyArg = setSpy1.takeFirst();
700 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
701 QVERIFY(setSpyArg.at(0).toInt() == 0);
702
703//====================================================================================
704// barset 1, bar 1
705 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 2).center().toPoint());
706 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
707
708 QCOMPARE(seriesSpy.count(), 1);
709 QCOMPARE(setSpy1.count(), 1);
710 QCOMPARE(setSpy2.count(), 0);
711
712 seriesSpyArg = seriesSpy.takeFirst();
713 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
714 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
715 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
716
717 setSpyArg = setSpy1.takeFirst();
718 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
719 QVERIFY(setSpyArg.at(0).toInt() == 1);
720
721//====================================================================================
722// barset 1, bar 2
723 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 4).center().toPoint());
724 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
725
726 QCOMPARE(seriesSpy.count(), 1);
727 QCOMPARE(setSpy1.count(), 1);
728 QCOMPARE(setSpy2.count(), 0);
729
730 seriesSpyArg = seriesSpy.takeFirst();
731 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
732 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
733 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
734
735 setSpyArg = setSpy1.takeFirst();
736 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
737 QVERIFY(setSpyArg.at(0).toInt() == 2);
738
739//====================================================================================
740// barset 2, bar 0
741 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 1).center().toPoint());
742 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
743
744 QCOMPARE(seriesSpy.count(), 1);
745 QCOMPARE(setSpy1.count(), 0);
746 QCOMPARE(setSpy2.count(), 1);
747
748 seriesSpyArg = seriesSpy.takeFirst();
749 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
750 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
751 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
752
753 setSpyArg = setSpy2.takeFirst();
754 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
755 QVERIFY(setSpyArg.at(0).toInt() == 0);
756
757//====================================================================================
758// barset 2, bar 1
759 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 3).center().toPoint());
760 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
761
762 QCOMPARE(seriesSpy.count(), 1);
763 QCOMPARE(setSpy1.count(), 0);
764 QCOMPARE(setSpy2.count(), 1);
765
766 seriesSpyArg = seriesSpy.takeFirst();
767 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
768 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
769 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
770
771 setSpyArg = setSpy2.takeFirst();
772 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
773 QVERIFY(setSpyArg.at(0).toInt() == 1);
774
775//====================================================================================
776// barset 2, bar 2
777 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 5).center().toPoint());
778 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
779
780 QCOMPARE(seriesSpy.count(), 1);
781 QCOMPARE(setSpy1.count(), 0);
782 QCOMPARE(setSpy2.count(), 1);
783
784 seriesSpyArg = seriesSpy.takeFirst();
785 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
786 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
787 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
788
789 setSpyArg = setSpy2.takeFirst();
790 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
791 QVERIFY(setSpyArg.at(0).toInt() == 2);
792}
793
794void tst_QHorizontalStackedBarSeries::mouseReleased()
795{
796 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
797
798 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
799
800 QBarSet* set1 = new QBarSet(QString("set 1"));
801 *set1 << 10 << 10 << 10;
802 series->append(set: set1);
803
804 QBarSet* set2 = new QBarSet(QString("set 2"));
805 *set2 << 10 << 10 << 10;
806 series->append(set: set2);
807 QList<QBarSet*> barSets = series->barSets();
808
809 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
810 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
811 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
812
813 QChartView view(new QChart());
814 view.resize(w: 400,h: 300);
815 view.chart()->addSeries(series);
816 view.show();
817 QVERIFY(QTest::qWaitForWindowExposed(&view));
818
819 // Calculate expected layout for bars
820 QRectF plotArea = view.chart()->plotArea();
821 qreal width = plotArea.width();
822 qreal height = plotArea.height();
823 qreal rangeY = 3; // 3 values per set
824 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
825 qreal scaleY = (height / rangeY);
826 qreal scaleX = (width / rangeX);
827
828 qreal setCount = series->count();
829 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
830 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
831 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
832
833 QVector<QRectF> layout;
834
835 // 3 = count of values in set
836 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
837 for (int i = 0; i < 3; i++) {
838 qreal xMax = -scaleX * domainMinX + plotArea.left();
839 qreal xMin = -scaleX * domainMinX + plotArea.left();
840 for (int set = 0; set < setCount; set++) {
841 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
842 qreal rectWidth = barSets.at(i: set)->at(index: i) * scaleX;
843 if (rectWidth > 0) {
844 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
845 layout.append(t: rect);
846 xMax += rectWidth;
847 } else {
848 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
849 layout.append(t: rect);
850 xMin += rectWidth;
851 }
852 }
853 }
854
855//====================================================================================
856// barset 1, bar 0
857 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 0).center().toPoint());
858 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
859
860 QCOMPARE(seriesSpy.count(), 1);
861 QCOMPARE(setSpy1.count(), 1);
862 QCOMPARE(setSpy2.count(), 0);
863
864 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
865 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
866 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
867 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
868
869 QList<QVariant> setSpyArg = setSpy1.takeFirst();
870 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
871 QVERIFY(setSpyArg.at(0).toInt() == 0);
872
873//====================================================================================
874// barset 1, bar 1
875 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 2).center().toPoint());
876 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
877
878 QCOMPARE(seriesSpy.count(), 1);
879 QCOMPARE(setSpy1.count(), 1);
880 QCOMPARE(setSpy2.count(), 0);
881
882 seriesSpyArg = seriesSpy.takeFirst();
883 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
884 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
885 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
886
887 setSpyArg = setSpy1.takeFirst();
888 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
889 QVERIFY(setSpyArg.at(0).toInt() == 1);
890
891//====================================================================================
892// barset 1, bar 2
893 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 4).center().toPoint());
894 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
895
896 QCOMPARE(seriesSpy.count(), 1);
897 QCOMPARE(setSpy1.count(), 1);
898 QCOMPARE(setSpy2.count(), 0);
899
900 seriesSpyArg = seriesSpy.takeFirst();
901 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
902 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
903 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
904
905 setSpyArg = setSpy1.takeFirst();
906 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
907 QVERIFY(setSpyArg.at(0).toInt() == 2);
908
909//====================================================================================
910// barset 2, bar 0
911 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 1).center().toPoint());
912 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
913
914 QCOMPARE(seriesSpy.count(), 1);
915 QCOMPARE(setSpy1.count(), 0);
916 QCOMPARE(setSpy2.count(), 1);
917
918 seriesSpyArg = seriesSpy.takeFirst();
919 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
920 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
921 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
922
923 setSpyArg = setSpy2.takeFirst();
924 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
925 QVERIFY(setSpyArg.at(0).toInt() == 0);
926
927//====================================================================================
928// barset 2, bar 1
929 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 3).center().toPoint());
930 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
931
932 QCOMPARE(seriesSpy.count(), 1);
933 QCOMPARE(setSpy1.count(), 0);
934 QCOMPARE(setSpy2.count(), 1);
935
936 seriesSpyArg = seriesSpy.takeFirst();
937 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
938 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
939 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
940
941 setSpyArg = setSpy2.takeFirst();
942 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
943 QVERIFY(setSpyArg.at(0).toInt() == 1);
944
945//====================================================================================
946// barset 2, bar 2
947 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 5).center().toPoint());
948 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
949
950 QCOMPARE(seriesSpy.count(), 1);
951 QCOMPARE(setSpy1.count(), 0);
952 QCOMPARE(setSpy2.count(), 1);
953
954 seriesSpyArg = seriesSpy.takeFirst();
955 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
956 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
957 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
958
959 setSpyArg = setSpy2.takeFirst();
960 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
961 QVERIFY(setSpyArg.at(0).toInt() == 2);
962}
963
964void tst_QHorizontalStackedBarSeries::mouseDoubleClicked()
965{
966 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
967
968 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
969
970 QBarSet* set1 = new QBarSet(QString("set 1"));
971 *set1 << 10 << 10 << 10;
972 series->append(set: set1);
973
974 QBarSet* set2 = new QBarSet(QString("set 2"));
975 *set2 << 10 << 10 << 10;
976 series->append(set: set2);
977 QList<QBarSet*> barSets = series->barSets();
978
979 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
980 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
981 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
982
983 QChartView view(new QChart());
984 view.resize(w: 400,h: 300);
985 view.chart()->addSeries(series);
986 view.show();
987 QVERIFY(QTest::qWaitForWindowExposed(&view));
988
989 // Calculate expected layout for bars
990 QRectF plotArea = view.chart()->plotArea();
991 qreal width = plotArea.width();
992 qreal height = plotArea.height();
993 qreal rangeY = 3; // 3 values per set
994 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
995 qreal scaleY = (height / rangeY);
996 qreal scaleX = (width / rangeX);
997
998 qreal setCount = series->count();
999 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
1000 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
1001 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
1002
1003 QVector<QRectF> layout;
1004
1005 // 3 = count of values in set
1006 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1007 for (int i = 0; i < 3; i++) {
1008 qreal xMax = -scaleX * domainMinX + plotArea.left();
1009 qreal xMin = -scaleX * domainMinX + plotArea.left();
1010 for (int set = 0; set < setCount; set++) {
1011 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
1012 qreal rectWidth = barSets.at(i: set)->at(index: i) * scaleX;
1013 if (rectWidth > 0) {
1014 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
1015 layout.append(t: rect);
1016 xMax += rectWidth;
1017 } else {
1018 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
1019 layout.append(t: rect);
1020 xMin += rectWidth;
1021 }
1022 }
1023 }
1024
1025 // barset 1, bar 0
1026 QTest::mouseDClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 0).center().toPoint());
1027 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1028
1029 QCOMPARE(seriesSpy.count(), 1);
1030 QCOMPARE(setSpy1.count(), 1);
1031 QCOMPARE(setSpy2.count(), 0);
1032
1033 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1034 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1035 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1036 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1037
1038 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1039 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1040 QVERIFY(setSpyArg.at(0).toInt() == 0);
1041}
1042QTEST_MAIN(tst_QHorizontalStackedBarSeries)
1043
1044#include "tst_qhorizontalstackedbarseries.moc"
1045
1046

source code of qtcharts/tests/auto/qhorizontalstackedbarseries/tst_qhorizontalstackedbarseries.cpp