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()) kWarning() << "///// CANNOT CREATE TMP FILE in: " << tmpfile.fileName();
154 QFile xmlf(tmpfile.fileName());
155 xmlf.open(QIODevice::WriteOnly);
156 xmlf.write(doc.toString().toUtf8());
158 kDebug() << KIO::NetAccess::upload(tmpfile.fileName(), url, 0);
162 int TitleDocument::loadDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
168 if (KIO::NetAccess::download(url, tmpfile, 0)) {
170 if (file.open(QIODevice::ReadOnly)) {
171 doc.setContent(&file, false);
175 KIO::NetAccess::removeTempFile(tmpfile);
176 return loadFromXml(doc, startv, endv);
181 int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* /*startv*/, QGraphicsPolygonItem* /*endv*/) {
182 QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
186 QDomNodeList items = titles.item(0).childNodes();
187 for (int i = 0;i < items.count();i++) {
188 QGraphicsItem *gitem = NULL;
189 kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
190 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
192 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
193 QDomNamedNodeMap txtProperties = items.item(i).namedItem("content").attributes();
194 QFont font(txtProperties.namedItem("font").nodeValue());
195 font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
196 font.setItalic(txtProperties.namedItem("font-italic").nodeValue().toInt());
197 font.setUnderline(txtProperties.namedItem("font-underline").nodeValue().toInt());
198 // Older Kdenlive version did not store pixel size but point size
199 if (txtProperties.namedItem("font-pixel-size").isNull()) {
201 f2.setPointSize(txtProperties.namedItem("font-size").nodeValue().toInt());
202 font.setPixelSize(QFontInfo(f2).pixelSize());
204 else font.setPixelSize(txtProperties.namedItem("font-pixel-size").nodeValue().toInt());
205 QColor col(stringToColor(txtProperties.namedItem("font-color").nodeValue()));
206 QGraphicsTextItem *txt = scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
207 txt->setDefaultTextColor(col);
208 txt->setTextInteractionFlags(Qt::NoTextInteraction);
211 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
212 QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
213 QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
214 QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
215 double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
216 QGraphicsRectItem *rec = scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
219 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
220 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
222 QGraphicsPixmapItem *rec = scene->addPixmap(pix);
223 rec->setData(Qt::UserRole, url);
226 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
227 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
228 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
230 rec->setData(Qt::UserRole, url);
235 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
236 items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
238 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
239 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
240 if (zValue > maxZValue) maxZValue = zValue;
241 gitem->setZValue(zValue);
242 gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
244 if (items.item(i).nodeName() == "background") {
245 kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
246 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
247 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
248 QList<QGraphicsItem *> items = scene->items();
249 for (int i = 0; i < items.size(); i++) {
250 if (items.at(i)->zValue() == -1100) {
251 ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
255 } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
256 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
257 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
258 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
259 kDebug() << width << rect;
260 startv->setPolygon(rect);
262 } else if (items.item(i).nodeName() == "endviewport" && endv) {
263 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
264 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
265 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
266 kDebug() << width << rect;
267 endv->setPolygon(rect);
275 QString TitleDocument::colorToString(const QColor& c) {
276 QString ret = "%1,%2,%3,%4";
277 ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
281 QString TitleDocument::rectFToString(const QRectF& c) {
282 QString ret = "%1,%2,%3,%4";
283 ret = ret.arg(c.x()).arg(c.y()).arg(c.width()).arg(c.height());
287 QRectF TitleDocument::stringToRect(const QString & s) {
289 QStringList l = s.split(',');
292 return QRectF(l[0].toDouble(), l[1].toDouble(), l[2].toDouble(), l[3].toDouble());
295 QColor TitleDocument::stringToColor(const QString & s) {
296 QStringList l = s.split(',');
299 return QColor(l[0].toInt(), l[1].toInt(), l[2].toInt(), l[3].toInt());;
301 QTransform TitleDocument::stringToTransform(const QString& s) {
302 QStringList l = s.split(',');
306 l[0].toDouble(), l[1].toDouble(), l[2].toDouble(),
307 l[3].toDouble(), l[4].toDouble(), l[5].toDouble(),
308 l[6].toDouble(), l[7].toDouble(), l[8].toDouble()