]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/extended_panels.cpp
Temporary workaround for video filter wrapper in qt4 (clone).
[vlc] / modules / gui / qt4 / components / extended_panels.cpp
1 /*****************************************************************************
2  * extended_panels.cpp : Extended controls panels
3  ****************************************************************************
4  * Copyright (C) 2006-2008 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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <QLabel>
30 #include <QVariant>
31 #include <QString>
32 #include <QFont>
33 #include <QGridLayout>
34 #include <QSignalMapper>
35 #include <QComboBox>
36
37 #include "components/extended_panels.hpp"
38 #include "dialogs/preferences.hpp"
39 #include "qt4.hpp"
40 #include "input_manager.hpp"
41
42 #include "../../audio_filter/equalizer_presets.h"
43 #include <vlc_aout.h>
44 #include <vlc_intf_strings.h>
45 #include <vlc_vout.h>
46 #include <vlc_osd.h>
47
48 #include <vlc_charset.h> /* us_strtod */
49
50 #if 0
51 class ConfClickHandler : public QObject
52 {
53 public:
54     ConfClickHandler( intf_thread_t *_p_intf, ExtVideo *_e ) : QObject ( _e ) {
55         e = _e; p_intf = _p_intf;
56     }
57     virtual ~ConfClickHandler() {}
58     bool eventFilter( QObject *obj, QEvent *evt )
59     {
60         if( evt->type() == QEvent::MouseButtonPress )
61         {
62             e->gotoConf( obj );
63             return true;
64         }
65         return false;
66     }
67 private:
68     ExtVideo* e;
69     intf_thread_t *p_intf;
70 };
71 #endif
72
73 QString ModuleFromWidgetName( QObject *obj )
74 {
75     return obj->objectName().replace( "Enable","" );
76 }
77
78 QString OptionFromWidgetName( QObject *obj )
79 {
80     /* Gruik ? ... nah */
81     QString option = obj->objectName().replace( "Slider", "" )
82                                       .replace( "Combo" , "" )
83                                       .replace( "Dial"  , "" )
84                                       .replace( "Check" , "" )
85                                       .replace( "Spin"  , "" )
86                                       .replace( "Text"  , "" );
87     for( char a = 'A'; a <= 'Z'; a++ )
88     {
89         option = option.replace( QString( a ),
90                                  QString( '-' ) + QString( a + 'a' - 'A' ) );
91     }
92     return option;
93 }
94
95 ExtVideo::ExtVideo( intf_thread_t *_p_intf, QTabWidget *_parent ) :
96             QObject( _parent ), p_intf( _p_intf )
97 {
98     ui.setupUi( _parent );
99     p_vout = NULL;
100
101 #define SETUP_VFILTER( widget ) \
102     { \
103         vlc_object_t *p_obj = ( vlc_object_t * ) \
104             vlc_object_find_name( p_intf->p_libvlc, \
105                                   #widget, \
106                                   FIND_CHILD ); \
107         QCheckBox *checkbox = qobject_cast<QCheckBox*>( ui.widget##Enable ); \
108         QGroupBox *groupbox = qobject_cast<QGroupBox*>( ui.widget##Enable ); \
109         if( p_obj ) \
110         { \
111             vlc_object_release( p_obj ); \
112             if( checkbox ) checkbox->setChecked( true ); \
113             else groupbox->setChecked( true ); \
114         } \
115         else \
116         { \
117             if( checkbox ) checkbox->setChecked( false ); \
118             else groupbox->setChecked( false ); \
119         } \
120     } \
121     CONNECT( ui.widget##Enable, clicked(), this, updateFilters() );
122 #define SETUP_VFILTER_OPTION( widget, signal ) \
123     initComboBoxItems( ui.widget ); \
124     setWidgetValue( ui.widget ); \
125     CONNECT( ui.widget, signal, this, updateFilterOptions() );
126
127     SETUP_VFILTER( adjust )
128     SETUP_VFILTER_OPTION( hueSlider, valueChanged( int ) )
129     SETUP_VFILTER_OPTION( contrastSlider, valueChanged( int ) )
130     SETUP_VFILTER_OPTION( brightnessSlider, valueChanged( int ) )
131     SETUP_VFILTER_OPTION( saturationSlider, valueChanged( int ) )
132     SETUP_VFILTER_OPTION( gammaSlider, valueChanged( int ) )
133     SETUP_VFILTER_OPTION( brightnessThresholdCheck, stateChanged( int ) )
134
135     SETUP_VFILTER( extract )
136     SETUP_VFILTER_OPTION( extractComponentText, textChanged( const QString& ) )
137
138     SETUP_VFILTER( colorthres )
139     SETUP_VFILTER_OPTION( colorthresColorText, textChanged( const QString& ) )
140     SETUP_VFILTER_OPTION( colorthresSaturationthresSlider, valueChanged( int ) )
141     SETUP_VFILTER_OPTION( colorthresSimilaritythresSlider, valueChanged( int ) )
142
143     SETUP_VFILTER( invert )
144
145     SETUP_VFILTER( gradient )
146     SETUP_VFILTER_OPTION( gradientModeCombo, currentIndexChanged( QString ) )
147     SETUP_VFILTER_OPTION( gradientTypeCheck, stateChanged( int ) )
148     SETUP_VFILTER_OPTION( gradientCartoonCheck, stateChanged( int ) )
149
150     SETUP_VFILTER( motionblur )
151     SETUP_VFILTER_OPTION( blurFactorSlider, valueChanged( int ) )
152
153     SETUP_VFILTER( motiondetect )
154
155     SETUP_VFILTER( noise )
156
157     SETUP_VFILTER( psychedelic )
158
159     SETUP_VFILTER( sharpen )
160     SETUP_VFILTER_OPTION( sharpenSigmaSlider, valueChanged( int ) )
161
162     SETUP_VFILTER( ripple )
163
164     SETUP_VFILTER( wave )
165
166     SETUP_VFILTER( transform )
167     SETUP_VFILTER_OPTION( transformTypeCombo, currentIndexChanged( QString ) )
168
169     SETUP_VFILTER( rotate )
170     SETUP_VFILTER_OPTION( rotateAngleDial, valueChanged( int ) )
171     ui.rotateAngleDial->setWrapping( true );
172     ui.rotateAngleDial->setNotchesVisible( true );
173
174     SETUP_VFILTER( puzzle )
175     SETUP_VFILTER_OPTION( puzzleRowsSpin, valueChanged( int ) )
176     SETUP_VFILTER_OPTION( puzzleColsSpin, valueChanged( int ) )
177     SETUP_VFILTER_OPTION( puzzleBlackSlotCheck, stateChanged( int ) )
178
179     SETUP_VFILTER( magnify )
180
181     SETUP_VFILTER( clone )
182     SETUP_VFILTER_OPTION( cloneCountSpin, valueChanged( int ) )
183
184     SETUP_VFILTER( wall )
185     SETUP_VFILTER_OPTION( wallRowsSpin, valueChanged( int ) )
186     SETUP_VFILTER_OPTION( wallColsSpin, valueChanged( int ) )
187
188     SETUP_VFILTER( panoramix )
189     SETUP_VFILTER_OPTION( panoramixRowsSpin, valueChanged( int ) )
190     SETUP_VFILTER_OPTION( panoramixColsSpin, valueChanged( int ) )
191
192
193     SETUP_VFILTER( erase )
194     SETUP_VFILTER_OPTION( eraseMaskText, editingFinished() )
195     SETUP_VFILTER_OPTION( eraseYSpin, valueChanged( int ) )
196     SETUP_VFILTER_OPTION( eraseXSpin, valueChanged( int ) )
197
198     SETUP_VFILTER( marq )
199     SETUP_VFILTER_OPTION( marqMarqueeText, textChanged( const QString& ) )
200     SETUP_VFILTER_OPTION( marqPositionCombo, currentIndexChanged( QString ) )
201
202     SETUP_VFILTER( logo )
203     SETUP_VFILTER_OPTION( logoFileText, editingFinished() )
204     SETUP_VFILTER_OPTION( logoYSpin, valueChanged( int ) )
205     SETUP_VFILTER_OPTION( logoXSpin, valueChanged( int ) )
206     SETUP_VFILTER_OPTION( logoTransparencySlider, valueChanged( int ) )
207
208 #undef SETUP_VFILTER
209 #undef SETUP_VFILTER_OPTION
210
211     CONNECT( ui.cropTopPx, valueChanged( int ), this, cropChange() );
212     CONNECT( ui.cropBotPx, valueChanged( int ), this, cropChange() );
213     CONNECT( ui.cropLeftPx, valueChanged( int ), this, cropChange() );
214     CONNECT( ui.cropRightPx, valueChanged( int ), this, cropChange() );
215     CONNECT( ui.leftRightCropSync, toggled ( bool ), this, cropChange() );
216     CONNECT( ui.topBotCropSync, toggled ( bool ), this, cropChange() );
217     CONNECT( ui.topBotCropSync, toggled( bool ),
218              ui.cropBotPx, setDisabled( bool ) );
219     CONNECT( ui.leftRightCropSync, toggled( bool ),
220              ui.cropRightPx, setDisabled( bool ) );
221 }
222
223 ExtVideo::~ExtVideo()
224 {
225 }
226
227 void ExtVideo::cropChange()
228 {
229     if( ui.topBotCropSync->isChecked() )
230         ui.cropBotPx->setValue( ui.cropTopPx->value() );
231     if( ui.leftRightCropSync->isChecked() )
232         ui.cropRightPx->setValue( ui.cropLeftPx->value() );
233
234     p_vout = THEMIM->getVout();
235     if( p_vout )
236     {
237         var_SetInteger( p_vout, "crop-top", ui.cropTopPx->value() );
238         var_SetInteger( p_vout, "crop-bottom", ui.cropBotPx->value() );
239         var_SetInteger( p_vout, "crop-left", ui.cropLeftPx->value() );
240         var_SetInteger( p_vout, "crop-right", ui.cropRightPx->value() );
241         vlc_object_release( p_vout );
242     }
243 }
244
245 void ExtVideo::clean()
246 {
247     ui.cropTopPx->setValue( 0 );
248     ui.cropBotPx->setValue( 0 );
249     ui.cropLeftPx->setValue( 0 );
250     ui.cropRightPx->setValue( 0 );
251 }
252
253 void ExtVideo::ChangeVFiltersString( const char *psz_name, bool b_add )
254 {
255     char *psz_parser, *psz_string;
256     const char *psz_filter_type;
257
258     /* FIXME temporary hack */
259     const char *psz_module_name = psz_name;
260     if( !strcmp( psz_name, "magnify" ) ||
261         !strcmp( psz_name, "puzzle" ) ||
262         !strcmp( psz_name, "logo" ) ||
263         !strcmp( psz_name, "clone" ) )
264         psz_module_name = "video_filter_wrapper";
265
266     module_t *p_obj = module_find( psz_module_name );
267     if( !p_obj )
268     {
269         msg_Err( p_intf, "Unable to find filter module \"%s\".", psz_name );
270         return;
271     }
272
273     if( module_provides( p_obj, "video filter" ) )
274     {
275         psz_filter_type = "vout-filter";
276     }
277     else if( module_provides( p_obj, "video filter2" ) )
278     {
279         psz_filter_type = "video-filter";
280     }
281     else if( module_provides( p_obj, "sub filter" ) )
282     {
283         psz_filter_type = "sub-filter";
284     }
285     else
286     {
287         module_release (p_obj);
288         msg_Err( p_intf, "Unknown video filter type." );
289         return;
290     }
291     module_release (p_obj);
292
293     psz_string = config_GetPsz( p_intf, psz_filter_type );
294
295     if( !psz_string ) psz_string = strdup( "" );
296
297     psz_parser = strstr( psz_string, psz_name );
298
299     if( b_add )
300     {
301         if( !psz_parser )
302         {
303             psz_parser = psz_string;
304             if( asprintf( &psz_string, ( *psz_string ) ? "%s:%s" : "%s%s",
305                             psz_string, psz_name ) == -1 )
306             {
307                 free( psz_parser );
308                 return;
309             }
310             free( psz_parser );
311         }
312         else
313         {
314             return;
315         }
316     }
317     else
318     {
319         if( psz_parser )
320         {
321             if( *( psz_parser + strlen( psz_name ) ) == ':' )
322             {
323                 memmove( psz_parser, psz_parser + strlen( psz_name ) + 1,
324                          strlen( psz_parser + strlen( psz_name ) + 1 ) + 1 );
325             }
326             else
327             {
328                 *psz_parser = '\0';
329             }
330
331             /* Remove trailing : : */
332             if( strlen( psz_string ) > 0 &&
333                 *( psz_string + strlen( psz_string ) -1 ) == ':' )
334             {
335                 *( psz_string + strlen( psz_string ) -1 ) = '\0';
336             }
337         }
338         else
339         {
340             free( psz_string );
341             return;
342         }
343     }
344     /* Vout is not kept, so put that in the config */
345     config_PutPsz( p_intf, psz_filter_type, psz_string );
346     if( !strcmp( psz_filter_type, "video-filter" ) )
347         ui.videoFilterText->setText( psz_string );
348     else if( !strcmp( psz_filter_type, "vout-filter" ) )
349         ui.voutFilterText->setText( psz_string );
350     else if( !strcmp( psz_filter_type, "sub-filter" ) )
351         ui.subpictureFilterText->setText( psz_string );
352
353     /* Try to set on the fly */
354     p_vout = THEMIM->getVout();
355     if( p_vout )
356     {
357         if( !strcmp( psz_filter_type, "sub-filter" ) )
358             var_SetString( vout_GetSpu( p_vout ), psz_filter_type, psz_string );
359         else
360             var_SetString( p_vout, psz_filter_type, psz_string );
361         vlc_object_release( p_vout );
362     }
363
364     free( psz_string );
365 }
366
367 void ExtVideo::updateFilters()
368 {
369     QString module = ModuleFromWidgetName( sender() );
370
371     QCheckBox *checkbox = qobject_cast<QCheckBox*>( sender() );
372     QGroupBox *groupbox = qobject_cast<QGroupBox*>( sender() );
373
374     ChangeVFiltersString( qtu( module ),
375                           checkbox ? checkbox->isChecked()
376                                    : groupbox->isChecked() );
377 }
378
379 void ExtVideo::initComboBoxItems( QObject *widget )
380 {
381     QComboBox *combobox = qobject_cast<QComboBox*>( widget );
382     if( !combobox ) return;
383     QString option = OptionFromWidgetName( widget );
384     module_config_t *p_item = config_FindConfig( VLC_OBJECT( p_intf ),
385                                                  qtu( option ) );
386     if( p_item )
387     {
388         int i_type = p_item->i_type & CONFIG_ITEM;
389         for( int i_index = 0; i_index < p_item->i_list; i_index++ )
390         {
391             if( i_type == CONFIG_ITEM_INTEGER
392              || i_type == CONFIG_ITEM_BOOL )
393                 combobox->addItem( qfu( p_item->ppsz_list_text[i_index] ),
394                                    p_item->pi_list[i_index] );
395             else if( i_type == CONFIG_ITEM_STRING )
396                 combobox->addItem( qfu( p_item->ppsz_list_text[i_index] ),
397                                    p_item->ppsz_list[i_index] );
398         }
399     }
400     else
401     {
402         msg_Err( p_intf, "Couldn't find option \"%s\".",
403                  qtu( option ) );
404     }
405 }
406
407 void ExtVideo::setWidgetValue( QObject *widget )
408 {
409     QString module = ModuleFromWidgetName( widget->parent() );
410     //std::cout << "Module name: " << module.toStdString() << std::endl;
411     QString option = OptionFromWidgetName( widget );
412     //std::cout << "Option name: " << option.toStdString() << std::endl;
413
414     vlc_object_t *p_obj = ( vlc_object_t * )
415         vlc_object_find_name( p_intf->p_libvlc,
416                               qtu( module ),
417                               FIND_CHILD );
418     int i_type;
419     vlc_value_t val;
420
421     if( !p_obj )
422     {
423 #if 0
424         msg_Dbg( p_intf,
425                  "Module instance %s not found, looking in config values.",
426                  qtu( module ) );
427 #endif
428         i_type = config_GetType( p_intf, qtu( option ) ) & 0xf0;
429         switch( i_type )
430         {
431             case VLC_VAR_INTEGER:
432             case VLC_VAR_BOOL:
433                 val.i_int = config_GetInt( p_intf, qtu( option ) );
434                 break;
435             case VLC_VAR_FLOAT:
436                 val.f_float = config_GetFloat( p_intf, qtu( option ) );
437                 break;
438             case VLC_VAR_STRING:
439                 val.psz_string = config_GetPsz( p_intf, qtu( option ) );
440                 break;
441         }
442     }
443     else
444     {
445         i_type = var_Type( p_obj, qtu( option ) ) & 0xf0;
446         var_Get( p_obj, qtu( option ), &val );
447         vlc_object_release( p_obj );
448     }
449
450     /* Try to cast to all the widgets we're likely to encounter. Only
451      * one of the casts is expected to work. */
452     QSlider        *slider        = qobject_cast<QSlider*>       ( widget );
453     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( widget );
454     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( widget );
455     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( widget );
456     QDial          *dial          = qobject_cast<QDial*>         ( widget );
457     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( widget );
458     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( widget );
459
460     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
461     {
462         if( slider )        slider->setValue( val.i_int );
463         else if( checkbox ) checkbox->setCheckState( val.i_int? Qt::Checked
464                                                               : Qt::Unchecked );
465         else if( spinbox )  spinbox->setValue( val.i_int );
466         else if( dial )     dial->setValue( ( 540-val.i_int )%360 );
467         else if( lineedit )
468         {
469             char str[30];
470             snprintf( str, sizeof(str), "%06X", val.i_int );
471             lineedit->setText( str );
472         }
473         else if( combobox ) combobox->setCurrentIndex(
474                             combobox->findData( val.i_int ) );
475         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
476     }
477     else if( i_type == VLC_VAR_FLOAT )
478     {
479         if( slider ) slider->setValue( ( int )( val.f_float*( double )slider->tickInterval() ) ); /* hack alert! */
480         else if( doublespinbox ) doublespinbox->setValue( val.f_float );
481         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
482     }
483     else if( i_type == VLC_VAR_STRING )
484     {
485         if( lineedit ) lineedit->setText( qfu( val.psz_string ) );
486         else if( combobox ) combobox->setCurrentIndex(
487                             combobox->findData( qfu( val.psz_string ) ) );
488         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
489         free( val.psz_string );
490     }
491     else
492         msg_Err( p_intf,
493                  "Module %s's %s variable is of an unsupported type ( %d )",
494                  qtu( module ),
495                  qtu( option ),
496                  i_type );
497 }
498
499 void ExtVideo::updateFilterOptions()
500 {
501     QString module = ModuleFromWidgetName( sender()->parent() );
502     //std::cout << "Module name: " << module.toStdString() << std::endl;
503     QString option = OptionFromWidgetName( sender() );
504     //std::cout << "Option name: " << option.toStdString() << std::endl;
505
506     vlc_object_t *p_obj = ( vlc_object_t * )
507         vlc_object_find_name( p_intf->p_libvlc,
508                               qtu( module ),
509                               FIND_CHILD );
510     int i_type;
511     bool b_is_command;
512     if( !p_obj )
513     {
514         msg_Warn( p_intf, "Module %s not found. You'll need to restart the filter to take the change into account.", qtu( module ) );
515         i_type = config_GetType( p_intf, qtu( option ) );
516         b_is_command = false;
517     }
518     else
519     {
520         i_type = var_Type( p_obj, qtu( option ) );
521         b_is_command = ( i_type & VLC_VAR_ISCOMMAND );
522     }
523
524     if( !b_is_command )
525     {
526         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.",
527                  qtu( module ),
528                  qtu( option ) );
529         /* FIXME: restart automatically somewhere near the end of this function */
530     }
531
532     /* Try to cast to all the widgets we're likely to encounter. Only
533      * one of the casts is expected to work. */
534     QSlider        *slider        = qobject_cast<QSlider*>       ( sender() );
535     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( sender() );
536     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( sender() );
537     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( sender() );
538     QDial          *dial          = qobject_cast<QDial*>         ( sender() );
539     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( sender() );
540     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( sender() );
541
542     i_type &= 0xf0;
543     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
544     {
545         int i_int = 0;
546         if( slider )        i_int = slider->value();
547         else if( checkbox ) i_int = checkbox->checkState() == Qt::Checked;
548         else if( spinbox )  i_int = spinbox->value();
549         else if( dial )     i_int = ( 540-dial->value() )%360;
550         else if( lineedit ) i_int = lineedit->text().toInt( NULL,16 );
551         else if( combobox ) i_int = combobox->itemData( combobox->currentIndex() ).toInt();
552         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
553         config_PutInt( p_intf, qtu( option ), i_int );
554         if( b_is_command )
555         {
556             if( i_type == VLC_VAR_INTEGER )
557                 var_SetInteger( p_obj, qtu( option ), i_int );
558             else
559                 var_SetBool( p_obj, qtu( option ), i_int );
560         }
561     }
562     else if( i_type == VLC_VAR_FLOAT )
563     {
564         double f_float = 0;
565         if( slider )             f_float = ( double )slider->value()
566                                          / ( double )slider->tickInterval(); /* hack alert! */
567         else if( doublespinbox ) f_float = doublespinbox->value();
568         else if( lineedit ) f_float = lineedit->text().toDouble();
569         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
570         config_PutFloat( p_intf, qtu( option ), f_float );
571         if( b_is_command )
572             var_SetFloat( p_obj, qtu( option ), f_float );
573     }
574     else if( i_type == VLC_VAR_STRING )
575     {
576         char *psz_string = NULL;
577         if( lineedit ) psz_string = strdup( qtu( lineedit->text() ) );
578         else if( combobox ) psz_string = strdup( qtu( combobox->itemData(
579                                        combobox->currentIndex() ).toString() ) );
580         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
581         config_PutPsz( p_intf, qtu( option ), psz_string );
582         if( b_is_command )
583             var_SetString( p_obj, qtu( option ), psz_string );
584         free( psz_string );
585     }
586     else
587         msg_Err( p_intf,
588                  "Module %s's %s variable is of an unsupported type ( %d )",
589                  qtu( module ),
590                  qtu( option ),
591                  i_type );
592
593     if( p_obj ) vlc_object_release( p_obj );
594 }
595
596 #if 0
597 void ExtVideo::gotoConf( QObject* src )
598 {
599 #define SHOWCONF( module ) \
600     if( src->objectName().contains( module ) ) \
601     { \
602         PrefsDialog::getInstance( p_intf )->showModulePrefs( module ); \
603         return; \
604     }
605     SHOWCONF( "clone" );
606     SHOWCONF( "magnify" );
607     SHOWCONF( "wave" );
608     SHOWCONF( "ripple" );
609     SHOWCONF( "invert" );
610     SHOWCONF( "puzzle" );
611     SHOWCONF( "wall" );
612     SHOWCONF( "gradient" );
613     SHOWCONF( "colorthres" )
614 }
615 #endif
616
617 /**********************************************************************
618  * v4l2 controls
619  **********************************************************************/
620
621 ExtV4l2::ExtV4l2( intf_thread_t *_p_intf, QWidget *_parent )
622     : QWidget( _parent ), p_intf( _p_intf )
623 {
624     ui.setupUi( this );
625
626     BUTTONACT( ui.refresh, Refresh() );
627
628     box = NULL;
629 }
630
631 ExtV4l2::~ExtV4l2()
632 {
633     delete box;
634 }
635
636 void ExtV4l2::showEvent( QShowEvent *event )
637 {
638     QWidget::showEvent( event );
639     Refresh();
640 }
641
642 void ExtV4l2::Refresh( void )
643 {
644     vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( p_intf, "v4l2", FIND_ANYWHERE );
645     ui.help->hide();
646     if( box )
647     {
648         ui.vboxLayout->removeWidget( box );
649         delete box;
650         box = NULL;
651     }
652     if( p_obj )
653     {
654         vlc_value_t val, text, name;
655         int i_ret = var_Change( p_obj, "controls", VLC_VAR_GETCHOICES,
656                                 &val, &text );
657         if( i_ret < 0 )
658         {
659             msg_Err( p_intf, "Oops, v4l2 object doesn't have a 'controls' variable." );
660             ui.help->show();
661             vlc_object_release( p_obj );
662             return;
663         }
664
665         box = new QGroupBox( this );
666         ui.vboxLayout->addWidget( box );
667         QVBoxLayout *layout = new QVBoxLayout( box );
668         box->setLayout( layout );
669
670         for( int i = 0; i < val.p_list->i_count; i++ )
671         {
672             const char *psz_var = text.p_list->p_values[i].psz_string;
673             var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &name, NULL );
674             const char *psz_label = name.psz_string;
675             msg_Dbg( p_intf, "v4l2 control \"%x\": %s (%s)",
676                      val.p_list->p_values[i].i_int, psz_var, name.psz_string );
677
678             int i_type = var_Type( p_obj, psz_var );
679             switch( i_type & VLC_VAR_TYPE )
680             {
681                 case VLC_VAR_INTEGER:
682                 {
683                     QLabel *label = new QLabel( psz_label, box );
684                     QHBoxLayout *hlayout = new QHBoxLayout();
685                     hlayout->addWidget( label );
686                     int i_val = var_GetInteger( p_obj, psz_var );
687                     if( i_type & VLC_VAR_HASCHOICE )
688                     {
689                         QComboBox *combobox = new QComboBox( box );
690                         combobox->setObjectName( psz_var );
691
692                         vlc_value_t val2, text2;
693                         var_Change( p_obj, psz_var, VLC_VAR_GETCHOICES,
694                                     &val2, &text2 );
695                         for( int j = 0; j < val2.p_list->i_count; j++ )
696                         {
697                             combobox->addItem(
698                                        text2.p_list->p_values[j].psz_string,
699                                        val2.p_list->p_values[j].i_int );
700                             if( i_val == val2.p_list->p_values[j].i_int )
701                                 combobox->setCurrentIndex( j );
702                         }
703                         var_FreeList( &val2, &text2 );
704
705                         CONNECT( combobox, currentIndexChanged( int ), this,
706                                  ValueChange( int ) );
707                         hlayout->addWidget( combobox );
708                     }
709                     else
710                     {
711                         QSlider *slider = new QSlider( box );
712                         slider->setObjectName( psz_var );
713                         slider->setOrientation( Qt::Horizontal );
714                         vlc_value_t val2;
715                         var_Change( p_obj, psz_var, VLC_VAR_GETMIN,
716                                     &val2, NULL );
717                         slider->setMinimum( val2.i_int );
718                         var_Change( p_obj, psz_var, VLC_VAR_GETMAX,
719                                     &val2, NULL );
720                         slider->setMaximum( val2.i_int );
721                         var_Change( p_obj, psz_var, VLC_VAR_GETSTEP,
722                                     &val2, NULL );
723                         slider->setSingleStep( val2.i_int );
724                         slider->setValue( i_val );
725
726                         CONNECT( slider, valueChanged( int ), this,
727                                  ValueChange( int ) );
728                         hlayout->addWidget( slider );
729                     }
730                     layout->addLayout( hlayout );
731                     break;
732                 }
733                 case VLC_VAR_BOOL:
734                 {
735                     QCheckBox *button = new QCheckBox( psz_label, box );
736                     button->setObjectName( psz_var );
737                     button->setChecked( var_GetBool( p_obj, psz_var ) );
738
739                     CONNECT( button, clicked( bool ), this,
740                              ValueChange( bool ) );
741                     layout->addWidget( button );
742                     break;
743                 }
744                 case VLC_VAR_VOID:
745                 {
746                     if( i_type & VLC_VAR_ISCOMMAND )
747                     {
748                         QPushButton *button = new QPushButton( psz_label, box );
749                         button->setObjectName( psz_var );
750
751                         CONNECT( button, clicked( bool ), this,
752                                  ValueChange( bool ) );
753                         layout->addWidget( button );
754                     }
755                     else
756                     {
757                         QLabel *label = new QLabel( psz_label, box );
758                         layout->addWidget( label );
759                     }
760                     break;
761                 }
762                 default:
763                     msg_Warn( p_intf, "Unhandled var type for %s", psz_var );
764                     break;
765             }
766             free( name.psz_string );
767         }
768         var_FreeList( &val, &text );
769         vlc_object_release( p_obj );
770     }
771     else
772     {
773         msg_Dbg( p_intf, "Couldn't find v4l2 instance" );
774         ui.help->show();
775     }
776 }
777
778 void ExtV4l2::ValueChange( bool value )
779 {
780     ValueChange( (int)value );
781 }
782
783 void ExtV4l2::ValueChange( int value )
784 {
785     QObject *s = sender();
786     vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( p_intf, "v4l2", FIND_ANYWHERE );
787     if( p_obj )
788     {
789         char *psz_var = strdup( qtu( s->objectName() ) );
790         int i_type = var_Type( p_obj, psz_var );
791         switch( i_type & VLC_VAR_TYPE )
792         {
793             case VLC_VAR_INTEGER:
794                 if( i_type & VLC_VAR_HASCHOICE )
795                 {
796                     QComboBox *combobox = qobject_cast<QComboBox*>( s );
797                     value = combobox->itemData( value ).toInt();
798                 }
799                 var_SetInteger( p_obj, psz_var, value );
800                 break;
801             case VLC_VAR_BOOL:
802                 var_SetBool( p_obj, psz_var, value );
803                 break;
804             case VLC_VAR_VOID:
805                 var_SetVoid( p_obj, psz_var );
806                 break;
807         }
808         free( psz_var );
809         vlc_object_release( p_obj );
810     }
811     else
812     {
813         msg_Warn( p_intf, "Oops, v4l2 object isn't available anymore" );
814         Refresh();
815     }
816 }
817
818 /**********************************************************************
819  * Equalizer
820  **********************************************************************/
821
822 static const QString band_frequencies[] =
823 {
824     "  60 Hz  ", " 170 Hz ", " 310 Hz ", " 600 Hz ", "  1 kHz ",
825     "  3 kHz  ", "  6 kHz ", " 12 kHz ", " 14 kHz ", " 16 kHz "
826 };
827
828 Equalizer::Equalizer( intf_thread_t *_p_intf, QWidget *_parent ) :
829                             QWidget( _parent ) , p_intf( _p_intf )
830 {
831     QFont smallFont = QApplication::font( static_cast<QWidget*>( 0 ) );
832     smallFont.setPointSize( smallFont.pointSize() - 3 );
833
834     ui.setupUi( this );
835     ui.preampLabel->setFont( smallFont );
836
837     /* Setup of presetsComboBox */
838     presetsComboBox = ui.presetsCombo;
839     CONNECT( presetsComboBox, currentIndexChanged( int ),
840              this, updateUISliderValues( int ) );
841     CONNECT( presetsComboBox, activated( int ), this, setCorePreset( int ) );
842
843     /* Add the sliders for the Bands */
844     QGridLayout *grid = new QGridLayout( ui.frame );
845     grid->setMargin( 0 );
846     for( int i = 0 ; i < BANDS ; i++ )
847     {
848         bands[i] = new QSlider( Qt::Vertical );
849         bands[i]->setMaximum( 400 );
850         bands[i]->setValue( 200 );
851         CONNECT( bands[i], valueChanged( int ), this, setCoreBands() );
852
853         band_texts[i] = new QLabel( band_frequencies[i] + "\n0.0dB" );
854         band_texts[i]->setFont( smallFont );
855
856         grid->addWidget( bands[i], 0, i );
857         grid->addWidget( band_texts[i], 1, i );
858     }
859
860     /* Add the listed presets */
861     for( int i = 0 ; i < NB_PRESETS ; i ++ )
862     {
863         presetsComboBox->addItem( qtr( preset_list_text[i] ),
864                                   QVariant( preset_list[i] ) );
865     }
866
867     /* Connects */
868     BUTTONACT( ui.enableCheck, enable() );
869     BUTTONACT( ui.eq2PassCheck, set2Pass() );
870     CONNECT( ui.preampSlider, valueChanged( int ), this, setPreamp() );
871
872     /* Do the update from the value of the core */
873     updateUIFromCore();
874 }
875
876 Equalizer::~Equalizer()
877 {
878 }
879
880 void Equalizer::clean()
881 {
882     enable();
883 }
884 /* Write down initial values */
885 void Equalizer::updateUIFromCore()
886 {
887     char *psz_af, *psz_pres;
888     float f_preamp;
889     int i_preset;
890
891     aout_instance_t *p_aout = THEMIM->getAout();
892     if( p_aout )
893     {
894         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
895         psz_pres = var_GetString( p_aout, "equalizer-preset" );
896         if( var_GetBool( p_aout, "equalizer-2pass" ) )
897             ui.eq2PassCheck->setChecked( true );
898         f_preamp = var_GetFloat( p_aout, "equalizer-preamp" );
899         i_preset = presetsComboBox->findData( QVariant( psz_pres ) );
900         vlc_object_release( p_aout );
901     }
902     else
903     {
904         psz_af = config_GetPsz( p_intf, "audio-filter" );
905         psz_pres = config_GetPsz( p_intf, "equalizer-preset" );
906         if( config_GetInt( p_intf, "equalizer-2pass" ) )
907             ui.eq2PassCheck->setChecked( true );
908         f_preamp = config_GetFloat( p_intf, "equalizer-preamp" );
909         i_preset = presetsComboBox->findData( QVariant( psz_pres ) );
910     }
911     if( psz_af && strstr( psz_af, "equalizer" ) != NULL )
912         ui.enableCheck->setChecked( true );
913     enable( ui.enableCheck->isChecked() );
914
915     presetsComboBox->setCurrentIndex( i_preset );
916
917     free( psz_af );
918     free( psz_pres );
919 }
920
921 /* Functin called when enableButton is toggled */
922 void Equalizer::enable()
923 {
924     bool en = ui.enableCheck->isChecked();
925     aout_EnableFilter( VLC_OBJECT( p_intf ), "equalizer",
926                        en ? true : false );
927 //    aout_EnableFilter( VLC_OBJECT( p_intf ), "upmixer",
928 //                       en ? true : false );
929 //     aout_EnableFilter( VLC_OBJECT( p_intf ), "vsurround",
930 //                       en ? true : false );
931     enable( en );
932
933     if( presetsComboBox->currentIndex() < 0 )
934         presetsComboBox->setCurrentIndex( 0 );
935
936 }
937
938 void Equalizer::enable( bool en )
939 {
940     ui.eq2PassCheck->setEnabled( en );
941     presetsComboBox->setEnabled( en );
942     ui.presetLabel->setEnabled( en );
943     ui.preampLabel->setEnabled( en );
944     ui.preampSlider->setEnabled( en  );
945     for( int i = 0 ; i< BANDS; i++ )
946     {
947         bands[i]->setEnabled( en ); band_texts[i]->setEnabled( en );
948     }
949 }
950
951 /* Function called when the set2Pass button is activated */
952 void Equalizer::set2Pass()
953 {
954     aout_instance_t *p_aout= THEMIM->getAout();
955     bool b_2p = ui.eq2PassCheck->isChecked();
956
957     if( p_aout == NULL )
958         config_PutInt( p_intf, "equalizer-2pass", b_2p );
959     else
960     {
961         var_SetBool( p_aout, "equalizer-2pass", b_2p );
962         config_PutInt( p_intf, "equalizer-2pass", b_2p );
963         for( int i = 0; i < p_aout->i_nb_inputs; i++ )
964         {
965             p_aout->pp_inputs[i]->b_restart = true;
966         }
967         vlc_object_release( p_aout );
968     }
969 }
970
971 /* Function called when the preamp slider is moved */
972 void Equalizer::setPreamp()
973 {
974     const float f = ( float )(  ui.preampSlider->value() ) /10 - 20;
975     aout_instance_t *p_aout = THEMIM->getAout();
976
977     ui.preampLabel->setText( qtr( "Preamp\n" ) + QString::number( f, 'f', 1 )
978                                                + qtr( "dB" ) );
979     if( p_aout )
980     {
981         //delCallbacks( p_aout );
982         var_SetFloat( p_aout, "equalizer-preamp", f );
983         //addCallbacks( p_aout );
984         vlc_object_release( p_aout );
985     }
986     config_PutFloat( p_intf, "equalizer-preamp", f );
987 }
988
989 void Equalizer::setCoreBands()
990 {
991     /**\todo smoothing */
992
993     QString values;
994     for( int i = 0; i < BANDS; i++ )
995     {
996         const float f_val = (float)( bands[i]->value() ) / 10 - 20;
997         QString val = QString("%1").arg( f_val, 5, 'f', 1 );
998
999         band_texts[i]->setText( band_frequencies[i] + "\n" + val + "dB" );
1000         values += " " + val;
1001     }
1002     const char *psz_values = values.toAscii().constData();
1003
1004     aout_instance_t *p_aout = THEMIM->getAout();
1005     if( p_aout )
1006     {
1007         //delCallbacks( p_aout );
1008         var_SetString( p_aout, "equalizer-bands", psz_values );
1009         //addCallbacks( p_aout );
1010         vlc_object_release( p_aout );
1011     }
1012 }
1013
1014 void Equalizer::updateUISliderValues( int i_preset )
1015 {
1016     if( i_preset < 0 ) return;
1017
1018     char *p = createValuesFromPreset( i_preset );
1019     char *psz = p;
1020     float f_preamp = eqz_preset_10b[i_preset]->f_preamp;
1021
1022     if ( p )
1023     {
1024         for( int i = 0; i < BANDS; i++ )
1025         {
1026             const float f = us_strtod(p, &p );
1027
1028             bands[i]->setValue( (int)( ( f + 20 ) * 10 )  );
1029
1030             band_texts[i]->setText( band_frequencies[i] + "\n"
1031                                   + QString("%1").arg( f, 5, 'f', 1 ) + "dB" );
1032             if( p == NULL || *p == '\0' )
1033                 break;
1034             p++;
1035             if( *p == '\0' )
1036                 break;
1037         }
1038         free( psz );
1039     }
1040     ui.preampSlider->setValue( (int)( ( f_preamp + 20 ) * 10 ) );
1041     ui.preampLabel->setText( qtr( "Preamp\n" )
1042                    + QString::number( f_preamp, 'f', 1 ) + qtr( "dB" ) );
1043 }
1044
1045 char * Equalizer::createValuesFromPreset( int i_preset )
1046 {
1047     char *psz_values;
1048     QString values;
1049
1050     /* Create the QString in Qt */
1051     for( int i = 0 ; i< BANDS ;i++ )
1052         values += QString( " %1" ).arg( eqz_preset_10b[i_preset]->f_amp[i] );
1053
1054     /* Convert it to char * */
1055     if( !asprintf( &psz_values, "%s", values.toAscii().constData() ) )
1056         return NULL;
1057
1058     return psz_values;
1059 }
1060
1061 void Equalizer::setCorePreset( int i_preset )
1062 {
1063     char *psz_values = createValuesFromPreset( i_preset );
1064     if( !psz_values ) return ;
1065
1066     aout_instance_t *p_aout= THEMIM->getAout();
1067     if( p_aout )
1068     {
1069         delCallbacks( p_aout );
1070         var_SetString( p_aout , "equalizer-preset" , preset_list[i_preset] );
1071
1072         var_SetString( p_aout, "equalizer-bands", psz_values );
1073         var_SetFloat( p_aout, "equalizer-preamp",
1074                       eqz_preset_10b[i_preset]->f_preamp );
1075         addCallbacks( p_aout );
1076         vlc_object_release( p_aout );
1077     }
1078     config_PutPsz( p_intf, "equalizer-bands", psz_values );
1079     config_PutPsz( p_intf, "equalizer-preset", preset_list[i_preset] );
1080     config_PutFloat( p_intf, "equalizer-preamp",
1081                     eqz_preset_10b[i_preset]->f_preamp );
1082     free( psz_values );
1083 }
1084
1085 static int PresetCallback( vlc_object_t *p_this, char const *psz_cmd,
1086                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1087 {
1088     char *psz_preset = newval.psz_string;
1089     Equalizer *eq = ( Equalizer * )p_data;
1090     int i_preset = eq->presetsComboBox->findData( QVariant( psz_preset ) );
1091     eq->presetsComboBox->setCurrentIndex( i_preset );
1092     return VLC_SUCCESS;
1093 }
1094
1095 void Equalizer::delCallbacks( aout_instance_t *p_aout )
1096 {
1097     //var_DelCallback( p_aout, "equalizer-bands", EqzCallback, this );
1098     //var_DelCallback( p_aout, "equalizer-preamp", EqzCallback, this );
1099     var_DelCallback( p_aout, "equalizer-preset", PresetCallback, this );
1100 }
1101
1102 void Equalizer::addCallbacks( aout_instance_t *p_aout )
1103 {
1104     //var_AddCallback( p_aout, "equalizer-bands", EqzCallback, this );
1105     //var_AddCallback( p_aout, "equalizer-preamp", EqzCallback, this );
1106     var_AddCallback( p_aout, "equalizer-preset", PresetCallback, this );
1107 }
1108
1109 /**********************************************************************
1110  * Audio filters
1111  **********************************************************************/
1112
1113 /**********************************************************************
1114  * Spatializer
1115  **********************************************************************/
1116 static const char *psz_control_names[] =
1117 {
1118     "spatializer-roomsize", "spatializer-width",
1119     "spatializer-wet", "spatializer-dry", "spatializer-damp"
1120 };
1121
1122 Spatializer::Spatializer( intf_thread_t *_p_intf, QWidget *_parent ) :
1123     QWidget( _parent ) , p_intf( _p_intf )
1124 {
1125     QFont smallFont = QApplication::font( static_cast<QWidget*>( 0 ) );
1126     smallFont.setPointSize( smallFont.pointSize() - 3 );
1127
1128     QGridLayout *layout = new QGridLayout( this );
1129     layout->setMargin( 0 );
1130
1131     enableCheck = new QCheckBox( qtr( "Enable spatializer" ) );
1132     layout->addWidget( enableCheck, 0, 0, 1, NUM_SP_CTRL );
1133
1134     for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1135     {
1136         spatCtrl[i] = new QSlider( Qt::Vertical );
1137         if( i < 2 )
1138         {
1139             spatCtrl[i]->setMaximum( 10 );
1140             spatCtrl[i]->setValue( 2 );
1141         }
1142         else
1143         {
1144             spatCtrl[i]->setMaximum( 10 );
1145             spatCtrl[i]->setValue( 0 );
1146             spatCtrl[i]->setMinimum( -10 );
1147         }
1148         oldControlVars[i] = spatCtrl[i]->value();
1149         CONNECT( spatCtrl[i], valueChanged( int ), this, setInitValues() );
1150         ctrl_texts[i] = new QLabel( qfu( psz_control_names[i] ) + "\n" );
1151         ctrl_texts[i]->setFont( smallFont );
1152         ctrl_readout[i] = new QLabel( "" );
1153         ctrl_readout[i]->setFont( smallFont );
1154         layout->addWidget( spatCtrl[i], 1, i );
1155         layout->addWidget( ctrl_readout[i], 2, i );
1156         layout->addWidget( ctrl_texts[i], 3, i );
1157     }
1158
1159     BUTTONACT( enableCheck, enable() );
1160
1161     /* Write down initial values */
1162     aout_instance_t *p_aout = THEMIM->getAout();
1163     char *psz_af;
1164
1165     if( p_aout )
1166     {
1167         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
1168         for( int i = 0; i < NUM_SP_CTRL ; i++ )
1169         {
1170             controlVars[i] = var_GetFloat( p_aout, psz_control_names[i] );
1171         }
1172         vlc_object_release( p_aout );
1173     }
1174     else
1175     {
1176         psz_af = config_GetPsz( p_intf, "audio-filter" );
1177         for( int i = 0; i < NUM_SP_CTRL ; i++ )
1178         {
1179             controlVars[i] = config_GetFloat( p_intf, psz_control_names[i] );
1180         }
1181     }
1182     if( psz_af && strstr( psz_af, "spatializer" ) != NULL )
1183         enableCheck->setChecked( true );
1184     free( psz_af );
1185     enable( enableCheck->isChecked() );
1186     setValues( controlVars );
1187 }
1188
1189 Spatializer::~Spatializer()
1190 {
1191 }
1192
1193 void Spatializer::enable()
1194 {
1195     bool en = enableCheck->isChecked();
1196     aout_EnableFilter( VLC_OBJECT( p_intf ), "spatializer",
1197             en ? true : false );
1198     enable( en );
1199 }
1200
1201 void Spatializer::enable( bool en )
1202 {
1203     for( int i = 0 ; i< NUM_SP_CTRL; i++ )
1204     {
1205         spatCtrl[i]->setEnabled( en );
1206         ctrl_texts[i]->setEnabled( en );
1207         ctrl_readout[i]->setEnabled( en );
1208     }
1209 }
1210 void Spatializer::setInitValues()
1211 {
1212     setValues( controlVars );
1213 }
1214
1215 void Spatializer::setValues( float *controlVars )
1216 {
1217     aout_instance_t *p_aout = THEMIM->getAout();
1218
1219     for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1220     {
1221         float f = (float)(  spatCtrl[i]->value() );
1222         ctrl_readout[i]->setText( QString::number( f, 'f',  1 ) );
1223     }
1224     if( p_aout )
1225     {
1226         for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1227         {
1228             if( oldControlVars[i] != spatCtrl[i]->value() )
1229             {
1230                 var_SetFloat( p_aout, psz_control_names[i],
1231                         ( float )spatCtrl[i]->value() );
1232                 config_PutFloat( p_intf, psz_control_names[i],
1233                         ( float ) spatCtrl[i]->value() );
1234                 oldControlVars[i] = ( float ) spatCtrl[i]->value();
1235             }
1236         }
1237         vlc_object_release( p_aout );
1238     }
1239
1240 }
1241 void Spatializer::delCallbacks( aout_instance_t *p_aout )
1242 {
1243     //    var_DelCallback( p_aout, "Spatializer-bands", EqzCallback, this );
1244     //    var_DelCallback( p_aout, "Spatializer-preamp", EqzCallback, this );
1245 }
1246
1247 void Spatializer::addCallbacks( aout_instance_t *p_aout )
1248 {
1249     //    var_AddCallback( p_aout, "Spatializer-bands", EqzCallback, this );
1250     //    var_AddCallback( p_aout, "Spatializer-preamp", EqzCallback, this );
1251 }
1252
1253 #include <QToolButton>
1254 #include <QGridLayout>
1255
1256 SyncControls::SyncControls( intf_thread_t *_p_intf, QWidget *_parent ) :
1257                             QWidget( _parent ) , p_intf( _p_intf )
1258 {
1259     QGroupBox *AVBox, *subsBox;
1260
1261     QToolButton *moinsAV, *plusAV;
1262     QToolButton *moinssubs, *plussubs;
1263     QToolButton *moinssubSpeed, *plussubSpeed;
1264
1265     QToolButton *updateButton;
1266
1267     b_userAction = true;
1268
1269     QGridLayout *mainLayout = new QGridLayout( this );
1270
1271     /* AV sync */
1272     AVBox = new QGroupBox( qtr( "Audio/Video" ) );
1273     QGridLayout *AVLayout = new QGridLayout( AVBox );
1274
1275     moinsAV = new QToolButton;
1276     moinsAV->setToolButtonStyle( Qt::ToolButtonTextOnly );
1277     moinsAV->setAutoRaise( true );
1278     moinsAV->setText( "-" );
1279     AVLayout->addWidget( moinsAV, 0, 1, 1, 1 );
1280
1281     plusAV = new QToolButton;
1282     plusAV->setToolButtonStyle( Qt::ToolButtonTextOnly );
1283     plusAV->setAutoRaise( true );
1284     plusAV->setText( "+" );
1285     AVLayout->addWidget( plusAV, 0, 3, 1, 1 );
1286
1287     QLabel *AVLabel = new QLabel;
1288     AVLabel->setText( qtr( "Advance of audio over video:" ) );
1289     AVLayout->addWidget( AVLabel, 0, 0, 1, 1 );
1290
1291     AVSpin = new QDoubleSpinBox;
1292     AVSpin->setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter );
1293     AVSpin->setDecimals( 3 );
1294     AVSpin->setMinimum( -100.0 );
1295     AVSpin->setMaximum( 100.0 );
1296     AVSpin->setSingleStep( 0.1 );
1297     AVSpin->setToolTip( qtr( "A positive value means that\n"
1298                              "the audio is ahead of the video" ) );
1299     AVSpin->setSuffix( " s" );
1300     AVLayout->addWidget( AVSpin, 0, 2, 1, 1 );
1301     mainLayout->addWidget( AVBox, 1, 0, 1, 5 );
1302
1303
1304     /* Subs */
1305     subsBox = new QGroupBox( qtr( "Subtitles/Video" ) );
1306     QGridLayout *subsLayout = new QGridLayout( subsBox );
1307
1308     moinssubs = new QToolButton;
1309     moinssubs->setToolButtonStyle( Qt::ToolButtonTextOnly );
1310     moinssubs->setAutoRaise( true );
1311     moinssubs->setText( "-" );
1312     subsLayout->addWidget( moinssubs, 0, 1, 1, 1 );
1313
1314     plussubs = new QToolButton;
1315     plussubs->setToolButtonStyle( Qt::ToolButtonTextOnly );
1316     plussubs->setAutoRaise( true );
1317     plussubs->setText( "+" );
1318     subsLayout->addWidget( plussubs, 0, 3, 1, 1 );
1319
1320     QLabel *subsLabel = new QLabel;
1321     subsLabel->setText( qtr( "Advance of subtitles over video:" ) );
1322     subsLayout->addWidget( subsLabel, 0, 0, 1, 1 );
1323
1324     subsSpin = new QDoubleSpinBox;
1325     subsSpin->setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter );
1326     subsSpin->setDecimals( 3 );
1327     subsSpin->setMinimum( -100.0 );
1328     subsSpin->setMaximum( 100.0 );
1329     subsSpin->setSingleStep( 0.1 );
1330     subsSpin->setToolTip( qtr( "A positive value means that\n"
1331                              "the subtitles are ahead of the video" ) );
1332     subsSpin->setSuffix( " s" );
1333     subsLayout->addWidget( subsSpin, 0, 2, 1, 1 );
1334
1335
1336     moinssubSpeed = new QToolButton;
1337     moinssubSpeed->setToolButtonStyle( Qt::ToolButtonTextOnly );
1338     moinssubSpeed->setAutoRaise( true );
1339     moinssubSpeed->setText( "-" );
1340     subsLayout->addWidget( moinssubSpeed, 1, 1, 1, 1 );
1341
1342     plussubSpeed = new QToolButton;
1343     plussubSpeed->setToolButtonStyle( Qt::ToolButtonTextOnly );
1344     plussubSpeed->setAutoRaise( true );
1345     plussubSpeed->setText( "+" );
1346     subsLayout->addWidget( plussubSpeed, 1, 3, 1, 1 );
1347
1348     QLabel *subSpeedLabel = new QLabel;
1349     subSpeedLabel->setText( qtr( "Speed of the subtitles:" ) );
1350     subsLayout->addWidget( subSpeedLabel, 1, 0, 1, 1 );
1351
1352     subSpeedSpin = new QDoubleSpinBox;
1353     subSpeedSpin->setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter );
1354     subSpeedSpin->setDecimals( 3 );
1355     subSpeedSpin->setMinimum( 1 );
1356     subSpeedSpin->setMaximum( 100 );
1357     subSpeedSpin->setSingleStep( 0.2 );
1358     subSpeedSpin->setSuffix( " fps" );
1359     subsLayout->addWidget( subSpeedSpin, 1, 2, 1, 1 );
1360
1361     mainLayout->addWidget( subsBox, 2, 0, 2, 5 );
1362
1363     updateButton = new QToolButton;
1364     updateButton->setAutoRaise( true );
1365     mainLayout->addWidget( updateButton, 0, 4, 1, 1 );
1366
1367
1368     /* Various Connects */
1369     CONNECT( moinsAV, clicked(), AVSpin, stepDown () );
1370     CONNECT( plusAV, clicked(), AVSpin, stepUp () );
1371     CONNECT( moinssubs, clicked(), subsSpin, stepDown () );
1372     CONNECT( plussubs, clicked(), subsSpin, stepUp () );
1373     CONNECT( moinssubSpeed, clicked(), subSpeedSpin, stepDown () );
1374     CONNECT( plussubSpeed, clicked(), subSpeedSpin, stepUp () );
1375     CONNECT( AVSpin, valueChanged ( double ), this, advanceAudio( double ) ) ;
1376     CONNECT( subsSpin, valueChanged ( double ), this, advanceSubs( double ) ) ;
1377     CONNECT( subSpeedSpin, valueChanged ( double ),
1378              this, adjustSubsSpeed( double ) );
1379
1380     CONNECT( THEMIM->getIM(), synchroChanged(), this, update() );
1381     BUTTON_SET_ACT_I( updateButton, "", update,
1382             qtr( "Force update of this dialog's values" ), update() );
1383
1384     /* Set it */
1385     update();
1386 }
1387
1388 void SyncControls::clean()
1389 {
1390     b_userAction = false;
1391     AVSpin->setValue( 0.0 );
1392     subsSpin->setValue( 0.0 );
1393     subSpeedSpin->setValue( 1.0 );
1394     b_userAction = true;
1395 }
1396
1397 void SyncControls::update()
1398 {
1399     b_userAction = false;
1400
1401     int64_t i_delay;
1402     if( THEMIM->getInput() )
1403     {
1404         i_delay = var_GetTime( THEMIM->getInput(), "audio-delay" );
1405         AVSpin->setValue( ( (double)i_delay ) / 1000000 );
1406         i_delay = var_GetTime( THEMIM->getInput(), "spu-delay" );
1407         subsSpin->setValue( ( (double)i_delay ) / 1000000 );
1408         subSpeedSpin->setValue( var_GetFloat( THEMIM->getInput(), "sub-fps" ) );
1409     }
1410     b_userAction = true;
1411 }
1412
1413 void SyncControls::advanceAudio( double f_advance )
1414 {
1415     if( THEMIM->getInput() && b_userAction )
1416     {
1417         int64_t i_delay = f_advance * 1000000;
1418         var_SetTime( THEMIM->getInput(), "audio-delay", i_delay );
1419     }
1420 }
1421
1422 void SyncControls::advanceSubs( double f_advance )
1423 {
1424     if( THEMIM->getInput() && b_userAction )
1425     {
1426         int64_t i_delay = f_advance * 1000000;
1427         var_SetTime( THEMIM->getInput(), "spu-delay", i_delay );
1428     }
1429 }
1430
1431 void SyncControls::adjustSubsSpeed( double f_fps )
1432 {
1433     if( THEMIM->getInput() && b_userAction )
1434     {
1435         var_SetFloat( THEMIM->getInput(), "sub-fps", f_fps );
1436     }
1437 }
1438
1439 /**********************************************************************
1440  * Video filters / Adjust
1441  **********************************************************************/
1442
1443 /**********************************************************************
1444  * Extended playbak controls
1445  **********************************************************************/