]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
* Allow user to set number of audio/video tracks at project creation
[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 #include <KApplication>
27
28 #include "kdenlivedoc.h"
29 #include "docclipbase.h"
30 #include "profilesdialog.h"
31 #include "kdenlivesettings.h"
32 #include "renderer.h"
33 #include "clipmanager.h"
34 #include "addfoldercommand.h"
35 #include "editfoldercommand.h"
36 #include "titlewidget.h"
37 #include "mainwindow.h"
38
39
40 KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup *undoGroup, const QString &profileName, const QPoint tracks, MainWindow *parent): QObject(parent), m_render(NULL), m_url(url), m_projectFolder(projectFolder), m_commandStack(new QUndoStack(undoGroup)), m_modified(false), m_documentLoadingProgress(0), m_documentLoadingStep(0.0), m_startPos(0), m_zoom(7), m_autosave(NULL) {
41     m_clipManager = new ClipManager(this);
42     if (!url.isEmpty()) {
43         QString tmpFile;
44         if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
45             QFile file(tmpFile);
46             m_document.setContent(&file, false);
47             file.close();
48             QDomNode infoXmlNode = m_document.elementsByTagName("kdenlivedoc").at(0);
49             QDomNode westley = m_document.elementsByTagName("westley").at(0);
50             if (!infoXmlNode.isNull()) {
51                 QDomElement infoXml = infoXmlNode.toElement();
52                 QString profilePath = infoXml.attribute("profile");
53                 m_startPos = infoXml.attribute("position").toInt();
54                 m_zoom = infoXml.attribute("zoom", "7").toInt();
55                 setProfilePath(profilePath);
56                 double version = infoXml.attribute("version").toDouble();
57                 if (version < 0.7) convertDocument(version);
58                 else {
59                     //delete all mlt producers and instead, use Kdenlive saved producers
60                     QDomNodeList prods = m_document.elementsByTagName("producer");
61                     int maxprod = prods.count();
62                     int pos = 0;
63                     for (int i = 0; i < maxprod; i++) {
64                         QDomNode m = prods.at(pos);
65                         QString prodId = m.toElement().attribute("id");
66                         if (prodId == "black" || prodId.startsWith("slowmotion"))
67                             pos++;
68                         else westley.removeChild(m);
69                     }
70                     prods = m_document.elementsByTagName("kdenlive_producer");
71                     maxprod = prods.count();
72                     for (int i = 0; i < maxprod; i++) {
73                         prods.at(0).toElement().setTagName("producer");
74                         westley.insertBefore(prods.at(0), QDomNode());
75                     }
76                 }
77                 QDomElement e;
78                 QDomNodeList producers = m_document.elementsByTagName("producer");
79                 const int max = producers.count();
80                 if (max > 0) {
81                     m_documentLoadingStep = 100.0 / (max + m_document.elementsByTagName("entry").count());
82                     parent->slotGotProgressInfo(i18n("Loading project clips"), (int) m_documentLoadingProgress);
83                 }
84
85                 for (int i = 0; i < max; i++) {
86                     e = producers.item(i).cloneNode().toElement();
87                     if (m_documentLoadingStep > 0) {
88                         m_documentLoadingProgress += m_documentLoadingStep;
89                         parent->slotGotProgressInfo(QString(), (int) m_documentLoadingProgress);
90                         qApp->processEvents();
91                     }
92                     QString prodId = e.attribute("id");
93                     if (!e.isNull() && prodId != "black" && !prodId.startsWith("slowmotion")/*&& prodId.toInt() > 0*/) {
94                         addClip(e, prodId);
95                     }
96                 }
97
98                 QDomNode markers = m_document.elementsByTagName("markers").at(0);
99                 if (!markers.isNull()) {
100                     QDomNodeList markerslist = markers.childNodes();
101                     int maxchild = markerslist.count();
102                     for (int k = 0; k < maxchild; k++) {
103                         e = markerslist.at(k).toElement();
104                         if (e.tagName() == "marker") {
105                             m_clipManager->getClipById(e.attribute("id"))->addSnapMarker(GenTime(e.attribute("time").toDouble()), e.attribute("comment"));
106                         }
107                     }
108                     westley.removeChild(markers);
109                 }
110                 m_document.removeChild(infoXmlNode);
111
112                 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
113             } else {
114                 parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
115                 kWarning() << "  NO KDENLIVE INFO FOUND IN FILE: " << url.path();
116                 m_document = createEmptyDocument(tracks.x(), tracks.y());
117                 setProfilePath(profileName);
118             }
119             KIO::NetAccess::removeTempFile(tmpFile);
120         } else {
121             KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
122             parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
123             m_document = createEmptyDocument(tracks.x(), tracks.y());
124             setProfilePath(profileName);
125         }
126     } else {
127         m_document = createEmptyDocument(tracks.x(), tracks.y());
128         setProfilePath(profileName);
129     }
130     m_scenelist = m_document.toString();
131     kDebug() << "KDEnnlive document, init timecode: " << m_fps;
132     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
133     else m_timecode.setFormat((int) m_fps);
134
135     m_autoSaveTimer = new QTimer(this);
136     m_autoSaveTimer->setSingleShot(true);
137     connect(m_autoSaveTimer, SIGNAL(timeout()), this, SLOT(slotAutoSave()));
138 }
139
140 KdenliveDoc::~KdenliveDoc() {
141     delete m_commandStack;
142     delete m_clipManager;
143     delete m_autoSaveTimer;
144     m_autosave->remove();
145 }
146
147 QDomDocument KdenliveDoc::createEmptyDocument(const int videotracks, const int audiotracks) {
148     // Creating new document
149     QDomDocument doc;
150     QDomElement westley = doc.createElement("westley");
151     doc.appendChild(westley);
152
153     QDomElement tractor = doc.createElement("tractor");
154     tractor.setAttribute("id", "maintractor");
155     QDomElement multitrack = doc.createElement("multitrack");
156     QDomElement playlist = doc.createElement("playlist");
157     playlist.setAttribute("id", "black_track");
158     westley.appendChild(playlist);
159
160
161     // create playlists
162     int total = audiotracks + videotracks + 1;
163
164     for (int i = 1; i < total; i++) {
165         QDomElement playlist = doc.createElement("playlist");
166         playlist.setAttribute("id", "playlist" + QString::number(i));
167         westley.appendChild(playlist);
168     }
169
170     QDomElement track0 = doc.createElement("track");
171     track0.setAttribute("producer", "black_track");
172     tractor.appendChild(track0);
173
174     // create audio tracks
175     for (int i = 1; i < audiotracks + 1; i++) {
176         QDomElement track = doc.createElement("track");
177         track.setAttribute("producer", "playlist" + QString::number(i));
178         track.setAttribute("hide", "video");
179         tractor.appendChild(track);
180     }
181
182     // create video tracks
183     for (int i = audiotracks + 1; i < total; i++) {
184         QDomElement track = doc.createElement("track");
185         track.setAttribute("producer", "playlist" + QString::number(i));
186         tractor.appendChild(track);
187     }
188
189     for (uint i = 2; i < total ; i++) {
190         QDomElement transition = doc.createElement("transition");
191         transition.setAttribute("always_active", "1");
192
193         QDomElement property = doc.createElement("property");
194         property.setAttribute("name", "a_track");
195         QDomText value = doc.createTextNode(QString::number(1));
196         property.appendChild(value);
197         transition.appendChild(property);
198
199         property = doc.createElement("property");
200         property.setAttribute("name", "b_track");
201         value = doc.createTextNode(QString::number(i));
202         property.appendChild(value);
203         transition.appendChild(property);
204
205         property = doc.createElement("property");
206         property.setAttribute("name", "mlt_service");
207         value = doc.createTextNode("mix");
208         property.appendChild(value);
209         transition.appendChild(property);
210
211         property = doc.createElement("property");
212         property.setAttribute("name", "combine");
213         value = doc.createTextNode("1");
214         property.appendChild(value);
215         transition.appendChild(property);
216
217         property = doc.createElement("property");
218         property.setAttribute("name", "internal_added");
219         value = doc.createTextNode("237");
220         property.appendChild(value);
221         transition.appendChild(property);
222         tractor.appendChild(transition);
223     }
224     westley.appendChild(tractor);
225     return doc;
226 }
227
228
229 void KdenliveDoc::syncGuides(QList <Guide *> guides) {
230     QDomDocument doc;
231     QDomElement e;
232     m_guidesXml.clear();
233     m_guidesXml = doc.createElement("guides");
234     for (int i = 0; i < guides.count(); i++) {
235         e = doc.createElement("guide");
236         e.setAttribute("time", guides.at(i)->position().ms() / 1000);
237         e.setAttribute("comment", guides.at(i)->label());
238         m_guidesXml.appendChild(e);
239     }
240 }
241
242 void KdenliveDoc::slotAutoSave() {
243     if (m_render) {
244         if (!m_autosave->isOpen() && !m_autosave->open(QIODevice::ReadWrite)) {
245             // show error: could not open the autosave file
246             kDebug() << "ERROR; CANNOT CREATE AUTOSAVE FILE";
247         }
248         kDebug() << "// AUTOSAVE FILE: " << m_autosave->fileName();
249         m_render->saveSceneList(m_autosave->fileName(), documentInfoXml());
250     }
251 }
252
253 void KdenliveDoc::setZoom(int factor) {
254     m_zoom = factor;
255 }
256
257 int KdenliveDoc::zoom() const {
258     return m_zoom;
259 }
260
261 void KdenliveDoc::convertDocument(double version) {
262     // Opening a old Kdenlive document
263     QDomNode westley = m_document.elementsByTagName("westley").at(1);
264     QDomNode tractor = m_document.elementsByTagName("tractor").at(0);
265     QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
266     QDomNode multitrack = m_document.elementsByTagName("multitrack").at(0);
267     QDomNodeList playlists = m_document.elementsByTagName("playlist");
268
269     m_startPos = kdenlivedoc.toElement().attribute("timeline_position").toInt();
270
271     QDomNode props = m_document.elementsByTagName("properties").at(0).toElement();
272     QString profile = props.toElement().attribute("videoprofile");
273     if (profile == "dv_wide") profile = "dv_pal_wide";
274     setProfilePath(profile);
275
276     // move playlists outside of tractor and add the tracks instead
277     int max = playlists.count();
278     for (int i = 0; i < max; i++) {
279         QDomNode n = playlists.at(i);
280         westley.insertBefore(n, QDomNode());
281         QDomElement pl = n.toElement();
282         QDomElement track = m_document.createElement("track");
283         QString trackType = pl.attribute("hide");
284         if (!trackType.isEmpty()) track.setAttribute("hide", trackType);
285         QString playlist_id =  pl.attribute("id");
286         if (playlist_id.isEmpty()) {
287             playlist_id = "black_track";
288             pl.setAttribute("id", playlist_id);
289         }
290         track.setAttribute("producer", playlist_id);
291         //tractor.appendChild(track);
292         tractor.insertAfter(track, QDomNode());
293     }
294     tractor.removeChild(multitrack);
295
296     // audio track mixing transitions should not be added to track view, so add required attribute
297     QDomNodeList transitions = m_document.elementsByTagName("transition");
298     max = transitions.count();
299     for (int i = 0; i < max; i++) {
300         QDomElement tr = transitions.at(i).toElement();
301         if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") {
302             QDomElement property = m_document.createElement("property");
303             property.setAttribute("name", "internal_added");
304             QDomText value = m_document.createTextNode("237");
305             property.appendChild(value);
306             tr.appendChild(property);
307         } else {
308             // convert transition
309             QDomNamedNodeMap attrs = tr.attributes();
310             for (unsigned int j = 0; j < attrs.count(); j++) {
311                 QString attrName = attrs.item(j).nodeName();
312                 if (attrName != "in" && attrName != "out" && attrName != "id") {
313                     QDomElement property = m_document.createElement("property");
314                     property.setAttribute("name", attrName);
315                     QDomText value = m_document.createTextNode(attrs.item(j).nodeValue());
316                     property.appendChild(value);
317                     tr.appendChild(property);
318                 }
319             }
320         }
321     }
322
323     // move transitions after tracks
324     for (int i = 0; i < max; i++) {
325         tractor.insertAfter(transitions.at(0), QDomNode());
326     }
327
328     // Fix filters format
329     QDomNodeList entries = m_document.elementsByTagName("entry");
330     max = entries.count();
331     for (int i = 0; i < max; i++) {
332         QString last_id;
333         int effectix = 0;
334         QDomNode m = entries.at(i).firstChild();
335         while (!m.isNull()) {
336             if (m.toElement().tagName() == "filter") {
337                 QDomElement filt = m.toElement();
338                 QDomNamedNodeMap attrs = filt.attributes();
339                 QString current_id = filt.attribute("kdenlive_id");
340                 if (current_id != last_id) {
341                     effectix++;
342                     last_id = current_id;
343                 }
344                 QDomElement e = m_document.createElement("property");
345                 e.setAttribute("name", "kdenlive_ix");
346                 QDomText value = m_document.createTextNode(QString::number(effectix));
347                 e.appendChild(value);
348                 filt.appendChild(e);
349                 for (int j = 0; j < attrs.count(); j++) {
350                     QDomAttr a = attrs.item(j).toAttr();
351                     if (!a.isNull()) {
352                         kDebug() << " FILTER; adding :" << a.name() << ":" << a.value();
353                         QDomElement e = m_document.createElement("property");
354                         e.setAttribute("name", a.name());
355                         QDomText value = m_document.createTextNode(a.value());
356                         e.appendChild(value);
357                         filt.appendChild(e);
358
359                     }
360                 }
361             }
362             m = m.nextSibling();
363         }
364     }
365
366     /*
367         QDomNodeList filters = m_document.elementsByTagName("filter");
368         max = filters.count();
369         QString last_id;
370         int effectix = 0;
371         for (int i = 0; i < max; i++) {
372             QDomElement filt = filters.at(i).toElement();
373             QDomNamedNodeMap attrs = filt.attributes();
374      QString current_id = filt.attribute("kdenlive_id");
375      if (current_id != last_id) {
376          effectix++;
377          last_id = current_id;
378      }
379      QDomElement e = m_document.createElement("property");
380             e.setAttribute("name", "kdenlive_ix");
381             QDomText value = m_document.createTextNode(QString::number(1));
382             e.appendChild(value);
383             filt.appendChild(e);
384             for (int j = 0; j < attrs.count(); j++) {
385                 QDomAttr a = attrs.item(j).toAttr();
386                 if (!a.isNull()) {
387                     kDebug() << " FILTER; adding :" << a.name() << ":" << a.value();
388                     QDomElement e = m_document.createElement("property");
389                     e.setAttribute("name", a.name());
390                     QDomText value = m_document.createTextNode(a.value());
391                     e.appendChild(value);
392                     filt.appendChild(e);
393                 }
394             }
395         }*/
396
397     // fix slowmotion
398     QDomNodeList producers = westley.toElement().elementsByTagName("producer");
399     max = producers.count();
400     for (int i = 0; i < max; i++) {
401         QDomElement prod = producers.at(i).toElement();
402         if (prod.attribute("mlt_service") == "framebuffer") {
403             QString slowmotionprod = prod.attribute("resource");
404             slowmotionprod.replace(':', '?');
405             kDebug() << "// FOUND WRONG SLOWMO, new: " << slowmotionprod;
406             prod.setAttribute("resource", slowmotionprod);
407         }
408     }
409
410     // move producers to correct place, markers to a global list, fix clip descriptions
411     QDomElement markers = m_document.createElement("markers");
412     producers = m_document.elementsByTagName("producer");
413     max = producers.count();
414     for (int i = 0; i < max; i++) {
415         QDomElement prod = producers.at(0).toElement();
416         QDomNode m = prod.firstChild();
417         if (!m.isNull() && m.toElement().tagName() == "markers") {
418             QDomNodeList prodchilds = m.childNodes();
419             int maxchild = prodchilds.count();
420             for (int k = 0; k < maxchild; k++) {
421                 QDomElement mark = prodchilds.at(0).toElement();
422                 mark.setAttribute("id", prod.attribute("id"));
423                 markers.insertAfter(mark, QDomNode());
424             }
425             prod.removeChild(m);
426         } else if (m.isText()) {
427             QString comment = m.nodeValue();
428             if (!comment.isEmpty()) {
429                 prod.setAttribute("description", comment);
430             }
431             prod.removeChild(m);
432         }
433         int duration = prod.attribute("duration").toInt();
434         if (duration > 0) prod.setAttribute("out", QString::number(duration));
435         westley.insertBefore(prod, QDomNode());
436     }
437
438     QDomNode westley0 = m_document.elementsByTagName("westley").at(0);
439     if (!markers.firstChild().isNull()) westley0.appendChild(markers);
440     westley0.removeChild(kdenlivedoc);
441
442     QDomNodeList elements = westley.childNodes();
443     max = elements.count();
444     for (int i = 0; i < max; i++) {
445         QDomElement prod = elements.at(0).toElement();
446         westley0.insertAfter(prod, QDomNode());
447     }
448
449     westley0.removeChild(westley);
450
451     //kDebug() << "/////////////////  CONVERTED DOC:";
452     //kDebug() << m_document.toString();
453     //kDebug() << "/////////////////  END CONVERTED DOC:";
454 }
455
456 QDomElement KdenliveDoc::documentInfoXml() {
457     QDomDocument doc;
458     QDomElement e;
459     QDomElement addedXml = doc.createElement("kdenlivedoc");
460     QDomElement markers = doc.createElement("markers");
461     addedXml.setAttribute("version", "0.7");
462     addedXml.setAttribute("profile", profilePath());
463     addedXml.setAttribute("position", m_render->seekPosition().frames(m_fps));
464     addedXml.setAttribute("zoom", m_zoom);
465     QList <DocClipBase*> list = m_clipManager->documentClipList();
466     for (int i = 0; i < list.count(); i++) {
467         e = list.at(i)->toXML();
468         e.setTagName("kdenlive_producer");
469         addedXml.appendChild(doc.importNode(e, true));
470         QList < CommentedTime > marks = list.at(i)->commentedSnapMarkers();
471         for (int j = 0; j < marks.count(); j++) {
472             QDomElement marker = doc.createElement("marker");
473             marker.setAttribute("time", marks.at(j).time().ms() / 1000);
474             marker.setAttribute("comment", marks.at(j).comment());
475             marker.setAttribute("id", e.attribute("id"));
476             markers.appendChild(marker);
477         }
478     }
479     addedXml.appendChild(markers);
480     if (!m_guidesXml.isNull()) addedXml.appendChild(doc.importNode(m_guidesXml, true));
481     //kDebug() << m_document.toString();
482     return addedXml;
483 }
484
485
486 ClipManager *KdenliveDoc::clipManager() {
487     return m_clipManager;
488 }
489
490 KUrl KdenliveDoc::projectFolder() const {
491     if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
492     return m_projectFolder;
493 }
494
495 QString KdenliveDoc::getDocumentStandard() {
496     //WARNING: this way to tell the video standard is a bit hackish...
497     if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
498     return "NTSC";
499 }
500
501 QString KdenliveDoc::profilePath() const {
502     return m_profile.path;
503 }
504
505 MltVideoProfile KdenliveDoc::mltProfile() const {
506     return m_profile;
507 }
508
509 void KdenliveDoc::setProfilePath(QString path) {
510     if (path.isEmpty()) path = KdenliveSettings::default_profile();
511     if (path.isEmpty()) path = "dv_pal";
512     m_profile = ProfilesDialog::getVideoProfile(path);
513     KdenliveSettings::setProject_display_ratio((double) m_profile.display_aspect_num / m_profile.display_aspect_den);
514     m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
515     m_width = m_profile.width;
516     m_height = m_profile.height;
517     kDebug() << "KDEnnlive document, init timecode from path: " << path << ",  " << m_fps;
518     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
519     else m_timecode.setFormat((int) m_fps);
520 }
521
522 const double KdenliveDoc::dar() {
523     return (double) m_profile.display_aspect_num / m_profile.display_aspect_den;
524 }
525
526 void KdenliveDoc::setThumbsProgress(const QString &message, int progress) {
527     emit progressInfo(message, progress);
528 }
529
530 void KdenliveDoc::loadingProgressed() {
531     m_documentLoadingProgress += m_documentLoadingStep;
532     emit progressInfo(QString(), (int) m_documentLoadingProgress);
533 }
534
535 QUndoStack *KdenliveDoc::commandStack() {
536     return m_commandStack;
537 }
538
539 void KdenliveDoc::setRenderer(Render *render) {
540     if (m_render) return;
541     m_render = render;
542     emit progressInfo(i18n("Loading playlist..."), 0);
543     qApp->processEvents();
544     if (m_render) m_render->setSceneList(m_document.toString(), m_startPos);
545     emit progressInfo(QString(), -1);
546 }
547
548 Render *KdenliveDoc::renderer() {
549     return m_render;
550 }
551
552 void KdenliveDoc::updateClip(const QString &id) {
553     emit updateClipDisplay(id);
554 }
555
556 void KdenliveDoc::updateAllProjectClips() {
557     QList <DocClipBase*> list = m_clipManager->documentClipList();
558     for (int i = 0; i < list.count(); i++)
559         emit updateClipDisplay(list.at(i)->getId());
560 }
561
562 int KdenliveDoc::getFramePos(QString duration) {
563     return m_timecode.getFrameCount(duration, m_fps);
564 }
565
566 QString KdenliveDoc::producerName(const QString &id) {
567     QString result = "unnamed";
568     QDomNodeList prods = producersList();
569     int ct = prods.count();
570     for (int i = 0; i <  ct ; i++) {
571         QDomElement e = prods.item(i).toElement();
572         if (e.attribute("id") != "black" && e.attribute("id") == id) {
573             result = e.attribute("name");
574             if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
575             break;
576         }
577     }
578     return result;
579 }
580
581 void KdenliveDoc::setProducerDuration(const QString &id, int duration) {
582     QDomNodeList prods = producersList();
583     int ct = prods.count();
584     for (int i = 0; i <  ct ; i++) {
585         QDomElement e = prods.item(i).toElement();
586         if (e.attribute("id") != "black" && e.attribute("id") == id) {
587             e.setAttribute("duration", QString::number(duration));
588             break;
589         }
590     }
591 }
592
593 int KdenliveDoc::getProducerDuration(const QString &id) {
594     int result = 0;
595     QDomNodeList prods = producersList();
596     int ct = prods.count();
597     for (int i = 0; i <  ct ; i++) {
598         QDomElement e = prods.item(i).toElement();
599         if (e.attribute("id") != "black" && e.attribute("id") == id) {
600             result = e.attribute("duration").toInt();
601             break;
602         }
603     }
604     return result;
605 }
606
607
608 QDomDocument KdenliveDoc::generateSceneList() {
609     QDomDocument doc;
610     QDomElement westley = doc.createElement("westley");
611     doc.appendChild(westley);
612     QDomElement prod = doc.createElement("producer");
613 }
614
615 QDomDocument KdenliveDoc::toXml() const {
616     return m_document;
617 }
618
619 Timecode KdenliveDoc::timecode() const {
620     return m_timecode;
621 }
622
623 QDomNodeList KdenliveDoc::producersList() {
624     return m_document.elementsByTagName("producer");
625 }
626
627 void KdenliveDoc::backupMltPlaylist() {
628     if (m_render) m_scenelist = m_render->sceneList();
629 }
630
631 double KdenliveDoc::fps() const {
632     return m_fps;
633 }
634
635 int KdenliveDoc::width() const {
636     return m_width;
637 }
638
639 int KdenliveDoc::height() const {
640     return m_height;
641 }
642
643 KUrl KdenliveDoc::url() const {
644     return m_url;
645 }
646
647 void KdenliveDoc::setUrl(KUrl url) {
648     m_url = url;
649 }
650
651 void KdenliveDoc::setModified(bool mod) {
652     if (!m_url.isEmpty() && mod && KdenliveSettings::crashrecovery()) {
653         m_autoSaveTimer->start(3000);
654     }
655     if (mod == m_modified) return;
656     m_modified = mod;
657     emit docModified(m_modified);
658 }
659
660 bool KdenliveDoc::isModified() const {
661     return m_modified;
662 }
663
664 QString KdenliveDoc::description() const {
665     if (m_url.isEmpty())
666         return i18n("Untitled") + " / " + m_profile.description;
667     else
668         return m_url.fileName() + " / " + m_profile.description;
669 }
670
671 void KdenliveDoc::addClip(const QDomElement &elem, const QString &clipId) {
672     DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
673     kDebug() << "/////////  DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
674     m_clipManager->addClip(clip);
675     emit addProjectClip(clip);
676 }
677
678 void KdenliveDoc::addFolder(const QString foldername, const QString &clipId, bool edit) {
679     emit addProjectFolder(foldername, clipId, false, edit);
680 }
681
682 void KdenliveDoc::deleteFolder(const QString foldername, const QString &clipId) {
683     emit addProjectFolder(foldername, clipId, true);
684 }
685
686 void KdenliveDoc::deleteProjectClip(QList <QString> ids) {
687     for (int i = 0; i < ids.size(); ++i) {
688         emit deleteTimelineClip(ids.at(i));
689         m_clipManager->slotDeleteClip(ids.at(i));
690     }
691     setModified(true);
692 }
693
694 void KdenliveDoc::deleteProjectFolder(QMap <QString, QString> map) {
695     QMapIterator<QString, QString> i(map);
696     while (i.hasNext()) {
697         i.next();
698         slotDeleteFolder(i.key(), i.value());
699     }
700     setModified(true);
701 }
702
703 void KdenliveDoc::deleteClip(const QString &clipId) {
704     emit signalDeleteProjectClip(clipId);
705     m_clipManager->deleteClip(clipId);
706 }
707
708 void KdenliveDoc::slotAddClipList(const KUrl::List urls, const QString group, const QString &groupId) {
709     m_clipManager->slotAddClipList(urls, group, groupId);
710     emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
711     setModified(true);
712 }
713
714
715 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const QString &groupId) {
716     kDebug() << "/////////  DOCUM, ADD CLP: " << url;
717     m_clipManager->slotAddClipFile(url, group, groupId);
718     emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
719     setModified(true);
720 }
721
722 void KdenliveDoc::slotAddFolder(const QString folderName) {
723     AddFolderCommand *command = new AddFolderCommand(this, folderName, QString::number(m_clipManager->getFreeClipId()), true);
724     commandStack()->push(command);
725     setModified(true);
726 }
727
728 void KdenliveDoc::slotDeleteFolder(const QString folderName, const QString &id) {
729     AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
730     commandStack()->push(command);
731     setModified(true);
732 }
733
734 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, const QString &clipId) {
735     EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
736     commandStack()->push(command);
737     setModified(true);
738 }
739
740 const QString&KdenliveDoc::getFreeClipId() {
741     return QString::number(m_clipManager->getFreeClipId());
742 }
743
744 DocClipBase *KdenliveDoc::getBaseClip(const QString &clipId) {
745     return m_clipManager->getClipById(clipId);
746 }
747
748 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const QString &groupId) {
749     m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
750     setModified(true);
751 }
752
753 void KdenliveDoc::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, const bool loop, const bool fade, const QString &luma_duration, const QString &luma_file, const int softness, const QString group, const QString &groupId) {
754     m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, fade, luma_duration, luma_file, softness, group, groupId);
755     setModified(true);
756 }
757
758 void KdenliveDoc::slotCreateTextClip(QString group, const QString &groupId) {
759     QString titlesFolder = projectFolder().path() + "/titles/";
760     KStandardDirs::makeDir(titlesFolder);
761     TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, kapp->activeWindow());
762     if (dia_ui->exec() == QDialog::Accepted) {
763         QString titleName = "title";
764         int counter = 0;
765         QString path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
766         while (QFile::exists(path + ".png")) {
767             counter++;
768             path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
769         }
770         QPixmap pix = dia_ui->renderedPixmap();
771         pix.save(path + ".png");
772         //dia_ui->saveTitle(path + ".kdenlivetitle");
773         m_clipManager->slotAddTextClipFile(path, dia_ui->xml().toString(), QString(), QString());
774         setModified(true);
775     }
776     delete dia_ui;
777 }
778
779
780
781 #include "kdenlivedoc.moc"
782