projectclip.cpp 39.7 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 "mltcontroller/clippropertiescontroller.h"
35 36 37

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


Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
46
ProjectClip::ProjectClip(const QString &id, QIcon thumb, ClipController *controller, ProjectFolder* parent) :
47
    AbstractProjectItem(AbstractProjectItem::ClipItem, id, parent)
48
    , m_abortAudioThumb(false)
Vincent Pinon's avatar
Vincent Pinon committed
49
    , m_controller(controller)
50
    , m_thumbsProducer(NULL)
51
{
52
    m_clipStatus = StatusReady;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
53
    m_thumbnail = thumb;
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 60
    // Make sure we have a hash for this clip
    hash();
61
    setParent(parent);
62
    connect(this, &ProjectClip::updateJobStatus, this, &ProjectClip::setJobStatus);
63
    bin()->loadSubClips(id, m_controller->getPropertiesFromPrefix(QStringLiteral("kdenlive:clipzone.")));
64
    createAudioThumbs();
65 66
}

67
ProjectClip::ProjectClip(const QDomElement& description, QIcon thumb, ProjectFolder* parent) :
68
    AbstractProjectItem(AbstractProjectItem::ClipItem, description, parent)
69
    , m_abortAudioThumb(false)
Vincent Pinon's avatar
Vincent Pinon committed
70
    , m_controller(NULL)
71
    , m_type(Unknown)
72
    , m_thumbsProducer(NULL)
73 74
{
    Q_ASSERT(description.hasAttribute("id"));
75
    m_clipStatus = StatusWaiting;
76
    m_thumbnail = thumb;
77 78
    if (description.hasAttribute(QStringLiteral("type"))) {
        m_type = (ClipType) description.attribute(QStringLiteral("type")).toInt();
79
    }
80 81
    m_temporaryUrl = QUrl::fromLocalFile(getXmlProperty(description, QStringLiteral("resource")));
    QString clipName = getXmlProperty(description, QStringLiteral("kdenlive:clipname"));
82 83 84
    if (!clipName.isEmpty()) {
        m_name = clipName;
    }
85 86
    else if (m_temporaryUrl.isValid()) {
        m_name = m_temporaryUrl.fileName();
87
    }
88
    else m_name = i18n("Untitled");
89
    connect(this, &ProjectClip::updateJobStatus, this, &ProjectClip::setJobStatus);
90
    setParent(parent);
91 92 93 94 95
}


ProjectClip::~ProjectClip()
{
96
    // controller is deleted in bincontroller
97
    abortAudioThumbs();
98 99 100
    bin()->slotAbortAudioThumb(m_id);
    QMutexLocker audioLock(&m_audioMutex);
    m_thumbMutex.lock();
101
    m_requestedThumbs.clear();
102
    m_thumbMutex.unlock();
103
    m_thumbThread.waitForFinished();
104
    delete m_thumbsProducer;
105
    audioFrameCache.clear();
106 107
}

108 109 110
void ProjectClip::abortAudioThumbs()
{
    m_abortAudioThumb = true;
111
    emit doAbortAudioThumbs();
112 113
}

114 115 116 117 118
QString ProjectClip::getToolTip() const
{
    return url().toLocalFile();
}

119
QString ProjectClip::getXmlProperty(const QDomElement &producer, const QString &propertyName, const QString &defaultValue)
120
{
121
    QString value = defaultValue;
122
    QDomNodeList props = producer.elementsByTagName(QStringLiteral("property"));
123
    for (int i = 0; i < props.count(); ++i) {
124
        if (props.at(i).toElement().attribute(QStringLiteral("name")) == propertyName) {
125 126 127 128 129 130 131
            value = props.at(i).firstChild().nodeValue();
            break;
        }
    }
    return value;
}

132
void ProjectClip::updateAudioThumbnail(QVariantList audioLevels)
133
{
134
    audioFrameCache = audioLevels;
135
    m_controller->audioThumbCreated = true;
136
    bin()->emitRefreshAudioThumbs(m_id);
137 138 139 140 141 142 143 144 145 146 147
    emit gotAudioData();
}

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

bool ProjectClip::audioThumbCreated() const
{
148
    return (m_controller && m_controller->audioThumbCreated);
149 150
}

151 152
ClipType ProjectClip::clipType() const
{
153
    return m_type;
154 155
}

156 157 158 159 160 161 162 163 164 165 166 167
bool ProjectClip::hasParent(const QString &id) const
{
    AbstractProjectItem *par = parent();
    while (par) {
	if (par->clipId() == id) {
	    return true;
	}
	par = par->parent();
    }
    return false;
}

168 169 170 171 172 173 174 175
ProjectClip* ProjectClip::clip(const QString &id)
{
    if (id == m_id) {
        return this;
    }
    return NULL;
}

176 177
ProjectFolder* ProjectClip::folder(const QString &id)
{
178
    Q_UNUSED(id)
179 180 181
    return NULL;
}

182 183 184 185 186
void ProjectClip::disableEffects(bool disable)
{
    if (m_controller) m_controller->disableEffects(disable);
}

187 188 189
ProjectSubClip* ProjectClip::getSubClip(int in, int out)
{
    for (int i = 0; i < count(); ++i) {
Vincent Pinon's avatar
Vincent Pinon committed
190
        ProjectSubClip *clip = static_cast<ProjectSubClip *>(at(i))->subClip(in, out);
191 192 193 194 195 196 197
        if (clip) {
            return clip;
        }
    }
    return NULL;
}

198 199 200 201 202 203 204 205
ProjectClip* ProjectClip::clipAt(int ix)
{
    if (ix == index()) {
        return this;
    }
    return NULL;
}

206
/*bool ProjectClip::isValid() const
207
{
208 209
    return m_controller->isValid();
}*/
210 211 212

QUrl ProjectClip::url() const
{
213
    if (m_controller) return m_controller->clipUrl();
214
    return m_temporaryUrl;
215 216 217 218
}

bool ProjectClip::hasLimitedDuration() const
{
219 220 221 222
    if (m_controller) {
        return m_controller->hasLimitedDuration();
    }
    return true;
223 224
}

225
GenTime ProjectClip::duration() const
226
{
227 228
    if (m_controller) {
	return m_controller->getPlaytime();
229
    }
230
    return GenTime();
231 232
}

233
void ProjectClip::reloadProducer(bool thumbnailOnly)
234 235 236
{
    QDomDocument doc;
    QDomElement xml = toXml(doc);
237 238
    if (thumbnailOnly) {
        // set a special flag to request thumbnail only
239
        xml.setAttribute(QStringLiteral("thumbnailOnly"), QStringLiteral("1"));
240
    }
241
    bin()->reloadProducer(m_id, xml);
242 243 244 245
}

void ProjectClip::setCurrent(bool current, bool notify)
{
246
    Q_UNUSED(notify)
247
    if (current && m_controller) {
Jean-Baptiste Mardelle's avatar
cleanup  
Jean-Baptiste Mardelle committed
248
        bin()->openProducer(m_controller);
249
        bin()->editMasterEffect(m_controller);
250 251 252
    }
}

253
QDomElement ProjectClip::toXml(QDomDocument& document, bool includeMeta)
254
{
255
    if (m_controller) {
256
        m_controller->getProducerXML(document, includeMeta);
257
        return document.documentElement().firstChildElement(QStringLiteral("producer"));
258 259 260 261 262 263 264 265 266 267
    } 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;
268 269 270 271 272
    }
}

void ProjectClip::setThumbnail(QImage img)
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
273 274
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
275
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
276
        QPainter p(&thumb);
277
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
278
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
279 280 281 282 283 284 285 286
        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
287
    m_thumbnail = QIcon(thumb);
288
    emit thumbUpdated(img);
289 290 291
    bin()->emitItemUpdated(this);
}

292 293 294 295 296
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

297
bool ProjectClip::setProducer(ClipController *controller, bool replaceProducer)
298
{
299
    if (!replaceProducer && m_controller) {
Vincent Pinon's avatar
Vincent Pinon committed
300
        qDebug()<<"// RECEIVED PRODUCER BUT WE ALREADY HAVE ONE\n----------";
301
        return false;
302
    }
303
    bool isNewProducer = true;
304 305
    if (m_controller) {
        // Replace clip for this controller
306 307
        resetProducerProperty("kdenlive:file_hash");
        isNewProducer = false;
308
    }
309
    else if (controller) {
310 311 312
        // We did not yet have the controller, update info
        m_controller = controller;
        if (m_name.isEmpty()) m_name = m_controller->clipName();
313
        m_date = m_controller->date;
314
        m_description = m_controller->description();
315
        m_temporaryUrl.clear();
316
        if (m_type == Unknown) m_type = m_controller->clipType();
317
    }
318
    m_duration = m_controller->getStringDuration();
319
    m_clipStatus = StatusReady;
320
    if (!hasProxy()) bin()->emitRefreshPanel(m_id);
321
    bin()->emitItemUpdated(this);
322 323
    // Make sure we have a hash for this clip
    hash();
324
    createAudioThumbs();
325
    return isNewProducer;
326 327 328 329
}

void ProjectClip::createAudioThumbs()
{
330
    if (KdenliveSettings::audiothumbnails() && (m_type == AV || m_type == Audio)) {
331
        bin()->requestAudioThumbs(m_id);
332
    }
333 334
}

335
Mlt::Producer *ProjectClip::originalProducer()
336
{
337 338 339 340
    if (!m_controller) {
        return NULL;
    }
    return &m_controller->originalProducer();
341 342
}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
Mlt::Producer *ProjectClip::thumbProducer()
{
    QMutexLocker locker(&m_producerMutex);
    if (m_thumbsProducer) {
        return m_thumbsProducer;
    }
    if (!m_controller) {
        return NULL;
    }
    Mlt::Producer prod = m_controller->originalProducer();
    Clip clip(prod);
    m_thumbsProducer = clip.softClone(ClipController::getPassPropertiesList());
    // Check if we are using GPU accel, then we need to use alternate producer
    if (KdenliveSettings::gpu_accel()) {
        Mlt::Filter scaler(*prod.profile(), "swscale");
        Mlt::Filter converter(*prod.profile(), "avcolor_space");
        m_thumbsProducer->attach(scaler);
        m_thumbsProducer->attach(converter);
    }
    return m_thumbsProducer;
}

365 366 367 368 369
ClipController *ProjectClip::controller()
{
    return m_controller;
}

370
bool ProjectClip::isReady() const
371
{
372
    return m_controller != NULL && m_clipStatus == StatusReady;
373 374
}

375
/*void ProjectClip::setZone(const QPoint &zone)
376 377
{
    m_zone = zone;
378
}*/
379 380 381

QPoint ProjectClip::zone() const
{
382 383
    int x = getProducerIntProperty(QStringLiteral("kdenlive:zone_in"));
    int y = getProducerIntProperty(QStringLiteral("kdenlive:zone_out"));
384
    return QPoint(x, y);
385 386
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
387 388 389 390 391 392 393 394
void ProjectClip::resetProducerProperty(const QString &name)
{
    if (m_controller) {
        m_controller->resetProperty(name);
    }
}

void ProjectClip::setProducerProperty(const QString &name, int data)
395
{
396 397
    if (m_controller) {
	m_controller->setProperty(name, data);
398 399 400
    }
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
401
void ProjectClip::setProducerProperty(const QString &name, double data)
402
{
403 404
    if (m_controller) {
        m_controller->setProperty(name, data);
405 406 407
    }
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
408
void ProjectClip::setProducerProperty(const QString &name, const QString &data)
409
{
410 411
    if (m_controller) {
        m_controller->setProperty(name, data);
412 413 414
    }
}

415 416 417 418 419 420 421 422 423 424 425 426 427 428
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;
}

429 430 431 432 433 434 435 436
QColor ProjectClip::getProducerColorProperty(const QString &key) const
{
    if (m_controller) {
        return m_controller->color_property(key);
    }
    return QColor();
}

437 438 439 440 441 442 443 444
int ProjectClip::getProducerIntProperty(const QString &key) const
{
    int value = 0;
    if (m_controller) {
        value = m_controller->int_property(key);
    }
    return value;
}
445

446
QString ProjectClip::getProducerProperty(const QString &key) const
447 448
{
    QString value;
449 450
    if (m_controller) {
	value = m_controller->property(key);
451 452 453 454 455 456
    }
    return value;
}

const QString ProjectClip::hash()
{
457
    if (m_controller) {
458
        QString clipHash = m_controller->property(QStringLiteral("kdenlive:file_hash"));
459 460
        if (!clipHash.isEmpty()) {
            return clipHash;
461
        }
462
    }
463
    return getFileHash();
464 465
}

466
const QString ProjectClip::getFileHash() const
467
{
468 469 470 471 472 473 474 475
    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:
476
          fileData = m_controller ? m_controller->property(QStringLiteral("xmldata")).toUtf8() : name().toUtf8();
477 478
          fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
          break;
Vincent Pinon's avatar
Vincent Pinon committed
479 480 481 482
      case QText:
          fileData = m_controller ? m_controller->property(QStringLiteral("text")).toUtf8() : name().toUtf8();
          fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
          break;
483
      case Color:
484
          fileData = m_controller ? m_controller->property(QStringLiteral("resource")).toUtf8() : name().toUtf8();
485
          fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
486 487 488 489 490
          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
              /*
491 492 493
               * 1 MB = 1 second per 450 files (or faster)
               * 10 MB = 9 seconds per 450 files (or faster)
               */
494 495 496 497 498 499 500
            if (file.size() > 2000000) {
                fileData = file.read(1000000);
                if (file.seek(file.size() - 1000000))
                    fileData.append(file.readAll());
            } else
                fileData = file.readAll();
            file.close();
501
            if (m_controller) m_controller->setProperty(QStringLiteral("kdenlive:file_size"), QString::number(file.size()));
502 503 504 505 506 507 508
            fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
          }
          break;
    }
    if (fileHash.isEmpty()) return QString();
    QString result = fileHash.toHex();
    if (m_controller) {
509
	m_controller->setProperty(QStringLiteral("kdenlive:file_hash"), result);
510
    }
511
    return result;
512
}
513 514 515 516 517 518

double ProjectClip::getOriginalFps() const
{
    if (!m_controller) return 0;
    return m_controller->originalFps();
}
519 520 521

bool ProjectClip::hasProxy() const
{
522 523
    QString proxy = getProducerProperty(QStringLiteral("kdenlive:proxy"));
    if (proxy.isEmpty() || proxy == QLatin1String("-")) return false;
524 525 526
    return true;
}

527
void ProjectClip::setProperties(QMap <QString, QString> properties, bool refreshPanel)
528 529
{
    QMapIterator<QString, QString> i(properties);
530
    QMap <QString, QString> passProperties;
531
    bool refreshAnalysis = false;
532 533 534
    bool reload = false;
    // Some properties also need to be passed to track producers
    QStringList timelineProperties;
535
    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");
536
    QStringList keys;
537
    keys << QStringLiteral("luma_duration") << QStringLiteral("luma_file") << QStringLiteral("fade") << QStringLiteral("ttl") << QStringLiteral("softness") << QStringLiteral("crop") << QStringLiteral("animation");
538 539
    while (i.hasNext()) {
        i.next();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
540
        setProducerProperty(i.key(), i.value());
541 542 543
        if (m_type == SlideShow && keys.contains(i.key())) {
            reload = true;
        }
544
        if (i.key().startsWith(QLatin1String("kdenlive:clipanalysis"))) refreshAnalysis = true;
545 546 547
        if (timelineProperties.contains(i.key())) {
            passProperties.insert(i.key(), i.value());
        }
548
    }
549 550
    if (properties.contains(QStringLiteral("kdenlive:proxy"))) {
        QString value = properties.value(QStringLiteral("kdenlive:proxy"));
551
        // If value is "-", that means user manually disabled proxy on this clip
552
        if (value.isEmpty() || value == QLatin1String("-")) {
553 554 555 556
            // reset proxy
            if (bin()->hasPendingJob(m_id, AbstractClipJob::PROXYJOB)) {
                bin()->discardJobs(m_id, AbstractClipJob::PROXYJOB);
            }
557 558 559
            else {
                reloadProducer();
            }
560 561
        }
        else {
562
            // A proxy was requested, make sure to keep original url
563
            setProducerProperty(QStringLiteral("kdenlive:originalurl"), url().toLocalFile());
564
            bin()->startJob(m_id, AbstractClipJob::PROXYJOB);
565 566
        }
    }
567
    else if (properties.contains(QStringLiteral("resource"))) {
568
        // Clip resource changed, update thumbnail
569
        if (m_type != Color) {
570
            reloadProducer();
571
        }
572
        else reload = true;
573
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
574

575
    if (properties.contains(QStringLiteral("xmldata")) || !passProperties.isEmpty()) {
576
        reload = true;
577
    }
578
    if (refreshAnalysis) emit refreshAnalysisPanel();
579
    if (properties.contains(QStringLiteral("length"))) {
580 581 582 583
        m_duration = m_controller->getStringDuration();
        bin()->emitItemUpdated(this);
    }

584 585
    if (properties.contains(QStringLiteral("kdenlive:clipname"))) {
        m_name = properties.value(QStringLiteral("kdenlive:clipname"));
586 587
        bin()->emitItemUpdated(this);
    }
588 589 590 591
    if (refreshPanel) {
        // Some of the clip properties have changed through a command, update properties panel
        emit refreshPropertiesPanel();
    }
592
    if (reload) {
593
        // producer has changed, refresh monitor and thumbnail
594
        if (reload) reloadProducer(true);
595
        bin()->refreshClip(m_id);
596
    }
597 598 599
    if (!passProperties.isEmpty()) {
        bin()->updateTimelineProducers(m_id, passProperties);
    }
600 601
}

602
void ProjectClip::setJobStatus(int jobType, int status, int progress, const QString &statusMessage)
603
{
604
    m_jobType = (AbstractClipJob::JOBTYPE) jobType;
605
    if (progress > 0) {
606
        if (m_jobProgress == progress) return;
607 608 609
	m_jobProgress = progress;
    }
    else {
610 611
	m_jobProgress = (ClipJobStatus) status;
	if ((status == JobAborted || status == JobCrashed  || status == JobDone) && !statusMessage.isEmpty()) {
612
	    m_jobMessage = statusMessage;
613
            bin()->emitMessage(statusMessage, OperationCompletedMessage);
614 615 616 617
	}
    }
    bin()->emitItemUpdated(this);
}
618

619 620 621

ClipPropertiesController *ProjectClip::buildProperties(QWidget *parent)
{
622
    ClipPropertiesController *panel = new ClipPropertiesController(bin()->projectTimecode(), m_controller, parent);
623
    connect(this, SIGNAL(refreshPropertiesPanel()), panel, SLOT(slotReloadProperties()));
624
    connect(this, SIGNAL(refreshAnalysisPanel()), panel, SLOT(slotFillAnalysisData()));
625 626 627
    return panel;
}

628 629
void ProjectClip::updateParentInfo(const QString &folderid, const QString &foldername)
{
630
    Q_UNUSED(foldername)
631
    m_controller->setProperty(QStringLiteral("kdenlive:folderid"), folderid);
632 633
}

634 635 636
bool ProjectClip::matches(QString condition)
{
    //TODO
637
    Q_UNUSED(condition)
638 639 640
    return true;
}

641
const QString ProjectClip::codec(bool audioCodec) const
642
{
643 644
    if (!m_controller) return QString();
    return m_controller->codec(audioCodec);
645
}
646

647
bool ProjectClip::rename(const QString &name, int column)
648 649 650
{
    QMap <QString, QString> newProperites;
    QMap <QString, QString> oldProperites;
651 652 653 654 655
    bool edited = false;
    switch (column) {
      case 0:
        if (m_name == name) return false;
        // Rename clip
656 657
        oldProperites.insert(QStringLiteral("kdenlive:clipname"), m_name);
        newProperites.insert(QStringLiteral("kdenlive:clipname"), name);
658 659 660 661 662 663
        m_name = name;
        edited = true;
        break;
      case 2:
        if (m_description == name) return false;
        // Rename clip
664 665
        oldProperites.insert(QStringLiteral("kdenlive:description"), m_description);
        newProperites.insert(QStringLiteral("kdenlive:description"), name);
666 667 668 669 670 671 672 673
        m_description = name;
        edited = true;
        break;
    }
    if (edited) {
        bin()->slotEditClipCommand(m_id, oldProperites, newProperites);
    }
    return edited;
674 675
}

676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
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);
704
    return true;
705 706 707 708 709 710 711 712 713 714 715 716 717 718
}

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();
}
719

Vincent Pinon's avatar
Vincent Pinon committed
720
void ProjectClip::addEffect(const ProfileInfo &pInfo, QDomElement &effect)
721
{
722
    m_controller->addEffect(pInfo, effect);
723
    bin()->updateMasterEffect(m_controller);
724
    bin()->emitItemUpdated(this);
725 726
}

727
void ProjectClip::removeEffect(int ix)
728
{
729
    m_controller->removeEffect(ix);
730
    bin()->updateMasterEffect(m_controller);
731 732 733 734 735 736 737
    bin()->emitItemUpdated(this);
}

QVariant ProjectClip::data(DataType type) const
{
    switch (type) {
      case AbstractProjectItem::IconOverlay:
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
738
            return m_controller != NULL ? (m_controller->hasEffects() ? QVariant("kdenlive-track_has_effect") : QVariant()) : QVariant();
739 740 741 742 743
            break;
        default:
	    break;
    }
    return AbstractProjectItem::data(type);
744
}
745 746

void ProjectClip::slotExtractImage(QList <int> frames)
747 748 749 750 751 752 753 754 755 756 757 758 759 760
{
    QMutexLocker lock(&m_thumbMutex);
    for (int i = 0; i < frames.count(); i++) {
        if (!m_requestedThumbs.contains(frames.at(i))) {
            m_requestedThumbs << frames.at(i);
        }
    }
    qSort(m_requestedThumbs);
    if (!m_thumbThread.isRunning()) {
        m_thumbThread = QtConcurrent::run(this, &ProjectClip::doExtractImage);
    }
}

void ProjectClip::doExtractImage()
761
{
762
    Mlt::Producer *prod = thumbProducer();
763
    if (prod == NULL || !prod->is_valid()) return;
764 765
    int fullWidth = (int)((double) 150 * prod->profile()->dar() + 0.5);
    QDir thumbFolder(bin()->projectFolder().path() + "/thumbs/");
766 767 768 769 770 771
    int max = prod->get_length();
    int pos;
    while (!m_requestedThumbs.isEmpty()) {
        m_thumbMutex.lock();
        pos = m_requestedThumbs.takeFirst();
        m_thumbMutex.unlock();
772 773
        if (thumbFolder.exists(hash() + '#' + QString::number(pos) + ".png")) {
            emit thumbReady(pos, QImage(thumbFolder.absoluteFilePath(hash() + '#' + QString::number(pos) + ".png")));
774 775 776 777 778 779
            continue;
        }
	if (pos >= max) pos = max - 1;
	prod->seek(pos);
	Mlt::Frame *frame = prod->get_frame();
	if (frame && frame->is_valid()) {
780
            QImage img = KThumb::getFrame(frame, fullWidth, 150);
781
            emit thumbReady(pos, img);
782 783 784
        }
        delete frame;
    }
785
}
786

787 788
void ProjectClip::slotExtractSubImage(QList <int> frames)
{
789
    Mlt::Producer *prod = thumbProducer();
790
    if (prod == NULL || !prod->is_valid()) return;
791 792 793 794
    int fullWidth = (int)((double) 150 * prod->profile()->dar() + 0.5);
    QDir thumbFolder(bin()->projectFolder().path() + "/thumbs/");
    for (int i = 0; i < frames.count(); i++) {
        int pos = frames.at(i);
795 796
        QString path = thumbFolder.absoluteFilePath(hash() + "#" + QString::number(pos) + ".png");
        QImage img(path);
797 798
        if (!img.isNull()) {
            for (int i = 0; i < count(); ++i) {
Vincent Pinon's avatar
Vincent Pinon committed
799
                ProjectSubClip *clip = static_cast<ProjectSubClip *>(at(i));
800 801 802 803 804 805 806 807
                if (clip && clip->zone().x() == pos) {
                    clip->setThumbnail(img);
                }
            }
            continue;
        }
        int max = prod->get_out();
        if (pos >= max) pos = max - 1;
808
        if (pos < 0) pos = 0;
809 810 811
        prod->seek(pos);
        Mlt::Frame *frame = prod->get_frame();
        if (frame && frame->is_valid()) {
812
            QImage img = KThumb::getFrame(frame, fullWidth, 150);
813
            if (!img.isNull()) {
814
                img.save(path);
815
                for (int i = 0; i < count(); ++i) {
Vincent Pinon's avatar
Vincent Pinon committed
816
                    ProjectSubClip *clip = static_cast<ProjectSubClip *>(at(i));
817 818 819 820 821 822 823 824 825 826
                    if (clip && clip->zone().x() == pos) {
                        clip->setThumbnail(img);
                    }
                }
            }
        }
        delete frame;
    }
}

827 828 829 830 831 832
int ProjectClip::audioChannels() const
{
    if (!m_controller || !m_controller->audioInfo()) return 0;
    return m_controller->audioInfo()->channels();
}

833 834
void ProjectClip::slotCreateAudioThumbs()
{
835
    QMutexLocker lock(&m_audioMutex);
836
    Mlt::Producer *prod = originalProducer();
837
    if (!prod || !prod->is_valid()) return;
838
    AudioStreamInfo *audioInfo = m_controller->audioInfo();
839
    int audioStream = audioInfo->ffmpeg_audio_index();
840 841 842
    if (audioInfo == NULL) return;
    QString clipHash = hash();
    if (clipHash.isEmpty()) return;