extended_panels.cpp 57.2 KB
Newer Older
Clément Stenac's avatar
Clément Stenac committed
1
/*****************************************************************************
Clément Stenac's avatar
Clément Stenac committed
2
 * extended_panels.cpp : Extended controls panels
Clément Stenac's avatar
Clément Stenac committed
3
 ****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2006-2013 the VideoLAN team
5
 * $Id$
Clément Stenac's avatar
Clément Stenac committed
6 7
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 *          Antoine Cellerier <dionoea .t videolan d@t org>
9
 *          Jean-Baptiste Kempf <jb@videolan.org>
Clément Stenac's avatar
Clément Stenac committed
10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
14
 * ( at your option ) any later version.
Clément Stenac's avatar
Clément Stenac committed
15 16 17 18 19 20 21 22 23 24
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
25

26 27 28
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
Clément Stenac's avatar
Clément Stenac committed
29

30 31
#include <math.h>

Clément Stenac's avatar
Clément Stenac committed
32 33 34 35 36
#include <QLabel>
#include <QVariant>
#include <QString>
#include <QFont>
#include <QGridLayout>
37
#include <QComboBox>
38
#include <QTimer>
39
#include <QFileDialog>
40 41
#include <QGraphicsScene>
#include <QPainter>
Clément Stenac's avatar
Clément Stenac committed
42

Clément Stenac's avatar
Clément Stenac committed
43
#include "components/extended_panels.hpp"
44
#include "dialogs/preferences.hpp"
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
45
#include "qt.hpp"
46
#include "input_manager.hpp"
47
#include "util/qt_dirs.hpp"
48
#include "util/customwidgets.hpp"
Clément Stenac's avatar
Clément Stenac committed
49

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
50
#include "../../audio_filter/equalizer_presets.h"
Clément Stenac's avatar
Clément Stenac committed
51
#include <vlc_vout.h>
52
#include <vlc_modules.h>
53
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
54

55
static char *ChangeFiltersString( struct intf_thread_t *p_intf, const char *psz_filter_type, const char *psz_name, bool b_add );
56
static void ChangeAFiltersString( struct intf_thread_t *p_intf, const char *psz_name, bool b_add );
57 58
static void ChangeVFiltersString( struct intf_thread_t *p_intf, const char *psz_name, bool b_add );

59
static const QString ModuleFromWidgetName( QObject *obj )
60
{
61
    return obj->objectName().replace( "Enable","" );
62 63
}

64
static QString OptionFromWidgetName( QObject *obj )
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
{
    /* Gruik ? ... nah */
    QString option = obj->objectName().replace( "Slider", "" )
                                      .replace( "Combo" , "" )
                                      .replace( "Dial"  , "" )
                                      .replace( "Check" , "" )
                                      .replace( "Spin"  , "" )
                                      .replace( "Text"  , "" );
    for( char a = 'A'; a <= 'Z'; a++ )
    {
        option = option.replace( QString( a ),
                                 QString( '-' ) + QString( a + 'a' - 'A' ) );
    }
    return option;
}
Clément Stenac's avatar
Clément Stenac committed
80

81
static inline void setup_vfilter( intf_thread_t *p_intf, const char* psz_name, QWidget *widget )
Clément Stenac's avatar
Clément Stenac committed
82
{
83
    vlc_object_t *p_obj = ( vlc_object_t * )
84
        vlc_object_find_name( p_intf->obj.libvlc, psz_name );
85 86 87 88 89 90 91 92 93 94 95 96 97 98
    QCheckBox *checkbox = qobject_cast<QCheckBox*>( widget );
    QGroupBox *groupbox = qobject_cast<QGroupBox*>( widget );
    if( p_obj )
    {
        vlc_object_release( p_obj ); \
        if( checkbox ) checkbox->setChecked( true ); \
        else if (groupbox) groupbox->setChecked( true ); \
    }
    else
    {
        if( checkbox ) checkbox->setChecked( false );
        else if (groupbox) groupbox->setChecked( false );
    }
}
Clément Stenac's avatar
Clément Stenac committed
99

100
#define SETUP_VFILTER( widget ) \
101
    setup_vfilter( p_intf, #widget, ui.widget##Enable ); \
102
    CONNECT( ui.widget##Enable, clicked(), this, updateFilters() );
103

104
#define SETUP_VFILTER_OPTION( widget, signal ) \
105
    initComboBoxItems( ui.widget ); \
106 107
    setWidgetValue( ui.widget ); \
    CONNECT( ui.widget, signal, this, updateFilterOptions() );
Antoine Cellerier's avatar
Antoine Cellerier committed
108

109 110 111 112 113
ExtVideo::ExtVideo( intf_thread_t *_p_intf, QTabWidget *_parent ) :
            QObject( _parent ), p_intf( _p_intf )
{
    ui.setupUi( _parent );

114
    SETUP_VFILTER( adjust )
115 116 117 118 119 120
    SETUP_VFILTER_OPTION( hueSlider, valueChanged( int ) )
    SETUP_VFILTER_OPTION( contrastSlider, valueChanged( int ) )
    SETUP_VFILTER_OPTION( brightnessSlider, valueChanged( int ) )
    SETUP_VFILTER_OPTION( saturationSlider, valueChanged( int ) )
    SETUP_VFILTER_OPTION( gammaSlider, valueChanged( int ) )
    SETUP_VFILTER_OPTION( brightnessThresholdCheck, stateChanged( int ) )
121 122

    SETUP_VFILTER( extract )
123
    SETUP_VFILTER_OPTION( extractComponentText, textChanged( const QString& ) )
124

125 126
    SETUP_VFILTER( posterize )

127
    SETUP_VFILTER( colorthres )
128
    SETUP_VFILTER_OPTION( colorthresColorText, textChanged( const QString& ) )
129 130
    SETUP_VFILTER_OPTION( colorthresSaturationthresSlider, valueChanged( int ) )
    SETUP_VFILTER_OPTION( colorthresSimilaritythresSlider, valueChanged( int ) )
131

132 133 134
    SETUP_VFILTER( sepia )
    SETUP_VFILTER_OPTION( sepiaIntensitySpin, valueChanged( int ) )

135 136 137
    SETUP_VFILTER( invert )

    SETUP_VFILTER( gradient )
138 139 140
    SETUP_VFILTER_OPTION( gradientModeCombo, currentIndexChanged( QString ) )
    SETUP_VFILTER_OPTION( gradientTypeCheck, stateChanged( int ) )
    SETUP_VFILTER_OPTION( gradientCartoonCheck, stateChanged( int ) )
141

142
    SETUP_VFILTER( motionblur )
143
    SETUP_VFILTER_OPTION( blurFactorSlider, valueChanged( int ) )
144 145 146 147 148 149

    SETUP_VFILTER( motiondetect )

    SETUP_VFILTER( psychedelic )

    SETUP_VFILTER( sharpen )
150
    SETUP_VFILTER_OPTION( sharpenSigmaSlider, valueChanged( int ) )
151 152 153 154 155 156

    SETUP_VFILTER( ripple )

    SETUP_VFILTER( wave )

    SETUP_VFILTER( transform )
157
    SETUP_VFILTER_OPTION( transformTypeCombo, currentIndexChanged( QString ) )
158 159

    SETUP_VFILTER( rotate )
160
    SETUP_VFILTER_OPTION( rotateAngleDial, valueChanged( int ) )
161 162
    ui.rotateAngleDial->setWrapping( true );
    ui.rotateAngleDial->setNotchesVisible( true );
163 164

    SETUP_VFILTER( puzzle )
165 166 167
    SETUP_VFILTER_OPTION( puzzleRowsSpin, valueChanged( int ) )
    SETUP_VFILTER_OPTION( puzzleColsSpin, valueChanged( int ) )
    SETUP_VFILTER_OPTION( puzzleBlackSlotCheck, stateChanged( int ) )
168 169 170 171

    SETUP_VFILTER( magnify )

    SETUP_VFILTER( clone )
172
    SETUP_VFILTER_OPTION( cloneCountSpin, valueChanged( int ) )
173 174

    SETUP_VFILTER( wall )
175 176
    SETUP_VFILTER_OPTION( wallRowsSpin, valueChanged( int ) )
    SETUP_VFILTER_OPTION( wallColsSpin, valueChanged( int ) )
177

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
178

179 180
    SETUP_VFILTER( erase )
    SETUP_VFILTER_OPTION( eraseMaskText, editingFinished() )
181 182
    SETUP_VFILTER_OPTION( eraseYSpin, valueChanged( int ) )
    SETUP_VFILTER_OPTION( eraseXSpin, valueChanged( int ) )
183
    BUTTONACT( ui.eraseBrowseBtn, browseEraseFile() );
184 185

    SETUP_VFILTER( marq )
186
    SETUP_VFILTER_OPTION( marqMarqueeText, textChanged( const QString& ) )
187
    SETUP_VFILTER_OPTION( marqPositionCombo, currentIndexChanged( QString ) )
188 189 190

    SETUP_VFILTER( logo )
    SETUP_VFILTER_OPTION( logoFileText, editingFinished() )
191 192
    SETUP_VFILTER_OPTION( logoYSpin, valueChanged( int ) )
    SETUP_VFILTER_OPTION( logoXSpin, valueChanged( int ) )
193
    SETUP_VFILTER_OPTION( logoOpacitySlider, valueChanged( int ) )
194
    BUTTONACT( ui.logoBrowseBtn, browseLogo() );
195

196 197 198
    SETUP_VFILTER( gradfun )
    SETUP_VFILTER_OPTION( gradfunRadiusSlider, valueChanged( int ) )

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
199 200 201
    SETUP_VFILTER( grain )
    SETUP_VFILTER_OPTION( grainVarianceSlider, valueChanged( int ) )

202 203 204
    SETUP_VFILTER( mirror )

    SETUP_VFILTER( gaussianblur )
205
    SETUP_VFILTER_OPTION( gaussianblurSigmaSlider, valueChanged( int ) )
206 207 208 209

    SETUP_VFILTER( antiflicker )
    SETUP_VFILTER_OPTION( antiflickerSofteningSizeSlider, valueChanged( int ) )

210 211 212 213 214 215
    SETUP_VFILTER( hqdn3d )
    SETUP_VFILTER_OPTION( hqdn3dLumaSpatSlider, valueChanged( int ) )
    SETUP_VFILTER_OPTION( hqdn3dLumaTempSlider, valueChanged( int ) )
    SETUP_VFILTER_OPTION( hqdn3dChromaSpatSlider, valueChanged( int ) )
    SETUP_VFILTER_OPTION( hqdn3dChromaTempSlider, valueChanged( int ) )

216

217 218
    SETUP_VFILTER( anaglyph )

219 220
#undef SETUP_VFILTER
#undef SETUP_VFILTER_OPTION
221 222 223 224 225

    CONNECT( ui.cropTopPx, valueChanged( int ), this, cropChange() );
    CONNECT( ui.cropBotPx, valueChanged( int ), this, cropChange() );
    CONNECT( ui.cropLeftPx, valueChanged( int ), this, cropChange() );
    CONNECT( ui.cropRightPx, valueChanged( int ), this, cropChange() );
Dylan Yudaken's avatar
Dylan Yudaken committed
226 227
    CONNECT( ui.leftRightCropSync, toggled ( bool ), this, cropChange() );
    CONNECT( ui.topBotCropSync, toggled ( bool ), this, cropChange() );
228 229 230 231
    CONNECT( ui.topBotCropSync, toggled( bool ),
             ui.cropBotPx, setDisabled( bool ) );
    CONNECT( ui.leftRightCropSync, toggled( bool ),
             ui.cropRightPx, setDisabled( bool ) );
Clément Stenac's avatar
Clément Stenac committed
232 233
}

234 235
void ExtVideo::cropChange()
{
Dylan Yudaken's avatar
Dylan Yudaken committed
236 237 238 239 240
    if( ui.topBotCropSync->isChecked() )
        ui.cropBotPx->setValue( ui.cropTopPx->value() );
    if( ui.leftRightCropSync->isChecked() )
        ui.cropRightPx->setValue( ui.cropLeftPx->value() );

241
    vout_thread_t *p_vout = THEMIM->getVout();
242
    if( p_vout )
243
    {
244 245 246 247
        var_SetInteger( p_vout, "crop-top", ui.cropTopPx->value() );
        var_SetInteger( p_vout, "crop-bottom", ui.cropBotPx->value() );
        var_SetInteger( p_vout, "crop-left", ui.cropLeftPx->value() );
        var_SetInteger( p_vout, "crop-right", ui.cropRightPx->value() );
248
        vlc_object_release( p_vout );
249 250 251
    }
}

252 253 254 255 256 257 258 259
void ExtVideo::clean()
{
    ui.cropTopPx->setValue( 0 );
    ui.cropBotPx->setValue( 0 );
    ui.cropLeftPx->setValue( 0 );
    ui.cropRightPx->setValue( 0 );
}

260
static char *ChangeFiltersString( struct intf_thread_t *p_intf, const char *psz_filter_type, const char *psz_name, bool b_add )
Clément Stenac's avatar
Clément Stenac committed
261
{
262
    char* psz_chain = config_GetPsz( p_intf, psz_filter_type );
263

264 265
    QString const chain = QString( psz_chain ? psz_chain : "" );
    QStringList list = chain.split( ':', QString::SplitBehavior::SkipEmptyParts );
Clément Stenac's avatar
Clément Stenac committed
266

267 268
    if( b_add ) list << psz_name;
    else        list.removeAll( psz_name );
Clément Stenac's avatar
Clément Stenac committed
269

270
    free( psz_chain );
Clément Stenac's avatar
Clément Stenac committed
271

272
    return strdup( qtu( list.join( ':' ) ) );
273 274
}

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
static void ChangeAFiltersString( struct intf_thread_t *p_intf, const char *psz_name, bool b_add )
{
    char *psz_string;

    module_t *p_obj = module_find( psz_name );
    if( !p_obj )
    {
        msg_Err( p_intf, "Unable to find filter module \"%s\".", psz_name );
        return;
    }

    psz_string = ChangeFiltersString( p_intf, "audio-filter", psz_name, b_add );
    if( !psz_string )
        return;

    config_PutPsz( p_intf, "audio-filter", psz_string );

    free( psz_string );
}

295
static const char* GetVFilterType( struct intf_thread_t *p_intf, const char *psz_name )
296 297 298 299 300
{
    module_t *p_obj = module_find( psz_name );
    if( !p_obj )
    {
        msg_Err( p_intf, "Unable to find filter module \"%s\".", psz_name );
301
        return NULL;
302 303 304
    }

    if( module_provides( p_obj, "video splitter" ) )
305
        return "video-splitter";
306
    else if( module_provides( p_obj, "video filter" ) )
307
        return "video-filter";
308
    else if( module_provides( p_obj, "sub source" ) )
309
        return "sub-source";
310
    else if( module_provides( p_obj, "sub filter" ) )
311
        return "sub-filter";
312 313 314
    else
    {
        msg_Err( p_intf, "Unknown video filter type." );
315
        return NULL;
316
    }
317 318 319 320 321 322
}

static void ChangeVFiltersString( struct intf_thread_t *p_intf, const char *psz_name, bool b_add )
{
    char *psz_string;
    const char *psz_filter_type = GetVFilterType( p_intf, psz_name );
323 324 325 326 327

    psz_string = ChangeFiltersString( p_intf, psz_filter_type, psz_name, b_add );
    if( !psz_string )
        return;

Clément Stenac's avatar
Clément Stenac committed
328
    /* Vout is not kept, so put that in the config */
329
    config_PutPsz( p_intf, psz_filter_type, psz_string );
Clément Stenac's avatar
Clément Stenac committed
330 331

    /* Try to set on the fly */
332
    if( !strcmp( psz_filter_type, "video-splitter" ) )
Clément Stenac's avatar
Clément Stenac committed
333
    {
334
        playlist_t *p_playlist = THEPL;
335 336 337 338
        var_SetString( p_playlist, psz_filter_type, psz_string );
    }
    else
    {
339
        vout_thread_t *p_vout = THEMIM->getVout();
340 341 342 343 344
        if( p_vout )
        {
            var_SetString( p_vout, psz_filter_type, psz_string );
            vlc_object_release( p_vout );
        }
Clément Stenac's avatar
Clément Stenac committed
345 346 347 348 349 350 351
    }

    free( psz_string );
}

void ExtVideo::updateFilters()
{
352
    QString module = ModuleFromWidgetName( sender() );
Clément Stenac's avatar
Clément Stenac committed
353

354 355
    QCheckBox *checkbox = qobject_cast<QCheckBox*>( sender() );
    QGroupBox *groupbox = qobject_cast<QGroupBox*>( sender() );
356

357
    ChangeVFiltersString( p_intf, qtu( module ),
358 359
                          checkbox ? checkbox->isChecked()
                                   : groupbox->isChecked() );
Clément Stenac's avatar
Clément Stenac committed
360 361
}

362 363 364 365 366 367
#define UPDATE_AND_APPLY_TEXT( widget, file ) \
    CONNECT( ui.widget, textChanged( const QString& ), \
             this, updateFilterOptions() ); \
    ui.widget->setText( toNativeSeparators( file ) ); \
    ui.widget->disconnect( SIGNAL( textChanged( const QString& ) ) );

368 369 370 371
void ExtVideo::browseLogo()
{
    QString file = QFileDialog::getOpenFileName( NULL, qtr( "Logo filenames" ),
                   p_intf->p_sys->filepath, "Images (*.png *.jpg);;All (*)" );
372 373

    UPDATE_AND_APPLY_TEXT( logoFileText, file );
374 375 376 377 378 379
}

void ExtVideo::browseEraseFile()
{
    QString file = QFileDialog::getOpenFileName( NULL, qtr( "Image mask" ),
                   p_intf->p_sys->filepath, "Images (*.png *.jpg);;All (*)" );
380 381

    UPDATE_AND_APPLY_TEXT( eraseMaskText, file );
382 383
}

384 385
#undef UPDATE_AND_APPLY_TEXT

386 387
void ExtVideo::initComboBoxItems( QObject *widget )
{
388
    QComboBox *combobox = qobject_cast<QComboBox*>( widget );
389
    if( !combobox ) return;
390

391
    QString option = OptionFromWidgetName( widget );
392
    module_config_t *p_item = config_FindConfig( VLC_OBJECT( p_intf ),
393
                                                 qtu( option ) );
394
    if( p_item == NULL )
395
    {
396 397 398 399 400 401 402 403 404 405 406 407
        msg_Err( p_intf, "Couldn't find option \"%s\".", qtu( option ) );
        return;
    }

    if( p_item->i_type == CONFIG_ITEM_INTEGER
     || p_item->i_type == CONFIG_ITEM_BOOL )
    {
        int64_t *values;
        char **texts;
        ssize_t count = config_GetIntChoices( VLC_OBJECT( p_intf ),
                                              qtu( option ), &values, &texts );
        for( ssize_t i = 0; i < count; i++ )
408
        {
409
            combobox->addItem( qtr( texts[i] ), qlonglong(values[i]) );
410
            free( texts[i] );
411
        }
412 413
        free( texts );
        free( values );
414
    }
415
    else if( p_item->i_type == CONFIG_ITEM_STRING )
416
    {
417 418 419 420 421 422
        char **values;
        char **texts;
        ssize_t count = config_GetPszChoices( VLC_OBJECT( p_intf ),
                                              qtu( option ), &values, &texts );
        for( ssize_t i = 0; i < count; i++ )
        {
423
            combobox->addItem( qtr( texts[i] ), qfu(values[i]) );
424 425 426 427 428
            free( texts[i] );
            free( values[i] );
        }
        free( texts );
        free( values );
429 430 431
    }
}

432 433 434 435 436 437 438
void ExtVideo::setWidgetValue( QObject *widget )
{
    QString module = ModuleFromWidgetName( widget->parent() );
    //std::cout << "Module name: " << module.toStdString() << std::endl;
    QString option = OptionFromWidgetName( widget );
    //std::cout << "Option name: " << option.toStdString() << std::endl;

439
    vlc_object_t *p_obj = ( vlc_object_t * )
440
        vlc_object_find_name( p_intf->obj.libvlc, qtu( module ) );
441 442 443 444 445
    int i_type;
    vlc_value_t val;

    if( !p_obj )
    {
446
#if 0
447 448
        msg_Dbg( p_intf,
                 "Module instance %s not found, looking in config values.",
449
                 qtu( module ) );
450
#endif
451
        i_type = config_GetType( p_intf, qtu( option ) ) & VLC_VAR_CLASS;
452 453 454 455
        switch( i_type )
        {
            case VLC_VAR_INTEGER:
            case VLC_VAR_BOOL:
456
                val.i_int = config_GetInt( p_intf, qtu( option ) );
457 458
                break;
            case VLC_VAR_FLOAT:
459
                val.f_float = config_GetFloat( p_intf, qtu( option ) );
460 461
                break;
            case VLC_VAR_STRING:
462
                val.psz_string = config_GetPsz( p_intf, qtu( option ) );
463 464 465 466 467
                break;
        }
    }
    else
    {
468
        i_type = var_Type( p_obj, qtu( option ) ) & VLC_VAR_CLASS;
469
        var_Get( p_obj, qtu( option ), &val );
470 471 472 473 474
        vlc_object_release( p_obj );
    }

    /* Try to cast to all the widgets we're likely to encounter. Only
     * one of the casts is expected to work. */
475 476 477 478
    QSlider        *slider        = qobject_cast<QSlider*>       ( widget );
    QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( widget );
    QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( widget );
    QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( widget );
479
    VLCQDial       *dial          = qobject_cast<VLCQDial*>      ( widget );
480 481
    QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( widget );
    QComboBox      *combobox      = qobject_cast<QComboBox*>     ( widget );
482 483 484 485 486 487 488

    if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
    {
        if( slider )        slider->setValue( val.i_int );
        else if( checkbox ) checkbox->setCheckState( val.i_int? Qt::Checked
                                                              : Qt::Unchecked );
        else if( spinbox )  spinbox->setValue( val.i_int );
489
        else if( dial )     dial->setValue( (360 - val.i_int) % 360 );
490 491 492
        else if( lineedit )
        {
            char str[30];
Tristan Matthews's avatar
Tristan Matthews committed
493
            snprintf( str, sizeof(str), "%06" PRIX64, val.i_int );
494 495
            lineedit->setText( str );
        }
496
        else if( combobox ) combobox->setCurrentIndex(
497
                            combobox->findData( qlonglong(val.i_int) ) );
498
        else msg_Warn( p_intf, "Could not find the correct Integer widget" );
499 500 501
    }
    else if( i_type == VLC_VAR_FLOAT )
    {
502 503
        if( slider ) slider->setValue( ( int )( val.f_float*( double )slider->tickInterval() ) ); /* hack alert! */
        else if( doublespinbox ) doublespinbox->setValue( val.f_float );
504
        else if( dial ) dial->setValue( (360 - lroundf(val.f_float)) % 360 );
505
        else msg_Warn( p_intf, "Could not find the correct Float widget" );
506 507 508
    }
    else if( i_type == VLC_VAR_STRING )
    {
509
        if( lineedit ) lineedit->setText( qfu( val.psz_string ) );
510
        else if( combobox ) combobox->setCurrentIndex(
511
                            combobox->findData( qfu( val.psz_string ) ) );
512
        else msg_Warn( p_intf, "Could not find the correct String widget" );
513 514 515
        free( val.psz_string );
    }
    else
516 517 518 519 520 521
        if( p_obj )
            msg_Err( p_intf,
                     "Module %s's %s variable is of an unsupported type ( %d )",
                     qtu( module ),
                     qtu( option ),
                     i_type );
522 523
}

524 525
void ExtVideo::setFilterOption( struct intf_thread_t *p_intf, const char *psz_module, const char *psz_option,
        int i_int, double f_float, QString val )
526
{
527
    vlc_object_t *p_obj = ( vlc_object_t * )vlc_object_find_name( p_intf->obj.libvlc, psz_module );
528 529
    int i_type;
    bool b_is_command;
530

531 532
    if( !p_obj )
    {
533 534
        msg_Warn( p_intf, "Module %s not found. You'll need to restart the filter to take the change into account.", psz_module );
        i_type = config_GetType( p_intf, psz_option );
535 536 537 538
        b_is_command = false;
    }
    else
    {
539
        i_type = var_Type( p_obj, psz_option );
540
        if( i_type == 0 )
541
            i_type = config_GetType( p_intf, psz_option );
542
        b_is_command = ( i_type & VLC_VAR_ISCOMMAND );
543 544
    }

545
    i_type &= VLC_VAR_CLASS;
546 547
    if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
    {
548 549 550
        if( i_int == -1 )
            msg_Warn( p_intf, "Could not find the correct Integer widget" );
        config_PutInt( p_intf, psz_option, i_int );
551 552 553
        if( b_is_command )
        {
            if( i_type == VLC_VAR_INTEGER )
554
                var_SetInteger( p_obj, psz_option, i_int );
555
            else
556
                var_SetBool( p_obj, psz_option, i_int );
557
        }
558 559 560
    }
    else if( i_type == VLC_VAR_FLOAT )
    {
561 562 563
        if( f_float == -1 )
            msg_Warn( p_intf, "Could not find the correct Float widget" );
        config_PutFloat( p_intf, psz_option, f_float );
564
        if( b_is_command )
565
            var_SetFloat( p_obj, psz_option, f_float );
566 567 568
    }
    else if( i_type == VLC_VAR_STRING )
    {
569
        if( val.isNull() )
570
            msg_Warn( p_intf, "Could not find the correct String widget" );
571
        config_PutPsz( p_intf, psz_option, qtu( val ) );
572
        if( b_is_command )
573
            var_SetString( p_obj, psz_option, qtu( val ) );
574 575 576
    }
    else
        msg_Err( p_intf,
577
                 "Module %s's %s variable is of an unsupported type ( %d )",
578 579
                 psz_module,
                 psz_option,
580 581
                 i_type );

582 583 584
    if( !b_is_command )
    {
        msg_Warn( p_intf, "Module %s's %s variable isn't a command. Brute-restarting the filter.",
585 586 587 588
                 psz_module,
                 psz_option );
        ChangeVFiltersString( p_intf, psz_module, false );
        ChangeVFiltersString( p_intf, psz_module, true );
589 590
    }

591
    if( p_obj ) vlc_object_release( p_obj );
592 593
}

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
void ExtVideo::updateFilterOptions()
{
    QString module = ModuleFromWidgetName( sender()->parent() );
    //msg_Dbg( p_intf, "Module name: %s", qtu( module ) );
    QString option = OptionFromWidgetName( sender() );
    //msg_Dbg( p_intf, "Option name: %s", qtu( option ) );

    /* Try to cast to all the widgets we're likely to encounter. Only
     * one of the casts is expected to work. */
    QSlider        *slider        = qobject_cast<QSlider*>       ( sender() );
    QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( sender() );
    QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( sender() );
    QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( sender() );
    VLCQDial       *dial          = qobject_cast<VLCQDial*>      ( sender() );
    QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( sender() );
    QComboBox      *combobox      = qobject_cast<QComboBox*>     ( sender() );

    int i_int = -1;
    double f_float = -1.;
    QString val;

    if( slider ) {
        i_int = slider->value();
        f_float = ( double )slider->value() / ( double )slider->tickInterval(); /* hack alert! */
    }
    else if( checkbox ) i_int = checkbox->checkState() == Qt::Checked;
    else if( spinbox ) i_int = spinbox->value();
    else if( doublespinbox ) f_float = doublespinbox->value();
    else if( dial ) {
623 624
        i_int = (360 - dial->value()) % 360;
        f_float = i_int;
625 626 627 628 629 630 631 632 633 634 635 636 637 638
    }
    else if( lineedit ) {
        i_int = lineedit->text().toInt( NULL,16 );
        f_float = lineedit->text().toDouble();
        val = lineedit->text();
    }
    else if( combobox ) {
        i_int = combobox->itemData( combobox->currentIndex() ).toInt();
        val = combobox->itemData( combobox->currentIndex() ).toString();
    }

    setFilterOption( p_intf, qtu( module ), qtu( option ), i_int, f_float, val);
}

639 640 641 642 643 644 645 646 647 648 649 650
int ExtVideo::getPostprocessing( struct intf_thread_t *p_intf)
{
    char *psz_config = config_GetPsz(p_intf, "video-filter");
    int i_q = -1;
    if (psz_config) {
        if (strstr(psz_config, "postproc"))
            i_q = config_GetInt(p_intf, "postproc-q");
        free(psz_config);
    }
    return i_q;
}

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
void ExtVideo::setPostprocessing( struct intf_thread_t *p_intf, int q)
{
    const char *psz_name = "postproc";

    if( q == -1 )
    {
        ChangeVFiltersString( p_intf, psz_name, false );
    }
    else
    {
        ChangeVFiltersString( p_intf, psz_name, false );
        setFilterOption( p_intf, "postproc", "postproc-q", q, -1, QString() );
    }
}

666 667 668 669 670
/**********************************************************************
 * v4l2 controls
 **********************************************************************/

ExtV4l2::ExtV4l2( intf_thread_t *_p_intf, QWidget *_parent )
671
    : QWidget( _parent ), p_intf( _p_intf ), box( NULL )
672
{
673 674 675 676 677 678
    QVBoxLayout *layout = new QVBoxLayout( this );
    help = new QLabel( qtr("No v4l2 instance found.\n"
      "Please check that the device has been opened with VLC and is playing.\n\n"
      "Controls will automatically appear here.")
      , this );
    help->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
679
    help->setWordWrap( true );
680 681
    layout->addWidget( help );
    setLayout( layout );
682 683
}

684 685 686 687 688 689
void ExtV4l2::showEvent( QShowEvent *event )
{
    QWidget::showEvent( event );
    Refresh();
}

690 691
void ExtV4l2::Refresh( void )
{
692
    vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( THEPL, "v4l2" );
693
    help->hide();
694 695
    if( box )
    {
696
        layout()->removeWidget( box );
697 698 699
        delete box;
        box = NULL;
    }
700 701
    if( p_obj )
    {
702
        vlc_value_t val, text;
703 704 705 706
        int i_ret = var_Change( p_obj, "controls", VLC_VAR_GETCHOICES,
                                &val, &text );
        if( i_ret < 0 )
        {
707
            msg_Err( p_intf, "Oops, v4l2 object doesn't have a 'controls' variable." );
708
            help->show();
709 710 711
            vlc_object_release( p_obj );
            return;
        }
712 713

        box = new QGroupBox( this );
714
        layout()->addWidget( box );
715 716 717
        QVBoxLayout *layout = new QVBoxLayout( box );
        box->setLayout( layout );

718 719
        for( int i = 0; i < val.p_list->i_count; i++ )
        {
720
            vlc_value_t vartext;
721
            const char *psz_var = text.p_list->p_values[i].psz_string;
722 723 724 725 726 727

            if( var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &vartext, NULL ) )
                continue;

            QString name = qtr( vartext.psz_string );
            free( vartext.psz_string );
Tristan Matthews's avatar
Tristan Matthews committed
728
            msg_Dbg( p_intf, "v4l2 control \"%" PRIx64 "\": %s (%s)",
729
                     val.p_list->p_values[i].i_int, psz_var, qtu( name ) );
730 731 732 733 734 735

            int i_type = var_Type( p_obj, psz_var );
            switch( i_type & VLC_VAR_TYPE )
            {
                case VLC_VAR_INTEGER:
                {
736
                    QLabel *label = new QLabel( name, box );
737
                    QHBoxLayout *hlayout = new QHBoxLayout();
738 739 740 741
                    hlayout->addWidget( label );
                    int i_val = var_GetInteger( p_obj, psz_var );
                    if( i_type & VLC_VAR_HASCHOICE )
                    {
742
                        QComboBox *combobox = new QComboBox( box );
743
                        combobox->setObjectName( qfu( psz_var ) );
744 745 746 747 748 749 750 751

                        vlc_value_t val2, text2;
                        var_Change( p_obj, psz_var, VLC_VAR_GETCHOICES,
                                    &val2, &text2 );
                        for( int j = 0; j < val2.p_list->i_count; j++ )
                        {
                            combobox->addItem(
                                       text2.p_list->p_values[j].psz_string,
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
752
                                       qlonglong( val2.p_list->p_values[j].i_int) );
753 754 755
                            if( i_val == val2.p_list->p_values[j].i_int )
                                combobox->setCurrentIndex( j );
                        }
756
                        var_FreeList( &val2, &text2 );
757 758 759 760 761 762 763

                        CONNECT( combobox, currentIndexChanged( int ), this,
                                 ValueChange( int ) );
                        hlayout->addWidget( combobox );
                    }
                    else
                    {
764
                        QSlider *slider = new QSlider( box );
765
                        slider->setObjectName( qfu( psz_var ) );
766 767 768 769
                        slider->setOrientation( Qt::Horizontal );
                        vlc_value_t val2;
                        var_Change( p_obj, psz_var, VLC_VAR_GETMIN,
                                    &val2, NULL );
770 771
                        if( val2.i_int < INT_MIN )
                            val2.i_int = INT_MIN; /* FIXME */
772 773 774
                        slider->setMinimum( val2.i_int );
                        var_Change( p_obj, psz_var, VLC_VAR_GETMAX,
                                    &val2, NULL );
775 776
                        if( val2.i_int > INT_MAX )
                            val2.i_int = INT_MAX; /* FIXME */
777
                        slider->setMaximum( val2.i_int );
778 779 780
                        if( !var_Change( p_obj, psz_var, VLC_VAR_GETSTEP,
                                         &val2, NULL ) )
                            slider->setSingleStep( val2.i_int );
781 782 783 784 785 786 787 788 789 790
                        slider->setValue( i_val );
                        CONNECT( slider, valueChanged( int ), this,
                                 ValueChange( int ) );
                        hlayout->addWidget( slider );
                    }
                    layout->addLayout( hlayout );
                    break;
                }
                case VLC_VAR_BOOL:
                {
791
                    QCheckBox *button = new QCheckBox( name, box );
792
                    button->setObjectName( qfu( psz_var ) );
793 794 795 796 797 798 799 800 801
                    button->setChecked( var_GetBool( p_obj, psz_var ) );

                    CONNECT( button, clicked( bool ), this,
                             ValueChange( bool ) );
                    layout->addWidget( button );
                    break;
                }
                case VLC_VAR_VOID:
                {
802 803
                    if( i_type & VLC_VAR_ISCOMMAND )
                    {
804
                        QPushButton *button = new QPushButton( name, box );
805
                        button->setObjectName( qfu( psz_var ) );
806

807 808 809 810 811 812
                        CONNECT( button, clicked( bool ), this,
                                 ValueChange( bool ) );
                        layout->addWidget( button );
                    }
                    else
                    {
813
                        QLabel *label = new QLabel( name, box );
814 815
                        layout->addWidget( label );
                    }
816 817 818 819 820 821 822
                    break;
                }
                default:
                    msg_Warn( p_intf, "Unhandled var type for %s", psz_var );
                    break;
            }
        }
823
        var_FreeList( &val, &text );
824 825 826 827 828
        vlc_object_release( p_obj );
    }
    else
    {
        msg_Dbg( p_intf, "Couldn't find v4l2 instance" );
829 830 831
        help->show();
        if ( isVisible() )
            QTimer::singleShot( 2000, this, SLOT(Refresh()) );
832 833 834 835 836 837 838 839 840 841 842
    }
}

void ExtV4l2::ValueChange( bool value )
{
    ValueChange( (int)value );
}

void ExtV4l2::ValueChange( int value )
{
    QObject *s = sender();
843
    vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( THEPL, "v4l2" );
844 845
    if( p_obj )
    {
846 847
        QString var = s->objectName();
        int i_type = var_Type( p_obj, qtu( var ) );
848 849 850 851 852 853 854 855
        switch( i_type & VLC_VAR_TYPE )
        {
            case VLC_VAR_INTEGER:
                if( i_type & VLC_VAR_HASCHOICE )
                {
                    QComboBox *combobox = qobject_cast<QComboBox*>( s );
                    value = combobox->itemData( value ).toInt();
                }
856
                var_SetInteger( p_obj, qtu( var ), value );
857 858
                break;
            case VLC_VAR_BOOL:
859
                var_SetBool( p_obj, qtu( var ), value );
860 861
                break;
            case VLC_VAR_VOID:
862
                var_TriggerCallback( p_obj, qtu( var ) );
863 864 865 866 867 868 869 870 871 872 873
                break;
        }
        vlc_object_release( p_obj );
    }
    else
    {
        msg_Warn( p_intf, "Oops, v4l2 object isn't available anymore" );
        Refresh();
    }
}

874 875 876 877
/**********************************************************************
 * Sliders
 **********************************************************************/

878 879
FilterSliderData::FilterSliderData( QObject *parent, QSlider *_slider ) :
    QObject( parent ), slider( _slider )
880 881 882
{
    b_save_to_config = false;
}
883

884 885 886 887 888
FilterSliderData::FilterSliderData( QObject *parent,
                                    intf_thread_t *_p_intf,
                                    QSlider *_slider,
                                    QLabel *_label, QLabel *_nameLabel,
                                    const slider_data_t *_p_data ):
889 890
    QObject( parent ), slider( _slider ), valueLabel( _label ),
    nameLabel( _nameLabel ), p_data( _p_data ), p_intf( _p_intf )
891
{
892
    b_save_to_config = false;
893 894
    slider->setMinimum( p_data->f_min / p_data->f_resolution );
    slider->setMaximum( p_data->f_max / p_data->f_resolution );
895
    nameLabel->setText( p_data->descs );
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
    CONNECT( slider, valueChanged( int ), this, updateText( int ) );
    setValue( initialValue() );
    /* In case current == min|max text would not be first updated */
    if ( slider->value() == slider->maximum() ||
         slider->value() == slider->minimum() )
        updateText( slider->value() );
    CONNECT( slider, valueChanged( int ), this, onValueChanged( int ) );
}

void FilterSliderData::setValue( float f )
{
    slider->setValue( f / p_data->f_resolution );
}

void FilterSliderData::updateText( int i )
{
912
    float f = ((float) i) * p_data->f_resolution * p_data->f_visual_multiplier;
913
    valueLabel->setText( QString( p_data->units )
914
                    .prepend( "%1 " )
915 916 917 918 919 920 921 922 923
                    .arg( QString::number( f, 'f', 1 ) ) );
}

float FilterSliderData::initialValue()
{
    vlc_object_t *p_aout = (vlc_object_t *) THEMIM->getAout();
    float f = p_data->f_value;
    if( p_aout )
    {
924
        if ( var_Type( p_aout, qtu(p_data->name) ) == 0 )
925 926 927 928 929 930
        {
            vlc_object_release( p_aout );
            /* Not found, will try in config */
        }
        else
        {
931
            f = var_GetFloat( p_aout, qtu(p_data->name) );
932 933 934 935 936
            vlc_object_release( p_aout );
            return f;
        }
    }

937
    if ( ! config_FindConfig( VLC_OBJECT(p_intf), qtu(p_data->name) ) )
938 939
        return f;

940
    f = config_GetFloat( p_intf, qtu(p_data->name) );
941 942 943
    return f;
}

944
void FilterSliderData::onValueChanged( int i ) const
945 946 947 948 949
{
    float f = ((float) i) * p_data->f_resolution;
    vlc_object_t *p_aout = (vlc_object_t *) THEMIM->getAout();
    if ( p_aout )
    {
950
        var_SetFloat( p_aout, qtu(p_data->name),