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, 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) {
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);
52 KIO::NetAccess::removeTempFile(tmpFile);
54 KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
57 // Creating new document
58 QDomElement westley = m_document.createElement("westley");
59 m_document.appendChild(westley);
60 QDomElement doc = m_document.createElement("kdenlivedoc");
61 doc.setAttribute("version", "0.6");
62 westley.appendChild(doc);
63 QDomElement props = m_document.createElement("properties");
64 doc.setAttribute("width", m_width);
65 doc.setAttribute("height", m_height);
66 doc.setAttribute("projectfps", m_fps);
67 doc.appendChild(props);
70 /*QDomElement westley = m_document.createElement("westley");
71 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 QDomElement producer = m_document.createElement("producer");
79 /*producer.setAttribute("mlt_service", "colour");
80 producer.setAttribute("colour", "red");
81 playlist.appendChild(producer);*/
82 multitrack.appendChild(playlist);
83 QDomElement playlist1 = m_document.createElement("playlist");
84 playlist1.setAttribute("id", "playlist1");
85 playlist1.setAttribute("hide", "video");
86 multitrack.appendChild(playlist1);
87 QDomElement playlist2 = m_document.createElement("playlist");
88 playlist2.setAttribute("id", "playlist2");
89 playlist2.setAttribute("hide", "video");
90 multitrack.appendChild(playlist2);
91 QDomElement playlist3 = m_document.createElement("playlist");
92 multitrack.appendChild(playlist3);
93 playlist3.setAttribute("id", "playlist3");
94 QDomElement playlist4 = m_document.createElement("playlist");
95 multitrack.appendChild(playlist4);
96 playlist4.setAttribute("id", "playlist4");
97 QDomElement playlist5 = m_document.createElement("playlist");
98 multitrack.appendChild(playlist5);
99 playlist5.setAttribute("id", "playlist5");
100 tractor.appendChild(multitrack);
102 for (uint i = 2; i < 6 ; i++) {
103 QDomElement transition = m_document.createElement("transition");
104 transition.setAttribute("in", "0");
105 //TODO: Make audio mix last for all project duration
106 transition.setAttribute("out", "15000");
107 transition.setAttribute("a_track", QString::number(1));
108 transition.setAttribute("b_track", QString::number(i));
109 transition.setAttribute("mlt_service", "mix");
110 transition.setAttribute("combine", "1");
111 transition.setAttribute("internal_added", "237");
112 tractor.appendChild(transition);
114 QDomElement playlistmain = m_document.createElement("playlist");
115 playlistmain.setAttribute("id", "playlistmain");
116 QDomElement playentry = m_document.createElement("entry");
117 playentry.setAttribute("producer", "maintractor");
118 playentry.setAttribute("in", "0");
119 playentry.setAttribute("out", "15000");
120 playlistmain.appendChild(playentry);
121 doc.appendChild(tractor);
122 doc.appendChild(playlistmain);
125 m_scenelist = m_document.toString();
126 kDebug() << "scenelist" << m_scenelist;
127 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
128 else m_timecode.setFormat((int) m_fps);
131 KdenliveDoc::~KdenliveDoc() {
132 delete m_commandStack;
133 delete m_clipManager;
136 QDomElement KdenliveDoc::documentInfoXml() {
138 QDomElement addedXml = m_document.createElement("kdenlive");
139 addedXml.setAttribute("version", "0.7");
140 addedXml.setAttribute("profile", profilePath());
141 kDebug() << m_document.toString();
142 return m_document.documentElement();
146 ClipManager *KdenliveDoc::clipManager() {
147 return m_clipManager;
150 QString KdenliveDoc::getDocumentStandard() {
151 //WARNING: this way to tell the video standard is a bit hackish...
152 if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
156 QString KdenliveDoc::profilePath() const {
157 return m_profile.path;
160 void KdenliveDoc::setProfilePath(QString path) {
161 KdenliveSettings::setCurrent_profile(path);
162 m_profile = ProfilesDialog::getVideoProfile(path);
163 m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
164 m_width = m_profile.width;
165 m_height = m_profile.height;
166 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
167 else m_timecode.setFormat((int) m_fps);
170 void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
171 emit thumbsProgress(url, progress);
174 KUndoStack *KdenliveDoc::commandStack() {
175 return m_commandStack;
178 void KdenliveDoc::setRenderer(Render *render) {
180 if (m_render) m_render->setSceneList(m_scenelist);
183 Render *KdenliveDoc::renderer() {
187 void KdenliveDoc::updateClip(int id) {
188 emit updateClipDisplay(id);
191 int KdenliveDoc::getFramePos(QString duration) {
192 return m_timecode.getFrameCount(duration, m_fps);
195 QString KdenliveDoc::producerName(int id) {
196 QString result = "unnamed";
197 QDomNodeList prods = producersList();
198 int ct = prods.count();
199 for (int i = 0; i < ct ; i++) {
200 QDomElement e = prods.item(i).toElement();
201 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
202 result = e.attribute("name");
203 if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
210 void KdenliveDoc::setProducerDuration(int id, int duration) {
211 QDomNodeList prods = producersList();
212 int ct = prods.count();
213 for (int i = 0; i < ct ; i++) {
214 QDomElement e = prods.item(i).toElement();
215 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
216 e.setAttribute("duration", QString::number(duration));
222 int KdenliveDoc::getProducerDuration(int id) {
224 QDomNodeList prods = producersList();
225 int ct = prods.count();
226 for (int i = 0; i < ct ; i++) {
227 QDomElement e = prods.item(i).toElement();
228 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
229 result = e.attribute("duration").toInt();
237 QDomDocument KdenliveDoc::generateSceneList() {
239 QDomElement westley = doc.createElement("westley");
240 doc.appendChild(westley);
241 QDomElement prod = doc.createElement("producer");
244 QDomDocument KdenliveDoc::toXml() const {
248 Timecode KdenliveDoc::timecode() const {
252 QDomNodeList KdenliveDoc::producersList() {
253 return m_document.elementsByTagName("producer");
256 void KdenliveDoc::backupMltPlaylist() {
257 if (m_render) m_scenelist = m_render->sceneList();
260 double KdenliveDoc::fps() const {
264 int KdenliveDoc::width() const {
268 int KdenliveDoc::height() const {
272 KUrl KdenliveDoc::url() const {
276 void KdenliveDoc::setUrl(KUrl url) {
280 void KdenliveDoc::setModified(bool mod) {
281 if (mod == m_modified) return;
283 emit docModified(m_modified);
286 bool KdenliveDoc::isModified() {
290 QString KdenliveDoc::description() const {
292 return i18n("Untitled") + " / " + m_profile.description;
294 return m_url.fileName() + " / " + m_profile.description;
297 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
298 DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
299 kDebug() << "///////// DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
300 m_clipManager->addClip(clip);
301 emit addProjectClip(clip);
304 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
305 emit addProjectFolder(foldername, clipId, false, edit);
308 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
309 emit addProjectFolder(foldername, clipId, true);
312 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
313 for (int i = 0; i < ids.size(); ++i) {
314 emit deletTimelineClip(ids.at(i));
315 m_clipManager->slotDeleteClip(ids.at(i));
320 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
321 QMapIterator<QString, int> i(map);
322 while (i.hasNext()) {
324 slotDeleteFolder(i.key(), i.value());
329 void KdenliveDoc::deleteClip(const uint clipId) {
330 emit signalDeleteProjectClip(clipId);
331 m_clipManager->deleteClip(clipId);
334 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
335 kDebug() << "///////// DOCUM, ADD CLP: " << url;
336 m_clipManager->slotAddClipFile(url, group, groupId);
340 void KdenliveDoc::slotAddFolder(const QString folderName) {
341 AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
342 commandStack()->push(command);
346 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
347 AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
348 commandStack()->push(command);
352 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
353 EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
354 commandStack()->push(command);
358 int KdenliveDoc::getFreeClipId() {
359 return m_clipManager->getFreeClipId();
362 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
363 return m_clipManager->getClipById(clipId);
366 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
367 m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
371 void KdenliveDoc::slotCreateTextClip(QString group, int groupId) {
372 TitleWidget *dia_ui = new TitleWidget(m_render, 0);
373 if (dia_ui->exec() == QDialog::Accepted) {
374 QPixmap p = dia_ui->renderedPixmap();
375 p.save("/tmp/kdenlivetitle.png");
376 slotAddClipFile(KUrl("/tmp/kdenlivetitle.png"), QString(), -1);
382 #include "kdenlivedoc.moc"