Some cleanup here and there
[vlc.git] / modules / gui / qt4 / dialogs / prefs_dialog.cpp
1 /*****************************************************************************
2  * prefs_dialog.cpp : Preferences
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/
22
23 #include "dialogs/prefs_dialog.hpp"
24 #include "dialogs_provider.hpp"
25 #include "util/qvlcframe.hpp"
26
27 #include "components/preferences.hpp"
28 #include "components/simple_preferences.hpp"
29 #include "qt4.hpp"
30
31 #include <QHBoxLayout>
32 #include <QGroupBox>
33 #include <QRadioButton>
34 #include <QVBoxLayout>
35 #include <QPushButton>
36 #include <QCheckBox>
37
38 PrefsDialog *PrefsDialog::instance = NULL;
39
40 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
41 {
42      QGridLayout *main_layout = new QGridLayout(this);
43      setWindowTitle( qtr("Preferences" ) );
44      resize( 800, 450 );
45
46      tree_panel = new QWidget(0);
47      tree_panel_l = new QHBoxLayout;
48      tree_panel->setLayout( tree_panel_l );
49      main_panel = new QWidget(0);
50      main_panel_l = new QHBoxLayout;
51      main_panel->setLayout( main_panel_l );
52
53      // Choice for types
54      types = new QGroupBox( "Show settings" );
55      QHBoxLayout *tl = new QHBoxLayout(0);
56      tl->setSpacing( 3 ); tl->setMargin( 3 );
57      small = new QRadioButton( "Basic", types ); tl->addWidget( small );
58      all = new QRadioButton( "All", types ); tl->addWidget( all );
59      types->setLayout(tl);
60      small->setChecked( true );
61
62      advanced_tree = NULL;
63      simple_tree = NULL;
64      simple_panel = NULL;
65      advanced_panel = NULL;
66
67      main_layout->addWidget( types, 0,0,1,1 );
68      main_layout->addWidget( tree_panel, 1,0,1,1 );
69      main_layout->addWidget( main_panel, 0, 1, 2, 1 );
70
71      main_layout->setColumnMinimumWidth( 0, 200 );
72      main_layout->setColumnStretch( 0, 1 );
73      main_layout->setColumnStretch( 1,3 );
74
75      setSmall();
76
77      QPushButton *save, *cancel;
78      QHBoxLayout *buttonsLayout = QVLCFrame::doButtons( this, NULL, 
79                                                         &save, _("Save"),
80                                                         &cancel, _("Cancel"),
81                                                         NULL, NULL );
82      main_layout->addLayout( buttonsLayout, 2,0, 1 ,3 );
83      setLayout( main_layout );
84
85      CONNECT( save, clicked(), this, save() );
86      CONNECT( cancel, clicked(), this, cancel() );
87      CONNECT( small, clicked(), this, setSmall() );
88      CONNECT( all, clicked(), this, setAll() );
89
90      for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
91 }
92
93 void PrefsDialog::setAll()
94 {
95     if( simple_tree )
96     {
97         tree_panel_l->removeWidget( simple_tree );
98         simple_tree->hide();
99     }
100
101     if( !advanced_tree )
102     {
103          advanced_tree = new PrefsTree( p_intf, tree_panel );
104          connect( advanced_tree,
105           SIGNAL( currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *) ),
106           this, SLOT( changePanel( QTreeWidgetItem * ) ) );
107     }
108     tree_panel_l->addWidget( advanced_tree );
109     advanced_tree->show();
110
111     if( simple_panel )
112     {
113         main_panel_l->removeWidget( simple_panel );
114         simple_panel->hide();
115     }
116     if( !advanced_panel )
117          advanced_panel = new PrefsPanel( main_panel );
118     main_panel_l->addWidget( advanced_panel );
119     advanced_panel->show();
120 }
121
122 void PrefsDialog::setSmall()
123 {
124     if( advanced_tree )
125     {
126         tree_panel_l->removeWidget( advanced_tree );
127         advanced_tree->hide();
128     }
129     if( !simple_tree )
130     {
131          simple_tree = new SPrefsCatList( p_intf, tree_panel );
132          CONNECT( simple_tree,
133                   currentItemChanged( QListWidgetItem *, QListWidgetItem *),
134                   this,  changeSimplePanel( QListWidgetItem * ) );
135     }
136     tree_panel_l->addWidget( simple_tree );
137     simple_tree->show();
138
139     if( advanced_panel )
140     {
141         main_panel_l->removeWidget( advanced_panel );
142         advanced_panel->hide();
143     }
144     if( !simple_panel )
145         simple_panel = new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
146     main_panel_l->addWidget( simple_panel );
147     simple_panel->show();
148 }
149
150 void PrefsDialog::changeSimplePanel( QListWidgetItem *item )
151 {
152     int number = item->data( Qt::UserRole ).toInt();
153     if( simple_panel )
154     {
155         main_panel_l->removeWidget( simple_panel );
156         simple_panel->hide();
157     }
158     if( !simple_panels[number] )
159         simple_panel = new SPrefsPanel( p_intf, main_panel, number );
160     simple_panels[number] = simple_panel;
161     main_panel_l->addWidget( simple_panel );
162     simple_panel->show();
163 }
164
165 void PrefsDialog::changePanel( QTreeWidgetItem *item )
166 {
167     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
168
169     if( advanced_panel )
170     {
171         main_panel_l->removeWidget( advanced_panel );
172         advanced_panel->hide();
173     }
174     if( !data->panel )
175         data->panel = new PrefsPanel( p_intf, main_panel , data );
176
177     advanced_panel = data->panel;
178     main_panel_l->addWidget( advanced_panel );
179     advanced_panel->show();
180 }
181
182 void PrefsDialog::save()
183 {
184     if( small->isChecked() && simple_tree )
185     {
186         for( int i = 0 ; i< SPrefsMax; i++ )
187             if( simple_panels[i] ) simple_panels[i]->apply();
188     }
189     else if( all->isChecked() && advanced_tree )
190         advanced_tree->applyAll();
191     config_SaveConfigFile( p_intf, NULL );
192     hide();
193 }
194
195 void PrefsDialog::cancel()
196 {
197     if( small->isChecked() && simple_tree )
198     {
199         for( int i = 0 ; i< SPrefsMax; i++ )
200             if( simple_panels[i] ) simple_panels[i]->clean();
201     }
202     else if( all->isChecked() && advanced_tree )
203     {
204         advanced_tree->cleanAll();
205         advanced_panel = NULL;
206     }
207     hide();
208 }