projectclip.cpp 47.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
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
by the membership of KDE e.V.), which shall act as a proxy 
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 "projectfolder.h"
25
#include "projectsubclip.h"
26
#include "bin.h"
27
#include "timecode.h"
28 29
#include "doc/kthumb.h"
#include "kdenlivesettings.h"
30
#include "timeline/clip.h"
31
#include "project/projectcommands.h"
32
#include "mltcontroller/clipcontroller.h"
33
#include "lib/audio/audioStreamInfo.h"
34
#include "utils/KoIconUtils.h"
35
#include "mltcontroller/clippropertiescontroller.h"
36 37 38

#include <QDomElement>
#include <QFile>
39
#include <QDir>
40 41
#include <QDebug>
#include <QCryptographicHash>
42
#include <QtConcurrent>
43
#include <KLocalizedString>
44
#include <KMessageBox>
45 46


Laurent Montel's avatar
Laurent Montel committed
47
ProjectClip::ProjectClip(const QString &id, const QIcon &thumb, ClipController *controller, ProjectFolder* parent) :
48
    AbstractProjectItem(AbstractProjectItem::ClipItem, id, parent)
49
    , m_abortAudioThumb(false)
Vincent Pinon's avatar
Vincent Pinon committed
50
    , m_controller(controller)
51
    , m_thumbsProducer(Q_NULLPTR)
52
{
53
    m_clipStatus = StatusReady;
54 55
    m_name = m_controller->clipName();
    m_duration = m_controller->getStringDuration();
56
    m_date = m_controller->date;
57
    m_description = m_controller->description();
58
    m_type = m_controller->clipType();
59
    if (m_type == Audio) {
60
        m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
61 62 63
    } else {
        m_thumbnail = thumb;
    }
64 65
    // Make sure we have a hash for this clip
    hash();
66
    setParent(parent);
67
    connect(this, &ProjectClip::updateJobStatus, this, &ProjectClip::setJobStatus);
68
    bin()->loadSubClips(id, m_controller->getPropertiesFromPrefix(QStringLiteral("kdenlive:clipzone.")));
69
    connect(this, &ProjectClip::updateThumbProgress, bin(), &Bin::doUpdateThumbsProgress);
70
    createAudioThumbs();
71 72
}

Laurent Montel's avatar
Laurent Montel committed
73
ProjectClip::ProjectClip(const QDomElement& description, const QIcon &thumb, ProjectFolder* parent) :
74
    AbstractProjectItem(AbstractProjectItem::ClipItem, description, parent)
75
    , m_abortAudioThumb(false)
76
    , m_controller(Q_NULLPTR)
77
    , m_type(Unknown)
78
    , m_thumbsProducer(Q_NULLPTR)
79 80
{
    Q_ASSERT(description.hasAttribute("id"));
81
    m_clipStatus = StatusWaiting;
82
    m_thumbnail = thumb;
83 84
    if (description.hasAttribute(QStringLiteral("type"))) {
        m_type = (ClipType) description.attribute(QStringLiteral("type")).toInt();
85
        if (m_type == Audio) {
86
            m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
87
        }
88
    }
89 90
    m_temporaryUrl = QUrl::fromLocalFile(getXmlProperty(description, QStringLiteral("resource")));
    QString clipName = getXmlProperty(description, QStringLiteral("kdenlive:clipname"));
91 92 93
    if (!clipName.isEmpty()) {
        m_name = clipName;
    }
94 95
    else if (m_temporaryUrl.isValid()) {
        m_name = m_temporaryUrl.fileName();
96
    }
97
    else m_name = i18n("Untitled");
98
    connect(this, &ProjectClip::updateJobStatus, this, &ProjectClip::setJobStatus);
99
    setParent(parent);
100
    connect(this, &ProjectClip::updateThumbProgress, bin(), &Bin::doUpdateThumbsProgress);
101 102 103 104 105
}


ProjectClip::~ProjectClip()
{
106
    // controller is deleted in bincontroller
107
    abortAudioThumbs();
108
    bin()->slotAbortAudioThumb(m_id, duration().ms());
109 110
    QMutexLocker audioLock(&m_audioMutex);
    m_thumbMutex.lock();
111
    m_requestedThumbs.clear();
112
    m_thumbMutex.unlock();
113
    m_thumbThread.waitForFinished();
114
    delete m_thumbsProducer;
115
    audioFrameCache.clear();
116 117
}

118 119 120
void ProjectClip::abortAudioThumbs()
{
    m_abortAudioThumb = true;
121
    emit doAbortAudioThumbs();
122 123
}

124 125 126 127 128
QString ProjectClip::getToolTip() const
{
    return url().toLocalFile();
}

129
QString ProjectClip::getXmlProperty(const QDomElement &producer, const QString &propertyName, const QString &defaultValue)
130
{
131
    QString value = defaultValue;
132
    QDomNodeList props = producer.elementsByTagName(QStringLiteral("property"));
133
    for (int i = 0; i < props.count(); ++i) {
134
        if (props.at(i).toElement().attribute(QStringLiteral("name")) == propertyName) {
135 136 137 138 139 140 141
            value = props.at(i).firstChild().nodeValue();
            break;
        }
    }
    return value;
}

Laurent Montel's avatar
Laurent Montel committed
142
void ProjectClip::updateAudioThumbnail(const QVariantList &audioLevels)
143
{
144
    audioFrameCache = audioLevels;
145
    m_controller->audioThumbCreated = true;
146
    bin()->emitRefreshAudioThumbs(m_id);
147 148 149 150 151 152 153 154 155
    emit gotAudioData();
}

QList < CommentedTime > ProjectClip::commentedSnapMarkers() const
{
    if (m_controller) return m_controller->commentedSnapMarkers();
    return QList < CommentedTime > ();
}

156 157 158 159 160 161
QStringList ProjectClip::markersText(GenTime in, GenTime out) const
{
    if (m_controller) return m_controller->markerComments(in, out);
    return QStringList();
}

162 163
bool ProjectClip::audioThumbCreated() const
{
164
    return (m_controller && m_controller->audioThumbCreated);
165 166
}

167 168
ClipType ProjectClip::clipType() const
{
169
    return m_type;
170 171
}

172 173 174 175 176 177 178 179 180 181 182 183
bool ProjectClip::hasParent(const QString &id) const
{
    AbstractProjectItem *par = parent();
    while (par) {
	if (par->clipId() == id) {
	    return true;
	}
	par = par->parent();
    }
    return false;
}

184 185 186 187 188
ProjectClip* ProjectClip::clip(const QString &id)
{
    if (id == m_id) {
        return this;
    }
189
    return Q_NULLPTR;
190 191
}

192 193
ProjectFolder* ProjectClip::folder(const QString &id)
{
194
    Q_UNUSED(id)
195
    return Q_NULLPTR;
196 197
}

198 199 200 201 202
void ProjectClip::disableEffects(bool disable)
{
    if (m_controller) m_controller->disableEffects(disable);
}

203 204 205
ProjectSubClip* ProjectClip::getSubClip(int in, int out)
{
    for (int i = 0; i < count(); ++i) {
Vincent Pinon's avatar
Vincent Pinon committed
206
        ProjectSubClip *clip = static_cast<ProjectSubClip *>(at(i))->subClip(in, out);
207 208 209 210
        if (clip) {
            return clip;
        }
    }
211
    return Q_NULLPTR;
212 213
}

214 215 216 217 218 219 220 221 222 223 224 225
QStringList ProjectClip::subClipIds() const
{
    QStringList subIds;
    for (int i = 0; i < count(); ++i) {
        AbstractProjectItem *clip = at(i);
        if (clip) {
            subIds << clip->clipId();
        }
    }
    return subIds;
}

226 227 228 229 230
ProjectClip* ProjectClip::clipAt(int ix)
{
    if (ix == index()) {
        return this;
    }
231
    return Q_NULLPTR;
232 233
}

234
/*bool ProjectClip::isValid() const
235
{
236 237
    return m_controller->isValid();
}*/
238

239 240 241 242 243 244
bool ProjectClip::hasUrl() const
{
    if (m_controller && (m_type != Color) && (m_type != Unknown)) return (m_controller->clipUrl().isValid());
    return false;
}

245 246
QUrl ProjectClip::url() const
{
247
    if (m_controller) return m_controller->clipUrl();
248
    return m_temporaryUrl;
249 250 251 252
}

bool ProjectClip::hasLimitedDuration() const
{
253 254 255 256
    if (m_controller) {
        return m_controller->hasLimitedDuration();
    }
    return true;
257 258
}

259
GenTime ProjectClip::duration() const
260
{
261 262
    if (m_controller) {
	return m_controller->getPlaytime();
263
    }
264
    return GenTime();
265 266
}

267
void ProjectClip::reloadProducer(bool refreshOnly)
268 269 270
{
    QDomDocument doc;
    QDomElement xml = toXml(doc);
271
    if (refreshOnly) {
272
        // set a special flag to request thumbnail only
273
        xml.setAttribute(QStringLiteral("refreshOnly"), QStringLiteral("1"));
274
    }
275
    bin()->reloadProducer(m_id, xml);
276 277 278 279
}

void ProjectClip::setCurrent(bool current, bool notify)
{
280
    Q_UNUSED(notify)
281
    if (current && m_controller) {
Jean-Baptiste Mardelle's avatar
cleanup  
Jean-Baptiste Mardelle committed
282
        bin()->openProducer(m_controller);
283
        bin()->editMasterEffect(m_controller);
284 285 286
    }
}

287
QDomElement ProjectClip::toXml(QDomDocument& document, bool includeMeta)
288
{
289
    if (m_controller) {
290
        m_controller->getProducerXML(document, includeMeta);
291
        return document.documentElement().firstChildElement(QStringLiteral("producer"));
292 293 294 295 296 297 298 299 300 301
    } else {
        // We only have very basic infos, ike id and url, pass them
        QDomElement prod = document.createElement(QStringLiteral("producer"));
        prod.setAttribute(QStringLiteral("id"), m_id);
        EffectsList::setProperty(prod, QStringLiteral("resource"), m_temporaryUrl.path());
        if (m_type != Unknown) {
            prod.setAttribute(QStringLiteral("type"), (int) m_type);
        }
        document.appendChild(prod);
        return prod;
302 303 304
    }
}

Laurent Montel's avatar
Laurent Montel committed
305
void ProjectClip::setThumbnail(const QImage &img)
306
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
307 308
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
309
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
310
        QPainter p(&thumb);
311
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
312
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
313 314 315 316 317 318 319 320
        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
321
    m_thumbnail = QIcon(thumb);
322
    emit thumbUpdated(img);
323 324 325
    bin()->emitItemUpdated(this);
}

326 327 328 329 330
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

331
bool ProjectClip::setProducer(ClipController *controller, bool replaceProducer)
332
{
333
    if (!replaceProducer && m_controller) {
Vincent Pinon's avatar
Vincent Pinon committed
334
        qDebug()<<"// RECEIVED PRODUCER BUT WE ALREADY HAVE ONE\n----------";
335
        return false;
336
    }
337
    bool isNewProducer = true;
338 339
    if (m_controller) {
        // Replace clip for this controller
Laurent Montel's avatar
Laurent Montel committed
340
        resetProducerProperty(QStringLiteral("kdenlive:file_hash"));
341
        isNewProducer = false;
342
    }
343
    else if (controller) {
344 345 346
        // We did not yet have the controller, update info
        m_controller = controller;
        if (m_name.isEmpty()) m_name = m_controller->clipName();
347
        m_date = m_controller->date;
348
        m_description = m_controller->description();
349
        m_temporaryUrl.clear();
350 351 352
        if (m_type == Unknown) {
            m_type = m_controller->clipType();
            if (m_type == Audio) {
353
                m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
354 355
            }
        }
356
    }
Vincent Pinon's avatar
Vincent Pinon committed
357 358
    if (m_controller)
        m_duration = m_controller->getStringDuration();
359
    m_clipStatus = StatusReady;
360
    if (!hasProxy()) bin()->emitRefreshPanel(m_id);
361
    bin()->emitItemUpdated(this);
362 363
    // Make sure we have a hash for this clip
    hash();
364
    createAudioThumbs();
365
    return isNewProducer;
366 367 368 369
}

void ProjectClip::createAudioThumbs()
{
370
    if (KdenliveSettings::audiothumbnails() && (m_type == AV || m_type == Audio || m_type == Playlist)) {
371 372
        bin()->requestAudioThumbs(m_id, duration().ms());
        emit updateJobStatus(AbstractClipJob::THUMBJOB, JobWaiting, 0);
373
    }
374 375
}

376
Mlt::Producer *ProjectClip::originalProducer()
377
{
378
    if (!m_controller) {
379
        return Q_NULLPTR;
380 381
    }
    return &m_controller->originalProducer();
382 383
}

384 385 386 387 388 389
Mlt::Producer *ProjectClip::thumbProducer()
{
    QMutexLocker locker(&m_producerMutex);
    if (m_thumbsProducer) {
        return m_thumbsProducer;
    }
390
    if (!m_controller || m_controller->clipType() == Unknown) {
391
        return Q_NULLPTR;
392 393
    }
    Mlt::Producer prod = m_controller->originalProducer();
394
    if (!prod.is_valid())
395
        return Q_NULLPTR;
396 397
    Clip clip(prod);
    if (KdenliveSettings::gpu_accel()) {
398
        m_thumbsProducer = clip.softClone(ClipController::getPassPropertiesList());
399 400 401 402
        Mlt::Filter scaler(*prod.profile(), "swscale");
        Mlt::Filter converter(*prod.profile(), "avcolor_space");
        m_thumbsProducer->attach(scaler);
        m_thumbsProducer->attach(converter);
403 404
    } else {
        m_thumbsProducer = clip.clone();
405 406 407 408
    }
    return m_thumbsProducer;
}

409 410 411 412 413
ClipController *ProjectClip::controller()
{
    return m_controller;
}

414
bool ProjectClip::isReady() const
415
{
416
    return m_controller != Q_NULLPTR && m_clipStatus == StatusReady;
417 418
}

419
/*void ProjectClip::setZone(const QPoint &zone)
420 421
{
    m_zone = zone;
422
}*/
423 424 425

QPoint ProjectClip::zone() const
{
426 427
    int x = getProducerIntProperty(QStringLiteral("kdenlive:zone_in"));
    int y = getProducerIntProperty(QStringLiteral("kdenlive:zone_out"));
428
    return QPoint(x, y);
429 430
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
431 432 433 434 435 436 437 438
void ProjectClip::resetProducerProperty(const QString &name)
{
    if (m_controller) {
        m_controller->resetProperty(name);
    }
}

void ProjectClip::setProducerProperty(const QString &name, int data)
439
{
440 441
    if (m_controller) {
	m_controller->setProperty(name, data);
442 443 444
    }
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
445
void ProjectClip::setProducerProperty(const QString &name, double data)
446
{
447 448
    if (m_controller) {
        m_controller->setProperty(name, data);
449 450 451
    }
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
452
void ProjectClip::setProducerProperty(const QString &name, const QString &data)
453
{
454 455
    if (m_controller) {
        m_controller->setProperty(name, data);
456 457 458
    }
}

459 460 461 462 463 464 465 466 467 468 469 470 471 472
QMap <QString, QString> ProjectClip::currentProperties(const QMap <QString, QString> &props)
{
    QMap <QString, QString> currentProps;
    if (!m_controller) {
        return currentProps;
    }
    QMap<QString, QString>::const_iterator i = props.constBegin();
    while (i != props.constEnd()) {
        currentProps.insert(i.key(), m_controller->property(i.key()));
        ++i;
    }
    return currentProps;
}

473 474 475 476 477 478 479 480
QColor ProjectClip::getProducerColorProperty(const QString &key) const
{
    if (m_controller) {
        return m_controller->color_property(key);
    }
    return QColor();
}

481 482 483 484 485 486 487 488
int ProjectClip::getProducerIntProperty(const QString &key) const
{
    int value = 0;
    if (m_controller) {
        value = m_controller->int_property(key);
    }
    return value;
}
489

490 491 492 493 494 495 496 497 498
qint64 ProjectClip::getProducerInt64Property(const QString &key) const
{
    qint64 value = 0;
    if (m_controller) {
        value = m_controller->int64_property(key);
    }
    return value;
}

499 500 501 502 503 504 505 506 507
double ProjectClip::getDoubleProducerProperty(const QString &key) const
{
    double value = 0;
    if (m_controller) {
        value = m_controller->double_property(key);
    }
    return value;
}

508
QString ProjectClip::getProducerProperty(const QString &key) const
509 510
{
    QString value;
511 512
    if (m_controller) {
	value = m_controller->property(key);
513 514 515 516 517 518
    }
    return value;
}

const QString ProjectClip::hash()
{
519
    if (m_controller) {
520
        QString clipHash = m_controller->property(QStringLiteral("kdenlive:file_hash"));
521 522
        if (!clipHash.isEmpty()) {
            return clipHash;
523
        }
524
    }
525
    return getFileHash();
526 527
}

528
const QString ProjectClip::getFileHash() const
529
{
530 531 532 533 534 535 536 537
    QByteArray fileData;
    QByteArray fileHash;
    switch (m_type) {
      case SlideShow:
          fileData = m_controller ? m_controller->clipUrl().toLocalFile().toUtf8() : m_temporaryUrl.toLocalFile().toUtf8();
          fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
          break;
      case Text:
538
          fileData = m_controller ? m_controller->property(QStringLiteral("xmldata")).toUtf8() : name().toUtf8();
539 540
          fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
          break;
Vincent Pinon's avatar
Vincent Pinon committed
541 542 543 544
      case QText:
          fileData = m_controller ? m_controller->property(QStringLiteral("text")).toUtf8() : name().toUtf8();
          fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
          break;
545
      case Color:
546
          fileData = m_controller ? m_controller->property(QStringLiteral("resource")).toUtf8() : name().toUtf8();
547
          fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
548 549 550 551 552
          break;
      default:
          QFile file(m_controller ? m_controller->clipUrl().toLocalFile() : m_temporaryUrl.toLocalFile());
          if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
              /*
553 554 555
               * 1 MB = 1 second per 450 files (or faster)
               * 10 MB = 9 seconds per 450 files (or faster)
               */
556 557 558 559 560 561 562
            if (file.size() > 2000000) {
                fileData = file.read(1000000);
                if (file.seek(file.size() - 1000000))
                    fileData.append(file.readAll());
            } else
                fileData = file.readAll();
            file.close();
563
            if (m_controller) m_controller->setProperty(QStringLiteral("kdenlive:file_size"), QString::number(file.size()));
564 565 566 567 568 569 570
            fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
          }
          break;
    }
    if (fileHash.isEmpty()) return QString();
    QString result = fileHash.toHex();
    if (m_controller) {
571
	m_controller->setProperty(QStringLiteral("kdenlive:file_hash"), result);
572
    }
573
    return result;
574
}
575 576 577 578 579 580

double ProjectClip::getOriginalFps() const
{
    if (!m_controller) return 0;
    return m_controller->originalFps();
}
581 582 583

bool ProjectClip::hasProxy() const
{
584 585
    QString proxy = getProducerProperty(QStringLiteral("kdenlive:proxy"));
    if (proxy.isEmpty() || proxy == QLatin1String("-")) return false;
586 587 588
    return true;
}

Laurent Montel's avatar
Laurent Montel committed
589
void ProjectClip::setProperties(const QMap <QString, QString> &properties, bool refreshPanel)
590 591
{
    QMapIterator<QString, QString> i(properties);
592
    QMap <QString, QString> passProperties;
593
    bool refreshAnalysis = false;
594 595 596
    bool reload = false;
    // Some properties also need to be passed to track producers
    QStringList timelineProperties;
Laurent Montel's avatar
Laurent Montel committed
597 598
    if (properties.contains(QStringLiteral("templatetext"))) {
        m_description = properties.value(QStringLiteral("templatetext"));
599 600 601
        bin()->emitItemUpdated(this);
        refreshPanel = true;
    }
602
    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");
603
    QStringList keys;
604
    keys << QStringLiteral("luma_duration") << QStringLiteral("luma_file") << QStringLiteral("fade") << QStringLiteral("ttl") << QStringLiteral("softness") << QStringLiteral("crop") << QStringLiteral("animation");
605 606
    while (i.hasNext()) {
        i.next();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
607
        setProducerProperty(i.key(), i.value());
608 609 610
        if (m_type == SlideShow && keys.contains(i.key())) {
            reload = true;
        }
611
        if (i.key().startsWith(QLatin1String("kdenlive:clipanalysis"))) refreshAnalysis = true;
612 613 614
        if (timelineProperties.contains(i.key())) {
            passProperties.insert(i.key(), i.value());
        }
615
    }
616 617
    if (properties.contains(QStringLiteral("kdenlive:proxy"))) {
        QString value = properties.value(QStringLiteral("kdenlive:proxy"));
618
        // If value is "-", that means user manually disabled proxy on this clip
619
        if (value.isEmpty() || value == QLatin1String("-")) {
620 621 622 623
            // reset proxy
            if (bin()->hasPendingJob(m_id, AbstractClipJob::PROXYJOB)) {
                bin()->discardJobs(m_id, AbstractClipJob::PROXYJOB);
            }
624 625 626
            else {
                reloadProducer();
            }
627 628
        }
        else {
629
            // A proxy was requested, make sure to keep original url
630
            setProducerProperty(QStringLiteral("kdenlive:originalurl"), url().toLocalFile());
631
            bin()->startJob(m_id, AbstractClipJob::PROXYJOB);
632 633
        }
    }
634
    else if (properties.contains(QStringLiteral("resource")) || properties.contains(QStringLiteral("templatetext")) || properties.contains(QStringLiteral("autorotate"))) {
635
        // Clip resource changed, update thumbnail
636
        if (m_type != Color) {
637
            reloadProducer();
638
        }
639
        else reload = true;
640
    }
641
    if (properties.contains(QStringLiteral("xmldata")) || !passProperties.isEmpty()) {
642
        reload = true;
643
    }
644
    if (refreshAnalysis) emit refreshAnalysisPanel();
645
    if (properties.contains(QStringLiteral("length")) || properties.contains(QStringLiteral("kdenlive:duration"))) {
646 647 648 649
        m_duration = m_controller->getStringDuration();
        bin()->emitItemUpdated(this);
    }

650 651
    if (properties.contains(QStringLiteral("kdenlive:clipname"))) {
        m_name = properties.value(QStringLiteral("kdenlive:clipname"));
652
        refreshPanel = true;
653 654
        bin()->emitItemUpdated(this);
    }
655 656 657 658
    if (refreshPanel) {
        // Some of the clip properties have changed through a command, update properties panel
        emit refreshPropertiesPanel();
    }
659
    if (reload) {
660
        // producer has changed, refresh monitor and thumbnail
661
        reloadProducer(true);
662
        bin()->refreshClip(m_id);
663
    }
664 665 666
    if (!passProperties.isEmpty()) {
        bin()->updateTimelineProducers(m_id, passProperties);
    }
667 668
}

669
void ProjectClip::setJobStatus(int jobType, int status, int progress, const QString &statusMessage)
670
{
671
    m_jobType = (AbstractClipJob::JOBTYPE) jobType;
672
    if (progress > 0) {
673
        if (m_jobProgress == progress) return;
674
        m_jobProgress = progress;
675 676
    }
    else {
677
        m_jobProgress = status;
678 679 680
        if (m_jobType == AbstractClipJob::PROXYJOB && (status == JobAborted || status == JobCrashed)) {
            setProducerProperty(QStringLiteral("kdenlive:proxy"), QStringLiteral("-"));
        }
681 682
        if ((status == JobAborted || status == JobCrashed  || status == JobDone) && !statusMessage.isEmpty()) {
            m_jobMessage = statusMessage;
683
            bin()->emitMessage(statusMessage, 100, OperationCompletedMessage);
684
        }
685 686 687
    }
    bin()->emitItemUpdated(this);
}
688

689 690 691

ClipPropertiesController *ProjectClip::buildProperties(QWidget *parent)
{
692
    ClipPropertiesController *panel = new ClipPropertiesController(bin()->projectTimecode(), m_controller, parent);
Laurent Montel's avatar
Laurent Montel committed
693 694
    connect(this, &ProjectClip::refreshPropertiesPanel, panel, &ClipPropertiesController::slotReloadProperties);
    connect(this, &ProjectClip::refreshAnalysisPanel, panel, &ClipPropertiesController::slotFillAnalysisData);
695 696 697
    return panel;
}

698 699
void ProjectClip::updateParentInfo(const QString &folderid, const QString &foldername)
{
700
    Q_UNUSED(foldername)
701
    m_controller->setProperty(QStringLiteral("kdenlive:folderid"), folderid);
702 703
}

Laurent Montel's avatar
Laurent Montel committed
704
bool ProjectClip::matches(const QString &condition)
705 706
{
    //TODO
707
    Q_UNUSED(condition)
708 709 710
    return true;
}

711
const QString ProjectClip::codec(bool audioCodec) const
712
{
713 714
    if (!m_controller) return QString();
    return m_controller->codec(audioCodec);
715
}
716

717
bool ProjectClip::rename(const QString &name, int column)
718 719 720
{
    QMap <QString, QString> newProperites;
    QMap <QString, QString> oldProperites;
721 722 723 724 725
    bool edited = false;
    switch (column) {
      case 0:
        if (m_name == name) return false;
        // Rename clip
726 727
        oldProperites.insert(QStringLiteral("kdenlive:clipname"), m_name);
        newProperites.insert(QStringLiteral("kdenlive:clipname"), name);
728 729 730 731 732 733
        m_name = name;
        edited = true;
        break;
      case 2:
        if (m_description == name) return false;
        // Rename clip
734 735 736 737 738 739 740
        if (m_type == TextTemplate) {
            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);
        }
741 742 743 744 745 746 747 748
        m_description = name;
        edited = true;
        break;
    }
    if (edited) {
        bin()->slotEditClipCommand(m_id, oldProperites, newProperites);
    }
    return edited;
749 750
}

751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
void ProjectClip::addClipMarker(QList <CommentedTime> newMarkers, QUndoCommand *groupCommand)
{
    if (!m_controller) return;
    QList <CommentedTime> oldMarkers;
    for (int i = 0; i < newMarkers.count(); ++i) {
        CommentedTime oldMarker = m_controller->markerAt(newMarkers.at(i).time());
        if (oldMarker == CommentedTime()) {
            oldMarker = newMarkers.at(i);
            oldMarker.setMarkerType(-1);
        }
        oldMarkers << oldMarker;
    }
    (void) new AddMarkerCommand(this, oldMarkers, newMarkers, groupCommand);
}

bool ProjectClip::deleteClipMarkers(QUndoCommand *command)
{
    QList <CommentedTime> markers = commentedSnapMarkers();
    if (markers.isEmpty()) {
        return false;
    }
    QList <CommentedTime> newMarkers;
    for (int i = 0; i < markers.size(); ++i) {
        CommentedTime marker = markers.at(i);
        marker.setMarkerType(-1);
        newMarkers << marker;
    }
    new AddMarkerCommand(this, markers, newMarkers, command);
779
    return true;
780 781 782 783 784 785 786 787 788 789 790 791 792 793
}

void ProjectClip::addMarkers(QList <CommentedTime> &markers)
{
    if (!m_controller) return;
    for (int i = 0; i < markers.count(); ++i) {
      if (markers.at(i).markerType() < 0) m_controller->deleteSnapMarker(markers.at(i).time());
      else m_controller->addSnapMarker(markers.at(i));
    }
    // refresh markers in clip monitor
    bin()->refreshClipMarkers(m_id);
    // refresh markers in timeline clips
    emit refreshClipDisplay();
}
794

Vincent Pinon's avatar
Vincent Pinon committed
795
void ProjectClip::addEffect(const ProfileInfo &pInfo, QDomElement &effect)
796
{
797
    m_controller->addEffect(pInfo, effect);
798
    bin()->updateMasterEffect(m_controller);
799
    bin()->emitItemUpdated(this);
800 801
}

802 803 804 805 806 807 808 809
void ProjectClip::updateEffect(const ProfileInfo &pInfo, QDomElement &effect, int ix, bool refreshStack)
{
    m_controller->updateEffect(pInfo, effect, ix);
    if (refreshStack)
        bin()->updateMasterEffect(m_controller);
    bin()->emitItemUpdated(this);
}

810
void ProjectClip::removeEffect(int ix)
811
{
812
    m_controller->removeEffect(ix);
813
    bin()->updateMasterEffect(m_controller);
814 815 816 817 818 819 820
    bin()->emitItemUpdated(this);
}

QVariant ProjectClip::data(DataType type) const
{
    switch (type) {
      case AbstractProjectItem::IconOverlay:
821
            return m_controller != Q_NULLPTR ? (m_controller->hasEffects() ? QVariant("kdenlive-track_has_effect") : QVariant()) : QVariant();
822