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