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 <QDomElement>
20 #include <QGraphicsItem>
21 #include <QGraphicsRectItem>
22 #include <QGraphicsTextItem>
23 #include <QGraphicsSvgItem>
26 #include <KTemporaryFile>
27 #include <kio/netaccess.h>
29 TitleDocument::TitleDocument() {
33 void TitleDocument::setScene(QGraphicsScene* _scene) {
37 QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
40 QDomElement main = doc.createElement("kdenlivetitle");
41 doc.appendChild(main);
43 foreach(QGraphicsItem* item, scene->items()) {
44 QDomElement e = doc.createElement("item");
45 QDomElement content = doc.createElement("content");
49 switch (item->type()) {
51 e.setAttribute("type", "QGraphicsPixmapItem");
52 content.setAttribute("url", item->data(Qt::UserRole).toString());
55 e.setAttribute("type", "QGraphicsSvgItem");
56 content.setAttribute("url", item->data(Qt::UserRole).toString());
59 e.setAttribute("type", "QGraphicsRectItem");
60 content.setAttribute("rect", rectFToString(((QGraphicsRectItem*)item)->rect()));
61 content.setAttribute("pencolor", colorToString(((QGraphicsRectItem*)item)->pen().color()));
62 content.setAttribute("penwidth", ((QGraphicsRectItem*)item)->pen().width());
63 content.setAttribute("brushcolor", colorToString(((QGraphicsRectItem*)item)->brush().color()));
66 e.setAttribute("type", "QGraphicsTextItem");
67 t = static_cast<QGraphicsTextItem *>(item);
68 //content.appendChild(doc.createTextNode(((QGraphicsTextItem*)item)->toHtml()));
69 content.appendChild(doc.createTextNode(t->toPlainText()));
71 content.setAttribute("font", font.family());
72 content.setAttribute("font-bold", font.bold());
73 content.setAttribute("font-size", font.pointSize());
74 content.setAttribute("font-italic", font.italic());
75 content.setAttribute("font-underline", font.underline());
76 content.setAttribute("font-color", colorToString(t->defaultTextColor()));
81 QDomElement pos = doc.createElement("position");
82 pos.setAttribute("x", item->pos().x());
83 pos.setAttribute("y", item->pos().y());
84 QTransform transform = item->transform();
85 QDomElement tr = doc.createElement("transform");
86 tr.appendChild(doc.createTextNode(
87 QString("%1,%2,%3,%4,%5,%6,%7,%8,%9").arg(
88 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())
91 e.setAttribute("z-index", item->zValue());
96 e.appendChild(content);
97 if (item->zValue() > -1100) main.appendChild(e);
100 QDomElement endp = doc.createElement("endviewport");
101 QDomElement startp = doc.createElement("startviewport");
102 endp.setAttribute("x", endv->pos().x());
103 endp.setAttribute("y", endv->pos().y());
104 endp.setAttribute("size", endv->sceneBoundingRect().width() / 2);
106 startp.setAttribute("x", startv->pos().x());
107 startp.setAttribute("y", startv->pos().y());
108 startp.setAttribute("size", startv->sceneBoundingRect().width() / 2);
110 startp.setAttribute("z-index", startv->zValue());
111 endp.setAttribute("z-index", endv->zValue());
112 main.appendChild(startp);
113 main.appendChild(endp);
115 QDomElement backgr = doc.createElement("background");
116 QList<QGraphicsItem *> items = scene->items();
117 QColor color(0, 0, 0, 0);
118 for (int i = 0; i < items.size(); i++) {
119 if (items.at(i)->zValue() == -1100) {
120 color = ((QGraphicsRectItem *)items.at(i))->brush().color();
124 backgr.setAttribute("color", colorToString(color));
125 main.appendChild(backgr);
131 bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
135 QDomDocument doc = xml(startv, endv);
136 KTemporaryFile tmpfile;
137 if (!tmpfile.open()) kWarning() << "///// CANNOT CREATE TMP FILE in: " << tmpfile.fileName();
138 QFile xmlf(tmpfile.fileName());
139 xmlf.open(QIODevice::WriteOnly);
140 xmlf.write(doc.toString().toAscii());
142 kDebug() << KIO::NetAccess::upload(tmpfile.fileName(), url, 0);
146 int TitleDocument::loadDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
149 double aspect_ratio = 4.0 / 3.0;
153 if (KIO::NetAccess::download(url, tmpfile, 0)) {
155 if (file.open(QIODevice::ReadOnly)) {
156 doc.setContent(&file, false);
160 KIO::NetAccess::removeTempFile(tmpfile);
161 return loadFromXml(doc, startv, endv);
165 int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
166 QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
170 QDomNodeList items = titles.item(0).childNodes();
171 for (int i = 0;i < items.count();i++) {
172 QGraphicsItem *gitem = NULL;
173 kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
174 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
176 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
177 QFont font(items.item(i).namedItem("content").attributes().namedItem("font").nodeValue());
178 font.setBold(items.item(i).namedItem("content").attributes().namedItem("font-bold").nodeValue().toInt());
179 font.setItalic(items.item(i).namedItem("content").attributes().namedItem("font-italic").nodeValue().toInt());
180 font.setUnderline(items.item(i).namedItem("content").attributes().namedItem("font-underline").nodeValue().toInt());
181 font.setPointSize(items.item(i).namedItem("content").attributes().namedItem("font-size").nodeValue().toInt());
182 QColor col(stringToColor(items.item(i).namedItem("content").attributes().namedItem("font-color").nodeValue()));
183 QGraphicsTextItem *txt = scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
184 txt->setDefaultTextColor(col);
185 txt->setTextInteractionFlags(Qt::NoTextInteraction);
188 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
189 QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
190 QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
191 QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
192 double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
193 QGraphicsRectItem *rec = scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
196 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
197 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
199 QGraphicsPixmapItem *rec = scene->addPixmap(pix);
200 rec->setData(Qt::UserRole, url);
203 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
204 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
205 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
207 rec->setData(Qt::UserRole, url);
212 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
213 items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
215 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
216 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
217 if (zValue > maxZValue) maxZValue = zValue;
218 gitem->setZValue(zValue);
219 gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
221 if (items.item(i).nodeName() == "background") {
222 kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
223 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
224 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
225 QList<QGraphicsItem *> items = scene->items();
226 for (int i = 0; i < items.size(); i++) {
227 if (items.at(i)->zValue() == -1100) {
228 ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
232 } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
233 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
234 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
235 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
236 kDebug() << width << rect;
237 startv->setPolygon(rect);
239 } else if (items.item(i).nodeName() == "endviewport" && endv) {
240 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
241 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
242 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
243 kDebug() << width << rect;
244 endv->setPolygon(rect);
252 QString TitleDocument::colorToString(const QColor& c) {
253 QString ret = "%1,%2,%3,%4";
254 ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
258 QString TitleDocument::rectFToString(const QRectF& c) {
259 QString ret = "%1,%2,%3,%4";
260 ret = ret.arg(c.x()).arg(c.y()).arg(c.width()).arg(c.height());
264 QRectF TitleDocument::stringToRect(const QString & s) {
266 QStringList l = s.split(",");
269 return QRectF(l[0].toDouble(), l[1].toDouble(), l[2].toDouble(), l[3].toDouble());
272 QColor TitleDocument::stringToColor(const QString & s) {
273 QStringList l = s.split(",");
276 return QColor(l[0].toInt(), l[1].toInt(), l[2].toInt(), l[3].toInt());;
278 QTransform TitleDocument::stringToTransform(const QString& s) {
279 QStringList l = s.split(",");
283 l[0].toDouble(), l[1].toDouble(), l[2].toDouble(),
284 l[3].toDouble(), l[4].toDouble(), l[5].toDouble(),
285 l[6].toDouble(), l[7].toDouble(), l[8].toDouble()