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