]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
adapt to latest MLT changes
[kdenlive] / src / kdenlivedoc.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20 #include <KDebug>
21 #include <KStandardDirs>
22 #include <KMessageBox>
23 #include <KLocale>
24 #include <KFileDialog>
25 #include <KIO/NetAccess>
26
27
28 #include "kdenlivedoc.h"
29
30
31 KdenliveDoc::KdenliveDoc(const KUrl &url, double fps, int width, int height, QWidget *parent):QObject(parent), m_render(NULL), m_url(url), m_fps(fps), m_width(width), m_height(height), m_projectName(NULL)
32 {
33
34   m_commandStack = new KUndoStack();
35   if (!url.isEmpty()) {
36     QString tmpFile;
37     if(KIO::NetAccess::download(url.path(), tmpFile, parent))
38     {
39       QFile file(tmpFile);
40       m_document.setContent(&file, false);
41       file.close();
42       m_projectName = url.fileName();
43       KIO::NetAccess::removeTempFile(tmpFile);
44     }
45     else
46     {
47       KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
48     }
49   }
50   else {
51     // Creating new document
52     QDomElement westley = m_document.createElement("westley");
53     m_document.appendChild(westley);
54     QDomElement doc = m_document.createElement("kdenlivedoc");
55     doc.setAttribute("version", "0.6");
56     westley.appendChild(doc);
57     QDomElement props = m_document.createElement("properties");
58     doc.setAttribute("width", m_width);
59     doc.setAttribute("height", m_height);
60     doc.setAttribute("projectfps", m_fps);
61     doc.appendChild(props);
62
63
64     /*QDomElement westley = m_document.createElement("westley");
65     m_document.appendChild(westley);*/
66
67
68     QDomElement tractor = m_document.createElement("tractor");
69     QDomElement multitrack = m_document.createElement("multitrack");
70
71     QDomElement playlist1 = m_document.createElement("playlist");
72     playlist1.setAttribute("id", "playlist1");
73     multitrack.appendChild(playlist1);
74     QDomElement playlist2 = m_document.createElement("playlist");
75     playlist2.setAttribute("id", "playlist2");
76     multitrack.appendChild(playlist2);
77     QDomElement playlist3 = m_document.createElement("playlist");
78     multitrack.appendChild(playlist3);
79     playlist3.setAttribute("id", "playlist3");
80     QDomElement playlist4 = m_document.createElement("playlist");
81     playlist4.setAttribute("hide", "video");
82     multitrack.appendChild(playlist4);
83     playlist1.setAttribute("id", "playlist4");
84     QDomElement playlist5 = m_document.createElement("playlist");
85     playlist5.setAttribute("hide", "video");
86     multitrack.appendChild(playlist5);
87     playlist1.setAttribute("id", "playlist5");
88     tractor.appendChild(multitrack);
89     doc.appendChild(tractor);
90     
91   }
92   if (fps == 30000.0 / 1001.0 ) m_timecode.setFormat(30, true);
93     else m_timecode.setFormat((int) fps);
94 }
95
96 KdenliveDoc::~KdenliveDoc()
97 {
98   delete m_commandStack;
99 }
100
101 KUndoStack *KdenliveDoc::commandStack()
102 {
103   return m_commandStack;
104 }
105
106 void KdenliveDoc::setRenderer(Render *render)
107 {
108   m_render = render;
109   if (m_render) m_render->setSceneList(m_document);
110 }
111
112 QString KdenliveDoc::producerName(int id)
113 {
114   QString result = "unnamed";
115   QDomNodeList prods = producersList();
116   int ct = prods.count();
117   for (int i = 0; i <  ct ; i++)
118   {
119     QDomElement e = prods.item(i).toElement();
120     if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
121       result = e.attribute("name");
122       if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
123       break;
124     }
125   }
126   return result;
127 }
128
129 void KdenliveDoc::setProducerDuration(int id, int duration)
130 {
131   QDomNodeList prods = producersList();
132   int ct = prods.count();
133   for (int i = 0; i <  ct ; i++)
134   {
135     QDomElement e = prods.item(i).toElement();
136     if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
137       e.setAttribute("duration", QString::number(duration));
138       break;
139     }
140   }
141 }
142
143 int KdenliveDoc::getProducerDuration(int id)
144 {
145   int result = 0;
146   QDomNodeList prods = producersList();
147   int ct = prods.count();
148   for (int i = 0; i <  ct ; i++)
149   {
150     QDomElement e = prods.item(i).toElement();
151     if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
152       result = e.attribute("duration").toInt();
153       break;
154     }
155   }
156   return result;
157 }
158
159
160 QDomDocument KdenliveDoc::generateSceneList()
161 {
162     QDomDocument doc;
163     QDomElement westley = doc.createElement("westley");
164     doc.appendChild(westley);
165     QDomElement prod = doc.createElement("producer");
166 }
167
168 QDomDocument KdenliveDoc::toXml()
169 {
170   return m_document;
171 }
172
173 Timecode KdenliveDoc::timecode()
174 {
175   return m_timecode;
176 }
177
178 QString KdenliveDoc::documentName()
179 {
180   return m_projectName;
181 }
182
183 QDomNodeList KdenliveDoc::producersList()
184 {
185   return m_document.elementsByTagName("producer");
186 }
187
188 void KdenliveDoc::setProducers(QDomElement doc)
189 {
190   QDomNode kdenlivedocument = m_document.elementsByTagName("kdenlivedoc").item(0);
191
192   QDomNodeList list = m_document.elementsByTagName("producer");
193   int ct = list.count();
194     kDebug()<<"DELETING CHILD PRODUCERS: "<<ct;
195   for (int i = ct; i > 0; i--) {
196     kdenlivedocument.removeChild(list.item(i));
197    }
198
199   QDomNode n = doc.firstChild();
200   ct = 0;
201   while(!n.isNull()) {
202      QDomElement e = n.toElement(); // try to convert the node to an element.
203      if(!e.isNull() && e.tagName() == "producer") {
204          kdenlivedocument.appendChild(m_document.importNode(e, true));
205         ct++;
206      }
207      n = n.nextSibling();
208   }
209   kDebug()<<"ADDING CHILD PRODS: "<<ct<<"\n";
210   //kDebug()<<m_document.toString();
211 }
212
213 double KdenliveDoc::fps()
214 {
215   return m_fps;
216 }
217
218 int KdenliveDoc::width()
219 {
220   return m_width;
221 }
222
223 int KdenliveDoc::height()
224 {
225   return m_height;
226 }
227
228 KUrl KdenliveDoc::url()
229 {
230   return m_url;
231 }
232
233 #include "kdenlivedoc.moc"
234