]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Qt4 - SimplePrefs - Audio: Small fixes and normalization for volume.
[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                     VIntConfigControl( _p_this, _p_item )
556 {
557     slider = _slider;
558     label = _label;
559     slider->setMaximum( p_item->max.i );
560     slider->setMinimum( p_item->min.i );
561     slider->setValue( p_item->value.i );
562     slider->setToolTip( qfu(p_item->psz_longtext) );
563     if( label )
564         label->setToolTip( qfu(p_item->psz_longtext) );
565 }
566
567 int IntegerRangeSliderConfigControl::getValue()
568 {
569         return slider->value();
570 }
571
572
573 /********* Integer / choice list **********/
574 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
575                module_config_t *_p_item, QWidget *_parent, bool bycat,
576                QGridLayout *l, int &line) :
577                VIntConfigControl( _p_this, _p_item, _parent )
578 {
579     label = new QLabel( qfu(p_item->psz_text) );
580     combo = new QComboBox();
581     finish( bycat );
582     if( !l )
583     {
584         QHBoxLayout *layout = new QHBoxLayout();
585         layout->addWidget( label ); layout->addWidget( combo );
586         widget->setLayout( layout );
587     }
588     else
589     {
590         l->addWidget( label, line, 0 );
591         l->addWidget( combo, line, 1, Qt::AlignRight );
592     }
593 }
594 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
595                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
596                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
597 {
598     combo = _combo;
599     label = _label;
600     finish( bycat );
601 }
602
603 void IntegerListConfigControl::finish( bool bycat )
604 {
605     combo->setEditable( false );
606
607     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
608     {
609         combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
610                         QVariant( p_item->pi_list[i_index] ) );
611         if( p_item->value.i == p_item->pi_list[i_index] )
612             combo->setCurrentIndex( combo->count() - 1 );
613     }
614     combo->setToolTip( qfu(p_item->psz_longtext) );
615     if( label )
616         label->setToolTip( qfu(p_item->psz_longtext) );
617 }
618
619 int IntegerListConfigControl::getValue()
620 {
621     return combo->itemData( combo->currentIndex() ).toInt();
622 }
623
624 /*********** Boolean **************/
625 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
626                                       module_config_t *_p_item,
627                                       QWidget *_parent, QGridLayout *l,
628                                       int &line ) :
629                     VIntConfigControl( _p_this, _p_item, _parent )
630 {
631     checkbox = new QCheckBox( qfu(p_item->psz_text) );
632     finish();
633
634     if( !l )
635     {
636         QHBoxLayout *layout = new QHBoxLayout();
637         layout->addWidget( checkbox, 0 );
638         widget->setLayout( layout );
639     }
640     else
641     {
642         l->addWidget( checkbox, line, 0 );
643     }
644 }
645 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
646                                       module_config_t *_p_item,
647                                       QLabel *_label,
648                                       QCheckBox *_checkbox,
649                                       bool bycat ) :
650                    VIntConfigControl( _p_this, _p_item )
651 {
652     checkbox = _checkbox;
653     finish();
654 }
655
656 void BoolConfigControl::finish()
657 {
658     checkbox->setCheckState( p_item->value.i == VLC_TRUE ? Qt::Checked
659                                                         : Qt::Unchecked );
660     checkbox->setToolTip( qfu(p_item->psz_longtext) );
661 }
662
663 int BoolConfigControl::getValue()
664 {
665     return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
666 }
667
668 /**************************************************************************
669  * Float-based controls
670  *************************************************************************/
671
672 /*********** Float **************/
673 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
674                                         module_config_t *_p_item,
675                                         QWidget *_parent, QGridLayout *l,
676                                         int &line ) :
677                     VFloatConfigControl( _p_this, _p_item, _parent )
678 {
679     label = new QLabel( qfu(p_item->psz_text) );
680     spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
681     spin->setMaximumWidth( 90 );
682     finish();
683
684     if( !l )
685     {
686         QHBoxLayout *layout = new QHBoxLayout();
687         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
688         widget->setLayout( layout );
689     }
690     else
691     {
692         l->addWidget( label, line, 0 );
693         l->addWidget( spin, line, 1, Qt::AlignRight );
694     }
695 }
696
697 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
698                                         module_config_t *_p_item,
699                                         QLabel *_label,
700                                         QDoubleSpinBox *_spin ) :
701                     VFloatConfigControl( _p_this, _p_item )
702 {
703     spin = _spin;
704     label = _label;
705     finish();
706 }
707
708 void FloatConfigControl::finish()
709 {
710     spin->setMaximum( 2000000000. );
711     spin->setMinimum( -2000000000. );
712     spin->setSingleStep( 0.1 );
713     spin->setValue( (double)p_item->value.f );
714     spin->setToolTip( qfu(p_item->psz_longtext) );
715     if( label )
716         label->setToolTip( qfu(p_item->psz_longtext) );
717 }
718
719 float FloatConfigControl::getValue()
720 {
721     return (float)spin->value();
722 }
723
724 /*********** Float with range **************/
725 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
726                                         module_config_t *_p_item,
727                                         QWidget *_parent, QGridLayout *l,
728                                         int &line ) :
729                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
730 {
731     finish();
732 }
733
734 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
735                                         module_config_t *_p_item,
736                                         QLabel *_label,
737                                         QDoubleSpinBox *_spin ) :
738                 FloatConfigControl( _p_this, _p_item, _label, _spin )
739 {
740     finish();
741 }
742
743 void FloatRangeConfigControl::finish()
744 {
745     spin->setMaximum( (double)p_item->max.f );
746     spin->setMinimum( (double)p_item->min.f );
747 }
748
749
750 /**********************************************************************
751  * Key selector widget
752  **********************************************************************/
753 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
754                                       module_config_t *_p_item,
755                                       QWidget *_parent, QGridLayout *l,
756                                       int &line ) :
757                                 ConfigControl( _p_this, _p_item, _parent )
758
759 {
760     label = new QLabel( qtr("Select an action to change the associated hotkey") );
761     table = new QTreeWidget( 0 );
762     finish();
763
764     if( !l )
765     {
766         QVBoxLayout *layout = new QVBoxLayout();
767         layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
768         widget->setLayout( layout );
769     }
770     else
771     {
772         l->addWidget( label, line, 0, 1, 2 );
773         l->addWidget( table, line+1, 0, 1,2 );
774     }
775 }
776
777 void KeySelectorControl::finish()
778 {
779     if( label )
780         label->setToolTip( qfu(p_item->psz_longtext) );
781
782     /* Fill the table */
783     table->setColumnCount( 2 );
784     table->setAlternatingRowColors( true );
785
786     module_t *p_main = config_FindModule( p_this, "main" );
787     assert( p_main );
788
789     for (size_t i = 0; i < p_main->confsize; i++)
790     {
791         module_config_t *p_item = p_main->p_config + i;
792
793         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
794             strstr( p_item->psz_name , "key-" ) )
795         {
796             QTreeWidgetItem *treeItem = new QTreeWidgetItem();
797             treeItem->setText( 0, qfu( p_item->psz_text ) );
798             treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
799             treeItem->setData( 0, Qt::UserRole,
800                                   QVariant::fromValue( (void*)p_item ) );
801             values += p_item;
802             table->addTopLevelItem( treeItem );
803         }
804     }
805     table->resizeColumnToContents( 0 );
806
807     CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
808              this, selectKey( QTreeWidgetItem * ) );
809 }
810
811 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
812 {
813    module_config_t *p_keyItem = static_cast<module_config_t*>
814                           (keyItem->data( 0, Qt::UserRole ).value<void*>());
815
816     KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text );
817     d->exec();
818     if( d->result() == QDialog::Accepted )
819     {
820         p_keyItem->value.i = d->keyValue;
821         if( d->conflicts )
822         {
823             for( int i = 0; i < table->topLevelItemCount() ; i++ )
824             {
825                 QTreeWidgetItem *it = table->topLevelItem(i);
826                 module_config_t *p_item = static_cast<module_config_t*>
827                               (it->data( 0, Qt::UserRole ).value<void*>());
828                 it->setText( 1, VLCKeyToString( p_item->value.i ) );
829             }
830         }
831         else
832             keyItem->setText( 1, VLCKeyToString( p_keyItem->value.i ) );
833     }
834     delete d;
835 }
836
837 void KeySelectorControl::doApply()
838 {
839     foreach( module_config_t *p_current, values )
840     {
841         config_PutInt( p_this, p_current->psz_name, p_current->value.i );
842     }
843 }
844
845 KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
846                                 const char * _keyToChange ) :
847                                                 QDialog(0), keyValue(0)
848 {
849     setModal( true );
850     values = _values;
851     conflicts = false;
852     keyToChange = _keyToChange;
853     setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange)  );
854
855     QVBoxLayout *l = new QVBoxLayout( this );
856     selected = new QLabel( qtr("Press the new keys for ")  + qfu(keyToChange) );
857     warning = new QLabel();
858     l->addWidget( selected , Qt::AlignCenter );
859     l->addWidget( warning, Qt::AlignCenter );
860
861     QHBoxLayout *l2 = new QHBoxLayout();
862     QPushButton *ok = new QPushButton( qtr("OK") );
863     l2->addWidget( ok );
864     QPushButton *cancel = new QPushButton( qtr("Cancel") );
865     l2->addWidget( cancel );
866
867     BUTTONACT( ok, accept() );
868     BUTTONACT( cancel, reject() );
869
870     l->addLayout( l2 );
871 }
872
873 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
874 {
875     if( e->key() == Qt::Key_Tab ) return;
876     int i_vlck = qtEventToVLCKey( e );
877     selected->setText( VLCKeyToString( i_vlck ) );
878     conflicts = false;
879     module_config_t *p_current = NULL;
880     foreach( p_current, values )
881     {
882         if( p_current->value.i == i_vlck && strcmp( p_current->psz_text,
883                                                     keyToChange ) )
884         {
885             p_current->value.i = 0;
886             conflicts = true;
887             break;
888         }
889     }
890     if( conflicts )
891     {
892         warning->setText(
893           qtr("Warning: the  key is already assigned to \"") +
894           QString( p_current->psz_text ) + "\"" );
895     }
896     else warning->setText( "" );
897     keyValue = i_vlck;
898 }