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);
136 config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
141 VFloatConfigControl *vfcc =
142 qobject_cast<VFloatConfigControl *>(this);
144 config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
149 VStringConfigControl *vscc =
150 qobject_cast<VStringConfigControl *>(this);
152 config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
157 KeySelectorControl *ksc = qobject_cast<KeySelectorControl *>(this);
164 /**************************************************************************
165 * String-based controls
166 *************************************************************************/
168 /*********** String **************/
169 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
170 module_config_t *_p_item,
171 QWidget *_parent, QGridLayout *l,
172 int &line, bool pwd ) :
173 VStringConfigControl( _p_this, _p_item, _parent )
175 label = new QLabel( qfu(p_item->psz_text) );
176 text = new QLineEdit( qfu(p_item->psz_value) );
181 QHBoxLayout *layout = new QHBoxLayout();
182 layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
183 widget->setLayout( layout );
187 l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
191 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
192 module_config_t *_p_item,
193 QLabel *_label, QLineEdit *_text, bool pwd ):
194 VStringConfigControl( _p_this, _p_item )
201 void StringConfigControl::finish()
203 text->setText( qfu(p_item->psz_value) );
204 text->setToolTip( qfu(p_item->psz_longtext) );
206 label->setToolTip( qfu(p_item->psz_longtext) );
209 /********* String / choice list **********/
210 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
211 module_config_t *_p_item, QWidget *_parent, bool bycat,
212 QGridLayout *l, int &line) :
213 VStringConfigControl( _p_this, _p_item, _parent )
215 label = new QLabel( qfu(p_item->psz_text) );
216 combo = new QComboBox();
220 QHBoxLayout *layout = new QHBoxLayout();
221 layout->addWidget( label ); layout->addWidget( combo );
222 widget->setLayout( layout );
226 l->addWidget( label, line, 0 );
227 l->addWidget( combo, line, 1, Qt::AlignRight );
230 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
231 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
232 bool bycat ) : VStringConfigControl( _p_this, _p_item )
239 void StringListConfigControl::finish( bool bycat )
241 combo->setEditable( false );
243 for( int i_index = 0; i_index < p_item->i_list; i_index++ )
245 combo->addItem( qfu(p_item->ppsz_list_text ?
246 p_item->ppsz_list_text[i_index] :
247 p_item->ppsz_list[i_index] ),
248 QVariant( p_item->ppsz_list[i_index] ) );
249 if( p_item->psz_value && !strcmp( p_item->psz_value,
250 p_item->ppsz_list[i_index] ) )
251 combo->setCurrentIndex( combo->count() - 1 );
253 combo->setToolTip( qfu(p_item->psz_longtext) );
255 label->setToolTip( qfu(p_item->psz_longtext) );
258 QString StringListConfigControl::getValue()
260 return combo->itemData( combo->currentIndex() ).toString();
263 /********* Module **********/
264 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
265 module_config_t *_p_item, QWidget *_parent, bool bycat,
266 QGridLayout *l, int &line) :
267 VStringConfigControl( _p_this, _p_item, _parent )
269 label = new QLabel( qfu(p_item->psz_text) );
270 combo = new QComboBox();
274 QHBoxLayout *layout = new QHBoxLayout();
275 layout->addWidget( label ); layout->addWidget( combo );
276 widget->setLayout( layout );
280 l->addWidget( label, line, 0 );
281 l->addWidget( combo, line, 1, Qt::AlignRight );
284 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
285 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
286 bool bycat ) : VStringConfigControl( _p_this, _p_item )
293 void ModuleConfigControl::finish( bool bycat )
298 combo->setEditable( false );
300 /* build a list of available modules */
301 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
302 combo->addItem( qtr("Default") );
303 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
305 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
309 if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
311 module_config_t *p_config = p_parser->p_config;
314 /* Hack: required subcategory is stored in i_min */
315 if( p_config->i_type == CONFIG_SUBCATEGORY &&
316 p_config->i_value == p_item->i_min )
317 combo->addItem( qfu(p_parser->psz_longname),
318 QVariant( p_parser->psz_object_name ) );
319 if( p_item->psz_value && !strcmp( p_item->psz_value,
320 p_parser->psz_object_name) )
321 combo->setCurrentIndex( combo->count() - 1 );
322 } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
324 else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
326 combo->addItem( qfu(p_parser->psz_longname),
327 QVariant( p_parser->psz_object_name ) );
328 if( p_item->psz_value && !strcmp( p_item->psz_value,
329 p_parser->psz_object_name) )
330 combo->setCurrentIndex( combo->count() - 1 );
333 vlc_list_release( p_list );
334 combo->setToolTip( qfu(p_item->psz_longtext) );
336 label->setToolTip( qfu(p_item->psz_longtext) );
339 QString ModuleConfigControl::getValue()
341 return combo->itemData( combo->currentIndex() ).toString();
344 /********* Module list **********/
345 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
346 module_config_t *_p_item, QWidget *_parent, bool bycat,
347 QGridLayout *l, int &line) :
348 VStringConfigControl( _p_this, _p_item, _parent )
350 label = new QLabel( qfu(p_item->psz_text) );
351 text = new QLineEdit();
357 l = new QGridLayout();
361 for( QVector<QCheckBox*>::iterator it = modules.begin();
362 it != modules.end(); it++ )
364 l->addWidget( *it, line++, 1 );
366 l->addWidget( label, line, 0 );
367 l->addWidget( text, line, 1 );
369 widget->setLayout( l );
372 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
373 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
374 bool bycat ) : VStringConfigControl( _p_this, _p_item )
382 ModuleListConfigControl::~ModuleListConfigControl()
384 for( QVector<QCheckBox*>::iterator it = modules.begin();
385 it != modules.end(); it++ )
393 void ModuleListConfigControl::finish( bool bycat )
398 /* build a list of available modules */
399 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
400 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
402 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
406 if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
408 module_config_t *p_config = p_parser->p_config;
411 /* Hack: required subcategory is stored in i_min */
412 if( p_config->i_type == CONFIG_SUBCATEGORY &&
413 p_config->i_value == p_item->i_min )
416 new QCheckBox( qfu( p_parser->psz_object_name ) );
417 cb->setToolTip( qfu(p_parser->psz_longname) );
418 modules.push_back( cb );
420 } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
422 else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
425 new QCheckBox( qfu( p_parser->psz_object_name ) );
426 cb->setToolTip( qfu(p_parser->psz_longname) );
427 modules.push_back( cb );
430 vlc_list_release( p_list );
431 text->setToolTip( qfu(p_item->psz_longtext) );
433 label->setToolTip( qfu(p_item->psz_longtext) );
436 QString ModuleListConfigControl::getValue()
441 void ModuleListConfigControl::hide()
443 for( QVector<QCheckBox*>::iterator it = modules.begin();
444 it != modules.end(); it++ )
452 void ModuleListConfigControl::show()
454 for( QVector<QCheckBox*>::iterator it = modules.begin();
455 it != modules.end(); it++ )
464 void ModuleListConfigControl::wakeUp_TheUserJustClickedOnSomething( int value )
467 for( QVector<QCheckBox*>::iterator it = modules.begin();
468 it != modules.end(); it++ )
473 /**************************************************************************
474 * Integer-based controls
475 *************************************************************************/
477 /*********** Integer **************/
478 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
479 module_config_t *_p_item,
480 QWidget *_parent, QGridLayout *l,
482 VIntConfigControl( _p_this, _p_item, _parent )
484 label = new QLabel( qfu(p_item->psz_text) );
485 spin = new QSpinBox; spin->setMinimumWidth( 80 );
486 spin->setMaximumWidth( 90 );
491 QHBoxLayout *layout = new QHBoxLayout();
492 layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
493 widget->setLayout( layout );
497 l->addWidget( label, line, 0 );
498 l->addWidget( spin, line, 1, Qt::AlignRight );
501 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
502 module_config_t *_p_item,
503 QLabel *_label, QSpinBox *_spin ) :
504 VIntConfigControl( _p_this, _p_item )
511 void IntegerConfigControl::finish()
513 spin->setMaximum( 2000000000 );
514 spin->setMinimum( -2000000000 );
515 spin->setValue( p_item->i_value );
516 spin->setToolTip( qfu(p_item->psz_longtext) );
518 label->setToolTip( qfu(p_item->psz_longtext) );
521 int IntegerConfigControl::getValue()
523 return spin->value();
526 /********* Integer range **********/
527 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
528 module_config_t *_p_item,
529 QWidget *_parent, QGridLayout *l,
531 IntegerConfigControl( _p_this, _p_item, _parent, l, line )
536 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
537 module_config_t *_p_item,
538 QLabel *_label, QSpinBox *_spin ) :
539 IntegerConfigControl( _p_this, _p_item, _label, _spin )
544 void IntegerRangeConfigControl::finish()
546 spin->setMaximum( p_item->i_max );
547 spin->setMinimum( p_item->i_min );
550 /********* Integer / choice list **********/
551 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
552 module_config_t *_p_item, QWidget *_parent, bool bycat,
553 QGridLayout *l, int &line) :
554 VIntConfigControl( _p_this, _p_item, _parent )
556 label = new QLabel( qfu(p_item->psz_text) );
557 combo = new QComboBox();
561 QHBoxLayout *layout = new QHBoxLayout();
562 layout->addWidget( label ); layout->addWidget( combo );
563 widget->setLayout( layout );
567 l->addWidget( label, line, 0 );
568 l->addWidget( combo, line, 1, Qt::AlignRight );
571 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
572 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
573 bool bycat ) : VIntConfigControl( _p_this, _p_item )
580 void IntegerListConfigControl::finish( bool bycat )
582 combo->setEditable( false );
584 for( int i_index = 0; i_index < p_item->i_list; i_index++ )
586 combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
587 QVariant( p_item->pi_list[i_index] ) );
588 if( p_item->i_value == p_item->pi_list[i_index] )
589 combo->setCurrentIndex( combo->count() - 1 );
591 combo->setToolTip( qfu(p_item->psz_longtext) );
593 label->setToolTip( qfu(p_item->psz_longtext) );
596 int IntegerListConfigControl::getValue()
598 return combo->itemData( combo->currentIndex() ).toInt();
601 /*********** Boolean **************/
602 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
603 module_config_t *_p_item,
604 QWidget *_parent, QGridLayout *l,
606 VIntConfigControl( _p_this, _p_item, _parent )
608 checkbox = new QCheckBox( qfu(p_item->psz_text) );
613 QHBoxLayout *layout = new QHBoxLayout();
614 layout->addWidget( checkbox, 0 );
615 widget->setLayout( layout );
619 l->addWidget( checkbox, line, 0 );
622 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
623 module_config_t *_p_item,
625 QCheckBox *_checkbox,
627 VIntConfigControl( _p_this, _p_item )
629 checkbox = _checkbox;
633 void BoolConfigControl::finish()
635 checkbox->setCheckState( p_item->i_value == VLC_TRUE ? Qt::Checked
637 checkbox->setToolTip( qfu(p_item->psz_longtext) );
640 int BoolConfigControl::getValue()
642 return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
645 /**************************************************************************
646 * Float-based controls
647 *************************************************************************/
649 /*********** Float **************/
650 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
651 module_config_t *_p_item,
652 QWidget *_parent, QGridLayout *l,
654 VFloatConfigControl( _p_this, _p_item, _parent )
656 label = new QLabel( qfu(p_item->psz_text) );
657 spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
658 spin->setMaximumWidth( 90 );
663 QHBoxLayout *layout = new QHBoxLayout();
664 layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
665 widget->setLayout( layout );
669 l->addWidget( label, line, 0 );
670 l->addWidget( spin, line, 1, Qt::AlignRight );
674 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
675 module_config_t *_p_item,
677 QDoubleSpinBox *_spin ) :
678 VFloatConfigControl( _p_this, _p_item )
685 void FloatConfigControl::finish()
687 spin->setMaximum( 2000000000. );
688 spin->setMinimum( -2000000000. );
689 spin->setSingleStep( 0.1 );
690 spin->setValue( (double)p_item->f_value );
691 spin->setToolTip( qfu(p_item->psz_longtext) );
693 label->setToolTip( qfu(p_item->psz_longtext) );
696 float FloatConfigControl::getValue()
698 return (float)spin->value();
701 /*********** Float with range **************/
702 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
703 module_config_t *_p_item,
704 QWidget *_parent, QGridLayout *l,
706 FloatConfigControl( _p_this, _p_item, _parent, l, line )
711 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
712 module_config_t *_p_item,
714 QDoubleSpinBox *_spin ) :
715 FloatConfigControl( _p_this, _p_item, _label, _spin )
720 void FloatRangeConfigControl::finish()
722 spin->setMaximum( (double)p_item->f_max );
723 spin->setMinimum( (double)p_item->f_min );
727 /**********************************************************************
728 * Key selector widget
729 **********************************************************************/
730 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
731 module_config_t *_p_item,
732 QWidget *_parent, QGridLayout *l,
734 ConfigControl( _p_this, _p_item, _parent )
737 label = new QLabel( qtr("Select an action to change the associated hotkey") );
738 table = new QTreeWidget( 0 );
743 QVBoxLayout *layout = new QVBoxLayout();
744 layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
745 widget->setLayout( layout );
749 l->addWidget( label, line, 0, 1, 2 );
750 l->addWidget( table, line+1, 0, 1,2 );
754 void KeySelectorControl::finish()
757 label->setToolTip( qfu(p_item->psz_longtext) );
760 table->setColumnCount( 2 );
761 table->setAlternatingRowColors( true );
763 module_t *p_main = config_FindModule( p_this, "main" );
765 module_config_t *p_item = p_main->p_config;
769 if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
770 strstr( p_item->psz_name , "key-" ) )
772 QTreeWidgetItem *treeItem = new QTreeWidgetItem();
773 treeItem->setText( 0, qfu( p_item->psz_text ) );
774 treeItem->setText( 1, VLCKeyToString( p_item->i_value ) );
775 treeItem->setData( 0, Qt::UserRole,
776 QVariant::fromValue( (void*)p_item ) );
778 table->addTopLevelItem( treeItem );
780 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
781 table->resizeColumnToContents( 0 );
783 CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
784 this, selectKey( QTreeWidgetItem * ) );
787 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
789 module_config_t *p_keyItem = static_cast<module_config_t*>
790 (keyItem->data( 0, Qt::UserRole ).value<void*>());
792 KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text );
794 if( d->result() == QDialog::Accepted )
796 p_keyItem->i_value = d->keyValue;
799 for( int i = 0; i < table->topLevelItemCount() ; i++ )
801 QTreeWidgetItem *it = table->topLevelItem(i);
802 module_config_t *p_item = static_cast<module_config_t*>
803 (it->data( 0, Qt::UserRole ).value<void*>());
804 it->setText( 1, VLCKeyToString( p_item->i_value ) );
808 keyItem->setText( 1, VLCKeyToString( p_keyItem->i_value ) );
813 void KeySelectorControl::doApply()
815 foreach( module_config_t *p_current, values )
817 config_PutInt( p_this, p_current->psz_name, p_current->i_value );
821 KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
822 const char * _keyToChange ) :
823 QDialog(0), keyValue(0)
828 keyToChange = _keyToChange;
829 setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange) );
831 QVBoxLayout *l = new QVBoxLayout( this );
832 selected = new QLabel( qtr("Press the new keys for ") + qfu(keyToChange) );
833 warning = new QLabel();
834 l->addWidget( selected , Qt::AlignCenter );
835 l->addWidget( warning, Qt::AlignCenter );
837 QHBoxLayout *l2 = new QHBoxLayout();
838 QPushButton *ok = new QPushButton( qtr("OK") );
840 QPushButton *cancel = new QPushButton( qtr("Cancel") );
841 l2->addWidget( cancel );
843 BUTTONACT( ok, accept() );
844 BUTTONACT( cancel, reject() );
849 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
851 if( e->key() == Qt::Key_Tab ) return;
852 int i_vlck = qtEventToVLCKey( e );
853 selected->setText( VLCKeyToString( i_vlck ) );
855 module_config_t *p_current = NULL;
856 foreach( p_current, values )
858 if( p_current->i_value == i_vlck && strcmp( p_current->psz_text,
861 p_current->i_value = 0;
869 qtr("Warning: the key is already assigned to \"") +
870 QString( p_current->psz_text ) + "\"" );
872 else warning->setText( "" );