]> git.sesse.net Git - kdenlive/blob - src/titledocument.cpp
not use deprecated function anymore in initeffects/store z-value in titledocument
[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 <KDebug>
25 #include <QFile>
26 #include <kio/netaccess.h>
27
28 TitleDocument::TitleDocument() {
29     scene = NULL;
30 }
31
32 void TitleDocument::setScene(QGraphicsScene* _scene) {
33     scene = _scene;
34 }
35
36 bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
37     QDomDocument doc;
38
39     if (!scene)
40         return false;
41
42     QDomElement main = doc.createElement("kdenlivetitle");
43     doc.appendChild(main);
44
45     foreach(QGraphicsItem* item, scene->items()) {
46         QDomElement e = doc.createElement("item");
47         QDomElement content = doc.createElement("content");
48
49         switch (item->type()) {
50         case 3:
51             e.setAttribute("type", "QGraphicsRectItem");
52             content.setAttribute("rect", rectFToString(((QGraphicsRectItem*)item)->rect()));
53             content.setAttribute("pencolor", colorToString(((QGraphicsRectItem*)item)->pen().color()));
54             content.setAttribute("penwidth", ((QGraphicsRectItem*)item)->pen().width());
55             content.setAttribute("brushcolor", colorToString(((QGraphicsRectItem*)item)->brush().color()));
56             break;
57         case 8:
58             e.setAttribute("type", "QGraphicsTextItem");
59             content.appendChild(doc.createTextNode(((QGraphicsTextItem*)item)->toHtml()));
60             break;
61         default:
62             continue;
63         }
64         QDomElement pos = doc.createElement("position");
65         pos.setAttribute("x", item->pos().x());
66         pos.setAttribute("y", item->pos().y());
67         QTransform transform = item->transform();
68         QDomElement tr = doc.createElement("transform");
69         tr.appendChild(doc.createTextNode(
70                            QString("%1,%2,%3,%4,%5,%6,%7,%8,%9").arg(
71                                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())
72                        )
73                       );
74         e.setAttribute("z-index", item->zValue());
75         pos.appendChild(tr);
76
77
78         e.appendChild(pos);
79         e.appendChild(content);
80         main.appendChild(e);
81     }
82     if (startv && endv) {
83         QDomElement endp = doc.createElement("endviewport");
84         QDomElement startp = doc.createElement("startviewport");
85         endp.setAttribute("x", endv->pos().x());
86         endp.setAttribute("y", endv->pos().y());
87         endp.setAttribute("size", endv->sceneBoundingRect().width() / 2);
88
89         startp.setAttribute("x", startv->pos().x());
90         startp.setAttribute("y", startv->pos().y());
91         startp.setAttribute("size", startv->sceneBoundingRect().width() / 2);
92
93         startp.setAttribute("z-index", startv->zValue());
94         endp.setAttribute("z-index", endv->zValue());
95         main.appendChild(startp);
96         main.appendChild(endp);
97     }
98     QDomElement backgr = doc.createElement("background");
99     backgr.setAttribute("color", colorToString(scene->backgroundBrush().color()));
100     main.appendChild(backgr);
101
102     QString tmpfile = "/tmp/newtitle";
103     QFile xmlf(tmpfile);
104     xmlf.open(QIODevice::WriteOnly);
105     xmlf.write(doc.toString().toAscii());
106     xmlf.close();
107     kDebug() << KIO::NetAccess::upload(tmpfile, url, 0);
108 }
109
110 bool TitleDocument::loadDocument(const KUrl& url , QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
111     QString tmpfile;
112     QDomDocument doc;
113     double aspect_ratio = 4.0 / 3.0;
114     if (!scene)
115         return false;
116
117     if (KIO::NetAccess::download(url, tmpfile, 0)) {
118         QFile file(tmpfile);
119         if (file.open(QIODevice::ReadOnly)) {
120             doc.setContent(&file, false);
121             file.close();
122         } else
123             return false;
124         KIO::NetAccess::removeTempFile(tmpfile);
125         QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
126         if (titles.size()) {
127
128             QDomNodeList items = titles.item(0).childNodes();
129             for (int i = 0;i < items.count();i++) {
130                 QGraphicsItem *gitem = NULL;
131                 kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
132                 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
133                     QGraphicsTextItem *txt = scene->addText("");
134                     txt->setHtml(items.item(i).namedItem("content").firstChild().nodeValue());
135                     gitem = txt;
136                 } else
137                     if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
138                         QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
139                         QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
140                         QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
141                         double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
142                         QGraphicsRectItem *rec = scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
143                         gitem = rec;
144                     }
145                 //pos and transform
146                 if (gitem) {
147                     QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
148                               items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
149                     gitem->setPos(p);
150                     gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
151                 }
152                 if (items.item(i).nodeName() == "background") {
153                     kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
154                     scene->setBackgroundBrush(QBrush(stringToColor(items.item(i).attributes().namedItem("color").nodeValue())));
155                 } else if (items.item(i).nodeName() == "startviewport" && startv) {
156                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
157                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
158                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
159                     kDebug() << width << rect;
160                     startv->setPolygon(rect);
161                     startv->setPos(p);
162                 } else if (items.item(i).nodeName() == "endviewport" && endv) {
163                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
164                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
165                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
166                     kDebug() << width << rect;
167                     endv->setPolygon(rect);
168                     endv->setPos(p);
169                 }
170
171             }
172
173
174         }
175
176
177     }
178     return true;
179 }
180
181 QString TitleDocument::colorToString(const QColor& c) {
182     QString ret = "%1,%2,%3,%4";
183     ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
184     return ret;
185 }
186
187 QString TitleDocument::rectFToString(const QRectF& c) {
188     QString ret = "%1,%2,%3,%4";
189     ret = ret.arg(c.x()).arg(c.y()).arg(c.width()).arg(c.height());
190     return ret;
191 }
192
193 QRectF TitleDocument::stringToRect(const QString & s) {
194
195     QStringList l = s.split(",");
196     if (l.size() < 4)
197         return QRectF();
198     return QRectF(l[0].toDouble(), l[1].toDouble(), l[2].toDouble(), l[3].toDouble());
199 }
200
201 QColor TitleDocument::stringToColor(const QString & s) {
202     QStringList l = s.split(",");
203     if (l.size() < 4)
204         return QColor();
205     return QColor(l[0].toInt(), l[1].toInt(), l[2].toInt(), l[3].toInt());;
206 }
207 QTransform TitleDocument::stringToTransform(const QString& s) {
208     QStringList l = s.split(",");
209     if (l.size() < 9)
210         return QTransform();
211     return QTransform(
212                l[0].toDouble(), l[1].toDouble(), l[2].toDouble(),
213                l[3].toDouble(), l[4].toDouble(), l[5].toDouble(),
214                l[6].toDouble(), l[7].toDouble(), l[8].toDouble()
215            );
216 }