1 /*****************************************************************************
2 * preferences.cpp : Preferences
3 *****************************************************************************
4 * Copyright (C) 2006-2007 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Jean-Baptiste Kempf <jb@videolan.org>
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.
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.
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 *****************************************************************************/
25 #include "dialogs/preferences.hpp"
26 #include "dialogs_provider.hpp"
27 #include "util/qvlcframe.hpp"
29 #include "components/complete_preferences.hpp"
30 #include "components/simple_preferences.hpp"
32 #include <QHBoxLayout>
34 #include <QRadioButton>
35 #include <QVBoxLayout>
36 #include <QPushButton>
38 #include <QMessageBox>
39 #include <QDialogButtonBox>
41 PrefsDialog *PrefsDialog::instance = NULL;
43 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
45 QGridLayout *main_layout = new QGridLayout( this );
46 setWindowTitle( qtr( "Preferences" ) );
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 );
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 );
68 /* Tree and panel initialisations */
71 current_simple_panel = NULL;
72 advanced_panel = NULL;
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" ) );
80 buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
81 buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
82 buttonsBox->addButton( reset, QDialogButtonBox::ActionRole );
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 );
90 main_layout->setColumnMinimumWidth( 0, 150 );
91 main_layout->setColumnStretch( 0, 1 );
92 main_layout->setColumnStretch( 1, 3 );
94 main_layout->setRowStretch( 2, 4 );
96 setLayout( main_layout );
99 tree_panel_l->setMargin( 1 );
100 main_panel_l->setMargin( 3 );
102 for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
104 if( config_GetInt( p_intf, "qt-advanced-pref" ) == 1 )
109 BUTTONACT( save, save() );
110 BUTTONACT( cancel, cancel() );
111 BUTTONACT( reset, reset() );
113 BUTTONACT( small, setSmall() );
114 BUTTONACT( all, setAdvanced() );
117 void PrefsDialog::setAdvanced()
119 /* We already have a simple TREE, and we just want to hide it */
121 if( simple_tree->isVisible() ) simple_tree->hide();
123 /* If don't have already and advanced TREE, then create it */
127 advanced_tree = new PrefsTree( p_intf, tree_panel );
128 /* and connections */
129 CONNECT( advanced_tree,
130 currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem * ),
131 this, changeAdvPanel( QTreeWidgetItem * ) );
132 tree_panel_l->addWidget( advanced_tree );
136 advanced_tree->show();
138 /* Remove the simple current panel from the main panels*/
139 if( current_simple_panel )
140 if( current_simple_panel->isVisible() ) current_simple_panel->hide();
142 /* If no advanced Panel exist, create one, attach it and show it*/
143 if( !advanced_panel )
145 advanced_panel = new AdvPrefsPanel( main_panel );
146 main_panel_l->addWidget( advanced_panel );
148 advanced_panel->show();
150 all->setChecked( true );
153 void PrefsDialog::setSmall()
155 /* If an advanced TREE exists, remove and hide it */
157 if( advanced_tree->isVisible() ) advanced_tree->hide();
159 /* If no simple_tree, create one, connect it */
162 simple_tree = new SPrefsCatList( p_intf, tree_panel );
163 CONNECT( simple_tree,
164 currentItemChanged( int ),
165 this, changeSimplePanel( int ) );
166 tree_panel_l->addWidget( simple_tree );
172 /* If an Advanced PANEL exists, remove it */
174 if( advanced_panel->isVisible() ) advanced_panel->hide();
176 if( !current_simple_panel )
178 current_simple_panel =
179 new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
180 simple_panels[SPrefsDefaultCat] = current_simple_panel;
181 main_panel_l->addWidget( current_simple_panel );
184 current_simple_panel->show();
185 small->setChecked( true );
188 /* Switching from on simple panel to another */
189 void PrefsDialog::changeSimplePanel( int number )
191 if( current_simple_panel )
192 if( current_simple_panel->isVisible() ) current_simple_panel->hide();
194 current_simple_panel = simple_panels[number];
195 if( !current_simple_panel )
197 current_simple_panel = new SPrefsPanel( p_intf, main_panel, number );
198 simple_panels[number] = current_simple_panel;
199 main_panel_l->addWidget( current_simple_panel );
202 current_simple_panel->show();
205 /* Changing from one Advanced Panel to another */
206 void PrefsDialog::changeAdvPanel( QTreeWidgetItem *item )
208 PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
211 if( advanced_panel->isVisible() ) advanced_panel->hide();
215 data->panel = new AdvPrefsPanel( p_intf, main_panel , data );
216 main_panel_l->addWidget( data->panel );
219 advanced_panel = data->panel;
220 advanced_panel->show();
224 /*Called from extended settings, is not used anymore, but could be useful one day*/
225 void PrefsDialog::showModulePrefs( char *psz_module )
228 all->setChecked( true );
229 for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
232 QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
233 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
234 value<PrefsItemData *>();
235 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
238 QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
239 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
240 value<PrefsItemData *>();
241 for( int i_module = 0; i_module < subcat_item->childCount();
244 QTreeWidgetItem *module_item = subcat_item->child( i_module );
245 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
246 value<PrefsItemData *>();
247 if( !strcmp( mod_data->psz_name, psz_module ) ) {
248 advanced_tree->setCurrentItem( module_item );
257 /* Actual apply and save for the preferences */
258 void PrefsDialog::save()
260 if( small->isChecked() && simple_tree->isVisible() )
262 msg_Dbg( p_intf, "Saving the simple preferences" );
263 for( int i = 0 ; i< SPrefsMax; i++ ){
264 if( simple_panels[i] )simple_panels[i]->apply();
267 else if( all->isChecked() && advanced_tree->isVisible() )
269 msg_Dbg( p_intf, "Saving the advanced preferences" );
270 advanced_tree->applyAll();
274 config_SaveConfigFile( p_intf, NULL );
276 /* Delete the other panel in order to force its reload after clicking
277 on apply. In fact, if we don't do that, the preferences from the other
278 panels won't be accurate, so we would have to recreate the whole dialog,
279 and we don't want that.*/
280 if( small->isChecked() && advanced_panel )
282 /* Deleting only the active panel from the advanced config doesn't work
283 because the data records of PrefsItemData contains still a
284 reference to it only cleanAll() is sure to remove all Panels! */
285 advanced_tree->cleanAll();
286 advanced_panel = NULL;
288 if( all->isChecked() && current_simple_panel )
290 for( int i = 0 ; i< SPrefsMax; i++ )
292 if( simple_panels[i] )
294 delete simple_panels[i];
295 simple_panels[i] = NULL;
298 current_simple_panel = NULL;
304 /* Clean the preferences, dunno if it does something really */
305 void PrefsDialog::cancel()
307 if( small->isChecked() && simple_tree )
309 for( int i = 0 ; i< SPrefsMax; i++ )
310 if( simple_panels[i] ) simple_panels[i]->clean();
312 else if( all->isChecked() && advanced_tree )
314 advanced_tree->cleanAll();
315 advanced_panel = NULL;
320 /* Reset all the preferences, when you click the button */
321 void PrefsDialog::reset()
323 int ret = QMessageBox::question(
325 qtr( "Reset Preferences" ),
326 qtr( "This will reset your VLC media player preferences.\n"
327 "Are you sure you want to continue?" ),
328 QMessageBox::Ok | QMessageBox::Cancel,
331 if( ret == QMessageBox::Ok )
333 config_ResetAll( p_intf );
334 config_SaveConfigFile( p_intf, NULL );