Warning, file /graphics/kdiagram/examples/demo/diagramtypedialog.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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