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