projectclip.cpp 35.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
Copyright (C) 2012  Till Theato <root@ttill.de>
Copyright (C) 2014  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
11
by the membership of KDE e.V.), which shall act as a proxy
12 13 14 15 16 17 18 19 20 21 22 23
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 "projectclip.h"
24
#include "bin.h"
Nicolas Carion's avatar
Nicolas Carion committed
25
#include "core.h"
26 27
#include "doc/docundostack.hpp"
#include "doc/kdenlivedoc.h"
28
#include "doc/kthumb.h"
Nicolas Carion's avatar
Nicolas Carion committed
29
#include "effects/effectstack/model/effectstackmodel.hpp"
30 31
#include "jobs/jobmanager.h"
#include "jobs/thumbjob.hpp"
32 33
#include "jobs/loadjob.hpp"
#include <jobs/proxyclipjob.h>
34
#include "kdenlivesettings.h"
35
#include "lib/audio/audioStreamInfo.h"
36
#include "mltcontroller/clip.h"
37
#include "mltcontroller/clipcontroller.h"
38
#include "mltcontroller/clippropertiescontroller.h"
39
#include "model/markerlistmodel.hpp"
40
#include "profiles/profilemodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
41
#include "project/projectcommands.h"
42
#include "project/projectmanager.h"
Nicolas Carion's avatar
Nicolas Carion committed
43 44 45 46
#include "projectfolder.h"
#include "projectitemmodel.h"
#include "projectsubclip.h"
#include "timecode.h"
Nicolas Carion's avatar
style  
Nicolas Carion committed
47
#include "timeline2/model/snapmodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
48
#include "utils/KoIconUtils.h"
49
#include "utils/thumbnailcache.hpp"
50
#include "xml/xml.hpp"
51
#include <QPainter>
52
#include <kimagecache.h>
53

Laurent Montel's avatar
Laurent Montel committed
54
#include "kdenlive_debug.h"
55
#include <KLocalizedString>
56
#include <KMessageBox>
Nicolas Carion's avatar
Nicolas Carion committed
57 58 59 60 61
#include <QCryptographicHash>
#include <QDir>
#include <QDomElement>
#include <QFile>
#include <QtConcurrent>
Nicolas Carion's avatar
Nicolas Carion committed
62
#include <utility>
63

64 65
ProjectClip::ProjectClip(const QString &id, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model, std::shared_ptr<Mlt::Producer> producer)
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model)
66
    , ClipController(id, producer)
67
    , m_thumbsProducer(nullptr)
68
{
69
    m_markerModel = std::make_shared<MarkerListModel>(id, pCore->projectManager()->undoStack());
70
    m_clipStatus = StatusReady;
71 72 73 74
    m_name = clipName();
    m_duration = getStringDuration();
    m_date = date;
    m_description = ClipController::description();
75
    if (m_clipType == ClipType::Audio) {
76
        m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
77 78 79
    } else {
        m_thumbnail = thumb;
    }
80 81
    // Make sure we have a hash for this clip
    hash();
82
    connect(m_markerModel.get(), &MarkerListModel::modelChanged, [&]() { setProducerProperty(QStringLiteral("kdenlive:markers"), m_markerModel->toJson()); });
83 84
    QString markers = getProducerProperty(QStringLiteral("kdenlive:markers"));
    if (!markers.isEmpty()) {
85 86
        QMetaObject::invokeMethod(m_markerModel.get(), "importFromJson", Qt::QueuedConnection, Q_ARG(const QString &, markers), Q_ARG(bool, true),
                                  Q_ARG(bool, false));
87
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
88
    connectEffectStack();
89 90
}

91 92
// static
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model,
93
                                                    std::shared_ptr<Mlt::Producer> producer)
94
{
95
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, thumb, model, producer));
96
    baseFinishConstruct(self);
97
    model->loadSubClips(id, self->getPropertiesFromPrefix(QStringLiteral("kdenlive:clipzone.")));
98 99 100
    return self;
}

101
ProjectClip::ProjectClip(const QString &id, const QDomElement &description, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model)
102
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model)
103
    , ClipController(id)
104
    , m_thumbsProducer(nullptr)
105
{
106
    m_clipStatus = StatusWaiting;
107
    m_thumbnail = thumb;
108
    m_markerModel = std::make_shared<MarkerListModel>(m_binId, pCore->projectManager()->undoStack());
109
    if (description.hasAttribute(QStringLiteral("type"))) {
110
        m_clipType = (ClipType)description.attribute(QStringLiteral("type")).toInt();
111
        if (m_clipType == ClipType::Audio) {
112
            m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
113
        }
114
    }
115
    m_temporaryUrl = getXmlProperty(description, QStringLiteral("resource"));
116
    QString clipName = getXmlProperty(description, QStringLiteral("kdenlive:clipname"));
117 118
    if (!clipName.isEmpty()) {
        m_name = clipName;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
119
    } else if (!m_temporaryUrl.isEmpty()) {
120
        m_name = QFileInfo(m_temporaryUrl).fileName();
121 122
    } else {
        m_name = i18n("Untitled");
123
    }
124
    connect(m_markerModel.get(), &MarkerListModel::modelChanged, [&]() { setProducerProperty(QStringLiteral("kdenlive:markers"), m_markerModel->toJson()); });
125 126
}

127 128
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QDomElement &description, const QIcon &thumb,
                                                    std::shared_ptr<ProjectItemModel> model)
129
{
130
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, description, thumb, model));
131 132 133 134
    baseFinishConstruct(self);
    return self;
}

135 136
ProjectClip::~ProjectClip()
{
137
    // controller is deleted in bincontroller
138
    m_thumbMutex.lock();
139
    m_requestedThumbs.clear();
140
    m_thumbMutex.unlock();
141
    m_thumbThread.waitForFinished();
142
    audioFrameCache.clear();
143 144 145 146 147
    // delete all timeline producers
    std::map<int, std::shared_ptr<Mlt::Producer>>::iterator itr = m_timelineProducers.begin();
    while (itr != m_timelineProducers.end()) {
        itr = m_timelineProducers.erase(itr);
    }
148 149
}

150 151
void ProjectClip::connectEffectStack()
{
152 153 154 155
    connect(m_effectStack.get(), &EffectStackModel::modelChanged, this, &ProjectClip::updateChildProducers);
    connect(m_effectStack.get(), &EffectStackModel::dataChanged, this, &ProjectClip::updateChildProducers);
    /*connect(m_effectStack.get(), &EffectStackModel::modelChanged, [&](){
        qDebug()<<"/ / / STACK CHANGED";
156
        updateChildProducers();
157
    });*/
158 159
}

160 161
QString ProjectClip::getToolTip() const
{
162
    return url();
163 164
}

165
QString ProjectClip::getXmlProperty(const QDomElement &producer, const QString &propertyName, const QString &defaultValue)
166
{
167
    QString value = defaultValue;
168
    QDomNodeList props = producer.elementsByTagName(QStringLiteral("property"));
169
    for (int i = 0; i < props.count(); ++i) {
170
        if (props.at(i).toElement().attribute(QStringLiteral("name")) == propertyName) {
171 172 173 174 175 176 177
            value = props.at(i).firstChild().nodeValue();
            break;
        }
    }
    return value;
}

178
void ProjectClip::updateAudioThumbnail(QVariantList audioLevels)
179
{
180
    std::swap(audioFrameCache, audioLevels); // avoid second copy
181
    m_audioThumbCreated = true;
182 183 184 185
    if (auto ptr = m_model.lock()) {
        emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshAudioThumbs(m_binId);
    }
    updateTimelineClips({TimelineModel::AudioLevelsRole});
186 187 188 189
}

bool ProjectClip::audioThumbCreated() const
{
190
    return (m_audioThumbCreated);
191 192
}

193 194
ClipType ProjectClip::clipType() const
{
195
    return m_clipType;
196 197
}

198 199
bool ProjectClip::hasParent(const QString &id) const
{
200 201
    std::shared_ptr<AbstractProjectItem> par = parent();
    while (par) {
202 203 204 205
        if (par->clipId() == id) {
            return true;
        }
        par = par->parent();
206 207 208 209
    }
    return false;
}

210
std::shared_ptr<ProjectClip> ProjectClip::clip(const QString &id)
211
{
Nicolas Carion's avatar
Nicolas Carion committed
212
    if (id == m_binId) {
213
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
214
    }
215
    return std::shared_ptr<ProjectClip>();
216 217
}

218
std::shared_ptr<ProjectFolder> ProjectClip::folder(const QString &id)
219
{
220
    Q_UNUSED(id)
221
    return std::shared_ptr<ProjectFolder>();
222 223
}

224
std::shared_ptr<ProjectSubClip> ProjectClip::getSubClip(int in, int out)
225
{
226
    for (int i = 0; i < childCount(); ++i) {
227
        std::shared_ptr<ProjectSubClip> clip = std::static_pointer_cast<ProjectSubClip>(child(i))->subClip(in, out);
228 229 230 231
        if (clip) {
            return clip;
        }
    }
232
    return std::shared_ptr<ProjectSubClip>();
233 234
}

235 236 237
QStringList ProjectClip::subClipIds() const
{
    QStringList subIds;
238
    for (int i = 0; i < childCount(); ++i) {
239
        std::shared_ptr<AbstractProjectItem> clip = std::static_pointer_cast<AbstractProjectItem>(child(i));
240 241 242 243 244 245 246
        if (clip) {
            subIds << clip->clipId();
        }
    }
    return subIds;
}

247
std::shared_ptr<ProjectClip> ProjectClip::clipAt(int ix)
248
{
Nicolas Carion's avatar
Nicolas Carion committed
249
    if (ix == row()) {
250
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
251
    }
252
    return std::shared_ptr<ProjectClip>();
253 254
}

255
/*bool ProjectClip::isValid() const
256
{
257 258
    return m_controller->isValid();
}*/
259

260 261
bool ProjectClip::hasUrl() const
{
262
    if ((m_clipType != ClipType::Color) && (m_clipType != ClipType::Unknown)) {
263
        return (!clipUrl().isEmpty());
264
    }
265 266 267
    return false;
}

268
const QString ProjectClip::url() const
269
{
270
    return clipUrl();
271 272
}

273
GenTime ProjectClip::duration() const
274
{
275
    return getPlaytime();
276 277
}

278 279 280
int ProjectClip::frameDuration() const
{
    GenTime d = duration();
281
    return d.frames(pCore->getCurrentFps());
282 283
}

284
void ProjectClip::reloadProducer(bool refreshOnly)
285
{
286 287 288
    // we find if there are some loading job on that clip
    int loadjobId = -1;
    pCore->jobManager()->hasPendingJob(clipId(), AbstractClipJob::LOADJOB, &loadjobId);
289
    if (refreshOnly) {
290 291
        // In that case, we only want a new thumbnail.
        // We thus set up a thumb job. We must make sure that there is no pending LOADJOB
292 293 294
        // Clear cache first
        ThumbnailCache::get()->invalidateThumbsForClip(clipId());
        pCore->jobManager()->startJob<ThumbJob>({clipId()}, loadjobId, QString(), 150, -1, true, true);
295 296

    } else {
297 298 299 300
        //TODO: check if another load job is running?
        QDomDocument doc;
        QDomElement xml = toXml(doc);
        if (!xml.isNull()) {
301 302 303
            ThumbnailCache::get()->invalidateThumbsForClip(clipId());
            int loadJob = pCore->jobManager()->startJob<LoadJob>({clipId()}, -1, QString(), xml);
            pCore->jobManager()->startJob<ThumbJob>({clipId()}, loadJob, QString(), 150, -1, true, true);
304
        }
305
    }
306 307
}

308
QDomElement ProjectClip::toXml(QDomDocument &document, bool includeMeta)
309
{
310 311
    getProducerXML(document, includeMeta);
    QDomElement prod = document.documentElement().firstChildElement(QStringLiteral("producer"));
312
    if (m_clipType != ClipType::Unknown) {
313
        prod.setAttribute(QStringLiteral("type"), (int)m_clipType);
314
    }
315
    return prod;
316 317
}

Laurent Montel's avatar
Laurent Montel committed
318
void ProjectClip::setThumbnail(const QImage &img)
319
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
320 321
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
322
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
323
        QPainter p(&thumb);
324
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
325
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
326 327 328 329 330 331 332 333
        p.fillRect(r, c);
        QFont font = p.font();
        font.setPixelSize(r.height());
        font.setBold(true);
        p.setFont(font);
        p.setPen(Qt::black);
        p.drawText(r, Qt::AlignCenter, i18nc("The first letter of Proxy, used as abbreviation", "P"));
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
334
    m_thumbnail = QIcon(thumb);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
335 336 337
    if (auto ptr = m_model.lock()) {
        std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()), AbstractProjectItem::DataThumbnail);
    }
338 339
}

340 341 342 343 344
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

345
bool ProjectClip::setProducer(std::shared_ptr<Mlt::Producer> producer, bool replaceProducer)
346
{
Vincent Pinon's avatar
Vincent Pinon committed
347
    Q_UNUSED(replaceProducer)
348
    qDebug() << "################### ProjectClip::setproducer";
Nicolas Carion's avatar
Nicolas Carion committed
349
    updateProducer(std::move(producer));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
350
    connectEffectStack();
351

352 353 354
    // Update info
    if (m_name.isEmpty()) {
        m_name = clipName();
355
    }
356 357 358
    m_date = date;
    m_description = ClipController::description();
    m_temporaryUrl.clear();
359
    if (m_clipType == ClipType::Audio) {
360
        m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
361
    } else if (m_clipType == ClipType::Image) {
362
        if (getProducerIntProperty(QStringLiteral("meta.media.width")) < 8 || getProducerIntProperty(QStringLiteral("meta.media.height")) < 8) {
363 364
            KMessageBox::information(QApplication::activeWindow(),
                                     i18n("Image dimension smaller than 8 pixels.\nThis is not correctly supported by our video framework."));
365
        }
366
    }
367
    m_duration = getStringDuration();
368
    m_clipStatus = StatusReady;
369
    if (!hasProxy()) {
Nicolas Carion's avatar
Nicolas Carion committed
370
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshPanel(m_binId);
371
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
372
    if (auto ptr = m_model.lock()) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
373
        std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()), AbstractProjectItem::DataDuration);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
374
    }
375
    // Make sure we have a hash for this clip
376
    getFileHash();
377 378
    // set parent again (some info need to be stored in producer)
    updateParent(parentItem().lock());
379
    return true;
380 381
}

382
std::shared_ptr<Mlt::Producer> ProjectClip::thumbProducer()
383 384 385 386 387
{
    QMutexLocker locker(&m_producerMutex);
    if (m_thumbsProducer) {
        return m_thumbsProducer;
    }
388
    if (clipType() == ClipType::Unknown) {
Laurent Montel's avatar
Laurent Montel committed
389
        return nullptr;
390
    }
391 392
    std::shared_ptr<Mlt::Producer> prod = originalProducer();
    if (!prod->is_valid()) {
Laurent Montel's avatar
Laurent Montel committed
393
        return nullptr;
394
    }
395
    Clip clip(*prod.get());
396
    if (KdenliveSettings::gpu_accel()) {
397
        //TODO: when the original producer changes, we must reload this thumb producer
398
        m_thumbsProducer = std::make_shared<Mlt::Producer>(clip.softClone(ClipController::getPassPropertiesList()));
399 400
        Mlt::Filter scaler(*prod->profile(), "swscale");
        Mlt::Filter converter(*prod->profile(), "avcolor_space");
401 402
        m_thumbsProducer->attach(scaler);
        m_thumbsProducer->attach(converter);
403
    } else {
404
        m_thumbsProducer = std::make_shared<Mlt::Producer>(new Mlt::Producer(prod.get()));
405 406 407 408
    }
    return m_thumbsProducer;
}

409 410 411
std::shared_ptr<Mlt::Producer> ProjectClip::timelineProducer(PlaylistState::ClipState state, int track)
{
    if (!m_service.startsWith(QLatin1String("avformat"))) {
412
        std::shared_ptr<Mlt::Producer> prod(originalProducer()->cut());
413 414 415 416 417
        int length = getProducerIntProperty(QStringLiteral("kdenlive:duration"));
        if (length > 0) {
            prod->set_in_and_out(0, length);
        }
        return prod;
418 419 420
    }
    if (state == PlaylistState::VideoOnly) {
        if (m_timelineProducers.count(0) > 0) {
421
            return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(0)->second->cut());
422 423
        }
        std::shared_ptr<Mlt::Producer> videoProd = cloneProducer();
424
        videoProd->set("audio_index", -1);
425
        m_timelineProducers[0] = videoProd;
426
        return std::shared_ptr<Mlt::Producer>(videoProd->cut());
427 428 429
    }
    if (state == PlaylistState::AudioOnly) {
        if (m_timelineProducers.count(-track) > 0) {
430
            return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(-track)->second->cut());
431 432
        }
        std::shared_ptr<Mlt::Producer> audioProd = cloneProducer();
433
        audioProd->set("video_index", -1);
434
        m_timelineProducers[-track] = audioProd;
435
        return std::shared_ptr<Mlt::Producer>(audioProd->cut());
436 437
    }
    if (m_timelineProducers.count(track) > 0) {
438
        return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(track)->second->cut());
439 440 441
    }
    std::shared_ptr<Mlt::Producer> normalProd = cloneProducer();
    m_timelineProducers[track] = normalProd;
442
    return std::shared_ptr<Mlt::Producer>(normalProd->cut());
443 444 445
}

std::shared_ptr<Mlt::Producer> ProjectClip::cloneProducer()
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
{
    Mlt::Consumer c(*m_masterProducer->profile(), "xml", "string");
    Mlt::Service s(m_masterProducer->get_service());
    int ignore = s.get_int("ignore_points");
    if (ignore) {
        s.set("ignore_points", 0);
    }
    c.connect(s);
    c.set("time_format", "frames");
    c.set("no_meta", 1);
    c.set("no_root", 1);
    c.set("no_profile", 1);
    c.set("root", "/");
    c.set("store", "kdenlive");
    c.start();
    if (ignore) {
        s.set("ignore_points", ignore);
    }
    const QByteArray clipXml = c.get("string");
465
    std::shared_ptr<Mlt::Producer> prod(new Mlt::Producer(*m_masterProducer->profile(), "xml-string", clipXml.constData()));
466
    return prod;
467 468
}

469
bool ProjectClip::isReady() const
470
{
471
    return m_clipStatus == StatusReady;
472 473
}

474
/*void ProjectClip::setZone(const QPoint &zone)
475 476
{
    m_zone = zone;
477
}*/
478 479 480

QPoint ProjectClip::zone() const
{
481 482
    int x = getProducerIntProperty(QStringLiteral("kdenlive:zone_in"));
    int y = getProducerIntProperty(QStringLiteral("kdenlive:zone_out"));
483 484 485
    if (y <= x) {
        y = getFramePlaytime() - 1;
    }
486
    return QPoint(x, y);
487 488 489 490
}

const QString ProjectClip::hash()
{
491
    QString clipHash = getProducerProperty(QStringLiteral("kdenlive:file_hash"));
492 493
    if (!clipHash.isEmpty()) {
        return clipHash;
494
    }
495
    return getFileHash();
496 497
}

498
const QString ProjectClip::getFileHash()
499
{
500 501
    QByteArray fileData;
    QByteArray fileHash;
502
    switch (m_clipType) {
503
    case ClipType::SlideShow:
504
        fileData = clipUrl().toUtf8();
505 506
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
507
    case ClipType::Text:
508
    case ClipType::TextTemplate:
509
        fileData = getProducerProperty(QStringLiteral("xmldata")).toUtf8();
510 511
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
512
    case ClipType::QText:
513
        fileData = getProducerProperty(QStringLiteral("text")).toUtf8();
514 515
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
516
    case ClipType::Color:
517
        fileData = getProducerProperty(QStringLiteral("resource")).toUtf8();
518 519 520
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    default:
521
        QFile file(clipUrl());
522 523 524 525 526
        if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
            /*
             * 1 MB = 1 second per 450 files (or faster)
             * 10 MB = 9 seconds per 450 files (or faster)
             */
527 528
            if (file.size() > 2000000) {
                fileData = file.read(1000000);
529
                if (file.seek(file.size() - 1000000)) {
530
                    fileData.append(file.readAll());
531 532
                }
            } else {
533
                fileData = file.readAll();
534
            }
535
            file.close();
Nicolas Carion's avatar
Nicolas Carion committed
536
            ClipController::setProducerProperty(QStringLiteral("kdenlive:file_size"), QString::number(file.size()));
537
            fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
538 539 540 541
        }
        break;
    }
    if (fileHash.isEmpty()) {
542
        qDebug()<<"// WARNING EMPTY CLIP HASH: ";
543
        return QString();
544 545
    }
    QString result = fileHash.toHex();
Nicolas Carion's avatar
Nicolas Carion committed
546
    ClipController::setProducerProperty(QStringLiteral("kdenlive:file_hash"), result);
547
    return result;
548
}
549 550 551

double ProjectClip::getOriginalFps() const
{
552
    return originalFps();
553
}
554 555 556

bool ProjectClip::hasProxy() const
{
557
    QString proxy = getProducerProperty(QStringLiteral("kdenlive:proxy"));
558
    return proxy.size() > 2;
559 560
}

Laurent Montel's avatar
Laurent Montel committed
561
void ProjectClip::setProperties(const QMap<QString, QString> &properties, bool refreshPanel)
562 563
{
    QMapIterator<QString, QString> i(properties);
Laurent Montel's avatar
Laurent Montel committed
564
    QMap<QString, QString> passProperties;
565
    bool refreshAnalysis = false;
566
    bool reload = false;
567
    bool refreshOnly = true;
568 569
    // Some properties also need to be passed to track producers
    QStringList timelineProperties;
Laurent Montel's avatar
Laurent Montel committed
570 571
    if (properties.contains(QStringLiteral("templatetext"))) {
        m_description = properties.value(QStringLiteral("templatetext"));
572
        if (auto ptr = m_model.lock())
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
573
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()), AbstractProjectItem::ClipStatus);
574 575
        refreshPanel = true;
    }
Nicolas Carion's avatar
Nicolas Carion committed
576 577 578 579
    timelineProperties << QStringLiteral("force_aspect_ratio") << QStringLiteral("video_index") << QStringLiteral("audio_index")
                       << QStringLiteral("set.force_full_luma") << QStringLiteral("full_luma") << QStringLiteral("threads")
                       << QStringLiteral("force_colorspace") << QStringLiteral("force_tff") << QStringLiteral("force_progressive")
                       << QStringLiteral("force_fps");
580
    QStringList keys;
Nicolas Carion's avatar
Nicolas Carion committed
581 582
    keys << QStringLiteral("luma_duration") << QStringLiteral("luma_file") << QStringLiteral("fade") << QStringLiteral("ttl") << QStringLiteral("softness")
         << QStringLiteral("crop") << QStringLiteral("animation");
583 584
    while (i.hasNext()) {
        i.next();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
585
        setProducerProperty(i.key(), i.value());
586
        if (m_clipType == ClipType::SlideShow && keys.contains(i.key())) {
587
            reload = true;
588
            refreshOnly = false;
589
        }
590 591 592
        if (i.key().startsWith(QLatin1String("kdenlive:clipanalysis"))) {
            refreshAnalysis = true;
        }
593 594 595
        if (timelineProperties.contains(i.key())) {
            passProperties.insert(i.key(), i.value());
        }
596
    }
597 598
    if (properties.contains(QStringLiteral("kdenlive:proxy"))) {
        QString value = properties.value(QStringLiteral("kdenlive:proxy"));
599
        // If value is "-", that means user manually disabled proxy on this clip
600
        if (value.isEmpty() || value == QLatin1String("-")) {
601
            // reset proxy
602 603
            pCore->jobManager()->discardJobs(clipId(), AbstractClipJob::PROXYJOB);
            reloadProducer();
604
        } else {
605
            // A proxy was requested, make sure to keep original url
606
            setProducerProperty(QStringLiteral("kdenlive:originalurl"), url());
607
            pCore->jobManager()->startJob<ProxyJob>({clipId()}, -1, QString());
608
        }
Nicolas Carion's avatar
Nicolas Carion committed
609 610
    } else if (properties.contains(QStringLiteral("resource")) || properties.contains(QStringLiteral("templatetext")) ||
               properties.contains(QStringLiteral("autorotate"))) {
611
        // Clip resource changed, update thumbnail
612
        if (m_clipType != ClipType::Color) {
613
            reloadProducer();
614 615
        } else {
            reload = true;
616
        }
617
    }
618
    if (properties.contains(QStringLiteral("xmldata")) || !passProperties.isEmpty()) {
619
        reload = true;
620
    }
621 622 623
    if (refreshAnalysis) {
        emit refreshAnalysisPanel();
    }
624
    if (properties.contains(QStringLiteral("length")) || properties.contains(QStringLiteral("kdenlive:duration"))) {
625
        m_duration = getStringDuration();
626
        if (auto ptr = m_model.lock())
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
627
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()), AbstractProjectItem::DataDuration);
628 629
        refreshOnly = false;
        reload = true;
630 631
    }

632 633
    if (properties.contains(QStringLiteral("kdenlive:clipname"))) {
        m_name = properties.value(QStringLiteral("kdenlive:clipname"));
634
        refreshPanel = true;
635
        if (auto ptr = m_model.lock()) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
636
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()), AbstractProjectItem::DataName);
637 638 639
        }
        // update timeline clips
        updateTimelineClips(QVector<int>() << TimelineModel::NameRole);
640
    }
641 642 643 644
    if (refreshPanel) {
        // Some of the clip properties have changed through a command, update properties panel
        emit refreshPropertiesPanel();
    }
645
    if (reload) {
646
        // producer has changed, refresh monitor and thumbnail
647
        reloadProducer(refreshOnly);
648
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshClip(m_binId);
649
    }
650
    if (!passProperties.isEmpty()) {
651
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->updateTimelineProducers(m_binId, passProperties);
652
    }
653 654
}

655 656
ClipPropertiesController *ProjectClip::buildProperties(QWidget *parent)
{
657 658
    auto ptr = m_model.lock();
    Q_ASSERT(ptr);
659
    ClipPropertiesController *panel = new ClipPropertiesController(static_cast<ClipController *>(this), parent);
Laurent Montel's avatar
Laurent Montel committed
660 661
    connect(this, &ProjectClip::refreshPropertiesPanel, panel, &ClipPropertiesController::slotReloadProperties);
    connect(this, &ProjectClip::refreshAnalysisPanel, panel, &ClipPropertiesController::slotFillAnalysisData);
662 663 664
    return panel;
}

665
void ProjectClip::updateParent(std::shared_ptr<TreeItem> parent)
666
{
667 668 669 670 671 672
    if (parent) {
        auto item = std::static_pointer_cast<AbstractProjectItem>(parent);
        ClipController::setProducerProperty(QStringLiteral("kdenlive:folderid"), item->clipId());
        qDebug() << "Setting parent to " << item->clipId();
    }
    AbstractProjectItem::updateParent(parent);
673 674
}

Laurent Montel's avatar
Laurent Montel committed
675
bool ProjectClip::matches(const QString &condition)
676
{
Nicolas Carion's avatar
Nicolas Carion committed
677
    // TODO
678
    Q_UNUSED(condition)
679 680 681
    return true;
}

682
bool ProjectClip::rename(const QString &name, int column)
683
{
Laurent Montel's avatar
Laurent Montel committed