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