]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/preferences.cpp
Compile fix for L4MERS on Qt4.2 :D
[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::ResetRole );
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, 2 );
91     main_layout->addWidget( buttonsBox, 4, 2, 1 ,1 );
92
93     main_layout->setColumnMinimumWidth( 0, 150 );
94     main_layout->setColumnMinimumWidth( 1, 10 );
95     main_layout->setColumnStretch( 0, 1 );
96     main_layout->setColumnStretch( 1, 0 );
97     main_layout->setColumnStretch( 2, 3 );
98
99     main_layout->setRowStretch( 2, 4 );
100
101     main_layout->setMargin( 9 );
102     setLayout( main_layout );
103
104     /* Margins */
105     tree_panel_l->setMargin( 1 );
106 #if HAS_QT43
107     main_panel_l->setContentsMargins( 6, 0, 0, 3 );
108 #else
109     main_panel_l->setMargin( 3 );
110 #endif
111
112     for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
113
114     if( config_GetInt( p_intf, "qt-advanced-pref" ) == 1 )
115         setAdvanced();
116     else
117         setSmall();
118
119     BUTTONACT( save, save() );
120     BUTTONACT( cancel, cancel() );
121     BUTTONACT( reset, reset() );
122
123     BUTTONACT( small, setSmall() );
124     BUTTONACT( all, setAdvanced() );
125
126     resize( 750, sizeHint().height() );
127 }
128
129 void PrefsDialog::setAdvanced()
130 {
131     /* We already have a simple TREE, and we just want to hide it */
132     if( simple_tree )
133         if( simple_tree->isVisible() ) simple_tree->hide();
134
135     /* If don't have already and advanced TREE, then create it */
136     if( !advanced_tree )
137     {
138         /* Creation */
139          advanced_tree = new PrefsTree( p_intf, tree_panel );
140         /* and connections */
141          CONNECT( advanced_tree,
142                   currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem * ),
143                   this, changeAdvPanel( QTreeWidgetItem * ) );
144         tree_panel_l->addWidget( advanced_tree );
145     }
146
147     /* Show it */
148     advanced_tree->show();
149
150     /* Remove the simple current panel from the main panels*/
151     if( current_simple_panel )
152         if( current_simple_panel->isVisible() ) current_simple_panel->hide();
153
154     /* If no advanced Panel exist, create one, attach it and show it*/
155     if( !advanced_panel )
156     {
157         advanced_panel = new AdvPrefsPanel( main_panel );
158         main_panel_l->addWidget( advanced_panel );
159     }
160     advanced_panel->show();
161
162     /* Select the first Item of the preferences. Maybe you want to select a specified
163        category... */
164     advanced_tree->setCurrentIndex(
165             advanced_tree->model()->index( 0, 0, QModelIndex() ) );
166
167     all->setChecked( true );
168 }
169
170 void PrefsDialog::setSmall()
171 {
172     /* If an advanced TREE exists, remove and hide it */
173     if( advanced_tree )
174         if( advanced_tree->isVisible() ) advanced_tree->hide();
175
176     /* If no simple_tree, create one, connect it */
177     if( !simple_tree )
178     {
179          simple_tree = new SPrefsCatList( p_intf, tree_panel );
180          CONNECT( simple_tree,
181                   currentItemChanged( int ),
182                   this,  changeSimplePanel( int ) );
183         tree_panel_l->addWidget( simple_tree );
184     }
185
186     /*show it */
187     simple_tree->show();
188
189     /* If an Advanced PANEL exists, remove it */
190     if( advanced_panel )
191         if( advanced_panel->isVisible() ) advanced_panel->hide();
192
193     if( !current_simple_panel )
194     {
195         current_simple_panel =
196             new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
197         simple_panels[SPrefsDefaultCat] =  current_simple_panel;
198         main_panel_l->addWidget( current_simple_panel );
199     }
200
201     current_simple_panel->show();
202     small->setChecked( true );
203 }
204
205 /* Switching from on simple panel to another */
206 void PrefsDialog::changeSimplePanel( int number )
207 {
208     if( current_simple_panel )
209         if( current_simple_panel->isVisible() ) current_simple_panel->hide();
210
211     current_simple_panel = simple_panels[number];
212     if( !current_simple_panel )
213     {
214         current_simple_panel  = new SPrefsPanel( p_intf, main_panel, number );
215         simple_panels[number] = current_simple_panel;
216         main_panel_l->addWidget( current_simple_panel );
217     }
218
219     current_simple_panel->show();
220 }
221
222 /* Changing from one Advanced Panel to another */
223 void PrefsDialog::changeAdvPanel( QTreeWidgetItem *item )
224 {
225     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
226
227     if( advanced_panel )
228         if( advanced_panel->isVisible() ) advanced_panel->hide();
229
230     if( !data->panel )
231     {
232         data->panel = new AdvPrefsPanel( p_intf, main_panel , data );
233         main_panel_l->addWidget( data->panel );
234     }
235
236     advanced_panel = data->panel;
237     advanced_panel->show();
238 }
239
240 #if 0
241 /*Called from extended settings, is not used anymore, but could be useful one day*/
242 void PrefsDialog::showModulePrefs( char *psz_module )
243 {
244     setAdvanced();
245     all->setChecked( true );
246     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
247          i_cat_index++ )
248     {
249         QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
250         PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
251                                                    value<PrefsItemData *>();
252         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
253                                   i_sc_index++ )
254         {
255             QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
256             PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
257                                                     value<PrefsItemData *>();
258             for( int i_module = 0; i_module < subcat_item->childCount();
259                                    i_module++ )
260             {
261                 QTreeWidgetItem *module_item = subcat_item->child( i_module );
262                 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
263                                                     value<PrefsItemData *>();
264                 if( !strcmp( mod_data->psz_name, psz_module ) ) {
265                     advanced_tree->setCurrentItem( module_item );
266                 }
267             }
268         }
269     }
270     show();
271 }
272 #endif
273
274 /* Actual apply and save for the preferences */
275 void PrefsDialog::save()
276 {
277     if( small->isChecked() && simple_tree->isVisible() )
278     {
279         msg_Dbg( p_intf, "Saving the simple preferences" );
280         for( int i = 0 ; i< SPrefsMax; i++ ){
281             if( simple_panels[i] )simple_panels[i]->apply();
282         }
283     }
284     else if( all->isChecked() && advanced_tree->isVisible() )
285     {
286         msg_Dbg( p_intf, "Saving the advanced preferences" );
287         advanced_tree->applyAll();
288     }
289
290     /* Save to file */
291     config_SaveConfigFile( p_intf, NULL );
292
293     destroyPanels();
294
295     hide();
296 }
297
298 void PrefsDialog::destroyPanels()
299 {
300     msg_Dbg( p_intf, "Destroying the Panels" );
301     /* Delete the other panel in order to force its reload after clicking
302        on apply. In fact, if we don't do that, the preferences from the other
303        panels won't be accurate, so we would have to recreate the whole dialog,
304        and we don't want that.*/
305     if( small->isChecked() && advanced_panel )
306     {
307         /* Deleting only the active panel from the advanced config doesn't work
308            because the data records of PrefsItemData  contains still a
309            reference to it only cleanAll() is sure to remove all Panels! */
310         advanced_tree->cleanAll();
311         advanced_panel = NULL;
312     }
313     if( all->isChecked() && current_simple_panel )
314     {
315         for( int i = 0 ; i< SPrefsMax; i++ )
316         {
317             if( simple_panels[i] )
318             {
319                delete simple_panels[i];
320                simple_panels[i] = NULL;
321             }
322         }
323         current_simple_panel  = NULL;
324     }
325 }
326
327
328 /* Clean the preferences, dunno if it does something really */
329 void PrefsDialog::cancel()
330 {
331     if( small->isChecked() && simple_tree )
332     {
333         for( int i = 0 ; i< SPrefsMax; i++ )
334             if( simple_panels[i] ) simple_panels[i]->clean();
335     }
336     else if( all->isChecked() && advanced_tree )
337     {
338         advanced_tree->cleanAll();
339         advanced_panel = NULL;
340     }
341     hide();
342 }
343
344 /* Reset all the preferences, when you click the button */
345 void PrefsDialog::reset()
346 {
347     int ret = QMessageBox::question(
348                  this,
349                  qtr( "Reset Preferences" ),
350                  qtr( "This will reset your VLC media player preferences.\n"
351                       "Are you sure you want to continue?" ),
352                  QMessageBox::Ok | QMessageBox::Cancel,
353                  QMessageBox::Ok);
354
355     if( ret == QMessageBox::Ok )
356     {
357         config_ResetAll( p_intf );
358         config_SaveConfigFile( p_intf, NULL );
359         /* FIXME reset the panels */
360         destroyPanels();
361     }
362 }