]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
ae61dc479bc06fcd5fb4f57be74557354e0f5a8f
[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
34 #include "components/preferences_widgets.hpp"
35 #include "util/customwidgets.hpp"
36
37 #include <vlc_keys.h>
38
39 #include <QString>
40 #include <QVariant>
41 #include <QGridLayout>
42 #include <QSlider>
43 #include <QFileDialog>
44 #include <QFontDialog>
45 #include <QGroupBox>
46 #include <QTreeWidgetItem>
47 #include <QSignalMapper>
48 #include <QDialogButtonBox>
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
78     switch( p_item->i_type )
79     {
80     case CONFIG_ITEM_MODULE:
81         p_control = new ModuleConfigControl( p_this, p_item, parent, false,
82                                              l, line );
83         break;
84     case CONFIG_ITEM_MODULE_CAT:
85         p_control = new ModuleConfigControl( p_this, p_item, parent, true,
86                                              l, line );
87         break;
88     case CONFIG_ITEM_MODULE_LIST:
89         p_control = new ModuleListConfigControl( p_this, p_item, parent, false,
90                                              l, line );
91         break;
92     case CONFIG_ITEM_MODULE_LIST_CAT:
93         p_control = new ModuleListConfigControl( p_this, p_item, parent, true,
94                                              l, line );
95         break;
96     case CONFIG_ITEM_STRING:
97         if( !p_item->i_list )
98             p_control = new StringConfigControl( p_this, p_item, parent,
99                                                  l, line, false );
100         else
101             p_control = new StringListConfigControl( p_this, p_item,
102                                             parent, false, l, line );
103         break;
104     case CONFIG_ITEM_PASSWORD:
105         if( !p_item->i_list )
106             p_control = new StringConfigControl( p_this, p_item, parent,
107                                                  l, line, true );
108         else
109             p_control = new StringListConfigControl( p_this, p_item,
110                                             parent, true, l, line );
111         break;
112     case CONFIG_ITEM_INTEGER:
113         if( p_item->i_list )
114             p_control = new IntegerListConfigControl( p_this, p_item,
115                                             parent, false, l, line );
116         else if( p_item->min.i || p_item->max.i )
117             p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
118                                                        l, line );
119         else
120             p_control = new IntegerConfigControl( p_this, p_item, parent,
121                                                   l, line );
122         break;
123     case CONFIG_ITEM_FILE:
124         p_control = new FileConfigControl( p_this, p_item, parent, l,
125                                                 line, false );
126         break;
127     case CONFIG_ITEM_DIRECTORY:
128         p_control = new DirectoryConfigControl( p_this, p_item, parent, l,
129                                                 line, false );
130         break;
131     case CONFIG_ITEM_FONT:
132         p_control = new FontConfigControl( p_this, p_item, parent, l,
133                                            line, false );
134         break;
135     case CONFIG_ITEM_KEY:
136         p_control = new KeySelectorControl( p_this, p_item, parent, l, line );
137         break;
138     case CONFIG_ITEM_BOOL:
139         p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
140         break;
141     case CONFIG_ITEM_FLOAT:
142         if( p_item->min.f || p_item->max.f )
143             p_control = new FloatRangeConfigControl( p_this, p_item, parent,
144                                                      l, line );
145         else
146             p_control = new FloatConfigControl( p_this, p_item, parent,
147                                                   l, line );
148         break;
149     default:
150         break;
151     }
152     return p_control;
153 }
154
155 void ConfigControl::doApply( intf_thread_t *p_intf )
156 {
157     switch( getType() )
158     {
159         case 1:
160         {
161             VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(this);
162             assert( vicc );
163             config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
164             break;
165         }
166         case 2:
167         {
168             VFloatConfigControl *vfcc =
169                                     qobject_cast<VFloatConfigControl *>(this);
170             assert( vfcc );
171             config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
172             break;
173         }
174         case 3:
175         {
176             VStringConfigControl *vscc =
177                             qobject_cast<VStringConfigControl *>(this);
178             assert( vscc );
179             config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
180             break;
181         }
182         case 4:
183         {
184             KeySelectorControl *ksc = qobject_cast<KeySelectorControl *>(this);
185             assert( ksc );
186             ksc->doApply();
187         }
188     }
189 }
190
191 /**************************************************************************
192  * String-based controls
193  *************************************************************************/
194
195 /*********** String **************/
196 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
197                                           module_config_t *_p_item,
198                                           QWidget *_parent, QGridLayout *l,
199                                           int &line, bool pwd ) :
200                            VStringConfigControl( _p_this, _p_item, _parent )
201 {
202     label = new QLabel( qtr(p_item->psz_text) );
203     text = new QLineEdit( qfu(p_item->value.psz) );
204     if( pwd ) text->setEchoMode( QLineEdit::Password );
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     combo->setMinimumWidth( 80 );
357     combo->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
358     finish( bycat );
359     if( !l )
360     {
361         QHBoxLayout *layout = new QHBoxLayout();
362         layout->addWidget( label ); layout->addWidget( combo );
363         widget->setLayout( layout );
364     }
365     else
366     {
367         l->addWidget( label, line, 0 );
368         l->addWidget( combo, line, 1, Qt::AlignRight );
369     }
370
371     if( p_item->i_action )
372     {
373         QSignalMapper *signalMapper = new QSignalMapper(this);
374
375         /* Some stringLists like Capture listings have action associated */
376         for( int i = 0; i < p_item->i_action; i++ )
377         {
378             QPushButton *button =
379                 new QPushButton( qfu( p_item->ppsz_action_text[i] ));
380             CONNECT( button, clicked(), signalMapper, map() );
381             signalMapper->setMapping( button, i );
382             l->addWidget( button, line, 2 + i, Qt::AlignRight );
383         }
384         CONNECT( signalMapper, mapped( int ),
385                 this, actionRequested( int ) );
386     }
387 }
388
389 void StringListConfigControl::actionRequested( int i_action )
390 {
391     /* Supplementary check for boundaries */
392     if( i_action < 0 || i_action >= p_item->i_action ) return;
393
394     vlc_value_t val;
395     val.psz_string =
396         qtu( (combo->itemData( combo->currentIndex() ).toString() ) );
397
398     p_item->ppf_action[i_action]( p_this, getName(), val, val, 0 );
399
400     if( p_item->b_dirty )
401     {
402         combo->clear();
403         finish( true );
404         p_item->b_dirty = VLC_FALSE;
405     }
406 }
407 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
408                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
409                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
410 {
411     combo = _combo;
412     label = _label;
413     finish( bycat );
414 }
415
416 void StringListConfigControl::finish( bool bycat )
417 {
418     combo->setEditable( false );
419
420     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
421     {
422         combo->addItem( qfu(p_item->ppsz_list_text ?
423                             p_item->ppsz_list_text[i_index] :
424                             p_item->ppsz_list[i_index] ),
425                         QVariant( p_item->ppsz_list[i_index] ) );
426         if( p_item->value.psz && !strcmp( p_item->value.psz,
427                                           p_item->ppsz_list[i_index] ) )
428             combo->setCurrentIndex( combo->count() - 1 );
429     }
430     combo->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
431     if( label )
432         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
433 }
434
435 QString StringListConfigControl::getValue()
436 {
437     return combo->itemData( combo->currentIndex() ).toString();
438 }
439
440 void setfillVLCConfigCombo( const char *configname, intf_thread_t *p_intf,
441                         QComboBox *combo, QWidget *parent )
442 {
443     module_config_t *p_config =
444                       config_FindConfig( VLC_OBJECT(p_intf), configname );
445     if( p_config )
446     {
447         for ( int i_index = 0; i_index < p_config->i_list; i_index++ )
448         {
449             combo->addItem( qfu( p_config->ppsz_list_text[i_index] ),
450                     QVariant( p_config->pi_list[i_index] ) );
451             if( p_config->value.i == p_config->pi_list[i_index] )
452             {
453                 combo->setCurrentIndex( i_index );
454             }
455         }
456         combo->setToolTip( qfu( p_config->psz_longtext ) );
457     }
458 }
459
460 /********* Module **********/
461 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
462                module_config_t *_p_item, QWidget *_parent, bool bycat,
463                QGridLayout *l, int &line) :
464                VStringConfigControl( _p_this, _p_item, _parent )
465 {
466     label = new QLabel( qtr(p_item->psz_text) );
467     combo = new QComboBox();
468     combo->setMinimumWidth( 80 );
469     finish( bycat );
470     if( !l )
471     {
472         QHBoxLayout *layout = new QHBoxLayout();
473         layout->addWidget( label ); layout->addWidget( combo );
474         widget->setLayout( layout );
475     }
476     else
477     {
478         l->addWidget( label, line, 0 );
479         l->addWidget( combo, line, 1, Qt::AlignRight );
480     }
481 }
482
483 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
484                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
485                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
486 {
487     combo = _combo;
488     label = _label;
489     finish( bycat );
490 }
491
492 void ModuleConfigControl::finish( bool bycat )
493 {
494     vlc_list_t *p_list;
495     module_t *p_parser;
496
497     combo->setEditable( false );
498
499     /* build a list of available modules */
500     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
501     combo->addItem( qtr("Default") );
502     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
503     {
504         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
505
506         if( bycat )
507         {
508             if( !strcmp( module_GetObjName( p_parser ), "main" ) ) continue;
509
510             unsigned confsize;
511             module_config_t *p_config;
512
513             p_config = module_GetConfig (p_parser, &confsize);
514              for (size_t i = 0; i < confsize; i++)
515             {
516                 /* Hack: required subcategory is stored in i_min */
517                 const module_config_t *p_cfg = p_config + i;
518                 if( p_cfg->i_type == CONFIG_SUBCATEGORY &&
519                     p_cfg->value.i == p_item->min.i )
520                     combo->addItem( qtr( module_GetLongName( p_parser )),
521                                     QVariant( module_GetObjName( p_parser ) ) );
522                 if( p_item->value.psz && !strcmp( p_item->value.psz,
523                                                   module_GetObjName( p_parser ) ) )
524                     combo->setCurrentIndex( combo->count() - 1 );
525             }
526             module_PutConfig (p_config);
527         }
528         else if( module_IsCapable( p_parser, p_item->psz_type ) )
529         {
530             combo->addItem( qtr(module_GetLongName( p_parser ) ),
531                             QVariant( module_GetObjName( p_parser ) ) );
532             if( p_item->value.psz && !strcmp( p_item->value.psz,
533                                               module_GetObjName( p_parser ) ) )
534                 combo->setCurrentIndex( combo->count() - 1 );
535         }
536     }
537     vlc_list_release( p_list );
538     combo->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
539     if( label )
540         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
541 }
542
543 QString ModuleConfigControl::getValue()
544 {
545     return combo->itemData( combo->currentIndex() ).toString();
546 }
547
548 /********* Module list **********/
549 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
550         module_config_t *_p_item, QWidget *_parent, bool bycat,
551         QGridLayout *l, int &line) :
552     VStringConfigControl( _p_this, _p_item, _parent )
553 {
554     groupBox = new QGroupBox ( qtr(p_item->psz_text) );
555     text = new QLineEdit();
556     QGridLayout *layoutGroupBox = new QGridLayout( groupBox );
557
558     finish( bycat );
559
560     int boxline = 0;
561     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
562             it != modules.end(); it++ )
563     {
564         layoutGroupBox->addWidget( (*it)->checkBox, boxline++, 0 );
565     }
566     layoutGroupBox->addWidget( text, boxline, 0 );
567
568     if( !l )
569     {
570         QVBoxLayout *layout = new QVBoxLayout();
571         layout->addWidget( groupBox, line, 0 );
572         widget->setLayout( layout );
573     }
574     else
575     {
576         l->addWidget( groupBox, line, 0, 1, -1 );
577     }
578
579     text->setToolTip( formatTooltip( qtr( p_item->psz_longtext) ) );
580 }
581
582 ModuleListConfigControl::~ModuleListConfigControl()
583 {
584     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
585             it != modules.end(); it++ )
586     {
587         delete *it;
588     }
589     delete groupBox;
590     delete text;
591 }
592
593 #define CHECKBOX_LISTS \
594 { \
595        QCheckBox *cb = new QCheckBox( qtr( module_GetLongName( p_parser ) ) );\
596        checkBoxListItem *cbl = new checkBoxListItem; \
597 \
598        CONNECT( cb, stateChanged( int ), this, onUpdate( int ) );\
599        cb->setToolTip( formatTooltip( qtr( module_GetLongName( p_parser ))));\
600        cbl->checkBox = cb; \
601 \
602        cbl->psz_module = strdup( module_GetObjName( p_parser ) ); \
603        modules.push_back( cbl ); \
604 }
605
606
607 void ModuleListConfigControl::finish( bool bycat )
608 {
609     vlc_list_t *p_list;
610     module_t *p_parser;
611
612     /* build a list of available modules */
613     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
614     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
615     {
616         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
617
618         if( bycat )
619         {
620             if( !strcmp( module_GetObjName( p_parser ), "main" ) ) continue;
621
622             unsigned confsize;
623             module_config_t *p_config = module_GetConfig (p_parser, &confsize);
624
625             for (size_t i = 0; i < confsize; i++)
626             {
627                 module_config_t *p_cfg = p_config + i;
628                 /* Hack: required subcategory is stored in i_min */
629                 if( p_cfg->i_type == CONFIG_SUBCATEGORY &&
630                         p_cfg->value.i == p_item->min.i )
631                 {
632                     CHECKBOX_LISTS;
633                 }
634             }
635             module_PutConfig (p_config);
636         }
637         else if( module_IsCapable( p_parser, p_item->psz_type ) )
638         {
639             CHECKBOX_LISTS;
640         }
641     }
642     vlc_list_release( p_list );
643     text->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
644     if( groupBox )
645         groupBox->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
646 }
647 #undef CHECKBOX_LISTS
648
649 QString ModuleListConfigControl::getValue()
650 {
651     return text->text();
652 }
653
654 void ModuleListConfigControl::hide()
655 {
656     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
657          it != modules.end(); it++ )
658     {
659         (*it)->checkBox->hide();
660     }
661     groupBox->hide();
662 }
663
664 void ModuleListConfigControl::show()
665 {
666     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
667          it != modules.end(); it++ )
668     {
669         (*it)->checkBox->show();
670     }
671     groupBox->show();
672 }
673
674
675 void ModuleListConfigControl::onUpdate( int value )
676 {
677     text->clear();
678     bool first = true;
679
680     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
681          it != modules.end(); it++ )
682     {
683         if( (*it)->checkBox->isChecked() )
684         {
685             if( first )
686             {
687                 text->setText( text->text() + (*it)->psz_module );
688                 first = false;
689             }
690             else
691             {
692                 text->setText( text->text() + ":" + (*it)->psz_module );
693             }
694         }
695     }
696 }
697
698 /**************************************************************************
699  * Integer-based controls
700  *************************************************************************/
701
702 /*********** Integer **************/
703 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
704                                             module_config_t *_p_item,
705                                             QWidget *_parent, QGridLayout *l,
706                                             int &line ) :
707                            VIntConfigControl( _p_this, _p_item, _parent )
708 {
709     label = new QLabel( qtr(p_item->psz_text) );
710     spin = new QSpinBox; spin->setMinimumWidth( 80 );
711     spin->setAlignment( Qt::AlignRight );
712     spin->setMaximumWidth( 90 );
713     finish();
714
715     if( !l )
716     {
717         QHBoxLayout *layout = new QHBoxLayout();
718         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
719         widget->setLayout( layout );
720     }
721     else
722     {
723         l->addWidget( label, line, 0 );
724         l->addWidget( spin, line, 1, Qt::AlignRight );
725     }
726 }
727 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
728                                             module_config_t *_p_item,
729                                             QLabel *_label, QSpinBox *_spin ) :
730                                       VIntConfigControl( _p_this, _p_item )
731 {
732     spin = _spin;
733     label = _label;
734     finish();
735 }
736
737 void IntegerConfigControl::finish()
738 {
739     spin->setMaximum( 2000000000 );
740     spin->setMinimum( -2000000000 );
741     spin->setValue( p_item->value.i );
742     spin->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
743     if( label )
744         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
745 }
746
747 int IntegerConfigControl::getValue()
748 {
749     return spin->value();
750 }
751
752 /********* Integer range **********/
753 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
754                                             module_config_t *_p_item,
755                                             QWidget *_parent, QGridLayout *l,
756                                             int &line ) :
757             IntegerConfigControl( _p_this, _p_item, _parent, l, line )
758 {
759     finish();
760 }
761
762 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
763                                             module_config_t *_p_item,
764                                             QLabel *_label, QSpinBox *_spin ) :
765             IntegerConfigControl( _p_this, _p_item, _label, _spin )
766 {
767     finish();
768 }
769
770 void IntegerRangeConfigControl::finish()
771 {
772     spin->setMaximum( p_item->max.i );
773     spin->setMinimum( p_item->min.i );
774 }
775
776 IntegerRangeSliderConfigControl::IntegerRangeSliderConfigControl(
777                                             vlc_object_t *_p_this,
778                                             module_config_t *_p_item,
779                                             QLabel *_label, QSlider *_slider ):
780                     VIntConfigControl( _p_this, _p_item )
781 {
782     slider = _slider;
783     label = _label;
784     slider->setMaximum( p_item->max.i );
785     slider->setMinimum( p_item->min.i );
786     slider->setValue( p_item->value.i );
787     slider->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
788     if( label )
789         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
790 }
791
792 int IntegerRangeSliderConfigControl::getValue()
793 {
794         return slider->value();
795 }
796
797
798 /********* Integer / choice list **********/
799 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
800                module_config_t *_p_item, QWidget *_parent, bool bycat,
801                QGridLayout *l, int &line) :
802                VIntConfigControl( _p_this, _p_item, _parent )
803 {
804     label = new QLabel( qtr(p_item->psz_text) );
805     combo = new QComboBox();
806     combo->setMinimumWidth( 80 );
807     finish( bycat );
808     if( !l )
809     {
810         QHBoxLayout *layout = new QHBoxLayout();
811         layout->addWidget( label ); layout->addWidget( combo );
812         widget->setLayout( layout );
813     }
814     else
815     {
816         l->addWidget( label, line, 0 );
817         l->addWidget( combo, line, 1, Qt::AlignRight );
818     }
819 }
820 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
821                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
822                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
823 {
824     combo = _combo;
825     label = _label;
826     finish( bycat );
827 }
828
829 void IntegerListConfigControl::finish( bool bycat )
830 {
831     combo->setEditable( false );
832
833     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
834     {
835         combo->addItem( qtr(p_item->ppsz_list_text[i_index] ),
836                         QVariant( p_item->pi_list[i_index] ) );
837         if( p_item->value.i == p_item->pi_list[i_index] )
838             combo->setCurrentIndex( combo->count() - 1 );
839     }
840     combo->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
841     if( label )
842         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
843 }
844
845 int IntegerListConfigControl::getValue()
846 {
847     return combo->itemData( combo->currentIndex() ).toInt();
848 }
849
850 /*********** Boolean **************/
851 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
852                                       module_config_t *_p_item,
853                                       QWidget *_parent, QGridLayout *l,
854                                       int &line ) :
855                     VIntConfigControl( _p_this, _p_item, _parent )
856 {
857     checkbox = new QCheckBox( qtr(p_item->psz_text) );
858     finish();
859
860     if( !l )
861     {
862         QHBoxLayout *layout = new QHBoxLayout();
863         layout->addWidget( checkbox, 0 );
864         widget->setLayout( layout );
865     }
866     else
867     {
868         l->addWidget( checkbox, line, 0 );
869     }
870 }
871 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
872                                       module_config_t *_p_item,
873                                       QLabel *_label,
874                                       QCheckBox *_checkbox,
875                                       bool bycat ) :
876                    VIntConfigControl( _p_this, _p_item )
877 {
878     checkbox = _checkbox;
879     finish();
880 }
881
882 void BoolConfigControl::finish()
883 {
884     checkbox->setCheckState( p_item->value.i == VLC_TRUE ? Qt::Checked
885                                                         : Qt::Unchecked );
886     checkbox->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
887 }
888
889 int BoolConfigControl::getValue()
890 {
891     return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
892 }
893
894 /**************************************************************************
895  * Float-based controls
896  *************************************************************************/
897
898 /*********** Float **************/
899 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
900                                         module_config_t *_p_item,
901                                         QWidget *_parent, QGridLayout *l,
902                                         int &line ) :
903                     VFloatConfigControl( _p_this, _p_item, _parent )
904 {
905     label = new QLabel( qtr(p_item->psz_text) );
906     spin = new QDoubleSpinBox; 
907     spin->setMinimumWidth( 80 );
908     spin->setMaximumWidth( 90 );
909     spin->setAlignment( Qt::AlignRight );
910     finish();
911
912     if( !l )
913     {
914         QHBoxLayout *layout = new QHBoxLayout();
915         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
916         widget->setLayout( layout );
917     }
918     else
919     {
920         l->addWidget( label, line, 0 );
921         l->addWidget( spin, line, 1, Qt::AlignRight );
922     }
923 }
924
925 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
926                                         module_config_t *_p_item,
927                                         QLabel *_label,
928                                         QDoubleSpinBox *_spin ) :
929                     VFloatConfigControl( _p_this, _p_item )
930 {
931     spin = _spin;
932     label = _label;
933     finish();
934 }
935
936 void FloatConfigControl::finish()
937 {
938     spin->setMaximum( 2000000000. );
939     spin->setMinimum( -2000000000. );
940     spin->setSingleStep( 0.1 );
941     spin->setValue( (double)p_item->value.f );
942     spin->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
943     if( label )
944         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
945 }
946
947 float FloatConfigControl::getValue()
948 {
949     return (float)spin->value();
950 }
951
952 /*********** Float with range **************/
953 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
954                                         module_config_t *_p_item,
955                                         QWidget *_parent, QGridLayout *l,
956                                         int &line ) :
957                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
958 {
959     finish();
960 }
961
962 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
963                                         module_config_t *_p_item,
964                                         QLabel *_label,
965                                         QDoubleSpinBox *_spin ) :
966                 FloatConfigControl( _p_this, _p_item, _label, _spin )
967 {
968     finish();
969 }
970
971 void FloatRangeConfigControl::finish()
972 {
973     spin->setMaximum( (double)p_item->max.f );
974     spin->setMinimum( (double)p_item->min.f );
975 }
976
977
978 /**********************************************************************
979  * Key selector widget
980  **********************************************************************/
981 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
982                                       module_config_t *_p_item,
983                                       QWidget *_parent, QGridLayout *l,
984                                       int &line ) :
985                                 ConfigControl( _p_this, _p_item, _parent )
986
987 {
988     QWidget *keyContainer = new QWidget;
989     QGridLayout *gLayout = new QGridLayout( keyContainer );
990
991     label = new QLabel(
992             qtr( "Select an action to change the associated hotkey") );
993
994     /* Deactivated for now
995     QLabel *searchLabel = new QLabel( qtr( "Search" ) );
996     QLineEdit *actionSearch = new QLineEdit;*/
997
998     table = new QTreeWidget;
999     table->setColumnCount(2);
1000     table->headerItem()->setText( 0, qtr( "Action" ) );
1001     table->headerItem()->setText( 1, qtr( "Shortcut" ) );
1002
1003     shortcutValue = new KeyShortcutEdit;
1004     shortcutValue->setReadOnly(true);
1005
1006     QPushButton *clearButton = new QPushButton( qtr( "Clear" ) );
1007     QPushButton *setButton = new QPushButton( qtr( "Set" ) );
1008     finish();
1009
1010     gLayout->addWidget( label, 0, 0, 1, 4 );
1011   /* deactivated for now
1012     gLayout->addWidget( searchLabel, 1, 0, 1, 2 );
1013     gLayout->addWidget( actionSearch, 1, 2, 1, 2 ); */
1014     gLayout->addWidget( table, 2, 0, 1, 4 );
1015     gLayout->addWidget( clearButton, 3, 0, 1, 1 );
1016     gLayout->addWidget( shortcutValue, 3, 1, 1, 2 );
1017     gLayout->addWidget( setButton, 3, 3, 1, 1 );
1018
1019     l->addWidget( keyContainer, line, 0, 1, 2 );
1020
1021     CONNECT( clearButton, clicked(), shortcutValue, clear() );
1022     BUTTONACT( setButton, setTheKey() );
1023 }
1024
1025 void KeySelectorControl::finish()
1026 {
1027     if( label )
1028         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
1029
1030     /* Fill the table */
1031     table->setColumnCount( 2 );
1032     table->setAlternatingRowColors( true );
1033
1034     module_t *p_main = module_Find( p_this, "main" );
1035     assert( p_main );
1036
1037     unsigned confsize;
1038     module_config_t *p_config;
1039
1040     p_config = module_GetConfig (p_main, &confsize);
1041
1042     for (size_t i = 0; i < confsize; i++)
1043     {
1044         module_config_t *p_item = p_config + i;
1045
1046         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
1047             strstr( p_item->psz_name , "key-" ) && !EMPTY_STR( p_item->psz_text ) )
1048         {
1049             QTreeWidgetItem *treeItem = new QTreeWidgetItem();
1050             treeItem->setText( 0, qtr( p_item->psz_text ) );
1051             treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
1052             treeItem->setData( 0, Qt::UserRole,
1053                                   QVariant::fromValue( (void*)p_item ) );
1054             values += p_item;
1055             table->addTopLevelItem( treeItem );
1056         }
1057     }
1058     module_PutConfig (p_config);
1059     module_Put (p_main);
1060
1061     table->resizeColumnToContents( 0 );
1062
1063     CONNECT( table, itemClicked( QTreeWidgetItem *, int ),
1064              this, select1Key( QTreeWidgetItem * ) );
1065     CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
1066              this, selectKey( QTreeWidgetItem * ) );
1067     CONNECT( shortcutValue, pressed(), this, selectKey() );
1068 }
1069
1070 void KeySelectorControl::select1Key( QTreeWidgetItem *keyItem )
1071 {
1072     shortcutValue->setText( keyItem->text( 1 ) );
1073 }
1074
1075 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
1076 {
1077     /* This happens when triggered by ClickEater */
1078     if( keyItem == NULL ) keyItem = table->currentItem();
1079
1080     /* This can happen when nothing is selected on the treeView
1081        and the shortcutValue is clicked */
1082     if( !keyItem ) return;
1083
1084     module_config_t *p_keyItem = static_cast<module_config_t*>
1085                           (keyItem->data( 0, Qt::UserRole ).value<void*>());
1086
1087     KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text, widget );
1088     d->exec();
1089     if( d->result() == QDialog::Accepted )
1090     {
1091         p_keyItem->value.i = d->keyValue;
1092         if( d->conflicts )
1093         {
1094             for( int i = 0; i < table->topLevelItemCount() ; i++ )
1095             {
1096                 QTreeWidgetItem *it = table->topLevelItem(i);
1097                 module_config_t *p_item = static_cast<module_config_t*>
1098                               (it->data( 0, Qt::UserRole ).value<void*>());
1099                 if( p_keyItem != p_item && p_item->value.i == d->keyValue )
1100                     p_item->value.i = 0;
1101                 shortcutValue->setText( VLCKeyToString( p_item->value.i ) );
1102             }
1103         }
1104         else
1105             shortcutValue->setText( VLCKeyToString( p_keyItem->value.i ) );
1106     }
1107     delete d;
1108 }
1109
1110 void KeySelectorControl::setTheKey()
1111 {
1112     table->currentItem()->setText( 1, shortcutValue->text() );
1113 }
1114
1115 void KeySelectorControl::doApply()
1116 {
1117     foreach( module_config_t *p_current, values )
1118     {
1119         config_PutInt( p_this, p_current->psz_name, p_current->value.i );
1120     }
1121 }
1122
1123 KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
1124                                 const char * _keyToChange,
1125                                 QWidget *_parent ) :
1126                                 QDialog( _parent ), keyValue(0)
1127 {
1128     setModal( true );
1129     values = _values;
1130     conflicts = false;
1131     keyToChange = _keyToChange;
1132
1133     setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange)  );
1134
1135     vLayout = new QVBoxLayout( this );
1136     selected = new QLabel( qtr("Press the new keys for ") + qfu( keyToChange ) );
1137     vLayout->addWidget( selected , Qt::AlignCenter );
1138
1139     buttonBox = new QDialogButtonBox;
1140     QPushButton *ok = new QPushButton( qtr("OK") );
1141     QPushButton *cancel = new QPushButton( qtr("Cancel") );
1142     buttonBox->addButton( ok, QDialogButtonBox::AcceptRole );
1143     buttonBox->addButton( cancel, QDialogButtonBox::RejectRole );
1144
1145     vLayout->addWidget( buttonBox );
1146     buttonBox->hide();
1147
1148     CONNECT( buttonBox, accepted(), this, accept() );
1149     CONNECT( buttonBox, rejected(), this, reject() );
1150 }
1151
1152 void KeyInputDialog::checkForConflicts( int i_vlckey )
1153 {
1154     conflicts = false;
1155     module_config_t *p_current = NULL;
1156     /* Search for conflicts */
1157     foreach( p_current, values )
1158     {
1159         if( p_current->value.i == i_vlckey && strcmp( p_current->psz_text,
1160                                                     keyToChange ) )
1161         {
1162             conflicts = true;
1163             break;
1164         }
1165     }
1166
1167     if( conflicts )
1168     {
1169         QLabel *warning = new QLabel(
1170           qtr("Warning: the  key is already assigned to \"") +
1171           qfu( p_current->psz_text ) + "\"" );
1172         warning->setWordWrap( true );
1173         vLayout->insertWidget( 1, warning );
1174         buttonBox->show();
1175     }
1176     else accept();
1177 }
1178
1179 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
1180 {
1181     if( e->key() == Qt::Key_Tab ) return;
1182     int i_vlck = qtEventToVLCKey( e );
1183     selected->setText( VLCKeyToString( i_vlck ) );
1184     checkForConflicts( i_vlck );
1185     keyValue = i_vlck;
1186 }
1187
1188 void KeyInputDialog::wheelEvent( QWheelEvent *e )
1189 {
1190     int i_vlck = qtWheelEventToVLCKey( e );
1191     selected->setText( VLCKeyToString( i_vlck ) );
1192     checkForConflicts( i_vlck );
1193     keyValue = i_vlck;
1194 }
1195
1196 void KeyShortcutEdit::mousePressEvent( QMouseEvent *)
1197 {
1198     emit pressed();
1199 }