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 <QGridLayout>
39 #include "components/complete_preferences.hpp"
40 #include "components/preferences_widgets.hpp"
42 #include <vlc_config_cat.h>
43 #include <vlc_intf_strings.h>
44 #include <vlc_modules.h>
47 #define ITEM_HEIGHT 25
49 /*********************************************************************
51 *********************************************************************/
52 PrefsTree::PrefsTree( intf_thread_t *_p_intf, QWidget *_parent ) :
53 QTreeWidget( _parent ), p_intf( _p_intf )
55 /* General Qt options */
56 setAlternatingRowColors( true );
57 setHeaderHidden( true );
59 setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
60 setTextElideMode( Qt::ElideNone );
63 #define BI( a,b) QIcon a##_icon = QIcon( b )
64 BI( audio, ":/prefsmenu/advanced/audio" );
65 BI( video, ":/prefsmenu/advanced/video" );
66 BI( input, ":/prefsmenu/advanced/codec" );
67 BI( sout, ":/prefsmenu/advanced/sout" );
68 BI( advanced, ":/prefsmenu/advanced/extended" );
69 BI( playlist, ":/prefsmenu/advanced/playlist" );
70 BI( interface, ":/prefsmenu/advanced/intf" );
73 /* Build the tree for the main module */
74 module_t *p_module = module_get_main();
76 /* Initialisation and get the confsize */
77 PrefsItemData *data = NULL;
78 PrefsItemData *data_sub = NULL;
79 QTreeWidgetItem *current_item = NULL;
81 module_config_t *const p_config = module_config_get (p_module, &confsize);
83 /* Go through the list of conf */
84 for( size_t i = 0; i < confsize; i++ )
89 /* Work on a new item */
90 module_config_t *p_item = p_config + i;
92 switch( p_item->i_type )
94 /* This is a category */
96 if( p_item->value.i == -1 ) break;
98 /* PrefsItemData Init */
99 data = new PrefsItemData();
100 data->name = qtr( config_CategoryNameGet( p_item->value.i ) );
101 psz_help = config_CategoryHelpGet( p_item->value.i );
103 data->help = qtr( psz_help );
106 data->i_type = TYPE_CATEGORY;
107 data->i_object_id = p_item->value.i;
109 /* This is a category, put a nice icon */
110 switch( p_item->value.i )
112 #define CI(a,b) case a: icon = b##_icon;break
113 CI( CAT_AUDIO, audio );
114 CI( CAT_VIDEO, video );
115 CI( CAT_INPUT, input );
116 CI( CAT_SOUT, sout );
117 CI( CAT_ADVANCED, advanced );
118 CI( CAT_PLAYLIST, playlist );
119 CI( CAT_INTERFACE, interface );
123 /* Create a new QTreeItem to display it in the tree at top level */
124 current_item = new QTreeWidgetItem();
125 current_item->setText( 0, data->name );
126 current_item->setIcon( 0 , icon );
127 //current_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
128 current_item->setData( 0, Qt::UserRole,
129 qVariantFromValue( data ) );
130 addTopLevelItem( current_item );
133 /* This is a subcategory */
134 case CONFIG_SUBCATEGORY:
135 if( p_item->value.i == -1 ) break;
137 /* Special cases: move the main subcategories to the parent cat*/
139 ( p_item->value.i == SUBCAT_VIDEO_GENERAL ||
140 p_item->value.i == SUBCAT_ADVANCED_MISC ||
141 p_item->value.i == SUBCAT_INPUT_GENERAL ||
142 p_item->value.i == SUBCAT_INTERFACE_GENERAL ||
143 p_item->value.i == SUBCAT_SOUT_GENERAL||
144 p_item->value.i == SUBCAT_PLAYLIST_GENERAL||
145 p_item->value.i == SUBCAT_AUDIO_GENERAL ) )
147 /* Data still contains the correct thing */
148 data->i_type = TYPE_CATSUBCAT;
149 data->i_subcat_id = p_item->value.i;
150 data->name = qtr( config_CategoryNameGet( p_item->value.i ) );
151 psz_help = config_CategoryHelpGet( p_item->value.i );
153 data->help = qtr( psz_help );
156 current_item->setData( 0, Qt::UserRole,
157 QVariant::fromValue( data ) );
161 /* Normal Subcategories */
163 /* Process the Data */
164 data_sub = new PrefsItemData();
165 data_sub->name = qtr( config_CategoryNameGet( p_item->value.i) );
166 psz_help = config_CategoryHelpGet( p_item->value.i );
168 data_sub->help = qtr( psz_help );
170 data_sub->help.clear();
171 data_sub->i_type = TYPE_SUBCATEGORY;
172 data_sub->i_object_id = p_item->value.i;
174 /* Create a new TreeWidget */
175 QTreeWidgetItem *subcat_item = new QTreeWidgetItem();
176 subcat_item->setText( 0, data_sub->name );
177 subcat_item->setData( 0, Qt::UserRole,
178 qVariantFromValue( data_sub ) );
179 //subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
181 /* Add it to the parent */
182 assert( current_item );
183 current_item->addChild( subcat_item );
186 /* Other items don't need yet a place on the tree */
189 module_config_free( p_config );
190 module_release( p_module );
193 module_t **p_list = module_list_get( NULL );
194 /* Build the tree of plugins */
195 for( size_t i = 0; (p_module = p_list[i]) != NULL; i++ )
197 // Main module excluded
198 if( module_is_main( p_module) ) continue;
201 int i_subcategory = 0, i_category = 0;
203 bool b_options = false;
204 module_config_t *const p_config = module_config_get (p_module, &confsize);
206 /* Loop through the configurations items */
207 for (size_t i = 0; i < confsize; i++)
209 const module_config_t *p_item = p_config + i;
211 if( p_item->i_type == CONFIG_CATEGORY )
212 i_category = p_item->value.i;
213 else if( p_item->i_type == CONFIG_SUBCATEGORY )
214 i_subcategory = p_item->value.i;
216 if( p_item->i_type & CONFIG_ITEM )
219 if( b_options && i_category && i_subcategory )
222 module_config_free (p_config);
224 /* Dummy item, please proceed */
225 if( !b_options || i_category == 0 || i_subcategory == 0 ) continue;
228 // Locate the category item;
229 QTreeWidgetItem *subcat_item = NULL;
230 bool b_found = false;
232 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
235 /* Get the treeWidgetItem that correspond to the category */
236 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
237 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
238 value<PrefsItemData *>();
240 /* If we match the good category */
241 if( data->i_object_id == i_category )
243 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
246 subcat_item = cat_item->child( i_sc_index );
247 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
248 value<PrefsItemData *>();
249 if( sc_data && sc_data->i_object_id == i_subcategory )
257 subcat_item = cat_item;
263 if( !b_found ) continue;
265 PrefsItemData *module_data = new PrefsItemData();
266 module_data->i_type = TYPE_MODULE;
267 module_data->psz_name = strdup( module_get_object( p_module ) );
268 module_data->help.clear();
269 QTreeWidgetItem *module_item = new QTreeWidgetItem();
270 module_item->setText( 0, qtr( module_get_name( p_module, false ) ) );
271 module_item->setData( 0, Qt::UserRole,
272 QVariant::fromValue( module_data) );
273 //module_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
274 subcat_item->addChild( module_item );
277 /* We got everything, just sort a bit */
278 sortItems( 0, Qt::AscendingOrder );
280 module_list_free( p_list );
281 resizeColumnToContents( 0 );
284 PrefsTree::~PrefsTree() {}
286 void PrefsTree::applyAll()
291 void PrefsTree::cleanAll()
296 void PrefsTree::doAll( bool doclean )
298 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
301 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
302 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
305 QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
306 for( int i_module = 0 ; i_module < sc_item->childCount();
309 PrefsItemData *data = sc_item->child( i_module )->
310 data( 0, Qt::UserRole).value<PrefsItemData *>();
311 if( data->panel && doclean )
316 else if( data->panel )
317 data->panel->apply();
319 PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
320 value<PrefsItemData *>();
321 if( data->panel && doclean )
326 else if( data->panel )
327 data->panel->apply();
329 PrefsItemData *data = cat_item->data( 0, Qt::UserRole).
330 value<PrefsItemData *>();
331 if( data->panel && doclean )
336 else if( data->panel )
337 data->panel->apply();
341 /*********************************************************************
343 *********************************************************************/
344 AdvPrefsPanel::AdvPrefsPanel( QWidget *_parent ) : QWidget( _parent )
347 AdvPrefsPanel::AdvPrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
348 PrefsItemData * data ) :
349 QWidget( _parent ), p_intf( _p_intf )
351 /* Find our module */
352 module_t *p_module = NULL;
353 if( data->i_type == TYPE_CATEGORY )
355 else if( data->i_type == TYPE_MODULE )
356 p_module = module_find( data->psz_name );
359 p_module = module_get_main();
364 module_config_t *const p_config = module_config_get (p_module, &confsize),
366 *p_end = p_config + confsize;
368 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
370 while (p_item < p_end)
372 if( p_item->i_type == CONFIG_SUBCATEGORY &&
373 ( ( data->i_type == TYPE_SUBCATEGORY &&
374 p_item->value.i == data->i_object_id ) ||
375 ( data->i_type == TYPE_CATSUBCAT &&
376 p_item->value.i == data->i_subcat_id ) ) )
383 global_layout = new QVBoxLayout();
384 global_layout->setMargin( 2 );
388 help = QString( data->help );
390 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
392 head = QString( data->name );
393 p_item++; // Why that ?
397 const char *psz_help = module_get_help (p_module);
398 head = QString( qtr( module_GetLongName( p_module ) ) );
402 help.append( qtr( psz_help ) );
406 QLabel *titleLabel = new QLabel( head );
407 QFont titleFont = QApplication::font();
408 titleFont.setPointSize( titleFont.pointSize() + 6 );
409 titleLabel->setFont( titleFont );
412 QFrame *title_line = new QFrame;
413 title_line->setFrameShape(QFrame::HLine);
414 title_line->setFrameShadow(QFrame::Sunken);
416 QLabel *helpLabel = new QLabel( help, this );
417 helpLabel->setWordWrap( true );
419 global_layout->addWidget( titleLabel );
420 global_layout->addWidget( title_line );
421 global_layout->addWidget( helpLabel );
423 QGroupBox *box = NULL;
424 QGridLayout *boxlayout = NULL;
426 QScrollArea *scroller= new QScrollArea;
427 scroller->setFrameStyle( QFrame::NoFrame );
428 QWidget *scrolled_area = new QWidget;
430 QGridLayout *layout = new QGridLayout();
431 int i_line = 0, i_boxline = 0;
432 bool has_hotkey = false;
436 if( ( ( data->i_type == TYPE_SUBCATEGORY &&
437 p_item->value.i != data->i_object_id ) ||
438 ( data->i_type == TYPE_CATSUBCAT &&
439 p_item->value.i != data->i_subcat_id ) ) &&
440 ( p_item->i_type == CONFIG_CATEGORY ||
441 p_item->i_type == CONFIG_SUBCATEGORY ) )
443 if( p_item->b_internal ) continue;
445 if( p_item->i_type == CONFIG_SECTION )
449 box->setLayout( boxlayout );
451 layout->addWidget( box, i_line, 0, 1, -1 );
454 box = new QGroupBox( qtr( p_item->psz_text ), this );
456 boxlayout = new QGridLayout();
458 /* Only one hotkey control */
459 if( (p_item->i_type & CONFIG_ITEM) == CONFIG_ITEM_KEY )
466 ConfigControl *control;
468 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
469 p_item, this, layout, i_line );
471 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
472 p_item, this, boxlayout, i_boxline );
476 if( box ) i_boxline++;
478 controls.append( control );
480 while( !( ( data->i_type == TYPE_SUBCATEGORY ||
481 data->i_type == TYPE_CATSUBCAT ) &&
482 ( p_item->i_type == CONFIG_CATEGORY ||
483 p_item->i_type == CONFIG_SUBCATEGORY ) )
484 && ( ++p_item < p_end ) );
488 box->setLayout( boxlayout );
490 layout->addWidget( box, i_line, 0, 1, -1 );
493 module_release (p_module);
495 scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
496 scrolled_area->setLayout( layout );
497 scroller->setWidget( scrolled_area );
498 scroller->setWidgetResizable( true );
499 global_layout->addWidget( scroller );
500 setLayout( global_layout );
503 void AdvPrefsPanel::apply()
505 foreach ( ConfigControl *cfg, controls )
509 void AdvPrefsPanel::clean()
512 AdvPrefsPanel::~AdvPrefsPanel()
514 qDeleteAll( controls ); controls.clear();