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