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

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

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

114 115 116
void ProjectClip::abortAudioThumbs()
{
    m_abortAudioThumb = true;
117
    emit doAbortAudioThumbs();
118 119
}

120 121
QString ProjectClip::getToolTip() const
{
122
    return url();
123 124
}

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

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

146 147
QStringList ProjectClip::markersText(GenTime in, GenTime out) const
{
148
    return markerComments(in, out);
149 150
}

151 152
bool ProjectClip::audioThumbCreated() const
{
153
    return (m_audioThumbCreated);
154 155
}

156 157
ClipType ProjectClip::clipType() const
{
158
    return m_type;
159 160
}

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

173
ProjectClip *ProjectClip::clip(const QString &id)
174 175 176 177
{
    if (id == m_id) {
        return this;
    }
Laurent Montel's avatar
Laurent Montel committed
178
    return nullptr;
179 180
}

181
ProjectFolder *ProjectClip::folder(const QString &id)
182
{
183
    Q_UNUSED(id)
Laurent Montel's avatar
Laurent Montel committed
184
    return nullptr;
185 186
}

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

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

210
ProjectClip *ProjectClip::clipAt(int ix)
211
{
Nicolas Carion's avatar
Nicolas Carion committed
212
    if (ix == row()) {
213 214
        return this;
    }
Laurent Montel's avatar
Laurent Montel committed
215
    return nullptr;
216 217
}

218
/*bool ProjectClip::isValid() const
219
{
220 221
    return m_controller->isValid();
}*/
222

223 224
bool ProjectClip::hasUrl() const
{
225 226
    if ((m_type != Color) && (m_type != Unknown)) {
        return (!clipUrl().isEmpty());
227
    }
228 229 230
    return false;
}

231
const QString ProjectClip::url() const
232
{
233
    return clipUrl();
234 235
}

236
GenTime ProjectClip::duration() const
237
{
238
    return getPlaytime();
239 240
}

241 242 243
int ProjectClip::frameDuration() const
{
    GenTime d = duration();
244
    return d.frames(pCore->getCurrentFps());
245 246
}

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

void ProjectClip::setCurrent(bool current, bool notify)
{
260 261
    Q_UNUSED(notify);
    if (current) {
Nicolas Carion's avatar
Nicolas Carion committed
262 263
        static_cast<ProjectItemModel *>(m_model)->bin()->openProducer(static_cast<ClipController *>(this));
        static_cast<ProjectItemModel *>(m_model)->bin()->requestShowEffectStack(getEffectStack());
264 265 266
    }
}

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

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

298 299 300 301 302
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

303
bool ProjectClip::setProducer(std::shared_ptr<Mlt::Producer> producer, bool replaceProducer)
304
{
305 306
    Q_UNUSED(replaceProducer);

Nicolas Carion's avatar
Nicolas Carion committed
307
    updateProducer(std::move(producer));
308 309 310 311

    // Update info
    if (m_name.isEmpty()) {
        m_name = clipName();
312
    }
313 314 315 316 317 318 319
    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"));
320
        }
321
    }
322
    m_duration = getStringDuration();
323
    m_clipStatus = StatusReady;
324
    if (!hasProxy()) {
Nicolas Carion's avatar
Nicolas Carion committed
325
        static_cast<ProjectItemModel *>(m_model)->bin()->emitRefreshPanel(m_id);
326
    }
Nicolas Carion's avatar
Nicolas Carion committed
327
    static_cast<ProjectItemModel *>(m_model)->bin()->emitItemUpdated(this);
328
    // Make sure we have a hash for this clip
329
    getFileHash();
330
    createAudioThumbs();
331
    return true;
332 333 334 335
}

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

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

368
bool ProjectClip::isReady() const
369
{
370
    return m_clipStatus == StatusReady;
371 372
}

373
/*void ProjectClip::setZone(const QPoint &zone)
374 375
{
    m_zone = zone;
376
}*/
377 378 379

QPoint ProjectClip::zone() const
{
380 381
    int x = getProducerIntProperty(QStringLiteral("kdenlive:zone_in"));
    int y = getProducerIntProperty(QStringLiteral("kdenlive:zone_out"));
382
    return QPoint(x, y);
383 384 385 386
}

const QString ProjectClip::hash()
{
387
    QString clipHash = getProducerProperty(QStringLiteral("kdenlive:file_hash"));
388 389
    if (!clipHash.isEmpty()) {
        return clipHash;
390
    }
391
    return getFileHash();
392 393
}

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

double ProjectClip::getOriginalFps() const
{
446
    return originalFps();
447
}
448 449 450

bool ProjectClip::hasProxy() const
{
451
    QString proxy = getProducerProperty(QStringLiteral("kdenlive:proxy"));
Nicolas Carion's avatar
Nicolas Carion committed
452
    return !proxy.isEmpty() || proxy == QLatin1String("-");
453 454
}

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

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

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

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

574 575
void ProjectClip::updateParentInfo(const QString &folderid, const QString &foldername)
{
576
    Q_UNUSED(foldername);
Nicolas Carion's avatar
Nicolas Carion committed
577
    ClipController::setProducerProperty(QStringLiteral("kdenlive:folderid"), folderid);
578 579
}

Laurent Montel's avatar
Laurent Montel committed
580
bool ProjectClip::matches(const QString &condition)
581
{
Nicolas Carion's avatar
Nicolas Carion committed
582
    // TODO
583
    Q_UNUSED(condition)
584 585 586
    return true;
}

587
const QString ProjectClip::codec(bool audioCodec) const
588
{
589
    return codec(audioCodec);
590
}
591

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

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

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

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

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

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