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/QBarSeries>
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(QList<QBarSet*>)
41Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
42
43class tst_QBarSeries : public QObject
44{
45 Q_OBJECT
46
47public slots:
48 void initTestCase();
49 void cleanupTestCase();
50 void init();
51 void cleanup();
52
53private slots:
54 void qbarseries_data();
55 void qbarseries();
56 void type_data();
57 void type();
58 void append_data();
59 void append();
60 void remove_data();
61 void remove();
62 void take_data();
63 void take();
64 void appendList_data();
65 void appendList();
66 void count_data();
67 void count();
68 void barSets_data();
69 void barSets();
70 void setLabelsVisible_data();
71 void setLabelsVisible();
72 void setLabelsFormat();
73 void setLabelsPosition();
74 void setLabelsAngle();
75 void setLabelsPrecision();
76 void opacity();
77 void mouseclicked_data();
78 void mouseclicked();
79 void mousehovered_data();
80 void mousehovered();
81 void clearWithAnimations();
82 void destruction();
83 void mousePressed();
84 void mouseReleased();
85 void mouseDoubleClicked();
86
87private:
88 QBarSeries* m_barseries;
89 QBarSeries* m_barseries_with_sets;
90
91 QList<QBarSet*> m_testSets;
92
93};
94
95void tst_QBarSeries::initTestCase()
96{
97 qRegisterMetaType<QBarSet*>(typeName: "QBarSet*");
98 qRegisterMetaType<QList<QBarSet*> >(typeName: "QList<QBarSet*>");
99 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>(typeName: "QAbstractBarSeries::LabelsPosition");
100}
101
102void tst_QBarSeries::cleanupTestCase()
103{
104 QTest::qWait(ms: 1); // Allow final deleteLaters to run
105}
106
107void tst_QBarSeries::init()
108{
109 m_barseries = new QBarSeries();
110 m_barseries_with_sets = new QBarSeries();
111
112 for (int i=0; i<5; i++) {
113 m_testSets.append(t: new QBarSet("testset"));
114 m_barseries_with_sets->append(set: m_testSets.at(i));
115 }
116}
117
118void tst_QBarSeries::cleanup()
119{
120 foreach (QBarSet* s, m_testSets) {
121 m_barseries_with_sets->remove(set: s);
122 }
123 m_testSets.clear();
124
125 delete m_barseries;
126 m_barseries = 0;
127 delete m_barseries_with_sets;
128 m_barseries_with_sets = 0;
129}
130
131void tst_QBarSeries::qbarseries_data()
132{
133}
134
135void tst_QBarSeries::qbarseries()
136{
137 QBarSeries *barseries = new QBarSeries();
138 QVERIFY(barseries != 0);
139 delete barseries;
140}
141
142void tst_QBarSeries::type_data()
143{
144
145}
146
147void tst_QBarSeries::type()
148{
149 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar);
150}
151
152void tst_QBarSeries::append_data()
153{
154}
155
156void tst_QBarSeries::append()
157{
158 QVERIFY(m_barseries->count() == 0);
159
160 bool ret = false;
161
162 // Try adding barset
163 QBarSet *barset = new QBarSet("testset");
164 ret = m_barseries->append(set: barset);
165
166 QVERIFY(ret == true);
167 QVERIFY(m_barseries->count() == 1);
168
169 // Try adding another set
170 QBarSet *barset2 = new QBarSet("testset2");
171 ret = m_barseries->append(set: barset2);
172
173 QVERIFY(ret == true);
174 QVERIFY(m_barseries->count() == 2);
175
176 // Try adding same set again
177 ret = m_barseries->append(set: barset2);
178 QVERIFY(ret == false);
179 QVERIFY(m_barseries->count() == 2);
180
181 // Try adding null set
182 ret = m_barseries->append(set: 0);
183 QVERIFY(ret == false);
184 QVERIFY(m_barseries->count() == 2);
185
186}
187
188void tst_QBarSeries::remove_data()
189{
190}
191
192void tst_QBarSeries::remove()
193{
194 int count = m_testSets.count();
195 QVERIFY(m_barseries_with_sets->count() == count);
196
197 // Try to remove null pointer (should not remove, should not crash)
198 bool ret = false;
199 ret = m_barseries_with_sets->remove(set: 0);
200 QVERIFY(ret == false);
201 QVERIFY(m_barseries_with_sets->count() == count);
202
203 // Try to remove invalid pointer (should not remove, should not crash)
204 ret = m_barseries_with_sets->remove(set: (QBarSet*) (m_testSets.at(i: 0) + 1) );
205 QVERIFY(ret == false);
206 QVERIFY(m_barseries_with_sets->count() == count);
207
208 // remove some sets
209 ret = m_barseries_with_sets->remove(set: m_testSets.at(i: 2));
210 QVERIFY(ret == true);
211 ret = m_barseries_with_sets->remove(set: m_testSets.at(i: 3));
212 QVERIFY(ret == true);
213 ret = m_barseries_with_sets->remove(set: m_testSets.at(i: 4));
214 QVERIFY(ret == true);
215
216 QVERIFY(m_barseries_with_sets->count() == 2);
217
218 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
219
220 QVERIFY(verifysets.at(0) == m_testSets.at(0));
221 QVERIFY(verifysets.at(1) == m_testSets.at(1));
222
223 // Try removing all sets again (should be ok, even if some sets have already been removed)
224 ret = false;
225 for (int i=0; i<count; i++) {
226 ret |= m_barseries_with_sets->remove(set: m_testSets.at(i));
227 }
228
229 QVERIFY(ret == true);
230 QVERIFY(m_barseries_with_sets->count() == 0);
231}
232
233void tst_QBarSeries::take_data()
234{
235
236}
237
238void tst_QBarSeries::take()
239{
240 int count = m_testSets.count();
241 QVERIFY(m_barseries_with_sets->count() == count);
242
243 QSignalSpy countSpy(m_barseries_with_sets,SIGNAL(countChanged()));
244 QSignalSpy removedSpy(m_barseries_with_sets,SIGNAL(barsetsRemoved(QList<QBarSet*>)));
245
246 for (int i=0; i<m_testSets.count(); i++) {
247 QBarSet* set = m_testSets.at(i);
248 bool success = m_barseries_with_sets->take(set);
249 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
250 QVERIFY(success);
251 TRY_COMPARE(countSpy.count(),1);
252 TRY_COMPARE(removedSpy.count(),1);
253
254 QList<QVariant> removedSpyArg = removedSpy.takeFirst();
255 QList<QBarSet*> removedSets = qvariant_cast<QList<QBarSet*> > (v: removedSpyArg.at(i: 0));
256 QCOMPARE(removedSets.at(0), m_testSets.at(i));
257 countSpy.takeFirst();
258 }
259}
260
261
262void tst_QBarSeries::appendList_data()
263{
264
265}
266
267void tst_QBarSeries::appendList()
268{
269 int count = 5;
270 QVERIFY(m_barseries->count() == 0);
271
272 QList<QBarSet*> sets;
273 for (int i=0; i<count; i++) {
274 sets.append(t: new QBarSet("testset"));
275 }
276
277 // Append new sets (should succeed, count should match the count of sets)
278 bool ret = false;
279 ret = m_barseries->append(sets);
280 QVERIFY(ret == true);
281 QVERIFY(m_barseries->count() == count);
282
283 // Append same sets again (should fail, count should remain same)
284 ret = m_barseries->append(sets);
285 QVERIFY(ret == false);
286 QVERIFY(m_barseries->count() == count);
287
288 // Try append empty list (should succeed, but count should remain same)
289 QList<QBarSet*> invalidList;
290 ret = m_barseries->append(sets: invalidList);
291 QVERIFY(ret == true);
292 QVERIFY(m_barseries->count() == count);
293
294 // Try append list with one new and one existing set (should fail, count remains same)
295 invalidList.append(t: new QBarSet("ok set"));
296 invalidList.append(t: sets.at(i: 0));
297 ret = m_barseries->append(sets: invalidList);
298 QVERIFY(ret == false);
299 QVERIFY(m_barseries->count() == count);
300 delete invalidList.at(i: 0);
301
302 // Try append list with null pointers (should fail, count remains same)
303 QList<QBarSet*> invalidList2;
304 invalidList2.append(t: 0);
305 invalidList2.append(t: 0);
306 invalidList2.append(t: 0);
307 ret = m_barseries->append(sets: invalidList2);
308 QVERIFY(ret == false);
309 QVERIFY(m_barseries->count() == count);
310}
311
312void tst_QBarSeries::count_data()
313{
314
315}
316
317void tst_QBarSeries::count()
318{
319 QVERIFY(m_barseries->count() == 0);
320 QVERIFY(m_barseries_with_sets->count() == m_testSets.count());
321}
322
323void tst_QBarSeries::barSets_data()
324{
325
326}
327
328void tst_QBarSeries::barSets()
329{
330 QVERIFY(m_barseries->barSets().count() == 0);
331
332 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
333 QVERIFY(sets.count() == m_testSets.count());
334
335 for (int i=0; i<m_testSets.count(); i++) {
336 QVERIFY(sets.at(i) == m_testSets.at(i));
337 }
338}
339
340void tst_QBarSeries::setLabelsVisible_data()
341{
342
343}
344
345void tst_QBarSeries::setLabelsVisible()
346{
347 // labels should be invisible by default
348 QVERIFY(m_barseries->isLabelsVisible() == false);
349 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
350
351 // turn labels to visible
352 m_barseries_with_sets->setLabelsVisible(true);
353 // TODO: test the signal
354 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
355
356 // turn labels to invisible
357 m_barseries_with_sets->setLabelsVisible(false);
358 // TODO: test the signal
359 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
360
361 // without parameter, should turn labels to visible
362 m_barseries_with_sets->setLabelsVisible();
363 // TODO: test the signal
364 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
365}
366
367void tst_QBarSeries::setLabelsFormat()
368{
369 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
370 QCOMPARE(m_barseries->labelsFormat(), QString());
371
372 QString format("(@value)");
373 m_barseries->setLabelsFormat(format);
374 TRY_COMPARE(labelsFormatSpy.count(), 1);
375 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
376 QVERIFY(arguments.at(0).toString() == format);
377 QCOMPARE(m_barseries->labelsFormat(), format);
378
379 m_barseries->setLabelsFormat(QString());
380 TRY_COMPARE(labelsFormatSpy.count(), 1);
381 arguments = labelsFormatSpy.takeFirst();
382 QVERIFY(arguments.at(0).toString() == QString());
383 QCOMPARE(m_barseries->labelsFormat(), QString());
384}
385
386void tst_QBarSeries::setLabelsPosition()
387{
388 QSignalSpy labelsPositionSpy(m_barseries,
389 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
390 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsCenter);
391
392 m_barseries->setLabelsPosition(QBarSeries::LabelsInsideEnd);
393 TRY_COMPARE(labelsPositionSpy.count(), 1);
394 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
395 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
396 == QBarSeries::LabelsInsideEnd);
397 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsInsideEnd);
398
399 m_barseries->setLabelsPosition(QBarSeries::LabelsInsideBase);
400 TRY_COMPARE(labelsPositionSpy.count(), 1);
401 arguments = labelsPositionSpy.takeFirst();
402 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
403 == QBarSeries::LabelsInsideBase);
404 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsInsideBase);
405
406 m_barseries->setLabelsPosition(QBarSeries::LabelsOutsideEnd);
407 TRY_COMPARE(labelsPositionSpy.count(), 1);
408 arguments = labelsPositionSpy.takeFirst();
409 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
410 == QBarSeries::LabelsOutsideEnd);
411 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsOutsideEnd);
412
413 m_barseries->setLabelsPosition(QBarSeries::LabelsCenter);
414 TRY_COMPARE(labelsPositionSpy.count(), 1);
415 arguments = labelsPositionSpy.takeFirst();
416 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
417 == QBarSeries::LabelsCenter);
418 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsCenter);
419}
420
421void tst_QBarSeries::setLabelsAngle()
422{
423 QSignalSpy labelsAngleSpy(m_barseries,
424 SIGNAL(labelsAngleChanged(qreal)));
425 QCOMPARE(m_barseries->labelsAngle(), 0.0);
426
427 m_barseries->setLabelsAngle(55.0);
428 TRY_COMPARE(labelsAngleSpy.count(), 1);
429 QList<QVariant> arguments = labelsAngleSpy.takeFirst();
430 QVERIFY(arguments.at(0).value<qreal>() == 55.0);
431 QCOMPARE(m_barseries->labelsAngle(), 55.0);
432}
433
434void tst_QBarSeries::setLabelsPrecision()
435{
436 QSignalSpy labelsPrecisionSpy(m_barseries,
437 SIGNAL(labelsPrecisionChanged(int)));
438 QCOMPARE(m_barseries->labelsPrecision(), 6);
439
440 m_barseries->setLabelsPrecision(9);
441 TRY_COMPARE(labelsPrecisionSpy.count(), 1);
442 QList<QVariant> arguments = labelsPrecisionSpy.takeFirst();
443 QVERIFY(arguments.at(0).value<int>() == 9);
444 QCOMPARE(m_barseries->labelsPrecision(), 9);
445}
446
447void tst_QBarSeries::opacity()
448{
449 QSignalSpy opacitySpy(m_barseries, SIGNAL(opacityChanged()));
450
451 QCOMPARE(m_barseries->opacity(), 1.0);
452
453 m_barseries->setOpacity(0.5);
454 QCOMPARE(m_barseries->opacity(), 0.5);
455 QCOMPARE(opacitySpy.count(), 1);
456
457 m_barseries->setOpacity(0.0);
458 QCOMPARE(m_barseries->opacity(), 0.0);
459 QCOMPARE(opacitySpy.count(), 2);
460
461 m_barseries->setOpacity(1.0);
462 QCOMPARE(m_barseries->opacity(), 1.0);
463 QCOMPARE(opacitySpy.count(), 3);
464}
465
466void tst_QBarSeries::mouseclicked_data()
467{
468
469}
470
471void tst_QBarSeries::mouseclicked()
472{
473 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
474
475 QBarSeries* series = new QBarSeries();
476
477 QBarSet* set1 = new QBarSet(QString("set 1"));
478 *set1 << 10 << 10 << 10;
479 series->append(set: set1);
480
481 QBarSet* set2 = new QBarSet(QString("set 2"));
482 *set2 << 10 << 10 << 10;
483 series->append(set: set2);
484 QList<QBarSet*> barSets = series->barSets();
485
486 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
487 QSignalSpy setSpy1(set1, SIGNAL(clicked(int)));
488 QSignalSpy setSpy2(set2, SIGNAL(clicked(int)));
489
490 QChartView view(new QChart());
491 view.resize(w: 400,h: 300);
492 view.chart()->addSeries(series);
493 view.show();
494 QVERIFY(QTest::qWaitForWindowExposed(&view));
495
496 // Calculate expected layout for bars
497 QRectF plotArea = view.chart()->plotArea();
498 qreal width = plotArea.width();
499 qreal height = plotArea.height();
500 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
501 qreal rangeX = 3; // 3 values per set
502 qreal scaleY = (height / rangeY);
503 qreal scaleX = (width / rangeX);
504
505 qreal setCount = series->count();
506 qreal domainMinY = 0; // These come from internal domain used by barseries.
507 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
508 qreal rectWidth = (scaleX / setCount) * series->barWidth();
509
510 QVector<QRectF> layout;
511
512 // 3 = count of values in set
513 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
514 for (int i = 0; i < 3; i++) {
515 qreal yPos = height + scaleY * domainMinY + plotArea.top();
516 for (int set = 0; set < setCount; set++) {
517 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
518 xPos -= series->count()*rectWidth/2;
519 xPos += set*rectWidth;
520
521 qreal rectHeight = barSets.at(i: set)->at(index: i) * scaleY;
522 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
523 layout.append(t: rect);
524 }
525 }
526
527//====================================================================================
528// barset 1, bar 0
529 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 0).center().toPoint());
530 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
531
532 QCOMPARE(seriesSpy.count(), 1);
533 QCOMPARE(setSpy1.count(), 1);
534 QCOMPARE(setSpy2.count(), 0);
535
536 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
537 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
538 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
539 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
540
541 QList<QVariant> setSpyArg = setSpy1.takeFirst();
542 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
543 QVERIFY(setSpyArg.at(0).toInt() == 0);
544
545//====================================================================================
546// barset 1, bar 1
547 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 2).center().toPoint());
548 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
549
550 QCOMPARE(seriesSpy.count(), 1);
551 QCOMPARE(setSpy1.count(), 1);
552 QCOMPARE(setSpy2.count(), 0);
553
554 seriesSpyArg = seriesSpy.takeFirst();
555 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
556 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
557 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
558
559 setSpyArg = setSpy1.takeFirst();
560 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
561 QVERIFY(setSpyArg.at(0).toInt() == 1);
562
563//====================================================================================
564// barset 1, bar 2
565 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 4).center().toPoint());
566 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
567
568 QCOMPARE(seriesSpy.count(), 1);
569 QCOMPARE(setSpy1.count(), 1);
570 QCOMPARE(setSpy2.count(), 0);
571
572 seriesSpyArg = seriesSpy.takeFirst();
573 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
574 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
575 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
576
577 setSpyArg = setSpy1.takeFirst();
578 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
579 QVERIFY(setSpyArg.at(0).toInt() == 2);
580
581//====================================================================================
582// barset 2, bar 0
583 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 1).center().toPoint());
584 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
585
586 QCOMPARE(seriesSpy.count(), 1);
587 QCOMPARE(setSpy1.count(), 0);
588 QCOMPARE(setSpy2.count(), 1);
589
590 seriesSpyArg = seriesSpy.takeFirst();
591 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
592 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
593 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
594
595 setSpyArg = setSpy2.takeFirst();
596 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
597 QVERIFY(setSpyArg.at(0).toInt() == 0);
598
599//====================================================================================
600// barset 2, bar 1
601 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 3).center().toPoint());
602 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
603
604 QCOMPARE(seriesSpy.count(), 1);
605 QCOMPARE(setSpy1.count(), 0);
606 QCOMPARE(setSpy2.count(), 1);
607
608 seriesSpyArg = seriesSpy.takeFirst();
609 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
610 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
611 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
612
613 setSpyArg = setSpy2.takeFirst();
614 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
615 QVERIFY(setSpyArg.at(0).toInt() == 1);
616
617//====================================================================================
618// barset 2, bar 2
619 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 5).center().toPoint());
620 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
621
622 QCOMPARE(seriesSpy.count(), 1);
623 QCOMPARE(setSpy1.count(), 0);
624 QCOMPARE(setSpy2.count(), 1);
625
626 seriesSpyArg = seriesSpy.takeFirst();
627 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
628 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
629 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
630
631 setSpyArg = setSpy2.takeFirst();
632 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
633 QVERIFY(setSpyArg.at(0).toInt() == 2);
634}
635
636void tst_QBarSeries::mousehovered_data()
637{
638 QTest::addColumn<uint>(name: "labelsPosition");
639 QTest::addColumn<bool>(name: "labelsVisible");
640
641 QTest::newRow(dataTag: "labelsCenter") << (uint)QAbstractBarSeries::LabelsCenter << true;
642 QTest::newRow(dataTag: "labelsInsideEnd") << (uint)QAbstractBarSeries::LabelsInsideEnd << true;
643 QTest::newRow(dataTag: "labelsInsideBase") << (uint)QAbstractBarSeries::LabelsInsideBase << true;
644 QTest::newRow(dataTag: "labelsOutsideEnd") << (uint)QAbstractBarSeries::LabelsOutsideEnd << true;
645 QTest::newRow(dataTag: "noLabelsCenter") << (uint)QAbstractBarSeries::LabelsCenter << false;
646}
647
648void tst_QBarSeries::mousehovered()
649{
650 QFETCH(uint, labelsPosition);
651 QFETCH(bool, labelsVisible);
652
653 QBarSeries* series = new QBarSeries();
654 series->setLabelsVisible(labelsVisible);
655 series->setLabelsPosition(static_cast<QAbstractBarSeries::LabelsPosition>(labelsPosition));
656 QBarSet* set1 = new QBarSet(QString("set 1"));
657 *set1 << 10 << 10 << 10;
658 series->append(set: set1);
659
660 QBarSet* set2 = new QBarSet(QString("set 2"));
661 *set2 << 10 << 10 << 10;
662 series->append(set: set2);
663 QList<QBarSet*> barSets = series->barSets();
664
665 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
666 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
667 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
668
669 QChartView view(new QChart());
670 view.resize(w: 400,h: 300);
671 view.chart()->addSeries(series);
672 view.show();
673 QVERIFY(QTest::qWaitForWindowExposed(&view));
674
675 //this is hack since view does not get events otherwise
676 view.setMouseTracking(true);
677
678 // Calculate expected layout for bars
679 QRectF plotArea = view.chart()->plotArea();
680 qreal width = plotArea.width();
681 qreal height = plotArea.height();
682 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
683 qreal rangeX = 3; // 3 values per set
684 qreal scaleY = (height / rangeY);
685 qreal scaleX = (width / rangeX);
686
687 qreal setCount = series->count();
688 qreal domainMinY = 0; // These come from internal domain used by barseries.
689 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
690 qreal rectWidth = (scaleX / setCount) * series->barWidth();
691
692 QVector<QRectF> layout;
693
694 // 3 = count of values in set
695 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
696 for (int i = 0; i < 3; i++) {
697 qreal yPos = height + scaleY * domainMinY + plotArea.top();
698 for (int set = 0; set < setCount; set++) {
699 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
700 xPos -= series->count()*rectWidth/2;
701 xPos += set*rectWidth;
702
703 qreal rectHeight = barSets.at(i: set)->at(index: i) * scaleY;
704 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
705 layout.append(t: rect);
706 }
707 }
708 // In order to make it feel like a normal user moving the mouse, we hover across from the edge.
709 // If we go right to the center, we can hit a label which does not emit hovered then, but that
710 // would not be a normal use case.
711
712//=======================================================================
713// move mouse to left border
714 QTest::mouseMove(widget: view.viewport(), pos: QPoint(0, layout.at(i: 0).center().y()));
715 QTest::qWait(ms: 5000);
716 QCOMPARE(seriesIndexSpy.count(), 0);
717 QCOMPARE(setIndexSpy1.count(), 0);
718 QCOMPARE(setIndexSpy2.count(), 0);
719
720//=======================================================================
721// move mouse to just inside left border to account for labels
722 QTest::mouseMove(widget: view.viewport(), pos: QPoint(layout.at(i: 0).x() + 1, layout.at(i: 0).y() + 1));
723 QTRY_COMPARE(seriesIndexSpy.count(), 1);
724 QTRY_COMPARE(setIndexSpy1.count(), 1);
725 QTRY_COMPARE(setIndexSpy2.count(), 0);
726
727 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
728 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
729 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
730 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
731
732 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
733 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
734 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
735
736//=======================================================================
737// move mouse to center of set1
738 QTest::mouseMove(widget: view.viewport(), pos: layout.at(i: 0).center().toPoint());
739 QTest::qWait(ms: 5000);
740 QCOMPARE(seriesIndexSpy.count(), 0);
741 QCOMPARE(setIndexSpy1.count(), 0);
742 QCOMPARE(setIndexSpy2.count(), 0);
743
744//=======================================================================
745// move mouse to bottom of set1
746 QTest::mouseMove(widget: view.viewport(), pos: QPoint(layout.at(i: 0).center().x(), layout.at(i: 0).bottom()));
747 QTest::qWait(ms: 5000);
748 QCOMPARE(seriesIndexSpy.count(), 0);
749 QCOMPARE(setIndexSpy1.count(), 0);
750 QCOMPARE(setIndexSpy2.count(), 0);
751
752//=======================================================================
753// move mouse from top of set1 to top of set2
754 QTest::mouseMove(widget: view.viewport(), pos: QPoint(layout.at(i: 1).x() + 1, layout.at(i: 1).y() + 1));
755 QTRY_COMPARE(seriesIndexSpy.count(), 2);
756 QTRY_COMPARE(setIndexSpy1.count(), 1);
757 QTRY_COMPARE(setIndexSpy2.count(), 1);
758
759 // should leave set1
760 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
761 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
762 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
763 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
764
765 setIndexSpyArg = setIndexSpy1.takeFirst();
766 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
767 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
768
769 // should enter set2
770 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
771 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
772 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
773 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
774
775 setIndexSpyArg = setIndexSpy2.takeFirst();
776 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
777 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
778
779//=======================================================================
780// move mouse from top of set2 to background
781 QTest::mouseMove(widget: view.viewport(), pos: QPoint(layout.at(i: 1).center().x(), 0));
782 QTRY_COMPARE(seriesIndexSpy.count(), 1);
783 QTRY_COMPARE(setIndexSpy1.count(), 0);
784 QTRY_COMPARE(setIndexSpy2.count(), 1);
785
786 // should leave set2
787 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
788 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
789 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
790 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
791
792 setIndexSpyArg = setIndexSpy2.takeFirst();
793 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
794 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
795
796//=======================================================================
797// move mouse on top of set1, bar0 to check the index (hover into set1)
798 QTest::mouseMove(widget: view.viewport(), pos: QPoint(layout.at(i: 0).x() + 1, layout.at(i: 0).y() + 1));
799
800 QTRY_COMPARE(seriesIndexSpy.count(), 1);
801 QTRY_COMPARE(setIndexSpy1.count(), 1);
802 QTRY_COMPARE(setIndexSpy2.count(), 0);
803
804 //should enter set1, bar0
805 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
806 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
807 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
808 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
809 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
810 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
811
812 setIndexSpyArg = setIndexSpy1.takeFirst();
813 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
814 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
815 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
816 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
817
818//=======================================================================
819// move mouse on top of set2, bar0 to check the index (hover out set1,
820// hover in set1)
821 QTest::mouseMove(widget: view.viewport(), pos: QPoint(layout.at(i: 1).x() + 1, layout.at(i: 1).y() + 1));
822
823 QTRY_COMPARE(seriesIndexSpy.count(), 2);
824 QTRY_COMPARE(setIndexSpy1.count(), 1);
825 QTRY_COMPARE(setIndexSpy2.count(), 1);
826
827 // should leave set1, bar0
828 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
829 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
830 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
831 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
832 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
833 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
834
835 setIndexSpyArg = setIndexSpy1.takeFirst();
836 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
837 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
838 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
839 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
840
841 // should enter set2, bar0
842 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
843 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
844 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
845 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
846 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
847 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
848
849 setIndexSpyArg = setIndexSpy2.takeFirst();
850 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
851 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
852 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
853 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
854
855//=======================================================================
856// move mouse on top of set1, bar1 to check the index (hover out set 2,
857// hover in set1)
858 QTest::mouseMove(widget: view.viewport(), pos: QPoint(layout.at(i: 2).x() + 1, layout.at(i: 2).y() + 1));
859
860 QTRY_COMPARE(seriesIndexSpy.count(), 2);
861 QTRY_COMPARE(setIndexSpy1.count(), 1);
862 QTRY_COMPARE(setIndexSpy2.count(), 1);
863
864 // should leave set2, bar0
865 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
866 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
867 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
868 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
869 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
870 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
871
872 setIndexSpyArg = setIndexSpy2.takeFirst();
873 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
874 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
875 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
876 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
877
878 // should enter set1, bar1
879 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
880 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
881 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
882 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
883 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
884 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
885
886 setIndexSpyArg = setIndexSpy1.takeFirst();
887 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
888 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
889 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
890 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
891
892//=======================================================================
893// move mouse on top of view between set1 and set2 to check the index
894// (hover out set1)
895 QTest::mouseMove(widget: view.viewport(), pos: QPoint((layout.at(i: 2).right() + layout.at(i: 3).left()) / 2, 0));
896
897 QTRY_COMPARE(seriesIndexSpy.count(), 1);
898 QTRY_COMPARE(setIndexSpy1.count(), 1);
899 QTRY_COMPARE(setIndexSpy2.count(), 0);
900
901 // should leave set1, bar1
902 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
903 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
904 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
905 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
906 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
907 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
908
909 setIndexSpyArg = setIndexSpy1.takeFirst();
910 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
911 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
912 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
913 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
914
915//=======================================================================
916// move mouse on top of set2, bar1 to check the index (hover in set2)
917 QTest::mouseMove(widget: view.viewport(), pos: QPoint(layout.at(i: 3).x() + 1, layout.at(i: 3).y() + 1));
918
919 QTRY_COMPARE(seriesIndexSpy.count(), 1);
920 QTRY_COMPARE(setIndexSpy1.count(), 0);
921 QTRY_COMPARE(setIndexSpy2.count(), 1);
922
923 // should enter set2, bar1
924 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
925 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
926 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
927 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
928 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
929 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
930
931 setIndexSpyArg = setIndexSpy2.takeFirst();
932 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
933 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
934 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
935 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
936
937//=======================================================================
938// move mouse on top of the view between bar1 and bar2 to check the index
939//(hover out set2)
940 QTest::mouseMove(widget: view.viewport(), pos: QPoint((layout.at(i: 3).right() + layout.at(i: 3).left()) / 2, 0));
941
942 QTRY_COMPARE(seriesIndexSpy.count(), 1);
943 QTRY_COMPARE(setIndexSpy1.count(), 0);
944 QTRY_COMPARE(setIndexSpy2.count(), 1);
945
946 // should leave set2, bar1
947 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
948 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
949 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
950 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
951 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
952 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
953
954 setIndexSpyArg = setIndexSpy2.takeFirst();
955 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
956 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
957 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
958 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
959}
960
961void tst_QBarSeries::clearWithAnimations()
962{
963 QBarSeries* series = new QBarSeries();
964
965 QBarSet* set1 = new QBarSet(QString("set 1"));
966 *set1 << 10 << 10 << 10;
967 series->append(set: set1);
968
969 QBarSet* set2 = new QBarSet(QString("set 2"));
970 *set2 << 10 << 10 << 10;
971 series->append(set: set2);
972
973 QChartView view(new QChart());
974 view.resize(w: 400,h: 300);
975 view.chart()->setAnimationOptions(QChart::SeriesAnimations);
976 view.chart()->addSeries(series);
977 view.show();
978
979 series->clear();
980}
981
982void tst_QBarSeries::destruction()
983{
984 // add a barset
985 QBarSeries *series = new QBarSeries();
986 QBarSet *set = new QBarSet("testset");
987 QSignalSpy spy1(set, SIGNAL(destroyed()));
988 series->append(set);
989
990 // delete the series
991 delete series;
992
993 // check that series deletes the set
994 QCOMPARE(spy1.count(), 1);
995}
996
997void tst_QBarSeries::mousePressed()
998{
999 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1000
1001 QBarSeries* series = new QBarSeries();
1002
1003 QBarSet* set1 = new QBarSet(QString("set 1"));
1004 *set1 << 10 << 10 << 10;
1005 series->append(set: set1);
1006
1007 QBarSet* set2 = new QBarSet(QString("set 2"));
1008 *set2 << 10 << 10 << 10;
1009 series->append(set: set2);
1010 QList<QBarSet*> barSets = series->barSets();
1011
1012 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
1013 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
1014 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
1015
1016 QChartView view(new QChart());
1017 view.resize(w: 400,h: 300);
1018 view.chart()->addSeries(series);
1019 view.show();
1020 QVERIFY(QTest::qWaitForWindowExposed(&view));
1021
1022 // Calculate expected layout for bars
1023 QRectF plotArea = view.chart()->plotArea();
1024 qreal width = plotArea.width();
1025 qreal height = plotArea.height();
1026 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
1027 qreal rangeX = 3; // 3 values per set
1028 qreal scaleY = (height / rangeY);
1029 qreal scaleX = (width / rangeX);
1030
1031 qreal setCount = series->count();
1032 qreal domainMinY = 0; // These come from internal domain used by barseries.
1033 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
1034 qreal rectWidth = (scaleX / setCount) * series->barWidth();
1035
1036 QVector<QRectF> layout;
1037
1038 // 3 = count of values in set
1039 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1040 for (int i = 0; i < 3; i++) {
1041 qreal yPos = height + scaleY * domainMinY + plotArea.top();
1042 for (int set = 0; set < setCount; set++) {
1043 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
1044 xPos -= series->count()*rectWidth/2;
1045 xPos += set*rectWidth;
1046
1047 qreal rectHeight = barSets.at(i: set)->at(index: i) * scaleY;
1048 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1049 layout.append(t: rect);
1050 }
1051 }
1052
1053//====================================================================================
1054// barset 1, bar 0
1055 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 0).center().toPoint());
1056 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1057
1058 QCOMPARE(seriesSpy.count(), 1);
1059 QCOMPARE(setSpy1.count(), 1);
1060 QCOMPARE(setSpy2.count(), 0);
1061
1062 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1063 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1064 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1065 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1066
1067 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1068 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1069 QVERIFY(setSpyArg.at(0).toInt() == 0);
1070
1071//====================================================================================
1072// barset 1, bar 1
1073 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 2).center().toPoint());
1074 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1075
1076 QCOMPARE(seriesSpy.count(), 1);
1077 QCOMPARE(setSpy1.count(), 1);
1078 QCOMPARE(setSpy2.count(), 0);
1079
1080 seriesSpyArg = seriesSpy.takeFirst();
1081 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1082 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1083 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1084
1085 setSpyArg = setSpy1.takeFirst();
1086 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1087 QVERIFY(setSpyArg.at(0).toInt() == 1);
1088
1089//====================================================================================
1090// barset 1, bar 2
1091 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 4).center().toPoint());
1092 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1093
1094 QCOMPARE(seriesSpy.count(), 1);
1095 QCOMPARE(setSpy1.count(), 1);
1096 QCOMPARE(setSpy2.count(), 0);
1097
1098 seriesSpyArg = seriesSpy.takeFirst();
1099 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1100 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1101 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1102
1103 setSpyArg = setSpy1.takeFirst();
1104 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1105 QVERIFY(setSpyArg.at(0).toInt() == 2);
1106
1107//====================================================================================
1108// barset 2, bar 0
1109 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 1).center().toPoint());
1110 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1111
1112 QCOMPARE(seriesSpy.count(), 1);
1113 QCOMPARE(setSpy1.count(), 0);
1114 QCOMPARE(setSpy2.count(), 1);
1115
1116 seriesSpyArg = seriesSpy.takeFirst();
1117 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1118 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1119 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1120
1121 setSpyArg = setSpy2.takeFirst();
1122 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1123 QVERIFY(setSpyArg.at(0).toInt() == 0);
1124
1125//====================================================================================
1126// barset 2, bar 1
1127 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 3).center().toPoint());
1128 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1129
1130 QCOMPARE(seriesSpy.count(), 1);
1131 QCOMPARE(setSpy1.count(), 0);
1132 QCOMPARE(setSpy2.count(), 1);
1133
1134 seriesSpyArg = seriesSpy.takeFirst();
1135 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1136 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1137 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1138
1139 setSpyArg = setSpy2.takeFirst();
1140 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1141 QVERIFY(setSpyArg.at(0).toInt() == 1);
1142
1143//====================================================================================
1144// barset 2, bar 2
1145 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 5).center().toPoint());
1146 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1147
1148 QCOMPARE(seriesSpy.count(), 1);
1149 QCOMPARE(setSpy1.count(), 0);
1150 QCOMPARE(setSpy2.count(), 1);
1151
1152 seriesSpyArg = seriesSpy.takeFirst();
1153 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1154 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1155 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1156
1157 setSpyArg = setSpy2.takeFirst();
1158 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1159 QVERIFY(setSpyArg.at(0).toInt() == 2);
1160}
1161
1162void tst_QBarSeries::mouseReleased()
1163{
1164 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1165
1166 QBarSeries* series = new QBarSeries();
1167
1168 QBarSet* set1 = new QBarSet(QString("set 1"));
1169 *set1 << 10 << 10 << 10;
1170 series->append(set: set1);
1171
1172 QBarSet* set2 = new QBarSet(QString("set 2"));
1173 *set2 << 10 << 10 << 10;
1174 series->append(set: set2);
1175 QList<QBarSet*> barSets = series->barSets();
1176
1177 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
1178 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
1179 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
1180
1181 QChartView view(new QChart());
1182 view.resize(w: 400,h: 300);
1183 view.chart()->addSeries(series);
1184 view.show();
1185 QVERIFY(QTest::qWaitForWindowExposed(&view));
1186
1187 // Calculate expected layout for bars
1188 QRectF plotArea = view.chart()->plotArea();
1189 qreal width = plotArea.width();
1190 qreal height = plotArea.height();
1191 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
1192 qreal rangeX = 3; // 3 values per set
1193 qreal scaleY = (height / rangeY);
1194 qreal scaleX = (width / rangeX);
1195
1196 qreal setCount = series->count();
1197 qreal domainMinY = 0; // These come from internal domain used by barseries.
1198 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
1199 qreal rectWidth = (scaleX / setCount) * series->barWidth();
1200
1201 QVector<QRectF> layout;
1202
1203 // 3 = count of values in set
1204 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1205 for (int i = 0; i < 3; i++) {
1206 qreal yPos = height + scaleY * domainMinY + plotArea.top();
1207 for (int set = 0; set < setCount; set++) {
1208 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
1209 xPos -= series->count()*rectWidth/2;
1210 xPos += set*rectWidth;
1211
1212 qreal rectHeight = barSets.at(i: set)->at(index: i) * scaleY;
1213 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1214 layout.append(t: rect);
1215 }
1216 }
1217
1218//====================================================================================
1219// barset 1, bar 0
1220 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 0).center().toPoint());
1221 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1222
1223 QCOMPARE(seriesSpy.count(), 1);
1224 QCOMPARE(setSpy1.count(), 1);
1225 QCOMPARE(setSpy2.count(), 0);
1226
1227 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1228 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1229 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1230 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1231
1232 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1233 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1234 QVERIFY(setSpyArg.at(0).toInt() == 0);
1235
1236//====================================================================================
1237// barset 1, bar 1
1238 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 2).center().toPoint());
1239 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1240
1241 QCOMPARE(seriesSpy.count(), 1);
1242 QCOMPARE(setSpy1.count(), 1);
1243 QCOMPARE(setSpy2.count(), 0);
1244
1245 seriesSpyArg = seriesSpy.takeFirst();
1246 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1247 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1248 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1249
1250 setSpyArg = setSpy1.takeFirst();
1251 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1252 QVERIFY(setSpyArg.at(0).toInt() == 1);
1253
1254//====================================================================================
1255// barset 1, bar 2
1256 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 4).center().toPoint());
1257 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1258
1259 QCOMPARE(seriesSpy.count(), 1);
1260 QCOMPARE(setSpy1.count(), 1);
1261 QCOMPARE(setSpy2.count(), 0);
1262
1263 seriesSpyArg = seriesSpy.takeFirst();
1264 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1265 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1266 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1267
1268 setSpyArg = setSpy1.takeFirst();
1269 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1270 QVERIFY(setSpyArg.at(0).toInt() == 2);
1271
1272//====================================================================================
1273// barset 2, bar 0
1274 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 1).center().toPoint());
1275 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1276
1277 QCOMPARE(seriesSpy.count(), 1);
1278 QCOMPARE(setSpy1.count(), 0);
1279 QCOMPARE(setSpy2.count(), 1);
1280
1281 seriesSpyArg = seriesSpy.takeFirst();
1282 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1283 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1284 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1285
1286 setSpyArg = setSpy2.takeFirst();
1287 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1288 QVERIFY(setSpyArg.at(0).toInt() == 0);
1289
1290//====================================================================================
1291// barset 2, bar 1
1292 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 3).center().toPoint());
1293 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1294
1295 QCOMPARE(seriesSpy.count(), 1);
1296 QCOMPARE(setSpy1.count(), 0);
1297 QCOMPARE(setSpy2.count(), 1);
1298
1299 seriesSpyArg = seriesSpy.takeFirst();
1300 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1301 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1302 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1303
1304 setSpyArg = setSpy2.takeFirst();
1305 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1306 QVERIFY(setSpyArg.at(0).toInt() == 1);
1307
1308//====================================================================================
1309// barset 2, bar 2
1310 QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 5).center().toPoint());
1311 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1312
1313 QCOMPARE(seriesSpy.count(), 1);
1314 QCOMPARE(setSpy1.count(), 0);
1315 QCOMPARE(setSpy2.count(), 1);
1316
1317 seriesSpyArg = seriesSpy.takeFirst();
1318 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1319 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1320 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1321
1322 setSpyArg = setSpy2.takeFirst();
1323 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1324 QVERIFY(setSpyArg.at(0).toInt() == 2);
1325}
1326
1327void tst_QBarSeries::mouseDoubleClicked()
1328{
1329 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1330
1331 QBarSeries* series = new QBarSeries();
1332
1333 QBarSet* set1 = new QBarSet(QString("set 1"));
1334 *set1 << 10 << 10 << 10;
1335 series->append(set: set1);
1336
1337 QBarSet* set2 = new QBarSet(QString("set 2"));
1338 *set2 << 10 << 10 << 10;
1339 series->append(set: set2);
1340 QList<QBarSet*> barSets = series->barSets();
1341
1342 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
1343 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
1344 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
1345
1346 QChartView view(new QChart());
1347 view.resize(w: 400,h: 300);
1348 view.chart()->addSeries(series);
1349 view.show();
1350 QVERIFY(QTest::qWaitForWindowExposed(&view));
1351
1352 // Calculate expected layout for bars
1353 QRectF plotArea = view.chart()->plotArea();
1354 qreal width = plotArea.width();
1355 qreal height = plotArea.height();
1356 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
1357 qreal rangeX = 3; // 3 values per set
1358 qreal scaleY = (height / rangeY);
1359 qreal scaleX = (width / rangeX);
1360
1361 qreal setCount = series->count();
1362 qreal domainMinY = 0; // These come from internal domain used by barseries.
1363 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
1364 qreal rectWidth = (scaleX / setCount) * series->barWidth();
1365
1366 QVector<QRectF> layout;
1367
1368 // 3 = count of values in set
1369 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1370 for (int i = 0; i < 3; i++) {
1371 qreal yPos = height + scaleY * domainMinY + plotArea.top();
1372 for (int set = 0; set < setCount; set++) {
1373 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
1374 xPos -= series->count()*rectWidth/2;
1375 xPos += set*rectWidth;
1376
1377 qreal rectHeight = barSets.at(i: set)->at(index: i) * scaleY;
1378 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1379 layout.append(t: rect);
1380 }
1381 }
1382
1383 // barset 1, bar 0
1384 QTest::mouseDClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: layout.at(i: 0).center().toPoint());
1385 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000);
1386
1387 QCOMPARE(seriesSpy.count(), 1);
1388 QCOMPARE(setSpy1.count(), 1);
1389 QCOMPARE(setSpy2.count(), 0);
1390
1391 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1392 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1393 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1394 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1395
1396 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1397 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1398 QVERIFY(setSpyArg.at(0).toInt() == 0);
1399}
1400
1401QTEST_MAIN(tst_QBarSeries)
1402
1403#include "tst_qbarseries.moc"
1404
1405

source code of qtcharts/tests/auto/qbarseries/tst_qbarseries.cpp