]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/complete_preferences.cpp
qt4 - Rename a confusing component
[vlc] / modules / gui / qt4 / components / complete_preferences.cpp
1 /*****************************************************************************
2  * preferences.cpp : "Normal preferences"
3  ****************************************************************************
4  * Copyright (C) 2006 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 #include "qt4.hpp"
43
44 #include <vlc_config_cat.h>
45 #include <vlc_intf_strings.h>
46 #include <assert.h>
47
48 #include "pixmaps/audio.xpm"
49 #include "pixmaps/video.xpm"
50 #include "pixmaps/type_net.xpm"
51 #include "pixmaps/type_playlist.xpm"
52 #include "pixmaps/advanced.xpm"
53 #include "pixmaps/codec.xpm"
54 #include "pixmaps/intf.xpm"
55
56 #define ITEM_HEIGHT 25
57
58 /*********************************************************************
59  * The Tree
60  *********************************************************************/
61 PrefsTree::PrefsTree( intf_thread_t *_p_intf, QWidget *_parent ) :
62                             QTreeWidget( _parent ), p_intf( _p_intf )
63 {
64     setColumnCount( 1 );
65     setAlternatingRowColors( true );
66     header()->hide();
67     setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) );
68
69 #define BI( a,b) QIcon a##_icon = QIcon( QPixmap( b##_xpm ))
70     BI( audio, audio );
71     BI( video, video );
72     BI( input, codec );
73     BI( sout, type_net );
74     BI( advanced, advanced );
75     BI( playlist, type_playlist );
76     BI( interface, intf );
77 #undef BI
78
79     /* Build the tree for the main module */
80     module_t *p_module = NULL;
81     vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
82                                         FIND_ANYWHERE );
83     if( !p_list ) return;
84     for( int i_index = 0; p_module == NULL; i_index++ )
85     {
86         assert (i_index < p_list->i_count);
87
88         p_module = (module_t *)p_list->p_values[i_index].p_object;
89         if( strcmp( p_module->psz_object_name, "main" ) )
90             p_module = NULL;
91     }
92
93     PrefsItemData *data = NULL;
94     QTreeWidgetItem *current_item = NULL;
95     for (size_t i = 0; i < p_module->confsize; i++)
96     {
97         module_config_t *p_item = p_module->p_config + i;
98         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( qfu( config_CategoryNameGet
106                                            ( p_item->value.i ) ) );
107             psz_help = config_CategoryHelpGet( p_item->value.i );
108             if( psz_help )
109                 data->help = QString( qfu(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             if( data &&
139                 ( p_item->value.i == SUBCAT_VIDEO_GENERAL ||
140                   p_item->value.i == SUBCAT_ADVANCED_MISC ||
141                   p_item->value.i == SUBCAT_INPUT_GENERAL ||
142                   p_item->value.i == SUBCAT_INTERFACE_GENERAL ||
143                   p_item->value.i == SUBCAT_SOUT_GENERAL||
144                   p_item->value.i == SUBCAT_PLAYLIST_GENERAL||
145                   p_item->value.i == SUBCAT_AUDIO_GENERAL ) )
146             {
147                 // Data still contains the correct thing
148                 data->i_type = TYPE_CATSUBCAT;
149                 data->i_subcat_id = p_item->value.i;
150                 data->name = QString( qfu( config_CategoryNameGet(
151                                             p_item->value.i )) );
152                 psz_help = config_CategoryHelpGet( p_item->value.i );
153                 if( psz_help )
154                     data->help = QString( qfu(psz_help) );
155                 else
156                     data->help.clear();
157                 current_item->setData( 0, Qt::UserRole,
158                                        QVariant::fromValue( data ) );
159                 continue;
160             }
161             data = new PrefsItemData();
162             data->name = QString( qfu( config_CategoryNameGet(
163                                                         p_item->value.i)) );
164             psz_help = config_CategoryHelpGet( p_item->value.i );
165             if( psz_help )
166                 data->help = QString( qfu(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
186     /* Build the tree of plugins */
187     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
188     {
189         int i_subcategory = -1, i_category = -1, i_options = 0;
190         p_module = (module_t *)p_list->p_values[i_index].p_object;
191
192         // Main module excluded
193         if( !strcmp( p_module->psz_object_name, "main" ) ) continue;
194
195         /* Exclude empty plugins (submodules don't have config options, they
196          * are stored in the parent module) */
197         if( p_module->b_submodule ) continue;
198
199         for (size_t i = 0; i < p_module->confsize; i++)
200         {
201             module_config_t *p_item = p_module->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             if( p_item->i_type & CONFIG_ITEM )
208                 i_options++;
209
210             if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
211                 break;
212         }
213         if( !i_options ) continue; // Nothing to display
214
215         // Locate the category item;
216         QTreeWidgetItem *subcat_item = NULL;
217         bool b_found = false;
218         for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount();
219                                    i_cat_index++ )
220         {
221             QTreeWidgetItem *cat_item = topLevelItem( i_cat_index );
222             PrefsItemData *data = cat_item->data( 0, Qt::UserRole ).
223                                              value<PrefsItemData *>();
224             if( data->i_object_id == i_category )
225             {
226                 for( int i_sc_index = 0; i_sc_index < cat_item->childCount();
227                          i_sc_index++ )
228                 {
229                     subcat_item = cat_item->child( i_sc_index );
230                     PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole).
231                                                 value<PrefsItemData *>();
232                     if( sc_data && sc_data->i_object_id == i_subcategory )
233                     {
234                         b_found = true;
235                         break;
236                     }
237                 }
238                 if( !b_found )
239                 {
240                     subcat_item = cat_item;
241                     b_found = true;
242                 }
243                 break;
244             }
245         }
246         if( !b_found ) continue;
247
248         PrefsItemData *module_data = new PrefsItemData();
249         module_data->b_submodule = p_module->b_submodule;
250         module_data->i_type = TYPE_MODULE;
251         module_data->psz_name = strdup( p_module->psz_object_name );
252         module_data->i_object_id = p_module->b_submodule ?
253                          ((module_t *)p_module->p_parent)->i_object_id :
254                          p_module->i_object_id;
255         module_data->help.clear();
256         // TODO image
257         QTreeWidgetItem *module_item = new QTreeWidgetItem();
258         module_item->setText( 0, qfu( p_module->psz_shortname ?
259                       p_module->psz_shortname : p_module->psz_object_name) );
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 PrefsPanel::PrefsPanel( QWidget *_parent ) : QWidget( _parent )
334 {}
335
336 PrefsPanel::PrefsPanel( 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_t *) vlc_object_get( p_intf, data->i_object_id );
346     else
347     {
348         p_module = config_FindModule( VLC_OBJECT(p_intf), "main" );
349         assert( p_module );
350         vlc_object_yield( p_module );
351     }
352
353     module_t *p_realmodule = p_module->b_submodule
354             ? (module_t *)(p_module->p_parent)
355             : p_module;
356
357     module_config_t *p_item = p_realmodule->p_config;
358     module_config_t *p_end = p_item + p_realmodule->confsize;
359
360     if( data->i_type == TYPE_SUBCATEGORY || data->i_type ==  TYPE_CATSUBCAT )
361     {
362         while (p_item < p_end)
363         {
364             if( p_item->i_type == CONFIG_SUBCATEGORY &&
365                             ( data->i_type == TYPE_SUBCATEGORY &&
366                               p_item->value.i == data->i_object_id ) ||
367                             ( data->i_type == TYPE_CATSUBCAT &&
368                               p_item->value.i == data->i_subcat_id ) )
369                 break;
370             p_item++;
371         }
372     }
373
374     global_layout = new QVBoxLayout();
375     QString head;
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         head = QString( qfu(p_module->psz_longname) );
384         if( p_module->psz_help )
385         {
386             head.append( "\n" );
387             head.append( qfu( p_module->psz_help ) );
388         }
389     }
390
391     QLabel *label = new QLabel( head );
392     global_layout->addWidget( label );
393     QFont myFont = QApplication::font( static_cast<QWidget*>(0) );
394     myFont.setPointSize( myFont.pointSize() + 3 ); myFont.setBold( true );
395
396     label->setFont( myFont );
397     QLabel *help = new QLabel( data->help, this );
398     help->setWordWrap( true );
399
400     global_layout->addWidget( help );
401
402     QGroupBox *box = NULL;
403     QGridLayout *boxlayout = NULL;
404
405     QScrollArea *scroller= new QScrollArea;
406     scroller->setFrameStyle( QFrame::NoFrame );
407     QWidget *scrolled_area = new QWidget;
408
409     QGridLayout *layout = new QGridLayout();
410     int i_line = 0, i_boxline = 0;
411     bool has_hotkey = false;
412
413     if( p_item ) do
414     {
415         if( ( ( data->i_type == TYPE_SUBCATEGORY &&
416                 p_item->value.i != data->i_object_id ) ||
417               ( data->i_type == TYPE_CATSUBCAT  &&
418                 p_item->value.i != data->i_subcat_id ) ) &&
419             ( p_item->i_type == CONFIG_CATEGORY ||
420               p_item->i_type == CONFIG_SUBCATEGORY ) )
421             break;
422         if( p_item->b_internal == VLC_TRUE ) continue;
423
424         if( p_item->i_type == CONFIG_SECTION )
425         {
426             if( box )
427             {
428                 box->setLayout( boxlayout );
429                 layout->addWidget( box, i_line, 0, 1, 2 );
430                 i_line++;
431             }
432             box = new QGroupBox( qfu(p_item->psz_text) );
433             boxlayout = new QGridLayout();
434         }
435         /* Only one hotkey control */
436         if( has_hotkey && p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
437                                          strstr( p_item->psz_name, "key-" ) )
438             continue;
439         if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
440                                             strstr( p_item->psz_name, "key-" ) )
441             has_hotkey = true;
442
443         ConfigControl *control;
444         if( ! box )
445             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
446                                         p_item, NULL, layout, i_line );
447         else
448             control = ConfigControl::createControl( VLC_OBJECT( p_intf ),
449                                     p_item, NULL, boxlayout, i_boxline );
450         if( !control )
451             continue;
452
453         if( has_hotkey )
454         {
455             /* A hotkey widget takes 2 lines */
456             if( box ) i_boxline ++;
457             else i_line++;
458         }
459
460         if( box ) i_boxline++;
461         else i_line++;
462         controls.append( control );
463     }
464     while( !( ( data->i_type == TYPE_SUBCATEGORY ||
465                data->i_type == TYPE_CATSUBCAT ) &&
466              ( p_item->i_type == CONFIG_CATEGORY ||
467                p_item->i_type == CONFIG_SUBCATEGORY ) )
468         && ( ++p_item < p_end ) );
469
470     if( box )
471     {
472         box->setLayout( boxlayout );
473         layout->addWidget( box, i_line, 0, 1, 2 );
474     }
475
476     vlc_object_release( p_module );
477
478     scrolled_area->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Fixed );
479     scrolled_area->setLayout( layout );
480     scroller->setWidget( scrolled_area );
481     scroller->setWidgetResizable( true );
482     global_layout->addWidget( scroller );
483     setLayout( global_layout );
484 }
485
486 void PrefsPanel::apply()
487 {
488     QList<ConfigControl *>::Iterator i;
489     for( i = controls.begin() ; i != controls.end() ; i++ )
490     {
491         ConfigControl *c = qobject_cast<ConfigControl *>(*i);
492         c->doApply( p_intf );
493     }
494 }
495 void PrefsPanel::clean()
496 {}