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