]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/simple_preferences.cpp
Qt4 - SPrefs, doing funny things with qobject_cast to improve a bit the code...
[vlc] / modules / gui / qt4 / components / simple_preferences.cpp
1 /*****************************************************************************
2  * simple_preferences.cpp : "Simple preferences"
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id: preferences.cpp 16348 2006-08-25 21:10:10Z zorglub $
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Antoine Cellerier <dionoea@videolan.org>
9  *          Jean-Baptiste Kempf <jb@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #include "components/simple_preferences.hpp"
27 #include "components/preferences_widgets.hpp"
28
29
30
31 #include <vlc_config_cat.h>
32 #include <vlc_configuration.h>
33
34 #include <QString>
35 #include <QFont>
36 #include <QToolButton>
37 #include <QButtonGroup>
38 #include <QUrl>
39 #include <QVBoxLayout>
40
41 #define ICON_HEIGHT 64
42 #define BUTTON_HEIGHT 74
43
44 /*********************************************************************
45  * The List of categories
46  *********************************************************************/
47 SPrefsCatList::SPrefsCatList( intf_thread_t *_p_intf, QWidget *_parent ) :
48                                   QWidget( _parent ), p_intf( _p_intf )
49 {
50     QVBoxLayout *layout = new QVBoxLayout();
51
52     QButtonGroup *buttonGroup = new QButtonGroup( this );
53     buttonGroup->setExclusive ( true );
54     CONNECT( buttonGroup, buttonClicked ( int ),
55             this, switchPanel( int ) );
56
57 #define ADD_CATEGORY( button, label, icon, numb )                           \
58     QToolButton * button = new QToolButton( this );                         \
59     button->setIcon( QIcon( ":/pixmaps/" #icon ) );                         \
60     button->setIconSize( QSize( ICON_HEIGHT , ICON_HEIGHT ) );              \
61     button->setText( label );                                               \
62     button->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );              \
63     button->resize( BUTTON_HEIGHT , BUTTON_HEIGHT);                         \
64     button->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding) ;  \
65     button->setAutoRaise( true );                                           \
66     button->setCheckable( true );                                           \
67     buttonGroup->addButton( button, numb );                                 \
68     layout->addWidget( button );
69
70     ADD_CATEGORY( SPrefsInterface, qtr("Interface"),
71                   spref_cone_Interface_64.png, 0 );
72     ADD_CATEGORY( SPrefsAudio, qtr("Audio"), spref_cone_Audio_64.png, 1 );
73     ADD_CATEGORY( SPrefsVideo, qtr("Video"), spref_cone_Video_64.png, 2 );
74     ADD_CATEGORY( SPrefsSubtitles, qtr("Subtitles"),
75                   spref_cone_Subtitles_64.png, 3 );
76     ADD_CATEGORY( SPrefsInputAndCodecs, qtr("Input and Codecs"),
77                   spref_cone_Input_64.png, 4 );
78     ADD_CATEGORY( SPrefsHotkeys, qtr("Hotkeys"), spref_cone_Hotkeys_64.png, 5 );
79
80     SPrefsInterface->setChecked( true );
81     layout->setMargin( 0 );
82     layout->setSpacing( 1 );
83
84     this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
85     setLayout( layout );
86
87 }
88
89 void SPrefsCatList::switchPanel( int i )
90 {
91     emit currentItemChanged( i );
92 }
93
94 /*********************************************************************
95  * The Panels
96  *********************************************************************/
97 SPrefsPanel::SPrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
98                           int _number ) : QWidget( _parent ), p_intf( _p_intf )
99 {
100     module_config_t *p_config;
101     ConfigControl *control;
102     number = _number;
103
104 #define CONFIG_GENERIC( option, type, label, qcontrol )                   \
105             p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
106             if( p_config )                                                \
107             {                                                             \
108                 control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
109                            p_config, label, ui.qcontrol, false );         \
110                 controls.append( control );                               \
111             }
112
113 #define CONFIG_GENERIC_NO_BOOL( option, type, label, qcontrol )           \
114             p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
115             if( p_config )                                                \
116             {                                                             \
117                 control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
118                            p_config, label, ui.qcontrol );                \
119                 controls.append( control );                               \
120             }
121
122 #define CONFIG_GENERIC_FILE( option, type, label, qcontrol, qbutton )         \
123                 p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
124                 if( p_config )                                                \
125                 {                                                             \
126                     control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
127                                p_config, label, ui.qcontrol, ui.qbutton,      \
128                             false );                                          \
129                     controls.append( control );                               \
130                 }
131
132 #define START_SPREFS_CAT( name , label )    \
133         case SPrefs ## name:                \
134         {                                   \
135             Ui::SPrefs ## name ui;      \
136             ui.setupUi( panel );            \
137             panel_label->setText( label );
138
139 #define END_SPREFS_CAT      \
140             break;          \
141         }
142
143     QVBoxLayout *panel_layout = new QVBoxLayout();
144     QWidget *panel = new QWidget();
145     panel_layout->setMargin( 3 );
146
147     // Title Label
148     QLabel *panel_label = new QLabel;
149     QFont labelFont = QApplication::font( static_cast<QWidget*>(0) );
150     labelFont.setPointSize( labelFont.pointSize() + 6 );
151     labelFont.setFamily( "Verdana" );
152     panel_label->setFont( labelFont );
153
154     // Title <hr>
155     QFrame *title_line = new QFrame;
156     title_line->setFrameShape(QFrame::HLine);
157     title_line->setFrameShadow(QFrame::Sunken);
158
159     QFont italicFont = QApplication::font( static_cast<QWidget*>(0) );
160     italicFont.setItalic( true );
161
162     switch( number )
163     {
164         /******************************
165          * VIDEO Panel Implementation *
166          ******************************/
167         START_SPREFS_CAT( Video , qtr("General video settings") );
168             CONFIG_GENERIC( "video", Bool, NULL, enableVideo );
169
170             CONFIG_GENERIC( "fullscreen", Bool, NULL, fullscreen );
171             CONFIG_GENERIC( "overlay", Bool, NULL, overlay );
172             CONFIG_GENERIC( "video-on-top", Bool, NULL, alwaysOnTop );
173             CONFIG_GENERIC( "video-deco", Bool, NULL, windowDecorations );
174             CONFIG_GENERIC( "skip-frames" , Bool, NULL, skipFrames );
175             CONFIG_GENERIC( "overlay", Bool, NULL, overlay );
176             CONFIG_GENERIC( "vout", Module, NULL, outputModule );
177
178 #ifdef WIN32
179             CONFIG_GENERIC( "directx-wallpaper" , Bool , NULL, wallpaperMode );
180             CONFIG_GENERIC( "directx-device", StringList, NULL,
181                             dXdisplayDevice );
182 #else
183             ui.directXBox->setVisible( false );
184 #endif
185
186             CONFIG_GENERIC_FILE( "snapshot-path", Directory, NULL,
187                     snapshotsDirectory, snapshotsDirectoryBrowse );
188             CONFIG_GENERIC( "snapshot-prefix", String, NULL, snapshotsPrefix );
189             CONFIG_GENERIC( "snapshot-sequential", Bool, NULL,
190                             snapshotsSequentialNumbering );
191             CONFIG_GENERIC( "snapshot-format", StringList, NULL,
192                             snapshotsFormat );
193          END_SPREFS_CAT;
194
195         /******************************
196          * AUDIO Panel Implementation *
197          ******************************/
198         START_SPREFS_CAT( Audio, qtr("General audio settings") );
199
200             CONFIG_GENERIC( "audio", Bool, NULL, enableAudio );
201
202             /* and hide if necessary */
203
204 #ifdef WIN32
205             ui.OSSControl->hide();
206             ui.alsaControl->hide();
207 #else
208             ui.DirectXControl->hide();
209 #endif
210             ui.lastfm_user_edit->hide();
211             ui.lastfm_user_label->hide();
212             ui.lastfm_pass_edit->hide();
213             ui.lastfm_pass_label->hide();
214
215             /* General Audio Options */
216             CONFIG_GENERIC_NO_BOOL( "volume" , IntegerRangeSlider, NULL,
217                                      defaultVolume );
218             CONFIG_GENERIC( "audio-language" , String , NULL,
219                             preferredAudioLanguage );
220
221             CONFIG_GENERIC( "spdif", Bool, NULL, spdifBox );
222             CONFIG_GENERIC( "force-dolby-surround" , IntegerList , NULL,
223                             detectionDolby );
224
225             CONFIG_GENERIC( "headphone-dolby" , Bool , NULL, headphoneEffect );
226
227             CONFIG_GENERIC_NO_BOOL( "norm-max-level" , Float , NULL,
228                                     volNormSpin );
229             CONFIG_GENERIC( "audio-visual" , Module , NULL, visualisation);
230
231             /* Audio Output Specifics */
232             CONFIG_GENERIC( "aout", Module, NULL, outputModule );
233
234             CONNECT( ui.outputModule, currentIndexChanged( int ), this,
235                              updateAudioOptions( int ) );
236             
237
238         //TODO: use modules_Exists
239 #ifndef WIN32
240             CONFIG_GENERIC( "alsadev" , StringList , ui.alsaLabel, alsaDevice );
241             CONFIG_GENERIC_FILE( "dspdev" , File , ui.OSSLabel, OSSDevice,
242                                  OSSBrowse );
243 #else
244             CONFIG_GENERIC( "directx-audio-device", IntegerList,
245                     ui.DirectXLabel, DirectXDevice );
246 #endif
247         // File exists everywhere
248             CONFIG_GENERIC_FILE( "audiofile-file" , File , ui.fileLabel,
249                                  fileName, fileBrowseButton );
250
251             optionWidgets.append( ui.alsaControl );
252             optionWidgets.append( ui.OSSControl );
253             optionWidgets.append( ui.DirectXControl );
254             optionWidgets.append( ui.fileControl );
255             optionWidgets.append( ui.outputModule );
256             optionWidgets.append( ui.volNormBox );
257             updateAudioOptions( ui.outputModule->currentIndex() );
258
259             /* LastFM */
260             CONFIG_GENERIC( "lastfm-username", String, ui.lastfm_user_label,
261                          lastfm_user_edit );
262             CONFIG_GENERIC( "lastfm-password", String, ui.lastfm_pass_label,
263                          lastfm_pass_edit );
264
265             if( config_ExistIntf( VLC_OBJECT( p_intf ), "audioscrobbler" ) )
266                 ui.lastfm->setCheckState( Qt::Checked );
267             else
268                 ui.lastfm->setCheckState( Qt::Unchecked );
269             CONNECT( ui.lastfm, stateChanged( int ), this ,
270                     lastfm_Changed( int ) );
271
272             /* Normalizer */
273           
274             CONNECT( ui.volNormBox, toggled( bool ), ui.volNormSpin,
275                      setEnabled( bool ) );
276             qs_filter = qfu( config_GetPsz( p_intf, "audio-filter" ) );
277             bool b_normalizer = ( qs_filter.contains( "volnorm" ) );
278             {
279                 ui.volNormBox->setChecked( b_normalizer );
280                 ui.volNormSpin->setEnabled( b_normalizer );
281             }
282
283         END_SPREFS_CAT;
284
285         /* Input and Codecs Panel Implementation */
286         START_SPREFS_CAT( InputAndCodecs, qtr("Input & Codecs settings") );
287
288             
289             /* Disk Devices */
290             {
291                 ui.DVDDevice->setToolTip(
292                     qtr( "If this propriety is blank, then you have\n"
293                          "values for DVD, VCD, and CDDA.\n"
294                          "You can define a unique one or set that in"
295                          "the advanced preferences" ) );
296                 char *psz_dvddiscpath = config_GetPsz( p_intf, "dvd" );
297                 char *psz_vcddiscpath = config_GetPsz( p_intf, "vcd" );
298                 char *psz_cddadiscpath = config_GetPsz( p_intf, "cd-audio" );
299                 if( ( *psz_cddadiscpath == *psz_dvddiscpath )
300                    && ( *psz_dvddiscpath == *psz_vcddiscpath ) )
301                 {
302                     ui.DVDDevice->setText( qfu( psz_dvddiscpath ) );
303                 }
304                 delete psz_cddadiscpath; delete psz_dvddiscpath;
305                 delete psz_vcddiscpath;
306             }
307
308             CONFIG_GENERIC_NO_BOOL( "server-port", Integer, NULL, UDPPort );
309             CONFIG_GENERIC( "http-proxy", String , NULL, proxy );
310         
311             /* Caching */
312             #define addToCachingBox( str, cachingNumber ) \
313                 ui.cachingCombo->addItem( str, QVariant( cachingNumber ) );
314             addToCachingBox( "Custom", CachingCustom );
315             addToCachingBox( "Lowest latency", CachingLowest );
316             addToCachingBox( "Low latency", CachingLow );
317             addToCachingBox( "Normal", CachingNormal );
318             addToCachingBox( "High latency", CachingHigh );
319             addToCachingBox( "Higher latency", CachingHigher );
320
321             CONFIG_GENERIC_NO_BOOL( "ffmpeg-pp-q", Integer, NULL, PostProcLevel );
322             CONFIG_GENERIC( "avi-index", IntegerList, NULL, AviRepair );
323             CONFIG_GENERIC( "rtsp-tcp", Bool, NULL, RTSP_TCPBox );
324 #ifdef WIN32
325             CONFIG_GENERIC( "prefer-system-codecs", Bool, NULL, systemCodecBox );
326 #else
327             ui.systemCodecBox->hide();
328 #endif  
329             /* Access Filters */
330             qs_filter = qfu( config_GetPsz( p_intf, "access-filter" ) );
331             ui.timeshiftBox->setChecked( qs_filter.contains( "timeshift" ) );
332             ui.dumpBox->setChecked( qs_filter.contains( "dump" ) );
333             ui.recordBox->setChecked( qs_filter.contains( "record" ) );
334             ui.bandwidthBox->setChecked( qs_filter.contains( "bandwidth" ) );
335             
336             optionWidgets.append( ui.recordBox );
337             optionWidgets.append( ui.dumpBox );
338             optionWidgets.append( ui.bandwidthBox );
339             optionWidgets.append( ui.timeshiftBox );
340             optionWidgets.append( ui.DVDDevice );
341             optionWidgets.append( ui.cachingCombo );
342         END_SPREFS_CAT;
343
344         /*******************
345          * Interface Panel *
346          *******************/
347         START_SPREFS_CAT( Interface, qtr("Interface settings") );
348             ui.defaultLabel->setFont( italicFont );
349             ui.skinsLabel->setFont( italicFont );
350
351 #if defined( WIN32 ) || defined (__APPLE__)
352             CONFIG_GENERIC( "language", StringList, NULL, language );
353 #else
354             ui.language->hide();
355             ui.languageLabel->hide();
356 #endif
357
358             /* interface */
359             char *psz_intf = config_GetPsz( p_intf, "intf" );
360             if( psz_intf )
361             {
362                 msg_Dbg( p_intf, "Interface in config file: %s", psz_intf );
363                 if( strstr( psz_intf, "skin" ) )
364                     ui.skins->setChecked( true );
365                 else if( strstr( psz_intf, "qt" ) )
366                     ui.qt4->setChecked( true );
367             }
368             delete psz_intf;
369
370             optionWidgets.append( ui.skins );
371             optionWidgets.append( ui.qt4 );
372
373             CONFIG_GENERIC( "qt-always-video", Bool, NULL, qtAlwaysVideo );
374             CONFIG_GENERIC_FILE( "skins2-last", File, NULL, fileSkin,
375                     skinBrowse );
376 #if defined( WIN32 ) || defined( HAVE_DBUS_3 )
377             CONFIG_GENERIC( "one-instance", Bool, NULL, OneInterfaceMode );
378             CONFIG_GENERIC( "playlist-enqueue", Bool, NULL,
379                     EnqueueOneInterfaceMode );
380 #else
381             ui.OneInterfaceBox->hide();
382 #endif
383         END_SPREFS_CAT;
384
385         START_SPREFS_CAT( Subtitles, qtr("Subtitles & OSD settings") );
386             CONFIG_GENERIC( "osd", Bool, NULL, OSDBox);
387
388             CONFIG_GENERIC( "subsdec-encoding", StringList, NULL, encoding );
389             CONFIG_GENERIC( "sub-language", String, NULL, preferredLanguage );
390             CONFIG_GENERIC_FILE( "freetype-font", File, NULL, font,
391                             fontBrowse );
392             CONFIG_GENERIC( "freetype-color", IntegerList, NULL, fontColor );
393             CONFIG_GENERIC( "freetype-rel-fontsize", IntegerList, NULL,
394                             fontSize );
395             CONFIG_GENERIC( "freetype-effect", IntegerList, NULL, effect );
396
397         END_SPREFS_CAT;
398
399         START_SPREFS_CAT( Hotkeys, "Configure Hotkeys" );
400         //FIMXE
401         END_SPREFS_CAT;
402         }
403
404     panel_layout->addWidget( panel_label );
405     panel_layout->addWidget( title_line );
406     panel_layout->addWidget( panel );
407     panel_layout->addStretch( 2 );
408
409     setLayout( panel_layout );
410 }
411
412 void SPrefsPanel::updateAudioOptions( int number)
413 {
414     QString value = qobject_cast<QComboBox *>(optionWidgets[audioOutCoB])
415                                             ->itemData( number ).toString();
416
417 #ifndef WIN32
418     optionWidgets[ossW]->setVisible( ( value == "oss" ) );
419     optionWidgets[alsaW]->setVisible( ( value == "alsa" ) );
420 #else
421     optionWidgets[directxW]->setVisible( ( value == "directx" ) );
422 #endif
423     optionWidgets[fileW]->setVisible( ( value == "aout_file" ) );
424 }
425
426 void SPrefsPanel::apply()
427 {
428     msg_Dbg( p_intf, "Trying to save the %i simple panel", number );
429
430     /* Generic save for ever panel */
431     QList<ConfigControl *>::Iterator i;
432     for( i = controls.begin() ; i != controls.end() ; i++ )
433     {
434         ConfigControl *c = qobject_cast<ConfigControl *>(*i);
435         c->doApply( p_intf );
436     }
437
438     /* Devices */
439     if( number == SPrefsInputAndCodecs )
440     {
441         /* Device default selection */
442         char *psz_devicepath = 
443               qtu( qobject_cast<QLineEdit *>(optionWidgets[inputLE] )->text() );
444         if( !EMPTY_STR( psz_devicepath ) )
445         {
446             config_PutPsz( p_intf, "dvd", psz_devicepath );
447             config_PutPsz( p_intf, "vcd", psz_devicepath );
448             config_PutPsz( p_intf, "cd-audio", psz_devicepath );
449         }
450
451         /* Access filters */
452 #define saveBox( name, box ) {\
453         if( box->isChecked() ) { \
454             if( b_first ) { \
455                 qs_filter.append( name ); \
456                 b_first = false; \
457             } \
458             else qs_filter.append( ":" ).append( name ); \
459         } } 
460
461         bool b_first = true;    
462         saveBox( "record", qobject_cast<QCheckBox *>(optionWidgets[recordChB]) );
463         saveBox( "dump", qobject_cast<QCheckBox *>(optionWidgets[dumpChB]) );
464         saveBox( "timeshift", qobject_cast<QCheckBox *>(optionWidgets[timeshiftChB]) );
465         saveBox( "bandwidth", qobject_cast<QCheckBox *>(optionWidgets[bandwidthChB] ) );
466         config_PutPsz( p_intf, "access-filter", qtu( qs_filter ) );
467
468         QComboBox *cachingCombo = qobject_cast<QComboBox *>(optionWidgets[cachingCoB]);
469         /* Caching */
470         msg_Dbg( p_intf, "%i", 
471                 cachingCombo->itemData( cachingCombo->currentIndex() ).toInt() );
472     }
473
474     /* Interfaces */
475     if( number == SPrefsInterface )
476     {
477         if( qobject_cast<QRadioButton *>(optionWidgets[skinRB])->isChecked() )
478             config_PutPsz( p_intf, "intf", "skins2" );
479         if( qobject_cast<QRadioButton *>(optionWidgets[qtRB])->isChecked() )
480             config_PutPsz( p_intf, "intf", "qt4" );
481     }
482
483     if( number == SPrefsAudio )
484     {
485         bool b_normChecked = 
486             qobject_cast<QCheckBox *>(optionWidgets[normalizerChB])->isChecked();
487         if( qs_filter.isEmpty() )
488         {
489             /* the psz_filter is already empty, so we just append it needed */
490             if( b_normChecked ) qs_filter = "volnorm";
491         }
492         else /* Not Empty */
493         {
494             if( qs_filter.contains( "volnorm" ) )
495             {
496                 /* The qs_filter not empty and contains "volnorm" 
497                    that we have to remove */
498                 if( !b_normChecked )
499                 {
500                     /* Ugly :D */
501                     qs_filter.remove( "volnorm:" );
502                     qs_filter.remove( ":volnorm" );
503                     qs_filter.remove( "volnorm" );
504                 }
505             }
506             else /* qs_filter not empty, but doesn't have volnorm inside */
507                 if( b_normChecked ) qs_filter.append( ":volnorm" );
508         }
509         config_PutPsz( p_intf, "audio-filter", qtu( qs_filter ) );
510     }
511 }
512
513 void SPrefsPanel::clean()
514 {}
515
516 void SPrefsPanel::lastfm_Changed( int i_state )
517 {
518     if( i_state == Qt::Checked )
519         config_AddIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
520     else if( i_state == Qt::Unchecked )
521         config_RemoveIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
522 }