]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Qt4 - Preferences. Implement the onUpdate for ModuleList.
[vlc] / modules / gui / qt4 / components / preferences_widgets.cpp
1 /*****************************************************************************
2  * preferences_widgets.cpp : Widgets for preferences displays
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Antoine Cellerier <dionoea@videolan.org>
9  *          Jean-Baptiste Kempf <jb@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /**
27  * Todo:
28  *  - Finish implementation (see WX)
29  *  - Improvements over WX
30  *      - Password field implementation (through "pwd" bool param
31  *      - Validator for modulelist
32  *  - Implement update stuff using a general Updated signal
33  */
34
35 #include "components/preferences_widgets.hpp"
36 #include "util/customwidgets.hpp"
37 #include "qt4.hpp"
38
39 #include <QLineEdit>
40 #include <QString>
41 #include <QSpinBox>
42 #include <QDoubleSpinBox>
43 #include <QVariant>
44 #include <QComboBox>
45 #include <QGridLayout>
46 #include <QPushButton>
47 #include <QSlider>
48 #include <QFileDialog>
49 #include <QFontDialog>
50 #include <QGroupBox>
51
52 #include <vlc_keys.h>
53
54
55 QString formatTooltip(const QString & tooltip)
56 {
57     QString formatted =
58     "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">"
59     " p, li { white-space: pre-wrap; } </style></head><body style=\" font-family:'Sans Serif';"
60     " font-size:9pt; font-weight:400; font-style:normal; text-decoration:none;\">"
61     "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; "
62     "-qt-block-indent:0; text-indent:0px;\">" +
63     tooltip +
64     "</p></body></html>";
65     return formatted;
66 }
67
68 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
69                                              module_config_t *p_item,
70                                              QWidget *parent )
71 {
72     int i=0;
73     return createControl( p_this, p_item, parent, NULL, i );
74 }
75
76 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
77                                              module_config_t *p_item,
78                                              QWidget *parent,
79                                              QGridLayout *l, int &line )
80 {
81     ConfigControl *p_control = NULL;
82     if( p_item->psz_current ) return NULL;
83
84     switch( p_item->i_type )
85     {
86     case CONFIG_ITEM_MODULE:
87         p_control = new ModuleConfigControl( p_this, p_item, parent, false,
88                                              l, line );
89         break;
90     case CONFIG_ITEM_MODULE_CAT:
91         p_control = new ModuleConfigControl( p_this, p_item, parent, true,
92                                              l, line );
93         break;
94     case CONFIG_ITEM_MODULE_LIST:
95         p_control = new ModuleListConfigControl( p_this, p_item, parent, false,
96                                              l, line );
97         break;
98     case CONFIG_ITEM_MODULE_LIST_CAT:
99         p_control = new ModuleListConfigControl( p_this, p_item, parent, true,
100                                              l, line );
101         break;
102     case CONFIG_ITEM_STRING:
103         if( !p_item->i_list )
104             p_control = new StringConfigControl( p_this, p_item, parent,
105                                                  l, line, false );
106         else
107             p_control = new StringListConfigControl( p_this, p_item,
108                                             parent, false, l, line );
109         break;
110     case CONFIG_ITEM_INTEGER:
111         if( p_item->i_list )
112             p_control = new IntegerListConfigControl( p_this, p_item,
113                                             parent, false, l, line );
114         else if( p_item->min.i || p_item->max.i )
115             p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
116                                                        l, line );
117         else
118             p_control = new IntegerConfigControl( p_this, p_item, parent,
119                                                   l, line );
120         break;
121     case CONFIG_ITEM_FILE:
122         p_control = new FileConfigControl( p_this, p_item, parent, l,
123                                                 line, false );
124         break;
125     case CONFIG_ITEM_DIRECTORY:
126         p_control = new DirectoryConfigControl( p_this, p_item, parent, l,
127                                                 line, false );
128         break;
129     case CONFIG_ITEM_FONT:
130         p_control = new FontConfigControl( p_this, p_item, parent, l,
131                                            line, false );
132         break;
133     case CONFIG_ITEM_KEY:
134         p_control = new KeySelectorControl( p_this, p_item, parent, l, line );
135         break;
136     case CONFIG_ITEM_BOOL:
137         p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
138         break;
139     case CONFIG_ITEM_FLOAT:
140         if( p_item->min.f || p_item->max.f )
141             p_control = new FloatRangeConfigControl( p_this, p_item, parent,
142                                                      l, line );
143         else
144             p_control = new FloatConfigControl( p_this, p_item, parent,
145                                                   l, line );
146         break;
147     default:
148         break;
149     }
150     return p_control;
151 }
152
153 void ConfigControl::doApply( intf_thread_t *p_intf )
154 {
155     switch( getType() )
156     {
157         case 1:
158         {
159             VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(this);
160             assert( vicc );
161             config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
162             break;
163         }
164         case 2:
165         {
166             VFloatConfigControl *vfcc =
167                                     qobject_cast<VFloatConfigControl *>(this);
168             assert( vfcc );
169             config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
170             break;
171         }
172         case 3:
173         {
174             VStringConfigControl *vscc =
175                             qobject_cast<VStringConfigControl *>(this);
176             assert( vscc );
177             config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
178             break;
179         }
180         case 4:
181         {
182             KeySelectorControl *ksc = qobject_cast<KeySelectorControl *>(this);
183             assert( ksc );
184             ksc->doApply();
185         }
186     }
187 }
188
189 /**************************************************************************
190  * String-based controls
191  *************************************************************************/
192
193 /*********** String **************/
194 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
195                                           module_config_t *_p_item,
196                                           QWidget *_parent, QGridLayout *l,
197                                           int &line, bool pwd ) :
198                            VStringConfigControl( _p_this, _p_item, _parent )
199 {
200     label = new QLabel( qfu(p_item->psz_text) );
201     text = new QLineEdit( qfu(p_item->value.psz) );
202     finish();
203
204     if( !l )
205     {
206         QHBoxLayout *layout = new QHBoxLayout();
207         layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
208         widget->setLayout( layout );
209     }
210     else
211     {
212         l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
213     }
214 }
215
216 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
217                                    module_config_t *_p_item,
218                                    QLabel *_label, QLineEdit *_text, bool pwd ):
219                            VStringConfigControl( _p_this, _p_item )
220 {
221     text = _text;
222     label = _label;
223     finish( );
224 }
225
226 void StringConfigControl::finish()
227 {
228     text->setText( qfu(p_item->value.psz) );
229     text->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
230     if( label )
231         label->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
232 }
233
234 /*********** File **************/
235 FileConfigControl::FileConfigControl( vlc_object_t *_p_this,
236                                           module_config_t *_p_item,
237                                           QWidget *_parent, QGridLayout *l,
238                                           int &line, bool pwd ) :
239                            VStringConfigControl( _p_this, _p_item, _parent )
240 {
241     label = new QLabel( qfu(p_item->psz_text) );
242     text = new QLineEdit( qfu(p_item->value.psz) );
243     browse = new QPushButton( qtr( "Browse..." ) );
244     QHBoxLayout *textAndButton = new QHBoxLayout();
245     textAndButton->setMargin( 0 );
246     textAndButton->addWidget( text, 2 );
247     textAndButton->addWidget( browse, 0 );
248
249     BUTTONACT( browse, updateField() );
250
251     finish();
252
253     if( !l )
254     {
255         QHBoxLayout *layout = new QHBoxLayout();
256         layout->addWidget( label, 0 );
257         layout->addLayout( textAndButton, 1 );
258         widget->setLayout( layout );
259     }
260     else
261     {
262         l->addWidget( label, line, 0 );
263         l->addLayout( textAndButton, line, 1 );
264     }
265 }
266
267
268 FileConfigControl::FileConfigControl( vlc_object_t *_p_this,
269                                    module_config_t *_p_item,
270                                    QLabel *_label, QLineEdit *_text,
271                                    QPushButton *_button, bool pwd ):
272                            VStringConfigControl( _p_this, _p_item )
273 {
274     browse = _button;
275     text = _text;
276     label = _label;
277
278     BUTTONACT( browse, updateField() );
279
280     finish( );
281 }
282
283 void FileConfigControl::updateField()
284 {
285     QString file = QFileDialog::getOpenFileName( NULL,
286                   qtr( "Select File" ), qfu( p_this->p_libvlc->psz_homedir ) );
287     if( file.isNull() ) return;
288     text->setText( file );
289 }
290
291 void FileConfigControl::finish()
292 {
293     text->setText( qfu(p_item->value.psz) );
294     text->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
295     if( label )
296         label->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
297 }
298
299 /********* String / Directory **********/
300 DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
301                         module_config_t *_p_item, QWidget *_p_widget,
302                         QGridLayout *_p_layout, int& _int, bool _pwd ) :
303      FileConfigControl( _p_this, _p_item, _p_widget, _p_layout, _int, _pwd)
304 {}
305
306 DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
307                         module_config_t *_p_item, QLabel *_p_label,
308                         QLineEdit *_p_line, QPushButton *_p_button, bool _pwd ):
309      FileConfigControl( _p_this, _p_item, _p_label, _p_line, _p_button, _pwd)
310 {}
311
312 void DirectoryConfigControl::updateField()
313 {
314     QString dir = QFileDialog::getExistingDirectory( NULL,
315                       qtr( "Select Directory" ),
316                       text->text().isEmpty() ?
317                         qfu( p_this->p_libvlc->psz_homedir ) : text->text(),
318                       QFileDialog::ShowDirsOnly |
319                         QFileDialog::DontResolveSymlinks );
320     if( dir.isNull() ) return;
321     text->setText( dir );
322 }
323
324 /********* String / Font **********/
325 FontConfigControl::FontConfigControl( vlc_object_t *_p_this,
326                         module_config_t *_p_item, QWidget *_p_widget,
327                         QGridLayout *_p_layout, int& _int, bool _pwd ) :
328      FileConfigControl( _p_this, _p_item, _p_widget, _p_layout, _int, _pwd)
329 {}
330
331 FontConfigControl::FontConfigControl( vlc_object_t *_p_this,
332                         module_config_t *_p_item, QLabel *_p_label,
333                         QLineEdit *_p_line, QPushButton *_p_button, bool _pwd ):
334      FileConfigControl( _p_this, _p_item, _p_label, _p_line, _p_button, _pwd)
335 {}
336
337 void FontConfigControl::updateField()
338 {
339     bool ok;
340     QFont font = QFontDialog::getFont( &ok, QFont( text->text() ), NULL );
341     if( !ok ) return;
342     text->setText( font.family() );
343 }
344
345 /********* String / choice list **********/
346 StringListConfigControl::StringListConfigControl( 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     combo = new QComboBox();
353     finish( bycat );
354     if( !l )
355     {
356         QHBoxLayout *layout = new QHBoxLayout();
357         layout->addWidget( label ); layout->addWidget( combo );
358         widget->setLayout( layout );
359     }
360     else
361     {
362         l->addWidget( label, line, 0 );
363         l->addWidget( combo, line, 1, Qt::AlignRight );
364     }
365 }
366 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
367                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
368                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
369 {
370     combo = _combo;
371     label = _label;
372     finish( bycat );
373 }
374
375 void StringListConfigControl::finish( bool bycat )
376 {
377     combo->setEditable( false );
378
379     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
380     {
381         combo->addItem( qfu(p_item->ppsz_list_text ?
382                             p_item->ppsz_list_text[i_index] :
383                             p_item->ppsz_list[i_index] ),
384                         QVariant( p_item->ppsz_list[i_index] ) );
385         if( p_item->value.psz && !strcmp( p_item->value.psz,
386                                           p_item->ppsz_list[i_index] ) )
387             combo->setCurrentIndex( combo->count() - 1 );
388     }
389     combo->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
390     if( label )
391         label->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
392 }
393
394 QString StringListConfigControl::getValue()
395 {
396     return combo->itemData( combo->currentIndex() ).toString();
397 }
398
399 /********* Module **********/
400 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
401                module_config_t *_p_item, QWidget *_parent, bool bycat,
402                QGridLayout *l, int &line) :
403                VStringConfigControl( _p_this, _p_item, _parent )
404 {
405     label = new QLabel( qfu(p_item->psz_text) );
406     combo = new QComboBox();
407     finish( bycat );
408     if( !l )
409     {
410         QHBoxLayout *layout = new QHBoxLayout();
411         layout->addWidget( label ); layout->addWidget( combo );
412         widget->setLayout( layout );
413     }
414     else
415     {
416         l->addWidget( label, line, 0 );
417         l->addWidget( combo, line, 1, Qt::AlignRight );
418     }
419 }
420 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
421                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
422                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
423 {
424     combo = _combo;
425     label = _label;
426     finish( bycat );
427 }
428
429 void ModuleConfigControl::finish( bool bycat )
430 {
431     vlc_list_t *p_list;
432     module_t *p_parser;
433
434     combo->setEditable( false );
435
436     /* build a list of available modules */
437     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
438     combo->addItem( qtr("Default") );
439     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
440     {
441         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
442
443         if( bycat )
444         {
445             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
446
447             for (size_t i = 0; i < p_parser->confsize; i++)
448             {
449                 module_config_t *p_config = p_parser->p_config + i;
450                 /* Hack: required subcategory is stored in i_min */
451                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
452                     p_config->value.i == p_item->min.i )
453                     combo->addItem( qfu(p_parser->psz_longname),
454                                     QVariant( p_parser->psz_object_name ) );
455                 if( p_item->value.psz && !strcmp( p_item->value.psz,
456                                                   p_parser->psz_object_name) )
457                     combo->setCurrentIndex( combo->count() - 1 );
458             }
459         }
460         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
461         {
462             combo->addItem( qfu(p_parser->psz_longname),
463                             QVariant( p_parser->psz_object_name ) );
464             if( p_item->value.psz && !strcmp( p_item->value.psz,
465                                               p_parser->psz_object_name) )
466                 combo->setCurrentIndex( combo->count() - 1 );
467         }
468     }
469     vlc_list_release( p_list );
470     combo->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
471     if( label )
472         label->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
473 }
474
475 QString ModuleConfigControl::getValue()
476 {
477     return combo->itemData( combo->currentIndex() ).toString();
478 }
479
480 /********* Module list **********/
481 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
482                module_config_t *_p_item, QWidget *_parent, bool bycat,
483                QGridLayout *l, int &line) :
484                VStringConfigControl( _p_this, _p_item, _parent )
485 {
486     groupBox = new QGroupBox ( qfu(p_item->psz_text) );
487     text = new QLineEdit();
488     QGridLayout *layoutGroupBox = new QGridLayout( groupBox );
489
490     finish( bycat );
491
492     int boxline = 0;
493     for( QVector<QCheckBox*>::iterator it = modules.begin();
494          it != modules.end(); it++ )
495     {
496         layoutGroupBox->addWidget( *it, boxline++, 0 );
497     }
498     layoutGroupBox->addWidget( text, boxline, 0 );
499
500     if( !l )
501     {
502         QVBoxLayout *layout = new QVBoxLayout();
503         layout->addWidget( groupBox, line, 0 );
504         widget->setLayout( layout );
505     }
506     else
507     {
508         l->addWidget( groupBox, line, 0, 1, -1 );
509     }
510 }
511 #if 0
512 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
513                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
514                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
515 {
516     combo = _combo;
517     label = _label;
518     finish( bycat );
519 }
520 #endif
521
522 ModuleListConfigControl::~ModuleListConfigControl()
523 {
524     for( QVector<QCheckBox*>::iterator it = modules.begin();
525          it != modules.end(); it++ )
526     {
527         delete *it;
528     }
529     delete groupBox;
530     delete text;
531 }
532
533 void ModuleListConfigControl::finish( bool bycat )
534 {
535     vlc_list_t *p_list;
536     module_t *p_parser;
537
538     /* build a list of available modules */
539     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
540     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
541     {
542         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
543
544         if( bycat )
545         {
546             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
547
548             for (size_t i = 0; i < p_parser->confsize; i++)
549             {
550                 module_config_t *p_config = p_parser->p_config + i;
551                 /* Hack: required subcategory is stored in i_min */
552                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
553                     p_config->value.i == p_item->min.i )
554                 {
555                     QCheckBox *cb =
556                         new QCheckBox( qfu( p_parser->psz_object_name ) );
557                     CONNECT( cb, stateChanged( int ), this, onUpdate( int ) );
558                     cb->setToolTip(
559                             formatTooltip( qfu(p_parser->psz_longname)) );
560                     modules.push_back( cb );
561                 }
562             }
563         }
564         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
565         {
566             QCheckBox *cb =
567                 new QCheckBox( qfu( p_parser->psz_object_name ) );
568                     CONNECT( cb, stateChanged( int ), this, onUpdate( int ) );
569             cb->setToolTip( formatTooltip(qfu(p_parser->psz_longname)) );
570             modules.push_back( cb );
571         }
572     }
573     vlc_list_release( p_list );
574     text->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
575     if( groupBox )
576         groupBox->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
577 }
578
579 QString ModuleListConfigControl::getValue()
580 {
581     return text->text();
582 }
583
584 void ModuleListConfigControl::hide()
585 {
586     for( QVector<QCheckBox*>::iterator it = modules.begin();
587          it != modules.end(); it++ )
588     {
589         (*it)->hide();
590     }
591     groupBox->hide();
592 }
593
594 void ModuleListConfigControl::show()
595 {
596     for( QVector<QCheckBox*>::iterator it = modules.begin();
597          it != modules.end(); it++ )
598     {
599         (*it)->show();
600     }
601     groupBox->show();
602 }
603
604
605 void ModuleListConfigControl::onUpdate( int value )
606 {
607     text->clear();
608     bool first = true;
609
610     for( QVector<QCheckBox*>::iterator it = modules.begin();
611          it != modules.end(); it++ )
612     {
613         if( (*it)->isChecked() )
614         {
615             if( first )
616             {
617                 text->setText( text->text() + (*it)->text() );
618                 first = false;
619             }
620             else
621             {
622                 text->setText( text->text() + ":" + (*it)->text() );
623             }
624         }
625     }
626 }
627
628 /**************************************************************************
629  * Integer-based controls
630  *************************************************************************/
631
632 /*********** Integer **************/
633 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
634                                             module_config_t *_p_item,
635                                             QWidget *_parent, QGridLayout *l,
636                                             int &line ) :
637                            VIntConfigControl( _p_this, _p_item, _parent )
638 {
639     label = new QLabel( qfu(p_item->psz_text) );
640     spin = new QSpinBox; spin->setMinimumWidth( 80 );
641     spin->setAlignment( Qt::AlignRight );
642     spin->setMaximumWidth( 90 );
643     finish();
644
645     if( !l )
646     {
647         QHBoxLayout *layout = new QHBoxLayout();
648         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
649         widget->setLayout( layout );
650     }
651     else
652     {
653         l->addWidget( label, line, 0 );
654         l->addWidget( spin, line, 1, Qt::AlignRight );
655     }
656 }
657 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
658                                             module_config_t *_p_item,
659                                             QLabel *_label, QSpinBox *_spin ) :
660                                       VIntConfigControl( _p_this, _p_item )
661 {
662     spin = _spin;
663     label = _label;
664     finish();
665 }
666
667 void IntegerConfigControl::finish()
668 {
669     spin->setMaximum( 2000000000 );
670     spin->setMinimum( -2000000000 );
671     spin->setValue( p_item->value.i );
672     spin->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
673     if( label )
674         label->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
675 }
676
677 int IntegerConfigControl::getValue()
678 {
679     return spin->value();
680 }
681
682 /********* Integer range **********/
683 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
684                                             module_config_t *_p_item,
685                                             QWidget *_parent, QGridLayout *l,
686                                             int &line ) :
687             IntegerConfigControl( _p_this, _p_item, _parent, l, line )
688 {
689     finish();
690 }
691
692 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
693                                             module_config_t *_p_item,
694                                             QLabel *_label, QSpinBox *_spin ) :
695             IntegerConfigControl( _p_this, _p_item, _label, _spin )
696 {
697     finish();
698 }
699
700 void IntegerRangeConfigControl::finish()
701 {
702     spin->setMaximum( p_item->max.i );
703     spin->setMinimum( p_item->min.i );
704 }
705
706 IntegerRangeSliderConfigControl::IntegerRangeSliderConfigControl(
707                                             vlc_object_t *_p_this,
708                                             module_config_t *_p_item,
709                                             QLabel *_label, QSlider *_slider ):
710                     VIntConfigControl( _p_this, _p_item )
711 {
712     slider = _slider;
713     label = _label;
714     slider->setMaximum( p_item->max.i );
715     slider->setMinimum( p_item->min.i );
716     slider->setValue( p_item->value.i );
717     slider->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
718     if( label )
719         label->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
720 }
721
722 int IntegerRangeSliderConfigControl::getValue()
723 {
724         return slider->value();
725 }
726
727
728 /********* Integer / choice list **********/
729 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
730                module_config_t *_p_item, QWidget *_parent, bool bycat,
731                QGridLayout *l, int &line) :
732                VIntConfigControl( _p_this, _p_item, _parent )
733 {
734     label = new QLabel( qfu(p_item->psz_text) );
735     combo = new QComboBox();
736     finish( bycat );
737     if( !l )
738     {
739         QHBoxLayout *layout = new QHBoxLayout();
740         layout->addWidget( label ); layout->addWidget( combo );
741         widget->setLayout( layout );
742     }
743     else
744     {
745         l->addWidget( label, line, 0 );
746         l->addWidget( combo, line, 1, Qt::AlignRight );
747     }
748 }
749 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
750                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
751                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
752 {
753     combo = _combo;
754     label = _label;
755     finish( bycat );
756 }
757
758 void IntegerListConfigControl::finish( bool bycat )
759 {
760     combo->setEditable( false );
761
762     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
763     {
764         combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
765                         QVariant( p_item->pi_list[i_index] ) );
766         if( p_item->value.i == p_item->pi_list[i_index] )
767             combo->setCurrentIndex( combo->count() - 1 );
768     }
769     combo->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
770     if( label )
771         label->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
772 }
773
774 int IntegerListConfigControl::getValue()
775 {
776     return combo->itemData( combo->currentIndex() ).toInt();
777 }
778
779 /*********** Boolean **************/
780 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
781                                       module_config_t *_p_item,
782                                       QWidget *_parent, QGridLayout *l,
783                                       int &line ) :
784                     VIntConfigControl( _p_this, _p_item, _parent )
785 {
786     checkbox = new QCheckBox( qfu(p_item->psz_text) );
787     finish();
788
789     if( !l )
790     {
791         QHBoxLayout *layout = new QHBoxLayout();
792         layout->addWidget( checkbox, 0 );
793         widget->setLayout( layout );
794     }
795     else
796     {
797         l->addWidget( checkbox, line, 0 );
798     }
799 }
800 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
801                                       module_config_t *_p_item,
802                                       QLabel *_label,
803                                       QCheckBox *_checkbox,
804                                       bool bycat ) :
805                    VIntConfigControl( _p_this, _p_item )
806 {
807     checkbox = _checkbox;
808     finish();
809 }
810
811 void BoolConfigControl::finish()
812 {
813     checkbox->setCheckState( p_item->value.i == VLC_TRUE ? Qt::Checked
814                                                         : Qt::Unchecked );
815     checkbox->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
816 }
817
818 int BoolConfigControl::getValue()
819 {
820     return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
821 }
822
823 /**************************************************************************
824  * Float-based controls
825  *************************************************************************/
826
827 /*********** Float **************/
828 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
829                                         module_config_t *_p_item,
830                                         QWidget *_parent, QGridLayout *l,
831                                         int &line ) :
832                     VFloatConfigControl( _p_this, _p_item, _parent )
833 {
834     label = new QLabel( qfu(p_item->psz_text) );
835     spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
836     spin->setMaximumWidth( 90 );
837     spin->setAlignment( Qt::AlignRight );
838     finish();
839
840     if( !l )
841     {
842         QHBoxLayout *layout = new QHBoxLayout();
843         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
844         widget->setLayout( layout );
845     }
846     else
847     {
848         l->addWidget( label, line, 0 );
849         l->addWidget( spin, line, 1, Qt::AlignRight );
850     }
851 }
852
853 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
854                                         module_config_t *_p_item,
855                                         QLabel *_label,
856                                         QDoubleSpinBox *_spin ) :
857                     VFloatConfigControl( _p_this, _p_item )
858 {
859     spin = _spin;
860     label = _label;
861     finish();
862 }
863
864 void FloatConfigControl::finish()
865 {
866     spin->setMaximum( 2000000000. );
867     spin->setMinimum( -2000000000. );
868     spin->setSingleStep( 0.1 );
869     spin->setValue( (double)p_item->value.f );
870     spin->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
871     if( label )
872         label->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
873 }
874
875 float FloatConfigControl::getValue()
876 {
877     return (float)spin->value();
878 }
879
880 /*********** Float with range **************/
881 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
882                                         module_config_t *_p_item,
883                                         QWidget *_parent, QGridLayout *l,
884                                         int &line ) :
885                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
886 {
887     finish();
888 }
889
890 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
891                                         module_config_t *_p_item,
892                                         QLabel *_label,
893                                         QDoubleSpinBox *_spin ) :
894                 FloatConfigControl( _p_this, _p_item, _label, _spin )
895 {
896     finish();
897 }
898
899 void FloatRangeConfigControl::finish()
900 {
901     spin->setMaximum( (double)p_item->max.f );
902     spin->setMinimum( (double)p_item->min.f );
903 }
904
905
906 /**********************************************************************
907  * Key selector widget
908  **********************************************************************/
909 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
910                                       module_config_t *_p_item,
911                                       QWidget *_parent, QGridLayout *l,
912                                       int &line ) :
913                                 ConfigControl( _p_this, _p_item, _parent )
914
915 {
916     label = new QLabel( qtr("Select an action to change the associated hotkey") );
917     table = new QTreeWidget( 0 );
918     finish();
919
920     if( !l )
921     {
922         QVBoxLayout *layout = new QVBoxLayout();
923         layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
924         widget->setLayout( layout );
925     }
926     else
927     {
928         l->addWidget( label, line, 0, 1, 2 );
929         l->addWidget( table, line+1, 0, 1,2 );
930     }
931 }
932
933 void KeySelectorControl::finish()
934 {
935     if( label )
936         label->setToolTip( formatTooltip(qfu(p_item->psz_longtext)) );
937
938     /* Fill the table */
939     table->setColumnCount( 2 );
940     table->setAlternatingRowColors( true );
941
942     module_t *p_main = config_FindModule( p_this, "main" );
943     assert( p_main );
944
945     for (size_t i = 0; i < p_main->confsize; i++)
946     {
947         module_config_t *p_item = p_main->p_config + i;
948
949         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
950             strstr( p_item->psz_name , "key-" ) )
951         {
952             QTreeWidgetItem *treeItem = new QTreeWidgetItem();
953             treeItem->setText( 0, qfu( p_item->psz_text ) );
954             treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
955             treeItem->setData( 0, Qt::UserRole,
956                                   QVariant::fromValue( (void*)p_item ) );
957             values += p_item;
958             table->addTopLevelItem( treeItem );
959         }
960     }
961     table->resizeColumnToContents( 0 );
962
963     CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
964              this, selectKey( QTreeWidgetItem * ) );
965 }
966
967 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
968 {
969    module_config_t *p_keyItem = static_cast<module_config_t*>
970                           (keyItem->data( 0, Qt::UserRole ).value<void*>());
971
972     KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text );
973     d->exec();
974     if( d->result() == QDialog::Accepted )
975     {
976         p_keyItem->value.i = d->keyValue;
977         if( d->conflicts )
978         {
979             for( int i = 0; i < table->topLevelItemCount() ; i++ )
980             {
981                 QTreeWidgetItem *it = table->topLevelItem(i);
982                 module_config_t *p_item = static_cast<module_config_t*>
983                               (it->data( 0, Qt::UserRole ).value<void*>());
984                 it->setText( 1, VLCKeyToString( p_item->value.i ) );
985             }
986         }
987         else
988             keyItem->setText( 1, VLCKeyToString( p_keyItem->value.i ) );
989     }
990     delete d;
991 }
992
993 void KeySelectorControl::doApply()
994 {
995     foreach( module_config_t *p_current, values )
996     {
997         config_PutInt( p_this, p_current->psz_name, p_current->value.i );
998     }
999 }
1000
1001 KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
1002                                 const char * _keyToChange ) :
1003                                                 QDialog(0), keyValue(0)
1004 {
1005     setModal( true );
1006     values = _values;
1007     conflicts = false;
1008     keyToChange = _keyToChange;
1009     setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange)  );
1010
1011     QVBoxLayout *l = new QVBoxLayout( this );
1012     selected = new QLabel( qtr("Press the new keys for ")  + qfu(keyToChange) );
1013     warning = new QLabel();
1014     l->addWidget( selected , Qt::AlignCenter );
1015     l->addWidget( warning, Qt::AlignCenter );
1016
1017     QHBoxLayout *l2 = new QHBoxLayout();
1018     QPushButton *ok = new QPushButton( qtr("OK") );
1019     l2->addWidget( ok );
1020     QPushButton *cancel = new QPushButton( qtr("Cancel") );
1021     l2->addWidget( cancel );
1022
1023     BUTTONACT( ok, accept() );
1024     BUTTONACT( cancel, reject() );
1025
1026     l->addLayout( l2 );
1027 }
1028
1029 void KeyInputDialog::checkForConflicts( int i_vlckey )
1030 {
1031     conflicts = false;
1032     module_config_t *p_current = NULL;
1033     foreach( p_current, values )
1034     {
1035         if( p_current->value.i == i_vlckey && strcmp( p_current->psz_text,
1036                                                     keyToChange ) )
1037         {
1038             p_current->value.i = 0;
1039             conflicts = true;
1040             break;
1041         }
1042     }
1043     if( conflicts )
1044     {
1045         warning->setText(
1046           qtr("Warning: the  key is already assigned to \"") +
1047           QString( p_current->psz_text ) + "\"" );
1048     }
1049     else warning->setText( "" );
1050 }
1051
1052 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
1053 {
1054     if( e->key() == Qt::Key_Tab ) return;
1055     int i_vlck = qtEventToVLCKey( e );
1056     selected->setText( VLCKeyToString( i_vlck ) );
1057     checkForConflicts( i_vlck );
1058     keyValue = i_vlck;
1059 }
1060
1061 void KeyInputDialog::wheelEvent( QWheelEvent *e )
1062 {
1063     int i_vlck = qtWheelEventToVLCKey( e );
1064     selected->setText( VLCKeyToString( i_vlck ) );
1065     checkForConflicts( i_vlck );
1066     keyValue = i_vlck;
1067 }