1 /*****************************************************************************
2 * preferences.cpp : "Normal preferences"
3 ****************************************************************************
4 * Copyright (C) 2006-2007 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 *****************************************************************************/
27 #include <QApplication>
29 #include <QTreeWidget>
30 #include <QTreeWidgetItem>
35 #include <QScrollArea>
36 #include <QVBoxLayout>
37 #include <QHBoxLayout>
38 #include <QGridLayout>
39 #include <QHeaderView>
43 #include "components/complete_preferences.hpp"
44 #include "components/preferences_widgets.hpp"
46 #include <vlc_config_cat.h>
47 #include <vlc_intf_strings.h>
50 #define ITEM_HEIGHT 25
52 /*********************************************************************
54 *********************************************************************/
55 PrefsTree::PrefsTree( intf_thread_t *_p_intf, QWidget *_parent ) :
56 QTreeWidget( _parent ), p_intf( _p_intf )
58 /* General Qt options */
60 setAlternatingRowColors( true );
63 setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
64 setTextElideMode( Qt::ElideNone );
65 setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
68 #define BI( a,b) QIcon a##_icon = QIcon( QPixmap( b ))
69 BI( audio, ":/pixmaps/advprefs_audio.png" );
70 BI( video, ":/pixmaps/advprefs_video.png" );
71 BI( input, ":/pixmaps/advprefs_codec.png" );
72 BI( sout, ":/pixmaps/advprefs_sout.png" );
73 BI( advanced, ":/pixmaps/advprefs_extended.png" );
74 BI( playlist, ":/pixmaps/advprefs_playlist.png" );
75 BI( interface, ":/pixmaps/advprefs_intf.png" );
78 /* Build the tree for the main module */
79 const module_t *p_module = NULL;
80 vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
84 /* Find the main module */
85 for( unsigned i = 0; p_module == NULL; i++ )
87 assert (i < (unsigned)p_list->i_count);
89 const module_t *p_main = (module_t *)p_list->p_values[i].p_object;
90 if( strcmp( module_GetObjName( p_main ), "main" ) == 0 )
94 /* Initialisation and get the confsize */
95 PrefsItemData *data = NULL;
96 PrefsItemData *data_sub = NULL;
97 QTreeWidgetItem *current_item = NULL;
99 module_config_t *const p_config = module_GetConfig (p_module, &confsize);
101 /* Go through the list of conf */
102 for( size_t i = 0; i < confsize; i++ )
104 const char *psz_help;
107 /* Work on a new item */
108 module_config_t *p_item = p_config + i;
110 switch( p_item->i_type )
112 /* This is a category */
113 case CONFIG_CATEGORY:
114 if( p_item->value.i == -1 ) break;
116 /* PrefsItemData Init */
117 data = new PrefsItemData();
118 data->name = qtr( config_CategoryNameGet( p_item->value.i ) );
119 psz_help = config_CategoryHelpGet( p_item->value.i );
121 data->help = qtr( psz_help );
124 data->i_type = TYPE_CATEGORY;
125 data->i_object_id = p_item->value.i;
127 /* This is a category, put a nice icon */
128 switch( p_item->value.i )
130 #define CI(a,b) case a: icon = b##_icon;break
131 CI( CAT_AUDIO, audio );
132 CI( CAT_VIDEO, video );
133 CI( CAT_INPUT, input );
134 CI( CAT_SOUT, sout );
135 CI( CAT_ADVANCED, advanced );
136 CI( CAT_PLAYLIST, playlist );
137 CI( CAT_INTERFACE, interface );
141 /* Create a new QTreeItem to display it in the tree at top level */
142 current_item = new QTreeWidgetItem();
143 current_item->setText( 0, data->name );
144 current_item->setIcon( 0 , icon );
145 current_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
146 current_item->setData( 0, Qt::UserRole,
147 qVariantFromValue( data ) );
148 addTopLevelItem( current_item );
151 /* This is a subcategory */
152 case CONFIG_SUBCATEGORY:
153 if( p_item->value.i == -1 ) break;
155 /* Special cases: move the main subcategories to the parent cat*/
157 ( p_item->value.i == SUBCAT_VIDEO_GENERAL ||
158 p_item->value.i == SUBCAT_ADVANCED_MISC ||
159 p_item->value.i == SUBCAT_INPUT_GENERAL ||
160 p_item->value.i == SUBCAT_INTERFACE_GENERAL ||
161 p_item->value.i == SUBCAT_SOUT_GENERAL||
162 p_item->value.i == SUBCAT_PLAYLIST_GENERAL||
163 p_item->value.i == SUBCAT_AUDIO_GENERAL ) )
165 /* Data still contains the correct thing */
166 data->i_type = TYPE_CATSUBCAT;
167 data->i_subcat_id = p_item->value.i;
168 data->name = qtr( config_CategoryNameGet( p_item->value.i ) );
169 psz_help = config_CategoryHelpGet( p_item->value.i );
171 data->help = qtr( psz_help );
174 current_item->setData( 0, Qt::UserRole,
175 QVariant::fromValue( data ) );
179 /* Normal Subcategories */
181 /* Process the Data */
182 data_sub = new PrefsItemData();
183 data_sub->name = qtr( config_CategoryNameGet( p_item->value.i) );
184 psz_help = config_CategoryHelpGet( p_item->value.i );
186 data_sub->help = qtr( psz_help );
188 data_sub->help.clear();
189 data_sub->i_type = TYPE_SUBCATEGORY;
190 data_sub->i_object_id = p_item->value.i;
192 /* Create a new TreeWidget */
193 QTreeWidgetItem *subcat_item = new QTreeWidgetItem();
194 subcat_item->setText( 0, data_sub->name );
195 /* TODO : Choose the image */
196 //subcat_item->setIcon( 0 , XXX );
197 subcat_item->setData( 0, Qt::UserRole,
198 qVariantFromValue( data_sub ) );
199 subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
201 /* Add it to the parent */
202 assert( current_item );
203 current_item->addChild( subcat_item );
206 /* Other items don't need yet a place on the tree */
209 module_PutConfig( p_config );
211 /* Build the tree of plugins */
212 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
214 /* Take every module */
215 p_module = (module_t *)p_list->p_values[i_index].p_object;
217 // Main module excluded
218 if( !strcmp( module_GetObjName( p_module ), "main" ) ) continue;
220 unsigned i_subcategory = 0, i_category = 0, confsize;
221 bool b_options = false;
222 module_config_t *const p_config = module_GetConfig (p_module, &confsize);
224 /* Loop through the configurations items */
225 for (size_t i = 0; i < confsize; i++)
227 const module_config_t *p_item = p_config + i;
229 if( p_item->i_type == CONFIG_CATEGORY )
230 i_category = p_item->value.i;
231 else if( p_item->i_type == CONFIG_SUBCATEGORY )
232 i_subcategory = p_item->value.i;
234 if( p_item->i_type & CONFIG_ITEM )
237 if( b_options && i_category && i_subcategory )
240 module_PutConfig (p_config);
242 /* Dummy item, please proceed */
243 if( !b_options || i_category == 0 || i_subcategory == 0 ) continue;
246 // Locate the category item;
247 QTreeWidgetItem *subcat_item = NULL;
248 bool b_found = false;
250 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
253 /* Get the treeWidgetItem that correspond to the category */
254 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
255 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
256 value<PrefsItemData *>();
258 /* If we match the good category */
259 if( data->i_object_id == i_category )
261 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
264 subcat_item = cat_item->child( i_sc_index );
265 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
266 value<PrefsItemData *>();
267 if( sc_data && sc_data->i_object_id == i_subcategory )
275 subcat_item = cat_item;
281 if( !b_found ) continue;
283 PrefsItemData *module_data = new PrefsItemData();
284 module_data->i_type = TYPE_MODULE;
285 module_data->psz_name = strdup( module_GetObjName( p_module ) );
286 module_data->help.clear();
288 QTreeWidgetItem *module_item = new QTreeWidgetItem();
289 module_item->setText( 0, qtr( module_GetName( p_module, VLC_FALSE ) ) );
290 //item->setIcon( 0 , XXX );
291 module_item->setData( 0, Qt::UserRole,
292 QVariant::fromValue( module_data) );
293 module_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
294 subcat_item->addChild( module_item );
297 /* We got everything, just sort a bit */
298 sortItems( 0, Qt::AscendingOrder );
300 vlc_list_release( p_list );
303 PrefsTree::~PrefsTree() {}
305 void PrefsTree::applyAll()
310 void PrefsTree::cleanAll()
315 void PrefsTree::doAll( bool doclean )
317 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
320 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
321 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
324 QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
325 for( int i_module = 0 ; i_module < sc_item->childCount();
328 PrefsItemData *data = sc_item->child( i_module )->
329 data( 0, Qt::UserRole).value<PrefsItemData *>();
330 if( data->panel && doclean )
335 else if( data->panel )
336 data->panel->apply();
338 PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
339 value<PrefsItemData *>();
340 if( data->panel && doclean )
345 else if( data->panel )
346 data->panel->apply();
348 PrefsItemData *data = cat_item->data( 0, Qt::UserRole).
349 value<PrefsItemData *>();
350 if( data->panel && doclean )
355 else if( data->panel )
356 data->panel->apply();
360 /*********************************************************************
362 *********************************************************************/
363 AdvPrefsPanel::AdvPrefsPanel( QWidget *_parent ) : QWidget( _parent )
366 AdvPrefsPanel::AdvPrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
367 PrefsItemData * data ) :
368 QWidget( _parent ), p_intf( _p_intf )
370 /* Find our module */
371 module_t *p_module = NULL;
372 if( data->i_type == TYPE_CATEGORY )
374 else if( data->i_type == TYPE_MODULE )
375 p_module = module_Find( VLC_OBJECT(p_intf), data->psz_name );
378 p_module = module_Find( VLC_OBJECT(p_intf), "main" );
383 module_config_t *const p_config = module_GetConfig (p_module, &confsize),
385 *p_end = p_config + confsize;
387 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
389 while (p_item < p_end)
391 if( p_item->i_type == CONFIG_SUBCATEGORY &&
392 ( data->i_type == TYPE_SUBCATEGORY &&
393 p_item->value.i == data->i_object_id ) ||
394 ( data->i_type == TYPE_CATSUBCAT &&
395 p_item->value.i == data->i_subcat_id ) )
402 global_layout = new QVBoxLayout();
403 global_layout->setMargin( 2 );
407 help = QString( data->help );
409 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
411 head = QString( data->name );
412 p_item++; // Why that ?
416 const char *psz_help = module_GetHelp (p_module);
417 head = QString( qtr( module_GetLongName( p_module ) ) );
421 help.append( qtr( psz_help ) );
425 QLabel *titleLabel = new QLabel( head );
426 QFont titleFont = QApplication::font( static_cast<QWidget*>(0) );
427 titleFont.setPointSize( titleFont.pointSize() + 6 );
428 titleFont.setFamily( "Verdana" );
429 titleLabel->setFont( titleFont );
432 QFrame *title_line = new QFrame;
433 title_line->setFrameShape(QFrame::HLine);
434 title_line->setFrameShadow(QFrame::Sunken);
436 QLabel *helpLabel = new QLabel( help, this );
437 helpLabel->setWordWrap( true );
439 global_layout->addWidget( titleLabel );
440 global_layout->addWidget( title_line );
441 global_layout->addWidget( helpLabel );
443 QGroupBox *box = NULL;
444 QGridLayout *boxlayout = NULL;
446 QScrollArea *scroller= new QScrollArea;
447 scroller->setFrameStyle( QFrame::NoFrame );
448 QWidget *scrolled_area = new QWidget;
450 QGridLayout *layout = new QGridLayout();
451 int i_line = 0, i_boxline = 0;
452 bool has_hotkey = false;
456 if( ( ( data->i_type == TYPE_SUBCATEGORY &&
457 p_item->value.i != data->i_object_id ) ||
458 ( data->i_type == TYPE_CATSUBCAT &&
459 p_item->value.i != data->i_subcat_id ) ) &&
460 ( p_item->i_type == CONFIG_CATEGORY ||
461 p_item->i_type == CONFIG_SUBCATEGORY ) )
463 if( p_item->b_internal == VLC_TRUE ) continue;
465 if( p_item->i_type == CONFIG_SECTION )
469 box->setLayout( boxlayout );
470 layout->addWidget( box, i_line, 0, 1, -1 );
473 box = new QGroupBox( qtr( p_item->psz_text ) );
474 boxlayout = new QGridLayout();
476 /* Only one hotkey control */
477 if( has_hotkey && p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
478 strstr( p_item->psz_name, "key-" ) )
480 if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
481 strstr( p_item->psz_name, "key-" ) )
484 ConfigControl *control;
486 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
487 p_item, NULL, layout, i_line );
489 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
490 p_item, NULL, boxlayout, i_boxline );
494 if( box ) i_boxline++;
496 controls.append( control );
498 while( !( ( data->i_type == TYPE_SUBCATEGORY ||
499 data->i_type == TYPE_CATSUBCAT ) &&
500 ( p_item->i_type == CONFIG_CATEGORY ||
501 p_item->i_type == CONFIG_SUBCATEGORY ) )
502 && ( ++p_item < p_end ) );
506 box->setLayout( boxlayout );
507 layout->addWidget( box, i_line, 0, 1, -1 );
510 module_Put( p_module );
512 scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
513 scrolled_area->setLayout( layout );
514 scroller->setWidget( scrolled_area );
515 scroller->setWidgetResizable( true );
516 global_layout->addWidget( scroller );
517 setLayout( global_layout );
520 void AdvPrefsPanel::apply()
522 QList<ConfigControl *>::Iterator i;
523 for( i = controls.begin() ; i != controls.end() ; i++ )
525 ConfigControl *c = qobject_cast<ConfigControl *>(*i);
526 c->doApply( p_intf );
529 void AdvPrefsPanel::clean()