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