1 /*****************************************************************************
2 * preferences.cpp : "Normal preferences"
3 ****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
24 #include <QApplication>
26 #include <QTreeWidget>
27 #include <QTreeWidgetItem>
32 #include <QScrollArea>
33 #include <QVBoxLayout>
34 #include <QHBoxLayout>
35 #include <QGridLayout>
36 #include <QHeaderView>
40 #include "components/preferences.hpp"
41 #include "components/preferences_widgets.hpp"
44 #include <vlc_config_cat.h>
45 #include <vlc_intf_strings.h>
48 #include "pixmaps/audio.xpm"
49 #include "pixmaps/video.xpm"
50 #include "pixmaps/type_net.xpm"
51 #include "pixmaps/type_playlist.xpm"
52 #include "pixmaps/advanced.xpm"
53 #include "pixmaps/codec.xpm"
54 #include "pixmaps/intf.xpm"
56 #define ITEM_HEIGHT 25
58 /*********************************************************************
60 *********************************************************************/
61 PrefsTree::PrefsTree( intf_thread_t *_p_intf, QWidget *_parent ) :
62 QTreeWidget( _parent ), p_intf( _p_intf )
65 vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
70 setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
71 setAlternatingRowColors( true );
74 #define BI( a,b) QIcon a##_icon = QIcon( QPixmap( b##_xpm ))
79 BI( advanced, advanced );
80 BI( playlist, type_playlist );
81 BI( interface, intf );
84 /* Build the tree for the main module */
86 for( i_index = 0; i_index < p_list->i_count; i_index++ )
88 p_module = (module_t *)p_list->p_values[i_index].p_object;
89 if( !strcmp( p_module->psz_object_name, "main" ) )
92 if( i_index < p_list->i_count )
94 module_config_t *p_item = p_module->p_config;
95 PrefsItemData *data = NULL;
96 QTreeWidgetItem *current_item = NULL;
101 switch( p_item->i_type )
103 case CONFIG_CATEGORY:
104 if( p_item->i_value == -1 ) break;
105 data = new PrefsItemData();
106 data->name = QString( qfu( config_CategoryNameGet
107 ( p_item->i_value ) ) );
108 psz_help = config_CategoryHelpGet( p_item->i_value );
110 data->help = QString( qfu(psz_help) );
113 data->i_type = TYPE_CATEGORY;
114 data->i_object_id = p_item->i_value;
116 switch( p_item->i_value )
118 #define CI(a,b) case a: icon = b##_icon;break
119 CI( CAT_AUDIO, audio );
120 CI( CAT_VIDEO, video );
121 CI( CAT_INPUT, input );
122 CI( CAT_SOUT, sout );
123 CI( CAT_ADVANCED, advanced );
124 CI( CAT_PLAYLIST, playlist );
125 CI( CAT_INTERFACE, interface );
129 current_item = new QTreeWidgetItem();
130 current_item->setText( 0, data->name );
131 current_item->setIcon( 0 , icon );
132 current_item->setData( 0, Qt::UserRole,
133 qVariantFromValue( data ) );
134 addTopLevelItem( current_item );
136 case CONFIG_SUBCATEGORY:
137 if( p_item->i_value == -1 ) break;
139 ( p_item->i_value == SUBCAT_VIDEO_GENERAL ||
140 p_item->i_value == SUBCAT_ADVANCED_MISC ||
141 p_item->i_value == SUBCAT_INPUT_GENERAL ||
142 p_item->i_value == SUBCAT_INTERFACE_GENERAL ||
143 p_item->i_value == SUBCAT_SOUT_GENERAL||
144 p_item->i_value == SUBCAT_PLAYLIST_GENERAL||
145 p_item->i_value == SUBCAT_AUDIO_GENERAL ) )
147 // Data still contains the correct thing
148 data->i_type = TYPE_CATSUBCAT;
149 data->i_subcat_id = p_item->i_value;
150 data->name = QString( qfu( config_CategoryNameGet(
151 p_item->i_value )) );
152 psz_help = config_CategoryHelpGet( p_item->i_value );
154 data->help = QString( qfu(psz_help) );
157 current_item->setData( 0, Qt::UserRole,
158 QVariant::fromValue( data ) );
161 data = new PrefsItemData();
162 data->name = QString( qfu( config_CategoryNameGet(
164 psz_help = config_CategoryHelpGet( p_item->i_value );
166 data->help = QString( qfu(psz_help) );
169 data->i_type = TYPE_SUBCATEGORY;
170 data->i_object_id = p_item->i_value;
172 assert( current_item );
174 /* TODO : Choose the image */
175 QTreeWidgetItem *subcat_item = new QTreeWidgetItem();
176 subcat_item->setText( 0, data->name );
177 //item->setIcon( 0 , XXX );
178 subcat_item->setData( 0, Qt::UserRole,
179 qVariantFromValue(data) );
180 subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
181 current_item->addChild( subcat_item );
184 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
187 /* Build the tree of plugins */
188 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
190 module_config_t *p_item;
191 int i_subcategory = -1, i_category = -1, i_options = 0;
192 p_module = (module_t *)p_list->p_values[i_index].p_object;
194 // Main module excluded
195 if( !strcmp( p_module->psz_object_name, "main" ) ) continue;
197 /* Exclude empty plugins (submodules don't have config options, they
198 * are stored in the parent module) */
199 if( p_module->b_submodule ) continue;
201 p_item = p_module->p_config;
202 if( !p_item ) continue;
205 if( p_item->i_type == CONFIG_CATEGORY )
206 i_category = p_item->i_value;
207 else if( p_item->i_type == CONFIG_SUBCATEGORY )
208 i_subcategory = p_item->i_value;
209 if( p_item->i_type & CONFIG_ITEM )
212 if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
214 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
216 if( !i_options ) continue; // Nothing to display
218 // Locate the category item;
219 QTreeWidgetItem *subcat_item = NULL;
220 bool b_found = false;
221 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
224 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
225 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
226 value<PrefsItemData *>();
227 if( data->i_object_id == i_category )
229 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
232 subcat_item = cat_item->child( i_sc_index );
233 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
234 value<PrefsItemData *>();
235 if( sc_data && sc_data->i_object_id == i_subcategory )
243 subcat_item = cat_item;
249 if( !b_found ) continue;
251 PrefsItemData *module_data = new PrefsItemData();
252 module_data->b_submodule = p_module->b_submodule;
253 module_data->i_type = TYPE_MODULE;
254 module_data->i_object_id = p_module->b_submodule ?
255 ((module_t *)p_module->p_parent)->i_object_id :
256 p_module->i_object_id;
257 module_data->help.clear();
259 QTreeWidgetItem *module_item = new QTreeWidgetItem();
260 module_item->setText( 0, qfu( p_module->psz_shortname ?
261 p_module->psz_shortname : p_module->psz_object_name) );
262 //item->setIcon( 0 , XXX );
263 module_item->setData( 0, Qt::UserRole,
264 QVariant::fromValue( module_data) );
265 module_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
266 subcat_item->addChild( module_item );
269 /* We got everything, just sort a bit */
270 sortItems( 0, Qt::AscendingOrder );
272 vlc_list_release( p_list );
275 PrefsTree::~PrefsTree() {}
277 void PrefsTree::applyAll()
282 void PrefsTree::cleanAll()
287 /// \todo When cleaning, we should remove the panel ?
288 void PrefsTree::doAll( bool doclean )
290 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
293 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
294 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
297 QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
298 for( int i_module = 0 ; i_module < sc_item->childCount();
301 PrefsItemData *data = sc_item->child( i_module )->
302 data( 0, Qt::UserRole).value<PrefsItemData *>();
303 if( data->panel && doclean )
308 else if( data->panel )
309 data->panel->apply();
311 PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
312 value<PrefsItemData *>();
313 if( data->panel && doclean )
318 else if( data->panel )
319 data->panel->apply();
321 PrefsItemData *data = cat_item->data( 0, Qt::UserRole).
322 value<PrefsItemData *>();
323 if( data->panel && doclean )
328 else if( data->panel )
329 data->panel->apply();
333 /*********************************************************************
335 *********************************************************************/
336 PrefsPanel::PrefsPanel( QWidget *_parent ) : QWidget( _parent )
339 PrefsPanel::PrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
340 PrefsItemData * data ) :
341 QWidget( _parent ), p_intf( _p_intf )
343 module_config_t *p_item;
345 /* Find our module */
346 module_t *p_module = NULL;
347 if( data->i_type == TYPE_CATEGORY )
349 else if( data->i_type == TYPE_MODULE )
350 p_module = (module_t *) vlc_object_get( p_intf, data->i_object_id );
353 /* List the plugins */
355 vlc_bool_t b_found = VLC_FALSE;
356 vlc_list_t *p_list = vlc_list_find( p_intf,
357 VLC_OBJECT_MODULE, FIND_ANYWHERE );
358 if( !p_list ) return;
360 for( i_index = 0; i_index < p_list->i_count; i_index++ )
362 p_module = (module_t *)p_list->p_values[i_index].p_object;
363 if( !strcmp( p_module->psz_object_name, "main" ) )
369 if( !p_module && !b_found )
371 msg_Warn( p_intf, "unable to create preferences (main not found)");
374 if( p_module ) vlc_object_yield( p_module );
375 vlc_list_release( p_list );
378 if( p_module->b_submodule )
379 p_item = ((module_t *)p_module->p_parent)->p_config;
381 p_item = p_module->p_config;
383 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
387 if( p_item->i_type == CONFIG_SUBCATEGORY &&
388 ( data->i_type == TYPE_SUBCATEGORY &&
389 p_item->i_value == data->i_object_id ) ||
390 ( data->i_type == TYPE_CATSUBCAT &&
391 p_item->i_value == data->i_subcat_id ) )
393 if( p_item->i_type == CONFIG_HINT_END ) break;
397 global_layout = new QVBoxLayout();
399 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
401 head = QString( data->name );
402 p_item++; // Why that ?
406 head = QString( qfu(p_module->psz_longname) );
407 if( p_module->psz_help )
410 head.append( qfu( p_module->psz_help ) );
414 QLabel *label = new QLabel( head );
415 global_layout->addWidget( label );
416 QFont myFont = QApplication::font(0);
417 myFont.setPointSize( myFont.pointSize() + 3 ); myFont.setBold( true );
419 label->setFont( myFont );
420 QLabel *help = new QLabel( data->help, this );
421 help->setWordWrap( true );
423 global_layout->addWidget( help );
425 QGroupBox *box = NULL;
426 QGridLayout *boxlayout = NULL;
428 QScrollArea *scroller= new QScrollArea;
429 scroller->setFrameStyle( QFrame::NoFrame );
430 QWidget *scrolled_area = new QWidget;
432 QGridLayout *layout = new QGridLayout();
433 int i_line = 0, i_boxline = 0;
437 if( ( ( data->i_type == TYPE_SUBCATEGORY &&
438 p_item->i_value != data->i_object_id ) ||
439 ( data->i_type == TYPE_CATSUBCAT &&
440 p_item->i_value != data->i_subcat_id ) ) &&
441 ( p_item->i_type == CONFIG_CATEGORY ||
442 p_item->i_type == CONFIG_SUBCATEGORY ) )
444 if( p_item->b_internal == VLC_TRUE ) continue;
446 if( p_item->i_type == CONFIG_SECTION )
450 box->setLayout( boxlayout );
451 layout->addWidget( box, i_line, 0, 1, 2 );
454 box = new QGroupBox( qfu(p_item->psz_text) );
455 boxlayout = new QGridLayout();
458 ConfigControl *control;
460 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
461 p_item, NULL, layout, i_line );
463 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
464 p_item, NULL, boxlayout, i_boxline );
468 if( box ) i_boxline++;
470 controls.append( control );
472 while( !(p_item->i_type == CONFIG_HINT_END ||
473 ( ( data->i_type == TYPE_SUBCATEGORY ||
474 data->i_type == TYPE_CATSUBCAT ) &&
475 ( p_item->i_type == CONFIG_CATEGORY ||
476 p_item->i_type == CONFIG_SUBCATEGORY ) ) ) && p_item++ );
480 box->setLayout( boxlayout );
481 layout->addWidget( box, i_line, 0, 1, 2 );
484 vlc_object_release( p_module );
486 scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
487 scrolled_area->setLayout( layout );
488 scroller->setWidget( scrolled_area );
489 scroller->setWidgetResizable( true );
490 global_layout->addWidget( scroller );
491 setLayout( global_layout );
494 void PrefsPanel::apply()
496 QList<ConfigControl *>::Iterator i;
497 for( i = controls.begin() ; i != controls.end() ; i++ )
499 ConfigControl *c = qobject_cast<ConfigControl *>(*i);
500 c->doApply( p_intf );
503 void PrefsPanel::clean()