]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
Connect project monitor
[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), m_commandStack(new KUndoStack())
32 {
33   if (!url.isEmpty()) {
34     QString tmpFile;
35     if(KIO::NetAccess::download(url.path(), tmpFile, parent))
36     {
37       QFile file(tmpFile);
38       m_document.setContent(&file, false);
39       file.close();
40       m_projectName = url.fileName();
41       KIO::NetAccess::removeTempFile(tmpFile);
42     }
43     else
44     {
45       KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
46     }
47   }
48   else {
49     // Creating new document
50     QDomElement westley = m_document.createElement("westley");
51     m_document.appendChild(westley);
52     QDomElement doc = m_document.createElement("kdenlivedoc");
53     doc.setAttribute("version", "0.6");
54     westley.appendChild(doc);
55     QDomElement props = m_document.createElement("properties");
56     doc.setAttribute("width", m_width);
57     doc.setAttribute("height", m_height);
58     doc.setAttribute("projectfps", m_fps);
59     doc.appendChild(props);
60
61
62     /*QDomElement westley = m_document.createElement("westley");
63     m_document.appendChild(westley);*/
64
65
66     QDomElement tractor = m_document.createElement("tractor");
67     QDomElement multitrack = m_document.createElement("multitrack");
68
69     QDomElement playlist1 = m_document.createElement("playlist");
70     playlist1.setAttribute("id", "playlist1");
71     multitrack.appendChild(playlist1);
72     QDomElement playlist2 = m_document.createElement("playlist");
73     playlist2.setAttribute("id", "playlist2");
74     multitrack.appendChild(playlist2);
75     QDomElement playlist3 = m_document.createElement("playlist");
76     multitrack.appendChild(playlist3);
77     playlist3.setAttribute("id", "playlist3");
78     QDomElement playlist4 = m_document.createElement("playlist");
79     playlist4.setAttribute("hide", "video");
80     multitrack.appendChild(playlist4);
81     playlist1.setAttribute("id", "playlist4");
82     QDomElement playlist5 = m_document.createElement("playlist");
83     playlist5.setAttribute("hide", "video");
84     multitrack.appendChild(playlist5);
85     playlist1.setAttribute("id", "playlist5");
86     tractor.appendChild(multitrack);
87     doc.appendChild(tractor);
88     
89   }
90   if (fps == 30000.0 / 1001.0 ) m_timecode.setFormat(30, true);
91     else m_timecode.setFormat((int) fps);
92 }
93
94 KdenliveDoc::~KdenliveDoc()
95 {
96   delete m_commandStack;
97 }
98
99 KUndoStack *KdenliveDoc::commandStack()
100 {
101   return m_commandStack;
102 }
103
104 void KdenliveDoc::setRenderer(Render *render)
105 {
106   m_render = render;
107   if (m_render) m_render->setSceneList(m_document);
108 }
109
110 Render *KdenliveDoc::renderer()
111 {
112   return m_render;
113 }
114
115 QString KdenliveDoc::producerName(int id)
116 {
117   QString result = "unnamed";
118   QDomNodeList prods = producersList();
119   int ct = prods.count();
120   for (int i = 0; i <  ct ; i++)
121   {
122     QDomElement e = prods.item(i).toElement();
123     if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
124       result = e.attribute("name");
125       if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
126       break;
127     }
128   }
129   return result;
130 }
131
132 void KdenliveDoc::setProducerDuration(int id, int duration)
133 {
134   QDomNodeList prods = producersList();
135   int ct = prods.count();
136   for (int i = 0; i <  ct ; i++)
137   {
138     QDomElement e = prods.item(i).toElement();
139     if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
140       e.setAttribute("duration", QString::number(duration));
141       break;
142     }
143   }
144 }
145
146 int KdenliveDoc::getProducerDuration(int id)
147 {
148   int result = 0;
149   QDomNodeList prods = producersList();
150   int ct = prods.count();
151   for (int i = 0; i <  ct ; i++)
152   {
153     QDomElement e = prods.item(i).toElement();
154     if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
155       result = e.attribute("duration").toInt();
156       break;
157     }
158   }
159   return result;
160 }
161
162
163 QDomDocument KdenliveDoc::generateSceneList()
164 {
165     QDomDocument doc;
166     QDomElement westley = doc.createElement("westley");
167     doc.appendChild(westley);
168     QDomElement prod = doc.createElement("producer");
169 }
170
171 QDomDocument KdenliveDoc::toXml()
172 {
173   return m_document;
174 }
175
176 Timecode KdenliveDoc::timecode()
177 {
178   return m_timecode;
179 }
180
181 QString KdenliveDoc::documentName()
182 {
183   return m_projectName;
184 }
185
186 QDomNodeList KdenliveDoc::producersList()
187 {
188   return m_document.elementsByTagName("producer");
189 }
190
191 void KdenliveDoc::setProducers(QDomElement doc)
192 {
193   QDomNode kdenlivedocument = m_document.elementsByTagName("kdenlivedoc").item(0);
194
195   QDomNodeList list = m_document.elementsByTagName("producer");
196   int ct = list.count();
197     kDebug()<<"DELETING CHILD PRODUCERS: "<<ct;
198   for (int i = ct; i > 0; i--) {
199     kdenlivedocument.removeChild(list.item(i));
200    }
201
202   QDomNode n = doc.firstChild();
203   ct = 0;
204   while(!n.isNull()) {
205      QDomElement e = n.toElement(); // try to convert the node to an element.
206      if(!e.isNull() && e.tagName() == "producer") {
207          kdenlivedocument.appendChild(m_document.importNode(e, true));
208         ct++;
209      }
210      n = n.nextSibling();
211   }
212   kDebug()<<"ADDING CHILD PRODS: "<<ct<<"\n";
213   //kDebug()<<m_document.toString();
214 }
215
216 double KdenliveDoc::fps()
217 {
218   return m_fps;
219 }
220
221 int KdenliveDoc::width()
222 {
223   return m_width;
224 }
225
226 int KdenliveDoc::height()
227 {
228   return m_height;
229 }
230
231 KUrl KdenliveDoc::url()
232 {
233   return m_url;
234 }
235
236 #include "kdenlivedoc.moc"
237