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