]> git.sesse.net Git - kdenlive/blobdiff - src/titledocument.cpp
Get ready for typewriter effect in titles
[kdenlive] / src / titledocument.cpp
index 959aac69ee887db2e63b29627f3e11d296c2337b..b68e1521646513adeb9d55d714c4b5ac9155d589 100644 (file)
 #include <QFile>
 #include <QTextCursor>
 
+#if QT_VERSION >= 0x040600
+#include <QGraphicsEffect>
+#include <QGraphicsBlurEffect>
+#include <QGraphicsDropShadowEffect>
+#endif
 
 TitleDocument::TitleDocument()
 {
     m_scene = NULL;
 }
 
-void TitleDocument::setScene(QGraphicsScene* _scene)
+void TitleDocument::setScene(QGraphicsScene* _scene, int width, int height)
 {
     m_scene = _scene;
+    m_width = width;
+    m_height = height;
 }
 
-QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
+QDomDocument TitleDocument::xml(QGraphicsRectItem* startv, QGraphicsRectItem* endv)
 {
     QDomDocument doc;
 
     QDomElement main = doc.createElement("kdenlivetitle");
+    main.setAttribute("width", m_width);
+    main.setAttribute("height", m_height);
     doc.appendChild(main);
 
     foreach(QGraphicsItem* item, m_scene->items()) {
@@ -88,6 +97,11 @@ QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonIt
             content.setAttribute("font-italic", font.italic());
             content.setAttribute("font-underline", font.underline());
             content.setAttribute("font-color", colorToString(t->defaultTextColor()));
+           
+           if (!t->data(100).isNull()) {
+               QStringList effectParams = t->data(100).toStringList();
+               content.setAttribute(effectParams.at(0), effectParams.at(1));
+           }
 
             // Only save when necessary.
             if (t->data(OriginXLeft).toInt() == AxisInverted) {
@@ -103,6 +117,8 @@ QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonIt
         default:
             continue;
         }
+
+        // position
         QDomElement pos = doc.createElement("position");
         pos.setAttribute("x", item->pos().x());
         pos.setAttribute("y", item->pos().y());
@@ -116,26 +132,40 @@ QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonIt
         e.setAttribute("z-index", item->zValue());
         pos.appendChild(tr);
 
+#if QT_VERSION >= 0x040600
+        // effects
+        QGraphicsEffect *eff = item->graphicsEffect();
+        if (eff) {
+            QGraphicsBlurEffect *blur = static_cast <QGraphicsBlurEffect *>(eff);
+            QDomElement effect = doc.createElement("effect");
+            if (blur) {
+                effect.setAttribute("type", "blur");
+                effect.setAttribute("blurradius", blur->blurRadius());
+            } else {
+                QGraphicsDropShadowEffect *shadow = static_cast <QGraphicsDropShadowEffect *>(eff);
+                if (shadow) {
+                    effect.setAttribute("type", "shadow");
+                    effect.setAttribute("blurradius", shadow->blurRadius());
+                    effect.setAttribute("xoffset", shadow->xOffset());
+                    effect.setAttribute("yoffset", shadow->yOffset());
+                }
+            }
+            e.appendChild(effect);
+        }
+#endif
 
         e.appendChild(pos);
         e.appendChild(content);
-        if (item->zValue() > -1100) main.appendChild(e);
+        if (item->zValue() > -1000) main.appendChild(e);
     }
     if (startv && endv) {
         QDomElement endp = doc.createElement("endviewport");
         QDomElement startp = doc.createElement("startviewport");
-        endp.setAttribute("x", endv->data(0).toString());
-        endp.setAttribute("y", endv->data(1).toString());
-        endp.setAttribute("size", endv->data(2).toString());
-        endp.setAttribute("rect", rectFToString(endv->boundingRect()));
-
-        startp.setAttribute("x", startv->data(0).toString());
-        startp.setAttribute("y", startv->data(1).toString());
-        startp.setAttribute("size", startv->data(2).toString());
-        startp.setAttribute("rect", rectFToString(startv->boundingRect()));
-
-        startp.setAttribute("z-index", startv->zValue());
-        endp.setAttribute("z-index", endv->zValue());
+        QRectF r(endv->pos().x(), endv->pos().y(), endv->rect().width(), endv->rect().height());
+        endp.setAttribute("rect", rectFToString(r));
+        QRectF r2(startv->pos().x(), startv->pos().y(), startv->rect().width(), startv->rect().height());
+        startp.setAttribute("rect", rectFToString(r2));
+
         main.appendChild(startp);
         main.appendChild(endp);
     }
@@ -165,12 +195,13 @@ QColor TitleDocument::getBackgroundColor()
 }
 
 
-bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
+bool TitleDocument::saveDocument(const KUrl& url, QGraphicsRectItem* startv, QGraphicsRectItem* endv, int out)
 {
     if (!m_scene)
         return false;
 
     QDomDocument doc = xml(startv, endv);
+    doc.documentElement().setAttribute("out", out);
     KTemporaryFile tmpfile;
     if (!tmpfile.open()) {
         kWarning() << "/////  CANNOT CREATE TMP FILE in: " << tmpfile.fileName();
@@ -187,29 +218,26 @@ bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv,
     return KIO::NetAccess::upload(tmpfile.fileName(), url, 0);
 }
 
-int TitleDocument::loadDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
+int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsRectItem* startv, QGraphicsRectItem* endv, int *out)
 {
-    QString tmpfile;
-    QDomDocument doc;
-    if (!m_scene)
-        return -1;
-
-    if (KIO::NetAccess::download(url, tmpfile, 0)) {
-        QFile file(tmpfile);
-        if (file.open(QIODevice::ReadOnly)) {
-            doc.setContent(&file, false);
-            file.close();
-        } else
-            return -1;
-        KIO::NetAccess::removeTempFile(tmpfile);
-        return loadFromXml(doc, startv, endv);
+    QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
+    //TODO: Check if the opened title size is equal to project size, otherwise warn user and rescale
+    if (doc.documentElement().hasAttribute("width") && doc.documentElement().hasAttribute("height")) {
+        int doc_width = doc.documentElement().attribute("width").toInt();
+        int doc_height = doc.documentElement().attribute("height").toInt();
+        if (doc_width != m_width || doc_height != m_height) {
+            KMessageBox::information(kapp->activeWindow(), i18n("This title clip was created with a different frame size."), i18n("Title Profile"));
+            //TODO: convert using QTransform
+            m_width = doc_width;
+            m_height = doc_height;
+        }
     }
-    return -1;
-}
+    //TODO: get default title duration instead of hardcoded one
+    if (doc.documentElement().hasAttribute("out"))
+        *out = doc.documentElement().attribute("out").toInt();
+    else
+        *out = 125;
 
-int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
-{
-    QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
     int maxZValue = 0;
     if (titles.size()) {
 
@@ -264,6 +292,12 @@ int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, Q
                     if (!txtProperties.namedItem("kdenlive-axis-y-inverted").isNull()) {
                         txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
                     }
+                   
+                   // Effects
+                   if (!txtProperties.namedItem("typewriter").isNull()) {
+                       QStringList effData = QStringList() << "typewriter" << QString::number(txtProperties.namedItem("typewriter").nodeValue().toInt());
+                       txt->setData(100, effData);
+                   }
 
                     gitem = txt;
                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
@@ -297,7 +331,26 @@ int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, Q
                 if (zValue > maxZValue) maxZValue = zValue;
                 gitem->setZValue(zValue);
                 gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
+
+#if QT_VERSION >= 0x040600
+                // effects
+                QDomNode eff = items.item(i).namedItem("effect");
+                if (!eff.isNull()) {
+                    QDomElement e = eff.toElement();
+                    if (e.attribute("type") == "blur") {
+                        QGraphicsBlurEffect *blur = new QGraphicsBlurEffect();
+                        blur->setBlurRadius(e.attribute("blurradius").toInt());
+                        gitem->setGraphicsEffect(blur);
+                    } else if (e.attribute("type") == "shadow") {
+                        QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
+                        shadow->setBlurRadius(e.attribute("blurradius").toInt());
+                        shadow->setOffset(e.attribute("xoffset").toInt(), e.attribute("yoffset").toInt());
+                        gitem->setGraphicsEffect(shadow);
+                    }
+                }
+#endif
             }
+
             if (items.item(i).nodeName() == "background") {
                 kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
                 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
@@ -311,23 +364,14 @@ int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, Q
                 }
             } else if (items.item(i).nodeName() == "startviewport" && startv) {
                 QString rect = items.item(i).attributes().namedItem("rect").nodeValue();
-                startv->setPolygon(stringToRect(rect));
-                int x = items.item(i).attributes().namedItem("x").nodeValue().toInt();
-                int y = items.item(i).attributes().namedItem("y").nodeValue().toInt();
-                int size = items.item(i).attributes().namedItem("size").nodeValue().toInt();
-                startv->setData(0, x);
-                startv->setData(1, y);
-                startv->setData(2, size);
-                //startv->setPos(p);
+                QRectF r = stringToRect(rect);
+                startv->setRect(0, 0, r.width(), r.height());
+                startv->setPos(r.topLeft());
             } else if (items.item(i).nodeName() == "endviewport" && endv) {
                 QString rect = items.item(i).attributes().namedItem("rect").nodeValue();
-                endv->setPolygon(stringToRect(rect));
-                int x = items.item(i).attributes().namedItem("x").nodeValue().toInt();
-                int y = items.item(i).attributes().namedItem("y").nodeValue().toInt();
-                int size = items.item(i).attributes().namedItem("size").nodeValue().toInt();
-                endv->setData(0, x);
-                endv->setData(1, y);
-                endv->setData(2, size);
+                QRectF r = stringToRect(rect);
+                endv->setRect(0, 0, r.width(), r.height());
+                endv->setPos(r.topLeft());
             }
         }
     }
@@ -375,3 +419,15 @@ QTransform TitleDocument::stringToTransform(const QString& s)
                l.at(6).toDouble(), l.at(7).toDouble(), l.at(8).toDouble()
            );
 }
+
+int TitleDocument::frameWidth() const
+{
+    return m_width;
+}
+
+int TitleDocument::frameHeight() const
+{
+    return m_height;
+}
+
+