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