]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Hotkey selector widget. Save not implemented yet
[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 "util/customwidgets.hpp"
36 #include "qt4.hpp"
37
38 #include <QLineEdit>
39 #include <QString>
40 #include <QSpinBox>
41 #include <QDoubleSpinBox>
42 #include <QVariant>
43 #include <QComboBox>
44 #include <QGridLayout>
45 #include <QPushButton>
46
47 #include <vlc_keys.h>
48
49 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
50                                              module_config_t *p_item,
51                                              QWidget *parent )
52 {
53     int i=0;
54     return createControl( p_this, p_item, parent, NULL, i );
55 }
56
57 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
58                                              module_config_t *p_item,
59                                              QWidget *parent,
60                                              QGridLayout *l, int &line )
61 {
62     ConfigControl *p_control = NULL;
63     if( p_item->psz_current ) return NULL;
64
65     switch( p_item->i_type )
66     {
67     case CONFIG_ITEM_MODULE:
68         p_control = new ModuleConfigControl( p_this, p_item, parent, false,
69                                              l, line );
70         break;
71     case CONFIG_ITEM_MODULE_CAT:
72         p_control = new ModuleConfigControl( p_this, p_item, parent, true,
73                                              l, line );
74         break;
75     case CONFIG_ITEM_MODULE_LIST:
76         p_control = new ModuleListConfigControl( p_this, p_item, parent, false,
77                                              l, line );
78         break;
79     case CONFIG_ITEM_MODULE_LIST_CAT:
80         p_control = new ModuleListConfigControl( p_this, p_item, parent, true,
81                                              l, line );
82         break;
83     case CONFIG_ITEM_STRING:
84         if( !p_item->i_list )
85             p_control = new StringConfigControl( p_this, p_item, parent,
86                                                  l, line, false );
87         else
88             p_control = new StringListConfigControl( p_this, p_item,
89                                             parent, false, l, line );
90         break;
91     case CONFIG_ITEM_INTEGER:
92         if( p_item->i_list )
93             p_control = new IntegerListConfigControl( p_this, p_item,
94                                             parent, false, l, line );
95         else if( p_item->i_min || p_item->i_max )
96             p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
97                                                        l, line );
98         else
99             p_control = new IntegerConfigControl( p_this, p_item, parent,
100                                                   l, line );
101         break;
102     case CONFIG_ITEM_FILE:
103         fprintf( stderr, "Todo (CONFIG_ITEM_FILE)\n" );
104         break;
105     case CONFIG_ITEM_DIRECTORY:
106         fprintf( stderr, "Todo (CONFIG_ITEM_DIRECTORY)\n" );
107         break;
108     case CONFIG_ITEM_KEY:
109         p_control = new KeySelectorControl( p_this, p_item, parent, l, line );
110         break;
111     case CONFIG_ITEM_BOOL:
112         p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
113         break;
114     case CONFIG_ITEM_FLOAT:
115         if( p_item->f_min || p_item->f_max )
116             p_control = new FloatRangeConfigControl( p_this, p_item, parent,
117                                                      l, line );
118         else
119             p_control = new FloatConfigControl( p_this, p_item, parent,
120                                                   l, line );
121         break;
122     default:
123         break;
124     }
125     return p_control;
126 }
127
128 void ConfigControl::doApply( intf_thread_t *p_intf )
129 {
130     switch( getType() )
131     {
132         case 1:
133         {
134             VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(this);
135             config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
136             break;
137         }
138         case 2:
139         {
140             VFloatConfigControl *vfcc =
141                                     qobject_cast<VFloatConfigControl *>(this);
142             config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
143             break;
144         }
145         case 3:
146         {
147             VStringConfigControl *vscc =
148                             qobject_cast<VStringConfigControl *>(this);
149             config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
150         }
151         case 4:
152         {
153             KeySelectorControl *ksc = qobject_cast<KeySelectorControl *>(this);
154             ksc->doApply();
155         }
156     }
157 }
158
159 /**************************************************************************
160  * String-based controls
161  *************************************************************************/
162
163 /*********** String **************/
164 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
165                                           module_config_t *_p_item,
166                                           QWidget *_parent, QGridLayout *l,
167                                           int &line, bool pwd ) :
168                            VStringConfigControl( _p_this, _p_item, _parent )
169 {
170     label = new QLabel( qfu(p_item->psz_text) );
171     text = new QLineEdit( qfu(p_item->psz_value) );
172     finish();
173
174     if( !l )
175     {
176         QHBoxLayout *layout = new QHBoxLayout();
177         layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
178         widget->setLayout( layout );
179     }
180     else
181     {
182         l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
183     }
184 }
185
186 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
187                                    module_config_t *_p_item,
188                                    QLabel *_label, QLineEdit *_text, bool pwd ):
189                            VStringConfigControl( _p_this, _p_item )
190 {
191     text = _text;
192     label = _label;
193     finish( );
194 }
195
196 void StringConfigControl::finish()
197 {
198     text->setText( qfu(p_item->psz_value) );
199     text->setToolTip( qfu(p_item->psz_longtext) );
200     if( label )
201         label->setToolTip( qfu(p_item->psz_longtext) );
202 }
203
204 /********* String / choice list **********/
205 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
206                module_config_t *_p_item, QWidget *_parent, bool bycat,
207                QGridLayout *l, int &line) :
208                VStringConfigControl( _p_this, _p_item, _parent )
209 {
210     label = new QLabel( qfu(p_item->psz_text) );
211     combo = new QComboBox();
212     finish( bycat );
213     if( !l )
214     {
215         QHBoxLayout *layout = new QHBoxLayout();
216         layout->addWidget( label ); layout->addWidget( combo );
217         widget->setLayout( layout );
218     }
219     else
220     {
221         l->addWidget( label, line, 0 );
222         l->addWidget( combo, line, 1, Qt::AlignRight );
223     }
224 }
225 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
226                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
227                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
228 {
229     combo = _combo;
230     label = _label;
231     finish( bycat );
232 }
233
234 void StringListConfigControl::finish( bool bycat )
235 {
236     combo->setEditable( false );
237
238     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
239     {
240         combo->addItem( qfu(p_item->ppsz_list_text ?
241                             p_item->ppsz_list_text[i_index] :
242                             p_item->ppsz_list[i_index] ),
243                         QVariant( p_item->ppsz_list[i_index] ) );
244         if( p_item->psz_value && !strcmp( p_item->psz_value,
245                                           p_item->ppsz_list[i_index] ) )
246             combo->setCurrentIndex( combo->count() - 1 );
247     }
248     combo->setToolTip( qfu(p_item->psz_longtext) );
249     if( label )
250         label->setToolTip( qfu(p_item->psz_longtext) );
251 }
252
253 QString StringListConfigControl::getValue()
254 {
255     return combo->itemData( combo->currentIndex() ).toString();
256 }
257
258 /********* Module **********/
259 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
260                module_config_t *_p_item, QWidget *_parent, bool bycat,
261                QGridLayout *l, int &line) :
262                VStringConfigControl( _p_this, _p_item, _parent )
263 {
264     label = new QLabel( qfu(p_item->psz_text) );
265     combo = new QComboBox();
266     finish( bycat );
267     if( !l )
268     {
269         QHBoxLayout *layout = new QHBoxLayout();
270         layout->addWidget( label ); layout->addWidget( combo );
271         widget->setLayout( layout );
272     }
273     else
274     {
275         l->addWidget( label, line, 0 );
276         l->addWidget( combo, line, 1, Qt::AlignRight );
277     }
278 }
279 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
280                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
281                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
282 {
283     combo = _combo;
284     label = _label;
285     finish( bycat );
286 }
287
288 void ModuleConfigControl::finish( bool bycat )
289 {
290     vlc_list_t *p_list;
291     module_t *p_parser;
292
293     combo->setEditable( false );
294
295     /* build a list of available modules */
296     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
297     combo->addItem( qtr("Default") );
298     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
299     {
300         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
301
302         if( bycat )
303         {
304             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
305
306             module_config_t *p_config = p_parser->p_config;
307             if( p_config ) do
308             {
309                 /* Hack: required subcategory is stored in i_min */
310                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
311                     p_config->i_value == p_item->i_min )
312                     combo->addItem( qfu(p_parser->psz_longname),
313                                     QVariant( p_parser->psz_object_name ) );
314                 if( p_item->psz_value && !strcmp( p_item->psz_value,
315                                                   p_parser->psz_object_name) )
316                     combo->setCurrentIndex( combo->count() - 1 );
317             } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
318         }
319         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
320         {
321             combo->addItem( qfu(p_parser->psz_longname),
322                             QVariant( p_parser->psz_object_name ) );
323             if( p_item->psz_value && !strcmp( p_item->psz_value,
324                                               p_parser->psz_object_name) )
325                 combo->setCurrentIndex( combo->count() - 1 );
326         }
327     }
328     vlc_list_release( p_list );
329     combo->setToolTip( qfu(p_item->psz_longtext) );
330     if( label )
331         label->setToolTip( qfu(p_item->psz_longtext) );
332 }
333
334 QString ModuleConfigControl::getValue()
335 {
336     return combo->itemData( combo->currentIndex() ).toString();
337 }
338
339 /********* Module list **********/
340 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
341                module_config_t *_p_item, QWidget *_parent, bool bycat,
342                QGridLayout *l, int &line) :
343                VStringConfigControl( _p_this, _p_item, _parent )
344 {
345     label = new QLabel( qfu(p_item->psz_text) );
346     text = new QLineEdit();
347     finish( bycat );
348
349     bool pom = false;
350     if( !l )
351     {
352         l = new QGridLayout();
353         line = 0;
354         pom = true;
355     }
356     for( QVector<QCheckBox*>::iterator it = modules.begin();
357          it != modules.end(); it++ )
358     {
359         l->addWidget( *it, line++, 1 );
360     }
361     l->addWidget( label, line, 0 );
362     l->addWidget( text, line, 1 );
363     if( pom )
364         widget->setLayout( l );
365 }
366 #if 0
367 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
368                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
369                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
370 {
371     combo = _combo;
372     label = _label;
373     finish( bycat );
374 }
375 #endif
376
377 ModuleListConfigControl::~ModuleListConfigControl()
378 {
379     for( QVector<QCheckBox*>::iterator it = modules.begin();
380          it != modules.end(); it++ )
381     {
382         delete *it;
383     }
384     delete label;
385     delete text;
386 }
387
388 void ModuleListConfigControl::finish( bool bycat )
389 {
390     vlc_list_t *p_list;
391     module_t *p_parser;
392
393     /* build a list of available modules */
394     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
395     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
396     {
397         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
398
399         if( bycat )
400         {
401             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
402
403             module_config_t *p_config = p_parser->p_config;
404             if( p_config ) do
405             {
406                 /* Hack: required subcategory is stored in i_min */
407                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
408                     p_config->i_value == p_item->i_min )
409                 {
410                     QCheckBox *cb =
411                         new QCheckBox( qfu( p_parser->psz_object_name ) );
412                     cb->setToolTip( qfu(p_parser->psz_longname) );
413                     modules.push_back( cb );
414                 }
415             } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
416         }
417         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
418         {
419             QCheckBox *cb =
420                 new QCheckBox( qfu( p_parser->psz_object_name ) );
421             cb->setToolTip( qfu(p_parser->psz_longname) );
422             modules.push_back( cb );
423         }
424     }
425     vlc_list_release( p_list );
426     text->setToolTip( qfu(p_item->psz_longtext) );
427     if( label )
428         label->setToolTip( qfu(p_item->psz_longtext) );
429 }
430
431 QString ModuleListConfigControl::getValue()
432 {
433     return text->text();
434 }
435
436 void ModuleListConfigControl::hide()
437 {
438     for( QVector<QCheckBox*>::iterator it = modules.begin();
439          it != modules.end(); it++ )
440     {
441         (*it)->hide();
442     }
443     text->hide();
444     label->hide();
445 }
446
447 void ModuleListConfigControl::show()
448 {
449     for( QVector<QCheckBox*>::iterator it = modules.begin();
450          it != modules.end(); it++ )
451     {
452         (*it)->show();
453     }
454     text->show();
455     label->show();
456 }
457
458
459 void ModuleListConfigControl::wakeUp_TheUserJustClickedOnSomething( int value )
460 {
461     text->clear();
462     for( QVector<QCheckBox*>::iterator it = modules.begin();
463          it != modules.end(); it++ )
464     {
465     }
466 }
467
468 /**************************************************************************
469  * Integer-based controls
470  *************************************************************************/
471
472 /*********** Integer **************/
473 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
474                                             module_config_t *_p_item,
475                                             QWidget *_parent, QGridLayout *l,
476                                             int &line ) :
477                            VIntConfigControl( _p_this, _p_item, _parent )
478 {
479     label = new QLabel( qfu(p_item->psz_text) );
480     spin = new QSpinBox; spin->setMinimumWidth( 80 );
481     spin->setMaximumWidth( 90 );
482     finish();
483
484     if( !l )
485     {
486         QHBoxLayout *layout = new QHBoxLayout();
487         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
488         widget->setLayout( layout );
489     }
490     else
491     {
492         l->addWidget( label, line, 0 );
493         l->addWidget( spin, line, 1, Qt::AlignRight );
494     }
495 }
496 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
497                                             module_config_t *_p_item,
498                                             QLabel *_label, QSpinBox *_spin ) :
499                                       VIntConfigControl( _p_this, _p_item )
500 {
501     spin = _spin;
502     label = _label;
503     finish();
504 }
505
506 void IntegerConfigControl::finish()
507 {
508     spin->setMaximum( 2000000000 );
509     spin->setMinimum( -2000000000 );
510     spin->setValue( p_item->i_value );
511     spin->setToolTip( qfu(p_item->psz_longtext) );
512     if( label )
513         label->setToolTip( qfu(p_item->psz_longtext) );
514 }
515
516 int IntegerConfigControl::getValue()
517 {
518     return spin->value();
519 }
520
521 /********* Integer range **********/
522 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
523                                             module_config_t *_p_item,
524                                             QWidget *_parent, QGridLayout *l,
525                                             int &line ) :
526             IntegerConfigControl( _p_this, _p_item, _parent, l, line )
527 {
528     finish();
529 }
530
531 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
532                                             module_config_t *_p_item,
533                                             QLabel *_label, QSpinBox *_spin ) :
534             IntegerConfigControl( _p_this, _p_item, _label, _spin )
535 {
536     finish();
537 }
538
539 void IntegerRangeConfigControl::finish()
540 {
541     spin->setMaximum( p_item->i_max );
542     spin->setMinimum( p_item->i_min );
543 }
544
545 /********* Integer / choice list **********/
546 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
547                module_config_t *_p_item, QWidget *_parent, bool bycat,
548                QGridLayout *l, int &line) :
549                VIntConfigControl( _p_this, _p_item, _parent )
550 {
551     label = new QLabel( qfu(p_item->psz_text) );
552     combo = new QComboBox();
553     finish( bycat );
554     if( !l )
555     {
556         QHBoxLayout *layout = new QHBoxLayout();
557         layout->addWidget( label ); layout->addWidget( combo );
558         widget->setLayout( layout );
559     }
560     else
561     {
562         l->addWidget( label, line, 0 );
563         l->addWidget( combo, line, 1, Qt::AlignRight );
564     }
565 }
566 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
567                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
568                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
569 {
570     combo = _combo;
571     label = _label;
572     finish( bycat );
573 }
574
575 void IntegerListConfigControl::finish( bool bycat )
576 {
577     combo->setEditable( false );
578
579     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
580     {
581         combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
582                         QVariant( p_item->pi_list[i_index] ) );
583         if( p_item->i_value == p_item->pi_list[i_index] )
584             combo->setCurrentIndex( combo->count() - 1 );
585     }
586     combo->setToolTip( qfu(p_item->psz_longtext) );
587     if( label )
588         label->setToolTip( qfu(p_item->psz_longtext) );
589 }
590
591 int IntegerListConfigControl::getValue()
592 {
593     return combo->itemData( combo->currentIndex() ).toInt();
594 }
595
596 /*********** Boolean **************/
597 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
598                                       module_config_t *_p_item,
599                                       QWidget *_parent, QGridLayout *l,
600                                       int &line ) :
601                     VIntConfigControl( _p_this, _p_item, _parent )
602 {
603     checkbox = new QCheckBox( qfu(p_item->psz_text) );
604     finish();
605
606     if( !l )
607     {
608         QHBoxLayout *layout = new QHBoxLayout();
609         layout->addWidget( checkbox, 0 );
610         widget->setLayout( layout );
611     }
612     else
613     {
614         l->addWidget( checkbox, line, 0 );
615     }
616 }
617 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
618                                       module_config_t *_p_item,
619                                       QLabel *_label,
620                                       QCheckBox *_checkbox,
621                                       bool bycat ) :
622                    VIntConfigControl( _p_this, _p_item )
623 {
624     checkbox = _checkbox;
625     finish();
626 }
627
628 void BoolConfigControl::finish()
629 {
630     checkbox->setCheckState( p_item->i_value == VLC_TRUE ? Qt::Checked
631                                                         : Qt::Unchecked );
632     checkbox->setToolTip( qfu(p_item->psz_longtext) );
633 }
634
635 int BoolConfigControl::getValue()
636 {
637     return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
638 }
639
640 /**************************************************************************
641  * Float-based controls
642  *************************************************************************/
643
644 /*********** Float **************/
645 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
646                                         module_config_t *_p_item,
647                                         QWidget *_parent, QGridLayout *l,
648                                         int &line ) :
649                     VFloatConfigControl( _p_this, _p_item, _parent )
650 {
651     label = new QLabel( qfu(p_item->psz_text) );
652     spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
653     spin->setMaximumWidth( 90 );
654     finish();
655
656     if( !l )
657     {
658         QHBoxLayout *layout = new QHBoxLayout();
659         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
660         widget->setLayout( layout );
661     }
662     else
663     {
664         l->addWidget( label, line, 0 );
665         l->addWidget( spin, line, 1, Qt::AlignRight );
666     }
667 }
668
669 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
670                                         module_config_t *_p_item,
671                                         QLabel *_label,
672                                         QDoubleSpinBox *_spin ) :
673                     VFloatConfigControl( _p_this, _p_item )
674 {
675     spin = _spin;
676     label = _label;
677     finish();
678 }
679
680 void FloatConfigControl::finish()
681 {
682     spin->setMaximum( 2000000000. );
683     spin->setMinimum( -2000000000. );
684     spin->setSingleStep( 0.1 );
685     spin->setValue( (double)p_item->f_value );
686     spin->setToolTip( qfu(p_item->psz_longtext) );
687     if( label )
688         label->setToolTip( qfu(p_item->psz_longtext) );
689 }
690
691 float FloatConfigControl::getValue()
692 {
693     return (float)spin->value();
694 }
695
696 /*********** Float with range **************/
697 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
698                                         module_config_t *_p_item,
699                                         QWidget *_parent, QGridLayout *l,
700                                         int &line ) :
701                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
702 {
703     finish();
704 }
705
706 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
707                                         module_config_t *_p_item,
708                                         QLabel *_label,
709                                         QDoubleSpinBox *_spin ) :
710                 FloatConfigControl( _p_this, _p_item, _label, _spin )
711 {
712     finish();
713 }
714
715 void FloatRangeConfigControl::finish()
716 {
717     spin->setMaximum( (double)p_item->f_max );
718     spin->setMinimum( (double)p_item->f_min );
719 }
720
721
722 /**********************************************************************
723  * Key selector widget
724  **********************************************************************/
725 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
726                                       module_config_t *_p_item,
727                                       QWidget *_parent, QGridLayout *l,
728                                       int &line ) :
729                                 ConfigControl( _p_this, _p_item, _parent )
730
731 {
732     label = new QLabel( qtr("Select an action to change the associated hotkey") );
733     table = new QTreeWidget( 0 );
734     finish();
735
736     if( !l )
737     {
738         QVBoxLayout *layout = new QVBoxLayout();
739         layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
740         widget->setLayout( layout );
741     }
742     else
743     {
744         l->addWidget( label, line, 0, 1, 2 );
745         l->addWidget( table, line+1, 0, 1,2 );
746     }
747 }
748
749 void KeySelectorControl::finish()
750 {
751     if( label )
752         label->setToolTip( qfu(p_item->psz_longtext) );
753
754     /* Fill the table */
755     table->setColumnCount( 2 );
756     table->setAlternatingRowColors( true );
757
758     module_t *p_main = config_FindModule( p_this, "main" );
759     assert( p_main );
760     module_config_t *p_item = p_main->p_config;
761
762     if( p_item ) do
763     {
764         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
765             strstr( p_item->psz_name , "key-" ) )
766         {
767             QTreeWidgetItem *treeItem = new QTreeWidgetItem();
768             treeItem->setText( 0, qfu( p_item->psz_text ) );
769             treeItem->setText( 1, VLCKeyToString( p_item->i_value ) );
770             treeItem->setData( 0, Qt::UserRole,
771                                   QVariant::fromValue( (void*)p_item ) );
772             values += p_item;
773             table->addTopLevelItem( treeItem );
774         }
775     } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
776     table->resizeColumnToContents( 0 );
777
778     CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
779              this, selectKey( QTreeWidgetItem * ) );
780 }
781
782 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
783 {
784    module_config_t *p_keyItem = static_cast<module_config_t*>
785                           (keyItem->data( 0, Qt::UserRole ).value<void*>());
786
787     KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text );
788     d->exec();
789     if( d->result() == QDialog::Accepted )
790     {
791         p_keyItem->i_value = d->keyValue;
792         if( d->conflicts )
793         {
794             for( int i = 0; i < table->topLevelItemCount() ; i++ )
795             {
796                 QTreeWidgetItem *it = table->topLevelItem(i);
797                 module_config_t *p_item = static_cast<module_config_t*>
798                               (it->data( 0, Qt::UserRole ).value<void*>());
799                 it->setText( 1, VLCKeyToString( p_item->i_value ) );
800             }
801         }
802         else
803             keyItem->setText( 1, VLCKeyToString( p_keyItem->i_value ) );
804     }
805     delete d;
806 }
807
808 void KeySelectorControl::doApply()
809 {
810 }
811
812 KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
813                                 char * _keyToChange ) :
814                                                 QDialog(0), keyValue(0)
815 {
816     setModal( true );
817     values = _values;
818     conflicts = false;
819     keyToChange = _keyToChange;
820     setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange)  );
821
822     QVBoxLayout *l = new QVBoxLayout( this );
823     selected = new QLabel( qtr("Press the new keys for ")  + qfu(keyToChange) );
824     warning = new QLabel();
825     l->addWidget( selected , Qt::AlignCenter );
826     l->addWidget( warning, Qt::AlignCenter );
827
828     QHBoxLayout *l2 = new QHBoxLayout();
829     QPushButton *ok = new QPushButton( qtr("OK") );
830     l2->addWidget( ok );
831     QPushButton *cancel = new QPushButton( qtr("Cancel") );
832     l2->addWidget( cancel );
833
834     BUTTONACT( ok, accept() );
835     BUTTONACT( cancel, reject() );
836
837     l->addLayout( l2 );
838 }
839
840 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
841 {
842     if( e->key() == Qt::Key_Tab ) return;
843     int i_vlck = qtEventToVLCKey( e );
844     selected->setText( VLCKeyToString( i_vlck ) );
845     conflicts = false;
846     module_config_t *p_current = NULL;
847     foreach( p_current, values )
848     {
849         if( p_current->i_value == i_vlck && strcmp( p_current->psz_text,
850                                                     keyToChange ) )
851         {
852             p_current->i_value = 0;
853             conflicts = true;
854             break;
855         }
856     }
857     if( conflicts )
858     {
859         warning->setText(
860           qtr("Warning: the  key is already assigned to \"") +
861           QString( p_current->psz_text ) + "\"" );
862     }
863     else warning->setText( "" );
864     keyValue = i_vlck;
865 }