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"
37 KdenliveDoc::KdenliveDoc(const KUrl &url, MltVideoProfile profile, QUndoGroup *undoGroup, QWidget *parent): QObject(parent), m_render(NULL), m_url(url), 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) {
38 m_clipManager = new ClipManager(this);
41 if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
43 m_document.setContent(&file, false);
45 QDomNode infoXmlNode = m_document.elementsByTagName("kdenlive").at(0);
46 if (!infoXmlNode.isNull()) {
47 QDomElement infoXml = infoXmlNode.toElement();
48 QString profilePath = infoXml.attribute("profile");
49 if (!profilePath.isEmpty()) setProfilePath(profilePath);
51 KIO::NetAccess::removeTempFile(tmpFile);
53 KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
56 // Creating new document
57 QDomElement westley = m_document.createElement("westley");
58 m_document.appendChild(westley);
59 QDomElement doc = m_document.createElement("kdenlivedoc");
60 doc.setAttribute("version", "0.6");
61 westley.appendChild(doc);
62 QDomElement props = m_document.createElement("properties");
63 doc.setAttribute("width", m_width);
64 doc.setAttribute("height", m_height);
65 doc.setAttribute("projectfps", m_fps);
66 doc.appendChild(props);
69 /*QDomElement westley = m_document.createElement("westley");
70 m_document.appendChild(westley);*/
73 QDomElement tractor = m_document.createElement("tractor");
74 tractor.setAttribute("id", "maintractor");
75 QDomElement multitrack = m_document.createElement("multitrack");
76 QDomElement playlist = m_document.createElement("playlist");
77 QDomElement producer = m_document.createElement("producer");
78 /*producer.setAttribute("mlt_service", "colour");
79 producer.setAttribute("colour", "red");
80 playlist.appendChild(producer);*/
81 multitrack.appendChild(playlist);
82 QDomElement playlist1 = m_document.createElement("playlist");
83 playlist1.setAttribute("id", "playlist1");
84 playlist1.setAttribute("hide", "video");
85 multitrack.appendChild(playlist1);
86 QDomElement playlist2 = m_document.createElement("playlist");
87 playlist2.setAttribute("id", "playlist2");
88 playlist2.setAttribute("hide", "video");
89 multitrack.appendChild(playlist2);
90 QDomElement playlist3 = m_document.createElement("playlist");
91 multitrack.appendChild(playlist3);
92 playlist3.setAttribute("id", "playlist3");
93 QDomElement playlist4 = m_document.createElement("playlist");
94 multitrack.appendChild(playlist4);
95 playlist4.setAttribute("id", "playlist4");
96 QDomElement playlist5 = m_document.createElement("playlist");
97 multitrack.appendChild(playlist5);
98 playlist5.setAttribute("id", "playlist5");
99 tractor.appendChild(multitrack);
101 for (uint i = 2; i < 6 ; i++) {
102 QDomElement transition = m_document.createElement("transition");
103 transition.setAttribute("in", "0");
104 //TODO: Make audio mix last for all project duration
105 transition.setAttribute("out", "15000");
106 transition.setAttribute("a_track", QString::number(1));
107 transition.setAttribute("b_track", QString::number(i));
108 transition.setAttribute("mlt_service", "mix");
109 transition.setAttribute("combine", "1");
110 tractor.appendChild(transition);
112 QDomElement playlistmain = m_document.createElement("playlist");
113 playlistmain.setAttribute("id", "playlistmain");
114 QDomElement playentry = m_document.createElement("entry");
115 playentry.setAttribute("producer", "maintractor");
116 playentry.setAttribute("in", "0");
117 playentry.setAttribute("out", "15000");
118 playlistmain.appendChild(playentry);
119 doc.appendChild(tractor);
120 doc.appendChild(playlistmain);
123 m_scenelist = m_document.toString();
124 kDebug() << "scenelist" << m_scenelist;
125 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
126 else m_timecode.setFormat((int) m_fps);
129 KdenliveDoc::~KdenliveDoc() {
130 delete m_commandStack;
131 delete m_clipManager;
134 QDomElement KdenliveDoc::documentInfoXml() {
136 QDomElement addedXml = m_document.createElement("kdenlive");
137 addedXml.setAttribute("version", "0.7");
138 addedXml.setAttribute("profile", profilePath());
139 kDebug() << m_document.toString();
140 return m_document.documentElement();
144 ClipManager *KdenliveDoc::clipManager() {
145 return m_clipManager;
148 QString KdenliveDoc::getDocumentStandard() {
149 //WARNING: this way to tell the video standard is a bit hackish...
150 if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
154 QString KdenliveDoc::profilePath() const {
155 return m_profile.path;
158 void KdenliveDoc::setProfilePath(QString path) {
159 KdenliveSettings::setCurrent_profile(path);
160 m_profile = ProfilesDialog::getVideoProfile(path);
161 m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
162 m_width = m_profile.width;
163 m_height = m_profile.height;
164 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
165 else m_timecode.setFormat((int) m_fps);
168 void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
169 emit thumbsProgress(url, progress);
172 KUndoStack *KdenliveDoc::commandStack() {
173 return m_commandStack;
176 void KdenliveDoc::setRenderer(Render *render) {
178 if (m_render) m_render->setSceneList(m_scenelist);
181 Render *KdenliveDoc::renderer() {
185 void KdenliveDoc::updateClip(int id) {
186 emit updateClipDisplay(id);
189 int KdenliveDoc::getFramePos(QString duration) {
190 return m_timecode.getFrameCount(duration, m_fps);
193 QString KdenliveDoc::producerName(int id) {
194 QString result = "unnamed";
195 QDomNodeList prods = producersList();
196 int ct = prods.count();
197 for (int i = 0; i < ct ; i++) {
198 QDomElement e = prods.item(i).toElement();
199 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
200 result = e.attribute("name");
201 if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
208 void KdenliveDoc::setProducerDuration(int id, int duration) {
209 QDomNodeList prods = producersList();
210 int ct = prods.count();
211 for (int i = 0; i < ct ; i++) {
212 QDomElement e = prods.item(i).toElement();
213 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
214 e.setAttribute("duration", QString::number(duration));
220 int KdenliveDoc::getProducerDuration(int id) {
222 QDomNodeList prods = producersList();
223 int ct = prods.count();
224 for (int i = 0; i < ct ; i++) {
225 QDomElement e = prods.item(i).toElement();
226 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
227 result = e.attribute("duration").toInt();
235 QDomDocument KdenliveDoc::generateSceneList() {
237 QDomElement westley = doc.createElement("westley");
238 doc.appendChild(westley);
239 QDomElement prod = doc.createElement("producer");
242 QDomDocument KdenliveDoc::toXml() const {
246 Timecode KdenliveDoc::timecode() const {
250 QDomNodeList KdenliveDoc::producersList() {
251 return m_document.elementsByTagName("producer");
254 void KdenliveDoc::backupMltPlaylist() {
255 if (m_render) m_scenelist = m_render->sceneList();
258 double KdenliveDoc::fps() const {
262 int KdenliveDoc::width() const {
266 int KdenliveDoc::height() const {
270 KUrl KdenliveDoc::url() const {
274 void KdenliveDoc::setUrl(KUrl url) {
278 void KdenliveDoc::setModified(bool mod) {
279 if (mod == m_modified) return;
281 emit docModified(m_modified);
284 bool KdenliveDoc::isModified() {
288 QString KdenliveDoc::description() const {
290 return i18n("Untitled") + " / " + m_profile.description;
292 return m_url.fileName() + " / " + m_profile.description;
295 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
296 DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
297 kDebug() << "///////// DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
298 m_clipManager->addClip(clip);
299 emit addProjectClip(clip);
302 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
303 emit addProjectFolder(foldername, clipId, false, edit);
306 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
307 emit addProjectFolder(foldername, clipId, true);
310 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
311 for (int i = 0; i < ids.size(); ++i) {
312 emit deletTimelineClip(ids.at(i));
313 m_clipManager->slotDeleteClip(ids.at(i));
318 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
319 QMapIterator<QString, int> i(map);
320 while (i.hasNext()) {
322 slotDeleteFolder(i.key(), i.value());
327 void KdenliveDoc::deleteClip(const uint clipId) {
328 emit signalDeleteProjectClip(clipId);
329 m_clipManager->deleteClip(clipId);
332 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
333 kDebug() << "///////// DOCUM, ADD CLP: " << url;
334 m_clipManager->slotAddClipFile(url, group, groupId);
338 void KdenliveDoc::slotAddFolder(const QString folderName) {
339 AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
340 commandStack()->push(command);
344 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
345 AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
346 commandStack()->push(command);
350 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
351 EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
352 commandStack()->push(command);
356 int KdenliveDoc::getFreeClipId() {
357 return m_clipManager->getFreeClipId();
360 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
361 return m_clipManager->getClipById(clipId);
364 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
365 m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
369 #include "kdenlivedoc.moc"