projectclip.cpp 47.8 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"
Nicolas Carion's avatar
Nicolas Carion committed
24
#include "core.h"
25 26
#include "doc/docundostack.hpp"
#include "doc/kdenlivedoc.h"
27 28
#include "doc/kthumb.h"
#include "kdenlivesettings.h"
29
#include "lib/audio/audioStreamInfo.h"
Nicolas Carion's avatar
Nicolas Carion committed
30 31
#include "mltcontroller/bincontroller.h"
#include "mltcontroller/clipcontroller.h"
32
#include "mltcontroller/clippropertiescontroller.h"
33
#include "model/markerlistmodel.hpp"
34
#include "profiles/profilemodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
35
#include "project/projectcommands.h"
36
#include "project/projectmanager.h"
Nicolas Carion's avatar
Nicolas Carion committed
37 38 39 40 41
#include "projectfolder.h"
#include "projectitemmodel.h"
#include "projectsubclip.h"
#include "timecode.h"
#include "timeline/clip.h"
Nicolas Carion's avatar
style  
Nicolas Carion committed
42
#include "timeline2/model/snapmodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
43
#include "utils/KoIconUtils.h"
44 45
#include <kimagecache.h>
#include <QPainter>
46

Laurent Montel's avatar
Laurent Montel committed
47
#include "kdenlive_debug.h"
48
#include <KLocalizedString>
49
#include <KMessageBox>
Nicolas Carion's avatar
Nicolas Carion committed
50 51 52 53 54
#include <QCryptographicHash>
#include <QDir>
#include <QDomElement>
#include <QFile>
#include <QtConcurrent>
Nicolas Carion's avatar
Nicolas Carion committed
55
#include <utility>
56

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

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

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

130
std::shared_ptr<ProjectClip> ProjectClip::construct(const QDomElement &description, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model)
131
{
132
    std::shared_ptr<ProjectClip> self(new ProjectClip(description, thumb, model));
133
    baseFinishConstruct(self);
134
    pCore->binController()->addClipToBin(description.attribute(QStringLiteral("id")), self, true);
135 136 137
    return self;
}

138 139
ProjectClip::~ProjectClip()
{
140
    // controller is deleted in bincontroller
141
    abortAudioThumbs();
142 143
    QMutexLocker audioLock(&m_audioMutex);
    m_thumbMutex.lock();
144
    m_requestedThumbs.clear();
145
    m_thumbMutex.unlock();
146
    m_thumbThread.waitForFinished();
147
    delete m_thumbsProducer;
148
    audioFrameCache.clear();
149 150
}

151 152 153
void ProjectClip::abortAudioThumbs()
{
    m_abortAudioThumb = true;
154
    emit doAbortAudioThumbs();
155 156
}

157 158
QString ProjectClip::getToolTip() const
{
159
    return url();
160 161
}

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

Laurent Montel's avatar
Laurent Montel committed
175
void ProjectClip::updateAudioThumbnail(const QVariantList &audioLevels)
176
{
177
    audioFrameCache = audioLevels;
178
    m_audioThumbCreated = true;
Nicolas Carion's avatar
Nicolas Carion committed
179
    if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshAudioThumbs(m_binId);
180 181 182 183 184
    emit gotAudioData();
}

bool ProjectClip::audioThumbCreated() const
{
185
    return (m_audioThumbCreated);
186 187
}

188 189
ClipType ProjectClip::clipType() const
{
190
    return m_clipType;
191 192
}

193 194
bool ProjectClip::hasParent(const QString &id) const
{
195 196
    std::shared_ptr<AbstractProjectItem> par = parent();
    while (par) {
197 198 199 200
        if (par->clipId() == id) {
            return true;
        }
        par = par->parent();
201 202 203 204
    }
    return false;
}

205
std::shared_ptr<ProjectClip> ProjectClip::clip(const QString &id)
206
{
Nicolas Carion's avatar
Nicolas Carion committed
207
    if (id == m_binId) {
208
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
209
    }
210
    return std::shared_ptr<ProjectClip>();
211 212
}

213
std::shared_ptr<ProjectFolder> ProjectClip::folder(const QString &id)
214
{
215
    Q_UNUSED(id)
216
    return std::shared_ptr<ProjectFolder>();
217 218
}

219
std::shared_ptr<ProjectSubClip> ProjectClip::getSubClip(int in, int out)
220
{
221
    for (int i = 0; i < childCount(); ++i) {
222
        std::shared_ptr<ProjectSubClip> clip = std::static_pointer_cast<ProjectSubClip>(child(i))->subClip(in, out);
223 224 225 226
        if (clip) {
            return clip;
        }
    }
227
    return std::shared_ptr<ProjectSubClip>();
228 229
}

230 231 232
QStringList ProjectClip::subClipIds() const
{
    QStringList subIds;
233
    for (int i = 0; i < childCount(); ++i) {
234
        std::shared_ptr<AbstractProjectItem> clip = std::static_pointer_cast<AbstractProjectItem>(child(i));
235 236 237 238 239 240 241
        if (clip) {
            subIds << clip->clipId();
        }
    }
    return subIds;
}

242
std::shared_ptr<ProjectClip> ProjectClip::clipAt(int ix)
243
{
Nicolas Carion's avatar
Nicolas Carion committed
244
    if (ix == row()) {
245
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
246
    }
247
    return std::shared_ptr<ProjectClip>();
248 249
}

250
/*bool ProjectClip::isValid() const
251
{
252 253
    return m_controller->isValid();
}*/
254

255 256
bool ProjectClip::hasUrl() const
{
257
    if ((m_clipType != Color) && (m_clipType != Unknown)) {
258
        return (!clipUrl().isEmpty());
259
    }
260 261 262
    return false;
}

263
const QString ProjectClip::url() const
264
{
265
    return clipUrl();
266 267
}

268
GenTime ProjectClip::duration() const
269
{
270
    return getPlaytime();
271 272
}

273 274 275
int ProjectClip::frameDuration() const
{
    GenTime d = duration();
276
    return d.frames(pCore->getCurrentFps());
277 278
}

279
void ProjectClip::reloadProducer(bool refreshOnly)
280 281 282
{
    QDomDocument doc;
    QDomElement xml = toXml(doc);
283
    if (refreshOnly) {
284
        // set a special flag to request thumbnail only
285
        xml.setAttribute(QStringLiteral("refreshOnly"), QStringLiteral("1"));
286
    }
Nicolas Carion's avatar
Nicolas Carion committed
287
    if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->reloadProducer(m_binId, xml);
288 289
}

290
QDomElement ProjectClip::toXml(QDomDocument &document, bool includeMeta)
291
{
292 293
    getProducerXML(document, includeMeta);
    QDomElement prod = document.documentElement().firstChildElement(QStringLiteral("producer"));
294 295
    if (m_clipType != Unknown) {
        prod.setAttribute(QStringLiteral("type"), (int)m_clipType);
296
    }
297
    return prod;
298 299
}

Laurent Montel's avatar
Laurent Montel committed
300
void ProjectClip::setThumbnail(const QImage &img)
301
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
302 303
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
304
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
305
        QPainter p(&thumb);
306
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
307
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
308 309 310 311 312 313 314 315
        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
316
    m_thumbnail = QIcon(thumb);
317
    emit thumbUpdated(img);
318
    if (auto ptr = m_model.lock()) std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
319 320
}

321 322 323 324 325
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

326
bool ProjectClip::setProducer(std::shared_ptr<Mlt::Producer> producer, bool replaceProducer)
327
{
328
    Q_UNUSED(replaceProducer);
Nicolas Carion's avatar
Nicolas Carion committed
329
    updateProducer(std::move(producer));
330

331 332 333
    // Update info
    if (m_name.isEmpty()) {
        m_name = clipName();
334
    }
335 336 337
    m_date = date;
    m_description = ClipController::description();
    m_temporaryUrl.clear();
338 339
    if (m_clipType == Audio) {
        m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
340
    }
341
    m_duration = getStringDuration();
342
    m_clipStatus = StatusReady;
343
    if (!hasProxy()) {
Nicolas Carion's avatar
Nicolas Carion committed
344
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshPanel(m_binId);
345
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
346 347 348
    if (auto ptr = m_model.lock()) {
        std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
    }
349
    // Make sure we have a hash for this clip
350
    getFileHash();
351
    createAudioThumbs();
352
    return true;
353 354 355 356
}

void ProjectClip::createAudioThumbs()
{
357
    if (KdenliveSettings::audiothumbnails() && (m_clipType == AV || m_clipType == Audio || m_clipType == Playlist)) {
Nicolas Carion's avatar
Nicolas Carion committed
358
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->requestAudioThumbs(m_binId, duration().ms());
359
        emit updateJobStatus(AbstractClipJob::THUMBJOB, JobWaiting, 0);
360
    }
361 362
}

363 364 365 366 367 368
Mlt::Producer *ProjectClip::thumbProducer()
{
    QMutexLocker locker(&m_producerMutex);
    if (m_thumbsProducer) {
        return m_thumbsProducer;
    }
369
    if (clipType() == Unknown) {
Laurent Montel's avatar
Laurent Montel committed
370
        return nullptr;
371
    }
372 373
    std::shared_ptr<Mlt::Producer> prod = originalProducer();
    if (!prod->is_valid()) {
Laurent Montel's avatar
Laurent Montel committed
374
        return nullptr;
375
    }
376
    Clip clip(*prod.get());
377
    if (KdenliveSettings::gpu_accel()) {
378
        m_thumbsProducer = clip.softClone(ClipController::getPassPropertiesList());
379 380
        Mlt::Filter scaler(*prod->profile(), "swscale");
        Mlt::Filter converter(*prod->profile(), "avcolor_space");
381 382
        m_thumbsProducer->attach(scaler);
        m_thumbsProducer->attach(converter);
383 384
    } else {
        m_thumbsProducer = clip.clone();
385 386 387 388
    }
    return m_thumbsProducer;
}

389
bool ProjectClip::isReady() const
390
{
391
    return m_clipStatus == StatusReady;
392 393
}

394
/*void ProjectClip::setZone(const QPoint &zone)
395 396
{
    m_zone = zone;
397
}*/
398 399 400

QPoint ProjectClip::zone() const
{
401 402
    int x = getProducerIntProperty(QStringLiteral("kdenlive:zone_in"));
    int y = getProducerIntProperty(QStringLiteral("kdenlive:zone_out"));
403
    return QPoint(x, y);
404 405 406 407
}

const QString ProjectClip::hash()
{
408
    QString clipHash = getProducerProperty(QStringLiteral("kdenlive:file_hash"));
409 410
    if (!clipHash.isEmpty()) {
        return clipHash;
411
    }
412
    return getFileHash();
413 414
}

415
const QString ProjectClip::getFileHash()
416
{
417 418
    QByteArray fileData;
    QByteArray fileHash;
419
    switch (m_clipType) {
420
    case SlideShow:
421
        fileData = clipUrl().toUtf8();
422 423 424
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    case Text:
425
        fileData = getProducerProperty(QStringLiteral("xmldata")).toUtf8();
426 427 428
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    case QText:
429
        fileData = getProducerProperty(QStringLiteral("text")).toUtf8();
430 431 432
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    case Color:
433
        fileData = getProducerProperty(QStringLiteral("resource")).toUtf8();
434 435 436
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    default:
437
        QFile file(clipUrl());
438 439 440 441 442
        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)
             */
443 444
            if (file.size() > 2000000) {
                fileData = file.read(1000000);
445
                if (file.seek(file.size() - 1000000)) {
446
                    fileData.append(file.readAll());
447 448
                }
            } else {
449
                fileData = file.readAll();
450
            }
451
            file.close();
Nicolas Carion's avatar
Nicolas Carion committed
452
            ClipController::setProducerProperty(QStringLiteral("kdenlive:file_size"), QString::number(file.size()));
453
            fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
454 455 456 457 458
        }
        break;
    }
    if (fileHash.isEmpty()) {
        return QString();
459 460
    }
    QString result = fileHash.toHex();
Nicolas Carion's avatar
Nicolas Carion committed
461
    ClipController::setProducerProperty(QStringLiteral("kdenlive:file_hash"), result);
462
    return result;
463
}
464 465 466

double ProjectClip::getOriginalFps() const
{
467
    return originalFps();
468
}
469

470

471 472
bool ProjectClip::hasProxy() const
{
473
    QString proxy = getProducerProperty(QStringLiteral("kdenlive:proxy"));
474
    return proxy.size() > 2;
475 476
}

Laurent Montel's avatar
Laurent Montel committed
477
void ProjectClip::setProperties(const QMap<QString, QString> &properties, bool refreshPanel)
478 479
{
    QMapIterator<QString, QString> i(properties);
Laurent Montel's avatar
Laurent Montel committed
480
    QMap<QString, QString> passProperties;
481
    bool refreshAnalysis = false;
482
    bool reload = false;
483
    bool refreshOnly = true;
484 485
    // Some properties also need to be passed to track producers
    QStringList timelineProperties;
Laurent Montel's avatar
Laurent Montel committed
486 487
    if (properties.contains(QStringLiteral("templatetext"))) {
        m_description = properties.value(QStringLiteral("templatetext"));
488 489
        if (auto ptr = m_model.lock())
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
490 491
        refreshPanel = true;
    }
Nicolas Carion's avatar
Nicolas Carion committed
492 493 494 495
    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");
496
    QStringList keys;
Nicolas Carion's avatar
Nicolas Carion committed
497 498
    keys << QStringLiteral("luma_duration") << QStringLiteral("luma_file") << QStringLiteral("fade") << QStringLiteral("ttl") << QStringLiteral("softness")
         << QStringLiteral("crop") << QStringLiteral("animation");
499 500
    while (i.hasNext()) {
        i.next();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
501
        setProducerProperty(i.key(), i.value());
502
        if (m_clipType == SlideShow && keys.contains(i.key())) {
503
            reload = true;
504
            refreshOnly = false;
505
        }
506 507 508
        if (i.key().startsWith(QLatin1String("kdenlive:clipanalysis"))) {
            refreshAnalysis = true;
        }
509 510 511
        if (timelineProperties.contains(i.key())) {
            passProperties.insert(i.key(), i.value());
        }
512
    }
513 514
    if (properties.contains(QStringLiteral("kdenlive:proxy"))) {
        QString value = properties.value(QStringLiteral("kdenlive:proxy"));
515
        // If value is "-", that means user manually disabled proxy on this clip
516
        if (value.isEmpty() || value == QLatin1String("-")) {
517
            // reset proxy
518
            if (auto ptr = m_model.lock()) {
519 520
                emit std::static_pointer_cast<ProjectItemModel>(ptr)->discardJobs(m_binId, AbstractClipJob::PROXYJOB);
                reloadProducer();
521
            }
522
        } else {
523
            // A proxy was requested, make sure to keep original url
524
            setProducerProperty(QStringLiteral("kdenlive:originalurl"), url());
525 526 527
            if (auto ptr = m_model.lock()) {
                emit std::static_pointer_cast<ProjectItemModel>(ptr)->startJob(m_binId, AbstractClipJob::PROXYJOB);
            }
528
        }
Nicolas Carion's avatar
Nicolas Carion committed
529 530
    } else if (properties.contains(QStringLiteral("resource")) || properties.contains(QStringLiteral("templatetext")) ||
               properties.contains(QStringLiteral("autorotate"))) {
531
        // Clip resource changed, update thumbnail
532
        if (m_clipType != Color) {
533
            reloadProducer();
534 535
        } else {
            reload = true;
536
        }
537
    }
538
    if (properties.contains(QStringLiteral("xmldata")) || !passProperties.isEmpty()) {
539
        reload = true;
540
    }
541 542 543
    if (refreshAnalysis) {
        emit refreshAnalysisPanel();
    }
544
    if (properties.contains(QStringLiteral("length")) || properties.contains(QStringLiteral("kdenlive:duration"))) {
545
        m_duration = getStringDuration();
546 547
        if (auto ptr = m_model.lock())
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
548 549
        refreshOnly = false;
        reload = true;
550 551
    }

552 553
    if (properties.contains(QStringLiteral("kdenlive:clipname"))) {
        m_name = properties.value(QStringLiteral("kdenlive:clipname"));
554
        refreshPanel = true;
555 556
        if (auto ptr = m_model.lock())
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
557
    }
558 559 560 561
    if (refreshPanel) {
        // Some of the clip properties have changed through a command, update properties panel
        emit refreshPropertiesPanel();
    }
562
    if (reload) {
563
        // producer has changed, refresh monitor and thumbnail
564
        reloadProducer(refreshOnly);
565
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshClip(m_binId);
566
    }
567
    if (!passProperties.isEmpty()) {
568
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->updateTimelineProducers(m_binId, passProperties);
569
    }
570 571
}

572
void ProjectClip::setJobStatus(int jobType, int status, int progress, const QString &statusMessage)
573
{
Nicolas Carion's avatar
Nicolas Carion committed
574
    m_jobType = (AbstractClipJob::JOBTYPE)jobType;
575
    if (progress > 0) {
576 577 578
        if (m_jobProgress == progress) {
            return;
        }
579
        m_jobProgress = progress;
580
    } else {
581
        m_jobProgress = status;
582 583 584
        if (m_jobType == AbstractClipJob::PROXYJOB && (status == JobAborted || status == JobCrashed)) {
            setProducerProperty(QStringLiteral("kdenlive:proxy"), QStringLiteral("-"));
        }
Nicolas Carion's avatar
Nicolas Carion committed
585
        if ((status == JobAborted || status == JobCrashed || status == JobDone) && !statusMessage.isEmpty()) {
586
            m_jobMessage = statusMessage;
587
            if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->emitMessage(statusMessage, 100, OperationCompletedMessage);
588
        }
589
    }
590
    if (auto ptr = m_model.lock()) std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
591
}
592

593 594
ClipPropertiesController *ProjectClip::buildProperties(QWidget *parent)
{
595 596
    auto ptr = m_model.lock();
    Q_ASSERT(ptr);
Nicolas Carion's avatar
Nicolas Carion committed
597
    ClipPropertiesController *panel =
598
        new ClipPropertiesController(static_cast<ClipController *>(this), parent);
Laurent Montel's avatar
Laurent Montel committed
599 600
    connect(this, &ProjectClip::refreshPropertiesPanel, panel, &ClipPropertiesController::slotReloadProperties);
    connect(this, &ProjectClip::refreshAnalysisPanel, panel, &ClipPropertiesController::slotFillAnalysisData);
601 602 603
    return panel;
}

604 605
void ProjectClip::updateParentInfo(const QString &folderid, const QString &foldername)
{
606
    Q_UNUSED(foldername);
Nicolas Carion's avatar
Nicolas Carion committed
607
    ClipController::setProducerProperty(QStringLiteral("kdenlive:folderid"), folderid);
608 609
}

Laurent Montel's avatar
Laurent Montel committed
610
bool ProjectClip::matches(const QString &condition)
611
{
Nicolas Carion's avatar
Nicolas Carion committed
612
    // TODO
613
    Q_UNUSED(condition)
614 615 616
    return true;
}

617
bool ProjectClip::rename(const QString &name, int column)
618
{
Laurent Montel's avatar
Laurent Montel committed
619 620
    QMap<QString, QString> newProperites;
    QMap<QString, QString> oldProperites;
621 622
    bool edited = false;
    switch (column) {
623 624 625 626
    case 0:
        if (m_name == name) {
            return false;
        }
627
        // Rename clip
628 629
        oldProperites.insert(QStringLiteral("kdenlive:clipname"), m_name);
        newProperites.insert(QStringLiteral("kdenlive:clipname"), name);
630 631 632
        m_name = name;
        edited = true;
        break;
633 634 635 636
    case 2:
        if (m_description == name) {
            return false;
        }
637
        // Rename clip
638
        if (m_clipType == TextTemplate) {
639 640 641 642 643 644
            oldProperites.insert(QStringLiteral("templatetext"), m_description);
            newProperites.insert(QStringLiteral("templatetext"), name);
        } else {
            oldProperites.insert(QStringLiteral("kdenlive:description"), m_description);
            newProperites.insert(QStringLiteral("kdenlive:description"), name);
        }
645 646 647 648 649
        m_description = name;
        edited = true;
        break;
    }
    if (edited) {
650 651
        // TODO refac
        //if (auto ptr = m_model.lock()) std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->slotEditClipCommand(m_binId, oldProperites, newProperites);
652 653
    }
    return edited;
654 655
}

Nicolas Carion's avatar
Nicolas Carion committed
656
/*QVariant ProjectClip::getData(DataType type) const
657 658
{
    switch (type) {
659
    case AbstractProjectItem::IconOverlay:
660
        return hasEffects() ? QVariant("kdenlive-track_has_effect") : QVariant();
661 662 663
        break;
    default:
        break;
664
    }
Nicolas Carion's avatar
Nicolas Carion committed
665 666
    return AbstractProjectItem::getData(type);
    }*/
667

Laurent Montel's avatar
Laurent Montel committed
668
void ProjectClip::slotQueryIntraThumbs(const QList<int> &frames)
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
{
    QMutexLocker lock(&m_intraThumbMutex);
    for (int i = 0; i < frames.count(); i++) {
        if (!m_intraThumbs.contains(frames.at(i))) {
            m_intraThumbs << frames.at(i);
        }
    }
    qSort(m_intraThumbs);
    if (!m_intraThread.isRunning()) {
        m_intraThread = QtConcurrent::run(this, &ProjectClip::doExtractIntra);
    }
}

void ProjectClip::doExtractIntra()
{
    Mlt::Producer *prod = thumbProducer();
Laurent Montel's avatar
Laurent Montel committed
685
    if (prod == nullptr || !prod->is_valid()) {
686 687
        return;
    }
688
    int fullWidth = 150 * prod->profile()->dar() + 0.5;
689 690 691 692 693 694
    int max = prod->get_length();
    int pos;
    while (!m_intraThumbs.isEmpty()) {
        m_intraThumbMutex.lock();
        pos = m_intraThumbs.takeFirst();
        m_intraThumbMutex.unlock();
695 696 697
        if (pos >= max) {
            pos = max - 1;
        }
Laurent Montel's avatar
Laurent Montel committed
698
        const QString path = url() + QLatin1Char('_') + QString::number(pos);