projectclip.cpp 48.9 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)
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model)
58 59 60
    , ClipController(pCore->binController(), producer)
    , m_abortAudioThumb(false)
    , m_thumbsProducer(nullptr)
61
{
62
    m_markerModel = std::make_shared<MarkerListModel>(id, pCore->projectManager()->current()->commandStack());
63
    m_clipStatus = StatusReady;
64 65 66 67
    m_name = clipName();
    m_duration = getStringDuration();
    m_date = date;
    m_description = ClipController::description();
68
    if (m_clipType == Audio) {
69
        m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
70 71 72
    } else {
        m_thumbnail = thumb;
    }
73 74
    // Make sure we have a hash for this clip
    hash();
75
    connect(this, &ProjectClip::updateJobStatus, this, &ProjectClip::setJobStatus);
76
    connect(this, &ProjectClip::updateThumbProgress, model.get(), &ProjectItemModel::updateThumbProgress);
77 78
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Nicolas Carion's avatar
Nicolas Carion committed
327
    updateProducer(std::move(producer));
328 329 330
    // Update info
    if (m_name.isEmpty()) {
        m_name = clipName();
331
    }
332 333 334
    m_date = date;
    m_description = ClipController::description();
    m_temporaryUrl.clear();
335 336
    if (m_clipType == Audio) {
        m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
337
    }
338
    m_duration = getStringDuration();
339
    m_clipStatus = StatusReady;
340
    if (!hasProxy()) {
Nicolas Carion's avatar
Nicolas Carion committed
341
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshPanel(m_binId);
342
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
343 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)) {
Nicolas Carion's avatar
Nicolas Carion committed
355
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->requestAudioThumbs(m_binId, 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 469
bool ProjectClip::hasProxy() const
{
470
    QString proxy = getProducerProperty(QStringLiteral("kdenlive:proxy"));
471
    return proxy.size() > 2;
472 473
}

Laurent Montel's avatar
Laurent Montel committed
474
void ProjectClip::setProperties(const QMap<QString, QString> &properties, bool refreshPanel)
475 476
{
    QMapIterator<QString, QString> i(properties);
Laurent Montel's avatar
Laurent Montel committed
477
    QMap<QString, QString> passProperties;
478
    bool refreshAnalysis = false;
479
    bool reload = false;
480
    bool refreshOnly = true;
481 482
    // Some properties also need to be passed to track producers
    QStringList timelineProperties;
Laurent Montel's avatar
Laurent Montel committed
483 484
    if (properties.contains(QStringLiteral("templatetext"))) {
        m_description = properties.value(QStringLiteral("templatetext"));
485 486
        if (auto ptr = m_model.lock())
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
487 488
        refreshPanel = true;
    }
Nicolas Carion's avatar
Nicolas Carion committed
489 490 491 492
    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");
493
    QStringList keys;
Nicolas Carion's avatar
Nicolas Carion committed
494 495
    keys << QStringLiteral("luma_duration") << QStringLiteral("luma_file") << QStringLiteral("fade") << QStringLiteral("ttl") << QStringLiteral("softness")
         << QStringLiteral("crop") << QStringLiteral("animation");
496 497
    while (i.hasNext()) {
        i.next();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
498
        setProducerProperty(i.key(), i.value());
499
        if (m_clipType == SlideShow && keys.contains(i.key())) {
500
            reload = true;
501
            refreshOnly = false;
502
        }
503 504 505
        if (i.key().startsWith(QLatin1String("kdenlive:clipanalysis"))) {
            refreshAnalysis = true;
        }
506 507 508
        if (timelineProperties.contains(i.key())) {
            passProperties.insert(i.key(), i.value());
        }
509
    }
510 511
    if (properties.contains(QStringLiteral("kdenlive:proxy"))) {
        QString value = properties.value(QStringLiteral("kdenlive:proxy"));
512
        // If value is "-", that means user manually disabled proxy on this clip
513
        if (value.isEmpty() || value == QLatin1String("-")) {
514
            // reset proxy
515
            if (auto ptr = m_model.lock()) {
Nicolas Carion's avatar
Nicolas Carion committed
516 517
                if (std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->hasPendingJob(m_binId, AbstractClipJob::PROXYJOB)) {
                    std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->discardJobs(m_binId, AbstractClipJob::PROXYJOB);
518 519 520
                } else {
                    reloadProducer();
                }
521
            }
522
        } else {
523
            // A proxy was requested, make sure to keep original url
524
            setProducerProperty(QStringLiteral("kdenlive:originalurl"), url());
Nicolas Carion's avatar
Nicolas Carion committed
525
            if (auto ptr = m_model.lock()) std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->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);
Nicolas Carion's avatar
Nicolas Carion committed
563
        if (auto ptr = m_model.lock()) std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->refreshClip(m_binId);
564
    }
565
    if (!passProperties.isEmpty()) {
Nicolas Carion's avatar
Nicolas Carion committed
566
        if (auto ptr = m_model.lock()) std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->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()) std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->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(std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->projectTimecode(), 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) {
Nicolas Carion's avatar
Nicolas Carion committed
648
        if (auto ptr = m_model.lock()) std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->slotEditClipCommand(m_binId, oldProperites, newProperites);
649 650
    }
    return edited;
651 652
}

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

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

Laurent Montel's avatar
Laurent Montel committed
681
void ProjectClip::addMarkers(QList<CommentedTime> &markers)
682 683
{
    for (int i = 0; i < markers.count(); ++i) {
684
        if (markers.at(i).markerType() < 0) {
685
            m_markerModel->removeMarker(markers.at(i).time());
686
        } else {
687