]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/preferences.cpp
7792bfc8ec1b6cbd60371f0fa359ca7f40197d8a
[vlc] / modules / gui / qt4 / components / preferences.cpp
1 /*****************************************************************************
2  * preferences.cpp : "Normal preferences"
3  ****************************************************************************
4  * Copyright (C) 2006 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
24 #include <QApplication>
25 #include <QLabel>
26 #include <QTreeWidget>
27 #include <QTreeWidgetItem>
28 #include <QVariant>
29 #include <QString>
30 #include <QFont>
31 #include <QGroupBox>
32 #include <QScrollArea>
33 #include <QVBoxLayout>
34 #include <QHBoxLayout>
35 #include <QGridLayout>
36 #include <QHeaderView>
37 #include <QPalette>
38 #include <QColor>
39
40 #include "components/preferences.hpp"
41 #include "components/preferences_widgets.hpp"
42 #include "qt4.hpp"
43
44 #include <vlc_config_cat.h>
45 #include <vlc_intf_strings.h>
46 #include <assert.h>
47
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"
55
56 #define ITEM_HEIGHT 25
57
58 /*********************************************************************
59  * The Tree
60  *********************************************************************/
61 PrefsTree::PrefsTree( intf_thread_t *_p_intf, QWidget *_parent ) :
62                             QTreeWidget( _parent ), p_intf( _p_intf )
63 {
64     setColumnCount( 1 );
65     setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
66     setAlternatingRowColors( true );
67     header()->hide();
68
69 #define BI( a,b) QIcon a##_icon = QIcon( QPixmap( b##_xpm ))
70     BI( audio, audio );
71     BI( video, video );
72     BI( input, codec );
73     BI( sout, type_net );
74     BI( advanced, advanced );
75     BI( playlist, type_playlist );
76     BI( interface, intf );
77 #undef BI
78
79     /* Build the tree for the main module */
80     int i_index;
81     module_t *p_module;
82     vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
83                                         FIND_ANYWHERE );
84     bool found = true;
85     if( !p_list ) return;
86     for( i_index = 0; i_index < p_list->i_count; i_index++ )
87     {
88         p_module = (module_t *)p_list->p_values[i_index].p_object;
89         if( !strcmp( p_module->psz_object_name, "main" ) ) {
90             found = true; break;
91         }
92     }
93     assert( found );
94
95     module_config_t *p_item = p_module->p_config;
96     PrefsItemData *data = NULL;
97     QTreeWidgetItem *current_item = NULL;
98     if( p_item ) do
99     {
100         char *psz_help;
101         QIcon icon;
102         switch( p_item->i_type )
103         {
104         case CONFIG_CATEGORY:
105             if( p_item->i_value == -1 ) break;
106             data = new PrefsItemData();
107             data->name = QString( qfu( config_CategoryNameGet
108                                            ( p_item->i_value ) ) );
109             psz_help = config_CategoryHelpGet( p_item->i_value );
110             if( psz_help )
111                 data->help = QString( qfu(psz_help) );
112             else
113                 data->help.clear();
114             data->i_type = TYPE_CATEGORY;
115             data->i_object_id = p_item->i_value;
116
117             switch( p_item->i_value )
118             {
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 );
127 #undef CI
128             }
129
130             current_item = new QTreeWidgetItem();
131             current_item->setText( 0, data->name );
132             current_item->setIcon( 0 , icon );
133             current_item->setData( 0, Qt::UserRole,
134                                    qVariantFromValue( data ) );
135             addTopLevelItem( current_item );
136             break;
137         case CONFIG_SUBCATEGORY:
138             if( p_item->i_value == -1 ) break;
139             if( data &&
140                 ( p_item->i_value == SUBCAT_VIDEO_GENERAL ||
141                   p_item->i_value == SUBCAT_ADVANCED_MISC ||
142                   p_item->i_value == SUBCAT_INPUT_GENERAL ||
143                   p_item->i_value == SUBCAT_INTERFACE_GENERAL ||
144                   p_item->i_value == SUBCAT_SOUT_GENERAL||
145                   p_item->i_value == SUBCAT_PLAYLIST_GENERAL||
146                   p_item->i_value == SUBCAT_AUDIO_GENERAL ) )
147             {
148                 // Data still contains the correct thing
149                 data->i_type = TYPE_CATSUBCAT;
150                 data->i_subcat_id = p_item->i_value;
151                 data->name = QString( qfu( config_CategoryNameGet(
152                                             p_item->i_value )) );
153                 psz_help = config_CategoryHelpGet( p_item->i_value );
154                 if( psz_help )
155                     data->help = QString( qfu(psz_help) );
156                 else
157                     data->help.clear();
158                 current_item->setData( 0, Qt::UserRole,
159                                        QVariant::fromValue( data ) );
160                 continue;
161             }
162             data = new PrefsItemData();
163             data->name = QString( qfu( config_CategoryNameGet(
164                                                         p_item->i_value)) );
165             psz_help = config_CategoryHelpGet( p_item->i_value );
166             if( psz_help )
167                 data->help = QString( qfu(psz_help) );
168             else
169                 data->help.clear();
170             data->i_type = TYPE_SUBCATEGORY;
171             data->i_object_id = p_item->i_value;
172
173             assert( current_item );
174
175             /* TODO : Choose the image */
176             QTreeWidgetItem *subcat_item = new QTreeWidgetItem();
177             subcat_item->setText( 0, data->name );
178             //item->setIcon( 0 , XXX );
179             subcat_item->setData( 0, Qt::UserRole,
180                                   qVariantFromValue(data) );
181             subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
182             current_item->addChild( subcat_item );
183             break;
184         }
185     } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
186
187     /* Build the tree of plugins */
188     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
189     {
190         module_config_t *p_item;
191         int i_subcategory = -1, i_category = -1, i_options = 0;
192         p_module = (module_t *)p_list->p_values[i_index].p_object;
193
194         // Main module excluded
195         if( !strcmp( p_module->psz_object_name, "main" ) ) continue;
196
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;
200
201         p_item = p_module->p_config;
202         if( !p_item ) continue;
203
204         do {
205             if( p_item->i_type == CONFIG_CATEGORY )
206                 i_category = p_item->i_value;
207             else if( p_item->i_type == CONFIG_SUBCATEGORY )
208                 i_subcategory = p_item->i_value;
209             if( p_item->i_type & CONFIG_ITEM )
210                 i_options++;
211
212             if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
213                 break;
214         } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
215
216         if( !i_options ) continue; // Nothing to display
217
218         // Locate the category item;
219         QTreeWidgetItem *subcat_item = NULL;
220         bool b_found = false;
221         for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
222                                    i_cat_index++ )
223         {
224             QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
225             PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
226                                              value<PrefsItemData *>();
227             if( data->i_object_id == i_category )
228             {
229                 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
230                          i_sc_index++ )
231                 {
232                     subcat_item = cat_item->child( i_sc_index );
233                     PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
234                                                 value<PrefsItemData *>();
235                     if( sc_data && sc_data->i_object_id == i_subcategory )
236                     {
237                         b_found = true;
238                         break;
239                     }
240                 }
241                 if( !b_found )
242                 {
243                     subcat_item = cat_item;
244                     b_found = true;
245                 }
246                 break;
247             }
248         }
249         if( !b_found ) continue;
250
251         PrefsItemData *module_data = new PrefsItemData();
252         module_data->b_submodule = p_module->b_submodule;
253         module_data->i_type = TYPE_MODULE;
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();
258         // TODO image
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 );
267     }
268
269     /* We got everything, just sort a bit */
270     sortItems( 0, Qt::AscendingOrder );
271
272     vlc_list_release( p_list );
273 }
274
275 PrefsTree::~PrefsTree() {}
276
277 void PrefsTree::applyAll()
278 {
279     doAll( false );
280 }
281
282 void PrefsTree::cleanAll()
283 {
284     doAll( true );
285 }
286
287 void PrefsTree::doAll( bool doclean )
288 {
289     for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
290              i_cat_index++ )
291     {
292         QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
293         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
294                  i_sc_index++ )
295         {
296             QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
297             for( int i_module = 0 ; i_module < sc_item->childCount();
298                      i_module++ )
299             {
300                 PrefsItemData *data = sc_item->child( i_module )->
301                                data( 0, Qt::UserRole).value<PrefsItemData *>();
302                 if( data->panel && doclean )
303                 {
304                     delete data->panel;
305                     data->panel = NULL;
306                 }
307                 else if( data->panel )
308                     data->panel->apply();
309             }
310             PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
311                                             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 = cat_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 }
331
332 /*********************************************************************
333  * The Panel
334  *********************************************************************/
335 PrefsPanel::PrefsPanel( QWidget *_parent ) : QWidget( _parent )
336 {}
337
338 PrefsPanel::PrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
339                         PrefsItemData * data ) :
340                         QWidget( _parent ), p_intf( _p_intf )
341 {
342     module_config_t *p_item;
343
344     /* Find our module */
345     module_t *p_module = NULL;
346     if( data->i_type == TYPE_CATEGORY )
347         return;
348     else if( data->i_type == TYPE_MODULE )
349         p_module = (module_t *) vlc_object_get( p_intf, data->i_object_id );
350     else
351     {
352         p_module = config_FindModule( VLC_OBJECT(p_intf), "main" );
353         assert( p_module );
354         vlc_object_yield( p_module );
355     }
356
357     if( p_module->b_submodule )
358         p_item = ((module_t *)p_module->p_parent)->p_config;
359     else
360         p_item = p_module->p_config;
361
362     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
363     {
364         do
365         {
366             if( p_item->i_type == CONFIG_SUBCATEGORY &&
367                             ( data->i_type == TYPE_SUBCATEGORY &&
368                               p_item->i_value == data->i_object_id ) ||
369                             ( data->i_type == TYPE_CATSUBCAT &&
370                               p_item->i_value == data->i_subcat_id ) )
371                 break;
372             if( p_item->i_type == CONFIG_HINT_END ) break;
373         } while( p_item++ );
374     }
375
376     global_layout = new QVBoxLayout();
377     QString head;
378     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
379     {
380         head = QString( data->name );
381         p_item++; // Why that ?
382     }
383     else
384     {
385         head = QString( qfu(p_module->psz_longname) );
386         if( p_module->psz_help )
387         {
388             head.append( "\n" );
389             head.append( qfu( p_module->psz_help ) );
390         }
391     }
392
393     QLabel *label = new QLabel( head );
394     global_layout->addWidget( label );
395     QFont myFont = QApplication::font( static_cast<QWidget*>(0) );
396     myFont.setPointSize( myFont.pointSize() + 3 ); myFont.setBold( true );
397
398     label->setFont( myFont );
399     QLabel *help = new QLabel( data->help, this );
400     help->setWordWrap( true );
401
402     global_layout->addWidget( help );
403
404     QGroupBox *box = NULL;
405     QGridLayout *boxlayout = NULL;
406
407     QScrollArea *scroller= new QScrollArea;
408     scroller->setFrameStyle( QFrame::NoFrame );
409     QWidget *scrolled_area = new QWidget;
410
411     QGridLayout *layout = new QGridLayout();
412     int i_line = 0, i_boxline = 0;
413     bool has_hotkey = false;
414
415     if( p_item ) do
416     {
417         if( ( ( data->i_type == TYPE_SUBCATEGORY &&
418                 p_item->i_value != data->i_object_id ) ||
419               ( data->i_type == TYPE_CATSUBCAT  &&
420                 p_item->i_value != data->i_subcat_id ) ) &&
421             ( p_item->i_type == CONFIG_CATEGORY ||
422               p_item->i_type == CONFIG_SUBCATEGORY ) )
423             break;
424         if( p_item->b_internal == VLC_TRUE ) continue;
425
426         if( p_item->i_type == CONFIG_SECTION )
427         {
428             if( box )
429             {
430                 box->setLayout( boxlayout );
431                 layout->addWidget( box, i_line, 0, 1, 2 );
432                 i_line++;
433             }
434             box = new QGroupBox( qfu(p_item->psz_text) );
435             boxlayout = new QGridLayout();
436         }
437         /* Only one hotkey control */
438         if( has_hotkey && p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
439                                          strstr( p_item->psz_name, "key-" ) )
440             continue;
441         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
442                                             strstr( p_item->psz_name, "key-" ) )
443             has_hotkey = true;
444
445         ConfigControl *control;
446         if( ! box )
447             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
448                                         p_item, NULL, layout, i_line );
449         else
450             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
451                                     p_item, NULL, boxlayout, i_boxline );
452         if( !control )
453             continue;
454
455         if( has_hotkey )
456         {
457             /* A hotkey widget takes 2 lines */
458             if( box ) i_boxline ++;
459             else i_line++;
460         }
461
462         if( box ) i_boxline++;
463         else i_line++;
464         controls.append( control );
465     }
466     while( !(p_item->i_type == CONFIG_HINT_END ||
467            ( ( 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 ) ) ) && p_item++ );
471
472     if( box )
473     {
474         box->setLayout( boxlayout );
475         layout->addWidget( box, i_line, 0, 1, 2 );
476     }
477
478     vlc_object_release( p_module );
479
480     scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
481     scrolled_area->setLayout( layout );
482     scroller->setWidget( scrolled_area );
483     scroller->setWidgetResizable( true );
484     global_layout->addWidget( scroller );
485     setLayout( global_layout );
486 }
487
488 void PrefsPanel::apply()
489 {
490     QList<ConfigControl *>::Iterator i;
491     for( i = controls.begin() ; i != controls.end() ; i++ )
492     {
493         ConfigControl *c = qobject_cast<ConfigControl *>(*i);
494         c->doApply( p_intf );
495     }
496 }
497 void PrefsPanel::clean()
498 {}