]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Skeleton for modules list widgets in the prefs. I'm not working on it atm so i figure...
[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     int i=0;
49     return createControl( p_this, p_item, parent, NULL, i );
50 }
51
52 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
53                                              module_config_t *p_item,
54                                              QWidget *parent,
55                                              QGridLayout *l, int &line )
56 {
57     ConfigControl *p_control = NULL;
58     if( p_item->psz_current ) return NULL;
59
60     switch( p_item->i_type )
61     {
62     case CONFIG_ITEM_MODULE:
63         p_control = new ModuleConfigControl( p_this, p_item, parent, false,
64                                              l, line );
65         break;
66     case CONFIG_ITEM_MODULE_CAT:
67         p_control = new ModuleConfigControl( p_this, p_item, parent, true,
68                                              l, line );
69         break;
70     case CONFIG_ITEM_MODULE_LIST:
71         p_control = new ModuleListConfigControl( p_this, p_item, parent, false,
72                                              l, line );
73         break;
74     case CONFIG_ITEM_MODULE_LIST_CAT:
75         p_control = new ModuleListConfigControl( p_this, p_item, parent, true,
76                                              l, line );
77         break;
78     case CONFIG_ITEM_STRING:
79         if( !p_item->i_list )
80             p_control = new StringConfigControl( p_this, p_item, parent,
81                                                  l, line, false );
82         else
83             p_control = new StringListConfigControl( p_this, p_item,
84                                             parent, false, l, line );
85         break;
86     case CONFIG_ITEM_INTEGER:
87         if( p_item->i_list )
88             p_control = new IntegerListConfigControl( p_this, p_item,
89                                             parent, false, l, line );
90         else if( p_item->i_min || p_item->i_max )
91             p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
92                                                        l, line );
93         else
94             p_control = new IntegerConfigControl( p_this, p_item, parent,
95                                                   l, line );
96         break;
97     case CONFIG_ITEM_FILE:
98         fprintf( stderr, "Todo (CONFIG_ITEM_FILE)\n" );
99         break;
100     case CONFIG_ITEM_DIRECTORY:
101         fprintf( stderr, "Todo (CONFIG_ITEM_DIRECTORY)\n" );
102         break;
103     case CONFIG_ITEM_BOOL:
104         p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
105         break;
106     case CONFIG_ITEM_FLOAT:
107         if( p_item->f_min || p_item->f_max )
108             p_control = new FloatRangeConfigControl( p_this, p_item, parent,
109                                                      l, line );
110         else
111             p_control = new FloatConfigControl( p_this, p_item, parent,
112                                                   l, line );
113         break;
114     default:
115         break;
116     }
117     return p_control;
118 }
119
120 void ConfigControl::doApply( intf_thread_t *p_intf )
121 {
122     switch( getType() )
123     {
124         case 1:
125         {
126             VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(this);
127             config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
128             break;
129         }
130         case 2:
131         {
132             VFloatConfigControl *vfcc = 
133                                     qobject_cast<VFloatConfigControl *>(this);
134             config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
135             break;
136         }
137         case 3:
138         {
139             VStringConfigControl *vscc =
140                             qobject_cast<VStringConfigControl *>(this);
141             config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
142         }
143     }
144 }
145
146 /**************************************************************************
147  * String-based controls
148  *************************************************************************/
149
150 /*********** String **************/
151 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
152                                           module_config_t *_p_item,
153                                           QWidget *_parent, QGridLayout *l,
154                                           int &line, bool pwd ) :
155                            VStringConfigControl( _p_this, _p_item, _parent )
156 {
157     label = new QLabel( qfu(p_item->psz_text) );
158     text = new QLineEdit( qfu(p_item->psz_value) );
159     finish();
160
161     if( !l )
162     {
163         QHBoxLayout *layout = new QHBoxLayout();
164         layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
165         widget->setLayout( layout );
166     }
167     else
168     {
169         l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
170     }
171 }
172
173 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
174                                    module_config_t *_p_item,
175                                    QLabel *_label, QLineEdit *_text, bool pwd ):
176                            VStringConfigControl( _p_this, _p_item )
177 {
178     text = _text;
179     label = _label;
180     finish( );
181 }
182
183 void StringConfigControl::finish()
184 {
185     text->setText( qfu(p_item->psz_value) );
186     text->setToolTip( qfu(p_item->psz_longtext) );
187     if( label )
188         label->setToolTip( qfu(p_item->psz_longtext) );
189 }
190
191 /********* String / choice list **********/
192 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
193                module_config_t *_p_item, QWidget *_parent, bool bycat,
194                QGridLayout *l, int &line) :
195                VStringConfigControl( _p_this, _p_item, _parent )
196 {
197     label = new QLabel( qfu(p_item->psz_text) );
198     combo = new QComboBox();
199     finish( bycat );
200     if( !l )
201     {
202         QHBoxLayout *layout = new QHBoxLayout();
203         layout->addWidget( label ); layout->addWidget( combo );
204         widget->setLayout( layout );
205     }
206     else
207     {
208         l->addWidget( label, line, 0 );
209         l->addWidget( combo, line, 1, Qt::AlignRight );
210     }
211 }
212 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
213                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
214                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
215 {
216     combo = _combo;
217     label = _label;
218     finish( bycat );
219 }
220
221 void StringListConfigControl::finish( bool bycat )
222 {
223     combo->setEditable( false );
224
225     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
226     {
227         combo->addItem( qfu(p_item->ppsz_list_text ?
228                             p_item->ppsz_list_text[i_index] :
229                             p_item->ppsz_list[i_index] ),
230                         QVariant( p_item->ppsz_list[i_index] ) );
231         if( p_item->psz_value && !strcmp( p_item->psz_value,
232                                           p_item->ppsz_list[i_index] ) )
233             combo->setCurrentIndex( combo->count() - 1 );
234     }
235     combo->setToolTip( qfu(p_item->psz_longtext) );
236     if( label )
237         label->setToolTip( qfu(p_item->psz_longtext) );
238 }
239
240 QString StringListConfigControl::getValue()
241 {
242     return combo->itemData( combo->currentIndex() ).toString();
243 }
244
245 /********* Module **********/
246 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
247                module_config_t *_p_item, QWidget *_parent, bool bycat,
248                QGridLayout *l, int &line) :
249                VStringConfigControl( _p_this, _p_item, _parent )
250 {
251     label = new QLabel( qfu(p_item->psz_text) );
252     combo = new QComboBox();
253     finish( bycat );
254     if( !l )
255     {
256         QHBoxLayout *layout = new QHBoxLayout();
257         layout->addWidget( label ); layout->addWidget( combo );
258         widget->setLayout( layout );
259     }
260     else
261     {
262         l->addWidget( label, line, 0 );
263         l->addWidget( combo, line, 1, Qt::AlignRight );
264     }
265 }
266 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
267                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
268                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
269 {
270     combo = _combo;
271     label = _label;
272     finish( bycat );
273 }
274
275 void ModuleConfigControl::finish( bool bycat )
276 {
277     vlc_list_t *p_list;
278     module_t *p_parser;
279
280     combo->setEditable( false );
281
282     /* build a list of available modules */
283     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
284     combo->addItem( qtr("Default") );
285     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
286     {
287         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
288
289         if( bycat )
290         {
291             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
292
293             module_config_t *p_config = p_parser->p_config;
294             if( p_config ) do
295             {
296                 /* Hack: required subcategory is stored in i_min */
297                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
298                     p_config->i_value == p_item->i_min )
299                     combo->addItem( qfu(p_parser->psz_longname),
300                                     QVariant( p_parser->psz_object_name ) );
301                 if( p_item->psz_value && !strcmp( p_item->psz_value,
302                                                   p_parser->psz_object_name) )
303                     combo->setCurrentIndex( combo->count() - 1 );
304             } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
305         }
306         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
307         {
308             combo->addItem( qfu(p_parser->psz_longname),
309                             QVariant( p_parser->psz_object_name ) );
310             if( p_item->psz_value && !strcmp( p_item->psz_value,
311                                               p_parser->psz_object_name) )
312                 combo->setCurrentIndex( combo->count() - 1 );
313         }
314     }
315     vlc_list_release( p_list );
316     combo->setToolTip( qfu(p_item->psz_longtext) );
317     if( label )
318         label->setToolTip( qfu(p_item->psz_longtext) );
319 }
320
321 QString ModuleConfigControl::getValue()
322 {
323     return combo->itemData( combo->currentIndex() ).toString();
324 }
325
326 /********* Module list **********/
327 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
328                module_config_t *_p_item, QWidget *_parent, bool bycat,
329                QGridLayout *l, int &line) :
330                VStringConfigControl( _p_this, _p_item, _parent )
331 {
332     label = new QLabel( qfu(p_item->psz_text) );
333     text = new QLineEdit();
334     finish( bycat );
335
336     bool pom = false;
337     if( !l )
338     {
339         l = new QGridLayout();
340         line = 0;
341         pom = true;
342     }
343     for( QVector<QCheckBox*>::iterator it = modules.begin();
344          it != modules.end(); it++ )
345     {
346         l->addWidget( *it, line++, 1 );
347     }
348     l->addWidget( label, line, 0 );
349     l->addWidget( text, line, 1 );
350     if( pom )
351         widget->setLayout( l );
352 }
353 #if 0
354 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
355                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
356                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
357 {
358     combo = _combo;
359     label = _label;
360     finish( bycat );
361 }
362 #endif
363
364 ModuleListConfigControl::~ModuleListConfigControl()
365 {
366     for( QVector<QCheckBox*>::iterator it = modules.begin();
367          it != modules.end(); it++ )
368     {
369         delete *it;
370     }
371     delete label;
372     delete text;
373 }
374
375 void ModuleListConfigControl::finish( bool bycat )
376 {
377     vlc_list_t *p_list;
378     module_t *p_parser;
379
380     /* build a list of available modules */
381     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
382     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
383     {
384         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
385
386         if( bycat )
387         {
388             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
389
390             module_config_t *p_config = p_parser->p_config;
391             if( p_config ) do
392             {
393                 /* Hack: required subcategory is stored in i_min */
394                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
395                     p_config->i_value == p_item->i_min )
396                 {
397                     QCheckBox *cb =
398                         new QCheckBox( qfu( p_parser->psz_object_name ) );
399                     cb->setToolTip( qfu(p_parser->psz_longname) );
400                     modules.push_back( cb );
401                 }
402             } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
403         }
404         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
405         {
406             QCheckBox *cb =
407                 new QCheckBox( qfu( p_parser->psz_object_name ) );
408             cb->setToolTip( qfu(p_parser->psz_longname) );
409             modules.push_back( cb );
410         }
411     }
412     vlc_list_release( p_list );
413     text->setToolTip( qfu(p_item->psz_longtext) );
414     if( label )
415         label->setToolTip( qfu(p_item->psz_longtext) );
416 }
417
418 QString ModuleListConfigControl::getValue()
419 {
420     return text->text();
421 }
422
423 void ModuleListConfigControl::hide()
424 {
425     for( QVector<QCheckBox*>::iterator it = modules.begin();
426          it != modules.end(); it++ )
427     {
428         (*it)->hide();
429     }
430     text->hide();
431     label->hide();
432 }
433
434 void ModuleListConfigControl::show()
435 {
436     for( QVector<QCheckBox*>::iterator it = modules.begin();
437          it != modules.end(); it++ )
438     {
439         (*it)->show();
440     }
441     text->show();
442     label->show();
443 }
444
445
446 void ModuleListConfigControl::wakeUp_TheUserJustClickedOnSomething( int value )
447 {
448     text->clear();
449     for( QVector<QCheckBox*>::iterator it = modules.begin();
450          it != modules.end(); it++ )
451     {
452     }
453 }
454
455 /**************************************************************************
456  * Integer-based controls
457  *************************************************************************/
458
459 /*********** Integer **************/
460 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
461                                             module_config_t *_p_item,
462                                             QWidget *_parent, QGridLayout *l,
463                                             int &line ) :
464                            VIntConfigControl( _p_this, _p_item, _parent )
465 {
466     label = new QLabel( qfu(p_item->psz_text) );
467     spin = new QSpinBox; spin->setMinimumWidth( 80 );
468     spin->setMaximumWidth( 90 );
469     finish();
470
471     if( !l )
472     {
473         QHBoxLayout *layout = new QHBoxLayout();
474         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
475         widget->setLayout( layout );
476     }
477     else
478     {
479         l->addWidget( label, line, 0 );
480         l->addWidget( spin, line, 1, Qt::AlignRight );
481     }
482 }
483 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
484                                             module_config_t *_p_item,
485                                             QLabel *_label, QSpinBox *_spin ) :
486                                       VIntConfigControl( _p_this, _p_item )
487 {
488     spin = _spin;
489     label = _label;
490     finish();
491 }
492
493 void IntegerConfigControl::finish()
494 {
495     spin->setMaximum( 2000000000 );
496     spin->setMinimum( -2000000000 );
497     spin->setValue( p_item->i_value );
498     spin->setToolTip( qfu(p_item->psz_longtext) );
499     if( label )
500         label->setToolTip( qfu(p_item->psz_longtext) );
501 }
502
503 int IntegerConfigControl::getValue()
504 {
505     return spin->value();
506 }
507
508 /********* Integer range **********/
509 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
510                                             module_config_t *_p_item,
511                                             QWidget *_parent, QGridLayout *l,
512                                             int &line ) :
513             IntegerConfigControl( _p_this, _p_item, _parent, l, line )
514 {
515     finish();
516 }
517
518 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
519                                             module_config_t *_p_item,
520                                             QLabel *_label, QSpinBox *_spin ) :
521             IntegerConfigControl( _p_this, _p_item, _label, _spin )
522 {
523     finish();
524 }
525
526 void IntegerRangeConfigControl::finish()
527 {
528     spin->setMaximum( p_item->i_max );
529     spin->setMinimum( p_item->i_min );
530 }
531
532 /********* Integer / choice list **********/
533 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
534                module_config_t *_p_item, QWidget *_parent, bool bycat,
535                QGridLayout *l, int &line) :
536                VIntConfigControl( _p_this, _p_item, _parent )
537 {
538     label = new QLabel( qfu(p_item->psz_text) );
539     combo = new QComboBox();
540     finish( bycat );
541     if( !l )
542     {
543         QHBoxLayout *layout = new QHBoxLayout();
544         layout->addWidget( label ); layout->addWidget( combo );
545         widget->setLayout( layout );
546     }
547     else
548     {
549         l->addWidget( label, line, 0 );
550         l->addWidget( combo, line, 1, Qt::AlignRight );
551     }
552 }
553 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
554                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
555                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
556 {
557     combo = _combo;
558     label = _label;
559     finish( bycat );
560 }
561
562 void IntegerListConfigControl::finish( bool bycat )
563 {
564     combo->setEditable( false );
565
566     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
567     {
568         combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
569                         QVariant( p_item->pi_list[i_index] ) );
570         if( p_item->i_value == p_item->pi_list[i_index] )
571             combo->setCurrentIndex( combo->count() - 1 );
572     }
573     combo->setToolTip( qfu(p_item->psz_longtext) );
574     if( label )
575         label->setToolTip( qfu(p_item->psz_longtext) );
576 }
577
578 int IntegerListConfigControl::getValue()
579 {
580     return combo->itemData( combo->currentIndex() ).toInt();
581 }
582
583 /*********** Boolean **************/
584 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
585                                       module_config_t *_p_item,
586                                       QWidget *_parent, QGridLayout *l,
587                                       int &line ) :
588                     VIntConfigControl( _p_this, _p_item, _parent )
589 {
590     checkbox = new QCheckBox( qfu(p_item->psz_text) );
591     finish();
592
593     if( !l )
594     {
595         QHBoxLayout *layout = new QHBoxLayout();
596         layout->addWidget( checkbox, 0 );
597         widget->setLayout( layout );
598     }
599     else
600     {
601         l->addWidget( checkbox, line, 0 );
602     }
603 }
604 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
605                                       module_config_t *_p_item,
606                                       QLabel *_label,
607                                       QCheckBox *_checkbox,
608                                       bool bycat ) :
609                    VIntConfigControl( _p_this, _p_item )
610 {
611     checkbox = _checkbox;
612     finish();
613 }
614
615 void BoolConfigControl::finish()
616 {
617     checkbox->setCheckState( p_item->i_value == VLC_TRUE ? Qt::Checked
618                                                         : Qt::Unchecked );
619     checkbox->setToolTip( qfu(p_item->psz_longtext) );
620 }
621
622 int BoolConfigControl::getValue()
623 {
624     return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
625 }
626
627 /**************************************************************************
628  * Float-based controls
629  *************************************************************************/
630
631 /*********** Float **************/
632 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
633                                         module_config_t *_p_item,
634                                         QWidget *_parent, QGridLayout *l,
635                                         int &line ) :
636                     VFloatConfigControl( _p_this, _p_item, _parent )
637 {
638     label = new QLabel( qfu(p_item->psz_text) );
639     spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
640     spin->setMaximumWidth( 90 );
641     finish();
642
643     if( !l )
644     {
645         QHBoxLayout *layout = new QHBoxLayout();
646         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
647         widget->setLayout( layout );
648     }
649     else
650     {
651         l->addWidget( label, line, 0 );
652         l->addWidget( spin, line, 1, Qt::AlignRight );
653     }
654 }
655
656 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
657                                         module_config_t *_p_item,
658                                         QLabel *_label,
659                                         QDoubleSpinBox *_spin ) :
660                     VFloatConfigControl( _p_this, _p_item )
661 {
662     spin = _spin;
663     label = _label;
664     finish();
665 }
666
667 void FloatConfigControl::finish()
668 {
669     spin->setMaximum( 2000000000. );
670     spin->setMinimum( -2000000000. );
671     spin->setSingleStep( 0.1 );
672     spin->setValue( (double)p_item->f_value );
673     spin->setToolTip( qfu(p_item->psz_longtext) );
674     if( label )
675         label->setToolTip( qfu(p_item->psz_longtext) );
676 }
677
678 float FloatConfigControl::getValue()
679 {
680     return (float)spin->value();
681 }
682
683 /*********** Float with range **************/
684 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
685                                         module_config_t *_p_item,
686                                         QWidget *_parent, QGridLayout *l,
687                                         int &line ) :
688                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
689 {
690     finish();
691 }
692
693 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
694                                         module_config_t *_p_item,
695                                         QLabel *_label,
696                                         QDoubleSpinBox *_spin ) :
697                 FloatConfigControl( _p_this, _p_item, _label, _spin )
698 {
699     finish();
700 }
701
702 void FloatRangeConfigControl::finish()
703 {
704     spin->setMaximum( (double)p_item->f_max );
705     spin->setMinimum( (double)p_item->f_min );
706 }