]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/preferences.cpp
Removes trailing spaces. Removes tabs.
[vlc] / modules / gui / qt4 / dialogs / preferences.cpp
1 /*****************************************************************************
2  * preferences.cpp : Preferences
3  *****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include "dialogs/preferences.hpp"
26 #include "dialogs_provider.hpp"
27 #include "util/qvlcframe.hpp"
28
29 #include "components/complete_preferences.hpp"
30 #include "components/simple_preferences.hpp"
31 #include "qt4.hpp"
32
33 #include <QHBoxLayout>
34 #include <QGroupBox>
35 #include <QRadioButton>
36 #include <QVBoxLayout>
37 #include <QPushButton>
38 #include <QCheckBox>
39 #include <QScrollArea>
40 #include <QMessageBox>
41 #include <QDialogButtonBox>
42
43 PrefsDialog *PrefsDialog::instance = NULL;
44
45 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
46 {
47     QGridLayout *main_layout = new QGridLayout( this );
48     setWindowTitle( qtr( "Preferences" ) );
49     resize( 750, 550 );
50
51     /* Create Panels */
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 );
58
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( qtr("Basic"), types );
65     types_l->addWidget( small );
66     all = new QRadioButton( qtr("All"), types ); types_l->addWidget( all );
67     types->setLayout( types_l );
68     small->setChecked( true );
69
70     /* Tree and panel initialisations */
71     advanced_tree = NULL;
72     simple_tree = NULL;
73     simple_panel = NULL;
74     advanced_panel = NULL;
75
76     /* Buttons */
77     QDialogButtonBox *buttonsBox = new QDialogButtonBox();
78     QPushButton *save = new QPushButton( qtr( "&Save" ) );
79     QPushButton *cancel = new QPushButton( qtr( "&Cancel" ) );
80     QPushButton *reset = new QPushButton( qtr( "&Reset Preferences" ) );
81
82     buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
83     buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
84     buttonsBox->addButton( reset, QDialogButtonBox::ActionRole );
85
86     /* Layout  */
87     main_layout->addWidget( tree_panel, 0, 0, 3, 1 );
88     main_layout->addWidget( types, 3, 0, 2, 1 );
89     main_layout->addWidget( main_panel, 0, 1, 4, 1 );
90     main_layout->addWidget( buttonsBox, 4, 1, 1 ,2 );
91
92     main_layout->setColumnMinimumWidth( 0, 150 );
93     main_layout->setColumnStretch( 0, 1 );
94     main_layout->setColumnStretch( 1, 3 );
95
96     main_layout->setRowStretch( 2, 4);
97
98     setLayout( main_layout );
99
100     /* Margins */
101     tree_panel_l->setMargin( 1 );
102     main_panel_l->setMargin( 3 );
103
104     if( config_GetInt( p_intf, "qt-advanced-pref") == 1 )
105     {
106         setAll();
107     }
108     else
109     {
110         setSmall();
111     }
112
113     BUTTONACT( save, save() );
114     BUTTONACT( cancel, cancel() );
115     BUTTONACT( reset, reset() );
116     BUTTONACT( small, setSmall() );
117     BUTTONACT( all, setAll() );
118
119     for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
120 }
121
122 void PrefsDialog::setAll()
123 {
124     if( simple_tree )
125     {
126         tree_panel_l->removeWidget( simple_tree );
127         simple_tree->hide();
128     }
129
130     if( !advanced_tree )
131     {
132          advanced_tree = new PrefsTree( p_intf, tree_panel );
133          CONNECT( advanced_tree,
134                   currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *),
135                   this, changePanel( QTreeWidgetItem * ) );
136     }
137     tree_panel_l->addWidget( advanced_tree );
138     advanced_tree->show();
139
140     if( simple_panel )
141     {
142         main_panel_l->removeWidget( simple_panel );
143         simple_panel->hide();
144     }
145     if( !advanced_panel )
146          advanced_panel = new PrefsPanel( main_panel );
147     main_panel_l->addWidget( advanced_panel );
148     all->setChecked( true );
149     advanced_panel->show();
150 }
151
152 void PrefsDialog::setSmall()
153 {
154     if( advanced_tree )
155     {
156         tree_panel_l->removeWidget( advanced_tree );
157         advanced_tree->hide();
158     }
159     if( !simple_tree )
160     {
161          simple_tree = new SPrefsCatList( p_intf, tree_panel );
162          CONNECT( simple_tree,
163                   currentItemChanged( int ),
164                   this,  changeSimplePanel( int ) );
165     }
166     tree_panel_l->addWidget( simple_tree );
167     simple_tree->show();
168
169     if( advanced_panel )
170     {
171         main_panel_l->removeWidget( advanced_panel );
172         advanced_panel->hide();
173     }
174     if( !simple_panel )
175         simple_panel = new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
176     main_panel_l->addWidget( simple_panel );
177     small->setChecked( true );
178     simple_panel->show();
179 }
180
181 void PrefsDialog::changeSimplePanel( int number )
182 {
183     if( simple_panel )
184     {
185         main_panel_l->removeWidget( simple_panel );
186         simple_panel->hide();
187     }
188     simple_panel = simple_panels[number];
189     if( !simple_panel )
190     {
191         simple_panel = new SPrefsPanel( p_intf, main_panel, number );
192         simple_panels[number] = simple_panel;
193     }
194     main_panel_l->addWidget( simple_panel );
195     simple_panel->show();
196 }
197
198 void PrefsDialog::changePanel( QTreeWidgetItem *item )
199 {
200     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
201
202     if( advanced_panel )
203     {
204         main_panel_l->removeWidget( advanced_panel );
205         advanced_panel->hide();
206     }
207     if( !data->panel )
208         data->panel = new PrefsPanel( p_intf, main_panel , data );
209
210     advanced_panel = data->panel;
211     main_panel_l->addWidget( advanced_panel );
212     advanced_panel->show();
213 }
214
215 void PrefsDialog::showModulePrefs( char *psz_module )
216 {
217     setAll();
218     all->setChecked( true );
219     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
220          i_cat_index++ )
221     {
222         QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
223         PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
224                                                    value<PrefsItemData *>();
225         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
226                                   i_sc_index++ )
227         {
228             QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
229             PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
230                                                     value<PrefsItemData *>();
231             for( int i_module = 0; i_module < subcat_item->childCount();
232                                    i_module++ )
233             {
234                 QTreeWidgetItem *module_item = subcat_item->child( i_module );
235                 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
236                                                     value<PrefsItemData *>();
237                 if( !strcmp( mod_data->psz_name, psz_module ) ) {
238                     advanced_tree->setCurrentItem( module_item );
239                 }
240             }
241         }
242     }
243     show();
244 }
245
246 void PrefsDialog::save()
247 {
248     apply();
249     hide();
250 }
251
252 void PrefsDialog::apply()
253 {
254     if( small->isChecked() && simple_tree )
255     {
256         for( int i = 0 ; i< SPrefsMax; i++ )
257             if( simple_panels[i] ) simple_panels[i]->apply();
258     }
259     else if( all->isChecked() && advanced_tree )
260         advanced_tree->applyAll();
261     config_SaveConfigFile( p_intf, NULL );
262
263     /* Delete the other panel in order to force its reload after clicking
264        on apply - UGLY but will work for now. */
265     if( simple_panel && simple_panel->isVisible() && advanced_panel )
266     {
267         delete advanced_panel;
268         advanced_panel = NULL;
269     }
270     if( advanced_panel && advanced_panel->isVisible() && simple_panel )
271     {
272         delete simple_panel;
273         simple_panel = NULL;
274     }
275 }
276
277 void PrefsDialog::cancel()
278 {
279     if( small->isChecked() && simple_tree )
280     {
281         for( int i = 0 ; i< SPrefsMax; i++ )
282             if( simple_panels[i] ) simple_panels[i]->clean();
283     }
284     else if( all->isChecked() && advanced_tree )
285     {
286         advanced_tree->cleanAll();
287         advanced_panel = NULL;
288     }
289     hide();
290 }
291
292 void PrefsDialog::reset()
293 {
294     int ret = QMessageBox::question(this, qtr("Reset Preferences"),
295                  qtr("This will reset your VLC media player preferences.\n"
296                          "Are you sure you want to continue?"),
297                   QMessageBox::Ok | QMessageBox::Cancel,
298                                                          QMessageBox::Ok);
299     if ( ret == QMessageBox::Ok )
300     {
301         config_ResetAll( p_intf );
302         config_SaveConfigFile( p_intf, NULL );
303     }
304 }