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>
50 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
51 module_config_t *p_item,
55 return createControl( p_this, p_item, parent, NULL, i );
58 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
59 module_config_t *p_item,
61 QGridLayout *l, int &line )
63 ConfigControl *p_control = NULL;
64 if( p_item->psz_current ) return NULL;
66 switch( p_item->i_type )
68 case CONFIG_ITEM_MODULE:
69 p_control = new ModuleConfigControl( p_this, p_item, parent, false,
72 case CONFIG_ITEM_MODULE_CAT:
73 p_control = new ModuleConfigControl( p_this, p_item, parent, true,
76 case CONFIG_ITEM_MODULE_LIST:
77 p_control = new ModuleListConfigControl( p_this, p_item, parent, false,
80 case CONFIG_ITEM_MODULE_LIST_CAT:
81 p_control = new ModuleListConfigControl( p_this, p_item, parent, true,
84 case CONFIG_ITEM_STRING:
86 p_control = new StringConfigControl( p_this, p_item, parent,
89 p_control = new StringListConfigControl( p_this, p_item,
90 parent, false, l, line );
92 case CONFIG_ITEM_INTEGER:
94 p_control = new IntegerListConfigControl( p_this, p_item,
95 parent, false, l, line );
96 else if( p_item->min.i || p_item->max.i )
97 p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
100 p_control = new IntegerConfigControl( p_this, p_item, parent,
103 case CONFIG_ITEM_FILE:
104 fprintf( stderr, "Todo (CONFIG_ITEM_FILE)\n" );
106 case CONFIG_ITEM_DIRECTORY:
107 fprintf( stderr, "Todo (CONFIG_ITEM_DIRECTORY)\n" );
109 case CONFIG_ITEM_KEY:
110 p_control = new KeySelectorControl( p_this, p_item, parent, l, line );
112 case CONFIG_ITEM_BOOL:
113 p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
115 case CONFIG_ITEM_FLOAT:
116 if( p_item->min.f || p_item->max.f )
117 p_control = new FloatRangeConfigControl( p_this, p_item, parent,
120 p_control = new FloatConfigControl( p_this, p_item, parent,
129 void ConfigControl::doApply( intf_thread_t *p_intf )
135 VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(this);
137 config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
142 VFloatConfigControl *vfcc =
143 qobject_cast<VFloatConfigControl *>(this);
145 config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
150 VStringConfigControl *vscc =
151 qobject_cast<VStringConfigControl *>(this);
153 config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
158 KeySelectorControl *ksc = qobject_cast<KeySelectorControl *>(this);
165 /**************************************************************************
166 * String-based controls
167 *************************************************************************/
169 /*********** String **************/
170 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
171 module_config_t *_p_item,
172 QWidget *_parent, QGridLayout *l,
173 int &line, bool pwd ) :
174 VStringConfigControl( _p_this, _p_item, _parent )
176 label = new QLabel( qfu(p_item->psz_text) );
177 text = new QLineEdit( qfu(p_item->value.psz) );
182 QHBoxLayout *layout = new QHBoxLayout();
183 layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
184 widget->setLayout( layout );
188 l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
192 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
193 module_config_t *_p_item,
194 QLabel *_label, QLineEdit *_text, bool pwd ):
195 VStringConfigControl( _p_this, _p_item )
202 void StringConfigControl::finish()
204 text->setText( qfu(p_item->value.psz) );
205 text->setToolTip( qfu(p_item->psz_longtext) );
207 label->setToolTip( qfu(p_item->psz_longtext) );
210 /********* String / choice list **********/
211 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
212 module_config_t *_p_item, QWidget *_parent, bool bycat,
213 QGridLayout *l, int &line) :
214 VStringConfigControl( _p_this, _p_item, _parent )
216 label = new QLabel( qfu(p_item->psz_text) );
217 combo = new QComboBox();
221 QHBoxLayout *layout = new QHBoxLayout();
222 layout->addWidget( label ); layout->addWidget( combo );
223 widget->setLayout( layout );
227 l->addWidget( label, line, 0 );
228 l->addWidget( combo, line, 1, Qt::AlignRight );
231 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
232 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
233 bool bycat ) : VStringConfigControl( _p_this, _p_item )
240 void StringListConfigControl::finish( bool bycat )
242 combo->setEditable( false );
244 for( int i_index = 0; i_index < p_item->i_list; i_index++ )
246 combo->addItem( qfu(p_item->ppsz_list_text ?
247 p_item->ppsz_list_text[i_index] :
248 p_item->ppsz_list[i_index] ),
249 QVariant( p_item->ppsz_list[i_index] ) );
250 if( p_item->value.psz && !strcmp( p_item->value.psz,
251 p_item->ppsz_list[i_index] ) )
252 combo->setCurrentIndex( combo->count() - 1 );
254 combo->setToolTip( qfu(p_item->psz_longtext) );
256 label->setToolTip( qfu(p_item->psz_longtext) );
259 QString StringListConfigControl::getValue()
261 return combo->itemData( combo->currentIndex() ).toString();
264 /********* Module **********/
265 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
266 module_config_t *_p_item, QWidget *_parent, bool bycat,
267 QGridLayout *l, int &line) :
268 VStringConfigControl( _p_this, _p_item, _parent )
270 label = new QLabel( qfu(p_item->psz_text) );
271 combo = new QComboBox();
275 QHBoxLayout *layout = new QHBoxLayout();
276 layout->addWidget( label ); layout->addWidget( combo );
277 widget->setLayout( layout );
281 l->addWidget( label, line, 0 );
282 l->addWidget( combo, line, 1, Qt::AlignRight );
285 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
286 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
287 bool bycat ) : VStringConfigControl( _p_this, _p_item )
294 void ModuleConfigControl::finish( bool bycat )
299 combo->setEditable( false );
301 /* build a list of available modules */
302 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
303 combo->addItem( qtr("Default") );
304 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
306 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
310 if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
312 for (size_t i = 0; i < p_parser->confsize; i++)
314 module_config_t *p_config = p_parser->p_config + i;
315 /* Hack: required subcategory is stored in i_min */
316 if( p_config->i_type == CONFIG_SUBCATEGORY &&
317 p_config->value.i == p_item->min.i )
318 combo->addItem( qfu(p_parser->psz_longname),
319 QVariant( p_parser->psz_object_name ) );
320 if( p_item->value.psz && !strcmp( p_item->value.psz,
321 p_parser->psz_object_name) )
322 combo->setCurrentIndex( combo->count() - 1 );
325 else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
327 combo->addItem( qfu(p_parser->psz_longname),
328 QVariant( p_parser->psz_object_name ) );
329 if( p_item->value.psz && !strcmp( p_item->value.psz,
330 p_parser->psz_object_name) )
331 combo->setCurrentIndex( combo->count() - 1 );
334 vlc_list_release( p_list );
335 combo->setToolTip( qfu(p_item->psz_longtext) );
337 label->setToolTip( qfu(p_item->psz_longtext) );
340 QString ModuleConfigControl::getValue()
342 return combo->itemData( combo->currentIndex() ).toString();
345 /********* Module list **********/
346 ModuleListConfigControl::ModuleListConfigControl( vlc_object_t *_p_this,
347 module_config_t *_p_item, QWidget *_parent, bool bycat,
348 QGridLayout *l, int &line) :
349 VStringConfigControl( _p_this, _p_item, _parent )
351 label = new QLabel( qfu(p_item->psz_text) );
352 text = new QLineEdit();
358 l = new QGridLayout();
362 for( QVector<QCheckBox*>::iterator it = modules.begin();
363 it != modules.end(); it++ )
365 l->addWidget( *it, line++, 1 );
367 l->addWidget( label, line, 0 );
368 l->addWidget( text, line, 1 );
370 widget->setLayout( l );
373 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
374 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
375 bool bycat ) : VStringConfigControl( _p_this, _p_item )
383 ModuleListConfigControl::~ModuleListConfigControl()
385 for( QVector<QCheckBox*>::iterator it = modules.begin();
386 it != modules.end(); it++ )
394 void ModuleListConfigControl::finish( bool bycat )
399 /* build a list of available modules */
400 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
401 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
403 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
407 if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
409 for (size_t i = 0; i < p_parser->confsize; i++)
411 module_config_t *p_config = p_parser->p_config + i;
412 /* Hack: required subcategory is stored in i_min */
413 if( p_config->i_type == CONFIG_SUBCATEGORY &&
414 p_config->value.i == p_item->min.i )
417 new QCheckBox( qfu( p_parser->psz_object_name ) );
418 cb->setToolTip( qfu(p_parser->psz_longname) );
419 modules.push_back( cb );
423 else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
426 new QCheckBox( qfu( p_parser->psz_object_name ) );
427 cb->setToolTip( qfu(p_parser->psz_longname) );
428 modules.push_back( cb );
431 vlc_list_release( p_list );
432 text->setToolTip( qfu(p_item->psz_longtext) );
434 label->setToolTip( qfu(p_item->psz_longtext) );
437 QString ModuleListConfigControl::getValue()
442 void ModuleListConfigControl::hide()
444 for( QVector<QCheckBox*>::iterator it = modules.begin();
445 it != modules.end(); it++ )
453 void ModuleListConfigControl::show()
455 for( QVector<QCheckBox*>::iterator it = modules.begin();
456 it != modules.end(); it++ )
465 void ModuleListConfigControl::wakeUp_TheUserJustClickedOnSomething( int value )
468 for( QVector<QCheckBox*>::iterator it = modules.begin();
469 it != modules.end(); it++ )
474 /**************************************************************************
475 * Integer-based controls
476 *************************************************************************/
478 /*********** Integer **************/
479 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
480 module_config_t *_p_item,
481 QWidget *_parent, QGridLayout *l,
483 VIntConfigControl( _p_this, _p_item, _parent )
485 label = new QLabel( qfu(p_item->psz_text) );
486 spin = new QSpinBox; spin->setMinimumWidth( 80 );
487 spin->setMaximumWidth( 90 );
492 QHBoxLayout *layout = new QHBoxLayout();
493 layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
494 widget->setLayout( layout );
498 l->addWidget( label, line, 0 );
499 l->addWidget( spin, line, 1, Qt::AlignRight );
502 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
503 module_config_t *_p_item,
504 QLabel *_label, QSpinBox *_spin ) :
505 VIntConfigControl( _p_this, _p_item )
512 void IntegerConfigControl::finish()
514 spin->setMaximum( 2000000000 );
515 spin->setMinimum( -2000000000 );
516 spin->setValue( p_item->value.i );
517 spin->setToolTip( qfu(p_item->psz_longtext) );
519 label->setToolTip( qfu(p_item->psz_longtext) );
522 int IntegerConfigControl::getValue()
524 return spin->value();
527 /********* Integer range **********/
528 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
529 module_config_t *_p_item,
530 QWidget *_parent, QGridLayout *l,
532 IntegerConfigControl( _p_this, _p_item, _parent, l, line )
537 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
538 module_config_t *_p_item,
539 QLabel *_label, QSpinBox *_spin ) :
540 IntegerConfigControl( _p_this, _p_item, _label, _spin )
545 void IntegerRangeConfigControl::finish()
547 spin->setMaximum( p_item->max.i );
548 spin->setMinimum( p_item->min.i );
551 IntegerRangeSliderConfigControl::IntegerRangeSliderConfigControl(
552 vlc_object_t *_p_this,
553 module_config_t *_p_item,
554 QLabel *_label, QSlider *_slider ):
555 VIntConfigControl( _p_this, _p_item )
559 slider->setMaximum( p_item->max.i );
560 slider->setMinimum( p_item->min.i );
561 slider->setValue( p_item->value.i );
562 slider->setToolTip( qfu(p_item->psz_longtext) );
564 label->setToolTip( qfu(p_item->psz_longtext) );
567 int IntegerRangeSliderConfigControl::getValue()
569 return slider->value();
573 /********* Integer / choice list **********/
574 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
575 module_config_t *_p_item, QWidget *_parent, bool bycat,
576 QGridLayout *l, int &line) :
577 VIntConfigControl( _p_this, _p_item, _parent )
579 label = new QLabel( qfu(p_item->psz_text) );
580 combo = new QComboBox();
584 QHBoxLayout *layout = new QHBoxLayout();
585 layout->addWidget( label ); layout->addWidget( combo );
586 widget->setLayout( layout );
590 l->addWidget( label, line, 0 );
591 l->addWidget( combo, line, 1, Qt::AlignRight );
594 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
595 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
596 bool bycat ) : VIntConfigControl( _p_this, _p_item )
603 void IntegerListConfigControl::finish( bool bycat )
605 combo->setEditable( false );
607 for( int i_index = 0; i_index < p_item->i_list; i_index++ )
609 combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
610 QVariant( p_item->pi_list[i_index] ) );
611 if( p_item->value.i == p_item->pi_list[i_index] )
612 combo->setCurrentIndex( combo->count() - 1 );
614 combo->setToolTip( qfu(p_item->psz_longtext) );
616 label->setToolTip( qfu(p_item->psz_longtext) );
619 int IntegerListConfigControl::getValue()
621 return combo->itemData( combo->currentIndex() ).toInt();
624 /*********** Boolean **************/
625 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
626 module_config_t *_p_item,
627 QWidget *_parent, QGridLayout *l,
629 VIntConfigControl( _p_this, _p_item, _parent )
631 checkbox = new QCheckBox( qfu(p_item->psz_text) );
636 QHBoxLayout *layout = new QHBoxLayout();
637 layout->addWidget( checkbox, 0 );
638 widget->setLayout( layout );
642 l->addWidget( checkbox, line, 0 );
645 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
646 module_config_t *_p_item,
648 QCheckBox *_checkbox,
650 VIntConfigControl( _p_this, _p_item )
652 checkbox = _checkbox;
656 void BoolConfigControl::finish()
658 checkbox->setCheckState( p_item->value.i == VLC_TRUE ? Qt::Checked
660 checkbox->setToolTip( qfu(p_item->psz_longtext) );
663 int BoolConfigControl::getValue()
665 return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
668 /**************************************************************************
669 * Float-based controls
670 *************************************************************************/
672 /*********** Float **************/
673 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
674 module_config_t *_p_item,
675 QWidget *_parent, QGridLayout *l,
677 VFloatConfigControl( _p_this, _p_item, _parent )
679 label = new QLabel( qfu(p_item->psz_text) );
680 spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
681 spin->setMaximumWidth( 90 );
686 QHBoxLayout *layout = new QHBoxLayout();
687 layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
688 widget->setLayout( layout );
692 l->addWidget( label, line, 0 );
693 l->addWidget( spin, line, 1, Qt::AlignRight );
697 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
698 module_config_t *_p_item,
700 QDoubleSpinBox *_spin ) :
701 VFloatConfigControl( _p_this, _p_item )
708 void FloatConfigControl::finish()
710 spin->setMaximum( 2000000000. );
711 spin->setMinimum( -2000000000. );
712 spin->setSingleStep( 0.1 );
713 spin->setValue( (double)p_item->value.f );
714 spin->setToolTip( qfu(p_item->psz_longtext) );
716 label->setToolTip( qfu(p_item->psz_longtext) );
719 float FloatConfigControl::getValue()
721 return (float)spin->value();
724 /*********** Float with range **************/
725 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
726 module_config_t *_p_item,
727 QWidget *_parent, QGridLayout *l,
729 FloatConfigControl( _p_this, _p_item, _parent, l, line )
734 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
735 module_config_t *_p_item,
737 QDoubleSpinBox *_spin ) :
738 FloatConfigControl( _p_this, _p_item, _label, _spin )
743 void FloatRangeConfigControl::finish()
745 spin->setMaximum( (double)p_item->max.f );
746 spin->setMinimum( (double)p_item->min.f );
750 /**********************************************************************
751 * Key selector widget
752 **********************************************************************/
753 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
754 module_config_t *_p_item,
755 QWidget *_parent, QGridLayout *l,
757 ConfigControl( _p_this, _p_item, _parent )
760 label = new QLabel( qtr("Select an action to change the associated hotkey") );
761 table = new QTreeWidget( 0 );
766 QVBoxLayout *layout = new QVBoxLayout();
767 layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
768 widget->setLayout( layout );
772 l->addWidget( label, line, 0, 1, 2 );
773 l->addWidget( table, line+1, 0, 1,2 );
777 void KeySelectorControl::finish()
780 label->setToolTip( qfu(p_item->psz_longtext) );
783 table->setColumnCount( 2 );
784 table->setAlternatingRowColors( true );
786 module_t *p_main = config_FindModule( p_this, "main" );
789 for (size_t i = 0; i < p_main->confsize; i++)
791 module_config_t *p_item = p_main->p_config + i;
793 if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
794 strstr( p_item->psz_name , "key-" ) )
796 QTreeWidgetItem *treeItem = new QTreeWidgetItem();
797 treeItem->setText( 0, qfu( p_item->psz_text ) );
798 treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
799 treeItem->setData( 0, Qt::UserRole,
800 QVariant::fromValue( (void*)p_item ) );
802 table->addTopLevelItem( treeItem );
805 table->resizeColumnToContents( 0 );
807 CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
808 this, selectKey( QTreeWidgetItem * ) );
811 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
813 module_config_t *p_keyItem = static_cast<module_config_t*>
814 (keyItem->data( 0, Qt::UserRole ).value<void*>());
816 KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text );
818 if( d->result() == QDialog::Accepted )
820 p_keyItem->value.i = d->keyValue;
823 for( int i = 0; i < table->topLevelItemCount() ; i++ )
825 QTreeWidgetItem *it = table->topLevelItem(i);
826 module_config_t *p_item = static_cast<module_config_t*>
827 (it->data( 0, Qt::UserRole ).value<void*>());
828 it->setText( 1, VLCKeyToString( p_item->value.i ) );
832 keyItem->setText( 1, VLCKeyToString( p_keyItem->value.i ) );
837 void KeySelectorControl::doApply()
839 foreach( module_config_t *p_current, values )
841 config_PutInt( p_this, p_current->psz_name, p_current->value.i );
845 KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
846 const char * _keyToChange ) :
847 QDialog(0), keyValue(0)
852 keyToChange = _keyToChange;
853 setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange) );
855 QVBoxLayout *l = new QVBoxLayout( this );
856 selected = new QLabel( qtr("Press the new keys for ") + qfu(keyToChange) );
857 warning = new QLabel();
858 l->addWidget( selected , Qt::AlignCenter );
859 l->addWidget( warning, Qt::AlignCenter );
861 QHBoxLayout *l2 = new QHBoxLayout();
862 QPushButton *ok = new QPushButton( qtr("OK") );
864 QPushButton *cancel = new QPushButton( qtr("Cancel") );
865 l2->addWidget( cancel );
867 BUTTONACT( ok, accept() );
868 BUTTONACT( cancel, reject() );
873 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
875 if( e->key() == Qt::Key_Tab ) return;
876 int i_vlck = qtEventToVLCKey( e );
877 selected->setText( VLCKeyToString( i_vlck ) );
879 module_config_t *p_current = NULL;
880 foreach( p_current, values )
882 if( p_current->value.i == i_vlck && strcmp( p_current->psz_text,
885 p_current->value.i = 0;
893 qtr("Warning: the key is already assigned to \"") +
894 QString( p_current->psz_text ) + "\"" );
896 else warning->setText( "" );