1 /***************************************************************************
2 titledocument.h - description
5 copyright : (C) 2008 by Marco Gittler
6 email : g.marco@freenet.de
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
17 #include "titledocument.h"
18 #include <QGraphicsScene>
19 #include <QDomDocument>
20 #include <QDomElement>
21 #include <QGraphicsItem>
22 #include <QGraphicsRectItem>
23 #include <QGraphicsTextItem>
26 #include <kio/netaccess.h>
28 TitleDocument::TitleDocument() {
32 void TitleDocument::setScene(QGraphicsScene* _scene) {
36 bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
42 QDomElement main = doc.createElement("kdenlivetitle");
43 doc.appendChild(main);
45 foreach(QGraphicsItem* item, scene->items()) {
46 QDomElement e = doc.createElement("item");
47 QDomElement content = doc.createElement("content");
49 switch (item->type()) {
51 e.setAttribute("type", "QGraphicsRectItem");
52 content.setAttribute("rect", rectFToString(((QGraphicsRectItem*)item)->rect()));
53 content.setAttribute("pencolor", colorToString(((QGraphicsRectItem*)item)->pen().color()));
54 content.setAttribute("penwidth", ((QGraphicsRectItem*)item)->pen().width());
55 content.setAttribute("brushcolor", colorToString(((QGraphicsRectItem*)item)->brush().color()));
58 e.setAttribute("type", "QGraphicsTextItem");
59 content.appendChild(doc.createTextNode(((QGraphicsTextItem*)item)->toHtml()));
64 QDomElement pos = doc.createElement("position");
65 pos.setAttribute("x", item->pos().x());
66 pos.setAttribute("y", item->pos().y());
67 QTransform transform = item->transform();
68 QDomElement tr = doc.createElement("transform");
69 tr.appendChild(doc.createTextNode(
70 QString("%1,%2,%3,%4,%5,%6,%7,%8,%9").arg(
71 transform.m11()).arg(transform.m12()).arg(transform.m13()).arg(transform.m21()).arg(transform.m22()).arg(transform.m23()).arg(transform.m31()).arg(transform.m32()).arg(transform.m33())
74 e.setAttribute("z-index", item->zValue());
79 e.appendChild(content);
83 QDomElement endp = doc.createElement("endviewport");
84 QDomElement startp = doc.createElement("startviewport");
85 endp.setAttribute("x", endv->pos().x());
86 endp.setAttribute("y", endv->pos().y());
87 endp.setAttribute("size", endv->sceneBoundingRect().width() / 2);
89 startp.setAttribute("x", startv->pos().x());
90 startp.setAttribute("y", startv->pos().y());
91 startp.setAttribute("size", startv->sceneBoundingRect().width() / 2);
93 startp.setAttribute("z-index", startv->zValue());
94 endp.setAttribute("z-index", endv->zValue());
95 main.appendChild(startp);
96 main.appendChild(endp);
98 QDomElement backgr = doc.createElement("background");
99 backgr.setAttribute("color", colorToString(scene->backgroundBrush().color()));
100 main.appendChild(backgr);
102 QString tmpfile = "/tmp/newtitle";
104 xmlf.open(QIODevice::WriteOnly);
105 xmlf.write(doc.toString().toAscii());
107 kDebug() << KIO::NetAccess::upload(tmpfile, url, 0);
110 bool TitleDocument::loadDocument(const KUrl& url , QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
113 double aspect_ratio = 4.0 / 3.0;
117 if (KIO::NetAccess::download(url, tmpfile, 0)) {
119 if (file.open(QIODevice::ReadOnly)) {
120 doc.setContent(&file, false);
124 KIO::NetAccess::removeTempFile(tmpfile);
125 QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
128 QDomNodeList items = titles.item(0).childNodes();
129 for (int i = 0;i < items.count();i++) {
130 QGraphicsItem *gitem = NULL;
131 kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
132 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
133 QGraphicsTextItem *txt = scene->addText("");
134 txt->setHtml(items.item(i).namedItem("content").firstChild().nodeValue());
137 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
138 QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
139 QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
140 QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
141 double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
142 QGraphicsRectItem *rec = scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
147 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
148 items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
150 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
152 if (items.item(i).nodeName() == "background") {
153 kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
154 scene->setBackgroundBrush(QBrush(stringToColor(items.item(i).attributes().namedItem("color").nodeValue())));
155 } else if (items.item(i).nodeName() == "startviewport" && startv) {
156 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
157 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
158 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
159 kDebug() << width << rect;
160 startv->setPolygon(rect);
162 } else if (items.item(i).nodeName() == "endviewport" && endv) {
163 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
164 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
165 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
166 kDebug() << width << rect;
167 endv->setPolygon(rect);
181 QString TitleDocument::colorToString(const QColor& c) {
182 QString ret = "%1,%2,%3,%4";
183 ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
187 QString TitleDocument::rectFToString(const QRectF& c) {
188 QString ret = "%1,%2,%3,%4";
189 ret = ret.arg(c.x()).arg(c.y()).arg(c.width()).arg(c.height());
193 QRectF TitleDocument::stringToRect(const QString & s) {
195 QStringList l = s.split(",");
198 return QRectF(l[0].toDouble(), l[1].toDouble(), l[2].toDouble(), l[3].toDouble());
201 QColor TitleDocument::stringToColor(const QString & s) {
202 QStringList l = s.split(",");
205 return QColor(l[0].toInt(), l[1].toInt(), l[2].toInt(), l[3].toInt());;
207 QTransform TitleDocument::stringToTransform(const QString& s) {
208 QStringList l = s.split(",");
212 l[0].toDouble(), l[1].toDouble(), l[2].toDouble(),
213 l[3].toDouble(), l[4].toDouble(), l[5].toDouble(),
214 l[6].toDouble(), l[7].toDouble(), l[8].toDouble()