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( QWidget *parent, intf_thread_t *_p_intf )
43 : QVLCDialog( parent, _p_intf )
45 QGridLayout *main_layout = new QGridLayout( this );
46 setWindowTitle( qtr( "Preferences" ) );
47 setWindowRole( "vlc-preferences" );
49 /* Whether we want it or not, we need to destroy on close to get
50 consistency when reset */
51 setAttribute( Qt::WA_DeleteOnClose );
54 tree_panel = new QWidget;
55 tree_panel_l = new QHBoxLayout;
56 tree_panel->setLayout( tree_panel_l );
57 main_panel = new QWidget;
58 main_panel_l = new QHBoxLayout;
59 main_panel->setLayout( main_panel_l );
61 /* Choice for types */
62 types = new QGroupBox( qtr("Show settings") );
63 types->setAlignment( Qt::AlignHCenter );
64 QHBoxLayout *types_l = new QHBoxLayout;
65 types_l->setSpacing( 3 ); types_l->setMargin( 3 );
66 small = new QRadioButton( qtr( "Simple" ), types );
67 small->setToolTip( qtr( "Switch to simple preferences view" ) );
68 types_l->addWidget( small );
69 all = new QRadioButton( qtr("All"), types ); types_l->addWidget( all );
70 all->setToolTip( qtr( "Switch to full preferences view" ) );
71 types->setLayout( types_l );
72 small->setChecked( true );
74 /* Tree and panel initialisations */
77 current_simple_panel = NULL;
78 advanced_panel = NULL;
81 QDialogButtonBox *buttonsBox = new QDialogButtonBox();
82 QPushButton *save = new QPushButton( qtr( "&Save" ) );
83 save->setToolTip( qtr( "Save and close the dialog" ) );
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, 10 );
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 b_small = (p_intf->p_sys->i_screenHeight < 750);
113 if( b_small ) msg_Dbg( p_intf, "Small");
114 setMaximumHeight( p_intf->p_sys->i_screenHeight );
115 for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
117 if( config_GetInt( p_intf, "qt-advanced-pref" ) || config_GetInt( p_intf, "advanced" ) )
122 BUTTONACT( save, save() );
123 BUTTONACT( cancel, cancel() );
124 BUTTONACT( reset, reset() );
126 BUTTONACT( small, setSmall() );
127 BUTTONACT( all, setAdvanced() );
129 resize( 780, sizeHint().height() );
132 void PrefsDialog::setAdvanced()
134 /* We already have a simple TREE, and we just want to hide it */
136 if( simple_tree->isVisible() ) simple_tree->hide();
138 /* If don't have already and advanced TREE, then create it */
142 advanced_tree = new PrefsTree( p_intf, tree_panel );
143 /* and connections */
144 CONNECT( advanced_tree,
145 currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem * ),
146 this, changeAdvPanel( QTreeWidgetItem * ) );
147 tree_panel_l->addWidget( advanced_tree );
151 advanced_tree->show();
153 /* Remove the simple current panel from the main panels*/
154 if( current_simple_panel )
155 if( current_simple_panel->isVisible() ) current_simple_panel->hide();
157 /* If no advanced Panel exist, create one, attach it and show it*/
158 if( !advanced_panel )
160 advanced_panel = new AdvPrefsPanel( main_panel );
161 main_panel_l->addWidget( advanced_panel );
163 advanced_panel->show();
165 /* Select the first Item of the preferences. Maybe you want to select a specified
167 advanced_tree->setCurrentIndex(
168 advanced_tree->model()->index( 0, 0, QModelIndex() ) );
170 all->setChecked( true );
173 void PrefsDialog::setSmall()
175 /* If an advanced TREE exists, remove and hide it */
177 if( advanced_tree->isVisible() ) advanced_tree->hide();
179 /* If no simple_tree, create one, connect it */
182 simple_tree = new SPrefsCatList( p_intf, tree_panel, b_small );
183 CONNECT( simple_tree,
184 currentItemChanged( int ),
185 this, changeSimplePanel( int ) );
186 tree_panel_l->addWidget( simple_tree );
192 /* If an Advanced PANEL exists, remove it */
194 if( advanced_panel->isVisible() ) advanced_panel->hide();
196 if( !current_simple_panel )
198 current_simple_panel =
199 new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat, b_small );
200 simple_panels[SPrefsDefaultCat] = current_simple_panel;
201 main_panel_l->addWidget( current_simple_panel );
204 current_simple_panel->show();
205 small->setChecked( true );
208 /* Switching from on simple panel to another */
209 void PrefsDialog::changeSimplePanel( int number )
211 if( current_simple_panel )
212 if( current_simple_panel->isVisible() ) current_simple_panel->hide();
214 current_simple_panel = simple_panels[number];
215 if( !current_simple_panel )
217 current_simple_panel = new SPrefsPanel( p_intf, main_panel, number, b_small );
218 simple_panels[number] = current_simple_panel;
219 main_panel_l->addWidget( current_simple_panel );
222 current_simple_panel->show();
225 /* Changing from one Advanced Panel to another */
226 void PrefsDialog::changeAdvPanel( QTreeWidgetItem *item )
228 if( item == NULL ) return;
229 PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
232 if( advanced_panel->isVisible() ) advanced_panel->hide();
236 data->panel = new AdvPrefsPanel( p_intf, main_panel , data );
237 main_panel_l->addWidget( data->panel );
240 advanced_panel = data->panel;
241 advanced_panel->show();
245 /*Called from extended settings, is not used anymore, but could be useful one day*/
246 void PrefsDialog::showModulePrefs( char *psz_module )
249 all->setChecked( true );
250 for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
253 QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
254 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
255 value<PrefsItemData *>();
256 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
259 QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
260 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
261 value<PrefsItemData *>();
262 for( int i_module = 0; i_module < subcat_item->childCount();
265 QTreeWidgetItem *module_item = subcat_item->child( i_module );
266 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
267 value<PrefsItemData *>();
268 if( !strcmp( mod_data->psz_name, psz_module ) ) {
269 advanced_tree->setCurrentItem( module_item );
278 /* Actual apply and save for the preferences */
279 void PrefsDialog::save()
281 if( small->isChecked() && simple_tree->isVisible() )
283 msg_Dbg( p_intf, "Saving the simple preferences" );
284 for( int i = 0 ; i< SPrefsMax; i++ ){
285 if( simple_panels[i] )simple_panels[i]->apply();
288 else if( all->isChecked() && advanced_tree->isVisible() )
290 msg_Dbg( p_intf, "Saving the advanced preferences" );
291 advanced_tree->applyAll();
295 config_SaveConfigFile( p_intf, NULL );
299 /* Clean the preferences, dunno if it does something really */
300 void PrefsDialog::cancel()
305 /* Reset all the preferences, when you click the button */
306 void PrefsDialog::reset()
308 int ret = QMessageBox::question(
310 qtr( "Reset Preferences" ),
311 qtr( "Are you sure you want to reset your VLC media player preferences?" ),
312 QMessageBox::Ok | QMessageBox::Cancel,
315 if( ret == QMessageBox::Ok )
317 config_ResetAll( p_intf );
318 config_SaveConfigFile( p_intf, NULL );
319 getSettings()->clear();