preferences.cpp 12 KB
Newer Older
Clément Stenac's avatar
Clément Stenac committed
1
/*****************************************************************************
2
 * preferences.cpp : Preferences
3 4
 *****************************************************************************
 * Copyright (C) 2006-2007 the VideoLAN team
5
 * $Id$
Clément Stenac's avatar
Clément Stenac committed
6 7
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 *          Jean-Baptiste Kempf <jb@videolan.org>
9
 *
Clément Stenac's avatar
Clément Stenac committed
10 11 12 13 14 15 16 17 18 19 20 21
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
22 23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
24

25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
Clément Stenac's avatar
Clément Stenac committed
28

29
#include "dialogs/preferences.hpp"
Clément Stenac's avatar
Clément Stenac committed
30
#include "util/qvlcframe.hpp"
31
#include "dialogs/errors.hpp"
Clément Stenac's avatar
Clément Stenac committed
32

33
#include "components/complete_preferences.hpp"
Clément Stenac's avatar
Clément Stenac committed
34
#include "components/simple_preferences.hpp"
Yuval Tze's avatar
Yuval Tze committed
35
#include "util/searchlineedit.hpp"
36
#include "util/qvlcframe.hpp"
37
#include "main_interface.hpp"
Clément Stenac's avatar
Clément Stenac committed
38 39 40 41 42

#include <QHBoxLayout>
#include <QGroupBox>
#include <QRadioButton>
#include <QPushButton>
43
#include <QMessageBox>
44
#include <QDialogButtonBox>
45
#include <QStackedWidget>
46
#include <QSplitter>
47
#include <QShortcut>
48
#include <QScrollArea>
49

50 51
PrefsDialog::PrefsDialog( QWidget *parent, intf_thread_t *_p_intf )
            : QVLCDialog( parent, _p_intf )
Clément Stenac's avatar
Clément Stenac committed
52
{
53 54
    QGridLayout *main_layout = new QGridLayout( this );
    setWindowTitle( qtr( "Preferences" ) );
Nick Pope's avatar
Nick Pope committed
55
    setWindowRole( "vlc-preferences" );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
56
    setWindowModality( Qt::WindowModal );
Clément Stenac's avatar
Clément Stenac committed
57

58 59 60 61
    /* Whether we want it or not, we need to destroy on close to get
       consistency when reset */
    setAttribute( Qt::WA_DeleteOnClose );

62
    /* Create Panels */
63 64 65 66 67 68
    simple_tree_panel = new QWidget;
    simple_tree_panel->setLayout( new QVBoxLayout );

    advanced_tree_panel = new QWidget;
    advanced_tree_panel->setLayout( new QVBoxLayout );

69
    /* Choice for types */
Christophe Mutricy's avatar
Christophe Mutricy committed
70
    types = new QGroupBox( qtr("Show settings") );
71
    types->setAlignment( Qt::AlignHCenter );
72
    QHBoxLayout *types_l = new QHBoxLayout;
73
    types_l->setSpacing( 3 ); types_l->setMargin( 3 );
74 75 76
    simple = new QRadioButton( qtr( "Simple" ), types );
    simple->setToolTip( qtr( "Switch to simple preferences view" ) );
    types_l->addWidget( simple );
Christophe Mutricy's avatar
Christophe Mutricy committed
77
    all = new QRadioButton( qtr("All"), types ); types_l->addWidget( all );
78
    all->setToolTip( qtr( "Switch to full preferences view" ) );
79
    types->setLayout( types_l );
80
    simple->setChecked( true );
Clément Stenac's avatar
Clément Stenac committed
81

82 83
    /* Tree and panel initialisations */
    advanced_tree = NULL;
Yuval Tze's avatar
Yuval Tze committed
84
    tree_filter = NULL;
85
    current_filter = NULL;
86
    simple_tree = NULL;
87
    simple_panels_stack = new QStackedWidget;
88
    advanced_panels_stack = new QStackedWidget;
89

90 91 92
    /* Buttons */
    QDialogButtonBox *buttonsBox = new QDialogButtonBox();
    QPushButton *save = new QPushButton( qtr( "&Save" ) );
93
    save->setToolTip( qtr( "Save and close the dialog" ) );
94 95
    QPushButton *cancel = new QPushButton( qtr( "&Cancel" ) );
    QPushButton *reset = new QPushButton( qtr( "&Reset Preferences" ) );
96

97 98
    buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
    buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
99
    buttonsBox->addButton( reset, QDialogButtonBox::ResetRole );
Clément Stenac's avatar
Clément Stenac committed
100

101
    simple_split_widget = new QWidget();
102
    simple_split_widget->setLayout( new QVBoxLayout );
103

104
    advanced_split_widget = new QSplitter();
105 106 107 108 109 110

    stack = new QStackedWidget();
    stack->insertWidget( SIMPLE, simple_split_widget );
    stack->insertWidget( ADVANCED, advanced_split_widget );

    simple_split_widget->layout()->addWidget( simple_tree_panel );
111
    simple_split_widget->layout()->addWidget( simple_panels_stack );
112 113
    simple_split_widget->layout()->setMargin( 0 );

114 115
    advanced_split_widget->addWidget( advanced_tree_panel );
    advanced_split_widget->addWidget( advanced_panels_stack );
116

117
    /* Layout  */
118
    main_layout->addWidget( stack, 0, 0, 3, 3 );
119
    main_layout->addWidget( types, 3, 0, 2, 1 );
120
    main_layout->addWidget( buttonsBox, 4, 2, 1 ,1 );
121
    main_layout->setRowStretch( 2, 4 );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
122
    main_layout->setMargin( 9 );
123
    setLayout( main_layout );
Clément Stenac's avatar
Clément Stenac committed
124

125
    /* Margins */
126
    simple_tree_panel->layout()->setMargin( 1 );
127
    simple_panels_stack->layout()->setContentsMargins( 6, 0, 0, 3 );
128

129 130
    for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
131 132
    if( var_InheritBool( p_intf, "qt-advanced-pref" )
     || var_InheritBool( p_intf, "advanced" ) )
133
        setAdvanced();
134
    else
135
        setSimple();
136

137 138 139
    BUTTONACT( save, save() );
    BUTTONACT( cancel, cancel() );
    BUTTONACT( reset, reset() );
Clément Stenac's avatar
Clément Stenac committed
140

141
    BUTTONACT( simple, setSimple() );
142
    BUTTONACT( all, setAdvanced() );
143

144
    QVLCTools::restoreWidgetPosition( p_intf, "Preferences", this, QSize( 800 , 700 ) );
Clément Stenac's avatar
Clément Stenac committed
145
}
Clément Stenac's avatar
Clément Stenac committed
146

147
void PrefsDialog::setAdvanced()
Clément Stenac's avatar
Clément Stenac committed
148
{
Yuval Tze's avatar
Yuval Tze committed
149 150
    if ( !tree_filter )
    {
151
        tree_filter = new SearchLineEdit( simple_tree_panel );
Yuval Tze's avatar
Yuval Tze committed
152 153 154 155 156
        tree_filter->setMinimumHeight( 26 );

        CONNECT( tree_filter, textChanged( const QString &  ),
                this, advancedTreeFilterChanged( const QString & ) );

157
        advanced_tree_panel->layout()->addWidget( tree_filter );
158 159 160 161 162 163 164

        current_filter = new QCheckBox( qtr("Only show current") );
        current_filter->setToolTip(
                    qtr("Only show modules related to current playback") );
        CONNECT( current_filter, stateChanged(int),
                 this, onlyLoadedToggled() );
        advanced_tree_panel->layout()->addWidget( current_filter );
165 166 167

        QShortcut *search = new QShortcut( QKeySequence( QKeySequence::Find ), tree_filter );
        CONNECT( search, activated(), tree_filter, setFocus() );
Yuval Tze's avatar
Yuval Tze committed
168 169
    }

170
    /* If don't have already and advanced TREE, then create it */
Clément Stenac's avatar
Clément Stenac committed
171 172
    if( !advanced_tree )
    {
173
        /* Creation */
174
         advanced_tree = new PrefsTree( p_intf, simple_tree_panel );
175
        /* and connections */
Clément Stenac's avatar
Clément Stenac committed
176
         CONNECT( advanced_tree,
177
                  currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem * ),
178
                  this, changeAdvPanel( QTreeWidgetItem * ) );
179 180
        advanced_tree_panel->layout()->addWidget( advanced_tree );
        advanced_tree_panel->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
Clément Stenac's avatar
Clément Stenac committed
181
    }
182

183
    /* If no advanced Panel exist, create one, attach it and show it*/
184
    if( advanced_panels_stack->count() < 1 )
185
    {
186 187
        AdvPrefsPanel *insert = new AdvPrefsPanel( advanced_panels_stack );
        advanced_panels_stack->insertWidget( 0, insert );
188
    }
189

190 191 192 193 194
    /* Select the first Item of the preferences. Maybe you want to select a specified
       category... */
    advanced_tree->setCurrentIndex(
            advanced_tree->model()->index( 0, 0, QModelIndex() ) );

195
    all->setChecked( true );
196
    stack->setCurrentIndex( ADVANCED );
197
    setWindowTitle( qtr( "Advanced Preferences" ) );
Clément Stenac's avatar
Clément Stenac committed
198 199
}

200
void PrefsDialog::setSimple()
Clément Stenac's avatar
Clément Stenac committed
201
{
202
    /* If no simple_tree, create one, connect it */
Clément Stenac's avatar
Clément Stenac committed
203
    if( !simple_tree )
Clément Stenac's avatar
Clément Stenac committed
204
    {
205
         simple_tree = new SPrefsCatList( p_intf, simple_tree_panel );
Clément Stenac's avatar
Clément Stenac committed
206
         CONNECT( simple_tree,
207 208
                  currentItemChanged( int ),
                  this,  changeSimplePanel( int ) );
209 210
        simple_tree_panel->layout()->addWidget( simple_tree );
        simple_tree_panel->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Preferred );
Clément Stenac's avatar
Clément Stenac committed
211
    }
212

213 214
    if( ! simple_panels[SPrefsDefaultCat] )
        changeSimplePanel( SPrefsDefaultCat );
215

216
    simple->setChecked( true );
217
    stack->setCurrentIndex( SIMPLE );
218
    setWindowTitle( qtr( "Simple Preferences" ) );
Clément Stenac's avatar
Clément Stenac committed
219 220
}

221
/* Switching from on simple panel to another */
222
void PrefsDialog::changeSimplePanel( int number )
Clément Stenac's avatar
Clément Stenac committed
223
{
224
    if( ! simple_panels[number] )
225
    {
226
        SPrefsPanel *insert = new SPrefsPanel( p_intf, simple_panels_stack, number ) ;
227 228
        simple_panels_stack->insertWidget( number, insert );
        simple_panels[number] = insert;
229
    }
230
    simple_panels_stack->setCurrentWidget( simple_panels[number] );
Clément Stenac's avatar
Clément Stenac committed
231 232
}

233 234
/* Changing from one Advanced Panel to another */
void PrefsDialog::changeAdvPanel( QTreeWidgetItem *item )
Clément Stenac's avatar
Clément Stenac committed
235
{
236
    if( item == NULL ) return;
Clément Stenac's avatar
Clément Stenac committed
237 238 239
    PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();

    if( !data->panel )
240
    {
241 242 243
        data->panel = new AdvPrefsPanel( p_intf, advanced_panels_stack, data );
        advanced_panels_stack->insertWidget( advanced_panels_stack->count(),
                                             data->panel );
244
    }
245
    advanced_panels_stack->setCurrentWidget( data->panel );
Clément Stenac's avatar
Clément Stenac committed
246 247
}

248
#if 0
249
/*Called from extended settings, is not used anymore, but could be useful one day*/
Clément Stenac's avatar
Clément Stenac committed
250 251
void PrefsDialog::showModulePrefs( char *psz_module )
{
252
    setAdvanced();
Clément Stenac's avatar
Clément Stenac committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    all->setChecked( true );
    for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
         i_cat_index++ )
    {
        QTreeWidgetItem *cat_item = advanced_tree->topLevelItem( i_cat_index );
        PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
                                                   value<PrefsItemData *>();
        for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
                                  i_sc_index++ )
        {
            QTreeWidgetItem *subcat_item = cat_item->child( i_sc_index );
            PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
                                                    value<PrefsItemData *>();
            for( int i_module = 0; i_module < subcat_item->childCount();
                                   i_module++ )
            {
                QTreeWidgetItem *module_item = subcat_item->child( i_module );
270
                PrefsItemData *mod_data = module_item->data( 0, Qt::UserRole ).
Clément Stenac's avatar
Clément Stenac committed
271 272 273 274 275 276 277 278 279
                                                    value<PrefsItemData *>();
                if( !strcmp( mod_data->psz_name, psz_module ) ) {
                    advanced_tree->setCurrentItem( module_item );
                }
            }
        }
    }
    show();
}
280
#endif
Clément Stenac's avatar
Clément Stenac committed
281

282
/* Actual apply and save for the preferences */
Clément Stenac's avatar
Clément Stenac committed
283 284
void PrefsDialog::save()
{
285
    if( simple->isChecked() && simple_tree->isVisible() )
Clément Stenac's avatar
Clément Stenac committed
286
    {
287
        msg_Dbg( p_intf, "Saving the simple preferences" );
288
        for( int i = 0 ; i< SPrefsMax; i++ ){
289 290
            if( simple_panels_stack->widget(i) )
                qobject_cast<SPrefsPanel *>(simple_panels_stack->widget(i))->apply();
291
        }
Clément Stenac's avatar
Clément Stenac committed
292
    }
293 294 295
    else if( all->isChecked() && advanced_tree->isVisible() )
    {
        msg_Dbg( p_intf, "Saving the advanced preferences" );
Clément Stenac's avatar
Clément Stenac committed
296
        advanced_tree->applyAll();
297
    }
298

299
    /* Save to file */
300
    if( config_SaveConfigFile( p_intf ) != 0 )
301 302 303 304
    {
        ErrorsDialog::getInstance (p_intf)->addError( qtr( "Cannot save Configuration" ),
            qtr("Preferences file could not be saved") );
    }
305 306 307

    if( p_intf->p_sys->p_mi )
        p_intf->p_sys->p_mi->reloadPrefs();
308
    accept();
309 310 311

    QVLCTools::saveWidgetPosition( p_intf, "Preferences", this );

312 313
}

314
/* Clean the preferences, dunno if it does something really */
Clément Stenac's avatar
Clément Stenac committed
315 316
void PrefsDialog::cancel()
{
317 318
    QVLCTools::saveWidgetPosition( p_intf, "Preferences", this );

319
    reject();
Clément Stenac's avatar
Clément Stenac committed
320
}
321

322
/* Reset all the preferences, when you click the button */
323 324
void PrefsDialog::reset()
{
325 326 327
    int ret = QMessageBox::question(
                 this,
                 qtr( "Reset Preferences" ),
328
                 qtr( "Are you sure you want to reset your VLC media player preferences?" ),
329 330 331
                 QMessageBox::Ok | QMessageBox::Cancel,
                 QMessageBox::Ok);

332
    if( ret == QMessageBox::Ok )
333 334
    {
        config_ResetAll( p_intf );
335
        config_SaveConfigFile( p_intf );
336
        getSettings()->clear();
337

338 339 340 341
#ifdef _WIN32
        simple_panels[0]->cleanLang();
#endif

342
        accept();
343 344
    }
}
Yuval Tze's avatar
Yuval Tze committed
345 346 347 348 349

void PrefsDialog::advancedTreeFilterChanged( const QString & text )
{
    advanced_tree->filter( text );
}
350 351 352 353 354

void PrefsDialog::onlyLoadedToggled()
{
    advanced_tree->setLoadedOnly( current_filter->isChecked() );
}