File indexing completed on 2024-05-12 04:20:14

0001 /**
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include <QtTest/QtTest>
0010 #include <QStandardItemModel>
0011 #include <QPointF>
0012 #include <QPair>
0013 #include <QString>
0014 #include <KChartChart>
0015 #include <KChartCartesianCoordinatePlane>
0016 #include <KChartBarDiagram>
0017 #include <KChartPlotter>
0018 #include <KChartGridAttributes>
0019 
0020 
0021 using namespace KChart;
0022 
0023 class NumericDataModel : public QStandardItemModel
0024 {
0025     Q_OBJECT
0026 public:
0027     void setYValues( const QList< qreal > &values )
0028     {
0029         min = QPointF( 0, 1e9 );
0030         max = QPointF( values.size() - 1, -1e9 );
0031         setRowCount( values.size() );
0032         setColumnCount( 1 );
0033         for ( int i = 0; i < values.size(); i++ ) {
0034             min.setY( qMin( min.y(), values[ i ] ) );
0035             max.setY( qMax( max.y(), values[ i ] ) );
0036 
0037             QModelIndex idx = index( i, 0 );
0038             QStandardItemModel::setData( idx, values[ i ] );
0039         }
0040     }
0041 
0042     void setXyValues( const QList< QPointF > &values )
0043     {
0044         min = QPointF( 1e9, 1e9 );
0045         max = QPointF( -1e9, -1e9 );
0046         setRowCount( values.size() );
0047         setColumnCount( 2 );
0048         for ( int i = 0; i < values.size(); i++ ) {
0049             min.setX( qMin( min.x(), values[ i ].x() ) );
0050             max.setX( qMax( max.x(), values[ i ].x() ) );
0051             min.setY( qMin( min.y(), values[ i ].y() ) );
0052             max.setY( qMax( max.y(), values[ i ].y() ) );
0053 
0054             QModelIndex idx = index( i, 0 );
0055             QStandardItemModel::setData( idx, values[ i ].x() );
0056             idx = index( i, 1 );
0057             QStandardItemModel::setData( idx, values[ i ].y() );
0058         }
0059     }
0060 
0061     QPointF min;
0062     QPointF max;
0063 };
0064 
0065 class TestCartesianPlanes : public QObject {
0066     Q_OBJECT
0067 private Q_SLOTS:
0068 
0069     void init();
0070     void cleanup();
0071     void testIntialOwnership();
0072     void testDiagramOwnership();
0073     void testIsometricScalingSettings();
0074     void testZoomFactorsSettings();
0075     void testRangeSettingsBars();
0076     void testRangeSettingsPlotter();
0077     void testGlobalGridAttributesSettings();
0078     void testGridAttributesSettings();
0079     void testAxesCalcModesSettings();
0080 
0081 private:
0082     void doTestRangeSettings( AbstractCartesianDiagram *diagram, const QPointF &min, const QPointF &max );
0083 
0084     Chart *m_chart;
0085     BarDiagram *m_bars;
0086     Plotter *m_plotter;
0087     CartesianCoordinatePlane *m_plane;
0088     NumericDataModel *m_model;
0089 
0090 };
0091 
0092 void TestCartesianPlanes::init()
0093 {
0094     m_chart = new Chart( nullptr );
0095     m_model = new NumericDataModel();
0096     m_model->setParent( m_chart );
0097     m_bars = new BarDiagram( m_chart );
0098     m_bars->setModel( m_model );
0099     m_plotter = new Plotter( m_chart );
0100     m_plotter->setModel( m_model );
0101     m_plane = new CartesianCoordinatePlane( m_chart );
0102     m_chart->addCoordinatePlane( m_plane );
0103     m_plane->setReferenceCoordinatePlane( m_chart->coordinatePlane() );
0104     qDebug() << m_plotter->datasetDimension();
0105 }
0106 
0107 void TestCartesianPlanes::cleanup()
0108 {
0109     delete m_chart;
0110 }
0111 
0112 
0113 void TestCartesianPlanes::testIntialOwnership()
0114 {
0115     AbstractCoordinatePlane *plane = m_chart->coordinatePlane();
0116     QCOMPARE( m_plane->referenceCoordinatePlane(), m_chart->coordinatePlane() );
0117     m_chart->takeCoordinatePlane( nullptr );
0118     delete plane;
0119     QCOMPARE( m_plane->referenceCoordinatePlane(), (AbstractCoordinatePlane*)nullptr );
0120 }
0121 
0122 void TestCartesianPlanes::testDiagramOwnership()
0123 {
0124     m_plane->addDiagram( m_bars );
0125     QCOMPARE( m_plane->diagrams().size(), 1 );
0126     m_plane->addDiagram( m_plotter );
0127     QCOMPARE( m_plane->diagrams().size(), 2 );
0128     QCOMPARE( dynamic_cast< BarDiagram * >( m_plane->diagram() ), m_bars );
0129     m_plane->takeDiagram( m_bars );
0130     QCOMPARE( m_plane->diagrams().size(), 1 );
0131     QCOMPARE( dynamic_cast< Plotter * >( m_plane->diagram() ), m_plotter );
0132     m_plane->replaceDiagram( m_bars,  m_plotter );
0133     QCOMPARE( m_plane->diagrams().size(), 1 );
0134     QCOMPARE( dynamic_cast< BarDiagram * >( m_plane->diagram() ), m_bars );
0135     m_plane->takeDiagram( m_bars );
0136     QCOMPARE( m_plane->diagrams().size(), 0 );
0137     delete m_bars;
0138 }
0139 
0140 void TestCartesianPlanes::testIsometricScalingSettings()
0141 {
0142     QVERIFY( m_plane->doesIsometricScaling() == false );
0143     m_plane->setIsometricScaling( true );
0144     QVERIFY( m_plane->doesIsometricScaling() == true );
0145 }
0146 
0147 void TestCartesianPlanes::testZoomFactorsSettings()
0148 {
0149     QCOMPARE( m_plane->zoomFactorX(), 1.0 );
0150     QCOMPARE( m_plane->zoomFactorY(), 1.0 );
0151     QCOMPARE( m_plane->zoomCenter(), QPointF( 0.5, 0.5 ) );
0152     m_plane->setZoomFactorX( 1.5 );
0153     m_plane->setZoomFactorY( 1.5 );
0154     m_plane->setZoomCenter( QPointF( 1.0, 1.0 ) );
0155     QCOMPARE( m_plane->zoomFactorX(), 1.5 );
0156     QCOMPARE( m_plane->zoomFactorY(), 1.5 );
0157     QCOMPARE( m_plane->zoomCenter(), QPointF( 1.0, 1.0 ) );
0158 }
0159 
0160 void TestCartesianPlanes::doTestRangeSettings( AbstractCartesianDiagram *diagram,
0161                                                const QPointF &min, const QPointF &max )
0162 {
0163     m_plane->addDiagram( diagram );
0164     // the range is null when auto adjustment is turned off - check that...
0165     m_plane->setAutoAdjustHorizontalRangeToData( 100 );
0166     m_plane->setAutoAdjustVerticalRangeToData( 100 );
0167 
0168     {
0169         const QPair< qreal, qreal > hrange = m_plane->horizontalRange();
0170         const QPair< qreal, qreal > vrange = m_plane->verticalRange();
0171         QCOMPARE( hrange.first, qreal( 0.0 ) );
0172         QCOMPARE( hrange.second, qreal( 0.0 ) );
0173         QCOMPARE( vrange.first, qreal( 0.0 ) );
0174         QCOMPARE( vrange.second, qreal( 0.0 ) );
0175     }
0176 
0177     // now check that auto adjustment works when enabled
0178     m_plane->setAutoAdjustHorizontalRangeToData( 67 );
0179     m_plane->setAutoAdjustVerticalRangeToData( 67 );
0180     {
0181         const QPair< qreal, qreal > hrange = m_plane->horizontalRange();
0182         const QPair< qreal, qreal > vrange = m_plane->verticalRange();
0183         QCOMPARE( hrange.first, min.x() );
0184         QCOMPARE( hrange.second, max.x() );
0185         QCOMPARE( vrange.first, min.y() );
0186         QCOMPARE( vrange.second, max.y() );
0187     }
0188 
0189     {
0190         QPair< qreal, qreal> hboundaries( diagram->dataBoundaries().first.x(),
0191                                           diagram->dataBoundaries().second.x() );
0192         QPair< qreal, qreal> vboundaries( int( diagram->dataBoundaries().first.y() + 0.5 ),
0193                                           int( diagram->dataBoundaries().second.y() + 0.5 ) );
0194         m_plane->setHorizontalRange( hboundaries );
0195         m_plane->setVerticalRange( vboundaries );
0196         const QPair< qreal, qreal > newhb = m_plane->horizontalRange();
0197         const QPair< qreal, qreal > newvb = m_plane->verticalRange();
0198         QCOMPARE( newhb.first, hboundaries.first );
0199         QCOMPARE( newhb.second, hboundaries.second );
0200         QCOMPARE( newvb.first, vboundaries.first );
0201         QCOMPARE( newvb.second, vboundaries.second );
0202     }
0203 }
0204 
0205 void TestCartesianPlanes::testRangeSettingsBars()
0206 {
0207     QList< qreal > points;
0208     points << 40 << 45 << 42 << 34 << 34;
0209     m_model->setYValues( points );
0210     // data point "0" is shown at "0.5" in bar diagrams, which requires some data range hackery.
0211     // we correct for that like so:
0212     m_model->max.rx() += 1;
0213 
0214     doTestRangeSettings( m_bars, m_model->min, m_model->max );
0215 }
0216 
0217 void TestCartesianPlanes::testRangeSettingsPlotter()
0218 {
0219     QList< QPointF > points;
0220     points << QPointF( 0.0, 40.0 ) << QPointF( 1.0, 45.0 ) << QPointF( 2.0, 42.0 )
0221            << QPointF( 3.0, 34.0 ) << QPointF( 4.0, 34.0 );
0222     m_model->setXyValues( points );
0223     doTestRangeSettings( m_plotter, m_model->min, m_model->max );
0224 }
0225 
0226 void TestCartesianPlanes::testGlobalGridAttributesSettings()
0227 {
0228     GridAttributes ga = m_plane->globalGridAttributes();
0229     QVERIFY( ga.isGridVisible() == true );
0230     ga.setGridVisible( false );
0231     m_plane->setGlobalGridAttributes( ga );
0232     QVERIFY( m_plane->globalGridAttributes().isGridVisible() == false );
0233     //reset to normal
0234     ga.setGridVisible( true );
0235     QVERIFY( m_plane->globalGridAttributes().isGridVisible() == false );
0236     m_plane->setGlobalGridAttributes( ga );
0237     QVERIFY( m_plane->globalGridAttributes().isGridVisible() == true );
0238 }
0239 
0240 void TestCartesianPlanes::testGridAttributesSettings()
0241 {
0242     GridAttributes gh = m_plane->gridAttributes( Qt::Horizontal );
0243     GridAttributes gv = m_plane->gridAttributes( Qt::Vertical );
0244     QVERIFY( gh.isGridVisible() == true );
0245     QVERIFY( gv.isGridVisible() == true );
0246     gh.setGridVisible( false );
0247     m_plane->setGridAttributes( Qt::Horizontal, gh );
0248     QVERIFY( m_plane->hasOwnGridAttributes( Qt::Horizontal ) == true );
0249     QVERIFY( m_plane->hasOwnGridAttributes( Qt::Vertical ) == false );
0250     QVERIFY( m_plane->gridAttributes( Qt::Horizontal ).isGridVisible() == false );
0251     QVERIFY( m_plane->gridAttributes( Qt::Vertical ).isGridVisible() == true );
0252     gv.setGridVisible( false );
0253     m_plane->setGridAttributes( Qt::Vertical, gv );
0254     QVERIFY( m_plane->hasOwnGridAttributes( Qt::Horizontal ) == true );
0255     QVERIFY( m_plane->hasOwnGridAttributes( Qt::Vertical ) == true );
0256     QVERIFY( m_plane->gridAttributes( Qt::Horizontal ).isGridVisible() == false );
0257     QVERIFY( m_plane->gridAttributes( Qt::Vertical ).isGridVisible() == false );
0258     m_plane->resetGridAttributes( Qt::Horizontal );
0259     m_plane->resetGridAttributes( Qt::Vertical );
0260     QVERIFY( m_plane->gridAttributes( Qt::Horizontal ).isGridVisible() == true );
0261     QVERIFY( m_plane->gridAttributes( Qt::Vertical ).isGridVisible() == true );
0262     QVERIFY( m_plane->hasOwnGridAttributes( Qt::Horizontal ) == false );
0263     QVERIFY( m_plane->hasOwnGridAttributes( Qt::Vertical ) == false );
0264 }
0265 
0266 void TestCartesianPlanes::testAxesCalcModesSettings()
0267 {
0268     QCOMPARE( m_plane->axesCalcModeX(), AbstractCoordinatePlane::Linear );
0269     QCOMPARE( m_plane->axesCalcModeY(), AbstractCoordinatePlane::Linear );
0270     m_plane->setAxesCalcModes( AbstractCoordinatePlane::Logarithmic );
0271     QCOMPARE( m_plane->axesCalcModeX(), AbstractCoordinatePlane::Logarithmic );
0272     QCOMPARE( m_plane->axesCalcModeY(), AbstractCoordinatePlane::Logarithmic );
0273     m_plane->setAxesCalcModeX( AbstractCoordinatePlane::Linear );
0274     QCOMPARE( m_plane->axesCalcModeX(), AbstractCoordinatePlane::Linear );
0275     QCOMPARE( m_plane->axesCalcModeY(), AbstractCoordinatePlane::Logarithmic );
0276     m_plane->setAxesCalcModeY( AbstractCoordinatePlane::Linear );
0277     QCOMPARE( m_plane->axesCalcModeX(), AbstractCoordinatePlane::Linear );
0278     QCOMPARE( m_plane->axesCalcModeY(), AbstractCoordinatePlane::Linear );
0279 }
0280 
0281 
0282 QTEST_MAIN(TestCartesianPlanes)
0283 
0284 #include "main.moc"