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, ":/advprefs_audio" );
69 BI( video, ":/advprefs_video" );
70 BI( input, ":/advprefs_codec" );
71 BI( sout, ":/advprefs_sout" );
72 BI( advanced, ":/advprefs_extended" );
73 BI( playlist, ":/advprefs_playlist" );
74 BI( interface, ":/advprefs_intf" );
77 /* Build the tree for the main module */
78 module_t *p_module = module_GetMainModule( p_intf );
80 /* Initialisation and get the confsize */
81 PrefsItemData *data = NULL;
82 PrefsItemData *data_sub = NULL;
83 QTreeWidgetItem *current_item = NULL;
85 module_config_t *const p_config = module_GetConfig (p_module, &confsize);
87 /* Go through the list of conf */
88 for( size_t i = 0; i < confsize; i++ )
93 /* Work on a new item */
94 module_config_t *p_item = p_config + i;
96 switch( p_item->i_type )
98 /* This is a category */
100 if( p_item->value.i == -1 ) break;
102 /* PrefsItemData Init */
103 data = new PrefsItemData();
104 data->name = qtr( config_CategoryNameGet( p_item->value.i ) );
105 psz_help = config_CategoryHelpGet( p_item->value.i );
107 data->help = qtr( psz_help );
110 data->i_type = TYPE_CATEGORY;
111 data->i_object_id = p_item->value.i;
113 /* This is a category, put a nice icon */
114 switch( p_item->value.i )
116 #define CI(a,b) case a: icon = b##_icon;break
117 CI( CAT_AUDIO, audio );
118 CI( CAT_VIDEO, video );
119 CI( CAT_INPUT, input );
120 CI( CAT_SOUT, sout );
121 CI( CAT_ADVANCED, advanced );
122 CI( CAT_PLAYLIST, playlist );
123 CI( CAT_INTERFACE, interface );
127 /* Create a new QTreeItem to display it in the tree at top level */
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 );
137 /* This is a subcategory */
138 case CONFIG_SUBCATEGORY:
139 if( p_item->value.i == -1 ) break;
141 /* Special cases: move the main subcategories to the parent cat*/
143 ( p_item->value.i == SUBCAT_VIDEO_GENERAL ||
144 p_item->value.i == SUBCAT_ADVANCED_MISC ||
145 p_item->value.i == SUBCAT_INPUT_GENERAL ||
146 p_item->value.i == SUBCAT_INTERFACE_GENERAL ||
147 p_item->value.i == SUBCAT_SOUT_GENERAL||
148 p_item->value.i == SUBCAT_PLAYLIST_GENERAL||
149 p_item->value.i == SUBCAT_AUDIO_GENERAL ) )
151 /* Data still contains the correct thing */
152 data->i_type = TYPE_CATSUBCAT;
153 data->i_subcat_id = p_item->value.i;
154 data->name = qtr( config_CategoryNameGet( p_item->value.i ) );
155 psz_help = config_CategoryHelpGet( p_item->value.i );
157 data->help = qtr( psz_help );
160 current_item->setData( 0, Qt::UserRole,
161 QVariant::fromValue( data ) );
165 /* Normal Subcategories */
167 /* Process the Data */
168 data_sub = new PrefsItemData();
169 data_sub->name = qtr( config_CategoryNameGet( p_item->value.i) );
170 psz_help = config_CategoryHelpGet( p_item->value.i );
172 data_sub->help = qtr( psz_help );
174 data_sub->help.clear();
175 data_sub->i_type = TYPE_SUBCATEGORY;
176 data_sub->i_object_id = p_item->value.i;
178 /* Create a new TreeWidget */
179 QTreeWidgetItem *subcat_item = new QTreeWidgetItem();
180 subcat_item->setText( 0, data_sub->name );
181 subcat_item->setData( 0, Qt::UserRole,
182 qVariantFromValue( data_sub ) );
183 subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
185 /* Add it to the parent */
186 assert( current_item );
187 current_item->addChild( subcat_item );
190 /* Other items don't need yet a place on the tree */
193 module_PutConfig( p_config );
194 vlc_object_release( (vlc_object_t*)p_module );
197 vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
199 /* Build the tree of plugins */
200 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
202 /* Take every module */
203 p_module = (module_t *)p_list->p_values[i_index].p_object;
205 // Main module excluded
206 if( module_IsMainModule( p_module) ) continue;
208 unsigned i_subcategory = 0, i_category = 0, confsize;
209 bool b_options = false;
210 module_config_t *const p_config = module_GetConfig (p_module, &confsize);
212 /* Loop through the configurations items */
213 for (size_t i = 0; i < confsize; i++)
215 const module_config_t *p_item = p_config + i;
217 if( p_item->i_type == CONFIG_CATEGORY )
218 i_category = p_item->value.i;
219 else if( p_item->i_type == CONFIG_SUBCATEGORY )
220 i_subcategory = p_item->value.i;
222 if( p_item->i_type & CONFIG_ITEM )
225 if( b_options && i_category && i_subcategory )
228 module_PutConfig (p_config);
230 /* Dummy item, please proceed */
231 if( !b_options || i_category == 0 || i_subcategory == 0 ) continue;
234 // Locate the category item;
235 QTreeWidgetItem *subcat_item = NULL;
236 bool b_found = false;
238 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
241 /* Get the treeWidgetItem that correspond to the category */
242 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
243 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
244 value<PrefsItemData *>();
246 /* If we match the good category */
247 if( data->i_object_id == i_category )
249 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
252 subcat_item = cat_item->child( i_sc_index );
253 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
254 value<PrefsItemData *>();
255 if( sc_data && sc_data->i_object_id == i_subcategory )
263 subcat_item = cat_item;
269 if( !b_found ) continue;
271 PrefsItemData *module_data = new PrefsItemData();
272 module_data->i_type = TYPE_MODULE;
273 module_data->psz_name = strdup( module_GetObjName( p_module ) );
274 module_data->help.clear();
275 QTreeWidgetItem *module_item = new QTreeWidgetItem();
276 module_item->setText( 0, qtr( module_GetName( p_module, false ) ) );
277 module_item->setData( 0, Qt::UserRole,
278 QVariant::fromValue( module_data) );
279 module_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
280 subcat_item->addChild( module_item );
283 /* We got everything, just sort a bit */
284 sortItems( 0, Qt::AscendingOrder );
286 vlc_list_release( p_list );
289 PrefsTree::~PrefsTree() {}
291 void PrefsTree::applyAll()
296 void PrefsTree::cleanAll()
301 void PrefsTree::doAll( bool doclean )
303 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
306 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
307 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
310 QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
311 for( int i_module = 0 ; i_module < sc_item->childCount();
314 PrefsItemData *data = sc_item->child( i_module )->
315 data( 0, Qt::UserRole).value<PrefsItemData *>();
316 if( data->panel && doclean )
321 else if( data->panel )
322 data->panel->apply();
324 PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
325 value<PrefsItemData *>();
326 if( data->panel && doclean )
331 else if( data->panel )
332 data->panel->apply();
334 PrefsItemData *data = cat_item->data( 0, Qt::UserRole).
335 value<PrefsItemData *>();
336 if( data->panel && doclean )
341 else if( data->panel )
342 data->panel->apply();
346 /*********************************************************************
348 *********************************************************************/
349 AdvPrefsPanel::AdvPrefsPanel( QWidget *_parent ) : QWidget( _parent )
352 AdvPrefsPanel::AdvPrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
353 PrefsItemData * data ) :
354 QWidget( _parent ), p_intf( _p_intf )
356 /* Find our module */
357 module_t *p_module = NULL;
358 if( data->i_type == TYPE_CATEGORY )
360 else if( data->i_type == TYPE_MODULE )
361 p_module = module_Find( p_intf, data->psz_name );
364 p_module = module_GetMainModule( p_intf );
369 module_config_t *const p_config = module_GetConfig (p_module, &confsize),
371 *p_end = p_config + confsize;
373 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
375 while (p_item < p_end)
377 if( p_item->i_type == CONFIG_SUBCATEGORY &&
378 ( data->i_type == TYPE_SUBCATEGORY &&
379 p_item->value.i == data->i_object_id ) ||
380 ( data->i_type == TYPE_CATSUBCAT &&
381 p_item->value.i == data->i_subcat_id ) )
388 global_layout = new QVBoxLayout();
389 global_layout->setMargin( 2 );
393 help = QString( data->help );
395 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
397 head = QString( data->name );
398 p_item++; // Why that ?
402 const char *psz_help = module_GetHelp (p_module);
403 head = QString( qtr( module_GetLongName( p_module ) ) );
407 help.append( qtr( psz_help ) );
411 QLabel *titleLabel = new QLabel( head );
412 QFont titleFont = QApplication::font( static_cast<QWidget*>(0) );
413 titleFont.setPointSize( titleFont.pointSize() + 6 );
414 titleFont.setFamily( "Verdana" );
415 titleLabel->setFont( titleFont );
418 QFrame *title_line = new QFrame;
419 title_line->setFrameShape(QFrame::HLine);
420 title_line->setFrameShadow(QFrame::Sunken);
422 QLabel *helpLabel = new QLabel( help, this );
423 helpLabel->setWordWrap( true );
425 global_layout->addWidget( titleLabel );
426 global_layout->addWidget( title_line );
427 global_layout->addWidget( helpLabel );
429 QGroupBox *box = NULL;
430 QGridLayout *boxlayout = NULL;
432 QScrollArea *scroller= new QScrollArea;
433 scroller->setFrameStyle( QFrame::NoFrame );
434 QWidget *scrolled_area = new QWidget;
436 QGridLayout *layout = new QGridLayout();
437 int i_line = 0, i_boxline = 0;
438 bool has_hotkey = false;
442 if( ( ( data->i_type == TYPE_SUBCATEGORY &&
443 p_item->value.i != data->i_object_id ) ||
444 ( data->i_type == TYPE_CATSUBCAT &&
445 p_item->value.i != data->i_subcat_id ) ) &&
446 ( p_item->i_type == CONFIG_CATEGORY ||
447 p_item->i_type == CONFIG_SUBCATEGORY ) )
449 if( p_item->b_internal == true ) continue;
451 if( p_item->i_type == CONFIG_SECTION )
455 box->setLayout( boxlayout );
457 layout->addWidget( box, i_line, 0, 1, -1 );
460 box = new QGroupBox( qtr( p_item->psz_text ) );
462 boxlayout = new QGridLayout();
464 /* Only one hotkey control */
465 if( has_hotkey && p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
466 strstr( p_item->psz_name, "key-" ) )
468 if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
469 strstr( p_item->psz_name, "key-" ) )
472 ConfigControl *control;
474 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
475 p_item, this, layout, i_line );
477 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
478 p_item, this, boxlayout, i_boxline );
482 if( box ) i_boxline++;
484 controls.append( control );
486 while( !( ( data->i_type == TYPE_SUBCATEGORY ||
487 data->i_type == TYPE_CATSUBCAT ) &&
488 ( p_item->i_type == CONFIG_CATEGORY ||
489 p_item->i_type == CONFIG_SUBCATEGORY ) )
490 && ( ++p_item < p_end ) );
494 box->setLayout( boxlayout );
496 layout->addWidget( box, i_line, 0, 1, -1 );
499 module_release (p_module);
501 scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
502 scrolled_area->setLayout( layout );
503 scroller->setWidget( scrolled_area );
504 scroller->setWidgetResizable( true );
505 global_layout->addWidget( scroller );
506 setLayout( global_layout );
509 void AdvPrefsPanel::apply()
511 QList<ConfigControl *>::Iterator i;
512 for( i = controls.begin() ; i != controls.end() ; i++ )
514 ConfigControl *c = qobject_cast<ConfigControl *>(*i);
515 c->doApply( p_intf );
519 void AdvPrefsPanel::clean()
522 AdvPrefsPanel::~AdvPrefsPanel()
524 qDeleteAll( controls ); controls.clear();