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