]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/preferences.cpp
Qt: remove Preferences singleton. Simplify and Close #2552 and #2551
[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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "dialogs/preferences.hpp"
30 #include "util/qvlcframe.hpp"
31
32 #include "components/complete_preferences.hpp"
33 #include "components/simple_preferences.hpp"
34
35 #include <QHBoxLayout>
36 #include <QGroupBox>
37 #include <QRadioButton>
38 #include <QPushButton>
39 #include <QMessageBox>
40 #include <QDialogButtonBox>
41
42 PrefsDialog::PrefsDialog( QWidget *parent, intf_thread_t *_p_intf )
43             : QVLCDialog( parent, _p_intf )
44 {
45     QGridLayout *main_layout = new QGridLayout( this );
46     setWindowTitle( qtr( "Preferences" ) );
47
48     /* Whether we want it or not, we need to destroy on close to get
49        consistency when reset */
50     setAttribute( Qt::WA_DeleteOnClose );
51
52     /* Create Panels */
53     tree_panel = new QWidget;
54     tree_panel_l = new QHBoxLayout;
55     tree_panel->setLayout( tree_panel_l );
56     main_panel = new QWidget;
57     main_panel_l = new QHBoxLayout;
58     main_panel->setLayout( main_panel_l );
59
60     /* Choice for types */
61     types = new QGroupBox( qtr("Show settings") );
62     types->setAlignment( Qt::AlignHCenter );
63     QHBoxLayout *types_l = new QHBoxLayout;
64     types_l->setSpacing( 3 ); types_l->setMargin( 3 );
65     small = new QRadioButton( qtr( "Simple" ), types );
66     small->setToolTip( qtr( "Switch to simple preferences view" ) );
67     types_l->addWidget( small );
68     all = new QRadioButton( qtr("All"), types ); types_l->addWidget( all );
69     all->setToolTip( qtr( "Switch to full preferences view" ) );
70     types->setLayout( types_l );
71     small->setChecked( true );
72
73     /* Tree and panel initialisations */
74     advanced_tree = NULL;
75     simple_tree = NULL;
76     current_simple_panel  = NULL;
77     advanced_panel = NULL;
78
79     /* Buttons */
80     QDialogButtonBox *buttonsBox = new QDialogButtonBox();
81     QPushButton *save = new QPushButton( qtr( "&Save" ) );
82     save->setToolTip( qtr( "Save and close the dialog" ) );
83     QPushButton *cancel = new QPushButton( qtr( "&Cancel" ) );
84     QPushButton *reset = new QPushButton( qtr( "&Reset Preferences" ) );
85
86     buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
87     buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
88     buttonsBox->addButton( reset, QDialogButtonBox::ResetRole );
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, 2 );
94     main_layout->addWidget( buttonsBox, 4, 2, 1 ,1 );
95
96     main_layout->setColumnMinimumWidth( 0, 150 );
97     main_layout->setColumnMinimumWidth( 1, 10 );
98     main_layout->setColumnStretch( 0, 1 );
99     main_layout->setColumnStretch( 1, 0 );
100     main_layout->setColumnStretch( 2, 10 );
101
102     main_layout->setRowStretch( 2, 4 );
103
104     main_layout->setMargin( 9 );
105     setLayout( main_layout );
106
107     /* Margins */
108     tree_panel_l->setMargin( 1 );
109     main_panel_l->setLayoutMargins( 6, 0, 0, 3, 3 );
110
111     b_small = (p_intf->p_sys->i_screenHeight < 750);
112     if( b_small ) msg_Dbg( p_intf, "Small");
113     setMaximumHeight( p_intf->p_sys->i_screenHeight );
114     for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
115
116     if( config_GetInt( p_intf, "qt-advanced-pref" ) || config_GetInt( p_intf, "advanced" ) )
117         setAdvanced();
118     else
119         setSmall();
120
121     BUTTONACT( save, save() );
122     BUTTONACT( cancel, cancel() );
123     BUTTONACT( reset, reset() );
124
125     BUTTONACT( small, setSmall() );
126     BUTTONACT( all, setAdvanced() );
127
128     resize( 780, sizeHint().height() );
129 }
130
131 void PrefsDialog::setAdvanced()
132 {
133     /* We already have a simple TREE, and we just want to hide it */
134     if( simple_tree )
135         if( simple_tree->isVisible() ) simple_tree->hide();
136
137     /* If don't have already and advanced TREE, then create it */
138     if( !advanced_tree )
139     {
140         /* Creation */
141          advanced_tree = new PrefsTree( p_intf, tree_panel );
142         /* and connections */
143          CONNECT( advanced_tree,
144                   currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem * ),
145                   this, changeAdvPanel( QTreeWidgetItem * ) );
146         tree_panel_l->addWidget( advanced_tree );
147     }
148
149     /* Show it */
150     advanced_tree->show();
151
152     /* Remove the simple current panel from the main panels*/
153     if( current_simple_panel )
154         if( current_simple_panel->isVisible() ) current_simple_panel->hide();
155
156     /* If no advanced Panel exist, create one, attach it and show it*/
157     if( !advanced_panel )
158     {
159         advanced_panel = new AdvPrefsPanel( main_panel );
160         main_panel_l->addWidget( advanced_panel );
161     }
162     advanced_panel->show();
163
164     /* Select the first Item of the preferences. Maybe you want to select a specified
165        category... */
166     advanced_tree->setCurrentIndex(
167             advanced_tree->model()->index( 0, 0, QModelIndex() ) );
168
169     all->setChecked( true );
170 }
171
172 void PrefsDialog::setSmall()
173 {
174     /* If an advanced TREE exists, remove and hide it */
175     if( advanced_tree )
176         if( advanced_tree->isVisible() ) advanced_tree->hide();
177
178     /* If no simple_tree, create one, connect it */
179     if( !simple_tree )
180     {
181          simple_tree = new SPrefsCatList( p_intf, tree_panel, b_small );
182          CONNECT( simple_tree,
183                   currentItemChanged( int ),
184                   this,  changeSimplePanel( int ) );
185         tree_panel_l->addWidget( simple_tree );
186     }
187
188     /*show it */
189     simple_tree->show();
190
191     /* If an Advanced PANEL exists, remove it */
192     if( advanced_panel )
193         if( advanced_panel->isVisible() ) advanced_panel->hide();
194
195     if( !current_simple_panel )
196     {
197         current_simple_panel =
198             new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat, b_small );
199         simple_panels[SPrefsDefaultCat] =  current_simple_panel;
200         main_panel_l->addWidget( current_simple_panel );
201     }
202
203     current_simple_panel->show();
204     small->setChecked( true );
205 }
206
207 /* Switching from on simple panel to another */
208 void PrefsDialog::changeSimplePanel( int number )
209 {
210     if( current_simple_panel )
211         if( current_simple_panel->isVisible() ) current_simple_panel->hide();
212
213     current_simple_panel = simple_panels[number];
214     if( !current_simple_panel )
215     {
216         current_simple_panel  = new SPrefsPanel( p_intf, main_panel, number, b_small );
217         simple_panels[number] = current_simple_panel;
218         main_panel_l->addWidget( current_simple_panel );
219     }
220
221     current_simple_panel->show();
222 }
223
224 /* Changing from one Advanced Panel to another */
225 void PrefsDialog::changeAdvPanel( QTreeWidgetItem *item )
226 {
227     if( item == NULL ) return;
228     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
229
230     if( advanced_panel )
231         if( advanced_panel->isVisible() ) advanced_panel->hide();
232
233     if( !data->panel )
234     {
235         data->panel = new AdvPrefsPanel( p_intf, main_panel , data );
236         main_panel_l->addWidget( data->panel );
237     }
238
239     advanced_panel = data->panel;
240     advanced_panel->show();
241 }
242
243 #if 0
244 /*Called from extended settings, is not used anymore, but could be useful one day*/
245 void PrefsDialog::showModulePrefs( char *psz_module )
246 {
247     setAdvanced();
248     all->setChecked( true );
249     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
250          i_cat_index++ )
251     {
252         QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
253         PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
254                                                    value<PrefsItemData *>();
255         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
256                                   i_sc_index++ )
257         {
258             QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
259             PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
260                                                     value<PrefsItemData *>();
261             for( int i_module = 0; i_module < subcat_item->childCount();
262                                    i_module++ )
263             {
264                 QTreeWidgetItem *module_item = subcat_item->child( i_module );
265                 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
266                                                     value<PrefsItemData *>();
267                 if( !strcmp( mod_data->psz_name, psz_module ) ) {
268                     advanced_tree->setCurrentItem( module_item );
269                 }
270             }
271         }
272     }
273     show();
274 }
275 #endif
276
277 /* Actual apply and save for the preferences */
278 void PrefsDialog::save()
279 {
280     if( small->isChecked() && simple_tree->isVisible() )
281     {
282         msg_Dbg( p_intf, "Saving the simple preferences" );
283         for( int i = 0 ; i< SPrefsMax; i++ ){
284             if( simple_panels[i] )simple_panels[i]->apply();
285         }
286     }
287     else if( all->isChecked() && advanced_tree->isVisible() )
288     {
289         msg_Dbg( p_intf, "Saving the advanced preferences" );
290         advanced_tree->applyAll();
291     }
292
293     /* Save to file */
294     config_SaveConfigFile( p_intf, NULL );
295     accept();
296 }
297
298 /* Clean the preferences, dunno if it does something really */
299 void PrefsDialog::cancel()
300 {
301     reject();
302 }
303
304 /* Reset all the preferences, when you click the button */
305 void PrefsDialog::reset()
306 {
307     int ret = QMessageBox::question(
308                  this,
309                  qtr( "Reset Preferences" ),
310                  qtr( "Are you sure you want to reset your VLC media player preferences?" ),
311                  QMessageBox::Ok | QMessageBox::Cancel,
312                  QMessageBox::Ok);
313
314     if( ret == QMessageBox::Ok )
315     {
316         config_ResetAll( p_intf );
317         config_SaveConfigFile( p_intf, NULL );
318         getSettings()->clear();
319
320         accept();
321     }
322 }