Moves volume icon update in the timer
[vlc.git] / 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
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/complete_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     setAlternatingRowColors( true );
66     header()->hide();
67     setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
68     setTextElideMode( Qt::ElideNone );
69     setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
70
71 #define BI( a,b) QIcon a##_icon = QIcon( QPixmap( b##_xpm ))
72     BI( audio, audio );
73     BI( video, video );
74     BI( input, codec );
75     BI( sout, type_net );
76     BI( advanced, advanced );
77     BI( playlist, type_playlist );
78     BI( interface, intf );
79 #undef BI
80
81     /* Build the tree for the main module */
82     const module_t *p_module = NULL;
83     vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
84                                         FIND_ANYWHERE );
85     if( !p_list ) return;
86     for( unsigned i = 0; p_module == NULL; i++ )
87     {
88         assert (i < (unsigned)p_list->i_count);
89
90         const module_t *p_main = (module_t *)p_list->p_values[i].p_object;
91         if( strcmp( module_GetObjName( p_main ), "main" ) == 0 )
92             p_module = p_main;
93     }
94
95     PrefsItemData *data = NULL;
96     QTreeWidgetItem *current_item = NULL;
97     for (size_t i = 0; i < p_module->confsize; i++)
98     {
99         const module_config_t *p_item = p_module->p_config + i;
100         const char *psz_help;
101         QIcon icon;
102         switch( p_item->i_type )
103         {
104         case CONFIG_CATEGORY:
105             if( p_item->value.i == -1 ) break;
106             data = new PrefsItemData();
107             data->name = QString( qtr( config_CategoryNameGet
108                                            ( p_item->value.i ) ) );
109             psz_help = config_CategoryHelpGet( p_item->value.i );
110             if( psz_help )
111                 data->help = QString( qtr(psz_help) );
112             else
113                 data->help.clear();
114             data->i_type = TYPE_CATEGORY;
115             data->i_object_id = p_item->value.i;
116
117             switch( p_item->value.i )
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->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
134             current_item->setData( 0, Qt::UserRole,
135                                    qVariantFromValue( data ) );
136             addTopLevelItem( current_item );
137             break;
138         case CONFIG_SUBCATEGORY:
139             if( p_item->value.i == -1 ) break;
140             if( data &&
141                 ( p_item->value.i == SUBCAT_VIDEO_GENERAL ||
142                   p_item->value.i == SUBCAT_ADVANCED_MISC ||
143                   p_item->value.i == SUBCAT_INPUT_GENERAL ||
144                   p_item->value.i == SUBCAT_INTERFACE_GENERAL ||
145                   p_item->value.i == SUBCAT_SOUT_GENERAL||
146                   p_item->value.i == SUBCAT_PLAYLIST_GENERAL||
147                   p_item->value.i == SUBCAT_AUDIO_GENERAL ) )
148             {
149                 // Data still contains the correct thing
150                 data->i_type = TYPE_CATSUBCAT;
151                 data->i_subcat_id = p_item->value.i;
152                 data->name = QString( qtr( config_CategoryNameGet(
153                                             p_item->value.i )) );
154                 psz_help = config_CategoryHelpGet( p_item->value.i );
155                 if( psz_help )
156                     data->help = QString( qtr(psz_help) );
157                 else
158                     data->help.clear();
159                 current_item->setData( 0, Qt::UserRole,
160                                        QVariant::fromValue( data ) );
161                 continue;
162             }
163             data = new PrefsItemData();
164             data->name = QString( qtr( config_CategoryNameGet(
165                                                         p_item->value.i)) );
166             psz_help = config_CategoryHelpGet( p_item->value.i );
167             if( psz_help )
168                 data->help = QString( qtr(psz_help) );
169             else
170                 data->help.clear();
171             data->i_type = TYPE_SUBCATEGORY;
172             data->i_object_id = p_item->value.i;
173
174             assert( current_item );
175
176             /* TODO : Choose the image */
177             QTreeWidgetItem *subcat_item = new QTreeWidgetItem();
178             subcat_item->setText( 0, data->name );
179             //item->setIcon( 0 , XXX );
180             subcat_item->setData( 0, Qt::UserRole,
181                                   qVariantFromValue(data) );
182             subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
183             current_item->addChild( subcat_item );
184             break;
185         }
186     }
187
188     /* Build the tree of plugins */
189     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
190     {
191         p_module = (module_t *)p_list->p_values[i_index].p_object;
192
193         // Main module excluded
194         if( !strcmp( module_GetObjName( p_module ), "main" ) ) continue;
195
196         /* Exclude submodules; they have no config options of their own */
197         if( p_module->b_submodule ) continue;
198
199         unsigned i_subcategory = 0, i_category = 0;
200         bool b_options = false;
201
202         for (size_t i = 0; i < p_module->confsize; i++)
203         {
204             const module_config_t *p_item = p_module->p_config + i;
205
206             if( p_item->i_type == CONFIG_CATEGORY )
207                 i_category = p_item->value.i;
208             else if( p_item->i_type == CONFIG_SUBCATEGORY )
209                 i_subcategory = p_item->value.i;
210
211             if( p_item->i_type & CONFIG_ITEM )
212                 b_options = true;
213
214             if( b_options && i_category && i_subcategory )
215                 break;
216         }
217         if( !b_options || i_category == 0 || i_subcategory == 0 ) continue;
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->psz_name = strdup( module_GetObjName( p_module ) );
256         module_data->i_object_id = p_module->b_submodule ?
257                          ((module_t *)p_module->p_parent)->i_object_id :
258                          p_module->i_object_id;
259         module_data->help.clear();
260         // TODO image
261         QTreeWidgetItem *module_item = new QTreeWidgetItem();
262         module_item->setText( 0, qtr( module_GetName( p_module, VLC_FALSE ) ) );
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 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     /* Find our module */
344     module_t *p_module = NULL;
345     if( data->i_type == TYPE_CATEGORY )
346         return;
347     else if( data->i_type == TYPE_MODULE )
348         p_module = (module_t *) vlc_object_get( p_intf, data->i_object_id );
349     else
350     {
351         p_module = config_FindModule( VLC_OBJECT(p_intf), "main" );
352         assert( p_module );
353         vlc_object_yield( p_module );
354     }
355
356     module_t *p_realmodule = p_module->b_submodule
357             ? (module_t *)(p_module->p_parent)
358             : p_module;
359
360     module_config_t *p_item = p_realmodule->p_config;
361     module_config_t *p_end = p_item + p_realmodule->confsize;
362
363     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
364     {
365         while (p_item < p_end)
366         {
367             if( p_item->i_type == CONFIG_SUBCATEGORY &&
368                             ( data->i_type == TYPE_SUBCATEGORY &&
369                               p_item->value.i == data->i_object_id ) ||
370                             ( data->i_type == TYPE_CATSUBCAT &&
371                               p_item->value.i == data->i_subcat_id ) )
372                 break;
373             p_item++;
374         }
375     }
376
377     /* Widgets now */
378     global_layout = new QVBoxLayout();
379     global_layout->setMargin( 2 );
380     QString head;
381     QString help;
382
383     help = QString( data->help );
384
385     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
386     {
387         head = QString( data->name );
388         p_item++; // Why that ?
389     }
390     else
391     {
392         head = QString( qtr( module_GetLongName( p_module ) ) );
393         if( p_module->psz_help )
394         {
395             help.append( "\n" );
396             help.append( qtr( module_GetHelp( p_module ) ) );
397         }
398     }
399
400     QLabel *titleLabel = new QLabel( head );
401     QFont titleFont = QApplication::font( static_cast<QWidget*>(0) );
402     titleFont.setPointSize( titleFont.pointSize() + 6 );
403     titleFont.setFamily( "Verdana" );
404     titleLabel->setFont( titleFont );
405
406     // Title <hr>
407     QFrame *title_line = new QFrame;
408     title_line->setFrameShape(QFrame::HLine);
409     title_line->setFrameShadow(QFrame::Sunken);
410
411     QLabel *helpLabel = new QLabel( help, this );
412     helpLabel->setWordWrap( true );
413
414     global_layout->addWidget( titleLabel );
415     global_layout->addWidget( title_line );
416     global_layout->addWidget( helpLabel );
417
418     QGroupBox *box = NULL;
419     QGridLayout *boxlayout = NULL;
420
421     QScrollArea *scroller= new QScrollArea;
422     scroller->setFrameStyle( QFrame::NoFrame );
423     QWidget *scrolled_area = new QWidget;
424
425     QGridLayout *layout = new QGridLayout();
426     int i_line = 0, i_boxline = 0;
427     bool has_hotkey = false;
428
429     if( p_item ) do
430     {
431         if( ( ( data->i_type == TYPE_SUBCATEGORY &&
432                 p_item->value.i != data->i_object_id ) ||
433               ( data->i_type == TYPE_CATSUBCAT  &&
434                 p_item->value.i != data->i_subcat_id ) ) &&
435             ( p_item->i_type == CONFIG_CATEGORY ||
436               p_item->i_type == CONFIG_SUBCATEGORY ) )
437             break;
438         if( p_item->b_internal == VLC_TRUE ) continue;
439
440         if( p_item->i_type == CONFIG_SECTION )
441         {
442             if( box )
443             {
444                 box->setLayout( boxlayout );
445                 layout->addWidget( box, i_line, 0, 1, 2 );
446                 i_line++;
447             }
448             box = new QGroupBox( qtr(p_item->psz_text) );
449             boxlayout = new QGridLayout();
450         }
451         /* Only one hotkey control */
452         if( has_hotkey && p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
453                                          strstr( p_item->psz_name, "key-" ) )
454             continue;
455         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
456                                             strstr( p_item->psz_name, "key-" ) )
457             has_hotkey = true;
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             continue;
468
469         if( has_hotkey )
470         {
471             /* A hotkey widget takes 2 lines */
472             if( box ) i_boxline ++;
473             else i_line++;
474         }
475
476         if( box ) i_boxline++;
477         else i_line++;
478         controls.append( control );
479     }
480     while( !( ( data->i_type == TYPE_SUBCATEGORY ||
481                data->i_type == TYPE_CATSUBCAT ) &&
482              ( p_item->i_type == CONFIG_CATEGORY ||
483                p_item->i_type == CONFIG_SUBCATEGORY ) )
484         && ( ++p_item < p_end ) );
485
486     if( box )
487     {
488         box->setLayout( boxlayout );
489         layout->addWidget( box, i_line, 0, 1, 2 );
490     }
491
492     vlc_object_release( p_module );
493
494     scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
495     scrolled_area->setLayout( layout );
496     scroller->setWidget( scrolled_area );
497     scroller->setWidgetResizable( true );
498     global_layout->addWidget( scroller );
499     setLayout( global_layout );
500 }
501
502 void PrefsPanel::apply()
503 {
504     QList<ConfigControl *>::Iterator i;
505     for( i = controls.begin() ; i != controls.end() ; i++ )
506     {
507         ConfigControl *c = qobject_cast<ConfigControl *>(*i);
508         c->doApply( p_intf );
509     }
510 }
511 void PrefsPanel::clean()
512 {}