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