]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/extended_panels.cpp
Fix alignment and appearance of ISO standard equalizer centers in Qt GUI
[vlc] / modules / gui / qt4 / components / extended_panels.cpp
1 /*****************************************************************************
2  * extended_panels.cpp : Extended controls panels
3  ****************************************************************************
4  * Copyright (C) 2006-2012 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Antoine Cellerier <dionoea .t videolan d@t 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 #define __STDC_FORMAT_MACROS 1
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <math.h>
33
34 #include <QLabel>
35 #include <QVariant>
36 #include <QString>
37 #include <QFont>
38 #include <QGridLayout>
39 #include <QComboBox>
40 #include <QTimer>
41 #include <QFileDialog>
42
43 #include "components/extended_panels.hpp"
44 #include "dialogs/preferences.hpp"
45 #include "qt4.hpp"
46 #include "input_manager.hpp"
47 #include "util/qt_dirs.hpp"
48
49 #include "../../audio_filter/equalizer_presets.h"
50 #include <vlc_intf_strings.h>
51 #include <vlc_vout.h>
52 #include <vlc_modules.h>
53 #include <vlc_plugin.h>
54
55 #include <vlc_charset.h> /* us_strtod */
56
57 static void ChangeVFiltersString( struct intf_thread_t *p_intf, const char *psz_name, bool b_add );
58
59 #if 0
60 class ConfClickHandler : public QObject
61 {
62 public:
63     ConfClickHandler( intf_thread_t *_p_intf, ExtVideo *_e ) : QObject ( _e ) {
64         e = _e; p_intf = _p_intf;
65     }
66     virtual ~ConfClickHandler() {}
67     bool eventFilter( QObject *obj, QEvent *evt )
68     {
69         if( evt->type() == QEvent::MouseButtonPress )
70         {
71             e->gotoConf( obj );
72             return true;
73         }
74         return false;
75     }
76 private:
77     ExtVideo* e;
78     intf_thread_t *p_intf;
79 };
80 #endif
81
82 const QString ModuleFromWidgetName( QObject *obj )
83 {
84     return obj->objectName().replace( "Enable","" );
85 }
86
87 QString OptionFromWidgetName( QObject *obj )
88 {
89     /* Gruik ? ... nah */
90     QString option = obj->objectName().replace( "Slider", "" )
91                                       .replace( "Combo" , "" )
92                                       .replace( "Dial"  , "" )
93                                       .replace( "Check" , "" )
94                                       .replace( "Spin"  , "" )
95                                       .replace( "Text"  , "" );
96     for( char a = 'A'; a <= 'Z'; a++ )
97     {
98         option = option.replace( QString( a ),
99                                  QString( '-' ) + QString( a + 'a' - 'A' ) );
100     }
101     return option;
102 }
103
104 ExtVideo::ExtVideo( intf_thread_t *_p_intf, QTabWidget *_parent ) :
105             QObject( _parent ), p_intf( _p_intf )
106 {
107     ui.setupUi( _parent );
108
109 #define SETUP_VFILTER( widget ) \
110     { \
111         vlc_object_t *p_obj = ( vlc_object_t * ) \
112             vlc_object_find_name( p_intf->p_libvlc, \
113                                   #widget ); \
114         QCheckBox *checkbox = qobject_cast<QCheckBox*>( ui.widget##Enable ); \
115         QGroupBox *groupbox = qobject_cast<QGroupBox*>( ui.widget##Enable ); \
116         if( p_obj ) \
117         { \
118             vlc_object_release( p_obj ); \
119             if( checkbox ) checkbox->setChecked( true ); \
120             else if (groupbox) groupbox->setChecked( true ); \
121         } \
122         else \
123         { \
124             if( checkbox ) checkbox->setChecked( false ); \
125             else if (groupbox)  groupbox->setChecked( false ); \
126         } \
127     } \
128     CONNECT( ui.widget##Enable, clicked(), this, updateFilters() );
129
130
131 #define SETUP_VFILTER_OPTION( widget, signal ) \
132     initComboBoxItems( ui.widget ); \
133     setWidgetValue( ui.widget ); \
134     CONNECT( ui.widget, signal, this, updateFilterOptions() );
135
136     SETUP_VFILTER( adjust )
137     SETUP_VFILTER_OPTION( hueSlider, valueChanged( int ) )
138     SETUP_VFILTER_OPTION( contrastSlider, valueChanged( int ) )
139     SETUP_VFILTER_OPTION( brightnessSlider, valueChanged( int ) )
140     SETUP_VFILTER_OPTION( saturationSlider, valueChanged( int ) )
141     SETUP_VFILTER_OPTION( gammaSlider, valueChanged( int ) )
142     SETUP_VFILTER_OPTION( brightnessThresholdCheck, stateChanged( int ) )
143
144     SETUP_VFILTER( extract )
145     SETUP_VFILTER_OPTION( extractComponentText, textChanged( const QString& ) )
146
147     SETUP_VFILTER( posterize )
148
149     SETUP_VFILTER( colorthres )
150     SETUP_VFILTER_OPTION( colorthresColorText, textChanged( const QString& ) )
151     SETUP_VFILTER_OPTION( colorthresSaturationthresSlider, valueChanged( int ) )
152     SETUP_VFILTER_OPTION( colorthresSimilaritythresSlider, valueChanged( int ) )
153
154     SETUP_VFILTER( sepia )
155     SETUP_VFILTER_OPTION( sepiaIntensitySpin, valueChanged( int ) )
156
157     SETUP_VFILTER( invert )
158
159     SETUP_VFILTER( gradient )
160     SETUP_VFILTER_OPTION( gradientModeCombo, currentIndexChanged( QString ) )
161     SETUP_VFILTER_OPTION( gradientTypeCheck, stateChanged( int ) )
162     SETUP_VFILTER_OPTION( gradientCartoonCheck, stateChanged( int ) )
163
164     SETUP_VFILTER( motionblur )
165     SETUP_VFILTER_OPTION( blurFactorSlider, valueChanged( int ) )
166
167     SETUP_VFILTER( motiondetect )
168
169     SETUP_VFILTER( psychedelic )
170
171     SETUP_VFILTER( sharpen )
172     SETUP_VFILTER_OPTION( sharpenSigmaSlider, valueChanged( int ) )
173
174     SETUP_VFILTER( ripple )
175
176     SETUP_VFILTER( wave )
177
178     SETUP_VFILTER( transform )
179     SETUP_VFILTER_OPTION( transformTypeCombo, currentIndexChanged( QString ) )
180
181     SETUP_VFILTER( rotate )
182     SETUP_VFILTER_OPTION( rotateAngleDial, valueChanged( int ) )
183     ui.rotateAngleDial->setWrapping( true );
184     ui.rotateAngleDial->setNotchesVisible( true );
185
186     SETUP_VFILTER( puzzle )
187     SETUP_VFILTER_OPTION( puzzleRowsSpin, valueChanged( int ) )
188     SETUP_VFILTER_OPTION( puzzleColsSpin, valueChanged( int ) )
189     SETUP_VFILTER_OPTION( puzzleBlackSlotCheck, stateChanged( int ) )
190
191     SETUP_VFILTER( magnify )
192
193     SETUP_VFILTER( clone )
194     SETUP_VFILTER_OPTION( cloneCountSpin, valueChanged( int ) )
195
196     SETUP_VFILTER( wall )
197     SETUP_VFILTER_OPTION( wallRowsSpin, valueChanged( int ) )
198     SETUP_VFILTER_OPTION( wallColsSpin, valueChanged( int ) )
199
200
201     SETUP_VFILTER( erase )
202     SETUP_VFILTER_OPTION( eraseMaskText, editingFinished() )
203     SETUP_VFILTER_OPTION( eraseYSpin, valueChanged( int ) )
204     SETUP_VFILTER_OPTION( eraseXSpin, valueChanged( int ) )
205     BUTTONACT( ui.eraseBrowseBtn, browseEraseFile() );
206
207     SETUP_VFILTER( marq )
208     SETUP_VFILTER_OPTION( marqMarqueeText, textChanged( const QString& ) )
209     SETUP_VFILTER_OPTION( marqPositionCombo, currentIndexChanged( QString ) )
210
211     SETUP_VFILTER( logo )
212     SETUP_VFILTER_OPTION( logoFileText, editingFinished() )
213     SETUP_VFILTER_OPTION( logoYSpin, valueChanged( int ) )
214     SETUP_VFILTER_OPTION( logoXSpin, valueChanged( int ) )
215     SETUP_VFILTER_OPTION( logoOpacitySlider, valueChanged( int ) )
216     BUTTONACT( ui.logoBrowseBtn, browseLogo() );
217
218     SETUP_VFILTER( gradfun )
219     SETUP_VFILTER_OPTION( gradfunRadiusSlider, valueChanged( int ) )
220
221     SETUP_VFILTER( grain )
222     SETUP_VFILTER_OPTION( grainVarianceSlider, valueChanged( int ) )
223
224     SETUP_VFILTER( mirror )
225
226     SETUP_VFILTER( gaussianblur )
227     SETUP_VFILTER_OPTION( gaussianbluSigmaSlider, valueChanged( int ) )
228
229     SETUP_VFILTER( antiflicker )
230     SETUP_VFILTER_OPTION( antiflickerSofteningSizeSlider, valueChanged( int ) )
231
232
233     if( module_exists( "atmo" ) )
234     {
235         SETUP_VFILTER( atmo )
236         SETUP_VFILTER_OPTION( atmoEdgeweightningSlider, valueChanged( int ) )
237         SETUP_VFILTER_OPTION( atmoBrightnessSlider, valueChanged( int ) )
238         SETUP_VFILTER_OPTION( atmoDarknesslimitSlider, valueChanged( int ) )
239         SETUP_VFILTER_OPTION( atmoMeanlengthSlider, valueChanged( int ) )
240         SETUP_VFILTER_OPTION( atmoMeanthresholdSlider, valueChanged( int ) )
241         SETUP_VFILTER_OPTION( atmoPercentnewSlider, valueChanged( int ) )
242         SETUP_VFILTER_OPTION( atmoFiltermodeCombo, currentIndexChanged( int ) )
243         SETUP_VFILTER_OPTION( atmoShowdotsCheck, stateChanged( int ) )
244     }
245     else
246     {
247         _parent->removeTab( _parent->indexOf( ui.tab_atmo ) );
248     }
249
250     SETUP_VFILTER( anaglyph )
251
252 #undef SETUP_VFILTER
253 #undef SETUP_VFILTER_OPTION
254
255     CONNECT( ui.cropTopPx, valueChanged( int ), this, cropChange() );
256     CONNECT( ui.cropBotPx, valueChanged( int ), this, cropChange() );
257     CONNECT( ui.cropLeftPx, valueChanged( int ), this, cropChange() );
258     CONNECT( ui.cropRightPx, valueChanged( int ), this, cropChange() );
259     CONNECT( ui.leftRightCropSync, toggled ( bool ), this, cropChange() );
260     CONNECT( ui.topBotCropSync, toggled ( bool ), this, cropChange() );
261     CONNECT( ui.topBotCropSync, toggled( bool ),
262              ui.cropBotPx, setDisabled( bool ) );
263     CONNECT( ui.leftRightCropSync, toggled( bool ),
264              ui.cropRightPx, setDisabled( bool ) );
265 }
266
267 void ExtVideo::cropChange()
268 {
269     if( ui.topBotCropSync->isChecked() )
270         ui.cropBotPx->setValue( ui.cropTopPx->value() );
271     if( ui.leftRightCropSync->isChecked() )
272         ui.cropRightPx->setValue( ui.cropLeftPx->value() );
273
274     vout_thread_t *p_vout = THEMIM->getVout();
275     if( p_vout )
276     {
277         var_SetInteger( p_vout, "crop-top", ui.cropTopPx->value() );
278         var_SetInteger( p_vout, "crop-bottom", ui.cropBotPx->value() );
279         var_SetInteger( p_vout, "crop-left", ui.cropLeftPx->value() );
280         var_SetInteger( p_vout, "crop-right", ui.cropRightPx->value() );
281         vlc_object_release( p_vout );
282     }
283 }
284
285 void ExtVideo::clean()
286 {
287     ui.cropTopPx->setValue( 0 );
288     ui.cropBotPx->setValue( 0 );
289     ui.cropLeftPx->setValue( 0 );
290     ui.cropRightPx->setValue( 0 );
291 }
292
293 static void ChangeVFiltersString( struct intf_thread_t *p_intf, const char *psz_name, bool b_add )
294 {
295     char *psz_parser, *psz_string;
296     const char *psz_filter_type;
297
298     module_t *p_obj = module_find( psz_name );
299     if( !p_obj )
300     {
301         msg_Err( p_intf, "Unable to find filter module \"%s\".", psz_name );
302         return;
303     }
304
305     if( module_provides( p_obj, "video splitter" ) )
306     {
307         psz_filter_type = "video-splitter";
308     }
309     else if( module_provides( p_obj, "video filter2" ) )
310     {
311         psz_filter_type = "video-filter";
312     }
313     else if( module_provides( p_obj, "sub source" ) )
314     {
315         psz_filter_type = "sub-source";
316     }
317     else if( module_provides( p_obj, "sub filter" ) )
318     {
319         psz_filter_type = "sub-filter";
320     }
321     else
322     {
323         msg_Err( p_intf, "Unknown video filter type." );
324         return;
325     }
326
327     psz_string = config_GetPsz( p_intf, psz_filter_type );
328
329     if( !psz_string ) psz_string = strdup( "" );
330
331     psz_parser = strstr( psz_string, psz_name );
332
333     if( b_add )
334     {
335         if( !psz_parser )
336         {
337             psz_parser = psz_string;
338             if( asprintf( &psz_string, ( *psz_string ) ? "%s:%s" : "%s%s",
339                             psz_string, psz_name ) == -1 )
340             {
341                 free( psz_parser );
342                 return;
343             }
344             free( psz_parser );
345         }
346         else
347         {
348             free( psz_string );
349             return;
350         }
351     }
352     else
353     {
354         if( psz_parser )
355         {
356             if( *( psz_parser + strlen( psz_name ) ) == ':' )
357             {
358                 memmove( psz_parser, psz_parser + strlen( psz_name ) + 1,
359                          strlen( psz_parser + strlen( psz_name ) + 1 ) + 1 );
360             }
361             else
362             {
363                 *psz_parser = '\0';
364             }
365
366             /* Remove trailing : : */
367             size_t i_len = strlen( psz_string );
368             if( i_len > 0 && *( psz_string + i_len - 1 ) == ':' )
369             {
370                 *( psz_string + i_len - 1 ) = '\0';
371             }
372         }
373         else
374         {
375             free( psz_string );
376             return;
377         }
378     }
379     /* Vout is not kept, so put that in the config */
380     config_PutPsz( p_intf, psz_filter_type, psz_string );
381
382     /* Try to set on the fly */
383     if( !strcmp( psz_filter_type, "video-splitter" ) )
384     {
385         playlist_t *p_playlist = pl_Get( p_intf );
386         var_SetString( p_playlist, psz_filter_type, psz_string );
387     }
388     else
389     {
390         vout_thread_t *p_vout = THEMIM->getVout();
391         if( p_vout )
392         {
393             var_SetString( p_vout, psz_filter_type, psz_string );
394             vlc_object_release( p_vout );
395         }
396     }
397
398     free( psz_string );
399 }
400
401 void ExtVideo::updateFilters()
402 {
403     QString module = ModuleFromWidgetName( sender() );
404
405     QCheckBox *checkbox = qobject_cast<QCheckBox*>( sender() );
406     QGroupBox *groupbox = qobject_cast<QGroupBox*>( sender() );
407
408     ChangeVFiltersString( p_intf, qtu( module ),
409                           checkbox ? checkbox->isChecked()
410                                    : groupbox->isChecked() );
411 }
412
413 #define UPDATE_AND_APPLY_TEXT( widget, file ) \
414     CONNECT( ui.widget, textChanged( const QString& ), \
415              this, updateFilterOptions() ); \
416     ui.widget->setText( toNativeSeparators( file ) ); \
417     ui.widget->disconnect( SIGNAL( textChanged( const QString& ) ) );
418
419 void ExtVideo::browseLogo()
420 {
421     QString file = QFileDialog::getOpenFileName( NULL, qtr( "Logo filenames" ),
422                    p_intf->p_sys->filepath, "Images (*.png *.jpg);;All (*)" );
423
424     UPDATE_AND_APPLY_TEXT( logoFileText, file );
425 }
426
427 void ExtVideo::browseEraseFile()
428 {
429     QString file = QFileDialog::getOpenFileName( NULL, qtr( "Image mask" ),
430                    p_intf->p_sys->filepath, "Images (*.png *.jpg);;All (*)" );
431
432     UPDATE_AND_APPLY_TEXT( eraseMaskText, file );
433 }
434
435 #undef UPDATE_AND_APPLY_TEXT
436
437 void ExtVideo::initComboBoxItems( QObject *widget )
438 {
439     QComboBox *combobox = qobject_cast<QComboBox*>( widget );
440     if( !combobox ) return;
441
442     QString option = OptionFromWidgetName( widget );
443     module_config_t *p_item = config_FindConfig( VLC_OBJECT( p_intf ),
444                                                  qtu( option ) );
445     if( p_item == NULL )
446     {
447         msg_Err( p_intf, "Couldn't find option \"%s\".", qtu( option ) );
448         return;
449     }
450
451     if( p_item->i_type == CONFIG_ITEM_INTEGER
452      || p_item->i_type == CONFIG_ITEM_BOOL )
453     {
454         int64_t *values;
455         char **texts;
456         ssize_t count = config_GetIntChoices( VLC_OBJECT( p_intf ),
457                                               qtu( option ), &values, &texts );
458         for( ssize_t i = 0; i < count; i++ )
459         {
460             combobox->addItem( qtr( texts[i] ), qlonglong(values[i]) );
461             free( texts[i] );
462         }
463         free( texts );
464         free( values );
465     }
466     else if( p_item->i_type == CONFIG_ITEM_STRING )
467     {
468         char **values;
469         char **texts;
470         ssize_t count = config_GetPszChoices( VLC_OBJECT( p_intf ),
471                                               qtu( option ), &values, &texts );
472         for( ssize_t i = 0; i < count; i++ )
473         {
474             combobox->addItem( qtr( texts[i] ), qfu(values[i]) );
475             free( texts[i] );
476             free( values[i] );
477         }
478         free( texts );
479         free( values );
480     }
481 }
482
483 void ExtVideo::setWidgetValue( QObject *widget )
484 {
485     QString module = ModuleFromWidgetName( widget->parent() );
486     //std::cout << "Module name: " << module.toStdString() << std::endl;
487     QString option = OptionFromWidgetName( widget );
488     //std::cout << "Option name: " << option.toStdString() << std::endl;
489
490     vlc_object_t *p_obj = ( vlc_object_t * )
491         vlc_object_find_name( p_intf->p_libvlc, qtu( module ) );
492     int i_type;
493     vlc_value_t val;
494
495     if( !p_obj )
496     {
497 #if 0
498         msg_Dbg( p_intf,
499                  "Module instance %s not found, looking in config values.",
500                  qtu( module ) );
501 #endif
502         i_type = config_GetType( p_intf, qtu( option ) ) & VLC_VAR_CLASS;
503         switch( i_type )
504         {
505             case VLC_VAR_INTEGER:
506             case VLC_VAR_BOOL:
507                 val.i_int = config_GetInt( p_intf, qtu( option ) );
508                 break;
509             case VLC_VAR_FLOAT:
510                 val.f_float = config_GetFloat( p_intf, qtu( option ) );
511                 break;
512             case VLC_VAR_STRING:
513                 val.psz_string = config_GetPsz( p_intf, qtu( option ) );
514                 break;
515         }
516     }
517     else
518     {
519         i_type = var_Type( p_obj, qtu( option ) ) & VLC_VAR_CLASS;
520         var_Get( p_obj, qtu( option ), &val );
521         vlc_object_release( p_obj );
522     }
523
524     /* Try to cast to all the widgets we're likely to encounter. Only
525      * one of the casts is expected to work. */
526     QSlider        *slider        = qobject_cast<QSlider*>       ( widget );
527     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( widget );
528     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( widget );
529     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( widget );
530     QDial          *dial          = qobject_cast<QDial*>         ( widget );
531     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( widget );
532     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( widget );
533
534     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
535     {
536         if( slider )        slider->setValue( val.i_int );
537         else if( checkbox ) checkbox->setCheckState( val.i_int? Qt::Checked
538                                                               : Qt::Unchecked );
539         else if( spinbox )  spinbox->setValue( val.i_int );
540         else if( dial )     dial->setValue( ( 540-val.i_int )%360 );
541         else if( lineedit )
542         {
543             char str[30];
544             snprintf( str, sizeof(str), "%06"PRIX64, val.i_int );
545             lineedit->setText( str );
546         }
547         else if( combobox ) combobox->setCurrentIndex(
548                             combobox->findData( qlonglong(val.i_int) ) );
549         else msg_Warn( p_intf, "Could not find the correct Integer widget" );
550     }
551     else if( i_type == VLC_VAR_FLOAT )
552     {
553         if( slider ) slider->setValue( ( int )( val.f_float*( double )slider->tickInterval() ) ); /* hack alert! */
554         else if( doublespinbox ) doublespinbox->setValue( val.f_float );
555         else if( dial ) dial->setValue( (540 - lroundf(val.f_float)) % 360 );
556         else msg_Warn( p_intf, "Could not find the correct Float widget" );
557     }
558     else if( i_type == VLC_VAR_STRING )
559     {
560         if( lineedit ) lineedit->setText( qfu( val.psz_string ) );
561         else if( combobox ) combobox->setCurrentIndex(
562                             combobox->findData( qfu( val.psz_string ) ) );
563         else msg_Warn( p_intf, "Could not find the correct String widget" );
564         free( val.psz_string );
565     }
566     else
567         if( p_obj )
568             msg_Err( p_intf,
569                      "Module %s's %s variable is of an unsupported type ( %d )",
570                      qtu( module ),
571                      qtu( option ),
572                      i_type );
573 }
574
575 void ExtVideo::updateFilterOptions()
576 {
577     QString module = ModuleFromWidgetName( sender()->parent() );
578     //std::cout << "Module name: " << module.toStdString() << std::endl;
579     QString option = OptionFromWidgetName( sender() );
580     //std::cout << "Option name: " << option.toStdString() << std::endl;
581
582     vlc_object_t *p_obj = ( vlc_object_t * )
583         vlc_object_find_name( p_intf->p_libvlc, qtu( module ) );
584     int i_type;
585     bool b_is_command;
586     if( !p_obj )
587     {
588         msg_Warn( p_intf, "Module %s not found. You'll need to restart the filter to take the change into account.", qtu( module ) );
589         i_type = config_GetType( p_intf, qtu( option ) );
590         b_is_command = false;
591     }
592     else
593     {
594         i_type = var_Type( p_obj, qtu( option ) );
595         if( i_type == 0 )
596             i_type = config_GetType( p_intf, qtu( option ) );
597         b_is_command = ( i_type & VLC_VAR_ISCOMMAND );
598     }
599
600     /* Try to cast to all the widgets we're likely to encounter. Only
601      * one of the casts is expected to work. */
602     QSlider        *slider        = qobject_cast<QSlider*>       ( sender() );
603     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( sender() );
604     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( sender() );
605     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( sender() );
606     QDial          *dial          = qobject_cast<QDial*>         ( sender() );
607     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( sender() );
608     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( sender() );
609
610     i_type &= VLC_VAR_CLASS;
611     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
612     {
613         int i_int = 0;
614         if( slider )        i_int = slider->value();
615         else if( checkbox ) i_int = checkbox->checkState() == Qt::Checked;
616         else if( spinbox )  i_int = spinbox->value();
617         else if( dial )     i_int = ( 540-dial->value() )%360;
618         else if( lineedit ) i_int = lineedit->text().toInt( NULL,16 );
619         else if( combobox ) i_int = combobox->itemData( combobox->currentIndex() ).toInt();
620         else msg_Warn( p_intf, "Could not find the correct Integer widget" );
621         config_PutInt( p_intf, qtu( option ), i_int );
622         if( b_is_command )
623         {
624             if( i_type == VLC_VAR_INTEGER )
625                 var_SetInteger( p_obj, qtu( option ), i_int );
626             else
627                 var_SetBool( p_obj, qtu( option ), i_int );
628         }
629     }
630     else if( i_type == VLC_VAR_FLOAT )
631     {
632         double f_float = 0;
633         if( slider )             f_float = ( double )slider->value()
634                                          / ( double )slider->tickInterval(); /* hack alert! */
635         else if( doublespinbox ) f_float = doublespinbox->value();
636         else if( dial ) f_float = (540 - dial->value()) % 360;
637         else if( lineedit ) f_float = lineedit->text().toDouble();
638         else msg_Warn( p_intf, "Could not find the correct Float widget" );
639         config_PutFloat( p_intf, qtu( option ), f_float );
640         if( b_is_command )
641             var_SetFloat( p_obj, qtu( option ), f_float );
642     }
643     else if( i_type == VLC_VAR_STRING )
644     {
645         QString val;
646         if( lineedit )
647             val = lineedit->text();
648         else if( combobox )
649             val = combobox->itemData( combobox->currentIndex() ).toString();
650         else
651             msg_Warn( p_intf, "Could not find the correct String widget" );
652         config_PutPsz( p_intf, qtu( option ), qtu( val ) );
653         if( b_is_command )
654             var_SetString( p_obj, qtu( option ), qtu( val ) );
655     }
656     else
657         msg_Err( p_intf,
658                  "Module %s's %s variable is of an unsupported type ( %d )",
659                  qtu( module ),
660                  qtu( option ),
661                  i_type );
662
663     if( !b_is_command )
664     {
665         msg_Warn( p_intf, "Module %s's %s variable isn't a command. Brute-restarting the filter.",
666                  qtu( module ),
667                  qtu( option ) );
668         ChangeVFiltersString( p_intf, qtu( module ), false );
669         ChangeVFiltersString( p_intf, qtu( module ), true );
670     }
671
672     if( p_obj ) vlc_object_release( p_obj );
673 }
674
675 #if 0
676 void ExtVideo::gotoConf( QObject* src )
677 {
678 #define SHOWCONF( module ) \
679     if( src->objectName().contains( module ) ) \
680     { \
681         PrefsDialog::getInstance( p_intf )->showModulePrefs( module ); \
682         return; \
683     }
684     SHOWCONF( "clone" );
685     SHOWCONF( "magnify" );
686     SHOWCONF( "wave" );
687     SHOWCONF( "ripple" );
688     SHOWCONF( "invert" );
689     SHOWCONF( "puzzle" );
690     SHOWCONF( "wall" );
691     SHOWCONF( "gradient" );
692     SHOWCONF( "colorthres" );
693     SHOWCONF( "anaglyph" )
694 }
695 #endif
696
697 /**********************************************************************
698  * v4l2 controls
699  **********************************************************************/
700
701 ExtV4l2::ExtV4l2( intf_thread_t *_p_intf, QWidget *_parent )
702     : QWidget( _parent ), p_intf( _p_intf ), box( NULL )
703 {
704     QVBoxLayout *layout = new QVBoxLayout( this );
705     help = new QLabel( qtr("No v4l2 instance found.\n"
706       "Please check that the device has been opened with VLC and is playing.\n\n"
707       "Controls will automatically appear here.")
708       , this );
709     help->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
710     layout->addWidget( help );
711     setLayout( layout );
712 }
713
714 void ExtV4l2::showEvent( QShowEvent *event )
715 {
716     QWidget::showEvent( event );
717     Refresh();
718 }
719
720 void ExtV4l2::Refresh( void )
721 {
722     vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( pl_Get(p_intf), "v4l2" );
723     help->hide();
724     if( box )
725     {
726         layout()->removeWidget( box );
727         delete box;
728         box = NULL;
729     }
730     if( p_obj )
731     {
732         vlc_value_t val, text;
733         int i_ret = var_Change( p_obj, "controls", VLC_VAR_GETCHOICES,
734                                 &val, &text );
735         if( i_ret < 0 )
736         {
737             msg_Err( p_intf, "Oops, v4l2 object doesn't have a 'controls' variable." );
738             help->show();
739             vlc_object_release( p_obj );
740             return;
741         }
742
743         box = new QGroupBox( this );
744         layout()->addWidget( box );
745         QVBoxLayout *layout = new QVBoxLayout( box );
746         box->setLayout( layout );
747
748         for( int i = 0; i < val.p_list->i_count; i++ )
749         {
750             vlc_value_t vartext;
751             const char *psz_var = text.p_list->p_values[i].psz_string;
752
753             if( var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &vartext, NULL ) )
754                 continue;
755
756             QString name = qtr( vartext.psz_string );
757             free( vartext.psz_string );
758             msg_Dbg( p_intf, "v4l2 control \"%"PRIx64"\": %s (%s)",
759                      val.p_list->p_values[i].i_int, psz_var, qtu( name ) );
760
761             int i_type = var_Type( p_obj, psz_var );
762             switch( i_type & VLC_VAR_TYPE )
763             {
764                 case VLC_VAR_INTEGER:
765                 {
766                     QLabel *label = new QLabel( name, box );
767                     QHBoxLayout *hlayout = new QHBoxLayout();
768                     hlayout->addWidget( label );
769                     int i_val = var_GetInteger( p_obj, psz_var );
770                     if( i_type & VLC_VAR_HASCHOICE )
771                     {
772                         QComboBox *combobox = new QComboBox( box );
773                         combobox->setObjectName( qfu( psz_var ) );
774
775                         vlc_value_t val2, text2;
776                         var_Change( p_obj, psz_var, VLC_VAR_GETCHOICES,
777                                     &val2, &text2 );
778                         for( int j = 0; j < val2.p_list->i_count; j++ )
779                         {
780                             combobox->addItem(
781                                        text2.p_list->p_values[j].psz_string,
782                                        qlonglong( val2.p_list->p_values[j].i_int) );
783                             if( i_val == val2.p_list->p_values[j].i_int )
784                                 combobox->setCurrentIndex( j );
785                         }
786                         var_FreeList( &val2, &text2 );
787
788                         CONNECT( combobox, currentIndexChanged( int ), this,
789                                  ValueChange( int ) );
790                         hlayout->addWidget( combobox );
791                     }
792                     else
793                     if( (i_type & VLC_VAR_HASMIN) && (i_type & VLC_VAR_HASMAX) )
794                     {
795                         QSlider *slider = new QSlider( box );
796                         slider->setObjectName( qfu( psz_var ) );
797                         slider->setOrientation( Qt::Horizontal );
798                         vlc_value_t val2;
799                         var_Change( p_obj, psz_var, VLC_VAR_GETMIN,
800                                     &val2, NULL );
801                         if( val2.i_int < INT_MIN )
802                             val2.i_int = INT_MIN; /* FIXME */
803                         slider->setMinimum( val2.i_int );
804                         var_Change( p_obj, psz_var, VLC_VAR_GETMAX,
805                                     &val2, NULL );
806                         if( val2.i_int > INT_MAX )
807                             val2.i_int = INT_MAX; /* FIXME */
808                         slider->setMaximum( val2.i_int );
809                         if( !var_Change( p_obj, psz_var, VLC_VAR_GETSTEP,
810                                          &val2, NULL ) )
811                             slider->setSingleStep( val2.i_int );
812                         slider->setValue( i_val );
813                         CONNECT( slider, valueChanged( int ), this,
814                                  ValueChange( int ) );
815                         hlayout->addWidget( slider );
816                     }
817                     else
818                     {
819                         QSpinBox *spinBox = new QSpinBox( box );
820                         spinBox->setObjectName( qfu( psz_var ) );
821                         spinBox->setMinimum( INT_MIN );
822                         spinBox->setMaximum( INT_MAX );
823                         spinBox->setValue( i_val );
824                         CONNECT( spinBox, valueChanged( int ), this,
825                                  ValueChange( int ) );
826                         hlayout->addWidget( spinBox );
827                     }
828                     layout->addLayout( hlayout );
829                     break;
830                 }
831                 case VLC_VAR_BOOL:
832                 {
833                     QCheckBox *button = new QCheckBox( name, box );
834                     button->setObjectName( qfu( psz_var ) );
835                     button->setChecked( var_GetBool( p_obj, psz_var ) );
836
837                     CONNECT( button, clicked( bool ), this,
838                              ValueChange( bool ) );
839                     layout->addWidget( button );
840                     break;
841                 }
842                 case VLC_VAR_VOID:
843                 {
844                     if( i_type & VLC_VAR_ISCOMMAND )
845                     {
846                         QPushButton *button = new QPushButton( name, box );
847                         button->setObjectName( qfu( psz_var ) );
848
849                         CONNECT( button, clicked( bool ), this,
850                                  ValueChange( bool ) );
851                         layout->addWidget( button );
852                     }
853                     else
854                     {
855                         QLabel *label = new QLabel( name, box );
856                         layout->addWidget( label );
857                     }
858                     break;
859                 }
860                 default:
861                     msg_Warn( p_intf, "Unhandled var type for %s", psz_var );
862                     break;
863             }
864         }
865         var_FreeList( &val, &text );
866         vlc_object_release( p_obj );
867     }
868     else
869     {
870         msg_Dbg( p_intf, "Couldn't find v4l2 instance" );
871         help->show();
872         if ( isVisible() )
873             QTimer::singleShot( 2000, this, SLOT(Refresh()) );
874     }
875 }
876
877 void ExtV4l2::ValueChange( bool value )
878 {
879     ValueChange( (int)value );
880 }
881
882 void ExtV4l2::ValueChange( int value )
883 {
884     QObject *s = sender();
885     vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( pl_Get(p_intf), "v4l2" );
886     if( p_obj )
887     {
888         QString var = s->objectName();
889         int i_type = var_Type( p_obj, qtu( var ) );
890         switch( i_type & VLC_VAR_TYPE )
891         {
892             case VLC_VAR_INTEGER:
893                 if( i_type & VLC_VAR_HASCHOICE )
894                 {
895                     QComboBox *combobox = qobject_cast<QComboBox*>( s );
896                     value = combobox->itemData( value ).toInt();
897                 }
898                 var_SetInteger( p_obj, qtu( var ), value );
899                 break;
900             case VLC_VAR_BOOL:
901                 var_SetBool( p_obj, qtu( var ), value );
902                 break;
903             case VLC_VAR_VOID:
904                 var_TriggerCallback( p_obj, qtu( var ) );
905                 break;
906         }
907         vlc_object_release( p_obj );
908     }
909     else
910     {
911         msg_Warn( p_intf, "Oops, v4l2 object isn't available anymore" );
912         Refresh();
913     }
914 }
915
916 /**********************************************************************
917  * Sliders
918  **********************************************************************/
919
920 FilterSliderData::FilterSliderData( QObject *parent,
921                                     intf_thread_t *_p_intf,
922                                     QSlider *_slider,
923                                     QLabel *_label, QLabel *_nameLabel,
924                                     const slider_data_t *_p_data ):
925     QObject( parent ), slider( _slider ), p_intf( _p_intf ),
926     valueLabel( _label ), nameLabel( _nameLabel ), p_data( _p_data )
927 {
928     slider->setMinimum( p_data->f_min / p_data->f_resolution );
929     slider->setMaximum( p_data->f_max / p_data->f_resolution );
930     nameLabel->setText( p_data->descs );
931     CONNECT( slider, valueChanged( int ), this, updateText( int ) );
932     setValue( initialValue() );
933     /* In case current == min|max text would not be first updated */
934     if ( slider->value() == slider->maximum() ||
935          slider->value() == slider->minimum() )
936         updateText( slider->value() );
937     CONNECT( slider, valueChanged( int ), this, onValueChanged( int ) );
938 }
939
940 void FilterSliderData::setValue( float f )
941 {
942     slider->setValue( f / p_data->f_resolution );
943 }
944
945 void FilterSliderData::updateText( int i )
946 {
947     float f = ((float) i) * p_data->f_resolution * p_data->f_visual_multiplier;
948     valueLabel->setText( QString( p_data->units )
949                     .prepend( "%1" )
950                     .arg( QString::number( f, 'f', 1 ) ) );
951 }
952
953 float FilterSliderData::initialValue()
954 {
955     vlc_object_t *p_aout = (vlc_object_t *) THEMIM->getAout();
956     float f = p_data->f_value;
957     if( p_aout )
958     {
959         if ( var_Type( p_aout, qtu(p_data->name) ) == 0 )
960         {
961             vlc_object_release( p_aout );
962             /* Not found, will try in config */
963         }
964         else
965         {
966             f = var_GetFloat( p_aout, qtu(p_data->name) );
967             vlc_object_release( p_aout );
968             return f;
969         }
970     }
971
972     if ( ! config_FindConfig( VLC_OBJECT(p_intf), qtu(p_data->name) ) )
973         return f;
974
975     f = config_GetFloat( p_intf, qtu(p_data->name) );
976     return f;
977 }
978
979 void FilterSliderData::onValueChanged( int i )
980 {
981     float f = ((float) i) * p_data->f_resolution;
982     vlc_object_t *p_aout = (vlc_object_t *) THEMIM->getAout();
983     if ( p_aout )
984     {
985         var_SetFloat( p_aout, qtu(p_data->name), f );
986         vlc_object_release( p_aout );
987     }
988     writeToConfig();
989 }
990
991 void FilterSliderData::writeToConfig()
992 {
993     float f = ((float) slider->value()) * p_data->f_resolution;
994     config_PutFloat( p_intf, qtu(p_data->name), f );
995 }
996
997 AudioFilterControlWidget::AudioFilterControlWidget
998 ( intf_thread_t *_p_intf, QWidget *parent, const char *_name ) :
999     QWidget( parent ), p_intf( _p_intf ), name( _name ), i_smallfont(0)
1000 {}
1001
1002 void AudioFilterControlWidget::build()
1003 {
1004     QFont smallFont = QApplication::font();
1005     smallFont.setPointSize( smallFont.pointSize() + i_smallfont );
1006
1007     QVBoxLayout *layout = new QVBoxLayout( this );
1008     slidersBox = new QGroupBox( qtr( "Enable" ) );
1009     slidersBox->setCheckable( true );
1010     layout->addWidget( slidersBox );
1011
1012     QGridLayout *ctrlLayout = new QGridLayout( slidersBox );
1013
1014     int i = 0;
1015     foreach( const FilterSliderData::slider_data_t &data, controls )
1016     {
1017         QSlider *slider = new QSlider( Qt::Vertical );
1018         QLabel *valueLabel = new QLabel();
1019         valueLabel->setFont( smallFont );
1020         valueLabel->setAlignment( Qt::AlignHCenter );
1021         QLabel *nameLabel = new QLabel();
1022         nameLabel->setFont( smallFont );
1023         nameLabel->setAlignment( Qt::AlignHCenter );
1024         FilterSliderData *filter =
1025             new FilterSliderData( this, p_intf,
1026                                   slider, valueLabel, nameLabel, & data );
1027         ctrlLayout->addWidget( slider, 0, i, Qt::AlignHCenter );
1028         ctrlLayout->addWidget( valueLabel, 1, i, Qt::AlignHCenter );
1029         ctrlLayout->addWidget( nameLabel, 2, i, Qt::AlignHCenter );
1030         i++;
1031     }
1032
1033     vlc_object_t *p_aout = (vlc_object_t *)THEMIM->getAout();
1034     char *psz_af;
1035     if( p_aout )
1036     {
1037         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
1038         vlc_object_release( p_aout );
1039     }
1040     else
1041         psz_af = config_GetPsz( p_intf, "audio-filter" );
1042
1043     if( psz_af && strstr( psz_af, qtu(name) ) != NULL )
1044         slidersBox->setChecked( true );
1045     else
1046         slidersBox->setChecked( false );
1047     CONNECT( slidersBox, toggled(bool), this, enable() );
1048
1049     free( psz_af );
1050 }
1051
1052 AudioFilterControlWidget::~AudioFilterControlWidget()
1053 {
1054
1055 }
1056
1057 void AudioFilterControlWidget::enable()
1058 {
1059     playlist_EnableAudioFilter( THEPL, qtu(name), slidersBox->isChecked() );
1060 }
1061
1062 /**********************************************************************
1063  * Equalizer
1064  **********************************************************************/
1065
1066 static const QString vlc_band_frequencies[] =
1067 {
1068     "  60 Hz  ", " 170 Hz ", " 310 Hz ", " 600 Hz ", "  1 kHz ",
1069     "  3 kHz  ", "  6 kHz ", " 12 kHz ", " 14 kHz ", " 16 kHz "
1070 };
1071
1072 static const QString iso_band_frequencies[] =
1073 {
1074     "  31 Hz ", "  63 Hz ", " 125 Hz ", " 250 Hz ", " 500 Hz ",
1075     "  1 kHz ", "  2 kHz ", "  4 kHz ", "  8 kHz ", " 16 kHz "
1076 };
1077
1078 Equalizer::Equalizer( intf_thread_t *_p_intf, QWidget *_parent ) :
1079                             QWidget( _parent ) , p_intf( _p_intf )
1080 {
1081     QFont smallFont = QApplication::font();
1082     smallFont.setPointSize( smallFont.pointSize() - 3 );
1083
1084     ui.setupUi( this );
1085     ui.preampLabel->setFont( smallFont );
1086
1087     /* Setup of presetsComboBox */
1088     presetsComboBox = ui.presetsCombo;
1089     CONNECT( presetsComboBox, activated( int ), this, setCorePreset( int ) );
1090
1091     b_vlcBands = var_InheritBool( p_intf, "equalizer-vlcfreqs" );
1092
1093     /* Add the sliders for the Bands */
1094     QGridLayout *grid = new QGridLayout( ui.slidersPlaceholder );
1095     grid->setMargin( 0 );
1096     for( int i = 0 ; i < BANDS ; i++ )
1097     {
1098         bands[i] = new QSlider( Qt::Vertical );
1099         bands[i]->setMaximum( 400 );
1100         bands[i]->setValue( 200 );
1101         bands[i]->setMinimumWidth(36);
1102         CONNECT( bands[i], valueChanged( int ), this, setCoreBands() );
1103
1104         QString val = QString("%1").arg( 0.0, 5, 'f', 1 );
1105         band_texts[i] = new QLabel( (b_vlcBands ? vlc_band_frequencies[i]
1106                                                : iso_band_frequencies[i]) + "\n" + val + "dB" );
1107         band_texts[i]->setFont( smallFont );
1108
1109         grid->addWidget( bands[i], 0, i );
1110         grid->addWidget( band_texts[i], 1, i );
1111     }
1112
1113     /* Add the listed presets */
1114     for( int i = 0 ; i < NB_PRESETS ; i ++ )
1115     {
1116         presetsComboBox->addItem( qtr( preset_list_text[i] ),
1117                                   QVariant( preset_list[i] ) );
1118     }
1119
1120     /* Connects */
1121     BUTTONACT( ui.enableCheck, enable() );
1122     BUTTONACT( ui.eq2PassCheck, set2Pass() );
1123     CONNECT( ui.preampSlider, valueChanged( int ), this, setPreamp() );
1124
1125     /* Do the update from the value of the core */
1126     updateUIFromCore();
1127 }
1128
1129 /* Write down initial values */
1130 void Equalizer::updateUIFromCore()
1131 {
1132     char *psz_af, *psz_pres, *psz_bands;
1133     float f_preamp;
1134     int i_preset;
1135
1136     vlc_object_t *p_aout = (vlc_object_t *)THEMIM->getAout();
1137     if( p_aout )
1138     {
1139         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
1140         vlc_object_release( p_aout );
1141     }
1142     else
1143     {
1144         psz_af = config_GetPsz( p_intf, "audio-filter" );
1145     }
1146
1147     psz_pres = var_InheritString( p_aout, "equalizer-preset" );
1148     if( var_InheritBool( p_aout, "equalizer-2pass" ) )
1149         ui.eq2PassCheck->setChecked( true );
1150     f_preamp = var_InheritFloat( p_aout, "equalizer-preamp" );
1151     psz_bands = var_InheritString( p_aout, "equalizer-bands" );
1152     i_preset = presetsComboBox->findData( QVariant( psz_pres ) );
1153
1154     if( psz_af && strstr( psz_af, "equalizer" ) != NULL )
1155         ui.enableCheck->setChecked( true );
1156     enable( ui.enableCheck->isChecked() );
1157
1158     presetsComboBox->setCurrentIndex( i_preset );
1159
1160     ui.preampSlider->setValue( (int)( ( f_preamp + 20 ) * 10 ) );
1161
1162     if( psz_bands && strlen( psz_bands ) > 1 )
1163     {
1164         char *psz_bands_orig = psz_bands;
1165         for( int i = 0; i < BANDS; i++ )
1166         {
1167             const float f = us_strtod(psz_bands, &psz_bands );
1168             bands[i]->setValue( (int)( ( f + 20 ) * 10 )  );
1169             if( psz_bands == NULL || *psz_bands == '\0' ) break;
1170             psz_bands++;
1171             if( *psz_bands == '\0' ) break;
1172         }
1173         free( psz_bands_orig );
1174     }
1175     else free( psz_bands );
1176
1177     free( psz_af );
1178     free( psz_pres );
1179 }
1180
1181 void Equalizer::changeFreqLabels( bool b_useVlcBands )
1182 {
1183     b_vlcBands = b_useVlcBands;
1184
1185     const QString *band_frequencies = b_vlcBands
1186                                     ? vlc_band_frequencies
1187                                     : iso_band_frequencies;
1188
1189     for( int i = 0; i < BANDS; i++ )
1190     {
1191         const float f_val = (float)( bands[i]->value() ) / 10 - 20;
1192         QString val = QString("%1").arg( f_val, 5, 'f', 1 );
1193         band_texts[i]->setText( band_frequencies[i] + "\n" + val + "dB" );
1194     }
1195 }
1196
1197 /* Function called when enableButton is toggled */
1198 void Equalizer::enable()
1199 {
1200     bool en = ui.enableCheck->isChecked();
1201     playlist_EnableAudioFilter( THEPL, "equalizer", en );
1202     //playlist_EnableAudioFilter( THEPL, "upmixer", en );
1203     //playlist_EnableAudioFilter( THEPL, "vsurround", en );
1204     enable( en );
1205
1206     if( presetsComboBox->currentIndex() < 0 )
1207         presetsComboBox->setCurrentIndex( 0 );
1208
1209 }
1210
1211 void Equalizer::enable( bool en )
1212 {
1213     ui.eq2PassCheck->setEnabled( en );
1214     presetsComboBox->setEnabled( en );
1215     ui.presetLabel->setEnabled( en );
1216     ui.preampLabel->setEnabled( en );
1217     ui.preampSlider->setEnabled( en  );
1218     for( int i = 0 ; i< BANDS; i++ )
1219     {
1220         bands[i]->setEnabled( en ); band_texts[i]->setEnabled( en );
1221     }
1222 }
1223
1224 /* Function called when the set2Pass button is activated */
1225 void Equalizer::set2Pass()
1226 {
1227     vlc_object_t *p_aout= (vlc_object_t *)THEMIM->getAout();
1228     bool b_2p = ui.eq2PassCheck->isChecked();
1229
1230     if( p_aout )
1231     {
1232         var_SetBool( p_aout, "equalizer-2pass", b_2p );
1233         vlc_object_release( p_aout );
1234     }
1235     config_PutInt( p_intf, "equalizer-2pass", b_2p );
1236 }
1237
1238 /* Function called when the preamp slider is moved */
1239 void Equalizer::setPreamp()
1240 {
1241     const float f = ( float )(  ui.preampSlider->value() ) /10 - 20;
1242     vlc_object_t *p_aout = (vlc_object_t *)THEMIM->getAout();
1243
1244     ui.preampLabel->setText( qtr( "Preamp\n" ) + QString::number( f, 'f', 1 )
1245                                                + qtr( "dB" ) );
1246     if( p_aout )
1247     {
1248         //delCallbacks( p_aout );
1249         var_SetFloat( p_aout, "equalizer-preamp", f );
1250         //addCallbacks( p_aout );
1251         vlc_object_release( p_aout );
1252     }
1253     config_PutFloat( p_intf, "equalizer-preamp", f );
1254 }
1255
1256 void Equalizer::setCoreBands()
1257 {
1258     /**\todo smoothing */
1259
1260     const QString *band_frequencies = b_vlcBands
1261                                     ? vlc_band_frequencies
1262                                     : iso_band_frequencies;
1263
1264     QString values;
1265     for( int i = 0; i < BANDS; i++ )
1266     {
1267         const float f_val = (float)( bands[i]->value() ) / 10 - 20;
1268         QString val = QString("%1").arg( f_val, 5, 'f', 1 );
1269
1270         band_texts[i]->setText( band_frequencies[i] + "\n" + val + "dB" );
1271         values += " " + val;
1272     }
1273
1274     vlc_object_t *p_aout = (vlc_object_t *)THEMIM->getAout();
1275     if( p_aout )
1276     {
1277         //delCallbacks( p_aout );
1278         var_SetString( p_aout, "equalizer-bands", qtu( values ) );
1279         //addCallbacks( p_aout );
1280         vlc_object_release( p_aout );
1281     }
1282 }
1283
1284 char * Equalizer::createValuesFromPreset( int i_preset )
1285 {
1286     QString values;
1287
1288     /* Create the QString in Qt */
1289     for( int i = 0 ; i< BANDS ;i++ )
1290         values += QString( " %1" ).arg( eqz_preset_10b[i_preset].f_amp[i], 5, 'f', 1 );
1291
1292     /* Convert it to char * */
1293     return strdup( values.toAscii().constData() );
1294 }
1295
1296 void Equalizer::setCorePreset( int i_preset )
1297 {
1298     if( i_preset < 0 )
1299         return;
1300
1301     /* Update pre-amplification in the UI */
1302     float f_preamp = eqz_preset_10b[i_preset].f_preamp;
1303     ui.preampSlider->setValue( (int)( ( f_preamp + 20 ) * 10 ) );
1304     ui.preampLabel->setText( qtr( "Preamp\n" )
1305                    + QString::number( f_preamp, 'f', 1 ) + qtr( "dB" ) );
1306
1307     char *psz_values = createValuesFromPreset( i_preset );
1308     if( !psz_values ) return ;
1309
1310     const QString *band_frequencies = b_vlcBands
1311                                     ? vlc_band_frequencies
1312                                     : iso_band_frequencies;
1313
1314     char *p = psz_values;
1315     for( int i = 0; i < BANDS && *p; i++ )
1316     {
1317         const float f = us_strtod( p, &p );
1318
1319         bands[i]->setValue( (int)( ( f + 20 ) * 10 )  );
1320         band_texts[i]->setText( band_frequencies[i] + "\n"
1321                               + QString("%1").arg( f, 5, 'f', 1 ) + "dB" );
1322         if( *p )
1323             p++; /* skip separator */
1324     }
1325
1326     /* Apply presets to audio output */
1327     vlc_object_t *p_aout = (vlc_object_t *)THEMIM->getAout();
1328     if( p_aout )
1329     {
1330         var_SetString( p_aout , "equalizer-preset" , preset_list[i_preset] );
1331
1332         var_SetString( p_aout, "equalizer-bands", psz_values );
1333         var_SetFloat( p_aout, "equalizer-preamp",
1334                       eqz_preset_10b[i_preset].f_preamp );
1335         vlc_object_release( p_aout );
1336     }
1337     config_PutPsz( p_intf, "equalizer-bands", psz_values );
1338     config_PutPsz( p_intf, "equalizer-preset", preset_list[i_preset] );
1339     config_PutFloat( p_intf, "equalizer-preamp",
1340                     eqz_preset_10b[i_preset].f_preamp );
1341     free( psz_values );
1342 }
1343
1344 static int PresetCallback( vlc_object_t *p_this, char const *psz_cmd,
1345                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1346 {
1347     VLC_UNUSED( p_this ); VLC_UNUSED( psz_cmd ); VLC_UNUSED( oldval );
1348
1349     char *psz_preset = newval.psz_string;
1350     Equalizer *eq = ( Equalizer * )p_data;
1351     int i_preset = eq->presetsComboBox->findData( QVariant( psz_preset ) );
1352     eq->presetsComboBox->setCurrentIndex( i_preset );
1353     return VLC_SUCCESS;
1354 }
1355
1356 void Equalizer::delCallbacks( vlc_object_t *p_aout )
1357 {
1358     //var_DelCallback( p_aout, "equalizer-bands", EqzCallback, this );
1359     //var_DelCallback( p_aout, "equalizer-preamp", EqzCallback, this );
1360     var_DelCallback( p_aout, "equalizer-preset", PresetCallback, this );
1361 }
1362
1363 void Equalizer::addCallbacks( vlc_object_t *p_aout )
1364 {
1365     //var_AddCallback( p_aout, "equalizer-bands", EqzCallback, this );
1366     //var_AddCallback( p_aout, "equalizer-preamp", EqzCallback, this );
1367     var_AddCallback( p_aout, "equalizer-preset", PresetCallback, this );
1368 }
1369
1370 /**********************************************************************
1371  * Audio filters
1372  **********************************************************************/
1373
1374 /**********************************************************************
1375  * Dynamic range compressor
1376  **********************************************************************/
1377
1378 Compressor::Compressor( intf_thread_t *p_intf, QWidget *parent )
1379     : AudioFilterControlWidget( p_intf, parent, "compressor" )
1380 {
1381     i_smallfont = -2;
1382     const FilterSliderData::slider_data_t a[7] =
1383     {
1384         { "compressor-rms-peak",    qtr("RMS/peak"),         "",       0.0f,   1.0f,   0.00f, 0.001f, 1.0 },
1385         { "compressor-attack",      qtr("Attack"),       qtr(" ms"),   1.5f, 400.0f,  25.00f, 0.100f, 1.0 },
1386         { "compressor-release",     qtr("Release"),      qtr(" ms"),   2.0f, 800.0f, 100.00f, 0.100f, 1.0 },
1387         { "compressor-threshold",   qtr("Threshold"),    qtr(" dB"), -30.0f,   0.0f, -11.00f, 0.010f, 1.0 },
1388         { "compressor-ratio",       qtr("Ratio"),            ":1",     1.0f,  20.0f,   8.00f, 0.010f, 1.0 },
1389         { "compressor-knee",        qtr("Knee\nradius"), qtr(" dB"),   1.0f,  10.0f,   2.50f, 0.010f, 1.0 },
1390         { "compressor-makeup-gain", qtr("Makeup\ngain"), qtr(" dB"),   0.0f,  24.0f,   7.00f, 0.010f, 1.0 },
1391     };
1392     for( int i=0; i<7 ;i++ ) controls.append( a[i] );
1393     build();
1394 }
1395
1396 /**********************************************************************
1397  * Spatializer
1398  **********************************************************************/
1399
1400 Spatializer::Spatializer( intf_thread_t *p_intf, QWidget *parent )
1401     : AudioFilterControlWidget( p_intf, parent, "spatializer" )
1402 {
1403     i_smallfont = -1;
1404     const FilterSliderData::slider_data_t a[5] =
1405     {
1406         { "spatializer-roomsize",   qtr("Size"),    "", 0.0f, 1.1f, 0.85f, 0.1f, 10.0 },
1407         { "spatializer-width",      qtr("Width"),   "", 0.0f, 1.0f, 1.0f, 0.1f, 10.0 },
1408         { "spatializer-wet",        qtr("Wet"),     "", 0.0f, 1.0f, 0.4f, 0.1f, 10.0 },
1409         { "spatializer-dry",        qtr("Dry"),     "", 0.0f, 1.0f, 0.5f, 0.1f, 10.0 },
1410         { "spatializer-damp",       qtr("Damp"),    "", 0.0f, 1.0f, 0.5f, 0.1f, 10.0 },
1411     };
1412     for( int i=0; i<5 ;i++ ) controls.append( a[i] );
1413     build();
1414 }
1415
1416 #include <QToolButton>
1417 #include <QGridLayout>
1418
1419 #define SUBSDELAY_CFG_MODE                     "subsdelay-mode"
1420 #define SUBSDELAY_CFG_FACTOR                   "subsdelay-factor"
1421 #define SUBSDELAY_MODE_ABSOLUTE                0
1422 #define SUBSDELAY_MODE_RELATIVE_SOURCE_DELAY   1
1423 #define SUBSDELAY_MODE_RELATIVE_SOURCE_CONTENT 2
1424
1425 SyncWidget::SyncWidget( QWidget *_parent ) : QWidget( _parent )
1426 {
1427     QHBoxLayout *layout = new QHBoxLayout;
1428     spinBox.setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter );
1429     spinBox.setDecimals( 3 );
1430     spinBox.setMinimum( -600.0 );
1431     spinBox.setMaximum( 600.0 );
1432     spinBox.setSingleStep( 0.1 );
1433     spinBox.setSuffix( " s" );
1434     spinBox.setButtonSymbols( QDoubleSpinBox::PlusMinus );
1435     CONNECT( &spinBox, valueChanged( double ), this, valueChangedHandler( double ) );
1436     layout->addWidget( &spinBox );
1437     layout->addWidget( &spinLabel );
1438     layout->setContentsMargins( 0, 0, 0, 0 );
1439     setLayout( layout );
1440 }
1441
1442 void SyncWidget::valueChangedHandler( double d )
1443 {
1444     if ( d < 0 )
1445         spinLabel.setText( qtr("(Hastened)") );
1446     else if ( d > 0 )
1447         spinLabel.setText( qtr("(Delayed)") );
1448     else
1449         spinLabel.setText( "" );
1450     emit valueChanged( d );
1451 }
1452
1453 void SyncWidget::setValue( double d )
1454 {
1455     spinBox.setValue( d );
1456 }
1457
1458 SyncControls::SyncControls( intf_thread_t *_p_intf, QWidget *_parent ) :
1459                             QWidget( _parent ) , p_intf( _p_intf )
1460 {
1461     QGroupBox *AVBox, *subsBox;
1462     QToolButton *updateButton;
1463
1464     b_userAction = true;
1465
1466     QGridLayout *mainLayout = new QGridLayout( this );
1467
1468     /* AV sync */
1469     AVBox = new QGroupBox( qtr( "Audio/Video" ) );
1470     QGridLayout *AVLayout = new QGridLayout( AVBox );
1471
1472     QLabel *AVLabel = new QLabel;
1473     AVLabel->setText( qtr( "Audio track synchronization:" ) );
1474     AVLayout->addWidget( AVLabel, 0, 0, 1, 1 );
1475
1476     AVSpin = new SyncWidget( this );
1477     AVLayout->addWidget( AVSpin, 0, 2, 1, 1 );
1478     mainLayout->addWidget( AVBox, 1, 0, 1, 5 );
1479
1480     /* Subs */
1481     subsBox = new QGroupBox( qtr( "Subtitles/Video" ) );
1482     QGridLayout *subsLayout = new QGridLayout( subsBox );
1483
1484     QLabel *subsLabel = new QLabel;
1485     subsLabel->setText( qtr( "Subtitle track syncronization:" ) );
1486     subsLayout->addWidget( subsLabel, 0, 0, 1, 1 );
1487
1488     subsSpin = new SyncWidget( this );
1489     subsLayout->addWidget( subsSpin, 0, 2, 1, 1 );
1490
1491     QLabel *subSpeedLabel = new QLabel;
1492     subSpeedLabel->setText( qtr( "Subtitles speed:" ) );
1493     subsLayout->addWidget( subSpeedLabel, 1, 0, 1, 1 );
1494
1495     subSpeedSpin = new QDoubleSpinBox;
1496     subSpeedSpin->setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter );
1497     subSpeedSpin->setDecimals( 3 );
1498     subSpeedSpin->setMinimum( 1 );
1499     subSpeedSpin->setMaximum( 100 );
1500     subSpeedSpin->setSingleStep( 0.2 );
1501     subSpeedSpin->setSuffix( " fps" );
1502     subSpeedSpin->setButtonSymbols( QDoubleSpinBox::PlusMinus );
1503     subsLayout->addWidget( subSpeedSpin, 1, 2, 1, 1 );
1504
1505     QLabel *subDurationLabel = new QLabel;
1506     subDurationLabel->setText( qtr( "Subtitles duration factor:" ) );
1507     subsLayout->addWidget( subDurationLabel, 2, 0, 1, 1 );
1508
1509     subDurationSpin = new QDoubleSpinBox;
1510     subDurationSpin->setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter );
1511     subDurationSpin->setDecimals( 3 );
1512     subDurationSpin->setMinimum( 0 );
1513     subDurationSpin->setMaximum( 20 );
1514     subDurationSpin->setSingleStep( 0.2 );
1515     subDurationSpin->setButtonSymbols( QDoubleSpinBox::PlusMinus );
1516     subsLayout->addWidget( subDurationSpin, 2, 2, 1, 1 );
1517
1518     mainLayout->addWidget( subsBox, 2, 0, 2, 5 );
1519
1520     updateButton = new QToolButton;
1521     updateButton->setAutoRaise( true );
1522     mainLayout->addWidget( updateButton, 0, 4, 1, 1 );
1523
1524     /* Various Connects */
1525     CONNECT( AVSpin, valueChanged ( double ), this, advanceAudio( double ) ) ;
1526     CONNECT( subsSpin, valueChanged ( double ), this, advanceSubs( double ) ) ;
1527     CONNECT( subSpeedSpin, valueChanged ( double ),
1528              this, adjustSubsSpeed( double ) );
1529     CONNECT( subDurationSpin, valueChanged ( double ),
1530              this, adjustSubsDuration( double ) );
1531
1532     CONNECT( THEMIM->getIM(), synchroChanged(), this, update() );
1533     BUTTON_SET_ACT_I( updateButton, "", update,
1534             qtr( "Force update of this dialog's values" ), update() );
1535
1536     initSubsDuration();
1537
1538     /* Set it */
1539     update();
1540 }
1541
1542 SyncControls::~SyncControls()
1543 {
1544     subsdelayClean();
1545 }
1546
1547 void SyncControls::clean()
1548 {
1549     b_userAction = false;
1550     AVSpin->setValue( 0.0 );
1551     subsSpin->setValue( 0.0 );
1552     subSpeedSpin->setValue( 1.0 );
1553     subsdelayClean();
1554     b_userAction = true;
1555 }
1556
1557 void SyncControls::update()
1558 {
1559     b_userAction = false;
1560
1561     int64_t i_delay;
1562     if( THEMIM->getInput() )
1563     {
1564         i_delay = var_GetTime( THEMIM->getInput(), "audio-delay" );
1565         AVSpin->setValue( ( (double)i_delay ) / 1000000 );
1566         i_delay = var_GetTime( THEMIM->getInput(), "spu-delay" );
1567         subsSpin->setValue( ( (double)i_delay ) / 1000000 );
1568         subSpeedSpin->setValue( var_GetFloat( THEMIM->getInput(), "sub-fps" ) );
1569         subDurationSpin->setValue( var_InheritFloat( p_intf, SUBSDELAY_CFG_FACTOR ) );
1570     }
1571     b_userAction = true;
1572 }
1573
1574 void SyncControls::advanceAudio( double f_advance )
1575 {
1576     if( THEMIM->getInput() && b_userAction )
1577     {
1578         int64_t i_delay = f_advance * 1000000;
1579         var_SetTime( THEMIM->getInput(), "audio-delay", i_delay );
1580     }
1581 }
1582
1583 void SyncControls::advanceSubs( double f_advance )
1584 {
1585     if( THEMIM->getInput() && b_userAction )
1586     {
1587         int64_t i_delay = f_advance * 1000000;
1588         var_SetTime( THEMIM->getInput(), "spu-delay", i_delay );
1589     }
1590 }
1591
1592 void SyncControls::adjustSubsSpeed( double f_fps )
1593 {
1594     if( THEMIM->getInput() && b_userAction )
1595     {
1596         var_SetFloat( THEMIM->getInput(), "sub-fps", f_fps );
1597     }
1598 }
1599
1600 void SyncControls::adjustSubsDuration( double f_factor )
1601 {
1602     if( THEMIM->getInput() && b_userAction )
1603     {
1604         subsdelaySetFactor( f_factor );
1605         ChangeVFiltersString( p_intf, "subsdelay", f_factor > 0 );
1606     }
1607 }
1608
1609 void SyncControls::initSubsDuration()
1610 {
1611     int i_mode = var_InheritInteger( p_intf, SUBSDELAY_CFG_MODE );
1612
1613     switch (i_mode)
1614     {
1615     default:
1616     case SUBSDELAY_MODE_ABSOLUTE:
1617         subDurationSpin->setToolTip( qtr( "Extend subtitles duration by this value.\n"
1618                                           "Set 0 to disable." ) );
1619         subDurationSpin->setSuffix( " s" );
1620         break;
1621     case SUBSDELAY_MODE_RELATIVE_SOURCE_DELAY:
1622         subDurationSpin->setToolTip( qtr( "Multiply subtitles duration by this value.\n"
1623                                           "Set 0 to disable." ) );
1624         subDurationSpin->setSuffix( "" );
1625         break;
1626     case SUBSDELAY_MODE_RELATIVE_SOURCE_CONTENT:
1627         subDurationSpin->setToolTip( qtr( "Recalculate subtitles duration according\n"
1628                                           "to their content and this value.\n"
1629                                           "Set 0 to disable." ) );
1630         subDurationSpin->setSuffix( "" );
1631         break;
1632     }
1633 }
1634
1635 void SyncControls::subsdelayClean()
1636 {
1637     /* Remove subsdelay filter */
1638     ChangeVFiltersString( p_intf, "subsdelay", false );
1639 }
1640
1641 void SyncControls::subsdelaySetFactor( double f_factor )
1642 {
1643     /* Set the factor in the preferences */
1644     config_PutFloat( p_intf, SUBSDELAY_CFG_FACTOR, f_factor );
1645
1646     /* Try to find an instance of subsdelay, and set its factor */
1647     vlc_object_t *p_obj = ( vlc_object_t * ) vlc_object_find_name( p_intf->p_libvlc, "subsdelay" );
1648     if( p_obj )
1649     {
1650         var_SetFloat( p_obj, SUBSDELAY_CFG_FACTOR, f_factor );
1651         vlc_object_release( p_obj );
1652     }
1653 }
1654
1655
1656 /**********************************************************************
1657  * Video filters / Adjust
1658  **********************************************************************/
1659
1660 /**********************************************************************
1661  * Extended playbak controls
1662  **********************************************************************/