Qt4 - Update Preferences buttons to please any OS/DE.
[vlc.git] / 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( 700, 650 );
50      setMaximumHeight( 650 );
51      setMaximumWidth( 700 );
52
53      tree_panel = new QWidget( 0 );
54      tree_panel_l = new QHBoxLayout;
55      tree_panel->setLayout( tree_panel_l );
56      main_panel = new QWidget( 0 );
57      main_panel_l = new QHBoxLayout;
58      main_panel->setLayout( main_panel_l );
59
60      // Choice for types
61      types = new QGroupBox( "Show settings" );
62      types->setAlignment( Qt::AlignHCenter );
63      QHBoxLayout *types_l = new QHBoxLayout(0);
64      types_l->setSpacing( 3 ); types_l->setMargin( 3 );
65      small = new QRadioButton( "Basic", types ); types_l->addWidget( small );
66      all = new QRadioButton( "All", types ); types_l->addWidget( all );
67      types->setLayout( types_l );
68      small->setChecked( true );
69
70      advanced_tree = NULL;
71      simple_tree = NULL;
72      simple_panel = NULL;
73      advanced_panel = NULL;
74
75      main_layout->addWidget( tree_panel, 0, 0, 3, 1 );
76      main_layout->addWidget( types, 3, 0, 1, 1 );
77
78      main_layout->addWidget( main_panel, 0, 1, 4, 1 );
79
80      main_layout->setColumnMinimumWidth( 0, 150 );
81      main_layout->setColumnStretch( 0, 1 );
82      main_layout->setColumnStretch( 1,3 );
83
84      main_layout->setRowStretch( 2, 4);
85
86      setSmall();
87
88      QDialogButtonBox *buttonsBox = new QDialogButtonBox();
89      QPushButton *save = new QPushButton( qtr( "&Save" ) );
90      QPushButton *cancel = new QPushButton( qtr( "&Cancel" ) );
91      QPushButton *reset = new QPushButton( qtr( "&Reset Preferences" ) );
92
93      buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
94      buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
95      buttonsBox->addButton( reset, QDialogButtonBox::ActionRole );
96
97      main_layout->addWidget( buttonsBox, 4, 0, 1 ,3 );
98      setLayout( main_layout );
99
100      BUTTONACT( save, save() );
101      BUTTONACT( cancel, cancel() );
102      BUTTONACT( reset, reset() );
103      BUTTONACT( small, setSmall() );
104      BUTTONACT( all, setAll() );
105
106      for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
107 }
108
109 void PrefsDialog::setAll()
110 {
111     if( simple_tree )
112     {
113         tree_panel_l->removeWidget( simple_tree );
114         simple_tree->hide();
115     }
116
117     if( !advanced_tree )
118     {
119          advanced_tree = new PrefsTree( p_intf, tree_panel );
120          CONNECT( advanced_tree,
121                   currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *),
122                   this, changePanel( QTreeWidgetItem * ) );
123     }
124     tree_panel_l->addWidget( advanced_tree );
125     advanced_tree->show();
126
127     if( simple_panel )
128     {
129         main_panel_l->removeWidget( simple_panel );
130         simple_panel->hide();
131     }
132     if( !advanced_panel )
133          advanced_panel = new PrefsPanel( main_panel );
134     main_panel_l->addWidget( advanced_panel );
135     advanced_panel->show();
136 }
137
138 void PrefsDialog::setSmall()
139 {
140     if( advanced_tree )
141     {
142         tree_panel_l->removeWidget( advanced_tree );
143         advanced_tree->hide();
144     }
145     if( !simple_tree )
146     {
147          simple_tree = new SPrefsCatList( p_intf, tree_panel );
148          CONNECT( simple_tree,
149                   currentItemChanged( int ),
150                   this,  changeSimplePanel( int ) );
151     }
152     tree_panel_l->addWidget( simple_tree );
153     simple_tree->show();
154
155     if( advanced_panel )
156     {
157         main_panel_l->removeWidget( advanced_panel );
158         advanced_panel->hide();
159     }
160     if( !simple_panel )
161         simple_panel = new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
162     main_panel_l->addWidget( simple_panel );
163     simple_panel->show();
164 }
165
166 void PrefsDialog::changeSimplePanel( int number )
167 {
168     if( simple_panel )
169     {
170         main_panel_l->removeWidget( simple_panel );
171         simple_panel->hide();
172     }
173     simple_panel = simple_panels[number];
174     if( !simple_panel )
175     {
176         simple_panel = new SPrefsPanel( p_intf, main_panel, number );
177         simple_panels[number] = simple_panel;
178     }
179     main_panel_l->addWidget( simple_panel );
180     simple_panel->show();
181 }
182
183 void PrefsDialog::changePanel( QTreeWidgetItem *item )
184 {
185     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
186
187     if( advanced_panel )
188     {
189         main_panel_l->removeWidget( advanced_panel );
190         advanced_panel->hide();
191     }
192     if( !data->panel )
193         data->panel = new PrefsPanel( p_intf, main_panel , data );
194
195     advanced_panel = data->panel;
196     main_panel_l->addWidget( advanced_panel );
197     advanced_panel->show();
198 }
199
200 void PrefsDialog::showModulePrefs( char *psz_module )
201 {
202     setAll();
203     all->setChecked( true );
204     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
205          i_cat_index++ )
206     {
207         QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
208         PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
209                                                    value<PrefsItemData *>();
210         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
211                                   i_sc_index++ )
212         {
213             QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
214             PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
215                                                     value<PrefsItemData *>();
216             for( int i_module = 0; i_module < subcat_item->childCount();
217                                    i_module++ )
218             {
219                 QTreeWidgetItem *module_item = subcat_item->child( i_module );
220                 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
221                                                     value<PrefsItemData *>();
222                 if( !strcmp( mod_data->psz_name, psz_module ) ) {
223                     advanced_tree->setCurrentItem( module_item );
224                 }
225             }
226         }
227     }
228     show();
229 }
230
231 void PrefsDialog::save()
232 {
233     if( small->isChecked() && simple_tree )
234     {
235         for( int i = 0 ; i< SPrefsMax; i++ )
236             if( simple_panels[i] ) simple_panels[i]->apply();
237     }
238     else if( all->isChecked() && advanced_tree )
239         advanced_tree->applyAll();
240     config_SaveConfigFile( p_intf, NULL );
241     hide();
242 }
243
244 void PrefsDialog::cancel()
245 {
246     if( small->isChecked() && simple_tree )
247     {
248         for( int i = 0 ; i< SPrefsMax; i++ )
249             if( simple_panels[i] ) simple_panels[i]->clean();
250     }
251     else if( all->isChecked() && advanced_tree )
252     {
253         advanced_tree->cleanAll();
254         advanced_panel = NULL;
255     }
256     hide();
257 }
258
259 void PrefsDialog::reset()
260 {
261     int ret = QMessageBox::question(this, qtr("Reset Preferences"),
262                  qtr("This will reset your VLC media player preferences.\n"
263                          "Are you sure you want to continue?"),
264                   QMessageBox::Ok | QMessageBox::Cancel,
265                                                          QMessageBox::Ok);
266     if ( ret == QMessageBox::Ok )
267     {
268         config_ResetAll( p_intf );
269         // TODO reset changes ?
270         config_SaveConfigFile( p_intf, NULL );
271     }
272 }