1 /***************************************************************************
2 titledocument.h - description
5 copyright : (C) 2008 by Marco Gittler
6 email : g.marco@freenet.de
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
17 #include "titledocument.h"
18 #include <QGraphicsScene>
19 #include <QDomDocument>
20 #include <QDomElement>
21 #include <QGraphicsItem>
22 #include <QGraphicsRectItem>
23 #include <QGraphicsTextItem>
26 #include <kio/netaccess.h>
28 TitleDocument::TitleDocument(){
32 void TitleDocument::setScene(QGraphicsScene* _scene){
36 bool TitleDocument::saveDocument(const KUrl& url,QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv){
42 QDomElement main=doc.createElement("kdenlivetitle");
43 doc.appendChild(main);
45 foreach(QGraphicsItem* item, scene->items()){
46 QDomElement e=doc.createElement("item");
47 QDomElement content=doc.createElement("content");
49 switch (item->type()){
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()) );
58 e.setAttribute("type","QGraphicsTextItem");
59 content.appendChild(doc.createTextNode( ((QGraphicsTextItem*)item)->toHtml() ) );
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())
78 e.appendChild(content);
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);
88 startp.setAttribute("x",startv->pos().x());
89 startp.setAttribute("y",startv->pos().y());
90 startp.setAttribute("size",startv->sceneBoundingRect().width()/2);
92 main.appendChild(startp);
93 main.appendChild(endp);
95 QDomElement backgr=doc.createElement("background");
96 backgr.setAttribute("color",colorToString(scene->backgroundBrush().color()));
97 main.appendChild(backgr);
99 QString tmpfile="/tmp/newtitle";
101 xmlf.open(QIODevice::WriteOnly);
102 xmlf.write(doc.toString().toAscii());
104 kDebug() << KIO::NetAccess::upload(tmpfile,url,0);
107 bool TitleDocument::loadDocument(const KUrl& url ,QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) {
110 double aspect_ratio=4.0/3.0;
114 if (KIO::NetAccess::download(url, tmpfile, 0)) {
116 if (file.open(QIODevice::ReadOnly)) {
117 doc.setContent(&file, false);
121 KIO::NetAccess::removeTempFile(tmpfile);
122 QDomNodeList titles=doc.elementsByTagName("kdenlivetitle");
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());
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) ) );
144 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
145 items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
147 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
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);
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);
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());
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());
190 QRectF TitleDocument::stringToRect(const QString & s){
192 QStringList l=s.split(",");
195 return QRectF(l[0].toDouble(),l[1].toDouble(),l[2].toDouble(),l[3].toDouble());
198 QColor TitleDocument::stringToColor(const QString & s){
199 QStringList l=s.split(",");
202 return QColor(l[0].toInt(),l[1].toInt(),l[2].toInt(),l[3].toInt());;
204 QTransform TitleDocument::stringToTransform(const QString& s){
205 QStringList l=s.split(",");
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()