]> git.sesse.net Git - kdenlive/blob - src/titledocument.cpp
[PATCH by Ray Lehtiniem] Delete some unused variables
[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     QColor color = getBackgroundColor();
117     backgr.setAttribute("color", colorToString(color));
118     main.appendChild(backgr);
119
120     return doc;
121 }
122
123 /** \brief Get the background color (incl. alpha) from the document, if possibly
124   * \returns The background color of the document, inclusive alpha. If none found, returns (0,0,0,0) */
125 QColor TitleDocument::getBackgroundColor() {
126     QColor color(0, 0, 0, 0);
127     if (scene) {
128         QList<QGraphicsItem *> items = scene->items();
129         for (int i = 0; i < items.size(); i++) {
130             if (items.at(i)->zValue() == -1100) {
131                 color = ((QGraphicsRectItem *)items.at(i))->brush().color();
132                 return color;
133             }
134         }
135     }
136     return color;
137 }
138
139
140 bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
141     if (!scene)
142         return false;
143
144     QDomDocument doc = xml(startv, endv);
145     KTemporaryFile tmpfile;
146     if (!tmpfile.open()) kWarning() << "/////  CANNOT CREATE TMP FILE in: " << tmpfile.fileName();
147     QFile xmlf(tmpfile.fileName());
148     xmlf.open(QIODevice::WriteOnly);
149     xmlf.write(doc.toString().toUtf8());
150     xmlf.close();
151     kDebug() << KIO::NetAccess::upload(tmpfile.fileName(), url, 0);
152     return true;
153 }
154
155 int TitleDocument::loadDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
156     QString tmpfile;
157     QDomDocument doc;
158     if (!scene)
159         return -1;
160
161     if (KIO::NetAccess::download(url, tmpfile, 0)) {
162         QFile file(tmpfile);
163         if (file.open(QIODevice::ReadOnly)) {
164             doc.setContent(&file, false);
165             file.close();
166         } else
167             return -1;
168         KIO::NetAccess::removeTempFile(tmpfile);
169         return loadFromXml(doc, startv, endv);
170     }
171     return -1;
172 }
173
174 int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
175     QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
176     int maxZValue = 0;
177     if (titles.size()) {
178
179         QDomNodeList items = titles.item(0).childNodes();
180         for (int i = 0;i < items.count();i++) {
181             QGraphicsItem *gitem = NULL;
182             kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
183             int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
184             if (zValue > -1000)
185                 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
186                     QFont font(items.item(i).namedItem("content").attributes().namedItem("font").nodeValue());
187                     font.setBold(items.item(i).namedItem("content").attributes().namedItem("font-bold").nodeValue().toInt());
188                     font.setItalic(items.item(i).namedItem("content").attributes().namedItem("font-italic").nodeValue().toInt());
189                     font.setUnderline(items.item(i).namedItem("content").attributes().namedItem("font-underline").nodeValue().toInt());
190                     font.setPointSize(items.item(i).namedItem("content").attributes().namedItem("font-size").nodeValue().toInt());
191                     QColor col(stringToColor(items.item(i).namedItem("content").attributes().namedItem("font-color").nodeValue()));
192                     QGraphicsTextItem *txt = scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
193                     txt->setDefaultTextColor(col);
194                     txt->setTextInteractionFlags(Qt::NoTextInteraction);
195                     gitem = txt;
196                 } else
197                     if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
198                         QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
199                         QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
200                         QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
201                         double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
202                         QGraphicsRectItem *rec = scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
203                         gitem = rec;
204                     } else
205                         if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
206                             QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
207                             QPixmap pix(url);
208                             QGraphicsPixmapItem *rec = scene->addPixmap(pix);
209                             rec->setData(Qt::UserRole, url);
210                             gitem = rec;
211                         } else
212                             if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
213                                 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
214                                 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
215                                 scene->addItem(rec);
216                                 rec->setData(Qt::UserRole, url);
217                                 gitem = rec;
218                             }
219             //pos and transform
220             if (gitem) {
221                 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
222                           items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
223                 gitem->setPos(p);
224                 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
225                 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
226                 if (zValue > maxZValue) maxZValue = zValue;
227                 gitem->setZValue(zValue);
228                 gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
229             }
230             if (items.item(i).nodeName() == "background") {
231                 kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
232                 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
233                 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
234                 QList<QGraphicsItem *> items = scene->items();
235                 for (int i = 0; i < items.size(); i++) {
236                     if (items.at(i)->zValue() == -1100) {
237                         ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
238                         break;
239                     }
240                 }
241             } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
242                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
243                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
244                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
245                     kDebug() << width << rect;
246                     startv->setPolygon(rect);
247                     startv->setPos(p);
248                 } else if (items.item(i).nodeName() == "endviewport" && endv) {
249                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
250                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
251                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
252                     kDebug() << width << rect;
253                     endv->setPolygon(rect);
254                     endv->setPos(p);
255                 }*/
256         }
257     }
258     return maxZValue;
259 }
260
261 QString TitleDocument::colorToString(const QColor& c) {
262     QString ret = "%1,%2,%3,%4";
263     ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
264     return ret;
265 }
266
267 QString TitleDocument::rectFToString(const QRectF& c) {
268     QString ret = "%1,%2,%3,%4";
269     ret = ret.arg(c.x()).arg(c.y()).arg(c.width()).arg(c.height());
270     return ret;
271 }
272
273 QRectF TitleDocument::stringToRect(const QString & s) {
274
275     QStringList l = s.split(',');
276     if (l.size() < 4)
277         return QRectF();
278     return QRectF(l[0].toDouble(), l[1].toDouble(), l[2].toDouble(), l[3].toDouble());
279 }
280
281 QColor TitleDocument::stringToColor(const QString & s) {
282     QStringList l = s.split(',');
283     if (l.size() < 4)
284         return QColor();
285     return QColor(l[0].toInt(), l[1].toInt(), l[2].toInt(), l[3].toInt());;
286 }
287 QTransform TitleDocument::stringToTransform(const QString& s) {
288     QStringList l = s.split(',');
289     if (l.size() < 9)
290         return QTransform();
291     return QTransform(
292                l[0].toDouble(), l[1].toDouble(), l[2].toDouble(),
293                l[3].toDouble(), l[4].toDouble(), l[5].toDouble(),
294                l[6].toDouble(), l[7].toDouble(), l[8].toDouble()
295            );
296 }