1 /*****************************************************************************
2 * complete_preferences.cpp : "Normal preferences"
3 ****************************************************************************
4 * Copyright (C) 2006-2011 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>
45 #include <vlc_plugin.h>
48 #define ITEM_HEIGHT 25
50 /*********************************************************************
52 *********************************************************************/
53 PrefsTree::PrefsTree( intf_thread_t *_p_intf, QWidget *_parent ) :
54 QTreeWidget( _parent ), p_intf( _p_intf )
56 /* General Qt options */
57 setAlternatingRowColors( true );
58 setHeaderHidden( true );
60 setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
61 setTextElideMode( Qt::ElideNone );
63 setUniformRowHeights( true );
64 CONNECT( this, itemExpanded(QTreeWidgetItem*), this, resizeColumns() );
67 #define BI( a,b) QIcon a##_icon = QIcon( b )
68 BI( audio, ":/prefsmenu/advanced/audio" );
69 BI( video, ":/prefsmenu/advanced/video" );
70 BI( input, ":/prefsmenu/advanced/codec" );
71 BI( sout, ":/prefsmenu/advanced/sout" );
72 BI( advanced, ":/prefsmenu/advanced/extended" );
73 BI( playlist, ":/prefsmenu/advanced/playlist" );
74 BI( interface, ":/prefsmenu/advanced/intf" );
77 /* Build the tree for the main module */
78 module_t *p_module = module_get_main();
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_config_get (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 );
135 expandItem( current_item );
138 /* This is a subcategory */
139 case CONFIG_SUBCATEGORY:
140 if( p_item->value.i == -1 ) break;
142 /* Special cases: move the main subcategories to the parent cat*/
144 ( p_item->value.i == SUBCAT_VIDEO_GENERAL ||
145 p_item->value.i == SUBCAT_ADVANCED_MISC ||
146 p_item->value.i == SUBCAT_INPUT_GENERAL ||
147 p_item->value.i == SUBCAT_INTERFACE_GENERAL ||
148 p_item->value.i == SUBCAT_SOUT_GENERAL||
149 p_item->value.i == SUBCAT_PLAYLIST_GENERAL||
150 p_item->value.i == SUBCAT_AUDIO_GENERAL ) )
152 /* Data still contains the correct thing */
153 data->i_type = TYPE_CATSUBCAT;
154 data->i_subcat_id = p_item->value.i;
155 data->name = qtr( config_CategoryNameGet( p_item->value.i ) );
156 psz_help = config_CategoryHelpGet( p_item->value.i );
158 data->help = qtr( psz_help );
161 current_item->setData( 0, Qt::UserRole,
162 QVariant::fromValue( data ) );
166 /* Normal Subcategories */
168 /* Process the Data */
169 data_sub = new PrefsItemData();
170 data_sub->name = qtr( config_CategoryNameGet( p_item->value.i) );
171 psz_help = config_CategoryHelpGet( p_item->value.i );
173 data_sub->help = qtr( psz_help );
175 data_sub->help.clear();
176 data_sub->i_type = TYPE_SUBCATEGORY;
177 data_sub->i_object_id = p_item->value.i;
179 /* Create a new TreeWidget */
180 QTreeWidgetItem *subcat_item = new QTreeWidgetItem();
181 subcat_item->setText( 0, data_sub->name );
182 subcat_item->setData( 0, Qt::UserRole,
183 qVariantFromValue( data_sub ) );
184 //subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
186 /* Add it to the parent */
187 assert( current_item );
188 current_item->addChild( subcat_item );
191 /* Other items don't need yet a place on the tree */
194 module_config_free( p_config );
197 module_t **p_list = module_list_get( NULL );
198 /* Build the tree of plugins */
199 for( size_t i = 0; (p_module = p_list[i]) != NULL; i++ )
201 // Main module excluded
202 if( module_is_main( p_module) ) continue;
205 int i_subcategory = 0, i_category = 0;
207 bool b_options = false;
208 module_config_t *const p_config = module_config_get (p_module, &confsize);
210 /* Loop through the configurations items */
211 for (size_t i = 0; i < confsize; i++)
213 const module_config_t *p_item = p_config + i;
215 if( p_item->i_type == CONFIG_CATEGORY )
216 i_category = p_item->value.i;
217 else if( p_item->i_type == CONFIG_SUBCATEGORY )
218 i_subcategory = p_item->value.i;
220 if( CONFIG_ITEM(p_item->i_type) )
223 if( b_options && i_category && i_subcategory )
226 module_config_free (p_config);
228 /* Dummy item, please proceed */
229 if( !b_options || i_category == 0 || i_subcategory == 0 ) continue;
232 // Locate the category item;
233 QTreeWidgetItem *subcat_item = NULL;
234 bool b_found = false;
236 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
239 /* Get the treeWidgetItem that correspond to the category */
240 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
241 PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
242 value<PrefsItemData *>();
244 /* If we match the good category */
245 if( data->i_object_id == i_category )
247 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
250 subcat_item = cat_item->child( i_sc_index );
251 PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
252 value<PrefsItemData *>();
253 if( sc_data && sc_data->i_object_id == i_subcategory )
261 subcat_item = cat_item;
267 if( !b_found ) continue;
269 PrefsItemData *module_data = new PrefsItemData();
270 module_data->i_type = TYPE_MODULE;
271 module_data->psz_name = strdup( module_get_object( p_module ) );
272 module_data->name = qtr( module_get_name( p_module, false ) );
273 module_data->help.clear();
274 const char *psz_help = module_get_help( p_module );
276 module_data->help = qtr( psz_help );
278 QTreeWidgetItem *module_item = new QTreeWidgetItem();
279 module_item->setText( 0, module_data->name );
280 module_item->setData( 0, Qt::UserRole,
281 QVariant::fromValue( module_data) );
282 //module_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
283 subcat_item->addChild( module_item );
286 /* We got everything, just sort a bit */
287 sortItems( 0, Qt::AscendingOrder );
289 module_list_free( p_list );
290 resizeColumnToContents( 0 );
293 void PrefsTree::applyAll()
298 void PrefsTree::cleanAll()
303 void PrefsTree::doAll( bool doclean )
305 for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
308 QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
309 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
312 QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
313 for( int i_module = 0 ; i_module < sc_item->childCount();
316 PrefsItemData *data = sc_item->child( i_module )->
317 data( 0, Qt::UserRole).value<PrefsItemData *>();
318 if( data->panel && doclean )
323 else if( data->panel )
324 data->panel->apply();
326 PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
327 value<PrefsItemData *>();
328 if( data->panel && doclean )
333 else if( data->panel )
334 data->panel->apply();
336 PrefsItemData *data = cat_item->data( 0, Qt::UserRole).
337 value<PrefsItemData *>();
338 if( data->panel && doclean )
343 else if( data->panel )
344 data->panel->apply();
348 /* apply filter on tree item and recursively on its sub items
349 * returns whether the item was filtered */
350 bool PrefsTree::filterItems( QTreeWidgetItem *item, const QString &text,
351 Qt::CaseSensitivity cs )
353 bool sub_filtered = true;
355 for( int i = 0; i < item->childCount(); i++ )
357 QTreeWidgetItem *sub_item = item->child( i );
358 if ( !filterItems( sub_item, text, cs ) )
360 /* not all the sub items were filtered */
361 sub_filtered = false;
365 PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData *>();
367 bool filtered = sub_filtered && !data->contains( text, cs );
368 item->setExpanded( !sub_filtered );
369 item->setHidden( filtered );
375 /* collapse item if it's not selected or one of its sub items
376 * returns whether the item was collapsed */
377 bool PrefsTree::collapseUnselectedItems( QTreeWidgetItem *item )
379 bool sub_collapsed = true;
381 for( int i = 0; i < item->childCount(); i++ )
383 QTreeWidgetItem *sub_item = item->child( i );
384 if ( !collapseUnselectedItems( sub_item ) )
386 /* not all the sub items were collapsed */
387 sub_collapsed = false;
391 bool collapsed = sub_collapsed && !item->isSelected();
392 item->setExpanded( !sub_collapsed );
393 item->setHidden( false );
398 /* apply filter on tree */
399 void PrefsTree::filter( const QString &text )
401 bool clear_filter = text.isEmpty();
403 for( int i = 0 ; i < topLevelItemCount(); i++ )
405 QTreeWidgetItem *cat_item = topLevelItem( i );
408 collapseUnselectedItems( cat_item );
412 filterItems( cat_item, text, Qt::CaseInsensitive );
417 void PrefsTree::resizeColumns()
419 resizeColumnToContents( 0 );
422 /* go over the module config items and search text in psz_text
423 * also search the module name and head */
424 bool PrefsItemData::contains( const QString &text, Qt::CaseSensitivity cs )
426 /* Find our module */
427 module_t *p_module = NULL;
428 if( this->i_type == TYPE_CATEGORY )
430 else if( this->i_type == TYPE_MODULE )
431 p_module = module_find( this->psz_name );
434 p_module = module_get_main();
439 module_config_t *const p_config = module_config_get (p_module, &confsize),
441 *p_end = p_config + confsize;
443 if( this->i_type == TYPE_SUBCATEGORY || this->i_type == TYPE_CATSUBCAT )
445 while ( p_item < p_end )
447 if( p_item->i_type == CONFIG_SUBCATEGORY &&
449 ( this->i_type == TYPE_SUBCATEGORY &&
450 p_item->value.i == this->i_object_id )
452 ( this->i_type == TYPE_CATSUBCAT &&
453 p_item->value.i == this->i_subcat_id )
463 if( this->i_type == TYPE_SUBCATEGORY || this->i_type == TYPE_CATSUBCAT )
466 p_item++; // Why that ? +1
470 head = QString( qtr( module_GetLongName( p_module ) ) );
473 if (name.contains( text, cs ) || head.contains( text, cs ) || help.contains( text, cs ))
480 ( this->i_type == TYPE_SUBCATEGORY && p_item->value.i != this->i_object_id )
482 ( this->i_type == TYPE_CATSUBCAT && p_item->value.i != this->i_subcat_id )
484 ( p_item->i_type == CONFIG_CATEGORY || p_item->i_type == CONFIG_SUBCATEGORY )
487 if( p_item->b_internal ) continue;
489 if ( p_item->psz_text && qtr( p_item->psz_text ).contains( text, cs ) )
494 ( this->i_type == TYPE_SUBCATEGORY || this->i_type == TYPE_CATSUBCAT )
496 ( p_item->i_type == CONFIG_CATEGORY || p_item->i_type == CONFIG_SUBCATEGORY )
498 && ( ++p_item < p_end )
504 /*********************************************************************
506 *********************************************************************/
507 AdvPrefsPanel::AdvPrefsPanel( QWidget *_parent ) : QWidget( _parent )
510 AdvPrefsPanel::AdvPrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
511 PrefsItemData * data ) :
512 QWidget( _parent ), p_intf( _p_intf )
514 /* Find our module */
515 module_t *p_module = NULL;
516 if( data->i_type == TYPE_CATEGORY )
518 else if( data->i_type == TYPE_MODULE )
519 p_module = module_find( data->psz_name );
522 p_module = module_get_main();
527 module_config_t *const p_config = module_config_get (p_module, &confsize),
529 *p_end = p_config + confsize;
531 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
533 while (p_item < p_end)
535 if( p_item->i_type == CONFIG_SUBCATEGORY &&
536 ( ( data->i_type == TYPE_SUBCATEGORY &&
537 p_item->value.i == data->i_object_id ) ||
538 ( data->i_type == TYPE_CATSUBCAT &&
539 p_item->value.i == data->i_subcat_id ) ) )
546 global_layout = new QVBoxLayout();
547 global_layout->setMargin( 2 );
551 help = QString( data->help );
553 if( data->i_type == TYPE_SUBCATEGORY || data->i_type == TYPE_CATSUBCAT )
555 head = QString( data->name );
556 p_item++; // Why that ?
560 head = QString( qtr( module_GetLongName( p_module ) ) );
563 QLabel *titleLabel = new QLabel( head );
564 QFont titleFont = QApplication::font();
565 titleFont.setPointSize( titleFont.pointSize() + 6 );
566 titleLabel->setFont( titleFont );
569 QFrame *title_line = new QFrame;
570 title_line->setFrameShape(QFrame::HLine);
571 title_line->setFrameShadow(QFrame::Sunken);
573 QLabel *helpLabel = new QLabel( help, this );
574 helpLabel->setWordWrap( true );
576 global_layout->addWidget( titleLabel );
577 global_layout->addWidget( title_line );
578 global_layout->addWidget( helpLabel );
580 QGroupBox *box = NULL;
581 QGridLayout *boxlayout = NULL;
583 QScrollArea *scroller= new QScrollArea;
584 scroller->setFrameStyle( QFrame::NoFrame );
585 QWidget *scrolled_area = new QWidget;
587 QGridLayout *layout = new QGridLayout();
588 int i_line = 0, i_boxline = 0;
589 bool has_hotkey = false;
593 if( ( ( data->i_type == TYPE_SUBCATEGORY &&
594 p_item->value.i != data->i_object_id ) ||
595 ( data->i_type == TYPE_CATSUBCAT &&
596 p_item->value.i != data->i_subcat_id ) ) &&
597 ( p_item->i_type == CONFIG_CATEGORY ||
598 p_item->i_type == CONFIG_SUBCATEGORY ) )
600 if( p_item->b_internal ) continue;
602 if( p_item->i_type == CONFIG_SECTION )
606 box->setLayout( boxlayout );
608 layout->addWidget( box, i_line, 0, 1, -1 );
611 box = new QGroupBox( qtr( p_item->psz_text ), this );
613 boxlayout = new QGridLayout();
615 /* Only one hotkey control */
616 if( p_item->i_type == CONFIG_ITEM_KEY )
623 ConfigControl *control;
625 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
626 p_item, this, layout, i_line );
628 control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
629 p_item, this, boxlayout, i_boxline );
633 if( box ) i_boxline++;
635 controls.append( control );
637 while( !( ( data->i_type == TYPE_SUBCATEGORY ||
638 data->i_type == TYPE_CATSUBCAT ) &&
639 ( p_item->i_type == CONFIG_CATEGORY ||
640 p_item->i_type == CONFIG_SUBCATEGORY ) )
641 && ( ++p_item < p_end ) );
643 if( box && i_boxline > 0 )
645 box->setLayout( boxlayout );
647 layout->addWidget( box, i_line, 0, 1, -1 );
650 scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
651 scrolled_area->setLayout( layout );
652 scroller->setWidget( scrolled_area );
653 scroller->setWidgetResizable( true );
654 global_layout->addWidget( scroller );
655 setLayout( global_layout );
658 void AdvPrefsPanel::apply()
660 foreach ( ConfigControl *cfg, controls )
664 void AdvPrefsPanel::clean()
667 AdvPrefsPanel::~AdvPrefsPanel()
669 qDeleteAll( controls ); controls.clear();