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 *****************************************************************************/
29 #include "dialogs/preferences.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 <QPushButton>
39 #include <QMessageBox>
40 #include <QDialogButtonBox>
42 PrefsDialog *PrefsDialog::instance = NULL;
44 PrefsDialog::PrefsDialog( QWidget *parent, intf_thread_t *_p_intf )
45 : QVLCDialog( parent, _p_intf )
47 QGridLayout *main_layout = new QGridLayout( this );
48 setWindowTitle( qtr( "Preferences" ) );
50 /* Whether we want it or not, we need to destroy on close to get
51 consistency when reset */
52 setAttribute( Qt::WA_DeleteOnClose );
55 tree_panel = new QWidget;
56 tree_panel_l = new QHBoxLayout;
57 tree_panel->setLayout( tree_panel_l );
58 main_panel = new QWidget;
59 main_panel_l = new QHBoxLayout;
60 main_panel->setLayout( main_panel_l );
62 /* Choice for types */
63 types = new QGroupBox( qtr("Show settings") );
64 types->setAlignment( Qt::AlignHCenter );
65 QHBoxLayout *types_l = new QHBoxLayout;
66 types_l->setSpacing( 3 ); types_l->setMargin( 3 );
67 small = new QRadioButton( qtr( "Simple" ), types );
68 small->setToolTip( qtr( "Switch to simple preferences view" ) );
69 types_l->addWidget( small );
70 all = new QRadioButton( qtr("All"), types ); types_l->addWidget( all );
71 all->setToolTip( qtr( "Switch to full preferences view" ) );
72 types->setLayout( types_l );
73 small->setChecked( true );
75 /* Tree and panel initialisations */
78 current_simple_panel = NULL;
79 advanced_panel = NULL;
82 QDialogButtonBox *buttonsBox = new QDialogButtonBox();
83 QPushButton *save = new QPushButton( qtr( "&Save" ) );
84 QPushButton *cancel = new QPushButton( qtr( "&Cancel" ) );
85 QPushButton *reset = new QPushButton( qtr( "&Reset Preferences" ) );
87 buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
88 buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
89 buttonsBox->addButton( reset, QDialogButtonBox::ResetRole );
92 main_layout->addWidget( tree_panel, 0, 0, 3, 1 );
93 main_layout->addWidget( types, 3, 0, 2, 1 );
94 main_layout->addWidget( main_panel, 0, 1, 4, 2 );
95 main_layout->addWidget( buttonsBox, 4, 2, 1 ,1 );
97 main_layout->setColumnMinimumWidth( 0, 150 );
98 main_layout->setColumnMinimumWidth( 1, 10 );
99 main_layout->setColumnStretch( 0, 1 );
100 main_layout->setColumnStretch( 1, 0 );
101 main_layout->setColumnStretch( 2, 3 );
103 main_layout->setRowStretch( 2, 4 );
105 main_layout->setMargin( 9 );
106 setLayout( main_layout );
109 tree_panel_l->setMargin( 1 );
110 main_panel_l->setLayoutMargins( 6, 0, 0, 3, 3 );
112 for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
114 if( config_GetInt( p_intf, "qt-advanced-pref" ) == 1 )
119 BUTTONACT( save, save() );
120 BUTTONACT( cancel, cancel() );
121 BUTTONACT( reset, reset() );
123 BUTTONACT( small, setSmall() );
124 BUTTONACT( all, setAdvanced() );
126 resize( 750, sizeHint().height() );
129 void PrefsDialog::setAdvanced()
131 /* We already have a simple TREE, and we just want to hide it */
133 if( simple_tree->isVisible() ) simple_tree->hide();
135 /* If don't have already and advanced TREE, then create it */
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 );
148 advanced_tree->show();
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();
154 /* If no advanced Panel exist, create one, attach it and show it*/
155 if( !advanced_panel )
157 advanced_panel = new AdvPrefsPanel( main_panel );
158 main_panel_l->addWidget( advanced_panel );
160 advanced_panel->show();
162 /* Select the first Item of the preferences. Maybe you want to select a specified
164 advanced_tree->setCurrentIndex(
165 advanced_tree->model()->index( 0, 0, QModelIndex() ) );
167 all->setChecked( true );
170 void PrefsDialog::setSmall()
172 /* If an advanced TREE exists, remove and hide it */
174 if( advanced_tree->isVisible() ) advanced_tree->hide();
176 /* If no simple_tree, create one, connect it */
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 );
189 /* If an Advanced PANEL exists, remove it */
191 if( advanced_panel->isVisible() ) advanced_panel->hide();
193 if( !current_simple_panel )
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 );
201 current_simple_panel->show();
202 small->setChecked( true );
205 /* Switching from on simple panel to another */
206 void PrefsDialog::changeSimplePanel( int number )
208 if( current_simple_panel )
209 if( current_simple_panel->isVisible() ) current_simple_panel->hide();
211 current_simple_panel = simple_panels[number];
212 if( !current_simple_panel )
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 );
219 current_simple_panel->show();
222 /* Changing from one Advanced Panel to another */
223 void PrefsDialog::changeAdvPanel( QTreeWidgetItem *item )
225 if( item == NULL ) return;
226 PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
229 if( advanced_panel->isVisible() ) advanced_panel->hide();
233 data->panel = new AdvPrefsPanel( p_intf, main_panel , data );
234 main_panel_l->addWidget( data->panel );
237 advanced_panel = data->panel;
238 advanced_panel->show();
242 /*Called from extended settings, is not used anymore, but could be useful one day*/
243 void PrefsDialog::showModulePrefs( char *psz_module )
246 all->setChecked( true );
247 for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
250 QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
251 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
252 value<PrefsItemData *>();
253 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
256 QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
257 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
258 value<PrefsItemData *>();
259 for( int i_module = 0; i_module < subcat_item->childCount();
262 QTreeWidgetItem *module_item = subcat_item->child( i_module );
263 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
264 value<PrefsItemData *>();
265 if( !strcmp( mod_data->psz_name, psz_module ) ) {
266 advanced_tree->setCurrentItem( module_item );
275 /* Actual apply and save for the preferences */
276 void PrefsDialog::save()
278 if( small->isChecked() && simple_tree->isVisible() )
280 msg_Dbg( p_intf, "Saving the simple preferences" );
281 for( int i = 0 ; i< SPrefsMax; i++ ){
282 if( simple_panels[i] )simple_panels[i]->apply();
285 else if( all->isChecked() && advanced_tree->isVisible() )
287 msg_Dbg( p_intf, "Saving the advanced preferences" );
288 advanced_tree->applyAll();
292 config_SaveConfigFile( p_intf, NULL );
299 void PrefsDialog::destroyPanels()
301 msg_Dbg( p_intf, "Destroying the Panels" );
302 /* Delete the other panel in order to force its reload after clicking
303 on apply. In fact, if we don't do that, the preferences from the other
304 panels won't be accurate, so we would have to recreate the whole dialog,
305 and we don't want that.*/
306 if( small->isChecked() && advanced_panel )
308 /* Deleting only the active panel from the advanced config doesn't work
309 because the data records of PrefsItemData contains still a
310 reference to it only cleanAll() is sure to remove all Panels! */
311 advanced_tree->cleanAll();
312 advanced_panel = NULL;
314 if( all->isChecked() && current_simple_panel )
316 for( int i = 0 ; i< SPrefsMax; i++ )
318 if( simple_panels[i] )
320 delete simple_panels[i];
321 simple_panels[i] = NULL;
324 current_simple_panel = NULL;
329 /* Clean the preferences, dunno if it does something really */
330 void PrefsDialog::cancel()
332 if( small->isChecked() && simple_tree )
334 for( int i = 0 ; i< SPrefsMax; i++ )
335 if( simple_panels[i] ) simple_panels[i]->clean();
337 else if( all->isChecked() && advanced_tree )
339 advanced_tree->cleanAll();
340 advanced_panel = NULL;
345 /* Reset all the preferences, when you click the button */
346 void PrefsDialog::reset()
348 int ret = QMessageBox::question(
350 qtr( "Reset Preferences" ),
351 qtr( "Are you sure you want to reset your VLC media player preferences?" ),
352 QMessageBox::Ok | QMessageBox::Cancel,
355 if( ret == QMessageBox::Ok )
357 config_ResetAll( p_intf );
358 config_SaveConfigFile( p_intf, NULL );
359 getSettings()->clear();
363 PrefsDialog::getInstance( p_intf )->show();