]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Save hotkeys (Closes:#714)
[vlc] / modules / gui / qt4 / components / preferences_widgets.cpp
1 /*****************************************************************************
2  * preferences_widgets.cpp : Widgets for preferences displays
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Antoine Cellerier <dionoea@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /**
26  * Todo:
27  *  - Finish implementation (see WX)
28  *  - Improvements over WX
29  *      - Password field implementation (through "pwd" bool param
30  *      - Validator for modulelist
31  *  - Implement update stuff using a general Updated signal
32  */
33
34 #include "components/preferences_widgets.hpp"
35 #include "util/customwidgets.hpp"
36 #include "qt4.hpp"
37
38 #include <QLineEdit>
39 #include <QString>
40 #include <QSpinBox>
41 #include <QDoubleSpinBox>
42 #include <QVariant>
43 #include <QComboBox>
44 #include <QGridLayout>
45 #include <QPushButton>
46
47 #include <vlc_keys.h>
48
49 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
50                                              module_config_t *p_item,
51                                              QWidget *parent )
52 {
53     int i=0;
54     return createControl( p_this, p_item, parent, NULL, i );
55 }
56
57 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
58                                              module_config_t *p_item,
59                                              QWidget *parent,
60                                              QGridLayout *l, int &line )
61 {
62     ConfigControl *p_control = NULL;
63     if( p_item->psz_current ) return NULL;
64
65     switch( p_item->i_type )
66     {
67     case CONFIG_ITEM_MODULE:
68         p_control = new ModuleConfigControl( p_this, p_item, parent, false,
69                                              l, line );
70         break;
71     case CONFIG_ITEM_MODULE_CAT:
72         p_control = new ModuleConfigControl( p_this, p_item, parent, true,
73                                              l, line );
74         break;
75     case CONFIG_ITEM_MODULE_LIST:
76         p_control = new ModuleListConfigControl( p_this, p_item, parent, false,
77                                              l, line );
78         break;
79     case CONFIG_ITEM_MODULE_LIST_CAT:
80         p_control = new ModuleListConfigControl( p_this, p_item, parent, true,
81                                              l, line );
82         break;
83     case CONFIG_ITEM_STRING:
84         if( !p_item->i_list )
85             p_control = new StringConfigControl( p_this, p_item, parent,
86                                                  l, line, false );
87         else
88             p_control = new StringListConfigControl( p_this, p_item,
89                                             parent, false, l, line );
90         break;
91     case CONFIG_ITEM_INTEGER:
92         if( p_item->i_list )
93             p_control = new IntegerListConfigControl( p_this, p_item,
94                                             parent, false, l, line );
95         else if( p_item->i_min || p_item->i_max )
96             p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
97                                                        l, line );
98         else
99             p_control = new IntegerConfigControl( p_this, p_item, parent,
100                                                   l, line );
101         break;
102     case CONFIG_ITEM_FILE:
103         fprintf( stderr, "Todo (CONFIG_ITEM_FILE)\n" );
104         break;
105     case CONFIG_ITEM_DIRECTORY:
106         fprintf( stderr, "Todo (CONFIG_ITEM_DIRECTORY)\n" );
107         break;
108     case CONFIG_ITEM_KEY:
109         p_control = new KeySelectorControl( p_this, p_item, parent, l, line );
110         break;
111     case CONFIG_ITEM_BOOL:
112         p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
113         break;
114     case CONFIG_ITEM_FLOAT:
115         if( p_item->f_min || p_item->f_max )
116             p_control = new FloatRangeConfigControl( p_this, p_item, parent,
117                                                      l, line );
118         else
119             p_control = new FloatConfigControl( p_this, p_item, parent,
120                                                   l, line );
121         break;
122     default:
123         break;
124     }
125     return p_control;
126 }
127
128 void ConfigControl::doApply( intf_thread_t *p_intf )
129 {
130     switch( getType() )
131     {
132         case 1:
133         {
134             VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(this);
135             assert( vicc );
136             config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
137             break;
138         }
139         case 2:
140         {
141             VFloatConfigControl *vfcc =
142                                     qobject_cast<VFloatConfigControl *>(this);
143             assert( vfcc );
144             config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
145             break;
146         }
147         case 3:
148         {
149             VStringConfigControl *vscc =
150                             qobject_cast<VStringConfigControl *>(this);
151             assert( vscc );
152             config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
153             break;
154         }
155         case 4:
156         {
157             KeySelectorControl *ksc = qobject_cast<KeySelectorControl *>(this);
158             assert( ksc );
159             ksc->doApply();
160         }
161     }
162 }
163
164 /**************************************************************************
165  * String-based controls
166  *************************************************************************/
167
168 /*********** String **************/
169 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
170                                           module_config_t *_p_item,
171                                           QWidget *_parent, QGridLayout *l,
172                                           int &line, bool pwd ) :
173                            VStringConfigControl( _p_this, _p_item, _parent )
174 {
175     label = new QLabel( qfu(p_item->psz_text) );
176     text = new QLineEdit( qfu(p_item->psz_value) );
177     finish();
178
179     if( !l )
180     {
181         QHBoxLayout *layout = new QHBoxLayout();
182         layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
183         widget->setLayout( layout );
184     }
185     else
186     {
187         l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
188     }
189 }
190
191 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
192                                    module_config_t *_p_item,
193                                    QLabel *_label, QLineEdit *_text, bool pwd ):
194                            VStringConfigControl( _p_this, _p_item )
195 {
196     text = _text;
197     label = _label;
198     finish( );
199 }
200
201 void StringConfigControl::finish()
202 {
203     text->setText( qfu(p_item->psz_value) );
204     text->setToolTip( qfu(p_item->psz_longtext) );
205     if( label )
206         label->setToolTip( qfu(p_item->psz_longtext) );
207 }
208
209 /********* String / choice list **********/
210 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
211                module_config_t *_p_item, QWidget *_parent, bool bycat,
212                QGridLayout *l, int &line) :
213                VStringConfigControl( _p_this, _p_item, _parent )
214 {
215     label = new QLabel( qfu(p_item->psz_text) );
216     combo = new QComboBox();
217     finish( bycat );
218     if( !l )
219     {
220         QHBoxLayout *layout = new QHBoxLayout();
221         layout->addWidget( label ); layout->addWidget( combo );
222         widget->setLayout( layout );
223     }
224     else
225     {
226         l->addWidget( label, line, 0 );
227         l->addWidget( combo, line, 1, Qt::AlignRight );
228     }
229 }
230 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
231                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
232                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
233 {
234     combo = _combo;
235     label = _label;
236     finish( bycat );
237 }
238
239 void StringListConfigControl::finish( bool bycat )
240 {
241     combo->setEditable( false );
242
243     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
244     {
245         combo->addItem( qfu(p_item->ppsz_list_text ?
246                             p_item->ppsz_list_text[i_index] :
247                             p_item->ppsz_list[i_index] ),
248                         QVariant( p_item->ppsz_list[i_index] ) );
249         if( p_item->psz_value && !strcmp( p_item->psz_value,
250                                           p_item->ppsz_list[i_index] ) )
251             combo->setCurrentIndex( combo->count() - 1 );
252     }
253     combo->setToolTip( qfu(p_item->psz_longtext) );
254     if( label )
255         label->setToolTip( qfu(p_item->psz_longtext) );
256 }
257
258 QString StringListConfigControl::getValue()
259 {
260     return combo->itemData( combo->currentIndex() ).toString();
261 }
262
263 /********* Module **********/
264 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
265                module_config_t *_p_item, QWidget *_parent, bool bycat,
266                QGridLayout *l, int &line) :
267                VStringConfigControl( _p_this, _p_item, _parent )
268 {
269     label = new QLabel( qfu(p_item->psz_text) );
270     combo = new QComboBox();
271     finish( bycat );
272     if( !l )
273     {
274         QHBoxLayout *layout = new QHBoxLayout();
275         layout->addWidget( label ); layout->addWidget( combo );
276         widget->setLayout( layout );
277     }
278     else
279     {
280         l->addWidget( label, line, 0 );
281         l->addWidget( combo, line, 1, Qt::AlignRight );
282     }
283 }
284 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
285                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
286                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
287 {
288     combo = _combo;
289     label = _label;
290     finish( bycat );
291 }
292
293 void ModuleConfigControl::finish( bool bycat )
294 {
295     vlc_list_t *p_list;
296     module_t *p_parser;
297
298     combo->setEditable( false );
299
300     /* build a list of available modules */
301     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
302     combo->addItem( qtr("Default") );
303     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
304     {
305         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
306
307         if( bycat )
308         {
309             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
310
311             module_config_t *p_config = p_parser->p_config;
312             if( p_config ) do
313             {
314                 /* Hack: required subcategory is stored in i_min */
315                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
316                     p_config->i_value == p_item->i_min )
317                     combo->addItem( qfu(p_parser->psz_longname),
318                                     QVariant( p_parser->psz_object_name ) );
319                 if( p_item->psz_value && !strcmp( p_item->psz_value,
320                                                   p_parser->psz_object_name) )
321                     combo->setCurrentIndex( combo->count() - 1 );
322             } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
323         }
324         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
325         {
326             combo->addItem( qfu(p_parser->psz_longname),
327                             QVariant( p_parser->psz_object_name ) );
328             if( p_item->psz_value && !strcmp( p_item->psz_value,
329                                               p_parser->psz_object_name) )
330                 combo->setCurrentIndex( combo->count() - 1 );
331         }
332     }
333     vlc_list_release( p_list );
334     combo->setToolTip( qfu(p_item->psz_longtext) );
335     if( label )
336         label->setToolTip( qfu(p_item->psz_longtext) );
337 }
338
339 QString ModuleConfigControl::getValue()
340 {
341     return combo->itemData( combo->currentIndex() ).toString();
342 }
343
344 /********* Module list **********/
345 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
346                module_config_t *_p_item, QWidget *_parent, bool bycat,
347                QGridLayout *l, int &line) :
348                VStringConfigControl( _p_this, _p_item, _parent )
349 {
350     label = new QLabel( qfu(p_item->psz_text) );
351     text = new QLineEdit();
352     finish( bycat );
353
354     bool pom = false;
355     if( !l )
356     {
357         l = new QGridLayout();
358         line = 0;
359         pom = true;
360     }
361     for( QVector<QCheckBox*>::iterator it = modules.begin();
362          it != modules.end(); it++ )
363     {
364         l->addWidget( *it, line++, 1 );
365     }
366     l->addWidget( label, line, 0 );
367     l->addWidget( text, line, 1 );
368     if( pom )
369         widget->setLayout( l );
370 }
371 #if 0
372 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
373                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
374                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
375 {
376     combo = _combo;
377     label = _label;
378     finish( bycat );
379 }
380 #endif
381
382 ModuleListConfigControl::~ModuleListConfigControl()
383 {
384     for( QVector<QCheckBox*>::iterator it = modules.begin();
385          it != modules.end(); it++ )
386     {
387         delete *it;
388     }
389     delete label;
390     delete text;
391 }
392
393 void ModuleListConfigControl::finish( bool bycat )
394 {
395     vlc_list_t *p_list;
396     module_t *p_parser;
397
398     /* build a list of available modules */
399     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
400     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
401     {
402         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
403
404         if( bycat )
405         {
406             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
407
408             module_config_t *p_config = p_parser->p_config;
409             if( p_config ) do
410             {
411                 /* Hack: required subcategory is stored in i_min */
412                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
413                     p_config->i_value == p_item->i_min )
414                 {
415                     QCheckBox *cb =
416                         new QCheckBox( qfu( p_parser->psz_object_name ) );
417                     cb->setToolTip( qfu(p_parser->psz_longname) );
418                     modules.push_back( cb );
419                 }
420             } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
421         }
422         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
423         {
424             QCheckBox *cb =
425                 new QCheckBox( qfu( p_parser->psz_object_name ) );
426             cb->setToolTip( qfu(p_parser->psz_longname) );
427             modules.push_back( cb );
428         }
429     }
430     vlc_list_release( p_list );
431     text->setToolTip( qfu(p_item->psz_longtext) );
432     if( label )
433         label->setToolTip( qfu(p_item->psz_longtext) );
434 }
435
436 QString ModuleListConfigControl::getValue()
437 {
438     return text->text();
439 }
440
441 void ModuleListConfigControl::hide()
442 {
443     for( QVector<QCheckBox*>::iterator it = modules.begin();
444          it != modules.end(); it++ )
445     {
446         (*it)->hide();
447     }
448     text->hide();
449     label->hide();
450 }
451
452 void ModuleListConfigControl::show()
453 {
454     for( QVector<QCheckBox*>::iterator it = modules.begin();
455          it != modules.end(); it++ )
456     {
457         (*it)->show();
458     }
459     text->show();
460     label->show();
461 }
462
463
464 void ModuleListConfigControl::wakeUp_TheUserJustClickedOnSomething( int value )
465 {
466     text->clear();
467     for( QVector<QCheckBox*>::iterator it = modules.begin();
468          it != modules.end(); it++ )
469     {
470     }
471 }
472
473 /**************************************************************************
474  * Integer-based controls
475  *************************************************************************/
476
477 /*********** Integer **************/
478 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
479                                             module_config_t *_p_item,
480                                             QWidget *_parent, QGridLayout *l,
481                                             int &line ) :
482                            VIntConfigControl( _p_this, _p_item, _parent )
483 {
484     label = new QLabel( qfu(p_item->psz_text) );
485     spin = new QSpinBox; spin->setMinimumWidth( 80 );
486     spin->setMaximumWidth( 90 );
487     finish();
488
489     if( !l )
490     {
491         QHBoxLayout *layout = new QHBoxLayout();
492         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
493         widget->setLayout( layout );
494     }
495     else
496     {
497         l->addWidget( label, line, 0 );
498         l->addWidget( spin, line, 1, Qt::AlignRight );
499     }
500 }
501 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
502                                             module_config_t *_p_item,
503                                             QLabel *_label, QSpinBox *_spin ) :
504                                       VIntConfigControl( _p_this, _p_item )
505 {
506     spin = _spin;
507     label = _label;
508     finish();
509 }
510
511 void IntegerConfigControl::finish()
512 {
513     spin->setMaximum( 2000000000 );
514     spin->setMinimum( -2000000000 );
515     spin->setValue( p_item->i_value );
516     spin->setToolTip( qfu(p_item->psz_longtext) );
517     if( label )
518         label->setToolTip( qfu(p_item->psz_longtext) );
519 }
520
521 int IntegerConfigControl::getValue()
522 {
523     return spin->value();
524 }
525
526 /********* Integer range **********/
527 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
528                                             module_config_t *_p_item,
529                                             QWidget *_parent, QGridLayout *l,
530                                             int &line ) :
531             IntegerConfigControl( _p_this, _p_item, _parent, l, line )
532 {
533     finish();
534 }
535
536 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
537                                             module_config_t *_p_item,
538                                             QLabel *_label, QSpinBox *_spin ) :
539             IntegerConfigControl( _p_this, _p_item, _label, _spin )
540 {
541     finish();
542 }
543
544 void IntegerRangeConfigControl::finish()
545 {
546     spin->setMaximum( p_item->i_max );
547     spin->setMinimum( p_item->i_min );
548 }
549
550 /********* Integer / choice list **********/
551 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
552                module_config_t *_p_item, QWidget *_parent, bool bycat,
553                QGridLayout *l, int &line) :
554                VIntConfigControl( _p_this, _p_item, _parent )
555 {
556     label = new QLabel( qfu(p_item->psz_text) );
557     combo = new QComboBox();
558     finish( bycat );
559     if( !l )
560     {
561         QHBoxLayout *layout = new QHBoxLayout();
562         layout->addWidget( label ); layout->addWidget( combo );
563         widget->setLayout( layout );
564     }
565     else
566     {
567         l->addWidget( label, line, 0 );
568         l->addWidget( combo, line, 1, Qt::AlignRight );
569     }
570 }
571 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
572                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
573                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
574 {
575     combo = _combo;
576     label = _label;
577     finish( bycat );
578 }
579
580 void IntegerListConfigControl::finish( bool bycat )
581 {
582     combo->setEditable( false );
583
584     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
585     {
586         combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
587                         QVariant( p_item->pi_list[i_index] ) );
588         if( p_item->i_value == p_item->pi_list[i_index] )
589             combo->setCurrentIndex( combo->count() - 1 );
590     }
591     combo->setToolTip( qfu(p_item->psz_longtext) );
592     if( label )
593         label->setToolTip( qfu(p_item->psz_longtext) );
594 }
595
596 int IntegerListConfigControl::getValue()
597 {
598     return combo->itemData( combo->currentIndex() ).toInt();
599 }
600
601 /*********** Boolean **************/
602 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
603                                       module_config_t *_p_item,
604                                       QWidget *_parent, QGridLayout *l,
605                                       int &line ) :
606                     VIntConfigControl( _p_this, _p_item, _parent )
607 {
608     checkbox = new QCheckBox( qfu(p_item->psz_text) );
609     finish();
610
611     if( !l )
612     {
613         QHBoxLayout *layout = new QHBoxLayout();
614         layout->addWidget( checkbox, 0 );
615         widget->setLayout( layout );
616     }
617     else
618     {
619         l->addWidget( checkbox, line, 0 );
620     }
621 }
622 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
623                                       module_config_t *_p_item,
624                                       QLabel *_label,
625                                       QCheckBox *_checkbox,
626                                       bool bycat ) :
627                    VIntConfigControl( _p_this, _p_item )
628 {
629     checkbox = _checkbox;
630     finish();
631 }
632
633 void BoolConfigControl::finish()
634 {
635     checkbox->setCheckState( p_item->i_value == VLC_TRUE ? Qt::Checked
636                                                         : Qt::Unchecked );
637     checkbox->setToolTip( qfu(p_item->psz_longtext) );
638 }
639
640 int BoolConfigControl::getValue()
641 {
642     return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
643 }
644
645 /**************************************************************************
646  * Float-based controls
647  *************************************************************************/
648
649 /*********** Float **************/
650 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
651                                         module_config_t *_p_item,
652                                         QWidget *_parent, QGridLayout *l,
653                                         int &line ) :
654                     VFloatConfigControl( _p_this, _p_item, _parent )
655 {
656     label = new QLabel( qfu(p_item->psz_text) );
657     spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
658     spin->setMaximumWidth( 90 );
659     finish();
660
661     if( !l )
662     {
663         QHBoxLayout *layout = new QHBoxLayout();
664         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
665         widget->setLayout( layout );
666     }
667     else
668     {
669         l->addWidget( label, line, 0 );
670         l->addWidget( spin, line, 1, Qt::AlignRight );
671     }
672 }
673
674 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
675                                         module_config_t *_p_item,
676                                         QLabel *_label,
677                                         QDoubleSpinBox *_spin ) :
678                     VFloatConfigControl( _p_this, _p_item )
679 {
680     spin = _spin;
681     label = _label;
682     finish();
683 }
684
685 void FloatConfigControl::finish()
686 {
687     spin->setMaximum( 2000000000. );
688     spin->setMinimum( -2000000000. );
689     spin->setSingleStep( 0.1 );
690     spin->setValue( (double)p_item->f_value );
691     spin->setToolTip( qfu(p_item->psz_longtext) );
692     if( label )
693         label->setToolTip( qfu(p_item->psz_longtext) );
694 }
695
696 float FloatConfigControl::getValue()
697 {
698     return (float)spin->value();
699 }
700
701 /*********** Float with range **************/
702 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
703                                         module_config_t *_p_item,
704                                         QWidget *_parent, QGridLayout *l,
705                                         int &line ) :
706                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
707 {
708     finish();
709 }
710
711 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
712                                         module_config_t *_p_item,
713                                         QLabel *_label,
714                                         QDoubleSpinBox *_spin ) :
715                 FloatConfigControl( _p_this, _p_item, _label, _spin )
716 {
717     finish();
718 }
719
720 void FloatRangeConfigControl::finish()
721 {
722     spin->setMaximum( (double)p_item->f_max );
723     spin->setMinimum( (double)p_item->f_min );
724 }
725
726
727 /**********************************************************************
728  * Key selector widget
729  **********************************************************************/
730 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
731                                       module_config_t *_p_item,
732                                       QWidget *_parent, QGridLayout *l,
733                                       int &line ) :
734                                 ConfigControl( _p_this, _p_item, _parent )
735
736 {
737     label = new QLabel( qtr("Select an action to change the associated hotkey") );
738     table = new QTreeWidget( 0 );
739     finish();
740
741     if( !l )
742     {
743         QVBoxLayout *layout = new QVBoxLayout();
744         layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
745         widget->setLayout( layout );
746     }
747     else
748     {
749         l->addWidget( label, line, 0, 1, 2 );
750         l->addWidget( table, line+1, 0, 1,2 );
751     }
752 }
753
754 void KeySelectorControl::finish()
755 {
756     if( label )
757         label->setToolTip( qfu(p_item->psz_longtext) );
758
759     /* Fill the table */
760     table->setColumnCount( 2 );
761     table->setAlternatingRowColors( true );
762
763     module_t *p_main = config_FindModule( p_this, "main" );
764     assert( p_main );
765     module_config_t *p_item = p_main->p_config;
766
767     if( p_item ) do
768     {
769         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
770             strstr( p_item->psz_name , "key-" ) )
771         {
772             QTreeWidgetItem *treeItem = new QTreeWidgetItem();
773             treeItem->setText( 0, qfu( p_item->psz_text ) );
774             treeItem->setText( 1, VLCKeyToString( p_item->i_value ) );
775             treeItem->setData( 0, Qt::UserRole,
776                                   QVariant::fromValue( (void*)p_item ) );
777             values += p_item;
778             table->addTopLevelItem( treeItem );
779         }
780     } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
781     table->resizeColumnToContents( 0 );
782
783     CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
784              this, selectKey( QTreeWidgetItem * ) );
785 }
786
787 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
788 {
789    module_config_t *p_keyItem = static_cast<module_config_t*>
790                           (keyItem->data( 0, Qt::UserRole ).value<void*>());
791
792     KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text );
793     d->exec();
794     if( d->result() == QDialog::Accepted )
795     {
796         p_keyItem->i_value = d->keyValue;
797         if( d->conflicts )
798         {
799             for( int i = 0; i < table->topLevelItemCount() ; i++ )
800             {
801                 QTreeWidgetItem *it = table->topLevelItem(i);
802                 module_config_t *p_item = static_cast<module_config_t*>
803                               (it->data( 0, Qt::UserRole ).value<void*>());
804                 it->setText( 1, VLCKeyToString( p_item->i_value ) );
805             }
806         }
807         else
808             keyItem->setText( 1, VLCKeyToString( p_keyItem->i_value ) );
809     }
810     delete d;
811 }
812
813 void KeySelectorControl::doApply()
814 {
815     foreach( module_config_t *p_current, values )
816     {
817         config_PutInt( p_this, p_current->psz_name, p_current->i_value );
818     }
819 }
820
821 KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
822                                 char * _keyToChange ) :
823                                                 QDialog(0), keyValue(0)
824 {
825     setModal( true );
826     values = _values;
827     conflicts = false;
828     keyToChange = _keyToChange;
829     setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange)  );
830
831     QVBoxLayout *l = new QVBoxLayout( this );
832     selected = new QLabel( qtr("Press the new keys for ")  + qfu(keyToChange) );
833     warning = new QLabel();
834     l->addWidget( selected , Qt::AlignCenter );
835     l->addWidget( warning, Qt::AlignCenter );
836
837     QHBoxLayout *l2 = new QHBoxLayout();
838     QPushButton *ok = new QPushButton( qtr("OK") );
839     l2->addWidget( ok );
840     QPushButton *cancel = new QPushButton( qtr("Cancel") );
841     l2->addWidget( cancel );
842
843     BUTTONACT( ok, accept() );
844     BUTTONACT( cancel, reject() );
845
846     l->addLayout( l2 );
847 }
848
849 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
850 {
851     if( e->key() == Qt::Key_Tab ) return;
852     int i_vlck = qtEventToVLCKey( e );
853     selected->setText( VLCKeyToString( i_vlck ) );
854     conflicts = false;
855     module_config_t *p_current = NULL;
856     foreach( p_current, values )
857     {
858         if( p_current->i_value == i_vlck && strcmp( p_current->psz_text,
859                                                     keyToChange ) )
860         {
861             p_current->i_value = 0;
862             conflicts = true;
863             break;
864         }
865     }
866     if( conflicts )
867     {
868         warning->setText(
869           qtr("Warning: the  key is already assigned to \"") +
870           QString( p_current->psz_text ) + "\"" );
871     }
872     else warning->setText( "" );
873     keyValue = i_vlck;
874 }