profile_selector.cpp 29.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*****************************************************************************
 * profile_selector.cpp : A small profile selector and editor
 ****************************************************************************
 * Copyright (C) 2009 the VideoLAN team
 * $Id$
 *
 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
 *
 * 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
 * (at your option) any later version.
 *
 * 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.
 *****************************************************************************/

24 25

#include "components/sout/profile_selector.hpp"
26
#include "components/sout/profiles.hpp"
27 28 29 30 31 32 33
#include "dialogs/sout.hpp"

#include <QHBoxLayout>
#include <QToolButton>
#include <QComboBox>
#include <QLabel>
#include <QMessageBox>
34
#include <QRadioButton>
35 36 37
#include <QRegExp>
#include <QButtonGroup>
#include <QSpinBox>
38
#include <QUrl>
39
#include <QListWidgetItem>
40

41
#include <assert.h>
42
#include <vlc_modules.h>
43

44 45 46 47
#define CATPROP2NAME( val ) QString("valueholder_%1").arg( val )
#define CATANDPROP( cat, prop ) QString("%1_%2").arg( cat ).arg( prop )
#define OLDFORMAT "^\\w+;\\d+;\\d+;\\d+;"

48 49 50 51 52 53 54 55 56 57 58
VLCProfileSelector::VLCProfileSelector( QWidget *_parent ): QWidget( _parent )
{
    QHBoxLayout *layout = new QHBoxLayout( this );

    QLabel *prLabel = new QLabel( qtr( "Profile"), this );
    layout->addWidget( prLabel );

    profileBox = new QComboBox( this );
    layout->addWidget( profileBox );

    QToolButton *editButton = new QToolButton( this );
59
    editButton->setIcon( QIcon( ":/menu/preferences" ) );
60 61 62 63
    editButton->setToolTip( qtr( "Edit selected profile" ) );
    layout->addWidget( editButton );

    QToolButton *deleteButton = new QToolButton( this );
64
    deleteButton->setIcon( QIcon( ":/toolbar/clear" ) );
65 66 67 68
    deleteButton->setToolTip( qtr( "Delete selected profile" ) );
    layout->addWidget( deleteButton );

    QToolButton *newButton = new QToolButton( this );
69
    newButton->setIcon( QIcon( ":/new" ) );
70 71 72 73 74 75 76 77 78 79
    newButton->setToolTip( qtr( "Create a new profile" ) );
    layout->addWidget(newButton);

    BUTTONACT( newButton, newProfile() );
    BUTTONACT( editButton, editProfile() );
    BUTTONACT( deleteButton, deleteProfile() );
    fillProfilesCombo();

    CONNECT( profileBox, activated( int ),
             this, updateOptions( int ) );
80
    updateOptions( 0 );
81 82 83 84 85
}

inline void VLCProfileSelector::fillProfilesCombo()
{
    QSettings settings(
86
#ifdef _WIN32
87 88 89 90 91 92 93 94 95 96 97
            QSettings::IniFormat,
#else
            QSettings::NativeFormat,
#endif
            QSettings::UserScope, "vlc", "vlc-qt-interface" );

    int i_size = settings.beginReadArray( "codecs-profiles" );

    for( int i = 0; i < i_size; i++ )
    {
        settings.setArrayIndex( i );
98
        if( settings.value( "Profile-Name" ).toString().isEmpty() ) continue;
99 100 101
        profileBox->addItem( settings.value( "Profile-Name" ).toString(),
                settings.value( "Profile-Value" ) );
    }
102 103
    if( i_size == 0 )
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
104
        for( size_t i = 0; i < NB_PROFILE; i++ )
105 106 107 108 109
        {
            profileBox->addItem( video_profile_name_list[i],
                                 video_profile_value_list[i] );
        }
    }
110 111 112 113 114 115 116 117 118 119 120 121 122 123
    settings.endArray();
}

void VLCProfileSelector::newProfile()
{
    editProfile( "", "" );
}

void VLCProfileSelector::editProfile()
{
    editProfile( profileBox->currentText(),
                 profileBox->itemData( profileBox->currentIndex() ).toString() );
}

124
void VLCProfileSelector::editProfile( const QString& qs, const QString& value )
125
{
126
    /* Create the Profile Editor */
127 128
    VLCProfileEditor *editor = new VLCProfileEditor( qs, value, this );

129
    /* Show it */
130 131
    if( QDialog::Accepted == editor->exec() )
    {
132
        /* New Profile */
133 134
        if( qs.isEmpty() )
            profileBox->addItem( editor->name, QVariant( editor->transcodeValue() ) );
135
        /* Update old profile */
136 137
        else
        {
138
            /* Look for the profile */
139
            int i_profile = profileBox->findText( qs );
140
            assert( i_profile != -1 );
141
            profileBox->setItemText( i_profile, editor->name );
142
            profileBox->setItemData( i_profile, editor->transcodeValue() );
143
            /* Force mrl recreation */
144
            updateOptions( i_profile );
145 146 147 148 149 150 151 152 153 154 155
        }
    }
    delete editor;

    saveProfiles();
    emit optionsChanged();
}

void VLCProfileSelector::deleteProfile()
{
    profileBox->removeItem( profileBox->currentIndex() );
156
    saveProfiles();
157 158 159 160 161
}

void VLCProfileSelector::saveProfiles()
{
    QSettings settings(
162
#ifdef _WIN32
163 164 165 166 167 168
            QSettings::IniFormat,
#else
            QSettings::NativeFormat,
#endif
            QSettings::UserScope, "vlc", "vlc-qt-interface" );

169
    settings.remove( "codecs-profiles" ); /* Erase old profiles to be rewritten */
170 171 172 173 174 175 176 177 178 179 180
    settings.beginWriteArray( "codecs-profiles" );
    for( int i = 0; i < profileBox->count(); i++ )
    {
        settings.setArrayIndex( i );
        settings.setValue( "Profile-Name", profileBox->itemText( i ) );
        settings.setValue( "Profile-Value", profileBox->itemData( i ).toString() );
    }
    settings.endArray();
}

void VLCProfileSelector::updateOptions( int i )
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
{
    QString options = profileBox->itemData( i ).toString();
    QRegExp rx(OLDFORMAT);
    if ( !options.contains( ";" ) ) return;
    if ( rx.indexIn( options ) != -1 )
        return updateOptionsOldFormat( i );

    transcode = "";

    QStringList tuples = options.split( ";" );
    typedef QHash<QString, QString> proptovalueHashType;
    QHash<QString, proptovalueHashType *> categtopropHash;
    proptovalueHashType *proptovalueHash;
    QString value;

    /* Build a double hash structure because we need to make ordered lookups */
    foreach ( const QString &tuple, tuples )
    {
        QStringList keyvalue = tuple.split( "=" );
        if ( keyvalue.count() != 2 ) continue;
        QString key = keyvalue[0];
        value = keyvalue[1];
        keyvalue = key.split( "_" );
        if ( keyvalue.count() != 2 ) continue;
        QString categ = keyvalue[0];
        QString prop = keyvalue[1];

        if ( ! categtopropHash.contains( categ ) )
        {
            proptovalueHash = new proptovalueHashType();
            categtopropHash.insert( categ, proptovalueHash );
        } else {
            proptovalueHash = categtopropHash.value( categ );
        }
        proptovalueHash->insert( prop, value );
    }

    /* Now we can build the/translate into MRL */
#define HASHPICK( categ, prop ) \
    if ( categtopropHash.contains( categ ) ) \
    {\
        proptovalueHash = categtopropHash.value( categ );\
        value = proptovalueHash->take( prop );\
    }\
    else value = QString()

    SoutMrl smrl;
    smrl.begin( "transcode" );

    /* First muxer options */
    HASHPICK( "muxer", "mux" );
    if ( value.isEmpty() ) goto cleanup;
    mux = value;

    HASHPICK( "video", "enable" );
    if ( !value.isEmpty() )
    {
        HASHPICK( "video", "codec" );

        if ( !value.isEmpty() )
        {
            smrl.option( "vcodec", value );

            HASHPICK( "vcodec", "bitrate" );
245 246 247
            if ( value.toInt() > 0 )
            {
                smrl.option( "vb", value.toInt() );
248 249
            }

250 251 252
            HASHPICK( "video", "filters" );
            if ( !value.isEmpty() )
            {
253
                QStringList valuesList = QUrl::fromPercentEncoding( value.toLatin1() ).split( ";" );
254
                smrl.option( "vfilter", valuesList.join( ":" ) );
255 256
            }

257 258
            /*if ( codec is h264 )*/
            {
259 260 261 262 263 264 265 266 267
                /* special handling */
                QStringList codecoptions;

                HASHPICK( "vcodec", "qp" );
                if( value.toInt() > 0 )
                    codecoptions << QString( "qp=%1" ).arg( value );

                HASHPICK( "vcodec", "custom" );
                if( !value.isEmpty() )
268
                    codecoptions << QUrl::fromPercentEncoding( value.toLatin1() );
269

270 271 272
                if ( codecoptions.count() )
                    smrl.option( "venc",
                        QString("x264{%1}").arg( codecoptions.join(",") ) );
273
            }
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

            HASHPICK( "vcodec", "framerate" );
            if ( !value.isEmpty() && value.toInt() > 0 )
                smrl.option( "fps", value );

            HASHPICK( "vcodec", "scale" );
            if ( !value.isEmpty() )
                smrl.option( "scale", value );

            HASHPICK( "vcodec", "width" );
            if ( !value.isEmpty() && value.toInt() > 0 )
                smrl.option( "width", value );

            HASHPICK( "vcodec", "height" );
            if ( !value.isEmpty() && value.toInt() > 0 )
                smrl.option( "height", value );
        }
291 292
    } else {
        smrl.option( "vcodec", "none" );
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    }

    HASHPICK( "audio", "enable" );
    if ( !value.isEmpty() )
    {
        HASHPICK( "audio", "codec" );
        if ( !value.isEmpty() )
        {
            smrl.option( "acodec", value );

            HASHPICK( "acodec", "bitrate" );
            smrl.option( "ab", value.toInt() );

            HASHPICK( "acodec", "channels" );
            smrl.option( "channels", value.toInt() );

            HASHPICK( "acodec", "samplerate" );
            smrl.option( "samplerate", value.toInt() );
311 312 313 314

            HASHPICK( "audio", "filters" );
            if ( !value.isEmpty() )
            {
315
                QStringList valuesList = QUrl::fromPercentEncoding( value.toLatin1() ).split( ";" );
316
                smrl.option( "afilter", valuesList.join( ":" ) );
317 318
            }

319
        }
320 321
    } else {
        smrl.option( "acodec", "none" );
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    }

    HASHPICK( "subtitles", "enable" );
    if( !value.isEmpty() )
    {
        HASHPICK( "subtitles", "codec" );
        smrl.option( "scodec", value );

        HASHPICK( "subtitles", "overlay" );
        if ( !value.isEmpty() )
            smrl.option( "soverlay" );
    }
    smrl.end();
#undef HASHPICK

    transcode = smrl.getMrl();

    cleanup:
    /* Temp hash tables cleanup */
    foreach( proptovalueHashType *hash, categtopropHash )
        delete hash;

    emit optionsChanged();
}

void VLCProfileSelector::updateOptionsOldFormat( int i )
348 349
{
    QStringList options = profileBox->itemData( i ).toString().split( ";" );
350
    if( options.count() < 16 )
351 352 353 354 355
        return;

    mux = options[0];

    SoutMrl smrl;
356
    if( options[1].toInt() || options[2].toInt() || options[3].toInt() )
357 358 359 360 361 362
    {
        smrl.begin( "transcode" );

        if( options[1].toInt() )
        {
            smrl.option( "vcodec", options[4] );
363 364 365
            if( options[4] != "none" )
            {
                smrl.option( "vb", options[5].toInt() );
366 367
                if( !options[7].isEmpty() && options[7].toInt() > 0 )
                    smrl.option( "fps", options[7] );
368 369
                if( !options[6].isEmpty() )
                    smrl.option( "scale", options[6] );
370
                if( !options[8].isEmpty() && options[8].toInt() > 0 )
371
                    smrl.option( "width", options[8].toInt() );
372
                if( !options[9].isEmpty() && options[9].toInt() > 0 )
373
                    smrl.option( "height", options[9].toInt() );
374
            }
375 376 377 378 379
        }

        if( options[2].toInt() )
        {
            smrl.option( "acodec", options[10] );
380 381 382 383 384 385
            if( options[10] != "none" )
            {
                smrl.option( "ab", options[11].toInt() );
                smrl.option( "channels", options[12].toInt() );
                smrl.option( "samplerate", options[13].toInt() );
            }
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
        }

        if( options[3].toInt() )
        {
            smrl.option( "scodec", options[14] );
            if( options[15].toInt() )
                smrl.option( "soverlay" );
        }

        smrl.end();

        transcode = smrl.getMrl();
    }
    else
        transcode = "";
401
    emit optionsChanged();
402 403 404 405 406 407
}


/**
 * VLCProfileEditor
 **/
408
VLCProfileEditor::VLCProfileEditor( const QString& qs_name, const QString& value,
409 410 411 412
        QWidget *_parent )
                 : QVLCDialog( _parent, NULL )
{
    ui.setupUi( this );
413
    ui.buttonGroup->setObjectName( CATPROP2NAME( CATANDPROP( "muxer", "mux" ) ) );
414 415 416 417 418
    if( !qs_name.isEmpty() )
    {
        ui.profileLine->setText( qs_name );
        ui.profileLine->setReadOnly( true );
    }
419
    loadCapabilities();
420
    registerCodecs();
421
    registerFilters();
422

423 424
    QPushButton *saveButton = new QPushButton(
                ( qs_name.isEmpty() ) ? qtr( "Create" ) : qtr( "Save" ) );
425 426 427 428 429 430
    ui.buttonBox->addButton( saveButton, QDialogButtonBox::AcceptRole );
    BUTTONACT( saveButton, close() );
    QPushButton *cancelButton = new QPushButton( qtr( "Cancel" ) );
    ui.buttonBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
    BUTTONACT( cancelButton, reject() );

431 432 433 434
    CONNECT( ui.valueholder_video_copy, stateChanged( int ),
             this, activatePanels() );
    CONNECT( ui.valueholder_audio_copy, stateChanged( int ),
             this, activatePanels() );
435 436 437 438 439 440
    CONNECT( ui.valueholder_vcodec_bitrate, editingFinished( ),
             this, fixBirateState() );
    CONNECT( ui.valueholder_vcodec_qp, editingFinished( ),
             this, fixQPState() );
    CONNECT( ui.valueholder_video_codec, currentIndexChanged( int ),
             this, codecSelected() );
441 442
    reset();

443
    fillProfile( value );
444
    muxSelected();
445
    codecSelected();
446 447 448 449 450 451 452 453
}

void VLCProfileEditor::loadCapabilities()
{
    size_t count;
    module_t **p_all = module_list_get (&count);

    /* Parse the module list for capabilities and probe each of them */
454
    for (size_t i = 0; i < count; i++)
455
    {
456 457 458 459 460 461
         module_t *p_module = p_all[i];

         if( module_provides( p_module, "sout mux" ) )
             caps["muxers"].insert( module_get_object( p_module ) );
//        else if ( module_provides( p_module, "encoder" ) )
//            caps["encoders"].insert( module_get_object( p_module ) );
462 463
    }
    module_list_free (p_all);
464 465
}

466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
inline void VLCProfileEditor::registerFilters()
{
    size_t count;
    module_t **p_all = module_list_get (&count);

    for (size_t i = 0; i < count; i++)
    {
        module_t *p_module = p_all[i];
        if ( module_get_score( p_module ) > 0 ) continue;

        QString capability = module_get_capability( p_module );
        QListWidget *listWidget = NULL;
        QListWidgetItem *item;

        if ( capability == "video filter2" )
            listWidget = ui.valueholder_video_filters;
        else if ( capability == "audio filter" )
            listWidget = ui.valueholder_audio_filters;

        if ( !listWidget ) continue;

        item = new QListWidgetItem( module_get_name( p_module, true ) );
        item->setCheckState( Qt::Unchecked );
        item->setToolTip( QString( module_get_help( p_module ) ) );
        item->setData( Qt::UserRole, QString( module_get_object( p_module ) ) );
        listWidget->addItem( item );
    }
    module_list_free (p_all);

    ui.valueholder_video_filters->sortItems();
    ui.valueholder_audio_filters->sortItems();
}

499 500
inline void VLCProfileEditor::registerCodecs()
{
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
#define SETMUX( button, val,    vid, aud, men, sub, stream, chaps ) \
    ui.button->setProperty( "sout", val );\
    ui.button->setProperty( "capvideo", vid );\
    ui.button->setProperty( "capaudio", aud );\
    ui.button->setProperty( "capmenu", men );\
    ui.button->setProperty( "capsubs", sub );\
    ui.button->setProperty( "capstream", stream );\
    ui.button->setProperty( "capchaps", chaps );\
    CONNECT( ui.button, clicked(bool), this, muxSelected() );
    SETMUX( PSMux, "ps",        true, true, false, true, false, true )
    SETMUX( TSMux, "ts",        true, true, false, true, true, false )
    SETMUX( WEBMux, "webm",     true, true, false, false, true, false )
    SETMUX( MPEG1Mux, "mpeg1",  true, true, false, false, false, false )
    SETMUX( OggMux, "ogg",      true, true, false, false, true, true )
    SETMUX( ASFMux, "asf",      true, true, false, true, true, true )
    SETMUX( MOVMux, "mp4",      true, true, true, true, true, false )
    SETMUX( WAVMux, "wav",      false, true, false, false, false, false )
    SETMUX( RAWMux, "raw",      true, true, false, false, false, false )
    SETMUX( FLVMux, "flv",      true, true, false, false, true, false )
    SETMUX( MKVMux, "mkv",      true, true, true, true, true, true )
    SETMUX( AVIMux, "avi",      true, true, false, false, false, false )
    SETMUX( MJPEGMux, "mpjpeg", true, false, false, false, false, false )
523
#undef SETMUX
524

525 526
#define ADD_VCODEC( name, fourcc ) \
            ui.valueholder_video_codec->addItem( name, QVariant( fourcc ) );
527 528 529 530 531 532 533 534
    ADD_VCODEC( "MPEG-1", "mp1v" )
    ADD_VCODEC( "MPEG-2", "mp2v" )
    ADD_VCODEC( "MPEG-4", "mp4v" )
    ADD_VCODEC( "DIVX 1" , "DIV1" )
    ADD_VCODEC( "DIVX 2" , "DIV2" )
    ADD_VCODEC( "DIVX 3" , "DIV3" )
    ADD_VCODEC( "H-263", "H263" )
    ADD_VCODEC( "H-264", "h264" )
535
    ADD_VCODEC( "H-265", "hevc" )
Ilkka Ollakka's avatar
Ilkka Ollakka committed
536
    ADD_VCODEC( "VP8", "VP80" )
537 538 539 540 541
    ADD_VCODEC( "WMV1", "WMV1" )
    ADD_VCODEC( "WMV2" , "WMV2" )
    ADD_VCODEC( "M-JPEG", "MJPG" )
    ADD_VCODEC( "Theora", "theo" )
    ADD_VCODEC( "Dirac", "drac" )
Rémi Duraffort's avatar
Rémi Duraffort committed
542
#undef ADD_VCODEC
543 544
    /* can do quality */
    qpcodecsList << "h264";
545

546
#define ADD_ACODEC( name, fourcc ) ui.valueholder_audio_codec->addItem( name, QVariant( fourcc ) );
547 548 549 550 551 552 553 554
    ADD_ACODEC( "MPEG Audio", "mpga" )
    ADD_ACODEC( "MP3", "mp3" )
    ADD_ACODEC( "MPEG 4 Audio ( AAC )", "mp4a" )
    ADD_ACODEC( "A52/AC-3", "a52" )
    ADD_ACODEC( "Vorbis", "vorb" )
    ADD_ACODEC( "Flac", "flac" )
    ADD_ACODEC( "Speex", "spx" )
    ADD_ACODEC( "WAV", "s16l" )
555
    ADD_ACODEC( "WMA2", "wma2" )
Rémi Duraffort's avatar
Rémi Duraffort committed
556
#undef ADD_ACODEC
557

558
#define ADD_SCALING( factor ) ui.valueholder_vcodec_scale->addItem( factor );
559
    ADD_SCALING( qtr("Auto") );
560 561 562 563 564 565 566 567
    ADD_SCALING( "1" )
    ADD_SCALING( "0.25" )
    ADD_SCALING( "0.5" )
    ADD_SCALING( "0.75" )
    ADD_SCALING( "1.25" )
    ADD_SCALING( "1.5" )
    ADD_SCALING( "1.75" )
    ADD_SCALING( "2" )
Rémi Duraffort's avatar
Rémi Duraffort committed
568
#undef ADD_SCALING
569

570
#define ADD_SAMPLERATE( sample, val ) ui.valueholder_acodec_samplerate->addItem( sample, val );
571 572 573 574 575
    ADD_SAMPLERATE( "8000 Hz", 8000 )
    ADD_SAMPLERATE( "11025 Hz", 11025 )
    ADD_SAMPLERATE( "22050 Hz", 22050 )
    ADD_SAMPLERATE( "44100 Hz", 44100 )
    ADD_SAMPLERATE( "48000 Hz", 48000 )
Rémi Duraffort's avatar
Rémi Duraffort committed
576
#undef ADD_SAMPLERATE
577

578
#define ADD_SCODEC( name, fourcc ) ui.valueholder_subtitles_codec->addItem( name, QVariant( fourcc ) );
579 580
    ADD_SCODEC( "DVB subtitle", "dvbs" )
    ADD_SCODEC( "T.140", "t140" )
Rémi Duraffort's avatar
Rémi Duraffort committed
581
#undef ADD_SCODEC
582 583
}

584 585
void VLCProfileEditor::muxSelected()
{
586 587 588
    QRadioButton *current =
            qobject_cast<QRadioButton *>( ui.buttonGroup->checkedButton() );

589 590 591
#define SETYESNOSTATE( name, prop ) \
    ui.name->setChecked( current->property( prop ).toBool() )

592 593 594 595 596 597 598 599
    /* dumb :/ */
    SETYESNOSTATE( capvideo, "capvideo" );
    SETYESNOSTATE( capaudio, "capaudio" );
    SETYESNOSTATE( capmenu, "capmenu" );
    SETYESNOSTATE( capsubs, "capsubs" );
    SETYESNOSTATE( capstream, "capstream" );
    SETYESNOSTATE( capchaps, "capchaps" );
    bool b = caps["muxers"].contains( "mux_" + current->property("sout").toString() );
600
    if ( !b )
601 602 603 604 605 606 607 608
        ui.muxerwarning->setText(
                    QString( "<img src=\":/menu/info\"/> %1" )
                    .arg( qtr( "This muxer is not provided directly by VLC: It could be missing." ) )
                    );
    else
        ui.muxerwarning->setText("");
    return;

609 610 611
#undef SETYESNOSTATE
}

612 613 614 615 616 617 618 619
void VLCProfileEditor::codecSelected()
{
    /* Enable quality preset */
    QString currentcodec = ui.valueholder_video_codec->
            itemData(ui.valueholder_video_codec->currentIndex() ).toString();
    ui.valueholder_vcodec_qp->setEnabled( qpcodecsList.contains( currentcodec ) );
}

620
void VLCProfileEditor::fillProfile( const QString& qs )
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
{
    QRegExp rx(OLDFORMAT);
    if ( rx.indexIn( qs ) != -1 ) return fillProfileOldFormat( qs );

    QStringList tuples = qs.split( ";" );
    foreach ( const QString &tuple, tuples )
    {
        QStringList keyvalue = tuple.split( "=" );
        if ( keyvalue.count() != 2 ) continue;
        QString key = keyvalue[0];
        QString value = keyvalue[1];
        QObject *object = findChild<QObject *>( CATPROP2NAME( key ) );
        if ( object )
        {
            if( object->inherits( "QButtonGroup" ) )
            { /* Buttongroup for Radios */
                const QButtonGroup *group = qobject_cast<const QButtonGroup *>( object );
                foreach( QAbstractButton *button, group->buttons() )
                {
                    if ( button->property("sout").toString() == value )
                    {
                        button->setChecked( true );
                        break;/* radios are exclusive */
                    }
                }
            }
            else if( object->inherits( "QCheckBox" ) )
            {
                QCheckBox *box = qobject_cast<QCheckBox *>( object );
                box->setChecked( ! value.isEmpty() );
            }
652 653 654 655 656
            else if( object->inherits( "QGroupBox" ) )
            {
                QGroupBox *box = qobject_cast<QGroupBox *>( object );
                box->setChecked( ! value.isEmpty() );
            }
657 658 659 660 661 662 663 664 665 666 667 668 669 670
            else if( object->inherits( "QSpinBox" ) )
            {
                QSpinBox *box = qobject_cast<QSpinBox *>( object );
                box->setValue( value.toInt() );
            }
            else if( object->inherits( "QDoubleSpinBox" ) )
            {
                QDoubleSpinBox *box = qobject_cast<QDoubleSpinBox *>( object );
                box->setValue( value.toDouble() );
            }
            else if( object->inherits( "QComboBox" ) )
            {
                QComboBox *box = qobject_cast<QComboBox *>( object );
                box->setCurrentIndex( box->findData( value ) );
671 672
                if ( box->lineEdit() && box->currentIndex() == -1 )
                    box->lineEdit()->setText( value );
673
            }
674 675 676
            else if( object->inherits( "QLineEdit" ) )
            {
                QLineEdit *box = qobject_cast<QLineEdit *>( object );
677
                box->setText( QUrl::fromPercentEncoding( value.toLatin1() ) );
678
            }
679 680
            else if ( object->inherits( "QListWidget" ) )
            {
681
                QStringList valuesList = QUrl::fromPercentEncoding( value.toLatin1() ).split( ";" );
682 683 684 685 686 687 688 689 690 691
                const QListWidget *list = qobject_cast<const QListWidget *>( object );
                for( int i=0; i < list->count(); i++ )
                {
                    QListWidgetItem *item = list->item( i );
                    if ( valuesList.contains( item->data( Qt::UserRole ).toString() ) )
                        item->setCheckState( Qt::Checked );
                    else
                        item->setCheckState( Qt::Unchecked );
                }
            }
692 693 694 695 696
        }
    }
}

void VLCProfileEditor::fillProfileOldFormat( const QString& qs )
697 698
{
    QStringList options = qs.split( ";" );
699
    if( options.count() < 16 )
700 701
        return;

702
    const QString mux = options[0];
703 704 705 706 707 708 709 710 711 712 713
    for ( int i=0; i< ui.muxer->layout()->count(); i++ )
    {
        QRadioButton *current =
                qobject_cast<QRadioButton *>(ui.muxer->layout()->itemAt(i)->widget());
        if ( unlikely( !current ) ) continue;/* someone is messing up with ui */
        if ( current->property("sout").toString() == mux )
        {
            current->setChecked( true );
            break;/* radios are exclusive */
        }
    }
714

715 716 717 718 719
    ui.valueholder_video_copy->setChecked( !options[1].toInt() );
    ui.valueholder_video_enable->setChecked( ( options[4] != "none" ) );
    ui.valueholder_audio_copy->setChecked( !options[2].toInt() );
    ui.valueholder_audio_enable->setChecked( ( options[10] != "none" ) );
    ui.valueholder_subtitles_enable->setChecked( options[3].toInt() );
720

721 722
    ui.valueholder_video_codec->setCurrentIndex( ui.valueholder_video_codec->findData( options[4] ) );
    ui.valueholder_vcodec_bitrate->setValue( options[5].toInt() );
723
    if ( options[6].toInt() > 0 )
724
        ui.valueholder_vcodec_scale->setEditText( options[6] );
725
    else
726 727 728 729
        ui.valueholder_vcodec_scale->setCurrentIndex( 0 );
    ui.valueholder_vcodec_framerate->setValue( options[7].toDouble() );
    ui.valueholder_vcodec_width->setValue( options[8].toInt() );
    ui.valueholder_vcodec_height->setValue( options[9].toInt() );
730

731 732 733
    ui.valueholder_audio_codec->setCurrentIndex( ui.valueholder_audio_codec->findData( options[10] ) );
    ui.valueholder_acodec_bitrate->setValue( options[11].toInt() );
    ui.valueholder_acodec_channels->setValue( options[12].toInt() );
734

735 736 737
    int index = ui.valueholder_acodec_samplerate->findData( options[13] );
    if ( index == -1 ) index = ui.valueholder_acodec_samplerate->findData( 44100 );
    ui.valueholder_acodec_samplerate->setCurrentIndex( index );
738

739 740
    ui.valueholder_subtitles_codec->setCurrentIndex( ui.valueholder_subtitles_codec->findData( options[14] ) );
    ui.valueholder_subtitles_overlay->setChecked( options[15].toInt() );
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
}

void VLCProfileEditor::close()
{
    if( ui.profileLine->text().isEmpty() )
    {
        QMessageBox::warning( this, qtr(" Profile Name Missing" ),
                qtr( "You must set a name for the profile." ) );
        ui.profileLine->setFocus();
        return;
    }
    name = ui.profileLine->text();

    accept();
}

757 758
#define currentData( box ) box->itemData( box->currentIndex() )

759 760
QString VLCProfileEditor::transcodeValue()
{
761 762 763 764
    QList<QObject *> allwidgets = findChildren<QObject *>();
    QStringList configuration;

    foreach( const QObject *object, allwidgets )
765
    {
766 767 768 769 770 771 772 773 774 775 776 777 778
        if ( ! object->objectName().startsWith( CATPROP2NAME( "" ) ) )
            continue;
        if ( object->inherits( "QWidget" ) &&
             ! qobject_cast<const QWidget *>(object)->isEnabled() ) continue;

        QString name = object->objectName();
        QStringList vals = object->objectName().split( "_" );
        if ( vals.count() != 3 ) continue;
        QString &categ = vals[1];
        QString &prop = vals[2];
        QString value;

        if( object->inherits( "QButtonGroup" ) )
779
        {
780 781 782 783 784 785 786
            const QButtonGroup *group = qobject_cast<const QButtonGroup *>( object );
            value = group->checkedButton()->property( "sout" ).toString();
        }
        else if( object->inherits( "QCheckBox" ) )
        {
            const QCheckBox *box = qobject_cast<const QCheckBox *>( object );
            value = box->isChecked() ? "yes" : "";
787
        }
788 789 790 791 792
        else if( object->inherits( "QGroupBox" ) )
        {
            const QGroupBox *box = qobject_cast<const QGroupBox *>( object );
            value = box->isChecked() ? "yes" : "";
        }
793 794 795 796 797 798 799 800 801 802 803 804 805 806
        else if( object->inherits( "QSpinBox" ) )
        {
            const QSpinBox *box = qobject_cast<const QSpinBox *>( object );
            value = QString::number( box->value() );
        }
        else if( object->inherits( "QDoubleSpinBox" ) )
        {
            const QDoubleSpinBox *box = qobject_cast<const QDoubleSpinBox *>( object );
            value =  QString::number( box->value() );
        }
        else if( object->inherits( "QComboBox" ) )
        {
            const QComboBox *box = qobject_cast<const QComboBox *>( object );
            value = currentData( box ).toString();
807
            if ( value.isEmpty() && box->lineEdit() ) value = box->lineEdit()->text();
808
        }
809 810 811 812 813
        else if( object->inherits( "QLineEdit" ) )
        {
            const QLineEdit *box = qobject_cast<const QLineEdit *>( object );
            value = QUrl::toPercentEncoding( box->text(), "", "_;" );
        }
814 815 816 817 818 819 820 821 822 823 824 825
        else if ( object->inherits( "QListWidget" ) )
        {
            const QListWidget *list = qobject_cast<const QListWidget *>( object );
            QStringList valuesList;
            for( int i=0; i < list->count(); i++ )
            {
                const QListWidgetItem *item = list->item( i );
                if ( item->checkState() == Qt::Checked )
                    valuesList.append( item->data( Qt::UserRole ).toString() );
            }
            value = QUrl::toPercentEncoding( valuesList.join( ";" ), "", "_;" );
        }
826 827 828 829

        if ( !value.isEmpty() )
            configuration << QString( "%1_%2=%3" )
                             .arg( categ ).arg( prop ).arg( value );
830
    }
831

832
    return configuration.join( ";" );
833 834
}

835 836 837 838 839 840
void VLCProfileEditor::reset()
{
    /* reset to default state as we can only check/enable existing values */
    ui.valueholder_video_copy->setChecked( false );
    ui.valueholder_audio_copy->setChecked( false );
    activatePanels();
841
    fixBirateState(); /* defaults to bitrate, not qp */
842 843 844 845 846 847 848 849 850 851 852 853
    /* end with top level ones for cascaded setEnabled() */
    ui.valueholder_video_enable->setChecked( false );
    ui.valueholder_audio_enable->setChecked( false );
    ui.valueholder_subtitles_enable->setChecked( false );
}

void VLCProfileEditor::activatePanels()
{
    ui.transcodevideo->setEnabled( ! ui.valueholder_video_copy->isChecked() );
    ui.transcodeaudio->setEnabled( ! ui.valueholder_audio_copy->isChecked() );
}

854 855 856 857 858 859 860 861 862 863 864 865
void VLCProfileEditor::fixBirateState()
{
    /* exclusive bitrate choice */
    ui.valueholder_vcodec_qp->setValue( 0 );
}

void VLCProfileEditor::fixQPState()
{
    /* exclusive bitrate choice */
    ui.valueholder_vcodec_bitrate->setValue( 0 );
}

866 867 868
#undef CATPROP2NAME
#undef CATANDPROP
#undef OLDFORMAT