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"
33 #include <QHBoxLayout>
35 #include <QRadioButton>
36 #include <QVBoxLayout>
37 #include <QPushButton>
39 #include <QScrollArea>
40 #include <QMessageBox>
41 #include <QDialogButtonBox>
43 PrefsDialog *PrefsDialog::instance = NULL;
45 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
47 QGridLayout *main_layout = new QGridLayout( this );
48 setWindowTitle( qtr( "Preferences" ) );
52 tree_panel = new QWidget( 0 );
53 tree_panel_l = new QHBoxLayout;
54 tree_panel->setLayout( tree_panel_l );
55 main_panel = new QWidget( 0 );
56 main_panel_l = new QHBoxLayout;
57 main_panel->setLayout( main_panel_l );
59 /* Choice for types */
60 types = new QGroupBox( "Show settings" );
61 types->setAlignment( Qt::AlignHCenter );
62 QHBoxLayout *types_l = new QHBoxLayout(0);
63 types_l->setSpacing( 3 ); types_l->setMargin( 3 );
64 small = new QRadioButton( "Basic", types ); types_l->addWidget( small );
65 all = new QRadioButton( "All", types ); types_l->addWidget( all );
66 types->setLayout( types_l );
67 small->setChecked( true );
69 /* Tree and panel initialisations */
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 if( config_GetInt( p_intf, "qt-advanced-pref") == 1 )
112 BUTTONACT( save, save() );
113 BUTTONACT( cancel, cancel() );
114 BUTTONACT( reset, reset() );
115 BUTTONACT( small, setSmall() );
116 BUTTONACT( all, setAll() );
118 for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
121 void PrefsDialog::setAll()
125 tree_panel_l->removeWidget( simple_tree );
131 advanced_tree = new PrefsTree( p_intf, tree_panel );
132 CONNECT( advanced_tree,
133 currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *),
134 this, changePanel( QTreeWidgetItem * ) );
136 tree_panel_l->addWidget( advanced_tree );
137 advanced_tree->show();
141 main_panel_l->removeWidget( simple_panel );
142 simple_panel->hide();
144 if( !advanced_panel )
145 advanced_panel = new PrefsPanel( main_panel );
146 main_panel_l->addWidget( advanced_panel );
147 all->setChecked( true );
148 advanced_panel->show();
151 void PrefsDialog::setSmall()
155 tree_panel_l->removeWidget( advanced_tree );
156 advanced_tree->hide();
160 simple_tree = new SPrefsCatList( p_intf, tree_panel );
161 CONNECT( simple_tree,
162 currentItemChanged( int ),
163 this, changeSimplePanel( int ) );
165 tree_panel_l->addWidget( simple_tree );
170 main_panel_l->removeWidget( advanced_panel );
171 advanced_panel->hide();
174 simple_panel = new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
175 main_panel_l->addWidget( simple_panel );
176 small->setChecked( true );
177 simple_panel->show();
180 void PrefsDialog::changeSimplePanel( int number )
184 main_panel_l->removeWidget( simple_panel );
185 simple_panel->hide();
187 simple_panel = simple_panels[number];
190 simple_panel = new SPrefsPanel( p_intf, main_panel, number );
191 simple_panels[number] = simple_panel;
193 main_panel_l->addWidget( simple_panel );
194 simple_panel->show();
197 void PrefsDialog::changePanel( QTreeWidgetItem *item )
199 PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
203 main_panel_l->removeWidget( advanced_panel );
204 advanced_panel->hide();
207 data->panel = new PrefsPanel( p_intf, main_panel , data );
209 advanced_panel = data->panel;
210 main_panel_l->addWidget( advanced_panel );
211 advanced_panel->show();
214 void PrefsDialog::showModulePrefs( char *psz_module )
217 all->setChecked( true );
218 for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
221 QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
222 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
223 value<PrefsItemData *>();
224 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
227 QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
228 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
229 value<PrefsItemData *>();
230 for( int i_module = 0; i_module < subcat_item->childCount();
233 QTreeWidgetItem *module_item = subcat_item->child( i_module );
234 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
235 value<PrefsItemData *>();
236 if( !strcmp( mod_data->psz_name, psz_module ) ) {
237 advanced_tree->setCurrentItem( module_item );
245 void PrefsDialog::save()
251 void PrefsDialog::apply()
253 if( small->isChecked() && simple_tree )
255 for( int i = 0 ; i< SPrefsMax; i++ )
256 if( simple_panels[i] ) simple_panels[i]->apply();
258 else if( all->isChecked() && advanced_tree )
259 advanced_tree->applyAll();
260 config_SaveConfigFile( p_intf, NULL );
262 /* Delete the other panel in order to force its reload after clicking
263 on apply - UGLY but will work for now. */
264 if( simple_panel && simple_panel->isVisible() && advanced_panel )
266 delete advanced_panel;
267 advanced_panel = NULL;
269 if( advanced_panel && advanced_panel->isVisible() && simple_panel )
276 void PrefsDialog::cancel()
278 if( small->isChecked() && simple_tree )
280 for( int i = 0 ; i< SPrefsMax; i++ )
281 if( simple_panels[i] ) simple_panels[i]->clean();
283 else if( all->isChecked() && advanced_tree )
285 advanced_tree->cleanAll();
286 advanced_panel = NULL;
291 void PrefsDialog::reset()
293 int ret = QMessageBox::question(this, qtr("Reset Preferences"),
294 qtr("This will reset your VLC media player preferences.\n"
295 "Are you sure you want to continue?"),
296 QMessageBox::Ok | QMessageBox::Cancel,
298 if ( ret == QMessageBox::Ok )
300 config_ResetAll( p_intf );
301 config_SaveConfigFile( p_intf, NULL );