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