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>
32 #include <QTextCursor>
35 TitleDocument::TitleDocument()
40 void TitleDocument::setScene(QGraphicsScene* _scene)
45 QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
49 QDomElement main = doc.createElement("kdenlivetitle");
50 doc.appendChild(main);
52 foreach(QGraphicsItem* item, m_scene->items()) {
53 QDomElement e = doc.createElement("item");
54 QDomElement content = doc.createElement("content");
58 switch (item->type()) {
60 e.setAttribute("type", "QGraphicsPixmapItem");
61 content.setAttribute("url", item->data(Qt::UserRole).toString());
64 e.setAttribute("type", "QGraphicsSvgItem");
65 content.setAttribute("url", item->data(Qt::UserRole).toString());
68 e.setAttribute("type", "QGraphicsRectItem");
69 content.setAttribute("rect", rectFToString(((QGraphicsRectItem*)item)->rect().normalized()));
70 content.setAttribute("pencolor", colorToString(((QGraphicsRectItem*)item)->pen().color()));
71 content.setAttribute("penwidth", ((QGraphicsRectItem*)item)->pen().width());
72 content.setAttribute("brushcolor", colorToString(((QGraphicsRectItem*)item)->brush().color()));
75 e.setAttribute("type", "QGraphicsTextItem");
76 t = static_cast<QGraphicsTextItem *>(item);
77 // Don't save empty text nodes
78 if (t->toPlainText().simplified().isEmpty()) continue;
79 //content.appendChild(doc.createTextNode(((QGraphicsTextItem*)item)->toHtml()));
80 content.appendChild(doc.createTextNode(t->toPlainText()));
82 content.setAttribute("font", font.family());
83 content.setAttribute("font-bold", font.bold());
84 content.setAttribute("font-pixel-size", font.pixelSize());
85 content.setAttribute("font-italic", font.italic());
86 content.setAttribute("font-underline", font.underline());
87 content.setAttribute("font-color", colorToString(t->defaultTextColor()));
89 // Only save when necessary.
90 if (t->data(OriginXLeft).toInt() == AxisInverted) {
91 content.setAttribute("kdenlive-axis-x-inverted", t->data(OriginXLeft).toInt());
93 if (t->data(OriginYTop).toInt() == AxisInverted) {
94 content.setAttribute("kdenlive-axis-y-inverted", t->data(OriginYTop).toInt());
96 if (t->textWidth() != -1) {
97 content.setAttribute("alignment", t->textCursor().blockFormat().alignment());
103 QDomElement pos = doc.createElement("position");
104 pos.setAttribute("x", item->pos().x());
105 pos.setAttribute("y", item->pos().y());
106 QTransform transform = item->transform();
107 QDomElement tr = doc.createElement("transform");
108 tr.appendChild(doc.createTextNode(
109 QString("%1,%2,%3,%4,%5,%6,%7,%8,%9").arg(
110 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())
113 e.setAttribute("z-index", item->zValue());
118 e.appendChild(content);
119 if (item->zValue() > -1100) main.appendChild(e);
121 if (startv && endv) {
122 QDomElement endp = doc.createElement("endviewport");
123 QDomElement startp = doc.createElement("startviewport");
124 endp.setAttribute("x", endv->pos().x());
125 endp.setAttribute("y", endv->pos().y());
126 endp.setAttribute("size", endv->sceneBoundingRect().width() / 2);
128 startp.setAttribute("x", startv->pos().x());
129 startp.setAttribute("y", startv->pos().y());
130 startp.setAttribute("size", startv->sceneBoundingRect().width() / 2);
132 startp.setAttribute("z-index", startv->zValue());
133 endp.setAttribute("z-index", endv->zValue());
134 main.appendChild(startp);
135 main.appendChild(endp);
137 QDomElement backgr = doc.createElement("background");
138 QColor color = getBackgroundColor();
139 backgr.setAttribute("color", colorToString(color));
140 main.appendChild(backgr);
145 /** \brief Get the background color (incl. alpha) from the document, if possibly
146 * \returns The background color of the document, inclusive alpha. If none found, returns (0,0,0,0) */
147 QColor TitleDocument::getBackgroundColor()
149 QColor color(0, 0, 0, 0);
151 QList<QGraphicsItem *> items = m_scene->items();
152 for (int i = 0; i < items.size(); i++) {
153 if (items.at(i)->zValue() == -1100) {
154 color = ((QGraphicsRectItem *)items.at(i))->brush().color();
163 bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
168 QDomDocument doc = xml(startv, endv);
169 KTemporaryFile tmpfile;
170 if (!tmpfile.open()) {
171 kWarning() << "///// CANNOT CREATE TMP FILE in: " << tmpfile.fileName();
174 QFile xmlf(tmpfile.fileName());
175 xmlf.open(QIODevice::WriteOnly);
176 xmlf.write(doc.toString().toUtf8());
177 if (xmlf.error() != QFile::NoError) {
182 return KIO::NetAccess::upload(tmpfile.fileName(), url, 0);
185 int TitleDocument::loadDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
192 if (KIO::NetAccess::download(url, tmpfile, 0)) {
194 if (file.open(QIODevice::ReadOnly)) {
195 doc.setContent(&file, false);
199 KIO::NetAccess::removeTempFile(tmpfile);
200 return loadFromXml(doc, startv, endv);
205 int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* /*startv*/, QGraphicsPolygonItem* /*endv*/)
207 QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
211 QDomNodeList items = titles.item(0).childNodes();
212 for (int i = 0;i < items.count();i++) {
213 QGraphicsItem *gitem = NULL;
214 kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
215 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
216 if (zValue > -1000) {
217 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
218 QDomNamedNodeMap txtProperties = items.item(i).namedItem("content").attributes();
219 QFont font(txtProperties.namedItem("font").nodeValue());
220 font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
221 font.setItalic(txtProperties.namedItem("font-italic").nodeValue().toInt());
222 font.setUnderline(txtProperties.namedItem("font-underline").nodeValue().toInt());
223 // Older Kdenlive version did not store pixel size but point size
224 if (txtProperties.namedItem("font-pixel-size").isNull()) {
226 f2.setPointSize(txtProperties.namedItem("font-size").nodeValue().toInt());
227 font.setPixelSize(QFontInfo(f2).pixelSize());
228 } else font.setPixelSize(txtProperties.namedItem("font-pixel-size").nodeValue().toInt());
229 QColor col(stringToColor(txtProperties.namedItem("font-color").nodeValue()));
230 QGraphicsTextItem *txt = m_scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
231 txt->setDefaultTextColor(col);
232 txt->setTextInteractionFlags(Qt::NoTextInteraction);
233 if (txtProperties.namedItem("alignment").isNull() == false) {
234 txt->setTextWidth(txt->boundingRect().width());
235 QTextCursor cur = txt->textCursor();
236 QTextBlockFormat format = cur.blockFormat();
237 format.setAlignment((Qt::Alignment) txtProperties.namedItem("alignment").nodeValue().toInt());
238 cur.select(QTextCursor::Document);
239 cur.setBlockFormat(format);
240 txt->setTextCursor(cur);
241 cur.clearSelection();
242 txt->setTextCursor(cur);
245 if (!txtProperties.namedItem("kdenlive-axis-x-inverted").isNull()) {
246 txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
248 if (!txtProperties.namedItem("kdenlive-axis-y-inverted").isNull()) {
249 txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
253 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
254 QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
255 QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
256 QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
257 double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
258 QGraphicsRectItem *rec = m_scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
260 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
261 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
263 QGraphicsPixmapItem *rec = m_scene->addPixmap(pix);
264 rec->setData(Qt::UserRole, url);
266 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
267 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
268 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
269 m_scene->addItem(rec);
270 rec->setData(Qt::UserRole, url);
276 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
277 items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
279 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
280 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
281 if (zValue > maxZValue) maxZValue = zValue;
282 gitem->setZValue(zValue);
283 gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
285 if (items.item(i).nodeName() == "background") {
286 kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
287 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
288 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
289 QList<QGraphicsItem *> items = m_scene->items();
290 for (int i = 0; i < items.size(); i++) {
291 if (items.at(i)->zValue() == -1100) {
292 ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
296 } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
297 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
298 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
299 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
300 kDebug() << width << rect;
301 startv->setPolygon(rect);
303 } else if (items.item(i).nodeName() == "endviewport" && endv) {
304 QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
305 double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
306 QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
307 kDebug() << width << rect;
308 endv->setPolygon(rect);
316 QString TitleDocument::colorToString(const QColor& c)
318 QString ret = "%1,%2,%3,%4";
319 ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
323 QString TitleDocument::rectFToString(const QRectF& c)
325 QString ret = "%1,%2,%3,%4";
326 ret = ret.arg(c.top()).arg(c.left()).arg(c.width()).arg(c.height());
330 QRectF TitleDocument::stringToRect(const QString & s)
333 QStringList l = s.split(',');
336 return QRectF(l.at(0).toDouble(), l.at(1).toDouble(), l.at(2).toDouble(), l.at(3).toDouble()).normalized();
339 QColor TitleDocument::stringToColor(const QString & s)
341 QStringList l = s.split(',');
344 return QColor(l.at(0).toInt(), l.at(1).toInt(), l.at(2).toInt(), l.at(3).toInt());;
346 QTransform TitleDocument::stringToTransform(const QString& s)
348 QStringList l = s.split(',');
352 l.at(0).toDouble(), l.at(1).toDouble(), l.at(2).toDouble(),
353 l.at(3).toDouble(), l.at(4).toDouble(), l.at(5).toDouble(),
354 l.at(6).toDouble(), l.at(7).toDouble(), l.at(8).toDouble()