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 <TableModel.h>
0022 #include <KChartGlobal>
0023 #include <KChartAttributesModel>
0024 #include <KChartBarDiagram>
0025 #include <KChartLineDiagram>
0026 #include <KChartCartesianCoordinatePlane>
0027 #include <KChartDataValueAttributes>
0028 
0029 using namespace KChart;
0030 
0031 class TestKChartAttributesModel : public QObject {
0032   Q_OBJECT
0033 private Q_SLOTS:
0034 
0035   void initTestCase()
0036   {
0037       TableModel *tableModel = new TableModel( this );
0038       tableModel->loadFromCSV( ":/data" );
0039       tableModel->setSupplyHeaderData( false );
0040       m_model = tableModel;
0041       m_plane = new CartesianCoordinatePlane(nullptr);
0042       m_bars = new BarDiagram();
0043       m_bars->setModel( m_model );
0044       m_lines = new LineDiagram();
0045       m_lines->setModel( m_model );
0046   }
0047 
0048   void testKChartAttributesModelSetSimpleData()
0049   {
0050       QModelIndex idx = m_model->index( 0, 2, QModelIndex() );
0051       DataValueAttributes a = m_bars->dataValueAttributes( idx );
0052       QCOMPARE( a.isVisible(), false );
0053       a.setVisible( true );
0054       m_bars->setDataValueAttributes( 2, a );
0055       a = m_bars->dataValueAttributes( idx );
0056       QCOMPARE( a.isVisible(), true );
0057   }
0058 
0059   void testKChartAttributesModelTestPrivateModel()
0060   {
0061       // Private is now default
0062       //m_lines->usePrivateAttributes( true );
0063       // now we should be getting defaults again
0064       QModelIndex idx = m_bars->model()->index( 0, 2, QModelIndex() );
0065       DataValueAttributes a = m_lines->dataValueAttributes( idx );
0066       QCOMPARE( a.isVisible(), false );
0067       // set the defaults on the other model and make sure they have
0068       // an effect
0069       m_bars->setDataValueAttributes( 2, a );
0070       DataValueAttributes b = m_bars->dataValueAttributes( idx );
0071       QCOMPARE( b.isVisible(), false );
0072       // now change a, set it on the lines, check that the bars
0073       // didn't change
0074       a.setVisible( true );
0075       m_lines->setDataValueAttributes( 2, a );
0076       QCOMPARE( a.isVisible(), true );
0077       b = m_bars->dataValueAttributes( idx );
0078       QCOMPARE( b.isVisible(), false );
0079   }
0080 
0081   void testKChartAttributesModelTestSharedModel()
0082   {
0083       // Note: a SHARED atributes-model must be owned by the USER
0084       //       but it may not be owned by any of the diagrams
0085       //       see API docu of AbstractDiagram::setAttributesModel()
0086       AttributesModel *attrsmodel = new AttributesModel( m_model, nullptr );
0087 
0088       m_lines->setAttributesModel( attrsmodel );
0089       m_bars->setAttributesModel(  attrsmodel );
0090 
0091       QModelIndex idx = m_model->index( 0, 2, QModelIndex() );
0092 
0093       DataValueAttributes attrLin = m_lines->dataValueAttributes( idx );
0094       attrLin.setVisible( false );
0095       m_lines->setDataValueAttributes( idx, attrLin );
0096 
0097       DataValueAttributes attrBar = m_bars->dataValueAttributes( idx );
0098       QCOMPARE( attrBar.isVisible(), false );
0099 
0100       attrLin.setVisible( true );
0101       m_lines->setDataValueAttributes( idx, attrLin );
0102 
0103       attrBar = m_bars->dataValueAttributes( idx );
0104       QCOMPARE( attrBar.isVisible(), true );
0105   }
0106 
0107   void testKChartAttributesModelTestSharedFromStart()
0108   {
0109       delete m_lines;
0110       delete m_bars;
0111       delete m_plane;
0112       m_plane = new CartesianCoordinatePlane(nullptr);
0113       m_bars = new BarDiagram();
0114       m_bars->setModel( m_model );
0115       m_lines = new LineDiagram();
0116       m_lines->setModel( m_model );
0117 
0118       AttributesModel* attrsmodel = new AttributesModel( m_model, m_plane );
0119       m_lines->setAttributesModel(attrsmodel);
0120       m_bars->setAttributesModel(attrsmodel);
0121       
0122       QModelIndex idx = m_bars->model()->index( 0, 2, QModelIndex() );
0123       DataValueAttributes a = m_lines->dataValueAttributes( idx );
0124       DataValueAttributes b = m_bars->dataValueAttributes( idx );
0125       QCOMPARE( a.isVisible(), false );
0126       QCOMPARE( b.isVisible(), false );
0127       a.setVisible( true );
0128       QCOMPARE( a.isVisible(), true );
0129       m_lines->setDataValueAttributes( 2, a );
0130       b = m_bars->dataValueAttributes( idx );
0131       QCOMPARE( b.isVisible(), true ); // Should be true by sharing
0132   }
0133 
0134   void testKChartAttributesModelTestPrivate()
0135   {
0136       m_lines->setAttributesModel( new AttributesModel(m_model,m_lines) );
0137       m_bars->setAttributesModel( new AttributesModel(m_model,m_bars) );
0138       QModelIndex idx = m_lines->model()->index( 0, 2, QModelIndex() );
0139       DataValueAttributes a = m_lines->dataValueAttributes( idx );
0140       QCOMPARE( a.isVisible(), false ); // we got a default model again
0141       a.setVisible( true );
0142       m_lines->setDataValueAttributes( 2, a );
0143       // should now have propagated to the bars
0144       DataValueAttributes b = m_bars->dataValueAttributes( idx );
0145       QCOMPARE( b.isVisible(), false ); // No sharing
0146   }
0147 
0148 
0149   void cleanupTestCase()
0150   {
0151       delete m_plane;
0152   }
0153 
0154 private:
0155   QAbstractItemModel *m_model;
0156   CartesianCoordinatePlane* m_plane;
0157   BarDiagram *m_bars;
0158   LineDiagram *m_lines;
0159 
0160 };
0161 
0162 QTEST_MAIN(TestKChartAttributesModel)
0163 
0164 #include "main.moc"