]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
0d7fd96c12314001950de8a9c0d7448614c0f57a
[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, 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);
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             }
52             KIO::NetAccess::removeTempFile(tmpFile);
53         } else {
54             KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
55         }
56     } else {
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);
68
69
70         /*QDomElement westley = m_document.createElement("westley");
71         m_document.appendChild(westley);*/
72
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         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);
101
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);
113         }
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);
123
124     }
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);
129 }
130
131 KdenliveDoc::~KdenliveDoc() {
132     delete m_commandStack;
133     delete m_clipManager;
134 }
135
136 QDomElement KdenliveDoc::documentInfoXml() {
137     //QDomDocument doc;
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();
143 }
144
145
146 ClipManager *KdenliveDoc::clipManager() {
147     return m_clipManager;
148 }
149
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";
153     return "NTSC";
154 }
155
156 QString KdenliveDoc::profilePath() const {
157     return m_profile.path;
158 }
159
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);
168 }
169
170 void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
171     emit thumbsProgress(url, progress);
172 }
173
174 KUndoStack *KdenliveDoc::commandStack() {
175     return m_commandStack;
176 }
177
178 void KdenliveDoc::setRenderer(Render *render) {
179     m_render = render;
180     if (m_render) m_render->setSceneList(m_scenelist);
181 }
182
183 Render *KdenliveDoc::renderer() {
184     return m_render;
185 }
186
187 void KdenliveDoc::updateClip(int id) {
188     emit updateClipDisplay(id);
189 }
190
191 int KdenliveDoc::getFramePos(QString duration) {
192     return m_timecode.getFrameCount(duration, m_fps);
193 }
194
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();
204             break;
205         }
206     }
207     return result;
208 }
209
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));
217             break;
218         }
219     }
220 }
221
222 int KdenliveDoc::getProducerDuration(int id) {
223     int result = 0;
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();
230             break;
231         }
232     }
233     return result;
234 }
235
236
237 QDomDocument KdenliveDoc::generateSceneList() {
238     QDomDocument doc;
239     QDomElement westley = doc.createElement("westley");
240     doc.appendChild(westley);
241     QDomElement prod = doc.createElement("producer");
242 }
243
244 QDomDocument KdenliveDoc::toXml() const {
245     return m_document;
246 }
247
248 Timecode KdenliveDoc::timecode() const {
249     return m_timecode;
250 }
251
252 QDomNodeList KdenliveDoc::producersList() {
253     return m_document.elementsByTagName("producer");
254 }
255
256 void KdenliveDoc::backupMltPlaylist() {
257     if (m_render) m_scenelist = m_render->sceneList();
258 }
259
260 double KdenliveDoc::fps() const {
261     return m_fps;
262 }
263
264 int KdenliveDoc::width() const {
265     return m_width;
266 }
267
268 int KdenliveDoc::height() const {
269     return m_height;
270 }
271
272 KUrl KdenliveDoc::url() const {
273     return m_url;
274 }
275
276 void KdenliveDoc::setUrl(KUrl url) {
277     m_url = url;
278 }
279
280 void KdenliveDoc::setModified(bool mod) {
281     if (mod == m_modified) return;
282     m_modified = mod;
283     emit docModified(m_modified);
284 }
285
286 bool KdenliveDoc::isModified() {
287     return m_modified;
288 }
289
290 QString KdenliveDoc::description() const {
291     if (m_url.isEmpty())
292         return i18n("Untitled") + " / " + m_profile.description;
293     else
294         return m_url.fileName() + " / " + m_profile.description;
295 }
296
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);
302 }
303
304 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
305     emit addProjectFolder(foldername, clipId, false, edit);
306 }
307
308 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
309     emit addProjectFolder(foldername, clipId, true);
310 }
311
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));
316     }
317     setModified(true);
318 }
319
320 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
321     QMapIterator<QString, int> i(map);
322     while (i.hasNext()) {
323         i.next();
324         slotDeleteFolder(i.key(), i.value());
325     }
326     setModified(true);
327 }
328
329 void KdenliveDoc::deleteClip(const uint clipId) {
330     emit signalDeleteProjectClip(clipId);
331     m_clipManager->deleteClip(clipId);
332 }
333
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);
337     setModified(true);
338 }
339
340 void KdenliveDoc::slotAddFolder(const QString folderName) {
341     AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
342     commandStack()->push(command);
343     setModified(true);
344 }
345
346 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
347     AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
348     commandStack()->push(command);
349     setModified(true);
350 }
351
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);
355     setModified(true);
356 }
357
358 int KdenliveDoc::getFreeClipId() {
359     return m_clipManager->getFreeClipId();
360 }
361
362 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
363     return m_clipManager->getClipById(clipId);
364 }
365
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);
368     setModified(true);
369 }
370
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);
377     }
378     delete dia_ui;
379 }
380
381
382 #include "kdenlivedoc.moc"
383