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 *****************************************************************************/
28 #include "dialogs/preferences.hpp"
29 #include "dialogs_provider.hpp"
30 #include "util/qvlcframe.hpp"
32 #include "components/complete_preferences.hpp"
33 #include "components/simple_preferences.hpp"
35 #include <QHBoxLayout>
37 #include <QRadioButton>
38 #include <QVBoxLayout>
39 #include <QPushButton>
41 #include <QMessageBox>
42 #include <QDialogButtonBox>
44 PrefsDialog *PrefsDialog::instance = NULL;
46 PrefsDialog::PrefsDialog( QWidget *parent, intf_thread_t *_p_intf )
47 : QVLCDialog( parent, _p_intf )
49 QGridLayout *main_layout = new QGridLayout( this );
50 setWindowTitle( qtr( "Preferences" ) );
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 );
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( "Simple" ), 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 );
71 /* Tree and panel initialisations */
74 current_simple_panel = NULL;
75 advanced_panel = NULL;
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" ) );
83 buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
84 buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
85 buttonsBox->addButton( reset, QDialogButtonBox::ResetRole );
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 );
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 );
99 main_layout->setRowStretch( 2, 4 );
101 main_layout->setMargin( 9 );
102 setLayout( main_layout );
105 tree_panel_l->setMargin( 1 );
106 main_panel_l->setLayoutMargins( 6, 0, 0, 3, 3 );
108 for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
110 if( config_GetInt( p_intf, "qt-advanced-pref" ) == 1 )
115 BUTTONACT( save, save() );
116 BUTTONACT( cancel, cancel() );
117 BUTTONACT( reset, reset() );
119 BUTTONACT( small, setSmall() );
120 BUTTONACT( all, setAdvanced() );
122 resize( 750, sizeHint().height() );
125 void PrefsDialog::setAdvanced()
127 /* We already have a simple TREE, and we just want to hide it */
129 if( simple_tree->isVisible() ) simple_tree->hide();
131 /* If don't have already and advanced TREE, then create it */
135 advanced_tree = new PrefsTree( p_intf, tree_panel );
136 /* and connections */
137 CONNECT( advanced_tree,
138 currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem * ),
139 this, changeAdvPanel( QTreeWidgetItem * ) );
140 tree_panel_l->addWidget( advanced_tree );
144 advanced_tree->show();
146 /* Remove the simple current panel from the main panels*/
147 if( current_simple_panel )
148 if( current_simple_panel->isVisible() ) current_simple_panel->hide();
150 /* If no advanced Panel exist, create one, attach it and show it*/
151 if( !advanced_panel )
153 advanced_panel = new AdvPrefsPanel( main_panel );
154 main_panel_l->addWidget( advanced_panel );
156 advanced_panel->show();
158 /* Select the first Item of the preferences. Maybe you want to select a specified
160 advanced_tree->setCurrentIndex(
161 advanced_tree->model()->index( 0, 0, QModelIndex() ) );
163 all->setChecked( true );
166 void PrefsDialog::setSmall()
168 /* If an advanced TREE exists, remove and hide it */
170 if( advanced_tree->isVisible() ) advanced_tree->hide();
172 /* If no simple_tree, create one, connect it */
175 simple_tree = new SPrefsCatList( p_intf, tree_panel );
176 CONNECT( simple_tree,
177 currentItemChanged( int ),
178 this, changeSimplePanel( int ) );
179 tree_panel_l->addWidget( simple_tree );
185 /* If an Advanced PANEL exists, remove it */
187 if( advanced_panel->isVisible() ) advanced_panel->hide();
189 if( !current_simple_panel )
191 current_simple_panel =
192 new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
193 simple_panels[SPrefsDefaultCat] = current_simple_panel;
194 main_panel_l->addWidget( current_simple_panel );
197 current_simple_panel->show();
198 small->setChecked( true );
201 /* Switching from on simple panel to another */
202 void PrefsDialog::changeSimplePanel( int number )
204 if( current_simple_panel )
205 if( current_simple_panel->isVisible() ) current_simple_panel->hide();
207 current_simple_panel = simple_panels[number];
208 if( !current_simple_panel )
210 current_simple_panel = new SPrefsPanel( p_intf, main_panel, number );
211 simple_panels[number] = current_simple_panel;
212 main_panel_l->addWidget( current_simple_panel );
215 current_simple_panel->show();
218 /* Changing from one Advanced Panel to another */
219 void PrefsDialog::changeAdvPanel( QTreeWidgetItem *item )
221 PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
224 if( advanced_panel->isVisible() ) advanced_panel->hide();
228 data->panel = new AdvPrefsPanel( p_intf, main_panel , data );
229 main_panel_l->addWidget( data->panel );
232 advanced_panel = data->panel;
233 advanced_panel->show();
237 /*Called from extended settings, is not used anymore, but could be useful one day*/
238 void PrefsDialog::showModulePrefs( char *psz_module )
241 all->setChecked( true );
242 for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
245 QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
246 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
247 value<PrefsItemData *>();
248 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
251 QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
252 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
253 value<PrefsItemData *>();
254 for( int i_module = 0; i_module < subcat_item->childCount();
257 QTreeWidgetItem *module_item = subcat_item->child( i_module );
258 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
259 value<PrefsItemData *>();
260 if( !strcmp( mod_data->psz_name, psz_module ) ) {
261 advanced_tree->setCurrentItem( module_item );
270 /* Actual apply and save for the preferences */
271 void PrefsDialog::save()
273 if( small->isChecked() && simple_tree->isVisible() )
275 msg_Dbg( p_intf, "Saving the simple preferences" );
276 for( int i = 0 ; i< SPrefsMax; i++ ){
277 if( simple_panels[i] )simple_panels[i]->apply();
280 else if( all->isChecked() && advanced_tree->isVisible() )
282 msg_Dbg( p_intf, "Saving the advanced preferences" );
283 advanced_tree->applyAll();
287 config_SaveConfigFile( p_intf, NULL );
294 void PrefsDialog::destroyPanels()
296 msg_Dbg( p_intf, "Destroying the Panels" );
297 /* Delete the other panel in order to force its reload after clicking
298 on apply. In fact, if we don't do that, the preferences from the other
299 panels won't be accurate, so we would have to recreate the whole dialog,
300 and we don't want that.*/
301 if( small->isChecked() && advanced_panel )
303 /* Deleting only the active panel from the advanced config doesn't work
304 because the data records of PrefsItemData contains still a
305 reference to it only cleanAll() is sure to remove all Panels! */
306 advanced_tree->cleanAll();
307 advanced_panel = NULL;
309 if( all->isChecked() && current_simple_panel )
311 for( int i = 0 ; i< SPrefsMax; i++ )
313 if( simple_panels[i] )
315 delete simple_panels[i];
316 simple_panels[i] = NULL;
319 current_simple_panel = NULL;
324 /* Clean the preferences, dunno if it does something really */
325 void PrefsDialog::cancel()
327 if( small->isChecked() && simple_tree )
329 for( int i = 0 ; i< SPrefsMax; i++ )
330 if( simple_panels[i] ) simple_panels[i]->clean();
332 else if( all->isChecked() && advanced_tree )
334 advanced_tree->cleanAll();
335 advanced_panel = NULL;
340 /* Reset all the preferences, when you click the button */
341 void PrefsDialog::reset()
343 int ret = QMessageBox::question(
345 qtr( "Reset Preferences" ),
346 qtr( "This will reset your VLC media player preferences.\n"
347 "Are you sure you want to continue?" ),
348 QMessageBox::Ok | QMessageBox::Cancel,
351 if( ret == QMessageBox::Ok )
353 config_ResetAll( p_intf );
354 config_SaveConfigFile( p_intf, NULL );
355 /* FIXME reset the panels */