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