projectclip.cpp 48.7 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();
Nicolas Carion's avatar
Nicolas Carion committed
131
    if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->abortAudioThumb(m_binId, duration().ms());
132 133
    QMutexLocker audioLock(&m_audioMutex);
    m_thumbMutex.lock();
134
    m_requestedThumbs.clear();
135
    m_thumbMutex.unlock();
136
    m_thumbThread.waitForFinished();
137
    delete m_thumbsProducer;
138
    audioFrameCache.clear();
139 140
}

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

147 148
QString ProjectClip::getToolTip() const
{
149
    return url();
150 151
}

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

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

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

178 179
bool ProjectClip::audioThumbCreated() const
{
180
    return (m_audioThumbCreated);
181 182
}

183 184
ClipType ProjectClip::clipType() const
{
185
    return m_clipType;
186 187
}

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

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

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

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

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

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

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

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

258
const QString ProjectClip::url() const
259
{
260
    return clipUrl();
261 262
}

263
GenTime ProjectClip::duration() const
264
{
265
    return getPlaytime();
266 267
}

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

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

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

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

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

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

Nicolas Carion's avatar
Nicolas Carion committed
325
    updateProducer(std::move(producer));
326 327 328 329

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

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

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

383
bool ProjectClip::isReady() const
384
{
385
    return m_clipStatus == StatusReady;
386 387
}

388
/*void ProjectClip::setZone(const QPoint &zone)
389 390
{
    m_zone = zone;
391
}*/
392 393 394

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

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

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

double ProjectClip::getOriginalFps() const
{
461
    return originalFps();
462
}
463 464 465

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

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

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

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

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

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

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

611
const QString ProjectClip::codec(bool audioCodec) const
612
{
613
    return codec(audioCodec);
614
}
615

616
bool ProjectClip::rename(const QString &name, int column)
617
{
Laurent Montel's avatar
Laurent Montel committed
618 619
    QMap<QString, QString> newProperites;
    QMap<QString, QString> oldProperites;
620 621
    bool edited = false;
    switch (column) {
622 623 624 625
    case 0:
        if (m_name == name) {
            return false;
        }
626
        // Rename clip
627 628
        oldProperites.insert(QStringLiteral("kdenlive:clipname"), m_name);
        newProperites.insert(QStringLiteral("kdenlive:clipname"), name);
629 630 631
        m_name = name;
        edited = true;
        break;
632 633 634 635
    case 2:
        if (m_description == name) {
            return false;
        }
636
        // Rename clip
637
        if (m_clipType == TextTemplate) {
638 639 640 641 642 643
            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);
        }
644 645 646 647 648
        m_description = name;
        edited = true;
        break;
    }
    if (edited) {
Nicolas Carion's avatar
Nicolas Carion committed
649
        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
{
    for (int i = 0; i < markers.count(); ++i) {
685
        if (markers.at(i).markerType() < 0) {
Jean-Baptiste Mardelle's avatar