]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/extended_panels.cpp
Qt4 - Cleaning trailing spaces...
[vlc] / modules / gui / qt4 / components / extended_panels.cpp
1 /*****************************************************************************
2  * extended_panels.cpp : Extended controls panels
3  ****************************************************************************
4  * Copyright ( C ) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Antoine Cellerier <dionoea .t videolan d@t org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * ( at your option ) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <QLabel>
26 #include <QVariant>
27 #include <QString>
28 #include <QFont>
29 #include <QGridLayout>
30 #include <QSignalMapper>
31
32 #include "components/extended_panels.hpp"
33 #include "dialogs/preferences.hpp"
34 #include "dialogs_provider.hpp"
35 #include "qt4.hpp"
36
37 #include "../../audio_filter/equalizer_presets.h"
38 #include <vlc_aout.h>
39 #include <vlc_intf_strings.h>
40 #include <vlc_vout.h>
41 #include <vlc_osd.h>
42
43 #include <iostream>
44 #include <string.h>
45
46 #if 0
47 class ConfClickHandler : public QObject
48 {
49 public:
50     ConfClickHandler( intf_thread_t *_p_intf, ExtVideo *_e ) : QObject ( _e ) {
51         e = _e; p_intf = _p_intf;
52     }
53     virtual ~ConfClickHandler() {}
54     bool eventFilter( QObject *obj, QEvent *evt )
55     {
56         if( evt->type() == QEvent::MouseButtonPress )
57         {
58             e->gotoConf( obj );
59             return true;
60         }
61         return false;
62     }
63 private:
64     ExtVideo* e;
65     intf_thread_t *p_intf;
66 };
67 #endif
68
69 QString ModuleFromWidgetName( QObject *obj )
70 {
71     return obj->objectName().replace( "Enable","" );
72 }
73
74 QString OptionFromWidgetName( QObject *obj )
75 {
76     /* Gruik ? ... nah */
77     QString option = obj->objectName().replace( "Slider", "" )
78                                       .replace( "Combo" , "" )
79                                       .replace( "Dial"  , "" )
80                                       .replace( "Check" , "" )
81                                       .replace( "Spin"  , "" )
82                                       .replace( "Text"  , "" );
83     for( char a = 'A'; a <= 'Z'; a++ )
84     {
85         option = option.replace( QString( a ),
86                                  QString( '-' ) + QString( a + 'a' - 'A' ) );
87     }
88     return option;
89 }
90
91 ExtVideo::ExtVideo( intf_thread_t *_p_intf, QWidget *_parent ) :
92                            QWidget( _parent ) , p_intf( _p_intf )
93 {
94     ui.setupUi( this );
95
96 #define SETUP_VFILTER( widget ) \
97     { \
98         vlc_object_t *p_obj = ( vlc_object_t * ) \
99             vlc_object_find_name( p_intf->p_libvlc, \
100                                   #widget, \
101                                   FIND_CHILD ); \
102         QCheckBox *checkbox = qobject_cast<QCheckBox*>( ui.widget##Enable ); \
103         QGroupBox *groupbox = qobject_cast<QGroupBox*>( ui.widget##Enable ); \
104         if( p_obj ) \
105         { \
106             vlc_object_release( p_obj ); \
107             if( checkbox ) checkbox->setCheckState( Qt::Checked ); \
108             else groupbox->setChecked( true ); \
109         } \
110         else \
111         { \
112             if( checkbox ) checkbox->setCheckState( Qt::Unchecked ); \
113             else groupbox->setChecked( false ); \
114         } \
115     } \
116     CONNECT( ui.widget##Enable, clicked(), this, updateFilters() );
117 #define SETUP_VFILTER_OPTION( widget, signal ) \
118     initComboBoxItems( ui.widget ); \
119     setWidgetValue( ui.widget ); \
120     CONNECT( ui.widget, signal, this, updateFilterOptions() );
121
122     SETUP_VFILTER( adjust )
123     SETUP_VFILTER_OPTION( hueSlider, valueChanged( int ) )
124     SETUP_VFILTER_OPTION( contrastSlider, valueChanged( int ) )
125     SETUP_VFILTER_OPTION( brightnessSlider, valueChanged( int ) )
126     SETUP_VFILTER_OPTION( saturationSlider, valueChanged( int ) )
127     SETUP_VFILTER_OPTION( gammaSlider, valueChanged( int ) )
128     SETUP_VFILTER_OPTION( brightnessThresholdCheck, stateChanged( int ) )
129
130     SETUP_VFILTER( extract )
131     SETUP_VFILTER_OPTION( extractComponentText, textChanged( QString ) )
132
133     SETUP_VFILTER( colorthres )
134     SETUP_VFILTER_OPTION( colorthresColorText, textChanged( QString ) )
135     SETUP_VFILTER_OPTION( colorthresSaturationthresSlider, valueChanged( int ) )
136     SETUP_VFILTER_OPTION( colorthresSimilaritythresSlider, valueChanged( int ) )
137
138     SETUP_VFILTER( invert )
139
140     SETUP_VFILTER( gradient )
141     SETUP_VFILTER_OPTION( gradientModeCombo, currentIndexChanged( QString ) )
142     SETUP_VFILTER_OPTION( gradientTypeCheck, stateChanged( int ) )
143     SETUP_VFILTER_OPTION( gradientCartoonCheck, stateChanged( int ) )
144
145     SETUP_VFILTER( motionblur )
146     SETUP_VFILTER_OPTION( blurFactorSlider, valueChanged( int ) )
147
148     SETUP_VFILTER( motiondetect )
149
150     SETUP_VFILTER( noise )
151
152     SETUP_VFILTER( psychedelic )
153
154     SETUP_VFILTER( sharpen )
155     SETUP_VFILTER_OPTION( sharpenSigmaSlider, valueChanged( int ) )
156
157     SETUP_VFILTER( ripple )
158
159     SETUP_VFILTER( wave )
160
161     SETUP_VFILTER( transform )
162     SETUP_VFILTER_OPTION( transformTypeCombo, currentIndexChanged( QString ) )
163
164     SETUP_VFILTER( rotate )
165     SETUP_VFILTER_OPTION( rotateAngleDial, valueChanged( int ) )
166     ui.rotateAngleDial->setWrapping( true );
167     ui.rotateAngleDial->setNotchesVisible( true );
168
169     SETUP_VFILTER( puzzle )
170     SETUP_VFILTER_OPTION( puzzleRowsSpin, valueChanged( int ) )
171     SETUP_VFILTER_OPTION( puzzleColsSpin, valueChanged( int ) )
172     SETUP_VFILTER_OPTION( puzzleBlackSlotCheck, stateChanged( int ) )
173
174     SETUP_VFILTER( magnify )
175
176     SETUP_VFILTER( clone )
177     SETUP_VFILTER_OPTION( cloneCountSpin, valueChanged( int ) )
178
179     SETUP_VFILTER( wall )
180     SETUP_VFILTER_OPTION( wallRowsSpin, valueChanged( int ) )
181     SETUP_VFILTER_OPTION( wallColsSpin, valueChanged( int ) )
182
183     SETUP_VFILTER( erase )
184     SETUP_VFILTER_OPTION( eraseMaskText, editingFinished() )
185     SETUP_VFILTER_OPTION( eraseYSpin, valueChanged( int ) )
186     SETUP_VFILTER_OPTION( eraseXSpin, valueChanged( int ) )
187
188     SETUP_VFILTER( marq )
189     SETUP_VFILTER_OPTION( marqMarqueeText, textChanged( QString ) )
190     SETUP_VFILTER_OPTION( marqPositionCombo, currentIndexChanged( QString ) )
191
192     SETUP_VFILTER( logo )
193     SETUP_VFILTER_OPTION( logoFileText, editingFinished() )
194     SETUP_VFILTER_OPTION( logoYSpin, valueChanged( int ) )
195     SETUP_VFILTER_OPTION( logoXSpin, valueChanged( int ) )
196     SETUP_VFILTER_OPTION( logoTransparencySlider, valueChanged( int ) )
197
198 #undef SETUP_VFILTER
199 #undef SETUP_VFILTER_OPTION
200 }
201
202 ExtVideo::~ExtVideo()
203 {
204 }
205
206 void ExtVideo::ChangeVFiltersString( char *psz_name, vlc_bool_t b_add )
207 {
208     vout_thread_t *p_vout;
209     char *psz_parser, *psz_string;
210     const char *psz_filter_type;
211
212     /* Please leave p_libvlc_global. This is where cached modules are
213      * stored. We're not trying to find a module instance. */
214     module_t *p_obj = module_FindName( p_intf, psz_name );
215     if( !p_obj )
216     {
217         msg_Err( p_intf, "Unable to find filter module \"%s\n.", psz_name );
218         return;
219     }
220
221     if( module_IsCapable( p_obj, "video filter2" ) )
222     {
223         psz_filter_type = "video-filter";
224     }
225     else if( module_IsCapable( p_obj, "video filter" ) )
226     {
227         psz_filter_type = "vout-filter";
228     }
229     else if( module_IsCapable( p_obj, "sub filter" ) )
230     {
231         psz_filter_type = "sub-filter";
232     }
233     else
234     {
235         vlc_object_release( p_obj );
236         msg_Err( p_intf, "Unknown video filter type." );
237         return;
238     }
239     vlc_object_release( p_obj );
240
241     psz_string = config_GetPsz( p_intf, psz_filter_type );
242
243     if( !psz_string ) psz_string = strdup( "" );
244
245     psz_parser = strstr( psz_string, psz_name );
246
247     if( b_add )
248     {
249         if( !psz_parser )
250         {
251             psz_parser = psz_string;
252             asprintf( &psz_string, ( *psz_string ) ? "%s:%s" : "%s%s",
253                             psz_string, psz_name );
254             free( psz_parser );
255         }
256         else
257         {
258             return;
259         }
260     }
261     else
262     {
263         if( psz_parser )
264         {
265             if( *( psz_parser + strlen( psz_name ) ) == ':' )
266             {
267                 memmove( psz_parser, psz_parser + strlen( psz_name ) + 1,
268                          strlen( psz_parser + strlen( psz_name ) + 1 ) + 1 );
269             }
270             else
271             {
272                 *psz_parser = '\0';
273             }
274
275             /* Remove trailing : : */
276             if( strlen( psz_string ) > 0 &&
277                 *( psz_string + strlen( psz_string ) -1 ) == ':' )
278             {
279                 *( psz_string + strlen( psz_string ) -1 ) = '\0';
280             }
281         }
282         else
283         {
284             free( psz_string );
285             return;
286         }
287     }
288     /* Vout is not kept, so put that in the config */
289     config_PutPsz( p_intf, psz_filter_type, psz_string );
290     if( !strcmp( psz_filter_type, "video-filter" ) )
291         ui.videoFilterText->setText( psz_string );
292     else if( !strcmp( psz_filter_type, "vout-filter" ) )
293         ui.voutFilterText->setText( psz_string );
294     else if( !strcmp( psz_filter_type, "sub-filter" ) )
295         ui.subpictureFilterText->setText( psz_string );
296
297     /* Try to set on the fly */
298     p_vout = ( vout_thread_t * )vlc_object_find( p_intf, VLC_OBJECT_VOUT,
299                                               FIND_ANYWHERE );
300     if( p_vout )
301     {
302         if( !strcmp( psz_filter_type, "sub-filter" ) )
303             var_SetString( p_vout->p_spu, psz_filter_type, psz_string );
304         else
305             var_SetString( p_vout, psz_filter_type, psz_string );
306         vlc_object_release( p_vout );
307     }
308
309     free( psz_string );
310 }
311
312 void ExtVideo::updateFilters()
313 {
314     QString module = ModuleFromWidgetName( sender() );
315     //std::cout << "Module name: " << module.toStdString() << std::endl;
316
317     QCheckBox *checkbox = qobject_cast<QCheckBox*>( sender() );
318     QGroupBox *groupbox = qobject_cast<QGroupBox*>( sender() );
319
320     ChangeVFiltersString( qtu( module ),
321                           checkbox ? checkbox->isChecked()
322                                    : groupbox->isChecked() );
323 }
324
325 void ExtVideo::initComboBoxItems( QObject *widget )
326 {
327     QComboBox *combobox = qobject_cast<QComboBox*>( widget );
328     if( !combobox ) return;
329     QString option = OptionFromWidgetName( widget );
330     module_config_t *p_item = config_FindConfig( VLC_OBJECT( p_intf ),
331                                                  option.toStdString().c_str() );
332     if( p_item )
333     {
334         int i_type = p_item->i_type & CONFIG_ITEM;
335         for( int i_index = 0; i_index < p_item->i_list; i_index++ )
336         {
337             if( i_type == CONFIG_ITEM_INTEGER
338              || i_type == CONFIG_ITEM_BOOL )
339                 combobox->addItem( qfu( p_item->ppsz_list_text[i_index] ),
340                                    p_item->pi_list[i_index] );
341             else if( i_type == CONFIG_ITEM_STRING )
342                 combobox->addItem( qfu( p_item->ppsz_list_text[i_index] ),
343                                    p_item->ppsz_list[i_index] );
344         }
345     }
346     else
347     {
348         msg_Err( p_intf, "Couldn't find option \"%s\".",
349                  option.toStdString().c_str() );
350     }
351 }
352
353 void ExtVideo::setWidgetValue( QObject *widget )
354 {
355     QString module = ModuleFromWidgetName( widget->parent() );
356     //std::cout << "Module name: " << module.toStdString() << std::endl;
357     QString option = OptionFromWidgetName( widget );
358     //std::cout << "Option name: " << option.toStdString() << std::endl;
359
360     vlc_object_t *p_obj = ( vlc_object_t * )
361         vlc_object_find_name( p_intf->p_libvlc,
362                               module.toStdString().c_str(),
363                               FIND_CHILD );
364     int i_type;
365     vlc_value_t val;
366
367     if( !p_obj )
368     {
369 #if 0
370         msg_Dbg( p_intf,
371                  "Module instance %s not found, looking in config values.",
372                  module.toStdString().c_str() );
373 #endif
374         i_type = config_GetType( p_intf, option.toStdString().c_str() ) & 0xf0;
375         switch( i_type )
376         {
377             case VLC_VAR_INTEGER:
378             case VLC_VAR_BOOL:
379                 val.i_int = config_GetInt( p_intf, option.toStdString().c_str() );
380                 break;
381             case VLC_VAR_FLOAT:
382                 val.f_float = config_GetFloat( p_intf, option.toStdString().c_str() );
383                 break;
384             case VLC_VAR_STRING:
385                 val.psz_string = config_GetPsz( p_intf, option.toStdString().c_str() );
386                 break;
387         }
388     }
389     else
390     {
391         i_type = var_Type( p_obj, option.toStdString().c_str() ) & 0xf0;
392         var_Get( p_obj, option.toStdString().c_str(), &val );
393         vlc_object_release( p_obj );
394     }
395
396     /* Try to cast to all the widgets we're likely to encounter. Only
397      * one of the casts is expected to work. */
398     QSlider        *slider        = qobject_cast<QSlider*>       ( widget );
399     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( widget );
400     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( widget );
401     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( widget );
402     QDial          *dial          = qobject_cast<QDial*>         ( widget );
403     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( widget );
404     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( widget );
405
406     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
407     {
408         int i_int = 0;
409         if( slider )        slider->setValue( val.i_int );
410         else if( checkbox ) checkbox->setCheckState( val.i_int? Qt::Checked
411                                                               : Qt::Unchecked );
412         else if( spinbox )  spinbox->setValue( val.i_int );
413         else if( dial )     dial->setValue( ( 540-val.i_int )%360 );
414         else if( lineedit )
415         {
416             char str[30];
417             sprintf( str, "%06X", val.i_int );
418             lineedit->setText( str );
419         }
420         else if( combobox ) combobox->setCurrentIndex(
421                             combobox->findData( val.i_int ) );
422         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
423     }
424     else if( i_type == VLC_VAR_FLOAT )
425     {
426         double f_float = 0;
427         if( slider ) slider->setValue( ( int )( val.f_float*( double )slider->tickInterval() ) ); /* hack alert! */
428         else if( doublespinbox ) doublespinbox->setValue( val.f_float );
429         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
430     }
431     else if( i_type == VLC_VAR_STRING )
432     {
433         if( lineedit ) lineedit->setText( qfu( val.psz_string ) );
434         else if( combobox ) combobox->setCurrentIndex(
435                             combobox->findData( qfu( val.psz_string ) ) );
436         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
437         free( val.psz_string );
438     }
439     else
440         msg_Err( p_intf,
441                  "Module %s's %s variable is of an unsupported type ( %d )",
442                  module.toStdString().c_str(),
443                  option.toStdString().c_str(),
444                  i_type );
445 }
446
447 void ExtVideo::updateFilterOptions()
448 {
449     QString module = ModuleFromWidgetName( sender()->parent() );
450     //std::cout << "Module name: " << module.toStdString() << std::endl;
451     QString option = OptionFromWidgetName( sender() );
452     //std::cout << "Option name: " << option.toStdString() << std::endl;
453
454     vlc_object_t *p_obj = ( vlc_object_t * )
455         vlc_object_find_name( p_intf->p_libvlc,
456                               module.toStdString().c_str(),
457                               FIND_CHILD );
458     if( !p_obj )
459     {
460         msg_Err( p_intf, "Module %s not found.", module.toStdString().c_str() );
461         return;
462     }
463
464     int i_type = var_Type( p_obj, option.toStdString().c_str() );
465     bool b_is_command = ( i_type & VLC_VAR_ISCOMMAND );
466     if( !b_is_command )
467     {
468         msg_Warn( p_intf, "Module %s's %s variable isn't a command. You'll need to restart the filter to take change into account.",
469                  module.toStdString().c_str(),
470                  option.toStdString().c_str() );
471         /* FIXME: restart automatically somewhere near the end of this function */
472     }
473
474     /* Try to cast to all the widgets we're likely to encounter. Only
475      * one of the casts is expected to work. */
476     QSlider        *slider        = qobject_cast<QSlider*>       ( sender() );
477     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( sender() );
478     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( sender() );
479     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( sender() );
480     QDial          *dial          = qobject_cast<QDial*>         ( sender() );
481     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( sender() );
482     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( sender() );
483
484     i_type &= 0xf0;
485     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
486     {
487         int i_int = 0;
488         if( slider )        i_int = slider->value();
489         else if( checkbox ) i_int = checkbox->checkState() == Qt::Checked;
490         else if( spinbox )  i_int = spinbox->value();
491         else if( dial )     i_int = ( 540-dial->value() )%360;
492         else if( lineedit ) i_int = lineedit->text().toInt( NULL,16 );
493         else if( combobox ) i_int = combobox->itemData( combobox->currentIndex() ).toInt();
494         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
495         config_PutInt( p_intf, option.toStdString().c_str(), i_int );
496         if( b_is_command )
497         {
498             if( i_type == VLC_VAR_INTEGER )
499                 var_SetInteger( p_obj, option.toStdString().c_str(), i_int );
500             else
501                 var_SetBool( p_obj, option.toStdString().c_str(), i_int );
502         }
503     }
504     else if( i_type == VLC_VAR_FLOAT )
505     {
506         double f_float = 0;
507         if( slider )             f_float = ( double )slider->value()
508                                          / ( double )slider->tickInterval(); /* hack alert! */
509         else if( doublespinbox ) f_float = doublespinbox->value();
510         else if( lineedit ) f_float = lineedit->text().toDouble();
511         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
512         config_PutFloat( p_intf, option.toStdString().c_str(), f_float );
513         if( b_is_command )
514             var_SetFloat( p_obj, option.toStdString().c_str(), f_float );
515     }
516     else if( i_type == VLC_VAR_STRING )
517     {
518         char *psz_string = NULL;
519         if( lineedit ) psz_string = strdup( qtu( lineedit->text() ) );
520         else if( combobox ) psz_string = strdup( qtu( combobox->itemData(
521                                          combobox->currentIndex() ).toString() ) );
522         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
523         config_PutPsz( p_intf, option.toStdString().c_str(), psz_string );
524         if( b_is_command )
525             var_SetString( p_obj, option.toStdString().c_str(), psz_string );
526         free( psz_string );
527     }
528     else
529         msg_Err( p_intf,
530                  "Module %s's %s variable is of an unsupported type ( %d )",
531                  module.toStdString().c_str(),
532                  option.toStdString().c_str(),
533                  i_type );
534
535     vlc_object_release( p_obj );
536 }
537
538 #if 0
539 void ExtVideo::gotoConf( QObject* src )
540 {
541 #define SHOWCONF( module ) \
542     if( src->objectName().contains( module ) ) \
543     { \
544         PrefsDialog::getInstance( p_intf )->showModulePrefs( module ); \
545         return; \
546     }
547     SHOWCONF( "clone" );
548     SHOWCONF( "magnify" );
549     SHOWCONF( "wave" );
550     SHOWCONF( "ripple" );
551     SHOWCONF( "invert" );
552     SHOWCONF( "puzzle" );
553     SHOWCONF( "wall" );
554     SHOWCONF( "gradient" );
555     SHOWCONF( "colorthres" )
556 }
557 #endif
558
559 /**********************************************************************
560  * Equalizer
561  **********************************************************************/
562
563 static const QString band_frequencies[] =
564 {
565     "  60 Hz  ", " 170 Hz ", " 310 Hz ", " 600 Hz ", "  1 kHz ",
566     "  3 kHz  ", "  6 kHz ", " 12 kHz ", " 14 kHz ", " 16 kHz "
567 };
568
569 Equalizer::Equalizer( intf_thread_t *_p_intf, QWidget *_parent ) :
570                             QWidget( _parent ) , p_intf( _p_intf )
571 {
572     QFont smallFont = QApplication::font( static_cast<QWidget*>( 0 ) );
573     smallFont.setPointSize( smallFont.pointSize() - 3 );
574
575     ui.setupUi( this );
576     presetsComboBox = ui.presetsCombo;
577
578     ui.preampLabel->setFont( smallFont );
579     ui.preampSlider->setMaximum( 400 );
580     for( int i = 0 ; i < NB_PRESETS ; i ++ )
581     {
582         ui.presetsCombo->addItem( qtr( preset_list_text[i] ),
583                                   QVariant( i ) );
584     }
585     CONNECT( ui.presetsCombo, activated( int ), this, setPreset( int ) );
586
587     BUTTONACT( ui.enableCheck, enable() );
588     BUTTONACT( ui.eq2PassCheck, set2Pass() );
589
590     CONNECT( ui.preampSlider, valueChanged( int ), this, setPreamp() );
591
592     QGridLayout *grid = new QGridLayout( ui.frame );
593     grid->setMargin( 0 );
594     for( int i = 0 ; i < BANDS ; i++ )
595     {
596         bands[i] = new QSlider( Qt::Vertical );
597         bands[i]->setMaximum( 400 );
598         bands[i]->setValue( 200 );
599         CONNECT( bands[i], valueChanged( int ), this, setBand() );
600         band_texts[i] = new QLabel( band_frequencies[i] + "\n0.0dB" );
601         band_texts[i]->setFont( smallFont );
602         grid->addWidget( bands[i], 0, i );
603         grid->addWidget( band_texts[i], 1, i );
604     }
605
606     /* Write down initial values */
607     aout_instance_t *p_aout = ( aout_instance_t * )vlc_object_find( p_intf,
608                                     VLC_OBJECT_AOUT, FIND_ANYWHERE );
609     char *psz_af;
610     char *psz_bands;
611     float f_preamp;
612     if( p_aout )
613     {
614         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
615         if( var_GetBool( p_aout, "equalizer-2pass" ) )
616             ui.eq2PassCheck->setChecked( true );
617         psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
618         f_preamp = var_GetFloat( p_aout, "equalizer-preamp" );
619         vlc_object_release( p_aout );
620     }
621     else
622     {
623         psz_af = config_GetPsz( p_intf, "audio-filter" );
624         if( config_GetInt( p_intf, "equalizer-2pass" ) )
625             ui.eq2PassCheck->setChecked( true );
626         psz_bands = config_GetPsz( p_intf, "equalizer-bands" );
627         f_preamp = config_GetFloat( p_intf, "equalizer-preamp" );
628     }
629     if( psz_af && strstr( psz_af, "equalizer" ) != NULL )
630         ui.enableCheck->setChecked( true );
631     free( psz_af );
632     enable( ui.enableCheck->isChecked() );
633
634     setValues( psz_bands, f_preamp );
635 }
636
637 Equalizer::~Equalizer()
638 {
639 }
640
641 void Equalizer::enable()
642 {
643     bool en = ui.enableCheck->isChecked();
644     aout_EnableFilter( VLC_OBJECT( p_intf ), "equalizer",
645                        en ? VLC_TRUE : VLC_FALSE );
646 //    aout_EnableFilter( VLC_OBJECT( p_intf ), "upmixer",
647 //                       en ? VLC_TRUE : VLC_FALSE );
648 //     aout_EnableFilter( VLC_OBJECT( p_intf ), "vsurround",
649 //                       en ? VLC_TRUE : VLC_FALSE );
650      enable( en );
651 }
652
653 void Equalizer::enable( bool en )
654 {
655     ui.eq2PassCheck->setEnabled( en );
656     ui.preampLabel->setEnabled( en );
657     ui.preampSlider->setEnabled( en  );
658     for( int i = 0 ; i< BANDS; i++ )
659     {
660         bands[i]->setEnabled( en ); band_texts[i]->setEnabled( en );
661     }
662 }
663
664 void Equalizer::set2Pass()
665 {
666     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
667                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
668     vlc_bool_t b_2p = ui.eq2PassCheck->isChecked();
669
670     if( p_aout == NULL )
671         config_PutInt( p_intf, "equalizer-2pass", b_2p );
672     else
673     {
674         var_SetBool( p_aout, "equalizer-2pass", b_2p );
675         config_PutInt( p_intf, "equalizer-2pass", b_2p );
676         for( int i = 0; i < p_aout->i_nb_inputs; i++ )
677         {
678             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
679         }
680         vlc_object_release( p_aout );
681     }
682 }
683
684 void Equalizer::setPreamp()
685 {
686     float f= ( float )(  ui.preampSlider->value() ) /10 - 20;
687     char psz_val[5];
688     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
689                                        VLC_OBJECT_AOUT, FIND_ANYWHERE );
690
691     sprintf( psz_val, "%.1f", f );
692     ui.preampLabel->setText( qtr( "Preamp\n" ) + psz_val + qtr( "dB" ) );
693     if( p_aout )
694     {
695         delCallbacks( p_aout );
696         var_SetFloat( p_aout, "equalizer-preamp", f );
697         addCallbacks( p_aout );
698         vlc_object_release( p_aout );
699     }
700     config_PutFloat( p_intf, "equalizer-preamp", f );
701 }
702
703 void Equalizer::setBand()
704 {
705     char psz_values[102]; memset( psz_values, 0, 102 );
706
707     /**\todo smoothing */
708
709     for( int i = 0 ; i< BANDS ; i++ )
710     {
711         char psz_val[5];
712         float f_val = ( float )(  bands[i]->value() ) / 10 - 20 ;
713         sprintf( psz_values, "%s %f", psz_values, f_val );
714         sprintf( psz_val, "% 5.1f", f_val );
715         band_texts[i]->setText( band_frequencies[i] + "\n" + psz_val + "dB" );
716     }
717     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
718                                           VLC_OBJECT_AOUT, FIND_ANYWHERE );
719     if( p_aout )
720     {
721         delCallbacks( p_aout );
722         var_SetString( p_aout, "equalizer-bands", psz_values );
723         addCallbacks( p_aout );
724         vlc_object_release( p_aout );
725     }
726 }
727 void Equalizer::setValues( char *psz_bands, float f_preamp )
728 {
729     char *p = psz_bands;
730     if ( p )
731     {
732         for( int i = 0; i < 10; i++ )
733         {
734             char psz_val[5];
735             float f = strtof( p, &p );
736             int  i_val= ( int )( ( f + 20 ) * 10 );
737             bands[i]->setValue(  i_val );
738             sprintf( psz_val, "% 5.1f", f );
739             band_texts[i]->setText( band_frequencies[i] + "\n" + psz_val +
740                                     "dB" );
741             if( p == NULL || *p == '\0' ) break;
742             p++;
743             if( *p == '\0' )  break;
744         }
745     }
746     char psz_val[5];
747     int i_val = ( int )( ( f_preamp + 20 ) * 10 );
748     sprintf( psz_val, "%.1f", f_preamp );
749     ui.preampSlider->setValue( i_val );
750     ui.preampLabel->setText( qtr( "Preamp\n" ) + psz_val + qtr( "dB" ) );
751 }
752
753 void Equalizer::setPreset( int preset )
754 {
755     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
756                                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
757
758     char psz_values[102]; memset( psz_values, 0, 102 );
759     for( int i = 0 ; i< 10 ;i++ )
760         sprintf( psz_values, "%s %.1f", psz_values,
761                                         eqz_preset_10b[preset]->f_amp[i] );
762
763     if( p_aout )
764     {
765         delCallbacks( p_aout );
766         var_SetString( p_aout, "equalizer-bands", psz_values );
767         var_SetFloat( p_aout, "equalizer-preamp",
768                       eqz_preset_10b[preset]->f_preamp );
769         addCallbacks( p_aout );
770         vlc_object_release( p_aout );
771     }
772     config_PutPsz( p_intf, "equalizer-bands", psz_values );
773     config_PutFloat( p_intf, "equalizer-preamp",
774                     eqz_preset_10b[preset]->f_preamp );
775
776     setValues( psz_values, eqz_preset_10b[preset]->f_preamp );
777 }
778
779 static int PresetCallback( vlc_object_t *p_this, char const *psz_cmd,
780                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
781 {
782     char *psz_preset = newval.psz_string;
783     Equalizer *eq = ( Equalizer * )p_data;
784     eq->presetsComboBox->setCurrentIndex( eq->presetsComboBox->findText( qfu( psz_preset ) ) );
785     return VLC_SUCCESS;
786 }
787
788 void Equalizer::delCallbacks( aout_instance_t *p_aout )
789 {
790     //var_DelCallback( p_aout, "equalizer-bands", EqzCallback, this );
791     //var_DelCallback( p_aout, "equalizer-preamp", EqzCallback, this );
792     var_DelCallback( p_aout, "equalizer-preset", PresetCallback, this );
793 }
794
795 void Equalizer::addCallbacks( aout_instance_t *p_aout )
796 {
797     //var_AddCallback( p_aout, "equalizer-bands", EqzCallback, this );
798     //var_AddCallback( p_aout, "equalizer-preamp", EqzCallback, this );
799     var_AddCallback( p_aout, "equalizer-preset", PresetCallback, this );
800 }
801
802 /**********************************************************************
803  * Audio filters
804  **********************************************************************/
805
806 /**********************************************************************
807  * Spatializer
808  **********************************************************************/
809 static const char *psz_control_names[] =
810 {
811     "Roomsize", "Width" , "Wet", "Dry", "Damp"
812 };
813
814 Spatializer::Spatializer( intf_thread_t *_p_intf, QWidget *_parent ) :
815     QWidget( _parent ) , p_intf( _p_intf )
816 {
817     QFont smallFont = QApplication::font( static_cast<QWidget*>( 0 ) );
818     smallFont.setPointSize( smallFont.pointSize() - 3 );
819
820     QGridLayout *layout = new QGridLayout( this );
821     layout->setMargin( 0 );
822
823     enableCheck = new QCheckBox( qfu( "Enable spatializer" ) );
824     layout->addWidget( enableCheck, 0, 0, 1, NUM_SP_CTRL );
825
826     for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
827     {
828         spatCtrl[i] = new QSlider( Qt::Vertical );
829         if( i < 2 )
830         {
831             spatCtrl[i]->setMaximum( 10 );
832             spatCtrl[i]->setValue( 2 );
833         }
834         else
835         {
836             spatCtrl[i]->setMaximum( 10 );
837             spatCtrl[i]->setValue( 0 );
838             spatCtrl[i]->setMinimum( -10 );
839         }
840         oldControlVars[i] = spatCtrl[i]->value();
841         CONNECT( spatCtrl[i], valueChanged( int ), this, setInitValues() );
842         ctrl_texts[i] = new QLabel( qfu( psz_control_names[i] ) + "\n" );
843         ctrl_texts[i]->setFont( smallFont );
844         ctrl_readout[i] = new QLabel( "" );
845         ctrl_readout[i]->setFont( smallFont );
846         layout->addWidget( spatCtrl[i], 1, i );
847         layout->addWidget( ctrl_readout[i], 2, i );
848         layout->addWidget( ctrl_texts[i], 3, i );
849     }
850
851     BUTTONACT( enableCheck, enable() );
852
853     /* Write down initial values */
854     aout_instance_t *p_aout = ( aout_instance_t * )
855         vlc_object_find( p_intf, VLC_OBJECT_AOUT, FIND_ANYWHERE );
856     char *psz_af;
857
858     if( p_aout )
859     {
860         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
861         for( int i = 0; i < NUM_SP_CTRL ; i++ )
862         {
863             controlVars[i] = var_GetFloat( p_aout, psz_control_names[i] );
864         }
865         vlc_object_release( p_aout );
866     }
867     else
868     {
869         psz_af = config_GetPsz( p_aout, "audio-filter" );
870         for( int i = 0; i < NUM_SP_CTRL ; i++ )
871         {
872             controlVars[i] = config_GetFloat( p_intf, psz_control_names[i] );
873         }
874     }
875     if( psz_af && strstr( psz_af, "spatializer" ) != NULL )
876         enableCheck->setChecked( true );
877     free( psz_af );
878     enable( enableCheck->isChecked() );
879     setValues( controlVars );
880 }
881
882 Spatializer::~Spatializer()
883 {
884 }
885
886 void Spatializer::enable()
887 {
888     bool en = enableCheck->isChecked();
889     aout_EnableFilter( VLC_OBJECT( p_intf ), "spatializer",
890             en ? VLC_TRUE : VLC_FALSE );
891     enable( en );
892 }
893
894 void Spatializer::enable( bool en )
895 {
896     for( int i = 0 ; i< NUM_SP_CTRL; i++ )
897     {
898         spatCtrl[i]->setEnabled( en );
899         ctrl_texts[i]->setEnabled( en );
900         ctrl_readout[i]->setEnabled( en );
901     }
902 }
903 void Spatializer::setInitValues()
904 {
905     setValues( controlVars );
906 }
907
908 void Spatializer::setValues( float *controlVars )
909 {
910     char psz_val[5];
911     char var_name[5];
912     aout_instance_t *p_aout= ( aout_instance_t * )
913         vlc_object_find( p_intf, VLC_OBJECT_AOUT, FIND_ANYWHERE );
914
915     for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
916     {
917         float f= ( float )(  spatCtrl[i]->value() );
918         sprintf( psz_val, "%.1f", f );
919         ctrl_readout[i]->setText( psz_val );
920     }
921     if( p_aout )
922     {
923         for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
924         {
925             if( oldControlVars[i] != spatCtrl[i]->value() )
926             {
927                 var_SetFloat( p_aout, psz_control_names[i],
928                         ( float )spatCtrl[i]->value() );
929                 config_PutFloat( p_intf, psz_control_names[i],
930                         ( float ) spatCtrl[i]->value() );
931                 oldControlVars[i] = ( float ) spatCtrl[i]->value();
932             }
933         }
934         vlc_object_release( p_aout );
935     }
936
937 }
938 void Spatializer::delCallbacks( aout_instance_t *p_aout )
939 {
940     //    var_DelCallback( p_aout, "Spatializer-bands", EqzCallback, this );
941     //    var_DelCallback( p_aout, "Spatializer-preamp", EqzCallback, this );
942 }
943
944 void Spatializer::addCallbacks( aout_instance_t *p_aout )
945 {
946     //    var_AddCallback( p_aout, "Spatializer-bands", EqzCallback, this );
947     //    var_AddCallback( p_aout, "Spatializer-preamp", EqzCallback, this );
948 }
949
950 /**********************************************************************
951  * Video filters / Adjust
952  **********************************************************************/
953
954 /**********************************************************************
955  * Extended playbak controls
956  **********************************************************************/