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 ***************************************************************************/
18 #include "titledocument.h"
21 #include <KTemporaryFile>
22 #include <kio/netaccess.h>
24 #include <QGraphicsScene>
25 #include <QDomElement>
26 #include <QGraphicsItem>
27 #include <QGraphicsRectItem>
28 #include <QGraphicsTextItem>
29 #include <QGraphicsSvgItem>
34 TitleDocument::TitleDocument() {
38 void TitleDocument::setScene(QGraphicsScene* _scene) {
42 QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
45 QDomElement main = doc.createElement("kdenlivetitle");
46 doc.appendChild(main);
48 foreach(QGraphicsItem* item, scene->items()) {
49 QDomElement e = doc.createElement("item");
50 QDomElement content = doc.createElement("content");
54 switch (item->type()) {
56 e.setAttribute("type", "QGraphicsPixmapItem");
57 content.setAttribute("url", item->data(Qt::UserRole).toString());
60 e.setAttribute("type", "QGraphicsSvgItem");
61 content.setAttribute("url", item->data(Qt::UserRole).toString());
64 e.setAttribute("type", "QGraphicsRectItem");
65 content.setAttribute("rect", rectFToString(((QGraphicsRectItem*)item)->rect()));
66 content.setAttribute("pencolor", colorToString(((QGraphicsRectItem*)item)->pen().color()));
67 content.setAttribute("penwidth", ((QGraphicsRectItem*)item)->pen().width());
68 content.setAttribute("brushcolor", colorToString(((QGraphicsRectItem*)item)->brush().color()));
71 e.setAttribute("type", "QGraphicsTextItem");
72 t = static_cast<QGraphicsTextItem *>(item);
73 // Don't save empty text nodes
74 if (t->toPlainText().simplified().isEmpty()) continue;
75 //content.appendChild(doc.createTextNode(((QGraphicsTextItem*)item)->toHtml()));
76 content.appendChild(doc.createTextNode(t->toPlainText()));
78 content.setAttribute("font", font.family());
79 content.setAttribute("font-bold", font.bold());
80 content.setAttribute("font-pixel-size", font.pixelSize());
81 content.setAttribute("font-italic", font.italic());
82 content.setAttribute("font-underline", font.underline());
83 content.setAttribute("font-color", colorToString(t->defaultTextColor()));
88 QDomElement pos = doc.createElement("position");
89 pos.setAttribute("x", item->pos().x());
90 pos.setAttribute("y", item->pos().y());
91 QTransform transform = item->transform();
92 QDomElement tr = doc.createElement("transform");
93 tr.appendChild(doc.createTextNode(
94 QString("%1,%2,%3,%4,%5,%6,%7,%8,%9").arg(
95 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())
98 e.setAttribute("z-index", item->zValue());
103 e.appendChild(content);
104 if (item->zValue() > -1100) main.appendChild(e);
106 if (startv && endv) {
107 QDomElement endp = doc.createElement("endviewport");
108 QDomElement startp = doc.createElement("startviewport");
109 endp.setAttribute("x", endv->pos().x());
110 endp.setAttribute("y", endv->pos().y());
111 endp.setAttribute("size", endv->sceneBoundingRect().width() / 2);
113 startp.setAttribute("x", startv->pos().x());
114 startp.setAttribute("y", startv->pos().y());
115 startp.setAttribute("size", startv->sceneBoundingRect().width() / 2);
117 startp.setAttribute("z-index", startv->zValue());
118 endp.setAttribute("z-index", endv->zValue());
119 main.appendChild(startp);
120 main.appendChild(endp);
122 QDomElement backgr = doc.createElement("background");
123 QColor color = getBackgroundColor();
124 backgr.setAttribute("color", colorToString(color));
125 main.appendChild(backgr);
130 /** \brief Get the background color (incl. alpha) from the document, if possibly
131 * \returns The background color of the document, inclusive alpha. If none found, returns (0,0,0,0) */
132 QColor TitleDocument::getBackgroundColor() {
133 QColor color(0, 0, 0, 0);
135 QList<QGraphicsItem *> items = scene->items();
136 for (int i = 0; i < items.size(); i++) {
137 if (items.at(i)->zValue() == -1100) {
138 color = ((QGraphicsRectItem *)items.at(i))->brush().color();
147 bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
151 QDomDocument doc = xml(startv, endv);
152 KTemporaryFile tmpfile;
153 if (!tmpfile.open()) {
154 kWarning() << "///// CANNOT CREATE TMP FILE in: " << tmpfile.fileName();
157 QFile xmlf(tmpfile.fileName());
158 xmlf.open(QIODevice::WriteOnly);
159 xmlf.write(doc.toString().toUtf8());
160 if (xmlf.error() != QFile::NoError) {
165 return KIO::NetAccess::upload(tmpfile.fileName(), url, 0);
168 int TitleDocument::loadDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
174 if (KIO::NetAccess::download(url, tmpfile, 0)) {
176 if (file.open(QIODevice::ReadOnly)) {
177 doc.setContent(&file, false);
181 KIO::NetAccess::removeTempFile(tmpfile);
182 return loadFromXml(doc, startv, endv);
187 int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* /*startv*/, QGraphicsPolygonItem* /*endv*/) {
188 QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
192 QDomNodeList items = titles.item(0).childNodes();
193 for (int i = 0;i < items.count();i++) {
194 QGraphicsItem *gitem = NULL;
195 kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
196 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
198 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
199 QDomNamedNodeMap txtProperties = items.item(i).namedItem("content").attributes();
200 QFont font(txtProperties.namedItem("font").nodeValue());
201 font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
202 font.setItalic(txtProperties.namedItem("font-italic").nodeValue().toInt());
203 font.setUnderline(txtProperties.namedItem("font-underline").nodeValue().toInt());
204 // Older Kdenlive version did not store pixel size but point size
205 if (txtProperties.namedItem("font-pixel-size").isNull()) {
207 f2.setPointSize(txtProperties.namedItem("font-size").nodeValue().toInt());
208 font.setPixelSize(QFontInfo(f2).pixelSize());
209 } else font.setPixelSize(txtProperties.namedItem("font-pixel-size").nodeValue().toInt());
210 QColor col(stringToColor(txtProperties.namedItem("font-color").nodeValue()));
211 QGraphicsTextItem *txt = scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
212 txt->setDefaultTextColor(col);
213 txt->setTextInteractionFlags(Qt::NoTextInteraction);
216 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
217 QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
218 QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
219 QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
220 double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
221 QGraphicsRectItem *rec = scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
224 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
225 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
227 QGraphicsPixmapItem *rec = scene->addPixmap(pix);
228 rec->setData(Qt::UserRole, url);
231 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
232 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
233 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
235 rec->setData(Qt::UserRole, url);
240 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
241 items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
243 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
244 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
245 if (zValue > maxZValue) maxZValue = zValue;
246 gitem->setZValue(zValue);
247 gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
249 if (items.item(i).nodeName() == "background") {
250 kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
251 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
252 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
253 QList<QGraphicsItem *> items = scene->items();
254 for (int i = 0; i < items.size(); i++) {
255 if (items.at(i)->zValue() == -1100) {
256 ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
260 } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
261 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
262 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
263 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
264 kDebug() << width << rect;
265 startv->setPolygon(rect);
267 } else if (items.item(i).nodeName() == "endviewport" && endv) {
268 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
269 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
270 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
271 kDebug() << width << rect;
272 endv->setPolygon(rect);
280 QString TitleDocument::colorToString(const QColor& c) {
281 QString ret = "%1,%2,%3,%4";
282 ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
286 QString TitleDocument::rectFToString(const QRectF& c) {
287 QString ret = "%1,%2,%3,%4";
288 ret = ret.arg(c.x()).arg(c.y()).arg(c.width()).arg(c.height());
292 QRectF TitleDocument::stringToRect(const QString & s) {
294 QStringList l = s.split(',');
297 return QRectF(l[0].toDouble(), l[1].toDouble(), l[2].toDouble(), l[3].toDouble());
300 QColor TitleDocument::stringToColor(const QString & s) {
301 QStringList l = s.split(',');
304 return QColor(l[0].toInt(), l[1].toInt(), l[2].toInt(), l[3].toInt());;
306 QTransform TitleDocument::stringToTransform(const QString& s) {
307 QStringList l = s.split(',');
311 l[0].toDouble(), l[1].toDouble(), l[2].toDouble(),
312 l[3].toDouble(), l[4].toDouble(), l[5].toDouble(),
313 l[6].toDouble(), l[7].toDouble(), l[8].toDouble()