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
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
#include "doc/docundostack.hpp"
26
#include "bin.h"
27
#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/clip.h"
34
#include "mltcontroller/clippropertiescontroller.h"
35
#include "model/markerlistmodel.hpp"
36
#include "profiles/profilemodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
37
#include "project/projectcommands.h"
38
#include "project/projectmanager.h"
Nicolas Carion's avatar
Nicolas Carion committed
39 40 41 42
#include "projectfolder.h"
#include "projectitemmodel.h"
#include "projectsubclip.h"
#include "timecode.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
#include "xml/xml.hpp"
46 47
#include <kimagecache.h>
#include <QPainter>
48

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

59 60
ProjectClip::ProjectClip(const QString &id, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model, std::shared_ptr<Mlt::Producer> producer)
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model)
61
    , ClipController(id, pCore->binController(), producer)
62 63
    , m_abortAudioThumb(false)
    , m_thumbsProducer(nullptr)
64
{
65
    m_markerModel = std::make_shared<MarkerListModel>(id, pCore->projectManager()->undoStack());
66
    m_clipStatus = StatusReady;
67 68 69 70
    m_name = clipName();
    m_duration = getStringDuration();
    m_date = date;
    m_description = ClipController::description();
71
    if (m_clipType == Audio) {
72
        m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
73 74 75
    } else {
        m_thumbnail = thumb;
    }
76 77
    // Make sure we have a hash for this clip
    hash();
78
    connect(this, &ProjectClip::updateJobStatus, this, &ProjectClip::setJobStatus);
79
    connect(this, &ProjectClip::updateThumbProgress, model.get(), &ProjectItemModel::updateThumbProgress);
80 81 82
    connect(m_markerModel.get(), &MarkerListModel::modelChanged, [&](){
            setProducerProperty(QStringLiteral("kdenlive:markers"), m_markerModel->toJson());
        });
83 84
    QString markers = getProducerProperty(QStringLiteral("kdenlive:markers"));
    if (!markers.isEmpty()) {
85
        QMetaObject::invokeMethod(m_markerModel.get(), "importFromJson", Qt::QueuedConnection, Q_ARG(const QString &, markers), Q_ARG(bool, true), Q_ARG(bool, false));
86
    }
87 88
}

89 90
// static
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model,
91
                                                    std::shared_ptr<Mlt::Producer> producer)
92
{
93
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, thumb, model, producer));
94
    baseFinishConstruct(self);
95
    model->loadSubClips(id, self->getPropertiesFromPrefix(QStringLiteral("kdenlive:clipzone.")));
96 97 98
    return self;
}

99 100 101
ProjectClip::ProjectClip(const QString &id, const QDomElement &description, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model)
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, description, model)
    , ClipController(id, pCore->binController())
102 103
    , m_abortAudioThumb(false)
    , m_thumbsProducer(nullptr)
104
{
105
    m_clipStatus = StatusWaiting;
106
    m_thumbnail = thumb;
107
    m_markerModel = std::make_shared<MarkerListModel>(m_binId, pCore->projectManager()->undoStack());
108
    if (description.hasAttribute(QStringLiteral("type"))) {
109 110
        m_clipType = (ClipType)description.attribute(QStringLiteral("type")).toInt();
        if (m_clipType == Audio) {
111
            m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
112
        }
113
    }
114
    m_temporaryUrl = getXmlProperty(description, QStringLiteral("resource"));
115
    QString clipName = getXmlProperty(description, QStringLiteral("kdenlive:clipname"));
116 117
    if (!clipName.isEmpty()) {
        m_name = clipName;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
118
    } else if (!m_temporaryUrl.isEmpty()) {
119
        m_name = QFileInfo(m_temporaryUrl).fileName();
120 121
    } else {
        m_name = i18n("Untitled");
122
    }
123
    connect(this, &ProjectClip::updateJobStatus, this, &ProjectClip::setJobStatus);
124
    connect(this, &ProjectClip::updateThumbProgress, model.get(), &ProjectItemModel::updateThumbProgress);
125 126 127
    connect(m_markerModel.get(), &MarkerListModel::modelChanged, [&](){
            setProducerProperty(QStringLiteral("kdenlive:markers"), m_markerModel->toJson());
        });
128 129
}

130
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QDomElement &description, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model)
131
{
132
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, description, thumb, model));
133 134 135 136
    baseFinishConstruct(self);
    return self;
}

137 138
ProjectClip::~ProjectClip()
{
139
    // controller is deleted in bincontroller
140
    abortAudioThumbs();
141 142
    QMutexLocker audioLock(&m_audioMutex);
    m_thumbMutex.lock();
143
    m_requestedThumbs.clear();
144
    m_thumbMutex.unlock();
145
    m_thumbThread.waitForFinished();
146
    delete m_thumbsProducer;
147
    audioFrameCache.clear();
148 149
}

150 151 152
void ProjectClip::abortAudioThumbs()
{
    m_abortAudioThumb = true;
153
    emit doAbortAudioThumbs();
154 155
}

156 157
QString ProjectClip::getToolTip() const
{
158
    return url();
159 160
}

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

Laurent Montel's avatar
Laurent Montel committed
174
void ProjectClip::updateAudioThumbnail(const QVariantList &audioLevels)
175
{
176
    audioFrameCache = audioLevels;
177
    m_audioThumbCreated = true;
Nicolas Carion's avatar
Nicolas Carion committed
178
    if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshAudioThumbs(m_binId);
179 180 181 182 183
    emit gotAudioData();
}

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
{
Nicolas Carion's avatar
Nicolas Carion committed
206
    if (id == m_binId) {
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
    }
Nicolas Carion's avatar
Nicolas Carion committed
286
    if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->reloadProducer(m_binId, 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
    updateTimelineClips(QVector<int>() << TimelineModel::ReloadThumb);
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
{
Nicolas Carion's avatar
Nicolas Carion committed
327
    updateProducer(std::move(producer));
328

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 340 341
    } else if (m_clipType == Image) {
        if (getProducerIntProperty(QStringLiteral("meta.media.width")) < 8 || getProducerIntProperty(QStringLiteral("meta.media.height")) < 8) {
            KMessageBox::information(QApplication::activeWindow(), i18n("Image dimension smaller than 8 pixels.\nThis is not correctly supported by our video framework."));
        }
342
    }
343
    m_duration = getStringDuration();
344
    m_clipStatus = StatusReady;
345
    if (!hasProxy()) {
Nicolas Carion's avatar
Nicolas Carion committed
346
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshPanel(m_binId);
347
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
348 349 350
    if (auto ptr = m_model.lock()) {
        std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
    }
351
    // Make sure we have a hash for this clip
352
    getFileHash();
353
    createAudioThumbs();
354 355 356 357
    if (replaceProducer) {
        // Recreate thumbnail
        
    }
358
    return true;
359 360 361 362
}

void ProjectClip::createAudioThumbs()
{
363
    if (KdenliveSettings::audiothumbnails() && (m_clipType == AV || m_clipType == Audio || m_clipType == Playlist)) {
Nicolas Carion's avatar
Nicolas Carion committed
364
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->requestAudioThumbs(m_binId, duration().ms());
365
        emit updateJobStatus(AbstractClipJob::THUMBJOB, JobWaiting, 0);
366
    }
367 368
}

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

395
bool ProjectClip::isReady() const
396
{
397
    return m_clipStatus == StatusReady;
398 399
}

400
/*void ProjectClip::setZone(const QPoint &zone)
401 402
{
    m_zone = zone;
403
}*/
404 405 406

QPoint ProjectClip::zone() const
{
407 408
    int x = getProducerIntProperty(QStringLiteral("kdenlive:zone_in"));
    int y = getProducerIntProperty(QStringLiteral("kdenlive:zone_out"));
409
    return QPoint(x, y);
410 411 412 413
}

const QString ProjectClip::hash()
{
414
    QString clipHash = getProducerProperty(QStringLiteral("kdenlive:file_hash"));
415 416
    if (!clipHash.isEmpty()) {
        return clipHash;
417
    }
418
    return getFileHash();
419 420
}

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

double ProjectClip::getOriginalFps() const
{
473
    return originalFps();
474
}
475

476

477 478
bool ProjectClip::hasProxy() const
{
479
    QString proxy = getProducerProperty(QStringLiteral("kdenlive:proxy"));
480
    return proxy.size() > 2;
481 482
}

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

558 559
    if (properties.contains(QStringLiteral("kdenlive:clipname"))) {
        m_name = properties.value(QStringLiteral("kdenlive:clipname"));
560
        refreshPanel = true;
561
        if (auto ptr = m_model.lock()) {
562
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
563 564 565
        }
        // update timeline clips
        updateTimelineClips(QVector<int>() << TimelineModel::NameRole);
566
    }
567 568 569 570
    if (refreshPanel) {
        // Some of the clip properties have changed through a command, update properties panel
        emit refreshPropertiesPanel();
    }
571
    if (reload) {
572
        // producer has changed, refresh monitor and thumbnail
573
        reloadProducer(refreshOnly);
574
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshClip(m_binId);
575
    }
576
    if (!passProperties.isEmpty()) {
577
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->updateTimelineProducers(m_binId, passProperties);
578
    }
579 580
}

581
void ProjectClip::setJobStatus(int jobType, int status, int progress, const QString &statusMessage)
582
{
Nicolas Carion's avatar
Nicolas Carion committed
583
    m_jobType = (AbstractClipJob::JOBTYPE)jobType;
584
    if (progress > 0) {
585 586 587
        if (m_jobProgress == progress) {
            return;
        }
588
        m_jobProgress = progress;
589
    } else {
590
        m_jobProgress = status;
591 592 593
        if (m_jobType == AbstractClipJob::PROXYJOB && (status == JobAborted || status == JobCrashed)) {
            setProducerProperty(QStringLiteral("kdenlive:proxy"), QStringLiteral("-"));
        }
Nicolas Carion's avatar
Nicolas Carion committed
594
        if ((status == JobAborted || status == JobCrashed || status == JobDone) && !statusMessage.isEmpty()) {
595
            m_jobMessage = statusMessage;
596
            if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->emitMessage(statusMessage, 100, OperationCompletedMessage);
597
        }
598
    }
599
    if (auto ptr = m_model.lock()) std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
600
}
601

602 603
ClipPropertiesController *ProjectClip::buildProperties(QWidget *parent)
{
604 605
    auto ptr = m_model.lock();
    Q_ASSERT(ptr);
Nicolas Carion's avatar
Nicolas Carion committed
606
    ClipPropertiesController *panel =
607
        new ClipPropertiesController(static_cast<ClipController *>(this), parent);
Laurent Montel's avatar
Laurent Montel committed
608 609
    connect(this, &ProjectClip::refreshPropertiesPanel, panel, &ClipPropertiesController::slotReloadProperties);
    connect(this, &ProjectClip::refreshAnalysisPanel, panel, &ClipPropertiesController::slotFillAnalysisData);
610 611 612
    return panel;
}

613 614
void ProjectClip::updateParentInfo(const QString &folderid, const QString &foldername)
{
615
    Q_UNUSED(foldername);
Nicolas Carion's avatar
Nicolas Carion committed
616
    ClipController::setProducerProperty(QStringLiteral("kdenlive:folderid"), folderid);
617 618
}

Laurent Montel's avatar
Laurent Montel committed
619
bool ProjectClip::matches(const QString &condition)
620
{
Nicolas Carion's avatar
Nicolas Carion committed
621
    // TODO
622
    Q_UNUSED(condition)
623 624 625
    return true;
}

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

Nicolas Carion's avatar
Nicolas Carion committed
664
/*QVariant ProjectClip::getData(DataType type) const
665 666
{
    switch (type) {
667
    case AbstractProjectItem::IconOverlay:
668
        return hasEffects() ? QVariant("kdenlive-track_has_effect") : QVariant();
669 670 671
        break;
    default:
        break;
672
    }
Nicolas Carion's avatar
Nicolas Carion committed
673 674
    return AbstractProjectItem::getData(type);
    }*/
675

Laurent Montel's avatar
Laurent Montel committed
676
void ProjectClip::slotQueryIntraThumbs(const QList<int> &frames)
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
{
    QMutexLocker lock(&m_intraThumbMutex);
    for (int i = 0; i < frames.count(); i++) {
        if (!m_intraThumbs.contains(frames.at(i))) {
            m_intraThumbs << frames.at(i);
        }
    }
    qSort(m_intraThumbs);
    if (!m_intraThread.isRunning()) {
        m_intraThread = QtConcurrent::run(this, &ProjectClip::doExtractIntra);
    }
}

void ProjectClip::doExtractIntra()
{
    Mlt::Producer *prod = thumbProducer();
Laurent Montel's avatar
Laurent Montel committed
693
    if (prod == nullptr || !prod->is_valid()) {
694 695
        return;
    }
696
    int fullWidth = 150 * prod->profile()->dar() + 0.5;
697