]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
* qt: really implement DirectoryConfigControl
[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,
247                                    QPushButton *_button, bool pwd ):
248                            VStringConfigControl( _p_this, _p_item )
249 {
250     browse = _button;
251     text = _text;
252     label = _label;
253
254     BUTTONACT( browse, updateField() );
255
256     finish( );
257 }
258
259 void FileConfigControl::updateField()
260 {
261     QString file = QFileDialog::getOpenFileName( NULL,
262                   qtr( "Select File" ), qfu( p_this->p_libvlc->psz_homedir ) );
263     if( file.isNull() ) return;
264     text->setText( file );
265 }
266
267 void FileConfigControl::finish()
268 {
269     text->setText( qfu(p_item->value.psz) );
270     text->setToolTip( qfu(p_item->psz_longtext) );
271     if( label )
272         label->setToolTip( qfu(p_item->psz_longtext) );
273 }
274
275 /********* String / Directory **********/
276
277 DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
278                         module_config_t *_p_item, QWidget *_p_widget,
279                         QGridLayout *_p_layout, int& _int, bool _pwd ) :
280      FileConfigControl( _p_this, _p_item, _p_widget, _p_layout, _int, _pwd)
281 {}
282
283 DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
284                         module_config_t *_p_item, QLabel *_p_label,
285                         QLineEdit *_p_line, QPushButton *_p_button, bool _pwd ):
286      FileConfigControl( _p_this, _p_item, _p_label, _p_line, _p_button, _pwd)
287 {}
288
289
290 void DirectoryConfigControl::updateField()
291 {
292     QString dir = QFileDialog::getExistingDirectory( NULL,
293                       qtr( "Select Directory" ),
294                       qfu( p_this->p_libvlc->psz_homedir ),
295                       QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks );
296     if( dir.isNull() ) return;
297     text->setText( dir );
298 }
299
300 /********* String / choice list **********/
301 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
302                module_config_t *_p_item, QWidget *_parent, bool bycat,
303                QGridLayout *l, int &line) :
304                VStringConfigControl( _p_this, _p_item, _parent )
305 {
306     label = new QLabel( qfu(p_item->psz_text) );
307     combo = new QComboBox();
308     finish( bycat );
309     if( !l )
310     {
311         QHBoxLayout *layout = new QHBoxLayout();
312         layout->addWidget( label ); layout->addWidget( combo );
313         widget->setLayout( layout );
314     }
315     else
316     {
317         l->addWidget( label, line, 0 );
318         l->addWidget( combo, line, 1, Qt::AlignRight );
319     }
320 }
321 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
322                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
323                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
324 {
325     combo = _combo;
326     label = _label;
327     finish( bycat );
328 }
329
330 void StringListConfigControl::finish( bool bycat )
331 {
332     combo->setEditable( false );
333
334     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
335     {
336         combo->addItem( qfu(p_item->ppsz_list_text ?
337                             p_item->ppsz_list_text[i_index] :
338                             p_item->ppsz_list[i_index] ),
339                         QVariant( p_item->ppsz_list[i_index] ) );
340         if( p_item->value.psz && !strcmp( p_item->value.psz,
341                                           p_item->ppsz_list[i_index] ) )
342             combo->setCurrentIndex( combo->count() - 1 );
343     }
344     combo->setToolTip( qfu(p_item->psz_longtext) );
345     if( label )
346         label->setToolTip( qfu(p_item->psz_longtext) );
347 }
348
349 QString StringListConfigControl::getValue()
350 {
351     return combo->itemData( combo->currentIndex() ).toString();
352 }
353
354 /********* Module **********/
355 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
356                module_config_t *_p_item, QWidget *_parent, bool bycat,
357                QGridLayout *l, int &line) :
358                VStringConfigControl( _p_this, _p_item, _parent )
359 {
360     label = new QLabel( qfu(p_item->psz_text) );
361     combo = new QComboBox();
362     finish( bycat );
363     if( !l )
364     {
365         QHBoxLayout *layout = new QHBoxLayout();
366         layout->addWidget( label ); layout->addWidget( combo );
367         widget->setLayout( layout );
368     }
369     else
370     {
371         l->addWidget( label, line, 0 );
372         l->addWidget( combo, line, 1, Qt::AlignRight );
373     }
374 }
375 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
376                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
377                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
378 {
379     combo = _combo;
380     label = _label;
381     finish( bycat );
382 }
383
384 void ModuleConfigControl::finish( bool bycat )
385 {
386     vlc_list_t *p_list;
387     module_t *p_parser;
388
389     combo->setEditable( false );
390
391     /* build a list of available modules */
392     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
393     combo->addItem( qtr("Default") );
394     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
395     {
396         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
397
398         if( bycat )
399         {
400             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
401
402             for (size_t i = 0; i < p_parser->confsize; i++)
403             {
404                 module_config_t *p_config = p_parser->p_config + i;
405                 /* Hack: required subcategory is stored in i_min */
406                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
407                     p_config->value.i == p_item->min.i )
408                     combo->addItem( qfu(p_parser->psz_longname),
409                                     QVariant( p_parser->psz_object_name ) );
410                 if( p_item->value.psz && !strcmp( p_item->value.psz,
411                                                   p_parser->psz_object_name) )
412                     combo->setCurrentIndex( combo->count() - 1 );
413             }
414         }
415         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
416         {
417             combo->addItem( qfu(p_parser->psz_longname),
418                             QVariant( p_parser->psz_object_name ) );
419             if( p_item->value.psz && !strcmp( p_item->value.psz,
420                                               p_parser->psz_object_name) )
421                 combo->setCurrentIndex( combo->count() - 1 );
422         }
423     }
424     vlc_list_release( p_list );
425     combo->setToolTip( qfu(p_item->psz_longtext) );
426     if( label )
427         label->setToolTip( qfu(p_item->psz_longtext) );
428 }
429
430 QString ModuleConfigControl::getValue()
431 {
432     return combo->itemData( combo->currentIndex() ).toString();
433 }
434
435 /********* Module list **********/
436 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
437                module_config_t *_p_item, QWidget *_parent, bool bycat,
438                QGridLayout *l, int &line) :
439                VStringConfigControl( _p_this, _p_item, _parent )
440 {
441     label = new QLabel( qfu(p_item->psz_text) );
442     text = new QLineEdit();
443     finish( bycat );
444
445     bool pom = false;
446     if( !l )
447     {
448         l = new QGridLayout();
449         line = 0;
450         pom = true;
451     }
452     for( QVector<QCheckBox*>::iterator it = modules.begin();
453          it != modules.end(); it++ )
454     {
455         l->addWidget( *it, line++, 1 );
456     }
457     l->addWidget( label, line, 0 );
458     l->addWidget( text, line, 1 );
459     if( pom )
460         widget->setLayout( l );
461 }
462 #if 0
463 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
464                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
465                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
466 {
467     combo = _combo;
468     label = _label;
469     finish( bycat );
470 }
471 #endif
472
473 ModuleListConfigControl::~ModuleListConfigControl()
474 {
475     for( QVector<QCheckBox*>::iterator it = modules.begin();
476          it != modules.end(); it++ )
477     {
478         delete *it;
479     }
480     delete label;
481     delete text;
482 }
483
484 void ModuleListConfigControl::finish( bool bycat )
485 {
486     vlc_list_t *p_list;
487     module_t *p_parser;
488
489     /* build a list of available modules */
490     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
491     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
492     {
493         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
494
495         if( bycat )
496         {
497             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
498
499             for (size_t i = 0; i < p_parser->confsize; i++)
500             {
501                 module_config_t *p_config = p_parser->p_config + i;
502                 /* Hack: required subcategory is stored in i_min */
503                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
504                     p_config->value.i == p_item->min.i )
505                 {
506                     QCheckBox *cb =
507                         new QCheckBox( qfu( p_parser->psz_object_name ) );
508                     cb->setToolTip( qfu(p_parser->psz_longname) );
509                     modules.push_back( cb );
510                 }
511             }
512         }
513         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
514         {
515             QCheckBox *cb =
516                 new QCheckBox( qfu( p_parser->psz_object_name ) );
517             cb->setToolTip( qfu(p_parser->psz_longname) );
518             modules.push_back( cb );
519         }
520     }
521     vlc_list_release( p_list );
522     text->setToolTip( qfu(p_item->psz_longtext) );
523     if( label )
524         label->setToolTip( qfu(p_item->psz_longtext) );
525 }
526
527 QString ModuleListConfigControl::getValue()
528 {
529     return text->text();
530 }
531
532 void ModuleListConfigControl::hide()
533 {
534     for( QVector<QCheckBox*>::iterator it = modules.begin();
535          it != modules.end(); it++ )
536     {
537         (*it)->hide();
538     }
539     text->hide();
540     label->hide();
541 }
542
543 void ModuleListConfigControl::show()
544 {
545     for( QVector<QCheckBox*>::iterator it = modules.begin();
546          it != modules.end(); it++ )
547     {
548         (*it)->show();
549     }
550     text->show();
551     label->show();
552 }
553
554
555 void ModuleListConfigControl::wakeUp_TheUserJustClickedOnSomething( int value )
556 {
557     text->clear();
558     for( QVector<QCheckBox*>::iterator it = modules.begin();
559          it != modules.end(); it++ )
560     {
561     }
562 }
563
564 /**************************************************************************
565  * Integer-based controls
566  *************************************************************************/
567
568 /*********** Integer **************/
569 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
570                                             module_config_t *_p_item,
571                                             QWidget *_parent, QGridLayout *l,
572                                             int &line ) :
573                            VIntConfigControl( _p_this, _p_item, _parent )
574 {
575     label = new QLabel( qfu(p_item->psz_text) );
576     spin = new QSpinBox; spin->setMinimumWidth( 80 );
577     spin->setMaximumWidth( 90 );
578     finish();
579
580     if( !l )
581     {
582         QHBoxLayout *layout = new QHBoxLayout();
583         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
584         widget->setLayout( layout );
585     }
586     else
587     {
588         l->addWidget( label, line, 0 );
589         l->addWidget( spin, line, 1, Qt::AlignRight );
590     }
591 }
592 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
593                                             module_config_t *_p_item,
594                                             QLabel *_label, QSpinBox *_spin ) :
595                                       VIntConfigControl( _p_this, _p_item )
596 {
597     spin = _spin;
598     label = _label;
599     finish();
600 }
601
602 void IntegerConfigControl::finish()
603 {
604     spin->setMaximum( 2000000000 );
605     spin->setMinimum( -2000000000 );
606     spin->setValue( p_item->value.i );
607     spin->setToolTip( qfu(p_item->psz_longtext) );
608     if( label )
609         label->setToolTip( qfu(p_item->psz_longtext) );
610 }
611
612 int IntegerConfigControl::getValue()
613 {
614     return spin->value();
615 }
616
617 /********* Integer range **********/
618 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
619                                             module_config_t *_p_item,
620                                             QWidget *_parent, QGridLayout *l,
621                                             int &line ) :
622             IntegerConfigControl( _p_this, _p_item, _parent, l, line )
623 {
624     finish();
625 }
626
627 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
628                                             module_config_t *_p_item,
629                                             QLabel *_label, QSpinBox *_spin ) :
630             IntegerConfigControl( _p_this, _p_item, _label, _spin )
631 {
632     finish();
633 }
634
635 void IntegerRangeConfigControl::finish()
636 {
637     spin->setMaximum( p_item->max.i );
638     spin->setMinimum( p_item->min.i );
639 }
640
641 IntegerRangeSliderConfigControl::IntegerRangeSliderConfigControl(
642                                             vlc_object_t *_p_this,
643                                             module_config_t *_p_item,
644                                             QLabel *_label, QSlider *_slider ):
645                     VIntConfigControl( _p_this, _p_item )
646 {
647     slider = _slider;
648     label = _label;
649     slider->setMaximum( p_item->max.i );
650     slider->setMinimum( p_item->min.i );
651     slider->setValue( p_item->value.i );
652     slider->setToolTip( qfu(p_item->psz_longtext) );
653     if( label )
654         label->setToolTip( qfu(p_item->psz_longtext) );
655 }
656
657 int IntegerRangeSliderConfigControl::getValue()
658 {
659         return slider->value();
660 }
661
662
663 /********* Integer / choice list **********/
664 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
665                module_config_t *_p_item, QWidget *_parent, bool bycat,
666                QGridLayout *l, int &line) :
667                VIntConfigControl( _p_this, _p_item, _parent )
668 {
669     label = new QLabel( qfu(p_item->psz_text) );
670     combo = new QComboBox();
671     finish( bycat );
672     if( !l )
673     {
674         QHBoxLayout *layout = new QHBoxLayout();
675         layout->addWidget( label ); layout->addWidget( combo );
676         widget->setLayout( layout );
677     }
678     else
679     {
680         l->addWidget( label, line, 0 );
681         l->addWidget( combo, line, 1, Qt::AlignRight );
682     }
683 }
684 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
685                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
686                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
687 {
688     combo = _combo;
689     label = _label;
690     finish( bycat );
691 }
692
693 void IntegerListConfigControl::finish( bool bycat )
694 {
695     combo->setEditable( false );
696
697     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
698     {
699         combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
700                         QVariant( p_item->pi_list[i_index] ) );
701         if( p_item->value.i == p_item->pi_list[i_index] )
702             combo->setCurrentIndex( combo->count() - 1 );
703     }
704     combo->setToolTip( qfu(p_item->psz_longtext) );
705     if( label )
706         label->setToolTip( qfu(p_item->psz_longtext) );
707 }
708
709 int IntegerListConfigControl::getValue()
710 {
711     return combo->itemData( combo->currentIndex() ).toInt();
712 }
713
714 /*********** Boolean **************/
715 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
716                                       module_config_t *_p_item,
717                                       QWidget *_parent, QGridLayout *l,
718                                       int &line ) :
719                     VIntConfigControl( _p_this, _p_item, _parent )
720 {
721     checkbox = new QCheckBox( qfu(p_item->psz_text) );
722     finish();
723
724     if( !l )
725     {
726         QHBoxLayout *layout = new QHBoxLayout();
727         layout->addWidget( checkbox, 0 );
728         widget->setLayout( layout );
729     }
730     else
731     {
732         l->addWidget( checkbox, line, 0 );
733     }
734 }
735 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
736                                       module_config_t *_p_item,
737                                       QLabel *_label,
738                                       QCheckBox *_checkbox,
739                                       bool bycat ) :
740                    VIntConfigControl( _p_this, _p_item )
741 {
742     checkbox = _checkbox;
743     finish();
744 }
745
746 void BoolConfigControl::finish()
747 {
748     checkbox->setCheckState( p_item->value.i == VLC_TRUE ? Qt::Checked
749                                                         : Qt::Unchecked );
750     checkbox->setToolTip( qfu(p_item->psz_longtext) );
751 }
752
753 int BoolConfigControl::getValue()
754 {
755     return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
756 }
757
758 /**************************************************************************
759  * Float-based controls
760  *************************************************************************/
761
762 /*********** Float **************/
763 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
764                                         module_config_t *_p_item,
765                                         QWidget *_parent, QGridLayout *l,
766                                         int &line ) :
767                     VFloatConfigControl( _p_this, _p_item, _parent )
768 {
769     label = new QLabel( qfu(p_item->psz_text) );
770     spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
771     spin->setMaximumWidth( 90 );
772     finish();
773
774     if( !l )
775     {
776         QHBoxLayout *layout = new QHBoxLayout();
777         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
778         widget->setLayout( layout );
779     }
780     else
781     {
782         l->addWidget( label, line, 0 );
783         l->addWidget( spin, line, 1, Qt::AlignRight );
784     }
785 }
786
787 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
788                                         module_config_t *_p_item,
789                                         QLabel *_label,
790                                         QDoubleSpinBox *_spin ) :
791                     VFloatConfigControl( _p_this, _p_item )
792 {
793     spin = _spin;
794     label = _label;
795     finish();
796 }
797
798 void FloatConfigControl::finish()
799 {
800     spin->setMaximum( 2000000000. );
801     spin->setMinimum( -2000000000. );
802     spin->setSingleStep( 0.1 );
803     spin->setValue( (double)p_item->value.f );
804     spin->setToolTip( qfu(p_item->psz_longtext) );
805     if( label )
806         label->setToolTip( qfu(p_item->psz_longtext) );
807 }
808
809 float FloatConfigControl::getValue()
810 {
811     return (float)spin->value();
812 }
813
814 /*********** Float with range **************/
815 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
816                                         module_config_t *_p_item,
817                                         QWidget *_parent, QGridLayout *l,
818                                         int &line ) :
819                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
820 {
821     finish();
822 }
823
824 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
825                                         module_config_t *_p_item,
826                                         QLabel *_label,
827                                         QDoubleSpinBox *_spin ) :
828                 FloatConfigControl( _p_this, _p_item, _label, _spin )
829 {
830     finish();
831 }
832
833 void FloatRangeConfigControl::finish()
834 {
835     spin->setMaximum( (double)p_item->max.f );
836     spin->setMinimum( (double)p_item->min.f );
837 }
838
839
840 /**********************************************************************
841  * Key selector widget
842  **********************************************************************/
843 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
844                                       module_config_t *_p_item,
845                                       QWidget *_parent, QGridLayout *l,
846                                       int &line ) :
847                                 ConfigControl( _p_this, _p_item, _parent )
848
849 {
850     label = new QLabel( qtr("Select an action to change the associated hotkey") );
851     table = new QTreeWidget( 0 );
852     finish();
853
854     if( !l )
855     {
856         QVBoxLayout *layout = new QVBoxLayout();
857         layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
858         widget->setLayout( layout );
859     }
860     else
861     {
862         l->addWidget( label, line, 0, 1, 2 );
863         l->addWidget( table, line+1, 0, 1,2 );
864     }
865 }
866
867 void KeySelectorControl::finish()
868 {
869     if( label )
870         label->setToolTip( qfu(p_item->psz_longtext) );
871
872     /* Fill the table */
873     table->setColumnCount( 2 );
874     table->setAlternatingRowColors( true );
875
876     module_t *p_main = config_FindModule( p_this, "main" );
877     assert( p_main );
878
879     for (size_t i = 0; i < p_main->confsize; i++)
880     {
881         module_config_t *p_item = p_main->p_config + i;
882
883         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
884             strstr( p_item->psz_name , "key-" ) )
885         {
886             QTreeWidgetItem *treeItem = new QTreeWidgetItem();
887             treeItem->setText( 0, qfu( p_item->psz_text ) );
888             treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
889             treeItem->setData( 0, Qt::UserRole,
890                                   QVariant::fromValue( (void*)p_item ) );
891             values += p_item;
892             table->addTopLevelItem( treeItem );
893         }
894     }
895     table->resizeColumnToContents( 0 );
896
897     CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
898              this, selectKey( QTreeWidgetItem * ) );
899 }
900
901 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
902 {
903    module_config_t *p_keyItem = static_cast<module_config_t*>
904                           (keyItem->data( 0, Qt::UserRole ).value<void*>());
905
906     KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text );
907     d->exec();
908     if( d->result() == QDialog::Accepted )
909     {
910         p_keyItem->value.i = d->keyValue;
911         if( d->conflicts )
912         {
913             for( int i = 0; i < table->topLevelItemCount() ; i++ )
914             {
915                 QTreeWidgetItem *it = table->topLevelItem(i);
916                 module_config_t *p_item = static_cast<module_config_t*>
917                               (it->data( 0, Qt::UserRole ).value<void*>());
918                 it->setText( 1, VLCKeyToString( p_item->value.i ) );
919             }
920         }
921         else
922             keyItem->setText( 1, VLCKeyToString( p_keyItem->value.i ) );
923     }
924     delete d;
925 }
926
927 void KeySelectorControl::doApply()
928 {
929     foreach( module_config_t *p_current, values )
930     {
931         config_PutInt( p_this, p_current->psz_name, p_current->value.i );
932     }
933 }
934
935 KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
936                                 const char * _keyToChange ) :
937                                                 QDialog(0), keyValue(0)
938 {
939     setModal( true );
940     values = _values;
941     conflicts = false;
942     keyToChange = _keyToChange;
943     setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange)  );
944
945     QVBoxLayout *l = new QVBoxLayout( this );
946     selected = new QLabel( qtr("Press the new keys for ")  + qfu(keyToChange) );
947     warning = new QLabel();
948     l->addWidget( selected , Qt::AlignCenter );
949     l->addWidget( warning, Qt::AlignCenter );
950
951     QHBoxLayout *l2 = new QHBoxLayout();
952     QPushButton *ok = new QPushButton( qtr("OK") );
953     l2->addWidget( ok );
954     QPushButton *cancel = new QPushButton( qtr("Cancel") );
955     l2->addWidget( cancel );
956
957     BUTTONACT( ok, accept() );
958     BUTTONACT( cancel, reject() );
959
960     l->addLayout( l2 );
961 }
962
963 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
964 {
965     if( e->key() == Qt::Key_Tab ) return;
966     int i_vlck = qtEventToVLCKey( e );
967     selected->setText( VLCKeyToString( i_vlck ) );
968     conflicts = false;
969     module_config_t *p_current = NULL;
970     foreach( p_current, values )
971     {
972         if( p_current->value.i == i_vlck && strcmp( p_current->psz_text,
973                                                     keyToChange ) )
974         {
975             p_current->value.i = 0;
976             conflicts = true;
977             break;
978         }
979     }
980     if( conflicts )
981     {
982         warning->setText(
983           qtr("Warning: the  key is already assigned to \"") +
984           QString( p_current->psz_text ) + "\"" );
985     }
986     else warning->setText( "" );
987     keyValue = i_vlck;
988 }