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