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