]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/preferences.cpp
Qt4 - Preferences. Add an apply button and warnings to force saving
[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     types->setToolTip( qtr("You have to apply the preferences before\n"
63                             "switching settings details if you want\n"
64                             "to keep them") );
65     QHBoxLayout *types_l = new QHBoxLayout(0);
66     types_l->setSpacing( 3 ); types_l->setMargin( 3 );
67     small = new QRadioButton( "Basic", types ); types_l->addWidget( small );
68     all = new QRadioButton( "All", types ); types_l->addWidget( all );
69     types->setLayout( types_l );
70     small->setChecked( true );
71
72     /* Tree and panel initialisations */
73     advanced_tree = NULL;
74     simple_tree = NULL;
75     simple_panel = NULL;
76     advanced_panel = NULL;
77
78     /* Buttons */
79     QDialogButtonBox *buttonsBox = new QDialogButtonBox();
80     QPushButton *save = new QPushButton( qtr( "&Save" ) );
81     QPushButton *apply = new QPushButton( qtr( "&Apply" ) );
82     QPushButton *cancel = new QPushButton( qtr( "&Cancel" ) );
83     QPushButton *reset = new QPushButton( qtr( "&Reset Preferences" ) );
84
85     buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
86     buttonsBox->addButton( apply, QDialogButtonBox::AcceptRole );
87     buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
88     buttonsBox->addButton( reset, QDialogButtonBox::ActionRole );
89
90     /* Layout  */
91     main_layout->addWidget( tree_panel, 0, 0, 3, 1 );
92     main_layout->addWidget( types, 3, 0, 2, 1 );
93     main_layout->addWidget( main_panel, 0, 1, 4, 1 );
94     main_layout->addWidget( buttonsBox, 4, 1, 1 ,2 );
95
96     main_layout->setColumnMinimumWidth( 0, 150 );
97     main_layout->setColumnStretch( 0, 1 );
98     main_layout->setColumnStretch( 1, 3 );
99
100     main_layout->setRowStretch( 2, 4);
101
102     setLayout( main_layout );
103
104     /* Margins */
105     tree_panel_l->setMargin( 1 );
106     main_panel_l->setMargin( 3 );
107
108     if( config_GetInt( p_intf, "qt-advanced-pref") == 1 )
109     {
110         setAll();
111     }
112     else
113     {
114         setSmall();
115     }
116
117     BUTTONACT( save, save() );
118     BUTTONACT( apply, apply() );
119     BUTTONACT( cancel, cancel() );
120     BUTTONACT( reset, reset() );
121     BUTTONACT( small, setSmall() );
122     BUTTONACT( all, setAll() );
123
124     for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
125 }
126
127 void PrefsDialog::setAll()
128 {
129     if( simple_tree )
130     {
131         tree_panel_l->removeWidget( simple_tree );
132         simple_tree->hide();
133     }
134
135     if( !advanced_tree )
136     {
137          advanced_tree = new PrefsTree( p_intf, tree_panel );
138          CONNECT( advanced_tree,
139                   currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *),
140                   this, changePanel( QTreeWidgetItem * ) );
141     }
142     tree_panel_l->addWidget( advanced_tree );
143     advanced_tree->show();
144
145     if( simple_panel )
146     {
147         main_panel_l->removeWidget( simple_panel );
148         simple_panel->hide();
149     }
150     if( !advanced_panel )
151          advanced_panel = new PrefsPanel( main_panel );
152     main_panel_l->addWidget( advanced_panel );
153     all->setChecked( true );
154     advanced_panel->show();
155 }
156
157 void PrefsDialog::setSmall()
158 {
159     if( advanced_tree )
160     {
161         tree_panel_l->removeWidget( advanced_tree );
162         advanced_tree->hide();
163     }
164     if( !simple_tree )
165     {
166          simple_tree = new SPrefsCatList( p_intf, tree_panel );
167          CONNECT( simple_tree,
168                   currentItemChanged( int ),
169                   this,  changeSimplePanel( int ) );
170     }
171     tree_panel_l->addWidget( simple_tree );
172     simple_tree->show();
173
174     if( advanced_panel )
175     {
176         main_panel_l->removeWidget( advanced_panel );
177         advanced_panel->hide();
178     }
179     if( !simple_panel )
180         simple_panel = new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
181     main_panel_l->addWidget( simple_panel );
182     small->setChecked( true );
183     simple_panel->show();
184 }
185
186 void PrefsDialog::changeSimplePanel( int number )
187 {
188     if( simple_panel )
189     {
190         main_panel_l->removeWidget( simple_panel );
191         simple_panel->hide();
192     }
193     simple_panel = simple_panels[number];
194     if( !simple_panel )
195     {
196         simple_panel = new SPrefsPanel( p_intf, main_panel, number );
197         simple_panels[number] = simple_panel;
198     }
199     main_panel_l->addWidget( simple_panel );
200     simple_panel->show();
201 }
202
203 void PrefsDialog::changePanel( QTreeWidgetItem *item )
204 {
205     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
206
207     if( advanced_panel )
208     {
209         main_panel_l->removeWidget( advanced_panel );
210         advanced_panel->hide();
211     }
212     if( !data->panel )
213         data->panel = new PrefsPanel( p_intf, main_panel , data );
214
215     advanced_panel = data->panel;
216     main_panel_l->addWidget( advanced_panel );
217     advanced_panel->show();
218 }
219
220 void PrefsDialog::showModulePrefs( char *psz_module )
221 {
222     setAll();
223     all->setChecked( true );
224     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
225          i_cat_index++ )
226     {
227         QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
228         PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
229                                                    value<PrefsItemData *>();
230         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
231                                   i_sc_index++ )
232         {
233             QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
234             PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
235                                                     value<PrefsItemData *>();
236             for( int i_module = 0; i_module < subcat_item->childCount();
237                                    i_module++ )
238             {
239                 QTreeWidgetItem *module_item = subcat_item->child( i_module );
240                 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
241                                                     value<PrefsItemData *>();
242                 if( !strcmp( mod_data->psz_name, psz_module ) ) {
243                     advanced_tree->setCurrentItem( module_item );
244                 }
245             }
246         }
247     }
248     show();
249 }
250
251 void PrefsDialog::save()
252 {
253     apply();
254     hide();
255 }
256
257 void PrefsDialog::apply()
258 {
259     if( small->isChecked() && simple_tree )
260     {
261         for( int i = 0 ; i< SPrefsMax; i++ )
262             if( simple_panels[i] ) simple_panels[i]->apply();
263     }
264     else if( all->isChecked() && advanced_tree )
265         advanced_tree->applyAll();
266     config_SaveConfigFile( p_intf, NULL );
267
268     /* Delete the other panel in order to force its reload after clicking 
269        on apply - UGLY but will work for now. */
270     if( simple_panel->isVisible() )
271         delete advanced_panel;
272     if( advanced_panel->isVisible() )
273         delete simple_panel;
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 }