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 )
59 setAlternatingRowColors( true );
61 setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
62 setTextElideMode( Qt::ElideNone );
63 setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
65 #define BI( a,b) QIcon a##_icon = QIcon( QPixmap( b ))
66 BI( audio, ":/pixmaps/advprefs_audio.png" );
67 BI( video, ":/pixmaps/advprefs_video.png" );
68 BI( input, ":/pixmaps/advprefs_codec.png" );
69 BI( sout, ":/pixmaps/advprefs_sout.png" );
70 BI( advanced, ":/pixmaps/advprefs_extended.png" );
71 BI( playlist, ":/pixmaps/advprefs_playlist.png" );
72 BI( interface, ":/pixmaps/advprefs_intf.png" );
75 /* Build the tree for the main module */
76 const module_t *p_module = NULL;
77 vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
80 for( unsigned i = 0; p_module == NULL; i++ )
82 assert (i < (unsigned)p_list->i_count);
84 const module_t *p_main = (module_t *)p_list->p_values[i].p_object;
85 if( strcmp( module_GetObjName( p_main ), "main" ) == 0 )
89 PrefsItemData *data = NULL;
90 QTreeWidgetItem *current_item = NULL;
92 module_config_t *const p_config = module_GetConfig (p_module, &confsize);
94 for (size_t i = 0; i < confsize; i++)
96 module_config_t *p_item = p_config + i;
100 switch( p_item->i_type )
102 case CONFIG_CATEGORY:
103 if( p_item->value.i == -1 ) break;
104 data = new PrefsItemData();
105 data->name = QString( qtr( config_CategoryNameGet
106 ( p_item->value.i ) ) );
107 psz_help = config_CategoryHelpGet( p_item->value.i );
109 data->help = QString( qtr(psz_help) );
112 data->i_type = TYPE_CATEGORY;
113 data->i_object_id = p_item->value.i;
115 switch( p_item->value.i )
117 #define CI(a,b) case a: icon = b##_icon;break
118 CI( CAT_AUDIO, audio );
119 CI( CAT_VIDEO, video );
120 CI( CAT_INPUT, input );
121 CI( CAT_SOUT, sout );
122 CI( CAT_ADVANCED, advanced );
123 CI( CAT_PLAYLIST, playlist );
124 CI( CAT_INTERFACE, interface );
128 current_item = new QTreeWidgetItem();
129 current_item->setText( 0, data->name );
130 current_item->setIcon( 0 , icon );
131 current_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
132 current_item->setData( 0, Qt::UserRole,
133 qVariantFromValue( data ) );
134 addTopLevelItem( current_item );
136 case CONFIG_SUBCATEGORY:
137 if( p_item->value.i == -1 ) break;
139 /* Special cases: move the main subcategories to the parent cat*/
141 ( p_item->value.i == SUBCAT_VIDEO_GENERAL ||
142 p_item->value.i == SUBCAT_ADVANCED_MISC ||
143 p_item->value.i == SUBCAT_INPUT_GENERAL ||
144 p_item->value.i == SUBCAT_INTERFACE_GENERAL ||
145 p_item->value.i == SUBCAT_SOUT_GENERAL||
146 p_item->value.i == SUBCAT_PLAYLIST_GENERAL||
147 p_item->value.i == SUBCAT_AUDIO_GENERAL ) )
149 // Data still contains the correct thing
150 data->i_type = TYPE_CATSUBCAT;
151 data->i_subcat_id = p_item->value.i;
152 data->name = QString( qtr( config_CategoryNameGet(
153 p_item->value.i )) );
154 psz_help = config_CategoryHelpGet( p_item->value.i );
156 data->help = QString( qtr(psz_help) );
159 current_item->setData( 0, Qt::UserRole,
160 QVariant::fromValue( data ) );
164 data = new PrefsItemData();
165 data->name = QString( qtr( config_CategoryNameGet(
167 psz_help = config_CategoryHelpGet( p_item->value.i );
169 data->help = QString( qtr(psz_help) );
172 data->i_type = TYPE_SUBCATEGORY;
173 data->i_object_id = p_item->value.i;
175 assert( current_item );
177 /* TODO : Choose the image */
178 QTreeWidgetItem *subcat_item = new QTreeWidgetItem();
179 subcat_item->setText( 0, data->name );
180 //item->setIcon( 0 , XXX );
181 subcat_item->setData( 0, Qt::UserRole,
182 qVariantFromValue(data) );
183 subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
184 current_item->addChild( subcat_item );
188 module_PutConfig (p_config);
190 /* Build the tree of plugins */
191 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
193 p_module = (module_t *)p_list->p_values[i_index].p_object;
195 // Main module excluded
196 if( !strcmp( module_GetObjName( p_module ), "main" ) ) continue;
198 unsigned i_subcategory = 0, i_category = 0, confsize;
199 bool b_options = false;
200 module_config_t *const p_config = module_GetConfig (p_module, &confsize);
202 for (size_t i = 0; i < confsize; i++)
204 const module_config_t *p_item = p_config + i;
206 if( p_item->i_type == CONFIG_CATEGORY )
207 i_category = p_item->value.i;
208 else if( p_item->i_type == CONFIG_SUBCATEGORY )
209 i_subcategory = p_item->value.i;
211 if( p_item->i_type & CONFIG_ITEM )
214 if( b_options && i_category && i_subcategory )
217 module_PutConfig (p_config);
218 if( !b_options || i_category == 0 || i_subcategory == 0 ) continue;
220 // Locate the category item;
221 QTreeWidgetItem *subcat_item = NULL;
222 bool b_found = false;
223 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
226 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
227 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
228 value<PrefsItemData *>();
229 if( data->i_object_id == i_category )
231 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
234 subcat_item = cat_item->child( i_sc_index );
235 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
236 value<PrefsItemData *>();
237 if( sc_data && sc_data->i_object_id == i_subcategory )
245 subcat_item = cat_item;
251 if( !b_found ) continue;
253 PrefsItemData *module_data = new PrefsItemData();
254 module_data->i_type = TYPE_MODULE;
255 module_data->psz_name = strdup( module_GetObjName( p_module ) );
256 module_data->help.clear();
258 QTreeWidgetItem *module_item = new QTreeWidgetItem();
259 module_item->setText( 0, qtr( module_GetName( p_module, VLC_FALSE ) ) );
260 //item->setIcon( 0 , XXX );
261 module_item->setData( 0, Qt::UserRole,
262 QVariant::fromValue( module_data) );
263 module_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
264 subcat_item->addChild( module_item );
267 /* We got everything, just sort a bit */
268 sortItems( 0, Qt::AscendingOrder );
270 vlc_list_release( p_list );
273 PrefsTree::~PrefsTree() {}
275 void PrefsTree::applyAll()
280 void PrefsTree::cleanAll()
285 void PrefsTree::doAll( bool doclean )
287 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
290 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
291 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
294 QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
295 for( int i_module = 0 ; i_module < sc_item->childCount();
298 PrefsItemData *data = sc_item->child( i_module )->
299 data( 0, Qt::UserRole).value<PrefsItemData *>();
300 if( data->panel && doclean )
305 else if( data->panel )
306 data->panel->apply();
308 PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
309 value<PrefsItemData *>();
310 if( data->panel && doclean )
315 else if( data->panel )
316 data->panel->apply();
318 PrefsItemData *data = cat_item->data( 0, Qt::UserRole).
319 value<PrefsItemData *>();
320 if( data->panel && doclean )
325 else if( data->panel )
326 data->panel->apply();
330 /*********************************************************************
332 *********************************************************************/
333 AdvPrefsPanel::AdvPrefsPanel( QWidget *_parent ) : QWidget( _parent )
336 AdvPrefsPanel::AdvPrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
337 PrefsItemData * data ) :
338 QWidget( _parent ), p_intf( _p_intf )
340 /* Find our module */
341 module_t *p_module = NULL;
342 if( data->i_type == TYPE_CATEGORY )
344 else if( data->i_type == TYPE_MODULE )
345 p_module = module_Find( VLC_OBJECT(p_intf), data->psz_name );
348 p_module = module_Find( VLC_OBJECT(p_intf), "main" );
353 module_config_t *const p_config = module_GetConfig (p_module, &confsize),
355 *p_end = p_config + confsize;
357 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
359 while (p_item < p_end)
361 if( p_item->i_type == CONFIG_SUBCATEGORY &&
362 ( data->i_type == TYPE_SUBCATEGORY &&
363 p_item->value.i == data->i_object_id ) ||
364 ( data->i_type == TYPE_CATSUBCAT &&
365 p_item->value.i == data->i_subcat_id ) )
372 global_layout = new QVBoxLayout();
373 global_layout->setMargin( 2 );
377 help = QString( data->help );
379 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
381 head = QString( data->name );
382 p_item++; // Why that ?
386 const char *psz_help = module_GetHelp (p_module);
387 head = QString( qtr( module_GetLongName( p_module ) ) );
391 help.append( qtr( psz_help ) );
395 QLabel *titleLabel = new QLabel( head );
396 QFont titleFont = QApplication::font( static_cast<QWidget*>(0) );
397 titleFont.setPointSize( titleFont.pointSize() + 6 );
398 titleFont.setFamily( "Verdana" );
399 titleLabel->setFont( titleFont );
402 QFrame *title_line = new QFrame;
403 title_line->setFrameShape(QFrame::HLine);
404 title_line->setFrameShadow(QFrame::Sunken);
406 QLabel *helpLabel = new QLabel( help, this );
407 helpLabel->setWordWrap( true );
409 global_layout->addWidget( titleLabel );
410 global_layout->addWidget( title_line );
411 global_layout->addWidget( helpLabel );
413 QGroupBox *box = NULL;
414 QGridLayout *boxlayout = NULL;
416 QScrollArea *scroller= new QScrollArea;
417 scroller->setFrameStyle( QFrame::NoFrame );
418 QWidget *scrolled_area = new QWidget;
420 QGridLayout *layout = new QGridLayout();
421 int i_line = 0, i_boxline = 0;
422 bool has_hotkey = false;
426 if( ( ( data->i_type == TYPE_SUBCATEGORY &&
427 p_item->value.i != data->i_object_id ) ||
428 ( data->i_type == TYPE_CATSUBCAT &&
429 p_item->value.i != data->i_subcat_id ) ) &&
430 ( p_item->i_type == CONFIG_CATEGORY ||
431 p_item->i_type == CONFIG_SUBCATEGORY ) )
433 if( p_item->b_internal == VLC_TRUE ) continue;
435 if( p_item->i_type == CONFIG_SECTION )
439 box->setLayout( boxlayout );
440 layout->addWidget( box, i_line, 0, 1, -1 );
443 box = new QGroupBox( qtr( p_item->psz_text ) );
444 boxlayout = new QGridLayout();
446 /* Only one hotkey control */
447 if( has_hotkey && p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
448 strstr( p_item->psz_name, "key-" ) )
450 if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
451 strstr( p_item->psz_name, "key-" ) )
454 ConfigControl *control;
456 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
457 p_item, NULL, layout, i_line );
459 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
460 p_item, NULL, boxlayout, i_boxline );
464 if( box ) i_boxline++;
466 controls.append( control );
468 while( !( ( data->i_type == TYPE_SUBCATEGORY ||
469 data->i_type == TYPE_CATSUBCAT ) &&
470 ( p_item->i_type == CONFIG_CATEGORY ||
471 p_item->i_type == CONFIG_SUBCATEGORY ) )
472 && ( ++p_item < p_end ) );
476 box->setLayout( boxlayout );
477 layout->addWidget( box, i_line, 0, 1, -1 );
480 module_Put( 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 );
487 setLayout( global_layout );
490 void AdvPrefsPanel::apply()
492 QList<ConfigControl *>::Iterator i;
493 for( i = controls.begin() ; i != controls.end() ; i++ )
495 ConfigControl *c = qobject_cast<ConfigControl *>(*i);
496 c->doApply( p_intf );
499 void AdvPrefsPanel::clean()