1 /*****************************************************************************
2 * preferences.cpp : "Normal preferences"
3 ****************************************************************************
4 * Copyright (C) 2006 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 *****************************************************************************/
24 #include <QApplication>
26 #include <QTreeWidget>
27 #include <QTreeWidgetItem>
32 #include <QScrollArea>
33 #include <QVBoxLayout>
34 #include <QHBoxLayout>
35 #include <QGridLayout>
36 #include <QHeaderView>
40 #include "components/complete_preferences.hpp"
41 #include "components/preferences_widgets.hpp"
44 #include <vlc_config_cat.h>
45 #include <vlc_intf_strings.h>
48 #include "pixmaps/audio.xpm"
49 #include "pixmaps/video.xpm"
50 #include "pixmaps/type_net.xpm"
51 #include "pixmaps/type_playlist.xpm"
52 #include "pixmaps/advanced.xpm"
53 #include "pixmaps/codec.xpm"
54 #include "pixmaps/intf.xpm"
56 #define ITEM_HEIGHT 25
58 /*********************************************************************
60 *********************************************************************/
61 PrefsTree::PrefsTree( intf_thread_t *_p_intf, QWidget *_parent ) :
62 QTreeWidget( _parent ), p_intf( _p_intf )
65 setAlternatingRowColors( true );
67 setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
68 setTextElideMode( Qt::ElideNone );
69 setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
71 #define BI( a,b) QIcon a##_icon = QIcon( QPixmap( b##_xpm ))
76 BI( advanced, advanced );
77 BI( playlist, type_playlist );
78 BI( interface, intf );
81 /* Build the tree for the main module */
82 const module_t *p_module = NULL;
83 vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
86 for( unsigned i = 0; p_module == NULL; i++ )
88 assert (i < (unsigned)p_list->i_count);
90 const module_t *p_main = (module_t *)p_list->p_values[i].p_object;
91 if( strcmp( p_main->psz_object_name, "main" ) == 0 )
95 PrefsItemData *data = NULL;
96 QTreeWidgetItem *current_item = NULL;
97 for (size_t i = 0; i < p_module->confsize; i++)
99 const module_config_t *p_item = p_module->p_config + i;
102 switch( p_item->i_type )
104 case CONFIG_CATEGORY:
105 if( p_item->value.i == -1 ) break;
106 data = new PrefsItemData();
107 data->name = QString( qfu( config_CategoryNameGet
108 ( p_item->value.i ) ) );
109 psz_help = config_CategoryHelpGet( p_item->value.i );
111 data->help = QString( qfu(psz_help) );
114 data->i_type = TYPE_CATEGORY;
115 data->i_object_id = p_item->value.i;
117 switch( p_item->value.i )
119 #define CI(a,b) case a: icon = b##_icon;break
120 CI( CAT_AUDIO, audio );
121 CI( CAT_VIDEO, video );
122 CI( CAT_INPUT, input );
123 CI( CAT_SOUT, sout );
124 CI( CAT_ADVANCED, advanced );
125 CI( CAT_PLAYLIST, playlist );
126 CI( CAT_INTERFACE, interface );
130 current_item = new QTreeWidgetItem();
131 current_item->setText( 0, data->name );
132 current_item->setIcon( 0 , icon );
133 current_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
134 current_item->setData( 0, Qt::UserRole,
135 qVariantFromValue( data ) );
136 addTopLevelItem( current_item );
138 case CONFIG_SUBCATEGORY:
139 if( p_item->value.i == -1 ) break;
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( qfu( config_CategoryNameGet(
153 p_item->value.i )) );
154 psz_help = config_CategoryHelpGet( p_item->value.i );
156 data->help = QString( qfu(psz_help) );
159 current_item->setData( 0, Qt::UserRole,
160 QVariant::fromValue( data ) );
163 data = new PrefsItemData();
164 data->name = QString( qfu( config_CategoryNameGet(
166 psz_help = config_CategoryHelpGet( p_item->value.i );
168 data->help = QString( qfu(psz_help) );
171 data->i_type = TYPE_SUBCATEGORY;
172 data->i_object_id = p_item->value.i;
174 assert( current_item );
176 /* TODO : Choose the image */
177 QTreeWidgetItem *subcat_item = new QTreeWidgetItem();
178 subcat_item->setText( 0, data->name );
179 //item->setIcon( 0 , XXX );
180 subcat_item->setData( 0, Qt::UserRole,
181 qVariantFromValue(data) );
182 subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
183 current_item->addChild( subcat_item );
188 /* Build the tree of plugins */
189 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
191 int i_subcategory = -1, i_category = -1, i_options = 0;
192 p_module = (module_t *)p_list->p_values[i_index].p_object;
194 // Main module excluded
195 if( !strcmp( p_module->psz_object_name, "main" ) ) continue;
197 /* Exclude empty plugins (submodules don't have config options, they
198 * are stored in the parent module) */
199 if( p_module->b_submodule ) continue;
201 for (size_t i = 0; i < p_module->confsize; i++)
203 module_config_t *p_item = p_module->p_config + i;
205 if( p_item->i_type == CONFIG_CATEGORY )
206 i_category = p_item->value.i;
207 else if( p_item->i_type == CONFIG_SUBCATEGORY )
208 i_subcategory = p_item->value.i;
209 if( p_item->i_type & CONFIG_ITEM )
212 if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
215 if( !i_options ) continue; // Nothing to display
217 // Locate the category item;
218 QTreeWidgetItem *subcat_item = NULL;
219 bool b_found = false;
220 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
223 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
224 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
225 value<PrefsItemData *>();
226 if( data->i_object_id == i_category )
228 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
231 subcat_item = cat_item->child( i_sc_index );
232 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
233 value<PrefsItemData *>();
234 if( sc_data && sc_data->i_object_id == i_subcategory )
242 subcat_item = cat_item;
248 if( !b_found ) continue;
250 PrefsItemData *module_data = new PrefsItemData();
251 module_data->b_submodule = p_module->b_submodule;
252 module_data->i_type = TYPE_MODULE;
253 module_data->psz_name = strdup( p_module->psz_object_name );
254 module_data->i_object_id = p_module->b_submodule ?
255 ((module_t *)p_module->p_parent)->i_object_id :
256 p_module->i_object_id;
257 module_data->help.clear();
259 QTreeWidgetItem *module_item = new QTreeWidgetItem();
260 module_item->setText( 0, qfu( p_module->psz_shortname ?
261 p_module->psz_shortname : p_module->psz_object_name) );
262 //item->setIcon( 0 , XXX );
263 module_item->setData( 0, Qt::UserRole,
264 QVariant::fromValue( module_data) );
265 module_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
266 subcat_item->addChild( module_item );
269 /* We got everything, just sort a bit */
270 sortItems( 0, Qt::AscendingOrder );
272 vlc_list_release( p_list );
275 PrefsTree::~PrefsTree() {}
277 void PrefsTree::applyAll()
282 void PrefsTree::cleanAll()
287 void PrefsTree::doAll( bool doclean )
289 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
292 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
293 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
296 QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
297 for( int i_module = 0 ; i_module < sc_item->childCount();
300 PrefsItemData *data = sc_item->child( i_module )->
301 data( 0, Qt::UserRole).value<PrefsItemData *>();
302 if( data->panel && doclean )
307 else if( data->panel )
308 data->panel->apply();
310 PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
311 value<PrefsItemData *>();
312 if( data->panel && doclean )
317 else if( data->panel )
318 data->panel->apply();
320 PrefsItemData *data = cat_item->data( 0, Qt::UserRole).
321 value<PrefsItemData *>();
322 if( data->panel && doclean )
327 else if( data->panel )
328 data->panel->apply();
332 /*********************************************************************
334 *********************************************************************/
335 PrefsPanel::PrefsPanel( QWidget *_parent ) : QWidget( _parent )
338 PrefsPanel::PrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
339 PrefsItemData * data ) :
340 QWidget( _parent ), p_intf( _p_intf )
342 /* Find our module */
343 module_t *p_module = NULL;
344 if( data->i_type == TYPE_CATEGORY )
346 else if( data->i_type == TYPE_MODULE )
347 p_module = (module_t *) vlc_object_get( p_intf, data->i_object_id );
350 p_module = config_FindModule( VLC_OBJECT(p_intf), "main" );
352 vlc_object_yield( p_module );
355 module_t *p_realmodule = p_module->b_submodule
356 ? (module_t *)(p_module->p_parent)
359 module_config_t *p_item = p_realmodule->p_config;
360 module_config_t *p_end = p_item + p_realmodule->confsize;
362 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
364 while (p_item < p_end)
366 if( p_item->i_type == CONFIG_SUBCATEGORY &&
367 ( data->i_type == TYPE_SUBCATEGORY &&
368 p_item->value.i == data->i_object_id ) ||
369 ( data->i_type == TYPE_CATSUBCAT &&
370 p_item->value.i == data->i_subcat_id ) )
376 global_layout = new QVBoxLayout();
377 global_layout->setMargin( 2 );
379 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
381 head = QString( data->name );
382 p_item++; // Why that ?
386 head = QString( qfu(p_module->psz_longname) );
387 if( p_module->psz_help )
390 head.append( qfu( p_module->psz_help ) );
394 QLabel *label = new QLabel( head );
395 global_layout->addWidget( label );
396 QFont myFont = QApplication::font( static_cast<QWidget*>(0) );
397 myFont.setPointSize( myFont.pointSize() + 3 ); myFont.setBold( true );
399 label->setFont( myFont );
400 QLabel *help = new QLabel( data->help, this );
401 help->setWordWrap( true );
403 global_layout->addWidget( help );
405 QGroupBox *box = NULL;
406 QGridLayout *boxlayout = NULL;
408 QScrollArea *scroller= new QScrollArea;
409 scroller->setFrameStyle( QFrame::NoFrame );
410 QWidget *scrolled_area = new QWidget;
412 QGridLayout *layout = new QGridLayout();
413 int i_line = 0, i_boxline = 0;
414 bool has_hotkey = false;
418 if( ( ( data->i_type == TYPE_SUBCATEGORY &&
419 p_item->value.i != data->i_object_id ) ||
420 ( data->i_type == TYPE_CATSUBCAT &&
421 p_item->value.i != data->i_subcat_id ) ) &&
422 ( p_item->i_type == CONFIG_CATEGORY ||
423 p_item->i_type == CONFIG_SUBCATEGORY ) )
425 if( p_item->b_internal == VLC_TRUE ) continue;
427 if( p_item->i_type == CONFIG_SECTION )
431 box->setLayout( boxlayout );
432 layout->addWidget( box, i_line, 0, 1, 2 );
435 box = new QGroupBox( qfu(p_item->psz_text) );
436 boxlayout = new QGridLayout();
438 /* Only one hotkey control */
439 if( has_hotkey && p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
440 strstr( p_item->psz_name, "key-" ) )
442 if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
443 strstr( p_item->psz_name, "key-" ) )
446 ConfigControl *control;
448 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
449 p_item, NULL, layout, i_line );
451 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
452 p_item, NULL, boxlayout, i_boxline );
458 /* A hotkey widget takes 2 lines */
459 if( box ) i_boxline ++;
463 if( box ) i_boxline++;
465 controls.append( control );
467 while( !( ( data->i_type == TYPE_SUBCATEGORY ||
468 data->i_type == TYPE_CATSUBCAT ) &&
469 ( p_item->i_type == CONFIG_CATEGORY ||
470 p_item->i_type == CONFIG_SUBCATEGORY ) )
471 && ( ++p_item < p_end ) );
475 box->setLayout( boxlayout );
476 layout->addWidget( box, i_line, 0, 1, 2 );
479 vlc_object_release( p_module );
481 scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
482 scrolled_area->setLayout( layout );
483 scroller->setWidget( scrolled_area );
484 scroller->setWidgetResizable( true );
485 global_layout->addWidget( scroller );
486 setLayout( global_layout );
489 void PrefsPanel::apply()
491 QList<ConfigControl *>::Iterator i;
492 for( i = controls.begin() ; i != controls.end() ; i++ )
494 ConfigControl *c = qobject_cast<ConfigControl *>(*i);
495 c->doApply( p_intf );
498 void PrefsPanel::clean()