4c1c99d40312158578eed077734ce7cde963c6e1
[vlc.git] / 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 PrefsDialog *PrefsDialog::instance = NULL;
38
39 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
40 {
41      QGridLayout *main_layout = new QGridLayout(this);
42      setWindowTitle( qtr("Preferences" ) );
43      resize( 800, 450 );
44
45      tree_panel = new QWidget(0);
46      tree_panel_l = new QHBoxLayout;
47      tree_panel->setLayout( tree_panel_l );
48      main_panel = new QWidget(0);
49      main_panel_l = new QHBoxLayout;
50      main_panel->setLayout( main_panel_l );
51
52      // Choice for types
53      types = new QGroupBox( "Show settings" );
54      QHBoxLayout *tl = new QHBoxLayout(0);
55      tl->setSpacing( 3 ); tl->setMargin( 3 );
56      small = new QRadioButton( "Basic", types ); tl->addWidget( small );
57      all = new QRadioButton( "All", types ); tl->addWidget( all );
58      types->setLayout(tl);
59      small->setChecked( true );
60
61      advanced_tree = NULL;
62      simple_tree = NULL;
63      simple_panel = NULL;
64      advanced_panel = NULL;
65
66      main_layout->addWidget( types, 0,0,1,1 );
67      main_layout->addWidget( tree_panel, 1,0,1,1 );
68      main_layout->addWidget( main_panel, 0, 1, 2, 1 );
69
70      main_layout->setColumnMinimumWidth( 0, 200 );
71      main_layout->setColumnStretch( 0, 1 );
72      main_layout->setColumnStretch( 1,3 );
73
74      setSmall();
75
76      QPushButton *save, *cancel;
77      QHBoxLayout *buttonsLayout =
78          QVLCFrame::doButtons( this, NULL, &save, _("Save"),
79                                            &cancel, _("Cancel"),
80                                                 NULL, NULL );
81      connect( save, SIGNAL( clicked() ), this, SLOT( save() ) );
82      connect( cancel, SIGNAL( clicked() ), this, SLOT( cancel() ) );
83      main_layout->addLayout( buttonsLayout, 2,0, 1 ,3 );
84
85      setLayout( main_layout );
86
87      connect( small, SIGNAL( clicked() ), this, SLOT( setSmall()) );
88      connect( all, SIGNAL( clicked() ), this, SLOT( setAll()) );
89 }
90
91 void PrefsDialog::setAll()
92 {
93     if( simple_tree )
94     {
95         tree_panel_l->removeWidget( simple_tree );
96         simple_tree->hide();
97     }
98
99     if( !advanced_tree )
100     {
101          advanced_tree = new PrefsTree( p_intf, tree_panel );
102          connect( advanced_tree,
103           SIGNAL( currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *) ),
104           this, SLOT( changePanel( QTreeWidgetItem * ) ) );
105     }
106     tree_panel_l->addWidget( advanced_tree );
107     advanced_tree->show();
108
109     if( simple_panel )
110     {
111         main_panel_l->removeWidget( simple_panel );
112         simple_panel->hide();
113     }
114     if( !advanced_panel )
115          advanced_panel = new PrefsPanel( main_panel );
116     main_panel_l->addWidget( advanced_panel );
117     advanced_panel->show();
118 }
119
120 void PrefsDialog::setSmall()
121 {
122     if( advanced_tree )
123     {
124         tree_panel_l->removeWidget( advanced_tree );
125         advanced_tree->hide();
126     }
127     if( !simple_tree )
128     {
129          simple_tree = new SPrefsCatList( p_intf, tree_panel );
130          connect( simple_tree,
131           SIGNAL( currentItemChanged( QListWidgetItem *, QListWidgetItem *) ),
132           this, SLOT( changeSimplePanel( QListWidgetItem * ) ) );
133     }
134     tree_panel_l->addWidget( simple_tree );
135     simple_tree->show();
136
137     if( advanced_panel )
138     {
139         main_panel_l->removeWidget( advanced_panel );
140         advanced_panel->hide();
141     }
142     if( !simple_panel )
143         simple_panel = new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
144     main_panel_l->addWidget( simple_panel );
145     simple_panel->show();
146 }
147
148 PrefsDialog::~PrefsDialog()
149 {
150 }
151
152 void PrefsDialog::changeSimplePanel( QListWidgetItem *item )
153 {
154     int number = item->data( Qt::UserRole ).toInt();
155     if( simple_panel )
156     {
157         main_panel_l->removeWidget( simple_panel );
158         simple_panel->hide();
159         /* Don't do this once it works, you would loose all changes */
160         delete simple_panel;
161     }
162     simple_panel = new SPrefsPanel( p_intf, main_panel, number );
163     main_panel_l->addWidget( simple_panel );
164     simple_panel->show();
165 }
166
167 void PrefsDialog::changePanel( QTreeWidgetItem *item )
168 {
169     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
170
171     if( advanced_panel )
172     {
173         main_panel_l->removeWidget( advanced_panel );
174         advanced_panel->hide();
175     }
176     if( !data->panel )
177         data->panel = new PrefsPanel( p_intf, main_panel , data, true );
178
179     advanced_panel = data->panel;
180     main_panel_l->addWidget( advanced_panel );
181     advanced_panel->show();
182 }
183
184 void PrefsDialog::save()
185 {
186     if( small->isChecked() && simple_tree )
187         simple_tree->applyAll();
188     else if( all->isChecked() && advanced_tree )
189         advanced_tree->applyAll();
190     config_SaveConfigFile( p_intf, NULL );
191     hide();
192 }
193
194 void PrefsDialog::cancel()
195 {
196     if( small->isChecked() && simple_tree )
197     {
198         simple_tree->cleanAll();
199         simple_panel = NULL;
200     }
201     else if( all->isChecked() && advanced_tree )
202     {
203         advanced_tree->cleanAll();
204         advanced_panel = NULL;
205     }
206     hide();
207 }