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 double version = infoXml.attribute("version").toDouble();
51 if (version < 0.7) convertDocument(version);
52 if (!profilePath.isEmpty()) setProfilePath(profilePath);
53 QDomNodeList producers = infoXmlNode.childNodes();
55 for (int i = 0; i < producers.count(); i++) {
56 e = producers.item(i).cloneNode().toElement();
57 if (!e.isNull() && e.tagName() == "kdenlive_producer") {
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 // move playlists outside of tractor and add the tracks instead
171 int max = playlists.count();
172 for (int i = 0; i < max; i++) {
173 QDomNode n = playlists.at(i);
174 westley.insertBefore(n, QDomNode());
175 QDomElement track = m_document.createElement("track");
176 QString playlist_id = n.toElement().attribute("id");
177 if (playlist_id.isEmpty()) {
178 playlist_id = "black_track";
179 n.toElement().setAttribute("id", playlist_id);
181 track.setAttribute("producer", playlist_id);
182 tractor.appendChild(track);
184 tractor.removeChild(multitrack);
186 // audio track mixing transitions should not be added to track view, so add required attribute
187 QDomNodeList transitions = m_document.elementsByTagName("transition");
188 max = transitions.count();
189 for (int i = 0; i < max; i++) {
190 QDomElement tr = transitions.at(i).toElement();
191 if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") {
192 QDomElement property = m_document.createElement("property");
193 property.setAttribute("name", "internal_added");
194 QDomText value = m_document.createTextNode("237");
195 property.appendChild(value);
196 tr.appendChild(property);
200 // move transitions after tracks
201 for (int i = 0; i < max; i++) {
202 tractor.insertAfter(transitions.at(0), QDomNode());
205 // change producer names
206 QDomNodeList producers = m_document.elementsByTagName("producer");
207 max = producers.count();
208 for (int i = 0; i < max; i++) {
209 QDomNode prod = producers.at(i);
210 prod.toElement().setTagName("kdenlive_producer");
211 //kdenlivedoc.insertBefore(prod, QDomNode());
215 kDebug() << "///////////////// CONVERTED DOC:";
216 kDebug() << m_document.toString();
217 kDebug() << "///////////////// END CONVERTED DOC:";
220 QDomElement KdenliveDoc::documentInfoXml() {
223 QDomElement addedXml = doc.createElement("kdenlivedoc");
224 addedXml.setAttribute("version", "0.7");
225 addedXml.setAttribute("profile", profilePath());
226 QList <DocClipBase*> list = m_clipManager->documentClipList();
227 for (int i = 0; i < list.count(); i++) {
228 e = list.at(i)->toXML();
229 e.setTagName("kdenlive_producer");
230 addedXml.appendChild(doc.importNode(e, true));
233 //kDebug() << m_document.toString();
238 ClipManager *KdenliveDoc::clipManager() {
239 return m_clipManager;
242 KUrl KdenliveDoc::projectFolder() const {
243 if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
244 return m_projectFolder;
247 QString KdenliveDoc::getDocumentStandard() {
248 //WARNING: this way to tell the video standard is a bit hackish...
249 if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
253 QString KdenliveDoc::profilePath() const {
254 return m_profile.path;
257 void KdenliveDoc::setProfilePath(QString path) {
258 KdenliveSettings::setCurrent_profile(path);
259 m_profile = ProfilesDialog::getVideoProfile(path);
260 m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
261 m_width = m_profile.width;
262 m_height = m_profile.height;
263 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
264 else m_timecode.setFormat((int) m_fps);
267 void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
268 emit thumbsProgress(url, progress);
271 KUndoStack *KdenliveDoc::commandStack() {
272 return m_commandStack;
275 void KdenliveDoc::setRenderer(Render *render) {
277 if (m_render) m_render->setSceneList(m_scenelist);
280 Render *KdenliveDoc::renderer() {
284 void KdenliveDoc::updateClip(int id) {
285 emit updateClipDisplay(id);
288 int KdenliveDoc::getFramePos(QString duration) {
289 return m_timecode.getFrameCount(duration, m_fps);
292 QString KdenliveDoc::producerName(int id) {
293 QString result = "unnamed";
294 QDomNodeList prods = producersList();
295 int ct = prods.count();
296 for (int i = 0; i < ct ; i++) {
297 QDomElement e = prods.item(i).toElement();
298 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
299 result = e.attribute("name");
300 if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
307 void KdenliveDoc::setProducerDuration(int id, int duration) {
308 QDomNodeList prods = producersList();
309 int ct = prods.count();
310 for (int i = 0; i < ct ; i++) {
311 QDomElement e = prods.item(i).toElement();
312 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
313 e.setAttribute("duration", QString::number(duration));
319 int KdenliveDoc::getProducerDuration(int id) {
321 QDomNodeList prods = producersList();
322 int ct = prods.count();
323 for (int i = 0; i < ct ; i++) {
324 QDomElement e = prods.item(i).toElement();
325 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
326 result = e.attribute("duration").toInt();
334 QDomDocument KdenliveDoc::generateSceneList() {
336 QDomElement westley = doc.createElement("westley");
337 doc.appendChild(westley);
338 QDomElement prod = doc.createElement("producer");
341 QDomDocument KdenliveDoc::toXml() const {
345 Timecode KdenliveDoc::timecode() const {
349 QDomNodeList KdenliveDoc::producersList() {
350 return m_document.elementsByTagName("producer");
353 void KdenliveDoc::backupMltPlaylist() {
354 if (m_render) m_scenelist = m_render->sceneList();
357 double KdenliveDoc::fps() const {
361 int KdenliveDoc::width() const {
365 int KdenliveDoc::height() const {
369 KUrl KdenliveDoc::url() const {
373 void KdenliveDoc::setUrl(KUrl url) {
377 void KdenliveDoc::setModified(bool mod) {
378 if (mod == m_modified) return;
380 emit docModified(m_modified);
383 bool KdenliveDoc::isModified() const {
387 QString KdenliveDoc::description() const {
389 return i18n("Untitled") + " / " + m_profile.description;
391 return m_url.fileName() + " / " + m_profile.description;
394 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
395 DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
396 kDebug() << "///////// DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
397 m_clipManager->addClip(clip);
398 emit addProjectClip(clip);
401 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
402 emit addProjectFolder(foldername, clipId, false, edit);
405 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
406 emit addProjectFolder(foldername, clipId, true);
409 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
410 for (int i = 0; i < ids.size(); ++i) {
411 emit deletTimelineClip(ids.at(i));
412 m_clipManager->slotDeleteClip(ids.at(i));
417 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
418 QMapIterator<QString, int> i(map);
419 while (i.hasNext()) {
421 slotDeleteFolder(i.key(), i.value());
426 void KdenliveDoc::deleteClip(const uint clipId) {
427 emit signalDeleteProjectClip(clipId);
428 m_clipManager->deleteClip(clipId);
431 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
432 kDebug() << "///////// DOCUM, ADD CLP: " << url;
433 m_clipManager->slotAddClipFile(url, group, groupId);
437 void KdenliveDoc::slotAddTextClipFile(const QString path, const QString group, const int groupId) {
438 kDebug() << "///////// DOCUM, ADD TXT CLP: " << path;
439 m_clipManager->slotAddTextClipFile(path, group, groupId);
443 void KdenliveDoc::slotAddFolder(const QString folderName) {
444 AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
445 commandStack()->push(command);
449 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
450 AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
451 commandStack()->push(command);
455 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
456 EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
457 commandStack()->push(command);
461 int KdenliveDoc::getFreeClipId() {
462 return m_clipManager->getFreeClipId();
465 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
466 return m_clipManager->getClipById(clipId);
469 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
470 m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
474 void KdenliveDoc::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, bool loop, const QString group, const int groupId) {
475 m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, group, groupId);
479 void KdenliveDoc::slotCreateTextClip(QString group, int groupId) {
480 QString titlesFolder = projectFolder().path() + "/titles/";
481 KStandardDirs::makeDir(titlesFolder);
482 TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, 0);
483 if (dia_ui->exec() == QDialog::Accepted) {
484 QString titleName = "title";
486 QString path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
487 while (QFile::exists(path + ".kdenlivetitle")) {
489 path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
491 QPixmap pix = dia_ui->renderedPixmap();
492 pix.save(path + ".png");
493 dia_ui->saveTitle(path + ".kdenlivetitle");
494 slotAddTextClipFile(path, QString(), -1);
499 void KdenliveDoc::editTextClip(QString path, int id) {
500 TitleWidget *dia_ui = new TitleWidget(KUrl(path + ".kdenlivetitle"), path, m_render, 0);
501 if (dia_ui->exec() == QDialog::Accepted) {
502 QPixmap pix = dia_ui->renderedPixmap();
503 pix.save(path + ".png");
504 dia_ui->saveTitle(path + ".kdenlivetitle");
505 //slotAddClipFile(KUrl("/tmp/kdenlivetitle.png"), QString(), -1);
506 emit refreshClipThumbnail(id);
512 #include "kdenlivedoc.moc"