]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences_widgets.cpp
Improved layout for preferences
[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  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /**
25  * Todo:
26  *  - Finish implementation (see WX)
27  *  - Improvements over WX
28  *      - Password field implementation (through "pwd" bool param
29  *      - Validator for modulelist
30  *  - Implement update stuff using a general Updated signal
31  */
32
33 #include "components/preferences_widgets.hpp"
34 #include "qt4.hpp"
35 #include <QLineEdit>
36 #include <QString>
37 #include <QSpinBox>
38 #include <QDoubleSpinBox>
39 #include <QVariant>
40 #include <QComboBox>
41 #include <QGridLayout>
42
43 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
44                                              module_config_t *p_item,
45                                              QWidget *parent )
46 {
47     return createControl( p_this, p_item, parent, NULL, 0 );
48 }
49
50 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
51                                              module_config_t *p_item,
52                                              QWidget *parent,
53                                              QGridLayout *l, int line )
54 {
55     ConfigControl *p_control = NULL;
56     if( p_item->psz_current ) return NULL;
57
58     switch( p_item->i_type )
59     {
60     case CONFIG_ITEM_MODULE:
61         p_control = new ModuleConfigControl( p_this, p_item, parent, false,
62                                              l, line );
63         break;
64     case CONFIG_ITEM_MODULE_CAT:
65         p_control = new ModuleConfigControl( p_this, p_item, parent, true,
66                                              l, line );
67         break;
68     case CONFIG_ITEM_STRING:
69         if( !p_item->i_list )
70             p_control = new StringConfigControl( p_this, p_item, parent,
71                                                  l, line, false );
72         else
73             fprintf(stderr, "TODO\n" );
74         break;
75     case CONFIG_ITEM_INTEGER:
76         if( p_item->i_list )
77             fprintf( stderr, "Todo\n" );
78         else if( p_item->i_min || p_item->i_max )
79             fprintf( stderr, "Todo\n" );
80         else
81             p_control = new IntegerConfigControl( p_this, p_item, parent,
82                                                   l, line );
83         break;
84     default:
85         break;
86     }
87     return p_control;
88 }
89
90 /**************************************************************************
91  * String-based controls
92  *************************************************************************/
93
94 /*********** String **************/
95 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
96                                           module_config_t *_p_item,
97                                           QWidget *_parent, QGridLayout *l,
98                                           int line, bool pwd ) :
99                            VStringConfigControl( _p_this, _p_item, _parent )
100 {
101     label = new QLabel( qfu(p_item->psz_text) );
102     text = new QLineEdit( qfu(p_item->psz_value) );
103     finish();
104
105     if( !l )
106     {
107         QHBoxLayout *layout = new QHBoxLayout();
108         layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
109         widget->setLayout( layout );
110     }
111     else
112     {
113         l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
114     }
115 }
116
117 StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
118                                    module_config_t *_p_item,
119                                    QLabel *_label, QLineEdit *_text, bool pwd ):
120                            VStringConfigControl( _p_this, _p_item )
121 {
122     text = _text;
123     label = _label;
124     finish( );
125 }
126
127 void StringConfigControl::finish()
128 {
129     text->setText( qfu(p_item->psz_value) );
130     text->setToolTip( qfu(p_item->psz_longtext) );
131     label->setToolTip( qfu(p_item->psz_longtext) );
132 }
133
134 /********* Module **********/
135 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
136                module_config_t *_p_item, QWidget *_parent, bool bycat,
137                QGridLayout *l, int line) :
138                VStringConfigControl( _p_this, _p_item, _parent )
139 {
140     label = new QLabel( qfu(p_item->psz_text) );
141     combo = new QComboBox();
142     finish( bycat );
143     if( !l )
144     {
145         QHBoxLayout *layout = new QHBoxLayout();
146         layout->addWidget( label ); layout->addWidget( combo );
147         widget->setLayout( layout );
148     }
149     else
150     {
151         l->addWidget( label, line, 0 );
152         l->addWidget( combo, line, 1, Qt::AlignRight );
153     }
154 }
155 ModuleConfigControl::ModuleConfigControl( vlc_object_t *_p_this,
156                 module_config_t *_p_item, QLabel *_label, QComboBox *_combo,
157                 bool bycat ) : VStringConfigControl( _p_this, _p_item )
158 {
159     combo = _combo;
160     label = _label;
161     finish( bycat );
162 }
163
164 void ModuleConfigControl::finish( bool bycat )
165 {
166     vlc_list_t *p_list;
167     module_t *p_parser;
168
169     combo->setEditable( false );
170
171     /* build a list of available modules */
172     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
173     combo->addItem( qtr("Default") );
174     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
175     {
176         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
177
178         if( bycat )
179         {
180             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
181
182             module_config_t *p_config = p_parser->p_config;
183             if( p_config ) do
184             {
185                 /* Hack: required subcategory is stored in i_min */
186                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
187                     p_config->i_value == p_item->i_min )
188                     combo->addItem( qfu(p_parser->psz_longname),
189                                     QVariant( p_parser->psz_object_name ) );
190                 if( p_item->psz_value && !strcmp( p_item->psz_value,
191                                                   p_parser->psz_object_name) )
192                     combo->setCurrentIndex( combo->count() - 1 );
193             } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
194         }
195         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
196         {
197             combo->addItem( qfu(p_parser->psz_longname),
198                             QVariant( p_parser->psz_object_name ) );
199             if( p_item->psz_value && !strcmp( p_item->psz_value,
200                                               p_parser->psz_object_name) )
201                 combo->setCurrentIndex( combo->count() - 1 );
202         }
203     }
204     vlc_list_release( p_list );
205     combo->setToolTip( qfu(p_item->psz_longtext) );
206     label->setToolTip( qfu(p_item->psz_longtext) );
207 }
208
209 QString ModuleConfigControl::getValue()
210 {
211     return combo->itemData( combo->currentIndex() ).toString();
212 }
213
214 /**************************************************************************
215  * Integer-based controls
216  *************************************************************************/
217
218 /*********** Integer **************/
219 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
220                                             module_config_t *_p_item,
221                                             QWidget *_parent, QGridLayout *l,
222                                             int line ) :
223                            VIntConfigControl( _p_this, _p_item, _parent )
224 {
225     label = new QLabel( qfu(p_item->psz_text) );
226     spin = new QSpinBox; spin->setMinimumWidth( 80 );
227     spin->setMaximumWidth( 90 );
228     finish();
229
230     if( !l )
231     {
232         QHBoxLayout *layout = new QHBoxLayout();
233         layout->addWidget( label, 0 ); layout->addWidget( spin, 1 );
234         widget->setLayout( layout );
235     }
236     else
237     {
238         l->addWidget( label, line, 0 );
239         l->addWidget( spin, line, 1, Qt::AlignRight );
240     }
241 }
242 IntegerConfigControl::IntegerConfigControl( vlc_object_t *_p_this,
243                                             module_config_t *_p_item,
244                                             QLabel *_label, QSpinBox *_spin ) :
245                                       VIntConfigControl( _p_this, _p_item )
246 {
247     spin = _spin;
248     label = _label;
249     finish();
250 }
251
252 void IntegerConfigControl::finish()
253 {
254     spin->setMaximum( 2000000000 );
255     spin->setValue( p_item->i_value );
256     spin->setToolTip( qfu(p_item->psz_longtext) );
257     label->setToolTip( qfu(p_item->psz_longtext) );
258 }
259
260 int IntegerConfigControl::getValue()
261 {
262     return spin->value();
263 }