1 /**
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "diagramtypedialog.h"
21 #include "ui_diagramtypedialog.h"
22 
23 #include <KChartChart>
24 #include <KChartAbstractCoordinatePlane>
25 #include <KChartCartesianCoordinatePlane>
26 #include <KChartPolarCoordinatePlane>
27 
28 #include <KChartBarDiagram>
29 #include <KChartLineDiagram>
30 #include <KChartPieDiagram>
31 #include <KChartPlotter>
32 
33 #include <QDebug>
34 
35 using namespace KChart;
36 
37 class DiagramTypeDialog::Private : public QObject
38 {
39     Q_OBJECT
40 public:
41     Private( KChart::Chart *chart, DiagramTypeDialog * q );
42     void init();
43     void createPlanes();
44 
45     int lastIndex;
46     Chart *m_chart;
47     DiagramTypeDialog::DiagramType type;
48     DiagramTypeDialog::Subtype subType;
49     QHash< DiagramTypeDialog::DiagramType, QAbstractItemModel* > m_defaultModels;
50     QHash< DiagramTypeDialog::DiagramType, AbstractCoordinatePlane* > m_planes;
51     QHash< DiagramTypeDialog::DiagramType, DiagramTypeDialog::Subtype > m_typemap;
52     DiagramTypeDialog *qq;
53     Ui::DiagramTypeDialog ui;
54 public Q_SLOTS:
55     void typeChanged( int index );
56     void subtypeChanged( int index );
57 };
58 
Private(KChart::Chart * chart,DiagramTypeDialog * q)59 DiagramTypeDialog::Private::Private( KChart::Chart *chart, DiagramTypeDialog * q )
60     : lastIndex( 0 )
61     , m_chart( chart )
62     , type( DiagramTypeDialog::Bar )
63     , subType( DiagramTypeDialog::Normal )
64     , qq( q )
65 {
66 
67 }
68 
init()69 void DiagramTypeDialog::Private::init()
70 {
71     ui.setupUi( qq );
72     ui.typeSelector->addItem( QIcon(), tr( "BarDiagram" ), DiagramTypeDialog::Bar );
73     ui.typeSelector->addItem( QIcon(), tr( "Lying BarDiagram" ), DiagramTypeDialog::LyingBar );
74     ui.typeSelector->addItem( QIcon(), tr( "LineDiagram" ), DiagramTypeDialog::Line );
75     ui.typeSelector->addItem( QIcon(), tr( "Plotter" ), DiagramTypeDialog::Plotter );
76     ui.typeSelector->addItem( QIcon(), tr( "PieDiagram" ), DiagramTypeDialog::Pie );
77 
78     ui.subtypeSelector->addItem( QIcon(), tr( "Normal" ), DiagramTypeDialog::Normal );
79     ui.subtypeSelector->addItem( QIcon(), tr( "Stacked" ), DiagramTypeDialog::Stacked );
80     ui.subtypeSelector->addItem( QIcon(), tr( "Percent" ), DiagramTypeDialog::Percent );
81 
82     createPlanes();
83     m_typemap[ DiagramTypeDialog::Bar ] = DiagramTypeDialog::Normal;
84     m_typemap[ DiagramTypeDialog::LyingBar ] = DiagramTypeDialog::Normal;
85     m_typemap[ DiagramTypeDialog::Line ] = DiagramTypeDialog::Normal;
86     m_typemap[ DiagramTypeDialog::Plotter ] = DiagramTypeDialog::Normal;
87 
88     connect( ui.typeSelector, SIGNAL(currentIndexChanged(int)), this, SLOT(typeChanged(int)) );
89     connect( ui.subtypeSelector, SIGNAL(currentIndexChanged(int)), this, SLOT(subtypeChanged(int)) );
90 }
91 
createPlanes()92 void DiagramTypeDialog::Private::createPlanes()
93 {
94     m_planes[ DiagramTypeDialog::Bar ] = m_chart->coordinatePlane();
95     m_planes[ DiagramTypeDialog::LyingBar ] = m_chart->coordinatePlane();
96 
97     CartesianCoordinatePlane *linePlane = new CartesianCoordinatePlane;
98     linePlane->addDiagram( new LineDiagram );
99     m_planes[ DiagramTypeDialog::Line ] = linePlane;
100 
101     CartesianCoordinatePlane *plotterPlane = new CartesianCoordinatePlane;
102     plotterPlane->addDiagram( new KChart::Plotter );
103     m_planes[ DiagramTypeDialog::Plotter ] = plotterPlane;
104 
105     PolarCoordinatePlane *piePlane = new PolarCoordinatePlane;
106     piePlane->addDiagram( new PieDiagram );
107     m_planes[ DiagramTypeDialog::Pie ] = piePlane;
108 }
109 
typeChanged(int index)110 void DiagramTypeDialog::Private::typeChanged( int index )
111 {
112     if ( index == lastIndex )
113         return;
114     const DiagramTypeDialog::DiagramType type = static_cast< DiagramTypeDialog::DiagramType >( ui.typeSelector->itemData( index ).toInt() );
115     const DiagramTypeDialog::DiagramType lastType = static_cast< DiagramTypeDialog::DiagramType >( ui.typeSelector->itemData( lastIndex ).toInt() );
116     if ( m_planes.contains( type ) )
117     {
118         ui.subtypeSelector->setEnabled( true );
119         if ( type == DiagramTypeDialog::LyingBar )
120         {
121             BarDiagram * diag = qobject_cast< BarDiagram* >( m_planes[ type ]->diagram() );
122             diag->setOrientation( Qt::Horizontal );
123         }
124         else if ( type == DiagramTypeDialog::Bar )
125         {
126             BarDiagram * diag = qobject_cast< BarDiagram* >( m_planes[ type ]->diagram() );
127             diag->setOrientation( Qt::Vertical );
128         }
129         else if ( type == DiagramTypeDialog::Pie )
130             ui.subtypeSelector->setEnabled( false );
131         this->type = type;
132         ui.subtypeSelector->setCurrentIndex( m_typemap[ type ] );
133         m_chart->takeCoordinatePlane( m_planes[ lastType ] );
134         m_chart->addCoordinatePlane( m_planes[ type ] );
135 
136         lastIndex = index;
137         Q_EMIT qq->diagramTypeChanged( type, subType );
138     }
139     else
140     {
141         ui.typeSelector->setCurrentIndex( lastIndex );
142     }
143 }
144 
subtypeChanged(int index)145 void DiagramTypeDialog::Private::subtypeChanged( int index )
146 {
147     const DiagramTypeDialog::DiagramType type = static_cast< DiagramTypeDialog::DiagramType >( ui.typeSelector->itemData( ui.typeSelector->currentIndex() ).toInt() );
148     switch ( type )
149     {
150     case( DiagramTypeDialog::Bar ):
151     case( DiagramTypeDialog::LyingBar ):
152     {
153         BarDiagram *bar = qobject_cast< BarDiagram* >( m_planes[ type ]->diagram() );
154         Q_ASSERT( bar );
155         bar->setType( static_cast< BarDiagram::BarType >( index ) );
156         m_typemap[ type ] = static_cast< DiagramTypeDialog::Subtype >( index );
157         break;
158     }
159     case( DiagramTypeDialog::Line ):
160     {
161         LineDiagram *line = qobject_cast< LineDiagram* >( m_planes[ type ]->diagram() );
162         Q_ASSERT( line );
163         line->setType( static_cast< LineDiagram::LineType >( index ) );
164         m_typemap[ type ] = static_cast< DiagramTypeDialog::Subtype >( index );
165         break;
166         break;
167     }
168     case( DiagramTypeDialog::Pie ):
169         break;
170     default:
171         Q_ASSERT( false );
172     }
173 }
174 
DiagramTypeDialog(KChart::Chart * chart,QWidget * parent)175 DiagramTypeDialog::DiagramTypeDialog( KChart::Chart *chart, QWidget *parent )
176     : QDialog( parent )
177     , d( new Private( chart, this ) )
178 {
179     d->init();
180 }
181 
~DiagramTypeDialog()182 DiagramTypeDialog::~DiagramTypeDialog()
183 {
184     delete d;
185 }
186 
setDefaultModels(QHash<DiagramType,QAbstractItemModel * > models)187 void DiagramTypeDialog::setDefaultModels( QHash< DiagramType, QAbstractItemModel* > models )
188 {
189     d->m_defaultModels = models;
190     for ( QHash< DiagramType, AbstractCoordinatePlane* >::iterator it = d->m_planes.begin(); it != d->m_planes.end(); ++it )
191     {
192         AbstractDiagram * diagram = it.value()->diagram();
193         diagram->setModel( d->m_defaultModels[ it.key() ] );
194     }
195 }
196 
197 #include "diagramtypedialog.moc"
198