]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
35e50d319e7a2c278032ceb27ba8a83a9d25a34a
[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
31 KdenliveDoc::KdenliveDoc(const KUrl &url, MltVideoProfile profile, 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_projectName(NULL), m_commandStack(new KUndoStack()) {
32     m_clipManager = new ClipManager(this);
33     if (!url.isEmpty()) {
34         QString tmpFile;
35         if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
36             QFile file(tmpFile);
37             m_document.setContent(&file, false);
38             file.close();
39             m_projectName = url.fileName();
40             KIO::NetAccess::removeTempFile(tmpFile);
41         } else {
42             KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
43         }
44     } else {
45         // Creating new document
46         QDomElement westley = m_document.createElement("westley");
47         m_document.appendChild(westley);
48         QDomElement doc = m_document.createElement("kdenlivedoc");
49         doc.setAttribute("version", "0.6");
50         westley.appendChild(doc);
51         QDomElement props = m_document.createElement("properties");
52         doc.setAttribute("width", m_width);
53         doc.setAttribute("height", m_height);
54         doc.setAttribute("projectfps", m_fps);
55         doc.appendChild(props);
56
57
58         /*QDomElement westley = m_document.createElement("westley");
59         m_document.appendChild(westley);*/
60
61
62         QDomElement tractor = m_document.createElement("tractor");
63         QDomElement multitrack = m_document.createElement("multitrack");
64         QDomElement playlist = m_document.createElement("playlist");
65         QDomElement producer = m_document.createElement("producer");
66         /*producer.setAttribute("mlt_service", "colour");
67         producer.setAttribute("colour", "red");
68         playlist.appendChild(producer);*/
69         multitrack.appendChild(playlist);
70         QDomElement playlist1 = m_document.createElement("playlist");
71         playlist1.setAttribute("id", "playlist1");
72         playlist1.setAttribute("hide", "video");
73         multitrack.appendChild(playlist1);
74         QDomElement playlist2 = m_document.createElement("playlist");
75         playlist2.setAttribute("id", "playlist2");
76         playlist2.setAttribute("hide", "video");
77         multitrack.appendChild(playlist2);
78         QDomElement playlist3 = m_document.createElement("playlist");
79         multitrack.appendChild(playlist3);
80         playlist3.setAttribute("id", "playlist3");
81         QDomElement playlist4 = m_document.createElement("playlist");
82         multitrack.appendChild(playlist4);
83         playlist4.setAttribute("id", "playlist4");
84         QDomElement playlist5 = m_document.createElement("playlist");
85         multitrack.appendChild(playlist5);
86         playlist5.setAttribute("id", "playlist5");
87         tractor.appendChild(multitrack);
88
89         for (uint i = 2; i < 6 ; i++) {
90             QDomElement transition = m_document.createElement("transition");
91             transition.setAttribute("in", "0");
92             //TODO: Make audio mix last for all project duration
93             transition.setAttribute("out", "15000");
94             transition.setAttribute("a_track", QString::number(1));
95             transition.setAttribute("b_track", QString::number(i));
96             transition.setAttribute("mlt_service", "mix");
97             transition.setAttribute("combine", "1");
98             tractor.appendChild(transition);
99         }
100
101         doc.appendChild(tractor);
102
103     }
104     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
105     else m_timecode.setFormat((int) m_fps);
106 }
107
108 KdenliveDoc::~KdenliveDoc() {
109     delete m_commandStack;
110     delete m_clipManager;
111 }
112
113 ClipManager *KdenliveDoc::clipManager() {
114     return m_clipManager;
115 }
116
117 QString KdenliveDoc::profilePath() {
118     return m_profile.path;
119 }
120
121 void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
122     emit thumbsProgress(url, progress);
123 }
124
125 KUndoStack *KdenliveDoc::commandStack() {
126     return m_commandStack;
127 }
128
129 void KdenliveDoc::setRenderer(Render *render) {
130     m_render = render;
131     if (m_render) m_render->setSceneList(m_document);
132 }
133
134 Render *KdenliveDoc::renderer() {
135     return m_render;
136 }
137
138 void KdenliveDoc::updateClip(int id) {
139     emit updateClipDisplay(id);
140 }
141
142 int KdenliveDoc::getFramePos(QString duration) {
143     return m_timecode.getFrameCount(duration, m_fps);
144 }
145
146 QString KdenliveDoc::producerName(int id) {
147     QString result = "unnamed";
148     QDomNodeList prods = producersList();
149     int ct = prods.count();
150     for (int i = 0; i <  ct ; i++) {
151         QDomElement e = prods.item(i).toElement();
152         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
153             result = e.attribute("name");
154             if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
155             break;
156         }
157     }
158     return result;
159 }
160
161 void KdenliveDoc::setProducerDuration(int id, int duration) {
162     QDomNodeList prods = producersList();
163     int ct = prods.count();
164     for (int i = 0; i <  ct ; i++) {
165         QDomElement e = prods.item(i).toElement();
166         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
167             e.setAttribute("duration", QString::number(duration));
168             break;
169         }
170     }
171 }
172
173 int KdenliveDoc::getProducerDuration(int id) {
174     int result = 0;
175     QDomNodeList prods = producersList();
176     int ct = prods.count();
177     for (int i = 0; i <  ct ; i++) {
178         QDomElement e = prods.item(i).toElement();
179         if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
180             result = e.attribute("duration").toInt();
181             break;
182         }
183     }
184     return result;
185 }
186
187
188 QDomDocument KdenliveDoc::generateSceneList() {
189     QDomDocument doc;
190     QDomElement westley = doc.createElement("westley");
191     doc.appendChild(westley);
192     QDomElement prod = doc.createElement("producer");
193 }
194
195 QDomDocument KdenliveDoc::toXml() {
196     return m_document;
197 }
198
199 Timecode KdenliveDoc::timecode() {
200     return m_timecode;
201 }
202
203 QString KdenliveDoc::documentName() {
204     return m_projectName;
205 }
206
207 QDomNodeList KdenliveDoc::producersList() {
208     return m_document.elementsByTagName("producer");
209 }
210
211 void KdenliveDoc::setProducers(QDomElement doc) {
212     QDomNode kdenlivedocument = m_document.elementsByTagName("kdenlivedoc").item(0);
213
214     QDomNodeList list = m_document.elementsByTagName("producer");
215     int ct = list.count();
216     kDebug() << "DELETING CHILD PRODUCERS: " << ct;
217     for (int i = ct; i > 0; i--) {
218         kdenlivedocument.removeChild(list.item(i));
219     }
220
221     QDomNode n = doc.firstChild();
222     ct = 0;
223     while (!n.isNull()) {
224         QDomElement e = n.toElement(); // try to convert the node to an element.
225         if (!e.isNull() && e.tagName() == "producer") {
226             kdenlivedocument.appendChild(m_document.importNode(e, true));
227             ct++;
228         }
229         n = n.nextSibling();
230     }
231     kDebug() << "ADDING CHILD PRODS: " << ct << "\n";
232     //kDebug()<<m_document.toString();
233 }
234
235 double KdenliveDoc::fps() {
236     return m_fps;
237 }
238
239 int KdenliveDoc::width() {
240     return m_width;
241 }
242
243 int KdenliveDoc::height() {
244     return m_height;
245 }
246
247 KUrl KdenliveDoc::url() {
248     return m_url;
249 }
250
251 void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
252     kDebug() << "/////////  DOCUM, CREATING NEW CLIP, ID:" << clipId;
253     DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
254     m_clipManager->addClip(clip);
255     emit addProjectClip(clip);
256 }
257
258 void KdenliveDoc::deleteProjectClip(const uint clipId) {
259     emit deletTimelineClip(clipId);
260     m_clipManager->slotDeleteClip(clipId);
261 }
262
263 void KdenliveDoc::deleteClip(const uint clipId) {
264     emit signalDeleteProjectClip(clipId);
265     m_clipManager->deleteClip(clipId);
266 }
267
268 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group) {
269     kDebug() << "/////////  DOCUM, ADD CLP: " << url;
270     m_clipManager->slotAddClipFile(url, group);
271 }
272
273 DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
274     return m_clipManager->getClipById(clipId);
275 }
276
277 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group) {
278     m_clipManager->slotAddColorClipFile(name, color, duration, group);
279 }
280
281 #include "kdenlivedoc.moc"
282