]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
allow to open the kdenlive KDE3 project files (not finished yet)
[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("kdenlivedoc").at(0);
47             if (!infoXmlNode.isNull()) {
48                 QDomElement infoXml = infoXmlNode.toElement();
49                 QString profilePath = infoXml.attribute("profile");
50                 double version = infoXml.attribute("version").toDouble();
51                 if (version < 0.7) convertDocument(version);
52                 if (!profilePath.isEmpty()) setProfilePath(profilePath);
53                 QDomNodeList producers = infoXmlNode.childNodes();
54                 QDomElement e;
55                 for (int i = 0; i < producers.count(); i++) {
56                     e = producers.item(i).cloneNode().toElement();
57                     if (!e.isNull() && e.tagName() == "kdenlive_producer") {
58                         e.setTagName("producer");
59                         addClip(e, e.attribute("id").toInt());
60                     }
61                 }
62                 m_document.removeChild(infoXmlNode);
63                 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
64             } else kWarning() << "  NO KDENLIVE INFO FOUND IN FILE: " << url.path();
65             KIO::NetAccess::removeTempFile(tmpFile);
66         } else {
67             KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
68         }
69     } else {
70         // Creating new document
71         QDomElement westley = m_document.createElement("westley");
72         m_document.appendChild(westley);
73
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         playlist.setAttribute("id", "black_track");
79         westley.appendChild(playlist);
80
81
82         // create playlists
83         int audiotracks = 2;
84         int videotracks = 3;
85         int total = audiotracks + videotracks + 1;
86
87         for (int i = 1; i < total; i++) {
88             QDomElement playlist = m_document.createElement("playlist");
89             playlist.setAttribute("id", "playlist" + QString::number(i));
90             westley.appendChild(playlist);
91         }
92
93         QDomElement track0 = m_document.createElement("track");
94         track0.setAttribute("producer", "black_track");
95         tractor.appendChild(track0);
96
97         // create audio tracks
98         for (int i = 1; i < audiotracks + 1; i++) {
99             QDomElement track = m_document.createElement("track");
100             track.setAttribute("producer", "playlist" + QString::number(i));
101             track.setAttribute("hide", "video");
102             tractor.appendChild(track);
103         }
104
105         // create video tracks
106         for (int i = audiotracks + 1; i < total; i++) {
107             QDomElement track = m_document.createElement("track");
108             track.setAttribute("producer", "playlist" + QString::number(i));
109             tractor.appendChild(track);
110         }
111
112         for (uint i = 2; i < total ; i++) {
113             QDomElement transition = m_document.createElement("transition");
114             transition.setAttribute("in", "0");
115             //TODO: Make audio mix last for all project duration
116             transition.setAttribute("out", "15000");
117
118             QDomElement property = m_document.createElement("property");
119             property.setAttribute("name", "a_track");
120             QDomText value = m_document.createTextNode(QString::number(1));
121             property.appendChild(value);
122             transition.appendChild(property);
123
124             property = m_document.createElement("property");
125             property.setAttribute("name", "b_track");
126             value = m_document.createTextNode(QString::number(i));
127             property.appendChild(value);
128             transition.appendChild(property);
129
130             property = m_document.createElement("property");
131             property.setAttribute("name", "mlt_service");
132             value = m_document.createTextNode("mix");
133             property.appendChild(value);
134             transition.appendChild(property);
135
136             property = m_document.createElement("property");
137             property.setAttribute("name", "combine");
138             value = m_document.createTextNode("1");
139             property.appendChild(value);
140             transition.appendChild(property);
141
142             property = m_document.createElement("property");
143             property.setAttribute("name", "internal_added");
144             value = m_document.createTextNode("237");
145             property.appendChild(value);
146             transition.appendChild(property);
147             tractor.appendChild(transition);
148         }
149         westley.appendChild(tractor);
150     }
151     m_scenelist = m_document.toString();
152     // kDebug() << "scenelist" << m_scenelist;
153     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
154     else m_timecode.setFormat((int) m_fps);
155 }
156
157 KdenliveDoc::~KdenliveDoc() {
158     delete m_commandStack;
159     delete m_clipManager;
160 }
161
162 void KdenliveDoc::convertDocument(double version) {
163     // Opening a old Kdenlive document
164     QDomNode westley = m_document.elementsByTagName("westley").at(1);
165     QDomNode tractor = m_document.elementsByTagName("tractor").at(0);
166     QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
167     QDomNode multitrack = m_document.elementsByTagName("multitrack").at(0);
168     QDomNodeList playlists = m_document.elementsByTagName("playlist");
169
170     // move playlists outside of tractor and add the tracks instead
171     int max = playlists.count();
172     for (int i = 0; i < max; i++) {
173         QDomNode n = playlists.at(i);
174         westley.insertBefore(n, QDomNode());
175         QDomElement track = m_document.createElement("track");
176         QString playlist_id =  n.toElement().attribute("id");
177         if (playlist_id.isEmpty()) {
178             playlist_id = "black_track";
179             n.toElement().setAttribute("id", playlist_id);
180         }
181         track.setAttribute("producer", playlist_id);
182         tractor.appendChild(track);
183     }
184     tractor.removeChild(multitrack);
185
186     // audio track mixing transitions should not be added to track view, so add required attribute
187     QDomNodeList transitions = m_document.elementsByTagName("transition");
188     max = transitions.count();
189     for (int i = 0; i < max; i++) {
190         QDomElement tr = transitions.at(i).toElement();
191         if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") {
192             QDomElement property = m_document.createElement("property");
193             property.setAttribute("name", "internal_added");
194             QDomText value = m_document.createTextNode("237");
195             property.appendChild(value);
196             tr.appendChild(property);
197         }
198     }
199
200     // move transitions after tracks
201     for (int i = 0; i < max; i++) {
202         tractor.insertAfter(transitions.at(0), QDomNode());
203     }
204
205     // change producer names
206     QDomNodeList producers = m_document.elementsByTagName("producer");
207     max = producers.count();
208     for (int i = 0; i < max; i++) {
209         QDomNode prod = producers.at(i);
210         prod.toElement().setTagName("kdenlive_producer");
211         //kdenlivedoc.insertBefore(prod, QDomNode());
212     }
213
214
215     kDebug() << "/////////////////  CONVERTED DOC:";
216     kDebug() << m_document.toString();
217     kDebug() << "/////////////////  END CONVERTED DOC:";
218 }
219
220 QDomElement KdenliveDoc::documentInfoXml() {
221     QDomDocument doc;
222     QDomElement e;
223     QDomElement addedXml = doc.createElement("kdenlivedoc");
224     addedXml.setAttribute("version", "0.7");
225     addedXml.setAttribute("profile", profilePath());
226     QList <DocClipBase*> list = m_clipManager->documentClipList();
227     for (int i = 0; i < list.count(); i++) {
228         e = list.at(i)->toXML();
229         e.setTagName("kdenlive_producer");
230         addedXml.appendChild(doc.importNode(e, true));
231     }
232
233     //kDebug() << m_document.toString();
234     return addedXml;
235 }
236
237
238 ClipManager *KdenliveDoc::clipManager() {
239     return m_clipManager;
240 }
241
242 KUrl KdenliveDoc::projectFolder() const {
243     if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
244     return m_projectFolder;
245 }
246
247 QString KdenliveDoc::getDocumentStandard() {
248     //WARNING: this way to tell the video standard is a bit hackish...
249     if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
250     return "NTSC";
251 }
252
253 QString KdenliveDoc::profilePath() const {
254     return m_profile.path;
255 }
256
257 void KdenliveDoc::setProfilePath(QString path) {
258     KdenliveSettings::setCurrent_profile(path);
259     m_profile = ProfilesDialog::getVideoProfile(path);
260     m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
261     m_width = m_profile.width;
262     m_height = m_profile.height;
263     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
264     else m_timecode.setFormat((int) m_fps);
265 }
266
267 void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
268     emit thumbsProgress(url, progress);
269 }
270
271 KUndoStack *KdenliveDoc::commandStack() {
272     return m_commandStack;
273 }
274
275 void KdenliveDoc::setRenderer(Render *render) {
276     m_render = render;
277     if (m_render) m_render->setSceneList(m_scenelist);
278 }
279
280 Render *KdenliveDoc::renderer() {
281     return m_render;
282 }
283
284 void KdenliveDoc::updateClip(int id) {
285     emit updateClipDisplay(id);
286 }
287
288 int KdenliveDoc::getFramePos(QString duration) {
289     return m_timecode.getFrameCount(duration, m_fps);
290 }
291
292 QString KdenliveDoc::producerName(int id) {
293     QString result = "unnamed";
294     QDomNodeList prods = producersList();
295     int ct = prods.count();
296     for (int i = 0; i <  ct ; i++) {
297         QDomElement e = prods.item(i).toElement();
298         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
299             result = e.attribute("name");
300             if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
301             break;
302         }
303     }
304     return result;
305 }
306
307 void KdenliveDoc::setProducerDuration(int id, int duration) {
308     QDomNodeList prods = producersList();
309     int ct = prods.count();
310     for (int i = 0; i <  ct ; i++) {
311         QDomElement e = prods.item(i).toElement();
312         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
313             e.setAttribute("duration", QString::number(duration));
314             break;
315         }
316     }
317 }
318
319 int KdenliveDoc::getProducerDuration(int id) {
320     int result = 0;
321     QDomNodeList prods = producersList();
322     int ct = prods.count();
323     for (int i = 0; i <  ct ; i++) {
324         QDomElement e = prods.item(i).toElement();
325         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
326             result = e.attribute("duration").toInt();
327             break;
328         }
329     }
330     return result;
331 }
332
333
334 QDomDocument KdenliveDoc::generateSceneList() {
335     QDomDocument doc;
336     QDomElement westley = doc.createElement("westley");
337     doc.appendChild(westley);
338     QDomElement prod = doc.createElement("producer");
339 }
340
341 QDomDocument KdenliveDoc::toXml() const {
342     return m_document;
343 }
344
345 Timecode KdenliveDoc::timecode() const {
346     return m_timecode;
347 }
348
349 QDomNodeList KdenliveDoc::producersList() {
350     return m_document.elementsByTagName("producer");
351 }
352
353 void KdenliveDoc::backupMltPlaylist() {
354     if (m_render) m_scenelist = m_render->sceneList();
355 }
356
357 double KdenliveDoc::fps() const {
358     return m_fps;
359 }
360
361 int KdenliveDoc::width() const {
362     return m_width;
363 }
364
365 int KdenliveDoc::height() const {
366     return m_height;
367 }
368
369 KUrl KdenliveDoc::url() const {
370     return m_url;
371 }
372
373 void KdenliveDoc::setUrl(KUrl url) {
374     m_url = url;
375 }
376
377 void KdenliveDoc::setModified(bool mod) {
378     if (mod == m_modified) return;
379     m_modified = mod;
380     emit docModified(m_modified);
381 }
382
383 bool KdenliveDoc::isModified() const {
384     return m_modified;
385 }
386
387 QString KdenliveDoc::description() const {
388     if (m_url.isEmpty())
389         return i18n("Untitled") + " / " + m_profile.description;
390     else
391         return m_url.fileName() + " / " + m_profile.description;
392 }
393
394 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
395     DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
396     kDebug() << "/////////  DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
397     m_clipManager->addClip(clip);
398     emit addProjectClip(clip);
399 }
400
401 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
402     emit addProjectFolder(foldername, clipId, false, edit);
403 }
404
405 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
406     emit addProjectFolder(foldername, clipId, true);
407 }
408
409 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
410     for (int i = 0; i < ids.size(); ++i) {
411         emit deletTimelineClip(ids.at(i));
412         m_clipManager->slotDeleteClip(ids.at(i));
413     }
414     setModified(true);
415 }
416
417 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
418     QMapIterator<QString, int> i(map);
419     while (i.hasNext()) {
420         i.next();
421         slotDeleteFolder(i.key(), i.value());
422     }
423     setModified(true);
424 }
425
426 void KdenliveDoc::deleteClip(const uint clipId) {
427     emit signalDeleteProjectClip(clipId);
428     m_clipManager->deleteClip(clipId);
429 }
430
431 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
432     kDebug() << "/////////  DOCUM, ADD CLP: " << url;
433     m_clipManager->slotAddClipFile(url, group, groupId);
434     setModified(true);
435 }
436
437 void KdenliveDoc::slotAddTextClipFile(const QString path, const QString group, const int groupId) {
438     kDebug() << "/////////  DOCUM, ADD TXT CLP: " << path;
439     m_clipManager->slotAddTextClipFile(path, group, groupId);
440     setModified(true);
441 }
442
443 void KdenliveDoc::slotAddFolder(const QString folderName) {
444     AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
445     commandStack()->push(command);
446     setModified(true);
447 }
448
449 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
450     AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
451     commandStack()->push(command);
452     setModified(true);
453 }
454
455 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
456     EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
457     commandStack()->push(command);
458     setModified(true);
459 }
460
461 int KdenliveDoc::getFreeClipId() {
462     return m_clipManager->getFreeClipId();
463 }
464
465 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
466     return m_clipManager->getClipById(clipId);
467 }
468
469 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
470     m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
471     setModified(true);
472 }
473
474 void KdenliveDoc::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, bool loop, const QString group, const int groupId) {
475     m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, group, groupId);
476     setModified(true);
477 }
478
479 void KdenliveDoc::slotCreateTextClip(QString group, int groupId) {
480     QString titlesFolder = projectFolder().path() + "/titles/";
481     KStandardDirs::makeDir(titlesFolder);
482     TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, 0);
483     if (dia_ui->exec() == QDialog::Accepted) {
484         QString titleName = "title";
485         int counter = 0;
486         QString path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
487         while (QFile::exists(path + ".kdenlivetitle")) {
488             counter++;
489             path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
490         }
491         QPixmap pix = dia_ui->renderedPixmap();
492         pix.save(path + ".png");
493         dia_ui->saveTitle(path + ".kdenlivetitle");
494         slotAddTextClipFile(path, QString(), -1);
495     }
496     delete dia_ui;
497 }
498
499 void KdenliveDoc::editTextClip(QString path, int id) {
500     TitleWidget *dia_ui = new TitleWidget(KUrl(path + ".kdenlivetitle"), path, m_render, 0);
501     if (dia_ui->exec() == QDialog::Accepted) {
502         QPixmap pix = dia_ui->renderedPixmap();
503         pix.save(path + ".png");
504         dia_ui->saveTitle(path + ".kdenlivetitle");
505         //slotAddClipFile(KUrl("/tmp/kdenlivetitle.png"), QString(), -1);
506         emit refreshClipThumbnail(id);
507     }
508     delete dia_ui;
509 }
510
511
512 #include "kdenlivedoc.moc"
513