]> git.sesse.net Git - kdenlive/blob - src/titledocument.cpp
load and save titles (except svg's)
[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 <kio/netaccess.h>
27
28 TitleDocument::TitleDocument(){
29         scene=NULL;     
30 }
31
32 void TitleDocument::setScene(QGraphicsScene* _scene){
33         scene=_scene;
34 }
35
36 bool TitleDocument::saveDocument(const KUrl& url,QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv){
37         QDomDocument doc;
38         
39         if (!scene)
40                 return false;
41         
42         QDomElement main=doc.createElement("kdenlivetitle");
43         doc.appendChild(main);
44         
45         foreach(QGraphicsItem* item, scene->items()){
46                 QDomElement e=doc.createElement("item");
47                 QDomElement content=doc.createElement("content");
48
49                 switch (item->type()){
50                         case 3:
51                                 e.setAttribute("type","QGraphicsRectItem");
52                                 content.setAttribute("rect",rectFToString(((QGraphicsRectItem*)item)->rect() ));
53                                 content.setAttribute("pencolor",colorToString(((QGraphicsRectItem*)item)->pen().color()) );
54                                 content.setAttribute("penwidth",((QGraphicsRectItem*)item)->pen().width() );
55                                 content.setAttribute("brushcolor",colorToString(((QGraphicsRectItem*)item)->brush().color()) );
56                                 break;
57                         case 8:
58                                 e.setAttribute("type","QGraphicsTextItem");
59                                 content.appendChild(doc.createTextNode( ((QGraphicsTextItem*)item)->toHtml() ) );
60                                 break;
61                         default:
62                                 continue;
63                 }
64                 QDomElement pos=doc.createElement("position");
65                 pos.setAttribute("x",item->pos().x());
66                 pos.setAttribute("y",item->pos().y());
67                 QTransform transform=item->transform();
68                 QDomElement tr=doc.createElement("transform");
69                 tr.appendChild(doc.createTextNode(
70                   QString("%1,%2,%3,%4,%5,%6,%7,%8,%9").arg(
71                     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())
72                   )
73                 );
74                 pos.appendChild(tr);
75                 
76                 
77                 e.appendChild(pos);
78                 e.appendChild(content);
79                 main.appendChild(e);
80         }
81         if (startv && endv){
82                 QDomElement endp=doc.createElement("endviewport");
83                 QDomElement startp=doc.createElement("startviewport");
84                 endp.setAttribute("x",endv->pos().x());
85                 endp.setAttribute("y",endv->pos().y());
86                 endp.setAttribute("size",endv->sceneBoundingRect().width()/2);
87                 
88                 startp.setAttribute("x",startv->pos().x());
89                 startp.setAttribute("y",startv->pos().y());
90                 startp.setAttribute("size",startv->sceneBoundingRect().width()/2);
91                 
92                 main.appendChild(startp);
93                 main.appendChild(endp);
94         }
95         QDomElement backgr=doc.createElement("background");
96         backgr.setAttribute("color",colorToString(scene->backgroundBrush().color()));
97         main.appendChild(backgr);
98         
99         QString tmpfile="/tmp/newtitle";
100         QFile xmlf(tmpfile);
101         xmlf.open(QIODevice::WriteOnly);
102         xmlf.write(doc.toString().toAscii());
103         xmlf.close();
104         kDebug() << KIO::NetAccess::upload(tmpfile,url,0);
105 }
106
107 bool TitleDocument::loadDocument(const KUrl& url ,QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
108         QString tmpfile;
109         QDomDocument doc;
110         double aspect_ratio=4.0/3.0;
111         if (!scene)
112                 return false;
113         
114         if (KIO::NetAccess::download(url, tmpfile, 0)) {
115                 QFile file(tmpfile);
116                 if (file.open(QIODevice::ReadOnly)) {
117                         doc.setContent(&file, false);
118                         file.close();
119                 }else
120                         return false;
121                 KIO::NetAccess::removeTempFile(tmpfile);
122                 QDomNodeList titles=doc.elementsByTagName("kdenlivetitle");
123                 if (titles.size()){
124                         
125                         QDomNodeList items=titles.item(0).childNodes();
126                         for(int i=0;i<items.count();i++){
127                                 QGraphicsItem *gitem=NULL;
128                                 kDebug() << items.item(i).attributes().namedItem("type").nodeValue();
129                                 if (items.item(i).attributes().namedItem("type").nodeValue()=="QGraphicsTextItem"){
130                                         QGraphicsTextItem *txt=scene->addText("");
131                                         txt->setHtml(items.item(i).namedItem("content").firstChild().nodeValue());
132                                         gitem=txt;
133                                 }else
134                                 if (items.item(i).attributes().namedItem("type").nodeValue()=="QGraphicsRectItem"){
135                                         QString rect=items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
136                                         QString br_str=items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
137                                         QString pen_str=items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
138                                         double penwidth=items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
139                                         QGraphicsRectItem *rec=scene->addRect(stringToRect(rect),QPen(QBrush(stringToColor(pen_str)),penwidth),QBrush(stringToColor(br_str) ) );
140                                         gitem=rec;
141                                 }
142                                 //pos and transform
143                                 if (gitem ){
144                                         QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
145                                                 items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
146                                         gitem->setPos(p);
147                                         gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
148                                 }
149                                 if (items.item(i).nodeName()=="background"){
150                                         kDebug() << items.item(i).attributes().namedItem("color").nodeValue();
151                                         scene->setBackgroundBrush(QBrush(stringToColor(items.item(i).attributes().namedItem("color").nodeValue())));
152                                 }else   if (items.item(i).nodeName()=="startviewport" && startv){
153                                         QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(),items.item(i).attributes().namedItem("y").nodeValue().toDouble());
154                                         double width=items.item(i).attributes().namedItem("size").nodeValue().toDouble();
155                                         QRectF rect(-width,-width/aspect_ratio,width*2.0,width/aspect_ratio*2.0);
156                                         kDebug() << width << rect;
157                                         startv->setPolygon(rect);
158                                         startv->setPos(p);
159                                 }else   if (items.item(i).nodeName()=="endviewport" && endv){
160                                         QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(),items.item(i).attributes().namedItem("y").nodeValue().toDouble());
161                                         double width=items.item(i).attributes().namedItem("size").nodeValue().toDouble();
162                                         QRectF rect(-width,-width/aspect_ratio,width*2.0,width/aspect_ratio*2.0);
163                                         kDebug() << width << rect;
164                                         endv->setPolygon(rect);
165                                         endv->setPos(p);
166                                 }
167                                 
168                         }
169                         
170                         
171                 }
172                 
173                 
174         }
175         return true;
176 }
177
178 QString TitleDocument::colorToString(const QColor& c){
179         QString ret="%1,%2,%3,%4";
180         ret=ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
181         return ret;
182 }
183
184 QString TitleDocument::rectFToString(const QRectF& c){
185         QString ret="%1,%2,%3,%4";
186         ret=ret.arg(c.x()).arg(c.y()).arg(c.width()).arg(c.height());
187         return ret;
188 }
189
190 QRectF TitleDocument::stringToRect(const QString & s){
191         
192         QStringList l=s.split(",");
193         if (l.size()<4)
194                 return QRectF();
195         return QRectF(l[0].toDouble(),l[1].toDouble(),l[2].toDouble(),l[3].toDouble());
196 }
197
198 QColor TitleDocument::stringToColor(const QString & s){
199         QStringList l=s.split(",");
200         if (l.size()<4)
201                 return QColor();
202         return QColor(l[0].toInt(),l[1].toInt(),l[2].toInt(),l[3].toInt());;
203 }
204 QTransform TitleDocument::stringToTransform(const QString& s){
205         QStringList l=s.split(",");
206         if (l.size()<9)
207                 return QTransform();
208         return QTransform(
209                 l[0].toDouble(),l[1].toDouble(),l[2].toDouble(),
210                 l[3].toDouble(),l[4].toDouble(),l[5].toDouble(),
211                 l[6].toDouble(),l[7].toDouble(),l[8].toDouble()
212                 );
213 }