]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/complete_preferences.cpp
Qt4 - Fix Advanced preferences images location...
[vlc] / 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
43 #include <vlc_config_cat.h>
44 #include <vlc_intf_strings.h>
45 #include <assert.h>
46
47 #define ITEM_HEIGHT 25
48
49 /*********************************************************************
50  * The Tree
51  *********************************************************************/
52 PrefsTree::PrefsTree( intf_thread_t *_p_intf, QWidget *_parent ) :
53                             QTreeWidget( _parent ), p_intf( _p_intf )
54 {
55     setColumnCount( 1 );
56     setAlternatingRowColors( true );
57     header()->hide();
58     setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
59     setTextElideMode( Qt::ElideNone );
60     setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
61
62 #define BI( a,b) QIcon a##_icon = QIcon( QPixmap( b ))
63     BI( audio, ":/pixmaps/advprefs_audio.png" );
64     BI( video, ":/pixmaps/advprefs_video.png" );
65     BI( input, ":/pixmaps/advprefs_codec.png" );
66     BI( sout, ":/pixmaps/advprefs_sout.png" );
67     BI( advanced, ":/pixmaps/advprefs_extended.png" );
68     BI( playlist, ":/pixmaps/advprefs_playlist.png" );
69     BI( interface, ":/pixmaps/advprefs_intf.png" );
70 #undef BI
71
72     /* Build the tree for the main module */
73     const module_t *p_module = NULL;
74     vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
75                                         FIND_ANYWHERE );
76     if( !p_list ) return;
77     for( unsigned i = 0; p_module == NULL; i++ )
78     {
79         assert (i < (unsigned)p_list->i_count);
80
81         const module_t *p_main = (module_t *)p_list->p_values[i].p_object;
82         if( strcmp( module_GetObjName( p_main ), "main" ) == 0 )
83             p_module = p_main;
84     }
85
86     PrefsItemData *data = NULL;
87     QTreeWidgetItem *current_item = NULL;
88     unsigned confsize;
89     module_config_t *const p_config = module_GetConfig (p_module, &confsize);
90
91     for (size_t i = 0; i < confsize; i++)
92     {
93         module_config_t *p_item = p_config + i;
94
95         const char *psz_help;
96         QIcon icon;
97         switch( p_item->i_type )
98         {
99         case CONFIG_CATEGORY:
100             if( p_item->value.i == -1 ) break;
101             data = new PrefsItemData();
102             data->name = QString( qtr( config_CategoryNameGet
103                                            ( p_item->value.i ) ) );
104             psz_help = config_CategoryHelpGet( p_item->value.i );
105             if( psz_help )
106                 data->help = QString( qtr(psz_help) );
107             else
108                 data->help.clear();
109             data->i_type = TYPE_CATEGORY;
110             data->i_object_id = p_item->value.i;
111
112             switch( p_item->value.i )
113             {
114 #define CI(a,b) case a: icon = b##_icon;break
115             CI( CAT_AUDIO, audio );
116             CI( CAT_VIDEO, video );
117             CI( CAT_INPUT, input );
118             CI( CAT_SOUT, sout );
119             CI( CAT_ADVANCED, advanced );
120             CI( CAT_PLAYLIST, playlist );
121             CI( CAT_INTERFACE, interface );
122 #undef CI
123             }
124
125             current_item = new QTreeWidgetItem();
126             current_item->setText( 0, data->name );
127             current_item->setIcon( 0 , icon );
128             current_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
129             current_item->setData( 0, Qt::UserRole,
130                                    qVariantFromValue( data ) );
131             addTopLevelItem( current_item );
132             break;
133         case CONFIG_SUBCATEGORY:
134             if( p_item->value.i == -1 ) break;
135
136             /* Special cases: move the main subcategories to the parent cat*/
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( qtr( config_CategoryNameGet(
150                                             p_item->value.i )) );
151                 psz_help = config_CategoryHelpGet( p_item->value.i );
152                 if( psz_help )
153                     data->help = QString( qtr(psz_help) );
154                 else
155                     data->help.clear();
156                 current_item->setData( 0, Qt::UserRole,
157                                        QVariant::fromValue( data ) );
158                 continue;
159             }
160
161             data = new PrefsItemData();
162             data->name = QString( qtr( config_CategoryNameGet(
163                                                         p_item->value.i)) );
164             psz_help = config_CategoryHelpGet( p_item->value.i );
165             if( psz_help )
166                 data->help = QString( qtr(psz_help) );
167             else
168                 data->help.clear();
169             data->i_type = TYPE_SUBCATEGORY;
170             data->i_object_id = p_item->value.i;
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     }
185     module_PutConfig (p_config);
186
187     /* Build the tree of plugins */
188     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
189     {
190         p_module = (module_t *)p_list->p_values[i_index].p_object;
191
192         // Main module excluded
193         if( !strcmp( module_GetObjName( p_module ), "main" ) ) continue;
194
195         unsigned i_subcategory = 0, i_category = 0, confsize;
196         bool b_options = false;
197         module_config_t *const p_config = module_GetConfig (p_module, &confsize);
198
199         for (size_t i = 0; i < confsize; i++)
200         {
201             const module_config_t *p_item = p_config + i;
202
203             if( p_item->i_type == CONFIG_CATEGORY )
204                 i_category = p_item->value.i;
205             else if( p_item->i_type == CONFIG_SUBCATEGORY )
206                 i_subcategory = p_item->value.i;
207
208             if( p_item->i_type & CONFIG_ITEM )
209                 b_options = true;
210
211             if( b_options && i_category && i_subcategory )
212                 break;
213         }
214         module_PutConfig (p_config);
215         if( !b_options || i_category == 0 || i_subcategory == 0 ) continue;
216
217         // Locate the category item;
218         QTreeWidgetItem *subcat_item = NULL;
219         bool b_found = false;
220         for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
221                                    i_cat_index++ )
222         {
223             QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
224             PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
225                                              value<PrefsItemData *>();
226             if( data->i_object_id == i_category )
227             {
228                 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
229                          i_sc_index++ )
230                 {
231                     subcat_item = cat_item->child( i_sc_index );
232                     PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
233                                                 value<PrefsItemData *>();
234                     if( sc_data && sc_data->i_object_id == i_subcategory )
235                     {
236                         b_found = true;
237                         break;
238                     }
239                 }
240                 if( !b_found )
241                 {
242                     subcat_item = cat_item;
243                     b_found = true;
244                 }
245                 break;
246             }
247         }
248         if( !b_found ) continue;
249
250         PrefsItemData *module_data = new PrefsItemData();
251         module_data->i_type = TYPE_MODULE;
252         module_data->psz_name = strdup( module_GetObjName( p_module ) );
253         module_data->help.clear();
254         // TODO image
255         QTreeWidgetItem *module_item = new QTreeWidgetItem();
256         module_item->setText( 0, qtr( module_GetName( p_module, VLC_FALSE ) ) );
257         //item->setIcon( 0 , XXX );
258         module_item->setData( 0, Qt::UserRole,
259                               QVariant::fromValue( module_data) );
260         module_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
261         subcat_item->addChild( module_item );
262     }
263
264     /* We got everything, just sort a bit */
265     sortItems( 0, Qt::AscendingOrder );
266
267     vlc_list_release( p_list );
268 }
269
270 PrefsTree::~PrefsTree() {}
271
272 void PrefsTree::applyAll()
273 {
274     doAll( false );
275 }
276
277 void PrefsTree::cleanAll()
278 {
279     doAll( true );
280 }
281
282 void PrefsTree::doAll( bool doclean )
283 {
284     for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
285              i_cat_index++ )
286     {
287         QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
288         for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
289                  i_sc_index++ )
290         {
291             QTreeWidgetItem *sc_item = cat_item->child( i_sc_index );
292             for( int i_module = 0 ; i_module < sc_item->childCount();
293                      i_module++ )
294             {
295                 PrefsItemData *data = sc_item->child( i_module )->
296                                data( 0, Qt::UserRole).value<PrefsItemData *>();
297                 if( data->panel && doclean )
298                 {
299                     delete data->panel;
300                     data->panel = NULL;
301                 }
302                 else if( data->panel )
303                     data->panel->apply();
304             }
305             PrefsItemData *data = sc_item->data( 0, Qt::UserRole).
306                                             value<PrefsItemData *>();
307             if( data->panel && doclean )
308             {
309                 delete data->panel;
310                 data->panel = NULL;
311             }
312             else if( data->panel )
313                 data->panel->apply();
314         }
315         PrefsItemData *data = cat_item->data( 0, Qt::UserRole).
316                                             value<PrefsItemData *>();
317         if( data->panel && doclean )
318         {
319             delete data->panel;
320             data->panel = NULL;
321         }
322         else if( data->panel )
323             data->panel->apply();
324     }
325 }
326
327 /*********************************************************************
328  * The Panel
329  *********************************************************************/
330 AdvPrefsPanel::AdvPrefsPanel( QWidget *_parent ) : QWidget( _parent )
331 {}
332
333 AdvPrefsPanel::AdvPrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
334                         PrefsItemData * data ) :
335                         QWidget( _parent ), p_intf( _p_intf )
336 {
337     /* Find our module */
338     module_t *p_module = NULL;
339     if( data->i_type == TYPE_CATEGORY )
340         return;
341     else if( data->i_type == TYPE_MODULE )
342         p_module = module_Find( VLC_OBJECT(p_intf), data->psz_name );
343     else
344     {
345         p_module = module_Find( VLC_OBJECT(p_intf), "main" );
346         assert( p_module );
347     }
348
349     unsigned confsize;
350     module_config_t *const p_config = module_GetConfig (p_module, &confsize),
351                     *p_item = p_config,
352                     *p_end = p_config + confsize;
353
354     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
355     {
356         while (p_item < p_end)
357         {
358             if( p_item->i_type == CONFIG_SUBCATEGORY &&
359                             ( data->i_type == TYPE_SUBCATEGORY &&
360                               p_item->value.i == data->i_object_id ) ||
361                             ( data->i_type == TYPE_CATSUBCAT &&
362                               p_item->value.i == data->i_subcat_id ) )
363                 break;
364             p_item++;
365         }
366     }
367
368     /* Widgets now */
369     global_layout = new QVBoxLayout();
370     global_layout->setMargin( 2 );
371     QString head;
372     QString help;
373
374     help = QString( data->help );
375
376     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
377     {
378         head = QString( data->name );
379         p_item++; // Why that ?
380     }
381     else
382     {
383         const char *psz_help = module_GetHelp (p_module);
384         head = QString( qtr( module_GetLongName( p_module ) ) );
385         if( psz_help )
386         {
387             help.append( "\n" );
388             help.append( qtr( psz_help ) );
389         }
390     }
391
392     QLabel *titleLabel = new QLabel( head );
393     QFont titleFont = QApplication::font( static_cast<QWidget*>(0) );
394     titleFont.setPointSize( titleFont.pointSize() + 6 );
395     titleFont.setFamily( "Verdana" );
396     titleLabel->setFont( titleFont );
397
398     // Title <hr>
399     QFrame *title_line = new QFrame;
400     title_line->setFrameShape(QFrame::HLine);
401     title_line->setFrameShadow(QFrame::Sunken);
402
403     QLabel *helpLabel = new QLabel( help, this );
404     helpLabel->setWordWrap( true );
405
406     global_layout->addWidget( titleLabel );
407     global_layout->addWidget( title_line );
408     global_layout->addWidget( helpLabel );
409
410     QGroupBox *box = NULL;
411     QGridLayout *boxlayout = NULL;
412
413     QScrollArea *scroller= new QScrollArea;
414     scroller->setFrameStyle( QFrame::NoFrame );
415     QWidget *scrolled_area = new QWidget;
416
417     QGridLayout *layout = new QGridLayout();
418     int i_line = 0, i_boxline = 0;
419     bool has_hotkey = false;
420
421     if( p_item ) do
422     {
423         if( ( ( data->i_type == TYPE_SUBCATEGORY &&
424                 p_item->value.i != data->i_object_id ) ||
425               ( data->i_type == TYPE_CATSUBCAT  &&
426                 p_item->value.i != data->i_subcat_id ) ) &&
427             ( p_item->i_type == CONFIG_CATEGORY ||
428               p_item->i_type == CONFIG_SUBCATEGORY ) )
429             break;
430         if( p_item->b_internal == VLC_TRUE ) continue;
431
432         if( p_item->i_type == CONFIG_SECTION )
433         {
434             if( box )
435             {
436                 box->setLayout( boxlayout );
437                 layout->addWidget( box, i_line, 0, 1, 2 );
438                 i_line++;
439             }
440             box = new QGroupBox( qtr(p_item->psz_text) );
441             boxlayout = new QGridLayout();
442         }
443         /* Only one hotkey control */
444         if( has_hotkey && p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
445                                          strstr( p_item->psz_name, "key-" ) )
446             continue;
447         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
448                                             strstr( p_item->psz_name, "key-" ) )
449             has_hotkey = true;
450
451         ConfigControl *control;
452         if( ! box )
453             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
454                                         p_item, NULL, layout, i_line );
455         else
456             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
457                                     p_item, NULL, boxlayout, i_boxline );
458         if( !control )
459             continue;
460
461         if( box ) i_boxline++;
462         else i_line++;
463         controls.append( control );
464     }
465     while( !( ( data->i_type == TYPE_SUBCATEGORY ||
466                data->i_type == TYPE_CATSUBCAT ) &&
467              ( p_item->i_type == CONFIG_CATEGORY ||
468                p_item->i_type == CONFIG_SUBCATEGORY ) )
469         && ( ++p_item < p_end ) );
470
471     if( box )
472     {
473         box->setLayout( boxlayout );
474         layout->addWidget( box, i_line, 0, 1, 2 );
475     }
476
477     module_Put( p_module );
478
479     scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
480     scrolled_area->setLayout( layout );
481     scroller->setWidget( scrolled_area );
482     scroller->setWidgetResizable( true );
483     global_layout->addWidget( scroller );
484     setLayout( global_layout );
485 }
486
487 void AdvPrefsPanel::apply()
488 {
489     QList<ConfigControl *>::Iterator i;
490     for( i = controls.begin() ; i != controls.end() ; i++ )
491     {
492         ConfigControl *c = qobject_cast<ConfigControl *>(*i);
493         c->doApply( p_intf );
494     }
495 }
496 void AdvPrefsPanel::clean()
497 {}