]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Qt4 - Simple prefs, re-do [22621], but removes all the connects for all the configcon...
[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)
29  *  - Improvements over WX
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
37 #include <vlc_keys.h>
38
39 #include <QString>
40 #include <QVariant>
41 #include <QGridLayout>
42 #include <QSlider>
43 #include <QFileDialog>
44 #include <QFontDialog>
45 #include <QGroupBox>
46 #include <QTreeWidgetItem>
47
48 QString formatTooltip(const QString & tooltip)
49 {
50     QString formatted =
51     "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">"
52     " p, li { white-space: pre-wrap; } </style></head><body style=\" font-family:'Sans Serif';"
53     " font-size:9pt; font-weight:400; font-style:normal; text-decoration:none;\">"
54     "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; "
55     "-qt-block-indent:0; text-indent:0px;\">" +
56     tooltip +
57     "</p></body></html>";
58     return formatted;
59 }
60
61 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
62                                              module_config_t *p_item,
63                                              QWidget *parent )
64 {
65     int i=0;
66     return createControl( p_this, p_item, parent, NULL, i );
67 }
68
69 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
70                                              module_config_t *p_item,
71                                              QWidget *parent,
72                                              QGridLayout *l, int &line )
73 {
74     ConfigControl *p_control = NULL;
75     if( p_item->psz_current || p_item->b_unsaveable ) return NULL;
76
77     switch( p_item->i_type )
78     {
79     case CONFIG_ITEM_MODULE:
80         p_control = new ModuleConfigControl( p_this, p_item, parent, false,
81                                              l, line );
82         break;
83     case CONFIG_ITEM_MODULE_CAT:
84         p_control = new ModuleConfigControl( p_this, p_item, parent, true,
85                                              l, line );
86         break;
87     case CONFIG_ITEM_MODULE_LIST:
88         p_control = new ModuleListConfigControl( p_this, p_item, parent, false,
89                                              l, line );
90         break;
91     case CONFIG_ITEM_MODULE_LIST_CAT:
92         p_control = new ModuleListConfigControl( p_this, p_item, parent, true,
93                                              l, line );
94         break;
95     case CONFIG_ITEM_STRING:
96         if( !p_item->i_list )
97             p_control = new StringConfigControl( p_this, p_item, parent,
98                                                  l, line, false );
99         else
100             p_control = new StringListConfigControl( p_this, p_item,
101                                             parent, false, l, line );
102         break;
103     case CONFIG_ITEM_PASSWORD:
104         if( !p_item->i_list )
105             p_control = new StringConfigControl( p_this, p_item, parent,
106                                                  l, line, true );
107         else
108             p_control = new StringListConfigControl( p_this, p_item,
109                                             parent, true, l, line );
110         break;
111     case CONFIG_ITEM_INTEGER:
112         if( p_item->i_list )
113             p_control = new IntegerListConfigControl( p_this, p_item,
114                                             parent, false, l, line );
115         else if( p_item->min.i || p_item->max.i )
116             p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
117                                                        l, line );
118         else
119             p_control = new IntegerConfigControl( p_this, p_item, parent,
120                                                   l, line );
121         break;
122     case CONFIG_ITEM_FILE:
123         p_control = new FileConfigControl( p_this, p_item, parent, l,
124                                                 line, false );
125         break;
126     case CONFIG_ITEM_DIRECTORY:
127         p_control = new DirectoryConfigControl( p_this, p_item, parent, l,
128                                                 line, false );
129         break;
130     case CONFIG_ITEM_FONT:
131         p_control = new FontConfigControl( p_this, p_item, parent, l,
132                                            line, false );
133         break;
134     case CONFIG_ITEM_KEY:
135         p_control = new KeySelectorControl( p_this, p_item, parent, l, line );
136         break;
137     case CONFIG_ITEM_BOOL:
138         p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
139         break;
140     case CONFIG_ITEM_FLOAT:
141         if( p_item->min.f || p_item->max.f )
142             p_control = new FloatRangeConfigControl( p_this, p_item, parent,
143                                                      l, line );
144         else
145             p_control = new FloatConfigControl( p_this, p_item, parent,
146                                                   l, line );
147         break;
148     default:
149         break;
150     }
151     return p_control;
152 }
153
154 void ConfigControl::doApply( intf_thread_t *p_intf )
155 {
156     switch( getType() )
157     {
158         case 1:
159         {
160             VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(this);
161             assert( vicc );
162             config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
163             break;
164         }
165         case 2:
166         {
167             VFloatConfigControl *vfcc =
168                                     qobject_cast<VFloatConfigControl *>(this);
169             assert( vfcc );
170             config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
171             break;
172         }
173         case 3:
174         {
175             VStringConfigControl *vscc =
176                             qobject_cast<VStringConfigControl *>(this);
177             assert( vscc );
178             config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
179             break;
180         }
181         case 4:
182         {
183             KeySelectorControl *ksc = qobject_cast<KeySelectorControl *>(this);
184             assert( ksc );
185             ksc->doApply();
186         }
187     }
188 }
189
190 /**************************************************************************
191  * String-based controls
192  *************************************************************************/
193
194 /*********** String **************/
195 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
196                                           module_config_t *_p_item,
197                                           QWidget *_parent, QGridLayout *l,
198                                           int &line, bool pwd ) :
199                            VStringConfigControl( _p_this, _p_item, _parent )
200 {
201     label = new QLabel( qtr(p_item->psz_text) );
202     text = new QLineEdit( qfu(p_item->value.psz) );
203     if( pwd ) text->setEchoMode( QLineEdit::Password );
204     finish();
205
206     if( !l )
207     {
208         QHBoxLayout *layout = new QHBoxLayout();
209         layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
210         widget->setLayout( layout );
211     }
212     else
213     {
214         l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
215     }
216 }
217
218 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
219                                    module_config_t *_p_item,
220                                    QLabel *_label, QLineEdit *_text, bool pwd ):
221                            VStringConfigControl( _p_this, _p_item )
222 {
223     text = _text;
224     label = _label;
225     finish( );
226 }
227
228 void StringConfigControl::finish()
229 {
230     text->setText( qfu(p_item->value.psz) );
231     text->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
232     if( label )
233         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
234 }
235
236 /*********** File **************/
237 FileConfigControl::FileConfigControl( vlc_object_t *_p_this,
238                                           module_config_t *_p_item,
239                                           QWidget *_parent, QGridLayout *l,
240                                           int &line, bool pwd ) :
241                            VStringConfigControl( _p_this, _p_item, _parent )
242 {
243     label = new QLabel( qtr(p_item->psz_text) );
244     text = new QLineEdit( qfu(p_item->value.psz) );
245     browse = new QPushButton( qtr( "Browse..." ) );
246     QHBoxLayout *textAndButton = new QHBoxLayout();
247     textAndButton->setMargin( 0 );
248     textAndButton->addWidget( text, 2 );
249     textAndButton->addWidget( browse, 0 );
250
251     BUTTONACT( browse, updateField() );
252
253     finish();
254
255     if( !l )
256     {
257         QHBoxLayout *layout = new QHBoxLayout();
258         layout->addWidget( label, 0 );
259         layout->addLayout( textAndButton, 1 );
260         widget->setLayout( layout );
261     }
262     else
263     {
264         l->addWidget( label, line, 0 );
265         l->addLayout( textAndButton, line, 1 );
266     }
267 }
268
269
270 FileConfigControl::FileConfigControl( vlc_object_t *_p_this,
271                                    module_config_t *_p_item,
272                                    QLabel *_label, QLineEdit *_text,
273                                    QPushButton *_button, bool pwd ):
274                            VStringConfigControl( _p_this, _p_item )
275 {
276     browse = _button;
277     text = _text;
278     label = _label;
279
280     BUTTONACT( browse, updateField() );
281
282     finish( );
283 }
284
285 void FileConfigControl::updateField()
286 {
287     QString file = QFileDialog::getOpenFileName( NULL,
288                   qtr( "Select File" ), qfu( p_this->p_libvlc->psz_homedir ) );
289     if( file.isNull() ) return;
290     text->setText( file );
291 }
292
293 void FileConfigControl::finish()
294 {
295     text->setText( qfu(p_item->value.psz) );
296     text->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
297     if( label )
298         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
299 }
300
301 /********* String / Directory **********/
302 DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
303                         module_config_t *_p_item, QWidget *_p_widget,
304                         QGridLayout *_p_layout, int& _int, bool _pwd ) :
305      FileConfigControl( _p_this, _p_item, _p_widget, _p_layout, _int, _pwd)
306 {}
307
308 DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
309                         module_config_t *_p_item, QLabel *_p_label,
310                         QLineEdit *_p_line, QPushButton *_p_button, bool _pwd ):
311      FileConfigControl( _p_this, _p_item, _p_label, _p_line, _p_button, _pwd)
312 {}
313
314 void DirectoryConfigControl::updateField()
315 {
316     QString dir = QFileDialog::getExistingDirectory( NULL,
317                       qtr( "Select Directory" ),
318                       text->text().isEmpty() ?
319                         qfu( p_this->p_libvlc->psz_homedir ) : text->text(),
320                       QFileDialog::ShowDirsOnly |
321                         QFileDialog::DontResolveSymlinks );
322     if( dir.isNull() ) return;
323     text->setText( dir );
324 }
325
326 /********* String / Font **********/
327 FontConfigControl::FontConfigControl( vlc_object_t *_p_this,
328                         module_config_t *_p_item, QWidget *_p_widget,
329                         QGridLayout *_p_layout, int& _int, bool _pwd ) :
330      FileConfigControl( _p_this, _p_item, _p_widget, _p_layout, _int, _pwd)
331 {}
332
333 FontConfigControl::FontConfigControl( vlc_object_t *_p_this,
334                         module_config_t *_p_item, QLabel *_p_label,
335                         QLineEdit *_p_line, QPushButton *_p_button, bool _pwd ):
336      FileConfigControl( _p_this, _p_item, _p_label, _p_line, _p_button, _pwd)
337 {}
338
339 void FontConfigControl::updateField()
340 {
341     bool ok;
342     QFont font = QFontDialog::getFont( &ok, QFont( text->text() ), NULL );
343     if( !ok ) return;
344     text->setText( font.family() );
345 }
346
347 /********* String / choice list **********/
348 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
349                module_config_t *_p_item, QWidget *_parent, bool bycat,
350                QGridLayout *l, int &line) :
351                VStringConfigControl( _p_this, _p_item, _parent )
352 {
353     label = new QLabel( qtr(p_item->psz_text) );
354     combo = new QComboBox();
355     finish( bycat );
356     if( !l )
357     {
358         QHBoxLayout *layout = new QHBoxLayout();
359         layout->addWidget( label ); layout->addWidget( combo );
360         widget->setLayout( layout );
361     }
362     else
363     {
364         l->addWidget( label, line, 0 );
365         l->addWidget( combo, line, 1, Qt::AlignRight );
366     }
367 }
368 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
369                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
370                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
371 {
372     combo = _combo;
373     label = _label;
374     finish( bycat );
375 }
376
377 void StringListConfigControl::finish( bool bycat )
378 {
379     combo->setEditable( false );
380
381     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
382     {
383         combo->addItem( qfu(p_item->ppsz_list_text ?
384                             p_item->ppsz_list_text[i_index] :
385                             p_item->ppsz_list[i_index] ),
386                         QVariant( p_item->ppsz_list[i_index] ) );
387         if( p_item->value.psz && !strcmp( p_item->value.psz,
388                                           p_item->ppsz_list[i_index] ) )
389             combo->setCurrentIndex( combo->count() - 1 );
390     }
391     combo->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
392     if( label )
393         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
394 }
395
396 QString StringListConfigControl::getValue()
397 {
398     return combo->itemData( combo->currentIndex() ).toString();
399 }
400
401 /********* Module **********/
402 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
403                module_config_t *_p_item, QWidget *_parent, bool bycat,
404                QGridLayout *l, int &line) :
405                VStringConfigControl( _p_this, _p_item, _parent )
406 {
407     label = new QLabel( qtr(p_item->psz_text) );
408     combo = new QComboBox();
409     finish( bycat );
410     if( !l )
411     {
412         QHBoxLayout *layout = new QHBoxLayout();
413         layout->addWidget( label ); layout->addWidget( combo );
414         widget->setLayout( layout );
415     }
416     else
417     {
418         l->addWidget( label, line, 0 );
419         l->addWidget( combo, line, 1, Qt::AlignRight );
420     }
421 }
422
423 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
424                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
425                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
426 {
427     combo = _combo;
428     label = _label;
429     finish( bycat );
430 }
431
432 void ModuleConfigControl::finish( bool bycat )
433 {
434     vlc_list_t *p_list;
435     module_t *p_parser;
436
437     combo->setEditable( false );
438
439     /* build a list of available modules */
440     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
441     combo->addItem( qtr("Default") );
442     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
443     {
444         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
445
446         if( bycat )
447         {
448             if( !strcmp( module_GetObjName( p_parser ), "main" ) ) continue;
449
450             for (size_t i = 0; i < p_parser->confsize; i++)
451             {
452                 module_config_t *p_config = p_parser->p_config + i;
453                 /* Hack: required subcategory is stored in i_min */
454                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
455                     p_config->value.i == p_item->min.i )
456                     combo->addItem( qtr( module_GetLongName( p_parser )),
457                                     QVariant( module_GetObjName( p_parser ) ) );
458                 if( p_item->value.psz && !strcmp( p_item->value.psz,
459                                                   module_GetObjName( p_parser ) ) )
460                     combo->setCurrentIndex( combo->count() - 1 );
461             }
462         }
463         else if( module_IsCapable( p_parser, p_item->psz_type ) )
464         {
465             combo->addItem( qtr(module_GetLongName( p_parser ) ),
466                             QVariant( module_GetObjName( p_parser ) ) );
467             if( p_item->value.psz && !strcmp( p_item->value.psz,
468                                               module_GetObjName( p_parser ) ) )
469                 combo->setCurrentIndex( combo->count() - 1 );
470         }
471     }
472     vlc_list_release( p_list );
473     combo->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
474     if( label )
475         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
476 }
477
478 QString ModuleConfigControl::getValue()
479 {
480     return combo->itemData( combo->currentIndex() ).toString();
481 }
482
483 /********* Module list **********/
484 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
485         module_config_t *_p_item, QWidget *_parent, bool bycat,
486         QGridLayout *l, int &line) :
487     VStringConfigControl( _p_this, _p_item, _parent )
488 {
489     groupBox = new QGroupBox ( qtr(p_item->psz_text) );
490     text = new QLineEdit();
491     QGridLayout *layoutGroupBox = new QGridLayout( groupBox );
492
493     finish( bycat );
494
495     int boxline = 0;
496     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
497             it != modules.end(); it++ )
498     {
499         layoutGroupBox->addWidget( (*it)->checkBox, boxline++, 0 );
500     }
501     layoutGroupBox->addWidget( text, boxline, 0 );
502
503     if( !l )
504     {
505         QVBoxLayout *layout = new QVBoxLayout();
506         layout->addWidget( groupBox, line, 0 );
507         widget->setLayout( layout );
508     }
509     else
510     {
511         l->addWidget( groupBox, line, 0, 1, -1 );
512     }
513
514     text->setToolTip( formatTooltip( qtr( p_item->psz_longtext) ) );
515 }
516
517 ModuleListConfigControl::~ModuleListConfigControl()
518 {
519     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
520             it != modules.end(); it++ )
521     {
522         delete *it;
523     }
524     delete groupBox;
525     delete text;
526 }
527
528 #define CHECKBOX_LISTS \
529 { \
530        QCheckBox *cb = new QCheckBox( qtr( module_GetLongName( p_parser ) ) );\
531        checkBoxListItem *cbl = new checkBoxListItem; \
532 \
533        CONNECT( cb, stateChanged( int ), this, onUpdate( int ) );\
534        cb->setToolTip( formatTooltip( qtr( module_GetLongName( p_parser ))));\
535        cbl->checkBox = cb; \
536 \
537        int i = -1; \
538        while( p_parser->pp_shortcuts[++i] != NULL); \
539        i--; \
540 \
541        cbl->psz_module = strdup( i>=0?p_parser->pp_shortcuts[i] \
542                                  : module_GetObjName( p_parser ) ); \
543        modules.push_back( cbl ); \
544 }
545
546
547 void ModuleListConfigControl::finish( bool bycat )
548 {
549     vlc_list_t *p_list;
550     module_t *p_parser;
551
552     /* build a list of available modules */
553     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
554     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
555     {
556         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
557
558         if( bycat )
559         {
560             if( !strcmp( module_GetObjName( p_parser ), "main" ) ) continue;
561
562             for (size_t i = 0; i < p_parser->confsize; i++)
563             {
564                 module_config_t *p_config = p_parser->p_config + i;
565                 /* Hack: required subcategory is stored in i_min */
566                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
567                         p_config->value.i == p_item->min.i )
568                 {
569                     CHECKBOX_LISTS;
570                 }
571             }
572         }
573         else if( module_IsCapable( p_parser, p_item->psz_type ) )
574         {
575             CHECKBOX_LISTS;
576         }
577     }
578     vlc_list_release( p_list );
579     text->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
580     if( groupBox )
581         groupBox->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
582 }
583 #undef CHECKBOX_LISTS
584
585 QString ModuleListConfigControl::getValue()
586 {
587     return text->text();
588 }
589
590 void ModuleListConfigControl::hide()
591 {
592     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
593          it != modules.end(); it++ )
594     {
595         (*it)->checkBox->hide();
596     }
597     groupBox->hide();
598 }
599
600 void ModuleListConfigControl::show()
601 {
602     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
603          it != modules.end(); it++ )
604     {
605         (*it)->checkBox->show();
606     }
607     groupBox->show();
608 }
609
610
611 void ModuleListConfigControl::onUpdate( int value )
612 {
613     text->clear();
614     bool first = true;
615
616     for( QVector<checkBoxListItem*>::iterator it = modules.begin();
617          it != modules.end(); it++ )
618     {
619         if( (*it)->checkBox->isChecked() )
620         {
621             if( first )
622             {
623                 text->setText( text->text() + (*it)->psz_module );
624                 first = false;
625             }
626             else
627             {
628                 text->setText( text->text() + ":" + (*it)->psz_module );
629             }
630         }
631     }
632     emit Updated();
633 }
634
635 /**************************************************************************
636  * Integer-based controls
637  *************************************************************************/
638
639 /*********** Integer **************/
640 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
641                                             module_config_t *_p_item,
642                                             QWidget *_parent, QGridLayout *l,
643                                             int &line ) :
644                            VIntConfigControl( _p_this, _p_item, _parent )
645 {
646     label = new QLabel( qtr(p_item->psz_text) );
647     spin = new QSpinBox; spin->setMinimumWidth( 80 );
648     spin->setAlignment( Qt::AlignRight );
649     spin->setMaximumWidth( 90 );
650     finish();
651
652     if( !l )
653     {
654         QHBoxLayout *layout = new QHBoxLayout();
655         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
656         widget->setLayout( layout );
657     }
658     else
659     {
660         l->addWidget( label, line, 0 );
661         l->addWidget( spin, line, 1, Qt::AlignRight );
662     }
663 }
664 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
665                                             module_config_t *_p_item,
666                                             QLabel *_label, QSpinBox *_spin ) :
667                                       VIntConfigControl( _p_this, _p_item )
668 {
669     spin = _spin;
670     label = _label;
671     finish();
672 }
673
674 void IntegerConfigControl::finish()
675 {
676     spin->setMaximum( 2000000000 );
677     spin->setMinimum( -2000000000 );
678     spin->setValue( p_item->value.i );
679     spin->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
680     if( label )
681         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
682 }
683
684 int IntegerConfigControl::getValue()
685 {
686     return spin->value();
687 }
688
689 /********* Integer range **********/
690 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
691                                             module_config_t *_p_item,
692                                             QWidget *_parent, QGridLayout *l,
693                                             int &line ) :
694             IntegerConfigControl( _p_this, _p_item, _parent, l, line )
695 {
696     finish();
697 }
698
699 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
700                                             module_config_t *_p_item,
701                                             QLabel *_label, QSpinBox *_spin ) :
702             IntegerConfigControl( _p_this, _p_item, _label, _spin )
703 {
704     finish();
705 }
706
707 void IntegerRangeConfigControl::finish()
708 {
709     spin->setMaximum( p_item->max.i );
710     spin->setMinimum( p_item->min.i );
711 }
712
713 IntegerRangeSliderConfigControl::IntegerRangeSliderConfigControl(
714                                             vlc_object_t *_p_this,
715                                             module_config_t *_p_item,
716                                             QLabel *_label, QSlider *_slider ):
717                     VIntConfigControl( _p_this, _p_item )
718 {
719     slider = _slider;
720     label = _label;
721     slider->setMaximum( p_item->max.i );
722     slider->setMinimum( p_item->min.i );
723     slider->setValue( p_item->value.i );
724     slider->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
725     if( label )
726         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
727 }
728
729 int IntegerRangeSliderConfigControl::getValue()
730 {
731         return slider->value();
732 }
733
734
735 /********* Integer / choice list **********/
736 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
737                module_config_t *_p_item, QWidget *_parent, bool bycat,
738                QGridLayout *l, int &line) :
739                VIntConfigControl( _p_this, _p_item, _parent )
740 {
741     label = new QLabel( qtr(p_item->psz_text) );
742     combo = new QComboBox();
743     finish( bycat );
744     if( !l )
745     {
746         QHBoxLayout *layout = new QHBoxLayout();
747         layout->addWidget( label ); layout->addWidget( combo );
748         widget->setLayout( layout );
749     }
750     else
751     {
752         l->addWidget( label, line, 0 );
753         l->addWidget( combo, line, 1, Qt::AlignRight );
754     }
755 }
756 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
757                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
758                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
759 {
760     combo = _combo;
761     label = _label;
762     finish( bycat );
763 }
764
765 void IntegerListConfigControl::finish( bool bycat )
766 {
767     combo->setEditable( false );
768
769     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
770     {
771         combo->addItem( qtr(p_item->ppsz_list_text[i_index] ),
772                         QVariant( p_item->pi_list[i_index] ) );
773         if( p_item->value.i == p_item->pi_list[i_index] )
774             combo->setCurrentIndex( combo->count() - 1 );
775     }
776     combo->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
777     if( label )
778         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
779 }
780
781 int IntegerListConfigControl::getValue()
782 {
783     return combo->itemData( combo->currentIndex() ).toInt();
784 }
785
786 /*********** Boolean **************/
787 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
788                                       module_config_t *_p_item,
789                                       QWidget *_parent, QGridLayout *l,
790                                       int &line ) :
791                     VIntConfigControl( _p_this, _p_item, _parent )
792 {
793     checkbox = new QCheckBox( qtr(p_item->psz_text) );
794     finish();
795
796     if( !l )
797     {
798         QHBoxLayout *layout = new QHBoxLayout();
799         layout->addWidget( checkbox, 0 );
800         widget->setLayout( layout );
801     }
802     else
803     {
804         l->addWidget( checkbox, line, 0 );
805     }
806 }
807 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
808                                       module_config_t *_p_item,
809                                       QLabel *_label,
810                                       QCheckBox *_checkbox,
811                                       bool bycat ) :
812                    VIntConfigControl( _p_this, _p_item )
813 {
814     checkbox = _checkbox;
815     finish();
816 }
817
818 void BoolConfigControl::finish()
819 {
820     checkbox->setCheckState( p_item->value.i == VLC_TRUE ? Qt::Checked
821                                                         : Qt::Unchecked );
822     checkbox->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
823 }
824
825 int BoolConfigControl::getValue()
826 {
827     return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
828 }
829
830 /**************************************************************************
831  * Float-based controls
832  *************************************************************************/
833
834 /*********** Float **************/
835 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
836                                         module_config_t *_p_item,
837                                         QWidget *_parent, QGridLayout *l,
838                                         int &line ) :
839                     VFloatConfigControl( _p_this, _p_item, _parent )
840 {
841     label = new QLabel( qtr(p_item->psz_text) );
842     spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
843     spin->setMaximumWidth( 90 );
844     spin->setAlignment( Qt::AlignRight );
845     finish();
846
847     if( !l )
848     {
849         QHBoxLayout *layout = new QHBoxLayout();
850         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
851         widget->setLayout( layout );
852     }
853     else
854     {
855         l->addWidget( label, line, 0 );
856         l->addWidget( spin, line, 1, Qt::AlignRight );
857     }
858 }
859
860 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
861                                         module_config_t *_p_item,
862                                         QLabel *_label,
863                                         QDoubleSpinBox *_spin ) :
864                     VFloatConfigControl( _p_this, _p_item )
865 {
866     spin = _spin;
867     label = _label;
868     finish();
869 }
870
871 void FloatConfigControl::finish()
872 {
873     spin->setMaximum( 2000000000. );
874     spin->setMinimum( -2000000000. );
875     spin->setSingleStep( 0.1 );
876     spin->setValue( (double)p_item->value.f );
877     spin->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
878     if( label )
879         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
880 }
881
882 float FloatConfigControl::getValue()
883 {
884     return (float)spin->value();
885 }
886
887 /*********** Float with range **************/
888 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
889                                         module_config_t *_p_item,
890                                         QWidget *_parent, QGridLayout *l,
891                                         int &line ) :
892                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
893 {
894     finish();
895 }
896
897 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
898                                         module_config_t *_p_item,
899                                         QLabel *_label,
900                                         QDoubleSpinBox *_spin ) :
901                 FloatConfigControl( _p_this, _p_item, _label, _spin )
902 {
903     finish();
904 }
905
906 void FloatRangeConfigControl::finish()
907 {
908     spin->setMaximum( (double)p_item->max.f );
909     spin->setMinimum( (double)p_item->min.f );
910 }
911
912
913 /**********************************************************************
914  * Key selector widget
915  **********************************************************************/
916 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
917                                       module_config_t *_p_item,
918                                       QWidget *_parent, QGridLayout *l,
919                                       int &line ) :
920                                 ConfigControl( _p_this, _p_item, _parent )
921
922 {
923     label = new QLabel( qtr("Select an action to change the associated hotkey") );
924     table = new QTreeWidget( 0 );
925     finish();
926
927     if( !l )
928     {
929         QVBoxLayout *layout = new QVBoxLayout();
930         layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
931         widget->setLayout( layout );
932     }
933     else
934     {
935         l->addWidget( label, line, 0, 1, 2 );
936         l->addWidget( table, line+1, 0, 1,2 );
937     }
938 }
939
940 void KeySelectorControl::finish()
941 {
942     if( label )
943         label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) );
944
945     /* Fill the table */
946     table->setColumnCount( 2 );
947     table->setAlternatingRowColors( true );
948
949     module_t *p_main = config_FindModule( p_this, "main" );
950     assert( p_main );
951
952     for (size_t i = 0; i < p_main->confsize; i++)
953     {
954         module_config_t *p_item = p_main->p_config + i;
955
956         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
957             strstr( p_item->psz_name , "key-" ) )
958         {
959             QTreeWidgetItem *treeItem = new QTreeWidgetItem();
960             treeItem->setText( 0, qtr( p_item->psz_text ) );
961             treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
962             treeItem->setData( 0, Qt::UserRole,
963                                   QVariant::fromValue( (void*)p_item ) );
964             values += p_item;
965             table->addTopLevelItem( treeItem );
966         }
967     }
968     table->resizeColumnToContents( 0 );
969
970     CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
971              this, selectKey( QTreeWidgetItem * ) );
972 }
973
974 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
975 {
976    module_config_t *p_keyItem = static_cast<module_config_t*>
977                           (keyItem->data( 0, Qt::UserRole ).value<void*>());
978
979     KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text );
980     d->exec();
981     if( d->result() == QDialog::Accepted )
982     {
983         p_keyItem->value.i = d->keyValue;
984         if( d->conflicts )
985         {
986             for( int i = 0; i < table->topLevelItemCount() ; i++ )
987             {
988                 QTreeWidgetItem *it = table->topLevelItem(i);
989                 module_config_t *p_item = static_cast<module_config_t*>
990                               (it->data( 0, Qt::UserRole ).value<void*>());
991                 if( p_keyItem != p_item && p_item->value.i == d->keyValue )
992                     p_item->value.i = 0;
993                 it->setText( 1, VLCKeyToString( p_item->value.i ) );
994             }
995         }
996         else
997             keyItem->setText( 1, VLCKeyToString( p_keyItem->value.i ) );
998     }
999     delete d;
1000 }
1001
1002 void KeySelectorControl::doApply()
1003 {
1004     foreach( module_config_t *p_current, values )
1005     {
1006         config_PutInt( p_this, p_current->psz_name, p_current->value.i );
1007     }
1008 }
1009
1010 KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
1011                                 const char * _keyToChange ) :
1012                                                 QDialog(0), keyValue(0)
1013 {
1014     setModal( true );
1015     values = _values;
1016     conflicts = false;
1017     keyToChange = _keyToChange;
1018     setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange)  );
1019
1020     QVBoxLayout *l = new QVBoxLayout( this );
1021     selected = new QLabel( qtr("Press the new keys for ")  + qfu(keyToChange) );
1022     warning = new QLabel();
1023     l->addWidget( selected , Qt::AlignCenter );
1024     l->addWidget( warning, Qt::AlignCenter );
1025
1026     QHBoxLayout *l2 = new QHBoxLayout();
1027     QPushButton *ok = new QPushButton( qtr("OK") );
1028     l2->addWidget( ok );
1029     QPushButton *cancel = new QPushButton( qtr("Cancel") );
1030     l2->addWidget( cancel );
1031
1032     BUTTONACT( ok, accept() );
1033     BUTTONACT( cancel, reject() );
1034
1035     l->addLayout( l2 );
1036 }
1037
1038 void KeyInputDialog::checkForConflicts( int i_vlckey )
1039 {
1040     conflicts = false;
1041     module_config_t *p_current = NULL;
1042     foreach( p_current, values )
1043     {
1044         if( p_current->value.i == i_vlckey && strcmp( p_current->psz_text,
1045                                                     keyToChange ) )
1046         {
1047             conflicts = true;
1048             break;
1049         }
1050     }
1051     if( conflicts )
1052     {
1053         warning->setText(
1054           qtr("Warning: the  key is already assigned to \"") +
1055           QString( p_current->psz_text ) + "\"" );
1056     }
1057     else warning->setText( "" );
1058 }
1059
1060 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
1061 {
1062     if( e->key() == Qt::Key_Tab ) return;
1063     int i_vlck = qtEventToVLCKey( e );
1064     selected->setText( VLCKeyToString( i_vlck ) );
1065     checkForConflicts( i_vlck );
1066     keyValue = i_vlck;
1067 }
1068
1069 void KeyInputDialog::wheelEvent( QWheelEvent *e )
1070 {
1071     int i_vlck = qtWheelEventToVLCKey( e );
1072     selected->setText( VLCKeyToString( i_vlck ) );
1073     checkForConflicts( i_vlck );
1074     keyValue = i_vlck;
1075 }