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