]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Some cleanup here and there
[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             p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
83                                                        l, line );
84         else
85             p_control = new IntegerConfigControl( p_this, p_item, parent,
86                                                   l, line );
87         break;
88     case CONFIG_ITEM_FILE:
89         fprintf( stderr, "Todo\n" );
90         break;
91     case CONFIG_ITEM_BOOL:
92         p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
93         break;
94     case CONFIG_ITEM_FLOAT:
95         if( p_item->f_min || p_item->f_max )
96             p_control = new FloatRangeConfigControl( p_this, p_item, parent,
97                                                      l, line );
98         else
99             p_control = new FloatConfigControl( p_this, p_item, parent,
100                                                   l, line );
101         break;
102     default:
103         break;
104     }
105     return p_control;
106 }
107
108 void ConfigControl::doApply( intf_thread_t *p_intf )
109 {
110     switch( getType() )
111     {
112         case 1:
113         {
114             VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(this);
115             config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
116             break;
117         }
118         case 2:
119         {
120             VFloatConfigControl *vfcc = 
121                                     qobject_cast<VFloatConfigControl *>(this);
122             config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
123             break;
124         }
125         case 3:
126         {
127             VStringConfigControl *vscc =
128                             qobject_cast<VStringConfigControl *>(this);
129             config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
130         }
131     }
132 }
133
134 /**************************************************************************
135  * String-based controls
136  *************************************************************************/
137
138 /*********** String **************/
139 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
140                                           module_config_t *_p_item,
141                                           QWidget *_parent, QGridLayout *l,
142                                           int line, bool pwd ) :
143                            VStringConfigControl( _p_this, _p_item, _parent )
144 {
145     label = new QLabel( qfu(p_item->psz_text) );
146     text = new QLineEdit( qfu(p_item->psz_value) );
147     finish();
148
149     if( !l )
150     {
151         QHBoxLayout *layout = new QHBoxLayout();
152         layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
153         widget->setLayout( layout );
154     }
155     else
156     {
157         l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
158     }
159 }
160
161 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
162                                    module_config_t *_p_item,
163                                    QLabel *_label, QLineEdit *_text, bool pwd ):
164                            VStringConfigControl( _p_this, _p_item )
165 {
166     text = _text;
167     label = _label;
168     finish( );
169 }
170
171 void StringConfigControl::finish()
172 {
173     text->setText( qfu(p_item->psz_value) );
174     text->setToolTip( qfu(p_item->psz_longtext) );
175     if( label )
176         label->setToolTip( qfu(p_item->psz_longtext) );
177 }
178
179 /********* String / choice list **********/
180 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
181                module_config_t *_p_item, QWidget *_parent, bool bycat,
182                QGridLayout *l, int line) :
183                VStringConfigControl( _p_this, _p_item, _parent )
184 {
185     label = new QLabel( qfu(p_item->psz_text) );
186     combo = new QComboBox();
187     finish( bycat );
188     if( !l )
189     {
190         QHBoxLayout *layout = new QHBoxLayout();
191         layout->addWidget( label ); layout->addWidget( combo );
192         widget->setLayout( layout );
193     }
194     else
195     {
196         l->addWidget( label, line, 0 );
197         l->addWidget( combo, line, 1, Qt::AlignRight );
198     }
199 }
200 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
201                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
202                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
203 {
204     combo = _combo;
205     label = _label;
206     finish( bycat );
207 }
208
209 void StringListConfigControl::finish( bool bycat )
210 {
211     combo->setEditable( false );
212
213     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
214     {
215         combo->addItem( qfu(p_item->ppsz_list_text ?
216                             p_item->ppsz_list_text[i_index] :
217                             p_item->ppsz_list[i_index] ),
218                         QVariant( p_item->ppsz_list[i_index] ) );
219         if( p_item->psz_value && !strcmp( p_item->psz_value,
220                                           p_item->ppsz_list[i_index] ) )
221             combo->setCurrentIndex( combo->count() - 1 );
222     }
223     combo->setToolTip( qfu(p_item->psz_longtext) );
224     if( label )
225         label->setToolTip( qfu(p_item->psz_longtext) );
226 }
227
228 QString StringListConfigControl::getValue()
229 {
230     return combo->itemData( combo->currentIndex() ).toString();
231 }
232
233 /********* Module **********/
234 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
235                module_config_t *_p_item, QWidget *_parent, bool bycat,
236                QGridLayout *l, int line) :
237                VStringConfigControl( _p_this, _p_item, _parent )
238 {
239     label = new QLabel( qfu(p_item->psz_text) );
240     combo = new QComboBox();
241     finish( bycat );
242     if( !l )
243     {
244         QHBoxLayout *layout = new QHBoxLayout();
245         layout->addWidget( label ); layout->addWidget( combo );
246         widget->setLayout( layout );
247     }
248     else
249     {
250         l->addWidget( label, line, 0 );
251         l->addWidget( combo, line, 1, Qt::AlignRight );
252     }
253 }
254 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
255                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
256                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
257 {
258     combo = _combo;
259     label = _label;
260     finish( bycat );
261 }
262
263 void ModuleConfigControl::finish( bool bycat )
264 {
265     vlc_list_t *p_list;
266     module_t *p_parser;
267
268     combo->setEditable( false );
269
270     /* build a list of available modules */
271     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
272     combo->addItem( qtr("Default") );
273     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
274     {
275         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
276
277         if( bycat )
278         {
279             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
280
281             module_config_t *p_config = p_parser->p_config;
282             if( p_config ) do
283             {
284                 /* Hack: required subcategory is stored in i_min */
285                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
286                     p_config->i_value == p_item->i_min )
287                     combo->addItem( qfu(p_parser->psz_longname),
288                                     QVariant( p_parser->psz_object_name ) );
289                 if( p_item->psz_value && !strcmp( p_item->psz_value,
290                                                   p_parser->psz_object_name) )
291                     combo->setCurrentIndex( combo->count() - 1 );
292             } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
293         }
294         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
295         {
296             combo->addItem( qfu(p_parser->psz_longname),
297                             QVariant( p_parser->psz_object_name ) );
298             if( p_item->psz_value && !strcmp( p_item->psz_value,
299                                               p_parser->psz_object_name) )
300                 combo->setCurrentIndex( combo->count() - 1 );
301         }
302     }
303     vlc_list_release( p_list );
304     combo->setToolTip( qfu(p_item->psz_longtext) );
305     if( label )
306         label->setToolTip( qfu(p_item->psz_longtext) );
307 }
308
309 QString ModuleConfigControl::getValue()
310 {
311     return combo->itemData( combo->currentIndex() ).toString();
312 }
313
314 /**************************************************************************
315  * Integer-based controls
316  *************************************************************************/
317
318 /*********** Integer **************/
319 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
320                                             module_config_t *_p_item,
321                                             QWidget *_parent, QGridLayout *l,
322                                             int line ) :
323                            VIntConfigControl( _p_this, _p_item, _parent )
324 {
325     label = new QLabel( qfu(p_item->psz_text) );
326     spin = new QSpinBox; spin->setMinimumWidth( 80 );
327     spin->setMaximumWidth( 90 );
328     finish();
329
330     if( !l )
331     {
332         QHBoxLayout *layout = new QHBoxLayout();
333         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
334         widget->setLayout( layout );
335     }
336     else
337     {
338         l->addWidget( label, line, 0 );
339         l->addWidget( spin, line, 1, Qt::AlignRight );
340     }
341 }
342 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
343                                             module_config_t *_p_item,
344                                             QLabel *_label, QSpinBox *_spin ) :
345                                       VIntConfigControl( _p_this, _p_item )
346 {
347     spin = _spin;
348     label = _label;
349     finish();
350 }
351
352 void IntegerConfigControl::finish()
353 {
354     spin->setMaximum( 2000000000 );
355     spin->setMinimum( -2000000000 );
356     spin->setValue( p_item->i_value );
357     spin->setToolTip( qfu(p_item->psz_longtext) );
358     if( label )
359         label->setToolTip( qfu(p_item->psz_longtext) );
360 }
361
362 int IntegerConfigControl::getValue()
363 {
364     return spin->value();
365 }
366
367 /********* Integer range **********/
368 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
369                                             module_config_t *_p_item,
370                                             QWidget *_parent, QGridLayout *l,
371                                             int line ) :
372             IntegerConfigControl( _p_this, _p_item, _parent, l, line )
373 {
374     finish();
375 }
376
377 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
378                                             module_config_t *_p_item,
379                                             QLabel *_label, QSpinBox *_spin ) :
380             IntegerConfigControl( _p_this, _p_item, _label, _spin )
381 {
382     finish();
383 }
384
385 void IntegerRangeConfigControl::finish()
386 {
387     spin->setMaximum( p_item->i_max );
388     spin->setMinimum( p_item->i_min );
389 }
390
391 /********* Integer / choice list **********/
392 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
393                module_config_t *_p_item, QWidget *_parent, bool bycat,
394                QGridLayout *l, int line) :
395                VIntConfigControl( _p_this, _p_item, _parent )
396 {
397     label = new QLabel( qfu(p_item->psz_text) );
398     combo = new QComboBox();
399     finish( bycat );
400     if( !l )
401     {
402         QHBoxLayout *layout = new QHBoxLayout();
403         layout->addWidget( label ); layout->addWidget( combo );
404         widget->setLayout( layout );
405     }
406     else
407     {
408         l->addWidget( label, line, 0 );
409         l->addWidget( combo, line, 1, Qt::AlignRight );
410     }
411 }
412 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
413                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
414                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
415 {
416     combo = _combo;
417     label = _label;
418     finish( bycat );
419 }
420
421 void IntegerListConfigControl::finish( bool bycat )
422 {
423     combo->setEditable( false );
424
425     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
426     {
427         combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
428                         QVariant( p_item->pi_list[i_index] ) );
429         if( p_item->i_value == p_item->pi_list[i_index] )
430             combo->setCurrentIndex( combo->count() - 1 );
431     }
432     combo->setToolTip( qfu(p_item->psz_longtext) );
433     if( label )
434         label->setToolTip( qfu(p_item->psz_longtext) );
435 }
436
437 int IntegerListConfigControl::getValue()
438 {
439     return combo->itemData( combo->currentIndex() ).toInt();
440 }
441
442 /*********** Boolean **************/
443 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
444                                       module_config_t *_p_item,
445                                       QWidget *_parent, QGridLayout *l,
446                                       int line ) :
447                     VIntConfigControl( _p_this, _p_item, _parent )
448 {
449     checkbox = new QCheckBox( qfu(p_item->psz_text) );
450     finish();
451
452     if( !l )
453     {
454         QHBoxLayout *layout = new QHBoxLayout();
455         layout->addWidget( checkbox, 0 );
456         widget->setLayout( layout );
457     }
458     else
459     {
460         l->addWidget( checkbox, line, 0 );
461     }
462 }
463 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
464                                       module_config_t *_p_item,
465                                       QLabel *_label,
466                                       QCheckBox *_checkbox,
467                                       bool bycat ) :
468                    VIntConfigControl( _p_this, _p_item )
469 {
470     checkbox = _checkbox;
471     finish();
472 }
473
474 void BoolConfigControl::finish()
475 {
476     checkbox->setCheckState( p_item->i_value == VLC_TRUE ? Qt::Checked
477                                                         : Qt::Unchecked );
478     checkbox->setToolTip( qfu(p_item->psz_longtext) );
479 }
480
481 int BoolConfigControl::getValue()
482 {
483     return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
484 }
485
486 /**************************************************************************
487  * Float-based controls
488  *************************************************************************/
489
490 /*********** Float **************/
491 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
492                                         module_config_t *_p_item,
493                                         QWidget *_parent, QGridLayout *l,
494                                         int line ) :
495                     VFloatConfigControl( _p_this, _p_item, _parent )
496 {
497     label = new QLabel( qfu(p_item->psz_text) );
498     spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
499     spin->setMaximumWidth( 90 );
500     finish();
501
502     if( !l )
503     {
504         QHBoxLayout *layout = new QHBoxLayout();
505         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
506         widget->setLayout( layout );
507     }
508     else
509     {
510         l->addWidget( label, line, 0 );
511         l->addWidget( spin, line, 1, Qt::AlignRight );
512     }
513 }
514
515 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
516                                         module_config_t *_p_item,
517                                         QLabel *_label,
518                                         QDoubleSpinBox *_spin ) :
519                     VFloatConfigControl( _p_this, _p_item )
520 {
521     spin = _spin;
522     label = _label;
523     finish();
524 }
525
526 void FloatConfigControl::finish()
527 {
528     spin->setMaximum( 2000000000. );
529     spin->setMinimum( -2000000000. );
530     spin->setSingleStep( 0.1 );
531     spin->setValue( (double)p_item->f_value );
532     spin->setToolTip( qfu(p_item->psz_longtext) );
533     if( label )
534         label->setToolTip( qfu(p_item->psz_longtext) );
535 }
536
537 float FloatConfigControl::getValue()
538 {
539     return (float)spin->value();
540 }
541
542 /*********** Float with range **************/
543 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
544                                         module_config_t *_p_item,
545                                         QWidget *_parent, QGridLayout *l,
546                                         int line ) :
547                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
548 {
549     finish();
550 }
551
552 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
553                                         module_config_t *_p_item,
554                                         QLabel *_label,
555                                         QDoubleSpinBox *_spin ) :
556                 FloatConfigControl( _p_this, _p_item, _label, _spin )
557 {
558     finish();
559 }
560
561 void FloatRangeConfigControl::finish()
562 {
563     spin->setMaximum( (double)p_item->f_max );
564     spin->setMinimum( (double)p_item->f_min );
565 }