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,
556 VIntConfigControl( _p_this, _p_item )
560 slider->setMaximum( p_item->max.i );
561 slider->setMinimum( p_item->min.i );
562 slider->setValue( p_item->value.i );
563 slider->setToolTip( qfu(p_item->psz_longtext) );
565 label->setToolTip( qfu(p_item->psz_longtext) );
568 int IntegerRangeSliderConfigControl::getValue()
570 return slider->value();
574 /********* Integer / choice list **********/
575 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
576 module_config_t *_p_item, QWidget *_parent, bool bycat,
577 QGridLayout *l, int &line) :
578 VIntConfigControl( _p_this, _p_item, _parent )
580 label = new QLabel( qfu(p_item->psz_text) );
581 combo = new QComboBox();
585 QHBoxLayout *layout = new QHBoxLayout();
586 layout->addWidget( label ); layout->addWidget( combo );
587 widget->setLayout( layout );
591 l->addWidget( label, line, 0 );
592 l->addWidget( combo, line, 1, Qt::AlignRight );
595 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
596 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
597 bool bycat ) : VIntConfigControl( _p_this, _p_item )
604 void IntegerListConfigControl::finish( bool bycat )
606 combo->setEditable( false );
608 for( int i_index = 0; i_index < p_item->i_list; i_index++ )
610 combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
611 QVariant( p_item->pi_list[i_index] ) );
612 if( p_item->value.i == p_item->pi_list[i_index] )
613 combo->setCurrentIndex( combo->count() - 1 );
615 combo->setToolTip( qfu(p_item->psz_longtext) );
617 label->setToolTip( qfu(p_item->psz_longtext) );
620 int IntegerListConfigControl::getValue()
622 return combo->itemData( combo->currentIndex() ).toInt();
625 /*********** Boolean **************/
626 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
627 module_config_t *_p_item,
628 QWidget *_parent, QGridLayout *l,
630 VIntConfigControl( _p_this, _p_item, _parent )
632 checkbox = new QCheckBox( qfu(p_item->psz_text) );
637 QHBoxLayout *layout = new QHBoxLayout();
638 layout->addWidget( checkbox, 0 );
639 widget->setLayout( layout );
643 l->addWidget( checkbox, line, 0 );
646 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
647 module_config_t *_p_item,
649 QCheckBox *_checkbox,
651 VIntConfigControl( _p_this, _p_item )
653 checkbox = _checkbox;
657 void BoolConfigControl::finish()
659 checkbox->setCheckState( p_item->value.i == VLC_TRUE ? Qt::Checked
661 checkbox->setToolTip( qfu(p_item->psz_longtext) );
664 int BoolConfigControl::getValue()
666 return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
669 /**************************************************************************
670 * Float-based controls
671 *************************************************************************/
673 /*********** Float **************/
674 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
675 module_config_t *_p_item,
676 QWidget *_parent, QGridLayout *l,
678 VFloatConfigControl( _p_this, _p_item, _parent )
680 label = new QLabel( qfu(p_item->psz_text) );
681 spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
682 spin->setMaximumWidth( 90 );
687 QHBoxLayout *layout = new QHBoxLayout();
688 layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
689 widget->setLayout( layout );
693 l->addWidget( label, line, 0 );
694 l->addWidget( spin, line, 1, Qt::AlignRight );
698 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
699 module_config_t *_p_item,
701 QDoubleSpinBox *_spin ) :
702 VFloatConfigControl( _p_this, _p_item )
709 void FloatConfigControl::finish()
711 spin->setMaximum( 2000000000. );
712 spin->setMinimum( -2000000000. );
713 spin->setSingleStep( 0.1 );
714 spin->setValue( (double)p_item->value.f );
715 spin->setToolTip( qfu(p_item->psz_longtext) );
717 label->setToolTip( qfu(p_item->psz_longtext) );
720 float FloatConfigControl::getValue()
722 return (float)spin->value();
725 /*********** Float with range **************/
726 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
727 module_config_t *_p_item,
728 QWidget *_parent, QGridLayout *l,
730 FloatConfigControl( _p_this, _p_item, _parent, l, line )
735 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
736 module_config_t *_p_item,
738 QDoubleSpinBox *_spin ) :
739 FloatConfigControl( _p_this, _p_item, _label, _spin )
744 void FloatRangeConfigControl::finish()
746 spin->setMaximum( (double)p_item->max.f );
747 spin->setMinimum( (double)p_item->min.f );
751 /**********************************************************************
752 * Key selector widget
753 **********************************************************************/
754 KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
755 module_config_t *_p_item,
756 QWidget *_parent, QGridLayout *l,
758 ConfigControl( _p_this, _p_item, _parent )
761 label = new QLabel( qtr("Select an action to change the associated hotkey") );
762 table = new QTreeWidget( 0 );
767 QVBoxLayout *layout = new QVBoxLayout();
768 layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
769 widget->setLayout( layout );
773 l->addWidget( label, line, 0, 1, 2 );
774 l->addWidget( table, line+1, 0, 1,2 );
778 void KeySelectorControl::finish()
781 label->setToolTip( qfu(p_item->psz_longtext) );
784 table->setColumnCount( 2 );
785 table->setAlternatingRowColors( true );
787 module_t *p_main = config_FindModule( p_this, "main" );
790 for (size_t i = 0; i < p_main->confsize; i++)
792 module_config_t *p_item = p_main->p_config + i;
794 if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
795 strstr( p_item->psz_name , "key-" ) )
797 QTreeWidgetItem *treeItem = new QTreeWidgetItem();
798 treeItem->setText( 0, qfu( p_item->psz_text ) );
799 treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
800 treeItem->setData( 0, Qt::UserRole,
801 QVariant::fromValue( (void*)p_item ) );
803 table->addTopLevelItem( treeItem );
806 table->resizeColumnToContents( 0 );
808 CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
809 this, selectKey( QTreeWidgetItem * ) );
812 void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
814 module_config_t *p_keyItem = static_cast<module_config_t*>
815 (keyItem->data( 0, Qt::UserRole ).value<void*>());
817 KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text );
819 if( d->result() == QDialog::Accepted )
821 p_keyItem->value.i = d->keyValue;
824 for( int i = 0; i < table->topLevelItemCount() ; i++ )
826 QTreeWidgetItem *it = table->topLevelItem(i);
827 module_config_t *p_item = static_cast<module_config_t*>
828 (it->data( 0, Qt::UserRole ).value<void*>());
829 it->setText( 1, VLCKeyToString( p_item->value.i ) );
833 keyItem->setText( 1, VLCKeyToString( p_keyItem->value.i ) );
838 void KeySelectorControl::doApply()
840 foreach( module_config_t *p_current, values )
842 config_PutInt( p_this, p_current->psz_name, p_current->value.i );
846 KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
847 const char * _keyToChange ) :
848 QDialog(0), keyValue(0)
853 keyToChange = _keyToChange;
854 setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange) );
856 QVBoxLayout *l = new QVBoxLayout( this );
857 selected = new QLabel( qtr("Press the new keys for ") + qfu(keyToChange) );
858 warning = new QLabel();
859 l->addWidget( selected , Qt::AlignCenter );
860 l->addWidget( warning, Qt::AlignCenter );
862 QHBoxLayout *l2 = new QHBoxLayout();
863 QPushButton *ok = new QPushButton( qtr("OK") );
865 QPushButton *cancel = new QPushButton( qtr("Cancel") );
866 l2->addWidget( cancel );
868 BUTTONACT( ok, accept() );
869 BUTTONACT( cancel, reject() );
874 void KeyInputDialog::keyPressEvent( QKeyEvent *e )
876 if( e->key() == Qt::Key_Tab ) return;
877 int i_vlck = qtEventToVLCKey( e );
878 selected->setText( VLCKeyToString( i_vlck ) );
880 module_config_t *p_current = NULL;
881 foreach( p_current, values )
883 if( p_current->value.i == i_vlck && strcmp( p_current->psz_text,
886 p_current->value.i = 0;
894 qtr("Warning: the key is already assigned to \"") +
895 QString( p_current->psz_text ) + "\"" );
897 else warning->setText( "" );