From 02c84d5a1cd8d9e2f249cd050db55af6d3d832e5 2012-03-29 16:32:42 From: Michal Klocek Date: 2012-03-29 16:32:42 Subject: [PATCH] Fix padding to be int, update unit test --- diff --git a/src/qchart.cpp b/src/qchart.cpp index c73da90..ffd5834 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -268,7 +268,7 @@ QLegend* QChart::legend() const return d_ptr->m_legend; } -QRectF QChart::padding() const +QRect QChart::padding() const { return d_ptr->m_padding; } @@ -342,7 +342,7 @@ m_titleItem(0), m_legend(0), m_dataset(0), m_presenter(0), -m_padding(QRectF(50,50,50,50)) +m_padding(QRect(50,50,0,0)) { } diff --git a/src/qchart.h b/src/qchart.h index aa5a034..d807658 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -99,14 +99,13 @@ public: QLegend* legend() const; - QRectF padding() const; + QRect padding() const; protected: void resizeEvent(QGraphicsSceneResizeEvent *event); protected: QScopedPointer d_ptr; - friend class QChartView; friend class QLegend; Q_DISABLE_COPY(QChart) }; diff --git a/src/qchart_p.h b/src/qchart_p.h index 40db1a8..7f74f3f 100644 --- a/src/qchart_p.h +++ b/src/qchart_p.h @@ -57,7 +57,7 @@ struct QChartPrivate QLegend* m_legend; ChartDataSet *m_dataset; ChartPresenter *m_presenter; - QRectF m_padding; + QRect m_padding; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartview.cpp b/src/qchartview.cpp index 155161b..3340368 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -59,7 +59,6 @@ QChartView::QChartView(QChart *chart,QWidget *parent) : { d_ptr->m_scene = new QGraphicsScene(this); d_ptr->m_chart = chart; - d_ptr->m_presenter = chart->d_ptr->m_presenter; setFrameShape(QFrame::NoFrame); setBackgroundRole(QPalette::Window); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); @@ -190,14 +189,26 @@ void QChartView::keyPressEvent(QKeyEvent *event) { switch (event->key()) { case Qt::Key_Plus: - d_ptr->m_chart->zoomIn(); - break; + d_ptr->m_chart->zoomIn(); + break; case Qt::Key_Minus: - d_ptr->m_chart->zoomOut(); - break; + d_ptr->m_chart->zoomOut(); + break; + case Qt::Key_Left: + d_ptr->m_chart->scrollLeft(); + break; + case Qt::Key_Right: + d_ptr->m_chart->scrollRight(); + break; + case Qt::Key_Up: + d_ptr->m_chart->scrollUp(); + break; + case Qt::Key_Down: + d_ptr->m_chart->scrollDown(); + break; default: - QGraphicsView::keyPressEvent(event); - break; + QGraphicsView::keyPressEvent(event); + break; } } diff --git a/test/auto/qchartview/tst_qchartview.cpp b/test/auto/qchartview/tst_qchartview.cpp index 8925fc4..778b949 100644 --- a/test/auto/qchartview/tst_qchartview.cpp +++ b/test/auto/qchartview/tst_qchartview.cpp @@ -29,6 +29,7 @@ QTCOMMERCIALCHART_USE_NAMESPACE Q_DECLARE_METATYPE(QChart*) Q_DECLARE_METATYPE(QChartView::RubberBands) +Q_DECLARE_METATYPE(Qt::Key) class tst_QChartView : public QObject { @@ -47,6 +48,8 @@ private Q_SLOTS: void chart(); void rubberBand_data(); void rubberBand(); + void keys_data(); + void keys(); private: QChartView* m_view; @@ -54,6 +57,8 @@ private: void tst_QChartView::initTestCase() { + //test tracks mouse, give a while to user to relese it + QTest::qWait(1000); } void tst_QChartView::cleanupTestCase() @@ -111,8 +116,8 @@ void tst_QChartView::rubberBand_data() QTest::addColumn("maxY"); QTest::newRow("HorizonalRubberBand") << QChartView::RubberBands(QChartView::HorizonalRubberBand) << 0 << 1 << 10 << 90 << 0<< 100; - QTest::newRow("VerticalRubberBand") << QChartView::RubberBands(QChartView::VerticalRubberBand) << 1 << 0 << 10 << 90 << 0<< 100; - QTest::newRow("RectangleRubberBand") << QChartView::RubberBands(QChartView::RectangleRubberBand) << 1 << 1 <<10 << 90 << 0<< 100; + QTest::newRow("VerticalRubberBand") << QChartView::RubberBands(QChartView::VerticalRubberBand) << 1 << 0 << 0 << 100 << 10<< 90; + QTest::newRow("RectangleRubberBand") << QChartView::RubberBands(QChartView::RectangleRubberBand) << 1 << 1 <<10 << 90 << 10<< 90; } void tst_QChartView::rubberBand() @@ -126,14 +131,17 @@ void tst_QChartView::rubberBand() QFETCH(int, maxY); m_view->setRubberBand(rubberBand); + QRect padding = m_view->chart()->padding(); QCOMPARE(m_view->rubberBand(), rubberBand); QLineSeries* line = new QLineSeries(); *line << QPointF(0, 0) << QPointF(100, 100); m_view->chart()->addSeries(line); - m_view->resize(1000, 1000); + m_view->resize(100 + padding.left() + padding.right(), 100 + padding.top()+ padding.bottom()); m_view->show(); + + //this is hack since view does not get events otherwise m_view->setMouseTracking(true); QChartAxis* axisY = m_view->chart()->axisY(); @@ -141,20 +149,66 @@ void tst_QChartView::rubberBand() QChartAxis* axisX = m_view->chart()->axisX(); QSignalSpy spy1(axisX, SIGNAL(rangeChanged(qreal, qreal))); + QTest::qWaitForWindowShown(m_view); - QTest::mouseMove(m_view->viewport(), QPoint(250, 250)); - QTest::mousePress(m_view->viewport(), Qt::LeftButton, 0, QPoint(250, 250)); - QTest::mouseMove(m_view->viewport(), QPoint(900, 900)); - QTest::qWait(3000); - QTest::mouseRelease(m_view->viewport(), Qt::LeftButton, 0, QPoint(900, 900)); - QTest::qWait(3000); + QTest::mouseMove(m_view->viewport(), QPoint(minX, minY) + padding.topLeft()); + + QTest::mousePress(m_view->viewport(), Qt::LeftButton, 0, QPoint(minX, minY) + padding.topLeft()); + QTest::mouseMove(m_view->viewport(), QPoint(maxX, maxY) + padding.topLeft()); + + QTest::mouseRelease(m_view->viewport(), Qt::LeftButton, 0, QPoint(maxX, maxY)+ padding.topLeft()); + QCOMPARE(spy0.count(), Xcount); QCOMPARE(spy1.count(), Ycount); -qDebug()<min(); - QVERIFY(int(ceil(axisX->min())) - minX < 1); - QVERIFY(int(ceil(axisX->max())) - maxX < 1); - QVERIFY(int(ceil(axisY->min())) - minY < 1); - QVERIFY(int(ceil(axisY->max())) - maxY < 1); + + //this is hack since view does not get events otherwise + m_view->setMouseTracking(false); + + QVERIFY(axisX->min() - minX < 1); + QVERIFY(axisX->max() - maxX < 1); + QVERIFY(axisY->min() - minY < 1); + QVERIFY(axisY->max() - maxY < 1); +} + +void tst_QChartView::keys_data() +{ + QTest::addColumn("key"); + QTest::addColumn("Xcount"); + QTest::addColumn("Ycount"); + QTest::newRow("Qt::Key_Plus") << Qt::Key_Plus << 1 << 1; + QTest::newRow("Qt::Key_Minus") << Qt::Key_Minus << 1 << 1; + QTest::newRow("Qt::Key_Up") << Qt::Key_Up << 0 << 1; + QTest::newRow("Qt::Key_Down") << Qt::Key_Down << 0 << 1; + QTest::newRow("Qt::Key_Left") << Qt::Key_Left << 1 << 0; + QTest::newRow("Qt::Key_Right") << Qt::Key_Right << 1 << 0; +} + +void tst_QChartView::keys() +{ + QFETCH(Qt::Key,key); + QFETCH(int, Xcount); + QFETCH(int, Ycount); + + QRect padding = m_view->chart()->padding(); + + QLineSeries* line = new QLineSeries(); + *line << QPointF(0, 0) << QPointF(100, 100); + + m_view->chart()->addSeries(line); + m_view->resize(100 + padding.left() + padding.right(), 100 + padding.top()+ padding.bottom()); + m_view->show(); + + QChartAxis* axisY = m_view->chart()->axisY(); + QSignalSpy spy0(axisY, SIGNAL(rangeChanged(qreal, qreal))); + QChartAxis* axisX = m_view->chart()->axisX(); + QSignalSpy spy1(axisX, SIGNAL(rangeChanged(qreal, qreal))); + + QTest::keyPress(m_view, key); + QTest::keyRelease(m_view, key); + + QCOMPARE(spy0.count(), Ycount); + QCOMPARE(spy1.count(), Xcount); + } QTEST_MAIN(tst_QChartView)