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>
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"
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);
43 if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
45 m_document.setContent(&file, false);
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");
56 const int max = producers.count();
58 m_documentLoadingStep = 100.0 / (max + m_document.elementsByTagName("entry").count());
59 parent->slotGotProgressInfo(i18n("Loading project clips"), (int) m_documentLoadingProgress);
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();
69 if (!e.isNull() && e.attribute("id") != "black") {
70 addClip(e, e.attribute("id").toInt());
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"));
85 m_document.removeChild(markers);
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);
92 KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
95 // Creating new document
96 QDomElement westley = m_document.createElement("westley");
97 m_document.appendChild(westley);
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);
110 int total = audiotracks + videotracks + 1;
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);
118 QDomElement track0 = m_document.createElement("track");
119 track0.setAttribute("producer", "black_track");
120 tractor.appendChild(track0);
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);
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);
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");
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);
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);
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);
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);
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);
174 westley.appendChild(tractor);
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);
182 KdenliveDoc::~KdenliveDoc() {
183 delete m_commandStack;
184 delete m_clipManager;
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");
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");
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);
213 track.setAttribute("producer", playlist_id);
214 tractor.appendChild(track);
216 tractor.removeChild(multitrack);
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);
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);
245 // move transitions after tracks
246 for (int i = 0; i < max; i++) {
247 tractor.insertAfter(transitions.at(0), QDomNode());
250 QDomElement markers = m_document.createElement("markers");
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());
268 int duration = prod.attribute("duration").toInt();
269 if (duration > 0) prod.setAttribute("out", QString::number(duration));
270 westley.insertBefore(prod, QDomNode());
273 QDomNode westley0 = m_document.elementsByTagName("westley").at(0);
274 if (!markers.firstChild().isNull()) westley0.appendChild(markers);
275 westley0.removeChild(kdenlivedoc);
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());
284 westley0.removeChild(westley);
286 kDebug() << "///////////////// CONVERTED DOC:";
287 //kDebug() << m_document.toString();
288 //kDebug() << "///////////////// END CONVERTED DOC:";
291 QDomElement KdenliveDoc::documentInfoXml() {
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));
304 //kDebug() << m_document.toString();
309 ClipManager *KdenliveDoc::clipManager() {
310 return m_clipManager;
313 KUrl KdenliveDoc::projectFolder() const {
314 if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
315 return m_projectFolder;
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";
324 QString KdenliveDoc::profilePath() const {
325 return m_profile.path;
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);
338 void KdenliveDoc::setThumbsProgress(const QString &message, int progress) {
339 emit progressInfo(message, progress);
342 void KdenliveDoc::loadingProgressed() {
343 m_documentLoadingProgress += m_documentLoadingStep;
344 emit progressInfo(QString(), (int) m_documentLoadingProgress);
347 KUndoStack *KdenliveDoc::commandStack() {
348 return m_commandStack;
351 void KdenliveDoc::setRenderer(Render *render) {
353 if (m_render) m_render->setSceneList(m_scenelist);
356 Render *KdenliveDoc::renderer() {
360 void KdenliveDoc::updateClip(int id) {
361 emit updateClipDisplay(id);
364 int KdenliveDoc::getFramePos(QString duration) {
365 return m_timecode.getFrameCount(duration, m_fps);
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();
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));
395 int KdenliveDoc::getProducerDuration(int id) {
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();
410 QDomDocument KdenliveDoc::generateSceneList() {
412 QDomElement westley = doc.createElement("westley");
413 doc.appendChild(westley);
414 QDomElement prod = doc.createElement("producer");
417 QDomDocument KdenliveDoc::toXml() const {
421 Timecode KdenliveDoc::timecode() const {
425 QDomNodeList KdenliveDoc::producersList() {
426 return m_document.elementsByTagName("producer");
429 void KdenliveDoc::backupMltPlaylist() {
430 if (m_render) m_scenelist = m_render->sceneList();
433 double KdenliveDoc::fps() const {
437 int KdenliveDoc::width() const {
441 int KdenliveDoc::height() const {
445 KUrl KdenliveDoc::url() const {
449 void KdenliveDoc::setUrl(KUrl url) {
453 void KdenliveDoc::setModified(bool mod) {
454 if (mod == m_modified) return;
456 emit docModified(m_modified);
459 bool KdenliveDoc::isModified() const {
463 QString KdenliveDoc::description() const {
465 return i18n("Untitled") + " / " + m_profile.description;
467 return m_url.fileName() + " / " + m_profile.description;
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);
477 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
478 emit addProjectFolder(foldername, clipId, false, edit);
481 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
482 emit addProjectFolder(foldername, clipId, true);
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));
493 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
494 QMapIterator<QString, int> i(map);
495 while (i.hasNext()) {
497 slotDeleteFolder(i.key(), i.value());
502 void KdenliveDoc::deleteClip(const uint clipId) {
503 emit signalDeleteProjectClip(clipId);
504 m_clipManager->deleteClip(clipId);
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);
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);
519 void KdenliveDoc::slotAddFolder(const QString folderName) {
520 AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
521 commandStack()->push(command);
525 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
526 AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
527 commandStack()->push(command);
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);
537 int KdenliveDoc::getFreeClipId() {
538 return m_clipManager->getFreeClipId();
541 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
542 return m_clipManager->getClipById(clipId);
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);
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);
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";
562 QString path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
563 while (QFile::exists(path + ".kdenlivetitle")) {
565 path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
567 QPixmap pix = dia_ui->renderedPixmap();
568 pix.save(path + ".png");
569 dia_ui->saveTitle(path + ".kdenlivetitle");
570 slotAddTextClipFile(path, QString(), -1);
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);
588 #include "kdenlivedoc.moc"