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"
35 #include "util/customwidgets.hpp"
41 #include <QDoubleSpinBox>
44 #include <QGridLayout>
45 #include <QPushButton>
49 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
50 module_config_t *p_item,
54 return createControl( p_this, p_item, parent, NULL, i );
57 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
58 module_config_t *p_item,
60 QGridLayout *l, int &line )
62 ConfigControl *p_control = NULL;
63 if( p_item->psz_current ) return NULL;
65 switch( p_item->i_type )
67 case CONFIG_ITEM_MODULE:
68 p_control = new ModuleConfigControl( p_this, p_item, parent, false,
71 case CONFIG_ITEM_MODULE_CAT:
72 p_control = new ModuleConfigControl( p_this, p_item, parent, true,
75 case CONFIG_ITEM_MODULE_LIST:
76 p_control = new ModuleListConfigControl( p_this, p_item, parent, false,
79 case CONFIG_ITEM_MODULE_LIST_CAT:
80 p_control = new ModuleListConfigControl( p_this, p_item, parent, true,
83 case CONFIG_ITEM_STRING:
85 p_control = new StringConfigControl( p_this, p_item, parent,
88 p_control = new StringListConfigControl( p_this, p_item,
89 parent, false, l, line );
91 case CONFIG_ITEM_INTEGER:
93 p_control = new IntegerListConfigControl( p_this, p_item,
94 parent, false, l, line );
95 else if( p_item->i_min || p_item->i_max )
96 p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
99 p_control = new IntegerConfigControl( p_this, p_item, parent,
102 case CONFIG_ITEM_FILE:
103 fprintf( stderr, "Todo (CONFIG_ITEM_FILE)\n" );
105 case CONFIG_ITEM_DIRECTORY:
106 fprintf( stderr, "Todo (CONFIG_ITEM_DIRECTORY)\n" );
108 case CONFIG_ITEM_KEY:
109 p_control = new KeySelectorControl( p_this, p_item, parent, l, line );
111 case CONFIG_ITEM_BOOL:
112 p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
114 case CONFIG_ITEM_FLOAT:
115 if( p_item->f_min || p_item->f_max )
116 p_control = new FloatRangeConfigControl( p_this, p_item, parent,
119 p_control = new FloatConfigControl( p_this, p_item, parent,
128 void ConfigControl::doApply( intf_thread_t *p_intf )
134 VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(this);
135 config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
140 VFloatConfigControl *vfcc =
141 qobject_cast<VFloatConfigControl *>(this);
142 config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
147 VStringConfigControl *vscc =
148 qobject_cast<VStringConfigControl *>(this);
149 config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
153 KeySelectorControl *ksc = qobject_cast<KeySelectorControl *>(this);
159 /**************************************************************************
160 * String-based controls
161 *************************************************************************/
163 /*********** String **************/
164 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
165 module_config_t *_p_item,
166 QWidget *_parent, QGridLayout *l,
167 int &line, bool pwd ) :
168 VStringConfigControl( _p_this, _p_item, _parent )
170 label = new QLabel( qfu(p_item->psz_text) );
171 text = new QLineEdit( qfu(p_item->psz_value) );
176 QHBoxLayout *layout = new QHBoxLayout();
177 layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
178 widget->setLayout( layout );
182 l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
186 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
187 module_config_t *_p_item,
188 QLabel *_label, QLineEdit *_text, bool pwd ):
189 VStringConfigControl( _p_this, _p_item )
196 void StringConfigControl::finish()
198 text->setText( qfu(p_item->psz_value) );
199 text->setToolTip( qfu(p_item->psz_longtext) );
201 label->setToolTip( qfu(p_item->psz_longtext) );
204 /********* String / choice list **********/
205 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
206 module_config_t *_p_item, QWidget *_parent, bool bycat,
207 QGridLayout *l, int &line) :
208 VStringConfigControl( _p_this, _p_item, _parent )
210 label = new QLabel( qfu(p_item->psz_text) );
211 combo = new QComboBox();
215 QHBoxLayout *layout = new QHBoxLayout();
216 layout->addWidget( label ); layout->addWidget( combo );
217 widget->setLayout( layout );
221 l->addWidget( label, line, 0 );
222 l->addWidget( combo, line, 1, Qt::AlignRight );
225 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
226 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
227 bool bycat ) : VStringConfigControl( _p_this, _p_item )
234 void StringListConfigControl::finish( bool bycat )
236 combo->setEditable( false );
238 for( int i_index = 0; i_index < p_item->i_list; i_index++ )
240 combo->addItem( qfu(p_item->ppsz_list_text ?
241 p_item->ppsz_list_text[i_index] :
242 p_item->ppsz_list[i_index] ),
243 QVariant( p_item->ppsz_list[i_index] ) );
244 if( p_item->psz_value && !strcmp( p_item->psz_value,
245 p_item->ppsz_list[i_index] ) )
246 combo->setCurrentIndex( combo->count() - 1 );
248 combo->setToolTip( qfu(p_item->psz_longtext) );
250 label->setToolTip( qfu(p_item->psz_longtext) );
253 QString StringListConfigControl::getValue()
255 return combo->itemData( combo->currentIndex() ).toString();
258 /********* Module **********/
259 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
260 module_config_t *_p_item, QWidget *_parent, bool bycat,
261 QGridLayout *l, int &line) :
262 VStringConfigControl( _p_this, _p_item, _parent )
264 label = new QLabel( qfu(p_item->psz_text) );
265 combo = new QComboBox();
269 QHBoxLayout *layout = new QHBoxLayout();
270 layout->addWidget( label ); layout->addWidget( combo );
271 widget->setLayout( layout );
275 l->addWidget( label, line, 0 );
276 l->addWidget( combo, line, 1, Qt::AlignRight );
279 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
280 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
281 bool bycat ) : VStringConfigControl( _p_this, _p_item )
288 void ModuleConfigControl::finish( bool bycat )
293 combo->setEditable( false );
295 /* build a list of available modules */
296 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
297 combo->addItem( qtr("Default") );
298 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
300 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
304 if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
306 module_config_t *p_config = p_parser->p_config;
309 /* Hack: required subcategory is stored in i_min */
310 if( p_config->i_type == CONFIG_SUBCATEGORY &&
311 p_config->i_value == p_item->i_min )
312 combo->addItem( qfu(p_parser->psz_longname),
313 QVariant( p_parser->psz_object_name ) );
314 if( p_item->psz_value && !strcmp( p_item->psz_value,
315 p_parser->psz_object_name) )
316 combo->setCurrentIndex( combo->count() - 1 );
317 } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
319 else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
321 combo->addItem( qfu(p_parser->psz_longname),
322 QVariant( p_parser->psz_object_name ) );
323 if( p_item->psz_value && !strcmp( p_item->psz_value,
324 p_parser->psz_object_name) )
325 combo->setCurrentIndex( combo->count() - 1 );
328 vlc_list_release( p_list );
329 combo->setToolTip( qfu(p_item->psz_longtext) );
331 label->setToolTip( qfu(p_item->psz_longtext) );
334 QString ModuleConfigControl::getValue()
336 return combo->itemData( combo->currentIndex() ).toString();
339 /********* Module list **********/
340 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
341 module_config_t *_p_item, QWidget *_parent, bool bycat,
342 QGridLayout *l, int &line) :
343 VStringConfigControl( _p_this, _p_item, _parent )
345 label = new QLabel( qfu(p_item->psz_text) );
346 text = new QLineEdit();
352 l = new QGridLayout();
356 for( QVector<QCheckBox*>::iterator it = modules.begin();
357 it != modules.end(); it++ )
359 l->addWidget( *it, line++, 1 );
361 l->addWidget( label, line, 0 );
362 l->addWidget( text, line, 1 );
364 widget->setLayout( l );
367 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
368 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
369 bool bycat ) : VStringConfigControl( _p_this, _p_item )
377 ModuleListConfigControl::~ModuleListConfigControl()
379 for( QVector<QCheckBox*>::iterator it = modules.begin();
380 it != modules.end(); it++ )
388 void ModuleListConfigControl::finish( bool bycat )
393 /* build a list of available modules */
394 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
395 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
397 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
401 if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
403 module_config_t *p_config = p_parser->p_config;
406 /* Hack: required subcategory is stored in i_min */
407 if( p_config->i_type == CONFIG_SUBCATEGORY &&
408 p_config->i_value == p_item->i_min )
411 new QCheckBox( qfu( p_parser->psz_object_name ) );
412 cb->setToolTip( qfu(p_parser->psz_longname) );
413 modules.push_back( cb );
415 } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
417 else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
420 new QCheckBox( qfu( p_parser->psz_object_name ) );
421 cb->setToolTip( qfu(p_parser->psz_longname) );
422 modules.push_back( cb );
425 vlc_list_release( p_list );
426 text->setToolTip( qfu(p_item->psz_longtext) );
428 label->setToolTip( qfu(p_item->psz_longtext) );
431 QString ModuleListConfigControl::getValue()
436 void ModuleListConfigControl::hide()
438 for( QVector<QCheckBox*>::iterator it = modules.begin();
439 it != modules.end(); it++ )
447 void ModuleListConfigControl::show()
449 for( QVector<QCheckBox*>::iterator it = modules.begin();
450 it != modules.end(); it++ )
459 void ModuleListConfigControl::wakeUp_TheUserJustClickedOnSomething( int value )
462 for( QVector<QCheckBox*>::iterator it = modules.begin();
463 it != modules.end(); it++ )
468 /**************************************************************************
469 * Integer-based controls
470 *************************************************************************/
472 /*********** Integer **************/
473 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
474 module_config_t *_p_item,
475 QWidget *_parent, QGridLayout *l,
477 VIntConfigControl( _p_this, _p_item, _parent )
479 label = new QLabel( qfu(p_item->psz_text) );
480 spin = new QSpinBox; spin->setMinimumWidth( 80 );
481 spin->setMaximumWidth( 90 );
486 QHBoxLayout *layout = new QHBoxLayout();
487 layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
488 widget->setLayout( layout );
492 l->addWidget( label, line, 0 );
493 l->addWidget( spin, line, 1, Qt::AlignRight );
496 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
497 module_config_t *_p_item,
498 QLabel *_label, QSpinBox *_spin ) :
499 VIntConfigControl( _p_this, _p_item )
506 void IntegerConfigControl::finish()
508 spin->setMaximum( 2000000000 );
509 spin->setMinimum( -2000000000 );
510 spin->setValue( p_item->i_value );
511 spin->setToolTip( qfu(p_item->psz_longtext) );
513 label->setToolTip( qfu(p_item->psz_longtext) );
516 int IntegerConfigControl::getValue()
518 return spin->value();
521 /********* Integer range **********/
522 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
523 module_config_t *_p_item,
524 QWidget *_parent, QGridLayout *l,
526 IntegerConfigControl( _p_this, _p_item, _parent, l, line )
531 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
532 module_config_t *_p_item,
533 QLabel *_label, QSpinBox *_spin ) :
534 IntegerConfigControl( _p_this, _p_item, _label, _spin )
539 void IntegerRangeConfigControl::finish()
541 spin->setMaximum( p_item->i_max );
542 spin->setMinimum( p_item->i_min );
545 /********* Integer / choice list **********/
546 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
547 module_config_t *_p_item, QWidget *_parent, bool bycat,
548 QGridLayout *l, int &line) :
549 VIntConfigControl( _p_this, _p_item, _parent )
551 label = new QLabel( qfu(p_item->psz_text) );
552 combo = new QComboBox();
556 QHBoxLayout *layout = new QHBoxLayout();
557 layout->addWidget( label ); layout->addWidget( combo );
558 widget->setLayout( layout );
562 l->addWidget( label, line, 0 );
563 l->addWidget( combo, line, 1, Qt::AlignRight );
566 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
567 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
568 bool bycat ) : VIntConfigControl( _p_this, _p_item )
575 void IntegerListConfigControl::finish( bool bycat )
577 combo->setEditable( false );
579 for( int i_index = 0; i_index < p_item->i_list; i_index++ )
581 combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
582 QVariant( p_item->pi_list[i_index] ) );
583 if( p_item->i_value == p_item->pi_list[i_index] )
584 combo->setCurrentIndex( combo->count() - 1 );
586 combo->setToolTip( qfu(p_item->psz_longtext) );
588 label->setToolTip( qfu(p_item->psz_longtext) );
591 int IntegerListConfigControl::getValue()
593 return combo->itemData( combo->currentIndex() ).toInt();
596 /*********** Boolean **************/
597 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
598 module_config_t *_p_item,
599 QWidget *_parent, QGridLayout *l,
601 VIntConfigControl( _p_this, _p_item, _parent )
603 checkbox = new QCheckBox( qfu(p_item->psz_text) );
608 QHBoxLayout *layout = new QHBoxLayout();
609 layout->addWidget( checkbox, 0 );
610 widget->setLayout( layout );
614 l->addWidget( checkbox, line, 0 );
617 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
618 module_config_t *_p_item,
620 QCheckBox *_checkbox,
622 VIntConfigControl( _p_this, _p_item )
624 checkbox = _checkbox;
628 void BoolConfigControl::finish()
630 checkbox->setCheckState( p_item->i_value == VLC_TRUE ? Qt::Checked
632 checkbox->setToolTip( qfu(p_item->psz_longtext) );
635 int BoolConfigControl::getValue()
637 return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
640 /**************************************************************************
641 * Float-based controls
642 *************************************************************************/
644 /*********** Float **************/
645 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
646 module_config_t *_p_item,
647 QWidget *_parent, QGridLayout *l,
649 VFloatConfigControl( _p_this, _p_item, _parent )
651 label = new QLabel( qfu(p_item->psz_text) );
652 spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
653 spin->setMaximumWidth( 90 );
658 QHBoxLayout *layout = new QHBoxLayout();
659 layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
660 widget->setLayout( layout );
664 l->addWidget( label, line, 0 );
665 l->addWidget( spin, line, 1, Qt::AlignRight );
669 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
670 module_config_t *_p_item,
672 QDoubleSpinBox *_spin ) :
673 VFloatConfigControl( _p_this, _p_item )
680 void FloatConfigControl::finish()
682 spin->setMaximum( 2000000000. );
683 spin->setMinimum( -2000000000. );
684 spin->setSingleStep( 0.1 );
685 spin->setValue( (double)p_item->f_value );
686 spin->setToolTip( qfu(p_item->psz_longtext) );
688 label->setToolTip( qfu(p_item->psz_longtext) );
691 float FloatConfigControl::getValue()
693 return (float)spin->value();
696 /*********** Float with range **************/
697 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
698 module_config_t *_p_item,
699 QWidget *_parent, QGridLayout *l,
701 FloatConfigControl( _p_this, _p_item, _parent, l, line )
706 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
707 module_config_t *_p_item,
709 QDoubleSpinBox *_spin ) :
710 FloatConfigControl( _p_this, _p_item, _label, _spin )
715 void FloatRangeConfigControl::finish()
717 spin->setMaximum( (double)p_item->f_max );
718 spin->setMinimum( (double)p_item->f_min );
722 /**********************************************************************
723 * Key selector widget
724 **********************************************************************/
725 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
726 module_config_t *_p_item,
727 QWidget *_parent, QGridLayout *l,
729 ConfigControl( _p_this, _p_item, _parent )
732 label = new QLabel( qtr("Select an action to change the associated hotkey") );
733 table = new QTreeWidget( 0 );
738 QVBoxLayout *layout = new QVBoxLayout();
739 layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
740 widget->setLayout( layout );
744 l->addWidget( label, line, 0, 1, 2 );
745 l->addWidget( table, line+1, 0, 1,2 );
749 void KeySelectorControl::finish()
752 label->setToolTip( qfu(p_item->psz_longtext) );
755 table->setColumnCount( 2 );
756 table->setAlternatingRowColors( true );
758 module_t *p_main = config_FindModule( p_this, "main" );
760 module_config_t *p_item = p_main->p_config;
764 if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
765 strstr( p_item->psz_name , "key-" ) )
767 QTreeWidgetItem *treeItem = new QTreeWidgetItem();
768 treeItem->setText( 0, qfu( p_item->psz_text ) );
769 treeItem->setText( 1, VLCKeyToString( p_item->i_value ) );
770 treeItem->setData( 0, Qt::UserRole,
771 QVariant::fromValue( (void*)p_item ) );
773 table->addTopLevelItem( treeItem );
775 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
776 table->resizeColumnToContents( 0 );
778 CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
779 this, selectKey( QTreeWidgetItem * ) );
782 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
784 module_config_t *p_keyItem = static_cast<module_config_t*>
785 (keyItem->data( 0, Qt::UserRole ).value<void*>());
787 KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text );
789 if( d->result() == QDialog::Accepted )
791 p_keyItem->i_value = d->keyValue;
794 for( int i = 0; i < table->topLevelItemCount() ; i++ )
796 QTreeWidgetItem *it = table->topLevelItem(i);
797 module_config_t *p_item = static_cast<module_config_t*>
798 (it->data( 0, Qt::UserRole ).value<void*>());
799 it->setText( 1, VLCKeyToString( p_item->i_value ) );
803 keyItem->setText( 1, VLCKeyToString( p_keyItem->i_value ) );
808 void KeySelectorControl::doApply()
812 KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
813 char * _keyToChange ) :
814 QDialog(0), keyValue(0)
819 keyToChange = _keyToChange;
820 setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange) );
822 QVBoxLayout *l = new QVBoxLayout( this );
823 selected = new QLabel( qtr("Press the new keys for ") + qfu(keyToChange) );
824 warning = new QLabel();
825 l->addWidget( selected , Qt::AlignCenter );
826 l->addWidget( warning, Qt::AlignCenter );
828 QHBoxLayout *l2 = new QHBoxLayout();
829 QPushButton *ok = new QPushButton( qtr("OK") );
831 QPushButton *cancel = new QPushButton( qtr("Cancel") );
832 l2->addWidget( cancel );
834 BUTTONACT( ok, accept() );
835 BUTTONACT( cancel, reject() );
840 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
842 if( e->key() == Qt::Key_Tab ) return;
843 int i_vlck = qtEventToVLCKey( e );
844 selected->setText( VLCKeyToString( i_vlck ) );
846 module_config_t *p_current = NULL;
847 foreach( p_current, values )
849 if( p_current->i_value == i_vlck && strcmp( p_current->psz_text,
852 p_current->i_value = 0;
860 qtr("Warning: the key is already assigned to \"") +
861 QString( p_current->psz_text ) + "\"" );
863 else warning->setText( "" );