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