]> git.sesse.net Git - kdenlive/blob - src/titledocument.cpp
[PATCH by Ray Lehtiniemi] Fix up a missing return value
[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     double aspect_ratio = 4.0 / 3.0;
159     if (!scene)
160         return -1;
161
162     if (KIO::NetAccess::download(url, tmpfile, 0)) {
163         QFile file(tmpfile);
164         if (file.open(QIODevice::ReadOnly)) {
165             doc.setContent(&file, false);
166             file.close();
167         } else
168             return -1;
169         KIO::NetAccess::removeTempFile(tmpfile);
170         return loadFromXml(doc, startv, endv);
171     }
172     return -1;
173 }
174
175 int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
176     QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
177     int maxZValue = 0;
178     if (titles.size()) {
179
180         QDomNodeList items = titles.item(0).childNodes();
181         for (int i = 0;i < items.count();i++) {
182             QGraphicsItem *gitem = NULL;
183             kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
184             int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
185             if (zValue > -1000)
186                 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
187                     QFont font(items.item(i).namedItem("content").attributes().namedItem("font").nodeValue());
188                     font.setBold(items.item(i).namedItem("content").attributes().namedItem("font-bold").nodeValue().toInt());
189                     font.setItalic(items.item(i).namedItem("content").attributes().namedItem("font-italic").nodeValue().toInt());
190                     font.setUnderline(items.item(i).namedItem("content").attributes().namedItem("font-underline").nodeValue().toInt());
191                     font.setPointSize(items.item(i).namedItem("content").attributes().namedItem("font-size").nodeValue().toInt());
192                     QColor col(stringToColor(items.item(i).namedItem("content").attributes().namedItem("font-color").nodeValue()));
193                     QGraphicsTextItem *txt = scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
194                     txt->setDefaultTextColor(col);
195                     txt->setTextInteractionFlags(Qt::NoTextInteraction);
196                     gitem = txt;
197                 } else
198                     if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
199                         QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
200                         QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
201                         QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
202                         double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
203                         QGraphicsRectItem *rec = scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
204                         gitem = rec;
205                     } else
206                         if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
207                             QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
208                             QPixmap pix(url);
209                             QGraphicsPixmapItem *rec = scene->addPixmap(pix);
210                             rec->setData(Qt::UserRole, url);
211                             gitem = rec;
212                         } else
213                             if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
214                                 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
215                                 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
216                                 scene->addItem(rec);
217                                 rec->setData(Qt::UserRole, url);
218                                 gitem = rec;
219                             }
220             //pos and transform
221             if (gitem) {
222                 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
223                           items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
224                 gitem->setPos(p);
225                 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
226                 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
227                 if (zValue > maxZValue) maxZValue = zValue;
228                 gitem->setZValue(zValue);
229                 gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
230             }
231             if (items.item(i).nodeName() == "background") {
232                 kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
233                 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
234                 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
235                 QList<QGraphicsItem *> items = scene->items();
236                 for (int i = 0; i < items.size(); i++) {
237                     if (items.at(i)->zValue() == -1100) {
238                         ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
239                         break;
240                     }
241                 }
242             } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
243                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
244                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
245                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
246                     kDebug() << width << rect;
247                     startv->setPolygon(rect);
248                     startv->setPos(p);
249                 } else if (items.item(i).nodeName() == "endviewport" && endv) {
250                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
251                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
252                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
253                     kDebug() << width << rect;
254                     endv->setPolygon(rect);
255                     endv->setPos(p);
256                 }*/
257         }
258     }
259     return maxZValue;
260 }
261
262 QString TitleDocument::colorToString(const QColor& c) {
263     QString ret = "%1,%2,%3,%4";
264     ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
265     return ret;
266 }
267
268 QString TitleDocument::rectFToString(const QRectF& c) {
269     QString ret = "%1,%2,%3,%4";
270     ret = ret.arg(c.x()).arg(c.y()).arg(c.width()).arg(c.height());
271     return ret;
272 }
273
274 QRectF TitleDocument::stringToRect(const QString & s) {
275
276     QStringList l = s.split(',');
277     if (l.size() < 4)
278         return QRectF();
279     return QRectF(l[0].toDouble(), l[1].toDouble(), l[2].toDouble(), l[3].toDouble());
280 }
281
282 QColor TitleDocument::stringToColor(const QString & s) {
283     QStringList l = s.split(',');
284     if (l.size() < 4)
285         return QColor();
286     return QColor(l[0].toInt(), l[1].toInt(), l[2].toInt(), l[3].toInt());;
287 }
288 QTransform TitleDocument::stringToTransform(const QString& s) {
289     QStringList l = s.split(',');
290     if (l.size() < 9)
291         return QTransform();
292     return QTransform(
293                l[0].toDouble(), l[1].toDouble(), l[2].toDouble(),
294                l[3].toDouble(), l[4].toDouble(), l[5].toDouble(),
295                l[6].toDouble(), l[7].toDouble(), l[8].toDouble()
296            );
297 }