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"
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), m_startPos(0) {
40 kDebug() << "// init profile, ratnum: " << profile.frame_rate_num << ", " << profile.frame_rate_num << ", width: " << profile.width;
41 m_clipManager = new ClipManager(this);
44 if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
46 m_document.setContent(&file, false);
48 QDomNode infoXmlNode = m_document.elementsByTagName("kdenlivedoc").at(0);
49 QDomNode westley = m_document.elementsByTagName("westley").at(0);
50 if (!infoXmlNode.isNull()) {
51 QDomElement infoXml = infoXmlNode.toElement();
52 QString profilePath = infoXml.attribute("profile");
53 m_startPos = infoXml.attribute("position").toInt();
54 if (!profilePath.isEmpty()) setProfilePath(profilePath);
55 double version = infoXml.attribute("version").toDouble();
56 if (version < 0.7) convertDocument(version);
58 //delete all mlt producers and instead, use Kdenlive saved producers
59 QDomNodeList prods = m_document.elementsByTagName("producer");
60 int maxprod = prods.count();
62 for (int i = 0; i < maxprod; i++) {
63 QDomNode m = prods.at(pos);
64 if (m.toElement().attribute("id") == "black")
66 else westley.removeChild(m);
68 prods = m_document.elementsByTagName("kdenlive_producer");
69 maxprod = prods.count();
70 for (int i = 0; i < maxprod; i++) {
71 prods.at(0).toElement().setTagName("producer");
72 westley.insertBefore(prods.at(0), QDomNode());
76 QDomNodeList producers = m_document.elementsByTagName("producer");
77 const int max = producers.count();
79 m_documentLoadingStep = 100.0 / (max + m_document.elementsByTagName("entry").count());
80 parent->slotGotProgressInfo(i18n("Loading project clips"), (int) m_documentLoadingProgress);
83 for (int i = 0; i < max; i++) {
84 e = producers.item(i).cloneNode().toElement();
85 if (m_documentLoadingStep > 0) {
86 m_documentLoadingProgress += m_documentLoadingStep;
87 parent->slotGotProgressInfo(QString(), (int) m_documentLoadingProgress);
88 qApp->processEvents();
90 if (!e.isNull() && e.attribute("id") != "black") {
91 addClip(e, e.attribute("id").toInt());
95 QDomNode markers = m_document.elementsByTagName("markers").at(0);
96 if (!markers.isNull()) {
97 QDomNodeList markerslist = markers.childNodes();
98 int maxchild = markerslist.count();
99 for (int k = 0; k < maxchild; k++) {
100 e = markerslist.at(k).toElement();
101 if (e.tagName() == "marker") {
102 int id = e.attribute("id").toInt();
103 m_clipManager->getClipById(id)->addSnapMarker(GenTime(e.attribute("time").toDouble()), e.attribute("comment"));
106 m_document.removeChild(markers);
108 m_document.removeChild(infoXmlNode);
110 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
111 } else kWarning() << " NO KDENLIVE INFO FOUND IN FILE: " << url.path();
112 KIO::NetAccess::removeTempFile(tmpFile);
114 KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
117 // Creating new document
118 QDomElement westley = m_document.createElement("westley");
119 m_document.appendChild(westley);
121 QDomElement tractor = m_document.createElement("tractor");
122 tractor.setAttribute("id", "maintractor");
123 QDomElement multitrack = m_document.createElement("multitrack");
124 QDomElement playlist = m_document.createElement("playlist");
125 playlist.setAttribute("id", "black_track");
126 westley.appendChild(playlist);
132 int total = audiotracks + videotracks + 1;
134 for (int i = 1; i < total; i++) {
135 QDomElement playlist = m_document.createElement("playlist");
136 playlist.setAttribute("id", "playlist" + QString::number(i));
137 westley.appendChild(playlist);
140 QDomElement track0 = m_document.createElement("track");
141 track0.setAttribute("producer", "black_track");
142 tractor.appendChild(track0);
144 // create audio tracks
145 for (int i = 1; i < audiotracks + 1; i++) {
146 QDomElement track = m_document.createElement("track");
147 track.setAttribute("producer", "playlist" + QString::number(i));
148 track.setAttribute("hide", "video");
149 tractor.appendChild(track);
152 // create video tracks
153 for (int i = audiotracks + 1; i < total; i++) {
154 QDomElement track = m_document.createElement("track");
155 track.setAttribute("producer", "playlist" + QString::number(i));
156 tractor.appendChild(track);
159 for (uint i = 2; i < total ; i++) {
160 QDomElement transition = m_document.createElement("transition");
161 transition.setAttribute("in", "0");
162 //TODO: Make audio mix last for all project duration
163 transition.setAttribute("out", "15000");
165 QDomElement property = m_document.createElement("property");
166 property.setAttribute("name", "a_track");
167 QDomText value = m_document.createTextNode(QString::number(1));
168 property.appendChild(value);
169 transition.appendChild(property);
171 property = m_document.createElement("property");
172 property.setAttribute("name", "b_track");
173 value = m_document.createTextNode(QString::number(i));
174 property.appendChild(value);
175 transition.appendChild(property);
177 property = m_document.createElement("property");
178 property.setAttribute("name", "mlt_service");
179 value = m_document.createTextNode("mix");
180 property.appendChild(value);
181 transition.appendChild(property);
183 property = m_document.createElement("property");
184 property.setAttribute("name", "combine");
185 value = m_document.createTextNode("1");
186 property.appendChild(value);
187 transition.appendChild(property);
189 property = m_document.createElement("property");
190 property.setAttribute("name", "internal_added");
191 value = m_document.createTextNode("237");
192 property.appendChild(value);
193 transition.appendChild(property);
194 tractor.appendChild(transition);
196 westley.appendChild(tractor);
198 m_scenelist = m_document.toString();
199 kDebug() << "KDEnnlive document, init timecode: " << m_fps;
200 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
201 else m_timecode.setFormat((int) m_fps);
204 KdenliveDoc::~KdenliveDoc() {
205 delete m_commandStack;
206 delete m_clipManager;
209 void KdenliveDoc::convertDocument(double version) {
210 // Opening a old Kdenlive document
211 QDomNode westley = m_document.elementsByTagName("westley").at(1);
212 QDomNode tractor = m_document.elementsByTagName("tractor").at(0);
213 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
214 QDomNode multitrack = m_document.elementsByTagName("multitrack").at(0);
215 QDomNodeList playlists = m_document.elementsByTagName("playlist");
217 QDomNode props = m_document.elementsByTagName("properties").at(0).toElement();
218 QString profile = props.toElement().attribute("videoprofile");
219 if (profile == "dv_wide") profile = "dv_pal_wide";
220 if (!profile.isEmpty()) {
221 setProfilePath(profile);
222 } else setProfilePath("dv_pal");
224 // move playlists outside of tractor and add the tracks instead
225 int max = playlists.count();
226 for (int i = 0; i < max; i++) {
227 QDomNode n = playlists.at(i);
228 westley.insertBefore(n, QDomNode());
229 QDomElement pl = n.toElement();
230 QDomElement track = m_document.createElement("track");
231 QString trackType = pl.attribute("hide");
232 if (!trackType.isEmpty()) track.setAttribute("hide", trackType);
233 QString playlist_id = pl.attribute("id");
234 if (playlist_id.isEmpty()) {
235 playlist_id = "black_track";
236 pl.setAttribute("id", playlist_id);
238 track.setAttribute("producer", playlist_id);
239 tractor.appendChild(track);
241 tractor.removeChild(multitrack);
243 // audio track mixing transitions should not be added to track view, so add required attribute
244 QDomNodeList transitions = m_document.elementsByTagName("transition");
245 max = transitions.count();
246 for (int i = 0; i < max; i++) {
247 QDomElement tr = transitions.at(i).toElement();
248 if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") {
249 QDomElement property = m_document.createElement("property");
250 property.setAttribute("name", "internal_added");
251 QDomText value = m_document.createTextNode("237");
252 property.appendChild(value);
253 tr.appendChild(property);
255 // convert transition
256 QDomNamedNodeMap attrs = tr.attributes();
257 for (unsigned int j = 0; j < attrs.count(); j++) {
258 QString attrName = attrs.item(j).nodeName();
259 if (attrName != "in" && attrName != "out" && attrName != "id") {
260 QDomElement property = m_document.createElement("property");
261 property.setAttribute("name", attrName);
262 QDomText value = m_document.createTextNode(attrs.item(j).nodeValue());
263 property.appendChild(value);
264 tr.appendChild(property);
270 // move transitions after tracks
271 for (int i = 0; i < max; i++) {
272 tractor.insertAfter(transitions.at(0), QDomNode());
275 QDomElement markers = m_document.createElement("markers");
277 // change producer names
278 QDomNodeList producers = m_document.elementsByTagName("producer");
279 max = producers.count();
280 for (int i = 0; i < max; i++) {
281 QDomElement prod = producers.at(0).toElement();
282 QDomNode m = prod.firstChild();
283 if (!m.isNull() && m.toElement().tagName() == "markers") {
284 QDomNodeList prodchilds = m.childNodes();
285 int maxchild = prodchilds.count();
286 for (int k = 0; k < maxchild; k++) {
287 QDomElement mark = prodchilds.at(0).toElement();
288 mark.setAttribute("id", prod.attribute("id"));
289 markers.insertAfter(mark, QDomNode());
293 int duration = prod.attribute("duration").toInt();
294 if (duration > 0) prod.setAttribute("out", QString::number(duration));
295 westley.insertBefore(prod, QDomNode());
298 QDomNode westley0 = m_document.elementsByTagName("westley").at(0);
299 if (!markers.firstChild().isNull()) westley0.appendChild(markers);
300 westley0.removeChild(kdenlivedoc);
302 QDomNodeList elements = westley.childNodes();
303 max = elements.count();
304 for (int i = 0; i < max; i++) {
305 QDomElement prod = elements.at(0).toElement();
306 westley0.insertAfter(prod, QDomNode());
309 westley0.removeChild(westley);
311 //kDebug() << "///////////////// CONVERTED DOC:";
312 //kDebug() << m_document.toString();
313 //kDebug() << "///////////////// END CONVERTED DOC:";
316 QDomElement KdenliveDoc::documentInfoXml(QDomElement timelineInfo) {
319 QDomElement addedXml = doc.createElement("kdenlivedoc");
320 QDomElement markers = doc.createElement("markers");
321 addedXml.setAttribute("version", "0.7");
322 addedXml.setAttribute("profile", profilePath());
323 addedXml.setAttribute("position", m_render->seekPosition().frames(m_fps));
324 QList <DocClipBase*> list = m_clipManager->documentClipList();
325 for (int i = 0; i < list.count(); i++) {
326 e = list.at(i)->toXML();
327 e.setTagName("kdenlive_producer");
328 addedXml.appendChild(doc.importNode(e, true));
329 QList < CommentedTime > marks = list.at(i)->commentedSnapMarkers();
330 for (int j = 0; j < marks.count(); j++) {
331 QDomElement marker = doc.createElement("marker");
332 marker.setAttribute("time", marks.at(j).time().ms() / 1000);
333 marker.setAttribute("comment", marks.at(j).comment());
334 marker.setAttribute("id", e.attribute("id"));
335 markers.appendChild(marker);
338 addedXml.appendChild(markers);
339 addedXml.appendChild(doc.importNode(timelineInfo, true));
340 //kDebug() << m_document.toString();
345 ClipManager *KdenliveDoc::clipManager() {
346 return m_clipManager;
349 KUrl KdenliveDoc::projectFolder() const {
350 if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
351 return m_projectFolder;
354 QString KdenliveDoc::getDocumentStandard() {
355 //WARNING: this way to tell the video standard is a bit hackish...
356 if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
360 QString KdenliveDoc::profilePath() const {
361 return m_profile.path;
364 void KdenliveDoc::setProfilePath(QString path) {
365 KdenliveSettings::setCurrent_profile(path);
366 m_profile = ProfilesDialog::getVideoProfile(path);
367 m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
368 m_width = m_profile.width;
369 m_height = m_profile.height;
370 kDebug() << "KDEnnlive document, init timecode from path: " << path << ", " << m_fps;
371 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
372 else m_timecode.setFormat((int) m_fps);
375 void KdenliveDoc::setThumbsProgress(const QString &message, int progress) {
376 emit progressInfo(message, progress);
379 void KdenliveDoc::loadingProgressed() {
380 m_documentLoadingProgress += m_documentLoadingStep;
381 emit progressInfo(QString(), (int) m_documentLoadingProgress);
384 KUndoStack *KdenliveDoc::commandStack() {
385 return m_commandStack;
388 void KdenliveDoc::setRenderer(Render *render) {
390 emit progressInfo(i18n("Loading playlist..."), 0);
391 qApp->processEvents();
392 if (m_render) m_render->setSceneList(m_scenelist, m_startPos);
393 emit progressInfo(QString(), -1);
396 Render *KdenliveDoc::renderer() {
400 void KdenliveDoc::updateClip(int id) {
401 emit updateClipDisplay(id);
404 void KdenliveDoc::updateAllProjectClips() {
405 QList <DocClipBase*> list = m_clipManager->documentClipList();
406 for (int i = 0; i < list.count(); i++)
407 emit updateClipDisplay(list.at(i)->getId());
410 int KdenliveDoc::getFramePos(QString duration) {
411 return m_timecode.getFrameCount(duration, m_fps);
414 QString KdenliveDoc::producerName(int id) {
415 QString result = "unnamed";
416 QDomNodeList prods = producersList();
417 int ct = prods.count();
418 for (int i = 0; i < ct ; i++) {
419 QDomElement e = prods.item(i).toElement();
420 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
421 result = e.attribute("name");
422 if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
429 void KdenliveDoc::setProducerDuration(int id, int duration) {
430 QDomNodeList prods = producersList();
431 int ct = prods.count();
432 for (int i = 0; i < ct ; i++) {
433 QDomElement e = prods.item(i).toElement();
434 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
435 e.setAttribute("duration", QString::number(duration));
441 int KdenliveDoc::getProducerDuration(int id) {
443 QDomNodeList prods = producersList();
444 int ct = prods.count();
445 for (int i = 0; i < ct ; i++) {
446 QDomElement e = prods.item(i).toElement();
447 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
448 result = e.attribute("duration").toInt();
456 QDomDocument KdenliveDoc::generateSceneList() {
458 QDomElement westley = doc.createElement("westley");
459 doc.appendChild(westley);
460 QDomElement prod = doc.createElement("producer");
463 QDomDocument KdenliveDoc::toXml() const {
467 Timecode KdenliveDoc::timecode() const {
471 QDomNodeList KdenliveDoc::producersList() {
472 return m_document.elementsByTagName("producer");
475 void KdenliveDoc::backupMltPlaylist() {
476 if (m_render) m_scenelist = m_render->sceneList();
479 double KdenliveDoc::fps() const {
483 int KdenliveDoc::width() const {
487 int KdenliveDoc::height() const {
491 KUrl KdenliveDoc::url() const {
495 void KdenliveDoc::setUrl(KUrl url) {
499 void KdenliveDoc::setModified(bool mod) {
500 if (mod == m_modified) return;
502 emit docModified(m_modified);
505 bool KdenliveDoc::isModified() const {
509 QString KdenliveDoc::description() const {
511 return i18n("Untitled") + " / " + m_profile.description;
513 return m_url.fileName() + " / " + m_profile.description;
516 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
517 DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
518 kDebug() << "///////// DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
519 m_clipManager->addClip(clip);
520 emit addProjectClip(clip);
523 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
524 emit addProjectFolder(foldername, clipId, false, edit);
527 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
528 emit addProjectFolder(foldername, clipId, true);
531 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
532 for (int i = 0; i < ids.size(); ++i) {
533 emit deletTimelineClip(ids.at(i));
534 m_clipManager->slotDeleteClip(ids.at(i));
539 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
540 QMapIterator<QString, int> i(map);
541 while (i.hasNext()) {
543 slotDeleteFolder(i.key(), i.value());
548 void KdenliveDoc::deleteClip(const uint clipId) {
549 emit signalDeleteProjectClip(clipId);
550 m_clipManager->deleteClip(clipId);
553 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
554 kDebug() << "///////// DOCUM, ADD CLP: " << url;
555 m_clipManager->slotAddClipFile(url, group, groupId);
559 void KdenliveDoc::slotAddTextClipFile(const QString path, const QString group, const int groupId) {
560 kDebug() << "///////// DOCUM, ADD TXT CLP: " << path;
561 m_clipManager->slotAddTextClipFile(path, group, groupId);
565 void KdenliveDoc::slotAddFolder(const QString folderName) {
566 AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
567 commandStack()->push(command);
571 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
572 AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
573 commandStack()->push(command);
577 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
578 EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
579 commandStack()->push(command);
583 int KdenliveDoc::getFreeClipId() {
584 return m_clipManager->getFreeClipId();
587 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
588 return m_clipManager->getClipById(clipId);
591 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
592 m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
596 void KdenliveDoc::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, bool loop, const QString group, const int groupId) {
597 m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, group, groupId);
601 void KdenliveDoc::slotCreateTextClip(QString group, int groupId) {
602 QString titlesFolder = projectFolder().path() + "/titles/";
603 KStandardDirs::makeDir(titlesFolder);
604 TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, kapp->activeWindow());
605 if (dia_ui->exec() == QDialog::Accepted) {
606 QString titleName = "title";
608 QString path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
609 while (QFile::exists(path + ".kdenlivetitle")) {
611 path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
613 QPixmap pix = dia_ui->renderedPixmap();
614 pix.save(path + ".png");
615 dia_ui->saveTitle(path + ".kdenlivetitle");
616 slotAddTextClipFile(path, QString(), -1);
621 void KdenliveDoc::editTextClip(QString path, int id) {
622 TitleWidget *dia_ui = new TitleWidget(KUrl(path + ".kdenlivetitle"), path, m_render, kapp->activeWindow());
623 if (dia_ui->exec() == QDialog::Accepted) {
624 QPixmap pix = dia_ui->renderedPixmap();
625 pix.save(path + ".png");
626 dia_ui->saveTitle(path + ".kdenlivetitle");
627 //slotAddClipFile(KUrl("/tmp/kdenlivetitle.png"), QString(), -1);
628 emit refreshClipThumbnail(id);
634 #include "kdenlivedoc.moc"