]> git.sesse.net Git - kdenlive/blob - src/titledocument.cpp
Patches from Ray Lehtiniemi
[kdenlive] / src / titledocument.cpp
1 /***************************************************************************
2                           titledocument.h  -  description
3                              -------------------
4     begin                : Feb 28 2008
5     copyright            : (C) 2008 by Marco Gittler
6     email                : g.marco@freenet.de
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
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.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include "titledocument.h"
19
20 #include <KDebug>
21 #include <KTemporaryFile>
22 #include <kio/netaccess.h>
23
24 #include <QGraphicsScene>
25 #include <QDomElement>
26 #include <QGraphicsItem>
27 #include <QGraphicsRectItem>
28 #include <QGraphicsTextItem>
29 #include <QGraphicsSvgItem>
30 #include <QFontInfo>
31 #include <QFile>
32
33
34 TitleDocument::TitleDocument() {
35     scene = NULL;
36 }
37
38 void TitleDocument::setScene(QGraphicsScene* _scene) {
39     scene = _scene;
40 }
41
42 QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
43     QDomDocument doc;
44
45     QDomElement main = doc.createElement("kdenlivetitle");
46     doc.appendChild(main);
47
48     foreach(QGraphicsItem* item, scene->items()) {
49         QDomElement e = doc.createElement("item");
50         QDomElement content = doc.createElement("content");
51         QFont font;
52         QGraphicsTextItem *t;
53
54         switch (item->type()) {
55         case 7:
56             e.setAttribute("type", "QGraphicsPixmapItem");
57             content.setAttribute("url", item->data(Qt::UserRole).toString());
58             break;
59         case 13:
60             e.setAttribute("type", "QGraphicsSvgItem");
61             content.setAttribute("url", item->data(Qt::UserRole).toString());
62             break;
63         case 3:
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()));
69             break;
70         case 8:
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()));
77             font = t->font();
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()));
84             break;
85         default:
86             continue;
87         }
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())
96                        )
97                       );
98         e.setAttribute("z-index", item->zValue());
99         pos.appendChild(tr);
100
101
102         e.appendChild(pos);
103         e.appendChild(content);
104         if (item->zValue() > -1100) main.appendChild(e);
105     }
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);
112
113         startp.setAttribute("x", startv->pos().x());
114         startp.setAttribute("y", startv->pos().y());
115         startp.setAttribute("size", startv->sceneBoundingRect().width() / 2);
116
117         startp.setAttribute("z-index", startv->zValue());
118         endp.setAttribute("z-index", endv->zValue());
119         main.appendChild(startp);
120         main.appendChild(endp);
121     }
122     QDomElement backgr = doc.createElement("background");
123     QColor color = getBackgroundColor();
124     backgr.setAttribute("color", colorToString(color));
125     main.appendChild(backgr);
126
127     return doc;
128 }
129
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);
134     if (scene) {
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();
139                 return color;
140             }
141         }
142     }
143     return color;
144 }
145
146
147 bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
148     if (!scene)
149         return false;
150
151     QDomDocument doc = xml(startv, endv);
152     KTemporaryFile tmpfile;
153     if (!tmpfile.open()) {
154         kWarning() << "/////  CANNOT CREATE TMP FILE in: " << tmpfile.fileName();
155         return false;
156     }
157     QFile xmlf(tmpfile.fileName());
158     xmlf.open(QIODevice::WriteOnly);
159     xmlf.write(doc.toString().toUtf8());
160     if (xmlf.error() != QFile::NoError) {
161         xmlf.close();
162         return false;
163     }
164     xmlf.close();
165     return KIO::NetAccess::upload(tmpfile.fileName(), url, 0);
166 }
167
168 int TitleDocument::loadDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
169     QString tmpfile;
170     QDomDocument doc;
171     if (!scene)
172         return -1;
173
174     if (KIO::NetAccess::download(url, tmpfile, 0)) {
175         QFile file(tmpfile);
176         if (file.open(QIODevice::ReadOnly)) {
177             doc.setContent(&file, false);
178             file.close();
179         } else
180             return -1;
181         KIO::NetAccess::removeTempFile(tmpfile);
182         return loadFromXml(doc, startv, endv);
183     }
184     return -1;
185 }
186
187 int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* /*startv*/, QGraphicsPolygonItem* /*endv*/) {
188     QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
189     int maxZValue = 0;
190     if (titles.size()) {
191
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();
197             if (zValue > -1000) {
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()) {
206                         QFont f2;
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);
214                     gitem = txt;
215                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
216                     QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
217                     QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
218                     QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
219                     double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
220                     QGraphicsRectItem *rec = scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
221                     gitem = rec;
222                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
223                     QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
224                     QPixmap pix(url);
225                     QGraphicsPixmapItem *rec = scene->addPixmap(pix);
226                     rec->setData(Qt::UserRole, url);
227                     gitem = rec;
228                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
229                     QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
230                     QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
231                     scene->addItem(rec);
232                     rec->setData(Qt::UserRole, url);
233                     gitem = rec;
234                 }
235             }
236             //pos and transform
237             if (gitem) {
238                 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
239                           items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
240                 gitem->setPos(p);
241                 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
242                 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
243                 if (zValue > maxZValue) maxZValue = zValue;
244                 gitem->setZValue(zValue);
245                 gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
246             }
247             if (items.item(i).nodeName() == "background") {
248                 kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
249                 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
250                 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
251                 QList<QGraphicsItem *> items = scene->items();
252                 for (int i = 0; i < items.size(); i++) {
253                     if (items.at(i)->zValue() == -1100) {
254                         ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
255                         break;
256                     }
257                 }
258             } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
259                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
260                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
261                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
262                     kDebug() << width << rect;
263                     startv->setPolygon(rect);
264                     startv->setPos(p);
265                 } else if (items.item(i).nodeName() == "endviewport" && endv) {
266                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
267                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
268                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
269                     kDebug() << width << rect;
270                     endv->setPolygon(rect);
271                     endv->setPos(p);
272                 }*/
273         }
274     }
275     return maxZValue;
276 }
277
278 QString TitleDocument::colorToString(const QColor& c) {
279     QString ret = "%1,%2,%3,%4";
280     ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
281     return ret;
282 }
283
284 QString TitleDocument::rectFToString(const QRectF& c) {
285     QString ret = "%1,%2,%3,%4";
286     ret = ret.arg(c.x()).arg(c.y()).arg(c.width()).arg(c.height());
287     return ret;
288 }
289
290 QRectF TitleDocument::stringToRect(const QString & s) {
291
292     QStringList l = s.split(',');
293     if (l.size() < 4)
294         return QRectF();
295     return QRectF(l[0].toDouble(), l[1].toDouble(), l[2].toDouble(), l[3].toDouble());
296 }
297
298 QColor TitleDocument::stringToColor(const QString & s) {
299     QStringList l = s.split(',');
300     if (l.size() < 4)
301         return QColor();
302     return QColor(l[0].toInt(), l[1].toInt(), l[2].toInt(), l[3].toInt());;
303 }
304 QTransform TitleDocument::stringToTransform(const QString& s) {
305     QStringList l = s.split(',');
306     if (l.size() < 9)
307         return QTransform();
308     return QTransform(
309                l[0].toDouble(), l[1].toDouble(), l[2].toDouble(),
310                l[3].toDouble(), l[4].toDouble(), l[5].toDouble(),
311                l[6].toDouble(), l[7].toDouble(), l[8].toDouble()
312            );
313 }