]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/prefs_dialog.cpp
f0a663f2d3c67456089a96063fb61ceb65e1f72d
[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
40 PrefsDialog *PrefsDialog::instance = NULL;
41
42 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
43 {
44      QGridLayout *main_layout = new QGridLayout( this );
45      setWindowTitle( qtr( "Preferences" ) );
46      resize( 800, 650 );
47      setMaximumHeight( 650 );
48      setMaximumWidth( 800 );
49
50      tree_panel = new QWidget( 0 );
51      tree_panel_l = new QHBoxLayout;
52      tree_panel->setLayout( tree_panel_l );
53      main_panel = new QWidget( 0 );
54      main_panel_l = new QHBoxLayout;
55      main_panel->setLayout( main_panel_l );
56
57      // Choice for types
58      types = new QGroupBox( "Show settings" );
59      types->setAlignment( Qt::AlignHCenter );
60      QHBoxLayout *types_l = new QHBoxLayout(0);
61      types_l->setSpacing( 3 ); types_l->setMargin( 3 );
62      small = new QRadioButton( "Basic", types ); types_l->addWidget( small );
63      all = new QRadioButton( "All", types ); types_l->addWidget( all );
64      types->setLayout( types_l );
65      small->setChecked( true );
66
67      advanced_tree = NULL;
68      simple_tree = NULL;
69      simple_panel = NULL;
70      advanced_panel = NULL;
71
72      main_layout->addWidget( tree_panel, 0, 0, 3, 1 );
73      main_layout->addWidget( types, 3, 0, 1, 1 );
74
75      main_layout->addWidget( main_panel, 0, 1, 4, 1 );
76
77      main_layout->setColumnMinimumWidth( 0, 150 );
78      main_layout->setColumnStretch( 0, 1 );
79      main_layout->setColumnStretch( 1,3 );
80
81      main_layout->setRowStretch( 2, 4);
82
83      setSmall();
84
85      QPushButton *save, *cancel;
86      QHBoxLayout *buttonsLayout = QVLCFrame::doButtons( this, NULL,
87                                                         &save, _("Save"),
88                                                         &cancel, _("Cancel"),
89                                                         NULL, NULL );
90      main_layout->addLayout( buttonsLayout, 4, 0, 1 ,3 );
91      setLayout( main_layout );
92
93
94      BUTTONACT( save, save() );
95      BUTTONACT( cancel, cancel() );
96      BUTTONACT( small, setSmall() );
97      BUTTONACT( all, setAll() );
98
99      for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
100 }
101
102 void PrefsDialog::setAll()
103 {
104     if( simple_tree )
105     {
106         tree_panel_l->removeWidget( simple_tree );
107         simple_tree->hide();
108     }
109
110     if( !advanced_tree )
111     {
112          advanced_tree = new PrefsTree( p_intf, tree_panel );
113          CONNECT( advanced_tree,
114                   currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *),
115                   this, changePanel( QTreeWidgetItem * ) );
116     }
117     tree_panel_l->addWidget( advanced_tree );
118     advanced_tree->show();
119
120     if( simple_panel )
121     {
122         main_panel_l->removeWidget( simple_panel );
123         simple_panel->hide();
124     }
125     if( !advanced_panel )
126          advanced_panel = new PrefsPanel( main_panel );
127     main_panel_l->addWidget( advanced_panel );
128     advanced_panel->show();
129 }
130
131 void PrefsDialog::setSmall()
132 {
133     if( advanced_tree )
134     {
135         tree_panel_l->removeWidget( advanced_tree );
136         advanced_tree->hide();
137     }
138     if( !simple_tree )
139     {
140          simple_tree = new SPrefsCatList( p_intf, tree_panel );
141          CONNECT( simple_tree,
142                   currentItemChanged( QListWidgetItem *, QListWidgetItem *),
143                   this,  changeSimplePanel( QListWidgetItem * ) );
144     }
145     tree_panel_l->addWidget( simple_tree );
146     simple_tree->show();
147
148     if( advanced_panel )
149     {
150         main_panel_l->removeWidget( advanced_panel );
151         advanced_panel->hide();
152     }
153     if( !simple_panel )
154         simple_panel = new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
155     main_panel_l->addWidget( simple_panel );
156     simple_panel->show();
157 }
158
159 void PrefsDialog::changeSimplePanel( QListWidgetItem *item )
160 {
161     int number = item->data( Qt::UserRole ).toInt();
162     if( simple_panel )
163     {
164         main_panel_l->removeWidget( simple_panel );
165         simple_panel->hide();
166     }
167     simple_panel = simple_panels[number];
168     if( !simple_panel )
169     {
170         simple_panel = new SPrefsPanel( p_intf, main_panel, number );
171         simple_panels[number] = simple_panel;
172     }
173     main_panel_l->addWidget( simple_panel );
174     simple_panel->show();
175 //    panel_label->setText(qtr("Test")); //FIXME
176 }
177
178 void PrefsDialog::changePanel( QTreeWidgetItem *item )
179 {
180     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
181
182     if( advanced_panel )
183     {
184         main_panel_l->removeWidget( advanced_panel );
185         advanced_panel->hide();
186     }
187     if( !data->panel )
188         data->panel = new PrefsPanel( p_intf, main_panel , data );
189
190     advanced_panel = data->panel;
191     main_panel_l->addWidget( advanced_panel );
192     advanced_panel->show();
193 }
194
195 void PrefsDialog::showModulePrefs( char *psz_module )
196 {
197     setAll();
198     all->setChecked( true );
199     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
200          i_cat_index++ )
201     {
202         QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
203         PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
204                                                    value<PrefsItemData *>();
205         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
206                                   i_sc_index++ )
207         {
208             QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
209             PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
210                                                     value<PrefsItemData *>();
211             for( int i_module = 0; i_module < subcat_item->childCount();
212                                    i_module++ )
213             {
214                 QTreeWidgetItem *module_item = subcat_item->child( i_module );
215                 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
216                                                     value<PrefsItemData *>();
217                 if( !strcmp( mod_data->psz_name, psz_module ) ) {
218                     advanced_tree->setCurrentItem( module_item );
219                 }
220             }
221         }
222     }
223     show();
224 }
225
226 void PrefsDialog::save()
227 {
228     if( small->isChecked() && simple_tree )
229     {
230         for( int i = 0 ; i< SPrefsMax; i++ )
231             if( simple_panels[i] ) simple_panels[i]->apply();
232     }
233     else if( all->isChecked() && advanced_tree )
234         advanced_tree->applyAll();
235     config_SaveConfigFile( p_intf, NULL );
236     hide();
237 }
238
239 void PrefsDialog::cancel()
240 {
241     if( small->isChecked() && simple_tree )
242     {
243         for( int i = 0 ; i< SPrefsMax; i++ )
244             if( simple_panels[i] ) simple_panels[i]->clean();
245     }
246     else if( all->isChecked() && advanced_tree )
247     {
248         advanced_tree->cleanAll();
249         advanced_panel = NULL;
250     }
251     hide();
252 }