]> git.sesse.net Git - kdenlive/blob - src/titledocument.cpp
#914 working, #911 corrected.
[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
18 #include "titledocument.h"
19
20 #include <KDebug>
21 #include <KTemporaryFile>
22 #include <kio/netaccess.h>
23
24 #include <QGraphicsScene>
25 #include <QDomElement>
26 #include <QGraphicsItem>
27 #include <QGraphicsRectItem>
28 #include <QGraphicsTextItem>
29 #include <QGraphicsSvgItem>
30 #include <QFontInfo>
31 #include <QFile>
32 #include <QTextCursor>
33
34
35 TitleDocument::TitleDocument()
36 {
37     m_scene = NULL;
38 }
39
40 void TitleDocument::setScene(QGraphicsScene* _scene)
41 {
42     m_scene = _scene;
43 }
44
45 QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
46 {
47     QDomDocument doc;
48
49     QDomElement main = doc.createElement("kdenlivetitle");
50     doc.appendChild(main);
51
52     foreach(QGraphicsItem* item, m_scene->items()) {
53         QDomElement e = doc.createElement("item");
54         QDomElement content = doc.createElement("content");
55         QFont font;
56         QGraphicsTextItem *t;
57
58         switch (item->type()) {
59         case 7:
60             e.setAttribute("type", "QGraphicsPixmapItem");
61             content.setAttribute("url", item->data(Qt::UserRole).toString());
62             break;
63         case 13:
64             e.setAttribute("type", "QGraphicsSvgItem");
65             content.setAttribute("url", item->data(Qt::UserRole).toString());
66             break;
67         case 3:
68             e.setAttribute("type", "QGraphicsRectItem");
69             content.setAttribute("rect", rectFToString(((QGraphicsRectItem*)item)->rect().normalized()));
70             content.setAttribute("pencolor", colorToString(((QGraphicsRectItem*)item)->pen().color()));
71             content.setAttribute("penwidth", ((QGraphicsRectItem*)item)->pen().width());
72             content.setAttribute("brushcolor", colorToString(((QGraphicsRectItem*)item)->brush().color()));
73             break;
74         case 8:
75             e.setAttribute("type", "QGraphicsTextItem");
76             t = static_cast<QGraphicsTextItem *>(item);
77             // Don't save empty text nodes
78             if (t->toPlainText().simplified().isEmpty()) continue;
79             //content.appendChild(doc.createTextNode(((QGraphicsTextItem*)item)->toHtml()));
80             content.appendChild(doc.createTextNode(t->toPlainText()));
81             font = t->font();
82             content.setAttribute("font", font.family());
83             content.setAttribute("font-bold", font.bold());
84             content.setAttribute("font-pixel-size", font.pixelSize());
85             content.setAttribute("font-italic", font.italic());
86             content.setAttribute("font-underline", font.underline());
87             content.setAttribute("font-color", colorToString(t->defaultTextColor()));
88                         
89                         // Only save when necessary.
90                         if (t->data(OriginXLeft).toInt() == AxisInverted) {
91                                 content.setAttribute("kdenlive-axis-x-inverted", t->data(OriginXLeft).toInt());
92                         }
93                         if (t->data(OriginYTop).toInt() == AxisInverted) {
94                                 content.setAttribute("kdenlive-axis-y-inverted", t->data(OriginYTop).toInt());
95                         }
96             if (t->textWidth() != -1) {
97                 content.setAttribute("alignment", t->textCursor().blockFormat().alignment());
98             }
99             break;
100         default:
101             continue;
102         }
103         QDomElement pos = doc.createElement("position");
104         pos.setAttribute("x", item->pos().x());
105         pos.setAttribute("y", item->pos().y());
106         QTransform transform = item->transform();
107         QDomElement tr = doc.createElement("transform");
108         tr.appendChild(doc.createTextNode(
109                            QString("%1,%2,%3,%4,%5,%6,%7,%8,%9").arg(
110                                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())
111                        )
112                       );
113         e.setAttribute("z-index", item->zValue());
114         pos.appendChild(tr);
115
116
117         e.appendChild(pos);
118         e.appendChild(content);
119         if (item->zValue() > -1100) main.appendChild(e);
120     }
121     if (startv && endv) {
122         QDomElement endp = doc.createElement("endviewport");
123         QDomElement startp = doc.createElement("startviewport");
124         endp.setAttribute("x", endv->pos().x());
125         endp.setAttribute("y", endv->pos().y());
126         endp.setAttribute("size", endv->sceneBoundingRect().width() / 2);
127
128         startp.setAttribute("x", startv->pos().x());
129         startp.setAttribute("y", startv->pos().y());
130         startp.setAttribute("size", startv->sceneBoundingRect().width() / 2);
131
132         startp.setAttribute("z-index", startv->zValue());
133         endp.setAttribute("z-index", endv->zValue());
134         main.appendChild(startp);
135         main.appendChild(endp);
136     }
137     QDomElement backgr = doc.createElement("background");
138     QColor color = getBackgroundColor();
139     backgr.setAttribute("color", colorToString(color));
140     main.appendChild(backgr);
141
142     return doc;
143 }
144
145 /** \brief Get the background color (incl. alpha) from the document, if possibly
146   * \returns The background color of the document, inclusive alpha. If none found, returns (0,0,0,0) */
147 QColor TitleDocument::getBackgroundColor()
148 {
149     QColor color(0, 0, 0, 0);
150     if (m_scene) {
151         QList<QGraphicsItem *> items = m_scene->items();
152         for (int i = 0; i < items.size(); i++) {
153             if (items.at(i)->zValue() == -1100) {
154                 color = ((QGraphicsRectItem *)items.at(i))->brush().color();
155                 return color;
156             }
157         }
158     }
159     return color;
160 }
161
162
163 bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
164 {
165     if (!m_scene)
166         return false;
167
168     QDomDocument doc = xml(startv, endv);
169     KTemporaryFile tmpfile;
170     if (!tmpfile.open()) {
171         kWarning() << "/////  CANNOT CREATE TMP FILE in: " << tmpfile.fileName();
172         return false;
173     }
174     QFile xmlf(tmpfile.fileName());
175     xmlf.open(QIODevice::WriteOnly);
176     xmlf.write(doc.toString().toUtf8());
177     if (xmlf.error() != QFile::NoError) {
178         xmlf.close();
179         return false;
180     }
181     xmlf.close();
182     return KIO::NetAccess::upload(tmpfile.fileName(), url, 0);
183 }
184
185 int TitleDocument::loadDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
186 {
187     QString tmpfile;
188     QDomDocument doc;
189     if (!m_scene)
190         return -1;
191
192     if (KIO::NetAccess::download(url, tmpfile, 0)) {
193         QFile file(tmpfile);
194         if (file.open(QIODevice::ReadOnly)) {
195             doc.setContent(&file, false);
196             file.close();
197         } else
198             return -1;
199         KIO::NetAccess::removeTempFile(tmpfile);
200         return loadFromXml(doc, startv, endv);
201     }
202     return -1;
203 }
204
205 int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* /*startv*/, QGraphicsPolygonItem* /*endv*/)
206 {
207     QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
208     int maxZValue = 0;
209     if (titles.size()) {
210
211         QDomNodeList items = titles.item(0).childNodes();
212         for (int i = 0;i < items.count();i++) {
213             QGraphicsItem *gitem = NULL;
214             kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
215             int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
216             if (zValue > -1000) {
217                 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
218                     QDomNamedNodeMap txtProperties = items.item(i).namedItem("content").attributes();
219                     QFont font(txtProperties.namedItem("font").nodeValue());
220                     font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
221                     font.setItalic(txtProperties.namedItem("font-italic").nodeValue().toInt());
222                     font.setUnderline(txtProperties.namedItem("font-underline").nodeValue().toInt());
223                     // Older Kdenlive version did not store pixel size but point size
224                     if (txtProperties.namedItem("font-pixel-size").isNull()) {
225                         QFont f2;
226                         f2.setPointSize(txtProperties.namedItem("font-size").nodeValue().toInt());
227                         font.setPixelSize(QFontInfo(f2).pixelSize());
228                     } else font.setPixelSize(txtProperties.namedItem("font-pixel-size").nodeValue().toInt());
229                     QColor col(stringToColor(txtProperties.namedItem("font-color").nodeValue()));
230                     QGraphicsTextItem *txt = m_scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
231                     txt->setDefaultTextColor(col);
232                     txt->setTextInteractionFlags(Qt::NoTextInteraction);
233                     if (txtProperties.namedItem("alignment").isNull() == false) {
234                         txt->setTextWidth(txt->boundingRect().width());
235                         QTextCursor cur = txt->textCursor();
236                         QTextBlockFormat format = cur.blockFormat();
237                         format.setAlignment((Qt::Alignment) txtProperties.namedItem("alignment").nodeValue().toInt());
238                         cur.select(QTextCursor::Document);
239                         cur.setBlockFormat(format);
240                         txt->setTextCursor(cur);
241                         cur.clearSelection();
242                         txt->setTextCursor(cur);
243                     }
244                                         
245                                         if (!txtProperties.namedItem("kdenlive-axis-x-inverted").isNull()) {
246                                                 txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
247                                         }
248                                         if (!txtProperties.namedItem("kdenlive-axis-y-inverted").isNull()) {
249                                                 txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
250                                         }
251
252                     gitem = txt;
253                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
254                     QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
255                     QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
256                     QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
257                     double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
258                     QGraphicsRectItem *rec = m_scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
259                     gitem = rec;
260                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
261                     QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
262                     QPixmap pix(url);
263                     QGraphicsPixmapItem *rec = m_scene->addPixmap(pix);
264                     rec->setData(Qt::UserRole, url);
265                     gitem = rec;
266                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
267                     QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
268                     QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
269                     m_scene->addItem(rec);
270                     rec->setData(Qt::UserRole, url);
271                     gitem = rec;
272                 }
273             }
274             //pos and transform
275             if (gitem) {
276                 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
277                           items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
278                 gitem->setPos(p);
279                 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
280                 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
281                 if (zValue > maxZValue) maxZValue = zValue;
282                 gitem->setZValue(zValue);
283                 gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
284             }
285             if (items.item(i).nodeName() == "background") {
286                 kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
287                 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
288                 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
289                 QList<QGraphicsItem *> items = m_scene->items();
290                 for (int i = 0; i < items.size(); i++) {
291                     if (items.at(i)->zValue() == -1100) {
292                         ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
293                         break;
294                     }
295                 }
296             } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
297                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
298                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
299                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
300                     kDebug() << width << rect;
301                     startv->setPolygon(rect);
302                     startv->setPos(p);
303                 } else if (items.item(i).nodeName() == "endviewport" && endv) {
304                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
305                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
306                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
307                     kDebug() << width << rect;
308                     endv->setPolygon(rect);
309                     endv->setPos(p);
310                 }*/
311         }
312     }
313     return maxZValue;
314 }
315
316 QString TitleDocument::colorToString(const QColor& c)
317 {
318     QString ret = "%1,%2,%3,%4";
319     ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
320     return ret;
321 }
322
323 QString TitleDocument::rectFToString(const QRectF& c)
324 {
325     QString ret = "%1,%2,%3,%4";
326     ret = ret.arg(c.top()).arg(c.left()).arg(c.width()).arg(c.height());
327     return ret;
328 }
329
330 QRectF TitleDocument::stringToRect(const QString & s)
331 {
332
333     QStringList l = s.split(',');
334     if (l.size() < 4)
335         return QRectF();
336     return QRectF(l.at(0).toDouble(), l.at(1).toDouble(), l.at(2).toDouble(), l.at(3).toDouble()).normalized();
337 }
338
339 QColor TitleDocument::stringToColor(const QString & s)
340 {
341     QStringList l = s.split(',');
342     if (l.size() < 4)
343         return QColor();
344     return QColor(l.at(0).toInt(), l.at(1).toInt(), l.at(2).toInt(), l.at(3).toInt());;
345 }
346 QTransform TitleDocument::stringToTransform(const QString& s)
347 {
348     QStringList l = s.split(',');
349     if (l.size() < 9)
350         return QTransform();
351     return QTransform(
352                l.at(0).toDouble(), l.at(1).toDouble(), l.at(2).toDouble(),
353                l.at(3).toDouble(), l.at(4).toDouble(), l.at(5).toDouble(),
354                l.at(6).toDouble(), l.at(7).toDouble(), l.at(8).toDouble()
355            );
356 }