]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
preferences_widgets.*: Bool prefs widget
[vlc] / modules / gui / qt4 / components / preferences_widgets.cpp
1 /*****************************************************************************
2  * preferences_widgets.cpp : Widgets for preferences displays
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Antoine Cellerier <dionoea@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 /**
26  * Todo:
27  *  - Finish implementation (see WX)
28  *  - Improvements over WX
29  *      - Password field implementation (through "pwd" bool param
30  *      - Validator for modulelist
31  *  - Implement update stuff using a general Updated signal
32  */
33
34 #include "components/preferences_widgets.hpp"
35 #include "qt4.hpp"
36 #include <QLineEdit>
37 #include <QString>
38 #include <QSpinBox>
39 #include <QDoubleSpinBox>
40 #include <QVariant>
41 #include <QComboBox>
42 #include <QGridLayout>
43
44 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
45                                              module_config_t *p_item,
46                                              QWidget *parent )
47 {
48     return createControl( p_this, p_item, parent, NULL, 0 );
49 }
50
51 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
52                                              module_config_t *p_item,
53                                              QWidget *parent,
54                                              QGridLayout *l, int line )
55 {
56     ConfigControl *p_control = NULL;
57     if( p_item->psz_current ) return NULL;
58
59     switch( p_item->i_type )
60     {
61     case CONFIG_ITEM_MODULE:
62         p_control = new ModuleConfigControl( p_this, p_item, parent, false,
63                                              l, line );
64         break;
65     case CONFIG_ITEM_MODULE_CAT:
66         p_control = new ModuleConfigControl( p_this, p_item, parent, true,
67                                              l, line );
68         break;
69     case CONFIG_ITEM_STRING:
70         if( !p_item->i_list )
71             p_control = new StringConfigControl( p_this, p_item, parent,
72                                                  l, line, false );
73         else
74             p_control = new StringListConfigControl( p_this, p_item,
75                                             parent, false, l, line );
76         break;
77     case CONFIG_ITEM_INTEGER:
78         if( p_item->i_list )
79             p_control = new IntegerListConfigControl( p_this, p_item,
80                                             parent, false, l, line );
81         else if( p_item->i_min || p_item->i_max )
82             fprintf( stderr, "Todo\n" );
83         else
84             p_control = new IntegerConfigControl( p_this, p_item, parent,
85                                                   l, line );
86         break;
87     case CONFIG_ITEM_FILE:
88         fprintf( stderr, "Todo\n" );
89         break;
90     case CONFIG_ITEM_BOOL:
91         p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
92         break;
93     case CONFIG_ITEM_FLOAT:
94         if( p_item->f_min || p_item->f_max )
95             fprintf( stderr, "Todo\n" );
96         else
97             fprintf( stderr, "Todo\n" );
98         break;
99     default:
100         break;
101     }
102     return p_control;
103 }
104
105 /**************************************************************************
106  * String-based controls
107  *************************************************************************/
108
109 /*********** String **************/
110 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
111                                           module_config_t *_p_item,
112                                           QWidget *_parent, QGridLayout *l,
113                                           int line, bool pwd ) :
114                            VStringConfigControl( _p_this, _p_item, _parent )
115 {
116     label = new QLabel( qfu(p_item->psz_text) );
117     text = new QLineEdit( qfu(p_item->psz_value) );
118     finish();
119
120     if( !l )
121     {
122         QHBoxLayout *layout = new QHBoxLayout();
123         layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
124         widget->setLayout( layout );
125     }
126     else
127     {
128         l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
129     }
130 }
131
132 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
133                                    module_config_t *_p_item,
134                                    QLabel *_label, QLineEdit *_text, bool pwd ):
135                            VStringConfigControl( _p_this, _p_item )
136 {
137     text = _text;
138     label = _label;
139     finish( );
140 }
141
142 void StringConfigControl::finish()
143 {
144     text->setText( qfu(p_item->psz_value) );
145     text->setToolTip( qfu(p_item->psz_longtext) );
146     if( label )
147         label->setToolTip( qfu(p_item->psz_longtext) );
148 }
149
150 /********* String / choice list **********/
151 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
152                module_config_t *_p_item, QWidget *_parent, bool bycat,
153                QGridLayout *l, int line) :
154                VStringConfigControl( _p_this, _p_item, _parent )
155 {
156     label = new QLabel( qfu(p_item->psz_text) );
157     combo = new QComboBox();
158     finish( bycat );
159     if( !l )
160     {
161         QHBoxLayout *layout = new QHBoxLayout();
162         layout->addWidget( label ); layout->addWidget( combo );
163         widget->setLayout( layout );
164     }
165     else
166     {
167         l->addWidget( label, line, 0 );
168         l->addWidget( combo, line, 1, Qt::AlignRight );
169     }
170 }
171 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
172                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
173                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
174 {
175     combo = _combo;
176     label = _label;
177     finish( bycat );
178 }
179
180 void StringListConfigControl::finish( bool bycat )
181 {
182     combo->setEditable( false );
183
184     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
185     {
186         combo->addItem( qfu(p_item->ppsz_list_text ?
187                             p_item->ppsz_list_text[i_index] :
188                             p_item->ppsz_list[i_index] ),
189                         QVariant( p_item->ppsz_list[i_index] ) );
190         if( p_item->psz_value && !strcmp( p_item->psz_value,
191                                           p_item->ppsz_list[i_index] ) )
192             combo->setCurrentIndex( combo->count() - 1 );
193     }
194     combo->setToolTip( qfu(p_item->psz_longtext) );
195     if( label )
196         label->setToolTip( qfu(p_item->psz_longtext) );
197 }
198
199 QString StringListConfigControl::getValue()
200 {
201     return combo->itemData( combo->currentIndex() ).toString();
202 }
203
204 /********* Module **********/
205 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
206                module_config_t *_p_item, QWidget *_parent, bool bycat,
207                QGridLayout *l, int line) :
208                VStringConfigControl( _p_this, _p_item, _parent )
209 {
210     label = new QLabel( qfu(p_item->psz_text) );
211     combo = new QComboBox();
212     finish( bycat );
213     if( !l )
214     {
215         QHBoxLayout *layout = new QHBoxLayout();
216         layout->addWidget( label ); layout->addWidget( combo );
217         widget->setLayout( layout );
218     }
219     else
220     {
221         l->addWidget( label, line, 0 );
222         l->addWidget( combo, line, 1, Qt::AlignRight );
223     }
224 }
225 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
226                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
227                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
228 {
229     combo = _combo;
230     label = _label;
231     finish( bycat );
232 }
233
234 void ModuleConfigControl::finish( bool bycat )
235 {
236     vlc_list_t *p_list;
237     module_t *p_parser;
238
239     combo->setEditable( false );
240
241     /* build a list of available modules */
242     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
243     combo->addItem( qtr("Default") );
244     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
245     {
246         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
247
248         if( bycat )
249         {
250             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
251
252             module_config_t *p_config = p_parser->p_config;
253             if( p_config ) do
254             {
255                 /* Hack: required subcategory is stored in i_min */
256                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
257                     p_config->i_value == p_item->i_min )
258                     combo->addItem( qfu(p_parser->psz_longname),
259                                     QVariant( p_parser->psz_object_name ) );
260                 if( p_item->psz_value && !strcmp( p_item->psz_value,
261                                                   p_parser->psz_object_name) )
262                     combo->setCurrentIndex( combo->count() - 1 );
263             } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
264         }
265         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
266         {
267             combo->addItem( qfu(p_parser->psz_longname),
268                             QVariant( p_parser->psz_object_name ) );
269             if( p_item->psz_value && !strcmp( p_item->psz_value,
270                                               p_parser->psz_object_name) )
271                 combo->setCurrentIndex( combo->count() - 1 );
272         }
273     }
274     vlc_list_release( p_list );
275     combo->setToolTip( qfu(p_item->psz_longtext) );
276     if( label )
277         label->setToolTip( qfu(p_item->psz_longtext) );
278 }
279
280 QString ModuleConfigControl::getValue()
281 {
282     return combo->itemData( combo->currentIndex() ).toString();
283 }
284
285 /**************************************************************************
286  * Integer-based controls
287  *************************************************************************/
288
289 /*********** Integer **************/
290 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
291                                             module_config_t *_p_item,
292                                             QWidget *_parent, QGridLayout *l,
293                                             int line ) :
294                            VIntConfigControl( _p_this, _p_item, _parent )
295 {
296     label = new QLabel( qfu(p_item->psz_text) );
297     spin = new QSpinBox; spin->setMinimumWidth( 80 );
298     spin->setMaximumWidth( 90 );
299     finish();
300
301     if( !l )
302     {
303         QHBoxLayout *layout = new QHBoxLayout();
304         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
305         widget->setLayout( layout );
306     }
307     else
308     {
309         l->addWidget( label, line, 0 );
310         l->addWidget( spin, line, 1, Qt::AlignRight );
311     }
312 }
313 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
314                                             module_config_t *_p_item,
315                                             QLabel *_label, QSpinBox *_spin ) :
316                                       VIntConfigControl( _p_this, _p_item )
317 {
318     spin = _spin;
319     label = _label;
320     finish();
321 }
322
323 void IntegerConfigControl::finish()
324 {
325     spin->setMaximum( 2000000000 );
326     spin->setValue( p_item->i_value );
327     spin->setToolTip( qfu(p_item->psz_longtext) );
328     if( label )
329         label->setToolTip( qfu(p_item->psz_longtext) );
330 }
331
332 int IntegerConfigControl::getValue()
333 {
334     return spin->value();
335 }
336
337 /********* Integer / choice list **********/
338 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
339                module_config_t *_p_item, QWidget *_parent, bool bycat,
340                QGridLayout *l, int line) :
341                VIntConfigControl( _p_this, _p_item, _parent )
342 {
343     label = new QLabel( qfu(p_item->psz_text) );
344     combo = new QComboBox();
345     finish( bycat );
346     if( !l )
347     {
348         QHBoxLayout *layout = new QHBoxLayout();
349         layout->addWidget( label ); layout->addWidget( combo );
350         widget->setLayout( layout );
351     }
352     else
353     {
354         l->addWidget( label, line, 0 );
355         l->addWidget( combo, line, 1, Qt::AlignRight );
356     }
357 }
358 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
359                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
360                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
361 {
362     combo = _combo;
363     label = _label;
364     finish( bycat );
365 }
366
367 void IntegerListConfigControl::finish( bool bycat )
368 {
369     combo->setEditable( false );
370
371     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
372     {
373         combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
374                         QVariant( p_item->pi_list[i_index] ) );
375         if( p_item->i_value == p_item->pi_list[i_index] )
376             combo->setCurrentIndex( combo->count() - 1 );
377     }
378     combo->setToolTip( qfu(p_item->psz_longtext) );
379     if( label )
380         label->setToolTip( qfu(p_item->psz_longtext) );
381 }
382
383 int IntegerListConfigControl::getValue()
384 {
385     return combo->itemData( combo->currentIndex() ).toInt();
386 }
387
388 /*********** Boolean **************/
389 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
390                                       module_config_t *_p_item,
391                                       QWidget *_parent, QGridLayout *l,
392                                       int line ) :
393                     VIntConfigControl( _p_this, _p_item, _parent )
394 {
395     checkbox = new QCheckBox( qfu(p_item->psz_text) );
396     finish();
397
398     if( !l )
399     {
400         QHBoxLayout *layout = new QHBoxLayout();
401         layout->addWidget( checkbox, 0 );
402         widget->setLayout( layout );
403     }
404     else
405     {
406         l->addWidget( checkbox, line, 0 );
407     }
408 }
409 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
410                                       module_config_t *_p_item,
411                                       QLabel *_label,
412                                       QCheckBox *_checkbox,
413                                       bool bycat ) :
414                    VIntConfigControl( _p_this, _p_item )
415 {
416     checkbox = _checkbox;
417     finish();
418 }
419
420 void BoolConfigControl::finish()
421 {
422     checkbox->setCheckState( p_item->i_value == VLC_TRUE ? Qt::Checked
423                                                         : Qt::Unchecked );
424     checkbox->setToolTip( qfu(p_item->psz_longtext) );
425 }
426
427 int BoolConfigControl::getValue()
428 {
429     return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
430 }