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