]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/extended_panels.cpp
Add special case for category labels.
[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 #include <QComboBox>
32
33 #include "components/extended_panels.hpp"
34 #include "dialogs/preferences.hpp"
35 #include "dialogs_provider.hpp"
36 #include "qt4.hpp"
37
38 #include "../../audio_filter/equalizer_presets.h"
39 #include <vlc_aout.h>
40 #include <vlc_intf_strings.h>
41 #include <vlc_vout.h>
42 #include <vlc_osd.h>
43
44 #include <iostream>
45 #include <string.h>
46
47 #if 0
48 class ConfClickHandler : public QObject
49 {
50 public:
51     ConfClickHandler( intf_thread_t *_p_intf, ExtVideo *_e ) : QObject ( _e ) {
52         e = _e; p_intf = _p_intf;
53     }
54     virtual ~ConfClickHandler() {}
55     bool eventFilter( QObject *obj, QEvent *evt )
56     {
57         if( evt->type() == QEvent::MouseButtonPress )
58         {
59             e->gotoConf( obj );
60             return true;
61         }
62         return false;
63     }
64 private:
65     ExtVideo* e;
66     intf_thread_t *p_intf;
67 };
68 #endif
69
70 QString ModuleFromWidgetName( QObject *obj )
71 {
72     return obj->objectName().replace( "Enable","" );
73 }
74
75 QString OptionFromWidgetName( QObject *obj )
76 {
77     /* Gruik ? ... nah */
78     QString option = obj->objectName().replace( "Slider", "" )
79                                       .replace( "Combo" , "" )
80                                       .replace( "Dial"  , "" )
81                                       .replace( "Check" , "" )
82                                       .replace( "Spin"  , "" )
83                                       .replace( "Text"  , "" );
84     for( char a = 'A'; a <= 'Z'; a++ )
85     {
86         option = option.replace( QString( a ),
87                                  QString( '-' ) + QString( a + 'a' - 'A' ) );
88     }
89     return option;
90 }
91
92 ExtVideo::ExtVideo( intf_thread_t *_p_intf, QWidget *_parent ) :
93                            QWidget( _parent ) , p_intf( _p_intf )
94 {
95     ui.setupUi( this );
96
97 #define SETUP_VFILTER( widget ) \
98     { \
99         vlc_object_t *p_obj = ( vlc_object_t * ) \
100             vlc_object_find_name( p_intf->p_libvlc, \
101                                   #widget, \
102                                   FIND_CHILD ); \
103         QCheckBox *checkbox = qobject_cast<QCheckBox*>( ui.widget##Enable ); \
104         QGroupBox *groupbox = qobject_cast<QGroupBox*>( ui.widget##Enable ); \
105         if( p_obj ) \
106         { \
107             vlc_object_release( p_obj ); \
108             if( checkbox ) checkbox->setChecked( true ); \
109             else groupbox->setChecked( true ); \
110         } \
111         else \
112         { \
113             if( checkbox ) checkbox->setChecked( false ); \
114             else groupbox->setChecked( false ); \
115         } \
116     } \
117     CONNECT( ui.widget##Enable, clicked(), this, updateFilters() );
118 #define SETUP_VFILTER_OPTION( widget, signal ) \
119     initComboBoxItems( ui.widget ); \
120     setWidgetValue( ui.widget ); \
121     CONNECT( ui.widget, signal, this, updateFilterOptions() );
122
123     SETUP_VFILTER( adjust )
124     SETUP_VFILTER_OPTION( hueSlider, valueChanged( int ) )
125     SETUP_VFILTER_OPTION( contrastSlider, valueChanged( int ) )
126     SETUP_VFILTER_OPTION( brightnessSlider, valueChanged( int ) )
127     SETUP_VFILTER_OPTION( saturationSlider, valueChanged( int ) )
128     SETUP_VFILTER_OPTION( gammaSlider, valueChanged( int ) )
129     SETUP_VFILTER_OPTION( brightnessThresholdCheck, stateChanged( int ) )
130
131     SETUP_VFILTER( extract )
132     SETUP_VFILTER_OPTION( extractComponentText, textChanged( QString ) )
133
134     SETUP_VFILTER( colorthres )
135     SETUP_VFILTER_OPTION( colorthresColorText, textChanged( QString ) )
136     SETUP_VFILTER_OPTION( colorthresSaturationthresSlider, valueChanged( int ) )
137     SETUP_VFILTER_OPTION( colorthresSimilaritythresSlider, valueChanged( int ) )
138
139     SETUP_VFILTER( invert )
140
141     SETUP_VFILTER( gradient )
142     SETUP_VFILTER_OPTION( gradientModeCombo, currentIndexChanged( QString ) )
143     SETUP_VFILTER_OPTION( gradientTypeCheck, stateChanged( int ) )
144     SETUP_VFILTER_OPTION( gradientCartoonCheck, stateChanged( int ) )
145
146     SETUP_VFILTER( motionblur )
147     SETUP_VFILTER_OPTION( blurFactorSlider, valueChanged( int ) )
148
149     SETUP_VFILTER( motiondetect )
150
151     SETUP_VFILTER( noise )
152
153     SETUP_VFILTER( psychedelic )
154
155     SETUP_VFILTER( sharpen )
156     SETUP_VFILTER_OPTION( sharpenSigmaSlider, valueChanged( int ) )
157
158     SETUP_VFILTER( ripple )
159
160     SETUP_VFILTER( wave )
161
162     SETUP_VFILTER( transform )
163     SETUP_VFILTER_OPTION( transformTypeCombo, currentIndexChanged( QString ) )
164
165     SETUP_VFILTER( rotate )
166     SETUP_VFILTER_OPTION( rotateAngleDial, valueChanged( int ) )
167     ui.rotateAngleDial->setWrapping( true );
168     ui.rotateAngleDial->setNotchesVisible( true );
169
170     SETUP_VFILTER( puzzle )
171     SETUP_VFILTER_OPTION( puzzleRowsSpin, valueChanged( int ) )
172     SETUP_VFILTER_OPTION( puzzleColsSpin, valueChanged( int ) )
173     SETUP_VFILTER_OPTION( puzzleBlackSlotCheck, stateChanged( int ) )
174
175     SETUP_VFILTER( magnify )
176
177     SETUP_VFILTER( clone )
178     SETUP_VFILTER_OPTION( cloneCountSpin, valueChanged( int ) )
179
180     SETUP_VFILTER( wall )
181     SETUP_VFILTER_OPTION( wallRowsSpin, valueChanged( int ) )
182     SETUP_VFILTER_OPTION( wallColsSpin, valueChanged( int ) )
183
184     SETUP_VFILTER( erase )
185     SETUP_VFILTER_OPTION( eraseMaskText, editingFinished() )
186     SETUP_VFILTER_OPTION( eraseYSpin, valueChanged( int ) )
187     SETUP_VFILTER_OPTION( eraseXSpin, valueChanged( int ) )
188
189     SETUP_VFILTER( marq )
190     SETUP_VFILTER_OPTION( marqMarqueeText, textChanged( QString ) )
191     SETUP_VFILTER_OPTION( marqPositionCombo, currentIndexChanged( QString ) )
192
193     SETUP_VFILTER( logo )
194     SETUP_VFILTER_OPTION( logoFileText, editingFinished() )
195     SETUP_VFILTER_OPTION( logoYSpin, valueChanged( int ) )
196     SETUP_VFILTER_OPTION( logoXSpin, valueChanged( int ) )
197     SETUP_VFILTER_OPTION( logoTransparencySlider, valueChanged( int ) )
198
199 #undef SETUP_VFILTER
200 #undef SETUP_VFILTER_OPTION
201 }
202
203 ExtVideo::~ExtVideo()
204 {
205 }
206
207 void ExtVideo::ChangeVFiltersString( char *psz_name, vlc_bool_t b_add )
208 {
209     vout_thread_t *p_vout;
210     char *psz_parser, *psz_string;
211     const char *psz_filter_type;
212
213     /* Please leave p_libvlc_global. This is where cached modules are
214      * stored. We're not trying to find a module instance. */
215     module_t *p_obj = module_Find( p_intf, psz_name );
216     if( !p_obj )
217     {
218         msg_Err( p_intf, "Unable to find filter module \"%s\n.", psz_name );
219         return;
220     }
221
222     if( module_IsCapable( p_obj, "video filter2" ) )
223     {
224         psz_filter_type = "video-filter";
225     }
226     else if( module_IsCapable( p_obj, "video filter" ) )
227     {
228         psz_filter_type = "vout-filter";
229     }
230     else if( module_IsCapable( p_obj, "sub filter" ) )
231     {
232         psz_filter_type = "sub-filter";
233     }
234     else
235     {
236         module_Put( p_obj );
237         msg_Err( p_intf, "Unknown video filter type." );
238         return;
239     }
240     module_Put( p_obj );
241
242     psz_string = config_GetPsz( p_intf, psz_filter_type );
243
244     if( !psz_string ) psz_string = strdup( "" );
245
246     psz_parser = strstr( psz_string, psz_name );
247
248     if( b_add )
249     {
250         if( !psz_parser )
251         {
252             psz_parser = psz_string;
253             asprintf( &psz_string, ( *psz_string ) ? "%s:%s" : "%s%s",
254                             psz_string, psz_name );
255             free( psz_parser );
256         }
257         else
258         {
259             return;
260         }
261     }
262     else
263     {
264         if( psz_parser )
265         {
266             if( *( psz_parser + strlen( psz_name ) ) == ':' )
267             {
268                 memmove( psz_parser, psz_parser + strlen( psz_name ) + 1,
269                          strlen( psz_parser + strlen( psz_name ) + 1 ) + 1 );
270             }
271             else
272             {
273                 *psz_parser = '\0';
274             }
275
276             /* Remove trailing : : */
277             if( strlen( psz_string ) > 0 &&
278                 *( psz_string + strlen( psz_string ) -1 ) == ':' )
279             {
280                 *( psz_string + strlen( psz_string ) -1 ) = '\0';
281             }
282         }
283         else
284         {
285             free( psz_string );
286             return;
287         }
288     }
289     /* Vout is not kept, so put that in the config */
290     config_PutPsz( p_intf, psz_filter_type, psz_string );
291     if( !strcmp( psz_filter_type, "video-filter" ) )
292         ui.videoFilterText->setText( psz_string );
293     else if( !strcmp( psz_filter_type, "vout-filter" ) )
294         ui.voutFilterText->setText( psz_string );
295     else if( !strcmp( psz_filter_type, "sub-filter" ) )
296         ui.subpictureFilterText->setText( psz_string );
297
298     /* Try to set on the fly */
299     p_vout = ( vout_thread_t * )vlc_object_find( p_intf, VLC_OBJECT_VOUT,
300                                               FIND_ANYWHERE );
301     if( p_vout )
302     {
303         if( !strcmp( psz_filter_type, "sub-filter" ) )
304             var_SetString( p_vout->p_spu, psz_filter_type, psz_string );
305         else
306             var_SetString( p_vout, psz_filter_type, psz_string );
307         vlc_object_release( p_vout );
308     }
309
310     free( psz_string );
311 }
312
313 void ExtVideo::updateFilters()
314 {
315     QString module = ModuleFromWidgetName( sender() );
316     //std::cout << "Module name: " << module.toStdString() << std::endl;
317
318     QCheckBox *checkbox = qobject_cast<QCheckBox*>( sender() );
319     QGroupBox *groupbox = qobject_cast<QGroupBox*>( sender() );
320
321     ChangeVFiltersString( qtu( module ),
322                           checkbox ? checkbox->isChecked()
323                                    : groupbox->isChecked() );
324 }
325
326 void ExtVideo::initComboBoxItems( QObject *widget )
327 {
328     QComboBox *combobox = qobject_cast<QComboBox*>( widget );
329     if( !combobox ) return;
330     QString option = OptionFromWidgetName( widget );
331     module_config_t *p_item = config_FindConfig( VLC_OBJECT( p_intf ),
332                                                  option.toStdString().c_str() );
333     if( p_item )
334     {
335         int i_type = p_item->i_type & CONFIG_ITEM;
336         for( int i_index = 0; i_index < p_item->i_list; i_index++ )
337         {
338             if( i_type == CONFIG_ITEM_INTEGER
339              || i_type == CONFIG_ITEM_BOOL )
340                 combobox->addItem( qfu( p_item->ppsz_list_text[i_index] ),
341                                    p_item->pi_list[i_index] );
342             else if( i_type == CONFIG_ITEM_STRING )
343                 combobox->addItem( qfu( p_item->ppsz_list_text[i_index] ),
344                                    p_item->ppsz_list[i_index] );
345         }
346     }
347     else
348     {
349         msg_Err( p_intf, "Couldn't find option \"%s\".",
350                  option.toStdString().c_str() );
351     }
352 }
353
354 void ExtVideo::setWidgetValue( QObject *widget )
355 {
356     QString module = ModuleFromWidgetName( widget->parent() );
357     //std::cout << "Module name: " << module.toStdString() << std::endl;
358     QString option = OptionFromWidgetName( widget );
359     //std::cout << "Option name: " << option.toStdString() << std::endl;
360
361     vlc_object_t *p_obj = ( vlc_object_t * )
362         vlc_object_find_name( p_intf->p_libvlc,
363                               module.toStdString().c_str(),
364                               FIND_CHILD );
365     int i_type;
366     vlc_value_t val;
367
368     if( !p_obj )
369     {
370 #if 0
371         msg_Dbg( p_intf,
372                  "Module instance %s not found, looking in config values.",
373                  module.toStdString().c_str() );
374 #endif
375         i_type = config_GetType( p_intf, option.toStdString().c_str() ) & 0xf0;
376         switch( i_type )
377         {
378             case VLC_VAR_INTEGER:
379             case VLC_VAR_BOOL:
380                 val.i_int = config_GetInt( p_intf, option.toStdString().c_str() );
381                 break;
382             case VLC_VAR_FLOAT:
383                 val.f_float = config_GetFloat( p_intf, option.toStdString().c_str() );
384                 break;
385             case VLC_VAR_STRING:
386                 val.psz_string = config_GetPsz( p_intf, option.toStdString().c_str() );
387                 break;
388         }
389     }
390     else
391     {
392         i_type = var_Type( p_obj, option.toStdString().c_str() ) & 0xf0;
393         var_Get( p_obj, option.toStdString().c_str(), &val );
394         vlc_object_release( p_obj );
395     }
396
397     /* Try to cast to all the widgets we're likely to encounter. Only
398      * one of the casts is expected to work. */
399     QSlider        *slider        = qobject_cast<QSlider*>       ( widget );
400     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( widget );
401     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( widget );
402     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( widget );
403     QDial          *dial          = qobject_cast<QDial*>         ( widget );
404     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( widget );
405     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( widget );
406
407     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
408     {
409         int i_int = 0;
410         if( slider )        slider->setValue( val.i_int );
411         else if( checkbox ) checkbox->setCheckState( val.i_int? Qt::Checked
412                                                               : Qt::Unchecked );
413         else if( spinbox )  spinbox->setValue( val.i_int );
414         else if( dial )     dial->setValue( ( 540-val.i_int )%360 );
415         else if( lineedit )
416         {
417             char str[30];
418             sprintf( str, "%06X", val.i_int );
419             lineedit->setText( str );
420         }
421         else if( combobox ) combobox->setCurrentIndex(
422                             combobox->findData( val.i_int ) );
423         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
424     }
425     else if( i_type == VLC_VAR_FLOAT )
426     {
427         double f_float = 0;
428         if( slider ) slider->setValue( ( int )( val.f_float*( double )slider->tickInterval() ) ); /* hack alert! */
429         else if( doublespinbox ) doublespinbox->setValue( val.f_float );
430         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
431     }
432     else if( i_type == VLC_VAR_STRING )
433     {
434         if( lineedit ) lineedit->setText( qfu( val.psz_string ) );
435         else if( combobox ) combobox->setCurrentIndex(
436                             combobox->findData( qfu( val.psz_string ) ) );
437         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
438         free( val.psz_string );
439     }
440     else
441         msg_Err( p_intf,
442                  "Module %s's %s variable is of an unsupported type ( %d )",
443                  module.toStdString().c_str(),
444                  option.toStdString().c_str(),
445                  i_type );
446 }
447
448 void ExtVideo::updateFilterOptions()
449 {
450     QString module = ModuleFromWidgetName( sender()->parent() );
451     //std::cout << "Module name: " << module.toStdString() << std::endl;
452     QString option = OptionFromWidgetName( sender() );
453     //std::cout << "Option name: " << option.toStdString() << std::endl;
454
455     vlc_object_t *p_obj = ( vlc_object_t * )
456         vlc_object_find_name( p_intf->p_libvlc,
457                               module.toStdString().c_str(),
458                               FIND_CHILD );
459     if( !p_obj )
460     {
461         msg_Err( p_intf, "Module %s not found.", module.toStdString().c_str() );
462         return;
463     }
464
465     int i_type = var_Type( p_obj, option.toStdString().c_str() );
466     bool b_is_command = ( i_type & VLC_VAR_ISCOMMAND );
467     if( !b_is_command )
468     {
469         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.",
470                  module.toStdString().c_str(),
471                  option.toStdString().c_str() );
472         /* FIXME: restart automatically somewhere near the end of this function */
473     }
474
475     /* Try to cast to all the widgets we're likely to encounter. Only
476      * one of the casts is expected to work. */
477     QSlider        *slider        = qobject_cast<QSlider*>       ( sender() );
478     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( sender() );
479     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( sender() );
480     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( sender() );
481     QDial          *dial          = qobject_cast<QDial*>         ( sender() );
482     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( sender() );
483     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( sender() );
484
485     i_type &= 0xf0;
486     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
487     {
488         int i_int = 0;
489         if( slider )        i_int = slider->value();
490         else if( checkbox ) i_int = checkbox->checkState() == Qt::Checked;
491         else if( spinbox )  i_int = spinbox->value();
492         else if( dial )     i_int = ( 540-dial->value() )%360;
493         else if( lineedit ) i_int = lineedit->text().toInt( NULL,16 );
494         else if( combobox ) i_int = combobox->itemData( combobox->currentIndex() ).toInt();
495         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
496         config_PutInt( p_intf, option.toStdString().c_str(), i_int );
497         if( b_is_command )
498         {
499             if( i_type == VLC_VAR_INTEGER )
500                 var_SetInteger( p_obj, option.toStdString().c_str(), i_int );
501             else
502                 var_SetBool( p_obj, option.toStdString().c_str(), i_int );
503         }
504     }
505     else if( i_type == VLC_VAR_FLOAT )
506     {
507         double f_float = 0;
508         if( slider )             f_float = ( double )slider->value()
509                                          / ( double )slider->tickInterval(); /* hack alert! */
510         else if( doublespinbox ) f_float = doublespinbox->value();
511         else if( lineedit ) f_float = lineedit->text().toDouble();
512         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
513         config_PutFloat( p_intf, option.toStdString().c_str(), f_float );
514         if( b_is_command )
515             var_SetFloat( p_obj, option.toStdString().c_str(), f_float );
516     }
517     else if( i_type == VLC_VAR_STRING )
518     {
519         char *psz_string = NULL;
520         if( lineedit ) psz_string = strdup( qtu( lineedit->text() ) );
521         else if( combobox ) psz_string = strdup( qtu( combobox->itemData(
522                                          combobox->currentIndex() ).toString() ) );
523         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
524         config_PutPsz( p_intf, option.toStdString().c_str(), psz_string );
525         if( b_is_command )
526             var_SetString( p_obj, option.toStdString().c_str(), psz_string );
527         free( psz_string );
528     }
529     else
530         msg_Err( p_intf,
531                  "Module %s's %s variable is of an unsupported type ( %d )",
532                  module.toStdString().c_str(),
533                  option.toStdString().c_str(),
534                  i_type );
535
536     vlc_object_release( p_obj );
537 }
538
539 #if 0
540 void ExtVideo::gotoConf( QObject* src )
541 {
542 #define SHOWCONF( module ) \
543     if( src->objectName().contains( module ) ) \
544     { \
545         PrefsDialog::getInstance( p_intf )->showModulePrefs( module ); \
546         return; \
547     }
548     SHOWCONF( "clone" );
549     SHOWCONF( "magnify" );
550     SHOWCONF( "wave" );
551     SHOWCONF( "ripple" );
552     SHOWCONF( "invert" );
553     SHOWCONF( "puzzle" );
554     SHOWCONF( "wall" );
555     SHOWCONF( "gradient" );
556     SHOWCONF( "colorthres" )
557 }
558 #endif
559
560 /**********************************************************************
561  * v4l2 controls
562  **********************************************************************/
563
564 ExtV4l2::ExtV4l2( intf_thread_t *_p_intf, QWidget *_parent )
565     : QWidget( _parent ), p_intf( _p_intf )
566 {
567     ui.setupUi( this );
568
569     BUTTONACT( ui.refresh, Refresh() );
570
571     box = NULL;
572 }
573
574 ExtV4l2::~ExtV4l2()
575 {
576     if( box )
577         delete box;
578 }
579
580 void ExtV4l2::showEvent( QShowEvent *event )
581 {
582     QWidget::showEvent( event );
583     Refresh();
584 }
585
586 void ExtV4l2::Refresh( void )
587 {
588     vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( p_intf, "v4l2", FIND_ANYWHERE );
589     ui.help->hide();
590     if( box )
591     {
592         ui.vboxLayout->removeWidget( box );
593         delete box;
594         box = NULL;
595     }
596     if( p_obj )
597     {
598         msg_Dbg( p_intf, "Found v4l2 instance" );
599         vlc_value_t val, text, name;
600         int i_ret = var_Change( p_obj, "controls", VLC_VAR_GETCHOICES,
601                                 &val, &text );
602         if( i_ret < 0 )
603         {
604             msg_Err( p_intf, "Oops, v4l2 object doesn't have a 'controls' variable." );
605             ui.help->show();
606             vlc_object_release( p_obj );
607             return;
608         }
609
610         box = new QGroupBox( this );
611         ui.vboxLayout->addWidget( box );
612         QVBoxLayout *layout = new QVBoxLayout( box );
613         box->setLayout( layout );
614
615         for( int i = 0; i < val.p_list->i_count; i++ )
616         {
617             const char *psz_var = text.p_list->p_values[i].psz_string;
618             var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &name, NULL );
619             const char *psz_label = name.psz_string;
620             msg_Dbg( p_intf, "v4l2 control \"%x\": %s (%s)",
621                      val.p_list->p_values[i].i_int, psz_var, name.psz_string );
622
623             int i_type = var_Type( p_obj, psz_var );
624             switch( i_type & VLC_VAR_TYPE )
625             {
626                 case VLC_VAR_INTEGER:
627                 {
628                     QLabel *label = new QLabel( psz_label, box );
629                     QHBoxLayout *hlayout = new QHBoxLayout();
630                     hlayout->addWidget( label );
631                     int i_val = var_GetInteger( p_obj, psz_var );
632                     if( i_type & VLC_VAR_HASCHOICE )
633                     {
634                         QComboBox *combobox = new QComboBox( box );
635                         combobox->setObjectName( psz_var );
636
637                         vlc_value_t val2, text2;
638                         var_Change( p_obj, psz_var, VLC_VAR_GETCHOICES,
639                                     &val2, &text2 );
640                         for( int j = 0; j < val2.p_list->i_count; j++ )
641                         {
642                             combobox->addItem(
643                                        text2.p_list->p_values[j].psz_string,
644                                        val2.p_list->p_values[j].i_int );
645                             if( i_val == val2.p_list->p_values[j].i_int )
646                                 combobox->setCurrentIndex( j );
647                         }
648                         var_Change( p_obj, psz_var, VLC_VAR_FREELIST,
649                                     &val2, &text2 );
650
651                         CONNECT( combobox, currentIndexChanged( int ), this,
652                                  ValueChange( int ) );
653                         hlayout->addWidget( combobox );
654                     }
655                     else
656                     {
657                         QSlider *slider = new QSlider( box );
658                         slider->setObjectName( psz_var );
659                         slider->setOrientation( Qt::Horizontal );
660                         vlc_value_t val2;
661                         var_Change( p_obj, psz_var, VLC_VAR_GETMIN,
662                                     &val2, NULL );
663                         slider->setMinimum( val2.i_int );
664                         var_Change( p_obj, psz_var, VLC_VAR_GETMAX,
665                                     &val2, NULL );
666                         slider->setMaximum( val2.i_int );
667                         var_Change( p_obj, psz_var, VLC_VAR_GETSTEP,
668                                     &val2, NULL );
669                         slider->setSingleStep( val2.i_int );
670                         slider->setValue( i_val );
671
672                         CONNECT( slider, valueChanged( int ), this,
673                                  ValueChange( int ) );
674                         hlayout->addWidget( slider );
675                     }
676                     layout->addLayout( hlayout );
677                     break;
678                 }
679                 case VLC_VAR_BOOL:
680                 {
681                     QCheckBox *button = new QCheckBox( psz_label, box );
682                     button->setObjectName( psz_var );
683                     button->setChecked( var_GetBool( p_obj, psz_var ) );
684
685                     CONNECT( button, clicked( bool ), this,
686                              ValueChange( bool ) );
687                     layout->addWidget( button );
688                     break;
689                 }
690                 case VLC_VAR_VOID:
691                 {
692                     if( i_type & VLC_VAR_ISCOMMAND )
693                     {
694                         QPushButton *button = new QPushButton( psz_label, box );
695                         button->setObjectName( psz_var );
696
697                         CONNECT( button, clicked( bool ), this,
698                                  ValueChange( bool ) );
699                         layout->addWidget( button );
700                     }
701                     else
702                     {
703                         QLabel *label = new QLabel( psz_label, box );
704                         layout->addWidget( label );
705                     }
706                     break;
707                 }
708                 default:
709                     msg_Warn( p_intf, "Unhandled var type for %s", psz_var );
710                     break;
711             }
712             free( name.psz_string );
713         }
714         var_Change( p_obj, "controls", VLC_VAR_FREELIST, &val, &text );
715         vlc_object_release( p_obj );
716     }
717     else
718     {
719         msg_Dbg( p_intf, "Couldn't find v4l2 instance" );
720         ui.help->show();
721     }
722
723 }
724
725 void ExtV4l2::ValueChange( bool value )
726 {
727     ValueChange( (int)value );
728 }
729
730 void ExtV4l2::ValueChange( int value )
731 {
732     QObject *s = sender();
733     vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( p_intf, "v4l2", FIND_ANYWHERE );
734     if( p_obj )
735     {
736         char *psz_var = strdup( s->objectName().toStdString().c_str() );
737         int i_type = var_Type( p_obj, psz_var );
738         switch( i_type & VLC_VAR_TYPE )
739         {
740             case VLC_VAR_INTEGER:
741                 if( i_type & VLC_VAR_HASCHOICE )
742                 {
743                     QComboBox *combobox = qobject_cast<QComboBox*>( s );
744                     value = combobox->itemData( value ).toInt();
745                 }
746                 var_SetInteger( p_obj, psz_var, value );
747                 break;
748             case VLC_VAR_BOOL:
749                 var_SetBool( p_obj, psz_var, value );
750                 break;
751             case VLC_VAR_VOID:
752                 var_SetVoid( p_obj, psz_var );
753                 break;
754         }
755         free( psz_var );
756         vlc_object_release( p_obj );
757     }
758     else
759     {
760         msg_Warn( p_intf, "Oops, v4l2 object isn't available anymore" );
761         Refresh();
762     }
763 }
764
765 /**********************************************************************
766  * Equalizer
767  **********************************************************************/
768
769 static const QString band_frequencies[] =
770 {
771     "  60 Hz  ", " 170 Hz ", " 310 Hz ", " 600 Hz ", "  1 kHz ",
772     "  3 kHz  ", "  6 kHz ", " 12 kHz ", " 14 kHz ", " 16 kHz "
773 };
774
775 Equalizer::Equalizer( intf_thread_t *_p_intf, QWidget *_parent ) :
776                             QWidget( _parent ) , p_intf( _p_intf )
777 {
778     QFont smallFont = QApplication::font( static_cast<QWidget*>( 0 ) );
779     smallFont.setPointSize( smallFont.pointSize() - 3 );
780
781     ui.setupUi( this );
782     presetsComboBox = ui.presetsCombo;
783
784     ui.preampLabel->setFont( smallFont );
785     ui.preampSlider->setMaximum( 400 );
786     for( int i = 0 ; i < NB_PRESETS ; i ++ )
787     {
788         ui.presetsCombo->addItem( qtr( preset_list_text[i] ),
789                                   QVariant( i ) );
790     }
791     CONNECT( ui.presetsCombo, activated( int ), this, setPreset( int ) );
792
793     BUTTONACT( ui.enableCheck, enable() );
794     BUTTONACT( ui.eq2PassCheck, set2Pass() );
795
796     CONNECT( ui.preampSlider, valueChanged( int ), this, setPreamp() );
797
798     QGridLayout *grid = new QGridLayout( ui.frame );
799     grid->setMargin( 0 );
800     for( int i = 0 ; i < BANDS ; i++ )
801     {
802         bands[i] = new QSlider( Qt::Vertical );
803         bands[i]->setMaximum( 400 );
804         bands[i]->setValue( 200 );
805         CONNECT( bands[i], valueChanged( int ), this, setBand() );
806         band_texts[i] = new QLabel( band_frequencies[i] + "\n0.0dB" );
807         band_texts[i]->setFont( smallFont );
808         grid->addWidget( bands[i], 0, i );
809         grid->addWidget( band_texts[i], 1, i );
810     }
811
812     /* Write down initial values */
813     aout_instance_t *p_aout = ( aout_instance_t * )vlc_object_find( p_intf,
814                                     VLC_OBJECT_AOUT, FIND_ANYWHERE );
815     char *psz_af;
816     char *psz_bands;
817     float f_preamp;
818     if( p_aout )
819     {
820         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
821         if( var_GetBool( p_aout, "equalizer-2pass" ) )
822             ui.eq2PassCheck->setChecked( true );
823         psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
824         f_preamp = var_GetFloat( p_aout, "equalizer-preamp" );
825         vlc_object_release( p_aout );
826     }
827     else
828     {
829         psz_af = config_GetPsz( p_intf, "audio-filter" );
830         if( config_GetInt( p_intf, "equalizer-2pass" ) )
831             ui.eq2PassCheck->setChecked( true );
832         psz_bands = config_GetPsz( p_intf, "equalizer-bands" );
833         f_preamp = config_GetFloat( p_intf, "equalizer-preamp" );
834     }
835     if( psz_af && strstr( psz_af, "equalizer" ) != NULL )
836         ui.enableCheck->setChecked( true );
837     free( psz_af );
838     enable( ui.enableCheck->isChecked() );
839
840     setValues( psz_bands, f_preamp );
841 }
842
843 Equalizer::~Equalizer()
844 {
845 }
846
847 void Equalizer::enable()
848 {
849     bool en = ui.enableCheck->isChecked();
850     aout_EnableFilter( VLC_OBJECT( p_intf ), "equalizer",
851                        en ? VLC_TRUE : VLC_FALSE );
852 //    aout_EnableFilter( VLC_OBJECT( p_intf ), "upmixer",
853 //                       en ? VLC_TRUE : VLC_FALSE );
854 //     aout_EnableFilter( VLC_OBJECT( p_intf ), "vsurround",
855 //                       en ? VLC_TRUE : VLC_FALSE );
856      enable( en );
857 }
858
859 void Equalizer::enable( bool en )
860 {
861     ui.eq2PassCheck->setEnabled( en );
862     ui.preampLabel->setEnabled( en );
863     ui.preampSlider->setEnabled( en  );
864     for( int i = 0 ; i< BANDS; i++ )
865     {
866         bands[i]->setEnabled( en ); band_texts[i]->setEnabled( en );
867     }
868 }
869
870 void Equalizer::set2Pass()
871 {
872     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
873                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
874     vlc_bool_t b_2p = ui.eq2PassCheck->isChecked();
875
876     if( p_aout == NULL )
877         config_PutInt( p_intf, "equalizer-2pass", b_2p );
878     else
879     {
880         var_SetBool( p_aout, "equalizer-2pass", b_2p );
881         config_PutInt( p_intf, "equalizer-2pass", b_2p );
882         for( int i = 0; i < p_aout->i_nb_inputs; i++ )
883         {
884             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
885         }
886         vlc_object_release( p_aout );
887     }
888 }
889
890 void Equalizer::setPreamp()
891 {
892     float f= ( float )(  ui.preampSlider->value() ) /10 - 20;
893     char psz_val[5];
894     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
895                                        VLC_OBJECT_AOUT, FIND_ANYWHERE );
896
897     sprintf( psz_val, "%.1f", f );
898     ui.preampLabel->setText( qtr( "Preamp\n" ) + psz_val + qtr( "dB" ) );
899     if( p_aout )
900     {
901         delCallbacks( p_aout );
902         var_SetFloat( p_aout, "equalizer-preamp", f );
903         addCallbacks( p_aout );
904         vlc_object_release( p_aout );
905     }
906     config_PutFloat( p_intf, "equalizer-preamp", f );
907 }
908
909 void Equalizer::setBand()
910 {
911     char psz_values[102]; memset( psz_values, 0, 102 );
912
913     /**\todo smoothing */
914
915     for( int i = 0 ; i< BANDS ; i++ )
916     {
917         char psz_val[5];
918         float f_val = ( float )(  bands[i]->value() ) / 10 - 20 ;
919         sprintf( psz_values, "%s %f", psz_values, f_val );
920         sprintf( psz_val, "% 5.1f", f_val );
921         band_texts[i]->setText( band_frequencies[i] + "\n" + psz_val + "dB" );
922     }
923     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
924                                           VLC_OBJECT_AOUT, FIND_ANYWHERE );
925     if( p_aout )
926     {
927         delCallbacks( p_aout );
928         var_SetString( p_aout, "equalizer-bands", psz_values );
929         addCallbacks( p_aout );
930         vlc_object_release( p_aout );
931     }
932 }
933 void Equalizer::setValues( char *psz_bands, float f_preamp )
934 {
935     char *p = psz_bands;
936     if ( p )
937     {
938         for( int i = 0; i < 10; i++ )
939         {
940             char psz_val[5];
941             float f = strtof( p, &p );
942             int  i_val= ( int )( ( f + 20 ) * 10 );
943             bands[i]->setValue(  i_val );
944             sprintf( psz_val, "% 5.1f", f );
945             band_texts[i]->setText( band_frequencies[i] + "\n" + psz_val +
946                                     "dB" );
947             if( p == NULL || *p == '\0' ) break;
948             p++;
949             if( *p == '\0' )  break;
950         }
951     }
952     char psz_val[5];
953     int i_val = ( int )( ( f_preamp + 20 ) * 10 );
954     sprintf( psz_val, "%.1f", f_preamp );
955     ui.preampSlider->setValue( i_val );
956     ui.preampLabel->setText( qtr( "Preamp\n" ) + psz_val + qtr( "dB" ) );
957 }
958
959 void Equalizer::setPreset( int preset )
960 {
961     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
962                                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
963
964     char psz_values[102]; memset( psz_values, 0, 102 );
965     for( int i = 0 ; i< 10 ;i++ )
966         sprintf( psz_values, "%s %.1f", psz_values,
967                                         eqz_preset_10b[preset]->f_amp[i] );
968
969     if( p_aout )
970     {
971         delCallbacks( p_aout );
972         var_SetString( p_aout, "equalizer-bands", psz_values );
973         var_SetFloat( p_aout, "equalizer-preamp",
974                       eqz_preset_10b[preset]->f_preamp );
975         addCallbacks( p_aout );
976         vlc_object_release( p_aout );
977     }
978     config_PutPsz( p_intf, "equalizer-bands", psz_values );
979     config_PutFloat( p_intf, "equalizer-preamp",
980                     eqz_preset_10b[preset]->f_preamp );
981
982     setValues( psz_values, eqz_preset_10b[preset]->f_preamp );
983 }
984
985 static int PresetCallback( vlc_object_t *p_this, char const *psz_cmd,
986                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
987 {
988     char *psz_preset = newval.psz_string;
989     Equalizer *eq = ( Equalizer * )p_data;
990     eq->presetsComboBox->setCurrentIndex( eq->presetsComboBox->findText( qfu( psz_preset ) ) );
991     return VLC_SUCCESS;
992 }
993
994 void Equalizer::delCallbacks( aout_instance_t *p_aout )
995 {
996     //var_DelCallback( p_aout, "equalizer-bands", EqzCallback, this );
997     //var_DelCallback( p_aout, "equalizer-preamp", EqzCallback, this );
998     var_DelCallback( p_aout, "equalizer-preset", PresetCallback, this );
999 }
1000
1001 void Equalizer::addCallbacks( aout_instance_t *p_aout )
1002 {
1003     //var_AddCallback( p_aout, "equalizer-bands", EqzCallback, this );
1004     //var_AddCallback( p_aout, "equalizer-preamp", EqzCallback, this );
1005     var_AddCallback( p_aout, "equalizer-preset", PresetCallback, this );
1006 }
1007
1008 /**********************************************************************
1009  * Audio filters
1010  **********************************************************************/
1011
1012 /**********************************************************************
1013  * Spatializer
1014  **********************************************************************/
1015 static const char *psz_control_names[] =
1016 {
1017     "Roomsize", "Width" , "Wet", "Dry", "Damp"
1018 };
1019
1020 Spatializer::Spatializer( intf_thread_t *_p_intf, QWidget *_parent ) :
1021     QWidget( _parent ) , p_intf( _p_intf )
1022 {
1023     QFont smallFont = QApplication::font( static_cast<QWidget*>( 0 ) );
1024     smallFont.setPointSize( smallFont.pointSize() - 3 );
1025
1026     QGridLayout *layout = new QGridLayout( this );
1027     layout->setMargin( 0 );
1028
1029     enableCheck = new QCheckBox( qfu( "Enable spatializer" ) );
1030     layout->addWidget( enableCheck, 0, 0, 1, NUM_SP_CTRL );
1031
1032     for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1033     {
1034         spatCtrl[i] = new QSlider( Qt::Vertical );
1035         if( i < 2 )
1036         {
1037             spatCtrl[i]->setMaximum( 10 );
1038             spatCtrl[i]->setValue( 2 );
1039         }
1040         else
1041         {
1042             spatCtrl[i]->setMaximum( 10 );
1043             spatCtrl[i]->setValue( 0 );
1044             spatCtrl[i]->setMinimum( -10 );
1045         }
1046         oldControlVars[i] = spatCtrl[i]->value();
1047         CONNECT( spatCtrl[i], valueChanged( int ), this, setInitValues() );
1048         ctrl_texts[i] = new QLabel( qfu( psz_control_names[i] ) + "\n" );
1049         ctrl_texts[i]->setFont( smallFont );
1050         ctrl_readout[i] = new QLabel( "" );
1051         ctrl_readout[i]->setFont( smallFont );
1052         layout->addWidget( spatCtrl[i], 1, i );
1053         layout->addWidget( ctrl_readout[i], 2, i );
1054         layout->addWidget( ctrl_texts[i], 3, i );
1055     }
1056
1057     BUTTONACT( enableCheck, enable() );
1058
1059     /* Write down initial values */
1060     aout_instance_t *p_aout = ( aout_instance_t * )
1061         vlc_object_find( p_intf, VLC_OBJECT_AOUT, FIND_ANYWHERE );
1062     char *psz_af;
1063
1064     if( p_aout )
1065     {
1066         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
1067         for( int i = 0; i < NUM_SP_CTRL ; i++ )
1068         {
1069             controlVars[i] = var_GetFloat( p_aout, psz_control_names[i] );
1070         }
1071         vlc_object_release( p_aout );
1072     }
1073     else
1074     {
1075         psz_af = config_GetPsz( p_aout, "audio-filter" );
1076         for( int i = 0; i < NUM_SP_CTRL ; i++ )
1077         {
1078             controlVars[i] = config_GetFloat( p_intf, psz_control_names[i] );
1079         }
1080     }
1081     if( psz_af && strstr( psz_af, "spatializer" ) != NULL )
1082         enableCheck->setChecked( true );
1083     free( psz_af );
1084     enable( enableCheck->isChecked() );
1085     setValues( controlVars );
1086 }
1087
1088 Spatializer::~Spatializer()
1089 {
1090 }
1091
1092 void Spatializer::enable()
1093 {
1094     bool en = enableCheck->isChecked();
1095     aout_EnableFilter( VLC_OBJECT( p_intf ), "spatializer",
1096             en ? VLC_TRUE : VLC_FALSE );
1097     enable( en );
1098 }
1099
1100 void Spatializer::enable( bool en )
1101 {
1102     for( int i = 0 ; i< NUM_SP_CTRL; i++ )
1103     {
1104         spatCtrl[i]->setEnabled( en );
1105         ctrl_texts[i]->setEnabled( en );
1106         ctrl_readout[i]->setEnabled( en );
1107     }
1108 }
1109 void Spatializer::setInitValues()
1110 {
1111     setValues( controlVars );
1112 }
1113
1114 void Spatializer::setValues( float *controlVars )
1115 {
1116     char psz_val[5];
1117     char var_name[5];
1118     aout_instance_t *p_aout= ( aout_instance_t * )
1119         vlc_object_find( p_intf, VLC_OBJECT_AOUT, FIND_ANYWHERE );
1120
1121     for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1122     {
1123         float f= ( float )(  spatCtrl[i]->value() );
1124         sprintf( psz_val, "%.1f", f );
1125         ctrl_readout[i]->setText( psz_val );
1126     }
1127     if( p_aout )
1128     {
1129         for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1130         {
1131             if( oldControlVars[i] != spatCtrl[i]->value() )
1132             {
1133                 var_SetFloat( p_aout, psz_control_names[i],
1134                         ( float )spatCtrl[i]->value() );
1135                 config_PutFloat( p_intf, psz_control_names[i],
1136                         ( float ) spatCtrl[i]->value() );
1137                 oldControlVars[i] = ( float ) spatCtrl[i]->value();
1138             }
1139         }
1140         vlc_object_release( p_aout );
1141     }
1142
1143 }
1144 void Spatializer::delCallbacks( aout_instance_t *p_aout )
1145 {
1146     //    var_DelCallback( p_aout, "Spatializer-bands", EqzCallback, this );
1147     //    var_DelCallback( p_aout, "Spatializer-preamp", EqzCallback, this );
1148 }
1149
1150 void Spatializer::addCallbacks( aout_instance_t *p_aout )
1151 {
1152     //    var_AddCallback( p_aout, "Spatializer-bands", EqzCallback, this );
1153     //    var_AddCallback( p_aout, "Spatializer-preamp", EqzCallback, this );
1154 }
1155
1156 /**********************************************************************
1157  * Video filters / Adjust
1158  **********************************************************************/
1159
1160 /**********************************************************************
1161  * Extended playbak controls
1162  **********************************************************************/