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