]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
load transitions when opening project file
[kdenlive] / src / kdenlivedoc.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20 #include <KDebug>
21 #include <KStandardDirs>
22 #include <KMessageBox>
23 #include <KLocale>
24 #include <KFileDialog>
25 #include <KIO/NetAccess>
26
27
28 #include "kdenlivedoc.h"
29 #include "docclipbase.h"
30 #include "profilesdialog.h"
31 #include "kdenlivesettings.h"
32 #include "renderer.h"
33 #include "clipmanager.h"
34 #include "addfoldercommand.h"
35 #include "editfoldercommand.h"
36 #include "titlewidget.h"
37
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);
40     if (!url.isEmpty()) {
41         QString tmpFile;
42         if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
43             QFile file(tmpFile);
44             m_document.setContent(&file, false);
45             file.close();
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();
52                 QDomElement e;
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());
58                     }
59                 }
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);
64         } else {
65             KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
66         }
67     } else {
68         // Creating new document
69         QDomElement westley = m_document.createElement("westley");
70         m_document.appendChild(westley);
71
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);
78
79
80         // create playlists
81         int audiotracks = 2;
82         int videotracks = 3;
83         int total = audiotracks + videotracks + 1;
84
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);
89         }
90
91         QDomElement track0 = m_document.createElement("track");
92         track0.setAttribute("producer", "black_track");
93         tractor.appendChild(track0);
94
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);
101         }
102
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);
108         }
109
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");
115
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);
121
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);
127
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);
133
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);
139
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);
146         }
147         westley.appendChild(tractor);
148     }
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);
153 }
154
155 KdenliveDoc::~KdenliveDoc() {
156     delete m_commandStack;
157     delete m_clipManager;
158 }
159
160 QDomElement KdenliveDoc::documentInfoXml() {
161     QDomDocument doc;
162     QDomElement e;
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));
171     }
172
173     //kDebug() << m_document.toString();
174     return addedXml;
175 }
176
177
178 ClipManager *KdenliveDoc::clipManager() {
179     return m_clipManager;
180 }
181
182 KUrl KdenliveDoc::projectFolder() const {
183     if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
184     return m_projectFolder;
185 }
186
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";
190     return "NTSC";
191 }
192
193 QString KdenliveDoc::profilePath() const {
194     return m_profile.path;
195 }
196
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);
205 }
206
207 void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
208     emit thumbsProgress(url, progress);
209 }
210
211 KUndoStack *KdenliveDoc::commandStack() {
212     return m_commandStack;
213 }
214
215 void KdenliveDoc::setRenderer(Render *render) {
216     m_render = render;
217     if (m_render) m_render->setSceneList(m_scenelist);
218 }
219
220 Render *KdenliveDoc::renderer() {
221     return m_render;
222 }
223
224 void KdenliveDoc::updateClip(int id) {
225     emit updateClipDisplay(id);
226 }
227
228 int KdenliveDoc::getFramePos(QString duration) {
229     return m_timecode.getFrameCount(duration, m_fps);
230 }
231
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();
241             break;
242         }
243     }
244     return result;
245 }
246
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));
254             break;
255         }
256     }
257 }
258
259 int KdenliveDoc::getProducerDuration(int id) {
260     int result = 0;
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();
267             break;
268         }
269     }
270     return result;
271 }
272
273
274 QDomDocument KdenliveDoc::generateSceneList() {
275     QDomDocument doc;
276     QDomElement westley = doc.createElement("westley");
277     doc.appendChild(westley);
278     QDomElement prod = doc.createElement("producer");
279 }
280
281 QDomDocument KdenliveDoc::toXml() const {
282     return m_document;
283 }
284
285 Timecode KdenliveDoc::timecode() const {
286     return m_timecode;
287 }
288
289 QDomNodeList KdenliveDoc::producersList() {
290     return m_document.elementsByTagName("producer");
291 }
292
293 void KdenliveDoc::backupMltPlaylist() {
294     if (m_render) m_scenelist = m_render->sceneList();
295 }
296
297 double KdenliveDoc::fps() const {
298     return m_fps;
299 }
300
301 int KdenliveDoc::width() const {
302     return m_width;
303 }
304
305 int KdenliveDoc::height() const {
306     return m_height;
307 }
308
309 KUrl KdenliveDoc::url() const {
310     return m_url;
311 }
312
313 void KdenliveDoc::setUrl(KUrl url) {
314     m_url = url;
315 }
316
317 void KdenliveDoc::setModified(bool mod) {
318     if (mod == m_modified) return;
319     m_modified = mod;
320     emit docModified(m_modified);
321 }
322
323 bool KdenliveDoc::isModified() const {
324     return m_modified;
325 }
326
327 QString KdenliveDoc::description() const {
328     if (m_url.isEmpty())
329         return i18n("Untitled") + " / " + m_profile.description;
330     else
331         return m_url.fileName() + " / " + m_profile.description;
332 }
333
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);
339 }
340
341 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
342     emit addProjectFolder(foldername, clipId, false, edit);
343 }
344
345 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
346     emit addProjectFolder(foldername, clipId, true);
347 }
348
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));
353     }
354     setModified(true);
355 }
356
357 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
358     QMapIterator<QString, int> i(map);
359     while (i.hasNext()) {
360         i.next();
361         slotDeleteFolder(i.key(), i.value());
362     }
363     setModified(true);
364 }
365
366 void KdenliveDoc::deleteClip(const uint clipId) {
367     emit signalDeleteProjectClip(clipId);
368     m_clipManager->deleteClip(clipId);
369 }
370
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);
374     setModified(true);
375 }
376
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);
380     setModified(true);
381 }
382
383 void KdenliveDoc::slotAddFolder(const QString folderName) {
384     AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
385     commandStack()->push(command);
386     setModified(true);
387 }
388
389 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
390     AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
391     commandStack()->push(command);
392     setModified(true);
393 }
394
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);
398     setModified(true);
399 }
400
401 int KdenliveDoc::getFreeClipId() {
402     return m_clipManager->getFreeClipId();
403 }
404
405 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
406     return m_clipManager->getClipById(clipId);
407 }
408
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);
411     setModified(true);
412 }
413
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);
416     setModified(true);
417 }
418
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";
425         int counter = 0;
426         QString path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
427         while (QFile::exists(path + ".kdenlivetitle")) {
428             counter++;
429             path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
430         }
431         QPixmap pix = dia_ui->renderedPixmap();
432         pix.save(path + ".png");
433         dia_ui->saveTitle(path + ".kdenlivetitle");
434         slotAddTextClipFile(path, QString(), -1);
435     }
436     delete dia_ui;
437 }
438
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);
447     }
448     delete dia_ui;
449 }
450
451
452 #include "kdenlivedoc.moc"
453