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