]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
Fix document loading
[kdenlive] / src / kdenlivedoc.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20 #include <KDebug>
21 #include <KStandardDirs>
22 #include <KMessageBox>
23 #include <KLocale>
24 #include <KFileDialog>
25 #include <KIO/NetAccess>
26
27
28 #include "kdenlivedoc.h"
29 #include "docclipbase.h"
30 #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 KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, MltVideoProfile profile, QUndoGroup *undoGroup, MainWindow *parent): QObject(parent), m_render(NULL), m_url(url), m_projectFolder(projectFolder), m_profile(profile), m_fps((double)profile.frame_rate_num / profile.frame_rate_den), m_width(profile.width), m_height(profile.height), m_commandStack(new KUndoStack(undoGroup)), m_modified(false), m_documentLoadingProgress(0), m_documentLoadingStep(0.0) {
40     m_clipManager = new ClipManager(this);
41     if (!url.isEmpty()) {
42         QString tmpFile;
43         if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
44             QFile file(tmpFile);
45             m_document.setContent(&file, false);
46             file.close();
47             QDomNode infoXmlNode = m_document.elementsByTagName("kdenlivedoc").at(0);
48             if (!infoXmlNode.isNull()) {
49                 QDomElement infoXml = infoXmlNode.toElement();
50                 QString profilePath = infoXml.attribute("profile");
51                 if (!profilePath.isEmpty()) setProfilePath(profilePath);
52                 double version = infoXml.attribute("version").toDouble();
53                 if (version < 0.7) convertDocument(version);
54                 QDomNodeList producers = m_document.elementsByTagName("producer");
55                 QDomElement e;
56                                 const int max = producers.count();
57                                 if (max > 0) {
58                                         m_documentLoadingStep = 100.0 / (max + m_document.elementsByTagName("entry").count());
59                                         parent->slotGotProgressInfo(i18n("Loading project clips"), (int) m_documentLoadingProgress);
60                                 }
61
62                 for (int i = 0; i < max; i++) {
63                     e = producers.item(i).cloneNode().toElement();
64                                         if (m_documentLoadingStep > 0) {
65                                                 m_documentLoadingProgress += m_documentLoadingStep;
66                                                 parent->slotGotProgressInfo(QString(), (int) m_documentLoadingProgress);
67                                                 qApp->processEvents();
68                                         }
69                     if (!e.isNull() && e.attribute("id") != "black") {
70                         addClip(e, e.attribute("id").toInt());
71                     }
72                 }
73                 m_document.removeChild(infoXmlNode);
74                                 QDomNode markers = m_document.elementsByTagName("markers").at(0);
75                                 if (!markers.isNull()) {
76                                         QDomNodeList markerslist = markers.childNodes();
77                                         int maxchild = markerslist.count();
78                                         for (int k = 0; k < maxchild; k++) {
79                                                 e = markerslist.item(k).toElement();
80                                                 if (e.tagName() == "marker") {
81                                                         int id = e.attribute("id").toInt();
82                                                         m_clipManager->getClipById(id)->addSnapMarker(GenTime(e.attribute("time").toDouble()), e.attribute("comment"));
83                                                 }
84                                         }
85                                         m_document.removeChild(markers);
86                                 }
87
88                 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
89             } else kWarning() << "  NO KDENLIVE INFO FOUND IN FILE: " << url.path();
90             KIO::NetAccess::removeTempFile(tmpFile);
91         } else {
92             KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
93         }
94     } else {
95         // Creating new document
96         QDomElement westley = m_document.createElement("westley");
97         m_document.appendChild(westley);
98
99         QDomElement tractor = m_document.createElement("tractor");
100         tractor.setAttribute("id", "maintractor");
101         QDomElement multitrack = m_document.createElement("multitrack");
102         QDomElement playlist = m_document.createElement("playlist");
103         playlist.setAttribute("id", "black_track");
104         westley.appendChild(playlist);
105
106
107         // create playlists
108         int audiotracks = 2;
109         int videotracks = 3;
110         int total = audiotracks + videotracks + 1;
111
112         for (int i = 1; i < total; i++) {
113             QDomElement playlist = m_document.createElement("playlist");
114             playlist.setAttribute("id", "playlist" + QString::number(i));
115             westley.appendChild(playlist);
116         }
117
118         QDomElement track0 = m_document.createElement("track");
119         track0.setAttribute("producer", "black_track");
120         tractor.appendChild(track0);
121
122         // create audio tracks
123         for (int i = 1; i < audiotracks + 1; i++) {
124             QDomElement track = m_document.createElement("track");
125             track.setAttribute("producer", "playlist" + QString::number(i));
126             track.setAttribute("hide", "video");
127             tractor.appendChild(track);
128         }
129
130         // create video tracks
131         for (int i = audiotracks + 1; i < total; i++) {
132             QDomElement track = m_document.createElement("track");
133             track.setAttribute("producer", "playlist" + QString::number(i));
134             tractor.appendChild(track);
135         }
136
137         for (uint i = 2; i < total ; i++) {
138             QDomElement transition = m_document.createElement("transition");
139             transition.setAttribute("in", "0");
140             //TODO: Make audio mix last for all project duration
141             transition.setAttribute("out", "15000");
142
143             QDomElement property = m_document.createElement("property");
144             property.setAttribute("name", "a_track");
145             QDomText value = m_document.createTextNode(QString::number(1));
146             property.appendChild(value);
147             transition.appendChild(property);
148
149             property = m_document.createElement("property");
150             property.setAttribute("name", "b_track");
151             value = m_document.createTextNode(QString::number(i));
152             property.appendChild(value);
153             transition.appendChild(property);
154
155             property = m_document.createElement("property");
156             property.setAttribute("name", "mlt_service");
157             value = m_document.createTextNode("mix");
158             property.appendChild(value);
159             transition.appendChild(property);
160
161             property = m_document.createElement("property");
162             property.setAttribute("name", "combine");
163             value = m_document.createTextNode("1");
164             property.appendChild(value);
165             transition.appendChild(property);
166
167             property = m_document.createElement("property");
168             property.setAttribute("name", "internal_added");
169             value = m_document.createTextNode("237");
170             property.appendChild(value);
171             transition.appendChild(property);
172             tractor.appendChild(transition);
173         }
174         westley.appendChild(tractor);
175     }
176     m_scenelist = m_document.toString();
177     // kDebug() << "scenelist" << m_scenelist;
178     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
179     else m_timecode.setFormat((int) m_fps);
180 }
181
182 KdenliveDoc::~KdenliveDoc() {
183     delete m_commandStack;
184     delete m_clipManager;
185 }
186
187 void KdenliveDoc::convertDocument(double version) {
188     // Opening a old Kdenlive document
189     QDomNode westley = m_document.elementsByTagName("westley").at(1);
190     QDomNode tractor = m_document.elementsByTagName("tractor").at(0);
191     QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
192     QDomNode multitrack = m_document.elementsByTagName("multitrack").at(0);
193     QDomNodeList playlists = m_document.elementsByTagName("playlist");
194
195     QDomNode props = m_document.elementsByTagName("properties").at(0).toElement();
196     QString profile = props.toElement().attribute("videoprofile");
197         if (profile == "dv_wide") profile = "dv_pal_wide";
198     if (!profile.isEmpty()) {
199         setProfilePath(profile);
200     } else setProfilePath("dv_pal");
201
202     // move playlists outside of tractor and add the tracks instead
203     int max = playlists.count();
204     for (int i = 0; i < max; i++) {
205         QDomNode n = playlists.at(i);
206         westley.insertBefore(n, QDomNode());
207         QDomElement track = m_document.createElement("track");
208         QString playlist_id =  n.toElement().attribute("id");
209         if (playlist_id.isEmpty()) {
210             playlist_id = "black_track";
211             n.toElement().setAttribute("id", playlist_id);
212         }
213         track.setAttribute("producer", playlist_id);
214         tractor.appendChild(track);
215     }
216     tractor.removeChild(multitrack);
217
218     // audio track mixing transitions should not be added to track view, so add required attribute
219     QDomNodeList transitions = m_document.elementsByTagName("transition");
220     max = transitions.count();
221     for (int i = 0; i < max; i++) {
222         QDomElement tr = transitions.at(i).toElement();
223         if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") {
224             QDomElement property = m_document.createElement("property");
225             property.setAttribute("name", "internal_added");
226             QDomText value = m_document.createTextNode("237");
227             property.appendChild(value);
228             tr.appendChild(property);
229         } else {
230             // convert transition
231             QDomNamedNodeMap attrs = tr.attributes();
232             for (unsigned int j = 0; j < attrs.count(); j++) {
233                 QString attrName = attrs.item(j).nodeName();
234                 if (attrName != "in" && attrName != "out" && attrName != "id") {
235                     QDomElement property = m_document.createElement("property");
236                     property.setAttribute("name", attrName);
237                     QDomText value = m_document.createTextNode(attrs.item(j).nodeValue());
238                     property.appendChild(value);
239                     tr.appendChild(property);
240                 }
241             }
242         }
243     }
244
245     // move transitions after tracks
246     for (int i = 0; i < max; i++) {
247         tractor.insertAfter(transitions.at(0), QDomNode());
248     }
249
250         QDomElement markers = m_document.createElement("markers");
251
252     // change producer names
253     QDomNodeList producers = m_document.elementsByTagName("producer");
254     max = producers.count();
255     for (int i = 0; i < max; i++) {
256         QDomElement prod = producers.at(0).toElement();
257                 QDomNode m = prod.firstChild();
258                 if (!m.isNull() && m.toElement().tagName() == "markers") {
259                         QDomNodeList prodchilds = m.childNodes();
260                         int maxchild = prodchilds.count();
261                         for (int k = 0; k < maxchild; k++) {
262                                 QDomElement mark = prodchilds.at(0).toElement();
263                                 mark.setAttribute("id", prod.attribute("id"));
264                                 markers.insertAfter(mark, QDomNode());
265                         }
266                         prod.removeChild(m);
267                 }
268         int duration = prod.attribute("duration").toInt();
269         if (duration > 0) prod.setAttribute("out", QString::number(duration));
270         westley.insertBefore(prod, QDomNode());
271     }
272
273     QDomNode westley0 = m_document.elementsByTagName("westley").at(0);
274         if (!markers.firstChild().isNull()) westley0.appendChild(markers);
275     westley0.removeChild(kdenlivedoc);
276
277     QDomNodeList elements = westley.childNodes();
278     max = elements.count();
279     for (int i = 0; i < max; i++) {
280         QDomElement prod = elements.at(0).toElement();
281         westley0.insertAfter(prod, QDomNode());
282     }
283
284     westley0.removeChild(westley);
285
286     kDebug() << "/////////////////  CONVERTED DOC:";
287     //kDebug() << m_document.toString();
288     //kDebug() << "/////////////////  END CONVERTED DOC:";
289 }
290
291 QDomElement KdenliveDoc::documentInfoXml() {
292     QDomDocument doc;
293     QDomElement e;
294     QDomElement addedXml = doc.createElement("kdenlivedoc");
295     addedXml.setAttribute("version", "0.7");
296     addedXml.setAttribute("profile", profilePath());
297     QList <DocClipBase*> list = m_clipManager->documentClipList();
298     for (int i = 0; i < list.count(); i++) {
299         e = list.at(i)->toXML();
300         e.setTagName("kdenlive_producer");
301         addedXml.appendChild(doc.importNode(e, true));
302     }
303
304     //kDebug() << m_document.toString();
305     return addedXml;
306 }
307
308
309 ClipManager *KdenliveDoc::clipManager() {
310     return m_clipManager;
311 }
312
313 KUrl KdenliveDoc::projectFolder() const {
314     if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
315     return m_projectFolder;
316 }
317
318 QString KdenliveDoc::getDocumentStandard() {
319     //WARNING: this way to tell the video standard is a bit hackish...
320     if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
321     return "NTSC";
322 }
323
324 QString KdenliveDoc::profilePath() const {
325     return m_profile.path;
326 }
327
328 void KdenliveDoc::setProfilePath(QString path) {
329     KdenliveSettings::setCurrent_profile(path);
330     m_profile = ProfilesDialog::getVideoProfile(path);
331     m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
332     m_width = m_profile.width;
333     m_height = m_profile.height;
334     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
335     else m_timecode.setFormat((int) m_fps);
336 }
337
338 void KdenliveDoc::setThumbsProgress(const QString &message, int progress) {
339     emit progressInfo(message, progress);
340 }
341
342 void KdenliveDoc::loadingProgressed() {
343         m_documentLoadingProgress += m_documentLoadingStep;
344         emit progressInfo(QString(), (int) m_documentLoadingProgress);
345 }
346
347 KUndoStack *KdenliveDoc::commandStack() {
348     return m_commandStack;
349 }
350
351 void KdenliveDoc::setRenderer(Render *render) {
352     m_render = render;
353     if (m_render) m_render->setSceneList(m_scenelist);
354 }
355
356 Render *KdenliveDoc::renderer() {
357     return m_render;
358 }
359
360 void KdenliveDoc::updateClip(int id) {
361     emit updateClipDisplay(id);
362 }
363
364 int KdenliveDoc::getFramePos(QString duration) {
365     return m_timecode.getFrameCount(duration, m_fps);
366 }
367
368 QString KdenliveDoc::producerName(int id) {
369     QString result = "unnamed";
370     QDomNodeList prods = producersList();
371     int ct = prods.count();
372     for (int i = 0; i <  ct ; i++) {
373         QDomElement e = prods.item(i).toElement();
374         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
375             result = e.attribute("name");
376             if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
377             break;
378         }
379     }
380     return result;
381 }
382
383 void KdenliveDoc::setProducerDuration(int id, int duration) {
384     QDomNodeList prods = producersList();
385     int ct = prods.count();
386     for (int i = 0; i <  ct ; i++) {
387         QDomElement e = prods.item(i).toElement();
388         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
389             e.setAttribute("duration", QString::number(duration));
390             break;
391         }
392     }
393 }
394
395 int KdenliveDoc::getProducerDuration(int id) {
396     int result = 0;
397     QDomNodeList prods = producersList();
398     int ct = prods.count();
399     for (int i = 0; i <  ct ; i++) {
400         QDomElement e = prods.item(i).toElement();
401         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
402             result = e.attribute("duration").toInt();
403             break;
404         }
405     }
406     return result;
407 }
408
409
410 QDomDocument KdenliveDoc::generateSceneList() {
411     QDomDocument doc;
412     QDomElement westley = doc.createElement("westley");
413     doc.appendChild(westley);
414     QDomElement prod = doc.createElement("producer");
415 }
416
417 QDomDocument KdenliveDoc::toXml() const {
418     return m_document;
419 }
420
421 Timecode KdenliveDoc::timecode() const {
422     return m_timecode;
423 }
424
425 QDomNodeList KdenliveDoc::producersList() {
426     return m_document.elementsByTagName("producer");
427 }
428
429 void KdenliveDoc::backupMltPlaylist() {
430     if (m_render) m_scenelist = m_render->sceneList();
431 }
432
433 double KdenliveDoc::fps() const {
434     return m_fps;
435 }
436
437 int KdenliveDoc::width() const {
438     return m_width;
439 }
440
441 int KdenliveDoc::height() const {
442     return m_height;
443 }
444
445 KUrl KdenliveDoc::url() const {
446     return m_url;
447 }
448
449 void KdenliveDoc::setUrl(KUrl url) {
450     m_url = url;
451 }
452
453 void KdenliveDoc::setModified(bool mod) {
454     if (mod == m_modified) return;
455     m_modified = mod;
456     emit docModified(m_modified);
457 }
458
459 bool KdenliveDoc::isModified() const {
460     return m_modified;
461 }
462
463 QString KdenliveDoc::description() const {
464     if (m_url.isEmpty())
465         return i18n("Untitled") + " / " + m_profile.description;
466     else
467         return m_url.fileName() + " / " + m_profile.description;
468 }
469
470 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
471     DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
472     kDebug() << "/////////  DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
473     m_clipManager->addClip(clip);
474     emit addProjectClip(clip);
475 }
476
477 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
478     emit addProjectFolder(foldername, clipId, false, edit);
479 }
480
481 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
482     emit addProjectFolder(foldername, clipId, true);
483 }
484
485 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
486     for (int i = 0; i < ids.size(); ++i) {
487         emit deletTimelineClip(ids.at(i));
488         m_clipManager->slotDeleteClip(ids.at(i));
489     }
490     setModified(true);
491 }
492
493 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
494     QMapIterator<QString, int> i(map);
495     while (i.hasNext()) {
496         i.next();
497         slotDeleteFolder(i.key(), i.value());
498     }
499     setModified(true);
500 }
501
502 void KdenliveDoc::deleteClip(const uint clipId) {
503     emit signalDeleteProjectClip(clipId);
504     m_clipManager->deleteClip(clipId);
505 }
506
507 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
508     kDebug() << "/////////  DOCUM, ADD CLP: " << url;
509     m_clipManager->slotAddClipFile(url, group, groupId);
510     setModified(true);
511 }
512
513 void KdenliveDoc::slotAddTextClipFile(const QString path, const QString group, const int groupId) {
514     kDebug() << "/////////  DOCUM, ADD TXT CLP: " << path;
515     m_clipManager->slotAddTextClipFile(path, group, groupId);
516     setModified(true);
517 }
518
519 void KdenliveDoc::slotAddFolder(const QString folderName) {
520     AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
521     commandStack()->push(command);
522     setModified(true);
523 }
524
525 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
526     AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
527     commandStack()->push(command);
528     setModified(true);
529 }
530
531 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
532     EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
533     commandStack()->push(command);
534     setModified(true);
535 }
536
537 int KdenliveDoc::getFreeClipId() {
538     return m_clipManager->getFreeClipId();
539 }
540
541 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
542     return m_clipManager->getClipById(clipId);
543 }
544
545 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
546     m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
547     setModified(true);
548 }
549
550 void KdenliveDoc::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, bool loop, const QString group, const int groupId) {
551     m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, group, groupId);
552     setModified(true);
553 }
554
555 void KdenliveDoc::slotCreateTextClip(QString group, int groupId) {
556     QString titlesFolder = projectFolder().path() + "/titles/";
557     KStandardDirs::makeDir(titlesFolder);
558     TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, 0);
559     if (dia_ui->exec() == QDialog::Accepted) {
560         QString titleName = "title";
561         int counter = 0;
562         QString path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
563         while (QFile::exists(path + ".kdenlivetitle")) {
564             counter++;
565             path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
566         }
567         QPixmap pix = dia_ui->renderedPixmap();
568         pix.save(path + ".png");
569         dia_ui->saveTitle(path + ".kdenlivetitle");
570         slotAddTextClipFile(path, QString(), -1);
571     }
572     delete dia_ui;
573 }
574
575 void KdenliveDoc::editTextClip(QString path, int id) {
576     TitleWidget *dia_ui = new TitleWidget(KUrl(path + ".kdenlivetitle"), path, m_render, 0);
577     if (dia_ui->exec() == QDialog::Accepted) {
578         QPixmap pix = dia_ui->renderedPixmap();
579         pix.save(path + ".png");
580         dia_ui->saveTitle(path + ".kdenlivetitle");
581         //slotAddClipFile(KUrl("/tmp/kdenlivetitle.png"), QString(), -1);
582         emit refreshClipThumbnail(id);
583     }
584     delete dia_ui;
585 }
586
587
588 #include "kdenlivedoc.moc"
589