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