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

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

56 57
ProjectClip::ProjectClip(const QString &id, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model, std::shared_ptr<Mlt::Producer> producer,
                         std::shared_ptr<ProjectFolder> parent)
58 59 60 61
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model, parent)
    , ClipController(pCore->binController(), producer)
    , 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 78
    model->loadSubClips(id, getPropertiesFromPrefix(QStringLiteral("kdenlive:clipzone.")));
    connect(this, &ProjectClip::updateThumbProgress, model.get(), &ProjectItemModel::updateThumbProgress);
79
    createAudioThumbs();
80 81
}

82 83 84 85 86 87 88 89 90 91 92
// static
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model,
                                                    std::shared_ptr<Mlt::Producer> producer, std::shared_ptr<ProjectFolder> parent)
{
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, thumb, model, producer, parent));

    baseFinishConstruct(self);
    return self;
}

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

122 123 124 125 126 127 128 129
std::shared_ptr<ProjectClip> ProjectClip::construct(const QDomElement &description, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model,
                                                    std::shared_ptr<ProjectFolder> parent)
{
    std::shared_ptr<ProjectClip> self(new ProjectClip(description, thumb, model, parent));
    baseFinishConstruct(self);
    return self;
}

130 131
ProjectClip::~ProjectClip()
{
132
    // controller is deleted in bincontroller
133
    abortAudioThumbs();
134
    if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->abortAudioThumb(m_id, duration().ms());
135 136
    QMutexLocker audioLock(&m_audioMutex);
    m_thumbMutex.lock();
137
    m_requestedThumbs.clear();
138
    m_thumbMutex.unlock();
139
    m_thumbThread.waitForFinished();
140
    delete m_thumbsProducer;
141
    audioFrameCache.clear();
142 143
}

144 145 146
void ProjectClip::abortAudioThumbs()
{
    m_abortAudioThumb = true;
147
    emit doAbortAudioThumbs();
148 149
}

150 151
QString ProjectClip::getToolTip() const
{
152
    return url();
153 154
}

155
QString ProjectClip::getXmlProperty(const QDomElement &producer, const QString &propertyName, const QString &defaultValue)
156
{
157
    QString value = defaultValue;
158
    QDomNodeList props = producer.elementsByTagName(QStringLiteral("property"));
159
    for (int i = 0; i < props.count(); ++i) {
160
        if (props.at(i).toElement().attribute(QStringLiteral("name")) == propertyName) {
161 162 163 164 165 166 167
            value = props.at(i).firstChild().nodeValue();
            break;
        }
    }
    return value;
}

Laurent Montel's avatar
Laurent Montel committed
168
void ProjectClip::updateAudioThumbnail(const QVariantList &audioLevels)
169
{
170
    audioFrameCache = audioLevels;
171
    m_audioThumbCreated = true;
172
    if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshAudioThumbs(m_id);
173 174 175
    emit gotAudioData();
}

176 177
QStringList ProjectClip::markersText(GenTime in, GenTime out) const
{
178
    return markerComments(in, out);
179 180
}

181 182
bool ProjectClip::audioThumbCreated() const
{
183
    return (m_audioThumbCreated);
184 185
}

186 187
ClipType ProjectClip::clipType() const
{
188
    return m_clipType;
189 190
}

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

203
std::shared_ptr<ProjectClip> ProjectClip::clip(const QString &id)
204 205
{
    if (id == m_id) {
206
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
207
    }
208
    return std::shared_ptr<ProjectClip>();
209 210
}

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

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

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

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

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

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

261
const QString ProjectClip::url() const
262
{
263
    return clipUrl();
264 265
}

266
GenTime ProjectClip::duration() const
267
{
268
    return getPlaytime();
269 270
}

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

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

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

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

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

324
bool ProjectClip::setProducer(std::shared_ptr<Mlt::Producer> producer, bool replaceProducer)
325
{
326 327
    Q_UNUSED(replaceProducer);

Nicolas Carion's avatar
Nicolas Carion committed
328
    updateProducer(std::move(producer));
329 330 331 332

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

void ProjectClip::createAudioThumbs()
{
354
    if (KdenliveSettings::audiothumbnails() && (m_clipType == AV || m_clipType == Audio || m_clipType == Playlist)) {
355
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->requestAudioThumbs(m_id, duration().ms());
356
        emit updateJobStatus(AbstractClipJob::THUMBJOB, JobWaiting, 0);
357
    }
358 359
}

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

386
bool ProjectClip::isReady() const
387
{
388
    return m_clipStatus == StatusReady;
389 390
}

391
/*void ProjectClip::setZone(const QPoint &zone)
392 393
{
    m_zone = zone;
394
}*/
395 396 397

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

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

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

double ProjectClip::getOriginalFps() const
{
464
    return originalFps();
465
}
466 467 468

bool ProjectClip::hasProxy() const
{
469
    QString proxy = getProducerProperty(QStringLiteral("kdenlive:proxy"));
Nicolas Carion's avatar
Nicolas Carion committed
470
    return !proxy.isEmpty() || proxy == QLatin1String("-");
471 472
}

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

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

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

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

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

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

614
const QString ProjectClip::codec(bool audioCodec) const
615
{
616
    return codec(audioCodec);
617
}
618

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

Laurent Montel's avatar
Laurent Montel committed
657
void ProjectClip::addClipMarker(QList<CommentedTime> newMarkers, QUndoCommand *groupCommand)
658
{
Laurent Montel's avatar
Laurent Montel committed
659
    QList<CommentedTime> oldMarkers;
660
    for (int i = 0; i < newMarkers.count(); ++i) {
661
        CommentedTime oldMarker = markerAt(newMarkers.at(i).time());
662 663 664 665 666 667
        if (oldMarker == CommentedTime()) {
            oldMarker = newMarkers.at(i);
            oldMarker.setMarkerType(-1);
        }
        oldMarkers << oldMarker;
    }
Nicolas Carion's avatar
Nicolas Carion committed
668
    (void)new AddMarkerCommand(this, oldMarkers, newMarkers, groupCommand);
669 670 671 672
}

bool ProjectClip::deleteClipMarkers(QUndoCommand *command)
{
Laurent Montel's avatar
Laurent Montel committed
673
    QList<CommentedTime> markers = commentedSnapMarkers();
674 675 676
    if (markers.isEmpty()) {
        return false;
    }
Laurent Montel's avatar
Laurent Montel committed
677
    QList<CommentedTime> newMarkers;
678 679 680 681 682 683
    for (int i = 0; i < markers.size(); ++i) {
        CommentedTime marker = markers.at(i);
        marker.setMarkerType(-1);
        newMarkers << marker;
    }
    new AddMarkerCommand(this, markers, newMarkers, command);
684
    return true;
685 686
}

Laurent Montel's avatar
Laurent Montel committed
687
void ProjectClip::addMarkers(QList<CommentedTime> &markers)
688 689
{
    for (int i = 0; i < markers.count(); ++i) {
690
        if (markers.at(i).markerType() < 0) {
691
            m_markerModel->removeMarker(markers.at(i).time());
692
        } else {
693
            m_markerModel->addMarker(markers.at(i).time(), markers.at(i).comment(), markers.at(i).markerType());
694
        }
695 696
    }
    // refresh markers in clip monitor
697
    if (auto ptr = m_model.lock()) std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->refreshClipMarkers(m_id);
698 699 700
    // refresh markers in timeline clips
    emit refreshClipDisplay();
}