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 ) );
69 #define BI( a,b) QIcon a##_icon = QIcon( QPixmap( b##_xpm ))
74 BI( advanced, advanced );
75 BI( playlist, type_playlist );
76 BI( interface, intf );
79 /* Build the tree for the main module */
80 module_t *p_module = NULL;
81 vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
84 for( int i_index = 0; p_module == NULL; i_index++ )
86 assert (i_index < p_list->i_count);
88 p_module = (module_t *)p_list->p_values[i_index].p_object;
89 if( strcmp( p_module->psz_object_name, "main" ) )
93 PrefsItemData *data = NULL;
94 QTreeWidgetItem *current_item = NULL;
95 for (size_t i = 0; i < p_module->confsize; i++)
97 module_config_t *p_item = p_module->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( qfu( config_CategoryNameGet
106 ( p_item->value.i ) ) );
107 psz_help = config_CategoryHelpGet( p_item->value.i );
109 data->help = QString( qfu(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 ( 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 = QString( qfu( config_CategoryNameGet(
151 p_item->value.i )) );
152 psz_help = config_CategoryHelpGet( p_item->value.i );
154 data->help = QString( qfu(psz_help) );
157 current_item->setData( 0, Qt::UserRole,
158 QVariant::fromValue( data ) );
161 data = new PrefsItemData();
162 data->name = QString( qfu( config_CategoryNameGet(
164 psz_help = config_CategoryHelpGet( p_item->value.i );
166 data->help = QString( qfu(psz_help) );
169 data->i_type = TYPE_SUBCATEGORY;
170 data->i_object_id = p_item->value.i;
172 assert( current_item );
174 /* TODO : Choose the image */
175 QTreeWidgetItem *subcat_item = new QTreeWidgetItem();
176 subcat_item->setText( 0, data->name );
177 //item->setIcon( 0 , XXX );
178 subcat_item->setData( 0, Qt::UserRole,
179 qVariantFromValue(data) );
180 subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
181 current_item->addChild( subcat_item );
186 /* Build the tree of plugins */
187 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
189 int i_subcategory = -1, i_category = -1, i_options = 0;
190 p_module = (module_t *)p_list->p_values[i_index].p_object;
192 // Main module excluded
193 if( !strcmp( p_module->psz_object_name, "main" ) ) continue;
195 /* Exclude empty plugins (submodules don't have config options, they
196 * are stored in the parent module) */
197 if( p_module->b_submodule ) continue;
199 for (size_t i = 0; i < p_module->confsize; i++)
201 module_config_t *p_item = p_module->p_config + i;
203 if( p_item->i_type == CONFIG_CATEGORY )
204 i_category = p_item->value.i;
205 else if( p_item->i_type == CONFIG_SUBCATEGORY )
206 i_subcategory = p_item->value.i;
207 if( p_item->i_type & CONFIG_ITEM )
210 if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
213 if( !i_options ) continue; // Nothing to display
215 // Locate the category item;
216 QTreeWidgetItem *subcat_item = NULL;
217 bool b_found = false;
218 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
221 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
222 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
223 value<PrefsItemData *>();
224 if( data->i_object_id == i_category )
226 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
229 subcat_item = cat_item->child( i_sc_index );
230 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
231 value<PrefsItemData *>();
232 if( sc_data && sc_data->i_object_id == i_subcategory )
240 subcat_item = cat_item;
246 if( !b_found ) continue;
248 PrefsItemData *module_data = new PrefsItemData();
249 module_data->b_submodule = p_module->b_submodule;
250 module_data->i_type = TYPE_MODULE;
251 module_data->psz_name = strdup( p_module->psz_object_name );
252 module_data->i_object_id = p_module->b_submodule ?
253 ((module_t *)p_module->p_parent)->i_object_id :
254 p_module->i_object_id;
255 module_data->help.clear();
257 QTreeWidgetItem *module_item = new QTreeWidgetItem();
258 module_item->setText( 0, qfu( p_module->psz_shortname ?
259 p_module->psz_shortname : p_module->psz_object_name) );
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 PrefsPanel::PrefsPanel( QWidget *_parent ) : QWidget( _parent )
336 PrefsPanel::PrefsPanel( 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_t *) vlc_object_get( p_intf, data->i_object_id );
348 p_module = config_FindModule( VLC_OBJECT(p_intf), "main" );
350 vlc_object_yield( p_module );
353 module_t *p_realmodule = p_module->b_submodule
354 ? (module_t *)(p_module->p_parent)
357 module_config_t *p_item = p_realmodule->p_config;
358 module_config_t *p_end = p_item + p_realmodule->confsize;
360 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
362 while (p_item < p_end)
364 if( p_item->i_type == CONFIG_SUBCATEGORY &&
365 ( data->i_type == TYPE_SUBCATEGORY &&
366 p_item->value.i == data->i_object_id ) ||
367 ( data->i_type == TYPE_CATSUBCAT &&
368 p_item->value.i == data->i_subcat_id ) )
374 global_layout = new QVBoxLayout();
376 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
378 head = QString( data->name );
379 p_item++; // Why that ?
383 head = QString( qfu(p_module->psz_longname) );
384 if( p_module->psz_help )
387 head.append( qfu( p_module->psz_help ) );
391 QLabel *label = new QLabel( head );
392 global_layout->addWidget( label );
393 QFont myFont = QApplication::font( static_cast<QWidget*>(0) );
394 myFont.setPointSize( myFont.pointSize() + 3 ); myFont.setBold( true );
396 label->setFont( myFont );
397 QLabel *help = new QLabel( data->help, this );
398 help->setWordWrap( true );
400 global_layout->addWidget( help );
402 QGroupBox *box = NULL;
403 QGridLayout *boxlayout = NULL;
405 QScrollArea *scroller= new QScrollArea;
406 scroller->setFrameStyle( QFrame::NoFrame );
407 QWidget *scrolled_area = new QWidget;
409 QGridLayout *layout = new QGridLayout();
410 int i_line = 0, i_boxline = 0;
411 bool has_hotkey = false;
415 if( ( ( data->i_type == TYPE_SUBCATEGORY &&
416 p_item->value.i != data->i_object_id ) ||
417 ( data->i_type == TYPE_CATSUBCAT &&
418 p_item->value.i != data->i_subcat_id ) ) &&
419 ( p_item->i_type == CONFIG_CATEGORY ||
420 p_item->i_type == CONFIG_SUBCATEGORY ) )
422 if( p_item->b_internal == VLC_TRUE ) continue;
424 if( p_item->i_type == CONFIG_SECTION )
428 box->setLayout( boxlayout );
429 layout->addWidget( box, i_line, 0, 1, 2 );
432 box = new QGroupBox( qfu(p_item->psz_text) );
433 boxlayout = new QGridLayout();
435 /* Only one hotkey control */
436 if( has_hotkey && p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
437 strstr( p_item->psz_name, "key-" ) )
439 if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
440 strstr( p_item->psz_name, "key-" ) )
443 ConfigControl *control;
445 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
446 p_item, NULL, layout, i_line );
448 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
449 p_item, NULL, boxlayout, i_boxline );
455 /* A hotkey widget takes 2 lines */
456 if( box ) i_boxline ++;
460 if( box ) i_boxline++;
462 controls.append( control );
464 while( !( ( data->i_type == TYPE_SUBCATEGORY ||
465 data->i_type == TYPE_CATSUBCAT ) &&
466 ( p_item->i_type == CONFIG_CATEGORY ||
467 p_item->i_type == CONFIG_SUBCATEGORY ) )
468 && ( ++p_item < p_end ) );
472 box->setLayout( boxlayout );
473 layout->addWidget( box, i_line, 0, 1, 2 );
476 vlc_object_release( p_module );
478 scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
479 scrolled_area->setLayout( layout );
480 scroller->setWidget( scrolled_area );
481 scroller->setWidgetResizable( true );
482 global_layout->addWidget( scroller );
483 setLayout( global_layout );
486 void PrefsPanel::apply()
488 QList<ConfigControl *>::Iterator i;
489 for( i = controls.begin() ; i != controls.end() ; i++ )
491 ConfigControl *c = qobject_cast<ConfigControl *>(*i);
492 c->doApply( p_intf );
495 void PrefsPanel::clean()