]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/prefs_dialog.cpp
qt4 - Subtitles Sprefs icon change and About dialog modification... But the file...
[vlc] / modules / gui / qt4 / dialogs / prefs_dialog.cpp
1 /*****************************************************************************
2  * prefs_dialog.cpp : Preferences
3  ****************************************************************************
4  * Copyright (C) 2006 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 #include "dialogs/prefs_dialog.hpp"
25 #include "dialogs_provider.hpp"
26 #include "util/qvlcframe.hpp"
27
28 #include "components/preferences.hpp"
29 #include "components/simple_preferences.hpp"
30 #include "qt4.hpp"
31
32 #include <QHBoxLayout>
33 #include <QGroupBox>
34 #include <QRadioButton>
35 #include <QVBoxLayout>
36 #include <QPushButton>
37 #include <QCheckBox>
38 #include <QScrollArea>
39 #include <QMessageBox>
40
41
42 PrefsDialog *PrefsDialog::instance = NULL;
43
44 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
45 {
46      QGridLayout *main_layout = new QGridLayout( this );
47      setWindowTitle( qtr( "Preferences" ) );
48      resize( 700, 650 );
49      setMaximumHeight( 650 );
50      setMaximumWidth( 700 );
51
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      advanced_tree = NULL;
70      simple_tree = NULL;
71      simple_panel = NULL;
72      advanced_panel = NULL;
73
74      main_layout->addWidget( tree_panel, 0, 0, 3, 1 );
75      main_layout->addWidget( types, 3, 0, 1, 1 );
76
77      main_layout->addWidget( main_panel, 0, 1, 4, 1 );
78
79      main_layout->setColumnMinimumWidth( 0, 150 );
80      main_layout->setColumnStretch( 0, 1 );
81      main_layout->setColumnStretch( 1,3 );
82
83      main_layout->setRowStretch( 2, 4);
84
85      setSmall();
86
87      QPushButton *save, *cancel, *reset;
88      QHBoxLayout *buttonsLayout = QVLCFrame::doButtons( this, NULL,
89                                       &save, _("Save"),
90                                       &cancel, _("Cancel"),
91                                       &reset, _( "Reset Preferences" ) );
92      main_layout->addLayout( buttonsLayout, 4, 0, 1 ,3 );
93      setLayout( main_layout );
94
95
96      BUTTONACT( save, save() );
97      BUTTONACT( cancel, cancel() );
98      BUTTONACT( reset, reset() );
99      BUTTONACT( small, setSmall() );
100      BUTTONACT( all, setAll() );
101
102      for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
103 }
104
105 void PrefsDialog::setAll()
106 {
107     if( simple_tree )
108     {
109         tree_panel_l->removeWidget( simple_tree );
110         simple_tree->hide();
111     }
112
113     if( !advanced_tree )
114     {
115          advanced_tree = new PrefsTree( p_intf, tree_panel );
116          CONNECT( advanced_tree,
117                   currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *),
118                   this, changePanel( QTreeWidgetItem * ) );
119     }
120     tree_panel_l->addWidget( advanced_tree );
121     advanced_tree->show();
122
123     if( simple_panel )
124     {
125         main_panel_l->removeWidget( simple_panel );
126         simple_panel->hide();
127     }
128     if( !advanced_panel )
129          advanced_panel = new PrefsPanel( main_panel );
130     main_panel_l->addWidget( advanced_panel );
131     advanced_panel->show();
132 }
133
134 void PrefsDialog::setSmall()
135 {
136     if( advanced_tree )
137     {
138         tree_panel_l->removeWidget( advanced_tree );
139         advanced_tree->hide();
140     }
141     if( !simple_tree )
142     {
143          simple_tree = new SPrefsCatList( p_intf, tree_panel );
144          CONNECT( simple_tree,
145                   currentItemChanged( QListWidgetItem *, QListWidgetItem *),
146                   this,  changeSimplePanel( QListWidgetItem * ) );
147     }
148     tree_panel_l->addWidget( simple_tree );
149     simple_tree->show();
150
151     if( advanced_panel )
152     {
153         main_panel_l->removeWidget( advanced_panel );
154         advanced_panel->hide();
155     }
156     if( !simple_panel )
157         simple_panel = new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
158     main_panel_l->addWidget( simple_panel );
159     simple_panel->show();
160 }
161
162 void PrefsDialog::changeSimplePanel( QListWidgetItem *item )
163 {
164     int number = item->data( Qt::UserRole ).toInt();
165     if( simple_panel )
166     {
167         main_panel_l->removeWidget( simple_panel );
168         simple_panel->hide();
169     }
170     simple_panel = simple_panels[number];
171     if( !simple_panel )
172     {
173         simple_panel = new SPrefsPanel( p_intf, main_panel, number );
174         simple_panels[number] = simple_panel;
175     }
176     main_panel_l->addWidget( simple_panel );
177     simple_panel->show();
178 //    panel_label->setText(qtr("Test")); //FIXME
179 }
180
181 void PrefsDialog::changePanel( QTreeWidgetItem *item )
182 {
183     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
184
185     if( advanced_panel )
186     {
187         main_panel_l->removeWidget( advanced_panel );
188         advanced_panel->hide();
189     }
190     if( !data->panel )
191         data->panel = new PrefsPanel( p_intf, main_panel , data );
192
193     advanced_panel = data->panel;
194     main_panel_l->addWidget( advanced_panel );
195     advanced_panel->show();
196 }
197
198 void PrefsDialog::showModulePrefs( char *psz_module )
199 {
200     setAll();
201     all->setChecked( true );
202     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
203          i_cat_index++ )
204     {
205         QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
206         PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
207                                                    value<PrefsItemData *>();
208         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
209                                   i_sc_index++ )
210         {
211             QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
212             PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
213                                                     value<PrefsItemData *>();
214             for( int i_module = 0; i_module < subcat_item->childCount();
215                                    i_module++ )
216             {
217                 QTreeWidgetItem *module_item = subcat_item->child( i_module );
218                 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
219                                                     value<PrefsItemData *>();
220                 if( !strcmp( mod_data->psz_name, psz_module ) ) {
221                     advanced_tree->setCurrentItem( module_item );
222                 }
223             }
224         }
225     }
226     show();
227 }
228
229 void PrefsDialog::save()
230 {
231     if( small->isChecked() && simple_tree )
232     {
233         for( int i = 0 ; i< SPrefsMax; i++ )
234             if( simple_panels[i] ) simple_panels[i]->apply();
235     }
236     else if( all->isChecked() && advanced_tree )
237         advanced_tree->applyAll();
238     config_SaveConfigFile( p_intf, NULL );
239     hide();
240 }
241
242 void PrefsDialog::cancel()
243 {
244     if( small->isChecked() && simple_tree )
245     {
246         for( int i = 0 ; i< SPrefsMax; i++ )
247             if( simple_panels[i] ) simple_panels[i]->clean();
248     }
249     else if( all->isChecked() && advanced_tree )
250     {
251         advanced_tree->cleanAll();
252         advanced_panel = NULL;
253     }
254     hide();
255 }
256
257 void PrefsDialog::reset()
258 {
259     int ret = QMessageBox::question(this, qtr("Reset Preferences"),
260                  qtr("This will reset your VLC media player preferences.\n"
261                          "Are you sure you want to continue?"),
262                   QMessageBox::Ok | QMessageBox::Cancel,
263                                                          QMessageBox::Ok);
264     if ( ret == QMessageBox::Ok )
265     {
266         config_ResetAll( p_intf );
267         // TODO reset changes ?
268          config_SaveConfigFile( p_intf, NULL );
269     }
270 }