]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Revert "i18n fix"
[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 #include "util/qt_dirs.hpp"
40 #include <vlc_keys.h>
41 #include <vlc_intf_strings.h>
42
43 #include <QString>
44 #include <QVariant>
45 #include <QGridLayout>
46 #include <QSlider>
47 #include <QFileDialog>
48 #include <QGroupBox>
49 #include <QTreeWidgetItem>
50 #include <QSignalMapper>
51 #include <QDialogButtonBox>
52 #include <QKeyEvent>
53
54 #define MINWIDTH_BOX 90
55 #define LAST_COLUMN 10
56
57 QString formatTooltip(const QString & tooltip)
58 {
59     QString formatted =
60     "<html><head><meta name=\"qrichtext\" content=\"1\" />"
61     "<style type=\"text/css\"> p, li { white-space: pre-wrap; } </style></head>"
62     "<body style=\" font-family:'Sans Serif'; "
63     "font-style:normal; text-decoration:none;\">"
64     "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; "
65     "margin-right:0px; -qt-block-indent:0; text-indent:0px;\">" +
66     tooltip +
67     "</p></body></html>";
68     return formatted;
69 }
70
71 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
72                                              module_config_t *p_item,
73                                              QWidget *parent )
74 {
75     int i = 0;
76     return createControl( p_this, p_item, parent, NULL, i );
77 }
78
79 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
80                                              module_config_t *p_item,
81                                              QWidget *parent,
82                                              QGridLayout *l, int &line )
83 {
84     ConfigControl *p_control = NULL;
85
86     switch( p_item->i_type )
87     {
88     case CONFIG_ITEM_MODULE:
89         p_control = new ModuleConfigControl( p_this, p_item, parent, false,
90                                              l, line );
91         break;
92     case CONFIG_ITEM_MODULE_CAT:
93         p_control = new ModuleConfigControl( p_this, p_item, parent, true,
94                                              l, line );
95         break;
96     case CONFIG_ITEM_MODULE_LIST:
97         p_control = new ModuleListConfigControl( p_this, p_item, parent, false,
98                                              l, line );
99         break;
100     case CONFIG_ITEM_MODULE_LIST_CAT:
101         p_control = new ModuleListConfigControl( p_this, p_item, parent, true,
102                                              l, line );
103         break;
104     case CONFIG_ITEM_STRING:
105         if( !p_item->i_list )
106             p_control = new StringConfigControl( p_this, p_item, parent,
107                                                  l, line, false );
108         else
109             p_control = new StringListConfigControl( p_this, p_item,
110                                             parent, false, l, line );
111         break;
112     case CONFIG_ITEM_PASSWORD:
113         if( !p_item->i_list )
114             p_control = new StringConfigControl( p_this, p_item, parent,
115                                                  l, line, true );
116         else
117             p_control = new StringListConfigControl( p_this, p_item,
118                                             parent, true, l, line );
119         break;
120     case CONFIG_ITEM_INTEGER:
121         if( p_item->i_list )
122             p_control = new IntegerListConfigControl( p_this, p_item,
123                                             parent, false, l, line );
124         else if( p_item->min.i || p_item->max.i )
125             p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
126                                                        l, line );
127         else
128             p_control = new IntegerConfigControl( p_this, p_item, parent,
129                                                   l, line );
130         break;
131     case CONFIG_ITEM_FILE:
132         p_control = new FileConfigControl( p_this, p_item, parent, l, line);
133         break;
134     case CONFIG_ITEM_DIRECTORY:
135         p_control = new DirectoryConfigControl( p_this, p_item, parent, l,
136                                                 line );
137         break;
138     case CONFIG_ITEM_FONT:
139         p_control = new FontConfigControl( p_this, p_item, parent, l,
140                                            line);
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(), qtu( 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  * Simple widgets
201  *******************************************************/
202 InterfacePreviewWidget::InterfacePreviewWidget ( QWidget *parent ) : QLabel( parent )
203 {
204     setGeometry( 0, 0, 128, 100 );
205     setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
206 }
207
208 void InterfacePreviewWidget::setNormalPreview( bool b_minimal )
209 {
210     setPreview( ( b_minimal )?MINIMAL:COMPLETE );
211 }
212
213 void InterfacePreviewWidget::setPreview( enum_style e_style )
214 {
215     QString pixmapLocationString(":/prefsmenu/");
216
217     switch( e_style )
218     {
219     default:
220     case COMPLETE:
221         pixmapLocationString += "sample_complete";
222         break;
223     case MINIMAL:
224         pixmapLocationString += "sample_minimal";
225         break;
226     case SKINS:
227         pixmapLocationString += "sample_skins";
228         break;
229     }
230
231     setPixmap( QPixmap( pixmapLocationString ) );
232     update();
233 }
234
235
236
237 /**************************************************************************
238  * String-based controls
239  *************************************************************************/
240
241 /*********** String **************/
242 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
243                                           module_config_t *_p_item,
244                                           QWidget *_parent, QGridLayout *l,
245                                           int &line, bool pwd ) :
246                            VStringConfigControl( _p_this, _p_item, _parent )
247 {
248     label = new QLabel( qtr(p_item->psz_text) );
249     text = new QLineEdit( qfu(p_item->value.psz) );
250     if( pwd ) text->setEchoMode( QLineEdit::Password );
251     finish();
252
253     if( !l )
254     {
255         QHBoxLayout *layout = new QHBoxLayout();
256         layout->addWidget( label, 0 ); layout->insertSpacing( 1, 10 );
257         layout->addWidget( text, LAST_COLUMN );
258         widget->setLayout( layout );
259     }
260     else
261     {
262         l->addWidget( label, line, 0 );
263         l->setColumnMinimumWidth( 1, 10 );
264         l->addWidget( text, line, LAST_COLUMN );
265     }
266 }
267
268 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
269                                    module_config_t *_p_item,
270                                    QLabel *_label, QLineEdit *_text, bool pwd ):
271                            VStringConfigControl( _p_this, _p_item )
272 {
273     text = _text;
274     if( pwd ) text->setEchoMode( QLineEdit::Password );
275     label = _label;
276     finish( );
277 }
278
279 void StringConfigControl::finish()
280 {
281     text->setText( qfu(p_item->value.psz) );
282     text->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
283     if( label )
284     {
285         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
286         label->setBuddy( text );
287     }
288 }
289
290 /*********** File **************/
291 FileConfigControl::FileConfigControl( vlc_object_t *_p_this,
292                                           module_config_t *_p_item,
293                                           QWidget *_parent, QGridLayout *l,
294                                           int &line ) :
295                            VStringConfigControl( _p_this, _p_item, _parent )
296 {
297     label = new QLabel( qtr(p_item->psz_text) );
298     text = new QLineEdit( qfu(p_item->value.psz) );
299     browse = new QPushButton( qtr( "Browse..." ) );
300     QHBoxLayout *textAndButton = new QHBoxLayout();
301     textAndButton->setMargin( 0 );
302     textAndButton->addWidget( text, 2 );
303     textAndButton->addWidget( browse, 0 );
304
305     BUTTONACT( browse, updateField() );
306
307     finish();
308
309     if( !l )
310     {
311         QHBoxLayout *layout = new QHBoxLayout();
312         layout->addWidget( label, 0 );
313         layout->insertSpacing( 1, 10 );
314         layout->addLayout( textAndButton, LAST_COLUMN );
315         widget->setLayout( layout );
316     }
317     else
318     {
319         l->addWidget( label, line, 0 );
320         l->setColumnMinimumWidth( 1, 10 );
321         l->addLayout( textAndButton, line, LAST_COLUMN );
322     }
323 }
324
325
326 FileConfigControl::FileConfigControl( vlc_object_t *_p_this,
327                                    module_config_t *_p_item,
328                                    QLabel *_label, QLineEdit *_text,
329                                    QPushButton *_button ):
330                            VStringConfigControl( _p_this, _p_item )
331 {
332     browse = _button;
333     text = _text;
334     label = _label;
335
336     BUTTONACT( browse, updateField() );
337
338     finish( );
339 }
340
341 void FileConfigControl::updateField()
342 {
343     QString file = QFileDialog::getOpenFileName( NULL,
344                   qtr( "Select File" ), QVLCUserDir( VLC_HOME_DIR ) );
345     if( file.isNull() ) return;
346     text->setText( toNativeSeparators( file ) );
347 }
348
349 void FileConfigControl::finish()
350 {
351     text->setText( qfu(p_item->value.psz) );
352     text->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
353     if( label )
354     {
355         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
356         label->setBuddy( text );
357     }
358 }
359
360 /********* String / Directory **********/
361 DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
362                         module_config_t *_p_item, QWidget *_p_widget,
363                         QGridLayout *_p_layout, int& _int ) :
364      FileConfigControl( _p_this, _p_item, _p_widget, _p_layout, _int )
365 {}
366
367 DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
368                         module_config_t *_p_item, QLabel *_p_label,
369                         QLineEdit *_p_line, QPushButton *_p_button ):
370      FileConfigControl( _p_this, _p_item, _p_label, _p_line, _p_button)
371 {}
372
373 void DirectoryConfigControl::updateField()
374 {
375     QString dir = QFileDialog::getExistingDirectory( NULL,
376                       qtr( I_OP_SEL_DIR ),
377                       text->text().isEmpty() ?
378                         QVLCUserDir( VLC_HOME_DIR ) : text->text(),
379                   QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks );
380
381     if( dir.isNull() ) return;
382     text->setText( toNativeSepNoSlash( dir ) );
383 }
384
385 /********* String / Font **********/
386 FontConfigControl::FontConfigControl( vlc_object_t *_p_this,
387                         module_config_t *_p_item, QWidget *_parent,
388                         QGridLayout *_p_layout, int& line) :
389      VStringConfigControl( _p_this, _p_item, _parent )
390 {
391     label = new QLabel( qtr(p_item->psz_text) );
392     font = new QFontComboBox( _parent );
393     font->setCurrentFont( QFont( qfu( p_item->value.psz) ) );
394     if( !_p_layout )
395     {
396         QHBoxLayout *layout = new QHBoxLayout();
397         layout->addWidget( label, 0 );
398         layout->addWidget( font, 1 );
399         widget->setLayout( layout );
400     }
401     else
402     {
403         _p_layout->addWidget( label, line, 0 );
404         _p_layout->addWidget( font, line, 1, 1, -1 );
405     }
406 }
407
408 FontConfigControl::FontConfigControl( vlc_object_t *_p_this,
409                         module_config_t *_p_item, QLabel *_p_label,
410                         QFontComboBox *_p_font):
411      VStringConfigControl( _p_this, _p_item)
412 {
413     label = _p_label;
414     font = _p_font;
415     font->setCurrentFont( QFont( qfu( p_item->value.psz) ) );
416 }
417
418 /********* String / choice list **********/
419 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
420                module_config_t *_p_item, QWidget *_parent, bool bycat,
421                QGridLayout *l, int &line) :
422                VStringConfigControl( _p_this, _p_item, _parent )
423 {
424     label = new QLabel( qtr(p_item->psz_text) );
425     combo = new QComboBox();
426     combo->setMinimumWidth( MINWIDTH_BOX );
427     combo->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
428
429     module_config_t *p_module_config = config_FindConfig( p_this, p_item->psz_name );
430
431     finish( p_module_config, bycat );
432     if( !l )
433     {
434         l = new QGridLayout();
435         l->addWidget( label, 0, 0 ); l->addWidget( combo, 0, LAST_COLUMN );
436         widget->setLayout( l );
437     }
438     else
439     {
440         l->addWidget( label, line, 0 );
441         l->addWidget( combo, line, LAST_COLUMN, Qt::AlignRight );
442     }
443
444     if( p_item->i_action )
445     {
446         QSignalMapper *signalMapper = new QSignalMapper(this);
447
448         /* Some stringLists like Capture listings have action associated */
449         for( int i = 0; i < p_item->i_action; i++ )
450         {
451             QPushButton *button =
452                 new QPushButton( qtr( p_item->ppsz_action_text[i] ));
453             CONNECT( button, clicked(), signalMapper, map() );
454             signalMapper->setMapping( button, i );
455             l->addWidget( button, line, LAST_COLUMN - p_item->i_action + i,
456                     Qt::AlignRight );
457         }
458         CONNECT( signalMapper, mapped( int ),
459                 this, actionRequested( int ) );
460     }
461 }
462
463 void StringListConfigControl::actionRequested( int i_action )
464 {
465     /* Supplementary check for boundaries */
466     if( i_action < 0 || i_action >= p_item->i_action ) return;
467
468     module_config_t *p_module_config = config_FindConfig( p_this, getName() );
469     if(!p_module_config) return;
470
471     vlc_value_t val;
472     val.psz_string = const_cast<char *>
473         qtu( (combo->itemData( combo->currentIndex() ).toString() ) );
474
475     p_module_config->ppf_action[i_action]( p_this, getName(), val, val, 0 );
476
477     if( p_module_config->b_dirty )
478     {
479         combo->clear();
480         finish( p_module_config, true );
481         p_module_config->b_dirty = false;
482     }
483 }
484 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
485                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
486                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
487 {
488     combo = _combo;
489     label = _label;
490
491     module_config_t *p_module_config = config_FindConfig( p_this, getName() );
492
493     finish( p_module_config, bycat );
494 }
495
496 void StringListConfigControl::finish(module_config_t *p_module_config, bool bycat )
497 {
498     combo->setEditable( false );
499
500     if(!p_module_config) return;
501
502     if( p_module_config->pf_update_list )
503     {
504        vlc_value_t val;
505        val.psz_string = strdup(p_module_config->value.psz);
506
507        p_module_config->pf_update_list(p_this, p_item->psz_name, val, val, NULL);
508
509        // assume in any case that dirty was set to true
510        // because lazy programmes will use the same callback for
511        // this, like the one behind the refresh push button?
512        p_module_config->b_dirty = false;
513
514        free( val.psz_string );
515     }
516
517     for( int i_index = 0; i_index < p_module_config->i_list; i_index++ )
518     {
519         if( !p_module_config->ppsz_list[i_index] )
520               continue;
521         combo->addItem( qfu((p_module_config->ppsz_list_text &&
522                             p_module_config->ppsz_list_text[i_index])?
523                             p_module_config->ppsz_list_text[i_index] :
524                             p_module_config->ppsz_list[i_index] ),
525                    QVariant( qfu(p_module_config->ppsz_list[i_index] )) );
526         if( p_item->value.psz && !strcmp( p_module_config->value.psz,
527                                           p_module_config->ppsz_list[i_index] ) )
528             combo->setCurrentIndex( combo->count() - 1 );
529     }
530     combo->setToolTip( formatTooltip(qtr(p_module_config->psz_longtext)) );
531     if( label )
532     {
533         label->setToolTip( formatTooltip(qtr(p_module_config->psz_longtext)) );
534         label->setBuddy( combo );
535     }
536 }
537
538 QString StringListConfigControl::getValue()
539 {
540     return combo->itemData( combo->currentIndex() ).toString();
541 }
542
543 void setfillVLCConfigCombo( const char *configname, intf_thread_t *p_intf,
544                         QComboBox *combo )
545 {
546     module_config_t *p_config =
547                       config_FindConfig( VLC_OBJECT(p_intf), configname );
548     if( p_config )
549     {
550        if(p_config->pf_update_list)
551         {
552             vlc_value_t val;
553             val.i_int = p_config->value.i;
554             p_config->pf_update_list(VLC_OBJECT(p_intf), configname, val, val, NULL);
555             // assume in any case that dirty was set to true
556             // because lazy programmes will use the same callback for
557             // this, like the one behind the refresh push button?
558             p_config->b_dirty = false;
559         }
560
561         for ( int i_index = 0; i_index < p_config->i_list; i_index++ )
562         {
563             combo->addItem( qfu( p_config->ppsz_list_text[i_index] ),
564                     QVariant( p_config->pi_list[i_index] ) );
565             if( p_config->value.i == p_config->pi_list[i_index] )
566             {
567                 combo->setCurrentIndex( i_index );
568             }
569         }
570         combo->setToolTip( qfu( p_config->psz_longtext ) );
571     }
572 }
573
574 /********* Module **********/
575 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
576                module_config_t *_p_item, QWidget *_parent, bool bycat,
577                QGridLayout *l, int &line) :
578                VStringConfigControl( _p_this, _p_item, _parent )
579 {
580     label = new QLabel( qtr(p_item->psz_text) );
581     combo = new QComboBox();
582     combo->setMinimumWidth( MINWIDTH_BOX );
583     finish( bycat );
584     if( !l )
585     {
586         QHBoxLayout *layout = new QHBoxLayout();
587         layout->addWidget( label ); layout->addWidget( combo, LAST_COLUMN );
588         widget->setLayout( layout );
589     }
590     else
591     {
592         l->addWidget( label, line, 0 );
593         l->addWidget( combo, line, LAST_COLUMN, Qt::AlignRight );
594     }
595 }
596
597 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
598                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
599                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
600 {
601     combo = _combo;
602     label = _label;
603     finish( bycat );
604 }
605
606 void ModuleConfigControl::finish( bool bycat )
607 {
608     module_t *p_parser;
609
610     combo->setEditable( false );
611
612     /* build a list of available modules */
613     module_t **p_list = module_list_get( NULL );
614     combo->addItem( qtr("Default") );
615     for( size_t i = 0; (p_parser = p_list[i]) != NULL; i++ )
616     {
617         if( bycat )
618         {
619             if( !strcmp( module_get_object( p_parser ), "main" ) ) continue;
620
621             unsigned confsize;
622             module_config_t *p_config;
623
624             p_config = module_config_get (p_parser, &confsize);
625              for (size_t i = 0; i < confsize; i++)
626             {
627                 /* Hack: required subcategory is stored in i_min */
628                 const module_config_t *p_cfg = p_config + i;
629                 if( p_cfg->i_type == CONFIG_SUBCATEGORY &&
630                     p_cfg->value.i == p_item->min.i )
631                     combo->addItem( qtr( module_GetLongName( p_parser )),
632                                     QVariant( module_get_object( p_parser ) ) );
633                 if( p_item->value.psz && !strcmp( p_item->value.psz,
634                                                   module_get_object( p_parser ) ) )
635                     combo->setCurrentIndex( combo->count() - 1 );
636             }
637             module_config_free (p_config);
638         }
639         else if( module_provides( p_parser, p_item->psz_type ) )
640         {
641             combo->addItem( qtr(module_GetLongName( p_parser ) ),
642                             QVariant( module_get_object( p_parser ) ) );
643             if( p_item->value.psz && !strcmp( p_item->value.psz,
644                                               module_get_object( p_parser ) ) )
645                 combo->setCurrentIndex( combo->count() - 1 );
646         }
647     }
648     module_list_free( p_list );
649     combo->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
650     if( label )
651     {
652         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
653         label->setBuddy( combo );
654     }
655 }
656
657 QString ModuleConfigControl::getValue()
658 {
659     return combo->itemData( combo->currentIndex() ).toString();
660 }
661
662 /********* Module list **********/
663 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
664         module_config_t *_p_item, QWidget *_parent, bool bycat,
665         QGridLayout *l, int &line) :
666     VStringConfigControl( _p_this, _p_item, _parent )
667 {
668     groupBox = NULL;
669     /* Special Hack */
670     if( !p_item->psz_text ) return;
671
672     groupBox = new QGroupBox ( qtr(p_item->psz_text), _parent );
673     text = new QLineEdit;
674     QGridLayout *layoutGroupBox = new QGridLayout( groupBox );
675
676     finish( bycat );
677
678     int boxline = 0;
679     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
680             it != modules.end(); it++ )
681     {
682         layoutGroupBox->addWidget( (*it)->checkBox, boxline++, 0 );
683     }
684     layoutGroupBox->addWidget( text, boxline, 0 );
685
686     if( !l )
687     {
688         QVBoxLayout *layout = new QVBoxLayout();
689         layout->addWidget( groupBox, line, 0 );
690         widget->setLayout( layout );
691     }
692     else
693     {
694         l->addWidget( groupBox, line, 0, 1, -1 );
695     }
696
697     text->setToolTip( formatTooltip( qtr( p_item->psz_longtext) ) );
698 }
699
700 ModuleListConfigControl::~ModuleListConfigControl()
701 {
702     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
703             it != modules.end(); it++ )
704     {
705         delete *it;
706     }
707     delete groupBox;
708 }
709
710 #define CHECKBOX_LISTS \
711 { \
712        QCheckBox *cb = new QCheckBox( qtr( module_GetLongName( p_parser ) ) );\
713        checkBoxListItem *cbl = new checkBoxListItem; \
714 \
715        CONNECT( cb, stateChanged( int ), this, onUpdate() );\
716        cb->setToolTip( formatTooltip( qtr( module_get_help( p_parser ))));\
717        cbl->checkBox = cb; \
718 \
719        cbl->psz_module = strdup( module_get_object( p_parser ) ); \
720        modules.push_back( cbl ); \
721 \
722        if( p_item->value.psz && strstr( p_item->value.psz, cbl->psz_module ) ) \
723             cbl->checkBox->setChecked( true ); \
724 }
725
726
727 void ModuleListConfigControl::finish( bool bycat )
728 {
729     module_t *p_parser;
730
731     /* build a list of available modules */
732     module_t **p_list = module_list_get( NULL );
733     for( size_t i = 0; (p_parser = p_list[i]) != NULL; i++ )
734     {
735         if( bycat )
736         {
737             if( !strcmp( module_get_object( p_parser ), "main" ) ) continue;
738
739             unsigned confsize;
740             module_config_t *p_config = module_config_get (p_parser, &confsize);
741
742             for (size_t i = 0; i < confsize; i++)
743             {
744                 module_config_t *p_cfg = p_config + i;
745                 /* Hack: required subcategory is stored in i_min */
746                 if( p_cfg->i_type == CONFIG_SUBCATEGORY &&
747                         p_cfg->value.i == p_item->min.i )
748                 {
749                     CHECKBOX_LISTS;
750                 }
751             }
752             module_config_free (p_config);
753         }
754         else if( module_provides( p_parser, p_item->psz_type ) )
755         {
756             CHECKBOX_LISTS;
757         }
758     }
759     module_list_free( p_list );
760     text->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
761     assert( groupBox );
762     groupBox->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
763 }
764 #undef CHECKBOX_LISTS
765
766 QString ModuleListConfigControl::getValue()
767 {
768     assert( text );
769     return text->text();
770 }
771
772 void ModuleListConfigControl::hide()
773 {
774     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
775          it != modules.end(); it++ )
776     {
777         (*it)->checkBox->hide();
778     }
779     groupBox->hide();
780 }
781
782 void ModuleListConfigControl::show()
783 {
784     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
785          it != modules.end(); it++ )
786     {
787         (*it)->checkBox->show();
788     }
789     groupBox->show();
790 }
791
792
793 void ModuleListConfigControl::onUpdate()
794 {
795     text->clear();
796     bool first = true;
797
798     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
799          it != modules.end(); it++ )
800     {
801         if( (*it)->checkBox->isChecked() )
802         {
803             if( first )
804             {
805                 text->setText( text->text() + (*it)->psz_module );
806                 first = false;
807             }
808             else
809             {
810                 text->setText( text->text() + ":" + (*it)->psz_module );
811             }
812         }
813     }
814 }
815
816 /**************************************************************************
817  * Integer-based controls
818  *************************************************************************/
819
820 /*********** Integer **************/
821 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
822                                             module_config_t *_p_item,
823                                             QWidget *_parent, QGridLayout *l,
824                                             int &line ) :
825                            VIntConfigControl( _p_this, _p_item, _parent )
826 {
827     label = new QLabel( qtr(p_item->psz_text) );
828     spin = new QSpinBox; spin->setMinimumWidth( MINWIDTH_BOX );
829     spin->setAlignment( Qt::AlignRight );
830     spin->setMaximumWidth( MINWIDTH_BOX );
831     finish();
832
833     if( !l )
834     {
835         QHBoxLayout *layout = new QHBoxLayout();
836         layout->addWidget( label, 0 ); layout->addWidget( spin, LAST_COLUMN );
837         widget->setLayout( layout );
838     }
839     else
840     {
841         l->addWidget( label, line, 0 );
842         l->addWidget( spin, line, LAST_COLUMN, Qt::AlignRight );
843     }
844 }
845 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
846                                             module_config_t *_p_item,
847                                             QLabel *_label, QSpinBox *_spin ) :
848                                       VIntConfigControl( _p_this, _p_item )
849 {
850     spin = _spin;
851     label = _label;
852     finish();
853 }
854
855 void IntegerConfigControl::finish()
856 {
857     spin->setMaximum( 2000000000 );
858     spin->setMinimum( -2000000000 );
859     spin->setValue( p_item->value.i );
860     spin->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
861     if( label )
862     {
863         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
864         label->setBuddy( spin );
865     }
866 }
867
868 int IntegerConfigControl::getValue()
869 {
870     return spin->value();
871 }
872
873 /********* Integer range **********/
874 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
875                                             module_config_t *_p_item,
876                                             QWidget *_parent, QGridLayout *l,
877                                             int &line ) :
878             IntegerConfigControl( _p_this, _p_item, _parent, l, line )
879 {
880     finish();
881 }
882
883 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
884                                             module_config_t *_p_item,
885                                             QLabel *_label, QSpinBox *_spin ) :
886             IntegerConfigControl( _p_this, _p_item, _label, _spin )
887 {
888     finish();
889 }
890
891 void IntegerRangeConfigControl::finish()
892 {
893     spin->setMaximum( p_item->max.i );
894     spin->setMinimum( p_item->min.i );
895 }
896
897 IntegerRangeSliderConfigControl::IntegerRangeSliderConfigControl(
898                                             vlc_object_t *_p_this,
899                                             module_config_t *_p_item,
900                                             QLabel *_label, QSlider *_slider ):
901                     VIntConfigControl( _p_this, _p_item )
902 {
903     slider = _slider;
904     label = _label;
905     slider->setMaximum( p_item->max.i );
906     slider->setMinimum( p_item->min.i );
907     slider->setValue( p_item->value.i );
908     slider->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
909     if( label )
910     {
911         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
912         label->setBuddy( slider );
913     }
914 }
915
916 int IntegerRangeSliderConfigControl::getValue()
917 {
918         return slider->value();
919 }
920
921
922 /********* Integer / choice list **********/
923 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
924                module_config_t *_p_item, QWidget *_parent, bool bycat,
925                QGridLayout *l, int &line) :
926                VIntConfigControl( _p_this, _p_item, _parent )
927 {
928     label = new QLabel( qtr(p_item->psz_text) );
929     combo = new QComboBox();
930     combo->setMinimumWidth( MINWIDTH_BOX );
931
932     module_config_t *p_module_config = config_FindConfig( p_this, p_item->psz_name );
933
934     finish( p_module_config, bycat );
935     if( !l )
936     {
937         QHBoxLayout *layout = new QHBoxLayout();
938         layout->addWidget( label ); layout->addWidget( combo, LAST_COLUMN );
939         widget->setLayout( layout );
940     }
941     else
942     {
943         l->addWidget( label, line, 0 );
944         l->addWidget( combo, line, LAST_COLUMN, Qt::AlignRight );
945     }
946
947     if( p_item->i_action )
948     {
949         QSignalMapper *signalMapper = new QSignalMapper(this);
950
951         /* Some stringLists like Capture listings have action associated */
952         for( int i = 0; i < p_item->i_action; i++ )
953         {
954             QPushButton *button =
955                 new QPushButton( qfu( p_item->ppsz_action_text[i] ));
956             CONNECT( button, clicked(), signalMapper, map() );
957             signalMapper->setMapping( button, i );
958             l->addWidget( button, line, LAST_COLUMN - p_item->i_action + i,
959                     Qt::AlignRight );
960         }
961         CONNECT( signalMapper, mapped( int ),
962                 this, actionRequested( int ) );
963     }
964
965 }
966 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
967                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
968                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
969 {
970     combo = _combo;
971     label = _label;
972
973     module_config_t *p_module_config = config_FindConfig( p_this, getName() );
974
975     finish( p_module_config, bycat );
976 }
977
978 void IntegerListConfigControl::finish(module_config_t *p_module_config, bool bycat )
979 {
980     combo->setEditable( false );
981
982     if(!p_module_config) return;
983
984     if( p_module_config->pf_update_list )
985     {
986        vlc_value_t val;
987        val.i_int = p_module_config->value.i;
988
989        p_module_config->pf_update_list(p_this, p_item->psz_name, val, val, NULL);
990
991        // assume in any case that dirty was set to true
992        // because lazy programmes will use the same callback for
993        // this, like the one behind the refresh push button?
994        p_module_config->b_dirty = false;
995     }
996
997     for( int i_index = 0; i_index < p_module_config->i_list; i_index++ )
998     {
999         combo->addItem( qtr(p_module_config->ppsz_list_text[i_index] ),
1000                         QVariant( p_module_config->pi_list[i_index] ) );
1001         if( p_module_config->value.i == p_module_config->pi_list[i_index] )
1002             combo->setCurrentIndex( combo->count() - 1 );
1003     }
1004     combo->setToolTip( formatTooltip(qtr(p_module_config->psz_longtext)) );
1005     if( label )
1006     {
1007         label->setToolTip( formatTooltip(qtr(p_module_config->psz_longtext)) );
1008         label->setBuddy( combo );
1009     }
1010 }
1011
1012 void IntegerListConfigControl::actionRequested( int i_action )
1013 {
1014     /* Supplementary check for boundaries */
1015     if( i_action < 0 || i_action >= p_item->i_action ) return;
1016
1017     module_config_t *p_module_config = config_FindConfig( p_this, getName() );
1018     if(!p_module_config) return;
1019
1020
1021     vlc_value_t val;
1022     val.i_int = combo->itemData( combo->currentIndex() ).toInt();
1023
1024     p_module_config->ppf_action[i_action]( p_this, getName(), val, val, 0 );
1025
1026     if( p_module_config->b_dirty )
1027     {
1028         combo->clear();
1029         finish( p_module_config, true );
1030         p_module_config->b_dirty = false;
1031     }
1032 }
1033
1034 int IntegerListConfigControl::getValue()
1035 {
1036     return combo->itemData( combo->currentIndex() ).toInt();
1037 }
1038
1039 /*********** Boolean **************/
1040 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
1041                                       module_config_t *_p_item,
1042                                       QWidget *_parent, QGridLayout *l,
1043                                       int &line ) :
1044                     VIntConfigControl( _p_this, _p_item, _parent )
1045 {
1046     checkbox = new QCheckBox( qtr(p_item->psz_text) );
1047     finish();
1048
1049     if( !l )
1050     {
1051         QHBoxLayout *layout = new QHBoxLayout();
1052         layout->addWidget( checkbox, 0 );
1053         widget->setLayout( layout );
1054     }
1055     else
1056     {
1057         l->addWidget( checkbox, line, 0 );
1058     }
1059 }
1060
1061 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
1062                                       module_config_t *_p_item,
1063                                       QLabel *_label,
1064                                       QAbstractButton *_checkbox,
1065                                       bool bycat ) :
1066                    VIntConfigControl( _p_this, _p_item )
1067 {
1068     checkbox = _checkbox;
1069     VLC_UNUSED( _label );
1070     finish();
1071 }
1072
1073 void BoolConfigControl::finish()
1074 {
1075     checkbox->setChecked( p_item->value.i == true );
1076     checkbox->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
1077 }
1078
1079 int BoolConfigControl::getValue()
1080 {
1081     return checkbox->isChecked();
1082 }
1083
1084 /**************************************************************************
1085  * Float-based controls
1086  *************************************************************************/
1087
1088 /*********** Float **************/
1089 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
1090                                         module_config_t *_p_item,
1091                                         QWidget *_parent, QGridLayout *l,
1092                                         int &line ) :
1093                     VFloatConfigControl( _p_this, _p_item, _parent )
1094 {
1095     label = new QLabel( qtr(p_item->psz_text) );
1096     spin = new QDoubleSpinBox;
1097     spin->setMinimumWidth( MINWIDTH_BOX );
1098     spin->setMaximumWidth( MINWIDTH_BOX );
1099     spin->setAlignment( Qt::AlignRight );
1100     finish();
1101
1102     if( !l )
1103     {
1104         QHBoxLayout *layout = new QHBoxLayout();
1105         layout->addWidget( label, 0 ); layout->addWidget( spin, LAST_COLUMN );
1106         widget->setLayout( layout );
1107     }
1108     else
1109     {
1110         l->addWidget( label, line, 0 );
1111         l->addWidget( spin, line, LAST_COLUMN, Qt::AlignRight );
1112     }
1113 }
1114
1115 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
1116                                         module_config_t *_p_item,
1117                                         QLabel *_label,
1118                                         QDoubleSpinBox *_spin ) :
1119                     VFloatConfigControl( _p_this, _p_item )
1120 {
1121     spin = _spin;
1122     label = _label;
1123     finish();
1124 }
1125
1126 void FloatConfigControl::finish()
1127 {
1128     spin->setMaximum( 2000000000. );
1129     spin->setMinimum( -2000000000. );
1130     spin->setSingleStep( 0.1 );
1131     spin->setValue( (double)p_item->value.f );
1132     spin->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
1133     if( label )
1134     {
1135         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
1136         label->setBuddy( spin );
1137     }
1138 }
1139
1140 float FloatConfigControl::getValue()
1141 {
1142     return (float)spin->value();
1143 }
1144
1145 /*********** Float with range **************/
1146 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
1147                                         module_config_t *_p_item,
1148                                         QWidget *_parent, QGridLayout *l,
1149                                         int &line ) :
1150                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
1151 {
1152     finish();
1153 }
1154
1155 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
1156                                         module_config_t *_p_item,
1157                                         QLabel *_label,
1158                                         QDoubleSpinBox *_spin ) :
1159                 FloatConfigControl( _p_this, _p_item, _label, _spin )
1160 {
1161     finish();
1162 }
1163
1164 void FloatRangeConfigControl::finish()
1165 {
1166     spin->setMaximum( (double)p_item->max.f );
1167     spin->setMinimum( (double)p_item->min.f );
1168 }
1169
1170
1171 /**********************************************************************
1172  * Key selector widget
1173  **********************************************************************/
1174 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
1175                                       module_config_t *_p_item,
1176                                       QWidget *_parent, QGridLayout *l,
1177                                       int &line ) :
1178                                 ConfigControl( _p_this, _p_item, _parent )
1179
1180 {
1181     QWidget *keyContainer = new QWidget;
1182     QGridLayout *gLayout = new QGridLayout( keyContainer );
1183
1184     label = new QLabel(
1185             qtr( "Select an action to change the associated hotkey") );
1186
1187     QLabel *searchLabel = new QLabel( qtr( "Search" ) );
1188     actionSearch = new SearchLineEdit( keyContainer );
1189
1190     table = new QTreeWidget;
1191     table->setColumnCount(3);
1192     table->headerItem()->setText( 0, qtr( "Action" ) );
1193     table->headerItem()->setText( 1, qtr( "Hotkey" ) );
1194     table->headerItem()->setText( 2, qtr( "Global" ) );
1195     table->setAlternatingRowColors( true );
1196     table->setSelectionBehavior( QAbstractItemView::SelectItems );
1197
1198     shortcutValue = new KeyShortcutEdit;
1199     shortcutValue->setReadOnly(true);
1200
1201     QPushButton *clearButton = new QPushButton( qtr( "Clear" ) );
1202     QPushButton *setButton = new QPushButton( qtr( "Apply" ) );
1203     setButton->setDefault( true );
1204     finish();
1205
1206     gLayout->addWidget( label, 0, 0, 1, 4 );
1207     gLayout->addWidget( searchLabel, 1, 0, 1, 2 );
1208     gLayout->addWidget( actionSearch, 1, 2, 1, 2 );
1209     gLayout->addWidget( table, 2, 0, 1, 4 );
1210     gLayout->addWidget( clearButton, 3, 0, 1, 1 );
1211     gLayout->addWidget( shortcutValue, 3, 1, 1, 2 );
1212     gLayout->addWidget( setButton, 3, 3, 1, 1 );
1213
1214     l->addWidget( keyContainer, line, 0, 1, -1 );
1215
1216     CONNECT( clearButton, clicked(), shortcutValue, clear() );
1217     CONNECT( clearButton, clicked(), this, setTheKey() );
1218     BUTTONACT( setButton, setTheKey() );
1219     CONNECT( actionSearch, textChanged( const QString& ),
1220              this, filter( const QString& ) );
1221 }
1222
1223 void KeySelectorControl::finish()
1224 {
1225     if( label )
1226         label->setToolTip( formatTooltip( qtr( p_item->psz_longtext ) ) );
1227
1228     /* Fill the table */
1229
1230     /* Get the main Module */
1231     module_t *p_main = module_get_main();
1232     assert( p_main );
1233
1234     /* Access to the module_config_t */
1235     unsigned confsize;
1236     module_config_t *p_config;
1237
1238     p_config = module_config_get (p_main, &confsize);
1239
1240     for (size_t i = 0; i < confsize; i++)
1241     {
1242         module_config_t *p_item = p_config + i;
1243
1244         /* If we are a key option not empty */
1245         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name
1246             && strstr( p_item->psz_name , "key-" )
1247             && !strstr( p_item->psz_name , "global-key" )
1248             && !EMPTY_STR( p_item->psz_text ) )
1249         {
1250             /*
1251                Each tree item has:
1252                 - QString text in column 0
1253                 - QString name in data of column 0
1254                 - KeyValue in String in column 1
1255                 - KeyValue in int in column 1
1256              */
1257             QTreeWidgetItem *treeItem = new QTreeWidgetItem();
1258             treeItem->setText( 0, qtr( p_item->psz_text ) );
1259             treeItem->setData( 0, Qt::UserRole,
1260                                QVariant( qfu( p_item->psz_name ) ) );
1261             treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
1262             treeItem->setData( 1, Qt::UserRole, QVariant( p_item->value.i ) );
1263             table->addTopLevelItem( treeItem );
1264             continue;
1265         }
1266
1267         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name
1268                 && strstr( p_item->psz_name , "global-key" )
1269                 && !EMPTY_STR( p_item->psz_text ) )
1270         {
1271             QList<QTreeWidgetItem *> list =
1272                 table->findItems( qtr( p_item->psz_text ), Qt::MatchExactly );
1273             if( list.count() >= 1 )
1274             {
1275                 list[0]->setText( 2, VLCKeyToString( p_item->value.i ) );
1276                 list[0]->setData( 2, Qt::UserRole,
1277                                   QVariant( p_item->value.i ) );
1278             }
1279             if( list.count() >= 2 )
1280                 msg_Dbg( p_this, "This is probably wrong, %s", p_item->psz_text );
1281         }
1282     }
1283     module_config_free (p_config);
1284     module_release (p_main);
1285
1286     table->resizeColumnToContents( 0 );
1287
1288     CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
1289              this, selectKey( QTreeWidgetItem *, int ) );
1290     CONNECT( table, itemClicked( QTreeWidgetItem *, int ),
1291              this, select( QTreeWidgetItem *, int) );
1292     CONNECT( table, itemSelectionChanged(),
1293              this, select1Key() );
1294
1295     CONNECT( shortcutValue, pressed(), this, selectKey() );
1296 }
1297
1298 void KeySelectorControl::filter( const QString &qs_search )
1299 {
1300     QList<QTreeWidgetItem *> resultList =
1301             table->findItems( qs_search, Qt::MatchContains, 0 );
1302     for( int i = 0; i < table->topLevelItemCount(); i++ )
1303     {
1304         table->topLevelItem( i )->setHidden(
1305                 !resultList.contains( table->topLevelItem( i ) ) );
1306     }
1307 }
1308
1309 void KeySelectorControl::select( QTreeWidgetItem *keyItem, int column )
1310 {
1311     shortcutValue->setGlobal( column == 2 );
1312 }
1313
1314 /* Show the key selected from the table in the keySelector */
1315 void KeySelectorControl::select1Key()
1316 {
1317     QTreeWidgetItem *keyItem = table->currentItem();
1318     shortcutValue->setText( keyItem->text( 1 ) );
1319     shortcutValue->setValue( keyItem->data( 1, Qt::UserRole ).toInt() );
1320     shortcutValue->setGlobal( false );
1321 }
1322
1323 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem, int column )
1324 {
1325     /* This happens when triggered by ClickEater */
1326     if( keyItem == NULL ) keyItem = table->currentItem();
1327
1328     /* This can happen when nothing is selected on the treeView
1329        and the shortcutValue is clicked */
1330     if( !keyItem ) return;
1331
1332     /* If clicked on the first column, assuming user wants the normal hotkey */
1333     if( column == 0 ) column = 1;
1334
1335     bool b_global = ( column == 2 );
1336
1337     /* Launch a small dialog to ask for a new key */
1338     KeyInputDialog *d = new KeyInputDialog( table, keyItem->text( 0 ), widget, b_global );
1339     d->exec();
1340
1341     if( d->result() == QDialog::Accepted )
1342     {
1343         int newValue = d->keyValue;
1344         shortcutValue->setText( VLCKeyToString( newValue ) );
1345         shortcutValue->setValue( newValue );
1346         shortcutValue->setGlobal( b_global );
1347
1348         if( d->conflicts )
1349         {
1350             QTreeWidgetItem *it;
1351             for( int i = 0; i < table->topLevelItemCount() ; i++ )
1352             {
1353                 it = table->topLevelItem(i);
1354                 if( ( keyItem != it ) &&
1355                     ( it->data( b_global ? 2: 1, Qt::UserRole ).toInt() == newValue ) )
1356                 {
1357                     it->setData( b_global ? 2 : 1, Qt::UserRole, QVariant( -1 ) );
1358                     it->setText( b_global ? 2 : 1, qtr( "Unset" ) );
1359                 }
1360             }
1361             /* We already made an OK once. */
1362             setTheKey();
1363         }
1364     }
1365     delete d;
1366 }
1367
1368 void KeySelectorControl::setTheKey()
1369 {
1370     if( !table->currentItem() ) return;
1371     table->currentItem()->setText( shortcutValue->getGlobal() ? 2 : 1,
1372                                    shortcutValue->text() );
1373     table->currentItem()->setData( shortcutValue->getGlobal() ? 2 : 1,
1374                                    Qt::UserRole, shortcutValue->getValue() );
1375 }
1376
1377 void KeySelectorControl::doApply()
1378 {
1379     QTreeWidgetItem *it;
1380     for( int i = 0; i < table->topLevelItemCount() ; i++ )
1381     {
1382         it = table->topLevelItem(i);
1383         if( it->data( 1, Qt::UserRole ).toInt() >= 0 )
1384             config_PutInt( p_this,
1385                            qtu( it->data( 0, Qt::UserRole ).toString() ),
1386                            it->data( 1, Qt::UserRole ).toInt() );
1387         if( it->data( 2, Qt::UserRole ).toInt() >= 0 )
1388             config_PutInt( p_this,
1389                            qtu( "global-" + it->data( 0, Qt::UserRole ).toString() ),
1390                            it->data( 2, Qt::UserRole ).toInt() );
1391
1392     }
1393 }
1394
1395 /**
1396  * Class KeyInputDialog
1397  **/
1398 KeyInputDialog::KeyInputDialog( QTreeWidget *_table,
1399                                 const QString& keyToChange,
1400                                 QWidget *_parent,
1401                                 bool _b_global ) :
1402                                 QDialog( _parent ), keyValue(0), b_global( _b_global )
1403 {
1404     setModal( true );
1405     conflicts = false;
1406
1407     table = _table;
1408     setWindowTitle( b_global ? qtr( "Global" ): ""
1409                     + qtr( "Hotkey for " ) + keyToChange );
1410     setWindowRole( "vlc-key-input" );
1411
1412     vLayout = new QVBoxLayout( this );
1413     selected = new QLabel( qtr( "Press the new keys for " ) + keyToChange );
1414     vLayout->addWidget( selected , Qt::AlignCenter );
1415
1416     warning = new QLabel;
1417     warning->hide();
1418     vLayout->insertWidget( 1, warning );
1419
1420     buttonBox = new QDialogButtonBox;
1421     QPushButton *ok = new QPushButton( qtr("OK") );
1422     QPushButton *cancel = new QPushButton( qtr("Cancel") );
1423     buttonBox->addButton( ok, QDialogButtonBox::AcceptRole );
1424     buttonBox->addButton( cancel, QDialogButtonBox::RejectRole );
1425     ok->setDefault( true );
1426
1427     vLayout->addWidget( buttonBox );
1428     buttonBox->hide();
1429
1430     CONNECT( buttonBox, accepted(), this, accept() );
1431     CONNECT( buttonBox, rejected(), this, reject() );
1432 }
1433
1434 void KeyInputDialog::checkForConflicts( int i_vlckey )
1435 {
1436      QList<QTreeWidgetItem *> conflictList =
1437          table->findItems( VLCKeyToString( i_vlckey ), Qt::MatchExactly,
1438                            b_global ? 2 : 1 );
1439
1440     if( conflictList.size() &&
1441         conflictList[0]->data( b_global ? 2 : 1, Qt::UserRole ).toInt() > 1 )
1442         /* Avoid 0 or -1 that are the "Unset" states */
1443     {
1444         warning->setText( qtr("Warning: the key is already assigned to \"") +
1445                           conflictList[0]->text( 0 ) + "\"" );
1446         warning->show();
1447         buttonBox->show();
1448
1449         conflicts = true;
1450     }
1451     else accept();
1452 }
1453
1454 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
1455 {
1456     if( e->key() == Qt::Key_Tab ||
1457         e->key() == Qt::Key_Shift ||
1458         e->key() == Qt::Key_Control ||
1459         e->key() == Qt::Key_Meta ||
1460         e->key() == Qt::Key_Alt ||
1461         e->key() == Qt::Key_AltGr )
1462         return;
1463     int i_vlck = qtEventToVLCKey( e );
1464     selected->setText( qtr( "Key: " ) + VLCKeyToString( i_vlck ) );
1465     checkForConflicts( i_vlck );
1466     keyValue = i_vlck;
1467 }
1468
1469 void KeyInputDialog::wheelEvent( QWheelEvent *e )
1470 {
1471     int i_vlck = qtWheelEventToVLCKey( e );
1472     selected->setText( qtr( "Key: " ) + VLCKeyToString( i_vlck ) );
1473     checkForConflicts( i_vlck );
1474     keyValue = i_vlck;
1475 }
1476
1477 void KeyShortcutEdit::mousePressEvent( QMouseEvent *)
1478 {
1479     emit pressed();
1480 }
1481