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