]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/complete_preferences.cpp
Remove recursion into modules/control/dbus/ and clean up D-Bus rules
[vlc] / modules / gui / qt4 / components / complete_preferences.cpp
1 /*****************************************************************************
2  * complete_preferences.cpp : "Normal preferences"
3  ****************************************************************************
4  * Copyright (C) 2006-2011 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <QApplication>
28 #include <QLabel>
29 #include <QTreeWidget>
30 #include <QTreeWidgetItem>
31 #include <QVariant>
32 #include <QString>
33 #include <QFont>
34 #include <QGroupBox>
35 #include <QScrollArea>
36 #include <QVBoxLayout>
37 #include <QGridLayout>
38
39 #include "components/complete_preferences.hpp"
40 #include "components/preferences_widgets.hpp"
41
42 #include <vlc_config_cat.h>
43 #include <vlc_intf_strings.h>
44 #include <vlc_modules.h>
45 #include <vlc_plugin.h>
46 #include <assert.h>
47
48 #define ITEM_HEIGHT 25
49
50 /*********************************************************************
51  * The Tree
52  *********************************************************************/
53 PrefsTree::PrefsTree( intf_thread_t *_p_intf, QWidget *_parent ) :
54                             QTreeWidget( _parent ), p_intf( _p_intf )
55 {
56     /* General Qt options */
57     setAlternatingRowColors( true );
58     setHeaderHidden( true );
59
60     setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
61     setTextElideMode( Qt::ElideNone );
62
63     setUniformRowHeights( true );
64     CONNECT( this, itemExpanded(QTreeWidgetItem*), this, resizeColumns() );
65
66     /* Nice icons */
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" );
75 #undef BI
76
77     /* Build the tree for the main module */
78     module_t *p_module = module_get_main();
79
80     /* Initialisation and get the confsize */
81     PrefsItemData *data = NULL;
82     PrefsItemData *data_sub = NULL;
83     QTreeWidgetItem *current_item = NULL;
84     unsigned confsize;
85     module_config_t *const p_config = module_config_get (p_module, &confsize);
86
87     /* Go through the list of conf */
88     for( size_t i = 0; i < confsize; i++ )
89     {
90         const char *psz_help;
91         QIcon icon;
92
93         /* Work on a new item */
94         module_config_t *p_item = p_config + i;
95
96         switch( p_item->i_type )
97         {
98         /* This is a category */
99         case CONFIG_CATEGORY:
100             if( p_item->value.i == -1 ) break;
101
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 );
106             if( psz_help )
107                 data->help = qtr( psz_help );
108             else
109                 data->help.clear();
110             data->i_type = TYPE_CATEGORY;
111             data->i_object_id = p_item->value.i;
112
113             /* This is a category, put a nice icon */
114             switch( p_item->value.i )
115             {
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 );
124             }
125 #undef CI
126
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 );
136             break;
137
138         /* This is a subcategory */
139         case CONFIG_SUBCATEGORY:
140             if( p_item->value.i == -1 ) break;
141
142             /* Special cases: move the main subcategories to the parent cat*/
143             if( data &&
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 ) )
151             {
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 );
157                 if( psz_help )
158                     data->help = qtr( psz_help );
159                 else
160                     data->help.clear();
161                 current_item->setData( 0, Qt::UserRole,
162                                        QVariant::fromValue( data ) );
163                 continue;
164             }
165
166             /* Normal Subcategories */
167
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 );
172             if( psz_help )
173                 data_sub->help = qtr( psz_help );
174             else
175                 data_sub->help.clear();
176             data_sub->i_type = TYPE_SUBCATEGORY;
177             data_sub->i_object_id = p_item->value.i;
178
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 ) );
185
186             /* Add it to the parent */
187             assert( current_item );
188             current_item->addChild( subcat_item );
189             break;
190
191         /* Other items don't need yet a place on the tree */
192         }
193     }
194     module_config_free( p_config );
195
196
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++ )
200     {
201         // Main module excluded
202         if( module_is_main( p_module) ) continue;
203
204         unsigned  confsize;
205         int i_subcategory = 0, i_category = 0;
206
207         bool b_options = false;
208         module_config_t *const p_config = module_config_get (p_module, &confsize);
209
210         /* Loop through the configurations items */
211         for (size_t i = 0; i < confsize; i++)
212         {
213             const module_config_t *p_item = p_config + i;
214
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;
219
220             if( CONFIG_ITEM(p_item->i_type) )
221                 b_options = true;
222
223             if( b_options && i_category && i_subcategory )
224                 break;
225         }
226         module_config_free (p_config);
227
228         /* Dummy item, please proceed */
229         if( !b_options || i_category == 0 || i_subcategory == 0 ) continue;
230
231
232         // Locate the category item;
233         QTreeWidgetItem *subcat_item = NULL;
234         bool b_found = false;
235
236         for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
237                                    i_cat_index++ )
238         {
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 *>();
243
244             /* If we match the good category */
245             if( data->i_object_id == i_category )
246             {
247                 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
248                          i_sc_index++ )
249                 {
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 )
254                     {
255                         b_found = true;
256                         break;
257                     }
258                 }
259                 if( !b_found )
260                 {
261                     subcat_item = cat_item;
262                     b_found = true;
263                 }
264                 break;
265             }
266         }
267         if( !b_found ) continue;
268
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 );
275         if ( psz_help )
276             module_data->help = qtr( psz_help );
277
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 );
284     }
285
286     /* We got everything, just sort a bit */
287     sortItems( 0, Qt::AscendingOrder );
288
289     module_list_free( p_list );
290     resizeColumnToContents( 0 );
291 }
292
293 void PrefsTree::applyAll()
294 {
295     doAll( false );
296 }
297
298 void PrefsTree::cleanAll()
299 {
300     doAll( true );
301 }
302
303 void PrefsTree::doAll( bool doclean )
304 {
305     for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
306              i_cat_index++ )
307     {
308         QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
309         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
310                  i_sc_index++ )
311         {
312             QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
313             for( int i_module = 0 ; i_module < sc_item->childCount();
314                      i_module++ )
315             {
316                 PrefsItemData *data = sc_item->child( i_module )->
317                                data( 0, Qt::UserRole).value<PrefsItemData *>();
318                 if( data->panel && doclean )
319                 {
320                     delete data->panel;
321                     data->panel = NULL;
322                 }
323                 else if( data->panel )
324                     data->panel->apply();
325             }
326             PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
327                                             value<PrefsItemData *>();
328             if( data->panel && doclean )
329             {
330                 delete data->panel;
331                 data->panel = NULL;
332             }
333             else if( data->panel )
334                 data->panel->apply();
335         }
336         PrefsItemData *data = cat_item->data( 0, Qt::UserRole).
337                                             value<PrefsItemData *>();
338         if( data->panel && doclean )
339         {
340             delete data->panel;
341             data->panel = NULL;
342         }
343         else if( data->panel )
344             data->panel->apply();
345     }
346 }
347
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 )
352 {
353     bool sub_filtered = true;
354
355     for( int i = 0; i < item->childCount(); i++ )
356     {
357         QTreeWidgetItem *sub_item = item->child( i );
358         if ( !filterItems( sub_item, text, cs ) )
359         {
360             /* not all the sub items were filtered */
361             sub_filtered = false;
362         }
363     }
364
365     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData *>();
366
367     bool filtered = sub_filtered && !data->contains( text, cs );
368     item->setExpanded( !sub_filtered );
369     item->setHidden( filtered );
370
371     return filtered;
372 }
373
374
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 )
378 {
379     bool sub_collapsed = true;
380
381     for( int i = 0; i < item->childCount(); i++ )
382     {
383         QTreeWidgetItem *sub_item = item->child( i );
384         if ( !collapseUnselectedItems( sub_item ) )
385         {
386             /* not all the sub items were collapsed */
387             sub_collapsed = false;
388         }
389     }
390
391     bool collapsed = sub_collapsed && !item->isSelected();
392     item->setExpanded( !sub_collapsed );
393     item->setHidden( false );
394
395     return collapsed;
396 }
397
398 /* apply filter on tree */
399 void PrefsTree::filter( const QString &text )
400 {
401     bool clear_filter = text.isEmpty();
402
403     for( int i = 0 ; i < topLevelItemCount(); i++ )
404     {
405         QTreeWidgetItem *cat_item = topLevelItem( i );
406         if ( clear_filter )
407         {
408             collapseUnselectedItems( cat_item );
409         }
410         else
411         {
412             filterItems( cat_item, text, Qt::CaseInsensitive );
413         }
414     }
415 }
416
417 void PrefsTree::resizeColumns()
418 {
419     resizeColumnToContents( 0 );
420 }
421
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 )
425 {
426     /* Find our module */
427     module_t *p_module = NULL;
428     if( this->i_type == TYPE_CATEGORY )
429         return false;
430     else if( this->i_type == TYPE_MODULE )
431         p_module = module_find( this->psz_name );
432     else
433     {
434         p_module = module_get_main();
435         assert( p_module );
436     }
437
438     unsigned confsize;
439     module_config_t *const p_config = module_config_get (p_module, &confsize),
440                     *p_item = p_config,
441                     *p_end = p_config + confsize;
442
443     if( this->i_type == TYPE_SUBCATEGORY || this->i_type ==  TYPE_CATSUBCAT )
444     {
445         while ( p_item < p_end )
446         {
447             if( p_item->i_type == CONFIG_SUBCATEGORY &&
448                 (
449                     ( this->i_type == TYPE_SUBCATEGORY &&
450                               p_item->value.i == this->i_object_id )
451                     ||
452                     ( this->i_type == TYPE_CATSUBCAT &&
453                               p_item->value.i == this->i_subcat_id )
454                 )
455               )
456                 break;
457             p_item++;
458         }
459     }
460
461     QString head;
462
463     if( this->i_type == TYPE_SUBCATEGORY || this->i_type ==  TYPE_CATSUBCAT )
464     {
465         head.clear();
466         p_item++; // Why that ? +1
467     }
468     else
469     {
470         head = QString( qtr( module_GetLongName( p_module ) ) );
471     }
472
473     if (name.contains( text, cs ) || head.contains( text, cs ) || help.contains( text, cs ))
474         return true;
475
476     if( p_item ) do
477     {
478         if (
479             (
480                 ( this->i_type == TYPE_SUBCATEGORY && p_item->value.i != this->i_object_id )
481                 ||
482                 ( this->i_type == TYPE_CATSUBCAT && p_item->value.i != this->i_subcat_id )
483             ) &&
484             ( p_item->i_type == CONFIG_CATEGORY || p_item->i_type == CONFIG_SUBCATEGORY )
485            ) break;
486
487         if( p_item->b_internal ) continue;
488
489         if ( p_item->psz_text && qtr( p_item->psz_text ).contains( text, cs ) )
490             return true;
491     }
492     while (
493             !(
494                 ( this->i_type == TYPE_SUBCATEGORY || this->i_type == TYPE_CATSUBCAT )
495                 &&
496                 ( p_item->i_type == CONFIG_CATEGORY || p_item->i_type == CONFIG_SUBCATEGORY )
497              )
498              && ( ++p_item < p_end )
499           );
500
501     return false;
502 }
503
504 /*********************************************************************
505  * The Panel
506  *********************************************************************/
507 AdvPrefsPanel::AdvPrefsPanel( QWidget *_parent ) : QWidget( _parent )
508 {}
509
510 AdvPrefsPanel::AdvPrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
511                         PrefsItemData * data ) :
512                         QWidget( _parent ), p_intf( _p_intf )
513 {
514     /* Find our module */
515     module_t *p_module = NULL;
516     if( data->i_type == TYPE_CATEGORY )
517         return;
518     else if( data->i_type == TYPE_MODULE )
519         p_module = module_find( data->psz_name );
520     else
521     {
522         p_module = module_get_main();
523         assert( p_module );
524     }
525
526     unsigned confsize;
527     module_config_t *const p_config = module_config_get (p_module, &confsize),
528                     *p_item = p_config,
529                     *p_end = p_config + confsize;
530
531     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
532     {
533         while (p_item < p_end)
534         {
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 ) ) )
540                 break;
541             p_item++;
542         }
543     }
544
545     /* Widgets now */
546     global_layout = new QVBoxLayout();
547     global_layout->setMargin( 2 );
548     QString head;
549     QString help;
550
551     help = QString( data->help );
552
553     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
554     {
555         head = QString( data->name );
556         p_item++; // Why that ?
557     }
558     else
559     {
560         head = QString( qtr( module_GetLongName( p_module ) ) );
561     }
562
563     QLabel *titleLabel = new QLabel( head );
564     QFont titleFont = QApplication::font();
565     titleFont.setPointSize( titleFont.pointSize() + 6 );
566     titleLabel->setFont( titleFont );
567
568     // Title <hr>
569     QFrame *title_line = new QFrame;
570     title_line->setFrameShape(QFrame::HLine);
571     title_line->setFrameShadow(QFrame::Sunken);
572
573     QLabel *helpLabel = new QLabel( help, this );
574     helpLabel->setWordWrap( true );
575
576     global_layout->addWidget( titleLabel );
577     global_layout->addWidget( title_line );
578     global_layout->addWidget( helpLabel );
579
580     QGroupBox *box = NULL;
581     QGridLayout *boxlayout = NULL;
582
583     QScrollArea *scroller= new QScrollArea;
584     scroller->setFrameStyle( QFrame::NoFrame );
585     QWidget *scrolled_area = new QWidget;
586
587     QGridLayout *layout = new QGridLayout();
588     int i_line = 0, i_boxline = 0;
589     bool has_hotkey = false;
590
591     if( p_item ) do
592     {
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 ) )
599             break;
600         if( p_item->b_internal ) continue;
601
602         if( p_item->i_type == CONFIG_SECTION )
603         {
604             if( box )
605             {
606                 box->setLayout( boxlayout );
607                 box->show();
608                 layout->addWidget( box, i_line, 0, 1, -1 );
609                 i_line++;
610             }
611             box = new QGroupBox( qtr( p_item->psz_text ), this );
612             box->hide();
613             boxlayout = new QGridLayout();
614         }
615         /* Only one hotkey control */
616         if( p_item->i_type == CONFIG_ITEM_KEY )
617         {
618             if( has_hotkey )
619                 continue;
620             has_hotkey = true;
621         }
622
623         ConfigControl *control;
624         if( ! box )
625             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
626                                         p_item, this, layout, i_line );
627         else
628             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
629                                     p_item, this, boxlayout, i_boxline );
630         if( !control )
631             continue;
632
633         if( box ) i_boxline++;
634         else i_line++;
635         controls.append( control );
636     }
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 ) );
642
643     if( box && i_boxline > 0 )
644     {
645         box->setLayout( boxlayout );
646         box->show();
647         layout->addWidget( box, i_line, 0, 1, -1 );
648     }
649
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 );
656 }
657
658 void AdvPrefsPanel::apply()
659 {
660     foreach ( ConfigControl *cfg, controls )
661         cfg->doApply();
662 }
663
664 void AdvPrefsPanel::clean()
665 {}
666
667 AdvPrefsPanel::~AdvPrefsPanel()
668 {
669     qDeleteAll( controls ); controls.clear();
670 }
671