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