172d36b19aab26a4305e1744b041d899967177b8
[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  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *  
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/
23
24 #include "dialogs/prefs_dialog.hpp"
25 #include "dialogs_provider.hpp"
26 #include "util/qvlcframe.hpp"
27
28 #include "components/preferences.hpp"
29 #include "components/simple_preferences.hpp"
30 #include "qt4.hpp"
31
32 #include <QHBoxLayout>
33 #include <QGroupBox>
34 #include <QRadioButton>
35 #include <QVBoxLayout>
36 #include <QPushButton>
37 #include <QCheckBox>
38 #include <QScrollArea>
39 #include <QMessageBox>
40 #include <QKeyEvent>
41
42
43 PrefsDialog *PrefsDialog::instance = NULL;
44
45 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
46 {
47      QGridLayout *main_layout = new QGridLayout( this );
48      setWindowTitle( qtr( "Preferences" ) );
49      resize( 700, 650 );
50      setMaximumHeight( 650 );
51      setMaximumWidth( 700 );
52
53      tree_panel = new QWidget( 0 );
54      tree_panel_l = new QHBoxLayout;
55      tree_panel->setLayout( tree_panel_l );
56      main_panel = new QWidget( 0 );
57      main_panel_l = new QHBoxLayout;
58      main_panel->setLayout( main_panel_l );
59
60      // Choice for types
61      types = new QGroupBox( "Show settings" );
62      types->setAlignment( Qt::AlignHCenter );
63      QHBoxLayout *types_l = new QHBoxLayout(0);
64      types_l->setSpacing( 3 ); types_l->setMargin( 3 );
65      small = new QRadioButton( "Basic", types ); types_l->addWidget( small );
66      all = new QRadioButton( "All", types ); types_l->addWidget( all );
67      types->setLayout( types_l );
68      small->setChecked( true );
69
70      advanced_tree = NULL;
71      simple_tree = NULL;
72      simple_panel = NULL;
73      advanced_panel = NULL;
74
75      main_layout->addWidget( tree_panel, 0, 0, 3, 1 );
76      main_layout->addWidget( types, 3, 0, 1, 1 );
77
78      main_layout->addWidget( main_panel, 0, 1, 4, 1 );
79
80      main_layout->setColumnMinimumWidth( 0, 150 );
81      main_layout->setColumnStretch( 0, 1 );
82      main_layout->setColumnStretch( 1,3 );
83
84      main_layout->setRowStretch( 2, 4);
85
86      setSmall();
87
88      QPushButton *save, *cancel, *reset;
89      QHBoxLayout *buttonsLayout = QVLCFrame::doButtons( this, NULL,
90                                       &save, _("Save"),
91                                       &cancel, _("Cancel"),
92                                       &reset, _( "Reset Preferences" ) );
93      main_layout->addLayout( buttonsLayout, 4, 0, 1 ,3 );
94      setLayout( main_layout );
95
96
97      BUTTONACT( save, save() );
98      BUTTONACT( cancel, cancel() );
99      BUTTONACT( reset, reset() );
100      BUTTONACT( small, setSmall() );
101      BUTTONACT( all, setAll() );
102
103      for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
104 }
105
106 void PrefsDialog::setAll()
107 {
108     if( simple_tree )
109     {
110         tree_panel_l->removeWidget( simple_tree );
111         simple_tree->hide();
112     }
113
114     if( !advanced_tree )
115     {
116          advanced_tree = new PrefsTree( p_intf, tree_panel );
117          CONNECT( advanced_tree,
118                   currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *),
119                   this, changePanel( QTreeWidgetItem * ) );
120     }
121     tree_panel_l->addWidget( advanced_tree );
122     advanced_tree->show();
123
124     if( simple_panel )
125     {
126         main_panel_l->removeWidget( simple_panel );
127         simple_panel->hide();
128     }
129     if( !advanced_panel )
130          advanced_panel = new PrefsPanel( main_panel );
131     main_panel_l->addWidget( advanced_panel );
132     advanced_panel->show();
133 }
134
135 void PrefsDialog::setSmall()
136 {
137     if( advanced_tree )
138     {
139         tree_panel_l->removeWidget( advanced_tree );
140         advanced_tree->hide();
141     }
142     if( !simple_tree )
143     {
144          simple_tree = new SPrefsCatList( p_intf, tree_panel );
145          CONNECT( simple_tree,
146                   currentItemChanged( QListWidgetItem *, QListWidgetItem *),
147                   this,  changeSimplePanel( QListWidgetItem * ) );
148     }
149     tree_panel_l->addWidget( simple_tree );
150     simple_tree->show();
151
152     if( advanced_panel )
153     {
154         main_panel_l->removeWidget( advanced_panel );
155         advanced_panel->hide();
156     }
157     if( !simple_panel )
158         simple_panel = new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
159     main_panel_l->addWidget( simple_panel );
160     simple_panel->show();
161 }
162
163 void PrefsDialog::changeSimplePanel( QListWidgetItem *item )
164 {
165     int number = item->data( Qt::UserRole ).toInt();
166     if( simple_panel )
167     {
168         main_panel_l->removeWidget( simple_panel );
169         simple_panel->hide();
170     }
171     simple_panel = simple_panels[number];
172     if( !simple_panel )
173     {
174         simple_panel = new SPrefsPanel( p_intf, main_panel, number );
175         simple_panels[number] = simple_panel;
176     }
177     main_panel_l->addWidget( simple_panel );
178     simple_panel->show();
179 //    panel_label->setText(qtr("Test")); //FIXME
180 }
181
182 void PrefsDialog::changePanel( QTreeWidgetItem *item )
183 {
184     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
185
186     if( advanced_panel )
187     {
188         main_panel_l->removeWidget( advanced_panel );
189         advanced_panel->hide();
190     }
191     if( !data->panel )
192         data->panel = new PrefsPanel( p_intf, main_panel , data );
193
194     advanced_panel = data->panel;
195     main_panel_l->addWidget( advanced_panel );
196     advanced_panel->show();
197 }
198
199 void PrefsDialog::showModulePrefs( char *psz_module )
200 {
201     setAll();
202     all->setChecked( true );
203     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
204          i_cat_index++ )
205     {
206         QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
207         PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
208                                                    value<PrefsItemData *>();
209         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
210                                   i_sc_index++ )
211         {
212             QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
213             PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
214                                                     value<PrefsItemData *>();
215             for( int i_module = 0; i_module < subcat_item->childCount();
216                                    i_module++ )
217             {
218                 QTreeWidgetItem *module_item = subcat_item->child( i_module );
219                 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
220                                                     value<PrefsItemData *>();
221                 if( !strcmp( mod_data->psz_name, psz_module ) ) {
222                     advanced_tree->setCurrentItem( module_item );
223                 }
224             }
225         }
226     }
227     show();
228 }
229
230 void PrefsDialog::save()
231 {
232     if( small->isChecked() && simple_tree )
233     {
234         for( int i = 0 ; i< SPrefsMax; i++ )
235             if( simple_panels[i] ) simple_panels[i]->apply();
236     }
237     else if( all->isChecked() && advanced_tree )
238         advanced_tree->applyAll();
239     config_SaveConfigFile( p_intf, NULL );
240     hide();
241 }
242
243 void PrefsDialog::cancel()
244 {
245     if( small->isChecked() && simple_tree )
246     {
247         for( int i = 0 ; i< SPrefsMax; i++ )
248             if( simple_panels[i] ) simple_panels[i]->clean();
249     }
250     else if( all->isChecked() && advanced_tree )
251     {
252         advanced_tree->cleanAll();
253         advanced_panel = NULL;
254     }
255     hide();
256 }
257
258 void PrefsDialog::reset()
259 {
260     int ret = QMessageBox::question(this, qtr("Reset Preferences"),
261                  qtr("This will reset your VLC media player preferences.\n"
262                          "Are you sure you want to continue?"),
263                   QMessageBox::Ok | QMessageBox::Cancel,
264                                                          QMessageBox::Ok);
265     if ( ret == QMessageBox::Ok )
266     {
267         config_ResetAll( p_intf );
268         // TODO reset changes ?
269          config_SaveConfigFile( p_intf, NULL );
270     }
271 }
272
273 void PrefsDialog::keyPressEvent( QKeyEvent *e )
274 {
275     if( e->key() == Qt::Key_Escape )
276     {
277         cancel();
278     }
279 }