]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
e94b0ccd6681600535dcd57cf59d41ecf3e7e89c
[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
37 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) {
38     m_clipManager = new ClipManager(this);
39     if (!url.isEmpty()) {
40         QString tmpFile;
41         if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
42             QFile file(tmpFile);
43             m_document.setContent(&file, false);
44             file.close();
45             QDomNode infoXmlNode = m_document.elementsByTagName("kdenlive").at(0);
46             if (!infoXmlNode.isNull()) {
47                 QDomElement infoXml = infoXmlNode.toElement();
48                 QString profilePath = infoXml.attribute("profile");
49                 if (!profilePath.isEmpty()) setProfilePath(profilePath);
50             }
51             KIO::NetAccess::removeTempFile(tmpFile);
52         } else {
53             KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
54         }
55     } else {
56         // Creating new document
57         QDomElement westley = m_document.createElement("westley");
58         m_document.appendChild(westley);
59         QDomElement doc = m_document.createElement("kdenlivedoc");
60         doc.setAttribute("version", "0.6");
61         westley.appendChild(doc);
62         QDomElement props = m_document.createElement("properties");
63         doc.setAttribute("width", m_width);
64         doc.setAttribute("height", m_height);
65         doc.setAttribute("projectfps", m_fps);
66         doc.appendChild(props);
67
68
69         /*QDomElement westley = m_document.createElement("westley");
70         m_document.appendChild(westley);*/
71
72
73         QDomElement tractor = m_document.createElement("tractor");
74         tractor.setAttribute("id", "maintractor");
75         QDomElement multitrack = m_document.createElement("multitrack");
76         QDomElement playlist = m_document.createElement("playlist");
77         QDomElement producer = m_document.createElement("producer");
78         /*producer.setAttribute("mlt_service", "colour");
79         producer.setAttribute("colour", "red");
80         playlist.appendChild(producer);*/
81         multitrack.appendChild(playlist);
82         QDomElement playlist1 = m_document.createElement("playlist");
83         playlist1.setAttribute("id", "playlist1");
84         playlist1.setAttribute("hide", "video");
85         multitrack.appendChild(playlist1);
86         QDomElement playlist2 = m_document.createElement("playlist");
87         playlist2.setAttribute("id", "playlist2");
88         playlist2.setAttribute("hide", "video");
89         multitrack.appendChild(playlist2);
90         QDomElement playlist3 = m_document.createElement("playlist");
91         multitrack.appendChild(playlist3);
92         playlist3.setAttribute("id", "playlist3");
93         QDomElement playlist4 = m_document.createElement("playlist");
94         multitrack.appendChild(playlist4);
95         playlist4.setAttribute("id", "playlist4");
96         QDomElement playlist5 = m_document.createElement("playlist");
97         multitrack.appendChild(playlist5);
98         playlist5.setAttribute("id", "playlist5");
99         tractor.appendChild(multitrack);
100
101         for (uint i = 2; i < 6 ; i++) {
102             QDomElement transition = m_document.createElement("transition");
103             transition.setAttribute("in", "0");
104             //TODO: Make audio mix last for all project duration
105             transition.setAttribute("out", "15000");
106             transition.setAttribute("a_track", QString::number(1));
107             transition.setAttribute("b_track", QString::number(i));
108             transition.setAttribute("mlt_service", "mix");
109             transition.setAttribute("combine", "1");
110             transition.setAttribute("internal_added", "237");
111             tractor.appendChild(transition);
112         }
113         QDomElement playlistmain = m_document.createElement("playlist");
114         playlistmain.setAttribute("id", "playlistmain");
115         QDomElement playentry = m_document.createElement("entry");
116         playentry.setAttribute("producer", "maintractor");
117         playentry.setAttribute("in", "0");
118         playentry.setAttribute("out", "15000");
119         playlistmain.appendChild(playentry);
120         doc.appendChild(tractor);
121         doc.appendChild(playlistmain);
122
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 addedXml = m_document.createElement("kdenlive");
138     addedXml.setAttribute("version", "0.7");
139     addedXml.setAttribute("profile", profilePath());
140     kDebug() << m_document.toString();
141     return m_document.documentElement();
142 }
143
144
145 ClipManager *KdenliveDoc::clipManager() {
146     return m_clipManager;
147 }
148
149 QString KdenliveDoc::getDocumentStandard() {
150     //WARNING: this way to tell the video standard is a bit hackish...
151     if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
152     return "NTSC";
153 }
154
155 QString KdenliveDoc::profilePath() const {
156     return m_profile.path;
157 }
158
159 void KdenliveDoc::setProfilePath(QString path) {
160     KdenliveSettings::setCurrent_profile(path);
161     m_profile = ProfilesDialog::getVideoProfile(path);
162     m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
163     m_width = m_profile.width;
164     m_height = m_profile.height;
165     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
166     else m_timecode.setFormat((int) m_fps);
167 }
168
169 void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
170     emit thumbsProgress(url, progress);
171 }
172
173 KUndoStack *KdenliveDoc::commandStack() {
174     return m_commandStack;
175 }
176
177 void KdenliveDoc::setRenderer(Render *render) {
178     m_render = render;
179     if (m_render) m_render->setSceneList(m_scenelist);
180 }
181
182 Render *KdenliveDoc::renderer() {
183     return m_render;
184 }
185
186 void KdenliveDoc::updateClip(int id) {
187     emit updateClipDisplay(id);
188 }
189
190 int KdenliveDoc::getFramePos(QString duration) {
191     return m_timecode.getFrameCount(duration, m_fps);
192 }
193
194 QString KdenliveDoc::producerName(int id) {
195     QString result = "unnamed";
196     QDomNodeList prods = producersList();
197     int ct = prods.count();
198     for (int i = 0; i <  ct ; i++) {
199         QDomElement e = prods.item(i).toElement();
200         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
201             result = e.attribute("name");
202             if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
203             break;
204         }
205     }
206     return result;
207 }
208
209 void KdenliveDoc::setProducerDuration(int id, int duration) {
210     QDomNodeList prods = producersList();
211     int ct = prods.count();
212     for (int i = 0; i <  ct ; i++) {
213         QDomElement e = prods.item(i).toElement();
214         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
215             e.setAttribute("duration", QString::number(duration));
216             break;
217         }
218     }
219 }
220
221 int KdenliveDoc::getProducerDuration(int id) {
222     int result = 0;
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             result = e.attribute("duration").toInt();
229             break;
230         }
231     }
232     return result;
233 }
234
235
236 QDomDocument KdenliveDoc::generateSceneList() {
237     QDomDocument doc;
238     QDomElement westley = doc.createElement("westley");
239     doc.appendChild(westley);
240     QDomElement prod = doc.createElement("producer");
241 }
242
243 QDomDocument KdenliveDoc::toXml() const {
244     return m_document;
245 }
246
247 Timecode KdenliveDoc::timecode() const {
248     return m_timecode;
249 }
250
251 QDomNodeList KdenliveDoc::producersList() {
252     return m_document.elementsByTagName("producer");
253 }
254
255 void KdenliveDoc::backupMltPlaylist() {
256     if (m_render) m_scenelist = m_render->sceneList();
257 }
258
259 double KdenliveDoc::fps() const {
260     return m_fps;
261 }
262
263 int KdenliveDoc::width() const {
264     return m_width;
265 }
266
267 int KdenliveDoc::height() const {
268     return m_height;
269 }
270
271 KUrl KdenliveDoc::url() const {
272     return m_url;
273 }
274
275 void KdenliveDoc::setUrl(KUrl url) {
276     m_url = url;
277 }
278
279 void KdenliveDoc::setModified(bool mod) {
280     if (mod == m_modified) return;
281     m_modified = mod;
282     emit docModified(m_modified);
283 }
284
285 bool KdenliveDoc::isModified() {
286     return m_modified;
287 }
288
289 QString KdenliveDoc::description() const {
290     if (m_url.isEmpty())
291         return i18n("Untitled") + " / " + m_profile.description;
292     else
293         return m_url.fileName() + " / " + m_profile.description;
294 }
295
296 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
297     DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
298     kDebug() << "/////////  DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
299     m_clipManager->addClip(clip);
300     emit addProjectClip(clip);
301 }
302
303 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
304     emit addProjectFolder(foldername, clipId, false, edit);
305 }
306
307 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
308     emit addProjectFolder(foldername, clipId, true);
309 }
310
311 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
312     for (int i = 0; i < ids.size(); ++i) {
313         emit deletTimelineClip(ids.at(i));
314         m_clipManager->slotDeleteClip(ids.at(i));
315     }
316     setModified(true);
317 }
318
319 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
320     QMapIterator<QString, int> i(map);
321     while (i.hasNext()) {
322         i.next();
323         slotDeleteFolder(i.key(), i.value());
324     }
325     setModified(true);
326 }
327
328 void KdenliveDoc::deleteClip(const uint clipId) {
329     emit signalDeleteProjectClip(clipId);
330     m_clipManager->deleteClip(clipId);
331 }
332
333 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
334     kDebug() << "/////////  DOCUM, ADD CLP: " << url;
335     m_clipManager->slotAddClipFile(url, group, groupId);
336     setModified(true);
337 }
338
339 void KdenliveDoc::slotAddFolder(const QString folderName) {
340     AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
341     commandStack()->push(command);
342     setModified(true);
343 }
344
345 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
346     AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
347     commandStack()->push(command);
348     setModified(true);
349 }
350
351 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
352     EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
353     commandStack()->push(command);
354     setModified(true);
355 }
356
357 int KdenliveDoc::getFreeClipId() {
358     return m_clipManager->getFreeClipId();
359 }
360
361 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
362     return m_clipManager->getClipById(clipId);
363 }
364
365 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
366     m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
367     setModified(true);
368 }
369
370 #include "kdenlivedoc.moc"
371