]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Float, Float with range and Integer with range widgets.
[vlc] / modules / gui / qt4 / components / preferences_widgets.cpp
1 /*****************************************************************************
2  * preferences_widgets.cpp : Widgets for preferences displays
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Antoine Cellerier <dionoea@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /**
26  * Todo:
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
32  */
33
34 #include "components/preferences_widgets.hpp"
35 #include "qt4.hpp"
36 #include <QLineEdit>
37 #include <QString>
38 #include <QSpinBox>
39 #include <QDoubleSpinBox>
40 #include <QVariant>
41 #include <QComboBox>
42 #include <QGridLayout>
43
44 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
45                                              module_config_t *p_item,
46                                              QWidget *parent )
47 {
48     return createControl( p_this, p_item, parent, NULL, 0 );
49 }
50
51 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
52                                              module_config_t *p_item,
53                                              QWidget *parent,
54                                              QGridLayout *l, int line )
55 {
56     ConfigControl *p_control = NULL;
57     if( p_item->psz_current ) return NULL;
58
59     switch( p_item->i_type )
60     {
61     case CONFIG_ITEM_MODULE:
62         p_control = new ModuleConfigControl( p_this, p_item, parent, false,
63                                              l, line );
64         break;
65     case CONFIG_ITEM_MODULE_CAT:
66         p_control = new ModuleConfigControl( p_this, p_item, parent, true,
67                                              l, line );
68         break;
69     case CONFIG_ITEM_STRING:
70         if( !p_item->i_list )
71             p_control = new StringConfigControl( p_this, p_item, parent,
72                                                  l, line, false );
73         else
74             p_control = new StringListConfigControl( p_this, p_item,
75                                             parent, false, l, line );
76         break;
77     case CONFIG_ITEM_INTEGER:
78         if( p_item->i_list )
79             p_control = new IntegerListConfigControl( p_this, p_item,
80                                             parent, false, l, line );
81         else if( p_item->i_min || p_item->i_max )
82             p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
83                                                        l, line );
84         else
85             p_control = new IntegerConfigControl( p_this, p_item, parent,
86                                                   l, line );
87         break;
88     case CONFIG_ITEM_FILE:
89         fprintf( stderr, "Todo\n" );
90         break;
91     case CONFIG_ITEM_BOOL:
92         p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
93         break;
94     case CONFIG_ITEM_FLOAT:
95         if( p_item->f_min || p_item->f_max )
96             p_control = new FloatRangeConfigControl( p_this, p_item, parent,
97                                                      l, line );
98         else
99             p_control = new FloatConfigControl( p_this, p_item, parent,
100                                                   l, line );
101         break;
102     default:
103         break;
104     }
105     return p_control;
106 }
107
108 /**************************************************************************
109  * String-based controls
110  *************************************************************************/
111
112 /*********** String **************/
113 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
114                                           module_config_t *_p_item,
115                                           QWidget *_parent, QGridLayout *l,
116                                           int line, bool pwd ) :
117                            VStringConfigControl( _p_this, _p_item, _parent )
118 {
119     label = new QLabel( qfu(p_item->psz_text) );
120     text = new QLineEdit( qfu(p_item->psz_value) );
121     finish();
122
123     if( !l )
124     {
125         QHBoxLayout *layout = new QHBoxLayout();
126         layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
127         widget->setLayout( layout );
128     }
129     else
130     {
131         l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
132     }
133 }
134
135 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
136                                    module_config_t *_p_item,
137                                    QLabel *_label, QLineEdit *_text, bool pwd ):
138                            VStringConfigControl( _p_this, _p_item )
139 {
140     text = _text;
141     label = _label;
142     finish( );
143 }
144
145 void StringConfigControl::finish()
146 {
147     text->setText( qfu(p_item->psz_value) );
148     text->setToolTip( qfu(p_item->psz_longtext) );
149     if( label )
150         label->setToolTip( qfu(p_item->psz_longtext) );
151 }
152
153 /********* String / choice list **********/
154 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
155                module_config_t *_p_item, QWidget *_parent, bool bycat,
156                QGridLayout *l, int line) :
157                VStringConfigControl( _p_this, _p_item, _parent )
158 {
159     label = new QLabel( qfu(p_item->psz_text) );
160     combo = new QComboBox();
161     finish( bycat );
162     if( !l )
163     {
164         QHBoxLayout *layout = new QHBoxLayout();
165         layout->addWidget( label ); layout->addWidget( combo );
166         widget->setLayout( layout );
167     }
168     else
169     {
170         l->addWidget( label, line, 0 );
171         l->addWidget( combo, line, 1, Qt::AlignRight );
172     }
173 }
174 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
175                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
176                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
177 {
178     combo = _combo;
179     label = _label;
180     finish( bycat );
181 }
182
183 void StringListConfigControl::finish( bool bycat )
184 {
185     combo->setEditable( false );
186
187     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
188     {
189         combo->addItem( qfu(p_item->ppsz_list_text ?
190                             p_item->ppsz_list_text[i_index] :
191                             p_item->ppsz_list[i_index] ),
192                         QVariant( p_item->ppsz_list[i_index] ) );
193         if( p_item->psz_value && !strcmp( p_item->psz_value,
194                                           p_item->ppsz_list[i_index] ) )
195             combo->setCurrentIndex( combo->count() - 1 );
196     }
197     combo->setToolTip( qfu(p_item->psz_longtext) );
198     if( label )
199         label->setToolTip( qfu(p_item->psz_longtext) );
200 }
201
202 QString StringListConfigControl::getValue()
203 {
204     return combo->itemData( combo->currentIndex() ).toString();
205 }
206
207 /********* Module **********/
208 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
209                module_config_t *_p_item, QWidget *_parent, bool bycat,
210                QGridLayout *l, int line) :
211                VStringConfigControl( _p_this, _p_item, _parent )
212 {
213     label = new QLabel( qfu(p_item->psz_text) );
214     combo = new QComboBox();
215     finish( bycat );
216     if( !l )
217     {
218         QHBoxLayout *layout = new QHBoxLayout();
219         layout->addWidget( label ); layout->addWidget( combo );
220         widget->setLayout( layout );
221     }
222     else
223     {
224         l->addWidget( label, line, 0 );
225         l->addWidget( combo, line, 1, Qt::AlignRight );
226     }
227 }
228 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
229                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
230                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
231 {
232     combo = _combo;
233     label = _label;
234     finish( bycat );
235 }
236
237 void ModuleConfigControl::finish( bool bycat )
238 {
239     vlc_list_t *p_list;
240     module_t *p_parser;
241
242     combo->setEditable( false );
243
244     /* build a list of available modules */
245     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
246     combo->addItem( qtr("Default") );
247     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
248     {
249         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
250
251         if( bycat )
252         {
253             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
254
255             module_config_t *p_config = p_parser->p_config;
256             if( p_config ) do
257             {
258                 /* Hack: required subcategory is stored in i_min */
259                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
260                     p_config->i_value == p_item->i_min )
261                     combo->addItem( qfu(p_parser->psz_longname),
262                                     QVariant( p_parser->psz_object_name ) );
263                 if( p_item->psz_value && !strcmp( p_item->psz_value,
264                                                   p_parser->psz_object_name) )
265                     combo->setCurrentIndex( combo->count() - 1 );
266             } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
267         }
268         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
269         {
270             combo->addItem( qfu(p_parser->psz_longname),
271                             QVariant( p_parser->psz_object_name ) );
272             if( p_item->psz_value && !strcmp( p_item->psz_value,
273                                               p_parser->psz_object_name) )
274                 combo->setCurrentIndex( combo->count() - 1 );
275         }
276     }
277     vlc_list_release( p_list );
278     combo->setToolTip( qfu(p_item->psz_longtext) );
279     if( label )
280         label->setToolTip( qfu(p_item->psz_longtext) );
281 }
282
283 QString ModuleConfigControl::getValue()
284 {
285     return combo->itemData( combo->currentIndex() ).toString();
286 }
287
288 /**************************************************************************
289  * Integer-based controls
290  *************************************************************************/
291
292 /*********** Integer **************/
293 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
294                                             module_config_t *_p_item,
295                                             QWidget *_parent, QGridLayout *l,
296                                             int line ) :
297                            VIntConfigControl( _p_this, _p_item, _parent )
298 {
299     label = new QLabel( qfu(p_item->psz_text) );
300     spin = new QSpinBox; spin->setMinimumWidth( 80 );
301     spin->setMaximumWidth( 90 );
302     finish();
303
304     if( !l )
305     {
306         QHBoxLayout *layout = new QHBoxLayout();
307         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
308         widget->setLayout( layout );
309     }
310     else
311     {
312         l->addWidget( label, line, 0 );
313         l->addWidget( spin, line, 1, Qt::AlignRight );
314     }
315 }
316 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
317                                             module_config_t *_p_item,
318                                             QLabel *_label, QSpinBox *_spin ) :
319                                       VIntConfigControl( _p_this, _p_item )
320 {
321     spin = _spin;
322     label = _label;
323     finish();
324 }
325
326 void IntegerConfigControl::finish()
327 {
328     spin->setMaximum( 2000000000 );
329     spin->setMinimum( -2000000000 );
330     spin->setValue( p_item->i_value );
331     spin->setToolTip( qfu(p_item->psz_longtext) );
332     if( label )
333         label->setToolTip( qfu(p_item->psz_longtext) );
334 }
335
336 int IntegerConfigControl::getValue()
337 {
338     return spin->value();
339 }
340
341 /********* Integer range **********/
342 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
343                                             module_config_t *_p_item,
344                                             QWidget *_parent, QGridLayout *l,
345                                             int line ) :
346             IntegerConfigControl( _p_this, _p_item, _parent, l, line )
347 {
348     finish();
349 }
350
351 IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
352                                             module_config_t *_p_item,
353                                             QLabel *_label, QSpinBox *_spin ) :
354             IntegerConfigControl( _p_this, _p_item, _label, _spin )
355 {
356     finish();
357 }
358
359 void IntegerRangeConfigControl::finish()
360 {
361     spin->setMaximum( p_item->i_max );
362     spin->setMinimum( p_item->i_min );
363 }
364
365 /********* Integer / choice list **********/
366 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
367                module_config_t *_p_item, QWidget *_parent, bool bycat,
368                QGridLayout *l, int line) :
369                VIntConfigControl( _p_this, _p_item, _parent )
370 {
371     label = new QLabel( qfu(p_item->psz_text) );
372     combo = new QComboBox();
373     finish( bycat );
374     if( !l )
375     {
376         QHBoxLayout *layout = new QHBoxLayout();
377         layout->addWidget( label ); layout->addWidget( combo );
378         widget->setLayout( layout );
379     }
380     else
381     {
382         l->addWidget( label, line, 0 );
383         l->addWidget( combo, line, 1, Qt::AlignRight );
384     }
385 }
386 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
387                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
388                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
389 {
390     combo = _combo;
391     label = _label;
392     finish( bycat );
393 }
394
395 void IntegerListConfigControl::finish( bool bycat )
396 {
397     combo->setEditable( false );
398
399     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
400     {
401         combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
402                         QVariant( p_item->pi_list[i_index] ) );
403         if( p_item->i_value == p_item->pi_list[i_index] )
404             combo->setCurrentIndex( combo->count() - 1 );
405     }
406     combo->setToolTip( qfu(p_item->psz_longtext) );
407     if( label )
408         label->setToolTip( qfu(p_item->psz_longtext) );
409 }
410
411 int IntegerListConfigControl::getValue()
412 {
413     return combo->itemData( combo->currentIndex() ).toInt();
414 }
415
416 /*********** Boolean **************/
417 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
418                                       module_config_t *_p_item,
419                                       QWidget *_parent, QGridLayout *l,
420                                       int line ) :
421                     VIntConfigControl( _p_this, _p_item, _parent )
422 {
423     checkbox = new QCheckBox( qfu(p_item->psz_text) );
424     finish();
425
426     if( !l )
427     {
428         QHBoxLayout *layout = new QHBoxLayout();
429         layout->addWidget( checkbox, 0 );
430         widget->setLayout( layout );
431     }
432     else
433     {
434         l->addWidget( checkbox, line, 0 );
435     }
436 }
437 BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
438                                       module_config_t *_p_item,
439                                       QLabel *_label,
440                                       QCheckBox *_checkbox,
441                                       bool bycat ) :
442                    VIntConfigControl( _p_this, _p_item )
443 {
444     checkbox = _checkbox;
445     finish();
446 }
447
448 void BoolConfigControl::finish()
449 {
450     checkbox->setCheckState( p_item->i_value == VLC_TRUE ? Qt::Checked
451                                                         : Qt::Unchecked );
452     checkbox->setToolTip( qfu(p_item->psz_longtext) );
453 }
454
455 int BoolConfigControl::getValue()
456 {
457     return checkbox->checkState() == Qt::Checked ? VLC_TRUE : VLC_FALSE;
458 }
459
460 /**************************************************************************
461  * Float-based controls
462  *************************************************************************/
463
464 /*********** Float **************/
465 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
466                                         module_config_t *_p_item,
467                                         QWidget *_parent, QGridLayout *l,
468                                         int line ) :
469                     VFloatConfigControl( _p_this, _p_item, _parent )
470 {
471     label = new QLabel( qfu(p_item->psz_text) );
472     spin = new QDoubleSpinBox; spin->setMinimumWidth( 80 );
473     spin->setMaximumWidth( 90 );
474     finish();
475
476     if( !l )
477     {
478         QHBoxLayout *layout = new QHBoxLayout();
479         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
480         widget->setLayout( layout );
481     }
482     else
483     {
484         l->addWidget( label, line, 0 );
485         l->addWidget( spin, line, 1, Qt::AlignRight );
486     }
487 }
488
489 FloatConfigControl::FloatConfigControl( vlc_object_t *_p_this,
490                                         module_config_t *_p_item,
491                                         QLabel *_label,
492                                         QDoubleSpinBox *_spin ) :
493                     VFloatConfigControl( _p_this, _p_item )
494 {
495     spin = _spin;
496     label = _label;
497     finish();
498 }
499
500 void FloatConfigControl::finish()
501 {
502     spin->setMaximum( 2000000000. );
503     spin->setMinimum( -2000000000. );
504     spin->setSingleStep( 0.1 );
505     spin->setValue( (double)p_item->f_value );
506     spin->setToolTip( qfu(p_item->psz_longtext) );
507     if( label )
508         label->setToolTip( qfu(p_item->psz_longtext) );
509 }
510
511 float FloatConfigControl::getValue()
512 {
513     return (float)spin->value();
514 }
515
516 /*********** Float with range **************/
517 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
518                                         module_config_t *_p_item,
519                                         QWidget *_parent, QGridLayout *l,
520                                         int line ) :
521                 FloatConfigControl( _p_this, _p_item, _parent, l, line )
522 {
523     finish();
524 }
525
526 FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
527                                         module_config_t *_p_item,
528                                         QLabel *_label,
529                                         QDoubleSpinBox *_spin ) :
530                 FloatConfigControl( _p_this, _p_item, _label, _spin )
531 {
532     finish();
533 }
534
535 void FloatRangeConfigControl::finish()
536 {
537     spin->setMaximum( (double)p_item->f_max );
538     spin->setMinimum( (double)p_item->f_min );
539 }