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