projectclip.cpp 35 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"
34
#include "mltcontroller/clip.h"
35
#include "mltcontroller/clipcontroller.h"
36
#include "mltcontroller/clippropertiescontroller.h"
37
#include "model/markerlistmodel.hpp"
38
#include "profiles/profilemodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
39
#include "project/projectcommands.h"
40
#include "project/projectmanager.h"
Nicolas Carion's avatar
Nicolas Carion committed
41 42 43 44
#include "projectfolder.h"
#include "projectitemmodel.h"
#include "projectsubclip.h"
#include "timecode.h"
Nicolas Carion's avatar
style  
Nicolas Carion committed
45
#include "timeline2/model/snapmodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
46
#include "utils/KoIconUtils.h"
47
#include "utils/thumbnailcache.hpp"
48
#include "xml/xml.hpp"
49
#include <QPainter>
50
#include <kimagecache.h>
51

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

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

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

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

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

133 134
ProjectClip::~ProjectClip()
{
135
    // controller is deleted in bincontroller
136
    m_thumbMutex.lock();
137
    m_requestedThumbs.clear();
138
    m_thumbMutex.unlock();
139
    m_thumbThread.waitForFinished();
140
    audioFrameCache.clear();
141 142 143 144 145
    // 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);
    }
146 147
}

148 149
void ProjectClip::connectEffectStack()
{
150 151 152 153
    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";
154
        updateChildProducers();
155
    });*/
156 157
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

282
void ProjectClip::reloadProducer(bool refreshOnly)
283
{
284 285 286
    // we find if there are some loading job on that clip
    int loadjobId = -1;
    pCore->jobManager()->hasPendingJob(clipId(), AbstractClipJob::LOADJOB, &loadjobId);
287
    if (refreshOnly) {
288 289 290 291 292 293 294 295 296
        // 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);
297
    }
298 299
}

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

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

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

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

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

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

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

std::shared_ptr<Mlt::Producer> ProjectClip::cloneProducer()
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
{
    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");
453
    std::shared_ptr<Mlt::Producer> prod(new Mlt::Producer(*m_masterProducer->profile(), "xml-string", clipXml.constData()));
454
    return prod;
455 456
}

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

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

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

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

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

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

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

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

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

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

657
void ProjectClip::updateParent(std::shared_ptr<TreeItem> parent)
658
{
659 660 661 662 663 664
    if (parent) {
        auto item = std::static_pointer_cast<AbstractProjectItem>(parent);
        ClipController::setProducerProperty(QStringLiteral("kdenlive:folderid"), item->clipId());
        qDebug() << "Setting parent to " << item->clipId();
    }
    AbstractProjectItem::updateParent(parent);
665 666
}

Laurent Montel's avatar
Laurent Montel committed
667
bool ProjectClip::matches(const QString &condition)
668
{
Nicolas Carion's avatar
Nicolas Carion committed
669
    // TODO
670
    Q_UNUSED(condition)
671 672 673
    return true;
}

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