]> git.sesse.net Git - kdenlive/blob - src/titledocument.cpp
Images and SVG can now be added in titles through add image...
[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 #include "titledocument.h"
18 #include <QGraphicsScene>
19 #include <QDomDocument>
20 #include <QDomElement>
21 #include <QGraphicsItem>
22 #include <QGraphicsRectItem>
23 #include <QGraphicsTextItem>
24 #include <QGraphicsSvgItem>
25 #include <KDebug>
26 #include <QFile>
27 #include <KTemporaryFile>
28 #include <kio/netaccess.h>
29
30 TitleDocument::TitleDocument() {
31     scene = NULL;
32 }
33
34 void TitleDocument::setScene(QGraphicsScene* _scene) {
35     scene = _scene;
36 }
37
38 bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
39     QDomDocument doc;
40
41     if (!scene)
42         return false;
43
44     QDomElement main = doc.createElement("kdenlivetitle");
45     doc.appendChild(main);
46
47     foreach(QGraphicsItem* item, scene->items()) {
48         QDomElement e = doc.createElement("item");
49         QDomElement content = doc.createElement("content");
50         QFont font;
51         QGraphicsTextItem *t;
52
53         switch (item->type()) {
54         case 7:
55             e.setAttribute("type", "QGraphicsPixmapItem");
56             content.setAttribute("url", item->data(Qt::UserRole).toString());
57             break;
58         case 13:
59             e.setAttribute("type", "QGraphicsSvgItem");
60             content.setAttribute("url", item->data(Qt::UserRole).toString());
61             break;
62         case 3:
63             e.setAttribute("type", "QGraphicsRectItem");
64             content.setAttribute("rect", rectFToString(((QGraphicsRectItem*)item)->rect()));
65             content.setAttribute("pencolor", colorToString(((QGraphicsRectItem*)item)->pen().color()));
66             content.setAttribute("penwidth", ((QGraphicsRectItem*)item)->pen().width());
67             content.setAttribute("brushcolor", colorToString(((QGraphicsRectItem*)item)->brush().color()));
68             break;
69         case 8:
70             e.setAttribute("type", "QGraphicsTextItem");
71             t = static_cast<QGraphicsTextItem *>(item);
72             //content.appendChild(doc.createTextNode(((QGraphicsTextItem*)item)->toHtml()));
73             content.appendChild(doc.createTextNode(t->toPlainText()));
74             font = t->font();
75             content.setAttribute("font", font.family());
76             content.setAttribute("font-bold", font.bold());
77             content.setAttribute("font-size", font.pointSize());
78             content.setAttribute("font-italic", font.italic());
79             content.setAttribute("font-underline", font.underline());
80             content.setAttribute("font-color", colorToString(t->defaultTextColor()));
81             break;
82         default:
83             continue;
84         }
85         QDomElement pos = doc.createElement("position");
86         pos.setAttribute("x", item->pos().x());
87         pos.setAttribute("y", item->pos().y());
88         QTransform transform = item->transform();
89         QDomElement tr = doc.createElement("transform");
90         tr.appendChild(doc.createTextNode(
91                            QString("%1,%2,%3,%4,%5,%6,%7,%8,%9").arg(
92                                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())
93                        )
94                       );
95         e.setAttribute("z-index", item->zValue());
96         pos.appendChild(tr);
97
98
99         e.appendChild(pos);
100         e.appendChild(content);
101         if (item->zValue() > -1100) main.appendChild(e);
102     }
103     if (startv && endv) {
104         QDomElement endp = doc.createElement("endviewport");
105         QDomElement startp = doc.createElement("startviewport");
106         endp.setAttribute("x", endv->pos().x());
107         endp.setAttribute("y", endv->pos().y());
108         endp.setAttribute("size", endv->sceneBoundingRect().width() / 2);
109
110         startp.setAttribute("x", startv->pos().x());
111         startp.setAttribute("y", startv->pos().y());
112         startp.setAttribute("size", startv->sceneBoundingRect().width() / 2);
113
114         startp.setAttribute("z-index", startv->zValue());
115         endp.setAttribute("z-index", endv->zValue());
116         main.appendChild(startp);
117         main.appendChild(endp);
118     }
119     QDomElement backgr = doc.createElement("background");
120     QList<QGraphicsItem *> items = scene->items();
121     QColor color(0, 0, 0, 0);
122     for (int i = 0; i < items.size(); i++) {
123         if (items.at(i)->zValue() == -1100) {
124             color = ((QGraphicsRectItem *)items.at(i))->brush().color();
125             break;
126         }
127     }
128     backgr.setAttribute("color", colorToString(color));
129     main.appendChild(backgr);
130
131     KTemporaryFile tmpfile;
132     if (!tmpfile.open()) kWarning() << "/////  CANNOT CREATE TMP FILE in: " << tmpfile.fileName();
133     QFile xmlf(tmpfile.fileName());
134     xmlf.open(QIODevice::WriteOnly);
135     xmlf.write(doc.toString().toAscii());
136     xmlf.close();
137     kDebug() << KIO::NetAccess::upload(tmpfile.fileName(), url, 0);
138
139 }
140
141 int TitleDocument::loadDocument(const KUrl& url , QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
142     QString tmpfile;
143     QDomDocument doc;
144     int maxZValue = 0;
145     double aspect_ratio = 4.0 / 3.0;
146     if (!scene)
147         return -1;
148
149     if (KIO::NetAccess::download(url, tmpfile, 0)) {
150         QFile file(tmpfile);
151         if (file.open(QIODevice::ReadOnly)) {
152             doc.setContent(&file, false);
153             file.close();
154         } else
155             return -1;
156         KIO::NetAccess::removeTempFile(tmpfile);
157         QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
158         if (titles.size()) {
159
160             QDomNodeList items = titles.item(0).childNodes();
161             for (int i = 0;i < items.count();i++) {
162                 QGraphicsItem *gitem = NULL;
163                 kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
164                 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
165                 if (zValue > -1000)
166                     if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
167                         QFont font(items.item(i).namedItem("content").attributes().namedItem("font").nodeValue());
168                         font.setBold(items.item(i).namedItem("content").attributes().namedItem("font-bold").nodeValue().toInt());
169                         font.setItalic(items.item(i).namedItem("content").attributes().namedItem("font-italic").nodeValue().toInt());
170                         font.setUnderline(items.item(i).namedItem("content").attributes().namedItem("font-underline").nodeValue().toInt());
171                         font.setPointSize(items.item(i).namedItem("content").attributes().namedItem("font-size").nodeValue().toInt());
172                         QColor col(stringToColor(items.item(i).namedItem("content").attributes().namedItem("font-color").nodeValue()));
173                         QGraphicsTextItem *txt = scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
174                         txt->setDefaultTextColor(col);
175                         txt->setTextInteractionFlags(Qt::NoTextInteraction);
176                         gitem = txt;
177                     } else
178                         if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
179                             QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
180                             QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
181                             QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
182                             double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
183                             QGraphicsRectItem *rec = scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
184                             gitem = rec;
185                         } else
186                             if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
187                                 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
188                                 QPixmap pix(url);
189                                 QGraphicsPixmapItem *rec = scene->addPixmap(pix);
190                                 rec->setData(Qt::UserRole, url);
191                                 gitem = rec;
192                             } else
193                                 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
194                                     QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
195                                     QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
196                                     scene->addItem(rec);
197                                     rec->setData(Qt::UserRole, url);
198                                     gitem = rec;
199                                 }
200                 //pos and transform
201                 if (gitem) {
202                     QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
203                               items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
204                     gitem->setPos(p);
205                     gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
206                     int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
207                     if (zValue > maxZValue) maxZValue = zValue;
208                     gitem->setZValue(zValue);
209                     gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
210                 }
211                 if (items.item(i).nodeName() == "background") {
212                     kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
213                     QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
214                     //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
215                     QList<QGraphicsItem *> items = scene->items();
216                     for (int i = 0; i < items.size(); i++) {
217                         if (items.at(i)->zValue() == -1100) {
218                             ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
219                             break;
220                         }
221                     }
222                 } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
223                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
224                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
225                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
226                     kDebug() << width << rect;
227                     startv->setPolygon(rect);
228                     startv->setPos(p);
229                 } else if (items.item(i).nodeName() == "endviewport" && endv) {
230                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
231                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
232                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
233                     kDebug() << width << rect;
234                     endv->setPolygon(rect);
235                     endv->setPos(p);
236                 }*/
237             }
238         }
239     }
240     return maxZValue;
241 }
242
243 QString TitleDocument::colorToString(const QColor& c) {
244     QString ret = "%1,%2,%3,%4";
245     ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
246     return ret;
247 }
248
249 QString TitleDocument::rectFToString(const QRectF& c) {
250     QString ret = "%1,%2,%3,%4";
251     ret = ret.arg(c.x()).arg(c.y()).arg(c.width()).arg(c.height());
252     return ret;
253 }
254
255 QRectF TitleDocument::stringToRect(const QString & s) {
256
257     QStringList l = s.split(",");
258     if (l.size() < 4)
259         return QRectF();
260     return QRectF(l[0].toDouble(), l[1].toDouble(), l[2].toDouble(), l[3].toDouble());
261 }
262
263 QColor TitleDocument::stringToColor(const QString & s) {
264     QStringList l = s.split(",");
265     if (l.size() < 4)
266         return QColor();
267     return QColor(l[0].toInt(), l[1].toInt(), l[2].toInt(), l[3].toInt());;
268 }
269 QTransform TitleDocument::stringToTransform(const QString& s) {
270     QStringList l = s.split(",");
271     if (l.size() < 9)
272         return QTransform();
273     return QTransform(
274                l[0].toDouble(), l[1].toDouble(), l[2].toDouble(),
275                l[3].toDouble(), l[4].toDouble(), l[5].toDouble(),
276                l[6].toDouble(), l[7].toDouble(), l[8].toDouble()
277            );
278 }