|
|
/****************************************************************************
|
|
|
**
|
|
|
** Copyright (C) 2013 Digia Plc
|
|
|
** All rights reserved.
|
|
|
** For any questions to Digia, please use contact form at http://qt.digia.com
|
|
|
**
|
|
|
** This file is part of the Qt Enterprise Charts Add-on.
|
|
|
**
|
|
|
** $QT_BEGIN_LICENSE$
|
|
|
** Licensees holding valid Qt Enterprise licenses may use this file in
|
|
|
** accordance with the Qt Enterprise License Agreement provided with the
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
** a written agreement between you and Digia.
|
|
|
**
|
|
|
** If you have questions regarding the use of this file, please use
|
|
|
** contact form at http://qt.digia.com
|
|
|
** $QT_END_LICENSE$
|
|
|
**
|
|
|
****************************************************************************/
|
|
|
|
|
|
#include <QtTest/QtTest>
|
|
|
#include <qstackedbarseries.h>
|
|
|
#include <qbarset.h>
|
|
|
#include <qchartview.h>
|
|
|
#include <qchart.h>
|
|
|
#include "tst_definitions.h"
|
|
|
|
|
|
QTCOMMERCIALCHART_USE_NAMESPACE
|
|
|
|
|
|
Q_DECLARE_METATYPE(QBarSet*)
|
|
|
|
|
|
class tst_QStackedBarSeries : public QObject
|
|
|
{
|
|
|
Q_OBJECT
|
|
|
|
|
|
public slots:
|
|
|
void initTestCase();
|
|
|
void cleanupTestCase();
|
|
|
void init();
|
|
|
void cleanup();
|
|
|
|
|
|
private slots:
|
|
|
void qstackedbarseries_data();
|
|
|
void qstackedbarseries();
|
|
|
void type_data();
|
|
|
void type();
|
|
|
void mouseclicked_data();
|
|
|
void mouseclicked();
|
|
|
void mousehovered_data();
|
|
|
void mousehovered();
|
|
|
|
|
|
private:
|
|
|
QStackedBarSeries* m_barseries;
|
|
|
};
|
|
|
|
|
|
void tst_QStackedBarSeries::initTestCase()
|
|
|
{
|
|
|
qRegisterMetaType<QBarSet*>("QBarSet*");
|
|
|
}
|
|
|
|
|
|
void tst_QStackedBarSeries::cleanupTestCase()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
void tst_QStackedBarSeries::init()
|
|
|
{
|
|
|
m_barseries = new QStackedBarSeries();
|
|
|
}
|
|
|
|
|
|
void tst_QStackedBarSeries::cleanup()
|
|
|
{
|
|
|
delete m_barseries;
|
|
|
m_barseries = 0;
|
|
|
}
|
|
|
|
|
|
void tst_QStackedBarSeries::qstackedbarseries_data()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
void tst_QStackedBarSeries::qstackedbarseries()
|
|
|
{
|
|
|
QStackedBarSeries *barseries = new QStackedBarSeries();
|
|
|
QVERIFY(barseries != 0);
|
|
|
}
|
|
|
|
|
|
void tst_QStackedBarSeries::type_data()
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
void tst_QStackedBarSeries::type()
|
|
|
{
|
|
|
QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeStackedBar);
|
|
|
}
|
|
|
|
|
|
void tst_QStackedBarSeries::mouseclicked_data()
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
void tst_QStackedBarSeries::mouseclicked()
|
|
|
{
|
|
|
SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
|
|
|
|
|
|
QStackedBarSeries* series = new QStackedBarSeries();
|
|
|
|
|
|
QBarSet* set1 = new QBarSet(QString("set 1"));
|
|
|
*set1 << 10 << 10 << 10;
|
|
|
series->append(set1);
|
|
|
|
|
|
QBarSet* set2 = new QBarSet(QString("set 2"));
|
|
|
*set2 << 10 << 10 << 10;
|
|
|
series->append(set2);
|
|
|
|
|
|
QList<QBarSet*> barSets = series->barSets();
|
|
|
|
|
|
QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
|
|
|
|
|
|
QChartView view(new QChart());
|
|
|
view.resize(400,300);
|
|
|
view.chart()->addSeries(series);
|
|
|
view.show();
|
|
|
QTest::qWaitForWindowShown(&view);
|
|
|
|
|
|
// Calculate expected layout for bars
|
|
|
QRectF plotArea = view.chart()->plotArea();
|
|
|
qreal width = plotArea.width();
|
|
|
qreal height = plotArea.height();
|
|
|
qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
|
|
|
qreal rangeX = 3; // 3 values per set
|
|
|
qreal scaleY = (height / rangeY);
|
|
|
qreal scaleX = (width / rangeX);
|
|
|
|
|
|
qreal setCount = series->count();
|
|
|
qreal domainMinY = 0; // These come from internal domain used by barseries.
|
|
|
qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
|
|
|
qreal rectWidth = scaleX * series->barWidth();
|
|
|
|
|
|
QVector<QRectF> layout;
|
|
|
|
|
|
// 3 = count of values in set
|
|
|
// Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
qreal yMax = height + scaleY * domainMinY + plotArea.top();
|
|
|
qreal yMin = height + scaleY * domainMinY + plotArea.top();
|
|
|
for (int set = 0; set < setCount; set++) {
|
|
|
qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
|
|
|
qreal rectHeight = barSets.at(set)->at(i) * scaleY;
|
|
|
if (rectHeight < 0) {
|
|
|
QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
|
|
|
layout.append(rect);
|
|
|
yMax -= rectHeight;
|
|
|
} else {
|
|
|
QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
|
|
|
layout.append(rect);
|
|
|
yMin -= rectHeight;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//====================================================================================
|
|
|
// barset 1, bar 0
|
|
|
QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
|
|
|
|
|
|
QCOMPARE(seriesSpy.count(), 1);
|
|
|
|
|
|
QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
|
|
|
QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
|
|
|
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
|
|
|
QVERIFY(seriesSpyArg.at(0).toInt() == 0);
|
|
|
|
|
|
//====================================================================================
|
|
|
// barset 1, bar 1
|
|
|
QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
|
|
|
|
|
|
QCOMPARE(seriesSpy.count(), 1);
|
|
|
|
|
|
seriesSpyArg = seriesSpy.takeFirst();
|
|
|
QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
|
|
|
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
|
|
|
QVERIFY(seriesSpyArg.at(0).toInt() == 1);
|
|
|
|
|
|
//====================================================================================
|
|
|
// barset 1, bar 2
|
|
|
QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
|
|
|
|
|
|
QCOMPARE(seriesSpy.count(), 1);
|
|
|
|
|
|
seriesSpyArg = seriesSpy.takeFirst();
|
|
|
QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
|
|
|
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
|
|
|
QVERIFY(seriesSpyArg.at(0).toInt() == 2);
|
|
|
|
|
|
//====================================================================================
|
|
|
// barset 2, bar 0
|
|
|
QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
|
|
|
|
|
|
QCOMPARE(seriesSpy.count(), 1);
|
|
|
|
|
|
seriesSpyArg = seriesSpy.takeFirst();
|
|
|
QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
|
|
|
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
|
|
|
QVERIFY(seriesSpyArg.at(0).toInt() == 0);
|
|
|
|
|
|
//====================================================================================
|
|
|
// barset 2, bar 1
|
|
|
QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
|
|
|
|
|
|
QCOMPARE(seriesSpy.count(), 1);
|
|
|
|
|
|
seriesSpyArg = seriesSpy.takeFirst();
|
|
|
QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
|
|
|
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
|
|
|
QVERIFY(seriesSpyArg.at(0).toInt() == 1);
|
|
|
|
|
|
//====================================================================================
|
|
|
// barset 2, bar 2
|
|
|
QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
|
|
|
|
|
|
QCOMPARE(seriesSpy.count(), 1);
|
|
|
|
|
|
seriesSpyArg = seriesSpy.takeFirst();
|
|
|
QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
|
|
|
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
|
|
|
QVERIFY(seriesSpyArg.at(0).toInt() == 2);
|
|
|
}
|
|
|
|
|
|
void tst_QStackedBarSeries::mousehovered_data()
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
void tst_QStackedBarSeries::mousehovered()
|
|
|
{
|
|
|
SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
|
|
|
|
|
|
QStackedBarSeries* series = new QStackedBarSeries();
|
|
|
|
|
|
QBarSet* set1 = new QBarSet(QString("set 1"));
|
|
|
*set1 << 10 << 10 << 10;
|
|
|
series->append(set1);
|
|
|
|
|
|
QBarSet* set2 = new QBarSet(QString("set 2"));
|
|
|
*set2 << 10 << 10 << 10;
|
|
|
series->append(set2);
|
|
|
|
|
|
QList<QBarSet*> barSets = series->barSets();
|
|
|
|
|
|
QSignalSpy seriesSpy(series,SIGNAL(hovered(bool,QBarSet*)));
|
|
|
|
|
|
QChartView view(new QChart());
|
|
|
view.resize(400,300);
|
|
|
view.chart()->addSeries(series);
|
|
|
view.show();
|
|
|
QTest::qWaitForWindowShown(&view);
|
|
|
|
|
|
//this is hack since view does not get events otherwise
|
|
|
view.setMouseTracking(true);
|
|
|
|
|
|
// Calculate expected layout for bars
|
|
|
QRectF plotArea = view.chart()->plotArea();
|
|
|
qreal width = plotArea.width();
|
|
|
qreal height = plotArea.height();
|
|
|
qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
|
|
|
qreal rangeX = 3; // 3 values per set
|
|
|
qreal scaleY = (height / rangeY);
|
|
|
qreal scaleX = (width / rangeX);
|
|
|
|
|
|
qreal setCount = series->count();
|
|
|
qreal domainMinY = 0; // These come from internal domain used by barseries.
|
|
|
qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
|
|
|
qreal rectWidth = scaleX * series->barWidth();
|
|
|
|
|
|
QVector<QRectF> layout;
|
|
|
|
|
|
// 3 = count of values in set
|
|
|
// Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
qreal yMax = height + scaleY * domainMinY + plotArea.top();
|
|
|
qreal yMin = height + scaleY * domainMinY + plotArea.top();
|
|
|
for (int set = 0; set < setCount; set++) {
|
|
|
qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
|
|
|
qreal rectHeight = barSets.at(set)->at(i) * scaleY;
|
|
|
|
|
|
if (rectHeight < 0) {
|
|
|
QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
|
|
|
layout.append(rect);
|
|
|
yMax -= rectHeight;
|
|
|
} else {
|
|
|
QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
|
|
|
layout.append(rect);
|
|
|
yMin -= rectHeight;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//=======================================================================
|
|
|
// move mouse to left border
|
|
|
QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
|
|
|
TRY_COMPARE(seriesSpy.count(), 0);
|
|
|
|
|
|
//=======================================================================
|
|
|
// move mouse on top of set1
|
|
|
QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
|
|
|
TRY_COMPARE(seriesSpy.count(), 1);
|
|
|
|
|
|
QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
|
|
|
QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
|
|
|
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
|
|
|
QVERIFY(seriesSpyArg.at(0).toBool() == true);
|
|
|
|
|
|
//=======================================================================
|
|
|
// move mouse from top of set1 to top of set2
|
|
|
QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
|
|
|
TRY_COMPARE(seriesSpy.count(), 2);
|
|
|
|
|
|
// should leave set1
|
|
|
seriesSpyArg = seriesSpy.takeFirst();
|
|
|
QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
|
|
|
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
|
|
|
QVERIFY(seriesSpyArg.at(0).toBool() == false);
|
|
|
|
|
|
// should enter set2
|
|
|
seriesSpyArg = seriesSpy.takeFirst();
|
|
|
QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
|
|
|
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
|
|
|
QVERIFY(seriesSpyArg.at(0).toBool() == true);
|
|
|
|
|
|
//=======================================================================
|
|
|
// move mouse from top of set2 to background
|
|
|
QTest::mouseMove(view.viewport(), QPoint(layout.at(1).center().x(), 0));
|
|
|
TRY_COMPARE(seriesSpy.count(), 1);
|
|
|
|
|
|
// should leave set2
|
|
|
seriesSpyArg = seriesSpy.takeFirst();
|
|
|
QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
|
|
|
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
|
|
|
QVERIFY(seriesSpyArg.at(0).toBool() == false);
|
|
|
}
|
|
|
|
|
|
QTEST_MAIN(tst_QStackedBarSeries)
|
|
|
|
|
|
#include "tst_qstackedbarseries.moc"
|
|
|
|
|
|
|