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