]> git.sesse.net Git - kdenlive/blob - src/titledocument.cpp
Title widget improvements:
[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 <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 bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
38     QDomDocument doc;
39
40     if (!scene)
41         return false;
42
43     QDomElement main = doc.createElement("kdenlivetitle");
44     doc.appendChild(main);
45
46     foreach(QGraphicsItem* item, scene->items()) {
47         QDomElement e = doc.createElement("item");
48         QDomElement content = doc.createElement("content");
49         QFont font;
50         QGraphicsTextItem *t;
51
52         switch (item->type()) {
53         case 3:
54             e.setAttribute("type", "QGraphicsRectItem");
55             content.setAttribute("rect", rectFToString(((QGraphicsRectItem*)item)->rect()));
56             content.setAttribute("pencolor", colorToString(((QGraphicsRectItem*)item)->pen().color()));
57             content.setAttribute("penwidth", ((QGraphicsRectItem*)item)->pen().width());
58             content.setAttribute("brushcolor", colorToString(((QGraphicsRectItem*)item)->brush().color()));
59             break;
60         case 8:
61             e.setAttribute("type", "QGraphicsTextItem");
62             t = static_cast<QGraphicsTextItem *>(item);
63             //content.appendChild(doc.createTextNode(((QGraphicsTextItem*)item)->toHtml()));
64             content.appendChild(doc.createTextNode(t->toPlainText()));
65             font = t->font();
66             e.setAttribute("font", font.family());
67             e.setAttribute("font-bold", font.bold());
68             e.setAttribute("font-size", font.pointSize());
69             e.setAttribute("font-italic", font.italic());
70             e.setAttribute("font-underline", font.underline());
71             e.setAttribute("font-color", colorToString(t->defaultTextColor()));
72             break;
73         default:
74             continue;
75         }
76         QDomElement pos = doc.createElement("position");
77         pos.setAttribute("x", item->pos().x());
78         pos.setAttribute("y", item->pos().y());
79         QTransform transform = item->transform();
80         QDomElement tr = doc.createElement("transform");
81         tr.appendChild(doc.createTextNode(
82                            QString("%1,%2,%3,%4,%5,%6,%7,%8,%9").arg(
83                                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())
84                        )
85                       );
86         e.setAttribute("z-index", item->zValue());
87         pos.appendChild(tr);
88
89
90         e.appendChild(pos);
91         e.appendChild(content);
92         if (item->zValue() > -1100) main.appendChild(e);
93     }
94     if (startv && endv) {
95         QDomElement endp = doc.createElement("endviewport");
96         QDomElement startp = doc.createElement("startviewport");
97         endp.setAttribute("x", endv->pos().x());
98         endp.setAttribute("y", endv->pos().y());
99         endp.setAttribute("size", endv->sceneBoundingRect().width() / 2);
100
101         startp.setAttribute("x", startv->pos().x());
102         startp.setAttribute("y", startv->pos().y());
103         startp.setAttribute("size", startv->sceneBoundingRect().width() / 2);
104
105         startp.setAttribute("z-index", startv->zValue());
106         endp.setAttribute("z-index", endv->zValue());
107         main.appendChild(startp);
108         main.appendChild(endp);
109     }
110     QDomElement backgr = doc.createElement("background");
111     QList<QGraphicsItem *> items = scene->items();
112     QColor color(0, 0, 0, 0);
113     for (int i = 0; i < items.size(); i++) {
114         if (items.at(i)->zValue() == -1100) {
115             color = ((QGraphicsRectItem *)items.at(i))->brush().color();
116             break;
117         }
118     }
119     backgr.setAttribute("color", colorToString(color));
120     main.appendChild(backgr);
121
122     KTemporaryFile tmpfile;
123     if (!tmpfile.open()) kWarning() << "/////  CANNOT CREATE TMP FILE in: " << tmpfile.fileName();
124     QFile xmlf(tmpfile.fileName());
125     xmlf.open(QIODevice::WriteOnly);
126     xmlf.write(doc.toString().toAscii());
127     xmlf.close();
128     kDebug() << KIO::NetAccess::upload(tmpfile.fileName(), url, 0);
129
130 }
131
132 int TitleDocument::loadDocument(const KUrl& url , QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
133     QString tmpfile;
134     QDomDocument doc;
135     int maxZValue = 0;
136     double aspect_ratio = 4.0 / 3.0;
137     if (!scene)
138         return -1;
139
140     if (KIO::NetAccess::download(url, tmpfile, 0)) {
141         QFile file(tmpfile);
142         if (file.open(QIODevice::ReadOnly)) {
143             doc.setContent(&file, false);
144             file.close();
145         } else
146             return -1;
147         KIO::NetAccess::removeTempFile(tmpfile);
148         QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
149         if (titles.size()) {
150
151             QDomNodeList items = titles.item(0).childNodes();
152             for (int i = 0;i < items.count();i++) {
153                 QGraphicsItem *gitem = NULL;
154                 kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
155                 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
156                 if (zValue > -1000)
157                     if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
158                         QFont font(items.item(i).attributes().namedItem("font").nodeValue());
159                         font.setBold(items.item(i).attributes().namedItem("font-bold").nodeValue().toInt());
160                         font.setItalic(items.item(i).attributes().namedItem("font-italic").nodeValue().toInt());
161                         font.setUnderline(items.item(i).attributes().namedItem("font-underline").nodeValue().toInt());
162                         font.setPointSize(items.item(i).attributes().namedItem("font-size").nodeValue().toInt());
163                         QColor col(stringToColor(items.item(i).attributes().namedItem("font-color").nodeValue()));
164                         QGraphicsTextItem *txt = scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
165                         txt->setDefaultTextColor(col);
166                         txt->setTextInteractionFlags(Qt::NoTextInteraction);
167                         gitem = txt;
168                     } else
169                         if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
170                             QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
171                             QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
172                             QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
173                             double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
174                             QGraphicsRectItem *rec = scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
175                             gitem = rec;
176                         }
177                 //pos and transform
178                 if (gitem) {
179                     QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
180                               items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
181                     gitem->setPos(p);
182                     gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
183                     int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
184                     if (zValue > maxZValue) maxZValue = zValue;
185                     gitem->setZValue(zValue);
186                     gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
187                 }
188                 if (items.item(i).nodeName() == "background") {
189                     kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
190                     QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
191                     //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
192                     QList<QGraphicsItem *> items = scene->items();
193                     for (int i = 0; i < items.size(); i++) {
194                         if (items.at(i)->zValue() == -1100) {
195                             ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
196                             break;
197                         }
198                     }
199                 } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
200                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
201                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
202                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
203                     kDebug() << width << rect;
204                     startv->setPolygon(rect);
205                     startv->setPos(p);
206                 } else if (items.item(i).nodeName() == "endviewport" && endv) {
207                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
208                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
209                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
210                     kDebug() << width << rect;
211                     endv->setPolygon(rect);
212                     endv->setPos(p);
213                 }*/
214             }
215         }
216     }
217     return maxZValue;
218 }
219
220 QString TitleDocument::colorToString(const QColor& c) {
221     QString ret = "%1,%2,%3,%4";
222     ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
223     return ret;
224 }
225
226 QString TitleDocument::rectFToString(const QRectF& c) {
227     QString ret = "%1,%2,%3,%4";
228     ret = ret.arg(c.x()).arg(c.y()).arg(c.width()).arg(c.height());
229     return ret;
230 }
231
232 QRectF TitleDocument::stringToRect(const QString & s) {
233
234     QStringList l = s.split(",");
235     if (l.size() < 4)
236         return QRectF();
237     return QRectF(l[0].toDouble(), l[1].toDouble(), l[2].toDouble(), l[3].toDouble());
238 }
239
240 QColor TitleDocument::stringToColor(const QString & s) {
241     QStringList l = s.split(",");
242     if (l.size() < 4)
243         return QColor();
244     return QColor(l[0].toInt(), l[1].toInt(), l[2].toInt(), l[3].toInt());;
245 }
246 QTransform TitleDocument::stringToTransform(const QString& s) {
247     QStringList l = s.split(",");
248     if (l.size() < 9)
249         return QTransform();
250     return QTransform(
251                l[0].toDouble(), l[1].toDouble(), l[2].toDouble(),
252                l[3].toDouble(), l[4].toDouble(), l[5].toDouble(),
253                l[6].toDouble(), l[7].toDouble(), l[8].toDouble()
254            );
255 }