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 <QScrollArea>
39 #include <QMessageBox>
40 #include <QDialogButtonBox>
42 PrefsDialog *PrefsDialog::instance = NULL;
44 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
46 QGridLayout *main_layout = new QGridLayout( this );
47 setWindowTitle( qtr( "Preferences" ) );
51 tree_panel = new QWidget;
52 tree_panel_l = new QHBoxLayout;
53 tree_panel->setLayout( tree_panel_l );
54 main_panel = new QWidget;
55 main_panel_l = new QHBoxLayout;
56 main_panel->setLayout( main_panel_l );
58 /* Choice for types */
59 types = new QGroupBox( "Show settings" );
60 types->setAlignment( Qt::AlignHCenter );
61 QHBoxLayout *types_l = new QHBoxLayout;
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 );
69 /* Tree and panel initialisations */
72 current_simple_panel = NULL;
73 advanced_panel = NULL;
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" ) );
81 buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
82 buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
83 buttonsBox->addButton( reset, QDialogButtonBox::ActionRole );
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 );
91 main_layout->setColumnMinimumWidth( 0, 150 );
92 main_layout->setColumnStretch( 0, 1 );
93 main_layout->setColumnStretch( 1, 3 );
95 main_layout->setRowStretch( 2, 4);
97 setLayout( main_layout );
100 tree_panel_l->setMargin( 1 );
101 main_panel_l->setMargin( 3 );
103 for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
105 if( config_GetInt( p_intf, "qt-advanced-pref") == 1 )
110 BUTTONACT( save, save() );
111 BUTTONACT( cancel, cancel() );
112 BUTTONACT( reset, reset() );
114 BUTTONACT( small, setSmall() );
115 BUTTONACT( all, setAdvanced() );
118 void PrefsDialog::setAdvanced()
120 /* We already have a simple TREE, and we just want to hide it */
122 if( simple_tree->isVisible() ) simple_tree->hide();
124 /* If don't have already and advanced TREE, then create it */
128 advanced_tree = new PrefsTree( p_intf, tree_panel );
129 /* and connections */
130 CONNECT( advanced_tree,
131 currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *),
132 this, changeAdvPanel( QTreeWidgetItem * ) );
133 tree_panel_l->addWidget( advanced_tree );
137 advanced_tree->show();
139 /* Remove the simple current panel from the main panels*/
140 if( current_simple_panel )
141 if( current_simple_panel->isVisible() ) current_simple_panel->hide();
143 /* If no advanced Panel exist, create one, attach it and show it*/
144 if( !advanced_panel )
146 advanced_panel = new AdvPrefsPanel( main_panel );
147 main_panel_l->addWidget( advanced_panel );
149 advanced_panel->show();
151 all->setChecked( true );
154 void PrefsDialog::setSmall()
156 /* If an advanced TREE exists, remove and hide it */
158 if( advanced_tree->isVisible() ) advanced_tree->hide();
160 /* If no simple_tree, create one, connect it */
163 simple_tree = new SPrefsCatList( p_intf, tree_panel );
164 CONNECT( simple_tree,
165 currentItemChanged( int ),
166 this, changeSimplePanel( int ) );
167 tree_panel_l->addWidget( simple_tree );
173 /* If an Advanced PANEL exists, remove it */
175 if( advanced_panel->isVisible() ) advanced_panel->hide();
177 if( !current_simple_panel )
179 current_simple_panel = 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();
214 data->panel = new AdvPrefsPanel( p_intf, main_panel , data );
216 advanced_panel = data->panel;
217 main_panel_l->addWidget( advanced_panel );
218 advanced_panel->show();
222 /*Called from extended settings, is not used anymore, but could be useful one day*/
223 void PrefsDialog::showModulePrefs( char *psz_module )
226 all->setChecked( true );
227 for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
230 QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
231 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
232 value<PrefsItemData *>();
233 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
236 QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
237 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
238 value<PrefsItemData *>();
239 for( int i_module = 0; i_module < subcat_item->childCount();
242 QTreeWidgetItem *module_item = subcat_item->child( i_module );
243 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
244 value<PrefsItemData *>();
245 if( !strcmp( mod_data->psz_name, psz_module ) ) {
246 advanced_tree->setCurrentItem( module_item );
255 /* Actual apply and save for the preferences */
256 void PrefsDialog::save()
258 if( small->isChecked() && simple_tree->isVisible() )
260 msg_Dbg( p_intf, "Saving the simple preferences" );
261 for( int i = 0 ; i< SPrefsMax; i++ ){
262 if( simple_panels[i] )simple_panels[i]->apply();
265 else if( all->isChecked() && advanced_tree->isVisible() )
267 msg_Dbg( p_intf, "Saving the advanced preferences" );
268 advanced_tree->applyAll();
272 config_SaveConfigFile( p_intf, NULL );
274 /* Delete the other panel in order to force its reload after clicking
275 on apply - UGLY but will work for now. */
276 /* THIS CODE IS STILL WRONG, FIXME */
277 if( current_simple_panel && current_simple_panel->isVisible() && advanced_panel )
279 delete advanced_panel;
280 advanced_panel = NULL;
282 if( advanced_panel && advanced_panel->isVisible() && current_simple_panel )
284 delete current_simple_panel;
285 current_simple_panel = NULL;
291 /* Clean the preferences, dunno if it does something really */
292 void PrefsDialog::cancel()
294 if( small->isChecked() && simple_tree )
296 for( int i = 0 ; i< SPrefsMax; i++ )
297 if( simple_panels[i] ) simple_panels[i]->clean();
299 else if( all->isChecked() && advanced_tree )
301 advanced_tree->cleanAll();
302 advanced_panel = NULL;
307 /* Reset all the preferences, when you click the button */
308 void PrefsDialog::reset()
310 int ret = QMessageBox::question(this, qtr("Reset Preferences"),
311 qtr("This will reset your VLC media player preferences.\n"
312 "Are you sure you want to continue?"),
313 QMessageBox::Ok | QMessageBox::Cancel,
315 if ( ret == QMessageBox::Ok )
317 config_ResetAll( p_intf );
318 config_SaveConfigFile( p_intf, NULL );