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