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);
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 KUrl KdenliveDoc::projectFolder() const {
151 if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
152 return m_projectFolder;
155 QString KdenliveDoc::getDocumentStandard() {
156 //WARNING: this way to tell the video standard is a bit hackish...
157 if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
161 QString KdenliveDoc::profilePath() const {
162 return m_profile.path;
165 void KdenliveDoc::setProfilePath(QString path) {
166 KdenliveSettings::setCurrent_profile(path);
167 m_profile = ProfilesDialog::getVideoProfile(path);
168 m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
169 m_width = m_profile.width;
170 m_height = m_profile.height;
171 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
172 else m_timecode.setFormat((int) m_fps);
175 void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
176 emit thumbsProgress(url, progress);
179 KUndoStack *KdenliveDoc::commandStack() {
180 return m_commandStack;
183 void KdenliveDoc::setRenderer(Render *render) {
185 if (m_render) m_render->setSceneList(m_scenelist);
188 Render *KdenliveDoc::renderer() {
192 void KdenliveDoc::updateClip(int id) {
193 emit updateClipDisplay(id);
196 int KdenliveDoc::getFramePos(QString duration) {
197 return m_timecode.getFrameCount(duration, m_fps);
200 QString KdenliveDoc::producerName(int id) {
201 QString result = "unnamed";
202 QDomNodeList prods = producersList();
203 int ct = prods.count();
204 for (int i = 0; i < ct ; i++) {
205 QDomElement e = prods.item(i).toElement();
206 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
207 result = e.attribute("name");
208 if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
215 void KdenliveDoc::setProducerDuration(int id, int duration) {
216 QDomNodeList prods = producersList();
217 int ct = prods.count();
218 for (int i = 0; i < ct ; i++) {
219 QDomElement e = prods.item(i).toElement();
220 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
221 e.setAttribute("duration", QString::number(duration));
227 int KdenliveDoc::getProducerDuration(int id) {
229 QDomNodeList prods = producersList();
230 int ct = prods.count();
231 for (int i = 0; i < ct ; i++) {
232 QDomElement e = prods.item(i).toElement();
233 if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
234 result = e.attribute("duration").toInt();
242 QDomDocument KdenliveDoc::generateSceneList() {
244 QDomElement westley = doc.createElement("westley");
245 doc.appendChild(westley);
246 QDomElement prod = doc.createElement("producer");
249 QDomDocument KdenliveDoc::toXml() const {
253 Timecode KdenliveDoc::timecode() const {
257 QDomNodeList KdenliveDoc::producersList() {
258 return m_document.elementsByTagName("producer");
261 void KdenliveDoc::backupMltPlaylist() {
262 if (m_render) m_scenelist = m_render->sceneList();
265 double KdenliveDoc::fps() const {
269 int KdenliveDoc::width() const {
273 int KdenliveDoc::height() const {
277 KUrl KdenliveDoc::url() const {
281 void KdenliveDoc::setUrl(KUrl url) {
285 void KdenliveDoc::setModified(bool mod) {
286 if (mod == m_modified) return;
288 emit docModified(m_modified);
291 bool KdenliveDoc::isModified() const {
295 QString KdenliveDoc::description() const {
297 return i18n("Untitled") + " / " + m_profile.description;
299 return m_url.fileName() + " / " + m_profile.description;
302 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
303 DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
304 kDebug() << "///////// DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
305 m_clipManager->addClip(clip);
306 emit addProjectClip(clip);
309 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
310 emit addProjectFolder(foldername, clipId, false, edit);
313 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
314 emit addProjectFolder(foldername, clipId, true);
317 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
318 for (int i = 0; i < ids.size(); ++i) {
319 emit deletTimelineClip(ids.at(i));
320 m_clipManager->slotDeleteClip(ids.at(i));
325 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
326 QMapIterator<QString, int> i(map);
327 while (i.hasNext()) {
329 slotDeleteFolder(i.key(), i.value());
334 void KdenliveDoc::deleteClip(const uint clipId) {
335 emit signalDeleteProjectClip(clipId);
336 m_clipManager->deleteClip(clipId);
339 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
340 kDebug() << "///////// DOCUM, ADD CLP: " << url;
341 m_clipManager->slotAddClipFile(url, group, groupId);
345 void KdenliveDoc::slotAddTextClipFile(const QString path, const QString group, const int groupId) {
346 kDebug() << "///////// DOCUM, ADD TXT CLP: " << path;
347 m_clipManager->slotAddTextClipFile(path, group, groupId);
351 void KdenliveDoc::slotAddFolder(const QString folderName) {
352 AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
353 commandStack()->push(command);
357 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
358 AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
359 commandStack()->push(command);
363 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
364 EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
365 commandStack()->push(command);
369 int KdenliveDoc::getFreeClipId() {
370 return m_clipManager->getFreeClipId();
373 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
374 return m_clipManager->getClipById(clipId);
377 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
378 m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
382 void KdenliveDoc::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, bool loop, const QString group, const int groupId) {
383 m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, group, groupId);
387 void KdenliveDoc::slotCreateTextClip(QString group, int groupId) {
388 QString titlesFolder = projectFolder().path() + "/titles/";
389 KStandardDirs::makeDir(titlesFolder);
390 TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, 0);
391 if (dia_ui->exec() == QDialog::Accepted) {
392 QString titleName = "title";
394 QString path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
395 while (QFile::exists(path + ".kdenlivetitle")) {
397 path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
399 QPixmap pix = dia_ui->renderedPixmap();
400 pix.save(path + ".png");
401 dia_ui->saveTitle(path + ".kdenlivetitle");
402 slotAddTextClipFile(path, QString(), -1);
407 void KdenliveDoc::editTextClip(QString path, int id) {
408 TitleWidget *dia_ui = new TitleWidget(KUrl(path + ".kdenlivetitle"), path, m_render, 0);
409 if (dia_ui->exec() == QDialog::Accepted) {
410 QPixmap pix = dia_ui->renderedPixmap();
411 pix.save(path + ".png");
412 dia_ui->saveTitle(path + ".kdenlivetitle");
413 //slotAddClipFile(KUrl("/tmp/kdenlivetitle.png"), QString(), -1);
414 emit refreshClipThumbnail(id);
420 #include "kdenlivedoc.moc"