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 "components/preferences.hpp"
25 #include "components/preferences_widgets.hpp"
27 #include <vlc_config_cat.h>
28 #include <vlc_intf_strings.h>
31 #include "pixmaps/audio.xpm"
32 #include "pixmaps/video.xpm"
33 #include "pixmaps/type_net.xpm"
34 #include "pixmaps/type_playlist.xpm"
35 #include "pixmaps/advanced.xpm"
36 #include "pixmaps/codec.xpm"
37 #include "pixmaps/intf.xpm"
40 #include <QTreeWidget>
41 #include <QTreeWidgetItem>
46 #include <QScrollArea>
47 #include <QVBoxLayout>
48 #include <QHBoxLayout>
49 #include <QGridLayout>
50 #include <QHeaderView>
52 #define ITEM_HEIGHT 25
54 /*********************************************************************
56 *********************************************************************/
57 PrefsTree::PrefsTree( intf_thread_t *_p_intf, QWidget *_parent ) :
58 QTreeWidget( _parent ), p_intf( _p_intf )
61 vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
66 setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
67 setAlternatingRowColors( true );
73 int pSize = f.pointSize();
75 f.setPointSize( pSize + 1 );
77 f.setPixelSize( f.pixelSize() + 1 );
81 #define BI( a,b) QIcon a##_icon = QIcon( QPixmap( b##_xpm ))
86 BI( advanced, advanced );
87 BI( playlist, type_playlist );
88 BI( interface, intf );
91 /* Build the tree for the main module */
93 for( i_index = 0; i_index < p_list->i_count; i_index++ )
95 p_module = (module_t *)p_list->p_values[i_index].p_object;
96 if( !strcmp( p_module->psz_object_name, "main" ) )
99 if( i_index < p_list->i_count )
101 module_config_t *p_item = p_module->p_config;
102 PrefsItemData *data = NULL;
103 QTreeWidgetItem *current_item = NULL;
108 switch( p_item->i_type )
110 case CONFIG_CATEGORY:
111 if( p_item->i_value == -1 ) break;
112 data = new PrefsItemData();
113 data->name = QString( config_CategoryNameGet
114 ( p_item->i_value ) );
115 psz_help = config_CategoryHelpGet( p_item->i_value );
117 data->help = QString( psz_help );
120 data->i_type = TYPE_CATEGORY;
121 data->i_object_id = p_item->i_value;
123 switch( p_item->i_value )
125 #define CI(a,b) case a: icon = b##_icon;break
126 CI( CAT_AUDIO, audio );
127 CI( CAT_VIDEO, video );
128 CI( CAT_INPUT, input );
129 CI( CAT_SOUT, sout );
130 CI( CAT_ADVANCED, advanced );
131 CI( CAT_PLAYLIST, playlist );
132 CI( CAT_INTERFACE, interface );
136 current_item = new QTreeWidgetItem();
137 current_item->setText( 0, data->name );
138 current_item->setIcon( 0 , icon );
139 current_item->setData( 0, Qt::UserRole,
140 qVariantFromValue( data ) );
141 addTopLevelItem( current_item );
143 case CONFIG_SUBCATEGORY:
144 if( p_item->i_value == -1 ) break;
146 ( p_item->i_value == SUBCAT_VIDEO_GENERAL ||
147 p_item->i_value == SUBCAT_ADVANCED_MISC ||
148 p_item->i_value == SUBCAT_INPUT_GENERAL ||
149 p_item->i_value == SUBCAT_INTERFACE_GENERAL ||
150 p_item->i_value == SUBCAT_SOUT_GENERAL||
151 p_item->i_value == SUBCAT_PLAYLIST_GENERAL||
152 p_item->i_value == SUBCAT_AUDIO_GENERAL ) )
154 // Data still contains the correct thing
155 data->i_type = TYPE_CATSUBCAT;
156 data->i_subcat_id = p_item->i_value;
157 data->name = QString( config_CategoryNameGet(
159 psz_help = config_CategoryHelpGet( p_item->i_value );
161 data->help = QString( psz_help );
164 current_item->setData( 0, Qt::UserRole,
165 QVariant::fromValue( data ) );
168 data = new PrefsItemData();
169 data->name = QString( config_CategoryNameGet( p_item->i_value));
170 psz_help = config_CategoryHelpGet( p_item->i_value );
172 data->help = QString( psz_help );
175 data->i_type = TYPE_SUBCATEGORY;
176 data->i_object_id = p_item->i_value;
178 assert( current_item );
180 /* TODO : Choose the image */
181 QTreeWidgetItem *subcat_item = new QTreeWidgetItem();
182 subcat_item->setText( 0, data->name );
183 //item->setIcon( 0 , XXX );
184 subcat_item->setData( 0, Qt::UserRole,
185 qVariantFromValue(data) );
186 subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
187 current_item->addChild( subcat_item );
190 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
193 /* Build the tree of plugins */
194 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
196 module_config_t *p_item;
197 int i_subcategory = -1, i_category = -1, i_options = 0;
198 p_module = (module_t *)p_list->p_values[i_index].p_object;
200 // Main module excluded
201 if( !strcmp( p_module->psz_object_name, "main" ) ) continue;
203 /* Exclude empty plugins (submodules don't have config options, they
204 * are stored in the parent module) */
205 if( p_module->b_submodule ) continue;
207 p_item = p_module->p_config;
208 if( !p_item ) continue;
211 if( p_item->i_type == CONFIG_CATEGORY )
212 i_category = p_item->i_value;
213 else if( p_item->i_type == CONFIG_SUBCATEGORY )
214 i_subcategory = p_item->i_value;
215 if( p_item->i_type & CONFIG_ITEM )
218 if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
220 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
222 if( !i_options ) continue; // Nothing to display
224 // Locate the category item;
225 QTreeWidgetItem *subcat_item = NULL;
226 bool b_found = false;
227 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
230 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
231 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
232 value<PrefsItemData *>();
233 if( data->i_object_id == i_category )
235 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
238 subcat_item = cat_item->child( i_sc_index );
239 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
240 value<PrefsItemData *>();
241 if( sc_data && sc_data->i_object_id == i_subcategory )
249 subcat_item = cat_item;
255 if( !b_found ) continue;
257 PrefsItemData *module_data = new PrefsItemData();
258 module_data->b_submodule = p_module->b_submodule;
259 module_data->i_type = TYPE_MODULE;
260 module_data->i_object_id = p_module->b_submodule ?
261 ((module_t *)p_module->p_parent)->i_object_id :
262 p_module->i_object_id;
263 module_data->help.clear();
265 QTreeWidgetItem *module_item = new QTreeWidgetItem();
266 module_item->setText( 0, p_module->psz_shortname ?
267 p_module->psz_shortname : p_module->psz_object_name );
268 //item->setIcon( 0 , XXX );
269 module_item->setData( 0, Qt::UserRole,
270 QVariant::fromValue( module_data) );
271 module_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
272 subcat_item->addChild( module_item );
275 /* We got everything, just sort a bit */
276 sortItems( 0, Qt::AscendingOrder );
278 vlc_list_release( p_list );
281 PrefsTree::~PrefsTree() {}
283 void PrefsTree::ApplyAll()
288 void PrefsTree::CleanAll()
293 /// \todo When cleaning, we should remove the panel ?
294 void PrefsTree::DoAll( bool doclean )
296 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
299 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
300 for( int i_sc_index = 0; i_sc_index <= cat_item->childCount();
303 QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
304 for( int i_module = 0 ; i_module <= sc_item->childCount();
307 PrefsItemData *data = sc_item->child( i_sc_index )->
308 data( 0, Qt::UserRole ).
309 value<PrefsItemData *>();
310 if( data->panel && doclean )
311 data->panel->Clean();
312 else if( data->panel )
313 data->panel->Apply();
320 /*********************************************************************
322 *********************************************************************/
323 PrefsPanel::PrefsPanel( QWidget *_parent ) : QWidget( _parent )
326 PrefsPanel::PrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
327 PrefsItemData * data, bool currently_advanced ) :
328 QWidget( _parent ), p_intf( _p_intf )
330 module_config_t *p_item;
331 module_t *p_module = NULL;
332 vlc_list_t *p_list = NULL;
333 global_layout = new QVBoxLayout();
335 if( data->i_type == TYPE_CATEGORY )
340 else if( data->i_type == TYPE_MODULE )
342 p_module = (module_t *) vlc_object_get( p_intf, data->i_object_id );
346 /* List the plugins */
348 vlc_bool_t b_found = VLC_FALSE;
349 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
350 if( !p_list ) return;
352 for( i_index = 0; i_index < p_list->i_count; i_index++ )
354 p_module = (module_t *)p_list->p_values[i_index].p_object;
355 if( !strcmp( p_module->psz_object_name, "main" ) )
361 if( !p_module && !b_found )
363 msg_Warn( p_intf, "unable to create preferences (main not found)");
366 if( p_module ) vlc_object_yield( p_module );
367 vlc_list_release( p_list );
370 if( p_module->b_submodule )
371 p_item = ((module_t *)p_module->p_parent)->p_config;
373 p_item = p_module->p_config;
375 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
379 if( p_item->i_type == CONFIG_SUBCATEGORY &&
380 ( data->i_type == TYPE_SUBCATEGORY &&
381 p_item->i_value == data->i_object_id ) ||
382 ( data->i_type == TYPE_CATSUBCAT &&
383 p_item->i_value == data->i_subcat_id ) )
387 if( p_item->i_type == CONFIG_HINT_END ) break;
392 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
394 head = QString( data->name );
395 p_item++; // Why that ?
399 head = QString( qfu(p_module->psz_longname) );
400 if( p_module->psz_help )
403 head.append( qfu( p_module->psz_help ) );
407 QLabel *label = new QLabel( head, this );
408 QFont font = label->font();
409 font.setPointSize( font.pointSize() + 2 ); font.setBold( true );
410 label->setFont( font );
411 QLabel *help = new QLabel( data->help, this );
412 help->setWordWrap( true );
414 global_layout->addWidget( label );
415 global_layout->addWidget( help );
417 QGroupBox *box = NULL;
418 QGridLayout *boxlayout = NULL;
420 QScrollArea *scroller= new QScrollArea;
421 scroller->setFrameStyle( QFrame::NoFrame );
422 QWidget *scrolled_area = new QWidget;
424 QGridLayout *layout = new QGridLayout();
425 int i_line = 0, i_boxline = 0;
429 if( ( ( data->i_type == TYPE_SUBCATEGORY &&
430 p_item->i_value != data->i_object_id ) ||
431 ( data->i_type == TYPE_CATSUBCAT &&
432 p_item->i_value != data->i_subcat_id ) ) &&
433 ( p_item->i_type == CONFIG_CATEGORY ||
434 p_item->i_type == CONFIG_SUBCATEGORY ) )
436 if( p_item->b_internal == VLC_TRUE ) continue;
438 if( p_item->i_type == CONFIG_SECTION )
442 box->setLayout( boxlayout );
443 layout->addWidget( box, i_line, 0, 1, 2 );
446 box = new QGroupBox( qfu(p_item->psz_text) );
447 boxlayout = new QGridLayout();
449 ConfigControl *control;
452 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
453 p_item, NULL, layout, i_line );
457 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
458 p_item, NULL, boxlayout, i_boxline );
464 if( box ) i_boxline++;
466 controls.append( control );
468 while( !(p_item->i_type == CONFIG_HINT_END ||
469 ( ( data->i_type == TYPE_SUBCATEGORY ||
470 data->i_type == TYPE_CATSUBCAT ) &&
471 ( p_item->i_type == CONFIG_CATEGORY ||
472 p_item->i_type == CONFIG_SUBCATEGORY ) ) ) && p_item++ );
476 box->setLayout( boxlayout );
477 layout->addWidget( box, i_line, 0, 1, 2 );
480 vlc_object_release( p_module );
482 scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
483 scrolled_area->setLayout( layout );
484 scroller->setWidget( scrolled_area );
485 scroller->setWidgetResizable( true );
486 global_layout->addWidget( scroller );
488 some_hidden_text = new QLabel( qfu( I_HIDDEN_ADV ) );
489 some_hidden_text->setWordWrap( true );
491 setLayout( global_layout );
492 setAdvanced( currently_advanced, true );
495 void PrefsPanel::Apply()
498 QList<ConfigControl *>::Iterator i;
499 for( i = controls.begin() ; i != controls.end() ; i++ )
501 VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(*i);
504 VFloatConfigControl *vfcc = qobject_cast<VFloatConfigControl *>(*i);
507 VStringConfigControl *vscc =
508 qobject_cast<VStringConfigControl *>(*i);
510 config_PutPsz( p_intf, vscc->getName().toAscii().data(),
511 vscc->getValue().toAscii().data() );
514 config_PutFloat( p_intf, vfcc->getName().toAscii().data(),
518 config_PutInt( p_intf, vicc->getName().toAscii().data(),
523 void PrefsPanel::Clean()
526 void PrefsPanel::setAdvanced( bool adv, bool force )
528 bool some_hidden = false;
529 if( !force && adv == advanced ) return;
532 QList<ConfigControl *>::Iterator i;
533 for( i = controls.begin() ; i != controls.end() ; i++ )
535 if( (*i)->isAdvanced() )
546 if( some_hidden_text )
548 global_layout->removeWidget( some_hidden_text );
549 some_hidden_text->hide();
553 global_layout->addWidget( some_hidden_text );
554 some_hidden_text->show();