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