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