File indexing completed on 2024-05-12 04:20:06

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 "diagramtypedialog.h"
0010 #include "ui_diagramtypedialog.h"
0011 
0012 #include <KChartChart>
0013 #include <KChartAbstractCoordinatePlane>
0014 #include <KChartCartesianCoordinatePlane>
0015 #include <KChartPolarCoordinatePlane>
0016 
0017 #include <KChartBarDiagram>
0018 #include <KChartLineDiagram>
0019 #include <KChartPieDiagram>
0020 #include <KChartPlotter>
0021 
0022 #include <QDebug>
0023 
0024 using namespace KChart;
0025 
0026 class DiagramTypeDialog::Private : public QObject
0027 {
0028     Q_OBJECT
0029 public:
0030     Private( KChart::Chart *chart, DiagramTypeDialog * q );
0031     void init();
0032     void createPlanes();
0033 
0034     int lastIndex;
0035     Chart *m_chart;
0036     DiagramTypeDialog::DiagramType type;
0037     DiagramTypeDialog::Subtype subType;
0038     QHash< DiagramTypeDialog::DiagramType, QAbstractItemModel* > m_defaultModels;
0039     QHash< DiagramTypeDialog::DiagramType, AbstractCoordinatePlane* > m_planes;
0040     QHash< DiagramTypeDialog::DiagramType, DiagramTypeDialog::Subtype > m_typemap;
0041     DiagramTypeDialog *qq;
0042     Ui::DiagramTypeDialog ui;
0043 public Q_SLOTS:
0044     void typeChanged( int index );
0045     void subtypeChanged( int index );
0046 };
0047 
0048 DiagramTypeDialog::Private::Private( KChart::Chart *chart, DiagramTypeDialog * q )
0049     : lastIndex( 0 )
0050     , m_chart( chart )
0051     , type( DiagramTypeDialog::Bar )
0052     , subType( DiagramTypeDialog::Normal )
0053     , qq( q )
0054 {
0055 
0056 }
0057 
0058 void DiagramTypeDialog::Private::init()
0059 {
0060     ui.setupUi( qq );
0061     ui.typeSelector->addItem( QIcon(), tr( "BarDiagram" ), DiagramTypeDialog::Bar );
0062     ui.typeSelector->addItem( QIcon(), tr( "Lying BarDiagram" ), DiagramTypeDialog::LyingBar );
0063     ui.typeSelector->addItem( QIcon(), tr( "LineDiagram" ), DiagramTypeDialog::Line );
0064     ui.typeSelector->addItem( QIcon(), tr( "Plotter" ), DiagramTypeDialog::Plotter );
0065     ui.typeSelector->addItem( QIcon(), tr( "PieDiagram" ), DiagramTypeDialog::Pie );
0066 
0067     ui.subtypeSelector->addItem( QIcon(), tr( "Normal" ), DiagramTypeDialog::Normal );
0068     ui.subtypeSelector->addItem( QIcon(), tr( "Stacked" ), DiagramTypeDialog::Stacked );
0069     ui.subtypeSelector->addItem( QIcon(), tr( "Percent" ), DiagramTypeDialog::Percent );
0070 
0071     createPlanes();
0072     m_typemap[ DiagramTypeDialog::Bar ] = DiagramTypeDialog::Normal;
0073     m_typemap[ DiagramTypeDialog::LyingBar ] = DiagramTypeDialog::Normal;
0074     m_typemap[ DiagramTypeDialog::Line ] = DiagramTypeDialog::Normal;
0075     m_typemap[ DiagramTypeDialog::Plotter ] = DiagramTypeDialog::Normal;
0076 
0077     connect( ui.typeSelector, SIGNAL(currentIndexChanged(int)), this, SLOT(typeChanged(int)) );
0078     connect( ui.subtypeSelector, SIGNAL(currentIndexChanged(int)), this, SLOT(subtypeChanged(int)) );
0079 }
0080 
0081 void DiagramTypeDialog::Private::createPlanes()
0082 {
0083     m_planes[ DiagramTypeDialog::Bar ] = m_chart->coordinatePlane();
0084     m_planes[ DiagramTypeDialog::LyingBar ] = m_chart->coordinatePlane();
0085 
0086     CartesianCoordinatePlane *linePlane = new CartesianCoordinatePlane;
0087     linePlane->addDiagram( new LineDiagram );
0088     m_planes[ DiagramTypeDialog::Line ] = linePlane;
0089 
0090     CartesianCoordinatePlane *plotterPlane = new CartesianCoordinatePlane;
0091     plotterPlane->addDiagram( new KChart::Plotter );
0092     m_planes[ DiagramTypeDialog::Plotter ] = plotterPlane;
0093 
0094     PolarCoordinatePlane *piePlane = new PolarCoordinatePlane;
0095     piePlane->addDiagram( new PieDiagram );
0096     m_planes[ DiagramTypeDialog::Pie ] = piePlane;
0097 }
0098 
0099 void DiagramTypeDialog::Private::typeChanged( int index )
0100 {
0101     if ( index == lastIndex )
0102         return;
0103     const DiagramTypeDialog::DiagramType type = static_cast< DiagramTypeDialog::DiagramType >( ui.typeSelector->itemData( index ).toInt() );
0104     const DiagramTypeDialog::DiagramType lastType = static_cast< DiagramTypeDialog::DiagramType >( ui.typeSelector->itemData( lastIndex ).toInt() );
0105     if ( m_planes.contains( type ) )
0106     {
0107         ui.subtypeSelector->setEnabled( true );
0108         if ( type == DiagramTypeDialog::LyingBar )
0109         {
0110             BarDiagram * diag = qobject_cast< BarDiagram* >( m_planes[ type ]->diagram() );
0111             diag->setOrientation( Qt::Horizontal );
0112         }
0113         else if ( type == DiagramTypeDialog::Bar )
0114         {
0115             BarDiagram * diag = qobject_cast< BarDiagram* >( m_planes[ type ]->diagram() );
0116             diag->setOrientation( Qt::Vertical );
0117         }
0118         else if ( type == DiagramTypeDialog::Pie )
0119             ui.subtypeSelector->setEnabled( false );
0120         this->type = type;
0121         ui.subtypeSelector->setCurrentIndex( m_typemap[ type ] );
0122         m_chart->takeCoordinatePlane( m_planes[ lastType ] );
0123         m_chart->addCoordinatePlane( m_planes[ type ] );
0124 
0125         lastIndex = index;
0126         Q_EMIT qq->diagramTypeChanged( type, subType );
0127     }
0128     else
0129     {
0130         ui.typeSelector->setCurrentIndex( lastIndex );
0131     }
0132 }
0133 
0134 void DiagramTypeDialog::Private::subtypeChanged( int index )
0135 {
0136     const DiagramTypeDialog::DiagramType type = static_cast< DiagramTypeDialog::DiagramType >( ui.typeSelector->itemData( ui.typeSelector->currentIndex() ).toInt() );
0137     switch ( type )
0138     {
0139     case( DiagramTypeDialog::Bar ):
0140     case( DiagramTypeDialog::LyingBar ):
0141     {
0142         BarDiagram *bar = qobject_cast< BarDiagram* >( m_planes[ type ]->diagram() );
0143         Q_ASSERT( bar );
0144         bar->setType( static_cast< BarDiagram::BarType >( index ) );
0145         m_typemap[ type ] = static_cast< DiagramTypeDialog::Subtype >( index );
0146         break;
0147     }
0148     case( DiagramTypeDialog::Line ):
0149     {
0150         LineDiagram *line = qobject_cast< LineDiagram* >( m_planes[ type ]->diagram() );
0151         Q_ASSERT( line );
0152         line->setType( static_cast< LineDiagram::LineType >( index ) );
0153         m_typemap[ type ] = static_cast< DiagramTypeDialog::Subtype >( index );
0154         break;
0155         break;
0156     }
0157     case( DiagramTypeDialog::Pie ):
0158         break;
0159     default:
0160         Q_ASSERT( false );
0161     }
0162 }
0163 
0164 DiagramTypeDialog::DiagramTypeDialog( KChart::Chart *chart, QWidget *parent )
0165     : QDialog( parent )
0166     , d( new Private( chart, this ) )
0167 {
0168     d->init();
0169 }
0170 
0171 DiagramTypeDialog::~DiagramTypeDialog()
0172 {
0173     delete d;
0174 }
0175 
0176 void DiagramTypeDialog::setDefaultModels( QHash< DiagramType, QAbstractItemModel* > models )
0177 {
0178     d->m_defaultModels = models;
0179     for ( QHash< DiagramType, AbstractCoordinatePlane* >::iterator it = d->m_planes.begin(); it != d->m_planes.end(); ++it )
0180     {
0181         AbstractDiagram * diagram = it.value()->diagram();
0182         diagram->setModel( d->m_defaultModels[ it.key() ] );
0183     }
0184 }
0185 
0186 #include "diagramtypedialog.moc"