]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
folders in project view
[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             tractor.appendChild(transition);
111         }
112         QDomElement playlistmain = m_document.createElement("playlist");
113         playlistmain.setAttribute("id", "playlistmain");
114         QDomElement playentry = m_document.createElement("entry");
115         playentry.setAttribute("producer", "maintractor");
116         playentry.setAttribute("in", "0");
117         playentry.setAttribute("out", "15000");
118         playlistmain.appendChild(playentry);
119         doc.appendChild(tractor);
120         doc.appendChild(playlistmain);
121
122     }
123     m_scenelist = m_document.toString();
124     kDebug() << "scenelist" << m_scenelist;
125     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
126     else m_timecode.setFormat((int) m_fps);
127 }
128
129 KdenliveDoc::~KdenliveDoc() {
130     delete m_commandStack;
131     delete m_clipManager;
132 }
133
134 QDomElement KdenliveDoc::documentInfoXml() {
135     //QDomDocument doc;
136     QDomElement addedXml = m_document.createElement("kdenlive");
137     addedXml.setAttribute("version", "0.7");
138     addedXml.setAttribute("profile", profilePath());
139     kDebug() << m_document.toString();
140     return m_document.documentElement();
141 }
142
143
144 ClipManager *KdenliveDoc::clipManager() {
145     return m_clipManager;
146 }
147
148 QString KdenliveDoc::getDocumentStandard() {
149     //WARNING: this way to tell the video standard is a bit hackish...
150     if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
151     return "NTSC";
152 }
153
154 QString KdenliveDoc::profilePath() const {
155     return m_profile.path;
156 }
157
158 void KdenliveDoc::setProfilePath(QString path) {
159     KdenliveSettings::setCurrent_profile(path);
160     m_profile = ProfilesDialog::getVideoProfile(path);
161     m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
162     m_width = m_profile.width;
163     m_height = m_profile.height;
164     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
165     else m_timecode.setFormat((int) m_fps);
166 }
167
168 void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
169     emit thumbsProgress(url, progress);
170 }
171
172 KUndoStack *KdenliveDoc::commandStack() {
173     return m_commandStack;
174 }
175
176 void KdenliveDoc::setRenderer(Render *render) {
177     m_render = render;
178     if (m_render) m_render->setSceneList(m_scenelist);
179 }
180
181 Render *KdenliveDoc::renderer() {
182     return m_render;
183 }
184
185 void KdenliveDoc::updateClip(int id) {
186     emit updateClipDisplay(id);
187 }
188
189 int KdenliveDoc::getFramePos(QString duration) {
190     return m_timecode.getFrameCount(duration, m_fps);
191 }
192
193 QString KdenliveDoc::producerName(int id) {
194     QString result = "unnamed";
195     QDomNodeList prods = producersList();
196     int ct = prods.count();
197     for (int i = 0; i <  ct ; i++) {
198         QDomElement e = prods.item(i).toElement();
199         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
200             result = e.attribute("name");
201             if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
202             break;
203         }
204     }
205     return result;
206 }
207
208 void KdenliveDoc::setProducerDuration(int id, int duration) {
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             e.setAttribute("duration", QString::number(duration));
215             break;
216         }
217     }
218 }
219
220 int KdenliveDoc::getProducerDuration(int id) {
221     int result = 0;
222     QDomNodeList prods = producersList();
223     int ct = prods.count();
224     for (int i = 0; i <  ct ; i++) {
225         QDomElement e = prods.item(i).toElement();
226         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
227             result = e.attribute("duration").toInt();
228             break;
229         }
230     }
231     return result;
232 }
233
234
235 QDomDocument KdenliveDoc::generateSceneList() {
236     QDomDocument doc;
237     QDomElement westley = doc.createElement("westley");
238     doc.appendChild(westley);
239     QDomElement prod = doc.createElement("producer");
240 }
241
242 QDomDocument KdenliveDoc::toXml() const {
243     return m_document;
244 }
245
246 Timecode KdenliveDoc::timecode() const {
247     return m_timecode;
248 }
249
250 QDomNodeList KdenliveDoc::producersList() {
251     return m_document.elementsByTagName("producer");
252 }
253
254 void KdenliveDoc::backupMltPlaylist() {
255     if (m_render) m_scenelist = m_render->sceneList();
256 }
257
258 double KdenliveDoc::fps() const {
259     return m_fps;
260 }
261
262 int KdenliveDoc::width() const {
263     return m_width;
264 }
265
266 int KdenliveDoc::height() const {
267     return m_height;
268 }
269
270 KUrl KdenliveDoc::url() const {
271     return m_url;
272 }
273
274 void KdenliveDoc::setUrl(KUrl url) {
275     m_url = url;
276 }
277
278 void KdenliveDoc::setModified(bool mod) {
279     if (mod == m_modified) return;
280     m_modified = mod;
281     emit docModified(m_modified);
282 }
283
284 QString KdenliveDoc::description() const {
285     if (m_url.isEmpty())
286         return i18n("Untitled") + " / " + m_profile.description;
287     else
288         return m_url.fileName() + " / " + m_profile.description;
289 }
290
291 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
292     DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
293     kDebug() << "/////////  DOCUM, CREATING NEW CLIP, ID:" << clipId << ", PAR ID:" << elem.attribute("groupid");
294     m_clipManager->addClip(clip);
295     emit addProjectClip(clip);
296 }
297
298 void KdenliveDoc::addFolder(const QString foldername, int clipId, bool edit) {
299     emit addProjectFolder(foldername, clipId, false, edit);
300 }
301
302 void KdenliveDoc::deleteFolder(const QString foldername, int clipId) {
303     emit addProjectFolder(foldername, clipId, true);
304 }
305
306 void KdenliveDoc::deleteProjectClip(QList <int> ids) {
307     for (int i = 0; i < ids.size(); ++i) {
308         emit deletTimelineClip(ids.at(i));
309         m_clipManager->slotDeleteClip(ids.at(i));
310     }
311     setModified(true);
312 }
313
314 void KdenliveDoc::deleteProjectFolder(QMap <QString, int> map) {
315     QMapIterator<QString, int> i(map);
316     while (i.hasNext()) {
317         i.next();
318         slotDeleteFolder(i.key(), i.value());
319     }
320     setModified(true);
321 }
322
323 void KdenliveDoc::deleteClip(const uint clipId) {
324     emit signalDeleteProjectClip(clipId);
325     m_clipManager->deleteClip(clipId);
326 }
327
328 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int groupId) {
329     kDebug() << "/////////  DOCUM, ADD CLP: " << url;
330     m_clipManager->slotAddClipFile(url, group, groupId);
331     setModified(true);
332 }
333
334 void KdenliveDoc::slotAddFolder(const QString folderName) {
335     AddFolderCommand *command = new AddFolderCommand(this, folderName, m_clipManager->getFreeClipId(), true);
336     commandStack()->push(command);
337     setModified(true);
338 }
339
340 void KdenliveDoc::slotDeleteFolder(const QString folderName, const int id) {
341     AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
342     commandStack()->push(command);
343     setModified(true);
344 }
345
346 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, int clipId) {
347     EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
348     commandStack()->push(command);
349     setModified(true);
350 }
351
352 int KdenliveDoc::getFreeClipId() {
353     return m_clipManager->getFreeClipId();
354 }
355
356 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
357     return m_clipManager->getClipById(clipId);
358 }
359
360 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const int groupId) {
361     m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
362     setModified(true);
363 }
364
365 #include "kdenlivedoc.moc"
366