]> git.sesse.net Git - kdenlive/blobdiff - src/titledocument.cpp
Title files (*.kdenlivetitle) stored as utf-8 instead of ascii now. http://www.kdenli...
[kdenlive] / src / titledocument.cpp
index c334b715e3371eb4bb4a88c882af6990f0191a63..9b5c58237fb507536c2781798364276ae0bdeacc 100644 (file)
  ***************************************************************************/
 #include "titledocument.h"
 #include <QGraphicsScene>
-#include <QDomDocument>
 #include <QDomElement>
 #include <QGraphicsItem>
 #include <QGraphicsRectItem>
 #include <QGraphicsTextItem>
+#include <QGraphicsSvgItem>
 #include <KDebug>
 #include <QFile>
+#include <KTemporaryFile>
 #include <kio/netaccess.h>
 
 TitleDocument::TitleDocument() {
@@ -33,20 +34,27 @@ void TitleDocument::setScene(QGraphicsScene* _scene) {
     scene = _scene;
 }
 
-bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
+QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
     QDomDocument doc;
 
-    if (!scene)
-        return false;
-
     QDomElement main = doc.createElement("kdenlivetitle");
     doc.appendChild(main);
 
     foreach(QGraphicsItem* item, scene->items()) {
         QDomElement e = doc.createElement("item");
         QDomElement content = doc.createElement("content");
+        QFont font;
+        QGraphicsTextItem *t;
 
         switch (item->type()) {
+        case 7:
+            e.setAttribute("type", "QGraphicsPixmapItem");
+            content.setAttribute("url", item->data(Qt::UserRole).toString());
+            break;
+        case 13:
+            e.setAttribute("type", "QGraphicsSvgItem");
+            content.setAttribute("url", item->data(Qt::UserRole).toString());
+            break;
         case 3:
             e.setAttribute("type", "QGraphicsRectItem");
             content.setAttribute("rect", rectFToString(((QGraphicsRectItem*)item)->rect()));
@@ -56,7 +64,16 @@ bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv,
             break;
         case 8:
             e.setAttribute("type", "QGraphicsTextItem");
-            content.appendChild(doc.createTextNode(((QGraphicsTextItem*)item)->toHtml()));
+            t = static_cast<QGraphicsTextItem *>(item);
+            //content.appendChild(doc.createTextNode(((QGraphicsTextItem*)item)->toHtml()));
+            content.appendChild(doc.createTextNode(t->toPlainText()));
+            font = t->font();
+            content.setAttribute("font", font.family());
+            content.setAttribute("font-bold", font.bold());
+            content.setAttribute("font-size", font.pointSize());
+            content.setAttribute("font-italic", font.italic());
+            content.setAttribute("font-underline", font.underline());
+            content.setAttribute("font-color", colorToString(t->defaultTextColor()));
             break;
         default:
             continue;
@@ -77,7 +94,7 @@ bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv,
 
         e.appendChild(pos);
         e.appendChild(content);
-        main.appendChild(e);
+        if (item->zValue() > -1100) main.appendChild(e);
     }
     if (startv && endv) {
         QDomElement endp = doc.createElement("endviewport");
@@ -96,23 +113,51 @@ bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv,
         main.appendChild(endp);
     }
     QDomElement backgr = doc.createElement("background");
-    backgr.setAttribute("color", colorToString(scene->backgroundBrush().color()));
+    QColor color = getBackgroundColor();
+    backgr.setAttribute("color", colorToString(color));
     main.appendChild(backgr);
 
-    QString tmpfile = "/tmp/newtitle";
-    QFile xmlf(tmpfile);
+    return doc;
+}
+
+/** \brief Get the background color (incl. alpha) from the document, if possibly
+  * \returns The background color of the document, inclusive alpha. If none found, returns (0,0,0,0) */
+QColor TitleDocument::getBackgroundColor() {
+    QColor color(0, 0, 0, 0);
+    if (scene) {
+        QList<QGraphicsItem *> items = scene->items();
+        for (int i = 0; i < items.size(); i++) {
+            if (items.at(i)->zValue() == -1100) {
+                color = ((QGraphicsRectItem *)items.at(i))->brush().color();
+                return color;
+            }
+        }
+    }
+    return color;
+}
+
+
+bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
+    if (!scene)
+        return false;
+
+    QDomDocument doc = xml(startv, endv);
+    KTemporaryFile tmpfile;
+    if (!tmpfile.open()) kWarning() << "/////  CANNOT CREATE TMP FILE in: " << tmpfile.fileName();
+    QFile xmlf(tmpfile.fileName());
     xmlf.open(QIODevice::WriteOnly);
-    xmlf.write(doc.toString().toAscii());
+    xmlf.write(doc.toString().toUtf8());
     xmlf.close();
-    kDebug() << KIO::NetAccess::upload(tmpfile, url, 0);
+    kDebug() << KIO::NetAccess::upload(tmpfile.fileName(), url, 0);
+    return true;
 }
 
-bool TitleDocument::loadDocument(const KUrl& url , QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
+int TitleDocument::loadDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
     QString tmpfile;
     QDomDocument doc;
     double aspect_ratio = 4.0 / 3.0;
     if (!scene)
-        return false;
+        return -1;
 
     if (KIO::NetAccess::download(url, tmpfile, 0)) {
         QFile file(tmpfile);
@@ -120,18 +165,33 @@ bool TitleDocument::loadDocument(const KUrl& url , QGraphicsPolygonItem* startv,
             doc.setContent(&file, false);
             file.close();
         } else
-            return false;
+            return -1;
         KIO::NetAccess::removeTempFile(tmpfile);
-        QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
-        if (titles.size()) {
+        return loadFromXml(doc, startv, endv);
+    }
+}
 
-            QDomNodeList items = titles.item(0).childNodes();
-            for (int i = 0;i < items.count();i++) {
-                QGraphicsItem *gitem = NULL;
-                kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
+int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
+    QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
+    int maxZValue = 0;
+    if (titles.size()) {
+
+        QDomNodeList items = titles.item(0).childNodes();
+        for (int i = 0;i < items.count();i++) {
+            QGraphicsItem *gitem = NULL;
+            kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
+            int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
+            if (zValue > -1000)
                 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
-                    QGraphicsTextItem *txt = scene->addText("");
-                    txt->setHtml(items.item(i).namedItem("content").firstChild().nodeValue());
+                    QFont font(items.item(i).namedItem("content").attributes().namedItem("font").nodeValue());
+                    font.setBold(items.item(i).namedItem("content").attributes().namedItem("font-bold").nodeValue().toInt());
+                    font.setItalic(items.item(i).namedItem("content").attributes().namedItem("font-italic").nodeValue().toInt());
+                    font.setUnderline(items.item(i).namedItem("content").attributes().namedItem("font-underline").nodeValue().toInt());
+                    font.setPointSize(items.item(i).namedItem("content").attributes().namedItem("font-size").nodeValue().toInt());
+                    QColor col(stringToColor(items.item(i).namedItem("content").attributes().namedItem("font-color").nodeValue()));
+                    QGraphicsTextItem *txt = scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
+                    txt->setDefaultTextColor(col);
+                    txt->setTextInteractionFlags(Qt::NoTextInteraction);
                     gitem = txt;
                 } else
                     if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
@@ -141,18 +201,44 @@ bool TitleDocument::loadDocument(const KUrl& url , QGraphicsPolygonItem* startv,
                         double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
                         QGraphicsRectItem *rec = scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
                         gitem = rec;
+                    } else
+                        if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
+                            QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
+                            QPixmap pix(url);
+                            QGraphicsPixmapItem *rec = scene->addPixmap(pix);
+                            rec->setData(Qt::UserRole, url);
+                            gitem = rec;
+                        } else
+                            if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
+                                QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
+                                QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
+                                scene->addItem(rec);
+                                rec->setData(Qt::UserRole, url);
+                                gitem = rec;
+                            }
+            //pos and transform
+            if (gitem) {
+                QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
+                          items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
+                gitem->setPos(p);
+                gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
+                int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
+                if (zValue > maxZValue) maxZValue = zValue;
+                gitem->setZValue(zValue);
+                gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
+            }
+            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()));
+                //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
+                QList<QGraphicsItem *> items = scene->items();
+                for (int i = 0; i < items.size(); i++) {
+                    if (items.at(i)->zValue() == -1100) {
+                        ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
+                        break;
                     }
-                //pos and transform
-                if (gitem) {
-                    QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
-                              items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
-                    gitem->setPos(p);
-                    gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
                 }
-                if (items.item(i).nodeName() == "background") {
-                    kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
-                    scene->setBackgroundBrush(QBrush(stringToColor(items.item(i).attributes().namedItem("color").nodeValue())));
-                } else if (items.item(i).nodeName() == "startviewport" && startv) {
+            } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
@@ -166,16 +252,10 @@ bool TitleDocument::loadDocument(const KUrl& url , QGraphicsPolygonItem* startv,
                     kDebug() << width << rect;
                     endv->setPolygon(rect);
                     endv->setPos(p);
-                }
-
-            }
-
-
+                }*/
         }
-
-
     }
-    return true;
+    return maxZValue;
 }
 
 QString TitleDocument::colorToString(const QColor& c) {