1 /*****************************************************************************
2  * preferences.cpp : Preferences
3  *****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id: 110c60d1b6832d97aa7ed4e1f73e4fc9af4023d3 $
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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28 
29 #include "dialogs/preferences.hpp"
30 #include "util/qvlcframe.hpp"
31 #include "dialogs/errors.hpp"
32 
33 #include "components/complete_preferences.hpp"
34 #include "components/simple_preferences.hpp"
35 #include "util/searchlineedit.hpp"
36 #include "util/qvlcframe.hpp"
37 #include "main_interface.hpp"
38 
39 #include <QHBoxLayout>
40 #include <QGroupBox>
41 #include <QRadioButton>
42 #include <QPushButton>
43 #include <QMessageBox>
44 #include <QDialogButtonBox>
45 #include <QStackedWidget>
46 #include <QSplitter>
47 #include <QShortcut>
48 #include <QScrollArea>
49 
50 #include <vlc_modules.h>
51 
PrefsDialog(QWidget * parent,intf_thread_t * _p_intf)52 PrefsDialog::PrefsDialog( QWidget *parent, intf_thread_t *_p_intf )
53             : QVLCDialog( parent, _p_intf )
54 {
55     QGridLayout *main_layout = new QGridLayout( this );
56     setWindowTitle( qtr( "Preferences" ) );
57     setWindowRole( "vlc-preferences" );
58     setWindowModality( Qt::WindowModal );
59 
60     /* Whether we want it or not, we need to destroy on close to get
61        consistency when reset */
62     setAttribute( Qt::WA_DeleteOnClose );
63 
64     /* Create Panels */
65     simple_tree_panel = new QWidget;
66     simple_tree_panel->setLayout( new QVBoxLayout );
67 
68     advanced_tree_panel = new QWidget;
69     advanced_tree_panel->setLayout( new QVBoxLayout );
70 
71     /* Choice for types */
72     types = new QGroupBox( qtr("Show settings") );
73     types->setAlignment( Qt::AlignHCenter );
74     QHBoxLayout *types_l = new QHBoxLayout;
75     types_l->setSpacing( 3 ); types_l->setMargin( 3 );
76     simple = new QRadioButton( qtr( "Simple" ), types );
77     simple->setToolTip( qtr( "Switch to simple preferences view" ) );
78     types_l->addWidget( simple );
79     all = new QRadioButton( qtr("All"), types ); types_l->addWidget( all );
80     all->setToolTip( qtr( "Switch to full preferences view" ) );
81     types->setLayout( types_l );
82     simple->setChecked( true );
83 
84     /* Tree and panel initialisations */
85     advanced_tree = NULL;
86     p_list = NULL;
87     tree_filter = NULL;
88     current_filter = NULL;
89     simple_tree = NULL;
90     simple_panels_stack = new QStackedWidget;
91     advanced_panels_stack = new QStackedWidget;
92 
93     /* Buttons */
94     QDialogButtonBox *buttonsBox = new QDialogButtonBox();
95     QPushButton *save = new QPushButton( qtr( "&Save" ) );
96     save->setToolTip( qtr( "Save and close the dialog" ) );
97     QPushButton *cancel = new QPushButton( qtr( "&Cancel" ) );
98     QPushButton *reset = new QPushButton( qtr( "&Reset Preferences" ) );
99 
100     buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
101     buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
102     buttonsBox->addButton( reset, QDialogButtonBox::ResetRole );
103 
104     simple_split_widget = new QWidget();
105     simple_split_widget->setLayout( new QVBoxLayout );
106 
107     advanced_split_widget = new QSplitter();
108 
109     stack = new QStackedWidget();
110     stack->insertWidget( SIMPLE, simple_split_widget );
111     stack->insertWidget( ADVANCED, advanced_split_widget );
112 
113     simple_split_widget->layout()->addWidget( simple_tree_panel );
114     simple_split_widget->layout()->addWidget( simple_panels_stack );
115     simple_split_widget->layout()->setMargin( 0 );
116 
117     advanced_split_widget->addWidget( advanced_tree_panel );
118     advanced_split_widget->addWidget( advanced_panels_stack );
119 
120     /* Layout  */
121     main_layout->addWidget( stack, 0, 0, 3, 3 );
122     main_layout->addWidget( types, 3, 0, 2, 1 );
123     main_layout->addWidget( buttonsBox, 4, 2, 1 ,1 );
124     main_layout->setRowStretch( 2, 4 );
125     main_layout->setMargin( 9 );
126     setLayout( main_layout );
127 
128     /* Margins */
129     simple_tree_panel->layout()->setMargin( 1 );
130     simple_panels_stack->layout()->setContentsMargins( 6, 0, 0, 3 );
131 
132     for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
133 
134     if( var_InheritBool( p_intf, "qt-advanced-pref" )
135      || var_InheritBool( p_intf, "advanced" ) )
136         setAdvanced();
137     else
138         setSimple();
139 
140     BUTTONACT( save, save() );
141     BUTTONACT( cancel, cancel() );
142     BUTTONACT( reset, reset() );
143 
144     BUTTONACT( simple, setSimple() );
145     BUTTONACT( all, setAdvanced() );
146 
147     QVLCTools::restoreWidgetPosition( p_intf, "Preferences", this, QSize( 800 , 700 ) );
148 }
149 
~PrefsDialog()150 PrefsDialog::~PrefsDialog()
151 {
152     module_list_free( p_list );
153 }
154 
setAdvanced()155 void PrefsDialog::setAdvanced()
156 {
157     if ( !tree_filter )
158     {
159         tree_filter = new SearchLineEdit( simple_tree_panel );
160         tree_filter->setMinimumHeight( 26 );
161 
162         CONNECT( tree_filter, textChanged( const QString &  ),
163                 this, advancedTreeFilterChanged( const QString & ) );
164 
165         advanced_tree_panel->layout()->addWidget( tree_filter );
166 
167         current_filter = new QCheckBox( qtr("Only show current") );
168         current_filter->setToolTip(
169                     qtr("Only show modules related to current playback") );
170         CONNECT( current_filter, stateChanged(int),
171                  this, onlyLoadedToggled() );
172         advanced_tree_panel->layout()->addWidget( current_filter );
173 
174         QShortcut *search = new QShortcut( QKeySequence( QKeySequence::Find ), tree_filter );
175         CONNECT( search, activated(), tree_filter, setFocus() );
176     }
177 
178     /* If don't have already and advanced TREE, then create it */
179     if( !advanced_tree )
180     {
181         /* Creation */
182         p_list = module_list_get( &count );
183         advanced_tree = new PrefsTree( p_intf, simple_tree_panel, p_list, count );
184         /* and connections */
185         CONNECT( advanced_tree,
186                  currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem * ),
187                  this, changeAdvPanel( QTreeWidgetItem * ) );
188         advanced_tree_panel->layout()->addWidget( advanced_tree );
189         advanced_tree_panel->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
190     }
191 
192     /* If no advanced Panel exist, create one, attach it and show it*/
193     if( advanced_panels_stack->count() < 1 )
194     {
195         AdvPrefsPanel *insert = new AdvPrefsPanel( advanced_panels_stack );
196         advanced_panels_stack->insertWidget( 0, insert );
197     }
198 
199     /* Select the first Item of the preferences. Maybe you want to select a specified
200        category... */
201     advanced_tree->setCurrentIndex(
202             advanced_tree->model()->index( 0, 0, QModelIndex() ) );
203 
204     all->setChecked( true );
205     stack->setCurrentIndex( ADVANCED );
206     setWindowTitle( qtr( "Advanced Preferences" ) );
207 }
208 
setSimple()209 void PrefsDialog::setSimple()
210 {
211     /* If no simple_tree, create one, connect it */
212     if( !simple_tree )
213     {
214          simple_tree = new SPrefsCatList( p_intf, simple_tree_panel );
215          CONNECT( simple_tree,
216                   currentItemChanged( int ),
217                   this,  changeSimplePanel( int ) );
218         simple_tree_panel->layout()->addWidget( simple_tree );
219         simple_tree_panel->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Preferred );
220     }
221 
222     if( ! simple_panels[SPrefsDefaultCat] )
223         changeSimplePanel( SPrefsDefaultCat );
224 
225     simple->setChecked( true );
226     stack->setCurrentIndex( SIMPLE );
227     setWindowTitle( qtr( "Simple Preferences" ) );
228 }
229 
230 /* Switching from on simple panel to another */
changeSimplePanel(int number)231 void PrefsDialog::changeSimplePanel( int number )
232 {
233     if( ! simple_panels[number] )
234     {
235         SPrefsPanel *insert = new SPrefsPanel( p_intf, simple_panels_stack, number ) ;
236         simple_panels_stack->insertWidget( number, insert );
237         simple_panels[number] = insert;
238     }
239     simple_panels_stack->setCurrentWidget( simple_panels[number] );
240 }
241 
242 /* Changing from one Advanced Panel to another */
changeAdvPanel(QTreeWidgetItem * item)243 void PrefsDialog::changeAdvPanel( QTreeWidgetItem *item )
244 {
245     if( item == NULL ) return;
246     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
247 
248     if( !data->panel )
249     {
250         data->panel = new AdvPrefsPanel( p_intf, advanced_panels_stack, data );
251         advanced_panels_stack->insertWidget( advanced_panels_stack->count(),
252                                              data->panel );
253     }
254     advanced_panels_stack->setCurrentWidget( data->panel );
255 }
256 
257 #if 0
258 /*Called from extended settings, is not used anymore, but could be useful one day*/
259 void PrefsDialog::showModulePrefs( char *psz_module )
260 {
261     setAdvanced();
262     all->setChecked( true );
263     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
264          i_cat_index++ )
265     {
266         QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
267         PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
268                                                    value<PrefsItemData *>();
269         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
270                                   i_sc_index++ )
271         {
272             QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
273             PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
274                                                     value<PrefsItemData *>();
275             for( int i_module = 0; i_module < subcat_item->childCount();
276                                    i_module++ )
277             {
278                 QTreeWidgetItem *module_item = subcat_item->child( i_module );
279                 PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
280                                                     value<PrefsItemData *>();
281                 if( !strcmp( mod_data->psz_name, psz_module ) ) {
282                     advanced_tree->setCurrentItem( module_item );
283                 }
284             }
285         }
286     }
287     show();
288 }
289 #endif
290 
291 /* Actual apply and save for the preferences */
save()292 void PrefsDialog::save()
293 {
294     if( simple->isChecked() && simple_tree->isVisible() )
295     {
296         msg_Dbg( p_intf, "Saving the simple preferences" );
297         for( int i = 0 ; i< SPrefsMax; i++ ){
298             if( simple_panels_stack->widget(i) )
299                 qobject_cast<SPrefsPanel *>(simple_panels_stack->widget(i))->apply();
300         }
301     }
302     else if( all->isChecked() && advanced_tree->isVisible() )
303     {
304         msg_Dbg( p_intf, "Saving the advanced preferences" );
305         advanced_tree->applyAll();
306     }
307 
308     /* Save to file */
309     if( config_SaveConfigFile( p_intf ) != 0 )
310     {
311         ErrorsDialog::getInstance (p_intf)->addError( qtr( "Cannot save Configuration" ),
312             qtr("Preferences file could not be saved") );
313     }
314 
315     if( p_intf->p_sys->p_mi )
316         p_intf->p_sys->p_mi->reloadPrefs();
317     accept();
318 
319     QVLCTools::saveWidgetPosition( p_intf, "Preferences", this );
320 
321 }
322 
323 /* Clean the preferences, dunno if it does something really */
cancel()324 void PrefsDialog::cancel()
325 {
326     QVLCTools::saveWidgetPosition( p_intf, "Preferences", this );
327 
328     reject();
329 }
330 
331 /* Reset all the preferences, when you click the button */
reset()332 void PrefsDialog::reset()
333 {
334     int ret = QMessageBox::question(
335                  this,
336                  qtr( "Reset Preferences" ),
337                  qtr( "Are you sure you want to reset your VLC media player preferences?" ),
338                  QMessageBox::Ok | QMessageBox::Cancel,
339                  QMessageBox::Ok);
340 
341     if( ret == QMessageBox::Ok )
342     {
343         config_ResetAll( p_intf );
344         config_SaveConfigFile( p_intf );
345         getSettings()->clear();
346 
347 #ifdef _WIN32
348         simple_panels[0]->cleanLang();
349 #endif
350 
351         accept();
352     }
353 }
354 
advancedTreeFilterChanged(const QString & text)355 void PrefsDialog::advancedTreeFilterChanged( const QString & text )
356 {
357     advanced_tree->filter( text );
358 }
359 
onlyLoadedToggled()360 void PrefsDialog::onlyLoadedToggled()
361 {
362     advanced_tree->setLoadedOnly( current_filter->isChecked() );
363 }
364