162b86f46d495c6b260cc4e7b235f41018f3b017
[vlc.git] / 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 #include "components/preferences.hpp"
24 #include "components/preferences_widgets.hpp"
25 #include "qt4.hpp"
26 #include <vlc_config_cat.h>
27 #include <vlc_intf_strings.h>
28 #include <assert.h>
29
30 #include "pixmaps/audio.xpm"
31 #include "pixmaps/video.xpm"
32 #include "pixmaps/type_net.xpm"
33 #include "pixmaps/type_playlist.xpm"
34 #include "pixmaps/advanced.xpm"
35 #include "pixmaps/codec.xpm"
36 #include "pixmaps/intf.xpm"
37 #include <QApplication>
38 #include <QLabel>
39 #include <QTreeWidget>
40 #include <QTreeWidgetItem>
41 #include <QVariant>
42 #include <QString>
43 #include <QFont>
44 #include <QGroupBox>
45 #include <QScrollArea>
46 #include <QVBoxLayout>
47 #include <QHBoxLayout>
48 #include <QGridLayout>
49 #include <QHeaderView>
50
51 #include <QPalette>
52 #include <QColor>
53
54 #define ITEM_HEIGHT 25
55
56 /*********************************************************************
57  * The Tree
58  *********************************************************************/
59 PrefsTree::PrefsTree( intf_thread_t *_p_intf, QWidget *_parent ) :
60                             QTreeWidget( _parent ), p_intf( _p_intf )
61 {
62     module_t *p_module;
63     vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
64                                         FIND_ANYWHERE );
65     if( !p_list ) return;
66
67     setColumnCount( 1 );
68     setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
69     setAlternatingRowColors( true );
70     header()->hide();
71
72     QFont myFont = QApplication::font(0);
73     myFont.setPointSize( myFont.pointSize() + 3 ); myFont.setBold( true );
74
75 #define BI( a,b) QIcon a##_icon = QIcon( QPixmap( b##_xpm ))
76     BI( audio, audio );
77     BI( video, video );
78     BI( input, codec );
79     BI( sout, type_net );
80     BI( advanced, advanced );
81     BI( playlist, type_playlist );
82     BI( interface, intf );
83 #undef BI
84
85     /* Build the tree for the main module */
86     int i_index;
87     for( i_index = 0; i_index < p_list->i_count; i_index++ )
88     {
89         p_module = (module_t *)p_list->p_values[i_index].p_object;
90         if( !strcmp( p_module->psz_object_name, "main" ) )
91              break;
92     }
93     if( i_index < p_list->i_count )
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
188     /* Build the tree of plugins */
189     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
190     {
191         module_config_t *p_item;
192         int i_subcategory = -1, i_category = -1, i_options = 0;
193         p_module = (module_t *)p_list->p_values[i_index].p_object;
194
195         // Main module excluded
196         if( !strcmp( p_module->psz_object_name, "main" ) ) continue;
197
198         /* Exclude empty plugins (submodules don't have config options, they
199          * are stored in the parent module) */
200         if( p_module->b_submodule ) continue;
201
202         p_item = p_module->p_config;
203         if( !p_item ) continue;
204
205         do {
206             if( p_item->i_type == CONFIG_CATEGORY )
207                 i_category = p_item->i_value;
208             else if( p_item->i_type == CONFIG_SUBCATEGORY )
209                 i_subcategory = p_item->i_value;
210             if( p_item->i_type & CONFIG_ITEM )
211                 i_options++;
212
213             if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
214                 break;
215         } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
216
217         if( !i_options ) continue; // Nothing to display
218
219         // Locate the category item;
220         QTreeWidgetItem *subcat_item = NULL;
221         bool b_found = false;
222         for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
223                                    i_cat_index++ )
224         {
225             QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
226             PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
227                                              value<PrefsItemData *>();
228             if( data->i_object_id == i_category )
229             {
230                 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
231                          i_sc_index++ )
232                 {
233                     subcat_item = cat_item->child( i_sc_index );
234                     PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
235                                                 value<PrefsItemData *>();
236                     if( sc_data && sc_data->i_object_id == i_subcategory )
237                     {
238                         b_found = true;
239                         break;
240                     }
241                 }
242                 if( !b_found )
243                 {
244                     subcat_item = cat_item;
245                     b_found = true;
246                 }
247                 break;
248             }
249         }
250         if( !b_found ) continue;
251
252         PrefsItemData *module_data = new PrefsItemData();
253         module_data->b_submodule = p_module->b_submodule;
254         module_data->i_type = TYPE_MODULE;
255         module_data->i_object_id = p_module->b_submodule ?
256                          ((module_t *)p_module->p_parent)->i_object_id :
257                          p_module->i_object_id;
258         module_data->help.clear();
259         // TODO image
260         QTreeWidgetItem *module_item = new QTreeWidgetItem();
261         module_item->setText( 0, qfu( p_module->psz_shortname ?
262                       p_module->psz_shortname : p_module->psz_object_name) );
263         //item->setIcon( 0 , XXX );
264         module_item->setData( 0, Qt::UserRole,
265                               QVariant::fromValue( module_data) );
266         module_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
267         subcat_item->addChild( module_item );
268     }
269
270     /* We got everything, just sort a bit */
271     sortItems( 0, Qt::AscendingOrder );
272
273     vlc_list_release( p_list );
274 }
275
276 PrefsTree::~PrefsTree() {}
277
278 void PrefsTree::applyAll()
279 {
280     doAll( false );
281 }
282
283 void PrefsTree::cleanAll()
284 {
285     doAll( true );
286 }
287
288 /// \todo When cleaning, we should remove the panel ?
289 void PrefsTree::doAll( bool doclean )
290 {
291     for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
292              i_cat_index++ )
293     {
294         QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
295         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
296                  i_sc_index++ )
297         {
298             QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
299             for( int i_module = 0 ; i_module < sc_item->childCount();
300                      i_module++ )
301             {
302                 PrefsItemData *data = sc_item->child( i_module )->
303                                data( 0, Qt::UserRole).value<PrefsItemData *>();
304                 if( data->panel && doclean )
305                 {
306                     delete data->panel;
307                     data->panel = NULL;
308                 }
309                 else if( data->panel )
310                     data->panel->apply();
311             }
312             PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
313                                             value<PrefsItemData *>();
314             if( data->panel && doclean )
315             {
316                 delete data->panel;
317                 data->panel = NULL;
318             }
319             else if( data->panel )
320                 data->panel->apply();
321         }
322         PrefsItemData *data = cat_item->data( 0, Qt::UserRole).
323                                             value<PrefsItemData *>();
324         if( data->panel && doclean )
325         {
326             delete data->panel;
327             data->panel = NULL;
328         }
329         else if( data->panel )
330             data->panel->apply();
331     }
332 }
333
334 /*********************************************************************
335  * The Panel
336  *********************************************************************/
337 PrefsPanel::PrefsPanel( QWidget *_parent ) : QWidget( _parent )
338 {}
339
340 PrefsPanel::PrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
341                         PrefsItemData * data, bool currently_advanced ) :
342                         QWidget( _parent ), p_intf( _p_intf )
343 {
344     module_config_t *p_item;
345
346     /* Find our module */
347     module_t *p_module = NULL;
348     if( data->i_type == TYPE_CATEGORY )
349         return;
350     else if( data->i_type == TYPE_MODULE )
351         p_module = (module_t *) vlc_object_get( p_intf, data->i_object_id );
352     else
353     {
354         /* List the plugins */
355         int i_index;
356         vlc_bool_t b_found = VLC_FALSE;
357         vlc_list_t *p_list = vlc_list_find( p_intf,
358                                             VLC_OBJECT_MODULE, FIND_ANYWHERE );
359         if( !p_list ) return;
360
361         for( i_index = 0; i_index < p_list->i_count; i_index++ )
362         {
363             p_module = (module_t *)p_list->p_values[i_index].p_object;
364             if( !strcmp( p_module->psz_object_name, "main" ) )
365             {
366                 b_found = VLC_TRUE;
367                 break;
368             }
369         }
370         if( !p_module && !b_found )
371         {
372             msg_Warn( p_intf, "unable to create preferences (main not found)");
373             return;
374         }
375         if( p_module ) vlc_object_yield( p_module );
376         vlc_list_release( p_list );
377     }
378
379     if( p_module->b_submodule )
380         p_item = ((module_t *)p_module->p_parent)->p_config;
381     else
382         p_item = p_module->p_config;
383
384     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
385     {
386         do
387         {
388             if( p_item->i_type == CONFIG_SUBCATEGORY &&
389                             ( data->i_type == TYPE_SUBCATEGORY &&
390                               p_item->i_value == data->i_object_id ) ||
391                             ( data->i_type == TYPE_CATSUBCAT &&
392                               p_item->i_value == data->i_subcat_id ) )
393                 break;
394             if( p_item->i_type == CONFIG_HINT_END ) break;
395         } while( p_item++ );
396     }
397
398     global_layout = new QVBoxLayout();
399     QString head;
400     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
401     {
402         head = QString( data->name );
403         p_item++; // Why that ?
404     }
405     else
406     {
407         head = QString( qfu(p_module->psz_longname) );
408         if( p_module->psz_help )
409         {
410             head.append( "\n" );
411             head.append( qfu( p_module->psz_help ) );
412         }
413     }
414
415     QLabel *label = new QLabel( head );
416     global_layout->addWidget( label );
417     QFont myFont = QApplication::font(0);
418     myFont.setPointSize( myFont.pointSize() + 3 ); myFont.setBold( true );
419
420     label->setFont( myFont );
421     QLabel *help = new QLabel( data->help, this );
422     help->setWordWrap( true );
423
424     global_layout->addWidget( help );
425
426     QGroupBox *box = NULL;
427     QGridLayout *boxlayout = NULL;
428
429     QScrollArea *scroller= new QScrollArea;
430     scroller->setFrameStyle( QFrame::NoFrame );
431     QWidget *scrolled_area = new QWidget;
432
433     QGridLayout *layout = new QGridLayout();
434     int i_line = 0, i_boxline = 0;
435
436     if( p_item ) do
437     {
438         if( ( ( data->i_type == TYPE_SUBCATEGORY &&
439                 p_item->i_value != data->i_object_id ) ||
440               ( data->i_type == TYPE_CATSUBCAT  &&
441                 p_item->i_value != 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 == VLC_TRUE ) continue;
446
447         if( p_item->i_type == CONFIG_SECTION )
448         {
449             if( box )
450             {
451                 box->setLayout( boxlayout );
452                 layout->addWidget( box, i_line, 0, 1, 2 );
453                 i_line++;
454             }
455             box = new QGroupBox( qfu(p_item->psz_text) );
456             boxlayout = new QGridLayout();
457         }
458
459         ConfigControl *control;
460         if( ! box )
461             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
462                                         p_item, NULL, layout, i_line );
463         else
464             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
465                                     p_item, NULL, boxlayout, i_boxline );
466         if( !control )
467         {
468             continue;
469         }
470         if( box ) i_boxline++;
471         else i_line++;
472         controls.append( control );
473     }
474     while( !(p_item->i_type == CONFIG_HINT_END ||
475            ( ( data->i_type == TYPE_SUBCATEGORY ||
476                data->i_type == TYPE_CATSUBCAT ) &&
477              ( p_item->i_type == CONFIG_CATEGORY ||
478                p_item->i_type == CONFIG_SUBCATEGORY ) ) ) && p_item++ );
479
480     if( box )
481     {
482         box->setLayout( boxlayout );
483         layout->addWidget( box, i_line, 0, 1, 2 );
484     }
485
486     vlc_object_release( p_module );
487
488     scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
489     scrolled_area->setLayout( layout );
490     scroller->setWidget( scrolled_area );
491     scroller->setWidgetResizable( true );
492     global_layout->addWidget( scroller );
493     setLayout( global_layout );
494 }
495
496 void PrefsPanel::apply()
497 {
498     /* todo */
499     QList<ConfigControl *>::Iterator i;
500     for( i = controls.begin() ; i != controls.end() ; i++ )
501     {
502         ConfigControl *c = qobject_cast<ConfigControl *>(*i);
503         switch( c->getType() )
504         {
505         case 1:
506             {
507             VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(*i);
508             config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
509             break;
510             }
511         case 2:
512             {
513             VFloatConfigControl *vfcc = qobject_cast<VFloatConfigControl *>(*i);
514             config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
515             break;
516             }
517         case 3:
518             {
519             VStringConfigControl *vscc =
520                             qobject_cast<VStringConfigControl *>(*i);
521             config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
522             }
523         }
524     }
525 }
526
527 void PrefsPanel::clean()
528 {}