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