]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Qt4 - Preferences. Add CONFIG_DIRECTORY_ITEM. Does not work, but does not harm. Why...
[vlc] / modules / gui / qt4 / components / preferences_widgets.cpp
1 /*****************************************************************************
2  * preferences_widgets.cpp : Widgets for preferences displays
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Antoine Cellerier <dionoea@videolan.org>
9  *          Jean-Baptiste Kempf <jb@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /**
27  * Todo:
28  *  - Finish implementation (see WX)
29  *  - Improvements over WX
30  *      - Password field implementation (through "pwd" bool param
31  *      - Validator for modulelist
32  *  - Implement update stuff using a general Updated signal
33  */
34
35 #include "components/preferences_widgets.hpp"
36 #include "util/customwidgets.hpp"
37 #include "qt4.hpp"
38
39 #include <QLineEdit>
40 #include <QString>
41 #include <QSpinBox>
42 #include <QDoubleSpinBox>
43 #include <QVariant>
44 #include <QComboBox>
45 #include <QGridLayout>
46 #include <QPushButton>
47 #include <QSlider>
48 #include <QFileDialog>
49
50 #include <vlc_keys.h>
51
52 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
53                                              module_config_t *p_item,
54                                              QWidget *parent )
55 {
56     int i=0;
57     return createControl( p_this, p_item, parent, NULL, i );
58 }
59
60 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
61                                              module_config_t *p_item,
62                                              QWidget *parent,
63                                              QGridLayout *l, int &line )
64 {
65     ConfigControl *p_control = NULL;
66     if( p_item->psz_current ) return NULL;
67
68     switch( p_item->i_type )
69     {
70     case CONFIG_ITEM_MODULE:
71         p_control = new ModuleConfigControl( p_this, p_item, parent, false,
72                                              l, line );
73         break;
74     case CONFIG_ITEM_MODULE_CAT:
75         p_control = new ModuleConfigControl( p_this, p_item, parent, true,
76                                              l, line );
77         break;
78     case CONFIG_ITEM_MODULE_LIST:
79         p_control = new ModuleListConfigControl( p_this, p_item, parent, false,
80                                              l, line );
81         break;
82     case CONFIG_ITEM_MODULE_LIST_CAT:
83         p_control = new ModuleListConfigControl( p_this, p_item, parent, true,
84                                              l, line );
85         break;
86     case CONFIG_ITEM_STRING:
87         if( !p_item->i_list )
88             p_control = new StringConfigControl( p_this, p_item, parent,
89                                                  l, line, false );
90         else
91             p_control = new StringListConfigControl( p_this, p_item,
92                                             parent, false, l, line );
93         break;
94     case CONFIG_ITEM_INTEGER:
95         if( p_item->i_list )
96             p_control = new IntegerListConfigControl( p_this, p_item,
97                                             parent, false, l, line );
98         else if( p_item->min.i || p_item->max.i )
99             p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
100                                                        l, line );
101         else
102             p_control = new IntegerConfigControl( p_this, p_item, parent,
103                                                   l, line );
104         break;
105     case CONFIG_ITEM_FILE:
106         p_control = new FileConfigControl( p_this, p_item, parent, l,
107                                                 line, false );
108         break;
109     case CONFIG_ITEM_DIRECTORY:
110         p_control = new DirectoryConfigControl( p_this, p_item, parent, l,
111                                                 line, false );
112         break;
113     case CONFIG_ITEM_KEY:
114         p_control = new KeySelectorControl( p_this, p_item, parent, l, line );
115         break;
116     case CONFIG_ITEM_BOOL:
117         p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
118         break;
119     case CONFIG_ITEM_FLOAT:
120         if( p_item->min.f || p_item->max.f )
121             p_control = new FloatRangeConfigControl( p_this, p_item, parent,
122                                                      l, line );
123         else
124             p_control = new FloatConfigControl( p_this, p_item, parent,
125                                                   l, line );
126         break;
127     default:
128         break;
129     }
130     return p_control;
131 }
132
133 void ConfigControl::doApply( intf_thread_t *p_intf )
134 {
135     switch( getType() )
136     {
137         case 1:
138         {
139             VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(this);
140             assert( vicc );
141             config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
142             break;
143         }
144         case 2:
145         {
146             VFloatConfigControl *vfcc =
147                                     qobject_cast<VFloatConfigControl *>(this);
148             assert( vfcc );
149             config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
150             break;
151         }
152         case 3:
153         {
154             VStringConfigControl *vscc =
155                             qobject_cast<VStringConfigControl *>(this);
156             assert( vscc );
157             config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
158             break;
159         }
160         case 4:
161         {
162             KeySelectorControl *ksc = qobject_cast<KeySelectorControl *>(this);
163             assert( ksc );
164             ksc->doApply();
165         }
166     }
167 }
168
169 /**************************************************************************
170  * String-based controls
171  *************************************************************************/
172
173 /*********** String **************/
174 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
175                                           module_config_t *_p_item,
176                                           QWidget *_parent, QGridLayout *l,
177                                           int &line, bool pwd ) :
178                            VStringConfigControl( _p_this, _p_item, _parent )
179 {
180     label = new QLabel( qfu(p_item->psz_text) );
181     text = new QLineEdit( qfu(p_item->value.psz) );
182     finish();
183
184     if( !l )
185     {
186         QHBoxLayout *layout = new QHBoxLayout();
187         layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
188         widget->setLayout( layout );
189     }
190     else
191     {
192         l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
193     }
194 }
195
196 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
197                                    module_config_t *_p_item,
198                                    QLabel *_label, QLineEdit *_text, bool pwd ):
199                            VStringConfigControl( _p_this, _p_item )
200 {
201     text = _text;
202     label = _label;
203     finish( );
204 }
205
206 void StringConfigControl::finish()
207 {
208     text->setText( qfu(p_item->value.psz) );
209     text->setToolTip( qfu(p_item->psz_longtext) );
210     if( label )
211         label->setToolTip( qfu(p_item->psz_longtext) );
212 }
213
214 /*********** File **************/
215 FileConfigControl::FileConfigControl( vlc_object_t *_p_this,
216                                           module_config_t *_p_item,
217                                           QWidget *_parent, QGridLayout *l,
218                                           int &line, bool pwd ) :
219                            VStringConfigControl( _p_this, _p_item, _parent )
220 {
221     label = new QLabel( qfu(p_item->psz_text) );
222     text = new QLineEdit( qfu(p_item->value.psz) );
223     browse = new QPushButton( qtr( "Browse" ) );
224
225     BUTTONACT( browse, updateField() );
226
227     finish();
228
229     if( !l )
230     {
231         QHBoxLayout *layout = new QHBoxLayout();
232         layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
233         layout->addWidget( browse, 2 );
234         widget->setLayout( layout );
235     }
236     else
237     {
238         l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
239         l->addWidget( browse, line, 2 );
240     }
241 }
242
243
244 FileConfigControl::FileConfigControl( vlc_object_t *_p_this,
245                                    module_config_t *_p_item,
246                                    QLabel *_label, QLineEdit *_text, bool pwd ):
247                            VStringConfigControl( _p_this, _p_item )
248 {
249     text = _text;
250     label = _label;
251     finish( );
252 }
253
254 void FileConfigControl::updateField()
255 {
256         text->setText( QFileDialog::getOpenFileName( NULL, 
257                 qtr( "Select File" ), qfu( p_this->p_libvlc->psz_homedir ) ) );
258 }
259
260 void FileConfigControl::finish()
261 {
262     text->setText( qfu(p_item->value.psz) );
263     text->setToolTip( qfu(p_item->psz_longtext) );
264     if( label )
265         label->setToolTip( qfu(p_item->psz_longtext) );
266 }
267
268 /********* String / Directory **********/
269
270 DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
271                         module_config_t *_p_item, QWidget *_p_widget,
272                         QGridLayout *_p_layout, int& _int, bool _pwd ) :
273      FileConfigControl( _p_this, _p_item, _p_widget, _p_layout, _int, _pwd)
274 {}
275
276 DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
277                         module_config_t *_p_item, QLabel *_p_label,
278                         QLineEdit *_p_line, bool _pwd ) :
279      FileConfigControl( _p_this, _p_item, _p_label, _p_line, _pwd)
280 {}
281
282
283 void DirectoryConfigControl::updateField()
284 {
285     text->setText( QFileDialog::getOpenFileName( NULL,
286                 qtr( "Select File" ), qfu( p_this->p_libvlc->psz_homedir ),
287                 NULL, 0,  QFileDialog::ShowDirsOnly ) );
288 }
289
290 /********* String / choice list **********/
291 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
292                module_config_t *_p_item, QWidget *_parent, bool bycat,
293                QGridLayout *l, int &line) :
294                VStringConfigControl( _p_this, _p_item, _parent )
295 {
296     label = new QLabel( qfu(p_item->psz_text) );
297     combo = new QComboBox();
298     finish( bycat );
299     if( !l )
300     {
301         QHBoxLayout *layout = new QHBoxLayout();
302         layout->addWidget( label ); layout->addWidget( combo );
303         widget->setLayout( layout );
304     }
305     else
306     {
307         l->addWidget( label, line, 0 );
308         l->addWidget( combo, line, 1, Qt::AlignRight );
309     }
310 }
311 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
312                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
313                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
314 {
315     combo = _combo;
316     label = _label;
317     finish( bycat );
318 }
319
320 void StringListConfigControl::finish( bool bycat )
321 {
322     combo->setEditable( false );
323
324     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
325     {
326         combo->addItem( qfu(p_item->ppsz_list_text ?
327                             p_item->ppsz_list_text[i_index] :
328                             p_item->ppsz_list[i_index] ),
329                         QVariant( p_item->ppsz_list[i_index] ) );
330         if( p_item->value.psz && !strcmp( p_item->value.psz,
331                                           p_item->ppsz_list[i_index] ) )
332             combo->setCurrentIndex( combo->count() - 1 );
333     }
334     combo->setToolTip( qfu(p_item->psz_longtext) );
335     if( label )
336         label->setToolTip( qfu(p_item->psz_longtext) );
337 }
338
339 QString StringListConfigControl::getValue()
340 {
341     return combo->itemData( combo->currentIndex() ).toString();
342 }
343
344 /********* Module **********/
345 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
346                module_config_t *_p_item, QWidget *_parent, bool bycat,
347                QGridLayout *l, int &line) :
348                VStringConfigControl( _p_this, _p_item, _parent )
349 {
350     label = new QLabel( qfu(p_item->psz_text) );
351     combo = new QComboBox();
352     finish( bycat );
353     if( !l )
354     {
355         QHBoxLayout *layout = new QHBoxLayout();
356         layout->addWidget( label ); layout->addWidget( combo );
357         widget->setLayout( layout );
358     }
359     else
360     {
361         l->addWidget( label, line, 0 );
362         l->addWidget( combo, line, 1, Qt::AlignRight );
363     }
364 }
365 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
366                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
367                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
368 {
369     combo = _combo;
370     label = _label;
371     finish( bycat );
372 }
373
374 void ModuleConfigControl::finish( bool bycat )
375 {
376     vlc_list_t *p_list;
377     module_t *p_parser;
378
379     combo->setEditable( false );
380
381     /* build a list of available modules */
382     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
383     combo->addItem( qtr("Default") );
384     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
385     {
386         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
387
388         if( bycat )
389         {
390             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
391
392             for (size_t i = 0; i < p_parser->confsize; i++)
393             {
394                 module_config_t *p_config = p_parser->p_config + i;
395                 /* Hack: required subcategory is stored in i_min */
396                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
397                     p_config->value.i == p_item->min.i )
398                     combo->addItem( qfu(p_parser->psz_longname),
399                                     QVariant( p_parser->psz_object_name ) );
400                 if( p_item->value.psz && !strcmp( p_item->value.psz,
401                                                   p_parser->psz_object_name) )
402                     combo->setCurrentIndex( combo->count() - 1 );
403             }
404         }
405         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
406         {
407             combo->addItem( qfu(p_parser->psz_longname),
408                             QVariant( p_parser->psz_object_name ) );
409             if( p_item->value.psz && !strcmp( p_item->value.psz,
410                                               p_parser->psz_object_name) )
411                 combo->setCurrentIndex( combo->count() - 1 );
412         }
413     }
414     vlc_list_release( p_list );
415     combo->setToolTip( qfu(p_item->psz_longtext) );
416     if( label )
417         label->setToolTip( qfu(p_item->psz_longtext) );
418 }
419
420 QString ModuleConfigControl::getValue()
421 {
422     return combo->itemData( combo->currentIndex() ).toString();
423 }
424
425 /********* Module list **********/
426 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
427                module_config_t *_p_item, QWidget *_parent, bool bycat,
428                QGridLayout *l, int &line) :
429                VStringConfigControl( _p_this, _p_item, _parent )
430 {
431     label = new QLabel( qfu(p_item->psz_text) );
432     text = new QLineEdit();
433     finish( bycat );
434
435     bool pom = false;
436     if( !l )
437     {
438         l = new QGridLayout();
439         line = 0;
440         pom = true;
441     }
442     for( QVector<QCheckBox*>::iterator it = modules.begin();
443          it != modules.end(); it++ )
444     {
445         l->addWidget( *it, line++, 1 );
446     }
447     l->addWidget( label, line, 0 );
448     l->addWidget( text, line, 1 );
449     if( pom )
450         widget->setLayout( l );
451 }
452 #if 0
453 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
454                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
455                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
456 {
457     combo = _combo;
458     label = _label;
459     finish( bycat );
460 }
461 #endif
462
463 ModuleListConfigControl::~ModuleListConfigControl()
464 {
465     for( QVector<QCheckBox*>::iterator it = modules.begin();
466          it != modules.end(); it++ )
467     {
468         delete *it;
469     }
470     delete label;
471     delete text;
472 }
473
474 void ModuleListConfigControl::finish( bool bycat )
475 {
476     vlc_list_t *p_list;
477     module_t *p_parser;
478
479     /* build a list of available modules */
480     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
481     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
482     {
483         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
484
485         if( bycat )
486         {
487             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
488
489             for (size_t i = 0; i < p_parser->confsize; i++)
490             {
491                 module_config_t *p_config = p_parser->p_config + i;
492                 /* Hack: required subcategory is stored in i_min */
493                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
494                     p_config->value.i == p_item->min.i )
495                 {
496                     QCheckBox *cb =
497                         new QCheckBox( qfu( p_parser->psz_object_name ) );
498                     cb->setToolTip( qfu(p_parser->psz_longname) );
499                     modules.push_back( cb );
500                 }
501             }
502         }
503         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
504         {
505             QCheckBox *cb =
506                 new QCheckBox( qfu( p_parser->psz_object_name ) );
507             cb->setToolTip( qfu(p_parser->psz_longname) );
508             modules.push_back( cb );
509         }
510     }
511     vlc_list_release( p_list );
512     text->setToolTip( qfu(p_item->psz_longtext) );
513     if( label )
514         label->setToolTip( qfu(p_item->psz_longtext) );
515 }
516
517 QString ModuleListConfigControl::getValue()
518 {
519     return text->text();
520 }
521
522 void ModuleListConfigControl::hide()
523 {
524     for( QVector<QCheckBox*>::iterator it = modules.begin();
525          it != modules.end(); it++ )
526     {
527         (*it)->hide();
528     }
529     text->hide();
530     label->hide();
531 }
532
533 void ModuleListConfigControl::show()
534 {
535     for( QVector<QCheckBox*>::iterator it = modules.begin();
536          it != modules.end(); it++ )
537     {
538         (*it)->show();
539     }
540     text->show();
541     label->show();
542 }
543
544
545 void ModuleListConfigControl::wakeUp_TheUserJustClickedOnSomething( int value )
546 {
547     text->clear();
548     for( QVector<QCheckBox*>::iterator it = modules.begin();
549          it != modules.end(); it++ )
550     {
551     }
552 }
553
554 /**************************************************************************
555  * Integer-based controls
556  *************************************************************************/
557
558 /*********** Integer **************/
559 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
560                                             module_config_t *_p_item,
561                                             QWidget *_parent, QGridLayout *l,
562                                             int &line ) :
563                            VIntConfigControl( _p_this, _p_item, _parent )
564 {
565     label = new QLabel( qfu(p_item->psz_text) );
566     spin = new QSpinBox; spin->setMinimumWidth( 80 );
567     spin->setMaximumWidth( 90 );
568     finish();
569
570     if( !l )
571     {
572         QHBoxLayout *layout = new QHBoxLayout();
573         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
574         widget->setLayout( layout );
575     }
576     else
577     {
578         l->addWidget( label, line, 0 );
579         l->addWidget( spin, line, 1, Qt::AlignRight );
580     }
581 }
582 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
583                                             module_config_t *_p_item,
584                                             QLabel *_label, QSpinBox *_spin ) :
585                                       VIntConfigControl( _p_this, _p_item )
586 {
587     spin = _spin;
588     label = _label;
589     finish();
590 }
591
592 void IntegerConfigControl::finish()
593 {
594     spin->setMaximum( 2000000000 );
595     spin->setMinimum( -2000000000 );
596     spin->setValue( p_item->value.i );
597     spin->setToolTip( qfu(p_item->psz_longtext) );
598     if( label )
599         label->setToolTip( qfu(p_item->psz_longtext) );
600 }
601
602 int IntegerConfigControl::getValue()
603 {
604     return spin->value();
605 }
606
607 /********* Integer range **********/
608 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
609                                             module_config_t *_p_item,
610                                             QWidget *_parent, QGridLayout *l,
611                                             int &line ) :
612             IntegerConfigControl( _p_this, _p_item, _parent, l, line )
613 {
614     finish();
615 }
616
617 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
618                                             module_config_t *_p_item,
619                                             QLabel *_label, QSpinBox *_spin ) :
620             IntegerConfigControl( _p_this, _p_item, _label, _spin )
621 {
622     finish();
623 }
624
625 void IntegerRangeConfigControl::finish()
626 {
627     spin->setMaximum( p_item->max.i );
628     spin->setMinimum( p_item->min.i );
629 }
630
631 IntegerRangeSliderConfigControl::IntegerRangeSliderConfigControl(
632                                             vlc_object_t *_p_this,
633                                             module_config_t *_p_item,
634                                             QLabel *_label, QSlider *_slider ):
635                     VIntConfigControl( _p_this, _p_item )
636 {
637     slider = _slider;
638     label = _label;
639     slider->setMaximum( p_item->max.i );
640     slider->setMinimum( p_item->min.i );
641     slider->setValue( p_item->value.i );
642     slider->setToolTip( qfu(p_item->psz_longtext) );
643     if( label )
644         label->setToolTip( qfu(p_item->psz_longtext) );
645 }
646
647 int IntegerRangeSliderConfigControl::getValue()
648 {
649         return slider->value();
650 }
651
652
653 /********* Integer / choice list **********/
654 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
655                module_config_t *_p_item, QWidget *_parent, bool bycat,
656                QGridLayout *l, int &line) :
657                VIntConfigControl( _p_this, _p_item, _parent )
658 {
659     label = new QLabel( qfu(p_item->psz_text) );
660     combo = new QComboBox();
661     finish( bycat );
662     if( !l )
663     {
664         QHBoxLayout *layout = new QHBoxLayout();
665         layout->addWidget( label ); layout->addWidget( combo );
666         widget->setLayout( layout );
667     }
668     else
669     {
670         l->addWidget( label, line, 0 );
671         l->addWidget( combo, line, 1, Qt::AlignRight );
672     }
673 }
674 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
675                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
676                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
677 {
678     combo = _combo;
679     label = _label;
680     finish( bycat );
681 }
682
683 void IntegerListConfigControl::finish( bool bycat )
684 {
685     combo->setEditable( false );
686
687     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
688     {
689         combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
690                         QVariant( p_item->pi_list[i_index] ) );
691         if( p_item->value.i == p_item->pi_list[i_index] )
692             combo->setCurrentIndex( combo->count() - 1 );
693     }
694     combo->setToolTip( qfu(p_item->psz_longtext) );
695     if( label )
696         label->setToolTip( qfu(p_item->psz_longtext) );
697 }
698
699 int IntegerListConfigControl::getValue()
700 {
701     return combo->itemData( combo->currentIndex() ).toInt();
702 }
703
704 /*********** Boolean **************/
705 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
706                                       module_config_t *_p_item,
707                                       QWidget *_parent, QGridLayout *l,
708                                       int &line ) :
709                     VIntConfigControl( _p_this, _p_item, _parent )
710 {
711     checkbox = new QCheckBox( qfu(p_item->psz_text) );
712     finish();
713
714     if( !l )
715     {
716         QHBoxLayout *layout = new QHBoxLayout();
717         layout->addWidget( checkbox, 0 );
718         widget->setLayout( layout );
719     }
720     else
721     {
722         l->addWidget( checkbox, line, 0 );
723     }
724 }
725 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
726                                       module_config_t *_p_item,
727                                       QLabel *_label,
728                                       QCheckBox *_checkbox,
729                                       bool bycat ) :
730                    VIntConfigControl( _p_this, _p_item )
731 {
732     checkbox = _checkbox;
733     finish();
734 }
735
736 void BoolConfigControl::finish()
737 {
738     checkbox->setCheckState( p_item->value.i == VLC_TRUE ? Qt::Checked
739                                                         : Qt::Unchecked );
740     checkbox->setToolTip( qfu(p_item->psz_longtext) );
741 }
742
743 int BoolConfigControl::getValue()
744 {
745     return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
746 }
747
748 /**************************************************************************
749  * Float-based controls
750  *************************************************************************/
751
752 /*********** Float **************/
753 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
754                                         module_config_t *_p_item,
755                                         QWidget *_parent, QGridLayout *l,
756                                         int &line ) :
757                     VFloatConfigControl( _p_this, _p_item, _parent )
758 {
759     label = new QLabel( qfu(p_item->psz_text) );
760     spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
761     spin->setMaximumWidth( 90 );
762     finish();
763
764     if( !l )
765     {
766         QHBoxLayout *layout = new QHBoxLayout();
767         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
768         widget->setLayout( layout );
769     }
770     else
771     {
772         l->addWidget( label, line, 0 );
773         l->addWidget( spin, line, 1, Qt::AlignRight );
774     }
775 }
776
777 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
778                                         module_config_t *_p_item,
779                                         QLabel *_label,
780                                         QDoubleSpinBox *_spin ) :
781                     VFloatConfigControl( _p_this, _p_item )
782 {
783     spin = _spin;
784     label = _label;
785     finish();
786 }
787
788 void FloatConfigControl::finish()
789 {
790     spin->setMaximum( 2000000000. );
791     spin->setMinimum( -2000000000. );
792     spin->setSingleStep( 0.1 );
793     spin->setValue( (double)p_item->value.f );
794     spin->setToolTip( qfu(p_item->psz_longtext) );
795     if( label )
796         label->setToolTip( qfu(p_item->psz_longtext) );
797 }
798
799 float FloatConfigControl::getValue()
800 {
801     return (float)spin->value();
802 }
803
804 /*********** Float with range **************/
805 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
806                                         module_config_t *_p_item,
807                                         QWidget *_parent, QGridLayout *l,
808                                         int &line ) :
809                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
810 {
811     finish();
812 }
813
814 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
815                                         module_config_t *_p_item,
816                                         QLabel *_label,
817                                         QDoubleSpinBox *_spin ) :
818                 FloatConfigControl( _p_this, _p_item, _label, _spin )
819 {
820     finish();
821 }
822
823 void FloatRangeConfigControl::finish()
824 {
825     spin->setMaximum( (double)p_item->max.f );
826     spin->setMinimum( (double)p_item->min.f );
827 }
828
829
830 /**********************************************************************
831  * Key selector widget
832  **********************************************************************/
833 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
834                                       module_config_t *_p_item,
835                                       QWidget *_parent, QGridLayout *l,
836                                       int &line ) :
837                                 ConfigControl( _p_this, _p_item, _parent )
838
839 {
840     label = new QLabel( qtr("Select an action to change the associated hotkey") );
841     table = new QTreeWidget( 0 );
842     finish();
843
844     if( !l )
845     {
846         QVBoxLayout *layout = new QVBoxLayout();
847         layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
848         widget->setLayout( layout );
849     }
850     else
851     {
852         l->addWidget( label, line, 0, 1, 2 );
853         l->addWidget( table, line+1, 0, 1,2 );
854     }
855 }
856
857 void KeySelectorControl::finish()
858 {
859     if( label )
860         label->setToolTip( qfu(p_item->psz_longtext) );
861
862     /* Fill the table */
863     table->setColumnCount( 2 );
864     table->setAlternatingRowColors( true );
865
866     module_t *p_main = config_FindModule( p_this, "main" );
867     assert( p_main );
868
869     for (size_t i = 0; i < p_main->confsize; i++)
870     {
871         module_config_t *p_item = p_main->p_config + i;
872
873         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
874             strstr( p_item->psz_name , "key-" ) )
875         {
876             QTreeWidgetItem *treeItem = new QTreeWidgetItem();
877             treeItem->setText( 0, qfu( p_item->psz_text ) );
878             treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
879             treeItem->setData( 0, Qt::UserRole,
880                                   QVariant::fromValue( (void*)p_item ) );
881             values += p_item;
882             table->addTopLevelItem( treeItem );
883         }
884     }
885     table->resizeColumnToContents( 0 );
886
887     CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
888              this, selectKey( QTreeWidgetItem * ) );
889 }
890
891 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
892 {
893    module_config_t *p_keyItem = static_cast<module_config_t*>
894                           (keyItem->data( 0, Qt::UserRole ).value<void*>());
895
896     KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text );
897     d->exec();
898     if( d->result() == QDialog::Accepted )
899     {
900         p_keyItem->value.i = d->keyValue;
901         if( d->conflicts )
902         {
903             for( int i = 0; i < table->topLevelItemCount() ; i++ )
904             {
905                 QTreeWidgetItem *it = table->topLevelItem(i);
906                 module_config_t *p_item = static_cast<module_config_t*>
907                               (it->data( 0, Qt::UserRole ).value<void*>());
908                 it->setText( 1, VLCKeyToString( p_item->value.i ) );
909             }
910         }
911         else
912             keyItem->setText( 1, VLCKeyToString( p_keyItem->value.i ) );
913     }
914     delete d;
915 }
916
917 void KeySelectorControl::doApply()
918 {
919     foreach( module_config_t *p_current, values )
920     {
921         config_PutInt( p_this, p_current->psz_name, p_current->value.i );
922     }
923 }
924
925 KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
926                                 const char * _keyToChange ) :
927                                                 QDialog(0), keyValue(0)
928 {
929     setModal( true );
930     values = _values;
931     conflicts = false;
932     keyToChange = _keyToChange;
933     setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange)  );
934
935     QVBoxLayout *l = new QVBoxLayout( this );
936     selected = new QLabel( qtr("Press the new keys for ")  + qfu(keyToChange) );
937     warning = new QLabel();
938     l->addWidget( selected , Qt::AlignCenter );
939     l->addWidget( warning, Qt::AlignCenter );
940
941     QHBoxLayout *l2 = new QHBoxLayout();
942     QPushButton *ok = new QPushButton( qtr("OK") );
943     l2->addWidget( ok );
944     QPushButton *cancel = new QPushButton( qtr("Cancel") );
945     l2->addWidget( cancel );
946
947     BUTTONACT( ok, accept() );
948     BUTTONACT( cancel, reject() );
949
950     l->addLayout( l2 );
951 }
952
953 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
954 {
955     if( e->key() == Qt::Key_Tab ) return;
956     int i_vlck = qtEventToVLCKey( e );
957     selected->setText( VLCKeyToString( i_vlck ) );
958     conflicts = false;
959     module_config_t *p_current = NULL;
960     foreach( p_current, values )
961     {
962         if( p_current->value.i == i_vlck && strcmp( p_current->psz_text,
963                                                     keyToChange ) )
964         {
965             p_current->value.i = 0;
966             conflicts = true;
967             break;
968         }
969     }
970     if( conflicts )
971     {
972         warning->setText(
973           qtr("Warning: the  key is already assigned to \"") +
974           QString( p_current->psz_text ) + "\"" );
975     }
976     else warning->setText( "" );
977     keyValue = i_vlck;
978 }