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

source code of qtcharts/tests/auto/qhorizontalbarseries/tst_qhorizontalbarseries.cpp