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