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