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 <KTemporaryFile>
27 #include <kio/netaccess.h>
29 TitleDocument::TitleDocument() {
33 void TitleDocument::setScene(QGraphicsScene* _scene) {
37 bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
43 QDomElement main = doc.createElement("kdenlivetitle");
44 doc.appendChild(main);
46 foreach(QGraphicsItem* item, scene->items()) {
47 QDomElement e = doc.createElement("item");
48 QDomElement content = doc.createElement("content");
52 switch (item->type()) {
54 e.setAttribute("type", "QGraphicsRectItem");
55 content.setAttribute("rect", rectFToString(((QGraphicsRectItem*)item)->rect()));
56 content.setAttribute("pencolor", colorToString(((QGraphicsRectItem*)item)->pen().color()));
57 content.setAttribute("penwidth", ((QGraphicsRectItem*)item)->pen().width());
58 content.setAttribute("brushcolor", colorToString(((QGraphicsRectItem*)item)->brush().color()));
61 e.setAttribute("type", "QGraphicsTextItem");
62 t = static_cast<QGraphicsTextItem *>(item);
63 //content.appendChild(doc.createTextNode(((QGraphicsTextItem*)item)->toHtml()));
64 content.appendChild(doc.createTextNode(t->toPlainText()));
66 e.setAttribute("font", font.family());
67 e.setAttribute("font-bold", font.bold());
68 e.setAttribute("font-size", font.pointSize());
69 e.setAttribute("font-italic", font.italic());
70 e.setAttribute("font-underline", font.underline());
71 e.setAttribute("font-color", colorToString(t->defaultTextColor()));
76 QDomElement pos = doc.createElement("position");
77 pos.setAttribute("x", item->pos().x());
78 pos.setAttribute("y", item->pos().y());
79 QTransform transform = item->transform();
80 QDomElement tr = doc.createElement("transform");
81 tr.appendChild(doc.createTextNode(
82 QString("%1,%2,%3,%4,%5,%6,%7,%8,%9").arg(
83 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())
86 e.setAttribute("z-index", item->zValue());
91 e.appendChild(content);
92 if (item->zValue() > -1100) main.appendChild(e);
95 QDomElement endp = doc.createElement("endviewport");
96 QDomElement startp = doc.createElement("startviewport");
97 endp.setAttribute("x", endv->pos().x());
98 endp.setAttribute("y", endv->pos().y());
99 endp.setAttribute("size", endv->sceneBoundingRect().width() / 2);
101 startp.setAttribute("x", startv->pos().x());
102 startp.setAttribute("y", startv->pos().y());
103 startp.setAttribute("size", startv->sceneBoundingRect().width() / 2);
105 startp.setAttribute("z-index", startv->zValue());
106 endp.setAttribute("z-index", endv->zValue());
107 main.appendChild(startp);
108 main.appendChild(endp);
110 QDomElement backgr = doc.createElement("background");
111 QList<QGraphicsItem *> items = scene->items();
112 QColor color(0, 0, 0, 0);
113 for (int i = 0; i < items.size(); i++) {
114 if (items.at(i)->zValue() == -1100) {
115 color = ((QGraphicsRectItem *)items.at(i))->brush().color();
119 backgr.setAttribute("color", colorToString(color));
120 main.appendChild(backgr);
122 KTemporaryFile tmpfile;
123 if (!tmpfile.open()) kWarning() << "///// CANNOT CREATE TMP FILE in: " << tmpfile.fileName();
124 QFile xmlf(tmpfile.fileName());
125 xmlf.open(QIODevice::WriteOnly);
126 xmlf.write(doc.toString().toAscii());
128 kDebug() << KIO::NetAccess::upload(tmpfile.fileName(), url, 0);
132 int TitleDocument::loadDocument(const KUrl& url , QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
136 double aspect_ratio = 4.0 / 3.0;
140 if (KIO::NetAccess::download(url, tmpfile, 0)) {
142 if (file.open(QIODevice::ReadOnly)) {
143 doc.setContent(&file, false);
147 KIO::NetAccess::removeTempFile(tmpfile);
148 QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
151 QDomNodeList items = titles.item(0).childNodes();
152 for (int i = 0;i < items.count();i++) {
153 QGraphicsItem *gitem = NULL;
154 kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
155 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
157 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
158 QFont font(items.item(i).attributes().namedItem("font").nodeValue());
159 font.setBold(items.item(i).attributes().namedItem("font-bold").nodeValue().toInt());
160 font.setItalic(items.item(i).attributes().namedItem("font-italic").nodeValue().toInt());
161 font.setUnderline(items.item(i).attributes().namedItem("font-underline").nodeValue().toInt());
162 font.setPointSize(items.item(i).attributes().namedItem("font-size").nodeValue().toInt());
163 QColor col(stringToColor(items.item(i).attributes().namedItem("font-color").nodeValue()));
164 QGraphicsTextItem *txt = scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
165 txt->setDefaultTextColor(col);
166 txt->setTextInteractionFlags(Qt::NoTextInteraction);
169 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
170 QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
171 QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
172 QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
173 double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
174 QGraphicsRectItem *rec = scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
179 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
180 items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
182 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
183 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
184 if (zValue > maxZValue) maxZValue = zValue;
185 gitem->setZValue(zValue);
186 gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
188 if (items.item(i).nodeName() == "background") {
189 kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
190 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
191 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
192 QList<QGraphicsItem *> items = scene->items();
193 for (int i = 0; i < items.size(); i++) {
194 if (items.at(i)->zValue() == -1100) {
195 ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
199 } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
200 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
201 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
202 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
203 kDebug() << width << rect;
204 startv->setPolygon(rect);
206 } else if (items.item(i).nodeName() == "endviewport" && endv) {
207 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
208 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
209 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
210 kDebug() << width << rect;
211 endv->setPolygon(rect);
220 QString TitleDocument::colorToString(const QColor& c) {
221 QString ret = "%1,%2,%3,%4";
222 ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
226 QString TitleDocument::rectFToString(const QRectF& c) {
227 QString ret = "%1,%2,%3,%4";
228 ret = ret.arg(c.x()).arg(c.y()).arg(c.width()).arg(c.height());
232 QRectF TitleDocument::stringToRect(const QString & s) {
234 QStringList l = s.split(",");
237 return QRectF(l[0].toDouble(), l[1].toDouble(), l[2].toDouble(), l[3].toDouble());
240 QColor TitleDocument::stringToColor(const QString & s) {
241 QStringList l = s.split(",");
244 return QColor(l[0].toInt(), l[1].toInt(), l[2].toInt(), l[3].toInt());;
246 QTransform TitleDocument::stringToTransform(const QString& s) {
247 QStringList l = s.split(",");
251 l[0].toDouble(), l[1].toDouble(), l[2].toDouble(),
252 l[3].toDouble(), l[4].toDouble(), l[5].toDouble(),
253 l[6].toDouble(), l[7].toDouble(), l[8].toDouble()