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

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

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

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

#include "projectclip.h"
#include "projectfolder.h"
25
#include "projectsubclip.h"
26
#include "projectitemmodel.h"
27
#include "bin.h"
28
#include "timecode.h"
29 30
#include "doc/kthumb.h"
#include "kdenlivesettings.h"
31
#include "timeline/clip.h"
32
#include "project/projectcommands.h"
33
#include "mltcontroller/clipcontroller.h"
34
#include "mltcontroller/bincontroller.h"
35
#include "lib/audio/audioStreamInfo.h"
36
#include "utils/KoIconUtils.h"
37
#include "mltcontroller/clippropertiescontroller.h"
38 39
#include "core.h"
#include "profiles/profilemodel.hpp"
40 41 42

#include <QDomElement>
#include <QFile>
43
#include <QDir>
Laurent Montel's avatar
Laurent Montel committed
44
#include "kdenlive_debug.h"
45
#include <QCryptographicHash>
46
#include <QtConcurrent>
47
#include <KLocalizedString>
48
#include <KMessageBox>
Nicolas Carion's avatar
Nicolas Carion committed
49
#include <utility>
50

51 52 53
ProjectClip::ProjectClip(const QString &id, const QIcon &thumb, ProjectItemModel* model, std::shared_ptr<Mlt::Producer> producer, ProjectFolder *parent) :
    AbstractProjectItem(AbstractProjectItem::ClipItem, id, model, parent)
    , ClipController(pCore->binController(), producer)
54
    , m_abortAudioThumb(false)
Laurent Montel's avatar
Laurent Montel committed
55
    , m_thumbsProducer(nullptr)
56
{
57
    m_clipStatus = StatusReady;
58 59 60 61 62
    m_name = clipName();
    m_duration = getStringDuration();
    m_date = date;
    m_description = ClipController::description();
    m_type = clipType();
63
    if (m_type == Audio) {
64
        m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
65 66 67
    } else {
        m_thumbnail = thumb;
    }
68 69
    // Make sure we have a hash for this clip
    hash();
70
    connect(this, &ProjectClip::updateJobStatus, this, &ProjectClip::setJobStatus);
71 72
    static_cast<ProjectItemModel*>(m_model)->bin()->loadSubClips(id, getPropertiesFromPrefix(QStringLiteral("kdenlive:clipzone.")));
    connect(this, &ProjectClip::updateThumbProgress, static_cast<ProjectItemModel*>(m_model)->bin(), &Bin::doUpdateThumbsProgress);
73
    createAudioThumbs();
74 75
}

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

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

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

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

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

Laurent Montel's avatar
Laurent Montel committed
143
void ProjectClip::updateAudioThumbnail(const QVariantList &audioLevels)
144
{
145
    audioFrameCache = audioLevels;
146 147
    m_audioThumbCreated = true;
    static_cast<ProjectItemModel*>(m_model)->bin()->emitRefreshAudioThumbs(m_id);
148 149 150
    emit gotAudioData();
}

151 152
QStringList ProjectClip::markersText(GenTime in, GenTime out) const
{
153
    return markerComments(in, out);
154 155
}

156 157
bool ProjectClip::audioThumbCreated() const
{
158
    return (m_audioThumbCreated);
159 160
}

161 162
ClipType ProjectClip::clipType() const
{
163
    return m_type;
164 165
}

166 167 168
bool ProjectClip::hasParent(const QString &id) const
{
    AbstractProjectItem *par = parent();
Nicolas Carion's avatar
Nicolas Carion committed
169
    while (par != nullptr) {
170 171 172 173
        if (par->clipId() == id) {
            return true;
        }
        par = par->parent();
174 175 176 177
    }
    return false;
}

178
ProjectClip *ProjectClip::clip(const QString &id)
179 180 181 182
{
    if (id == m_id) {
        return this;
    }
Laurent Montel's avatar
Laurent Montel committed
183
    return nullptr;
184 185
}

186
ProjectFolder *ProjectClip::folder(const QString &id)
187
{
188
    Q_UNUSED(id)
Laurent Montel's avatar
Laurent Montel committed
189
    return nullptr;
190 191
}

192

193
ProjectSubClip *ProjectClip::getSubClip(int in, int out)
194
{
195 196
    for (int i = 0; i < childCount(); ++i) {
        ProjectSubClip *clip = static_cast<ProjectSubClip *>(child(i))->subClip(in, out);
197 198 199 200
        if (clip) {
            return clip;
        }
    }
Laurent Montel's avatar
Laurent Montel committed
201
    return nullptr;
202 203
}

204 205 206
QStringList ProjectClip::subClipIds() const
{
    QStringList subIds;
207 208
    for (int i = 0; i < childCount(); ++i) {
        AbstractProjectItem *clip = static_cast<AbstractProjectItem*>(child(i));
209 210 211 212 213 214 215
        if (clip) {
            subIds << clip->clipId();
        }
    }
    return subIds;
}

216
ProjectClip *ProjectClip::clipAt(int ix)
217
{
Nicolas Carion's avatar
Nicolas Carion committed
218
    if (ix == row()) {
219 220
        return this;
    }
Laurent Montel's avatar
Laurent Montel committed
221
    return nullptr;
222 223
}

224
/*bool ProjectClip::isValid() const
225
{
226 227
    return m_controller->isValid();
}*/
228

229 230
bool ProjectClip::hasUrl() const
{
231 232
    if ((m_type != Color) && (m_type != Unknown)) {
        return (!clipUrl().isEmpty());
233
    }
234 235 236
    return false;
}

237
const QString ProjectClip::url() const
238
{
239
    return clipUrl();
240 241 242
}


243
GenTime ProjectClip::duration() const
244
{
245
    return getPlaytime();
246 247
}

248 249 250
int ProjectClip::frameDuration() const
{
    GenTime d = duration();
251
    return d.frames(pCore->getCurrentFps());
252 253
}

254
void ProjectClip::reloadProducer(bool refreshOnly)
255 256 257
{
    QDomDocument doc;
    QDomElement xml = toXml(doc);
258
    if (refreshOnly) {
259
        // set a special flag to request thumbnail only
260
        xml.setAttribute(QStringLiteral("refreshOnly"), QStringLiteral("1"));
261
    }
262
    static_cast<ProjectItemModel*>(m_model)->bin()->reloadProducer(m_id, xml);
263 264 265 266
}

void ProjectClip::setCurrent(bool current, bool notify)
{
267 268 269 270
    Q_UNUSED(notify);
    if (current) {
        static_cast<ProjectItemModel*>(m_model)->bin()->openProducer(static_cast<ClipController*>(this));
        static_cast<ProjectItemModel*>(m_model)->bin()->requestShowEffectStack(getEffectStack());
271 272 273
    }
}

274
QDomElement ProjectClip::toXml(QDomDocument &document, bool includeMeta)
275
{
276 277 278 279
    getProducerXML(document, includeMeta);
    QDomElement prod = document.documentElement().firstChildElement(QStringLiteral("producer"));
    if (m_type != Unknown) {
        prod.setAttribute(QStringLiteral("type"), (int) m_type);
280
    }
281
    return prod;
282 283
}

Laurent Montel's avatar
Laurent Montel committed
284
void ProjectClip::setThumbnail(const QImage &img)
285
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
286 287
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
288
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
289
        QPainter p(&thumb);
290
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
291
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
292 293 294 295 296 297 298 299
        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
300
    m_thumbnail = QIcon(thumb);
301
    emit thumbUpdated(img);
302
    static_cast<ProjectItemModel*>(m_model)->bin()->emitItemUpdated(this);
303 304
}

305 306 307 308 309
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

310
bool ProjectClip::setProducer(std::shared_ptr<Mlt::Producer> producer, bool replaceProducer)
311
{
312 313
    Q_UNUSED(replaceProducer);

Nicolas Carion's avatar
Nicolas Carion committed
314
    updateProducer(std::move(producer));
315 316 317 318

    // Update info
    if (m_name.isEmpty()) {
        m_name = clipName();
319
    }
320 321 322 323 324 325 326
    m_date = date;
    m_description = ClipController::description();
    m_temporaryUrl.clear();
    if (m_type == Unknown) {
        m_type = clipType();
        if (m_type == Audio) {
            m_thumbnail = KoIconUtils::themedIcon(QStringLiteral("audio-x-generic"));
327
        }
328
    }
329
    m_duration = getStringDuration();
330
    m_clipStatus = StatusReady;
331
    if (!hasProxy()) {
332
        static_cast<ProjectItemModel*>(m_model)->bin()->emitRefreshPanel(m_id);
333
    }
334
    static_cast<ProjectItemModel*>(m_model)->bin()->emitItemUpdated(this);
335
    // Make sure we have a hash for this clip
336
    getFileHash();
337
    createAudioThumbs();
338
    return true;
339 340 341 342
}

void ProjectClip::createAudioThumbs()
{
343
    if (KdenliveSettings::audiothumbnails() && (m_type == AV || m_type == Audio || m_type == Playlist)) {
344
        static_cast<ProjectItemModel*>(m_model)->bin()->requestAudioThumbs(m_id, duration().ms());
345
        emit updateJobStatus(AbstractClipJob::THUMBJOB, JobWaiting, 0);
346
    }
347 348 349
}


350 351 352 353 354 355
Mlt::Producer *ProjectClip::thumbProducer()
{
    QMutexLocker locker(&m_producerMutex);
    if (m_thumbsProducer) {
        return m_thumbsProducer;
    }
356
    if (clipType() == Unknown) {
Laurent Montel's avatar
Laurent Montel committed
357
        return nullptr;
358
    }
359 360
    std::shared_ptr<Mlt::Producer> prod = originalProducer();
    if (!prod->is_valid()) {
Laurent Montel's avatar
Laurent Montel committed
361
        return nullptr;
362
    }
363
    Clip clip(*prod.get());
364
    if (KdenliveSettings::gpu_accel()) {
365
        m_thumbsProducer = clip.softClone(ClipController::getPassPropertiesList());
366 367
        Mlt::Filter scaler(*prod->profile(), "swscale");
        Mlt::Filter converter(*prod->profile(), "avcolor_space");
368 369
        m_thumbsProducer->attach(scaler);
        m_thumbsProducer->attach(converter);
370 371
    } else {
        m_thumbsProducer = clip.clone();
372 373 374 375
    }
    return m_thumbsProducer;
}

376
bool ProjectClip::isReady() const
377
{
378
    return m_clipStatus == StatusReady;
379 380
}

381
/*void ProjectClip::setZone(const QPoint &zone)
382 383
{
    m_zone = zone;
384
}*/
385 386 387

QPoint ProjectClip::zone() const
{
388 389
    int x = getProducerIntProperty(QStringLiteral("kdenlive:zone_in"));
    int y = getProducerIntProperty(QStringLiteral("kdenlive:zone_out"));
390
    return QPoint(x, y);
391 392 393 394 395
}


const QString ProjectClip::hash()
{
396
    QString clipHash = getProducerProperty(QStringLiteral("kdenlive:file_hash"));
397 398
    if (!clipHash.isEmpty()) {
        return clipHash;
399
    }
400
    return getFileHash();
401 402
}

403
const QString ProjectClip::getFileHash()
404
{
405 406 407
    QByteArray fileData;
    QByteArray fileHash;
    switch (m_type) {
408
    case SlideShow:
409
        fileData = clipUrl().toUtf8();
410 411 412
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    case Text:
413
        fileData = getProducerProperty(QStringLiteral("xmldata")).toUtf8();
414 415 416
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    case QText:
417
        fileData = getProducerProperty(QStringLiteral("text")).toUtf8();
418 419 420
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    case Color:
421
        fileData = getProducerProperty(QStringLiteral("resource")).toUtf8();
422 423 424
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        break;
    default:
425
        QFile file(clipUrl());
426 427 428 429 430
        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)
             */
431 432
            if (file.size() > 2000000) {
                fileData = file.read(1000000);
433
                if (file.seek(file.size() - 1000000)) {
434
                    fileData.append(file.readAll());
435 436
                }
            } else {
437
                fileData = file.readAll();
438
            }
439
            file.close();
Nicolas Carion's avatar
Nicolas Carion committed
440
            ClipController::setProducerProperty(QStringLiteral("kdenlive:file_size"), QString::number(file.size()));
441
            fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
442 443 444 445 446
        }
        break;
    }
    if (fileHash.isEmpty()) {
        return QString();
447 448
    }
    QString result = fileHash.toHex();
Nicolas Carion's avatar
Nicolas Carion committed
449
    ClipController::setProducerProperty(QStringLiteral("kdenlive:file_hash"), result);
450
    return result;
451
}
452 453 454

double ProjectClip::getOriginalFps() const
{
455
    return originalFps();
456
}
457 458 459

bool ProjectClip::hasProxy() const
{
460
    QString proxy = getProducerProperty(QStringLiteral("kdenlive:proxy"));
Nicolas Carion's avatar
Nicolas Carion committed
461
    return !proxy.isEmpty() || proxy == QLatin1String("-");
462 463
}

Laurent Montel's avatar
Laurent Montel committed
464
void ProjectClip::setProperties(const QMap<QString, QString> &properties, bool refreshPanel)
465 466
{
    QMapIterator<QString, QString> i(properties);
Laurent Montel's avatar
Laurent Montel committed
467
    QMap<QString, QString> passProperties;
468
    bool refreshAnalysis = false;
469
    bool reload = false;
470
    bool refreshOnly = true;
471 472
    // Some properties also need to be passed to track producers
    QStringList timelineProperties;
Laurent Montel's avatar
Laurent Montel committed
473 474
    if (properties.contains(QStringLiteral("templatetext"))) {
        m_description = properties.value(QStringLiteral("templatetext"));
475
        static_cast<ProjectItemModel*>(m_model)->bin()->emitItemUpdated(this);
476 477
        refreshPanel = true;
    }
478
    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");
479
    QStringList keys;
480
    keys << QStringLiteral("luma_duration") << QStringLiteral("luma_file") << QStringLiteral("fade") << QStringLiteral("ttl") << QStringLiteral("softness") << QStringLiteral("crop") << QStringLiteral("animation");
481 482
    while (i.hasNext()) {
        i.next();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
483
        setProducerProperty(i.key(), i.value());
484 485
        if (m_type == SlideShow && keys.contains(i.key())) {
            reload = true;
486
            refreshOnly = false;
487
        }
488 489 490
        if (i.key().startsWith(QLatin1String("kdenlive:clipanalysis"))) {
            refreshAnalysis = true;
        }
491 492 493
        if (timelineProperties.contains(i.key())) {
            passProperties.insert(i.key(), i.value());
        }
494
    }
495 496
    if (properties.contains(QStringLiteral("kdenlive:proxy"))) {
        QString value = properties.value(QStringLiteral("kdenlive:proxy"));
497
        // If value is "-", that means user manually disabled proxy on this clip
498
        if (value.isEmpty() || value == QLatin1String("-")) {
499
            // reset proxy
500 501
            if (static_cast<ProjectItemModel*>(m_model)->bin()->hasPendingJob(m_id, AbstractClipJob::PROXYJOB)) {
                static_cast<ProjectItemModel*>(m_model)->bin()->discardJobs(m_id, AbstractClipJob::PROXYJOB);
502
            } else {
503 504
                reloadProducer();
            }
505
        } else {
506
            // A proxy was requested, make sure to keep original url
507
            setProducerProperty(QStringLiteral("kdenlive:originalurl"), url());
508
            static_cast<ProjectItemModel*>(m_model)->bin()->startJob(m_id, AbstractClipJob::PROXYJOB);
509
        }
510
    } else if (properties.contains(QStringLiteral("resource")) || properties.contains(QStringLiteral("templatetext")) || properties.contains(QStringLiteral("autorotate"))) {
511
        // Clip resource changed, update thumbnail
512
        if (m_type != Color) {
513
            reloadProducer();
514 515
        } else {
            reload = true;
516
        }
517
    }
518
    if (properties.contains(QStringLiteral("xmldata")) || !passProperties.isEmpty()) {
519
        reload = true;
520
    }
521 522 523
    if (refreshAnalysis) {
        emit refreshAnalysisPanel();
    }
524
    if (properties.contains(QStringLiteral("length")) || properties.contains(QStringLiteral("kdenlive:duration"))) {
525 526
        m_duration = getStringDuration();
        static_cast<ProjectItemModel*>(m_model)->bin()->emitItemUpdated(this);
527 528
    }

529 530
    if (properties.contains(QStringLiteral("kdenlive:clipname"))) {
        m_name = properties.value(QStringLiteral("kdenlive:clipname"));
531
        refreshPanel = true;
532
        static_cast<ProjectItemModel*>(m_model)->bin()->emitItemUpdated(this);
533
    }
534 535 536 537
    if (refreshPanel) {
        // Some of the clip properties have changed through a command, update properties panel
        emit refreshPropertiesPanel();
    }
538
    if (reload) {
539
        // producer has changed, refresh monitor and thumbnail
540
        reloadProducer(refreshOnly);
541
        static_cast<ProjectItemModel*>(m_model)->bin()->refreshClip(m_id);
542
    }
543
    if (!passProperties.isEmpty()) {
544
        static_cast<ProjectItemModel*>(m_model)->bin()->updateTimelineProducers(m_id, passProperties);
545
    }
546 547
}

548
void ProjectClip::setJobStatus(int jobType, int status, int progress, const QString &statusMessage)
549
{
550
    m_jobType = (AbstractClipJob::JOBTYPE) jobType;
551
    if (progress > 0) {
552 553 554
        if (m_jobProgress == progress) {
            return;
        }
555
        m_jobProgress = progress;
556
    } else {
557
        m_jobProgress = status;
558 559 560
        if (m_jobType == AbstractClipJob::PROXYJOB && (status == JobAborted || status == JobCrashed)) {
            setProducerProperty(QStringLiteral("kdenlive:proxy"), QStringLiteral("-"));
        }
561 562
        if ((status == JobAborted || status == JobCrashed  || status == JobDone) && !statusMessage.isEmpty()) {
            m_jobMessage = statusMessage;
563
            static_cast<ProjectItemModel*>(m_model)->bin()->emitMessage(statusMessage, 100, OperationCompletedMessage);
564
        }
565
    }
566
    static_cast<ProjectItemModel*>(m_model)->bin()->emitItemUpdated(this);
567
}
568

569 570
ClipPropertiesController *ProjectClip::buildProperties(QWidget *parent)
{
571
    ClipPropertiesController *panel = new ClipPropertiesController(static_cast<ProjectItemModel*>(m_model)->bin()->projectTimecode(), static_cast<ClipController*>(this), parent);
Laurent Montel's avatar
Laurent Montel committed
572 573
    connect(this, &ProjectClip::refreshPropertiesPanel, panel, &ClipPropertiesController::slotReloadProperties);
    connect(this, &ProjectClip::refreshAnalysisPanel, panel, &ClipPropertiesController::slotFillAnalysisData);
574 575 576
    return panel;
}

577 578
void ProjectClip::updateParentInfo(const QString &folderid, const QString &foldername)
{
579
    Q_UNUSED(foldername);
Nicolas Carion's avatar
Nicolas Carion committed
580
    ClipController::setProducerProperty(QStringLiteral("kdenlive:folderid"), folderid);
581 582
}

Laurent Montel's avatar
Laurent Montel committed
583
bool ProjectClip::matches(const QString &condition)
584 585
{
    //TODO
586
    Q_UNUSED(condition)
587 588 589
    return true;
}

590
const QString ProjectClip::codec(bool audioCodec) const
591
{
592
    return codec(audioCodec);
593
}
594

595
bool ProjectClip::rename(const QString &name, int column)
596
{
Laurent Montel's avatar
Laurent Montel committed
597 598
    QMap<QString, QString> newProperites;
    QMap<QString, QString> oldProperites;
599 600
    bool edited = false;
    switch (column) {
601 602 603 604
    case 0:
        if (m_name == name) {
            return false;
        }
605
        // Rename clip
606 607
        oldProperites.insert(QStringLiteral("kdenlive:clipname"), m_name);
        newProperites.insert(QStringLiteral("kdenlive:clipname"), name);
608 609 610
        m_name = name;
        edited = true;
        break;
611 612 613 614
    case 2:
        if (m_description == name) {
            return false;
        }
615
        // Rename clip
616 617 618 619 620 621 622
        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);
        }
623 624 625 626 627
        m_description = name;
        edited = true;
        break;
    }
    if (edited) {
628
        static_cast<ProjectItemModel*>(m_model)->bin()->slotEditClipCommand(m_id, oldProperites, newProperites);
629 630
    }
    return edited;
631 632
}

Laurent Montel's avatar
Laurent Montel committed
633
void ProjectClip::addClipMarker(QList<CommentedTime> newMarkers, QUndoCommand *groupCommand)
634
{
Laurent Montel's avatar
Laurent Montel committed
635
    QList<CommentedTime> oldMarkers;
636
    for (int i = 0; i < newMarkers.count(); ++i) {
637
        CommentedTime oldMarker = markerAt(newMarkers.at(i).time());
638 639 640 641 642 643 644 645 646 647 648
        if (oldMarker == CommentedTime()) {
            oldMarker = newMarkers.at(i);
            oldMarker.setMarkerType(-1);
        }
        oldMarkers << oldMarker;
    }
    (void) new AddMarkerCommand(this, oldMarkers, newMarkers, groupCommand);
}

bool ProjectClip::deleteClipMarkers(QUndoCommand *command)
{
Laurent Montel's avatar
Laurent Montel committed
649
    QList<CommentedTime> markers = commentedSnapMarkers();
650 651 652
    if (markers.isEmpty()) {
        return false;
    }
Laurent Montel's avatar
Laurent Montel committed
653
    QList<CommentedTime> newMarkers;
654 655 656 657 658 659
    for (int i = 0; i < markers.size(); ++i) {
        CommentedTime marker = markers.at(i);
        marker.setMarkerType(-1);
        newMarkers << marker;
    }
    new AddMarkerCommand(this, markers, newMarkers, command);
660
    return true;
661 662
}

Laurent Montel's avatar
Laurent Montel committed
663
void ProjectClip::addMarkers(QList<CommentedTime> &markers)
664 665
{
    for (int i = 0; i < markers.count(); ++i) {
666
        if (markers.at(i).markerType() < 0) {
667
            deleteSnapMarker(markers.at(i).time());
668
        } else {
669
            addSnapMarker(markers.at(i));
670
        }
671 672
    }
    // refresh markers in clip monitor
673
    static_cast<ProjectItemModel*>(m_model)->bin()->refreshClipMarkers(m_id);
674 675 676
    // refresh markers in timeline clips
    emit refreshClipDisplay();
}
677

678

Nicolas Carion's avatar
Nicolas Carion committed
679
/*QVariant ProjectClip::getData(DataType type) const
680 681
{
    switch (type) {
682
    case AbstractProjectItem::IconOverlay:
683
        return hasEffects() ? QVariant("kdenlive-track_has_effect") : QVariant();
684 685 686
        break;
    default:
        break;
687
    }
Nicolas Carion's avatar
Nicolas Carion committed
688 689
    return AbstractProjectItem::getData(type);
    }*/
690

Laurent Montel's avatar
Laurent Montel committed
691
void ProjectClip::slotQueryIntraThumbs(const QList<int> &frames)
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
{
    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
708
    if (prod == nullptr || !prod->is_valid()) {
709 710
        return;
    }
711
    int fullWidth = 150 * prod->profile()->dar() + 0.5;
712 713 714 715 716 717
    int max = prod->get_length();
    int pos;
    while (!m_intraThumbs.isEmpty()) {
        m_intraThumbMutex.lock();
        pos = m_intraThumbs.takeFirst();
        m_intraThumbMutex.unlock();
718 719 720
        if (pos >= max) {
            pos = max - 1;
        }