054e107ee08969fe9de9af4ca87c6139e0b49bfe
[vlc.git] / 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  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/
22
23 #include "dialogs/prefs_dialog.hpp"
24 #include "dialogs_provider.hpp"
25 #include "util/qvlcframe.hpp"
26
27 #include "components/preferences.hpp"
28 #include "components/simple_preferences.hpp"
29 #include "qt4.hpp"
30
31 #include <QHBoxLayout>
32 #include <QGroupBox>
33 #include <QRadioButton>
34 #include <QVBoxLayout>
35 #include <QPushButton>
36 #include <QCheckBox>
37 #include <QScrollArea>
38 #include <QLabel>
39 PrefsDialog *PrefsDialog::instance = NULL;
40
41 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
42 {
43      QGridLayout *main_layout = new QGridLayout(this);
44      setWindowTitle( qtr( "Preferences" ) );
45      resize( 800, 450 );
46      setMaximumHeight (450);
47
48      tree_panel = new QWidget(0);
49      tree_panel_l = new QHBoxLayout;
50      tree_panel->setLayout( tree_panel_l );
51      main_panel = new QWidget(0);
52      main_panel_l = new QHBoxLayout;
53      main_panel->setLayout( main_panel_l );
54
55
56      // Choice for types
57      types = new QGroupBox( "Show settings" );
58      QHBoxLayout *types_l = new QHBoxLayout(0);
59      types_l->setSpacing( 3 ); types_l->setMargin( 3 );
60      small = new QRadioButton( "Basic", types ); types_l->addWidget( small );
61      all = new QRadioButton( "All", types ); types_l->addWidget( all );
62      types->setLayout(types_l);
63      small->setChecked( true );
64
65      // Title Label
66      QLabel *panel_label = new QLabel;
67      QFont labelFont = QApplication::font( static_cast<QWidget*>(0) );
68      labelFont.setPointSize( labelFont.pointSize() + 4 ); 
69      labelFont.setBold( true );
70      panel_label->setFont( labelFont );
71
72      // Title <hr>
73      QFrame *title_line = new QFrame;
74      title_line->setFrameShape(QFrame::HLine);
75      title_line->setFrameShadow(QFrame::Sunken);
76
77      QScrollArea *scrollArea = new QScrollArea;
78
79      advanced_tree = NULL;
80      simple_tree = NULL;
81      simple_panel = NULL;
82      advanced_panel = NULL;
83
84      main_layout->addWidget( tree_panel, 0, 0, 3, 1 );
85      main_layout->addWidget( types, 3, 0, 1, 1 );
86
87      main_layout->addWidget( panel_label, 0, 1, 1, 1 );
88      main_layout->addWidget( title_line, 1, 1, 1, 1 );
89      main_layout->addWidget( main_panel, 2, 1, 2, 1 );
90
91      main_layout->setColumnMinimumWidth( 0, 200 );
92      main_layout->setColumnStretch( 0, 1 );
93      main_layout->setColumnStretch( 1,3 );
94
95      main_layout->setRowStretch( 2, 4);
96
97      setSmall();
98
99      QPushButton *save, *cancel;
100      QHBoxLayout *buttonsLayout = QVLCFrame::doButtons( this, NULL,
101                                                         &save, _("Save"),
102                                                         &cancel, _("Cancel"),
103                                                         NULL, NULL );
104      main_layout->addLayout( buttonsLayout, 4, 0, 1 ,3 );
105      setLayout( main_layout );
106
107
108      BUTTONACT( save, save() );
109      BUTTONACT( cancel, cancel() );
110      BUTTONACT( small, setSmall() );
111      BUTTONACT( all, setAll() );
112
113      for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
114 }
115
116 void PrefsDialog::setAll()
117 {
118     if( simple_tree )
119     {
120         tree_panel_l->removeWidget( simple_tree );
121         simple_tree->hide();
122     }
123
124     if( !advanced_tree )
125     {
126          advanced_tree = new PrefsTree( p_intf, tree_panel );
127          CONNECT( advanced_tree,
128                   currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *),
129                   this, changePanel( QTreeWidgetItem * ) );
130     }
131     tree_panel_l->addWidget( advanced_tree );
132     advanced_tree->show();
133
134     if( simple_panel )
135     {
136         main_panel_l->removeWidget( simple_panel );
137         simple_panel->hide();
138     }
139     if( !advanced_panel )
140          advanced_panel = new PrefsPanel( main_panel );
141     main_panel_l->addWidget( advanced_panel );
142     advanced_panel->show();
143 }
144
145 void PrefsDialog::setSmall()
146 {
147     if( advanced_tree )
148     {
149         tree_panel_l->removeWidget( advanced_tree );
150         advanced_tree->hide();
151     }
152     if( !simple_tree )
153     {
154          simple_tree = new SPrefsCatList( p_intf, tree_panel );
155          CONNECT( simple_tree,
156                   currentItemChanged( QListWidgetItem *, QListWidgetItem *),
157                   this,  changeSimplePanel( QListWidgetItem * ) );
158     }
159     tree_panel_l->addWidget( simple_tree );
160     simple_tree->show();
161
162     if( advanced_panel )
163     {
164         main_panel_l->removeWidget( advanced_panel );
165         advanced_panel->hide();
166     }
167     if( !simple_panel )
168         simple_panel = new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
169     main_panel_l->addWidget( simple_panel );
170     simple_panel->show();
171 }
172
173 void PrefsDialog::changeSimplePanel( QListWidgetItem *item )
174 {
175     int number = item->data( Qt::UserRole ).toInt();
176     if( simple_panel )
177     {
178         main_panel_l->removeWidget( simple_panel );
179         simple_panel->hide();
180     }
181     simple_panel = simple_panels[number];
182     if( !simple_panel )
183     {
184         simple_panel = new SPrefsPanel( p_intf, main_panel, number );
185         simple_panels[number] = simple_panel;
186     }
187     main_panel_l->addWidget( simple_panel );
188     simple_panel->show();
189 }
190
191 void PrefsDialog::changePanel( QTreeWidgetItem *item )
192 {
193     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
194
195     if( advanced_panel )
196     {
197         main_panel_l->removeWidget( advanced_panel );
198         advanced_panel->hide();
199     }
200     if( !data->panel )
201         data->panel = new PrefsPanel( p_intf, main_panel , data );
202
203     advanced_panel = data->panel;
204     main_panel_l->addWidget( advanced_panel );
205     advanced_panel->show();
206 }
207
208 void PrefsDialog::showModulePrefs( char *psz_module )
209 {
210     setAll();
211     all->setChecked( true );
212     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
213          i_cat_index++ )
214     {
215         QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
216         PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
217                                                    value<PrefsItemData *>();
218         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
219                                   i_sc_index++ )
220         {
221             QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
222             PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
223                                                     value<PrefsItemData *>();
224             for( int i_module = 0; i_module < subcat_item->childCount();
225                                    i_module++ )
226             {
227                 QTreeWidgetItem *module_item = subcat_item->child( i_module );
228                 PrefsItemData *mod_data = module_item->data(0, Qt::UserRole).
229                                                     value<PrefsItemData *>();
230                 if( !strcmp( mod_data->psz_name, psz_module ) ) {
231                     advanced_tree->setCurrentItem( module_item );
232                 }
233             }
234         }
235     }
236     show();
237 }
238
239 void PrefsDialog::save()
240 {
241     if( small->isChecked() && simple_tree )
242     {
243         for( int i = 0 ; i< SPrefsMax; i++ )
244             if( simple_panels[i] ) simple_panels[i]->apply();
245     }
246     else if( all->isChecked() && advanced_tree )
247         advanced_tree->applyAll();
248     config_SaveConfigFile( p_intf, NULL );
249     hide();
250 }
251
252 void PrefsDialog::cancel()
253 {
254     if( small->isChecked() && simple_tree )
255     {
256         for( int i = 0 ; i< SPrefsMax; i++ )
257             if( simple_panels[i] ) simple_panels[i]->clean();
258     }
259     else if( all->isChecked() && advanced_tree )
260     {
261         advanced_tree->cleanAll();
262         advanced_panel = NULL;
263     }
264     hide();
265 }