]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/preferences.cpp
Qt4 - Preferences: Remove Apply button, and fix a few segfaults with Hannes Domani...
[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( "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 );
68
69     /* Tree and panel initialisations */
70     advanced_tree = NULL;
71     simple_tree = NULL;
72     simple_panel = NULL;
73     advanced_panel = NULL;
74
75     /* Buttons */
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" ) );
80
81     buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
82     buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
83     buttonsBox->addButton( reset, QDialogButtonBox::ActionRole );
84
85     /* Layout  */
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 );
90
91     main_layout->setColumnMinimumWidth( 0, 150 );
92     main_layout->setColumnStretch( 0, 1 );
93     main_layout->setColumnStretch( 1, 3 );
94
95     main_layout->setRowStretch( 2, 4);
96
97     setLayout( main_layout );
98
99     /* Margins */
100     tree_panel_l->setMargin( 1 );
101     main_panel_l->setMargin( 3 );
102
103     if( config_GetInt( p_intf, "qt-advanced-pref") == 1 )
104     {
105         setAll();
106     }
107     else
108     {
109         setSmall();
110     }
111
112     BUTTONACT( save, save() );
113     BUTTONACT( cancel, cancel() );
114     BUTTONACT( reset, reset() );
115     BUTTONACT( small, setSmall() );
116     BUTTONACT( all, setAll() );
117
118     for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
119 }
120
121 void PrefsDialog::setAll()
122 {
123     if( simple_tree )
124     {
125         tree_panel_l->removeWidget( simple_tree );
126         simple_tree->hide();
127     }
128
129     if( !advanced_tree )
130     {
131          advanced_tree = new PrefsTree( p_intf, tree_panel );
132          CONNECT( advanced_tree,
133                   currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *),
134                   this, changePanel( QTreeWidgetItem * ) );
135     }
136     tree_panel_l->addWidget( advanced_tree );
137     advanced_tree->show();
138
139     if( simple_panel )
140     {
141         main_panel_l->removeWidget( simple_panel );
142         simple_panel->hide();
143     }
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();
149 }
150
151 void PrefsDialog::setSmall()
152 {
153     if( advanced_tree )
154     {
155         tree_panel_l->removeWidget( advanced_tree );
156         advanced_tree->hide();
157     }
158     if( !simple_tree )
159     {
160          simple_tree = new SPrefsCatList( p_intf, tree_panel );
161          CONNECT( simple_tree,
162                   currentItemChanged( int ),
163                   this,  changeSimplePanel( int ) );
164     }
165     tree_panel_l->addWidget( simple_tree );
166     simple_tree->show();
167
168     if( advanced_panel )
169     {
170         main_panel_l->removeWidget( advanced_panel );
171         advanced_panel->hide();
172     }
173     if( !simple_panel )
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();
178 }
179
180 void PrefsDialog::changeSimplePanel( int number )
181 {
182     if( simple_panel )
183     {
184         main_panel_l->removeWidget( simple_panel );
185         simple_panel->hide();
186     }
187     simple_panel = simple_panels[number];
188     if( !simple_panel )
189     {
190         simple_panel = new SPrefsPanel( p_intf, main_panel, number );
191         simple_panels[number] = simple_panel;
192     }
193     main_panel_l->addWidget( simple_panel );
194     simple_panel->show();
195 }
196
197 void PrefsDialog::changePanel( QTreeWidgetItem *item )
198 {
199     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
200
201     if( advanced_panel )
202     {
203         main_panel_l->removeWidget( advanced_panel );
204         advanced_panel->hide();
205     }
206     if( !data->panel )
207         data->panel = new PrefsPanel( p_intf, main_panel , data );
208
209     advanced_panel = data->panel;
210     main_panel_l->addWidget( advanced_panel );
211     advanced_panel->show();
212 }
213
214 void PrefsDialog::showModulePrefs( char *psz_module )
215 {
216     setAll();
217     all->setChecked( true );
218     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
219          i_cat_index++ )
220     {
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();
225                                   i_sc_index++ )
226         {
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();
231                                    i_module++ )
232             {
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 );
238                 }
239             }
240         }
241     }
242     show();
243 }
244
245 void PrefsDialog::save()
246 {
247     apply();
248     hide();
249 }
250
251 void PrefsDialog::apply()
252 {
253     if( small->isChecked() && simple_tree )
254     {
255         for( int i = 0 ; i< SPrefsMax; i++ )
256             if( simple_panels[i] ) simple_panels[i]->apply();
257     }
258     else if( all->isChecked() && advanced_tree )
259         advanced_tree->applyAll();
260     config_SaveConfigFile( p_intf, NULL );
261
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 )
265     {
266         delete advanced_panel;
267         advanced_panel = NULL;
268     }
269     if( advanced_panel && advanced_panel->isVisible() && simple_panel )
270     {
271         delete simple_panel;
272         simple_panel = NULL;
273     }
274 }
275
276 void PrefsDialog::cancel()
277 {
278     if( small->isChecked() && simple_tree )
279     {
280         for( int i = 0 ; i< SPrefsMax; i++ )
281             if( simple_panels[i] ) simple_panels[i]->clean();
282     }
283     else if( all->isChecked() && advanced_tree )
284     {
285         advanced_tree->cleanAll();
286         advanced_panel = NULL;
287     }
288     hide();
289 }
290
291 void PrefsDialog::reset()
292 {
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,
297                                                          QMessageBox::Ok);
298     if ( ret == QMessageBox::Ok )
299     {
300         config_ResetAll( p_intf );
301         config_SaveConfigFile( p_intf, NULL );
302     }
303 }