Some cleanup here and there
[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
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     module_t *p_module;
65     vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
66                                         FIND_ANYWHERE );
67     if( !p_list ) return;
68
69     setColumnCount( 1 );
70     setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
71     setAlternatingRowColors( true );
72     header()->hide();
73
74 #define BI( a,b) QIcon a##_icon = QIcon( QPixmap( b##_xpm ))
75     BI( audio, audio );
76     BI( video, video );
77     BI( input, codec );
78     BI( sout, type_net );
79     BI( advanced, advanced );
80     BI( playlist, type_playlist );
81     BI( interface, intf );
82 #undef BI
83
84     /* Build the tree for the main module */
85     int i_index;
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              break;
91     }
92     if( i_index < p_list->i_count )
93     {
94         module_config_t *p_item = p_module->p_config;
95         PrefsItemData *data = NULL;
96         QTreeWidgetItem *current_item = NULL;
97         if( p_item ) do
98         {
99             char *psz_help;
100             QIcon icon;
101             switch( p_item->i_type )
102             {
103             case CONFIG_CATEGORY:
104                 if( p_item->i_value == -1 ) break;
105                 data = new PrefsItemData();
106                 data->name = QString( qfu( config_CategoryNameGet
107                                                ( p_item->i_value ) ) );
108                 psz_help = config_CategoryHelpGet( p_item->i_value );
109                 if( psz_help )
110                     data->help = QString( qfu(psz_help) );
111                 else
112                     data->help.clear();
113                 data->i_type = TYPE_CATEGORY;
114                 data->i_object_id = p_item->i_value;
115
116                 switch( p_item->i_value )
117                 {
118 #define CI(a,b) case a: icon = b##_icon;break
119                 CI( CAT_AUDIO, audio );
120                 CI( CAT_VIDEO, video );
121                 CI( CAT_INPUT, input );
122                 CI( CAT_SOUT, sout );
123                 CI( CAT_ADVANCED, advanced );
124                 CI( CAT_PLAYLIST, playlist );
125                 CI( CAT_INTERFACE, interface );
126 #undef CI
127                 }
128
129                 current_item = new QTreeWidgetItem();
130                 current_item->setText( 0, data->name );
131                 current_item->setIcon( 0 , icon );
132                 current_item->setData( 0, Qt::UserRole,
133                                        qVariantFromValue( data ) );
134                 addTopLevelItem( current_item );
135                 break;
136             case CONFIG_SUBCATEGORY:
137                 if( p_item->i_value == -1 ) break;
138                 if( data &&
139                     ( p_item->i_value == SUBCAT_VIDEO_GENERAL ||
140                       p_item->i_value == SUBCAT_ADVANCED_MISC ||
141                       p_item->i_value == SUBCAT_INPUT_GENERAL ||
142                       p_item->i_value == SUBCAT_INTERFACE_GENERAL ||
143                       p_item->i_value == SUBCAT_SOUT_GENERAL||
144                       p_item->i_value == SUBCAT_PLAYLIST_GENERAL||
145                       p_item->i_value == SUBCAT_AUDIO_GENERAL ) )
146                 {
147                     // Data still contains the correct thing
148                     data->i_type = TYPE_CATSUBCAT;
149                     data->i_subcat_id = p_item->i_value;
150                     data->name = QString( qfu( config_CategoryNameGet(
151                                                 p_item->i_value )) );
152                     psz_help = config_CategoryHelpGet( p_item->i_value );
153                     if( psz_help )
154                         data->help = QString( qfu(psz_help) );
155                     else
156                         data->help.clear();
157                     current_item->setData( 0, Qt::UserRole,
158                                            QVariant::fromValue( data ) );
159                     continue;
160                 }
161                 data = new PrefsItemData();
162                 data->name = QString( qfu( config_CategoryNameGet( 
163                                                             p_item->i_value)) );
164                 psz_help = config_CategoryHelpGet( p_item->i_value );
165                 if( psz_help )
166                     data->help = QString( qfu(psz_help) );
167                 else
168                     data->help.clear();
169                 data->i_type = TYPE_SUBCATEGORY;
170                 data->i_object_id = p_item->i_value;
171
172                 assert( current_item );
173
174                 /* TODO : Choose the image */
175                 QTreeWidgetItem *subcat_item = new QTreeWidgetItem();
176                 subcat_item->setText( 0, data->name );
177                 //item->setIcon( 0 , XXX );
178                 subcat_item->setData( 0, Qt::UserRole,
179                                       qVariantFromValue(data) );
180                 subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
181                 current_item->addChild( subcat_item );
182                 break;
183             }
184         } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
185     }
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 /// \todo When cleaning, we should remove the panel ?
288 void PrefsTree::doAll( bool doclean )
289 {
290     for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
291              i_cat_index++ )
292     {
293         QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
294         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
295                  i_sc_index++ )
296         {
297             QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
298             for( int i_module = 0 ; i_module < sc_item->childCount();
299                      i_module++ )
300             {
301                 PrefsItemData *data = sc_item->child( i_module )->
302                                data( 0, Qt::UserRole).value<PrefsItemData *>();
303                 if( data->panel && doclean )
304                 {
305                     delete data->panel;
306                     data->panel = NULL;
307                 }
308                 else if( data->panel )
309                     data->panel->apply();
310             }
311             PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
312                                             value<PrefsItemData *>();
313             if( data->panel && doclean )
314             {
315                 delete data->panel;
316                 data->panel = NULL;
317             }
318             else if( data->panel )
319                 data->panel->apply();
320         }
321         PrefsItemData *data = cat_item->data( 0, Qt::UserRole).
322                                             value<PrefsItemData *>();
323         if( data->panel && doclean )
324         {
325             delete data->panel;
326             data->panel = NULL;
327         }
328         else if( data->panel )
329             data->panel->apply();
330     }
331 }
332
333 /*********************************************************************
334  * The Panel
335  *********************************************************************/
336 PrefsPanel::PrefsPanel( QWidget *_parent ) : QWidget( _parent )
337 {}
338
339 PrefsPanel::PrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
340                         PrefsItemData * data ) :
341                         QWidget( _parent ), p_intf( _p_intf )
342 {
343     module_config_t *p_item;
344
345     /* Find our module */
346     module_t *p_module = NULL;
347     if( data->i_type == TYPE_CATEGORY )
348         return;
349     else if( data->i_type == TYPE_MODULE )
350         p_module = (module_t *) vlc_object_get( p_intf, data->i_object_id );
351     else
352     {
353         /* List the plugins */
354         int i_index;
355         vlc_bool_t b_found = VLC_FALSE;
356         vlc_list_t *p_list = vlc_list_find( p_intf,
357                                             VLC_OBJECT_MODULE, FIND_ANYWHERE );
358         if( !p_list ) return;
359
360         for( i_index = 0; i_index < p_list->i_count; i_index++ )
361         {
362             p_module = (module_t *)p_list->p_values[i_index].p_object;
363             if( !strcmp( p_module->psz_object_name, "main" ) )
364             {
365                 b_found = VLC_TRUE;
366                 break;
367             }
368         }
369         if( !p_module && !b_found )
370         {
371             msg_Warn( p_intf, "unable to create preferences (main not found)");
372             return;
373         }
374         if( p_module ) vlc_object_yield( p_module );
375         vlc_list_release( p_list );
376     }
377
378     if( p_module->b_submodule )
379         p_item = ((module_t *)p_module->p_parent)->p_config;
380     else
381         p_item = p_module->p_config;
382
383     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
384     {
385         do
386         {
387             if( p_item->i_type == CONFIG_SUBCATEGORY &&
388                             ( data->i_type == TYPE_SUBCATEGORY &&
389                               p_item->i_value == data->i_object_id ) ||
390                             ( data->i_type == TYPE_CATSUBCAT &&
391                               p_item->i_value == data->i_subcat_id ) )
392                 break;
393             if( p_item->i_type == CONFIG_HINT_END ) break;
394         } while( p_item++ );
395     }
396
397     global_layout = new QVBoxLayout();
398     QString head;
399     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
400     {
401         head = QString( data->name );
402         p_item++; // Why that ?
403     }
404     else
405     {
406         head = QString( qfu(p_module->psz_longname) );
407         if( p_module->psz_help )
408         {
409             head.append( "\n" );
410             head.append( qfu( p_module->psz_help ) );
411         }
412     }
413
414     QLabel *label = new QLabel( head );
415     global_layout->addWidget( label );
416     QFont myFont = QApplication::font(0);
417     myFont.setPointSize( myFont.pointSize() + 3 ); myFont.setBold( true );
418
419     label->setFont( myFont );
420     QLabel *help = new QLabel( data->help, this );
421     help->setWordWrap( true );
422
423     global_layout->addWidget( help );
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
435     if( p_item ) do
436     {
437         if( ( ( data->i_type == TYPE_SUBCATEGORY &&
438                 p_item->i_value != data->i_object_id ) ||
439               ( data->i_type == TYPE_CATSUBCAT  &&
440                 p_item->i_value != data->i_subcat_id ) ) &&
441             ( p_item->i_type == CONFIG_CATEGORY ||
442               p_item->i_type == CONFIG_SUBCATEGORY ) )
443             break;
444         if( p_item->b_internal == VLC_TRUE ) continue;
445
446         if( p_item->i_type == CONFIG_SECTION )
447         {
448             if( box )
449             {
450                 box->setLayout( boxlayout );
451                 layout->addWidget( box, i_line, 0, 1, 2 );
452                 i_line++;
453             }
454             box = new QGroupBox( qfu(p_item->psz_text) );
455             boxlayout = new QGridLayout();
456         }
457
458         ConfigControl *control;
459         if( ! box )
460             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
461                                         p_item, NULL, layout, i_line );
462         else
463             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
464                                     p_item, NULL, boxlayout, i_boxline );
465         if( !control )
466             continue;
467
468         if( box ) i_boxline++;
469         else i_line++;
470         controls.append( control );
471     }
472     while( !(p_item->i_type == CONFIG_HINT_END ||
473            ( ( data->i_type == TYPE_SUBCATEGORY ||
474                data->i_type == TYPE_CATSUBCAT ) &&
475              ( p_item->i_type == CONFIG_CATEGORY ||
476                p_item->i_type == CONFIG_SUBCATEGORY ) ) ) && p_item++ );
477
478     if( box )
479     {
480         box->setLayout( boxlayout );
481         layout->addWidget( box, i_line, 0, 1, 2 );
482     }
483
484     vlc_object_release( p_module );
485
486     scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
487     scrolled_area->setLayout( layout );
488     scroller->setWidget( scrolled_area );
489     scroller->setWidgetResizable( true );
490     global_layout->addWidget( scroller );
491     setLayout( global_layout );
492 }
493
494 void PrefsPanel::apply()
495 {
496     QList<ConfigControl *>::Iterator i;
497     for( i = controls.begin() ; i != controls.end() ; i++ )
498     {
499         ConfigControl *c = qobject_cast<ConfigControl *>(*i);
500         c->doApply( p_intf );
501     }
502 }
503 void PrefsPanel::clean()
504 {}