]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
Fix audio thumbs progress info when window was not focused
[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 #include "docclipbase.h"
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   m_clipManager = new ClipManager(this);
34   if (!url.isEmpty()) {
35     QString tmpFile;
36     if(KIO::NetAccess::download(url.path(), tmpFile, parent))
37     {
38       QFile file(tmpFile);
39       m_document.setContent(&file, false);
40       file.close();
41       m_projectName = url.fileName();
42       KIO::NetAccess::removeTempFile(tmpFile);
43     }
44     else
45     {
46       KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
47     }
48   }
49   else {
50     // Creating new document
51     QDomElement westley = m_document.createElement("westley");
52     m_document.appendChild(westley);
53     QDomElement doc = m_document.createElement("kdenlivedoc");
54     doc.setAttribute("version", "0.6");
55     westley.appendChild(doc);
56     QDomElement props = m_document.createElement("properties");
57     doc.setAttribute("width", m_width);
58     doc.setAttribute("height", m_height);
59     doc.setAttribute("projectfps", m_fps);
60     doc.appendChild(props);
61
62
63     /*QDomElement westley = m_document.createElement("westley");
64     m_document.appendChild(westley);*/
65
66
67     QDomElement tractor = m_document.createElement("tractor");
68     QDomElement multitrack = m_document.createElement("multitrack");
69     QDomElement playlist = m_document.createElement("playlist");
70     QDomElement producer = m_document.createElement("producer");
71     /*producer.setAttribute("mlt_service", "colour");
72     producer.setAttribute("colour", "red");
73     playlist.appendChild(producer);*/
74     multitrack.appendChild(playlist);
75     QDomElement playlist1 = m_document.createElement("playlist");
76     playlist1.setAttribute("id", "playlist1");
77     playlist1.setAttribute("hide", "video");
78     multitrack.appendChild(playlist1);
79     QDomElement playlist2 = m_document.createElement("playlist");
80     playlist2.setAttribute("id", "playlist2");
81     playlist2.setAttribute("hide", "video");
82     multitrack.appendChild(playlist2);
83     QDomElement playlist3 = m_document.createElement("playlist");
84     multitrack.appendChild(playlist3);
85     playlist3.setAttribute("id", "playlist3");
86     QDomElement playlist4 = m_document.createElement("playlist");
87     multitrack.appendChild(playlist4);
88     playlist4.setAttribute("id", "playlist4");
89     QDomElement playlist5 = m_document.createElement("playlist");
90     multitrack.appendChild(playlist5);
91     playlist5.setAttribute("id", "playlist5");
92     tractor.appendChild(multitrack);
93
94     for (uint i = 2; i < 6 ; i++) {
95       QDomElement transition = m_document.createElement("transition");
96       transition.setAttribute("in", "0");
97       //TODO: Make audio mix last for all project duration
98       transition.setAttribute("out", "15000");
99       transition.setAttribute("a_track", QString::number(1));
100       transition.setAttribute("b_track", QString::number(i));
101       transition.setAttribute("mlt_service", "mix");
102       transition.setAttribute("combine", "1");
103       tractor.appendChild(transition);
104     }
105
106     doc.appendChild(tractor);
107     
108   }
109   if (fps == 30000.0 / 1001.0 ) m_timecode.setFormat(30, true);
110     else m_timecode.setFormat((int) fps);
111 }
112
113 KdenliveDoc::~KdenliveDoc()
114 {
115   delete m_commandStack;
116   delete m_clipManager;
117 }
118
119 ClipManager *KdenliveDoc::clipManager()
120 {
121   return m_clipManager;
122 }
123
124 void KdenliveDoc::setThumbsProgress(KUrl url, int progress)
125 {
126   emit thumbsProgress(url, progress);
127 }
128
129 KUndoStack *KdenliveDoc::commandStack()
130 {
131   return m_commandStack;
132 }
133
134 void KdenliveDoc::setRenderer(Render *render)
135 {
136   m_render = render;
137   if (m_render) m_render->setSceneList(m_document);
138 }
139
140 Render *KdenliveDoc::renderer()
141 {
142   return m_render;
143 }
144
145 void KdenliveDoc::updateClip(int id)
146 {
147   emit updateClipDisplay(id);
148 }
149
150 int KdenliveDoc::getFramePos(QString duration)
151 {
152   return m_timecode.getFrameCount(duration, m_fps);
153 }
154
155 QString KdenliveDoc::producerName(int id)
156 {
157   QString result = "unnamed";
158   QDomNodeList prods = producersList();
159   int ct = prods.count();
160   for (int i = 0; i <  ct ; i++)
161   {
162     QDomElement e = prods.item(i).toElement();
163     if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
164       result = e.attribute("name");
165       if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
166       break;
167     }
168   }
169   return result;
170 }
171
172 void KdenliveDoc::setProducerDuration(int id, int duration)
173 {
174   QDomNodeList prods = producersList();
175   int ct = prods.count();
176   for (int i = 0; i <  ct ; i++)
177   {
178     QDomElement e = prods.item(i).toElement();
179     if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
180       e.setAttribute("duration", QString::number(duration));
181       break;
182     }
183   }
184 }
185
186 int KdenliveDoc::getProducerDuration(int id)
187 {
188   int result = 0;
189   QDomNodeList prods = producersList();
190   int ct = prods.count();
191   for (int i = 0; i <  ct ; i++)
192   {
193     QDomElement e = prods.item(i).toElement();
194     if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
195       result = e.attribute("duration").toInt();
196       break;
197     }
198   }
199   return result;
200 }
201
202
203 QDomDocument KdenliveDoc::generateSceneList()
204 {
205     QDomDocument doc;
206     QDomElement westley = doc.createElement("westley");
207     doc.appendChild(westley);
208     QDomElement prod = doc.createElement("producer");
209 }
210
211 QDomDocument KdenliveDoc::toXml()
212 {
213   return m_document;
214 }
215
216 Timecode KdenliveDoc::timecode()
217 {
218   return m_timecode;
219 }
220
221 QString KdenliveDoc::documentName()
222 {
223   return m_projectName;
224 }
225
226 QDomNodeList KdenliveDoc::producersList()
227 {
228   return m_document.elementsByTagName("producer");
229 }
230
231 void KdenliveDoc::setProducers(QDomElement doc)
232 {
233   QDomNode kdenlivedocument = m_document.elementsByTagName("kdenlivedoc").item(0);
234
235   QDomNodeList list = m_document.elementsByTagName("producer");
236   int ct = list.count();
237     kDebug()<<"DELETING CHILD PRODUCERS: "<<ct;
238   for (int i = ct; i > 0; i--) {
239     kdenlivedocument.removeChild(list.item(i));
240    }
241
242   QDomNode n = doc.firstChild();
243   ct = 0;
244   while(!n.isNull()) {
245      QDomElement e = n.toElement(); // try to convert the node to an element.
246      if(!e.isNull() && e.tagName() == "producer") {
247          kdenlivedocument.appendChild(m_document.importNode(e, true));
248         ct++;
249      }
250      n = n.nextSibling();
251   }
252   kDebug()<<"ADDING CHILD PRODS: "<<ct<<"\n";
253   //kDebug()<<m_document.toString();
254 }
255
256 double KdenliveDoc::fps()
257 {
258   return m_fps;
259 }
260
261 int KdenliveDoc::width()
262 {
263   return m_width;
264 }
265
266 int KdenliveDoc::height()
267 {
268   return m_height;
269 }
270
271 KUrl KdenliveDoc::url()
272 {
273   return m_url;
274 }
275
276 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId)
277 {
278   kDebug()<<"/////////  DOCUM, CREATING NEW CLIP, ID:"<<clipId;
279   DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
280   m_clipManager->addClip(clip);
281   emit addProjectClip(clip);
282 }
283
284 void KdenliveDoc::deleteProjectClip(const uint clipId)
285 {
286   emit deletTimelineClip(clipId);
287   m_clipManager->slotDeleteClip(clipId);
288 }
289
290 void KdenliveDoc::deleteClip(const uint clipId)
291 {
292   emit signalDeleteProjectClip(clipId);
293   m_clipManager->deleteClip(clipId);
294 }
295
296 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group)
297 {
298   kDebug()<<"/////////  DOCUM, ADD CLP: "<<url;
299   m_clipManager->slotAddClipFile(url, group);
300 }
301
302 DocClipBase *KdenliveDoc::getBaseClip(int clipId)
303 {
304   return m_clipManager->getClipById(clipId);
305 }
306
307 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group)
308 {
309   m_clipManager->slotAddColorClipFile(name, color, duration, group);
310 }
311
312 #include "kdenlivedoc.moc"
313