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"
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);
42 if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
44 m_document.setContent(&file, false);
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();
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());
62 m_document.removeChild(infoXmlNode);
63 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
64 } else kWarning() << " NO KDENLIVE INFO FOUND IN FILE: " << url.path();
65 KIO::NetAccess::removeTempFile(tmpFile);
67 KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
70 // Creating new document
71 QDomElement westley = m_document.createElement("westley");
72 m_document.appendChild(westley);
74 QDomElement tractor = m_document.createElement("tractor");
75 tractor.setAttribute("id", "maintractor");
76 QDomElement multitrack = m_document.createElement("multitrack");
77 QDomElement playlist = m_document.createElement("playlist");
78 playlist.setAttribute("id", "black_track");
79 westley.appendChild(playlist);
85 int total = audiotracks + videotracks + 1;
87 for (int i = 1; i < total; i++) {
88 QDomElement playlist = m_document.createElement("playlist");
89 playlist.setAttribute("id", "playlist" + QString::number(i));
90 westley.appendChild(playlist);
93 QDomElement track0 = m_document.createElement("track");
94 track0.setAttribute("producer", "black_track");
95 tractor.appendChild(track0);
97 // create audio tracks
98 for (int i = 1; i < audiotracks + 1; i++) {
99 QDomElement track = m_document.createElement("track");
100 track.setAttribute("producer", "playlist" + QString::number(i));
101 track.setAttribute("hide", "video");
102 tractor.appendChild(track);
105 // create video tracks
106 for (int i = audiotracks + 1; i < total; i++) {
107 QDomElement track = m_document.createElement("track");
108 track.setAttribute("producer", "playlist" + QString::number(i));
109 tractor.appendChild(track);
112 for (uint i = 2; i < total ; i++) {
113 QDomElement transition = m_document.createElement("transition");
114 transition.setAttribute("in", "0");
115 //TODO: Make audio mix last for all project duration
116 transition.setAttribute("out", "15000");
118 QDomElement property = m_document.createElement("property");
119 property.setAttribute("name", "a_track");
120 QDomText value = m_document.createTextNode(QString::number(1));
121 property.appendChild(value);
122 transition.appendChild(property);
124 property = m_document.createElement("property");
125 property.setAttribute("name", "b_track");
126 value = m_document.createTextNode(QString::number(i));
127 property.appendChild(value);
128 transition.appendChild(property);
130 property = m_document.createElement("property");
131 property.setAttribute("name", "mlt_service");
132 value = m_document.createTextNode("mix");
133 property.appendChild(value);
134 transition.appendChild(property);
136 property = m_document.createElement("property");
137 property.setAttribute("name", "combine");
138 value = m_document.createTextNode("1");
139 property.appendChild(value);
140 transition.appendChild(property);
142 property = m_document.createElement("property");
143 property.setAttribute("name", "internal_added");
144 value = m_document.createTextNode("237");
145 property.appendChild(value);
146 transition.appendChild(property);
147 tractor.appendChild(transition);
149 westley.appendChild(tractor);
151 m_scenelist = m_document.toString();
152 // kDebug() << "scenelist" << m_scenelist;
153 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
154 else m_timecode.setFormat((int) m_fps);
157 KdenliveDoc::~KdenliveDoc() {
158 delete m_commandStack;
159 delete m_clipManager;
162 void KdenliveDoc::convertDocument(double version) {
163 // Opening a old Kdenlive document
164 QDomNode westley = m_document.elementsByTagName("westley").at(1);
165 QDomNode tractor = m_document.elementsByTagName("tractor").at(0);
166 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
167 QDomNode multitrack = m_document.elementsByTagName("multitrack").at(0);
168 QDomNodeList playlists = m_document.elementsByTagName("playlist");
170 QDomNode props = m_document.elementsByTagName("properties").at(0).toElement();
171 QString profile = props.toElement().attribute("videoprofile");
172 if (!profile.isEmpty()) {
173 setProfilePath(profile);
174 } else setProfilePath("dv_pal");
176 // move playlists outside of tractor and add the tracks instead
177 int max = playlists.count();
178 for (int i = 0; i < max; i++) {
179 QDomNode n = playlists.at(i);
180 westley.insertBefore(n, QDomNode());
181 QDomElement track = m_document.createElement("track");
182 QString playlist_id = n.toElement().attribute("id");
183 if (playlist_id.isEmpty()) {
184 playlist_id = "black_track";
185 n.toElement().setAttribute("id", playlist_id);
187 track.setAttribute("producer", playlist_id);
188 tractor.appendChild(track);
190 tractor.removeChild(multitrack);
192 // audio track mixing transitions should not be added to track view, so add required attribute
193 QDomNodeList transitions = m_document.elementsByTagName("transition");
194 max = transitions.count();
195 for (int i = 0; i < max; i++) {
196 QDomElement tr = transitions.at(i).toElement();
197 if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") {
198 QDomElement property = m_document.createElement("property");
199 property.setAttribute("name", "internal_added");
200 QDomText value = m_document.createTextNode("237");
201 property.appendChild(value);
202 tr.appendChild(property);
204 // convert transition
205 QDomNamedNodeMap attrs = tr.attributes();
206 for (unsigned int j = 0; j < attrs.count(); j++) {
207 QString attrName = attrs.item(j).nodeName();
208 if (attrName != "in" && attrName != "out" && attrName != "id") {
209 QDomElement property = m_document.createElement("property");
210 property.setAttribute("name", attrName);
211 QDomText value = m_document.createTextNode(attrs.item(j).nodeValue());
212 property.appendChild(value);
213 tr.appendChild(property);
219 // move transitions after tracks
220 for (int i = 0; i < max; i++) {
221 tractor.insertAfter(transitions.at(0), QDomNode());
225 // change producer names
226 QDomNodeList producers = m_document.elementsByTagName("producer");
227 max = producers.count();
229 for (int i = 0; i < max; i++) {
230 QDomElement prod = producers.at(0).toElement();
231 int duration = prod.attribute("duration").toInt();
232 if (duration > 0) prod.setAttribute("out", QString::number(duration));
233 westley.insertBefore(prod, QDomNode());
236 QDomNode westley0 = m_document.elementsByTagName("westley").at(0);
237 westley0.removeChild(kdenlivedoc);
239 QDomNodeList elements = westley.childNodes();
240 max = elements.count();
241 for (int i = 0; i < max; i++) {
242 QDomElement prod = elements.at(0).toElement();
243 westley0.insertAfter(prod, QDomNode());
246 westley0.removeChild(westley);
248 kDebug() << "///////////////// CONVERTED DOC:";
249 kDebug() << m_document.toString();
250 kDebug() << "///////////////// END CONVERTED DOC:";
253 QDomElement KdenliveDoc::documentInfoXml() {
256 QDomElement addedXml = doc.createElement("kdenlivedoc");
257 addedXml.setAttribute("version", "0.7");
258 addedXml.setAttribute("profile", profilePath());
259 QList <DocClipBase*> list = m_clipManager->documentClipList();
260 for (int i = 0; i < list.count(); i++) {
261 e = list.at(i)->toXML();
262 e.setTagName("kdenlive_producer");
263 addedXml.appendChild(doc.importNode(e, true));
266 //kDebug() << m_document.toString();
271 ClipManager *KdenliveDoc::clipManager() {
272 return m_clipManager;
275 KUrl KdenliveDoc::projectFolder() const {
276 if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
277 return m_projectFolder;
280 QString KdenliveDoc::getDocumentStandard() {
281 //WARNING: this way to tell the video standard is a bit hackish...
282 if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
286 QString KdenliveDoc::profilePath() const {
287 return m_profile.path;
290 void KdenliveDoc::setProfilePath(QString path) {
291 KdenliveSettings::setCurrent_profile(path);
292 m_profile = ProfilesDialog::getVideoProfile(path);
293 m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
294 m_width = m_profile.width;
295 m_height = m_profile.height;
296 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
297 else m_timecode.setFormat((int) m_fps);
300 void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
301 emit thumbsProgress(url, progress);
304 KUndoStack *KdenliveDoc::commandStack() {
305 return m_commandStack;
308 void KdenliveDoc::setRenderer(Render *render) {
310 if (m_render) m_render->setSceneList(m_scenelist);
313 Render *KdenliveDoc::renderer() {
317 void KdenliveDoc::updateClip(int id) {
318 emit updateClipDisplay(id);
321 int KdenliveDoc::getFramePos(QString duration) {
322 return m_timecode.getFrameCount(duration, m_fps);
325 QString KdenliveDoc::producerName(int id) {
326 QString result = "unnamed";
327 QDomNodeList prods = producersList();
328 int ct = prods.count();
329 for (int i = 0; i < ct ; i++) {
330 QDomElement e = prods.item(i).toElement();
331 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
332 result = e.attribute("name");
333 if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
340 void KdenliveDoc::setProducerDuration(int id, int duration) {
341 QDomNodeList prods = producersList();
342 int ct = prods.count();
343 for (int i = 0; i < ct ; i++) {
344 QDomElement e = prods.item(i).toElement();
345 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
346 e.setAttribute("duration", QString::number(duration));
352 int KdenliveDoc::getProducerDuration(int id) {
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("duration").toInt();
367 QDomDocument KdenliveDoc::generateSceneList() {
369 QDomElement westley = doc.createElement("westley");
370 doc.appendChild(westley);
371 QDomElement prod = doc.createElement("producer");
374 QDomDocument KdenliveDoc::toXml() const {
378 Timecode KdenliveDoc::timecode() const {
382 QDomNodeList KdenliveDoc::producersList() {
383 return m_document.elementsByTagName("producer");
386 void KdenliveDoc::backupMltPlaylist() {
387 if (m_render) m_scenelist = m_render->sceneList();
390 double KdenliveDoc::fps() const {
394 int KdenliveDoc::width() const {
398 int KdenliveDoc::height() const {
402 KUrl KdenliveDoc::url() const {
406 void KdenliveDoc::setUrl(KUrl url) {
410 void KdenliveDoc::setModified(bool mod) {
411 if (mod == m_modified) return;
413 emit docModified(m_modified);
416 bool KdenliveDoc::isModified() const {
420 QString KdenliveDoc::description() const {
422 return i18n("Untitled") + " / " + m_profile.description;
424 return m_url.fileName() + " / " + m_profile.description;
427 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
428 DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
429 kDebug() << "///////// DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
430 m_clipManager->addClip(clip);
431 emit addProjectClip(clip);
434 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
435 emit addProjectFolder(foldername, clipId, false, edit);
438 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
439 emit addProjectFolder(foldername, clipId, true);
442 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
443 for (int i = 0; i < ids.size(); ++i) {
444 emit deletTimelineClip(ids.at(i));
445 m_clipManager->slotDeleteClip(ids.at(i));
450 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
451 QMapIterator<QString, int> i(map);
452 while (i.hasNext()) {
454 slotDeleteFolder(i.key(), i.value());
459 void KdenliveDoc::deleteClip(const uint clipId) {
460 emit signalDeleteProjectClip(clipId);
461 m_clipManager->deleteClip(clipId);
464 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
465 kDebug() << "///////// DOCUM, ADD CLP: " << url;
466 m_clipManager->slotAddClipFile(url, group, groupId);
470 void KdenliveDoc::slotAddTextClipFile(const QString path, const QString group, const int groupId) {
471 kDebug() << "///////// DOCUM, ADD TXT CLP: " << path;
472 m_clipManager->slotAddTextClipFile(path, group, groupId);
476 void KdenliveDoc::slotAddFolder(const QString folderName) {
477 AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
478 commandStack()->push(command);
482 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
483 AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
484 commandStack()->push(command);
488 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
489 EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
490 commandStack()->push(command);
494 int KdenliveDoc::getFreeClipId() {
495 return m_clipManager->getFreeClipId();
498 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
499 return m_clipManager->getClipById(clipId);
502 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
503 m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
507 void KdenliveDoc::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, bool loop, const QString group, const int groupId) {
508 m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, group, groupId);
512 void KdenliveDoc::slotCreateTextClip(QString group, int groupId) {
513 QString titlesFolder = projectFolder().path() + "/titles/";
514 KStandardDirs::makeDir(titlesFolder);
515 TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, 0);
516 if (dia_ui->exec() == QDialog::Accepted) {
517 QString titleName = "title";
519 QString path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
520 while (QFile::exists(path + ".kdenlivetitle")) {
522 path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
524 QPixmap pix = dia_ui->renderedPixmap();
525 pix.save(path + ".png");
526 dia_ui->saveTitle(path + ".kdenlivetitle");
527 slotAddTextClipFile(path, QString(), -1);
532 void KdenliveDoc::editTextClip(QString path, int id) {
533 TitleWidget *dia_ui = new TitleWidget(KUrl(path + ".kdenlivetitle"), path, m_render, 0);
534 if (dia_ui->exec() == QDialog::Accepted) {
535 QPixmap pix = dia_ui->renderedPixmap();
536 pix.save(path + ".png");
537 dia_ui->saveTitle(path + ".kdenlivetitle");
538 //slotAddClipFile(KUrl("/tmp/kdenlivetitle.png"), QString(), -1);
539 emit refreshClipThumbnail(id);
545 #include "kdenlivedoc.moc"