]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/preferences.cpp
Merge branch 'master' into lpcm_encoder
[vlc] / modules / gui / qt4 / dialogs / preferences.cpp
1 /*****************************************************************************
2  * preferences.cpp : Preferences
3  *****************************************************************************
4  * Copyright (C) 2006-2007 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
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "dialogs/preferences.hpp"
30 #include "util/qvlcframe.hpp"
31 #include "dialogs/errors.hpp"
32
33 #include "components/complete_preferences.hpp"
34 #include "components/simple_preferences.hpp"
35
36 #include <QHBoxLayout>
37 #include <QGroupBox>
38 #include <QRadioButton>
39 #include <QPushButton>
40 #include <QMessageBox>
41 #include <QDialogButtonBox>
42
43 PrefsDialog::PrefsDialog( QWidget *parent, intf_thread_t *_p_intf )
44             : QVLCDialog( parent, _p_intf )
45 {
46     QGridLayout *main_layout = new QGridLayout( this );
47     setWindowTitle( qtr( "Preferences" ) );
48     setWindowRole( "vlc-preferences" );
49     setWindowModality( Qt::WindowModal );
50
51     /* Whether we want it or not, we need to destroy on close to get
52        consistency when reset */
53     setAttribute( Qt::WA_DeleteOnClose );
54
55     /* Create Panels */
56     tree_panel = new QWidget;
57     tree_panel_l = new QHBoxLayout;
58     tree_panel->setLayout( tree_panel_l );
59     main_panel = new QWidget;
60     main_panel_l = new QHBoxLayout;
61     main_panel->setLayout( main_panel_l );
62
63     /* Choice for types */
64     types = new QGroupBox( qtr("Show settings") );
65     types->setAlignment( Qt::AlignHCenter );
66     QHBoxLayout *types_l = new QHBoxLayout;
67     types_l->setSpacing( 3 ); types_l->setMargin( 3 );
68     small = new QRadioButton( qtr( "Simple" ), types );
69     small->setToolTip( qtr( "Switch to simple preferences view" ) );
70     types_l->addWidget( small );
71     all = new QRadioButton( qtr("All"), types ); types_l->addWidget( all );
72     all->setToolTip( qtr( "Switch to full preferences view" ) );
73     types->setLayout( types_l );
74     small->setChecked( true );
75
76     /* Tree and panel initialisations */
77     advanced_tree = NULL;
78     simple_tree = NULL;
79     current_simple_panel  = NULL;
80     advanced_panel = NULL;
81
82     /* Buttons */
83     QDialogButtonBox *buttonsBox = new QDialogButtonBox();
84     QPushButton *save = new QPushButton( qtr( "&Save" ) );
85     save->setToolTip( qtr( "Save and close the dialog" ) );
86     QPushButton *cancel = new QPushButton( qtr( "&Cancel" ) );
87     QPushButton *reset = new QPushButton( qtr( "&Reset Preferences" ) );
88
89     buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
90     buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
91     buttonsBox->addButton( reset, QDialogButtonBox::ResetRole );
92
93     /* Layout  */
94     main_layout->addWidget( tree_panel, 0, 0, 3, 1 );
95     main_layout->addWidget( types, 3, 0, 2, 1 );
96     main_layout->addWidget( main_panel, 0, 1, 4, 2 );
97     main_layout->addWidget( buttonsBox, 4, 2, 1 ,1 );
98
99     main_layout->setColumnMinimumWidth( 0, 150 );
100     main_layout->setColumnMinimumWidth( 1, 10 );
101     main_layout->setColumnStretch( 0, 1 );
102     main_layout->setColumnStretch( 1, 0 );
103     main_layout->setColumnStretch( 2, 10 );
104
105     main_layout->setRowStretch( 2, 4 );
106
107     main_layout->setMargin( 9 );
108     setLayout( main_layout );
109
110     /* Margins */
111     tree_panel_l->setMargin( 1 );
112     main_panel_l->setContentsMargins( 6, 0, 0, 3 );
113
114     b_small = (p_intf->p_sys->i_screenHeight < 750);
115     if( b_small ) msg_Dbg( p_intf, "Small");
116     setMaximumHeight( p_intf->p_sys->i_screenHeight );
117     for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
118
119     if( var_InheritBool( p_intf, "qt-advanced-pref" )
120      || var_InheritBool( p_intf, "advanced" ) )
121         setAdvanced();
122     else
123         setSmall();
124
125     BUTTONACT( save, save() );
126     BUTTONACT( cancel, cancel() );
127     BUTTONACT( reset, reset() );
128
129     BUTTONACT( small, setSmall() );
130     BUTTONACT( all, setAdvanced() );
131
132     resize( 780, sizeHint().height() );
133 }
134
135 void PrefsDialog::setAdvanced()
136 {
137     /* We already have a simple TREE, and we just want to hide it */
138     if( simple_tree )
139         if( simple_tree->isVisible() ) simple_tree->hide();
140
141     /* If don't have already and advanced TREE, then create it */
142     if( !advanced_tree )
143     {
144         /* Creation */
145          advanced_tree = new PrefsTree( p_intf, tree_panel );
146         /* and connections */
147          CONNECT( advanced_tree,
148                   currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem * ),
149                   this, changeAdvPanel( QTreeWidgetItem * ) );
150         tree_panel_l->addWidget( advanced_tree );
151     }
152
153     /* Show it */
154     advanced_tree->show();
155
156     /* Remove the simple current panel from the main panels*/
157     if( current_simple_panel )
158         if( current_simple_panel->isVisible() ) current_simple_panel->hide();
159
160     /* If no advanced Panel exist, create one, attach it and show it*/
161     if( !advanced_panel )
162     {
163         advanced_panel = new AdvPrefsPanel( main_panel );
164         main_panel_l->addWidget( advanced_panel );
165     }
166     advanced_panel->show();
167
168     /* Select the first Item of the preferences. Maybe you want to select a specified
169        category... */
170     advanced_tree->setCurrentIndex(
171             advanced_tree->model()->index( 0, 0, QModelIndex() ) );
172
173     all->setChecked( true );
174 }
175
176 void PrefsDialog::setSmall()
177 {
178     /* If an advanced TREE exists, remove and hide it */
179     if( advanced_tree )
180         if( advanced_tree->isVisible() ) advanced_tree->hide();
181
182     /* If no simple_tree, create one, connect it */
183     if( !simple_tree )
184     {
185          simple_tree = new SPrefsCatList( p_intf, tree_panel, b_small );
186          CONNECT( simple_tree,
187                   currentItemChanged( int ),
188                   this,  changeSimplePanel( int ) );
189         tree_panel_l->addWidget( simple_tree );
190     }
191
192     /*show it */
193     simple_tree->show();
194
195     /* If an Advanced PANEL exists, remove it */
196     if( advanced_panel )
197         if( advanced_panel->isVisible() ) advanced_panel->hide();
198
199     if( !current_simple_panel )
200     {
201         current_simple_panel =
202             new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat, b_small );
203         simple_panels[SPrefsDefaultCat] =  current_simple_panel;
204         main_panel_l->addWidget( current_simple_panel );
205     }
206
207     current_simple_panel->show();
208     small->setChecked( true );
209 }
210
211 /* Switching from on simple panel to another */
212 void PrefsDialog::changeSimplePanel( int number )
213 {
214     if( current_simple_panel )
215         if( current_simple_panel->isVisible() ) current_simple_panel->hide();
216
217     current_simple_panel = simple_panels[number];
218     if( !current_simple_panel )
219     {
220         current_simple_panel  = new SPrefsPanel( p_intf, main_panel, number, b_small );
221         simple_panels[number] = current_simple_panel;
222         main_panel_l->addWidget( current_simple_panel );
223     }
224
225     current_simple_panel->show();
226 }
227
228 /* Changing from one Advanced Panel to another */
229 void PrefsDialog::changeAdvPanel( QTreeWidgetItem *item )
230 {
231     if( item == NULL ) return;
232     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
233
234     if( advanced_panel )
235         if( advanced_panel->isVisible() ) advanced_panel->hide();
236
237     if( !data->panel )
238     {
239         data->panel = new AdvPrefsPanel( p_intf, main_panel , data );
240         main_panel_l->addWidget( data->panel );
241     }
242
243     advanced_panel = data->panel;
244     advanced_panel->show();
245 }
246
247 #if 0
248 /*Called from extended settings, is not used anymore, but could be useful one day*/
249 void PrefsDialog::showModulePrefs( char *psz_module )
250 {
251     setAdvanced();
252     all->setChecked( true );
253     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
254          i_cat_index++ )
255     {
256         QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
257         PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
258                                                    value<PrefsItemData *>();
259         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
260                                   i_sc_index++ )
261         {
262             QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
263             PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
264                                                     value<PrefsItemData *>();
265             for( int i_module = 0; i_module < subcat_item->childCount();
266                                    i_module++ )
267             {
268                 QTreeWidgetItem *module_item = subcat_item->child( i_module );
269                 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
270                                                     value<PrefsItemData *>();
271                 if( !strcmp( mod_data->psz_name, psz_module ) ) {
272                     advanced_tree->setCurrentItem( module_item );
273                 }
274             }
275         }
276     }
277     show();
278 }
279 #endif
280
281 /* Actual apply and save for the preferences */
282 void PrefsDialog::save()
283 {
284     if( small->isChecked() && simple_tree->isVisible() )
285     {
286         msg_Dbg( p_intf, "Saving the simple preferences" );
287         for( int i = 0 ; i< SPrefsMax; i++ ){
288             if( simple_panels[i] )simple_panels[i]->apply();
289         }
290     }
291     else if( all->isChecked() && advanced_tree->isVisible() )
292     {
293         msg_Dbg( p_intf, "Saving the advanced preferences" );
294         advanced_tree->applyAll();
295     }
296
297     /* Save to file */
298     if( config_SaveConfigFile( p_intf, NULL ) != 0 )
299     {
300         ErrorsDialog::getInstance (p_intf)->addError( qtr( "Cannot save Configuration" ),
301             qtr("Preferences file could not be saved") );
302     }
303     accept();
304 }
305
306 /* Clean the preferences, dunno if it does something really */
307 void PrefsDialog::cancel()
308 {
309     reject();
310 }
311
312 /* Reset all the preferences, when you click the button */
313 void PrefsDialog::reset()
314 {
315     int ret = QMessageBox::question(
316                  this,
317                  qtr( "Reset Preferences" ),
318                  qtr( "Are you sure you want to reset your VLC media player preferences?" ),
319                  QMessageBox::Ok | QMessageBox::Cancel,
320                  QMessageBox::Ok);
321
322     if( ret == QMessageBox::Ok )
323     {
324         config_ResetAll( p_intf );
325         config_SaveConfigFile( p_intf, NULL );
326         getSettings()->clear();
327
328         accept();
329     }
330 }