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