]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/complete_preferences.cpp
b34a14ef0d2842c133f5db7a72afc5774d3cade0
[vlc] / modules / gui / qt4 / components / complete_preferences.cpp
1 /*****************************************************************************
2  * preferences.cpp : "Normal preferences"
3  ****************************************************************************
4  * Copyright (C) 2006-2007 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 <QHBoxLayout>
38 #include <QGridLayout>
39 #include <QHeaderView>
40 #include <QPalette>
41 #include <QColor>
42
43 #include "components/complete_preferences.hpp"
44 #include "components/preferences_widgets.hpp"
45
46 #include <vlc_config_cat.h>
47 #include <vlc_intf_strings.h>
48 #include <assert.h>
49
50 #define ITEM_HEIGHT 25
51
52 /*********************************************************************
53  * The Tree
54  *********************************************************************/
55 PrefsTree::PrefsTree( intf_thread_t *_p_intf, QWidget *_parent ) :
56                             QTreeWidget( _parent ), p_intf( _p_intf )
57 {
58     /* General Qt options */
59     setColumnCount( 1 );
60     setAlternatingRowColors( true );
61     header()->hide();
62
63     setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
64     setTextElideMode( Qt::ElideNone );
65
66     /* Nice icons */
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" );
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             break;
136
137         /* This is a subcategory */
138         case CONFIG_SUBCATEGORY:
139             if( p_item->value.i == -1 ) break;
140
141             /* Special cases: move the main subcategories to the parent cat*/
142             if( data &&
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 ) )
150             {
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 );
156                 if( psz_help )
157                     data->help = qtr( psz_help );
158                 else
159                     data->help.clear();
160                 current_item->setData( 0, Qt::UserRole,
161                                        QVariant::fromValue( data ) );
162                 continue;
163             }
164
165             /* Normal Subcategories */
166
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 );
171             if( psz_help )
172                 data_sub->help = qtr( psz_help );
173             else
174                 data_sub->help.clear();
175             data_sub->i_type = TYPE_SUBCATEGORY;
176             data_sub->i_object_id = p_item->value.i;
177
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 ) );
184
185             /* Add it to the parent */
186             assert( current_item );
187             current_item->addChild( subcat_item );
188             break;
189
190         /* Other items don't need yet a place on the tree */
191         }
192     }
193     module_config_free( p_config );
194     module_release( p_module );
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 i_subcategory = 0, i_category = 0, confsize;
205         bool b_options = false;
206         module_config_t *const p_config = module_config_get (p_module, &confsize);
207
208         /* Loop through the configurations items */
209         for (size_t i = 0; i < confsize; i++)
210         {
211             const module_config_t *p_item = p_config + i;
212
213             if( p_item->i_type == CONFIG_CATEGORY )
214                 i_category = p_item->value.i;
215             else if( p_item->i_type == CONFIG_SUBCATEGORY )
216                 i_subcategory = p_item->value.i;
217
218             if( p_item->i_type & CONFIG_ITEM )
219                 b_options = true;
220
221             if( b_options && i_category && i_subcategory )
222                 break;
223         }
224         module_config_free (p_config);
225
226         /* Dummy item, please proceed */
227         if( !b_options || i_category == 0 || i_subcategory == 0 ) continue;
228
229
230         // Locate the category item;
231         QTreeWidgetItem *subcat_item = NULL;
232         bool b_found = false;
233
234         for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
235                                    i_cat_index++ )
236         {
237             /* Get the treeWidgetItem that correspond to the category */
238             QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
239             PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
240                                              value<PrefsItemData *>();
241
242             /* If we match the good category */
243             if( data->i_object_id == i_category )
244             {
245                 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
246                          i_sc_index++ )
247                 {
248                     subcat_item = cat_item->child( i_sc_index );
249                     PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
250                                                 value<PrefsItemData *>();
251                     if( sc_data && sc_data->i_object_id == i_subcategory )
252                     {
253                         b_found = true;
254                         break;
255                     }
256                 }
257                 if( !b_found )
258                 {
259                     subcat_item = cat_item;
260                     b_found = true;
261                 }
262                 break;
263             }
264         }
265         if( !b_found ) continue;
266
267         PrefsItemData *module_data = new PrefsItemData();
268         module_data->i_type = TYPE_MODULE;
269         module_data->psz_name = strdup( module_get_object( p_module ) );
270         module_data->help.clear();
271         QTreeWidgetItem *module_item = new QTreeWidgetItem();
272         module_item->setText( 0, qtr( module_get_name( p_module, false ) ) );
273         module_item->setData( 0, Qt::UserRole,
274                               QVariant::fromValue( module_data) );
275         module_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
276         subcat_item->addChild( module_item );
277     }
278
279     /* We got everything, just sort a bit */
280     sortItems( 0, Qt::AscendingOrder );
281
282     module_list_free( p_list );
283 }
284
285 PrefsTree::~PrefsTree() {}
286
287 void PrefsTree::applyAll()
288 {
289     doAll( false );
290 }
291
292 void PrefsTree::cleanAll()
293 {
294     doAll( true );
295 }
296
297 void PrefsTree::doAll( bool doclean )
298 {
299     for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
300              i_cat_index++ )
301     {
302         QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
303         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
304                  i_sc_index++ )
305         {
306             QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
307             for( int i_module = 0 ; i_module < sc_item->childCount();
308                      i_module++ )
309             {
310                 PrefsItemData *data = sc_item->child( i_module )->
311                                data( 0, Qt::UserRole).value<PrefsItemData *>();
312                 if( data->panel && doclean )
313                 {
314                     delete data->panel;
315                     data->panel = NULL;
316                 }
317                 else if( data->panel )
318                     data->panel->apply();
319             }
320             PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
321                                             value<PrefsItemData *>();
322             if( data->panel && doclean )
323             {
324                 delete data->panel;
325                 data->panel = NULL;
326             }
327             else if( data->panel )
328                 data->panel->apply();
329         }
330         PrefsItemData *data = cat_item->data( 0, Qt::UserRole).
331                                             value<PrefsItemData *>();
332         if( data->panel && doclean )
333         {
334             delete data->panel;
335             data->panel = NULL;
336         }
337         else if( data->panel )
338             data->panel->apply();
339     }
340 }
341
342 /*********************************************************************
343  * The Panel
344  *********************************************************************/
345 AdvPrefsPanel::AdvPrefsPanel( QWidget *_parent ) : QWidget( _parent )
346 {}
347
348 AdvPrefsPanel::AdvPrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
349                         PrefsItemData * data ) :
350                         QWidget( _parent ), p_intf( _p_intf )
351 {
352     /* Find our module */
353     module_t *p_module = NULL;
354     if( data->i_type == TYPE_CATEGORY )
355         return;
356     else if( data->i_type == TYPE_MODULE )
357         p_module = module_find( data->psz_name );
358     else
359     {
360         p_module = module_get_main();
361         assert( p_module );
362     }
363
364     unsigned confsize;
365     module_config_t *const p_config = module_config_get (p_module, &confsize),
366                     *p_item = p_config,
367                     *p_end = p_config + confsize;
368
369     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
370     {
371         while (p_item < p_end)
372         {
373             if( p_item->i_type == CONFIG_SUBCATEGORY &&
374                             ( data->i_type == TYPE_SUBCATEGORY &&
375                               p_item->value.i == data->i_object_id ) ||
376                             ( data->i_type == TYPE_CATSUBCAT &&
377                               p_item->value.i == data->i_subcat_id ) )
378                 break;
379             p_item++;
380         }
381     }
382
383     /* Widgets now */
384     global_layout = new QVBoxLayout();
385     global_layout->setMargin( 2 );
386     QString head;
387     QString help;
388
389     help = QString( data->help );
390
391     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
392     {
393         head = QString( data->name );
394         p_item++; // Why that ?
395     }
396     else
397     {
398         const char *psz_help = module_get_help (p_module);
399         head = QString( qtr( module_GetLongName( p_module ) ) );
400         if( psz_help )
401         {
402             help.append( "\n" );
403             help.append( qtr( psz_help ) );
404         }
405     }
406
407     QLabel *titleLabel = new QLabel( head );
408     QFont titleFont = QApplication::font( static_cast<QWidget*>(0) );
409     titleFont.setPointSize( titleFont.pointSize() + 6 );
410     titleFont.setFamily( "Verdana" );
411     titleLabel->setFont( titleFont );
412
413     // Title <hr>
414     QFrame *title_line = new QFrame;
415     title_line->setFrameShape(QFrame::HLine);
416     title_line->setFrameShadow(QFrame::Sunken);
417
418     QLabel *helpLabel = new QLabel( help, this );
419     helpLabel->setWordWrap( true );
420
421     global_layout->addWidget( titleLabel );
422     global_layout->addWidget( title_line );
423     global_layout->addWidget( helpLabel );
424
425     QGroupBox *box = NULL;
426     QGridLayout *boxlayout = NULL;
427
428     QScrollArea *scroller= new QScrollArea;
429     scroller->setFrameStyle( QFrame::NoFrame );
430     QWidget *scrolled_area = new QWidget;
431
432     QGridLayout *layout = new QGridLayout();
433     int i_line = 0, i_boxline = 0;
434     bool has_hotkey = false;
435
436     if( p_item ) do
437     {
438         if( ( ( data->i_type == TYPE_SUBCATEGORY &&
439                 p_item->value.i != data->i_object_id ) ||
440               ( data->i_type == TYPE_CATSUBCAT  &&
441                 p_item->value.i != data->i_subcat_id ) ) &&
442             ( p_item->i_type == CONFIG_CATEGORY ||
443               p_item->i_type == CONFIG_SUBCATEGORY ) )
444             break;
445         if( p_item->b_internal == true ) continue;
446
447         if( p_item->i_type == CONFIG_SECTION )
448         {
449             if( box )
450             {
451                 box->setLayout( boxlayout );
452                 box->show();
453                 layout->addWidget( box, i_line, 0, 1, -1 );
454                 i_line++;
455             }
456             box = new QGroupBox( qtr( p_item->psz_text ), this );
457             box->hide();
458             boxlayout = new QGridLayout();
459         }
460         /* Only one hotkey control */
461         if( has_hotkey && p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
462                                          strstr( p_item->psz_name, "key-" ) )
463             continue;
464         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
465                                             strstr( p_item->psz_name, "key-" ) )
466             has_hotkey = true;
467
468         ConfigControl *control;
469         if( ! box )
470             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
471                                         p_item, this, layout, i_line );
472         else
473             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
474                                     p_item, this, boxlayout, i_boxline );
475         if( !control )
476             continue;
477
478         if( box ) i_boxline++;
479         else i_line++;
480         controls.append( control );
481     }
482     while( !( ( data->i_type == TYPE_SUBCATEGORY ||
483                data->i_type == TYPE_CATSUBCAT ) &&
484              ( p_item->i_type == CONFIG_CATEGORY ||
485                p_item->i_type == CONFIG_SUBCATEGORY ) )
486         && ( ++p_item < p_end ) );
487
488     if( box )
489     {
490         box->setLayout( boxlayout );
491         box->show();
492         layout->addWidget( box, i_line, 0, 1, -1 );
493     }
494
495     module_release (p_module);
496
497     scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
498     scrolled_area->setLayout( layout );
499     scroller->setWidget( scrolled_area );
500     scroller->setWidgetResizable( true );
501     global_layout->addWidget( scroller );
502     setLayout( global_layout );
503 }
504
505 void AdvPrefsPanel::apply()
506 {
507     QList<ConfigControl *>::Iterator i;
508     for( i = controls.begin() ; i != controls.end() ; i++ )
509     {
510         ConfigControl *c = qobject_cast<ConfigControl *>(*i);
511         c->doApply( p_intf );
512     }
513 }
514
515 void AdvPrefsPanel::clean()
516 {}
517
518 AdvPrefsPanel::~AdvPrefsPanel()
519 {
520     qDeleteAll( controls ); controls.clear();
521 }
522