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 QDomNode westley = m_document.elementsByTagName("westley").at(0);
49 if (!infoXmlNode.isNull()) {
50 QDomElement infoXml = infoXmlNode.toElement();
51 QString profilePath = infoXml.attribute("profile");
52 if (!profilePath.isEmpty()) setProfilePath(profilePath);
53 double version = infoXml.attribute("version").toDouble();
54 if (version < 0.7) convertDocument(version);
56 //delete all mlt producers and instead, use Kdenlive saved producers
57 QDomNodeList prods = m_document.elementsByTagName("producer");
58 int maxprod = prods.count();
60 for (int i = 0; i < maxprod; i++) {
61 QDomNode m = prods.at(pos);
62 if (m.toElement().attribute("id") == "black")
64 else westley.removeChild(m);
66 prods = m_document.elementsByTagName("kdenlive_producer");
67 maxprod = prods.count();
68 for (int i = 0; i < maxprod; i++) {
69 prods.at(0).toElement().setTagName("producer");
70 westley.insertBefore(prods.at(0), QDomNode());
74 QDomNodeList producers = m_document.elementsByTagName("producer");
75 const int max = producers.count();
77 m_documentLoadingStep = 100.0 / (max + m_document.elementsByTagName("entry").count());
78 parent->slotGotProgressInfo(i18n("Loading project clips"), (int) m_documentLoadingProgress);
81 for (int i = 0; i < max; i++) {
82 e = producers.item(i).cloneNode().toElement();
83 if (m_documentLoadingStep > 0) {
84 m_documentLoadingProgress += m_documentLoadingStep;
85 parent->slotGotProgressInfo(QString(), (int) m_documentLoadingProgress);
86 qApp->processEvents();
88 if (!e.isNull() && e.attribute("id") != "black") {
89 addClip(e, e.attribute("id").toInt());
93 QDomNode markers = m_document.elementsByTagName("markers").at(0);
94 if (!markers.isNull()) {
95 QDomNodeList markerslist = markers.childNodes();
96 int maxchild = markerslist.count();
97 for (int k = 0; k < maxchild; k++) {
98 e = markerslist.at(k).toElement();
99 if (e.tagName() == "marker") {
100 int id = e.attribute("id").toInt();
101 m_clipManager->getClipById(id)->addSnapMarker(GenTime(e.attribute("time").toDouble()), e.attribute("comment"));
104 m_document.removeChild(markers);
106 m_document.removeChild(infoXmlNode);
108 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
109 } else kWarning() << " NO KDENLIVE INFO FOUND IN FILE: " << url.path();
110 KIO::NetAccess::removeTempFile(tmpFile);
112 KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
115 // Creating new document
116 QDomElement westley = m_document.createElement("westley");
117 m_document.appendChild(westley);
119 QDomElement tractor = m_document.createElement("tractor");
120 tractor.setAttribute("id", "maintractor");
121 QDomElement multitrack = m_document.createElement("multitrack");
122 QDomElement playlist = m_document.createElement("playlist");
123 playlist.setAttribute("id", "black_track");
124 westley.appendChild(playlist);
130 int total = audiotracks + videotracks + 1;
132 for (int i = 1; i < total; i++) {
133 QDomElement playlist = m_document.createElement("playlist");
134 playlist.setAttribute("id", "playlist" + QString::number(i));
135 westley.appendChild(playlist);
138 QDomElement track0 = m_document.createElement("track");
139 track0.setAttribute("producer", "black_track");
140 tractor.appendChild(track0);
142 // create audio tracks
143 for (int i = 1; i < audiotracks + 1; i++) {
144 QDomElement track = m_document.createElement("track");
145 track.setAttribute("producer", "playlist" + QString::number(i));
146 track.setAttribute("hide", "video");
147 tractor.appendChild(track);
150 // create video tracks
151 for (int i = audiotracks + 1; i < total; i++) {
152 QDomElement track = m_document.createElement("track");
153 track.setAttribute("producer", "playlist" + QString::number(i));
154 tractor.appendChild(track);
157 for (uint i = 2; i < total ; i++) {
158 QDomElement transition = m_document.createElement("transition");
159 transition.setAttribute("in", "0");
160 //TODO: Make audio mix last for all project duration
161 transition.setAttribute("out", "15000");
163 QDomElement property = m_document.createElement("property");
164 property.setAttribute("name", "a_track");
165 QDomText value = m_document.createTextNode(QString::number(1));
166 property.appendChild(value);
167 transition.appendChild(property);
169 property = m_document.createElement("property");
170 property.setAttribute("name", "b_track");
171 value = m_document.createTextNode(QString::number(i));
172 property.appendChild(value);
173 transition.appendChild(property);
175 property = m_document.createElement("property");
176 property.setAttribute("name", "mlt_service");
177 value = m_document.createTextNode("mix");
178 property.appendChild(value);
179 transition.appendChild(property);
181 property = m_document.createElement("property");
182 property.setAttribute("name", "combine");
183 value = m_document.createTextNode("1");
184 property.appendChild(value);
185 transition.appendChild(property);
187 property = m_document.createElement("property");
188 property.setAttribute("name", "internal_added");
189 value = m_document.createTextNode("237");
190 property.appendChild(value);
191 transition.appendChild(property);
192 tractor.appendChild(transition);
194 westley.appendChild(tractor);
196 m_scenelist = m_document.toString();
197 // kDebug() << "scenelist" << m_scenelist;
198 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
199 else m_timecode.setFormat((int) m_fps);
202 KdenliveDoc::~KdenliveDoc() {
203 delete m_commandStack;
204 delete m_clipManager;
207 void KdenliveDoc::convertDocument(double version) {
208 // Opening a old Kdenlive document
209 QDomNode westley = m_document.elementsByTagName("westley").at(1);
210 QDomNode tractor = m_document.elementsByTagName("tractor").at(0);
211 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
212 QDomNode multitrack = m_document.elementsByTagName("multitrack").at(0);
213 QDomNodeList playlists = m_document.elementsByTagName("playlist");
215 QDomNode props = m_document.elementsByTagName("properties").at(0).toElement();
216 QString profile = props.toElement().attribute("videoprofile");
217 if (profile == "dv_wide") profile = "dv_pal_wide";
218 if (!profile.isEmpty()) {
219 setProfilePath(profile);
220 } else setProfilePath("dv_pal");
222 // move playlists outside of tractor and add the tracks instead
223 int max = playlists.count();
224 for (int i = 0; i < max; i++) {
225 QDomNode n = playlists.at(i);
226 westley.insertBefore(n, QDomNode());
227 QDomElement track = m_document.createElement("track");
228 QString playlist_id = n.toElement().attribute("id");
229 if (playlist_id.isEmpty()) {
230 playlist_id = "black_track";
231 n.toElement().setAttribute("id", playlist_id);
233 track.setAttribute("producer", playlist_id);
234 tractor.appendChild(track);
236 tractor.removeChild(multitrack);
238 // audio track mixing transitions should not be added to track view, so add required attribute
239 QDomNodeList transitions = m_document.elementsByTagName("transition");
240 max = transitions.count();
241 for (int i = 0; i < max; i++) {
242 QDomElement tr = transitions.at(i).toElement();
243 if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") {
244 QDomElement property = m_document.createElement("property");
245 property.setAttribute("name", "internal_added");
246 QDomText value = m_document.createTextNode("237");
247 property.appendChild(value);
248 tr.appendChild(property);
250 // convert transition
251 QDomNamedNodeMap attrs = tr.attributes();
252 for (unsigned int j = 0; j < attrs.count(); j++) {
253 QString attrName = attrs.item(j).nodeName();
254 if (attrName != "in" && attrName != "out" && attrName != "id") {
255 QDomElement property = m_document.createElement("property");
256 property.setAttribute("name", attrName);
257 QDomText value = m_document.createTextNode(attrs.item(j).nodeValue());
258 property.appendChild(value);
259 tr.appendChild(property);
265 // move transitions after tracks
266 for (int i = 0; i < max; i++) {
267 tractor.insertAfter(transitions.at(0), QDomNode());
270 QDomElement markers = m_document.createElement("markers");
272 // change producer names
273 QDomNodeList producers = m_document.elementsByTagName("producer");
274 max = producers.count();
275 for (int i = 0; i < max; i++) {
276 QDomElement prod = producers.at(0).toElement();
277 QDomNode m = prod.firstChild();
278 if (!m.isNull() && m.toElement().tagName() == "markers") {
279 QDomNodeList prodchilds = m.childNodes();
280 int maxchild = prodchilds.count();
281 for (int k = 0; k < maxchild; k++) {
282 QDomElement mark = prodchilds.at(0).toElement();
283 mark.setAttribute("id", prod.attribute("id"));
284 markers.insertAfter(mark, QDomNode());
288 int duration = prod.attribute("duration").toInt();
289 if (duration > 0) prod.setAttribute("out", QString::number(duration));
290 westley.insertBefore(prod, QDomNode());
293 QDomNode westley0 = m_document.elementsByTagName("westley").at(0);
294 if (!markers.firstChild().isNull()) westley0.appendChild(markers);
295 westley0.removeChild(kdenlivedoc);
297 QDomNodeList elements = westley.childNodes();
298 max = elements.count();
299 for (int i = 0; i < max; i++) {
300 QDomElement prod = elements.at(0).toElement();
301 westley0.insertAfter(prod, QDomNode());
304 westley0.removeChild(westley);
306 //kDebug() << "///////////////// CONVERTED DOC:";
307 //kDebug() << m_document.toString();
308 //kDebug() << "///////////////// END CONVERTED DOC:";
311 QDomElement KdenliveDoc::documentInfoXml() {
314 QDomElement addedXml = doc.createElement("kdenlivedoc");
315 QDomElement markers = doc.createElement("markers");
316 addedXml.setAttribute("version", "0.7");
317 addedXml.setAttribute("profile", profilePath());
318 QList <DocClipBase*> list = m_clipManager->documentClipList();
319 for (int i = 0; i < list.count(); i++) {
320 e = list.at(i)->toXML();
321 e.setTagName("kdenlive_producer");
322 addedXml.appendChild(doc.importNode(e, true));
323 QList < CommentedTime > marks = list.at(i)->commentedSnapMarkers();
324 for (int j = 0; j < marks.count(); j++) {
325 QDomElement marker = doc.createElement("marker");
326 marker.setAttribute("time", marks.at(j).time().ms() / 1000);
327 marker.setAttribute("comment", marks.at(j).comment());
328 marker.setAttribute("id", e.attribute("id"));
329 markers.appendChild(marker);
332 addedXml.appendChild(markers);
333 //kDebug() << m_document.toString();
338 ClipManager *KdenliveDoc::clipManager() {
339 return m_clipManager;
342 KUrl KdenliveDoc::projectFolder() const {
343 if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
344 return m_projectFolder;
347 QString KdenliveDoc::getDocumentStandard() {
348 //WARNING: this way to tell the video standard is a bit hackish...
349 if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
353 QString KdenliveDoc::profilePath() const {
354 return m_profile.path;
357 void KdenliveDoc::setProfilePath(QString path) {
358 KdenliveSettings::setCurrent_profile(path);
359 m_profile = ProfilesDialog::getVideoProfile(path);
360 m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
361 m_width = m_profile.width;
362 m_height = m_profile.height;
363 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
364 else m_timecode.setFormat((int) m_fps);
367 void KdenliveDoc::setThumbsProgress(const QString &message, int progress) {
368 emit progressInfo(message, progress);
371 void KdenliveDoc::loadingProgressed() {
372 m_documentLoadingProgress += m_documentLoadingStep;
373 emit progressInfo(QString(), (int) m_documentLoadingProgress);
376 KUndoStack *KdenliveDoc::commandStack() {
377 return m_commandStack;
380 void KdenliveDoc::setRenderer(Render *render) {
382 if (m_render) m_render->setSceneList(m_scenelist);
385 Render *KdenliveDoc::renderer() {
389 void KdenliveDoc::updateClip(int id) {
390 emit updateClipDisplay(id);
393 int KdenliveDoc::getFramePos(QString duration) {
394 return m_timecode.getFrameCount(duration, m_fps);
397 QString KdenliveDoc::producerName(int id) {
398 QString result = "unnamed";
399 QDomNodeList prods = producersList();
400 int ct = prods.count();
401 for (int i = 0; i < ct ; i++) {
402 QDomElement e = prods.item(i).toElement();
403 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
404 result = e.attribute("name");
405 if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
412 void KdenliveDoc::setProducerDuration(int id, int duration) {
413 QDomNodeList prods = producersList();
414 int ct = prods.count();
415 for (int i = 0; i < ct ; i++) {
416 QDomElement e = prods.item(i).toElement();
417 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
418 e.setAttribute("duration", QString::number(duration));
424 int KdenliveDoc::getProducerDuration(int id) {
426 QDomNodeList prods = producersList();
427 int ct = prods.count();
428 for (int i = 0; i < ct ; i++) {
429 QDomElement e = prods.item(i).toElement();
430 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
431 result = e.attribute("duration").toInt();
439 QDomDocument KdenliveDoc::generateSceneList() {
441 QDomElement westley = doc.createElement("westley");
442 doc.appendChild(westley);
443 QDomElement prod = doc.createElement("producer");
446 QDomDocument KdenliveDoc::toXml() const {
450 Timecode KdenliveDoc::timecode() const {
454 QDomNodeList KdenliveDoc::producersList() {
455 return m_document.elementsByTagName("producer");
458 void KdenliveDoc::backupMltPlaylist() {
459 if (m_render) m_scenelist = m_render->sceneList();
462 double KdenliveDoc::fps() const {
466 int KdenliveDoc::width() const {
470 int KdenliveDoc::height() const {
474 KUrl KdenliveDoc::url() const {
478 void KdenliveDoc::setUrl(KUrl url) {
482 void KdenliveDoc::setModified(bool mod) {
483 if (mod == m_modified) return;
485 emit docModified(m_modified);
488 bool KdenliveDoc::isModified() const {
492 QString KdenliveDoc::description() const {
494 return i18n("Untitled") + " / " + m_profile.description;
496 return m_url.fileName() + " / " + m_profile.description;
499 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
500 DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
501 kDebug() << "///////// DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
502 m_clipManager->addClip(clip);
503 emit addProjectClip(clip);
506 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
507 emit addProjectFolder(foldername, clipId, false, edit);
510 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
511 emit addProjectFolder(foldername, clipId, true);
514 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
515 for (int i = 0; i < ids.size(); ++i) {
516 emit deletTimelineClip(ids.at(i));
517 m_clipManager->slotDeleteClip(ids.at(i));
522 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
523 QMapIterator<QString, int> i(map);
524 while (i.hasNext()) {
526 slotDeleteFolder(i.key(), i.value());
531 void KdenliveDoc::deleteClip(const uint clipId) {
532 emit signalDeleteProjectClip(clipId);
533 m_clipManager->deleteClip(clipId);
536 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
537 kDebug() << "///////// DOCUM, ADD CLP: " << url;
538 m_clipManager->slotAddClipFile(url, group, groupId);
542 void KdenliveDoc::slotAddTextClipFile(const QString path, const QString group, const int groupId) {
543 kDebug() << "///////// DOCUM, ADD TXT CLP: " << path;
544 m_clipManager->slotAddTextClipFile(path, group, groupId);
548 void KdenliveDoc::slotAddFolder(const QString folderName) {
549 AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
550 commandStack()->push(command);
554 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
555 AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
556 commandStack()->push(command);
560 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
561 EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
562 commandStack()->push(command);
566 int KdenliveDoc::getFreeClipId() {
567 return m_clipManager->getFreeClipId();
570 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
571 return m_clipManager->getClipById(clipId);
574 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
575 m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
579 void KdenliveDoc::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, bool loop, const QString group, const int groupId) {
580 m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, group, groupId);
584 void KdenliveDoc::slotCreateTextClip(QString group, int groupId) {
585 QString titlesFolder = projectFolder().path() + "/titles/";
586 KStandardDirs::makeDir(titlesFolder);
587 TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, 0);
588 if (dia_ui->exec() == QDialog::Accepted) {
589 QString titleName = "title";
591 QString path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
592 while (QFile::exists(path + ".kdenlivetitle")) {
594 path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
596 QPixmap pix = dia_ui->renderedPixmap();
597 pix.save(path + ".png");
598 dia_ui->saveTitle(path + ".kdenlivetitle");
599 slotAddTextClipFile(path, QString(), -1);
604 void KdenliveDoc::editTextClip(QString path, int id) {
605 TitleWidget *dia_ui = new TitleWidget(KUrl(path + ".kdenlivetitle"), path, m_render, 0);
606 if (dia_ui->exec() == QDialog::Accepted) {
607 QPixmap pix = dia_ui->renderedPixmap();
608 pix.save(path + ".png");
609 dia_ui->saveTitle(path + ".kdenlivetitle");
610 //slotAddClipFile(KUrl("/tmp/kdenlivetitle.png"), QString(), -1);
611 emit refreshClipThumbnail(id);
617 #include "kdenlivedoc.moc"