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