File indexing completed on 2024-05-12 15:53:55

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 <KChartChart>
0022 #include <KChartGlobal>
0023 #include <KChartPieDiagram>
0024 #include <KChartPieAttributes>
0025 #include <KChartThreeDPieAttributes>
0026 #include <KChartPolarCoordinatePlane>
0027 
0028 #include <TableModel.h>
0029 
0030 using namespace KChart;
0031 
0032 class TestPieDiagrams: public QObject {
0033     Q_OBJECT
0034 private Q_SLOTS:
0035 
0036     void initTestCase()
0037     {
0038         m_chart = new Chart(nullptr);
0039         PolarCoordinatePlane* polarPlane = new PolarCoordinatePlane( m_chart );
0040         m_chart->replaceCoordinatePlane( polarPlane );
0041         m_model = new TableModel( this );
0042         m_model->loadFromCSV( ":/data" );
0043         m_pie = new PieDiagram();
0044         m_pie->setModel( m_model );
0045         m_chart->coordinatePlane()->replaceDiagram( m_pie );
0046     }
0047 
0048     void testPieDiagramsSettings()
0049     {
0050         QVERIFY( m_pie->granularity() == 1 );
0051         m_pie->setGranularity( 2 );
0052         QVERIFY( m_pie->granularity() == 2 );
0053     }
0054 
0055 
0056     void testPieAttributesLevelSettings()
0057     {
0058         //check segments
0059         const int cols = m_pie->model()->columnCount();
0060         QCOMPARE( m_pie->columnCount(), cols );
0061         // create attribut
0062         PieAttributes pa( m_pie->pieAttributes() );
0063         PieAttributes paCol( m_pie->pieAttributes() );
0064 
0065         // modify at different level and compare
0066         paCol.setExplode ( true );
0067         m_pie->setPieAttributes( pa );
0068         m_pie->setPieAttributes( cols-2,  paCol );
0069 
0070         QVERIFY( m_pie->pieAttributes() != m_pie->pieAttributes(cols-2) );
0071 
0072         QCOMPARE( m_pie->pieAttributes(),  pa );
0073         QCOMPARE( m_pie->pieAttributes( cols - 2 ),  paCol );
0074 
0075         // try and override the cols and index level - should not work
0076         m_pie->setPieAttributes( pa );
0077         QVERIFY( m_pie->pieAttributes().explode() == false );
0078         QVERIFY( m_pie->pieAttributes( cols-2 ).explode() == true );
0079 
0080     }
0081 
0082     void testPieAttributesValueSettings()
0083     {
0084         PieAttributes pa( m_pie->pieAttributes() );
0085 
0086         // check default values
0087         QVERIFY( pa.explode() == false );
0088         QVERIFY( pa.explodeFactor() == 0.0 );
0089         //change settings
0090         pa.setExplode ( true );
0091         pa.setExplodeFactor( 0.2 );
0092         m_pie->setPieAttributes(  pa );
0093         // get new values
0094         QVERIFY( m_pie->pieAttributes().explode() == true );
0095         QVERIFY( m_pie->pieAttributes().explodeFactor() == 0.2 );
0096     }
0097 
0098     void testThreeDPieAttributesLevelSettings()
0099     {
0100         //check segments
0101         const int cols = m_pie->model()->columnCount();
0102         QCOMPARE( m_pie->columnCount(), cols );
0103 
0104         // create attribut
0105         ThreeDPieAttributes td( m_pie->threeDPieAttributes() );
0106         ThreeDPieAttributes tdCol( m_pie->threeDPieAttributes() );
0107         // modify at different level and compare
0108         tdCol.setDepth(25 );
0109         m_pie->setThreeDPieAttributes( td  );
0110         m_pie->setThreeDPieAttributes( cols-2,  tdCol );
0111         QVERIFY( m_pie->threeDPieAttributes() !=
0112                  m_pie->threeDPieAttributes(cols-2) );
0113         QCOMPARE( m_pie->threeDPieAttributes(),  td );
0114         QCOMPARE( m_pie->threeDPieAttributes( cols - 2 ),  tdCol );
0115         // try and override the cols and index level - should not work
0116         m_pie->setThreeDPieAttributes( td );
0117         QVERIFY( m_pie->threeDPieAttributes().depth() == -10 );
0118         QVERIFY( m_pie->threeDPieAttributes( cols-2 ).depth() == 25 );
0119     }
0120 
0121 
0122     void testThreeDPieAttributesValueSettings()
0123     {
0124         ThreeDPieAttributes td( m_pie->threeDPieAttributes() );
0125         //check default values
0126         QVERIFY( td.isEnabled() == false );
0127         QVERIFY( td.depth() == -10 );
0128         QVERIFY( td.validDepth() == 0.0 );
0129         //pie specifics
0130         QVERIFY( td.useShadowColors() ==  true );
0131 
0132         //set new values
0133         td.setEnabled(  true );
0134         td.setDepth( 40 );
0135         td.setUseShadowColors( false ); // not implemented yet
0136          m_pie->setThreeDPieAttributes( td );
0137 
0138         //get new values
0139         QVERIFY( m_pie->threeDPieAttributes().isEnabled() == true );
0140         QVERIFY( m_pie->threeDPieAttributes().depth() == 40 );
0141         QVERIFY( m_pie->threeDPieAttributes().validDepth() == 40 );
0142         QVERIFY( m_pie->threeDPieAttributes().useShadowColors() == false );
0143     }
0144 
0145     void cleanupTestCase()
0146     {
0147     }
0148 
0149 private:
0150     Chart *m_chart;
0151     PieDiagram *m_pie;
0152     TableModel *m_model;
0153 
0154 };
0155 
0156 QTEST_MAIN(TestPieDiagrams)
0157 
0158 #include "main.moc"