projectclip.cpp 48.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
Copyright (C) 2012  Till Theato <root@ttill.de>
Copyright (C) 2014  Jean-Baptiste Mardelle <jb@kdenlive.org>
This file is part of Kdenlive. See www.kdenlive.org.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License or (at your option) version 3 or any later version
accepted by the membership of KDE e.V. (or its successor approved
11
by the membership of KDE e.V.), which shall act as a proxy
12 13 14 15 16 17 18 19 20 21 22 23
defined in Section 14 of version 3 of the license.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

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

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

57 58
ProjectClip::ProjectClip(const QString &id, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model, std::shared_ptr<Mlt::Producer> producer)
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model)
59 60 61
    , 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
    connect(this, &ProjectClip::updateThumbProgress, model.get(), &ProjectItemModel::updateThumbProgress);
78 79
}

80 81
// static
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model,
82
                                                    std::shared_ptr<Mlt::Producer> producer)
83
{
84
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, thumb, model, producer));
85
    baseFinishConstruct(self);
86 87
    model->loadSubClips(id, self->getPropertiesFromPrefix(QStringLiteral("kdenlive:clipzone.")));
    self->createAudioThumbs();
88 89 90
    return self;
}

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

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

128 129
ProjectClip::~ProjectClip()
{
130
    // controller is deleted in bincontroller
131
    abortAudioThumbs();
132 133 134
    if (auto ptr = m_model.lock()) {
        emit std::static_pointer_cast<ProjectItemModel>(ptr)->abortAudioThumb(m_binId, 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;
Nicolas Carion's avatar
Nicolas Carion committed
172
    if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshAudioThumbs(m_binId);
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
{
Nicolas Carion's avatar
Nicolas Carion committed
205
    if (id == m_binId) {
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
    }
Nicolas Carion's avatar
Nicolas Carion committed
285
    if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->reloadProducer(m_binId, 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
    // Update info
    if (m_name.isEmpty()) {
        m_name = clipName();
332
    }
333 334 335
    m_date = date;
    m_description = ClipController::description();
    m_temporaryUrl.clear();
336 337
    if (m_clipType == Audio) {
        m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
338
    }
339
    m_duration = getStringDuration();
340
    m_clipStatus = StatusReady;
341
    if (!hasProxy()) {
Nicolas Carion's avatar
Nicolas Carion committed
342
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshPanel(m_binId);
343
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
344 345 346
    if (auto ptr = m_model.lock()) {
        std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
    }
347
    // Make sure we have a hash for this clip
348
    getFileHash();
349
    createAudioThumbs();
350
    return true;
351 352 353 354
}

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

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

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

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

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

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

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

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

468

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

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

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

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

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

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

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

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

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

bool ProjectClip::deleteClipMarkers(QUndoCommand *command)
{
670
    QList<CommentedTime> markers = m_markerModel->getAllMarkers();
671 672 673
    if (markers.isEmpty()) {
        return false;
    }
674
    for (auto &marker : markers) {
675 676
        marker.setMarkerType(-1);
    }
Nicolas Carion's avatar
Nicolas Carion committed
677
    // TODO: group all markers in one undo/redo operation
678
    addMarkers(markers);
679
    return true;
680 681
}

Laurent Montel's avatar
Laurent Montel committed
682
void ProjectClip::addMarkers(QList<CommentedTime> &markers)
683
{
684
    // TODO refac : this must be cleaned
685
    for (int i = 0; i < markers.count(); ++i) {
686
        if (markers.at(i).markerType() < 0) {
687
            m_markerModel->removeMarker(markers.at(i).time());
688