clippropertiescontroller.cpp 39.8 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
/*
Copyright (C) 2015  Jean-Baptiste Mardelle <jb@kdenlive.org>
This file is part of Kdenlive. See www.kdenlive.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) version 3 or any later version
accepted by the membership of KDE e.V. (or its successor approved
by the membership of KDE e.V.), which shall act as a proxy 
defined in Section 14 of version 3 of the license.

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, see <http://www.gnu.org/licenses/>.
*/

#include "clippropertiescontroller.h"
#include "bincontroller.h"
24
#include "timeline/markerdialog.h"
25
#include "timecodedisplay.h"
26
#include "clipcontroller.h"
27
#include "kdenlivesettings.h"
28
#include "effectstack/widgets/choosecolorwidget.h"
29
#include "dialogs/profilesdialog.h"
30
#include "utils/KoIconUtils.h"
31 32

#include <KLocalizedString>
33
#include <KIO/Global>
34 35 36 37 38

#include <QUrl>
#include <QDebug>
#include <QPixmap>
#include <QVBoxLayout>
39 40
#include <QHBoxLayout>
#include <QDoubleSpinBox>
41
#include <QLabel>
42 43
#include <QFile>
#include <QProcess>
44 45
#include <QCheckBox>
#include <QFontDatabase>
46
#include <QToolBar>
47
#include <QFileDialog>
48

49 50 51
ClipPropertiesController::ClipPropertiesController(Timecode tc, ClipController *controller, QWidget *parent) : QTabWidget(parent)
    , m_controller(controller)
    , m_tc(tc)
52 53 54
    , m_id(controller->clipId())
    , m_type(controller->clipType())
    , m_properties(controller->properties())
55
{
56
    setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
57
    setDocumentMode(true);
58 59 60
    m_forcePage = new QWidget(this);
    m_propertiesPage = new QWidget(this);
    m_markersPage = new QWidget(this);
61
    m_metaPage = new QWidget(this);
62
    m_analysisPage = new QWidget(this);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    // Clip properties
    QVBoxLayout *propsBox = new QVBoxLayout;
    QTreeWidget *propsTree = new QTreeWidget;
    propsTree->setRootIsDecorated(false);
    propsTree->setColumnCount(2);
    propsTree->setAlternatingRowColors(true);
    propsTree->setHeaderHidden(true);
    propsBox->addWidget(propsTree);
    fillProperties(propsTree);
    m_propertiesPage->setLayout(propsBox);

    // Clip markers
    QVBoxLayout *mBox = new QVBoxLayout;
    m_markerTree = new QTreeWidget;
    m_markerTree->setRootIsDecorated(false);
    m_markerTree->setColumnCount(2);
    m_markerTree->setAlternatingRowColors(true);
    m_markerTree->setHeaderHidden(true);
82
    m_markerTree->setSelectionMode(QAbstractItemView::ExtendedSelection);
83
    mBox->addWidget(m_markerTree);
84
    QToolBar *bar = new QToolBar;
85 86
    bar->addAction(KoIconUtils::themedIcon("document-new"), i18n("Add marker"), this, SLOT(slotAddMarker()));
    bar->addAction(KoIconUtils::themedIcon("trash-empty"), i18n("Delete marker"), this, SLOT(slotDeleteMarker()));
87
    bar->addAction(KoIconUtils::themedIcon("document-edit"), i18n("Edit marker"), this, SLOT(slotEditMarker()));
88 89
    bar->addAction(KoIconUtils::themedIcon("document-save-as"), i18n("Export markers"), this, SLOT(slotSaveMarkers()));
    bar->addAction(KoIconUtils::themedIcon("document-open"), i18n("Import markers"), this, SLOT(slotLoadMarkers()));
90
    mBox->addWidget(bar);
91

92 93
    slotFillMarkers();
    m_markersPage->setLayout(mBox);
94
    connect(m_markerTree, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(slotSeekToMarker()));
95 96 97 98 99 100 101 102 103 104

    // metadata
    QVBoxLayout *m2Box = new QVBoxLayout;
    QTreeWidget *metaTree = new QTreeWidget;
    metaTree->setRootIsDecorated(true);
    metaTree->setColumnCount(2);
    metaTree->setAlternatingRowColors(true);
    metaTree->setHeaderHidden(true);
    m2Box->addWidget(metaTree);
    slotFillMeta(metaTree);
105 106 107 108 109 110 111 112 113 114 115 116 117
    m_metaPage->setLayout(m2Box );

    // Clip analysis
    QVBoxLayout *aBox = new QVBoxLayout;
    m_analysisTree = new QTreeWidget;
    m_analysisTree->setRootIsDecorated(false);
    m_analysisTree->setColumnCount(2);
    m_analysisTree->setAlternatingRowColors(true);
    m_analysisTree->setHeaderHidden(true);
    m_analysisTree->setDragEnabled(true);
    aBox ->addWidget(new QLabel(i18n("Analysis data")));
    aBox ->addWidget(m_analysisTree);
    QToolBar *bar2 = new QToolBar;
118 119 120
    bar2->addAction(KoIconUtils::themedIcon("trash-empty"), i18n("Delete analysis"), this, SLOT(slotDeleteAnalysis()));
    bar2->addAction(KoIconUtils::themedIcon("document-save-as"), i18n("Export analysis"), this, SLOT(slotSaveAnalysis()));
    bar2->addAction(KoIconUtils::themedIcon("document-open"), i18n("Import analysis"), this, SLOT(slotLoadAnalysis()));
121 122 123 124
    aBox->addWidget(bar2);

    slotFillAnalysisData();
    m_analysisPage->setLayout(aBox );
125

126
    // Force properties
127 128
    QVBoxLayout *vbox = new QVBoxLayout;
    if (m_type == Color || m_type == Image || m_type == AV || m_type == Video) {
129
        // Edit duration widget
130 131
        m_originalProperties.insert("out", m_properties.get("out"));
        m_originalProperties.insert("length", m_properties.get("length"));
132 133 134 135
        QHBoxLayout *hlay = new QHBoxLayout;
        QCheckBox *box = new QCheckBox(i18n("Duration"), this);
        box->setObjectName("force_duration");
        hlay->addWidget(box);
136
        TimecodeDisplay *timePos = new TimecodeDisplay(tc, this);
137
        timePos->setObjectName("force_duration_value");
138
        timePos->setValue(m_properties.get_int("out") + 1);
139 140 141 142 143 144 145 146 147
        int original_length = m_properties.get_int("kdenlive:original_length");
        if (original_length > 0) {
            box->setChecked(true);
        }
        else timePos->setEnabled(false);
        hlay->addWidget(timePos);
        vbox->addLayout(hlay);
        connect(box, SIGNAL(toggled(bool)), timePos, SLOT(setEnabled(bool)));
        connect(box, SIGNAL(stateChanged(int)), this, SLOT(slotEnableForce(int)));
148
        connect(timePos, SIGNAL(timeCodeEditingFinished(int)), this, SLOT(slotDurationChanged(int)));
149
        connect(this, SIGNAL(updateTimeCodeFormat()), timePos, SLOT(slotUpdateTimeCodeFormat()));
150
        connect(this, SIGNAL(modified(int)), timePos, SLOT(setValue(int)));
151 152 153 154 155 156 157 158 159 160 161
    }
    if (m_type == Color) {
        // Edit color widget
        m_originalProperties.insert("resource", m_properties.get("resource"));
        mlt_color color = m_properties.get_color("resource");
        ChooseColorWidget *choosecolor = new ChooseColorWidget(i18n("Color"), QColor::fromRgb(color.r, color.g, color.b).name(), false, this);
        vbox->addWidget(choosecolor);
        //connect(choosecolor, SIGNAL(displayMessage(QString,int)), this, SIGNAL(displayMessage(QString,int)));
        connect(choosecolor, SIGNAL(modified(QColor)), this, SLOT(slotColorModified(QColor)));
        connect(this, SIGNAL(modified(QColor)), choosecolor, SLOT(slotColorModified(QColor)));
    }
162 163 164 165 166 167 168 169 170 171 172
    if (m_type == Image) {
        int transparency = m_properties.get_int("kdenlive:transparency");
        m_originalProperties.insert("kdenlive:transparency", QString::number(transparency));
        QHBoxLayout *hlay = new QHBoxLayout;
        QCheckBox *box = new QCheckBox(i18n("Transparent"), this);
        box->setObjectName("kdenlive:transparency");
        box->setChecked(transparency == 1);
        connect(box, SIGNAL(stateChanged(int)), this, SLOT(slotEnableForce(int)));
        hlay->addWidget(box);
        vbox->addLayout(hlay);
    }
173 174
    
    if (m_type == AV || m_type == Video || m_type == Image) {
175 176 177
        // Aspect ratio
        int force_ar_num = m_properties.get_int("force_aspect_num");
        int force_ar_den  = m_properties.get_int("force_aspect_den");
178 179
        m_originalProperties.insert("force_aspect_den", (force_ar_den == 0) ? QString() : QString::number(force_ar_den));
        m_originalProperties.insert("force_aspect_num", (force_ar_num == 0) ? QString() : QString::number(force_ar_num));
180 181
        QHBoxLayout *hlay = new QHBoxLayout;
        QCheckBox *box = new QCheckBox(i18n("Aspect Ratio"), this);
182 183
        box->setObjectName("force_ar");
        vbox->addWidget(box);
184
        connect(box, SIGNAL(stateChanged(int)), this, SLOT(slotEnableForce(int)));
185
        QSpinBox *spin1 = new QSpinBox(this);
186
        spin1->setMaximum(8000);
187 188 189 190
        spin1->setObjectName("force_aspect_num_value");
        hlay->addWidget(spin1);
        hlay->addWidget(new QLabel(":"));
        QSpinBox *spin2 = new QSpinBox(this);
191 192
        spin2->setMinimum(1);
        spin2->setMaximum(8000);
193 194 195
        spin2->setObjectName("force_aspect_den_value");
        hlay->addWidget(spin2);
        if (force_ar_num == 0) {
196 197 198 199 200 201 202
            // use current ratio
            int num = m_properties.get_int("meta.media.sample_aspect_num");
            int den = m_properties.get_int("meta.media.sample_aspect_den");
            if (den == 0) {
                num = 1;
                den = 1;
            }
203 204
            spin1->setEnabled(false);
            spin2->setEnabled(false);
205 206
            spin1->setValue(num);
            spin2->setValue(den);
207 208 209 210 211 212 213 214
        }
        else {
            box->setChecked(true);
            spin1->setEnabled(true);
            spin2->setEnabled(true);
            spin1->setValue(force_ar_num);
            spin2->setValue(force_ar_den);
        }
215 216
        connect(spin2, SIGNAL(valueChanged(int)), this, SLOT(slotAspectValueChanged(int)));
        connect(spin1, SIGNAL(valueChanged(int)), this, SLOT(slotAspectValueChanged(int)));
217 218 219
        connect(box, SIGNAL(toggled(bool)), spin1, SLOT(setEnabled(bool)));
        connect(box, SIGNAL(toggled(bool)), spin2, SLOT(setEnabled(bool)));
        vbox->addLayout(hlay);
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 245 246 247 248
        }
    
    if (m_type == AV || m_type == Video) {
        QLocale locale;

        // Fps
        QString force_fps = m_properties.get("force_fps");
        m_originalProperties.insert("force_fps", force_fps.isEmpty() ? "-" : force_fps);
        QHBoxLayout *hlay = new QHBoxLayout;
        QCheckBox *box = new QCheckBox(i18n("Frame rate"), this);
        box->setObjectName("force_fps");
        connect(box, SIGNAL(stateChanged(int)), this, SLOT(slotEnableForce(int)));
        QDoubleSpinBox *spin = new QDoubleSpinBox(this);
        spin->setMaximum(1000);
        connect(spin, SIGNAL(valueChanged(double)), this, SLOT(slotValueChanged(double)));
        spin->setObjectName("force_fps_value");
        if (force_fps.isEmpty()) {
            spin->setValue(controller->originalFps());
        }
        else {
            spin->setValue(locale.toDouble(force_fps));
        }
        connect(box, SIGNAL(toggled(bool)), spin, SLOT(setEnabled(bool)));
        box->setChecked(!force_fps.isEmpty());
        spin->setEnabled(!force_fps.isEmpty());
        hlay->addWidget(box);
        hlay->addWidget(spin);
        vbox->addLayout(hlay);

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
        // Scanning
        QString force_prog = m_properties.get("force_progressive");
        m_originalProperties.insert("force_progressive", force_prog.isEmpty() ? "-" : force_prog);
        hlay = new QHBoxLayout;
        box = new QCheckBox(i18n("Scanning"), this);
        connect(box, SIGNAL(stateChanged(int)), this, SLOT(slotEnableForce(int)));
        box->setObjectName("force_progressive");
        QComboBox *combo = new QComboBox(this);
        combo->addItem(i18n("Interlaced"), 0);
        combo->addItem(i18n("Progressive"), 1);
        connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComboValueChanged()));
        combo->setObjectName("force_progressive_value");
        if (!force_prog.isEmpty()) {
            combo->setCurrentIndex(force_prog.toInt());
        }
        connect(box, SIGNAL(toggled(bool)), combo, SLOT(setEnabled(bool)));
        box->setChecked(!force_prog.isEmpty());
        combo->setEnabled(!force_prog.isEmpty());
        hlay->addWidget(box);
        hlay->addWidget(combo);
        vbox->addLayout(hlay);
270

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
        // Field order
        QString force_tff = m_properties.get("force_tff");
        m_originalProperties.insert("force_tff", force_tff.isEmpty() ? "-" : force_tff);
        hlay = new QHBoxLayout;
        box = new QCheckBox(i18n("Field order"), this);
        connect(box, SIGNAL(stateChanged(int)), this, SLOT(slotEnableForce(int)));
        box->setObjectName("force_tff");
        combo = new QComboBox(this);
        combo->addItem(i18n("Bottom first"), 0);
        combo->addItem(i18n("Top first"), 1);
        connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComboValueChanged()));
        combo->setObjectName("force_tff_value");
        if (!force_tff.isEmpty()) {
            combo->setCurrentIndex(force_tff.toInt());
        }
        connect(box, SIGNAL(toggled(bool)), combo, SLOT(setEnabled(bool)));
        box->setChecked(!force_tff.isEmpty());
        combo->setEnabled(!force_tff.isEmpty());
        hlay->addWidget(box);
        hlay->addWidget(combo);
        vbox->addLayout(hlay);
        
        //Decoding threads
        QString threads = m_properties.get("threads");
        m_originalProperties.insert("threads", threads);
        hlay = new QHBoxLayout;
        hlay->addWidget(new QLabel(i18n("Threads")));
        QSpinBox *spinI = new QSpinBox(this);
        spinI->setMaximum(4);
        spinI->setObjectName("threads_value");
        if (!threads.isEmpty()) {
            spinI->setValue(threads.toInt());
        }
        else spinI->setValue(1);
        connect(spinI, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
        hlay->addWidget(spinI);
        vbox->addLayout(hlay);

        //Video index
        QString vix = m_properties.get("video_index");
        m_originalProperties.insert("video_index", vix);
        hlay = new QHBoxLayout;
        hlay->addWidget(new QLabel(i18n("Video index")));
        spinI = new QSpinBox(this);
        spinI->setMaximum(m_clipProperties.value("video_max").toInt());
        spinI->setObjectName("video_index_value");
        if (vix.isEmpty()) {
            spinI->setValue(0);
        }
        else spinI->setValue(vix.toInt());
        connect(spinI, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
        hlay->addWidget(spinI);
        vbox->addLayout(hlay);
        
        //Audio index
        QString aix = m_properties.get("audio_index");
        m_originalProperties.insert("audio_index", aix);
        hlay = new QHBoxLayout;
        hlay->addWidget(new QLabel(i18n("Audio index")));
        spinI = new QSpinBox(this);
        spinI->setMaximum(m_clipProperties.value("audio_max").toInt());
        spinI->setObjectName("audio_index_value");
        if (aix.isEmpty()) {
            spinI->setValue(0);
        }
        else spinI->setValue(aix.toInt());
        connect(spinI, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
        hlay->addWidget(spinI);
        vbox->addLayout(hlay);

        // Colorspace
342 343 344 345
        hlay = new QHBoxLayout;
        box = new QCheckBox(i18n("Colorspace"), this);
        box->setObjectName("force_colorspace");
        connect(box, SIGNAL(stateChanged(int)), this, SLOT(slotEnableForce(int)));
346
        combo = new QComboBox(this);
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
        combo->setObjectName("force_colorspace_value");
        combo->addItem(ProfilesDialog::getColorspaceDescription(601), 601);
        combo->addItem(ProfilesDialog::getColorspaceDescription(709), 709);
        combo->addItem(ProfilesDialog::getColorspaceDescription(240), 240);
        int force_colorspace = m_properties.get_int("force_colorspace");
        m_originalProperties.insert("force_colorspace", force_colorspace == 0 ? "-" : QString::number(force_colorspace));
        int colorspace = controller->videoCodecProperty("colorspace").toInt();
        if (force_colorspace > 0) {
            box->setChecked(true);
            combo->setEnabled(true);
            combo->setCurrentIndex(combo->findData(force_colorspace));
        } else if (colorspace > 0) {
            combo->setEnabled(false);
            combo->setCurrentIndex(combo->findData(colorspace));
        }
        else combo->setEnabled(false);
        connect(box, SIGNAL(toggled(bool)), combo, SLOT(setEnabled(bool)));
364
        connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComboValueChanged()));
365 366
        hlay->addWidget(box);
        hlay->addWidget(combo);
367
        vbox->addLayout(hlay);
368
        
369
        //Full luma
370 371
        QString force_luma = m_properties.get("set.force_full_luma");
        m_originalProperties.insert("set.force_full_luma", force_luma);
372 373 374
        hlay = new QHBoxLayout;
        box = new QCheckBox(i18n("Full luma range"), this);
        connect(box, SIGNAL(stateChanged(int)), this, SLOT(slotEnableForce(int)));
375
        box->setObjectName("set.force_full_luma");
376 377
        box->setChecked(!force_luma.isEmpty());
        hlay->addWidget(box);
378
        vbox->addLayout(hlay);
379
    }
380
    m_forcePage->setLayout(vbox);
381
    vbox->addStretch(10);
382 383 384 385
    addTab(m_propertiesPage, QString());
    addTab(m_markersPage, QString());
    addTab(m_forcePage, QString());
    addTab(m_metaPage, QString());
386
    addTab(m_analysisPage, QString());
387
    setTabIcon(0, KoIconUtils::themedIcon("edit-find"));
388
    setTabToolTip(0, i18n("Properties"));
389
    setTabIcon(1, KoIconUtils::themedIcon("bookmark-new"));
390
    setTabToolTip(1, i18n("Markers"));
391
    setTabIcon(2, KoIconUtils::themedIcon("document-edit"));
392
    setTabToolTip(2, i18n("Force properties"));
393
    setTabIcon(3, KoIconUtils::themedIcon("view-grid"));
394
    setTabToolTip(3, i18n("Metadata"));
395
    setTabIcon(4, KoIconUtils::themedIcon("visibility"));
396
    setTabToolTip(4, i18n("Analysis"));
397 398
    setCurrentIndex(KdenliveSettings::properties_panel_page());
    if (m_type == Color) setTabEnabled(0, false);
399 400 401 402
}

ClipPropertiesController::~ClipPropertiesController()
{
403
    KdenliveSettings::setProperties_panel_page(currentIndex());
404 405
}

406 407 408 409 410
void ClipPropertiesController::slotRefreshTimeCode()
{
    emit updateTimeCodeFormat();
}

411 412 413 414
void ClipPropertiesController::slotReloadProperties()
{
    if (m_type == Color) {
        m_originalProperties.insert("resource", m_properties.get("resource"));
415 416 417
        m_originalProperties.insert("out", m_properties.get("out"));
        m_originalProperties.insert("length", m_properties.get("length"));
        emit modified(m_properties.get_int("length"));
418 419 420 421 422 423 424 425 426 427 428 429 430
        mlt_color color = m_properties.get_color("resource");
        emit modified(QColor::fromRgb(color.r, color.g, color.b));
    }
}

void ClipPropertiesController::slotColorModified(QColor newcolor)
{
    QMap <QString, QString> properties;
    properties.insert("resource", newcolor.name(QColor::HexArgb));
    emit updateClipProperties(m_id, m_originalProperties, properties);
    m_originalProperties = properties;
}

431 432 433
void ClipPropertiesController::slotDurationChanged(int duration)
{
    QMap <QString, QString> properties;
434 435 436 437
    int original_length = m_properties.get_int("kdenlive:original_length");
    if (original_length == 0) {
        m_properties.set("kdenlive:original_length", m_properties.get_int("length"));
    }
438 439 440 441 442 443
    properties.insert("length", QString::number(duration));
    properties.insert("out", QString::number(duration - 1));
    emit updateClipProperties(m_id, m_originalProperties, properties);
    m_originalProperties = properties;
}

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
void ClipPropertiesController::slotEnableForce(int state)
{
    QCheckBox *box = qobject_cast<QCheckBox *>(sender());
    if (!box) {
        return;
    }
    QString param = box->objectName();
    QMap <QString, QString> properties;
    QLocale locale;
    if (state == Qt::Unchecked) {
        // The force property was disable, remove it / reset default if necessary
        if (param == "force_duration") {
            // special case, reset original duration
            TimecodeDisplay *timePos = findChild<TimecodeDisplay *>(param + "_value");
            timePos->setValue(m_properties.get_int("kdenlive:original_length"));
            slotDurationChanged(m_properties.get_int("kdenlive:original_length"));
            m_properties.set("kdenlive:original_length", (char *) NULL);
            return;
        }
463 464 465
        else if (param == "kdenlive:transparency") {
            properties.insert(param, QString());
        }
466 467 468 469 470
        else if (param == "force_ar") {
            properties.insert("force_aspect_den", QString());
            properties.insert("force_aspect_num", QString());
            properties.insert("force_aspect_ratio", QString());
        }
471
        else {
472
            properties.insert(param, QString());
473 474 475 476 477 478 479 480 481
        }
    } else {
        // A force property was set
        if (param == "force_duration") {
            int original_length = m_properties.get_int("kdenlive:original_length");
            if (original_length == 0) {
                m_properties.set("kdenlive:original_length", m_properties.get_int("length"));
            }
        }
482
        else if (param == "force_fps") {
483 484 485 486
            QDoubleSpinBox *spin = findChild<QDoubleSpinBox *>(param + "_value");
            if (!spin) return;
            properties.insert(param, locale.toString(spin->value()));
        }
487 488 489 490 491 492
        else if (param == "threads") {
            QSpinBox *spin = findChild<QSpinBox *>(param + "_value");
            if (!spin) return;
            properties.insert(param, QString::number(spin->value()));
        }
        else if (param == "force_colorspace"  || param == "force_progressive" || param == "force_tff") {
493 494 495 496
            QComboBox *combo = findChild<QComboBox *>(param + "_value");
            if (!combo) return;
            properties.insert(param, QString::number(combo->currentData().toInt()));
        }
497
        else if (param == "kdenlive:transparency" || param == "set.force_full_luma") {
498 499
            properties.insert(param, "1");
        }
500 501 502 503 504 505 506 507 508
        else if (param == "force_ar") {
            QSpinBox *spin = findChild<QSpinBox *>("force_aspect_num_value");
            QSpinBox *spin2 = findChild<QSpinBox *>("force_aspect_den_value");
            if (!spin || !spin2) return;
            properties.insert("force_aspect_den", QString::number(spin2->value()));
            properties.insert("force_aspect_num", QString::number(spin->value()));
            QLocale locale;
            properties.insert("force_aspect_ratio", locale.toString((double) spin->value() / spin2->value()));
        }
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
    }
    if (properties.isEmpty()) return;
    emit updateClipProperties(m_id, m_originalProperties, properties);
    m_originalProperties = properties;
}

void ClipPropertiesController::slotValueChanged(double value)
{
    QDoubleSpinBox *box = qobject_cast<QDoubleSpinBox *>(sender());
    if (!box) {
        return;
    }
    QString param = box->objectName().section("_", 0, -2);
    QMap <QString, QString> properties;
    QLocale locale;
    properties.insert(param, locale.toString(value));
    emit updateClipProperties(m_id, m_originalProperties, properties);
    m_originalProperties = properties;
}
528

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
void ClipPropertiesController::slotValueChanged(int value)
{
    QSpinBox *box = qobject_cast<QSpinBox *>(sender());
    if (!box) {
        return;
    }
    QString param = box->objectName().section("_", 0, -2);
    QMap <QString, QString> properties;
    properties.insert(param, QString::number(value));
    emit updateClipProperties(m_id, m_originalProperties, properties);
    m_originalProperties = properties;
}

void ClipPropertiesController::slotAspectValueChanged(int)
{
    QSpinBox *spin = findChild<QSpinBox *>("force_aspect_num_value");
    QSpinBox *spin2 = findChild<QSpinBox *>("force_aspect_den_value");
    if (!spin || !spin2) return;
    QMap <QString, QString> properties;
    properties.insert("force_aspect_den", QString::number(spin2->value()));
    properties.insert("force_aspect_num", QString::number(spin->value()));
    QLocale locale;
    properties.insert("force_aspect_ratio", locale.toString((double) spin->value() / spin2->value()));
    emit updateClipProperties(m_id, m_originalProperties, properties);
    m_originalProperties = properties;
}

void ClipPropertiesController::slotComboValueChanged()
{
    QComboBox *box = qobject_cast<QComboBox *>(sender());
    if (!box) {
        return;
    }
    QString param = box->objectName().section("_", 0, -2);
    QMap <QString, QString> properties;
    QLocale locale;
    properties.insert(param, QString::number(box->currentData().toInt()));
    emit updateClipProperties(m_id, m_originalProperties, properties);
    m_originalProperties = properties;
}
569 570 571

void ClipPropertiesController::fillProperties(QTreeWidget *tree)
{
572 573
    m_clipProperties.clear();
    QList <QStringList> propertyMap;
574
    // Get the video_index
575
    if (m_type == AV || m_type == Video || m_type == Audio) {
576 577 578 579 580 581 582
        int vindex = m_controller->int_property("video_index");
        int video_max = 0;
        int default_audio = m_controller->int_property("audio_index");
        int audio_max = 0;

        // Find maximum stream index values
        for (int ix = 0; ix < m_controller->int_property("meta.media.nb_streams"); ++ix) {
Vincent Pinon's avatar
Vincent Pinon committed
583
            char property[200];
584 585 586 587 588 589 590
            snprintf(property, sizeof(property), "meta.media.%d.stream.type", ix);
            QString type = m_controller->property(property);
            if (type == "video")
                video_max = ix;
            else if (type == "audio")
                audio_max = ix;
        }
591 592 593 594
        m_clipProperties.insert("default_video", QString::number(vindex));
        m_clipProperties.insert("video_max", QString::number(video_max));
        m_clipProperties.insert("default_audio", QString::number(default_audio));
        m_clipProperties.insert("audio_max", QString::number(audio_max));
595 596 597

        if (vindex > -1) {
            // We have a video stream
Vincent Pinon's avatar
Vincent Pinon committed
598
            char property[200];
599 600 601 602 603 604 605 606
            snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", vindex);
            QString codec = m_controller->property(property);
            if (!codec.isEmpty()) {
                propertyMap.append(QStringList() << i18n("Video codec") << codec);
            }
            int width = m_controller->int_property("meta.media.width");
            int height = m_controller->int_property("meta.media.height");
            propertyMap.append(QStringList() << i18n("Frame size") << QString::number(width) + "x" + QString::number(height));
607

608
            snprintf(property, sizeof(property), "meta.media.%d.stream.frame_rate", vindex);
609 610 611
            QString fpsValue = m_controller->property(property);
            if (!fpsValue.isEmpty()) {
                propertyMap.append(QStringList() << i18n("Frame rate") << fpsValue);
612 613 614
            } else {
                int rate_den = m_controller->int_property("meta.media.frame_rate_den");
                if (rate_den > 0) {
615 616
                    double fps = (double) m_controller->int_property(QStringLiteral("meta.media.frame_rate_num")) / rate_den;
                    propertyMap.append(QStringList() << i18n("Frame rate") << QString::number(fps, 'f', 2));
617 618 619
                }
            }

620
            snprintf(property, sizeof(property), "meta.media.%d.codec.bit_rate", vindex);
621
            int bitrate = m_controller->int_property(property) / 1000;
622
            if (bitrate > 0) {
623
                propertyMap.append(QStringList() << i18n("Video bitrate") << QString::number(bitrate) + QStringLiteral(" ") + i18nc("Kilobytes per seconds", "kb/s"));
624 625
            }

626 627 628 629
            int scan = m_controller->int_property("meta.media.progressive");
            propertyMap.append(QStringList() << i18n("Scanning") << (scan == 1 ? i18n("Progressive") : i18n("Interlaced")));
            snprintf(property, sizeof(property), "meta.media.%d.codec.sample_aspect_ratio", vindex);
            double par = m_controller->double_property(property);
630 631 632 633
            if (par == 0) {
                // Read media aspect ratio
                par = m_controller->double_property("aspect_ratio");
            }
634 635 636 637

            propertyMap.append(QStringList() << i18n("Pixel aspect ratio") << QString::number(par, 'f', 3));
            propertyMap.append(QStringList() << i18n("Pixel format") << m_controller->videoCodecProperty(QStringLiteral("pix_fmt")));
            int colorspace = m_controller->videoCodecProperty(QStringLiteral("colorspace")).toInt();
638
            propertyMap.append(QStringList() << i18n("Colorspace") << ProfilesDialog::getColorspaceDescription(colorspace));
639 640 641
        }
        if (default_audio > -1) {
            char property[200];
642
            snprintf(property, sizeof(property), "meta.media.%d.codec.long_name", default_audio);
643
            QString codec = m_controller->property(property);
644 645 646 647 648 649 650 651 652
            if (!codec.isEmpty()) {
                propertyMap.append(QStringList() << i18n("Audio codec") << codec);
            }
            snprintf(property, sizeof(property), "meta.media.%d.codec.channels", default_audio);
            int channels = m_controller->int_property(property);
            propertyMap.append(QStringList() << i18n("Audio channels") << QString::number(channels));

            snprintf(property, sizeof(property), "meta.media.%d.codec.sample_rate", default_audio);
            int srate = m_controller->int_property(property);
653
            propertyMap.append(QStringList() << i18n("Audio frequency") << QString::number(srate) + QStringLiteral(" ") + i18nc("Herz", "Hz"));
654 655

            snprintf(property, sizeof(property), "meta.media.%d.codec.bit_rate", default_audio);
656
            int bitrate = m_controller->int_property(property) / 1000;
657
            if (bitrate > 0) {
658
                propertyMap.append(QStringList() << i18n("Audio bitrate") << QString::number(bitrate) + QStringLiteral(" ") + i18nc("Kilobytes per seconds", "kb/s"));
659
            }
660 661
        }
    }
662

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
    for (int i = 0; i < propertyMap.count(); i++) {
        new QTreeWidgetItem(tree, propertyMap.at(i));
    }
    tree->resizeColumnToContents(0);
}

void ClipPropertiesController::slotFillMarkers()
{
    m_markerTree->clear();
    QList <CommentedTime> markers = m_controller->commentedSnapMarkers();
    for (int count = 0; count < markers.count(); ++count) {
        QString time = m_tc.getTimecode(markers.at(count).time());
        QStringList itemtext;
        itemtext << time << markers.at(count).comment();
        QTreeWidgetItem *item = new QTreeWidgetItem(m_markerTree, itemtext);
        item->setData(0, Qt::DecorationRole, CommentedTime::markerColor(markers.at(count).markerType()));
        item->setData(0, Qt::UserRole, QVariant((int) markers.at(count).time().frames(m_tc.fps())));
    }
    m_markerTree->resizeColumnToContents(0);
}

684
void ClipPropertiesController::slotSeekToMarker()
685 686 687 688
{
    QTreeWidgetItem *item = m_markerTree->currentItem();
    int time = item->data(0, Qt::UserRole).toInt();
    emit seekToFrame(time);
689 690 691 692 693
}

void ClipPropertiesController::slotEditMarker()
{
    QList < CommentedTime > marks = m_controller->commentedSnapMarkers();
694 695
    int pos = m_markerTree->currentIndex().row();
    if (pos < 0 || pos > marks.count() - 1) return;
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
    QPointer<MarkerDialog> d = new MarkerDialog(m_controller, marks.at(pos), m_tc, i18n("Edit Marker"), this);
    if (d->exec() == QDialog::Accepted) {
        QList <CommentedTime> markers;
        markers << d->newMarker();
        emit addMarkers(m_id, markers);
    }
    delete d;
}

void ClipPropertiesController::slotDeleteMarker()
{
    QList < CommentedTime > marks = m_controller->commentedSnapMarkers();
    QList < CommentedTime > toDelete;
    for (int i = 0; i < marks.count(); ++i) {
        if (m_markerTree->topLevelItem(i)->isSelected()) {
            CommentedTime marker = marks.at(i);
            marker.setMarkerType(-1);
            toDelete << marker;
        }
    }
    emit addMarkers(m_id, toDelete);
}

void ClipPropertiesController::slotAddMarker()
{
    CommentedTime marker(GenTime(m_controller->originalProducer().position(), m_tc.fps()), i18n("Marker"));
    QPointer<MarkerDialog> d = new MarkerDialog(m_controller, marker,
                                                m_tc, i18n("Add Marker"), this);
724 725 726
    if (d->exec() == QDialog::Accepted) {
        QList <CommentedTime> markers;
        markers << d->newMarker();
727
        emit addMarkers(m_id, markers);
728
    }
729 730 731 732 733 734 735 736 737 738 739
    delete d;
}

void ClipPropertiesController::slotSaveMarkers()
{
    emit saveMarkers(m_id);
}

void ClipPropertiesController::slotLoadMarkers()
{
    emit loadMarkers(m_id);
740 741
}

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
void ClipPropertiesController::slotFillMeta(QTreeWidget *tree)
{
    tree->clear();
    if (m_type != AV && m_type != Video && m_type != Image) {
        // Currently, we only use exiftool on video files
        return;
    }
    int exifUsed = m_controller->int_property("kdenlive:exiftool");
    if (exifUsed == 1) {
          Mlt::Properties subProperties;
          subProperties.pass_values(m_properties, "kdenlive:meta.exiftool.");
          if (subProperties.count() > 0) {
              QTreeWidgetItem *exif = new QTreeWidgetItem(tree, QStringList() << i18n("Exif") << QString());
              exif->setExpanded(true);
              for (int i = 0; i < subProperties.count(); i++) {
                  new QTreeWidgetItem(exif, QStringList() << subProperties.get_name(i) << subProperties.get(i));
              }
          }
    }
    else if (KdenliveSettings::use_exiftool()) {
        QString url = m_controller->clipUrl().path();
        //Check for Canon THM file
        url = url.section('.', 0, -2) + ".THM";
        if (QFile::exists(url)) {
            // Read the exif metadata embedded in the THM file
            QProcess p;
            QStringList args;
            args << "-g" << "-args" << url;
            p.start("exiftool", args);
            p.waitForFinished();
            QString res = p.readAllStandardOutput();
            m_controller->setProperty("kdenlive:exiftool", 1);
            QTreeWidgetItem *exif = NULL;
            QStringList list = res.split('\n');
            foreach(const QString &tagline, list) {
                if (tagline.startsWith(QLatin1String("-File")) || tagline.startsWith(QLatin1String("-ExifTool"))) continue;
                QString tag = tagline.section(':', 1).simplified();
                if (tag.startsWith(QLatin1String("ImageWidth")) || tag.startsWith(QLatin1String("ImageHeight"))) continue;
                if (!tag.section('=', 0, 0).isEmpty() && !tag.section('=', 1).simplified().isEmpty()) {
                    if (!exif) {
                        exif = new QTreeWidgetItem(tree, QStringList() << i18n("Exif") << QString());
                        exif->setExpanded(true);
                    }
                    m_controller->setProperty("kdenlive:meta.exiftool." + tag.section('=', 0, 0), tag.section('=', 1).simplified());
                    new QTreeWidgetItem(exif, QStringList() << tag.section('=', 0, 0) << tag.section('=', 1).simplified());
                }
            }
        } else {
            if (m_type == Image || m_controller->codec(false) == "h264") {
                QProcess p;
                QStringList args;
                args << "-g" << "-args" << m_controller->clipUrl().path();
                p.start("exiftool", args);
                p.waitForFinished();
                QString res = p.readAllStandardOutput();
                if (m_type != Image) {
                    m_controller->setProperty("kdenlive:exiftool", 1);
                }
                QTreeWidgetItem *exif = NULL;
                QStringList list = res.split('\n');
                foreach(const QString &tagline, list) {
                    if (m_type != Image && !tagline.startsWith(QLatin1String("-H264"))) continue;
                    QString tag = tagline.section(':', 1);
                    if (tag.startsWith(QLatin1String("ImageWidth")) || tag.startsWith(QLatin1String("ImageHeight"))) continue;
                    if (!exif) {
                        exif = new QTreeWidgetItem(tree, QStringList() << i18n("Exif") << QString());
                        exif->setExpanded(true);
                    }
                    if (m_type != Image) {
                        // Do not store image exif metadata in project file, would be too much noise
                        m_controller->setProperty("kdenlive:meta.exiftool." + tag.section('=', 0, 0), tag.section('=', 1).simplified());
                    }
                    new QTreeWidgetItem(exif, QStringList() << tag.section('=', 0, 0) << tag.section('=', 1).simplified());
                }
            }
        }
    }
    int magic = m_controller->int_property("kdenlive:magiclantern");
    if (magic == 1) {
        Mlt::Properties subProperties;
        subProperties.pass_values(m_properties, "kdenlive:meta.magiclantern.");
        QTreeWidgetItem *magicL = NULL;
        for (int i = 0; i < subProperties.count(); i++) {
            if (!magicL) {
                magicL = new QTreeWidgetItem(tree, QStringList() << i18n("Magic Lantern") << QString());
                QIcon icon(QStandardPaths::locate(QStandardPaths::DataLocation, "meta_magiclantern.png"));
                magicL->setIcon(0, icon);
                magicL->setExpanded(true);
            }
            new QTreeWidgetItem(magicL, QStringList() << subProperties.get_name(i) << subProperties.get(i));
        }
    }
    else if (m_type != Image && KdenliveSettings::use_magicLantern()) {
        QString url = m_controller->clipUrl().path();
        url = url.section('.', 0, -2) + ".LOG";
        if (QFile::exists(url)) {
            QFile file(url);
            if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                m_controller->setProperty("kdenlive:magiclantern", 1);
                QTreeWidgetItem *magicL = NULL;
                while (!file.atEnd()) {
                    QString line = file.readLine().simplified();
                    if (line.startsWith('#') || line.isEmpty() || !line.contains(':')) continue;
                    if (line.startsWith(QLatin1String("CSV data"))) break;
                    m_controller->setProperty("kdenlive:meta.magiclantern." + line.section(':', 0, 0).simplified(), line.section(':', 1).simplified());
                    if (!magicL) {
                        magicL = new QTreeWidgetItem(tree, QStringList() << i18n("Magic Lantern") << QString());
                        QIcon icon(QStandardPaths::locate(QStandardPaths::DataLocation, "meta_magiclantern.png"));
                        magicL->setIcon(0, icon);
                        magicL->setExpanded(true);
                    }
                    new QTreeWidgetItem(magicL, QStringList() << line.section(':', 0, 0).simplified() << line.section(':', 1).simplified());
                }
            }
        }

        //if (!meta.isEmpty())
        //clip->setMetadata(meta, "Magic Lantern");
        //clip->setProperty("magiclantern", "1");
    }
    tree->resizeColumnToContents(0);
}

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
void ClipPropertiesController::slotFillAnalysisData()
{
    m_analysisTree->clear();
    Mlt::Properties subProperties;
    subProperties.pass_values(m_properties, "kdenlive:clipanalysis.");
    if (subProperties.count() > 0) {
        for (int i = 0; i < subProperties.count(); i++) {
            new QTreeWidgetItem(m_analysisTree, QStringList() << subProperties.get_name(i) << subProperties.get(i));
        }
    }
    m_analysisTree->resizeColumnToContents(0);
}

void ClipPropertiesController::slotDeleteAnalysis()
{
    QTreeWidgetItem *current = m_analysisTree->currentItem();
    if (!current) return;
    emit editAnalysis(m_id, "kdenlive:clipanalysis." + current->text(0), QString());
}

void ClipPropertiesController::slotSaveAnalysis()
{
    const QString url = QFileDialog::getSaveFileName(this, i18n("Save Analysis Data"), m_controller->clipUrl().adjusted(QUrl::RemoveFilename).path(), i18n("Text File (*.txt)"));
    if (url.isEmpty())
        return;
    KSharedConfigPtr config = KSharedConfig::openConfig(url, KConfig::SimpleConfig);
    KConfigGroup analysisConfig(config, "Analysis");
    QTreeWidgetItem *current = m_analysisTree->currentItem();
    analysisConfig.writeEntry(current->text(0), current->text(1));
}

void ClipPropertiesController::slotLoadAnalysis()
{
    const QString url = QFileDialog::getOpenFileName(this, i18n("Open Analysis Data"), m_controller->clipUrl().adjusted(QUrl::RemoveFilename).path(), i18n("Text File (*.txt)"));
    if (url.isEmpty())
        return;
    KSharedConfigPtr config = KSharedConfig::openConfig(url, KConfig::SimpleConfig);
    KConfigGroup transConfig(config, "Analysis");
    // read the entries
    QMap< QString, QString > profiles = transConfig.entryMap();
    QMapIterator<QString, QString> i(profiles);
    while (i.hasNext()) {
        i.next();
        emit editAnalysis(m_id, "kdenlive:clipanalysis." + i.key(), i.value());
    }
Vincent Pinon's avatar
Vincent Pinon committed
910
}