1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include <KStandardDirs>
22 #include <KMessageBox>
24 #include <KFileDialog>
25 #include <KIO/NetAccess>
26 #include <KApplication>
28 #include "kdenlivedoc.h"
29 #include "docclipbase.h"
30 #include "profilesdialog.h"
31 #include "kdenlivesettings.h"
33 #include "clipmanager.h"
34 #include "addfoldercommand.h"
35 #include "editfoldercommand.h"
36 #include "titlewidget.h"
37 #include "mainwindow.h"
40 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), m_startPos(0) {
41 kDebug() << "// init profile, ratnum: " << profile.frame_rate_num << ", " << profile.frame_rate_num << ", width: " << profile.width;
42 m_clipManager = new ClipManager(this);
45 if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
47 m_document.setContent(&file, false);
49 QDomNode infoXmlNode = m_document.elementsByTagName("kdenlivedoc").at(0);
50 QDomNode westley = m_document.elementsByTagName("westley").at(0);
51 if (!infoXmlNode.isNull()) {
52 QDomElement infoXml = infoXmlNode.toElement();
53 QString profilePath = infoXml.attribute("profile");
54 m_startPos = infoXml.attribute("position").toInt();
55 if (!profilePath.isEmpty()) setProfilePath(profilePath);
56 double version = infoXml.attribute("version").toDouble();
57 if (version < 0.7) convertDocument(version);
59 //delete all mlt producers and instead, use Kdenlive saved producers
60 QDomNodeList prods = m_document.elementsByTagName("producer");
61 int maxprod = prods.count();
63 for (int i = 0; i < maxprod; i++) {
64 QDomNode m = prods.at(pos);
65 if (m.toElement().attribute("id") == "black")
67 else westley.removeChild(m);
69 prods = m_document.elementsByTagName("kdenlive_producer");
70 maxprod = prods.count();
71 for (int i = 0; i < maxprod; i++) {
72 prods.at(0).toElement().setTagName("producer");
73 westley.insertBefore(prods.at(0), QDomNode());
77 QDomNodeList producers = m_document.elementsByTagName("producer");
78 const int max = producers.count();
80 m_documentLoadingStep = 100.0 / (max + m_document.elementsByTagName("entry").count());
81 parent->slotGotProgressInfo(i18n("Loading project clips"), (int) m_documentLoadingProgress);
84 for (int i = 0; i < max; i++) {
85 e = producers.item(i).cloneNode().toElement();
86 if (m_documentLoadingStep > 0) {
87 m_documentLoadingProgress += m_documentLoadingStep;
88 parent->slotGotProgressInfo(QString(), (int) m_documentLoadingProgress);
89 qApp->processEvents();
91 if (!e.isNull() && e.attribute("id") != "black") {
92 addClip(e, e.attribute("id").toInt());
96 QDomNode markers = m_document.elementsByTagName("markers").at(0);
97 if (!markers.isNull()) {
98 QDomNodeList markerslist = markers.childNodes();
99 int maxchild = markerslist.count();
100 for (int k = 0; k < maxchild; k++) {
101 e = markerslist.at(k).toElement();
102 if (e.tagName() == "marker") {
103 int id = e.attribute("id").toInt();
104 m_clipManager->getClipById(id)->addSnapMarker(GenTime(e.attribute("time").toDouble()), e.attribute("comment"));
107 m_document.removeChild(markers);
109 m_document.removeChild(infoXmlNode);
111 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
112 } else kWarning() << " NO KDENLIVE INFO FOUND IN FILE: " << url.path();
113 KIO::NetAccess::removeTempFile(tmpFile);
115 KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
118 // Creating new document
119 QDomElement westley = m_document.createElement("westley");
120 m_document.appendChild(westley);
122 QDomElement tractor = m_document.createElement("tractor");
123 tractor.setAttribute("id", "maintractor");
124 QDomElement multitrack = m_document.createElement("multitrack");
125 QDomElement playlist = m_document.createElement("playlist");
126 playlist.setAttribute("id", "black_track");
127 westley.appendChild(playlist);
133 int total = audiotracks + videotracks + 1;
135 for (int i = 1; i < total; i++) {
136 QDomElement playlist = m_document.createElement("playlist");
137 playlist.setAttribute("id", "playlist" + QString::number(i));
138 westley.appendChild(playlist);
141 QDomElement track0 = m_document.createElement("track");
142 track0.setAttribute("producer", "black_track");
143 tractor.appendChild(track0);
145 // create audio tracks
146 for (int i = 1; i < audiotracks + 1; i++) {
147 QDomElement track = m_document.createElement("track");
148 track.setAttribute("producer", "playlist" + QString::number(i));
149 track.setAttribute("hide", "video");
150 tractor.appendChild(track);
153 // create video tracks
154 for (int i = audiotracks + 1; i < total; i++) {
155 QDomElement track = m_document.createElement("track");
156 track.setAttribute("producer", "playlist" + QString::number(i));
157 tractor.appendChild(track);
160 for (uint i = 2; i < total ; i++) {
161 QDomElement transition = m_document.createElement("transition");
162 transition.setAttribute("in", "0");
163 //TODO: Make audio mix last for all project duration
164 transition.setAttribute("out", "15000");
166 QDomElement property = m_document.createElement("property");
167 property.setAttribute("name", "a_track");
168 QDomText value = m_document.createTextNode(QString::number(1));
169 property.appendChild(value);
170 transition.appendChild(property);
172 property = m_document.createElement("property");
173 property.setAttribute("name", "b_track");
174 value = m_document.createTextNode(QString::number(i));
175 property.appendChild(value);
176 transition.appendChild(property);
178 property = m_document.createElement("property");
179 property.setAttribute("name", "mlt_service");
180 value = m_document.createTextNode("mix");
181 property.appendChild(value);
182 transition.appendChild(property);
184 property = m_document.createElement("property");
185 property.setAttribute("name", "combine");
186 value = m_document.createTextNode("1");
187 property.appendChild(value);
188 transition.appendChild(property);
190 property = m_document.createElement("property");
191 property.setAttribute("name", "internal_added");
192 value = m_document.createTextNode("237");
193 property.appendChild(value);
194 transition.appendChild(property);
195 tractor.appendChild(transition);
197 westley.appendChild(tractor);
199 m_scenelist = m_document.toString();
200 kDebug() << "KDEnnlive document, init timecode: " << m_fps;
201 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
202 else m_timecode.setFormat((int) m_fps);
204 m_autoSaveTimer = new QTimer(this);
205 m_autoSaveTimer->setSingleShot(true);
206 QString directory = m_url.directory();
207 QString fileName = m_url.fileName();
208 m_recoveryUrl.setDirectory(directory);
209 m_recoveryUrl.setFileName("~" + fileName);
210 connect(m_autoSaveTimer, SIGNAL(timeout()), this, SLOT(slotAutoSave()));
213 KdenliveDoc::~KdenliveDoc() {
214 delete m_commandStack;
215 delete m_clipManager;
216 delete m_autoSaveTimer;
217 if (!m_url.isEmpty()) {
218 // remove backup file
219 if (KIO::NetAccess::exists(m_recoveryUrl, KIO::NetAccess::SourceSide, NULL))
220 KIO::NetAccess::del(m_recoveryUrl, NULL);
224 void KdenliveDoc::syncGuides(QList <Guide *> guides) {
228 m_guidesXml = doc.createElement("guides");
229 for (int i = 0; i < guides.count(); i++) {
230 e = doc.createElement("guide");
231 e.setAttribute("time", guides.at(i)->position().ms() / 1000);
232 e.setAttribute("comment", guides.at(i)->label());
233 m_guidesXml.appendChild(e);
237 void KdenliveDoc::slotAutoSave() {
239 m_render->saveSceneList(m_recoveryUrl.path(), documentInfoXml());
243 void KdenliveDoc::convertDocument(double version) {
244 // Opening a old Kdenlive document
245 QDomNode westley = m_document.elementsByTagName("westley").at(1);
246 QDomNode tractor = m_document.elementsByTagName("tractor").at(0);
247 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
248 QDomNode multitrack = m_document.elementsByTagName("multitrack").at(0);
249 QDomNodeList playlists = m_document.elementsByTagName("playlist");
251 QDomNode props = m_document.elementsByTagName("properties").at(0).toElement();
252 QString profile = props.toElement().attribute("videoprofile");
253 if (profile == "dv_wide") profile = "dv_pal_wide";
254 if (!profile.isEmpty()) {
255 setProfilePath(profile);
256 } else setProfilePath("dv_pal");
258 // move playlists outside of tractor and add the tracks instead
259 int max = playlists.count();
260 for (int i = 0; i < max; i++) {
261 QDomNode n = playlists.at(i);
262 westley.insertBefore(n, QDomNode());
263 QDomElement pl = n.toElement();
264 QDomElement track = m_document.createElement("track");
265 QString trackType = pl.attribute("hide");
266 if (!trackType.isEmpty()) track.setAttribute("hide", trackType);
267 QString playlist_id = pl.attribute("id");
268 if (playlist_id.isEmpty()) {
269 playlist_id = "black_track";
270 pl.setAttribute("id", playlist_id);
272 track.setAttribute("producer", playlist_id);
273 tractor.appendChild(track);
275 tractor.removeChild(multitrack);
277 // audio track mixing transitions should not be added to track view, so add required attribute
278 QDomNodeList transitions = m_document.elementsByTagName("transition");
279 max = transitions.count();
280 for (int i = 0; i < max; i++) {
281 QDomElement tr = transitions.at(i).toElement();
282 if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") {
283 QDomElement property = m_document.createElement("property");
284 property.setAttribute("name", "internal_added");
285 QDomText value = m_document.createTextNode("237");
286 property.appendChild(value);
287 tr.appendChild(property);
289 // convert transition
290 QDomNamedNodeMap attrs = tr.attributes();
291 for (unsigned int j = 0; j < attrs.count(); j++) {
292 QString attrName = attrs.item(j).nodeName();
293 if (attrName != "in" && attrName != "out" && attrName != "id") {
294 QDomElement property = m_document.createElement("property");
295 property.setAttribute("name", attrName);
296 QDomText value = m_document.createTextNode(attrs.item(j).nodeValue());
297 property.appendChild(value);
298 tr.appendChild(property);
304 // move transitions after tracks
305 for (int i = 0; i < max; i++) {
306 tractor.insertAfter(transitions.at(0), QDomNode());
309 QDomElement markers = m_document.createElement("markers");
311 // change producer names
312 QDomNodeList producers = m_document.elementsByTagName("producer");
313 max = producers.count();
314 for (int i = 0; i < max; i++) {
315 QDomElement prod = producers.at(0).toElement();
316 QDomNode m = prod.firstChild();
317 if (!m.isNull() && m.toElement().tagName() == "markers") {
318 QDomNodeList prodchilds = m.childNodes();
319 int maxchild = prodchilds.count();
320 for (int k = 0; k < maxchild; k++) {
321 QDomElement mark = prodchilds.at(0).toElement();
322 mark.setAttribute("id", prod.attribute("id"));
323 markers.insertAfter(mark, QDomNode());
327 int duration = prod.attribute("duration").toInt();
328 if (duration > 0) prod.setAttribute("out", QString::number(duration));
329 westley.insertBefore(prod, QDomNode());
332 QDomNode westley0 = m_document.elementsByTagName("westley").at(0);
333 if (!markers.firstChild().isNull()) westley0.appendChild(markers);
334 westley0.removeChild(kdenlivedoc);
336 QDomNodeList elements = westley.childNodes();
337 max = elements.count();
338 for (int i = 0; i < max; i++) {
339 QDomElement prod = elements.at(0).toElement();
340 westley0.insertAfter(prod, QDomNode());
343 westley0.removeChild(westley);
345 //kDebug() << "///////////////// CONVERTED DOC:";
346 //kDebug() << m_document.toString();
347 //kDebug() << "///////////////// END CONVERTED DOC:";
350 QDomElement KdenliveDoc::documentInfoXml() {
353 QDomElement addedXml = doc.createElement("kdenlivedoc");
354 QDomElement markers = doc.createElement("markers");
355 addedXml.setAttribute("version", "0.7");
356 addedXml.setAttribute("profile", profilePath());
357 addedXml.setAttribute("position", m_render->seekPosition().frames(m_fps));
358 QList <DocClipBase*> list = m_clipManager->documentClipList();
359 for (int i = 0; i < list.count(); i++) {
360 e = list.at(i)->toXML();
361 e.setTagName("kdenlive_producer");
362 addedXml.appendChild(doc.importNode(e, true));
363 QList < CommentedTime > marks = list.at(i)->commentedSnapMarkers();
364 for (int j = 0; j < marks.count(); j++) {
365 QDomElement marker = doc.createElement("marker");
366 marker.setAttribute("time", marks.at(j).time().ms() / 1000);
367 marker.setAttribute("comment", marks.at(j).comment());
368 marker.setAttribute("id", e.attribute("id"));
369 markers.appendChild(marker);
372 addedXml.appendChild(markers);
373 if (!m_guidesXml.isNull()) addedXml.appendChild(doc.importNode(m_guidesXml, true));
374 //kDebug() << m_document.toString();
379 ClipManager *KdenliveDoc::clipManager() {
380 return m_clipManager;
383 KUrl KdenliveDoc::projectFolder() const {
384 if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
385 return m_projectFolder;
388 QString KdenliveDoc::getDocumentStandard() {
389 //WARNING: this way to tell the video standard is a bit hackish...
390 if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
394 QString KdenliveDoc::profilePath() const {
395 return m_profile.path;
398 void KdenliveDoc::setProfilePath(QString path) {
399 KdenliveSettings::setCurrent_profile(path);
400 m_profile = ProfilesDialog::getVideoProfile(path);
401 m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
402 m_width = m_profile.width;
403 m_height = m_profile.height;
404 kDebug() << "KDEnnlive document, init timecode from path: " << path << ", " << m_fps;
405 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
406 else m_timecode.setFormat((int) m_fps);
409 void KdenliveDoc::setThumbsProgress(const QString &message, int progress) {
410 emit progressInfo(message, progress);
413 void KdenliveDoc::loadingProgressed() {
414 m_documentLoadingProgress += m_documentLoadingStep;
415 emit progressInfo(QString(), (int) m_documentLoadingProgress);
418 KUndoStack *KdenliveDoc::commandStack() {
419 return m_commandStack;
422 void KdenliveDoc::setRenderer(Render *render) {
424 emit progressInfo(i18n("Loading playlist..."), 0);
425 qApp->processEvents();
426 if (m_render) m_render->setSceneList(m_scenelist, m_startPos);
427 emit progressInfo(QString(), -1);
430 Render *KdenliveDoc::renderer() {
434 void KdenliveDoc::updateClip(int id) {
435 emit updateClipDisplay(id);
438 void KdenliveDoc::updateAllProjectClips() {
439 QList <DocClipBase*> list = m_clipManager->documentClipList();
440 for (int i = 0; i < list.count(); i++)
441 emit updateClipDisplay(list.at(i)->getId());
444 int KdenliveDoc::getFramePos(QString duration) {
445 return m_timecode.getFrameCount(duration, m_fps);
448 QString KdenliveDoc::producerName(int id) {
449 QString result = "unnamed";
450 QDomNodeList prods = producersList();
451 int ct = prods.count();
452 for (int i = 0; i < ct ; i++) {
453 QDomElement e = prods.item(i).toElement();
454 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
455 result = e.attribute("name");
456 if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
463 void KdenliveDoc::setProducerDuration(int id, int duration) {
464 QDomNodeList prods = producersList();
465 int ct = prods.count();
466 for (int i = 0; i < ct ; i++) {
467 QDomElement e = prods.item(i).toElement();
468 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
469 e.setAttribute("duration", QString::number(duration));
475 int KdenliveDoc::getProducerDuration(int id) {
477 QDomNodeList prods = producersList();
478 int ct = prods.count();
479 for (int i = 0; i < ct ; i++) {
480 QDomElement e = prods.item(i).toElement();
481 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
482 result = e.attribute("duration").toInt();
490 QDomDocument KdenliveDoc::generateSceneList() {
492 QDomElement westley = doc.createElement("westley");
493 doc.appendChild(westley);
494 QDomElement prod = doc.createElement("producer");
497 QDomDocument KdenliveDoc::toXml() const {
501 Timecode KdenliveDoc::timecode() const {
505 QDomNodeList KdenliveDoc::producersList() {
506 return m_document.elementsByTagName("producer");
509 void KdenliveDoc::backupMltPlaylist() {
510 if (m_render) m_scenelist = m_render->sceneList();
513 double KdenliveDoc::fps() const {
517 int KdenliveDoc::width() const {
521 int KdenliveDoc::height() const {
525 KUrl KdenliveDoc::url() const {
529 void KdenliveDoc::setUrl(KUrl url) {
531 QString directory = m_url.directory();
532 QString fileName = m_url.fileName();
533 m_recoveryUrl.setDirectory(directory);
534 m_recoveryUrl.setFileName("~" + fileName);
537 void KdenliveDoc::setModified(bool mod) {
538 if (!m_url.isEmpty() && mod && KdenliveSettings::crashrecovery()) {
539 m_autoSaveTimer->start(3000);
541 if (mod == m_modified) return;
543 emit docModified(m_modified);
546 bool KdenliveDoc::isModified() const {
550 QString KdenliveDoc::description() const {
552 return i18n("Untitled") + " / " + m_profile.description;
554 return m_url.fileName() + " / " + m_profile.description;
557 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
558 DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
559 kDebug() << "///////// DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
560 m_clipManager->addClip(clip);
561 emit addProjectClip(clip);
564 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
565 emit addProjectFolder(foldername, clipId, false, edit);
568 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
569 emit addProjectFolder(foldername, clipId, true);
572 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
573 for (int i = 0; i < ids.size(); ++i) {
574 emit deletTimelineClip(ids.at(i));
575 m_clipManager->slotDeleteClip(ids.at(i));
580 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
581 QMapIterator<QString, int> i(map);
582 while (i.hasNext()) {
584 slotDeleteFolder(i.key(), i.value());
589 void KdenliveDoc::deleteClip(const uint clipId) {
590 emit signalDeleteProjectClip(clipId);
591 m_clipManager->deleteClip(clipId);
594 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
595 kDebug() << "///////// DOCUM, ADD CLP: " << url;
596 m_clipManager->slotAddClipFile(url, group, groupId);
600 void KdenliveDoc::slotAddTextClipFile(const QString path, const QString group, const int groupId) {
601 kDebug() << "///////// DOCUM, ADD TXT CLP: " << path;
602 m_clipManager->slotAddTextClipFile(path, group, groupId);
606 void KdenliveDoc::slotAddFolder(const QString folderName) {
607 AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
608 commandStack()->push(command);
612 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
613 AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
614 commandStack()->push(command);
618 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
619 EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
620 commandStack()->push(command);
624 int KdenliveDoc::getFreeClipId() {
625 return m_clipManager->getFreeClipId();
628 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
629 return m_clipManager->getClipById(clipId);
632 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
633 m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
637 void KdenliveDoc::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, bool loop, const QString group, const int groupId) {
638 m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, group, groupId);
642 void KdenliveDoc::slotCreateTextClip(QString group, int groupId) {
643 QString titlesFolder = projectFolder().path() + "/titles/";
644 KStandardDirs::makeDir(titlesFolder);
645 TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, kapp->activeWindow());
646 if (dia_ui->exec() == QDialog::Accepted) {
647 QString titleName = "title";
649 QString path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
650 while (QFile::exists(path + ".kdenlivetitle")) {
652 path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
654 QPixmap pix = dia_ui->renderedPixmap();
655 pix.save(path + ".png");
656 dia_ui->saveTitle(path + ".kdenlivetitle");
657 slotAddTextClipFile(path, QString(), -1);
662 void KdenliveDoc::editTextClip(QString path, int id) {
663 TitleWidget *dia_ui = new TitleWidget(KUrl(path + ".kdenlivetitle"), path, m_render, kapp->activeWindow());
664 if (dia_ui->exec() == QDialog::Accepted) {
665 QPixmap pix = dia_ui->renderedPixmap();
666 pix.save(path + ".png");
667 dia_ui->saveTitle(path + ".kdenlivetitle");
668 //slotAddClipFile(KUrl("/tmp/kdenlivetitle.png"), QString(), -1);
669 emit refreshClipThumbnail(id);
675 #include "kdenlivedoc.moc"