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