]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/extended_panels.cpp
Compile "fix".
[vlc] / modules / gui / qt4 / components / extended_panels.cpp
1 /*****************************************************************************
2  * extended_panels.cpp : Extended controls panels
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id: preferences.cpp 16643 2006-09-13 12:45:46Z zorglub $
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <QLabel>
25 #include <QVariant>
26 #include <QString>
27 #include <QFont>
28 #include <QGridLayout>
29 #include <QSignalMapper>
30
31 #include "components/extended_panels.hpp"
32 #include "dialogs/preferences.hpp"
33 #include "dialogs_provider.hpp"
34 #include "qt4.hpp"
35
36 #include "../../audio_filter/equalizer_presets.h"
37 #include <vlc_aout.h>
38 #include <vlc_intf_strings.h>
39 #include <vlc_vout.h>
40
41 class ConfClickHandler : public QObject
42 {
43 public:
44     ConfClickHandler( intf_thread_t *_p_intf, ExtVideo *_e ) : QObject (_e) {
45         e = _e; p_intf = _p_intf;
46     }
47     virtual ~ConfClickHandler() {}
48     bool eventFilter( QObject *obj, QEvent *evt )
49     {
50         if( evt->type() == QEvent::MouseButtonPress )
51         {
52             e->gotoConf( obj );
53             return true;
54         }
55         return false;
56     }
57 private:
58     ExtVideo* e;
59     intf_thread_t *p_intf;
60 };
61
62 #define SETUP_VFILTER( widget, conf, tooltip, labtooltip ) \
63     ui.widget##Check->setToolTip( tooltip ); \
64     if (conf ) {\
65         ui.widget##Label->setToolTip( labtooltip ); \
66         ui.widget##Label->setPixmap( QPixmap(":/pixmaps/go-next.png") ); \
67         ui.widget##Label->installEventFilter(h); \
68     } \
69     CONNECT( ui.widget##Check, clicked(), this, updateFilters() ); \
70
71 ExtVideo::ExtVideo( intf_thread_t *_p_intf, QWidget *_parent ) :
72                            QWidget( _parent ) , p_intf( _p_intf )
73 {
74     ConfClickHandler* h = new ConfClickHandler( p_intf, this );
75
76     ui.setupUi( this );
77
78 #if 0
79
80     SETUP_VFILTER( clone, true, qtr(I_CLONE_TIP),
81                                 qtr("Configure the clone filter") );
82     SETUP_VFILTER( magnify, false, qtr(I_MAGNIFY_TIP),
83                                 qtr("Configure the magnification effect"));
84     SETUP_VFILTER( wave, false, qtr(I_WAVE_TIP),
85                                 qtr("Configure the waves effect"));
86     SETUP_VFILTER( ripple, false, qtr(I_RIPPLE_TIP),
87                                   qtr("Configure the \"water\" effect"));
88     SETUP_VFILTER( invert, false, qtr(I_INVERT_TIP),
89                                   qtr("Configure the color inversion effect"));
90     SETUP_VFILTER( puzzle, true, qtr(I_PUZZLE_TIP),
91                                   qtr("Configure the puzzle effect"));
92     SETUP_VFILTER( wall, true, qtr(I_WALL_TIP),
93                                qtr("Configure the wall filter") );
94     SETUP_VFILTER( gradient, true, qtr(I_GRADIENT_TIP),
95                                    qtr("Configure the \"gradient\" effect"));
96     SETUP_VFILTER( colorthres, true, qtr(I_COLORTHRES_TIP),
97                                    qtr("Configure the color detection effect"));
98 #endif
99 }
100
101 ExtVideo::~ExtVideo()
102 {
103 }
104
105 static void ChangeVFiltersString( intf_thread_t *p_intf,
106                                  char *psz_name, vlc_bool_t b_add )
107 {
108     vout_thread_t *p_vout;
109     char *psz_parser, *psz_string;
110
111     psz_string = config_GetPsz( p_intf, "video-filter" );
112
113     if( !psz_string ) psz_string = strdup("");
114
115     psz_parser = strstr( psz_string, psz_name );
116
117     if( b_add )
118     {
119         if( !psz_parser )
120         {
121             psz_parser = psz_string;
122             asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
123                             psz_string, psz_name );
124             free( psz_parser );
125         }
126         else
127         {
128             return;
129         }
130     }
131     else
132     {
133         if( psz_parser )
134         {
135             if( *(psz_parser + strlen(psz_name)) == ':' )
136             {
137                 memmove( psz_parser, psz_parser + strlen(psz_name) + 1,
138                          strlen(psz_parser + strlen(psz_name) + 1 ) + 1 );
139             }
140             else
141             {
142                 *psz_parser = '\0';
143             }
144
145             /* Remove trailing : : */
146             if( strlen( psz_string ) > 0 &&
147                 *( psz_string + strlen( psz_string ) -1 ) == ':' )
148             {
149                 *( psz_string + strlen( psz_string ) -1 ) = '\0';
150             }
151         }
152         else
153         {
154             free( psz_string );
155             return;
156         }
157     }
158     /* Vout is not kept, so put that in the config */
159     config_PutPsz( p_intf, "video-filter", psz_string );
160
161     /* Try to set on the fly */
162     p_vout = (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
163                                               FIND_ANYWHERE );
164     if( p_vout )
165     {
166         var_SetString( p_vout, "video-filter", psz_string );
167         vlc_object_release( p_vout );
168     }
169
170     free( psz_string );
171 }
172
173 void ExtVideo::updateFilters()
174 {
175     QCheckBox* filter = qobject_cast<QCheckBox*>(sender());
176     QString module = filter->objectName().replace("Check", "");
177
178     ChangeVFiltersString( p_intf, qtu(module), filter->isChecked() );
179 }
180
181 void ExtVideo::gotoConf( QObject* src )
182 {
183 #define SHOWCONF(module) \
184     if( src->objectName().contains(module) ) \
185     { \
186         PrefsDialog::getInstance( p_intf )->showModulePrefs( module ); \
187         return; \
188     }
189     SHOWCONF( "clone" );
190     SHOWCONF( "magnify" );
191     SHOWCONF( "wave" );
192     SHOWCONF( "ripple" );
193     SHOWCONF( "invert" );
194     SHOWCONF( "puzzle" );
195     SHOWCONF( "wall" );
196     SHOWCONF( "gradient" );
197     SHOWCONF( "colorthres" )
198 }
199
200 /**********************************************************************
201  * Equalizer
202  **********************************************************************/
203
204 static const QString band_frequencies[] =
205 {
206     "   60Hz  ", " 170 Hz " , " 310 Hz ", " 600 Hz ", "  1 kHz  ",
207     "  3 kHz  " , "  6 kHz ", " 12 kHz ", " 14 kHz ", " 16 kHz "
208 };
209
210 Equalizer::Equalizer( intf_thread_t *_p_intf, QWidget *_parent ) :
211                             QWidget( _parent ) , p_intf( _p_intf )
212 {
213     QFont smallFont = QApplication::font( static_cast<QWidget*>(0) );
214     smallFont.setPointSize( smallFont.pointSize() - 3 );
215
216     ui.setupUi( this );
217
218     ui.preampLabel->setFont( smallFont );
219     ui.preampSlider->setMaximum( 400 );
220     for( int i = 0 ; i < NB_PRESETS ; i ++ )
221     {
222         ui.presetsCombo->addItem( qtr( preset_list_text[i] ),
223                                   QVariant( i ) );
224     }
225     CONNECT( ui.presetsCombo, activated( int ), this, setPreset( int ) );
226
227     BUTTONACT( ui.enableCheck, enable() );
228     BUTTONACT( ui.eq2PassCheck, set2Pass() );
229
230     CONNECT( ui.preampSlider, valueChanged(int), this, setPreamp() );
231
232     QGridLayout *grid = new QGridLayout( ui.frame );
233     grid->setMargin( 0 );
234     for( int i = 0 ; i < BANDS ; i++ )
235     {
236         bands[i] = new QSlider( Qt::Vertical );
237         bands[i]->setMaximum( 400 );
238         bands[i]->setValue( 200 );
239         CONNECT( bands[i], valueChanged(int), this, setBand() );
240         band_texts[i] = new QLabel( band_frequencies[i] + "\n0.0dB" );
241         band_texts[i]->setFont( smallFont );
242         grid->addWidget( bands[i], 0, i );
243         grid->addWidget( band_texts[i], 1, i );
244     }
245
246     /* Write down initial values */
247     aout_instance_t *p_aout = (aout_instance_t *)vlc_object_find(p_intf,
248                                     VLC_OBJECT_AOUT, FIND_ANYWHERE);
249     char *psz_af = NULL;
250     char *psz_bands;
251     float f_preamp;
252     if( p_aout )
253     {
254         psz_af = var_GetString( p_aout, "audio-filter" );
255         if( var_GetBool( p_aout, "equalizer-2pass" ) )
256             ui.eq2PassCheck->setChecked( true );
257         psz_bands = var_GetString( p_aout, "equalizer-bands" );
258         f_preamp = var_GetFloat( p_aout, "equalizer-preamp" );
259         vlc_object_release( p_aout );
260     }
261     else
262     {
263         psz_af = config_GetPsz( p_intf, "audio-filter" );
264         if( config_GetInt( p_intf, "equalizer-2pass" ) )
265             ui.eq2PassCheck->setChecked( true );
266         psz_bands = config_GetPsz( p_intf, "equalizer-bands" );
267         f_preamp = config_GetFloat( p_intf, "equalizer-preamp" );
268     }
269     if( psz_af && strstr( psz_af, "equalizer" ) != NULL )
270         ui.enableCheck->setChecked( true );
271     enable( ui.enableCheck->isChecked() );
272
273     setValues( psz_bands, f_preamp );
274 }
275
276 Equalizer::~Equalizer()
277 {
278 }
279
280 void Equalizer::enable()
281 {
282     bool en = ui.enableCheck->isChecked();
283     aout_EnableFilter( VLC_OBJECT( p_intf ), "equalizer",
284                        en ? VLC_TRUE : VLC_FALSE );
285     enable( en );
286 }
287
288 void Equalizer::enable( bool en )
289 {
290     ui.eq2PassCheck->setEnabled( en );
291     ui.preampLabel->setEnabled( en );
292     ui.preampSlider->setEnabled( en  );
293     for( int i = 0 ; i< BANDS; i++ )
294     {
295         bands[i]->setEnabled( en ); band_texts[i]->setEnabled( en );
296     }
297 }
298
299 void Equalizer::set2Pass()
300 {
301     aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
302                                  VLC_OBJECT_AOUT, FIND_ANYWHERE);
303     vlc_bool_t b_2p = ui.eq2PassCheck->isChecked();
304
305     if( p_aout == NULL )
306         config_PutInt( p_intf, "equalizer-2pass", b_2p );
307     else
308     {
309         var_SetBool( p_aout, "equalizer-2pass", b_2p );
310         config_PutInt( p_intf, "equalizer-2pass", b_2p );
311         for( int i = 0; i < p_aout->i_nb_inputs; i++ )
312         {
313             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
314         }
315         vlc_object_release( p_aout );
316     }
317 }
318
319 void Equalizer::setPreamp()
320 {
321     float f= (float)(  ui.preampSlider->value() ) /10 - 20;
322     char psz_val[5];
323     aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
324                                        VLC_OBJECT_AOUT, FIND_ANYWHERE);
325
326     sprintf( psz_val, "%.1f", f );
327     ui.preampLabel->setText( qtr("Preamp\n") + psz_val + qtr("dB") );
328     if( p_aout )
329     {
330         delCallbacks( p_aout );
331         var_SetFloat( p_aout, "equalizer-preamp", f );
332         addCallbacks( p_aout );
333         vlc_object_release( p_aout );
334     }
335     config_PutFloat( p_intf, "equalizer-preamp", f );
336 }
337
338 void Equalizer::setBand()
339 {
340     char psz_values[102]; memset( psz_values, 0, 102 );
341
342     /**\todo smoothing */
343
344     for( int i = 0 ; i< BANDS ; i++ )
345     {
346         char psz_val[5];
347         float f_val = (float)(  bands[i]->value() ) / 10 - 20 ;
348         sprintf( psz_values, "%s %f", psz_values, f_val );
349         sprintf( psz_val, "% 5.1f", f_val );
350         band_texts[i]->setText( band_frequencies[i] + "\n" + psz_val + "dB" );
351     }
352     aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
353                                           VLC_OBJECT_AOUT, FIND_ANYWHERE);
354     if( p_aout )
355     {
356         delCallbacks( p_aout );
357         var_SetString( p_aout, "equalizer-bands", psz_values );
358         addCallbacks( p_aout );
359         vlc_object_release( p_aout );
360     }
361 }
362 void Equalizer::setValues( char *psz_bands, float f_preamp )
363 {
364     char *p = psz_bands;
365     if ( p )
366     {
367         for( int i = 0; i < 10; i++ )
368         {
369             char psz_val[5];
370             float f = strtof( p, &p );
371             int  i_val= (int)( ( f + 20 ) * 10 );
372             bands[i]->setValue(  i_val );
373             sprintf( psz_val, "% 5.1f", f );
374             band_texts[i]->setText( band_frequencies[i] + "\n" + psz_val +
375                                     "dB" );
376             if( p == NULL || *p == '\0' ) break;
377             p++;
378             if( *p == '\0' )  break;
379         }
380     }
381     char psz_val[5];
382     int i_val = (int)( ( f_preamp + 20 ) * 10 );
383     sprintf( psz_val, "%.1f", f_preamp );
384     ui.preampSlider->setValue( i_val );
385     ui.preampLabel->setText( qtr("Preamp\n") + psz_val + qtr("dB") );
386 }
387
388 void Equalizer::setPreset( int preset )
389 {
390     aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
391                                                 VLC_OBJECT_AOUT, FIND_ANYWHERE);
392
393     char psz_values[102]; memset( psz_values, 0, 102 );
394     for( int i = 0 ; i< 10 ;i++ )
395         sprintf( psz_values, "%s %.1f", psz_values,
396                                         eqz_preset_10b[preset]->f_amp[i] );
397
398     if( p_aout )
399     {
400         delCallbacks( p_aout );
401         var_SetString( p_aout, "equalizer-bands", psz_values );
402         var_SetFloat( p_aout, "equalizer-preamp",
403                       eqz_preset_10b[preset]->f_preamp );
404         addCallbacks( p_aout );
405         vlc_object_release( p_aout );
406     }
407     config_PutPsz( p_intf, "equalizer-bands", psz_values );
408     config_PutFloat( p_intf, "equalizer-preamp",
409                     eqz_preset_10b[preset]->f_preamp );
410
411     setValues( psz_values, eqz_preset_10b[preset]->f_preamp );
412 }
413
414 void Equalizer::delCallbacks( aout_instance_t *p_aout )
415 {
416 //    var_DelCallback( p_aout, "equalizer-bands", EqzCallback, this );
417 //    var_DelCallback( p_aout, "equalizer-preamp", EqzCallback, this );
418 }
419
420 void Equalizer::addCallbacks( aout_instance_t *p_aout )
421 {
422 //    var_AddCallback( p_aout, "equalizer-bands", EqzCallback, this );
423 //    var_AddCallback( p_aout, "equalizer-preamp", EqzCallback, this );
424 }
425
426
427 /**********************************************************************
428  * Video filters / Adjust
429  **********************************************************************/
430
431 /**********************************************************************
432  * Audio filters
433  **********************************************************************/
434
435 /**********************************************************************
436  * Extended playbak controls
437  **********************************************************************/