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
    connect(this, &ProjectClip::updateThumbProgress, model.get(), &ProjectItemModel::updateThumbProgress);
78 79
}

80 81 82 83 84 85
// 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);
86 87 88
    parent->appendChild(self);
    model->loadSubClips(id, self->getPropertiesFromPrefix(QStringLiteral("kdenlive:clipzone.")));
    self->createAudioThumbs();
89 90 91 92
    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
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);
127
    parent->appendChild(self);
128 129 130
    return self;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    // Update info
    if (m_name.isEmpty()) {
        m_name = clipName();
334
    }
335 336 337
    m_date = date;
    m_description = ClipController::description();
    m_temporaryUrl.clear();
338 339
    if (m_clipType == Audio) {
        m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
340
    }
341
    m_duration = getStringDuration();
342
    m_clipStatus = StatusReady;
343
    if (!hasProxy()) {
344
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshPanel(m_id);
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)) {
356
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->requestAudioThumbs(m_id, 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

bool ProjectClip::hasProxy() const
{
470
    QString proxy = getProducerProperty(QStringLiteral("kdenlive:proxy"));
Nicolas Carion's avatar
Nicolas Carion committed
471
    return !proxy.isEmpty() || proxy == QLatin1String("-");
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 516 517 518 519 520
            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();
                }
521
            }
522
        } else {
523
            // A proxy was requested, make sure to keep original url
524
            setProducerProperty(QStringLiteral("kdenlive:originalurl"), url());
525
            if (auto ptr = m_model.lock()) std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->startJob(m_id, 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()) std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->refreshClip(m_id);
564
    }
565
    if (!passProperties.isEmpty()) {
566
        if (auto ptr = m_model.lock()) std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->updateTimelineProducers(m_id, 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
const QString ProjectClip::codec(bool audioCodec) const
616
{
617
    return codec(audioCodec);
618
}
619

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

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

bool ProjectClip::deleteClipMarkers(QUndoCommand *command)
{
674
    QList<CommentedTime> markers = m_markerModel->getAllMarkers();
675 676 677
    if (markers.isEmpty()) {
        return false;
    }
678
    for (auto &marker : markers) {
679 680
        marker.setMarkerType(-1);
    }
681 682
    //TODO: group all markers in one undo/redo operation
    addMarkers(markers);
683
    return true;
684 685
}

Laurent Montel's avatar
Laurent Montel committed
686
void ProjectClip::addMarkers(QList<CommentedTime> &markers)
687 688
{
    for (int i = 0; i < markers.count(); ++i) {
689
        if (markers.at(i).markerType() < 0) {
690
            m_markerModel->removeMarker(markers.at(i).time());
691
        } else {
692
            m_markerModel->addMarker(markers.at(i).time(), markers.at(i).comment(), markers.at(i).markerType());
693
        }
694 695
    }
    // refresh markers in clip monitor