]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Some more prefs fun:
[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             fprintf( stderr, "Todo\n" );
83         else
84             p_control = new IntegerConfigControl( p_this, p_item, parent,
85                                                   l, line );
86         break;
87     default:
88         break;
89     }
90     return p_control;
91 }
92
93 /**************************************************************************
94  * String-based controls
95  *************************************************************************/
96
97 /*********** String **************/
98 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
99                                           module_config_t *_p_item,
100                                           QWidget *_parent, QGridLayout *l,
101                                           int line, bool pwd ) :
102                            VStringConfigControl( _p_this, _p_item, _parent )
103 {
104     label = new QLabel( qfu(p_item->psz_text) );
105     text = new QLineEdit( qfu(p_item->psz_value) );
106     finish();
107
108     if( !l )
109     {
110         QHBoxLayout *layout = new QHBoxLayout();
111         layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
112         widget->setLayout( layout );
113     }
114     else
115     {
116         l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
117     }
118 }
119
120 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
121                                    module_config_t *_p_item,
122                                    QLabel *_label, QLineEdit *_text, bool pwd ):
123                            VStringConfigControl( _p_this, _p_item )
124 {
125     text = _text;
126     label = _label;
127     finish( );
128 }
129
130 void StringConfigControl::finish()
131 {
132     text->setText( qfu(p_item->psz_value) );
133     text->setToolTip( qfu(p_item->psz_longtext) );
134     if( label )
135         label->setToolTip( qfu(p_item->psz_longtext) );
136 }
137
138 /********* String / choice list **********/
139 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
140                module_config_t *_p_item, QWidget *_parent, bool bycat,
141                QGridLayout *l, int line) :
142                VStringConfigControl( _p_this, _p_item, _parent )
143 {
144     label = new QLabel( qfu(p_item->psz_text) );
145     combo = new QComboBox();
146     finish( bycat );
147     if( !l )
148     {
149         QHBoxLayout *layout = new QHBoxLayout();
150         layout->addWidget( label ); layout->addWidget( combo );
151         widget->setLayout( layout );
152     }
153     else
154     {
155         l->addWidget( label, line, 0 );
156         l->addWidget( combo, line, 1, Qt::AlignRight );
157     }
158 }
159 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
160                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
161                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
162 {
163     combo = _combo;
164     label = _label;
165     finish( bycat );
166 }
167
168 void StringListConfigControl::finish( bool bycat )
169 {
170     combo->setEditable( false );
171
172     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
173     {
174         combo->addItem( qfu(p_item->ppsz_list_text ?
175                             p_item->ppsz_list_text[i_index] :
176                             p_item->ppsz_list[i_index] ),
177                         QVariant( p_item->ppsz_list[i_index] ) );
178         if( p_item->psz_value && !strcmp( p_item->psz_value,
179                                           p_item->ppsz_list[i_index] ) )
180             combo->setCurrentIndex( combo->count() - 1 );
181     }
182     combo->setToolTip( qfu(p_item->psz_longtext) );
183     if( label )
184         label->setToolTip( qfu(p_item->psz_longtext) );
185 }
186
187 QString StringListConfigControl::getValue()
188 {
189     return combo->itemData( combo->currentIndex() ).toString();
190 }
191
192 /********* Module **********/
193 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
194                module_config_t *_p_item, QWidget *_parent, bool bycat,
195                QGridLayout *l, int line) :
196                VStringConfigControl( _p_this, _p_item, _parent )
197 {
198     label = new QLabel( qfu(p_item->psz_text) );
199     combo = new QComboBox();
200     finish( bycat );
201     if( !l )
202     {
203         QHBoxLayout *layout = new QHBoxLayout();
204         layout->addWidget( label ); layout->addWidget( combo );
205         widget->setLayout( layout );
206     }
207     else
208     {
209         l->addWidget( label, line, 0 );
210         l->addWidget( combo, line, 1, Qt::AlignRight );
211     }
212 }
213 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
214                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
215                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
216 {
217     combo = _combo;
218     label = _label;
219     finish( bycat );
220 }
221
222 void ModuleConfigControl::finish( bool bycat )
223 {
224     vlc_list_t *p_list;
225     module_t *p_parser;
226
227     combo->setEditable( false );
228
229     /* build a list of available modules */
230     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
231     combo->addItem( qtr("Default") );
232     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
233     {
234         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
235
236         if( bycat )
237         {
238             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
239
240             module_config_t *p_config = p_parser->p_config;
241             if( p_config ) do
242             {
243                 /* Hack: required subcategory is stored in i_min */
244                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
245                     p_config->i_value == p_item->i_min )
246                     combo->addItem( qfu(p_parser->psz_longname),
247                                     QVariant( p_parser->psz_object_name ) );
248                 if( p_item->psz_value && !strcmp( p_item->psz_value,
249                                                   p_parser->psz_object_name) )
250                     combo->setCurrentIndex( combo->count() - 1 );
251             } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
252         }
253         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
254         {
255             combo->addItem( qfu(p_parser->psz_longname),
256                             QVariant( p_parser->psz_object_name ) );
257             if( p_item->psz_value && !strcmp( p_item->psz_value,
258                                               p_parser->psz_object_name) )
259                 combo->setCurrentIndex( combo->count() - 1 );
260         }
261     }
262     vlc_list_release( p_list );
263     combo->setToolTip( qfu(p_item->psz_longtext) );
264     if( label )
265         label->setToolTip( qfu(p_item->psz_longtext) );
266 }
267
268 QString ModuleConfigControl::getValue()
269 {
270     return combo->itemData( combo->currentIndex() ).toString();
271 }
272
273 /**************************************************************************
274  * Integer-based controls
275  *************************************************************************/
276
277 /*********** Integer **************/
278 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
279                                             module_config_t *_p_item,
280                                             QWidget *_parent, QGridLayout *l,
281                                             int line ) :
282                            VIntConfigControl( _p_this, _p_item, _parent )
283 {
284     label = new QLabel( qfu(p_item->psz_text) );
285     spin = new QSpinBox; spin->setMinimumWidth( 80 );
286     spin->setMaximumWidth( 90 );
287     finish();
288
289     if( !l )
290     {
291         QHBoxLayout *layout = new QHBoxLayout();
292         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
293         widget->setLayout( layout );
294     }
295     else
296     {
297         l->addWidget( label, line, 0 );
298         l->addWidget( spin, line, 1, Qt::AlignRight );
299     }
300 }
301 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
302                                             module_config_t *_p_item,
303                                             QLabel *_label, QSpinBox *_spin ) :
304                                       VIntConfigControl( _p_this, _p_item )
305 {
306     spin = _spin;
307     label = _label;
308     finish();
309 }
310
311 void IntegerConfigControl::finish()
312 {
313     spin->setMaximum( 2000000000 );
314     spin->setValue( p_item->i_value );
315     spin->setToolTip( qfu(p_item->psz_longtext) );
316     if( label )
317         label->setToolTip( qfu(p_item->psz_longtext) );
318 }
319
320 int IntegerConfigControl::getValue()
321 {
322     return spin->value();
323 }
324
325 /********* Integer / choice list **********/
326 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
327                module_config_t *_p_item, QWidget *_parent, bool bycat,
328                QGridLayout *l, int line) :
329                VIntConfigControl( _p_this, _p_item, _parent )
330 {
331     label = new QLabel( qfu(p_item->psz_text) );
332     combo = new QComboBox();
333     finish( bycat );
334     if( !l )
335     {
336         QHBoxLayout *layout = new QHBoxLayout();
337         layout->addWidget( label ); layout->addWidget( combo );
338         widget->setLayout( layout );
339     }
340     else
341     {
342         l->addWidget( label, line, 0 );
343         l->addWidget( combo, line, 1, Qt::AlignRight );
344     }
345 }
346 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
347                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
348                 bool bycat ) : VIntConfigControl( _p_this, _p_item )
349 {
350     combo = _combo;
351     label = _label;
352     finish( bycat );
353 }
354
355 void IntegerListConfigControl::finish( bool bycat )
356 {
357     combo->setEditable( false );
358
359     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
360     {
361         combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
362                         QVariant( p_item->pi_list[i_index] ) );
363         if( p_item->i_value == p_item->pi_list[i_index] )
364             combo->setCurrentIndex( combo->count() - 1 );
365     }
366     combo->setToolTip( qfu(p_item->psz_longtext) );
367     if( label )
368         label->setToolTip( qfu(p_item->psz_longtext) );
369 }
370
371 int IntegerListConfigControl::getValue()
372 {
373     return combo->itemData( combo->currentIndex() ).toInt();
374 }