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