]> git.sesse.net Git - kdenlive/blobdiff - src/docclipbase.cpp
* internal rework: switch clip id's from integer to string
[kdenlive] / src / docclipbase.cpp
index fdfc143d3d2a3919704b425ea61d33e4302d5c08..3c6620ccd721f0a1c2814ce276e3a1773d14ae9e 100644 (file)
 #include "kthumb.h"
 #include "clipmanager.h"
 
-DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, uint id):
-        m_xml(xml), m_id(id), m_description(""), m_refcount(0), m_projectThumbFrame(0), m_audioThumbCreated(false), m_duration(GenTime()), m_thumbProd(NULL), m_audioTimer(NULL) {
+DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id):
+        m_id(id), m_description(QString()), m_refcount(0), m_audioThumbCreated(false), m_duration(GenTime()), m_thumbProd(NULL), m_audioTimer(NULL), m_clipProducer(NULL) {
     int type = xml.attribute("type").toInt();
     m_clipType = (CLIPTYPE) type;
     m_name = xml.attribute("name");
-    m_xml.setAttribute("id", QString::number(id));
-    KUrl url = KUrl(xml.attribute("resource"));
-    int out = xml.attribute("out").toInt();
-    if (out != 0) setDuration(GenTime(out, 25));
-    if (m_name.isEmpty()) m_name = url.fileName();
-    if (!url.isEmpty()) {
-        m_thumbProd = new KThumb(clipManager, url);
-        connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
-        connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
 
+    QDomNamedNodeMap attributes = xml.attributes();
+    for (unsigned int i = 0; i < attributes.count(); i++) {
+        m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
     }
-    kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
 
-    if (m_clipType == AV || m_clipType == AUDIO || m_clipType == UNKNOWN) {
-        m_audioTimer = new QTimer(this);
-        connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
+    KUrl url = KUrl(xml.attribute("resource"));
+    int out = xml.attribute("out").toInt();
+    if (out != 0) {
+        setDuration(GenTime(out, KdenliveSettings::project_fps()));
+    } else {
+        out = xml.attribute("duration").toInt();
+        if (out != 0) setDuration(GenTime(out, KdenliveSettings::project_fps()));
     }
-}
-
-
+    if (m_name.isEmpty()) m_name = url.fileName();
 
-DocClipBase::DocClipBase(const DocClipBase& clip) {
-    m_xml = clip.toXML();
-    m_id = clip.getId();
-    m_clipType = clip.clipType();
-    m_name = clip.name();
-    m_duration = clip.duration();
-    m_audioThumbCreated = clip.audioThumbCreated();
+    if (!url.isEmpty() && QFile::exists(url.path())) {
+        m_thumbProd = new KThumb(clipManager, url);
+        if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
+    }
+    //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
 }
 
-DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
+/*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
     DocClipBase::operator=(clip);
-    m_xml = clip.toXML();
     m_id = clip.getId();
     m_clipType = clip.clipType();
     m_name = clip.name();
     m_duration = clip.duration();
     m_audioThumbCreated = clip.audioThumbCreated();
+    m_properties = clip.properties();
     return *this;
-}
+}*/
 
 DocClipBase::~DocClipBase() {
     if (m_thumbProd) delete m_thumbProd;
+    if (m_clipProducer) delete m_clipProducer;
+}
+
+void DocClipBase::slotCreateAudioTimer() {
+    connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
+    connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
+    m_audioTimer = new QTimer(this);
+    connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
 }
 
 void DocClipBase::slotRequestAudioThumbs() {
@@ -77,15 +78,12 @@ void DocClipBase::slotRequestAudioThumbs() {
 }
 
 void DocClipBase::slotClearAudioCache() {
+    if (m_thumbProd) m_thumbProd->stopAudioThumbs();
+    if (m_audioTimer != NULL) m_audioTimer->stop();
     audioFrameChache.clear();
     m_audioThumbCreated = false;
 }
 
-void DocClipBase::setGroup(const QString name, const QString id) {
-    m_xml.setAttribute("groupname", name);
-    m_xml.setAttribute("groupid", id);
-}
-
 KThumb *DocClipBase::thumbProducer() {
     return m_thumbProd;
 }
@@ -103,11 +101,11 @@ const QString & DocClipBase::name() const {
     return m_name;
 }
 
-uint DocClipBase::getId() const {
+const QString &DocClipBase::getId() const {
     return m_id;
 }
 
-void DocClipBase::setId(const uint &newId) {
+void DocClipBase::setId(const QString &newId) {
     m_id = newId;
 }
 
@@ -117,38 +115,54 @@ const CLIPTYPE & DocClipBase::clipType() const {
 
 void DocClipBase::setClipType(CLIPTYPE type) {
     m_clipType = type;
+    m_properties.insert("type", QString::number((int) type));
+    if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
+        slotCreateAudioTimer();
 }
 
 KUrl DocClipBase::fileURL() const {
-    QString res = m_xml.attribute("resource");
+    QString res = m_properties.value("resource");
     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
     return KUrl();
 }
 
-void DocClipBase::setProjectThumbFrame(const uint &ix) {
-    m_projectThumbFrame = ix;
+void DocClipBase::setClipThumbFrame(const uint &ix) {
+    m_properties.insert("thumbnail", QString::number((int) ix));
+}
+
+uint DocClipBase::getClipThumbFrame() const {
+    return (uint) m_properties.value("thumbnail").toInt();
 }
 
-uint DocClipBase::getProjectThumbFrame() const {
-    return m_projectThumbFrame;
+const QString DocClipBase::description() const {
+    return m_properties.value("description");
 }
 
-void DocClipBase::setDescription(const QString & description) {
-    m_description = description;
+bool DocClipBase::isTransparent() const {
+    return (m_properties.value("transparency") == "1");
 }
 
-const QString & DocClipBase::description() const {
-    return m_description;
+const QString DocClipBase::getProperty(const QString prop) const {
+    return m_properties.value(prop);
 }
 
 void DocClipBase::setDuration(GenTime dur) {
     m_duration = dur;
+    m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
 }
 
 const GenTime &DocClipBase::duration() const {
     return m_duration;
 }
 
+const GenTime &DocClipBase::maxDuration() const {
+    if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
+        const GenTime dur(10000, KdenliveSettings::project_fps());
+        return dur;
+    }
+    return m_duration;
+}
+
 bool DocClipBase::hasFileSize() const {
     return true;
 }
@@ -156,15 +170,18 @@ bool DocClipBase::hasFileSize() const {
 
 // virtual
 QDomElement DocClipBase::toXML() const {
-    /*
-        QDomDocument doc;
+    QDomDocument doc;
+
+    QDomElement clip = doc.createElement("producer");
 
-        QDomElement clip = doc.createElement("kdenliveclip");
-        QDomText text = doc.createTextNode(description());
-        clip.appendChild(text);
-        doc.appendChild(clip);
-    */
-    return m_xml;
+    QMapIterator<QString, QString> i(m_properties);
+    while (i.hasNext()) {
+        i.next();
+        if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
+    }
+    //doc.appendChild(clip);
+    //kDebug()<<"/// CLIP XML: "<<doc.toString();
+    return clip;
 }
 
 DocClipBase *DocClipBase::
@@ -175,8 +192,7 @@ createClip(KdenliveDoc *doc, const QDomElement & element) {
     node.normalize();
     if (element.tagName() != "kdenliveclip") {
         kWarning() <<
-        "DocClipBase::createClip() element has unknown tagName : " <<
-        element.tagName() << endl;
+        "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
         return 0;
     }
 
@@ -201,11 +217,12 @@ createClip(KdenliveDoc *doc, const QDomElement & element) {
         n = n.nextSibling();
     }
     if (clip == 0) {
-        kWarning() << "DocClipBase::createClip() unable to create clip" <<
-        endl;
+        kWarning() << "DocClipBase::createClip() unable to create clip";
     } else {
         // setup DocClipBase specifics of the clip.
-        clip->setDescription(description);
+        QMap <QString, QString> props;
+        props.insert("description", description);
+        clip->setProperties(props);
         clip->setAudioThumbCreated(false);
     }
     return clip;
@@ -216,9 +233,6 @@ void DocClipBase::setAudioThumbCreated(bool isDone) {
 }
 
 
-QDomDocument DocClipBase::generateSceneList(bool, bool) const {
-}
-
 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
     m_thumbnail = pixmap;
 }
@@ -260,9 +274,8 @@ void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
     }
 
     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
-        kError() <<
-        "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo"
-        << endl;
+        (*it).setComment(comment);
+        //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
     } else {
         CommentedTime t(time, comment);
         m_snapMarkers.insert(it, t);
@@ -279,8 +292,7 @@ void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
     if (it != m_snapMarkers.end()) {
         (*it).setComment(comment);
     } else {
-        kError() <<
-        "trying to edit Snap Marker that does not already exists"  << endl;
+        kError() << "trying to edit Snap Marker that does not already exists";
     }
 }
 
@@ -347,17 +359,114 @@ QString DocClipBase::markerComment(GenTime t) {
     return QString::null;
 }
 
+void DocClipBase::setProducer(Mlt::Producer *producer) {
+    m_clipProducer = producer;
+    m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
+    if (m_thumbProd) m_thumbProd->setProducer(producer);
+}
+
+Mlt::Producer *DocClipBase::producer() {
+    return m_clipProducer;
+}
+
+void DocClipBase::slotRefreshProducer() {
+    if (m_clipProducer == NULL) return;
+    kDebug() << "////////////   REFRESH CLIP !!!!!!!!!!!!!!!!";
+    if (m_clipType == SLIDESHOW) {
+        /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
+               Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
+               delete[] tmp;
+        delete m_clipProducer;
+        m_clipProducer = new Mlt::Producer(producer.get_producer());
+        if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
+        m_clipProducer->set("ttl", getProperty("ttl").toInt());
+        //m_clipProducer->set("id", getProperty("id"));
+        if (getProperty("fade") == "1") {
+            // we want a fade filter effect
+            kDebug() << "////////////   FADE WANTED";
+            Mlt::Service clipService(m_clipProducer->get_service());
+            int ct = 0;
+            Mlt::Filter *filter = clipService.filter(ct);
+            while (filter) {
+                if (filter->get("mlt_service") == "luma") {
+                    break;
+                }
+                ct++;
+                filter = clipService.filter(ct);
+            }
+
+            if (filter && filter->get("mlt_service") == "luma") {
+                filter->set("period", getProperty("ttl").toInt() - 1);
+                filter->set("luma.out", getProperty("luma_duration").toInt());
+                QString resource = getProperty("luma_file");
+                char *tmp = (char *) qstrdup(resource.toUtf8().data());
+                filter->set("luma.resource", tmp);
+                delete[] tmp;
+                if (getProperty("softness") != QString()) {
+                    int soft = getProperty("softness").toInt();
+                    filter->set("luma.softness", (double) soft / 100.0);
+                }
+            } else {
+                // filter does not exist, create it...
+                Mlt::Filter *filter = new Mlt::Filter(*(m_clipProducer->profile()), "luma");
+                filter->set("period", getProperty("ttl").toInt() - 1);
+                filter->set("luma.out", getProperty("luma_duration").toInt());
+                QString resource = getProperty("luma_file");
+                char *tmp = (char *) qstrdup(resource.toUtf8().data());
+                filter->set("luma.resource", tmp);
+                delete[] tmp;
+                if (getProperty("softness") != QString()) {
+                    int soft = getProperty("softness").toInt();
+                    filter->set("luma.softness", (double) soft / 100.0);
+                }
+                clipService.attach(*filter);
+            }
+        } else {
+            kDebug() << "////////////   FADE NOT WANTED!!!";
+            Mlt::Service clipService(m_clipProducer->get_service());
+            int ct = 0;
+            Mlt::Filter *filter = clipService.filter(0);
+            while (filter) {
+                if (filter->get("mlt_service") == "luma") {
+                    clipService.detach(*filter);
+                } else ct++;
+                filter = clipService.filter(ct);
+            }
+        }
+    }
+}
+
 void DocClipBase::setProperties(QMap <QString, QString> properties) {
-    m_properties = properties;
+    // changing clip type is not allowed
+    properties.remove("type");
+    QMapIterator<QString, QString> i(properties);
+    bool refreshProducer = false;
+    QStringList keys;
+    keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
+    while (i.hasNext()) {
+        i.next();
+        m_properties.insert(i.key(), i.value());
+        if (i.key() == "resource") m_thumbProd->updateClipUrl(KUrl(i.value()));
+        else if (i.key() == "out") setDuration(GenTime(i.value().toInt(), KdenliveSettings::project_fps()));
+        else if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
+        else if (i.key() == "transparency") m_clipProducer->set("transparency", i.value().toInt());
+    }
+    if (refreshProducer) slotRefreshProducer();
 }
 
-QMap <QString, QString> DocClipBase::properties() {
-    return m_properties;
+void DocClipBase::setProperty(QString key, QString value) {
+    m_properties.insert(key, value);
+    if (key == "resource") m_thumbProd->updateClipUrl(KUrl(value));
+    else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
+    else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
 }
 
+QMap <QString, QString> DocClipBase::properties() const {
+    return m_properties;
+}
 
 void DocClipBase::slotGetAudioThumbs() {
-
+    if (m_thumbProd == NULL) return;
     if (m_audioThumbCreated) {
         if (m_audioTimer != NULL)
             m_audioTimer->stop();