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 );
67 #define BI( a,b) QIcon a##_icon = QIcon( QPixmap( b ))
68 BI( audio, ":/pixmaps/advprefs_audio.png" );
69 BI( video, ":/pixmaps/advprefs_video.png" );
70 BI( input, ":/pixmaps/advprefs_codec.png" );
71 BI( sout, ":/pixmaps/advprefs_sout.png" );
72 BI( advanced, ":/pixmaps/advprefs_extended.png" );
73 BI( playlist, ":/pixmaps/advprefs_playlist.png" );
74 BI( interface, ":/pixmaps/advprefs_intf.png" );
77 /* Build the tree for the main module */
78 const module_t *p_module = NULL;
79 vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
83 /* Find the main module */
84 for( unsigned i = 0; p_module == NULL; i++ )
86 assert (i < (unsigned)p_list->i_count);
88 const module_t *p_main = (module_t *)p_list->p_values[i].p_object;
89 if( strcmp( module_GetObjName( p_main ), "main" ) == 0 )
93 /* Initialisation and get the confsize */
94 PrefsItemData *data = NULL;
95 PrefsItemData *data_sub = NULL;
96 QTreeWidgetItem *current_item = NULL;
98 module_config_t *const p_config = module_GetConfig (p_module, &confsize);
100 /* Go through the list of conf */
101 for( size_t i = 0; i < confsize; i++ )
103 const char *psz_help;
106 /* Work on a new item */
107 module_config_t *p_item = p_config + i;
109 switch( p_item->i_type )
111 /* This is a category */
112 case CONFIG_CATEGORY:
113 if( p_item->value.i == -1 ) break;
115 /* PrefsItemData Init */
116 data = new PrefsItemData();
117 data->name = qtr( config_CategoryNameGet( p_item->value.i ) );
118 psz_help = config_CategoryHelpGet( p_item->value.i );
120 data->help = qtr( psz_help );
123 data->i_type = TYPE_CATEGORY;
124 data->i_object_id = p_item->value.i;
126 /* This is a category, put a nice icon */
127 switch( p_item->value.i )
129 #define CI(a,b) case a: icon = b##_icon;break
130 CI( CAT_AUDIO, audio );
131 CI( CAT_VIDEO, video );
132 CI( CAT_INPUT, input );
133 CI( CAT_SOUT, sout );
134 CI( CAT_ADVANCED, advanced );
135 CI( CAT_PLAYLIST, playlist );
136 CI( CAT_INTERFACE, interface );
140 /* Create a new QTreeItem to display it in the tree at top level */
141 current_item = new QTreeWidgetItem();
142 current_item->setText( 0, data->name );
143 current_item->setIcon( 0 , icon );
144 current_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
145 current_item->setData( 0, Qt::UserRole,
146 qVariantFromValue( data ) );
147 addTopLevelItem( current_item );
150 /* This is a subcategory */
151 case CONFIG_SUBCATEGORY:
152 if( p_item->value.i == -1 ) break;
154 /* Special cases: move the main subcategories to the parent cat*/
156 ( p_item->value.i == SUBCAT_VIDEO_GENERAL ||
157 p_item->value.i == SUBCAT_ADVANCED_MISC ||
158 p_item->value.i == SUBCAT_INPUT_GENERAL ||
159 p_item->value.i == SUBCAT_INTERFACE_GENERAL ||
160 p_item->value.i == SUBCAT_SOUT_GENERAL||
161 p_item->value.i == SUBCAT_PLAYLIST_GENERAL||
162 p_item->value.i == SUBCAT_AUDIO_GENERAL ) )
164 /* Data still contains the correct thing */
165 data->i_type = TYPE_CATSUBCAT;
166 data->i_subcat_id = p_item->value.i;
167 data->name = qtr( config_CategoryNameGet( p_item->value.i ) );
168 psz_help = config_CategoryHelpGet( p_item->value.i );
170 data->help = qtr( psz_help );
173 current_item->setData( 0, Qt::UserRole,
174 QVariant::fromValue( data ) );
178 /* Normal Subcategories */
180 /* Process the Data */
181 data_sub = new PrefsItemData();
182 data_sub->name = qtr( config_CategoryNameGet( p_item->value.i) );
183 psz_help = config_CategoryHelpGet( p_item->value.i );
185 data_sub->help = qtr( psz_help );
187 data_sub->help.clear();
188 data_sub->i_type = TYPE_SUBCATEGORY;
189 data_sub->i_object_id = p_item->value.i;
191 /* Create a new TreeWidget */
192 QTreeWidgetItem *subcat_item = new QTreeWidgetItem();
193 subcat_item->setText( 0, data_sub->name );
194 /* TODO : Choose the image */
195 //subcat_item->setIcon( 0 , XXX );
196 subcat_item->setData( 0, Qt::UserRole,
197 qVariantFromValue( data_sub ) );
198 subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
200 /* Add it to the parent */
201 assert( current_item );
202 current_item->addChild( subcat_item );
205 /* Other items don't need yet a place on the tree */
208 module_PutConfig( p_config );
210 /* Build the tree of plugins */
211 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
213 /* Take every module */
214 p_module = (module_t *)p_list->p_values[i_index].p_object;
216 // Main module excluded
217 if( !strcmp( module_GetObjName( p_module ), "main" ) ) continue;
219 unsigned i_subcategory = 0, i_category = 0, confsize;
220 bool b_options = false;
221 module_config_t *const p_config = module_GetConfig (p_module, &confsize);
223 /* Loop through the configurations items */
224 for (size_t i = 0; i < confsize; i++)
226 const module_config_t *p_item = p_config + i;
228 if( p_item->i_type == CONFIG_CATEGORY )
229 i_category = p_item->value.i;
230 else if( p_item->i_type == CONFIG_SUBCATEGORY )
231 i_subcategory = p_item->value.i;
233 if( p_item->i_type & CONFIG_ITEM )
236 if( b_options && i_category && i_subcategory )
239 module_PutConfig (p_config);
241 /* Dummy item, please proceed */
242 if( !b_options || i_category == 0 || i_subcategory == 0 ) continue;
245 // Locate the category item;
246 QTreeWidgetItem *subcat_item = NULL;
247 bool b_found = false;
249 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
252 /* Get the treeWidgetItem that correspond to the category */
253 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
254 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
255 value<PrefsItemData *>();
257 /* If we match the good category */
258 if( data->i_object_id == i_category )
260 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
263 subcat_item = cat_item->child( i_sc_index );
264 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
265 value<PrefsItemData *>();
266 if( sc_data && sc_data->i_object_id == i_subcategory )
274 subcat_item = cat_item;
280 if( !b_found ) continue;
282 PrefsItemData *module_data = new PrefsItemData();
283 module_data->i_type = TYPE_MODULE;
284 module_data->psz_name = strdup( module_GetObjName( p_module ) );
285 module_data->help.clear();
287 QTreeWidgetItem *module_item = new QTreeWidgetItem();
288 module_item->setText( 0, qtr( module_GetName( p_module, VLC_FALSE ) ) );
289 //item->setIcon( 0 , XXX );
290 module_item->setData( 0, Qt::UserRole,
291 QVariant::fromValue( module_data) );
292 module_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
293 subcat_item->addChild( module_item );
296 /* We got everything, just sort a bit */
297 sortItems( 0, Qt::AscendingOrder );
299 vlc_list_release( p_list );
302 PrefsTree::~PrefsTree() {}
304 void PrefsTree::applyAll()
309 void PrefsTree::cleanAll()
314 void PrefsTree::doAll( bool doclean )
316 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
319 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
320 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
323 QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
324 for( int i_module = 0 ; i_module < sc_item->childCount();
327 PrefsItemData *data = sc_item->child( i_module )->
328 data( 0, Qt::UserRole).value<PrefsItemData *>();
329 if( data->panel && doclean )
334 else if( data->panel )
335 data->panel->apply();
337 PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
338 value<PrefsItemData *>();
339 if( data->panel && doclean )
344 else if( data->panel )
345 data->panel->apply();
347 PrefsItemData *data = cat_item->data( 0, Qt::UserRole).
348 value<PrefsItemData *>();
349 if( data->panel && doclean )
354 else if( data->panel )
355 data->panel->apply();
359 /*********************************************************************
361 *********************************************************************/
362 AdvPrefsPanel::AdvPrefsPanel( QWidget *_parent ) : QWidget( _parent )
365 AdvPrefsPanel::AdvPrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
366 PrefsItemData * data ) :
367 QWidget( _parent ), p_intf( _p_intf )
369 /* Find our module */
370 module_t *p_module = NULL;
371 if( data->i_type == TYPE_CATEGORY )
373 else if( data->i_type == TYPE_MODULE )
374 p_module = module_Find( VLC_OBJECT(p_intf), data->psz_name );
377 p_module = module_Find( VLC_OBJECT(p_intf), "main" );
382 module_config_t *const p_config = module_GetConfig (p_module, &confsize),
384 *p_end = p_config + confsize;
386 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
388 while (p_item < p_end)
390 if( p_item->i_type == CONFIG_SUBCATEGORY &&
391 ( data->i_type == TYPE_SUBCATEGORY &&
392 p_item->value.i == data->i_object_id ) ||
393 ( data->i_type == TYPE_CATSUBCAT &&
394 p_item->value.i == data->i_subcat_id ) )
401 global_layout = new QVBoxLayout();
402 global_layout->setMargin( 2 );
406 help = QString( data->help );
408 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
410 head = QString( data->name );
411 p_item++; // Why that ?
415 const char *psz_help = module_GetHelp (p_module);
416 head = QString( qtr( module_GetLongName( p_module ) ) );
420 help.append( qtr( psz_help ) );
424 QLabel *titleLabel = new QLabel( head );
425 QFont titleFont = QApplication::font( static_cast<QWidget*>(0) );
426 titleFont.setPointSize( titleFont.pointSize() + 6 );
427 titleFont.setFamily( "Verdana" );
428 titleLabel->setFont( titleFont );
431 QFrame *title_line = new QFrame;
432 title_line->setFrameShape(QFrame::HLine);
433 title_line->setFrameShadow(QFrame::Sunken);
435 QLabel *helpLabel = new QLabel( help, this );
436 helpLabel->setWordWrap( true );
438 global_layout->addWidget( titleLabel );
439 global_layout->addWidget( title_line );
440 global_layout->addWidget( helpLabel );
442 QGroupBox *box = NULL;
443 QGridLayout *boxlayout = NULL;
445 QScrollArea *scroller= new QScrollArea;
446 scroller->setFrameStyle( QFrame::NoFrame );
447 QWidget *scrolled_area = new QWidget;
449 QGridLayout *layout = new QGridLayout();
450 int i_line = 0, i_boxline = 0;
451 bool has_hotkey = false;
455 if( ( ( data->i_type == TYPE_SUBCATEGORY &&
456 p_item->value.i != data->i_object_id ) ||
457 ( data->i_type == TYPE_CATSUBCAT &&
458 p_item->value.i != data->i_subcat_id ) ) &&
459 ( p_item->i_type == CONFIG_CATEGORY ||
460 p_item->i_type == CONFIG_SUBCATEGORY ) )
462 if( p_item->b_internal == VLC_TRUE ) continue;
464 if( p_item->i_type == CONFIG_SECTION )
468 box->setLayout( boxlayout );
469 layout->addWidget( box, i_line, 0, 1, -1 );
472 box = new QGroupBox( qtr( p_item->psz_text ) );
473 boxlayout = new QGridLayout();
475 /* Only one hotkey control */
476 if( has_hotkey && p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
477 strstr( p_item->psz_name, "key-" ) )
479 if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
480 strstr( p_item->psz_name, "key-" ) )
483 ConfigControl *control;
485 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
486 p_item, NULL, layout, i_line );
488 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
489 p_item, NULL, boxlayout, i_boxline );
493 if( box ) i_boxline++;
495 controls.append( control );
497 while( !( ( data->i_type == TYPE_SUBCATEGORY ||
498 data->i_type == TYPE_CATSUBCAT ) &&
499 ( p_item->i_type == CONFIG_CATEGORY ||
500 p_item->i_type == CONFIG_SUBCATEGORY ) )
501 && ( ++p_item < p_end ) );
505 box->setLayout( boxlayout );
506 layout->addWidget( box, i_line, 0, 1, -1 );
509 module_Put( p_module );
511 scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
512 scrolled_area->setLayout( layout );
513 scroller->setWidget( scrolled_area );
514 scroller->setWidgetResizable( true );
515 global_layout->addWidget( scroller );
516 setLayout( global_layout );
519 void AdvPrefsPanel::apply()
521 QList<ConfigControl *>::Iterator i;
522 for( i = controls.begin() ; i != controls.end() ; i++ )
524 ConfigControl *c = qobject_cast<ConfigControl *>(*i);
525 c->doApply( p_intf );
528 void AdvPrefsPanel::clean()