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