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 <KChartChart>
0011 #include <KChartGlobal>
0012 #include <KChartBarDiagram>
0013 #include <KChartThreeDBarAttributes>
0014 #include <KChartCartesianCoordinatePlane>
0015 
0016 #include <TableModel.h>
0017 
0018 using namespace KChart;
0019 
0020 class TestBarDiagrams: public QObject {
0021     Q_OBJECT
0022 private Q_SLOTS:
0023 
0024     void initTestCase()
0025     {
0026         m_chart = new Chart(nullptr);
0027         m_model = new TableModel( nullptr );
0028         m_model->loadFromCSV( ":/data" );
0029         m_bars = new BarDiagram();
0030         m_bars->setModel( m_model );
0031         m_chart->coordinatePlane()->replaceDiagram( m_bars );
0032     }
0033 
0034     void testBarDiagramType()
0035     {
0036         QCOMPARE( m_bars->type(), BarDiagram::Normal );
0037         m_bars->setType( BarDiagram::Stacked );
0038         QCOMPARE(  m_bars->type(),  BarDiagram::Stacked );
0039         m_bars->setType( BarDiagram::Percent );
0040         QCOMPARE(  m_bars->type(),  BarDiagram::Percent );
0041 #ifdef TEMPORARILY_REMOVED
0042         m_bars->setType( BarDiagram::Rows );
0043         QCOMPARE(  m_bars->type(),  BarDiagram::Rows );
0044 #endif
0045         //reset to normal
0046         m_bars->setType( BarDiagram::Normal );
0047     }
0048 
0049     void testBarAttributesLevelSettings()
0050     {
0051         //check segments
0052         const int rows = m_bars->model()->rowCount();
0053         QCOMPARE( m_bars->numberOfAbscissaSegments(), rows );
0054         const int cols = m_bars->model()->columnCount();
0055         QCOMPARE( m_bars->numberOfOrdinateSegments(), cols );
0056         QModelIndex idx = m_model->index(rows-3, cols-3, QModelIndex());
0057         // create attribute
0058         BarAttributes ba( m_bars->barAttributes() );
0059         BarAttributes baCol( m_bars->barAttributes() );
0060         BarAttributes baIndex( m_bars->barAttributes() );
0061         // modify at different level and compare
0062         baCol.setBarGapFactor(2 );
0063         baIndex.setBarGapFactor( 3 );
0064         m_bars->setBarAttributes( ba );
0065         m_bars->setBarAttributes( cols-2,  baCol );
0066         m_bars->setBarAttributes( idx,  baIndex );
0067         QVERIFY( m_bars->barAttributes() != m_bars->barAttributes(cols-2) );
0068         QVERIFY( m_bars->barAttributes() != m_bars->barAttributes(idx) );
0069         QVERIFY( m_bars->barAttributes(cols-2) != m_bars->barAttributes(idx) );
0070         QCOMPARE( m_bars->barAttributes(),  ba );
0071         QCOMPARE( m_bars->barAttributes( cols - 2 ),  baCol );
0072         QCOMPARE( m_bars->barAttributes( idx ),  baIndex );
0073         // try and override the cols and index level - should not work
0074         m_bars->setBarAttributes( ba );
0075         QVERIFY( m_bars->barAttributes().barGapFactor() == 0.4 );
0076         QVERIFY( m_bars->barAttributes( cols-2 ).barGapFactor() == 2 );
0077         QVERIFY( m_bars->barAttributes( idx ).barGapFactor() == 3 );
0078     }
0079 
0080     void testBarAttributesValueSettings()
0081     {
0082         BarAttributes ba( m_bars->barAttributes() );
0083 
0084         // check default values
0085         QVERIFY( ba.fixedDataValueGap() ==  6 );
0086         QVERIFY( ba.useFixedDataValueGap() == false );
0087         QVERIFY( ba.fixedValueBlockGap() ==  24 );
0088         QVERIFY( ba.useFixedValueBlockGap() == false );
0089         QVERIFY( ba.fixedBarWidth() ==  -1 );
0090         QVERIFY( ba.useFixedBarWidth() == false );
0091         QVERIFY( ba.drawSolidExcessArrows() == false );
0092         QVERIFY( ba.groupGapFactor() == 2.0 );
0093         QVERIFY( ba.barGapFactor() == 0.4 );
0094         //change settings
0095         ba.setFixedDataValueGap( 7 );
0096         ba.setUseFixedDataValueGap( true );
0097         ba.setFixedValueBlockGap( 25 );
0098         ba.setUseFixedValueBlockGap( true );
0099         ba.setFixedBarWidth( 1 );
0100         ba.setUseFixedBarWidth( true );
0101         ba.setDrawSolidExcessArrows(  true ); //not implemented yet
0102         ba.setGroupGapFactor( 2 );
0103         ba.setBarGapFactor( 1 );
0104         m_bars->setBarAttributes(  ba );
0105         // get new values
0106         QVERIFY( m_bars->barAttributes().fixedDataValueGap() ==  7 );
0107         QVERIFY( m_bars->barAttributes().useFixedDataValueGap() == true );
0108         QVERIFY( m_bars->barAttributes().fixedValueBlockGap() ==  25 );
0109         QVERIFY( m_bars->barAttributes().useFixedValueBlockGap() == true );
0110         QVERIFY( m_bars->barAttributes().fixedBarWidth() ==  1 );
0111         QVERIFY( m_bars->barAttributes().useFixedBarWidth() == true );
0112         QVERIFY( m_bars->barAttributes().drawSolidExcessArrows() == true );
0113         QVERIFY( m_bars->barAttributes().groupGapFactor() == 2 );
0114         QVERIFY( m_bars->barAttributes().barGapFactor() == 1 );
0115     }
0116 
0117         void testThreeDBarAttributesLevelSettings()
0118     {
0119         //check segments
0120         const int rows = m_bars->model()->rowCount();
0121         QCOMPARE( m_bars->numberOfAbscissaSegments(), rows );
0122         const int cols = m_bars->model()->columnCount();
0123         QCOMPARE( m_bars->numberOfOrdinateSegments(), cols );
0124         QModelIndex idx = m_model->index(rows-3, cols-3, QModelIndex());
0125         // create attribute
0126         ThreeDBarAttributes td( m_bars->threeDBarAttributes() );
0127         ThreeDBarAttributes tdCol( m_bars->threeDBarAttributes() );
0128         ThreeDBarAttributes tdIndex( m_bars->threeDBarAttributes() );
0129         // modify at different level and compare
0130         tdCol.setDepth(25 );
0131         tdIndex.setDepth( 30 );
0132         m_bars->setThreeDBarAttributes( td  );
0133         m_bars->setThreeDBarAttributes( cols-2,  tdCol );
0134         m_bars->setThreeDBarAttributes( idx,  tdIndex );
0135         QVERIFY( m_bars->threeDBarAttributes() !=
0136                  m_bars->threeDBarAttributes(cols-2) );
0137         QVERIFY( m_bars->threeDBarAttributes() !=
0138                  m_bars->threeDBarAttributes(idx) );
0139         QVERIFY( m_bars->threeDBarAttributes(cols-2) !=
0140                  m_bars->threeDBarAttributes(idx) );
0141         QCOMPARE( m_bars->threeDBarAttributes(),  td );
0142         QCOMPARE( m_bars->threeDBarAttributes( cols - 2 ),  tdCol );
0143         QCOMPARE( m_bars->threeDBarAttributes( idx ),  tdIndex );
0144         // try and override the cols and index level - should not work
0145         m_bars->setThreeDBarAttributes( td );
0146         QVERIFY( m_bars->threeDBarAttributes().depth() == 20 );
0147         QVERIFY( m_bars->threeDBarAttributes( cols-2 ).depth() == 25 );
0148         QVERIFY( m_bars->threeDBarAttributes( idx ).depth() == 30 );
0149     }
0150 
0151     void testThreeDBarAttributesValueSettings()
0152     {
0153         ThreeDBarAttributes td( m_bars->threeDBarAttributes() );
0154 
0155         //check default values
0156         //generics == AbstractThreeD
0157         QVERIFY( td.isEnabled() == false );
0158         QVERIFY( td.depth() == 20 );
0159         QVERIFY( td.validDepth() == 0.0 );
0160         //bars specifics
0161         QVERIFY( td.useShadowColors() == true ); // Not implemented
0162         QVERIFY( td.angle() == 45 ); // Not implemented
0163 
0164         //set new values
0165         td.setEnabled(  true );
0166         td.setDepth( 40 );
0167         td.setUseShadowColors( false );
0168         td.setAngle( 75 );
0169         m_bars->setThreeDBarAttributes( td );
0170 
0171         //get new values
0172         QVERIFY( m_bars->threeDBarAttributes().isEnabled() == true );
0173         QVERIFY( m_bars->threeDBarAttributes().depth() == 40 );
0174         QVERIFY( m_bars->threeDBarAttributes().validDepth() == 40 );
0175         QVERIFY( m_bars->threeDBarAttributes().useShadowColors() == false );
0176         QVERIFY( m_bars->threeDBarAttributes().angle() == 75 );
0177     }
0178 
0179     void cleanupTestCase()
0180     {
0181     }
0182 
0183 private:
0184     Chart *m_chart;
0185     BarDiagram *m_bars;
0186     TableModel *m_model;
0187 
0188 };
0189 
0190 QTEST_MAIN(TestBarDiagrams)
0191 
0192 #include "main.moc"