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("kdenlive").at(0);
47 if (!infoXmlNode.isNull()) {
48 QDomElement infoXml = infoXmlNode.toElement();
49 QString profilePath = infoXml.attribute("profile");
50 if (!profilePath.isEmpty()) setProfilePath(profilePath);
51 QDomNodeList producers = infoXmlNode.childNodes();
53 for (int i = 0; i < producers.count(); i++) {
54 e = producers.item(i).cloneNode().toElement();
55 if (!e.isNull() && e.tagName() == "kdenlive_producer") {
56 e.setTagName("producer");
57 addClip(e, e.attribute("id").toInt());
60 m_document.removeChild(infoXmlNode);
61 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
62 } else kWarning() << " NO KDENLIVE INFO FOUND IN FILE: " << url.path();
63 KIO::NetAccess::removeTempFile(tmpFile);
65 KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
68 // Creating new document
69 QDomElement westley = m_document.createElement("westley");
70 m_document.appendChild(westley);
72 QDomElement tractor = m_document.createElement("tractor");
73 tractor.setAttribute("id", "maintractor");
74 QDomElement multitrack = m_document.createElement("multitrack");
75 QDomElement playlist = m_document.createElement("playlist");
76 playlist.setAttribute("id", "black_track");
77 westley.appendChild(playlist);
83 int total = audiotracks + videotracks + 1;
85 for (int i = 1; i < total; i++) {
86 QDomElement playlist = m_document.createElement("playlist");
87 playlist.setAttribute("id", "playlist" + QString::number(i));
88 westley.appendChild(playlist);
91 QDomElement track0 = m_document.createElement("track");
92 track0.setAttribute("producer", "black_track");
93 tractor.appendChild(track0);
95 // create audio tracks
96 for (int i = 1; i < audiotracks + 1; i++) {
97 QDomElement track = m_document.createElement("track");
98 track.setAttribute("producer", "playlist" + QString::number(i));
99 track.setAttribute("hide", "video");
100 tractor.appendChild(track);
103 // create video tracks
104 for (int i = audiotracks + 1; i < total; i++) {
105 QDomElement track = m_document.createElement("track");
106 track.setAttribute("producer", "playlist" + QString::number(i));
107 tractor.appendChild(track);
110 for (uint i = 2; i < total ; i++) {
111 QDomElement transition = m_document.createElement("transition");
112 transition.setAttribute("in", "0");
113 //TODO: Make audio mix last for all project duration
114 transition.setAttribute("out", "15000");
116 QDomElement property = m_document.createElement("property");
117 property.setAttribute("name", "a_track");
118 QDomText value = m_document.createTextNode(QString::number(1));
119 property.appendChild(value);
120 transition.appendChild(property);
122 property = m_document.createElement("property");
123 property.setAttribute("name", "b_track");
124 value = m_document.createTextNode(QString::number(i));
125 property.appendChild(value);
126 transition.appendChild(property);
128 property = m_document.createElement("property");
129 property.setAttribute("name", "mlt_service");
130 value = m_document.createTextNode("mix");
131 property.appendChild(value);
132 transition.appendChild(property);
134 property = m_document.createElement("property");
135 property.setAttribute("name", "combine");
136 value = m_document.createTextNode("1");
137 property.appendChild(value);
138 transition.appendChild(property);
140 property = m_document.createElement("property");
141 property.setAttribute("name", "internal_added");
142 value = m_document.createTextNode("237");
143 property.appendChild(value);
144 transition.appendChild(property);
145 tractor.appendChild(transition);
147 westley.appendChild(tractor);
149 m_scenelist = m_document.toString();
150 // kDebug() << "scenelist" << m_scenelist;
151 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
152 else m_timecode.setFormat((int) m_fps);
155 KdenliveDoc::~KdenliveDoc() {
156 delete m_commandStack;
157 delete m_clipManager;
160 QDomElement KdenliveDoc::documentInfoXml() {
163 QDomElement addedXml = doc.createElement("kdenlive");
164 addedXml.setAttribute("version", "0.7");
165 addedXml.setAttribute("profile", profilePath());
166 QList <DocClipBase*> list = m_clipManager->documentClipList();
167 for (int i = 0; i < list.count(); i++) {
168 e = list.at(i)->toXML();
169 e.setTagName("kdenlive_producer");
170 addedXml.appendChild(doc.importNode(e, true));
173 //kDebug() << m_document.toString();
178 ClipManager *KdenliveDoc::clipManager() {
179 return m_clipManager;
182 KUrl KdenliveDoc::projectFolder() const {
183 if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
184 return m_projectFolder;
187 QString KdenliveDoc::getDocumentStandard() {
188 //WARNING: this way to tell the video standard is a bit hackish...
189 if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
193 QString KdenliveDoc::profilePath() const {
194 return m_profile.path;
197 void KdenliveDoc::setProfilePath(QString path) {
198 KdenliveSettings::setCurrent_profile(path);
199 m_profile = ProfilesDialog::getVideoProfile(path);
200 m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
201 m_width = m_profile.width;
202 m_height = m_profile.height;
203 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
204 else m_timecode.setFormat((int) m_fps);
207 void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
208 emit thumbsProgress(url, progress);
211 KUndoStack *KdenliveDoc::commandStack() {
212 return m_commandStack;
215 void KdenliveDoc::setRenderer(Render *render) {
217 if (m_render) m_render->setSceneList(m_scenelist);
220 Render *KdenliveDoc::renderer() {
224 void KdenliveDoc::updateClip(int id) {
225 emit updateClipDisplay(id);
228 int KdenliveDoc::getFramePos(QString duration) {
229 return m_timecode.getFrameCount(duration, m_fps);
232 QString KdenliveDoc::producerName(int id) {
233 QString result = "unnamed";
234 QDomNodeList prods = producersList();
235 int ct = prods.count();
236 for (int i = 0; i < ct ; i++) {
237 QDomElement e = prods.item(i).toElement();
238 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
239 result = e.attribute("name");
240 if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
247 void KdenliveDoc::setProducerDuration(int id, int duration) {
248 QDomNodeList prods = producersList();
249 int ct = prods.count();
250 for (int i = 0; i < ct ; i++) {
251 QDomElement e = prods.item(i).toElement();
252 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
253 e.setAttribute("duration", QString::number(duration));
259 int KdenliveDoc::getProducerDuration(int id) {
261 QDomNodeList prods = producersList();
262 int ct = prods.count();
263 for (int i = 0; i < ct ; i++) {
264 QDomElement e = prods.item(i).toElement();
265 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
266 result = e.attribute("duration").toInt();
274 QDomDocument KdenliveDoc::generateSceneList() {
276 QDomElement westley = doc.createElement("westley");
277 doc.appendChild(westley);
278 QDomElement prod = doc.createElement("producer");
281 QDomDocument KdenliveDoc::toXml() const {
285 Timecode KdenliveDoc::timecode() const {
289 QDomNodeList KdenliveDoc::producersList() {
290 return m_document.elementsByTagName("producer");
293 void KdenliveDoc::backupMltPlaylist() {
294 if (m_render) m_scenelist = m_render->sceneList();
297 double KdenliveDoc::fps() const {
301 int KdenliveDoc::width() const {
305 int KdenliveDoc::height() const {
309 KUrl KdenliveDoc::url() const {
313 void KdenliveDoc::setUrl(KUrl url) {
317 void KdenliveDoc::setModified(bool mod) {
318 if (mod == m_modified) return;
320 emit docModified(m_modified);
323 bool KdenliveDoc::isModified() const {
327 QString KdenliveDoc::description() const {
329 return i18n("Untitled") + " / " + m_profile.description;
331 return m_url.fileName() + " / " + m_profile.description;
334 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
335 DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
336 kDebug() << "///////// DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
337 m_clipManager->addClip(clip);
338 emit addProjectClip(clip);
341 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
342 emit addProjectFolder(foldername, clipId, false, edit);
345 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
346 emit addProjectFolder(foldername, clipId, true);
349 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
350 for (int i = 0; i < ids.size(); ++i) {
351 emit deletTimelineClip(ids.at(i));
352 m_clipManager->slotDeleteClip(ids.at(i));
357 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
358 QMapIterator<QString, int> i(map);
359 while (i.hasNext()) {
361 slotDeleteFolder(i.key(), i.value());
366 void KdenliveDoc::deleteClip(const uint clipId) {
367 emit signalDeleteProjectClip(clipId);
368 m_clipManager->deleteClip(clipId);
371 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
372 kDebug() << "///////// DOCUM, ADD CLP: " << url;
373 m_clipManager->slotAddClipFile(url, group, groupId);
377 void KdenliveDoc::slotAddTextClipFile(const QString path, const QString group, const int groupId) {
378 kDebug() << "///////// DOCUM, ADD TXT CLP: " << path;
379 m_clipManager->slotAddTextClipFile(path, group, groupId);
383 void KdenliveDoc::slotAddFolder(const QString folderName) {
384 AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
385 commandStack()->push(command);
389 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
390 AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
391 commandStack()->push(command);
395 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
396 EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
397 commandStack()->push(command);
401 int KdenliveDoc::getFreeClipId() {
402 return m_clipManager->getFreeClipId();
405 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
406 return m_clipManager->getClipById(clipId);
409 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
410 m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
414 void KdenliveDoc::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, bool loop, const QString group, const int groupId) {
415 m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, group, groupId);
419 void KdenliveDoc::slotCreateTextClip(QString group, int groupId) {
420 QString titlesFolder = projectFolder().path() + "/titles/";
421 KStandardDirs::makeDir(titlesFolder);
422 TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, 0);
423 if (dia_ui->exec() == QDialog::Accepted) {
424 QString titleName = "title";
426 QString path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
427 while (QFile::exists(path + ".kdenlivetitle")) {
429 path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
431 QPixmap pix = dia_ui->renderedPixmap();
432 pix.save(path + ".png");
433 dia_ui->saveTitle(path + ".kdenlivetitle");
434 slotAddTextClipFile(path, QString(), -1);
439 void KdenliveDoc::editTextClip(QString path, int id) {
440 TitleWidget *dia_ui = new TitleWidget(KUrl(path + ".kdenlivetitle"), path, m_render, 0);
441 if (dia_ui->exec() == QDialog::Accepted) {
442 QPixmap pix = dia_ui->renderedPixmap();
443 pix.save(path + ".png");
444 dia_ui->saveTitle(path + ".kdenlivetitle");
445 //slotAddClipFile(KUrl("/tmp/kdenlivetitle.png"), QString(), -1);
446 emit refreshClipThumbnail(id);
452 #include "kdenlivedoc.moc"