1 /*****************************************************************************
2 * preferences_widgets.cpp : Widgets for preferences displays
3 ****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Antoine Cellerier <dionoea@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
27 * - Finish implementation (see WX)
28 * - Improvements over WX
29 * - Password field implementation (through "pwd" bool param
30 * - Validator for modulelist
31 * - Implement update stuff using a general Updated signal
34 #include "components/preferences_widgets.hpp"
39 #include <QDoubleSpinBox>
42 #include <QGridLayout>
44 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
45 module_config_t *p_item,
49 return createControl( p_this, p_item, parent, NULL, i );
52 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
53 module_config_t *p_item,
55 QGridLayout *l, int &line )
57 ConfigControl *p_control = NULL;
58 if( p_item->psz_current ) return NULL;
60 switch( p_item->i_type )
62 case CONFIG_ITEM_MODULE:
63 p_control = new ModuleConfigControl( p_this, p_item, parent, false,
66 case CONFIG_ITEM_MODULE_CAT:
67 p_control = new ModuleConfigControl( p_this, p_item, parent, true,
70 case CONFIG_ITEM_MODULE_LIST:
71 p_control = new ModuleListConfigControl( p_this, p_item, parent, false,
74 case CONFIG_ITEM_MODULE_LIST_CAT:
75 p_control = new ModuleListConfigControl( p_this, p_item, parent, true,
78 case CONFIG_ITEM_STRING:
80 p_control = new StringConfigControl( p_this, p_item, parent,
83 p_control = new StringListConfigControl( p_this, p_item,
84 parent, false, l, line );
86 case CONFIG_ITEM_INTEGER:
88 p_control = new IntegerListConfigControl( p_this, p_item,
89 parent, false, l, line );
90 else if( p_item->i_min || p_item->i_max )
91 p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
94 p_control = new IntegerConfigControl( p_this, p_item, parent,
97 case CONFIG_ITEM_FILE:
98 fprintf( stderr, "Todo (CONFIG_ITEM_FILE)\n" );
100 case CONFIG_ITEM_DIRECTORY:
101 fprintf( stderr, "Todo (CONFIG_ITEM_DIRECTORY)\n" );
103 case CONFIG_ITEM_KEY:
104 p_control = new KeySelectorControl( p_this, p_item, parent, l, line );
106 case CONFIG_ITEM_BOOL:
107 p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
109 case CONFIG_ITEM_FLOAT:
110 if( p_item->f_min || p_item->f_max )
111 p_control = new FloatRangeConfigControl( p_this, p_item, parent,
114 p_control = new FloatConfigControl( p_this, p_item, parent,
123 void ConfigControl::doApply( intf_thread_t *p_intf )
129 VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(this);
130 config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
135 VFloatConfigControl *vfcc =
136 qobject_cast<VFloatConfigControl *>(this);
137 config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
142 VStringConfigControl *vscc =
143 qobject_cast<VStringConfigControl *>(this);
144 config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
148 KeySelectorControl *ksc = qobject_cast<KeySelectorControl *>(this);
154 /**************************************************************************
155 * String-based controls
156 *************************************************************************/
158 /*********** String **************/
159 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
160 module_config_t *_p_item,
161 QWidget *_parent, QGridLayout *l,
162 int &line, bool pwd ) :
163 VStringConfigControl( _p_this, _p_item, _parent )
165 label = new QLabel( qfu(p_item->psz_text) );
166 text = new QLineEdit( qfu(p_item->psz_value) );
171 QHBoxLayout *layout = new QHBoxLayout();
172 layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
173 widget->setLayout( layout );
177 l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
181 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
182 module_config_t *_p_item,
183 QLabel *_label, QLineEdit *_text, bool pwd ):
184 VStringConfigControl( _p_this, _p_item )
191 void StringConfigControl::finish()
193 text->setText( qfu(p_item->psz_value) );
194 text->setToolTip( qfu(p_item->psz_longtext) );
196 label->setToolTip( qfu(p_item->psz_longtext) );
199 /********* String / choice list **********/
200 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
201 module_config_t *_p_item, QWidget *_parent, bool bycat,
202 QGridLayout *l, int &line) :
203 VStringConfigControl( _p_this, _p_item, _parent )
205 label = new QLabel( qfu(p_item->psz_text) );
206 combo = new QComboBox();
210 QHBoxLayout *layout = new QHBoxLayout();
211 layout->addWidget( label ); layout->addWidget( combo );
212 widget->setLayout( layout );
216 l->addWidget( label, line, 0 );
217 l->addWidget( combo, line, 1, Qt::AlignRight );
220 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
221 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
222 bool bycat ) : VStringConfigControl( _p_this, _p_item )
229 void StringListConfigControl::finish( bool bycat )
231 combo->setEditable( false );
233 for( int i_index = 0; i_index < p_item->i_list; i_index++ )
235 combo->addItem( qfu(p_item->ppsz_list_text ?
236 p_item->ppsz_list_text[i_index] :
237 p_item->ppsz_list[i_index] ),
238 QVariant( p_item->ppsz_list[i_index] ) );
239 if( p_item->psz_value && !strcmp( p_item->psz_value,
240 p_item->ppsz_list[i_index] ) )
241 combo->setCurrentIndex( combo->count() - 1 );
243 combo->setToolTip( qfu(p_item->psz_longtext) );
245 label->setToolTip( qfu(p_item->psz_longtext) );
248 QString StringListConfigControl::getValue()
250 return combo->itemData( combo->currentIndex() ).toString();
253 /********* Module **********/
254 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
255 module_config_t *_p_item, QWidget *_parent, bool bycat,
256 QGridLayout *l, int &line) :
257 VStringConfigControl( _p_this, _p_item, _parent )
259 label = new QLabel( qfu(p_item->psz_text) );
260 combo = new QComboBox();
264 QHBoxLayout *layout = new QHBoxLayout();
265 layout->addWidget( label ); layout->addWidget( combo );
266 widget->setLayout( layout );
270 l->addWidget( label, line, 0 );
271 l->addWidget( combo, line, 1, Qt::AlignRight );
274 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
275 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
276 bool bycat ) : VStringConfigControl( _p_this, _p_item )
283 void ModuleConfigControl::finish( bool bycat )
288 combo->setEditable( false );
290 /* build a list of available modules */
291 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
292 combo->addItem( qtr("Default") );
293 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
295 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
299 if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
301 module_config_t *p_config = p_parser->p_config;
304 /* Hack: required subcategory is stored in i_min */
305 if( p_config->i_type == CONFIG_SUBCATEGORY &&
306 p_config->i_value == p_item->i_min )
307 combo->addItem( qfu(p_parser->psz_longname),
308 QVariant( p_parser->psz_object_name ) );
309 if( p_item->psz_value && !strcmp( p_item->psz_value,
310 p_parser->psz_object_name) )
311 combo->setCurrentIndex( combo->count() - 1 );
312 } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
314 else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
316 combo->addItem( qfu(p_parser->psz_longname),
317 QVariant( p_parser->psz_object_name ) );
318 if( p_item->psz_value && !strcmp( p_item->psz_value,
319 p_parser->psz_object_name) )
320 combo->setCurrentIndex( combo->count() - 1 );
323 vlc_list_release( p_list );
324 combo->setToolTip( qfu(p_item->psz_longtext) );
326 label->setToolTip( qfu(p_item->psz_longtext) );
329 QString ModuleConfigControl::getValue()
331 return combo->itemData( combo->currentIndex() ).toString();
334 /********* Module list **********/
335 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
336 module_config_t *_p_item, QWidget *_parent, bool bycat,
337 QGridLayout *l, int &line) :
338 VStringConfigControl( _p_this, _p_item, _parent )
340 label = new QLabel( qfu(p_item->psz_text) );
341 text = new QLineEdit();
347 l = new QGridLayout();
351 for( QVector<QCheckBox*>::iterator it = modules.begin();
352 it != modules.end(); it++ )
354 l->addWidget( *it, line++, 1 );
356 l->addWidget( label, line, 0 );
357 l->addWidget( text, line, 1 );
359 widget->setLayout( l );
362 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
363 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
364 bool bycat ) : VStringConfigControl( _p_this, _p_item )
372 ModuleListConfigControl::~ModuleListConfigControl()
374 for( QVector<QCheckBox*>::iterator it = modules.begin();
375 it != modules.end(); it++ )
383 void ModuleListConfigControl::finish( bool bycat )
388 /* build a list of available modules */
389 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
390 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
392 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
396 if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
398 module_config_t *p_config = p_parser->p_config;
401 /* Hack: required subcategory is stored in i_min */
402 if( p_config->i_type == CONFIG_SUBCATEGORY &&
403 p_config->i_value == p_item->i_min )
406 new QCheckBox( qfu( p_parser->psz_object_name ) );
407 cb->setToolTip( qfu(p_parser->psz_longname) );
408 modules.push_back( cb );
410 } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
412 else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
415 new QCheckBox( qfu( p_parser->psz_object_name ) );
416 cb->setToolTip( qfu(p_parser->psz_longname) );
417 modules.push_back( cb );
420 vlc_list_release( p_list );
421 text->setToolTip( qfu(p_item->psz_longtext) );
423 label->setToolTip( qfu(p_item->psz_longtext) );
426 QString ModuleListConfigControl::getValue()
431 void ModuleListConfigControl::hide()
433 for( QVector<QCheckBox*>::iterator it = modules.begin();
434 it != modules.end(); it++ )
442 void ModuleListConfigControl::show()
444 for( QVector<QCheckBox*>::iterator it = modules.begin();
445 it != modules.end(); it++ )
454 void ModuleListConfigControl::wakeUp_TheUserJustClickedOnSomething( int value )
457 for( QVector<QCheckBox*>::iterator it = modules.begin();
458 it != modules.end(); it++ )
463 /**************************************************************************
464 * Integer-based controls
465 *************************************************************************/
467 /*********** Integer **************/
468 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
469 module_config_t *_p_item,
470 QWidget *_parent, QGridLayout *l,
472 VIntConfigControl( _p_this, _p_item, _parent )
474 label = new QLabel( qfu(p_item->psz_text) );
475 spin = new QSpinBox; spin->setMinimumWidth( 80 );
476 spin->setMaximumWidth( 90 );
481 QHBoxLayout *layout = new QHBoxLayout();
482 layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
483 widget->setLayout( layout );
487 l->addWidget( label, line, 0 );
488 l->addWidget( spin, line, 1, Qt::AlignRight );
491 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
492 module_config_t *_p_item,
493 QLabel *_label, QSpinBox *_spin ) :
494 VIntConfigControl( _p_this, _p_item )
501 void IntegerConfigControl::finish()
503 spin->setMaximum( 2000000000 );
504 spin->setMinimum( -2000000000 );
505 spin->setValue( p_item->i_value );
506 spin->setToolTip( qfu(p_item->psz_longtext) );
508 label->setToolTip( qfu(p_item->psz_longtext) );
511 int IntegerConfigControl::getValue()
513 return spin->value();
516 /********* Integer range **********/
517 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
518 module_config_t *_p_item,
519 QWidget *_parent, QGridLayout *l,
521 IntegerConfigControl( _p_this, _p_item, _parent, l, line )
526 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
527 module_config_t *_p_item,
528 QLabel *_label, QSpinBox *_spin ) :
529 IntegerConfigControl( _p_this, _p_item, _label, _spin )
534 void IntegerRangeConfigControl::finish()
536 spin->setMaximum( p_item->i_max );
537 spin->setMinimum( p_item->i_min );
540 /********* Integer / choice list **********/
541 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
542 module_config_t *_p_item, QWidget *_parent, bool bycat,
543 QGridLayout *l, int &line) :
544 VIntConfigControl( _p_this, _p_item, _parent )
546 label = new QLabel( qfu(p_item->psz_text) );
547 combo = new QComboBox();
551 QHBoxLayout *layout = new QHBoxLayout();
552 layout->addWidget( label ); layout->addWidget( combo );
553 widget->setLayout( layout );
557 l->addWidget( label, line, 0 );
558 l->addWidget( combo, line, 1, Qt::AlignRight );
561 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
562 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
563 bool bycat ) : VIntConfigControl( _p_this, _p_item )
570 void IntegerListConfigControl::finish( bool bycat )
572 combo->setEditable( false );
574 for( int i_index = 0; i_index < p_item->i_list; i_index++ )
576 combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
577 QVariant( p_item->pi_list[i_index] ) );
578 if( p_item->i_value == p_item->pi_list[i_index] )
579 combo->setCurrentIndex( combo->count() - 1 );
581 combo->setToolTip( qfu(p_item->psz_longtext) );
583 label->setToolTip( qfu(p_item->psz_longtext) );
586 int IntegerListConfigControl::getValue()
588 return combo->itemData( combo->currentIndex() ).toInt();
591 /*********** Boolean **************/
592 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
593 module_config_t *_p_item,
594 QWidget *_parent, QGridLayout *l,
596 VIntConfigControl( _p_this, _p_item, _parent )
598 checkbox = new QCheckBox( qfu(p_item->psz_text) );
603 QHBoxLayout *layout = new QHBoxLayout();
604 layout->addWidget( checkbox, 0 );
605 widget->setLayout( layout );
609 l->addWidget( checkbox, line, 0 );
612 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
613 module_config_t *_p_item,
615 QCheckBox *_checkbox,
617 VIntConfigControl( _p_this, _p_item )
619 checkbox = _checkbox;
623 void BoolConfigControl::finish()
625 checkbox->setCheckState( p_item->i_value == VLC_TRUE ? Qt::Checked
627 checkbox->setToolTip( qfu(p_item->psz_longtext) );
630 int BoolConfigControl::getValue()
632 return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
635 /**************************************************************************
636 * Float-based controls
637 *************************************************************************/
639 /*********** Float **************/
640 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
641 module_config_t *_p_item,
642 QWidget *_parent, QGridLayout *l,
644 VFloatConfigControl( _p_this, _p_item, _parent )
646 label = new QLabel( qfu(p_item->psz_text) );
647 spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
648 spin->setMaximumWidth( 90 );
653 QHBoxLayout *layout = new QHBoxLayout();
654 layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
655 widget->setLayout( layout );
659 l->addWidget( label, line, 0 );
660 l->addWidget( spin, line, 1, Qt::AlignRight );
664 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
665 module_config_t *_p_item,
667 QDoubleSpinBox *_spin ) :
668 VFloatConfigControl( _p_this, _p_item )
675 void FloatConfigControl::finish()
677 spin->setMaximum( 2000000000. );
678 spin->setMinimum( -2000000000. );
679 spin->setSingleStep( 0.1 );
680 spin->setValue( (double)p_item->f_value );
681 spin->setToolTip( qfu(p_item->psz_longtext) );
683 label->setToolTip( qfu(p_item->psz_longtext) );
686 float FloatConfigControl::getValue()
688 return (float)spin->value();
691 /*********** Float with range **************/
692 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
693 module_config_t *_p_item,
694 QWidget *_parent, QGridLayout *l,
696 FloatConfigControl( _p_this, _p_item, _parent, l, line )
701 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
702 module_config_t *_p_item,
704 QDoubleSpinBox *_spin ) :
705 FloatConfigControl( _p_this, _p_item, _label, _spin )
710 void FloatRangeConfigControl::finish()
712 spin->setMaximum( (double)p_item->f_max );
713 spin->setMinimum( (double)p_item->f_min );
717 /**********************************************************************
718 * Key selector widget
719 **********************************************************************/
720 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
721 module_config_t *_p_item,
722 QWidget *_parent, QGridLayout *l,
724 ConfigControl( _p_this, _p_item, _parent )
727 label = new QLabel( qtr("Select an action to change the associated hotkey") );
728 table = new QTreeWidget( 0 );
733 QVBoxLayout *layout = new QVBoxLayout();
734 layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
735 widget->setLayout( layout );
739 l->addWidget( label, line, 0, 1, 2 );
740 l->addWidget( table, line+1, 0, 1,2 );
744 void KeySelectorControl::finish()
747 label->setToolTip( qfu(p_item->psz_longtext) );
750 table->setColumnCount( 2 );
751 table->setAlternatingRowColors( true );
753 module_t *p_main = config_FindModule( p_this, "main" );
755 module_config_t *p_item = p_main->p_config;
759 if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
760 strstr( p_item->psz_name , "key-" ) )
762 QTreeWidgetItem *treeItem = new QTreeWidgetItem();
763 treeItem->setText( 0, qfu( p_item->psz_text ) );
764 treeItem->setText( 1, p_item->psz_value );
765 treeItem->setData( 0, Qt::UserRole, QVariant( p_item->psz_name ) );
766 table->addTopLevelItem( treeItem );
768 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
771 void KeySelectorControl::doApply()