]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/preferences.cpp
7910fa8167f5482435c60e237cd268251565043e
[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 <QScrollArea>
39 #include <QMessageBox>
40 #include <QDialogButtonBox>
41
42 PrefsDialog *PrefsDialog::instance = NULL;
43
44 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
45 {
46     QGridLayout *main_layout = new QGridLayout( this );
47     setWindowTitle( qtr( "Preferences" ) );
48     resize( 750, 550 );
49
50     /* Create Panels */
51     tree_panel = new QWidget( 0 );
52     tree_panel_l = new QHBoxLayout;
53     tree_panel->setLayout( tree_panel_l );
54     main_panel = new QWidget( 0 );
55     main_panel_l = new QHBoxLayout;
56     main_panel->setLayout( main_panel_l );
57
58     /* Choice for types */
59     types = new QGroupBox( "Show settings" );
60     types->setAlignment( Qt::AlignHCenter );
61     QHBoxLayout *types_l = new QHBoxLayout(0);
62     types_l->setSpacing( 3 ); types_l->setMargin( 3 );
63     small = new QRadioButton( qtr("Basic"), types );
64     types_l->addWidget( small );
65     all = new QRadioButton( qtr("All"), types ); types_l->addWidget( all );
66     types->setLayout( types_l );
67     small->setChecked( true );
68
69     /* Tree and panel initialisations */
70     advanced_tree = NULL;
71     simple_tree = NULL;
72     current_simple_panel  = NULL;
73     advanced_panel = NULL;
74
75     /* Buttons */
76     QDialogButtonBox *buttonsBox = new QDialogButtonBox();
77     QPushButton *save = new QPushButton( qtr( "&Save" ) );
78     QPushButton *cancel = new QPushButton( qtr( "&Cancel" ) );
79     QPushButton *reset = new QPushButton( qtr( "&Reset Preferences" ) );
80
81     buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
82     buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
83     buttonsBox->addButton( reset, QDialogButtonBox::ActionRole );
84
85     /* Layout  */
86     main_layout->addWidget( tree_panel, 0, 0, 3, 1 );
87     main_layout->addWidget( types, 3, 0, 2, 1 );
88     main_layout->addWidget( main_panel, 0, 1, 4, 1 );
89     main_layout->addWidget( buttonsBox, 4, 1, 1 ,2 );
90
91     main_layout->setColumnMinimumWidth( 0, 150 );
92     main_layout->setColumnStretch( 0, 1 );
93     main_layout->setColumnStretch( 1, 3 );
94
95     main_layout->setRowStretch( 2, 4);
96
97     setLayout( main_layout );
98
99     /* Margins */
100     tree_panel_l->setMargin( 1 );
101     main_panel_l->setMargin( 3 );
102
103     for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
104
105     if( config_GetInt( p_intf, "qt-advanced-pref") == 1 )
106     {
107         setAll();
108     }
109     else
110     {
111         setSmall();
112     }
113
114     BUTTONACT( save, save() );
115     BUTTONACT( cancel, cancel() );
116     BUTTONACT( reset, reset() );
117     BUTTONACT( small, setSmall() );
118     BUTTONACT( all, setAll() );
119
120 }
121
122 void PrefsDialog::setAll()
123 {
124     /* We already have a simple TREE, and we just want to hide it */
125     if( simple_tree )
126     {
127         tree_panel_l->removeWidget( simple_tree );
128         simple_tree->hide();
129     }
130
131     /* If don't have already and advanced TREE, then create it */
132     if( !advanced_tree )
133     {
134         /* Creation */
135          advanced_tree = new PrefsTree( p_intf, tree_panel );
136         /* and connections */
137          CONNECT( advanced_tree,
138                   currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *),
139                   this, changePanel( QTreeWidgetItem * ) );
140     }
141     /* Add the Advanced tree to the tree_panel, even if it is already inside,
142        since it can't hurt. And show it. */
143     tree_panel_l->addWidget( advanced_tree );
144     advanced_tree->show();
145
146     /* Remove the simple current panel from the main panels*/
147     if( current_simple_panel  )
148     {
149         main_panel_l->removeWidget( current_simple_panel );
150         current_simple_panel->hide();
151     }
152     /* If no advanced Panel exist, create one, attach it and show it*/
153     if( !advanced_panel )
154          advanced_panel = new PrefsPanel( main_panel );
155     main_panel_l->addWidget( advanced_panel );
156     all->setChecked( true );
157     advanced_panel->show();
158 }
159
160 void PrefsDialog::setSmall()
161 {
162     /* If an advanced TREE exists, remove and hide it */
163     if( advanced_tree )
164     {
165         tree_panel_l->removeWidget( advanced_tree );
166         advanced_tree->hide();
167     }
168     /* If no simple_tree, create one, connect it */
169     if( !simple_tree )
170     {
171          simple_tree = new SPrefsCatList( p_intf, tree_panel );
172          CONNECT( simple_tree,
173                   currentItemChanged( int ),
174                   this,  changeSimplePanel( int ) );
175     }
176     /* Attach anyway and show it */
177     tree_panel_l->addWidget( simple_tree );
178     simple_tree->show();
179
180     /* If an Advanced PANEL exists, remove it */
181     if( advanced_panel )
182     {
183         main_panel_l->removeWidget( advanced_panel );
184         advanced_panel->hide();
185     }
186     if( !current_simple_panel  )
187     {
188         current_simple_panel  = new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
189         simple_panels[SPrefsDefaultCat] =  current_simple_panel;
190     }
191     main_panel_l->addWidget( current_simple_panel  );
192     small->setChecked( true );
193     current_simple_panel->show();
194 }
195
196 void PrefsDialog::changeSimplePanel( int number )
197 {
198     if( current_simple_panel  )
199     {
200         main_panel_l->removeWidget( current_simple_panel  );
201         current_simple_panel->hide();
202     }
203     current_simple_panel = simple_panels[number];
204     if( !current_simple_panel  )
205     {
206         current_simple_panel  = new SPrefsPanel( p_intf, main_panel, number );
207         simple_panels[number] = current_simple_panel;
208     }
209     main_panel_l->addWidget( current_simple_panel  );
210     current_simple_panel->show();
211 }
212
213 void PrefsDialog::changePanel( QTreeWidgetItem *item )
214 {
215     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
216
217     if( advanced_panel )
218     {
219         main_panel_l->removeWidget( advanced_panel );
220         advanced_panel->hide();
221     }
222     if( !data->panel )
223         data->panel = new PrefsPanel( p_intf, main_panel , data );
224
225     advanced_panel = data->panel;
226     main_panel_l->addWidget( advanced_panel );
227     advanced_panel->show();
228 }
229
230 void PrefsDialog::showModulePrefs( char *psz_module )
231 {
232     setAll();
233     all->setChecked( true );
234     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
235          i_cat_index++ )
236     {
237         QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
238         PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
239                                                    value<PrefsItemData *>();
240         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
241                                   i_sc_index++ )
242         {
243             QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
244             PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
245                                                     value<PrefsItemData *>();
246             for( int i_module = 0; i_module < subcat_item->childCount();
247                                    i_module++ )
248             {
249                 QTreeWidgetItem *module_item = subcat_item->child( i_module );
250                 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
251                                                     value<PrefsItemData *>();
252                 if( !strcmp( mod_data->psz_name, psz_module ) ) {
253                     advanced_tree->setCurrentItem( module_item );
254                 }
255             }
256         }
257     }
258     show();
259 }
260
261 void PrefsDialog::save()
262 {
263     apply();
264     hide();
265 }
266
267 void PrefsDialog::apply()
268 {
269     if( small->isChecked() && simple_tree )
270     {
271         for( int i = 0 ; i< SPrefsMax; i++ ){
272             if( simple_panels[i] )simple_panels[i]->apply();
273         }
274     }
275     else if( all->isChecked() && advanced_tree )
276         advanced_tree->applyAll();
277
278     config_SaveConfigFile( p_intf, NULL );
279
280     /* Delete the other panel in order to force its reload after clicking
281        on apply - UGLY but will work for now. */
282     if( current_simple_panel  && current_simple_panel->isVisible() && advanced_panel )
283     {
284         delete advanced_panel;
285         advanced_panel = NULL;
286     }
287     if( advanced_panel && advanced_panel->isVisible() && current_simple_panel  )
288     {
289         delete current_simple_panel;
290         current_simple_panel  = NULL;
291     }
292 }
293
294 void PrefsDialog::cancel()
295 {
296     if( small->isChecked() && simple_tree )
297     {
298         for( int i = 0 ; i< SPrefsMax; i++ )
299             if( simple_panels[i] ) simple_panels[i]->clean();
300     }
301     else if( all->isChecked() && advanced_tree )
302     {
303         advanced_tree->cleanAll();
304         advanced_panel = NULL;
305     }
306     hide();
307 }
308
309 void PrefsDialog::reset()
310 {
311     int ret = QMessageBox::question(this, qtr("Reset Preferences"),
312                  qtr("This will reset your VLC media player preferences.\n"
313                          "Are you sure you want to continue?"),
314                   QMessageBox::Ok | QMessageBox::Cancel,
315                                                          QMessageBox::Ok);
316     if ( ret == QMessageBox::Ok )
317     {
318         config_ResetAll( p_intf );
319         config_SaveConfigFile( p_intf, NULL );
320     }
321 }