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

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

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

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

#include "projectclip.h"
24
#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
#include "doc/kthumb.h"
Nicolas Carion's avatar
Nicolas Carion committed
29
#include "effects/effectstack/model/effectstackmodel.hpp"
30 31
#include "jobs/jobmanager.h"
#include "jobs/thumbjob.hpp"
32
#include "kdenlivesettings.h"
33
#include "lib/audio/audioStreamInfo.h"
Nicolas Carion's avatar
Nicolas Carion committed
34
#include "mltcontroller/bincontroller.h"
35
#include "mltcontroller/clip.h"
36
#include "mltcontroller/clipcontroller.h"
37
#include "mltcontroller/clippropertiescontroller.h"
38
#include "model/markerlistmodel.hpp"
39
#include "profiles/profilemodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
40
#include "project/projectcommands.h"
41
#include "project/projectmanager.h"
Nicolas Carion's avatar
Nicolas Carion committed
42 43 44 45
#include "projectfolder.h"
#include "projectitemmodel.h"
#include "projectsubclip.h"
#include "timecode.h"
Nicolas Carion's avatar
style  
Nicolas Carion committed
46
#include "timeline2/model/snapmodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
47
#include "utils/KoIconUtils.h"
48
#include "utils/thumbnailcache.hpp"
49
#include "xml/xml.hpp"
50
#include <QPainter>
51
#include <kimagecache.h>
52

Laurent Montel's avatar
Laurent Montel committed
53
#include "kdenlive_debug.h"
54
#include <KLocalizedString>
55
#include <KMessageBox>
Nicolas Carion's avatar
Nicolas Carion committed
56 57 58 59 60
#include <QCryptographicHash>
#include <QDir>
#include <QDomElement>
#include <QFile>
#include <QtConcurrent>
Nicolas Carion's avatar
Nicolas Carion committed
61
#include <utility>
62

63 64
ProjectClip::ProjectClip(const QString &id, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model, std::shared_ptr<Mlt::Producer> producer)
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model)
65
    , ClipController(id, pCore->binController(), producer)
66
    , m_thumbsProducer(nullptr)
67
{
68
    m_markerModel = std::make_shared<MarkerListModel>(id, pCore->projectManager()->undoStack());
69
    m_clipStatus = StatusReady;
70 71 72 73
    m_name = clipName();
    m_duration = getStringDuration();
    m_date = date;
    m_description = ClipController::description();
74
    if (m_clipType == ClipType::Audio) {
75
        m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
76 77 78
    } else {
        m_thumbnail = thumb;
    }
79 80
    // Make sure we have a hash for this clip
    hash();
81
    connect(m_markerModel.get(), &MarkerListModel::modelChanged, [&]() { setProducerProperty(QStringLiteral("kdenlive:markers"), m_markerModel->toJson()); });
82 83
    QString markers = getProducerProperty(QStringLiteral("kdenlive:markers"));
    if (!markers.isEmpty()) {
84 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
ProjectClip::ProjectClip(const QString &id, const QDomElement &description, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model)
100
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model)
101
    , ClipController(id, pCore->binController())
102
    , m_thumbsProducer(nullptr)
103
{
104
    m_clipStatus = StatusWaiting;
105
    m_thumbnail = thumb;
106
    m_markerModel = std::make_shared<MarkerListModel>(m_binId, pCore->projectManager()->undoStack());
107
    if (description.hasAttribute(QStringLiteral("type"))) {
108
        m_clipType = (ClipType)description.attribute(QStringLiteral("type")).toInt();
109
        if (m_clipType == ClipType::Audio) {
110
            m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
111
        }
112
    }
113
    m_temporaryUrl = getXmlProperty(description, QStringLiteral("resource"));
114
    QString clipName = getXmlProperty(description, QStringLiteral("kdenlive:clipname"));
115 116
    if (!clipName.isEmpty()) {
        m_name = clipName;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
117
    } else if (!m_temporaryUrl.isEmpty()) {
118
        m_name = QFileInfo(m_temporaryUrl).fileName();
119 120
    } else {
        m_name = i18n("Untitled");
121
    }
122
    connect(m_markerModel.get(), &MarkerListModel::modelChanged, [&]() { setProducerProperty(QStringLiteral("kdenlive:markers"), m_markerModel->toJson()); });
123
    connectEffectStack();
124 125
}

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

134 135
ProjectClip::~ProjectClip()
{
136
    // controller is deleted in bincontroller
137
    m_thumbMutex.lock();
138
    m_requestedThumbs.clear();
139
    m_thumbMutex.unlock();
140
    m_thumbThread.waitForFinished();
141
    audioFrameCache.clear();
142 143 144 145 146
    // delete all timeline producers
    std::map<int, std::shared_ptr<Mlt::Producer>>::iterator itr = m_timelineProducers.begin();
    while (itr != m_timelineProducers.end()) {
        itr = m_timelineProducers.erase(itr);
    }
147 148
}

149 150
void ProjectClip::connectEffectStack()
{
151 152 153 154
    connect(m_effectStack.get(), &EffectStackModel::modelChanged, this, &ProjectClip::updateChildProducers);
    connect(m_effectStack.get(), &EffectStackModel::dataChanged, this, &ProjectClip::updateChildProducers);
    /*connect(m_effectStack.get(), &EffectStackModel::modelChanged, [&](){
        qDebug()<<"/ / / STACK CHANGED";
155
        updateChildProducers();
156
    });*/
157 158
}

159 160
QString ProjectClip::getToolTip() const
{
161
    return url();
162 163
}

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

177
void ProjectClip::updateAudioThumbnail(QVariantList audioLevels)
178
{
179
    std::swap(audioFrameCache, audioLevels); // avoid second copy
180
    m_audioThumbCreated = true;
181 182 183 184
    if (auto ptr = m_model.lock()) {
        emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshAudioThumbs(m_binId);
    }
    updateTimelineClips({TimelineModel::AudioLevelsRole});
185 186 187 188
}

bool ProjectClip::audioThumbCreated() const
{
189
    return (m_audioThumbCreated);
190 191
}

192 193
ClipType ProjectClip::clipType() const
{
194
    return m_clipType;
195 196
}

197 198
bool ProjectClip::hasParent(const QString &id) const
{
199 200
    std::shared_ptr<AbstractProjectItem> par = parent();
    while (par) {
201 202 203 204
        if (par->clipId() == id) {
            return true;
        }
        par = par->parent();
205 206 207 208
    }
    return false;
}

209
std::shared_ptr<ProjectClip> ProjectClip::clip(const QString &id)
210
{
Nicolas Carion's avatar
Nicolas Carion committed
211
    if (id == m_binId) {
212
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
213
    }
214
    return std::shared_ptr<ProjectClip>();
215 216
}

217
std::shared_ptr<ProjectFolder> ProjectClip::folder(const QString &id)
218
{
219
    Q_UNUSED(id)
220
    return std::shared_ptr<ProjectFolder>();
221 222
}

223
std::shared_ptr<ProjectSubClip> ProjectClip::getSubClip(int in, int out)
224
{
225
    for (int i = 0; i < childCount(); ++i) {
226
        std::shared_ptr<ProjectSubClip> clip = std::static_pointer_cast<ProjectSubClip>(child(i))->subClip(in, out);
227 228 229 230
        if (clip) {
            return clip;
        }
    }
231
    return std::shared_ptr<ProjectSubClip>();
232 233
}

234 235 236
QStringList ProjectClip::subClipIds() const
{
    QStringList subIds;
237
    for (int i = 0; i < childCount(); ++i) {
238
        std::shared_ptr<AbstractProjectItem> clip = std::static_pointer_cast<AbstractProjectItem>(child(i));
239 240 241 242 243 244 245
        if (clip) {
            subIds << clip->clipId();
        }
    }
    return subIds;
}

246
std::shared_ptr<ProjectClip> ProjectClip::clipAt(int ix)
247
{
Nicolas Carion's avatar
Nicolas Carion committed
248
    if (ix == row()) {
249
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
250
    }
251
    return std::shared_ptr<ProjectClip>();
252 253
}

254
/*bool ProjectClip::isValid() const
255
{
256 257
    return m_controller->isValid();
}*/
258

259 260
bool ProjectClip::hasUrl() const
{
261
    if ((m_clipType != ClipType::Color) && (m_clipType != ClipType::Unknown)) {
262
        return (!clipUrl().isEmpty());
263
    }
264 265 266
    return false;
}

267
const QString ProjectClip::url() const
268
{
269
    return clipUrl();
270 271
}

272
GenTime ProjectClip::duration() const
273
{
274
    return getPlaytime();
275 276
}

277 278 279
int ProjectClip::frameDuration() const
{
    GenTime d = duration();
280
    return d.frames(pCore->getCurrentFps());
281 282
}

283
void ProjectClip::reloadProducer(bool refreshOnly)
284
{
285 286 287
    // we find if there are some loading job on that clip
    int loadjobId = -1;
    pCore->jobManager()->hasPendingJob(clipId(), AbstractClipJob::LOADJOB, &loadjobId);
288
    if (refreshOnly) {
289 290 291 292 293 294 295 296 297
        // In that case, we only want a new thumbnail.
        // We thus set up a thumb job. We must make sure that there is no pending LOADJOB
        std::vector<int> parentJobs;
        if (loadjobId != -1) parentJobs.push_back(loadjobId);
        pCore->jobManager()->startJob<ThumbJob>({clipId()}, parentJobs, QString(), 150, -1, true);

    } else {
        // TODO refac: launch load job
        // if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->reloadProducer(m_binId, xml);
298
    }
299 300
}

301
QDomElement ProjectClip::toXml(QDomDocument &document, bool includeMeta)
302
{
303 304
    getProducerXML(document, includeMeta);
    QDomElement prod = document.documentElement().firstChildElement(QStringLiteral("producer"));
305
    if (m_clipType != ClipType::Unknown) {
306
        prod.setAttribute(QStringLiteral("type"), (int)m_clipType);
307
    }
308
    return prod;
309 310
}

Laurent Montel's avatar
Laurent Montel committed
311
void ProjectClip::setThumbnail(const QImage &img)
312
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
313 314
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
315
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
316
        QPainter p(&thumb);
317
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
318
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
319 320 321 322 323 324 325 326
        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
327
    m_thumbnail = QIcon(thumb);
328
    updateTimelineClips(QVector<int>() << TimelineModel::ReloadThumb);
329
    if (auto ptr = m_model.lock()) std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
330 331
}

332 333 334 335 336
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

337
bool ProjectClip::setProducer(std::shared_ptr<Mlt::Producer> producer, bool replaceProducer)
338
{
339
    qDebug() << "################### ProjectClip::setproducer";
Nicolas Carion's avatar
Nicolas Carion committed
340
    updateProducer(std::move(producer));
341

342 343 344
    // Update info
    if (m_name.isEmpty()) {
        m_name = clipName();
345
    }
346 347 348
    m_date = date;
    m_description = ClipController::description();
    m_temporaryUrl.clear();
349
    if (m_clipType == ClipType::Audio) {
350
        m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
351
    } else if (m_clipType == ClipType::Image) {
352
        if (getProducerIntProperty(QStringLiteral("meta.media.width")) < 8 || getProducerIntProperty(QStringLiteral("meta.media.height")) < 8) {
353 354
            KMessageBox::information(QApplication::activeWindow(),
                                     i18n("Image dimension smaller than 8 pixels.\nThis is not correctly supported by our video framework."));
355
        }
356
    }
357
    m_duration = getStringDuration();
358
    m_clipStatus = StatusReady;
359
    if (!hasProxy()) {
Nicolas Carion's avatar
Nicolas Carion committed
360
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshPanel(m_binId);
361
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
362 363 364
    if (auto ptr = m_model.lock()) {
        std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
    }
365
    // Make sure we have a hash for this clip
366
    getFileHash();
367
    return true;
368 369
}

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

396 397 398
std::shared_ptr<Mlt::Producer> ProjectClip::timelineProducer(PlaylistState::ClipState state, int track)
{
    if (!m_service.startsWith(QLatin1String("avformat"))) {
399 400 401 402 403 404
        std::shared_ptr<Mlt::Producer>prod(originalProducer()->cut());
        int length = getProducerIntProperty(QStringLiteral("kdenlive:duration"));
        if (length > 0) {
            prod->set_in_and_out(0, length);
        }
        return prod;
405 406 407
    }
    if (state == PlaylistState::VideoOnly) {
        if (m_timelineProducers.count(0) > 0) {
408
            return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(0)->second->cut());
409 410
        }
        std::shared_ptr<Mlt::Producer> videoProd = cloneProducer();
411
        videoProd->set("audio_index", -1);
412
        m_timelineProducers[0] = videoProd;
413
        return std::shared_ptr<Mlt::Producer>(videoProd->cut());
414 415 416
    }
    if (state == PlaylistState::AudioOnly) {
        if (m_timelineProducers.count(-track) > 0) {
417
            return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(-track)->second->cut());
418 419
        }
        std::shared_ptr<Mlt::Producer> audioProd = cloneProducer();
420
        audioProd->set("video_index", -1);
421
        m_timelineProducers[-track] = audioProd;
422
        return std::shared_ptr<Mlt::Producer>(audioProd->cut());
423 424
    }
    if (m_timelineProducers.count(track) > 0) {
425
        return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(track)->second->cut());
426 427 428
    }
    std::shared_ptr<Mlt::Producer> normalProd = cloneProducer();
    m_timelineProducers[track] = normalProd;
429
    return std::shared_ptr<Mlt::Producer>(normalProd->cut());
430 431 432
}

std::shared_ptr<Mlt::Producer> ProjectClip::cloneProducer()
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
{
    Mlt::Consumer c(*m_masterProducer->profile(), "xml", "string");
    Mlt::Service s(m_masterProducer->get_service());
    int ignore = s.get_int("ignore_points");
    if (ignore) {
        s.set("ignore_points", 0);
    }
    c.connect(s);
    c.set("time_format", "frames");
    c.set("no_meta", 1);
    c.set("no_root", 1);
    c.set("no_profile", 1);
    c.set("root", "/");
    c.set("store", "kdenlive");
    c.start();
    if (ignore) {
        s.set("ignore_points", ignore);
    }
    const QByteArray clipXml = c.get("string");
452
    std::shared_ptr<Mlt::Producer> prod(new Mlt::Producer(*m_masterProducer->profile(), "xml-string", clipXml.constData()));
453
    return prod;
454 455
}

456
bool ProjectClip::isReady() const
457
{
458
    return m_clipStatus == StatusReady;
459 460
}

461
/*void ProjectClip::setZone(const QPoint &zone)
462 463
{
    m_zone = zone;
464
}*/
465 466 467

QPoint ProjectClip::zone() const
{
468 469
    int x = getProducerIntProperty(QStringLiteral("kdenlive:zone_in"));
    int y = getProducerIntProperty(QStringLiteral("kdenlive:zone_out"));
470 471 472
    if (y <= x) {
        y = getFramePlaytime() - 1;
    }
473
    return QPoint(x, y);
474 475 476 477
}

const QString ProjectClip::hash()
{
478
    QString clipHash = getProducerProperty(QStringLiteral("kdenlive:file_hash"));
479 480
    if (!clipHash.isEmpty()) {
        return clipHash;
481
    }
482
    return getFileHash();
483 484
}

485
const QString ProjectClip::getFileHash()
486
{
487 488
    QByteArray fileData;
    QByteArray fileHash;
489
    switch (m_clipType) {
490
    case ClipType::SlideShow:
491
        fileData = clipUrl().toUtf8();
492 493
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
494
    case ClipType::Text:
495
        fileData = getProducerProperty(QStringLiteral("xmldata")).toUtf8();
496 497
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
498
    case ClipType::QText:
499
        fileData = getProducerProperty(QStringLiteral("text")).toUtf8();
500 501
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
502
    case ClipType::Color:
503
        fileData = getProducerProperty(QStringLiteral("resource")).toUtf8();
504 505 506
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    default:
507
        QFile file(clipUrl());
508 509 510 511 512
        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)
             */
513 514
            if (file.size() > 2000000) {
                fileData = file.read(1000000);
515
                if (file.seek(file.size() - 1000000)) {
516
                    fileData.append(file.readAll());
517 518
                }
            } else {
519
                fileData = file.readAll();
520
            }
521
            file.close();
Nicolas Carion's avatar
Nicolas Carion committed
522
            ClipController::setProducerProperty(QStringLiteral("kdenlive:file_size"), QString::number(file.size()));
523
            fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
524 525 526 527 528
        }
        break;
    }
    if (fileHash.isEmpty()) {
        return QString();
529 530
    }
    QString result = fileHash.toHex();
Nicolas Carion's avatar
Nicolas Carion committed
531
    ClipController::setProducerProperty(QStringLiteral("kdenlive:file_hash"), result);
532
    return result;
533
}
534 535 536

double ProjectClip::getOriginalFps() const
{
537
    return originalFps();
538
}
539 540 541

bool ProjectClip::hasProxy() const
{
542
    QString proxy = getProducerProperty(QStringLiteral("kdenlive:proxy"));
543
    return proxy.size() > 2;
544 545
}

Laurent Montel's avatar
Laurent Montel committed
546
void ProjectClip::setProperties(const QMap<QString, QString> &properties, bool refreshPanel)
547 548
{
    QMapIterator<QString, QString> i(properties);
Laurent Montel's avatar
Laurent Montel committed
549
    QMap<QString, QString> passProperties;
550
    bool refreshAnalysis = false;
551
    bool reload = false;
552
    bool refreshOnly = true;
553 554
    // Some properties also need to be passed to track producers
    QStringList timelineProperties;
Laurent Montel's avatar
Laurent Montel committed
555 556
    if (properties.contains(QStringLiteral("templatetext"))) {
        m_description = properties.value(QStringLiteral("templatetext"));
557 558
        if (auto ptr = m_model.lock())
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
559 560
        refreshPanel = true;
    }
Nicolas Carion's avatar
Nicolas Carion committed
561 562 563 564
    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");
565
    QStringList keys;
Nicolas Carion's avatar
Nicolas Carion committed
566 567
    keys << QStringLiteral("luma_duration") << QStringLiteral("luma_file") << QStringLiteral("fade") << QStringLiteral("ttl") << QStringLiteral("softness")
         << QStringLiteral("crop") << QStringLiteral("animation");
568 569
    while (i.hasNext()) {
        i.next();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
570
        setProducerProperty(i.key(), i.value());
571
        if (m_clipType == ClipType::SlideShow && keys.contains(i.key())) {
572
            reload = true;
573
            refreshOnly = false;
574
        }
575 576 577
        if (i.key().startsWith(QLatin1String("kdenlive:clipanalysis"))) {
            refreshAnalysis = true;
        }
578 579 580
        if (timelineProperties.contains(i.key())) {
            passProperties.insert(i.key(), i.value());
        }
581
    }
582 583
    if (properties.contains(QStringLiteral("kdenlive:proxy"))) {
        QString value = properties.value(QStringLiteral("kdenlive:proxy"));
584
        // If value is "-", that means user manually disabled proxy on this clip
585
        if (value.isEmpty() || value == QLatin1String("-")) {
586
            // reset proxy
587
            if (auto ptr = m_model.lock()) {
588 589
                // TODO refac
                // emit std::static_pointer_cast<ProjectItemModel>(ptr)->discardJobs(m_binId, AbstractClipJob::PROXYJOB);
590
                reloadProducer();
591
            }
592
        } else {
593
            // A proxy was requested, make sure to keep original url
594
            setProducerProperty(QStringLiteral("kdenlive:originalurl"), url());
595
            if (auto ptr = m_model.lock()) {
596 597
                // TODO refac
                //  emit std::static_pointer_cast<ProjectItemModel>(ptr)->startJob(m_binId, AbstractClipJob::PROXYJOB);
598
            }
599
        }
Nicolas Carion's avatar
Nicolas Carion committed
600 601
    } else if (properties.contains(QStringLiteral("resource")) || properties.contains(QStringLiteral("templatetext")) ||
               properties.contains(QStringLiteral("autorotate"))) {
602
        // Clip resource changed, update thumbnail
603
        if (m_clipType != ClipType::Color) {
604
            reloadProducer();
605 606
        } else {
            reload = true;
607
        }
608
    }
609
    if (properties.contains(QStringLiteral("xmldata")) || !passProperties.isEmpty()) {
610
        reload = true;
611
    }
612 613 614
    if (refreshAnalysis) {
        emit refreshAnalysisPanel();
    }
615
    if (properties.contains(QStringLiteral("length")) || properties.contains(QStringLiteral("kdenlive:duration"))) {
616
        m_duration = getStringDuration();
617 618
        if (auto ptr = m_model.lock())
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
619 620
        refreshOnly = false;
        reload = true;
621 622
    }

623 624
    if (properties.contains(QStringLiteral("kdenlive:clipname"))) {
        m_name = properties.value(QStringLiteral("kdenlive:clipname"));
625
        refreshPanel = true;
626
        if (auto ptr = m_model.lock()) {
627
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()));
628 629 630
        }
        // update timeline clips
        updateTimelineClips(QVector<int>() << TimelineModel::NameRole);
631
    }
632 633 634 635
    if (refreshPanel) {
        // Some of the clip properties have changed through a command, update properties panel
        emit refreshPropertiesPanel();
    }
636
    if (reload) {
637
        // producer has changed, refresh monitor and thumbnail
638
        reloadProducer(refreshOnly);
639
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshClip(m_binId);
640
    }
641
    if (!passProperties.isEmpty()) {
642
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->updateTimelineProducers(m_binId, passProperties);
643
    }
644 645
}

646 647
ClipPropertiesController *ProjectClip::buildProperties(QWidget *parent)
{
648 649
    auto ptr = m_model.lock();
    Q_ASSERT(ptr);
650
    ClipPropertiesController *panel = new ClipPropertiesController(static_cast<ClipController *>(this), parent);
Laurent Montel's avatar
Laurent Montel committed
651 652
    connect(this, &ProjectClip::refreshPropertiesPanel, panel, &ClipPropertiesController::slotReloadProperties);
    connect(this, &ProjectClip::refreshAnalysisPanel, panel, &ClipPropertiesController::slotFillAnalysisData);
653 654 655
    return panel;
}

656 657
void ProjectClip::updateParentInfo(const QString &folderid, const QString &foldername)
{
658
    Q_UNUSED(foldername);
Nicolas Carion's avatar
Nicolas Carion committed
659
    ClipController::setProducerProperty(QStringLiteral("kdenlive:folderid"), folderid);
660 661
}

Laurent Montel's avatar
Laurent Montel committed
662
bool ProjectClip::matches(const QString &condition)
663
{
Nicolas Carion's avatar
Nicolas Carion committed
664
    // TODO
665
    Q_UNUSED(condition)
666 667 668
    return true;
}

669
bool ProjectClip::rename(const QString &name, int column)
670
{
Laurent Montel's avatar
Laurent Montel committed
671 672
    QMap<QString, QString> newProperites;
    QMap<QString, QString> oldProperites;
673 674
    bool edited = false;
    switch (column) {
675 676 677 678
    case 0:
        if (m_name == name) {
            return false;
        }
679
        // Rename clip
680 681
        oldProperites.insert(QStringLiteral("kdenlive:clipname"), m_name);
        newProperites.insert(QStringLiteral("kdenlive:clipname"), name);
682 683 684
        m_name = name;
        edited = true;
        break;
685 686 687 688
    case 2:
        if (m_description == name) {
            return false;
        }
689
        // Rename clip
690
        if (m_clipType == ClipType::TextTemplate) {
691 692 693 694 695 696
            oldProperites<