projectclip.cpp 45.4 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 24
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"
#include "bin.h"
Nicolas Carion's avatar
Nicolas Carion committed
25
#include "core.h"
26 27
#include "doc/kthumb.h"
#include "kdenlivesettings.h"
28
#include "lib/audio/audioStreamInfo.h"
Nicolas Carion's avatar
Nicolas Carion committed
29 30
#include "mltcontroller/bincontroller.h"
#include "mltcontroller/clipcontroller.h"
31
#include "mltcontroller/clippropertiescontroller.h"
32
#include "profiles/profilemodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
33 34 35 36 37 38 39
#include "project/projectcommands.h"
#include "projectfolder.h"
#include "projectitemmodel.h"
#include "projectsubclip.h"
#include "timecode.h"
#include "timeline/clip.h"
#include "utils/KoIconUtils.h"
40

Laurent Montel's avatar
Laurent Montel committed
41
#include "kdenlive_debug.h"
42
#include <KLocalizedString>
43
#include <KMessageBox>
Nicolas Carion's avatar
Nicolas Carion committed
44 45 46 47 48
#include <QCryptographicHash>
#include <QDir>
#include <QDomElement>
#include <QFile>
#include <QtConcurrent>
Nicolas Carion's avatar
Nicolas Carion committed
49
#include <utility>
50

Nicolas Carion's avatar
Nicolas Carion committed
51
ProjectClip::ProjectClip(const QString &id, const QIcon &thumb, ProjectItemModel *model, std::shared_ptr<Mlt::Producer> producer, ProjectFolder *parent)
52 53 54 55
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model, parent)
    , ClipController(pCore->binController(), producer)
    , m_abortAudioThumb(false)
    , m_thumbsProducer(nullptr)
56
{
57
    m_clipStatus = StatusReady;
58 59 60 61 62
    m_name = clipName();
    m_duration = getStringDuration();
    m_date = date;
    m_description = ClipController::description();
    m_type = clipType();
63
    if (m_type == Audio) {
64
        m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
65 66 67
    } else {
        m_thumbnail = thumb;
    }
68 69
    // Make sure we have a hash for this clip
    hash();
70
    connect(this, &ProjectClip::updateJobStatus, this, &ProjectClip::setJobStatus);
Nicolas Carion's avatar
Nicolas Carion committed
71 72
    static_cast<ProjectItemModel *>(m_model)->bin()->loadSubClips(id, getPropertiesFromPrefix(QStringLiteral("kdenlive:clipzone.")));
    connect(this, &ProjectClip::updateThumbProgress, static_cast<ProjectItemModel *>(m_model)->bin(), &Bin::doUpdateThumbsProgress);
73
    createAudioThumbs();
74 75
}

Nicolas Carion's avatar
Nicolas Carion committed
76
ProjectClip::ProjectClip(const QDomElement &description, const QIcon &thumb, ProjectItemModel *model, ProjectFolder *parent)
77 78 79 80 81
    : AbstractProjectItem(AbstractProjectItem::ClipItem, description, model, parent)
    , ClipController(pCore->binController(), ClipController::mediaUnavailable)
    , m_abortAudioThumb(false)
    , m_type(Unknown)
    , m_thumbsProducer(nullptr)
82
{
Laurent Montel's avatar
Laurent Montel committed
83
    Q_ASSERT(description.hasAttribute(QStringLiteral("id")));
84
    m_clipStatus = StatusWaiting;
85
    m_thumbnail = thumb;
86
    if (description.hasAttribute(QStringLiteral("type"))) {
Nicolas Carion's avatar
Nicolas Carion committed
87
        m_type = (ClipType)description.attribute(QStringLiteral("type")).toInt();
88
        if (m_type == Audio) {
89
            m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
90
        }
91
    }
92
    m_temporaryUrl = getXmlProperty(description, QStringLiteral("resource"));
93
    QString clipName = getXmlProperty(description, QStringLiteral("kdenlive:clipname"));
94 95
    if (!clipName.isEmpty()) {
        m_name = clipName;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
96
    } else if (!m_temporaryUrl.isEmpty()) {
97
        m_name = QFileInfo(m_temporaryUrl).fileName();
98 99
    } else {
        m_name = i18n("Untitled");
100
    }
101
    connect(this, &ProjectClip::updateJobStatus, this, &ProjectClip::setJobStatus);
Nicolas Carion's avatar
Nicolas Carion committed
102
    connect(this, &ProjectClip::updateThumbProgress, static_cast<ProjectItemModel *>(m_model)->bin(), &Bin::doUpdateThumbsProgress);
103 104 105 106
}

ProjectClip::~ProjectClip()
{
107
    // controller is deleted in bincontroller
108
    abortAudioThumbs();
Nicolas Carion's avatar
Nicolas Carion committed
109
    static_cast<ProjectItemModel *>(m_model)->bin()->slotAbortAudioThumb(m_id, duration().ms());
110 111
    QMutexLocker audioLock(&m_audioMutex);
    m_thumbMutex.lock();
112
    m_requestedThumbs.clear();
113
    m_thumbMutex.unlock();
114
    m_thumbThread.waitForFinished();
115
    delete m_thumbsProducer;
116
    audioFrameCache.clear();
117 118
}

119 120 121
void ProjectClip::abortAudioThumbs()
{
    m_abortAudioThumb = true;
122
    emit doAbortAudioThumbs();
123 124
}

125 126
QString ProjectClip::getToolTip() const
{
127
    return url();
128 129
}

130
QString ProjectClip::getXmlProperty(const QDomElement &producer, const QString &propertyName, const QString &defaultValue)
131
{
132
    QString value = defaultValue;
133
    QDomNodeList props = producer.elementsByTagName(QStringLiteral("property"));
134
    for (int i = 0; i < props.count(); ++i) {
135
        if (props.at(i).toElement().attribute(QStringLiteral("name")) == propertyName) {
136 137 138 139 140 141 142
            value = props.at(i).firstChild().nodeValue();
            break;
        }
    }
    return value;
}

Laurent Montel's avatar
Laurent Montel committed
143
void ProjectClip::updateAudioThumbnail(const QVariantList &audioLevels)
144
{
145
    audioFrameCache = audioLevels;
146
    m_audioThumbCreated = true;
Nicolas Carion's avatar
Nicolas Carion committed
147
    static_cast<ProjectItemModel *>(m_model)->bin()->emitRefreshAudioThumbs(m_id);
148 149 150
    emit gotAudioData();
}

151 152
QStringList ProjectClip::markersText(GenTime in, GenTime out) const
{
153
    return markerComments(in, out);
154 155
}

156 157
bool ProjectClip::audioThumbCreated() const
{
158
    return (m_audioThumbCreated);
159 160
}

161 162
ClipType ProjectClip::clipType() const
{
163
    return m_type;
164 165
}

166 167 168
bool ProjectClip::hasParent(const QString &id) const
{
    AbstractProjectItem *par = parent();
Nicolas Carion's avatar
Nicolas Carion committed
169
    while (par != nullptr) {
170 171 172 173
        if (par->clipId() == id) {
            return true;
        }
        par = par->parent();
174 175 176 177
    }
    return false;
}

178
ProjectClip *ProjectClip::clip(const QString &id)
179 180 181 182
{
    if (id == m_id) {
        return this;
    }
Laurent Montel's avatar
Laurent Montel committed
183
    return nullptr;
184 185
}

186
ProjectFolder *ProjectClip::folder(const QString &id)
187
{
188
    Q_UNUSED(id)
Laurent Montel's avatar
Laurent Montel committed
189
    return nullptr;
190 191
}

192
ProjectSubClip *ProjectClip::getSubClip(int in, int out)
193
{
194 195
    for (int i = 0; i < childCount(); ++i) {
        ProjectSubClip *clip = static_cast<ProjectSubClip *>(child(i))->subClip(in, out);
196 197 198 199
        if (clip) {
            return clip;
        }
    }
Laurent Montel's avatar
Laurent Montel committed
200
    return nullptr;
201 202
}

203 204 205
QStringList ProjectClip::subClipIds() const
{
    QStringList subIds;
206
    for (int i = 0; i < childCount(); ++i) {
Nicolas Carion's avatar
Nicolas Carion committed
207
        AbstractProjectItem *clip = static_cast<AbstractProjectItem *>(child(i));
208 209 210 211 212 213 214
        if (clip) {
            subIds << clip->clipId();
        }
    }
    return subIds;
}

215
ProjectClip *ProjectClip::clipAt(int ix)
216
{
Nicolas Carion's avatar
Nicolas Carion committed
217
    if (ix == row()) {
218 219
        return this;
    }
Laurent Montel's avatar
Laurent Montel committed
220
    return nullptr;
221 222
}

223
/*bool ProjectClip::isValid() const
224
{
225 226
    return m_controller->isValid();
}*/
227

228 229
bool ProjectClip::hasUrl() const
{
230 231
    if ((m_type != Color) && (m_type != Unknown)) {
        return (!clipUrl().isEmpty());
232
    }
233 234 235
    return false;
}

236
const QString ProjectClip::url() const
237
{
238
    return clipUrl();
239 240
}

241
GenTime ProjectClip::duration() const
242
{
243
    return getPlaytime();
244 245
}

246 247 248
int ProjectClip::frameDuration() const
{
    GenTime d = duration();
249
    return d.frames(pCore->getCurrentFps());
250 251
}

252
void ProjectClip::reloadProducer(bool refreshOnly)
253 254 255
{
    QDomDocument doc;
    QDomElement xml = toXml(doc);
256
    if (refreshOnly) {
257
        // set a special flag to request thumbnail only
258
        xml.setAttribute(QStringLiteral("refreshOnly"), QStringLiteral("1"));
259
    }
Nicolas Carion's avatar
Nicolas Carion committed
260
    static_cast<ProjectItemModel *>(m_model)->bin()->reloadProducer(m_id, xml);
261 262 263 264
}

void ProjectClip::setCurrent(bool current, bool notify)
{
265 266
    Q_UNUSED(notify);
    if (current) {
Nicolas Carion's avatar
Nicolas Carion committed
267 268
        static_cast<ProjectItemModel *>(m_model)->bin()->openProducer(static_cast<ClipController *>(this));
        static_cast<ProjectItemModel *>(m_model)->bin()->requestShowEffectStack(getEffectStack());
269 270 271
    }
}

272
QDomElement ProjectClip::toXml(QDomDocument &document, bool includeMeta)
273
{
274 275 276
    getProducerXML(document, includeMeta);
    QDomElement prod = document.documentElement().firstChildElement(QStringLiteral("producer"));
    if (m_type != Unknown) {
Nicolas Carion's avatar
Nicolas Carion committed
277
        prod.setAttribute(QStringLiteral("type"), (int)m_type);
278
    }
279
    return prod;
280 281
}

Laurent Montel's avatar
Laurent Montel committed
282
void ProjectClip::setThumbnail(const QImage &img)
283
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
284 285
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
286
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
287
        QPainter p(&thumb);
288
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
289
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
290 291 292 293 294 295 296 297
        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
298
    m_thumbnail = QIcon(thumb);
299
    emit thumbUpdated(img);
Nicolas Carion's avatar
Nicolas Carion committed
300
    static_cast<ProjectItemModel *>(m_model)->bin()->emitItemUpdated(this);
301 302
}

303 304 305 306 307
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

308
bool ProjectClip::setProducer(std::shared_ptr<Mlt::Producer> producer, bool replaceProducer)
309
{
310 311
    Q_UNUSED(replaceProducer);

Nicolas Carion's avatar
Nicolas Carion committed
312
    updateProducer(std::move(producer));
313 314 315 316

    // Update info
    if (m_name.isEmpty()) {
        m_name = clipName();
317
    }
318 319 320 321 322 323 324
    m_date = date;
    m_description = ClipController::description();
    m_temporaryUrl.clear();
    if (m_type == Unknown) {
        m_type = clipType();
        if (m_type == Audio) {
            m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
325
        }
326
    }
327
    m_duration = getStringDuration();
328
    m_clipStatus = StatusReady;
329
    if (!hasProxy()) {
Nicolas Carion's avatar
Nicolas Carion committed
330
        static_cast<ProjectItemModel *>(m_model)->bin()->emitRefreshPanel(m_id);
331
    }
Nicolas Carion's avatar
Nicolas Carion committed
332
    static_cast<ProjectItemModel *>(m_model)->bin()->emitItemUpdated(this);
333
    // Make sure we have a hash for this clip
334
    getFileHash();
335
    createAudioThumbs();
336
    return true;
337 338 339 340
}

void ProjectClip::createAudioThumbs()
{
341
    if (KdenliveSettings::audiothumbnails() && (m_type == AV || m_type == Audio || m_type == Playlist)) {
Nicolas Carion's avatar
Nicolas Carion committed
342
        static_cast<ProjectItemModel *>(m_model)->bin()->requestAudioThumbs(m_id, duration().ms());
343
        emit updateJobStatus(AbstractClipJob::THUMBJOB, JobWaiting, 0);
344
    }
345 346
}

347 348 349 350 351 352
Mlt::Producer *ProjectClip::thumbProducer()
{
    QMutexLocker locker(&m_producerMutex);
    if (m_thumbsProducer) {
        return m_thumbsProducer;
    }
353
    if (clipType() == Unknown) {
Laurent Montel's avatar
Laurent Montel committed
354
        return nullptr;
355
    }
356 357
    std::shared_ptr<Mlt::Producer> prod = originalProducer();
    if (!prod->is_valid()) {
Laurent Montel's avatar
Laurent Montel committed
358
        return nullptr;
359
    }
360
    Clip clip(*prod.get());
361
    if (KdenliveSettings::gpu_accel()) {
362
        m_thumbsProducer = clip.softClone(ClipController::getPassPropertiesList());
363 364
        Mlt::Filter scaler(*prod->profile(), "swscale");
        Mlt::Filter converter(*prod->profile(), "avcolor_space");
365 366
        m_thumbsProducer->attach(scaler);
        m_thumbsProducer->attach(converter);
367 368
    } else {
        m_thumbsProducer = clip.clone();
369 370 371 372
    }
    return m_thumbsProducer;
}

373
bool ProjectClip::isReady() const
374
{
375
    return m_clipStatus == StatusReady;
376 377
}

378
/*void ProjectClip::setZone(const QPoint &zone)
379 380
{
    m_zone = zone;
381
}*/
382 383 384

QPoint ProjectClip::zone() const
{
385 386
    int x = getProducerIntProperty(QStringLiteral("kdenlive:zone_in"));
    int y = getProducerIntProperty(QStringLiteral("kdenlive:zone_out"));
387
    return QPoint(x, y);
388 389 390 391
}

const QString ProjectClip::hash()
{
392
    QString clipHash = getProducerProperty(QStringLiteral("kdenlive:file_hash"));
393 394
    if (!clipHash.isEmpty()) {
        return clipHash;
395
    }
396
    return getFileHash();
397 398
}

399
const QString ProjectClip::getFileHash()
400
{
401 402 403
    QByteArray fileData;
    QByteArray fileHash;
    switch (m_type) {
404
    case SlideShow:
405
        fileData = clipUrl().toUtf8();
406 407 408
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    case Text:
409
        fileData = getProducerProperty(QStringLiteral("xmldata")).toUtf8();
410 411 412
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    case QText:
413
        fileData = getProducerProperty(QStringLiteral("text")).toUtf8();
414 415 416
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    case Color:
417
        fileData = getProducerProperty(QStringLiteral("resource")).toUtf8();
418 419 420
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    default:
421
        QFile file(clipUrl());
422 423 424 425 426
        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)
             */
427 428
            if (file.size() > 2000000) {
                fileData = file.read(1000000);
429
                if (file.seek(file.size() - 1000000)) {
430
                    fileData.append(file.readAll());
431 432
                }
            } else {
433
                fileData = file.readAll();
434
            }
435
            file.close();
Nicolas Carion's avatar
Nicolas Carion committed
436
            ClipController::setProducerProperty(QStringLiteral("kdenlive:file_size"), QString::number(file.size()));
437
            fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
438 439 440 441 442
        }
        break;
    }
    if (fileHash.isEmpty()) {
        return QString();
443 444
    }
    QString result = fileHash.toHex();
Nicolas Carion's avatar
Nicolas Carion committed
445
    ClipController::setProducerProperty(QStringLiteral("kdenlive:file_hash"), result);
446
    return result;
447
}
448 449 450

double ProjectClip::getOriginalFps() const
{
451
    return originalFps();
452
}
453 454 455

bool ProjectClip::hasProxy() const
{
456
    QString proxy = getProducerProperty(QStringLiteral("kdenlive:proxy"));
Nicolas Carion's avatar
Nicolas Carion committed
457
    return !proxy.isEmpty() || proxy == QLatin1String("-");
458 459
}

Laurent Montel's avatar
Laurent Montel committed
460
void ProjectClip::setProperties(const QMap<QString, QString> &properties, bool refreshPanel)
461 462
{
    QMapIterator<QString, QString> i(properties);
Laurent Montel's avatar
Laurent Montel committed
463
    QMap<QString, QString> passProperties;
464
    bool refreshAnalysis = false;
465
    bool reload = false;
466
    bool refreshOnly = true;
467 468
    // Some properties also need to be passed to track producers
    QStringList timelineProperties;
Laurent Montel's avatar
Laurent Montel committed
469 470
    if (properties.contains(QStringLiteral("templatetext"))) {
        m_description = properties.value(QStringLiteral("templatetext"));
Nicolas Carion's avatar
Nicolas Carion committed
471
        static_cast<ProjectItemModel *>(m_model)->bin()->emitItemUpdated(this);
472 473
        refreshPanel = true;
    }
Nicolas Carion's avatar
Nicolas Carion committed
474 475 476 477
    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");
478
    QStringList keys;
Nicolas Carion's avatar
Nicolas Carion committed
479 480
    keys << QStringLiteral("luma_duration") << QStringLiteral("luma_file") << QStringLiteral("fade") << QStringLiteral("ttl") << QStringLiteral("softness")
         << QStringLiteral("crop") << QStringLiteral("animation");
481 482
    while (i.hasNext()) {
        i.next();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
483
        setProducerProperty(i.key(), i.value());
484 485
        if (m_type == SlideShow && keys.contains(i.key())) {
            reload = true;
486
            refreshOnly = false;
487
        }
488 489 490
        if (i.key().startsWith(QLatin1String("kdenlive:clipanalysis"))) {
            refreshAnalysis = true;
        }
491 492 493
        if (timelineProperties.contains(i.key())) {
            passProperties.insert(i.key(), i.value());
        }
494
    }
495 496
    if (properties.contains(QStringLiteral("kdenlive:proxy"))) {
        QString value = properties.value(QStringLiteral("kdenlive:proxy"));
497
        // If value is "-", that means user manually disabled proxy on this clip
498
        if (value.isEmpty() || value == QLatin1String("-")) {
499
            // reset proxy
Nicolas Carion's avatar
Nicolas Carion committed
500 501
            if (static_cast<ProjectItemModel *>(m_model)->bin()->hasPendingJob(m_id, AbstractClipJob::PROXYJOB)) {
                static_cast<ProjectItemModel *>(m_model)->bin()->discardJobs(m_id, AbstractClipJob::PROXYJOB);
502
            } else {
503 504
                reloadProducer();
            }
505
        } else {
506
            // A proxy was requested, make sure to keep original url
507
            setProducerProperty(QStringLiteral("kdenlive:originalurl"), url());
Nicolas Carion's avatar
Nicolas Carion committed
508
            static_cast<ProjectItemModel *>(m_model)->bin()->startJob(m_id, AbstractClipJob::PROXYJOB);
509
        }
Nicolas Carion's avatar
Nicolas Carion committed
510 511
    } else if (properties.contains(QStringLiteral("resource")) || properties.contains(QStringLiteral("templatetext")) ||
               properties.contains(QStringLiteral("autorotate"))) {
512
        // Clip resource changed, update thumbnail
513
        if (m_type != Color) {
514
            reloadProducer();
515 516
        } else {
            reload = true;
517
        }
518
    }
519
    if (properties.contains(QStringLiteral("xmldata")) || !passProperties.isEmpty()) {
520
        reload = true;
521
    }
522 523 524
    if (refreshAnalysis) {
        emit refreshAnalysisPanel();
    }
525
    if (properties.contains(QStringLiteral("length")) || properties.contains(QStringLiteral("kdenlive:duration"))) {
526
        m_duration = getStringDuration();
Nicolas Carion's avatar
Nicolas Carion committed
527
        static_cast<ProjectItemModel *>(m_model)->bin()->emitItemUpdated(this);
528 529
    }

530 531
    if (properties.contains(QStringLiteral("kdenlive:clipname"))) {
        m_name = properties.value(QStringLiteral("kdenlive:clipname"));
532
        refreshPanel = true;
Nicolas Carion's avatar
Nicolas Carion committed
533
        static_cast<ProjectItemModel *>(m_model)->bin()->emitItemUpdated(this);
534
    }
535 536 537 538
    if (refreshPanel) {
        // Some of the clip properties have changed through a command, update properties panel
        emit refreshPropertiesPanel();
    }
539
    if (reload) {
540
        // producer has changed, refresh monitor and thumbnail
541
        reloadProducer(refreshOnly);
Nicolas Carion's avatar
Nicolas Carion committed
542
        static_cast<ProjectItemModel *>(m_model)->bin()->refreshClip(m_id);
543
    }
544
    if (!passProperties.isEmpty()) {
Nicolas Carion's avatar
Nicolas Carion committed
545
        static_cast<ProjectItemModel *>(m_model)->bin()->updateTimelineProducers(m_id, passProperties);
546
    }
547 548
}

549
void ProjectClip::setJobStatus(int jobType, int status, int progress, const QString &statusMessage)
550
{
Nicolas Carion's avatar
Nicolas Carion committed
551
    m_jobType = (AbstractClipJob::JOBTYPE)jobType;
552
    if (progress > 0) {
553 554 555
        if (m_jobProgress == progress) {
            return;
        }
556
        m_jobProgress = progress;
557
    } else {
558
        m_jobProgress = status;
559 560 561
        if (m_jobType == AbstractClipJob::PROXYJOB && (status == JobAborted || status == JobCrashed)) {
            setProducerProperty(QStringLiteral("kdenlive:proxy"), QStringLiteral("-"));
        }
Nicolas Carion's avatar
Nicolas Carion committed
562
        if ((status == JobAborted || status == JobCrashed || status == JobDone) && !statusMessage.isEmpty()) {
563
            m_jobMessage = statusMessage;
Nicolas Carion's avatar
Nicolas Carion committed
564
            static_cast<ProjectItemModel *>(m_model)->bin()->emitMessage(statusMessage, 100, OperationCompletedMessage);
565
        }
566
    }
Nicolas Carion's avatar
Nicolas Carion committed
567
    static_cast<ProjectItemModel *>(m_model)->bin()->emitItemUpdated(this);
568
}
569

570 571
ClipPropertiesController *ProjectClip::buildProperties(QWidget *parent)
{
Nicolas Carion's avatar
Nicolas Carion committed
572 573
    ClipPropertiesController *panel =
        new ClipPropertiesController(static_cast<ProjectItemModel *>(m_model)->bin()->projectTimecode(), static_cast<ClipController *>(this), parent);
Laurent Montel's avatar
Laurent Montel committed
574 575
    connect(this, &ProjectClip::refreshPropertiesPanel, panel, &ClipPropertiesController::slotReloadProperties);
    connect(this, &ProjectClip::refreshAnalysisPanel, panel, &ClipPropertiesController::slotFillAnalysisData);
576 577 578
    return panel;
}

579 580
void ProjectClip::updateParentInfo(const QString &folderid, const QString &foldername)
{
581
    Q_UNUSED(foldername);
Nicolas Carion's avatar
Nicolas Carion committed
582
    ClipController::setProducerProperty(QStringLiteral("kdenlive:folderid"), folderid);
583 584
}

Laurent Montel's avatar
Laurent Montel committed
585
bool ProjectClip::matches(const QString &condition)
586
{
Nicolas Carion's avatar
Nicolas Carion committed
587
    // TODO
588
    Q_UNUSED(condition)
589 590 591
    return true;
}

592
const QString ProjectClip::codec(bool audioCodec) const
593
{
594
    return codec(audioCodec);
595
}
596

597
bool ProjectClip::rename(const QString &name, int column)
598
{
Laurent Montel's avatar
Laurent Montel committed
599 600
    QMap<QString, QString> newProperites;
    QMap<QString, QString> oldProperites;
601 602
    bool edited = false;
    switch (column) {
603 604 605 606
    case 0:
        if (m_name == name) {
            return false;
        }
607
        // Rename clip
608 609
        oldProperites.insert(QStringLiteral("kdenlive:clipname"), m_name);
        newProperites.insert(QStringLiteral("kdenlive:clipname"), name);
610 611 612
        m_name = name;
        edited = true;
        break;
613 614 615 616
    case 2:
        if (m_description == name) {
            return false;
        }
617
        // Rename clip
618 619 620 621 622 623 624
        if (m_type == TextTemplate) {
            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);
        }
625 626 627 628 629
        m_description = name;
        edited = true;
        break;
    }
    if (edited) {
Nicolas Carion's avatar
Nicolas Carion committed
630
        static_cast<ProjectItemModel *>(m_model)->bin()->slotEditClipCommand(m_id, oldProperites, newProperites);
631 632
    }
    return edited;
633 634
}

Laurent Montel's avatar
Laurent Montel committed
635
void ProjectClip::addClipMarker(QList<CommentedTime> newMarkers, QUndoCommand *groupCommand)
636
{
Laurent Montel's avatar
Laurent Montel committed
637
    QList<CommentedTime> oldMarkers;
638
    for (int i = 0; i < newMarkers.count(); ++i) {
639
        CommentedTime oldMarker = markerAt(newMarkers.at(i).time());
640 641 642 643 644 645
        if (oldMarker == CommentedTime()) {
            oldMarker = newMarkers.at(i);
            oldMarker.setMarkerType(-1);
        }
        oldMarkers << oldMarker;
    }
Nicolas Carion's avatar
Nicolas Carion committed
646
    (void)new AddMarkerCommand(this, oldMarkers, newMarkers, groupCommand);
647 648 649 650
}

bool ProjectClip::deleteClipMarkers(QUndoCommand *command)
{
Laurent Montel's avatar
Laurent Montel committed
651
    QList<CommentedTime> markers = commentedSnapMarkers();
652 653 654
    if (markers.isEmpty()) {
        return false;
    }
Laurent Montel's avatar
Laurent Montel committed
655
    QList<CommentedTime> newMarkers;
656 657 658 659 660 661
    for (int i = 0; i < markers.size(); ++i) {
        CommentedTime marker = markers.at(i);
        marker.setMarkerType(-1);
        newMarkers << marker;
    }
    new AddMarkerCommand(this, markers, newMarkers, command);
662
    return true;
663 664
}

Laurent Montel's avatar
Laurent Montel committed
665
void ProjectClip::addMarkers(QList<CommentedTime> &markers)
666 667
{
    for (int i = 0; i < markers.count(); ++i) {
668
        if (markers.at(i).markerType() < 0) {
669
            deleteSnapMarker(markers.at(i).time());
670
        } else {
671
            addSnapMarker(markers.at(i));
672
        }
673 674
    }
    // refresh markers in clip monitor
Nicolas Carion's avatar
Nicolas Carion committed
675
    static_cast<ProjectItemModel *>(m_model)->bin()->refreshClipMarkers(m_id);
676 677 678
    // refresh markers in timeline clips
    emit refreshClipDisplay();
}
679

Nicolas Carion's avatar
Nicolas Carion committed
680
/*QVariant ProjectClip::getData(DataType type) const
681 682
{
    switch (type) {
683
    case AbstractProjectItem::IconOverlay:
684
        return hasEffects() ? QVariant("kdenlive-track_has_effect") : QVariant();
685 686 687
        break;
    default:
        break;
688
    }
Nicolas Carion's avatar
Nicolas Carion committed
689 690
    return AbstractProjectItem::getData(type);
    }*/
691

Laurent Montel's avatar
Laurent Montel committed
692
void ProjectClip::slotQueryIntraThumbs(const QList<int> &frames)
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
{
    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
709
    if (prod == nullptr || !prod->is_valid()) {
710 711
        return;
    }
712
    int fullWidth = 150 * prod->profile()->dar() + 0.5;
713 714 715 716 717 718
    int max = prod->get_length();
    int pos;
    while (!m_intraThumbs.isEmpty()) {
        m_intraThumbMutex.lock();
        pos = m_intraThumbs.takeFirst();
        m_intraThumbMutex.unlock();
719 720 721
        if (pos >= max) {
            pos = max - 1;
        }
Laurent Montel's avatar
Laurent Montel committed
722
        const QString path = url() + QLatin1Char('_') + QString::number(pos);
Nicolas Carion's avatar
Nicolas Carion committed
723
        QImage img = static_cast<ProjectItemModel *>(m_model)->bin()->findCachedPixmap(path);
724 725 726