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