]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Qt4- SImple Preferences. Add file and directory configuration.
[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         text->setText( QFileDialog::getOpenFileName( NULL, 
262                 qtr( "Select File" ), qfu( p_this->p_libvlc->psz_homedir ) ) );
263 }
264
265 void FileConfigControl::finish()
266 {
267     text->setText( qfu(p_item->value.psz) );
268     text->setToolTip( qfu(p_item->psz_longtext) );
269     if( label )
270         label->setToolTip( qfu(p_item->psz_longtext) );
271 }
272
273 /********* String / Directory **********/
274
275 DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
276                         module_config_t *_p_item, QWidget *_p_widget,
277                         QGridLayout *_p_layout, int& _int, bool _pwd ) :
278      FileConfigControl( _p_this, _p_item, _p_widget, _p_layout, _int, _pwd)
279 {}
280
281 DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
282                         module_config_t *_p_item, QLabel *_p_label,
283                         QLineEdit *_p_line, QPushButton *_p_button, bool _pwd ):
284      FileConfigControl( _p_this, _p_item, _p_label, _p_line, _p_button, _pwd)
285 {}
286
287
288 void DirectoryConfigControl::updateField()
289 {
290     text->setText( QFileDialog::getOpenFileName( NULL,
291                 qtr( "Select File" ), qfu( p_this->p_libvlc->psz_homedir ),
292                 NULL, 0,  QFileDialog::ShowDirsOnly ) );
293 }
294
295 /********* String / choice list **********/
296 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
297                module_config_t *_p_item, QWidget *_parent, bool bycat,
298                QGridLayout *l, int &line) :
299                VStringConfigControl( _p_this, _p_item, _parent )
300 {
301     label = new QLabel( qfu(p_item->psz_text) );
302     combo = new QComboBox();
303     finish( bycat );
304     if( !l )
305     {
306         QHBoxLayout *layout = new QHBoxLayout();
307         layout->addWidget( label ); layout->addWidget( combo );
308         widget->setLayout( layout );
309     }
310     else
311     {
312         l->addWidget( label, line, 0 );
313         l->addWidget( combo, line, 1, Qt::AlignRight );
314     }
315 }
316 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
317                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
318                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
319 {
320     combo = _combo;
321     label = _label;
322     finish( bycat );
323 }
324
325 void StringListConfigControl::finish( bool bycat )
326 {
327     combo->setEditable( false );
328
329     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
330     {
331         combo->addItem( qfu(p_item->ppsz_list_text ?
332                             p_item->ppsz_list_text[i_index] :
333                             p_item->ppsz_list[i_index] ),
334                         QVariant( p_item->ppsz_list[i_index] ) );
335         if( p_item->value.psz && !strcmp( p_item->value.psz,
336                                           p_item->ppsz_list[i_index] ) )
337             combo->setCurrentIndex( combo->count() - 1 );
338     }
339     combo->setToolTip( qfu(p_item->psz_longtext) );
340     if( label )
341         label->setToolTip( qfu(p_item->psz_longtext) );
342 }
343
344 QString StringListConfigControl::getValue()
345 {
346     return combo->itemData( combo->currentIndex() ).toString();
347 }
348
349 /********* Module **********/
350 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
351                module_config_t *_p_item, QWidget *_parent, bool bycat,
352                QGridLayout *l, int &line) :
353                VStringConfigControl( _p_this, _p_item, _parent )
354 {
355     label = new QLabel( qfu(p_item->psz_text) );
356     combo = new QComboBox();
357     finish( bycat );
358     if( !l )
359     {
360         QHBoxLayout *layout = new QHBoxLayout();
361         layout->addWidget( label ); layout->addWidget( combo );
362         widget->setLayout( layout );
363     }
364     else
365     {
366         l->addWidget( label, line, 0 );
367         l->addWidget( combo, line, 1, Qt::AlignRight );
368     }
369 }
370 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
371                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
372                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
373 {
374     combo = _combo;
375     label = _label;
376     finish( bycat );
377 }
378
379 void ModuleConfigControl::finish( bool bycat )
380 {
381     vlc_list_t *p_list;
382     module_t *p_parser;
383
384     combo->setEditable( false );
385
386     /* build a list of available modules */
387     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
388     combo->addItem( qtr("Default") );
389     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
390     {
391         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
392
393         if( bycat )
394         {
395             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
396
397             for (size_t i = 0; i < p_parser->confsize; i++)
398             {
399                 module_config_t *p_config = p_parser->p_config + i;
400                 /* Hack: required subcategory is stored in i_min */
401                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
402                     p_config->value.i == p_item->min.i )
403                     combo->addItem( qfu(p_parser->psz_longname),
404                                     QVariant( p_parser->psz_object_name ) );
405                 if( p_item->value.psz && !strcmp( p_item->value.psz,
406                                                   p_parser->psz_object_name) )
407                     combo->setCurrentIndex( combo->count() - 1 );
408             }
409         }
410         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
411         {
412             combo->addItem( qfu(p_parser->psz_longname),
413                             QVariant( p_parser->psz_object_name ) );
414             if( p_item->value.psz && !strcmp( p_item->value.psz,
415                                               p_parser->psz_object_name) )
416                 combo->setCurrentIndex( combo->count() - 1 );
417         }
418     }
419     vlc_list_release( p_list );
420     combo->setToolTip( qfu(p_item->psz_longtext) );
421     if( label )
422         label->setToolTip( qfu(p_item->psz_longtext) );
423 }
424
425 QString ModuleConfigControl::getValue()
426 {
427     return combo->itemData( combo->currentIndex() ).toString();
428 }
429
430 /********* Module list **********/
431 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
432                module_config_t *_p_item, QWidget *_parent, bool bycat,
433                QGridLayout *l, int &line) :
434                VStringConfigControl( _p_this, _p_item, _parent )
435 {
436     label = new QLabel( qfu(p_item->psz_text) );
437     text = new QLineEdit();
438     finish( bycat );
439
440     bool pom = false;
441     if( !l )
442     {
443         l = new QGridLayout();
444         line = 0;
445         pom = true;
446     }
447     for( QVector<QCheckBox*>::iterator it = modules.begin();
448          it != modules.end(); it++ )
449     {
450         l->addWidget( *it, line++, 1 );
451     }
452     l->addWidget( label, line, 0 );
453     l->addWidget( text, line, 1 );
454     if( pom )
455         widget->setLayout( l );
456 }
457 #if 0
458 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
459                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
460                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
461 {
462     combo = _combo;
463     label = _label;
464     finish( bycat );
465 }
466 #endif
467
468 ModuleListConfigControl::~ModuleListConfigControl()
469 {
470     for( QVector<QCheckBox*>::iterator it = modules.begin();
471          it != modules.end(); it++ )
472     {
473         delete *it;
474     }
475     delete label;
476     delete text;
477 }
478
479 void ModuleListConfigControl::finish( bool bycat )
480 {
481     vlc_list_t *p_list;
482     module_t *p_parser;
483
484     /* build a list of available modules */
485     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
486     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
487     {
488         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
489
490         if( bycat )
491         {
492             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
493
494             for (size_t i = 0; i < p_parser->confsize; i++)
495             {
496                 module_config_t *p_config = p_parser->p_config + i;
497                 /* Hack: required subcategory is stored in i_min */
498                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
499                     p_config->value.i == p_item->min.i )
500                 {
501                     QCheckBox *cb =
502                         new QCheckBox( qfu( p_parser->psz_object_name ) );
503                     cb->setToolTip( qfu(p_parser->psz_longname) );
504                     modules.push_back( cb );
505                 }
506             }
507         }
508         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
509         {
510             QCheckBox *cb =
511                 new QCheckBox( qfu( p_parser->psz_object_name ) );
512             cb->setToolTip( qfu(p_parser->psz_longname) );
513             modules.push_back( cb );
514         }
515     }
516     vlc_list_release( p_list );
517     text->setToolTip( qfu(p_item->psz_longtext) );
518     if( label )
519         label->setToolTip( qfu(p_item->psz_longtext) );
520 }
521
522 QString ModuleListConfigControl::getValue()
523 {
524     return text->text();
525 }
526
527 void ModuleListConfigControl::hide()
528 {
529     for( QVector<QCheckBox*>::iterator it = modules.begin();
530          it != modules.end(); it++ )
531     {
532         (*it)->hide();
533     }
534     text->hide();
535     label->hide();
536 }
537
538 void ModuleListConfigControl::show()
539 {
540     for( QVector<QCheckBox*>::iterator it = modules.begin();
541          it != modules.end(); it++ )
542     {
543         (*it)->show();
544     }
545     text->show();
546     label->show();
547 }
548
549
550 void ModuleListConfigControl::wakeUp_TheUserJustClickedOnSomething( int value )
551 {
552     text->clear();
553     for( QVector<QCheckBox*>::iterator it = modules.begin();
554          it != modules.end(); it++ )
555     {
556     }
557 }
558
559 /**************************************************************************
560  * Integer-based controls
561  *************************************************************************/
562
563 /*********** Integer **************/
564 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
565                                             module_config_t *_p_item,
566                                             QWidget *_parent, QGridLayout *l,
567                                             int &line ) :
568                            VIntConfigControl( _p_this, _p_item, _parent )
569 {
570     label = new QLabel( qfu(p_item->psz_text) );
571     spin = new QSpinBox; spin->setMinimumWidth( 80 );
572     spin->setMaximumWidth( 90 );
573     finish();
574
575     if( !l )
576     {
577         QHBoxLayout *layout = new QHBoxLayout();
578         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
579         widget->setLayout( layout );
580     }
581     else
582     {
583         l->addWidget( label, line, 0 );
584         l->addWidget( spin, line, 1, Qt::AlignRight );
585     }
586 }
587 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
588                                             module_config_t *_p_item,
589                                             QLabel *_label, QSpinBox *_spin ) :
590                                       VIntConfigControl( _p_this, _p_item )
591 {
592     spin = _spin;
593     label = _label;
594     finish();
595 }
596
597 void IntegerConfigControl::finish()
598 {
599     spin->setMaximum( 2000000000 );
600     spin->setMinimum( -2000000000 );
601     spin->setValue( p_item->value.i );
602     spin->setToolTip( qfu(p_item->psz_longtext) );
603     if( label )
604         label->setToolTip( qfu(p_item->psz_longtext) );
605 }
606
607 int IntegerConfigControl::getValue()
608 {
609     return spin->value();
610 }
611
612 /********* Integer range **********/
613 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
614                                             module_config_t *_p_item,
615                                             QWidget *_parent, QGridLayout *l,
616                                             int &line ) :
617             IntegerConfigControl( _p_this, _p_item, _parent, l, line )
618 {
619     finish();
620 }
621
622 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
623                                             module_config_t *_p_item,
624                                             QLabel *_label, QSpinBox *_spin ) :
625             IntegerConfigControl( _p_this, _p_item, _label, _spin )
626 {
627     finish();
628 }
629
630 void IntegerRangeConfigControl::finish()
631 {
632     spin->setMaximum( p_item->max.i );
633     spin->setMinimum( p_item->min.i );
634 }
635
636 IntegerRangeSliderConfigControl::IntegerRangeSliderConfigControl(
637                                             vlc_object_t *_p_this,
638                                             module_config_t *_p_item,
639                                             QLabel *_label, QSlider *_slider ):
640                     VIntConfigControl( _p_this, _p_item )
641 {
642     slider = _slider;
643     label = _label;
644     slider->setMaximum( p_item->max.i );
645     slider->setMinimum( p_item->min.i );
646     slider->setValue( p_item->value.i );
647     slider->setToolTip( qfu(p_item->psz_longtext) );
648     if( label )
649         label->setToolTip( qfu(p_item->psz_longtext) );
650 }
651
652 int IntegerRangeSliderConfigControl::getValue()
653 {
654         return slider->value();
655 }
656
657
658 /********* Integer / choice list **********/
659 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
660                module_config_t *_p_item, QWidget *_parent, bool bycat,
661                QGridLayout *l, int &line) :
662                VIntConfigControl( _p_this, _p_item, _parent )
663 {
664     label = new QLabel( qfu(p_item->psz_text) );
665     combo = new QComboBox();
666     finish( bycat );
667     if( !l )
668     {
669         QHBoxLayout *layout = new QHBoxLayout();
670         layout->addWidget( label ); layout->addWidget( combo );
671         widget->setLayout( layout );
672     }
673     else
674     {
675         l->addWidget( label, line, 0 );
676         l->addWidget( combo, line, 1, Qt::AlignRight );
677     }
678 }
679 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
680                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
681                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
682 {
683     combo = _combo;
684     label = _label;
685     finish( bycat );
686 }
687
688 void IntegerListConfigControl::finish( bool bycat )
689 {
690     combo->setEditable( false );
691
692     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
693     {
694         combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
695                         QVariant( p_item->pi_list[i_index] ) );
696         if( p_item->value.i == p_item->pi_list[i_index] )
697             combo->setCurrentIndex( combo->count() - 1 );
698     }
699     combo->setToolTip( qfu(p_item->psz_longtext) );
700     if( label )
701         label->setToolTip( qfu(p_item->psz_longtext) );
702 }
703
704 int IntegerListConfigControl::getValue()
705 {
706     return combo->itemData( combo->currentIndex() ).toInt();
707 }
708
709 /*********** Boolean **************/
710 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
711                                       module_config_t *_p_item,
712                                       QWidget *_parent, QGridLayout *l,
713                                       int &line ) :
714                     VIntConfigControl( _p_this, _p_item, _parent )
715 {
716     checkbox = new QCheckBox( qfu(p_item->psz_text) );
717     finish();
718
719     if( !l )
720     {
721         QHBoxLayout *layout = new QHBoxLayout();
722         layout->addWidget( checkbox, 0 );
723         widget->setLayout( layout );
724     }
725     else
726     {
727         l->addWidget( checkbox, line, 0 );
728     }
729 }
730 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
731                                       module_config_t *_p_item,
732                                       QLabel *_label,
733                                       QCheckBox *_checkbox,
734                                       bool bycat ) :
735                    VIntConfigControl( _p_this, _p_item )
736 {
737     checkbox = _checkbox;
738     finish();
739 }
740
741 void BoolConfigControl::finish()
742 {
743     checkbox->setCheckState( p_item->value.i == VLC_TRUE ? Qt::Checked
744                                                         : Qt::Unchecked );
745     checkbox->setToolTip( qfu(p_item->psz_longtext) );
746 }
747
748 int BoolConfigControl::getValue()
749 {
750     return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
751 }
752
753 /**************************************************************************
754  * Float-based controls
755  *************************************************************************/
756
757 /*********** Float **************/
758 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
759                                         module_config_t *_p_item,
760                                         QWidget *_parent, QGridLayout *l,
761                                         int &line ) :
762                     VFloatConfigControl( _p_this, _p_item, _parent )
763 {
764     label = new QLabel( qfu(p_item->psz_text) );
765     spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
766     spin->setMaximumWidth( 90 );
767     finish();
768
769     if( !l )
770     {
771         QHBoxLayout *layout = new QHBoxLayout();
772         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
773         widget->setLayout( layout );
774     }
775     else
776     {
777         l->addWidget( label, line, 0 );
778         l->addWidget( spin, line, 1, Qt::AlignRight );
779     }
780 }
781
782 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
783                                         module_config_t *_p_item,
784                                         QLabel *_label,
785                                         QDoubleSpinBox *_spin ) :
786                     VFloatConfigControl( _p_this, _p_item )
787 {
788     spin = _spin;
789     label = _label;
790     finish();
791 }
792
793 void FloatConfigControl::finish()
794 {
795     spin->setMaximum( 2000000000. );
796     spin->setMinimum( -2000000000. );
797     spin->setSingleStep( 0.1 );
798     spin->setValue( (double)p_item->value.f );
799     spin->setToolTip( qfu(p_item->psz_longtext) );
800     if( label )
801         label->setToolTip( qfu(p_item->psz_longtext) );
802 }
803
804 float FloatConfigControl::getValue()
805 {
806     return (float)spin->value();
807 }
808
809 /*********** Float with range **************/
810 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
811                                         module_config_t *_p_item,
812                                         QWidget *_parent, QGridLayout *l,
813                                         int &line ) :
814                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
815 {
816     finish();
817 }
818
819 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
820                                         module_config_t *_p_item,
821                                         QLabel *_label,
822                                         QDoubleSpinBox *_spin ) :
823                 FloatConfigControl( _p_this, _p_item, _label, _spin )
824 {
825     finish();
826 }
827
828 void FloatRangeConfigControl::finish()
829 {
830     spin->setMaximum( (double)p_item->max.f );
831     spin->setMinimum( (double)p_item->min.f );
832 }
833
834
835 /**********************************************************************
836  * Key selector widget
837  **********************************************************************/
838 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
839                                       module_config_t *_p_item,
840                                       QWidget *_parent, QGridLayout *l,
841                                       int &line ) :
842                                 ConfigControl( _p_this, _p_item, _parent )
843
844 {
845     label = new QLabel( qtr("Select an action to change the associated hotkey") );
846     table = new QTreeWidget( 0 );
847     finish();
848
849     if( !l )
850     {
851         QVBoxLayout *layout = new QVBoxLayout();
852         layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
853         widget->setLayout( layout );
854     }
855     else
856     {
857         l->addWidget( label, line, 0, 1, 2 );
858         l->addWidget( table, line+1, 0, 1,2 );
859     }
860 }
861
862 void KeySelectorControl::finish()
863 {
864     if( label )
865         label->setToolTip( qfu(p_item->psz_longtext) );
866
867     /* Fill the table */
868     table->setColumnCount( 2 );
869     table->setAlternatingRowColors( true );
870
871     module_t *p_main = config_FindModule( p_this, "main" );
872     assert( p_main );
873
874     for (size_t i = 0; i < p_main->confsize; i++)
875     {
876         module_config_t *p_item = p_main->p_config + i;
877
878         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
879             strstr( p_item->psz_name , "key-" ) )
880         {
881             QTreeWidgetItem *treeItem = new QTreeWidgetItem();
882             treeItem->setText( 0, qfu( p_item->psz_text ) );
883             treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
884             treeItem->setData( 0, Qt::UserRole,
885                                   QVariant::fromValue( (void*)p_item ) );
886             values += p_item;
887             table->addTopLevelItem( treeItem );
888         }
889     }
890     table->resizeColumnToContents( 0 );
891
892     CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
893              this, selectKey( QTreeWidgetItem * ) );
894 }
895
896 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
897 {
898    module_config_t *p_keyItem = static_cast<module_config_t*>
899                           (keyItem->data( 0, Qt::UserRole ).value<void*>());
900
901     KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text );
902     d->exec();
903     if( d->result() == QDialog::Accepted )
904     {
905         p_keyItem->value.i = d->keyValue;
906         if( d->conflicts )
907         {
908             for( int i = 0; i < table->topLevelItemCount() ; i++ )
909             {
910                 QTreeWidgetItem *it = table->topLevelItem(i);
911                 module_config_t *p_item = static_cast<module_config_t*>
912                               (it->data( 0, Qt::UserRole ).value<void*>());
913                 it->setText( 1, VLCKeyToString( p_item->value.i ) );
914             }
915         }
916         else
917             keyItem->setText( 1, VLCKeyToString( p_keyItem->value.i ) );
918     }
919     delete d;
920 }
921
922 void KeySelectorControl::doApply()
923 {
924     foreach( module_config_t *p_current, values )
925     {
926         config_PutInt( p_this, p_current->psz_name, p_current->value.i );
927     }
928 }
929
930 KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
931                                 const char * _keyToChange ) :
932                                                 QDialog(0), keyValue(0)
933 {
934     setModal( true );
935     values = _values;
936     conflicts = false;
937     keyToChange = _keyToChange;
938     setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange)  );
939
940     QVBoxLayout *l = new QVBoxLayout( this );
941     selected = new QLabel( qtr("Press the new keys for ")  + qfu(keyToChange) );
942     warning = new QLabel();
943     l->addWidget( selected , Qt::AlignCenter );
944     l->addWidget( warning, Qt::AlignCenter );
945
946     QHBoxLayout *l2 = new QHBoxLayout();
947     QPushButton *ok = new QPushButton( qtr("OK") );
948     l2->addWidget( ok );
949     QPushButton *cancel = new QPushButton( qtr("Cancel") );
950     l2->addWidget( cancel );
951
952     BUTTONACT( ok, accept() );
953     BUTTONACT( cancel, reject() );
954
955     l->addLayout( l2 );
956 }
957
958 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
959 {
960     if( e->key() == Qt::Key_Tab ) return;
961     int i_vlck = qtEventToVLCKey( e );
962     selected->setText( VLCKeyToString( i_vlck ) );
963     conflicts = false;
964     module_config_t *p_current = NULL;
965     foreach( p_current, values )
966     {
967         if( p_current->value.i == i_vlck && strcmp( p_current->psz_text,
968                                                     keyToChange ) )
969         {
970             p_current->value.i = 0;
971             conflicts = true;
972             break;
973         }
974     }
975     if( conflicts )
976     {
977         warning->setText(
978           qtr("Warning: the  key is already assigned to \"") +
979           QString( p_current->psz_text ) + "\"" );
980     }
981     else warning->setText( "" );
982     keyValue = i_vlck;
983 }