projectclip.cpp 15.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
#include "project/projectcommands.h"
29
#include "mltcontroller/clipcontroller.h"
30
#include "mltcontroller/clippropertiescontroller.h"
31 32 33 34 35 36 37 38 39 40

#include <QDomElement>
#include <QFile>
#include <QDebug>
#include <QCryptographicHash>

#include <KLocalizedString>



41
ProjectClip::ProjectClip(const QString &id, ClipController *controller, ProjectFolder* parent) :
42 43 44 45
    AbstractProjectItem(AbstractProjectItem::ClipItem, id, parent)
    , m_controller(controller)
    , audioFrameCache()
    , m_audioThumbCreated(false)
46
{
47
    m_clipStatus = StatusReady;
48 49 50
    QPixmap pix(64, 36);
    pix.fill(Qt::lightGray);
    m_thumbnail = QIcon(pix);
51 52
    m_name = m_controller->clipName();
    m_duration = m_controller->getStringDuration();
53
    getFileHash();
54
    setParent(parent);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
55
    bin()->loadSubClips(id, m_controller->getSubClips());
56 57 58
}

ProjectClip::ProjectClip(const QDomElement& description, ProjectFolder* parent) :
59
    AbstractProjectItem(AbstractProjectItem::ClipItem, description, parent)
60
    , m_controller(NULL)
61 62
    , audioFrameCache()
    , m_audioThumbCreated(false)
63 64
{
    Q_ASSERT(description.hasAttribute("id"));
65
    m_clipStatus = StatusWaiting;
66 67 68
    QPixmap pix(64, 36);
    pix.fill(Qt::lightGray);
    m_thumbnail = QIcon(pix);
69
    QString resource = getXmlProperty(description, "resource");
70
    QString clipName = getXmlProperty(description, "kdenlive:clipname");
71 72 73 74
    if (!clipName.isEmpty()) {
        m_name = clipName;
    }
    else if (!resource.isEmpty()) {
75 76
        m_name = QUrl::fromLocalFile(resource).fileName();
    }
77
    else m_name = i18n("Untitled");
78
    setParent(parent);
79 80 81 82 83
}


ProjectClip::~ProjectClip()
{
84
    // controller is deleted in bincontroller
85 86
}

87 88 89 90 91
QString ProjectClip::getToolTip() const
{
    return url().toLocalFile();
}

92 93 94 95 96 97 98 99 100 101 102 103 104
QString ProjectClip::getXmlProperty(const QDomElement &producer, const QString &propertyName)
{
    QString value;
    QDomNodeList props = producer.elementsByTagName("property");
    for (int i = 0; i < props.count(); ++i) {
        if (props.at(i).toElement().attribute("name") == propertyName) {
            value = props.at(i).firstChild().nodeValue();
            break;
        }
    }
    return value;
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
void ProjectClip::updateAudioThumbnail(const audioByteArray& data)
{
    ////qDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
    audioFrameCache = data;
    m_audioThumbCreated = true;
    emit gotAudioData();
}

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

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

124 125
ClipType ProjectClip::clipType() const
{
126 127
    if (m_controller == NULL) return Unknown;
    return m_controller->clipType();
128 129 130 131 132 133 134 135 136 137
}

ProjectClip* ProjectClip::clip(const QString &id)
{
    if (id == m_id) {
        return this;
    }
    return NULL;
}

138 139 140 141 142
ProjectFolder* ProjectClip::folder(const QString &id)
{
    return NULL;
}

143 144 145 146 147 148 149 150 151 152 153 154
ProjectSubClip* ProjectClip::getSubClip(int in, int out)
{
    ProjectSubClip *clip;
    for (int i = 0; i < count(); ++i) {
        clip = static_cast<ProjectSubClip *>(at(i))->subClip(in, out);
        if (clip) {
            return clip;
        }
    }
    return NULL;
}

155 156 157 158 159 160 161 162
ProjectClip* ProjectClip::clipAt(int ix)
{
    if (ix == index()) {
        return this;
    }
    return NULL;
}

163
/*bool ProjectClip::isValid() const
164
{
165 166
    return m_controller->isValid();
}*/
167 168 169

QUrl ProjectClip::url() const
{
170
    return m_controller->clipUrl();
171 172 173 174
}

bool ProjectClip::hasLimitedDuration() const
{
175 176 177 178
    if (m_controller) {
        return m_controller->hasLimitedDuration();
    }
    return true;
179 180
}

181
GenTime ProjectClip::duration() const
182
{
183 184
    if (m_controller) {
	return m_controller->getPlaytime();
185
    }
186
    return GenTime();
187 188 189 190 191 192 193 194 195 196 197
}

QString ProjectClip::serializeClip()
{
    /*Mlt::Consumer *consumer = bin()->project()->xmlConsumer();
    consumer->connect(*m_baseProducer);
    consumer->run();
    return QString::fromUtf8(consumer->get("kdenlive_clip"));*/
    return QString();
}

198
void ProjectClip::reloadProducer(bool thumbnailOnly)
199 200 201
{
    QDomDocument doc;
    QDomElement xml = toXml(doc);
202 203 204 205
    if (thumbnailOnly) {
        // set a special flag to request thumbnail only
        xml.setAttribute("thumbnailOnly", "1");
    }
206
    bin()->reloadProducer(m_id, xml);
207 208 209 210 211
}

void ProjectClip::setCurrent(bool current, bool notify)
{
    AbstractProjectItem::setCurrent(current, notify);
212
    if (current && m_controller) {
Jean-Baptiste Mardelle's avatar
cleanup  
Jean-Baptiste Mardelle committed
213
        bin()->openProducer(m_controller);
214
        bin()->editMasterEffect(m_controller);
215 216 217 218 219
    }
}

QDomElement ProjectClip::toXml(QDomDocument& document)
{
220 221 222
    if (m_controller) {
        m_controller->getProducerXML(document);
        return document.documentElement().firstChildElement("producer");
223
    }
224
    return QDomElement();
225 226 227 228
}

void ProjectClip::setThumbnail(QImage img)
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
229 230
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
231
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
232
        QPainter p(&thumb);
233
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
234
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
235 236 237 238 239 240 241 242
        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
243
    m_thumbnail = QIcon(thumb);
244 245 246
    bin()->emitItemUpdated(this);
}

247
void ProjectClip::setProducer(ClipController *controller, bool replaceProducer)
248
{
249
    if (!replaceProducer && m_controller) {
250 251 252
        qDebug()<<"// RECIEVED PRODUCER BUT WE ALREADY HAVE ONE\n----------";
        return;
    }
253 254
    if (m_controller) {
        // Replace clip for this controller
255
        //m_controller->updateProducer(m_id, &(controller->originalProducer()));
256
        delete controller;
257
    }
258 259 260 261 262 263
    else {
        // We did not yet have the controller, update info
        m_controller = controller;
        if (m_name.isEmpty()) m_name = m_controller->clipName();
        m_duration = m_controller->getStringDuration();
    }
264
    m_clipStatus = StatusReady;
265 266 267 268 269 270
    bin()->emitItemUpdated(this);
    getFileHash();
}

Mlt::Producer *ProjectClip::producer()
{
271 272
    if (!m_controller) return NULL;
    return m_controller->masterProducer();
273 274
}

275 276 277 278 279
ClipController *ProjectClip::controller()
{
    return m_controller;
}

280
bool ProjectClip::isReady() const
281
{
282
    return m_controller!= NULL;
283 284
}

285
/*void ProjectClip::setZone(const QPoint &zone)
286 287
{
    m_zone = zone;
288
}*/
289 290 291

QPoint ProjectClip::zone() const
{
292 293 294
    int x = getProducerIntProperty("kdenlive:zone_in");
    int y = getProducerIntProperty("kdenlive:zone_out");
    return QPoint(x, y);
295 296 297 298 299 300 301 302 303 304 305 306 307 308
}

void ProjectClip::addMarker(int position)
{
    m_markers << position;
    //bin()->markersUpdated(m_id, m_markers);
}

void ProjectClip::removeMarker(int position)
{
    m_markers.removeAll(position);
    //bin()->markersUpdated(m_id, m_markers);
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
309 310 311 312 313 314 315 316
void ProjectClip::resetProducerProperty(const QString &name)
{
    if (m_controller) {
        m_controller->resetProperty(name);
    }
}

void ProjectClip::setProducerProperty(const QString &name, int data)
317
{
318 319
    if (m_controller) {
	m_controller->setProperty(name, data);
320 321 322
    }
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
323
void ProjectClip::setProducerProperty(const QString &name, double data)
324
{
325 326
    if (m_controller) {
        m_controller->setProperty(name, data);
327 328 329
    }
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
330
void ProjectClip::setProducerProperty(const QString &name, const QString &data)
331
{
332 333
    if (m_controller) {
        m_controller->setProperty(name, data);
334 335 336
    }
}

337 338 339 340 341 342 343 344
QColor ProjectClip::getProducerColorProperty(const QString &key) const
{
    if (m_controller) {
        return m_controller->color_property(key);
    }
    return QColor();
}

345 346 347 348 349 350 351 352
int ProjectClip::getProducerIntProperty(const QString &key) const
{
    int value = 0;
    if (m_controller) {
        value = m_controller->int_property(key);
    }
    return value;
}
353

354
QString ProjectClip::getProducerProperty(const QString &key) const
355 356
{
    QString value;
357 358
    if (m_controller) {
	value = m_controller->property(key);
359 360 361 362 363 364
    }
    return value;
}

const QString ProjectClip::hash()
{
365
    if (!m_controller) return QString();
366
    QString clipHash = m_controller->property("kdenlive:file_hash");
367 368 369 370
    if (clipHash.isEmpty()) {
        return getFileHash();
    }
    return clipHash;
371 372
}

373
const QString ProjectClip::getFileHash() const
374
{
375
    if (clipType() == SlideShow) return QString();
376
    QFile file(m_controller->clipUrl().toLocalFile());
377 378 379 380
    if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
        QByteArray fileData;
        QByteArray fileHash;
        ////qDebug() << "SETTING HASH of" << value;
381
        //m_properties.insert("file_size", QString::number(file.size()));
382 383 384 385 386 387 388 389 390 391 392 393
        /*
               * 1 MB = 1 second per 450 files (or faster)
               * 10 MB = 9 seconds per 450 files (or faster)
               */
        if (file.size() > 2000000) {
            fileData = file.read(1000000);
            if (file.seek(file.size() - 1000000))
                fileData.append(file.readAll());
        } else
            fileData = file.readAll();
        file.close();
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
394
        QString result = fileHash.toHex();
395
        m_controller->setProperty("kdenlive:file_hash", result);
396
        return result;
397
    }
398
    return QString();
399
}
400 401 402 403 404 405

double ProjectClip::getOriginalFps() const
{
    if (!m_controller) return 0;
    return m_controller->originalFps();
}
406 407 408

bool ProjectClip::hasProxy() const
{
409
    QString proxy = getProducerProperty("kdenlive:proxy");
410 411 412 413
    if (proxy.isEmpty() || proxy == "-") return false;
    return true;
}

414
void ProjectClip::setProperties(QMap <QString, QString> properties, bool refreshPanel)
415 416 417 418 419 420 421
{
    QMapIterator<QString, QString> i(properties);
    bool refreshProducer = false;
    QStringList keys;
    keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
    while (i.hasNext()) {
        i.next();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
422
        setProducerProperty(i.key(), i.value());
423 424
        if (clipType() == SlideShow && keys.contains(i.key())) refreshProducer = true;
    }
425 426
    if (properties.contains("kdenlive:proxy")) {
        QString value = properties.value("kdenlive:proxy");
427 428 429 430 431 432
        // If value is "-", that means user manually disabled proxy on this clip
        if (value.isEmpty() || value == "-") {
            // reset proxy
            if (bin()->hasPendingJob(m_id, AbstractClipJob::PROXYJOB)) {
                bin()->discardJobs(m_id, AbstractClipJob::PROXYJOB);
            }
433 434 435
            else {
                reloadProducer();
            }
436 437
        }
        else {
438
            // A proxy was requested, make sure to keep original url
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
439
            setProducerProperty("kdenlive:originalurl", url().toLocalFile());
440
            bin()->startJob(m_id, AbstractClipJob::PROXYJOB);
441 442
        }
    }
443 444
    else if (properties.contains("resource")) {
        // Clip resource changed, update thumbnail
445 446 447 448 449 450
        if (clipType() == Color) {
            //reloadProducer(true);
        }
        else {
            reloadProducer(true);
        }
451 452
        refreshProducer = true;
    }
453 454 455 456 457
    if (properties.contains("length")) {
        m_duration = m_controller->getStringDuration();
        bin()->emitItemUpdated(this);
    }

458 459 460 461
    if (properties.contains("kdenlive:clipname")) {
        m_name = properties.value("kdenlive:clipname");
        bin()->emitItemUpdated(this);
    }
462 463 464 465 466 467
    if (refreshPanel) {
        // Some of the clip properties have changed through a command, update properties panel
        emit refreshPropertiesPanel();
    }
    if (refreshProducer) {
        // producer has changed, refresh monitor
468
        bin()->refreshClip(m_id);
469
    }
470 471 472 473 474 475
}

void ProjectClip::setJobStatus(AbstractClipJob::JOBTYPE jobType, ClipJobStatus status, int progress, const QString &statusMessage)
{
    m_jobType = jobType;
    if (progress > 0) {
476
        if (m_jobProgress == progress) return;
477 478 479 480 481 482 483 484 485 486
	m_jobProgress = progress;
    }
    else {
	m_jobProgress = status;
	if ((status == JobAborted || status == JobCrashed  || status == JobDone) || !statusMessage.isEmpty()) {
	    m_jobMessage = statusMessage;
	}
    }
    bin()->emitItemUpdated(this);
}
487

488 489 490

ClipPropertiesController *ProjectClip::buildProperties(QWidget *parent)
{
491
    ClipPropertiesController *panel = new ClipPropertiesController(bin()->projectTimecode(), m_controller, parent);
492 493 494 495
    connect(this, SIGNAL(refreshPropertiesPanel()), panel, SLOT(slotReloadProperties()));
    return panel;
}

496 497
void ProjectClip::updateParentInfo(const QString &folderid, const QString &foldername)
{
498
    m_controller->setProperty("kdenlive:folderid", folderid);
499 500
}

501 502 503 504 505 506
bool ProjectClip::matches(QString condition)
{
    //TODO
    return true;
}

507
const QString ProjectClip::codec(bool audioCodec) const
508
{
509 510
    if (!m_controller) return QString();
    return m_controller->codec(audioCodec);
511
}
512 513 514 515 516 517 518 519 520 521 522 523 524 525

bool ProjectClip::rename(const QString &name)
{
    if (m_name == name) return false;
    // Rename clip
    QMap <QString, QString> newProperites;
    QMap <QString, QString> oldProperites;
    oldProperites.insert("kdenlive:clipname", m_name);
    newProperites.insert("kdenlive:clipname", name);
    bin()->slotEditClipCommand(m_id, oldProperites, newProperites);
    m_name = name;
    return true;
}

526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
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);
}

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

569
void ProjectClip::addEffect(const ProfileInfo pInfo, const QDomElement &effect)
570
{
571
    m_controller->addEffect(pInfo, effect);
572
    bin()->editMasterEffect(m_controller);
573 574
}