]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Merge branch 'master' into lpcm_encoder
[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        const char *help = module_get_help( p_parser ); \
740        if( help != NULL ) \
741            cb->setToolTip( formatTooltip( qtr( help ) ) ); \
742        cbl->checkBox = cb; \
743 \
744        cbl->psz_module = strdup( module_get_object( p_parser ) ); \
745        modules.push_back( cbl ); \
746 \
747        if( p_item->value.psz && strstr( p_item->value.psz, cbl->psz_module ) ) \
748             cbl->checkBox->setChecked( true ); \
749 }
750
751
752 void ModuleListConfigControl::finish( bool bycat )
753 {
754     module_t *p_parser;
755
756     /* build a list of available modules */
757     module_t **p_list = module_list_get( NULL );
758     for( size_t i = 0; (p_parser = p_list[i]) != NULL; i++ )
759     {
760         if( bycat )
761         {
762             if( !strcmp( module_get_object( p_parser ), "main" ) ) continue;
763
764             unsigned confsize;
765             module_config_t *p_config = module_config_get (p_parser, &confsize);
766
767             for (size_t i = 0; i < confsize; i++)
768             {
769                 module_config_t *p_cfg = p_config + i;
770                 /* Hack: required subcategory is stored in i_min */
771                 if( p_cfg->i_type == CONFIG_SUBCATEGORY &&
772                         p_cfg->value.i == p_item->min.i )
773                 {
774                     CHECKBOX_LISTS;
775                 }
776             }
777             module_config_free (p_config);
778         }
779         else if( module_provides( p_parser, p_item->psz_type ) )
780         {
781             CHECKBOX_LISTS;
782         }
783     }
784     module_list_free( p_list );
785
786     if( p_item->psz_longtext )
787     {
788         QString tipText = qtr(p_item->psz_longtext);
789
790         text->setToolTip( formatTooltip(tipText) );
791         assert( groupBox );
792         groupBox->setToolTip( formatTooltip(tipText) );
793    }
794 }
795 #undef CHECKBOX_LISTS
796
797 QString ModuleListConfigControl::getValue()
798 {
799     assert( text );
800     return text->text();
801 }
802
803 void ModuleListConfigControl::hide()
804 {
805     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
806          it != modules.end(); it++ )
807     {
808         (*it)->checkBox->hide();
809     }
810     groupBox->hide();
811 }
812
813 void ModuleListConfigControl::show()
814 {
815     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
816          it != modules.end(); it++ )
817     {
818         (*it)->checkBox->show();
819     }
820     groupBox->show();
821 }
822
823
824 void ModuleListConfigControl::onUpdate()
825 {
826     text->clear();
827     bool first = true;
828
829     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
830          it != modules.end(); it++ )
831     {
832         if( (*it)->checkBox->isChecked() )
833         {
834             if( first )
835             {
836                 text->setText( text->text() + (*it)->psz_module );
837                 first = false;
838             }
839             else
840             {
841                 text->setText( text->text() + ":" + (*it)->psz_module );
842             }
843         }
844     }
845 }
846
847 /**************************************************************************
848  * Integer-based controls
849  *************************************************************************/
850
851 /*********** Integer **************/
852 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
853                                             module_config_t *_p_item,
854                                             QWidget *_parent, QGridLayout *l,
855                                             int &line ) :
856                            VIntConfigControl( _p_this, _p_item, _parent )
857 {
858     label = new QLabel( qtr(p_item->psz_text) );
859     spin = new QSpinBox; spin->setMinimumWidth( MINWIDTH_BOX );
860     spin->setAlignment( Qt::AlignRight );
861     spin->setMaximumWidth( MINWIDTH_BOX );
862     finish();
863
864     if( !l )
865     {
866         QHBoxLayout *layout = new QHBoxLayout();
867         layout->addWidget( label, 0 ); layout->addWidget( spin, LAST_COLUMN );
868         widget->setLayout( layout );
869     }
870     else
871     {
872         l->addWidget( label, line, 0 );
873         l->addWidget( spin, line, LAST_COLUMN, Qt::AlignRight );
874     }
875 }
876 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
877                                             module_config_t *_p_item,
878                                             QLabel *_label, QSpinBox *_spin ) :
879                                       VIntConfigControl( _p_this, _p_item )
880 {
881     spin = _spin;
882     label = _label;
883     finish();
884 }
885
886 void IntegerConfigControl::finish()
887 {
888     spin->setMaximum( 2000000000 );
889     spin->setMinimum( -2000000000 );
890     spin->setValue( p_item->value.i );
891
892     if( p_item->psz_longtext )
893     {
894         QString tipText = qtr(p_item->psz_longtext);
895         spin->setToolTip( formatTooltip(tipText) );
896         if( label )
897             label->setToolTip( formatTooltip(tipText) );
898     }
899     if( label )
900         label->setBuddy( spin );
901 }
902
903 int IntegerConfigControl::getValue()
904 {
905     return spin->value();
906 }
907
908 /********* Integer range **********/
909 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
910                                             module_config_t *_p_item,
911                                             QWidget *_parent, QGridLayout *l,
912                                             int &line ) :
913             IntegerConfigControl( _p_this, _p_item, _parent, l, line )
914 {
915     finish();
916 }
917
918 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
919                                             module_config_t *_p_item,
920                                             QLabel *_label, QSpinBox *_spin ) :
921             IntegerConfigControl( _p_this, _p_item, _label, _spin )
922 {
923     finish();
924 }
925
926 void IntegerRangeConfigControl::finish()
927 {
928     spin->setMaximum( p_item->max.i );
929     spin->setMinimum( p_item->min.i );
930 }
931
932 IntegerRangeSliderConfigControl::IntegerRangeSliderConfigControl(
933                                             vlc_object_t *_p_this,
934                                             module_config_t *_p_item,
935                                             QLabel *_label, QSlider *_slider ):
936                     VIntConfigControl( _p_this, _p_item )
937 {
938     slider = _slider;
939     label = _label;
940     slider->setMaximum( p_item->max.i );
941     slider->setMinimum( p_item->min.i );
942     slider->setValue( p_item->value.i );
943     if( p_item->psz_longtext )
944     {
945         QString tipText = qtr(p_item->psz_longtext);
946         slider->setToolTip( formatTooltip(tipText) );
947         if( label )
948             label->setToolTip( formatTooltip(tipText) );
949     }
950     if( label )
951         label->setBuddy( slider );
952 }
953
954 int IntegerRangeSliderConfigControl::getValue()
955 {
956         return slider->value();
957 }
958
959
960 /********* Integer / choice list **********/
961 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
962                module_config_t *_p_item, QWidget *_parent, bool bycat,
963                QGridLayout *l, int &line) :
964                VIntConfigControl( _p_this, _p_item, _parent )
965 {
966     label = new QLabel( qtr(p_item->psz_text) );
967     combo = new QComboBox();
968     combo->setMinimumWidth( MINWIDTH_BOX );
969
970     module_config_t *p_module_config = config_FindConfig( p_this, p_item->psz_name );
971
972     finish( p_module_config, bycat );
973     if( !l )
974     {
975         QHBoxLayout *layout = new QHBoxLayout();
976         layout->addWidget( label ); layout->addWidget( combo, LAST_COLUMN );
977         widget->setLayout( layout );
978     }
979     else
980     {
981         l->addWidget( label, line, 0 );
982         l->addWidget( combo, line, LAST_COLUMN, Qt::AlignRight );
983     }
984
985     if( p_item->i_action )
986     {
987         QSignalMapper *signalMapper = new QSignalMapper(this);
988
989         /* Some stringLists like Capture listings have action associated */
990         for( int i = 0; i < p_item->i_action; i++ )
991         {
992             QPushButton *button =
993                 new QPushButton( qfu( p_item->ppsz_action_text[i] ));
994             CONNECT( button, clicked(), signalMapper, map() );
995             signalMapper->setMapping( button, i );
996             l->addWidget( button, line, LAST_COLUMN - p_item->i_action + i,
997                     Qt::AlignRight );
998         }
999         CONNECT( signalMapper, mapped( int ),
1000                 this, actionRequested( int ) );
1001     }
1002
1003 }
1004 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
1005                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
1006                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
1007 {
1008     combo = _combo;
1009     label = _label;
1010
1011     module_config_t *p_module_config = config_FindConfig( p_this, getName() );
1012
1013     finish( p_module_config, bycat );
1014 }
1015
1016 void IntegerListConfigControl::finish(module_config_t *p_module_config, bool bycat )
1017 {
1018     combo->setEditable( false );
1019
1020     if(!p_module_config) return;
1021
1022     if( p_module_config->pf_update_list )
1023     {
1024        vlc_value_t val;
1025        val.i_int = p_module_config->value.i;
1026
1027        p_module_config->pf_update_list(p_this, p_item->psz_name, val, val, NULL);
1028
1029        // assume in any case that dirty was set to true
1030        // because lazy programmes will use the same callback for
1031        // this, like the one behind the refresh push button?
1032        p_module_config->b_dirty = false;
1033     }
1034
1035     for( int i_index = 0; i_index < p_module_config->i_list; i_index++ )
1036     {
1037         combo->addItem( qtr(p_module_config->ppsz_list_text[i_index] ),
1038                         QVariant( p_module_config->pi_list[i_index] ) );
1039         if( p_module_config->value.i == p_module_config->pi_list[i_index] )
1040             combo->setCurrentIndex( combo->count() - 1 );
1041     }
1042     if( p_item->psz_longtext )
1043     {
1044         QString tipText = qtr(p_item->psz_longtext );
1045         combo->setToolTip( formatTooltip(tipText) );
1046         if( label )
1047             label->setToolTip( formatTooltip(tipText) );
1048     }
1049     if( label )
1050         label->setBuddy( combo );
1051 }
1052
1053 void IntegerListConfigControl::actionRequested( int i_action )
1054 {
1055     /* Supplementary check for boundaries */
1056     if( i_action < 0 || i_action >= p_item->i_action ) return;
1057
1058     module_config_t *p_module_config = config_FindConfig( p_this, getName() );
1059     if(!p_module_config) return;
1060
1061
1062     vlc_value_t val;
1063     val.i_int = combo->itemData( combo->currentIndex() ).toInt();
1064
1065     p_module_config->ppf_action[i_action]( p_this, getName(), val, val, 0 );
1066
1067     if( p_module_config->b_dirty )
1068     {
1069         combo->clear();
1070         finish( p_module_config, true );
1071         p_module_config->b_dirty = false;
1072     }
1073 }
1074
1075 int IntegerListConfigControl::getValue()
1076 {
1077     return combo->itemData( combo->currentIndex() ).toInt();
1078 }
1079
1080 /*********** Boolean **************/
1081 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
1082                                       module_config_t *_p_item,
1083                                       QWidget *_parent, QGridLayout *l,
1084                                       int &line ) :
1085                     VIntConfigControl( _p_this, _p_item, _parent )
1086 {
1087     checkbox = new QCheckBox( qtr(p_item->psz_text) );
1088     finish();
1089
1090     if( !l )
1091     {
1092         QHBoxLayout *layout = new QHBoxLayout();
1093         layout->addWidget( checkbox, 0 );
1094         widget->setLayout( layout );
1095     }
1096     else
1097     {
1098         l->addWidget( checkbox, line, 0 );
1099     }
1100 }
1101
1102 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
1103                                       module_config_t *_p_item,
1104                                       QLabel *_label,
1105                                       QAbstractButton *_checkbox,
1106                                       bool bycat ) :
1107                    VIntConfigControl( _p_this, _p_item )
1108 {
1109     checkbox = _checkbox;
1110     VLC_UNUSED( _label );
1111     finish();
1112 }
1113
1114 void BoolConfigControl::finish()
1115 {
1116     checkbox->setChecked( p_item->value.i == true );
1117     if( p_item->psz_longtext )
1118         checkbox->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
1119 }
1120
1121 int BoolConfigControl::getValue()
1122 {
1123     return checkbox->isChecked();
1124 }
1125
1126 /**************************************************************************
1127  * Float-based controls
1128  *************************************************************************/
1129
1130 /*********** Float **************/
1131 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
1132                                         module_config_t *_p_item,
1133                                         QWidget *_parent, QGridLayout *l,
1134                                         int &line ) :
1135                     VFloatConfigControl( _p_this, _p_item, _parent )
1136 {
1137     label = new QLabel( qtr(p_item->psz_text) );
1138     spin = new QDoubleSpinBox;
1139     spin->setMinimumWidth( MINWIDTH_BOX );
1140     spin->setMaximumWidth( MINWIDTH_BOX );
1141     spin->setAlignment( Qt::AlignRight );
1142     finish();
1143
1144     if( !l )
1145     {
1146         QHBoxLayout *layout = new QHBoxLayout();
1147         layout->addWidget( label, 0 ); layout->addWidget( spin, LAST_COLUMN );
1148         widget->setLayout( layout );
1149     }
1150     else
1151     {
1152         l->addWidget( label, line, 0 );
1153         l->addWidget( spin, line, LAST_COLUMN, Qt::AlignRight );
1154     }
1155 }
1156
1157 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
1158                                         module_config_t *_p_item,
1159                                         QLabel *_label,
1160                                         QDoubleSpinBox *_spin ) :
1161                     VFloatConfigControl( _p_this, _p_item )
1162 {
1163     spin = _spin;
1164     label = _label;
1165     finish();
1166 }
1167
1168 void FloatConfigControl::finish()
1169 {
1170     spin->setMaximum( 2000000000. );
1171     spin->setMinimum( -2000000000. );
1172     spin->setSingleStep( 0.1 );
1173     spin->setValue( (double)p_item->value.f );
1174     if( p_item->psz_longtext )
1175     {
1176         QString tipText = qtr(p_item->psz_longtext);
1177         spin->setToolTip( formatTooltip(tipText) );
1178         if( label )
1179             label->setToolTip( formatTooltip(tipText) );
1180     }
1181     if( label )
1182         label->setBuddy( spin );
1183 }
1184
1185 float FloatConfigControl::getValue()
1186 {
1187     return (float)spin->value();
1188 }
1189
1190 /*********** Float with range **************/
1191 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
1192                                         module_config_t *_p_item,
1193                                         QWidget *_parent, QGridLayout *l,
1194                                         int &line ) :
1195                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
1196 {
1197     finish();
1198 }
1199
1200 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
1201                                         module_config_t *_p_item,
1202                                         QLabel *_label,
1203                                         QDoubleSpinBox *_spin ) :
1204                 FloatConfigControl( _p_this, _p_item, _label, _spin )
1205 {
1206     finish();
1207 }
1208
1209 void FloatRangeConfigControl::finish()
1210 {
1211     spin->setMaximum( (double)p_item->max.f );
1212     spin->setMinimum( (double)p_item->min.f );
1213 }
1214
1215
1216 /**********************************************************************
1217  * Key selector widget
1218  **********************************************************************/
1219 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
1220                                       module_config_t *_p_item,
1221                                       QWidget *_parent, QGridLayout *l,
1222                                       int &line ) :
1223                                 ConfigControl( _p_this, _p_item, _parent )
1224
1225 {
1226     QWidget *keyContainer = new QWidget;
1227     QGridLayout *gLayout = new QGridLayout( keyContainer );
1228
1229     label = new QLabel(
1230             qtr( "Select an action to change the associated hotkey") );
1231
1232     QLabel *searchLabel = new QLabel( qtr( "Search" ) );
1233     actionSearch = new SearchLineEdit( keyContainer );
1234
1235     table = new QTreeWidget;
1236     table->setColumnCount(3);
1237     table->headerItem()->setText( 0, qtr( "Action" ) );
1238     table->headerItem()->setText( 1, qtr( "Hotkey" ) );
1239     table->headerItem()->setText( 2, qtr( "Global" ) );
1240     table->setAlternatingRowColors( true );
1241     table->setSelectionBehavior( QAbstractItemView::SelectItems );
1242
1243     shortcutValue = new KeyShortcutEdit;
1244     shortcutValue->setReadOnly(true);
1245
1246     QPushButton *clearButton = new QPushButton( qtr( "Clear" ) );
1247     QPushButton *setButton = new QPushButton( qtr( "Apply" ) );
1248     setButton->setDefault( true );
1249     finish();
1250
1251     gLayout->addWidget( label, 0, 0, 1, 4 );
1252     gLayout->addWidget( searchLabel, 1, 0, 1, 2 );
1253     gLayout->addWidget( actionSearch, 1, 2, 1, 2 );
1254     gLayout->addWidget( table, 2, 0, 1, 4 );
1255     gLayout->addWidget( clearButton, 3, 0, 1, 1 );
1256     gLayout->addWidget( shortcutValue, 3, 1, 1, 2 );
1257     gLayout->addWidget( setButton, 3, 3, 1, 1 );
1258
1259     l->addWidget( keyContainer, line, 0, 1, -1 );
1260
1261     CONNECT( clearButton, clicked(), shortcutValue, clear() );
1262     CONNECT( clearButton, clicked(), this, setTheKey() );
1263     BUTTONACT( setButton, setTheKey() );
1264     CONNECT( actionSearch, textChanged( const QString& ),
1265              this, filter( const QString& ) );
1266 }
1267
1268 void KeySelectorControl::finish()
1269 {
1270     if( label && p_item->psz_longtext )
1271         label->setToolTip( formatTooltip( qtr( p_item->psz_longtext ) ) );
1272
1273     /* Fill the table */
1274
1275     /* Get the main Module */
1276     module_t *p_main = module_get_main();
1277     assert( p_main );
1278
1279     /* Access to the module_config_t */
1280     unsigned confsize;
1281     module_config_t *p_config;
1282
1283     p_config = module_config_get (p_main, &confsize);
1284
1285     for (size_t i = 0; i < confsize; i++)
1286     {
1287         module_config_t *p_item = p_config + i;
1288
1289         /* If we are a key option not empty */
1290         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name
1291             && strstr( p_item->psz_name , "key-" )
1292             && !strstr( p_item->psz_name , "global-key" )
1293             && !EMPTY_STR( p_item->psz_text ) )
1294         {
1295             /*
1296                Each tree item has:
1297                 - QString text in column 0
1298                 - QString name in data of column 0
1299                 - KeyValue in String in column 1
1300                 - KeyValue in int64_t in column 1
1301              */
1302             QTreeWidgetItem *treeItem = new QTreeWidgetItem();
1303             treeItem->setText( 0, qtr( p_item->psz_text ) );
1304             treeItem->setData( 0, Qt::UserRole,
1305                                QVariant( qfu( p_item->psz_name ) ) );
1306             treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
1307             treeItem->setData( 1, Qt::UserRole, QVariant( qlonglong( p_item->value.i ) ) );
1308             table->addTopLevelItem( treeItem );
1309             continue;
1310         }
1311
1312         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name
1313                 && strstr( p_item->psz_name , "global-key" )
1314                 && !EMPTY_STR( p_item->psz_text ) )
1315         {
1316             QList<QTreeWidgetItem *> list =
1317                 table->findItems( qtr( p_item->psz_text ), Qt::MatchExactly );
1318             if( list.count() >= 1 )
1319             {
1320                 list[0]->setText( 2, VLCKeyToString( p_item->value.i ) );
1321                 list[0]->setData( 2, Qt::UserRole,
1322                                   QVariant( qlonglong( p_item->value.i ) ) );
1323             }
1324             if( list.count() >= 2 )
1325                 msg_Dbg( p_this, "This is probably wrong, %s", p_item->psz_text );
1326         }
1327     }
1328     module_config_free (p_config);
1329     module_release (p_main);
1330
1331     table->resizeColumnToContents( 0 );
1332
1333     CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
1334              this, selectKey( QTreeWidgetItem *, int ) );
1335     CONNECT( table, itemClicked( QTreeWidgetItem *, int ),
1336              this, select( QTreeWidgetItem *, int) );
1337     CONNECT( table, itemSelectionChanged(),
1338              this, select1Key() );
1339
1340     CONNECT( shortcutValue, pressed(), this, selectKey() );
1341 }
1342
1343 void KeySelectorControl::filter( const QString &qs_search )
1344 {
1345     QList<QTreeWidgetItem *> resultList =
1346             table->findItems( qs_search, Qt::MatchContains, 0 );
1347     for( int i = 0; i < table->topLevelItemCount(); i++ )
1348     {
1349         table->topLevelItem( i )->setHidden(
1350                 !resultList.contains( table->topLevelItem( i ) ) );
1351     }
1352 }
1353
1354 void KeySelectorControl::select( QTreeWidgetItem *keyItem, int column )
1355 {
1356     shortcutValue->setGlobal( column == 2 );
1357 }
1358
1359 /* Show the key selected from the table in the keySelector */
1360 void KeySelectorControl::select1Key()
1361 {
1362     QTreeWidgetItem *keyItem = table->currentItem();
1363     shortcutValue->setText( keyItem->text( 1 ) );
1364     shortcutValue->setValue( keyItem->data( 1, Qt::UserRole ).toInt() );
1365     shortcutValue->setGlobal( false );
1366 }
1367
1368 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem, int column )
1369 {
1370     /* This happens when triggered by ClickEater */
1371     if( keyItem == NULL ) keyItem = table->currentItem();
1372
1373     /* This can happen when nothing is selected on the treeView
1374        and the shortcutValue is clicked */
1375     if( !keyItem ) return;
1376
1377     /* If clicked on the first column, assuming user wants the normal hotkey */
1378     if( column == 0 ) column = 1;
1379
1380     bool b_global = ( column == 2 );
1381
1382     /* Launch a small dialog to ask for a new key */
1383     KeyInputDialog *d = new KeyInputDialog( table, keyItem->text( 0 ), widget, b_global );
1384     d->exec();
1385
1386     if( d->result() == QDialog::Accepted )
1387     {
1388         int newValue = d->keyValue;
1389         shortcutValue->setText( VLCKeyToString( newValue ) );
1390         shortcutValue->setValue( newValue );
1391         shortcutValue->setGlobal( b_global );
1392
1393         if( d->conflicts )
1394         {
1395             QTreeWidgetItem *it;
1396             for( int i = 0; i < table->topLevelItemCount() ; i++ )
1397             {
1398                 it = table->topLevelItem(i);
1399                 if( ( keyItem != it ) &&
1400                     ( it->data( b_global ? 2: 1, Qt::UserRole ).toInt() == newValue ) )
1401                 {
1402                     it->setData( b_global ? 2 : 1, Qt::UserRole, QVariant( -1 ) );
1403                     it->setText( b_global ? 2 : 1, qtr( "Unset" ) );
1404                 }
1405             }
1406             /* We already made an OK once. */
1407             setTheKey();
1408         }
1409     }
1410     delete d;
1411 }
1412
1413 void KeySelectorControl::setTheKey()
1414 {
1415     if( !table->currentItem() ) return;
1416     table->currentItem()->setText( shortcutValue->getGlobal() ? 2 : 1,
1417                                    shortcutValue->text() );
1418     table->currentItem()->setData( shortcutValue->getGlobal() ? 2 : 1,
1419                                    Qt::UserRole, shortcutValue->getValue() );
1420 }
1421
1422 void KeySelectorControl::doApply()
1423 {
1424     QTreeWidgetItem *it;
1425     for( int i = 0; i < table->topLevelItemCount() ; i++ )
1426     {
1427         it = table->topLevelItem(i);
1428         if( it->data( 1, Qt::UserRole ).toInt() >= 0 )
1429             config_PutInt( p_this,
1430                            qtu( it->data( 0, Qt::UserRole ).toString() ),
1431                            it->data( 1, Qt::UserRole ).toInt() );
1432         if( it->data( 2, Qt::UserRole ).toInt() >= 0 )
1433             config_PutInt( p_this,
1434                            qtu( "global-" + it->data( 0, Qt::UserRole ).toString() ),
1435                            it->data( 2, Qt::UserRole ).toInt() );
1436
1437     }
1438 }
1439
1440 /**
1441  * Class KeyInputDialog
1442  **/
1443 KeyInputDialog::KeyInputDialog( QTreeWidget *_table,
1444                                 const QString& keyToChange,
1445                                 QWidget *_parent,
1446                                 bool _b_global ) :
1447                                 QDialog( _parent ), keyValue(0), b_global( _b_global )
1448 {
1449     setModal( true );
1450     conflicts = false;
1451
1452     table = _table;
1453     setWindowTitle( b_global ? qtr( "Global" ): ""
1454                     + qtr( "Hotkey for " ) + keyToChange );
1455     setWindowRole( "vlc-key-input" );
1456
1457     vLayout = new QVBoxLayout( this );
1458     selected = new QLabel( qtr( "Press the new keys for " ) + keyToChange );
1459     vLayout->addWidget( selected , Qt::AlignCenter );
1460
1461     warning = new QLabel;
1462     warning->hide();
1463     vLayout->insertWidget( 1, warning );
1464
1465     buttonBox = new QDialogButtonBox;
1466     QPushButton *ok = new QPushButton( qtr("OK") );
1467     QPushButton *cancel = new QPushButton( qtr("Cancel") );
1468     buttonBox->addButton( ok, QDialogButtonBox::AcceptRole );
1469     buttonBox->addButton( cancel, QDialogButtonBox::RejectRole );
1470     ok->setDefault( true );
1471
1472     vLayout->addWidget( buttonBox );
1473     buttonBox->hide();
1474
1475     CONNECT( buttonBox, accepted(), this, accept() );
1476     CONNECT( buttonBox, rejected(), this, reject() );
1477 }
1478
1479 void KeyInputDialog::checkForConflicts( int i_vlckey )
1480 {
1481      QList<QTreeWidgetItem *> conflictList =
1482          table->findItems( VLCKeyToString( i_vlckey ), Qt::MatchExactly,
1483                            b_global ? 2 : 1 );
1484
1485     if( conflictList.size() &&
1486         conflictList[0]->data( b_global ? 2 : 1, Qt::UserRole ).toInt() > 1 )
1487         /* Avoid 0 or -1 that are the "Unset" states */
1488     {
1489         warning->setText( qtr("Warning: the key is already assigned to \"") +
1490                           conflictList[0]->text( 0 ) + "\"" );
1491         warning->show();
1492         buttonBox->show();
1493
1494         conflicts = true;
1495     }
1496     else accept();
1497 }
1498
1499 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
1500 {
1501     if( e->key() == Qt::Key_Tab ||
1502         e->key() == Qt::Key_Shift ||
1503         e->key() == Qt::Key_Control ||
1504         e->key() == Qt::Key_Meta ||
1505         e->key() == Qt::Key_Alt ||
1506         e->key() == Qt::Key_AltGr )
1507         return;
1508     int i_vlck = qtEventToVLCKey( e );
1509     selected->setText( qtr( "Key: " ) + VLCKeyToString( i_vlck ) );
1510     checkForConflicts( i_vlck );
1511     keyValue = i_vlck;
1512 }
1513
1514 void KeyInputDialog::wheelEvent( QWheelEvent *e )
1515 {
1516     int i_vlck = qtWheelEventToVLCKey( e );
1517     selected->setText( qtr( "Key: " ) + VLCKeyToString( i_vlck ) );
1518     checkForConflicts( i_vlck );
1519     keyValue = i_vlck;
1520 }
1521
1522 void KeyShortcutEdit::mousePressEvent( QMouseEvent *)
1523 {
1524     emit pressed();
1525 }
1526