]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
* Revert to an easier MLT playlist manipulation now that MLT allows easy transition...
[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             transition.setAttribute("a_track", QString::number(1));
116             transition.setAttribute("b_track", QString::number(i));
117             transition.setAttribute("mlt_service", "mix");
118             transition.setAttribute("combine", "1");
119             transition.setAttribute("internal_added", "237");
120             tractor.appendChild(transition);
121         }
122         westley.appendChild(tractor);
123     }
124     m_scenelist = m_document.toString();
125     // kDebug() << "scenelist" << m_scenelist;
126     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
127     else m_timecode.setFormat((int) m_fps);
128 }
129
130 KdenliveDoc::~KdenliveDoc() {
131     delete m_commandStack;
132     delete m_clipManager;
133 }
134
135 QDomElement KdenliveDoc::documentInfoXml() {
136     QDomDocument doc;
137     QDomElement e;
138     QDomElement addedXml = doc.createElement("kdenlive");
139     addedXml.setAttribute("version", "0.7");
140     addedXml.setAttribute("profile", profilePath());
141     QList <DocClipBase*> list = m_clipManager->documentClipList();
142     for (int i = 0; i < list.count(); i++) {
143         e = list.at(i)->toXML();
144         e.setTagName("kdenlive_producer");
145         addedXml.appendChild(doc.importNode(e, true));
146     }
147
148     //kDebug() << m_document.toString();
149     return addedXml;
150 }
151
152
153 ClipManager *KdenliveDoc::clipManager() {
154     return m_clipManager;
155 }
156
157 KUrl KdenliveDoc::projectFolder() const {
158     if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
159     return m_projectFolder;
160 }
161
162 QString KdenliveDoc::getDocumentStandard() {
163     //WARNING: this way to tell the video standard is a bit hackish...
164     if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
165     return "NTSC";
166 }
167
168 QString KdenliveDoc::profilePath() const {
169     return m_profile.path;
170 }
171
172 void KdenliveDoc::setProfilePath(QString path) {
173     KdenliveSettings::setCurrent_profile(path);
174     m_profile = ProfilesDialog::getVideoProfile(path);
175     m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
176     m_width = m_profile.width;
177     m_height = m_profile.height;
178     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
179     else m_timecode.setFormat((int) m_fps);
180 }
181
182 void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
183     emit thumbsProgress(url, progress);
184 }
185
186 KUndoStack *KdenliveDoc::commandStack() {
187     return m_commandStack;
188 }
189
190 void KdenliveDoc::setRenderer(Render *render) {
191     m_render = render;
192     if (m_render) m_render->setSceneList(m_scenelist);
193 }
194
195 Render *KdenliveDoc::renderer() {
196     return m_render;
197 }
198
199 void KdenliveDoc::updateClip(int id) {
200     emit updateClipDisplay(id);
201 }
202
203 int KdenliveDoc::getFramePos(QString duration) {
204     return m_timecode.getFrameCount(duration, m_fps);
205 }
206
207 QString KdenliveDoc::producerName(int id) {
208     QString result = "unnamed";
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             result = e.attribute("name");
215             if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
216             break;
217         }
218     }
219     return result;
220 }
221
222 void KdenliveDoc::setProducerDuration(int id, int duration) {
223     QDomNodeList prods = producersList();
224     int ct = prods.count();
225     for (int i = 0; i <  ct ; i++) {
226         QDomElement e = prods.item(i).toElement();
227         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
228             e.setAttribute("duration", QString::number(duration));
229             break;
230         }
231     }
232 }
233
234 int KdenliveDoc::getProducerDuration(int id) {
235     int result = 0;
236     QDomNodeList prods = producersList();
237     int ct = prods.count();
238     for (int i = 0; i <  ct ; i++) {
239         QDomElement e = prods.item(i).toElement();
240         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
241             result = e.attribute("duration").toInt();
242             break;
243         }
244     }
245     return result;
246 }
247
248
249 QDomDocument KdenliveDoc::generateSceneList() {
250     QDomDocument doc;
251     QDomElement westley = doc.createElement("westley");
252     doc.appendChild(westley);
253     QDomElement prod = doc.createElement("producer");
254 }
255
256 QDomDocument KdenliveDoc::toXml() const {
257     return m_document;
258 }
259
260 Timecode KdenliveDoc::timecode() const {
261     return m_timecode;
262 }
263
264 QDomNodeList KdenliveDoc::producersList() {
265     return m_document.elementsByTagName("producer");
266 }
267
268 void KdenliveDoc::backupMltPlaylist() {
269     if (m_render) m_scenelist = m_render->sceneList();
270 }
271
272 double KdenliveDoc::fps() const {
273     return m_fps;
274 }
275
276 int KdenliveDoc::width() const {
277     return m_width;
278 }
279
280 int KdenliveDoc::height() const {
281     return m_height;
282 }
283
284 KUrl KdenliveDoc::url() const {
285     return m_url;
286 }
287
288 void KdenliveDoc::setUrl(KUrl url) {
289     m_url = url;
290 }
291
292 void KdenliveDoc::setModified(bool mod) {
293     if (mod == m_modified) return;
294     m_modified = mod;
295     emit docModified(m_modified);
296 }
297
298 bool KdenliveDoc::isModified() const {
299     return m_modified;
300 }
301
302 QString KdenliveDoc::description() const {
303     if (m_url.isEmpty())
304         return i18n("Untitled") + " / " + m_profile.description;
305     else
306         return m_url.fileName() + " / " + m_profile.description;
307 }
308
309 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
310     DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
311     kDebug() << "/////////  DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
312     m_clipManager->addClip(clip);
313     emit addProjectClip(clip);
314 }
315
316 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
317     emit addProjectFolder(foldername, clipId, false, edit);
318 }
319
320 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
321     emit addProjectFolder(foldername, clipId, true);
322 }
323
324 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
325     for (int i = 0; i < ids.size(); ++i) {
326         emit deletTimelineClip(ids.at(i));
327         m_clipManager->slotDeleteClip(ids.at(i));
328     }
329     setModified(true);
330 }
331
332 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
333     QMapIterator<QString, int> i(map);
334     while (i.hasNext()) {
335         i.next();
336         slotDeleteFolder(i.key(), i.value());
337     }
338     setModified(true);
339 }
340
341 void KdenliveDoc::deleteClip(const uint clipId) {
342     emit signalDeleteProjectClip(clipId);
343     m_clipManager->deleteClip(clipId);
344 }
345
346 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
347     kDebug() << "/////////  DOCUM, ADD CLP: " << url;
348     m_clipManager->slotAddClipFile(url, group, groupId);
349     setModified(true);
350 }
351
352 void KdenliveDoc::slotAddTextClipFile(const QString path, const QString group, const int groupId) {
353     kDebug() << "/////////  DOCUM, ADD TXT CLP: " << path;
354     m_clipManager->slotAddTextClipFile(path, group, groupId);
355     setModified(true);
356 }
357
358 void KdenliveDoc::slotAddFolder(const QString folderName) {
359     AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
360     commandStack()->push(command);
361     setModified(true);
362 }
363
364 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
365     AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
366     commandStack()->push(command);
367     setModified(true);
368 }
369
370 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
371     EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
372     commandStack()->push(command);
373     setModified(true);
374 }
375
376 int KdenliveDoc::getFreeClipId() {
377     return m_clipManager->getFreeClipId();
378 }
379
380 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
381     return m_clipManager->getClipById(clipId);
382 }
383
384 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
385     m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
386     setModified(true);
387 }
388
389 void KdenliveDoc::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, bool loop, const QString group, const int groupId) {
390     m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, group, groupId);
391     setModified(true);
392 }
393
394 void KdenliveDoc::slotCreateTextClip(QString group, int groupId) {
395     QString titlesFolder = projectFolder().path() + "/titles/";
396     KStandardDirs::makeDir(titlesFolder);
397     TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, 0);
398     if (dia_ui->exec() == QDialog::Accepted) {
399         QString titleName = "title";
400         int counter = 0;
401         QString path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
402         while (QFile::exists(path + ".kdenlivetitle")) {
403             counter++;
404             path = titlesFolder + titleName + QString::number(counter).rightJustified(3, '0', false);
405         }
406         QPixmap pix = dia_ui->renderedPixmap();
407         pix.save(path + ".png");
408         dia_ui->saveTitle(path + ".kdenlivetitle");
409         slotAddTextClipFile(path, QString(), -1);
410     }
411     delete dia_ui;
412 }
413
414 void KdenliveDoc::editTextClip(QString path, int id) {
415     TitleWidget *dia_ui = new TitleWidget(KUrl(path + ".kdenlivetitle"), path, m_render, 0);
416     if (dia_ui->exec() == QDialog::Accepted) {
417         QPixmap pix = dia_ui->renderedPixmap();
418         pix.save(path + ".png");
419         dia_ui->saveTitle(path + ".kdenlivetitle");
420         //slotAddClipFile(KUrl("/tmp/kdenlivetitle.png"), QString(), -1);
421         emit refreshClipThumbnail(id);
422     }
423     delete dia_ui;
424 }
425
426
427 #include "kdenlivedoc.moc"
428