projectclip.cpp 12.3 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 25
/*
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"
#include "bin.h"
26
#include "mltcontroller/clipcontroller.h"
27
#include "mltcontroller/clippropertiescontroller.h"
28 29 30 31 32 33 34 35 36 37

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

#include <KLocalizedString>



38
ProjectClip::ProjectClip(const QString &id, ClipController *controller, ProjectFolder* parent) :
39 40 41 42
    AbstractProjectItem(AbstractProjectItem::ClipItem, id, parent)
    , m_controller(controller)
    , audioFrameCache()
    , m_audioThumbCreated(false)
43
{
44
    m_clipStatus = StatusReady;
45 46
    m_name = m_controller->clipName();
    m_duration = m_controller->getStringDuration();
47
    getFileHash();
48
    setParent(parent);
49 50 51
}

ProjectClip::ProjectClip(const QDomElement& description, ProjectFolder* parent) :
52
    AbstractProjectItem(AbstractProjectItem::ClipItem, description, parent)
53
    , m_controller(NULL)
54 55
    , audioFrameCache()
    , m_audioThumbCreated(false)
56 57
{
    Q_ASSERT(description.hasAttribute("id"));
58
    m_clipStatus = StatusWaiting;
59
    QString resource = getXmlProperty(description, "resource");
60
    QString clipName = getXmlProperty(description, "kdenlive:clipname");
61 62 63 64
    if (!clipName.isEmpty()) {
        m_name = clipName;
    }
    else if (!resource.isEmpty()) {
65 66
        m_name = QUrl::fromLocalFile(resource).fileName();
    }
67
    else m_name = i18n("Untitled");
68 69
    if (description.hasAttribute("zone"))
	m_zone = QPoint(description.attribute("zone").section(':', 0, 0).toInt(), description.attribute("zone").section(':', 1, 1).toInt());
70
    setParent(parent);
71 72 73 74 75
}


ProjectClip::~ProjectClip()
{
76
    // controller is deleted in bincontroller
77 78
}

79 80 81 82 83
QString ProjectClip::getToolTip() const
{
    return url().toLocalFile();
}

84 85 86 87 88 89 90 91 92 93 94 95 96
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;
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
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;
}

116 117
ClipType ProjectClip::clipType() const
{
118 119
    if (m_controller == NULL) return Unknown;
    return m_controller->clipType();
120 121 122 123 124 125 126 127 128 129
}

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

130 131 132 133 134
ProjectFolder* ProjectClip::folder(const QString &id)
{
    return NULL;
}

135 136 137 138 139 140 141 142
ProjectClip* ProjectClip::clipAt(int ix)
{
    if (ix == index()) {
        return this;
    }
    return NULL;
}

143
/*bool ProjectClip::isValid() const
144
{
145 146
    return m_controller->isValid();
}*/
147 148 149

QUrl ProjectClip::url() const
{
150
    return m_controller->clipUrl();
151 152 153 154
}

bool ProjectClip::hasLimitedDuration() const
{
155 156 157 158
    if (m_controller) {
        return m_controller->hasLimitedDuration();
    }
    return true;
159 160
}

161
GenTime ProjectClip::duration() const
162
{
163 164
    if (m_controller) {
	return m_controller->getPlaytime();
165
    }
166
    return GenTime();
167 168 169 170 171 172 173 174 175 176 177
}

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

178
void ProjectClip::reloadProducer(bool thumbnailOnly)
179 180 181
{
    QDomDocument doc;
    QDomElement xml = toXml(doc);
182 183 184 185
    if (thumbnailOnly) {
        // set a special flag to request thumbnail only
        xml.setAttribute("thumbnailOnly", "1");
    }
186
    bin()->reloadProducer(m_id, xml);
187 188 189 190 191
}

void ProjectClip::setCurrent(bool current, bool notify)
{
    AbstractProjectItem::setCurrent(current, notify);
Jean-Baptiste Mardelle's avatar
cleanup  
Jean-Baptiste Mardelle committed
192 193
    if (current) {
        bin()->openProducer(m_controller);
194 195 196 197 198
    }
}

QDomElement ProjectClip::toXml(QDomDocument& document)
{
199 200 201
    if (m_controller) {
        m_controller->getProducerXML(document);
        return document.documentElement().firstChildElement("producer");
202
    }
203
    return QDomElement();
204 205 206 207
}

void ProjectClip::setThumbnail(QImage img)
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
208 209
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
210
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
211
        QPainter p(&thumb);
212
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
213
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
214 215 216 217 218 219 220 221
        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
222
    m_thumbnail = QIcon(thumb);
223 224 225
    bin()->emitItemUpdated(this);
}

226
void ProjectClip::setProducer(ClipController *controller, bool replaceProducer)
227
{
228
    if (!replaceProducer && m_controller) {
229 230 231
        qDebug()<<"// RECIEVED PRODUCER BUT WE ALREADY HAVE ONE\n----------";
        return;
    }
232 233
    if (m_controller) {
        // Replace clip for this controller
234
        //m_controller->updateProducer(m_id, &(controller->originalProducer()));
235
        delete controller;
236
    }
237 238 239 240 241 242
    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();
    }
243
    m_clipStatus = StatusReady;
244 245 246 247 248 249
    bin()->emitItemUpdated(this);
    getFileHash();
}

Mlt::Producer *ProjectClip::producer()
{
250 251
    if (!m_controller) return NULL;
    return m_controller->masterProducer();
252 253
}

254
bool ProjectClip::isReady() const
255
{
256
    return m_controller!= NULL;
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
}

void ProjectClip::setZone(const QPoint &zone)
{
    m_zone = zone;
}

QPoint ProjectClip::zone() const
{
    return m_zone;
}

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

void ProjectClip::setProducerProperty(const char *name, int data)
{
283 284
    if (m_controller) {
	m_controller->setProperty(name, data);
285 286 287 288 289
    }
}

void ProjectClip::setProducerProperty(const char *name, double data)
{
290 291
    if (m_controller) {
        m_controller->setProperty(name, data);
292 293 294 295 296
    }
}

void ProjectClip::setProducerProperty(const char *name, const char *data)
{
297 298
    if (m_controller) {
        m_controller->setProperty(name, data);
299 300 301
    }
}

302 303 304 305 306 307 308 309
int ProjectClip::getProducerIntProperty(const QString &key) const
{
    int value = 0;
    if (m_controller) {
        value = m_controller->int_property(key);
    }
    return value;
}
310

311
QString ProjectClip::getProducerProperty(const QString &key) const
312 313
{
    QString value;
314 315
    if (m_controller) {
	value = m_controller->property(key);
316 317 318 319 320 321
    }
    return value;
}

const QString ProjectClip::hash()
{
322 323 324 325 326 327
    if (!m_controller) return QString();
    QString clipHash = m_controller->property("file_hash");
    if (clipHash.isEmpty()) {
        return getFileHash();
    }
    return clipHash;
328 329
}

330
const QString ProjectClip::getFileHash() const
331
{
332
    if (clipType() == SlideShow) return QString();
333
    QFile file(m_controller->clipUrl().toLocalFile());
334 335 336 337
    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;
338
        //m_properties.insert("file_size", QString::number(file.size()));
339 340 341 342 343 344 345 346 347 348 349 350
        /*
               * 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);
351 352 353
        QString result = fileHash.toHex();
        m_controller->setProperty("file_hash", result);
        return result;
354
    }
355
    return QString();
356 357 358 359
}

bool ProjectClip::hasProxy() const
{
360
    QString proxy = getProducerProperty("kdenlive:proxy");
361 362 363 364
    if (proxy.isEmpty() || proxy == "-") return false;
    return true;
}

365
void ProjectClip::setProperties(QMap <QString, QString> properties, bool refreshPanel)
366 367 368 369 370 371 372 373 374 375
{
    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();
        setProducerProperty(i.key().toUtf8().data(), i.value().toUtf8().data());
        if (clipType() == SlideShow && keys.contains(i.key())) refreshProducer = true;
    }
376 377
    if (properties.contains("kdenlive:proxy")) {
        QString value = properties.value("kdenlive:proxy");
378 379 380 381 382 383
        // 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);
            }
384 385 386
            else {
                reloadProducer();
            }
387 388
        }
        else {
389
            // A proxy was requested, make sure to keep original url
390
            setProducerProperty("kdenlive:originalurl", url().toLocalFile().toUtf8().data());
391
            bin()->startJob(m_id, AbstractClipJob::PROXYJOB);
392 393
        }
    }
394 395 396 397 398
    else if (properties.contains("resource")) {
        // Clip resource changed, update thumbnail
        reloadProducer(true);
        refreshProducer = true;
    }
399 400 401 402
    if (properties.contains("kdenlive:clipname")) {
        m_name = properties.value("kdenlive:clipname");
        bin()->emitItemUpdated(this);
    }
403 404 405 406 407 408 409 410
    if (refreshPanel) {
        // Some of the clip properties have changed through a command, update properties panel
        emit refreshPropertiesPanel();
    }
    if (refreshProducer) {
        // producer has changed, refresh monitor
        bin()->refreshMonitor(m_id);
    }
411 412 413 414 415 416
}

void ProjectClip::setJobStatus(AbstractClipJob::JOBTYPE jobType, ClipJobStatus status, int progress, const QString &statusMessage)
{
    m_jobType = jobType;
    if (progress > 0) {
417
        if (m_jobProgress == progress) return;
418 419 420 421 422 423 424 425 426 427
	m_jobProgress = progress;
    }
    else {
	m_jobProgress = status;
	if ((status == JobAborted || status == JobCrashed  || status == JobDone) || !statusMessage.isEmpty()) {
	    m_jobMessage = statusMessage;
	}
    }
    bin()->emitItemUpdated(this);
}
428

429 430 431 432 433 434 435 436

ClipPropertiesController *ProjectClip::buildProperties(QWidget *parent)
{
    ClipPropertiesController *panel = new ClipPropertiesController(m_id, clipType(), m_controller->properties(), parent);
    connect(this, SIGNAL(refreshPropertiesPanel()), panel, SLOT(slotReloadProperties()));
    return panel;
}

437 438
void ProjectClip::updateParentInfo(const QString &folderid, const QString &foldername)
{
439
    m_controller->setProperty("kdenlive:folderid", folderid);
440 441
}

442 443 444 445 446 447 448
bool ProjectClip::matches(QString condition)
{
    //TODO
    return true;
}