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