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>
24 #include <QGraphicsSvgItem>
27 #include <KTemporaryFile>
28 #include <kio/netaccess.h>
30 TitleDocument::TitleDocument() {
34 void TitleDocument::setScene(QGraphicsScene* _scene) {
38 bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
44 QDomElement main = doc.createElement("kdenlivetitle");
45 doc.appendChild(main);
47 foreach(QGraphicsItem* item, scene->items()) {
48 QDomElement e = doc.createElement("item");
49 QDomElement content = doc.createElement("content");
53 switch (item->type()) {
55 e.setAttribute("type", "QGraphicsPixmapItem");
56 content.setAttribute("url", item->data(Qt::UserRole).toString());
59 e.setAttribute("type", "QGraphicsSvgItem");
60 content.setAttribute("url", item->data(Qt::UserRole).toString());
63 e.setAttribute("type", "QGraphicsRectItem");
64 content.setAttribute("rect", rectFToString(((QGraphicsRectItem*)item)->rect()));
65 content.setAttribute("pencolor", colorToString(((QGraphicsRectItem*)item)->pen().color()));
66 content.setAttribute("penwidth", ((QGraphicsRectItem*)item)->pen().width());
67 content.setAttribute("brushcolor", colorToString(((QGraphicsRectItem*)item)->brush().color()));
70 e.setAttribute("type", "QGraphicsTextItem");
71 t = static_cast<QGraphicsTextItem *>(item);
72 //content.appendChild(doc.createTextNode(((QGraphicsTextItem*)item)->toHtml()));
73 content.appendChild(doc.createTextNode(t->toPlainText()));
75 content.setAttribute("font", font.family());
76 content.setAttribute("font-bold", font.bold());
77 content.setAttribute("font-size", font.pointSize());
78 content.setAttribute("font-italic", font.italic());
79 content.setAttribute("font-underline", font.underline());
80 content.setAttribute("font-color", colorToString(t->defaultTextColor()));
85 QDomElement pos = doc.createElement("position");
86 pos.setAttribute("x", item->pos().x());
87 pos.setAttribute("y", item->pos().y());
88 QTransform transform = item->transform();
89 QDomElement tr = doc.createElement("transform");
90 tr.appendChild(doc.createTextNode(
91 QString("%1,%2,%3,%4,%5,%6,%7,%8,%9").arg(
92 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())
95 e.setAttribute("z-index", item->zValue());
100 e.appendChild(content);
101 if (item->zValue() > -1100) main.appendChild(e);
103 if (startv && endv) {
104 QDomElement endp = doc.createElement("endviewport");
105 QDomElement startp = doc.createElement("startviewport");
106 endp.setAttribute("x", endv->pos().x());
107 endp.setAttribute("y", endv->pos().y());
108 endp.setAttribute("size", endv->sceneBoundingRect().width() / 2);
110 startp.setAttribute("x", startv->pos().x());
111 startp.setAttribute("y", startv->pos().y());
112 startp.setAttribute("size", startv->sceneBoundingRect().width() / 2);
114 startp.setAttribute("z-index", startv->zValue());
115 endp.setAttribute("z-index", endv->zValue());
116 main.appendChild(startp);
117 main.appendChild(endp);
119 QDomElement backgr = doc.createElement("background");
120 QList<QGraphicsItem *> items = scene->items();
121 QColor color(0, 0, 0, 0);
122 for (int i = 0; i < items.size(); i++) {
123 if (items.at(i)->zValue() == -1100) {
124 color = ((QGraphicsRectItem *)items.at(i))->brush().color();
128 backgr.setAttribute("color", colorToString(color));
129 main.appendChild(backgr);
131 KTemporaryFile tmpfile;
132 if (!tmpfile.open()) kWarning() << "///// CANNOT CREATE TMP FILE in: " << tmpfile.fileName();
133 QFile xmlf(tmpfile.fileName());
134 xmlf.open(QIODevice::WriteOnly);
135 xmlf.write(doc.toString().toAscii());
137 kDebug() << KIO::NetAccess::upload(tmpfile.fileName(), url, 0);
141 int TitleDocument::loadDocument(const KUrl& url , QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
145 double aspect_ratio = 4.0 / 3.0;
149 if (KIO::NetAccess::download(url, tmpfile, 0)) {
151 if (file.open(QIODevice::ReadOnly)) {
152 doc.setContent(&file, false);
156 KIO::NetAccess::removeTempFile(tmpfile);
157 QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
160 QDomNodeList items = titles.item(0).childNodes();
161 for (int i = 0;i < items.count();i++) {
162 QGraphicsItem *gitem = NULL;
163 kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
164 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
166 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
167 QFont font(items.item(i).namedItem("content").attributes().namedItem("font").nodeValue());
168 font.setBold(items.item(i).namedItem("content").attributes().namedItem("font-bold").nodeValue().toInt());
169 font.setItalic(items.item(i).namedItem("content").attributes().namedItem("font-italic").nodeValue().toInt());
170 font.setUnderline(items.item(i).namedItem("content").attributes().namedItem("font-underline").nodeValue().toInt());
171 font.setPointSize(items.item(i).namedItem("content").attributes().namedItem("font-size").nodeValue().toInt());
172 QColor col(stringToColor(items.item(i).namedItem("content").attributes().namedItem("font-color").nodeValue()));
173 QGraphicsTextItem *txt = scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
174 txt->setDefaultTextColor(col);
175 txt->setTextInteractionFlags(Qt::NoTextInteraction);
178 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
179 QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
180 QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
181 QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
182 double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
183 QGraphicsRectItem *rec = scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
186 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
187 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
189 QGraphicsPixmapItem *rec = scene->addPixmap(pix);
190 rec->setData(Qt::UserRole, url);
193 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
194 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
195 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
197 rec->setData(Qt::UserRole, url);
202 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
203 items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
205 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
206 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
207 if (zValue > maxZValue) maxZValue = zValue;
208 gitem->setZValue(zValue);
209 gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
211 if (items.item(i).nodeName() == "background") {
212 kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
213 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
214 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
215 QList<QGraphicsItem *> items = scene->items();
216 for (int i = 0; i < items.size(); i++) {
217 if (items.at(i)->zValue() == -1100) {
218 ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
222 } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
223 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
224 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
225 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
226 kDebug() << width << rect;
227 startv->setPolygon(rect);
229 } else if (items.item(i).nodeName() == "endviewport" && endv) {
230 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
231 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
232 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
233 kDebug() << width << rect;
234 endv->setPolygon(rect);
243 QString TitleDocument::colorToString(const QColor& c) {
244 QString ret = "%1,%2,%3,%4";
245 ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
249 QString TitleDocument::rectFToString(const QRectF& c) {
250 QString ret = "%1,%2,%3,%4";
251 ret = ret.arg(c.x()).arg(c.y()).arg(c.width()).arg(c.height());
255 QRectF TitleDocument::stringToRect(const QString & s) {
257 QStringList l = s.split(",");
260 return QRectF(l[0].toDouble(), l[1].toDouble(), l[2].toDouble(), l[3].toDouble());
263 QColor TitleDocument::stringToColor(const QString & s) {
264 QStringList l = s.split(",");
267 return QColor(l[0].toInt(), l[1].toInt(), l[2].toInt(), l[3].toInt());;
269 QTransform TitleDocument::stringToTransform(const QString& s) {
270 QStringList l = s.split(",");
274 l[0].toDouble(), l[1].toDouble(), l[2].toDouble(),
275 l[3].toDouble(), l[4].toDouble(), l[5].toDouble(),
276 l[6].toDouble(), l[7].toDouble(), l[8].toDouble()