extended_panels.cpp 55.1 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>
42
#include <QRegExp>
Clément Stenac's avatar
Clément Stenac committed
43

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

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

56 57 58 59 60 61 62 63 64 65 66
static bool filterIsPresent( const QString &filters, const QString &filter )
{
    QStringList list = filters.split( ':', QString::SplitBehavior::SkipEmptyParts );
    foreach( const QString &filterCmp, list )
    {
        if( filterCmp.compare( filter ) == 0 )
            return true;
    }
    return false;
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
static const char* GetVFilterType( struct intf_thread_t *p_intf, const char *psz_name )
{
    module_t *p_obj = module_find( psz_name );
    if( !p_obj )
    {
        msg_Err( p_intf, "Unable to find filter module \"%s\".", psz_name );
        return NULL;
    }

    if( module_provides( p_obj, "video splitter" ) )
        return "video-splitter";
    else if( module_provides( p_obj, "video filter" ) )
        return "video-filter";
    else if( module_provides( p_obj, "sub source" ) )
        return "sub-source";
    else if( module_provides( p_obj, "sub filter" ) )
        return "sub-filter";
    else
    {
        msg_Err( p_intf, "Unknown video filter type." );
        return NULL;
    }
}
90

91
static const QString ModuleFromWidgetName( QObject *obj )
92
{
93
    return obj->objectName().replace( "Enable","" );
94 95
}

96
static QString OptionFromWidgetName( QObject *obj )
97 98
{
    /* Gruik ? ... nah */
99 100 101 102
    return obj->objectName()
        .remove( QRegExp( "Slider|Combo|Dial|Check|Spin|Text" ) )
        .replace( QRegExp( "([A-Z])" ), "-\\1" )
        .toLower();
103
}
Clément Stenac's avatar
Clément Stenac committed
104

105
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
106
{
107 108 109 110 111 112 113 114
    const char *psz_filter_type = GetVFilterType( p_intf, psz_name );
    if( psz_filter_type == NULL )
        return;

    char *psz_filters = var_InheritString( THEPL, psz_filter_type );
    if( psz_filters == NULL )
        return;

115 116
    QCheckBox *checkbox = qobject_cast<QCheckBox*>( widget );
    QGroupBox *groupbox = qobject_cast<QGroupBox*>( widget );
117
    if( filterIsPresent( qfu(psz_filters), qfu(psz_name) ) )
118 119 120 121 122 123 124 125 126
    {
        if( checkbox ) checkbox->setChecked( true ); \
        else if (groupbox) groupbox->setChecked( true ); \
    }
    else
    {
        if( checkbox ) checkbox->setChecked( false );
        else if (groupbox) groupbox->setChecked( false );
    }
127
    free( psz_filters );
128
}
Clément Stenac's avatar
Clément Stenac committed
129

130
#define SETUP_VFILTER( widget ) \
131
    setup_vfilter( p_intf, #widget, ui.widget##Enable ); \
132
    CONNECT( ui.widget##Enable, clicked(), this, updateFilters() );
133

134
#define SETUP_VFILTER_OPTION( widget, signal ) \
135
    initComboBoxItems( ui.widget ); \
136 137
    setWidgetValue( ui.widget ); \
    CONNECT( ui.widget, signal, this, updateFilterOptions() );
Antoine Cellerier's avatar
Antoine Cellerier committed
138

139 140 141 142 143
ExtVideo::ExtVideo( intf_thread_t *_p_intf, QTabWidget *_parent ) :
            QObject( _parent ), p_intf( _p_intf )
{
    ui.setupUi( _parent );

144
    SETUP_VFILTER( adjust )
145 146 147 148 149 150
    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 ) )
151 152

    SETUP_VFILTER( extract )
153
    SETUP_VFILTER_OPTION( extractComponentText, textChanged( const QString& ) )
154

155 156
    SETUP_VFILTER( posterize )

157
    SETUP_VFILTER( colorthres )
158
    SETUP_VFILTER_OPTION( colorthresColorText, textChanged( const QString& ) )
159 160
    SETUP_VFILTER_OPTION( colorthresSaturationthresSlider, valueChanged( int ) )
    SETUP_VFILTER_OPTION( colorthresSimilaritythresSlider, valueChanged( int ) )
161

162 163 164
    SETUP_VFILTER( sepia )
    SETUP_VFILTER_OPTION( sepiaIntensitySpin, valueChanged( int ) )

165 166 167
    SETUP_VFILTER( invert )

    SETUP_VFILTER( gradient )
168 169 170
    SETUP_VFILTER_OPTION( gradientModeCombo, currentIndexChanged( QString ) )
    SETUP_VFILTER_OPTION( gradientTypeCheck, stateChanged( int ) )
    SETUP_VFILTER_OPTION( gradientCartoonCheck, stateChanged( int ) )
171

172
    SETUP_VFILTER( motionblur )
173
    SETUP_VFILTER_OPTION( blurFactorSlider, valueChanged( int ) )
174 175 176 177 178 179

    SETUP_VFILTER( motiondetect )

    SETUP_VFILTER( psychedelic )

    SETUP_VFILTER( sharpen )
180
    SETUP_VFILTER_OPTION( sharpenSigmaSlider, valueChanged( int ) )
181 182 183 184 185 186

    SETUP_VFILTER( ripple )

    SETUP_VFILTER( wave )

    SETUP_VFILTER( transform )
187
    SETUP_VFILTER_OPTION( transformTypeCombo, currentIndexChanged( QString ) )
188 189

    SETUP_VFILTER( rotate )
190
    SETUP_VFILTER_OPTION( rotateAngleDial, valueChanged( int ) )
191 192
    ui.rotateAngleDial->setWrapping( true );
    ui.rotateAngleDial->setNotchesVisible( true );
193 194

    SETUP_VFILTER( puzzle )
195 196
    SETUP_VFILTER_OPTION( puzzleRowsSpin, valueChanged( int ) )
    SETUP_VFILTER_OPTION( puzzleColsSpin, valueChanged( int ) )
197 198 199 200

    SETUP_VFILTER( magnify )

    SETUP_VFILTER( clone )
201
    SETUP_VFILTER_OPTION( cloneCountSpin, valueChanged( int ) )
202 203

    SETUP_VFILTER( wall )
204 205
    SETUP_VFILTER_OPTION( wallRowsSpin, valueChanged( int ) )
    SETUP_VFILTER_OPTION( wallColsSpin, valueChanged( int ) )
206

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

208 209
    SETUP_VFILTER( erase )
    SETUP_VFILTER_OPTION( eraseMaskText, editingFinished() )
210 211
    SETUP_VFILTER_OPTION( eraseYSpin, valueChanged( int ) )
    SETUP_VFILTER_OPTION( eraseXSpin, valueChanged( int ) )
212
    BUTTONACT( ui.eraseBrowseBtn, browseEraseFile() );
213 214

    SETUP_VFILTER( marq )
215
    SETUP_VFILTER_OPTION( marqMarqueeText, textChanged( const QString& ) )
216
    SETUP_VFILTER_OPTION( marqPositionCombo, currentIndexChanged( QString ) )
217 218 219

    SETUP_VFILTER( logo )
    SETUP_VFILTER_OPTION( logoFileText, editingFinished() )
220 221
    SETUP_VFILTER_OPTION( logoYSpin, valueChanged( int ) )
    SETUP_VFILTER_OPTION( logoXSpin, valueChanged( int ) )
222
    SETUP_VFILTER_OPTION( logoOpacitySlider, valueChanged( int ) )
223
    BUTTONACT( ui.logoBrowseBtn, browseLogo() );
224

225 226 227
    SETUP_VFILTER( gradfun )
    SETUP_VFILTER_OPTION( gradfunRadiusSlider, valueChanged( int ) )

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
228 229 230
    SETUP_VFILTER( grain )
    SETUP_VFILTER_OPTION( grainVarianceSlider, valueChanged( int ) )

231 232 233
    SETUP_VFILTER( mirror )

    SETUP_VFILTER( gaussianblur )
234
    SETUP_VFILTER_OPTION( gaussianblurSigmaSlider, valueChanged( int ) )
235 236 237 238

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

239 240 241 242 243 244
    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 ) )

245

246 247
    SETUP_VFILTER( anaglyph )

248 249
#undef SETUP_VFILTER
#undef SETUP_VFILTER_OPTION
250 251 252 253 254

    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
255 256
    CONNECT( ui.leftRightCropSync, toggled ( bool ), this, cropChange() );
    CONNECT( ui.topBotCropSync, toggled ( bool ), this, cropChange() );
257 258 259 260
    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
261 262
}

263 264
void ExtVideo::cropChange()
{
Dylan Yudaken's avatar
Dylan Yudaken committed
265 266 267 268 269
    if( ui.topBotCropSync->isChecked() )
        ui.cropBotPx->setValue( ui.cropTopPx->value() );
    if( ui.leftRightCropSync->isChecked() )
        ui.cropRightPx->setValue( ui.cropLeftPx->value() );

270 271
    QVector<vout_thread_t*> p_vouts = THEMIM->getVouts();
    foreach( vout_thread_t *p_vout, p_vouts )
272
    {
273 274 275 276
        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() );
277
        vlc_object_release( p_vout );
278 279 280
    }
}

281 282 283 284 285 286 287 288
void ExtVideo::clean()
{
    ui.cropTopPx->setValue( 0 );
    ui.cropBotPx->setValue( 0 );
    ui.cropLeftPx->setValue( 0 );
    ui.cropRightPx->setValue( 0 );
}

289
static QString 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
290
{
291
    char* psz_chain = var_GetString( THEPL, psz_filter_type );
292

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

296 297
    if( b_add ) list << psz_name;
    else        list.removeAll( psz_name );
Clément Stenac's avatar
Clément Stenac committed
298

299
    free( psz_chain );
Clément Stenac's avatar
Clément Stenac committed
300

KO Myung-Hun's avatar
KO Myung-Hun committed
301
    return list.join( ":" );
302 303
}

304 305
static void UpdateVFiltersString( struct intf_thread_t *p_intf,
                                  const char *psz_filter_type, const char *value )
306
{
307
    var_SetString( THEPL, psz_filter_type, value );
308 309 310

    /* Try to set non splitter filters on the fly */
    if( strcmp( psz_filter_type, "video-splitter" ) )
311
    {
312 313
        QVector<vout_thread_t*> p_vouts = THEMIM->getVouts();
        foreach( vout_thread_t *p_vout, p_vouts )
314
        {
315
            var_SetString( p_vout, psz_filter_type, value );
316 317
            vlc_object_release( p_vout );
        }
Clément Stenac's avatar
Clément Stenac committed
318 319 320
    }
}

321 322 323 324 325 326 327 328
void ExtVideo::changeVFiltersString( const char *psz_name, bool b_add )
{
    const char *psz_filter_type = GetVFilterType( p_intf, psz_name );
    if( psz_filter_type == NULL )
        return;

    QString result = ChangeFiltersString( p_intf, psz_filter_type, psz_name, b_add );

329
    emit configChanged( qfu( psz_filter_type ), result );
330 331 332 333

    UpdateVFiltersString( p_intf, psz_filter_type, qtu( result ) );
}

Clément Stenac's avatar
Clément Stenac committed
334 335
void ExtVideo::updateFilters()
{
336
    QString module = ModuleFromWidgetName( sender() );
Clément Stenac's avatar
Clément Stenac committed
337

338 339
    QCheckBox *checkbox = qobject_cast<QCheckBox*>( sender() );
    QGroupBox *groupbox = qobject_cast<QGroupBox*>( sender() );
340

341
    changeVFiltersString( qtu( module ),
342 343
                          checkbox ? checkbox->isChecked()
                                   : groupbox->isChecked() );
Clément Stenac's avatar
Clément Stenac committed
344 345
}

346 347 348 349 350 351
#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& ) ) );

352 353 354 355
void ExtVideo::browseLogo()
{
    QString file = QFileDialog::getOpenFileName( NULL, qtr( "Logo filenames" ),
                   p_intf->p_sys->filepath, "Images (*.png *.jpg);;All (*)" );
356 357

    UPDATE_AND_APPLY_TEXT( logoFileText, file );
358 359 360 361 362 363
}

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

    UPDATE_AND_APPLY_TEXT( eraseMaskText, file );
366 367
}

368 369
#undef UPDATE_AND_APPLY_TEXT

370 371
void ExtVideo::initComboBoxItems( QObject *widget )
{
372
    QComboBox *combobox = qobject_cast<QComboBox*>( widget );
373
    if( !combobox ) return;
374

375
    QString option = OptionFromWidgetName( widget );
376
    module_config_t *p_item = config_FindConfig( VLC_OBJECT( p_intf ),
377
                                                 qtu( option ) );
378
    if( p_item == NULL )
379
    {
380 381 382 383 384 385 386 387 388 389 390 391
        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++ )
392
        {
393
            combobox->addItem( qtr( texts[i] ), qlonglong(values[i]) );
394
            free( texts[i] );
395
        }
396 397
        free( texts );
        free( values );
398
    }
399
    else if( p_item->i_type == CONFIG_ITEM_STRING )
400
    {
401 402 403 404 405 406
        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++ )
        {
407
            combobox->addItem( qtr( texts[i] ), qfu(values[i]) );
408 409 410 411 412
            free( texts[i] );
            free( values[i] );
        }
        free( texts );
        free( values );
413 414 415
    }
}

416 417 418 419 420 421 422 423
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;

    vlc_value_t val;
424 425
    int i_type = config_GetType( p_intf, qtu( option ) ) & VLC_VAR_CLASS;
    switch( i_type )
426
    {
427 428 429 430 431 432 433 434 435 436
        case VLC_VAR_INTEGER:
        case VLC_VAR_BOOL:
        case VLC_VAR_FLOAT:
        case VLC_VAR_STRING:
            break;
        default:
            msg_Err( p_intf,
                     "Module %s's %s variable is of an unsupported type ( %d )",
                     qtu( module ), qtu( option ), i_type );
            return;
437
    }
438 439 440 441
    if( var_Create( THEPL, qtu( option ), i_type | VLC_VAR_DOINHERIT ) )
        return;
    if( var_GetChecked( THEPL, qtu( option ), i_type, &val ) )
        return;
442 443 444

    /* Try to cast to all the widgets we're likely to encounter. Only
     * one of the casts is expected to work. */
445 446 447 448
    QSlider        *slider        = qobject_cast<QSlider*>       ( widget );
    QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( widget );
    QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( widget );
    QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( widget );
449
    VLCQDial       *dial          = qobject_cast<VLCQDial*>      ( widget );
450 451
    QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( widget );
    QComboBox      *combobox      = qobject_cast<QComboBox*>     ( widget );
452 453 454 455 456 457 458

    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 );
459
        else if( dial )     dial->setValue( (360 - val.i_int) % 360 );
460 461 462
        else if( lineedit )
        {
            char str[30];
Tristan Matthews's avatar
Tristan Matthews committed
463
            snprintf( str, sizeof(str), "%06" PRIX64, val.i_int );
464 465
            lineedit->setText( str );
        }
466
        else if( combobox ) combobox->setCurrentIndex(
467
                            combobox->findData( qlonglong(val.i_int) ) );
468
        else msg_Warn( p_intf, "Could not find the correct Integer widget" );
469 470 471
    }
    else if( i_type == VLC_VAR_FLOAT )
    {
472 473
        if( slider ) slider->setValue( ( int )( val.f_float*( double )slider->tickInterval() ) ); /* hack alert! */
        else if( doublespinbox ) doublespinbox->setValue( val.f_float );
474
        else if( dial ) dial->setValue( (360 - lroundf(val.f_float)) % 360 );
475
        else msg_Warn( p_intf, "Could not find the correct Float widget" );
476 477 478
    }
    else if( i_type == VLC_VAR_STRING )
    {
479
        if( lineedit ) lineedit->setText( qfu( val.psz_string ) );
480
        else if( combobox ) combobox->setCurrentIndex(
481
                            combobox->findData( qfu( val.psz_string ) ) );
482
        else msg_Warn( p_intf, "Could not find the correct String widget" );
483 484 485 486
        free( val.psz_string );
    }
}

487
void ExtVideo::setFilterOption( const char *psz_module, const char *psz_option,
488
        int i_int, double f_float, const char *psz_string )
489
{
490 491 492
    QVector<vout_thread_t*> p_vouts = THEMIM->getVouts();
    int i_type = 0;
    bool b_is_command = false;
493

494
    if( !p_vouts.isEmpty() )
495
    {
496
        i_type = var_Type( p_vouts.at(0), psz_option );
497
        b_is_command = ( i_type & VLC_VAR_ISCOMMAND );
498
    }
499 500
    if( i_type == 0 )
        i_type = config_GetType( p_intf, psz_option );
501

502
    vlc_value_t val;
503
    i_type &= VLC_VAR_CLASS;
504 505
    if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
    {
506
        emit configChanged( qfu( psz_option ), QVariant( i_int ) );
507
        if( i_type == VLC_VAR_INTEGER )
508
        {
509
            val.i_int = i_int;
510 511
            var_SetInteger( THEPL, psz_option, i_int );
        }
512
        else
513 514
        {
            var_SetBool( THEPL, psz_option, i_int );
515
            val.b_bool = i_int;
516
        }
517 518 519
    }
    else if( i_type == VLC_VAR_FLOAT )
    {
520
        emit configChanged( qfu( psz_option ), QVariant( f_float ) );
521
        var_SetFloat( THEPL, psz_option, f_float );
522
        val.f_float = f_float;
523 524 525
    }
    else if( i_type == VLC_VAR_STRING )
    {
526 527
        if( psz_string == NULL )
            psz_string = "";
528
        emit configChanged( qfu( psz_option ), QVariant( psz_string ) );
529
        var_SetString( THEPL, psz_option, psz_string );
530
        val.psz_string = (char *) psz_string;
531 532
    }
    else
533
    {
534
        msg_Err( p_intf,
535
                 "Module %s's %s variable is of an unsupported type ( %d )",
536 537
                 psz_module,
                 psz_option,
538
                 i_type );
539 540
        b_is_command = false;
    }
541

542
    if( b_is_command )
543 544 545 546 547 548 549 550 551 552 553
    {
        foreach( vout_thread_t *p_vout, p_vouts )
        {
            var_SetChecked( p_vout, psz_option, i_type, val );
#ifndef NDEBUG
            int i_cur_type = var_Type( p_vout, psz_option );
            assert( ( i_cur_type & VLC_VAR_CLASS ) == i_type );
            assert( !!( i_cur_type & VLC_VAR_ISCOMMAND ) == b_is_command );
#endif
        }
    }
554

555 556
    foreach( vout_thread_t *p_vout, p_vouts )
        vlc_object_release( p_vout );
557 558
}

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
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 ) {
588 589
        i_int = (360 - dial->value()) % 360;
        f_float = i_int;
590 591 592 593 594 595 596 597 598 599 600
    }
    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();
    }

601
    setFilterOption( qtu( module ), qtu( option ), i_int, f_float, qtu( val ) );
602 603
}

604 605 606 607 608
/**********************************************************************
 * v4l2 controls
 **********************************************************************/

ExtV4l2::ExtV4l2( intf_thread_t *_p_intf, QWidget *_parent )
609
    : QWidget( _parent ), p_intf( _p_intf ), box( NULL )
610
{
611 612 613 614 615 616
    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 );
617
    help->setWordWrap( true );
618 619
    layout->addWidget( help );
    setLayout( layout );
620 621
}

622 623 624 625 626 627
void ExtV4l2::showEvent( QShowEvent *event )
{
    QWidget::showEvent( event );
    Refresh();
}

628 629
void ExtV4l2::Refresh( void )
{
630
    vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( THEPL, "v4l2" );
631
    help->hide();
632 633
    if( box )
    {
634
        layout()->removeWidget( box );
635 636 637
        delete box;
        box = NULL;
    }
638 639
    if( p_obj )
    {
640
        vlc_value_t val, text;
641 642 643 644
        int i_ret = var_Change( p_obj, "controls", VLC_VAR_GETCHOICES,
                                &val, &text );
        if( i_ret < 0 )
        {
645
            msg_Err( p_intf, "Oops, v4l2 object doesn't have a 'controls' variable." );
646
            help->show();
647 648 649
            vlc_object_release( p_obj );
            return;
        }
650 651

        box = new QGroupBox( this );
652
        layout()->addWidget( box );
653 654 655
        QVBoxLayout *layout = new QVBoxLayout( box );
        box->setLayout( layout );

656 657
        for( int i = 0; i < val.p_list->i_count; i++ )
        {
658
            vlc_value_t vartext;
659
            const char *psz_var = text.p_list->p_values[i].psz_string;
660 661 662 663 664 665

            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
666
            msg_Dbg( p_intf, "v4l2 control \"%" PRIx64 "\": %s (%s)",
667
                     val.p_list->p_values[i].i_int, psz_var, qtu( name ) );
668 669 670 671 672 673

            int i_type = var_Type( p_obj, psz_var );
            switch( i_type & VLC_VAR_TYPE )
            {
                case VLC_VAR_INTEGER:
                {
674
                    QLabel *label = new QLabel( name, box );
675
                    QHBoxLayout *hlayout = new QHBoxLayout();
676 677 678 679
                    hlayout->addWidget( label );
                    int i_val = var_GetInteger( p_obj, psz_var );
                    if( i_type & VLC_VAR_HASCHOICE )
                    {
680
                        QComboBox *combobox = new QComboBox( box );
681
                        combobox->setObjectName( qfu( psz_var ) );
682 683 684 685 686 687 688 689

                        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
690
                                       qlonglong( val2.p_list->p_values[j].i_int) );
691 692 693
                            if( i_val == val2.p_list->p_values[j].i_int )
                                combobox->setCurrentIndex( j );
                        }
694
                        var_FreeList( &val2, &text2 );
695 696 697 698 699 700 701

                        CONNECT( combobox, currentIndexChanged( int ), this,
                                 ValueChange( int ) );
                        hlayout->addWidget( combobox );
                    }
                    else
                    {
702
                        QSlider *slider = new QSlider( box );
703
                        slider->setObjectName( qfu( psz_var ) );
704 705 706 707
                        slider->setOrientation( Qt::Horizontal );
                        vlc_value_t val2;
                        var_Change( p_obj, psz_var, VLC_VAR_GETMIN,
                                    &val2, NULL );
708 709
                        if( val2.i_int < INT_MIN )
                            val2.i_int = INT_MIN; /* FIXME */
710 711 712
                        slider->setMinimum( val2.i_int );
                        var_Change( p_obj, psz_var, VLC_VAR_GETMAX,
                                    &val2, NULL );
713 714
                        if( val2.i_int > INT_MAX )
                            val2.i_int = INT_MAX; /* FIXME */
715
                        slider->setMaximum( val2.i_int );
716 717 718
                        if( !var_Change( p_obj, psz_var, VLC_VAR_GETSTEP,
                                         &val2, NULL ) )
                            slider->setSingleStep( val2.i_int );
719 720 721 722 723 724 725 726 727 728
                        slider->setValue( i_val );
                        CONNECT( slider, valueChanged( int ), this,
                                 ValueChange( int ) );
                        hlayout->addWidget( slider );
                    }
                    layout->addLayout( hlayout );
                    break;
                }
                case VLC_VAR_BOOL:
                {
729
                    QCheckBox *button = new QCheckBox( name, box );
730
                    button->setObjectName( qfu( psz_var ) );
731 732 733 734 735 736 737 738 739
                    button->setChecked( var_GetBool( p_obj, psz_var ) );

                    CONNECT( button, clicked( bool ), this,
                             ValueChange( bool ) );
                    layout->addWidget( button );
                    break;
                }
                case VLC_VAR_VOID:
                {
740 741
                    if( i_type & VLC_VAR_ISCOMMAND )
                    {
742
                        QPushButton *button = new QPushButton( name, box );
743
                        button->setObjectName( qfu( psz_var ) );
744

745 746 747 748 749 750
                        CONNECT( button, clicked( bool ), this,
                                 ValueChange( bool ) );
                        layout->addWidget( button );
                    }
                    else
                    {
751
                        QLabel *label = new QLabel( name, box );
752 753
                        layout->addWidget( label );
                    }
754 755 756 757 758 759 760
                    break;
                }
                default:
                    msg_Warn( p_intf, "Unhandled var type for %s", psz_var );
                    break;
            }
        }
761
        var_FreeList( &val, &text );
762 763 764 765 766
        vlc_object_release( p_obj );
    }
    else
    {
        msg_Dbg( p_intf, "Couldn't find v4l2 instance" );
767 768 769
        help->show();
        if ( isVisible() )
            QTimer::singleShot( 2000, this, SLOT(Refresh()) );
770 771 772 773 774 775 776 777 778 779 780
    }
}

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

void ExtV4l2::ValueChange( int value )
{
    QObject *s = sender();
781
    vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( THEPL, "v4l2" );
782 783
    if( p_obj )
    {
784 785
        QString var = s->objectName();
        int i_type = var_Type( p_obj, qtu( var ) );
786 787 788 789 790 791 792 793
        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();
                }
794
                var_SetInteger( p_obj, qtu( var ), value );
795 796
                break;
            case VLC_VAR_BOOL:
797
                var_SetBool( p_obj, qtu( var ), value );
798 799
                break;
            case VLC_VAR_VOID:
800
                var_TriggerCallback( p_obj, qtu( var ) );
801 802 803 804 805 806 807 808 809 810 811
                break;
        }
        vlc_object_release( p_obj );
    }
    else
    {
        msg_Warn( p_intf, "Oops, v4l2 object isn't available anymore" );
        Refresh();
    }
}

812 813 814 815
/**********************************************************************
 * Sliders
 **********************************************************************/

816 817
FilterSliderData::FilterSliderData( QObject *parent, QSlider *_slider ) :
    QObject( parent ), slider( _slider )
818 819
{
}
820

821 822 823 824 825
FilterSliderData::FilterSliderData( QObject *parent,
                                    intf_thread_t *_p_intf,
                                    QSlider *_slider,
                                    QLabel *_label, QLabel *_nameLabel,
                                    const slider_data_t *_p_data ):
826 827
    QObject( parent ), slider( _slider ), valueLabel( _label ),
    nameLabel( _nameLabel ), p_data( _p_data ), p_intf( _p_intf )
828 829 830
{
    slider->setMinimum( p_data->f_min / p_data->f_resolution );
    slider->setMaximum( p_data->f_max / p_data->f_resolution );
831
    nameLabel->setText( p_data->descs );
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
    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 )
{
848
    float f = ((float) i) * p_data->f_resolution * p_data->f_visual_multiplier;
849
    valueLabel->setText( QString( p_data->units )
850
                    .prepend( "%1 " )
851 852 853 854 855 856 857 858 859
                    .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 )
    {
860
        if ( var_Type( p_aout, qtu(p_data->name) ) == 0 )
861 862 863 864 865 866
        {
            vlc_object_release( p_aout );
            /* Not found, will try in config */
        }
        else
        {
867
            f = var_GetFloat( p_aout, qtu(p_data->name) );
868 869 870 871 872
            vlc_object_release( p_aout );
            return f;
        }
    }

873
    if ( ! config_FindConfig( VLC_OBJECT(p_intf), qtu(p_data->name) ) )
874 875
        return f;

876
    f = config_GetFloat( p_intf, qtu(p_data->name) );
877 878 879
    return f;
}

880
void FilterSliderData::onValueChanged( int i )
881 882 883 884 885
{
    float f = ((float) i) * p_data->f_resolution;
    vlc_object_t *p_aout = (vlc_object_t *) THEMIM->getAout();
    if ( p_aout )
    {
886
        var_SetFloat( p_aout, qtu(p_data->name), f );
887 888 889 890 891
        vlc_object_release( p_aout );
    }
    writeToConfig();
}

892
void FilterSliderData::writeToConfig()
893 894
{
    float f = ((float) slider->value()) * p_data->f_resolution;
895
    emit configChanged( p_data->name, QVariant( f ) );
896 897
}

898 899
AudioFilterControlWidget::AudioFilterControlWidget
( intf_thread_t *_p_intf, QWidget *parent, const char *_name ) :
900
    QWidget( parent ), p_intf( _p_intf ), name( _name ), i_smallfont(0)
901 902
{}

903 904 905 906 907 908
void AudioFilterControlWidget::connectConfigChanged( FilterSliderData *slider )
{
    connect( slider, SIGNAL( configChanged(QString, QVariant) ),
             this, SIGNAL( configChanged(QString, QVariant) ) );
}

909 910 911
void AudioFilterControlWidget::build()
{
    QFont smallFont = QApplication::font();
912
    smallFont.setPointSize( smallFont.pointSize() + i_smallfont );
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937

    QVBoxLayout *layout = new QVBoxLayout( this );
    slidersBox = new QGroupBox( qtr( "Enable" ) );
    slidersBox->setCheckable( true );
    layout->addWidget( slidersBox );

    QGridLayout *ctrlLayout = new QGridLayout( slidersBox );

    int i = 0;
    foreach( const FilterSliderData::slider_data_t &data, controls )
    {
        QSlider *slider = new QSlider( Qt::Vertical );
        QLabel *valueLabel = new QLabel();
        valueLabel->setFont( smallFont );
        valueLabel->setAlignment( Qt::AlignHCenter );
        QLabel *nameLabel = new QLabel();
        nameLabel->setFont( smallFont );
        nameLabel->setAlignment( Qt::AlignHCenter );
        FilterSliderData *filter =
            new FilterSliderData( this, p_intf,
                                  slider, valueLabel, nameLabel, & data );
        ctrlLayout->addWidget( slider, 0, i, Qt::AlignHCenter );
        ctrlLayout->addWidget( valueLabel, 1, i, Qt::AlignHCenter );
        ctrlLayout->addWidget( nameLabel, 2, i, Qt::AlignHCenter );
        i++;
938
        sliderDatas << filter;
939
        connectConfigChanged( filter );
940 941
    }

942
    char *psz_af = var_InheritString( THEPL, "audio-filter" );
943

944
    if( psz_af && filterIsPresent( qfu(psz_af), name ) )
945 946