1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "kdenlivedoc.h"
22 #include "docclipbase.h"
23 #include "profilesdialog.h"
24 #include "kdenlivesettings.h"
26 #include "clipmanager.h"
27 #include "titlewidget.h"
28 #include "mainwindow.h"
31 #include <KStandardDirs>
32 #include <KMessageBox>
34 #include <KFileDialog>
35 #include <KIO/NetAccess>
36 #include <KIO/CopyJob>
37 #include <KApplication>
39 #include <QCryptographicHash>
42 #include <mlt++/Mlt.h>
45 KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup *undoGroup, const QString &profileName, const QPoint tracks, Render *render, MainWindow *parent) :
52 m_commandStack(new QUndoStack(undoGroup)),
54 m_projectFolder(projectFolder),
55 m_documentLoadingStep(0.0),
56 m_documentLoadingProgress(0),
57 m_abortLoading(false),
61 m_clipManager = new ClipManager(this);
62 m_autoSaveTimer = new QTimer(this);
63 m_autoSaveTimer->setSingleShot(true);
66 bool success = KIO::NetAccess::download(url.path(), tmpFile, parent);
70 success = m_document.setContent(&file, false, &errorMsg);
72 if (success == false) {
73 // File is corrupted, warn user
74 KMessageBox::error(parent, errorMsg);
76 } else KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
79 QDomNode infoXmlNode = m_document.elementsByTagName("kdenlivedoc").at(0);
80 QDomNode westley = m_document.elementsByTagName("westley").at(0);
81 if (!infoXmlNode.isNull()) {
82 QDomElement infoXml = infoXmlNode.toElement();
83 double version = infoXml.attribute("version").toDouble();
85 // Upgrade old Kdenlive documents to current version
86 if (!convertDocument(version)) {
88 m_document = createEmptyDocument(tracks.x(), tracks.y());
89 setProfilePath(profileName);
92 * read again <kdenlivedoc> and <westley> to get all the new
93 * stuff (convertDocument() can now do anything without breaking
96 infoXmlNode = m_document.elementsByTagName("kdenlivedoc").at(0);
97 infoXml = infoXmlNode.toElement();
98 version = infoXml.attribute("version").toDouble();
99 westley = m_document.elementsByTagName("westley").at(0);
101 QString profilePath = infoXml.attribute("profile");
102 QString projectFolderPath = infoXml.attribute("projectfolder");
103 if (!projectFolderPath.isEmpty()) m_projectFolder = KUrl(projectFolderPath);
105 if (m_projectFolder.isEmpty() || !KIO::NetAccess::exists(m_projectFolder.path(), KIO::NetAccess::DestinationSide, parent)) {
106 // Make sure the project folder is usable
107 KMessageBox::information(parent, i18n("Document project folder is invalid, setting it to the default one: %1", KdenliveSettings::defaultprojectfolder()));
108 m_projectFolder = KUrl(KdenliveSettings::defaultprojectfolder());
110 m_startPos = infoXml.attribute("position").toInt();
111 m_zoom = infoXml.attribute("zoom", "7").toInt();
112 m_zoneEnd = infoXml.attribute("zoneout", "100").toInt();
113 setProfilePath(profilePath);
117 QDomNode tracksinfo = m_document.elementsByTagName("tracksinfo").at(0);
118 TrackInfo projectTrack;
119 if (!tracksinfo.isNull()) {
120 QDomNodeList trackslist = tracksinfo.childNodes();
121 int maxchild = trackslist.count();
122 for (int k = 0; k < maxchild; k++) {
123 e = trackslist.at(k).toElement();
124 if (e.tagName() == "trackinfo") {
125 if (e.attribute("type") == "audio") projectTrack.type = AUDIOTRACK;
126 else projectTrack.type = VIDEOTRACK;
127 projectTrack.isMute = e.attribute("mute").toInt();
128 projectTrack.isBlind = e.attribute("blind").toInt();
129 projectTrack.isLocked = e.attribute("locked").toInt();
130 m_tracksList.append(projectTrack);
133 westley.removeChild(tracksinfo);
136 QDomNodeList producers = m_document.elementsByTagName("producer");
137 QDomNodeList infoproducers = m_document.elementsByTagName("kdenlive_producer");
138 const int max = producers.count();
139 const int infomax = infoproducers.count();
141 QDomNodeList folders = m_document.elementsByTagName("folder");
142 for (int i = 0; i < folders.count(); i++) {
143 e = folders.item(i).cloneNode().toElement();
144 m_clipManager->addFolder(e.attribute("id"), e.attribute("name"));
148 m_documentLoadingStep = 100.0 / (max + infomax + m_document.elementsByTagName("entry").count());
149 parent->slotGotProgressInfo(i18n("Loading project clips"), (int) m_documentLoadingProgress);
153 for (int i = 0; i < infomax && !m_abortLoading; i++) {
154 e = infoproducers.item(i).cloneNode().toElement();
155 if (m_documentLoadingStep > 0) {
156 m_documentLoadingProgress += m_documentLoadingStep;
157 parent->slotGotProgressInfo(QString(), (int) m_documentLoadingProgress);
158 //qApp->processEvents();
160 QString prodId = e.attribute("id");
161 if (!e.isNull() && prodId != "black" && !prodId.startsWith("slowmotion") && !m_abortLoading) {
162 e.setTagName("producer");
163 // Get MLT's original producer properties
165 for (int j = 0; j < max; j++) {
166 QDomElement o = producers.item(j).cloneNode().toElement();
167 QString origId = o.attribute("id").section('_', 0, 0);
168 if (origId == prodId) {
173 addClipInfo(e, orig, prodId);
174 kDebug() << "// NLIVE PROD: " << prodId;
177 if (m_abortLoading) {
178 //parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
179 emit resetProjectList();
182 m_tracksList.clear();
183 kWarning() << "Aborted loading of: " << url.path();
184 m_document = createEmptyDocument(KdenliveSettings::videotracks(), KdenliveSettings::audiotracks());
185 setProfilePath(KdenliveSettings::default_profile());
186 m_clipManager->clear();
188 QDomNode markers = m_document.elementsByTagName("markers").at(0);
189 if (!markers.isNull()) {
190 QDomNodeList markerslist = markers.childNodes();
191 int maxchild = markerslist.count();
192 for (int k = 0; k < maxchild; k++) {
193 e = markerslist.at(k).toElement();
194 if (e.tagName() == "marker") {
195 m_clipManager->getClipById(e.attribute("id"))->addSnapMarker(GenTime(e.attribute("time").toDouble()), e.attribute("comment"));
198 westley.removeChild(markers);
200 m_document.removeChild(infoXmlNode);
201 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
205 parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
206 kWarning() << " NO KDENLIVE INFO FOUND IN FILE: " << url.path();
207 m_document = createEmptyDocument(KdenliveSettings::videotracks(), KdenliveSettings::audiotracks());
209 setProfilePath(KdenliveSettings::default_profile());
211 KIO::NetAccess::removeTempFile(tmpFile);
213 parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
215 m_document = createEmptyDocument(KdenliveSettings::videotracks(), KdenliveSettings::audiotracks());
216 setProfilePath(KdenliveSettings::default_profile());
219 m_document = createEmptyDocument(tracks.x(), tracks.y());
220 setProfilePath(profileName);
222 if (m_projectFolder.isEmpty()) m_projectFolder = KUrl(KdenliveSettings::defaultprojectfolder());
224 // make sure that the necessary folders exist
225 KStandardDirs::makeDir(m_projectFolder.path() + "/titles/");
226 KStandardDirs::makeDir(m_projectFolder.path() + "/thumbs/");
227 KStandardDirs::makeDir(m_projectFolder.path() + "/ladspa/");
229 kDebug() << "KDEnlive document, init timecode: " << m_fps;
230 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
231 else m_timecode.setFormat((int) m_fps);
233 //kDebug() << "// SETTING SCENE LIST:\n\n" << m_document.toString();
234 connect(m_autoSaveTimer, SIGNAL(timeout()), this, SLOT(slotAutoSave()));
238 KdenliveDoc::~KdenliveDoc()
240 delete m_commandStack;
241 delete m_clipManager;
242 delete m_autoSaveTimer;
244 m_autosave->remove();
249 void KdenliveDoc::setSceneList()
251 m_render->setSceneList(m_document.toString(), m_startPos);
252 //checkProjectClips();
255 QDomDocument KdenliveDoc::createEmptyDocument(const int videotracks, const int audiotracks)
257 // Creating new document
259 QDomElement westley = doc.createElement("westley");
260 doc.appendChild(westley);
263 TrackInfo videoTrack;
264 videoTrack.type = VIDEOTRACK;
265 videoTrack.isMute = false;
266 videoTrack.isBlind = false;
267 videoTrack.isLocked = false;
269 TrackInfo audioTrack;
270 audioTrack.type = AUDIOTRACK;
271 audioTrack.isMute = false;
272 audioTrack.isBlind = true;
273 audioTrack.isLocked = false;
275 QDomElement tractor = doc.createElement("tractor");
276 tractor.setAttribute("id", "maintractor");
277 QDomElement multitrack = doc.createElement("multitrack");
278 QDomElement playlist = doc.createElement("playlist");
279 playlist.setAttribute("id", "black_track");
280 westley.appendChild(playlist);
284 int total = audiotracks + videotracks + 1;
286 for (int i = 1; i < total; i++) {
287 QDomElement playlist = doc.createElement("playlist");
288 playlist.setAttribute("id", "playlist" + QString::number(i));
289 westley.appendChild(playlist);
292 QDomElement track0 = doc.createElement("track");
293 track0.setAttribute("producer", "black_track");
294 tractor.appendChild(track0);
296 // create audio tracks
297 for (int i = 1; i < audiotracks + 1; i++) {
298 QDomElement track = doc.createElement("track");
299 track.setAttribute("producer", "playlist" + QString::number(i));
300 track.setAttribute("hide", "video");
301 tractor.appendChild(track);
302 m_tracksList.append(audioTrack);
305 // create video tracks
306 for (int i = audiotracks + 1; i < total; i++) {
307 QDomElement track = doc.createElement("track");
308 track.setAttribute("producer", "playlist" + QString::number(i));
309 tractor.appendChild(track);
310 m_tracksList.append(videoTrack);
313 for (uint i = 2; i < total ; i++) {
314 QDomElement transition = doc.createElement("transition");
315 transition.setAttribute("always_active", "1");
317 QDomElement property = doc.createElement("property");
318 property.setAttribute("name", "a_track");
319 QDomText value = doc.createTextNode(QString::number(1));
320 property.appendChild(value);
321 transition.appendChild(property);
323 property = doc.createElement("property");
324 property.setAttribute("name", "b_track");
325 value = doc.createTextNode(QString::number(i));
326 property.appendChild(value);
327 transition.appendChild(property);
329 property = doc.createElement("property");
330 property.setAttribute("name", "mlt_service");
331 value = doc.createTextNode("mix");
332 property.appendChild(value);
333 transition.appendChild(property);
335 property = doc.createElement("property");
336 property.setAttribute("name", "combine");
337 value = doc.createTextNode("1");
338 property.appendChild(value);
339 transition.appendChild(property);
341 property = doc.createElement("property");
342 property.setAttribute("name", "internal_added");
343 value = doc.createTextNode("237");
344 property.appendChild(value);
345 transition.appendChild(property);
346 tractor.appendChild(transition);
348 westley.appendChild(tractor);
353 void KdenliveDoc::syncGuides(QList <Guide *> guides)
356 QDomElement guideNode = m_guidesXml.createElement("guides");
357 m_guidesXml.appendChild(guideNode);
360 for (int i = 0; i < guides.count(); i++) {
361 e = m_guidesXml.createElement("guide");
362 e.setAttribute("time", guides.at(i)->position().ms() / 1000);
363 e.setAttribute("comment", guides.at(i)->label());
364 guideNode.appendChild(e);
366 emit guidesUpdated();
369 QDomElement KdenliveDoc::guidesXml() const
371 return m_guidesXml.documentElement();
374 void KdenliveDoc::slotAutoSave()
376 if (m_render && m_autosave) {
377 if (!m_autosave->isOpen() && !m_autosave->open(QIODevice::ReadWrite)) {
378 // show error: could not open the autosave file
379 kDebug() << "ERROR; CANNOT CREATE AUTOSAVE FILE";
381 kDebug() << "// AUTOSAVE FILE: " << m_autosave->fileName();
383 if (KdenliveSettings::dropbframes()) {
384 KdenliveSettings::setDropbframes(false);
385 m_clipManager->updatePreviewSettings();
386 doc = m_render->sceneList();
387 KdenliveSettings::setDropbframes(true);
388 m_clipManager->updatePreviewSettings();
389 } else doc = m_render->sceneList();
390 saveSceneList(m_autosave->fileName(), doc);
394 void KdenliveDoc::setZoom(int factor)
399 int KdenliveDoc::zoom() const
404 bool KdenliveDoc::convertDocument(double version)
406 kDebug() << "Opening a document with version " << version;
407 const double current_version = 0.82;
409 if (version == current_version) return true;
411 if (version > current_version) {
412 kDebug() << "Unable to open document with version " << version;
413 KMessageBox::sorry(kapp->activeWindow(), i18n("This project type is unsupported (version %1) and can't be loaded.\nPlease consider upgrading you Kdenlive version.", version), i18n("Unable to open project"));
417 // Opening a old Kdenlive document
418 if (version == 0.5 || version == 0.7) {
419 KMessageBox::sorry(kapp->activeWindow(), i18n("This project type is unsupported (version %1) and can't be loaded.", version), i18n("Unable to open project"));
420 kDebug() << "Unable to open document with version " << version;
421 // TODO: convert 0.7 (0.5?) files to the new document format.
427 if (version == 0.81) {
428 // Add correct tracks info
429 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
430 QDomElement infoXml = kdenlivedoc.toElement();
431 infoXml.setAttribute("version", current_version);
432 QString currentTrackOrder = infoXml.attribute("tracks");
433 QDomElement tracksinfo = m_document.createElement("tracksinfo");
434 for (int i = 0; i < currentTrackOrder.size(); i++) {
435 QDomElement trackinfo = m_document.createElement("trackinfo");
436 if (currentTrackOrder.data()[i] == 'a') {
437 trackinfo.setAttribute("type", "audio");
438 trackinfo.setAttribute("blind", true);
439 } else trackinfo.setAttribute("blind", false);
440 trackinfo.setAttribute("mute", false);
441 trackinfo.setAttribute("locked", false);
442 tracksinfo.appendChild(trackinfo);
444 infoXml.appendChild(tracksinfo);
448 if (version == 0.8) {
449 // Add the tracks information
450 QDomNodeList tracks = m_document.elementsByTagName("track");
451 int max = tracks.count();
453 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
454 QDomElement infoXml = kdenlivedoc.toElement();
455 infoXml.setAttribute("version", current_version);
456 QDomElement tracksinfo = m_document.createElement("tracksinfo");
458 for (int i = 0; i < max; i++) {
459 QDomElement trackinfo = m_document.createElement("trackinfo");
460 QDomElement t = tracks.at(i).toElement();
461 if (t.attribute("hide") == "video") {
462 trackinfo.setAttribute("type", "audio");
463 trackinfo.setAttribute("blind", true);
464 } else trackinfo.setAttribute("blind", false);
465 trackinfo.setAttribute("mute", false);
466 trackinfo.setAttribute("locked", false);
467 if (t.attribute("producer") != "black_track") tracksinfo.appendChild(trackinfo);
469 infoXml.appendChild(tracksinfo);
473 QDomNode westley = m_document.elementsByTagName("westley").at(1);
474 QDomNode tractor = m_document.elementsByTagName("tractor").at(0);
475 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
476 QDomElement kdenlivedoc_old = kdenlivedoc.cloneNode(true).toElement(); // Needed for folders
477 QDomElement infoXml = kdenlivedoc.toElement();
478 infoXml.setAttribute("version", current_version);
479 QDomNode multitrack = m_document.elementsByTagName("multitrack").at(0);
480 QDomNodeList playlists = m_document.elementsByTagName("playlist");
482 QDomNode props = m_document.elementsByTagName("properties").at(0).toElement();
483 QString profile = props.toElement().attribute("videoprofile");
484 m_startPos = props.toElement().attribute("timeline_position").toInt();
485 if (profile == "dv_wide") profile = "dv_pal_wide";
487 // move playlists outside of tractor and add the tracks instead
488 int max = playlists.count();
489 if (westley.isNull()) {
490 westley = m_document.createElement("westley");
491 m_document.documentElement().appendChild(westley);
493 if (tractor.isNull()) {
494 kDebug() << "// NO WESTLEY PLAYLIST, building empty one";
495 QDomElement blank_tractor = m_document.createElement("tractor");
496 westley.appendChild(blank_tractor);
497 QDomElement blank_playlist = m_document.createElement("playlist");
498 blank_playlist.setAttribute("id", "black_track");
499 westley.insertBefore(blank_playlist, QDomNode());
500 QDomElement blank_track = m_document.createElement("track");
501 blank_track.setAttribute("producer", "black_track");
502 blank_tractor.appendChild(blank_track);
504 QDomNodeList kdenlivetracks = m_document.elementsByTagName("kdenlivetrack");
505 for (int i = 0; i < kdenlivetracks.count(); i++) {
506 blank_playlist = m_document.createElement("playlist");
507 blank_playlist.setAttribute("id", "playlist" + QString::number(i));
508 westley.insertBefore(blank_playlist, QDomNode());
509 blank_track = m_document.createElement("track");
510 blank_track.setAttribute("producer", "playlist" + QString::number(i));
511 blank_tractor.appendChild(blank_track);
512 if (kdenlivetracks.at(i).toElement().attribute("cliptype") == "Sound") {
513 blank_playlist.setAttribute("hide", "video");
514 blank_track.setAttribute("hide", "video");
517 } else for (int i = 0; i < max; i++) {
518 QDomNode n = playlists.at(i);
519 westley.insertBefore(n, QDomNode());
520 QDomElement pl = n.toElement();
521 QDomElement track = m_document.createElement("track");
522 QString trackType = pl.attribute("hide");
523 if (!trackType.isEmpty())
524 track.setAttribute("hide", trackType);
525 QString playlist_id = pl.attribute("id");
526 if (playlist_id.isEmpty()) {
527 playlist_id = "black_track";
528 pl.setAttribute("id", playlist_id);
530 track.setAttribute("producer", playlist_id);
531 //tractor.appendChild(track);
532 #define KEEP_TRACK_ORDER 1
533 #ifdef KEEP_TRACK_ORDER
534 tractor.insertAfter(track, QDomNode());
536 // Insert the new track in an order that hopefully matches the 3 video, then 2 audio tracks of Kdenlive 0.7.0
537 // insertion sort - O( tracks*tracks )
538 // Note, this breaks _all_ transitions - but you can move them up and down afterwards.
539 QDomElement tractor_elem = tractor.toElement();
540 if (! tractor_elem.isNull()) {
541 QDomNodeList tracks = tractor_elem.elementsByTagName("track");
542 int size = tracks.size();
544 tractor.insertAfter(track, QDomNode());
546 bool inserted = false;
547 for (int i = 0; i < size; ++i) {
548 QDomElement track_elem = tracks.at(i).toElement();
549 if (track_elem.isNull()) {
550 tractor.insertAfter(track, QDomNode());
554 kDebug() << "playlist_id: " << playlist_id << " producer:" << track_elem.attribute("producer");
555 if (playlist_id < track_elem.attribute("producer")) {
556 tractor.insertBefore(track, track_elem);
562 // Reach here, no insertion, insert last
564 tractor.insertAfter(track, QDomNode());
568 kWarning() << "tractor was not a QDomElement";
569 tractor.insertAfter(track, QDomNode());
573 tractor.removeChild(multitrack);
575 //Â audio track mixing transitions should not be added to track view, so add required attribute
576 QDomNodeList transitions = m_document.elementsByTagName("transition");
577 max = transitions.count();
578 for (int i = 0; i < max; i++) {
579 QDomElement tr = transitions.at(i).toElement();
580 if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") {
581 QDomElement property = m_document.createElement("property");
582 property.setAttribute("name", "internal_added");
583 QDomText value = m_document.createTextNode("237");
584 property.appendChild(value);
585 tr.appendChild(property);
586 property = m_document.createElement("property");
587 property.setAttribute("name", "mlt_service");
588 value = m_document.createTextNode("mix");
589 property.appendChild(value);
590 tr.appendChild(property);
592 // convert transition
593 QDomNamedNodeMap attrs = tr.attributes();
594 for (unsigned int j = 0; j < attrs.count(); j++) {
595 QString attrName = attrs.item(j).nodeName();
596 if (attrName != "in" && attrName != "out" && attrName != "id") {
597 QDomElement property = m_document.createElement("property");
598 property.setAttribute("name", attrName);
599 QDomText value = m_document.createTextNode(attrs.item(j).nodeValue());
600 property.appendChild(value);
601 tr.appendChild(property);
607 //Â move transitions after tracks
608 for (int i = 0; i < max; i++) {
609 tractor.insertAfter(transitions.at(0), QDomNode());
612 // Fix filters format
613 QDomNodeList entries = m_document.elementsByTagName("entry");
614 max = entries.count();
615 for (int i = 0; i < max; i++) {
618 QDomNode m = entries.at(i).firstChild();
619 while (!m.isNull()) {
620 if (m.toElement().tagName() == "filter") {
621 QDomElement filt = m.toElement();
622 QDomNamedNodeMap attrs = filt.attributes();
623 QString current_id = filt.attribute("kdenlive_id");
624 if (current_id != last_id) {
626 last_id = current_id;
628 QDomElement e = m_document.createElement("property");
629 e.setAttribute("name", "kdenlive_ix");
630 QDomText value = m_document.createTextNode(QString::number(effectix));
631 e.appendChild(value);
633 for (int j = 0; j < attrs.count(); j++) {
634 QDomAttr a = attrs.item(j).toAttr();
636 kDebug() << " FILTER; adding :" << a.name() << ":" << a.value();
637 QDomElement e = m_document.createElement("property");
638 e.setAttribute("name", a.name());
639 QDomText value = m_document.createTextNode(a.value());
640 e.appendChild(value);
651 QDomNodeList filters = m_document.elementsByTagName("filter");
652 max = filters.count();
655 for (int i = 0; i < max; i++) {
656 QDomElement filt = filters.at(i).toElement();
657 QDomNamedNodeMap attrs = filt.attributes();
658 QString current_id = filt.attribute("kdenlive_id");
659 if (current_id != last_id) {
661 last_id = current_id;
663 QDomElement e = m_document.createElement("property");
664 e.setAttribute("name", "kdenlive_ix");
665 QDomText value = m_document.createTextNode(QString::number(1));
666 e.appendChild(value);
668 for (int j = 0; j < attrs.count(); j++) {
669 QDomAttr a = attrs.item(j).toAttr();
671 kDebug() << " FILTER; adding :" << a.name() << ":" << a.value();
672 QDomElement e = m_document.createElement("property");
673 e.setAttribute("name", a.name());
674 QDomText value = m_document.createTextNode(a.value());
675 e.appendChild(value);
682 QDomNodeList producers = westley.toElement().elementsByTagName("producer");
683 max = producers.count();
684 for (int i = 0; i < max; i++) {
685 QDomElement prod = producers.at(i).toElement();
686 if (prod.attribute("mlt_service") == "framebuffer") {
687 QString slowmotionprod = prod.attribute("resource");
688 slowmotionprod.replace(':', '?');
689 kDebug() << "// FOUND WRONG SLOWMO, new: " << slowmotionprod;
690 prod.setAttribute("resource", slowmotionprod);
693 //Â move producers to correct place, markers to a global list, fix clip descriptions
694 QDomElement markers = m_document.createElement("markers");
695 // This will get the westley producers:
696 producers = m_document.elementsByTagName("producer");
697 max = producers.count();
698 for (int i = 0; i < max; i++) {
699 QDomElement prod = producers.at(0).toElement();
700 // add resource also as a property (to allow path correction in setNewResource())
701 // TODO: will it work with slowmotion? needs testing
702 /*if (!prod.attribute("resource").isEmpty()) {
703 QDomElement prop_resource = m_document.createElement("property");
704 prop_resource.setAttribute("name", "resource");
705 QDomText resource = m_document.createTextNode(prod.attribute("resource"));
706 prop_resource.appendChild(resource);
707 prod.appendChild(prop_resource);
709 QDomNode m = prod.firstChild();
711 if (m.toElement().tagName() == "markers") {
712 QDomNodeList prodchilds = m.childNodes();
713 int maxchild = prodchilds.count();
714 for (int k = 0; k < maxchild; k++) {
715 QDomElement mark = prodchilds.at(0).toElement();
716 mark.setAttribute("id", prod.attribute("id"));
717 markers.insertAfter(mark, QDomNode());
720 } else if (prod.attribute("type").toInt() == TEXT) {
721 // convert title clip
722 if (m.toElement().tagName() == "textclip") {
724 QDomElement titleclip = m.toElement();
725 QDomElement title = tdoc.createElement("kdenlivetitle");
726 tdoc.appendChild(title);
727 QDomNodeList objects = titleclip.childNodes();
728 int maxchild = objects.count();
729 for (int k = 0; k < maxchild; k++) {
731 QDomElement ob = objects.at(k).toElement();
732 if (ob.attribute("type") == "3") {
733 // text object - all of this goes into "xmldata"...
734 QDomElement item = tdoc.createElement("item");
735 item.setAttribute("z-index", ob.attribute("z"));
736 item.setAttribute("type", "QGraphicsTextItem");
737 QDomElement position = tdoc.createElement("position");
738 position.setAttribute("x", ob.attribute("x"));
739 position.setAttribute("y", ob.attribute("y"));
740 QDomElement content = tdoc.createElement("content");
741 content.setAttribute("font", ob.attribute("font_family"));
742 content.setAttribute("font-size", ob.attribute("font_size"));
743 content.setAttribute("font-bold", ob.attribute("bold"));
744 content.setAttribute("font-italic", ob.attribute("italic"));
745 content.setAttribute("font-underline", ob.attribute("underline"));
746 QString col = ob.attribute("color");
748 content.setAttribute("font-color", colorToString(c));
749 // todo: These fields are missing from the newly generated xmldata:
750 // transform, startviewport, endviewport, background
752 QDomText conttxt = tdoc.createTextNode(ob.attribute("text"));
753 content.appendChild(conttxt);
754 item.appendChild(position);
755 item.appendChild(content);
756 title.appendChild(item);
757 } else if (ob.attribute("type") == "5") {
759 QDomElement item = tdoc.createElement("item");
760 item.setAttribute("z-index", ob.attribute("z"));
761 item.setAttribute("type", "QGraphicsRectItem");
762 QDomElement position = tdoc.createElement("position");
763 position.setAttribute("x", ob.attribute("x"));
764 position.setAttribute("y", ob.attribute("y"));
765 QDomElement content = tdoc.createElement("content");
766 QString col = ob.attribute("color");
768 content.setAttribute("brushcolor", colorToString(c));
769 QString rect = "0,0,";
770 rect.append(ob.attribute("width"));
772 rect.append(ob.attribute("height"));
773 content.setAttribute("rect", rect);
774 item.appendChild(position);
775 item.appendChild(content);
776 title.appendChild(item);
779 prod.setAttribute("xmldata", tdoc.toString());
780 // mbd todo: This clearly does not work, as every title gets the same name - trying to leave it empty
781 // QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
782 // prod.setAttribute("titlename", titleInfo.at(0));
783 // prod.setAttribute("resource", titleInfo.at(1));
784 //kDebug()<<"TITLE DATA:\n"<<tdoc.toString();
786 } // End conversion of title clips.
788 } else if (m.isText()) {
789 QString comment = m.nodeValue();
790 if (!comment.isEmpty()) {
791 prod.setAttribute("description", comment);
796 int duration = prod.attribute("duration").toInt();
797 if (duration > 0) prod.setAttribute("out", QString::number(duration));
798 // The clip goes back in, but text clips should not go back in, at least not modified
799 westley.insertBefore(prod, QDomNode());
803 QDomNode westley0 = m_document.elementsByTagName("westley").at(0);
804 if (!markers.firstChild().isNull()) westley0.appendChild(markers);
807 // Convert as much of the kdenlivedoc as possible. Use the producer in westley
808 // First, remove the old stuff from westley, and add a new empty one
809 // Also, track the max id in order to use it for the adding of groups/folders
810 int max_kproducer_id = 0;
811 westley0.removeChild(kdenlivedoc);
812 QDomElement kdenlivedoc_new = m_document.createElement("kdenlivedoc");
813 kdenlivedoc_new.setAttribute("profile", profile);
816 QDomNodeList tracks = m_document.elementsByTagName("track");
817 max = tracks.count();
818 QDomElement tracksinfo = m_document.createElement("tracksinfo");
819 for (int i = 0; i < max; i++) {
820 QDomElement trackinfo = m_document.createElement("trackinfo");
821 QDomElement t = tracks.at(i).toElement();
822 if (t.attribute("hide") == "video") {
823 trackinfo.setAttribute("type", "audio");
824 trackinfo.setAttribute("blind", true);
825 } else trackinfo.setAttribute("blind", false);
826 trackinfo.setAttribute("mute", false);
827 trackinfo.setAttribute("locked", false);
828 if (t.attribute("producer") != "black_track") tracksinfo.appendChild(trackinfo);
830 kdenlivedoc_new.appendChild(tracksinfo);
832 // Add all the producers that has a ressource in westley
833 QDomElement westley_element = westley0.toElement();
834 if (westley_element.isNull()) {
835 kWarning() << "westley0 element in document was not a QDomElement - unable to add producers to new kdenlivedoc";
837 QDomNodeList wproducers = westley_element.elementsByTagName("producer");
838 int kmax = wproducers.count();
839 for (int i = 0; i < kmax; i++) {
840 QDomElement wproducer = wproducers.at(i).toElement();
841 if (wproducer.isNull()) {
842 kWarning() << "Found producer in westley0, that was not a QDomElement";
845 if (wproducer.attribute("id") == "black") continue;
846 // We have to do slightly different things, depending on the type
847 kDebug() << "Converting producer element with type" << wproducer.attribute("type");
848 if (wproducer.attribute("type").toInt() == TEXT) {
849 kDebug() << "Found TEXT element in producer" << endl;
850 QDomElement kproducer = wproducer.cloneNode(true).toElement();
851 kproducer.setTagName("kdenlive_producer");
852 kdenlivedoc_new.appendChild(kproducer);
853 // TODO: Perhaps needs some more changes here to "frequency", aspect ratio as a float, frame_size, channels, and later, ressource and title name
855 QDomElement kproducer = m_document.createElement("kdenlive_producer");
856 kproducer.setAttribute("id", wproducer.attribute("id"));
857 if (!wproducer.attribute("description").isEmpty())
858 kproducer.setAttribute("description", wproducer.attribute("description"));
859 kproducer.setAttribute("resource", wproducer.attribute("resource"));
860 kproducer.setAttribute("type", wproducer.attribute("type"));
861 // Testing fix for 358
862 if (!wproducer.attribute("aspect_ratio").isEmpty()) {
863 kproducer.setAttribute("aspect_ratio", wproducer.attribute("aspect_ratio"));
865 if (!wproducer.attribute("source_fps").isEmpty()) {
866 kproducer.setAttribute("fps", wproducer.attribute("source_fps"));
868 if (!wproducer.attribute("length").isEmpty()) {
869 kproducer.setAttribute("duration", wproducer.attribute("length"));
871 kdenlivedoc_new.appendChild(kproducer);
873 if (wproducer.attribute("id").toInt() > max_kproducer_id) {
874 max_kproducer_id = wproducer.attribute("id").toInt();
878 #define LOOKUP_FOLDER 1
880 // Look through all the folder elements of the old doc, for each folder, for each producer,
881 // get the id, look it up in the new doc, set the groupname and groupid
882 // Note, this does not work at the moment - at least one folders shows up missing, and clips with no folder
884 // QDomElement kdenlivedoc_old = kdenlivedoc.toElement();
885 if (!kdenlivedoc_old.isNull()) {
886 QDomNodeList folders = kdenlivedoc_old.elementsByTagName("folder");
887 int fsize = folders.size();
888 int groupId = max_kproducer_id + 1; // Start at +1 of max id of the kdenlive_producers
889 for (int i = 0; i < fsize; ++i) {
890 QDomElement folder = folders.at(i).toElement();
891 if (!folder.isNull()) {
892 QString groupName = folder.attribute("name");
893 kDebug() << "groupName: " << groupName << " with groupId: " << groupId;
894 QDomNodeList fproducers = folder.elementsByTagName("producer");
895 int psize = fproducers.size();
896 for (int j = 0; j < psize; ++j) {
897 QDomElement fproducer = fproducers.at(j).toElement();
898 if (!fproducer.isNull()) {
899 QString id = fproducer.attribute("id");
900 // This is not very effective, but compared to loading the clips, its a breeze
901 QDomNodeList kdenlive_producers = kdenlivedoc_new.elementsByTagName("kdenlive_producer");
902 int kpsize = kdenlive_producers.size();
903 for (int k = 0; k < kpsize; ++k) {
904 QDomElement kproducer = kdenlive_producers.at(k).toElement(); // Its an element for sure
905 if (id == kproducer.attribute("id")) {
906 // We do not check that it already is part of a folder
907 kproducer.setAttribute("groupid", groupId);
908 kproducer.setAttribute("groupname", groupName);
919 westley0.appendChild(kdenlivedoc_new);
921 QDomNodeList elements = westley.childNodes();
922 max = elements.count();
923 for (int i = 0; i < max; i++) {
924 QDomElement prod = elements.at(0).toElement();
925 westley0.insertAfter(prod, QDomNode());
928 westley0.removeChild(westley);
930 // experimental and probably slow
931 // adds <avfile /> information to <kdenlive_producer />
932 QDomNodeList kproducers = m_document.elementsByTagName("kdenlive_producer");
933 QDomNodeList avfiles = kdenlivedoc_old.elementsByTagName("avfile");
934 kDebug() << "found" << avfiles.count() << "<avfile />s and" << kproducers.count() << "<kdenlive_producer />s";
935 for (int i = 0; i < avfiles.count(); ++i) {
936 QDomElement avfile = avfiles.at(i).toElement();
937 QDomElement kproducer;
939 kWarning() << "found an <avfile /> that is not a QDomElement";
941 QString id = avfile.attribute("id");
942 // this is horrible, must be rewritten, it's just for test
943 for (int j = 0; j < kproducers.count(); ++j) {
944 //kDebug() << "checking <kdenlive_producer /> with id" << kproducers.at(j).toElement().attribute("id");
945 if (kproducers.at(j).toElement().attribute("id") == id) {
946 kproducer = kproducers.at(j).toElement();
950 if (kproducer == QDomElement())
951 kWarning() << "no match for <avfile /> with id =" << id;
953 //kDebug() << "ready to set additional <avfile />'s attributes (id =" << id << ")";
954 kproducer.setAttribute("channels", avfile.attribute("channels"));
955 kproducer.setAttribute("duration", avfile.attribute("duration"));
956 kproducer.setAttribute("frame_size", avfile.attribute("width") + 'x' + avfile.attribute("height"));
957 kproducer.setAttribute("frequency", avfile.attribute("frequency"));
958 if (kproducer.attribute("description").isEmpty() && !avfile.attribute("description").isEmpty())
959 kproducer.setAttribute("description", avfile.attribute("description"));
964 /*kDebug() << "///////////////// CONVERTEDÂ DOC:";
965 kDebug() << m_document.toString();
966 kDebug() << "///////////////// END CONVERTEDÂ DOC:";
968 QFile file("converted.kdenlive");
969 if (file.open(QIODevice::WriteOnly)) {
970 QTextStream stream(&file);
971 stream << m_document.toString().toUtf8();
974 kDebug() << "Unable to dump file to converted.kdenlive";
977 //kDebug() << "///////////////// END CONVERTEDÂ DOC:";
982 QString KdenliveDoc::colorToString(const QColor& c)
984 QString ret = "%1,%2,%3,%4";
985 ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
989 void KdenliveDoc::setZone(int start, int end)
995 QPoint KdenliveDoc::zone() const
997 return QPoint(m_zoneStart, m_zoneEnd);
1000 bool KdenliveDoc::saveSceneList(const QString &path, const QString &scene)
1002 QDomDocument sceneList;
1003 sceneList.setContent(scene, true);
1004 QDomNode wes = sceneList.elementsByTagName("westley").at(0);
1005 QDomElement addedXml = sceneList.createElement("kdenlivedoc");
1006 wes.appendChild(addedXml);
1008 QDomElement markers = sceneList.createElement("markers");
1009 addedXml.setAttribute("version", "0.82");
1010 addedXml.setAttribute("profile", profilePath());
1011 addedXml.setAttribute("position", m_render->seekPosition().frames(m_fps));
1012 addedXml.setAttribute("zonein", m_zoneStart);
1013 addedXml.setAttribute("zoneout", m_zoneEnd);
1014 addedXml.setAttribute("projectfolder", m_projectFolder.path());
1015 addedXml.setAttribute("zoom", m_zoom);
1018 QDomElement tracksinfo = sceneList.createElement("tracksinfo");
1019 foreach(const TrackInfo &info, m_tracksList) {
1020 QDomElement trackinfo = sceneList.createElement("trackinfo");
1021 if (info.type == AUDIOTRACK) trackinfo.setAttribute("type", "audio");
1022 trackinfo.setAttribute("mute", info.isMute);
1023 trackinfo.setAttribute("blind", info.isBlind);
1024 trackinfo.setAttribute("locked", info.isLocked);
1025 tracksinfo.appendChild(trackinfo);
1027 addedXml.appendChild(tracksinfo);
1029 // save project folders
1030 QMap <QString, QString> folderlist = m_clipManager->documentFolderList();
1032 QMapIterator<QString, QString> f(folderlist);
1033 while (f.hasNext()) {
1035 QDomElement folder = sceneList.createElement("folder");
1036 folder.setAttribute("id", f.key());
1037 folder.setAttribute("name", f.value());
1038 addedXml.appendChild(folder);
1041 // Save project clips
1043 QList <DocClipBase*> list = m_clipManager->documentClipList();
1044 for (int i = 0; i < list.count(); i++) {
1045 e = list.at(i)->toXML();
1046 e.setTagName("kdenlive_producer");
1047 addedXml.appendChild(sceneList.importNode(e, true));
1048 QList < CommentedTime > marks = list.at(i)->commentedSnapMarkers();
1049 for (int j = 0; j < marks.count(); j++) {
1050 QDomElement marker = sceneList.createElement("marker");
1051 marker.setAttribute("time", marks.at(j).time().ms() / 1000);
1052 marker.setAttribute("comment", marks.at(j).comment());
1053 marker.setAttribute("id", e.attribute("id"));
1054 markers.appendChild(marker);
1057 addedXml.appendChild(markers);
1060 if (!m_guidesXml.isNull()) addedXml.appendChild(sceneList.importNode(m_guidesXml.documentElement(), true));
1063 addedXml.appendChild(sceneList.importNode(m_clipManager->groupsXml(), true));
1065 //wes.appendChild(doc.importNode(kdenliveData, true));
1068 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
1069 kWarning() << "////// ERROR writing to file: " << path;
1070 KMessageBox::error(kapp->activeWindow(), i18n("Cannot write to file %1", path));
1074 file.write(sceneList.toString().toUtf8());
1075 if (file.error() != QFile::NoError) {
1076 KMessageBox::error(kapp->activeWindow(), i18n("Cannot write to file %1", path));
1084 ClipManager *KdenliveDoc::clipManager()
1086 return m_clipManager;
1089 KUrl KdenliveDoc::projectFolder() const
1091 //if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
1092 return m_projectFolder;
1095 void KdenliveDoc::setProjectFolder(KUrl url)
1097 if (url == m_projectFolder) return;
1099 KStandardDirs::makeDir(url.path());
1100 KStandardDirs::makeDir(url.path() + "/titles/");
1101 KStandardDirs::makeDir(url.path() + "/thumbs/");
1102 if (KMessageBox::questionYesNo(kapp->activeWindow(), i18n("You have changed the project folder. Do you want to copy the cached data from %1 to the new folder %2?").arg(m_projectFolder.path(), url.path())) == KMessageBox::Yes) moveProjectData(url);
1103 m_projectFolder = url;
1106 void KdenliveDoc::moveProjectData(KUrl url)
1108 QList <DocClipBase*> list = m_clipManager->documentClipList();
1109 //TODO: Also move ladspa effects files
1110 for (int i = 0; i < list.count(); i++) {
1111 DocClipBase *clip = list.at(i);
1112 if (clip->clipType() == TEXT) {
1113 // the image for title clip must be moved
1114 KUrl oldUrl = clip->fileURL();
1115 KUrl newUrl = KUrl(url.path() + "/titles/" + oldUrl.fileName());
1116 KIO::Job *job = KIO::copy(oldUrl, newUrl);
1117 if (KIO::NetAccess::synchronousRun(job, 0)) clip->setProperty("resource", newUrl.path());
1119 QString hash = clip->getClipHash();
1120 KUrl oldVideoThumbUrl = KUrl(m_projectFolder.path() + "/thumbs/" + hash + ".png");
1121 KUrl oldAudioThumbUrl = KUrl(m_projectFolder.path() + "/thumbs/" + hash + ".thumb");
1122 if (KIO::NetAccess::exists(oldVideoThumbUrl, KIO::NetAccess::SourceSide, 0)) {
1123 KUrl newUrl = KUrl(url.path() + "/thumbs/" + hash + ".png");
1124 KIO::Job *job = KIO::copy(oldVideoThumbUrl, newUrl);
1125 KIO::NetAccess::synchronousRun(job, 0);
1127 if (KIO::NetAccess::exists(oldAudioThumbUrl, KIO::NetAccess::SourceSide, 0)) {
1128 KUrl newUrl = KUrl(url.path() + "/thumbs/" + hash + ".thumb");
1129 KIO::Job *job = KIO::copy(oldAudioThumbUrl, newUrl);
1130 if (KIO::NetAccess::synchronousRun(job, 0)) clip->refreshThumbUrl();
1135 const QString &KdenliveDoc::profilePath() const
1137 return m_profile.path;
1140 MltVideoProfile KdenliveDoc::mltProfile() const
1145 void KdenliveDoc::setProfilePath(QString path)
1147 if (path.isEmpty()) path = KdenliveSettings::default_profile();
1148 if (path.isEmpty()) path = "dv_pal";
1149 m_profile = ProfilesDialog::getVideoProfile(path);
1150 KdenliveSettings::setProject_display_ratio((double) m_profile.display_aspect_num / m_profile.display_aspect_den);
1151 m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
1152 KdenliveSettings::setProject_fps(m_fps);
1153 m_width = m_profile.width;
1154 m_height = m_profile.height;
1155 kDebug() << "KDEnnlive document, init timecode from path: " << path << ", " << m_fps;
1156 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
1157 else m_timecode.setFormat((int) m_fps);
1160 double KdenliveDoc::dar()
1162 return (double) m_profile.display_aspect_num / m_profile.display_aspect_den;
1165 void KdenliveDoc::setThumbsProgress(const QString &message, int progress)
1167 emit progressInfo(message, progress);
1170 void KdenliveDoc::loadingProgressed()
1172 m_documentLoadingProgress += m_documentLoadingStep;
1173 emit progressInfo(QString(), (int) m_documentLoadingProgress);
1176 QUndoStack *KdenliveDoc::commandStack()
1178 return m_commandStack;
1182 void KdenliveDoc::setRenderer(Render *render) {
1183 if (m_render) return;
1185 emit progressInfo(i18n("Loading playlist..."), 0);
1186 //qApp->processEvents();
1188 m_render->setSceneList(m_document.toString(), m_startPos);
1189 kDebug() << "// SETTING SCENE LIST:\n\n" << m_document.toString();
1190 checkProjectClips();
1192 emit progressInfo(QString(), -1);
1195 void KdenliveDoc::checkProjectClips()
1197 if (m_render == NULL) return;
1198 m_clipManager->resetProducersList(m_render->producersList());
1202 QList <Mlt::Producer *> prods = m_render->producersList();
1206 for (int i = 0; i < prods.count(); i++) {
1207 id = prods.at(i)->get("id");
1208 prodId = id.section('_', 0, 0);
1209 prodTrack = id.section('_', 1, 1);
1210 DocClipBase *clip = m_clipManager->getClipById(prodId);
1211 if (clip) clip->setProducer(prods.at(i));
1212 if (clip && clip->clipType() == TEXT && !QFile::exists(clip->fileURL().path())) {
1213 // regenerate text clip image if required
1214 //kDebug() << "// TITLE: " << clip->getProperty("titlename") << " Preview file: " << clip->getProperty("resource") << " DOES NOT EXIST";
1215 QString titlename = clip->getProperty("name");
1216 QString titleresource;
1217 if (titlename.isEmpty()) {
1218 QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
1219 titlename = titleInfo.at(0);
1220 titleresource = titleInfo.at(1);
1221 clip->setProperty("name", titlename);
1222 kDebug() << "// New title set to: " << titlename;
1224 titleresource = TitleWidget::getFreeTitleInfo(projectFolder()).at(1);
1225 //titleresource = TitleWidget::getTitleResourceFromName(projectFolder(), titlename);
1227 TitleWidget *dia_ui = new TitleWidget(KUrl(), KUrl(titleresource).directory(), m_render, kapp->activeWindow());
1229 doc.setContent(clip->getProperty("xmldata"));
1230 dia_ui->setXml(doc);
1231 QImage pix = dia_ui->renderedPixmap();
1232 pix.save(titleresource);
1233 clip->setProperty("resource", titleresource);
1235 clip->producer()->set("force_reload", 1);
1240 void KdenliveDoc::updatePreviewSettings()
1242 m_clipManager->updatePreviewSettings();
1243 m_render->updatePreviewSettings();
1244 m_clipManager->resetProducersList(m_render->producersList());
1248 Render *KdenliveDoc::renderer()
1253 void KdenliveDoc::updateClip(const QString &id)
1255 emit updateClipDisplay(id);
1258 int KdenliveDoc::getFramePos(QString duration)
1260 return m_timecode.getFrameCount(duration, m_fps);
1263 QString KdenliveDoc::producerName(const QString &id)
1265 QString result = "unnamed";
1266 QDomNodeList prods = producersList();
1267 int ct = prods.count();
1268 for (int i = 0; i < ct ; i++) {
1269 QDomElement e = prods.item(i).toElement();
1270 if (e.attribute("id") != "black" && e.attribute("id") == id) {
1271 result = e.attribute("name");
1272 if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
1279 void KdenliveDoc::setProducerDuration(const QString &id, int duration)
1281 QDomNodeList prods = producersList();
1282 int ct = prods.count();
1283 for (int i = 0; i < ct ; i++) {
1284 QDomElement e = prods.item(i).toElement();
1285 if (e.attribute("id") != "black" && e.attribute("id") == id) {
1286 e.setAttribute("duration", QString::number(duration));
1292 int KdenliveDoc::getProducerDuration(const QString &id)
1295 QDomNodeList prods = producersList();
1296 int ct = prods.count();
1297 for (int i = 0; i < ct ; i++) {
1298 QDomElement e = prods.item(i).toElement();
1299 if (e.attribute("id") != "black" && e.attribute("id") == id) {
1300 result = e.attribute("duration").toInt();
1307 QDomDocument KdenliveDoc::toXml()
1312 Timecode KdenliveDoc::timecode() const
1317 QDomNodeList KdenliveDoc::producersList()
1319 return m_document.elementsByTagName("producer");
1322 double KdenliveDoc::projectDuration() const
1325 return GenTime(m_render->getLength(), m_fps).ms() / 1000;
1330 double KdenliveDoc::fps() const
1335 int KdenliveDoc::width() const
1340 int KdenliveDoc::height() const
1345 KUrl KdenliveDoc::url() const
1350 void KdenliveDoc::setUrl(KUrl url)
1355 void KdenliveDoc::setModified(bool mod)
1357 if (!m_url.isEmpty() && mod && KdenliveSettings::crashrecovery()) {
1358 m_autoSaveTimer->start(3000);
1360 if (mod == m_modified) return;
1362 emit docModified(m_modified);
1365 bool KdenliveDoc::isModified() const
1370 const QString KdenliveDoc::description() const
1372 if (m_url.isEmpty())
1373 return i18n("Untitled") + " / " + m_profile.description;
1375 return m_url.fileName() + " / " + m_profile.description;
1378 void KdenliveDoc::addClip(QDomElement elem, QString clipId, bool createClipItem)
1380 const QString producerId = clipId.section('_', 0, 0);
1381 DocClipBase *clip = m_clipManager->getClipById(producerId);
1383 elem.setAttribute("id", producerId);
1384 QString path = elem.attribute("resource");
1386 if (elem.attribute("type").toInt() == SLIDESHOW) {
1387 extension = KUrl(path).fileName();
1388 path = KUrl(path).directory();
1389 } else if (elem.attribute("type").toInt() == TEXT && QFile::exists(path) == false) {
1390 kDebug() << "// TITLE: " << elem.attribute("name") << " Preview file: " << elem.attribute("resource") << " DOES NOT EXIST";
1391 QString titlename = elem.attribute("name");
1392 QString titleresource;
1393 if (titlename.isEmpty()) {
1394 QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
1395 titlename = titleInfo.at(0);
1396 titleresource = titleInfo.at(1);
1397 elem.setAttribute("name", titlename);
1398 kDebug() << "// New title set to: " << titlename;
1400 titleresource = TitleWidget::getFreeTitleInfo(projectFolder()).at(1);
1401 //titleresource = TitleWidget::getTitleResourceFromName(projectFolder(), titlename);
1403 TitleWidget *dia_ui = new TitleWidget(KUrl(), KUrl(titleresource).directory(), m_render, kapp->activeWindow());
1405 doc.setContent(elem.attribute("xmldata"));
1406 dia_ui->setXml(doc);
1407 QImage pix = dia_ui->renderedPixmap();
1408 pix.save(titleresource);
1409 elem.setAttribute("resource", titleresource);
1410 setNewClipResource(clipId, titleresource);
1413 if (path.isEmpty() == false && QFile::exists(path) == false && elem.attribute("type").toInt() != TEXT) {
1414 kDebug() << "// FOUNDÂ MISSING CLIP: " << path << ", TYPE: " << elem.attribute("type").toInt();
1415 const QString size = elem.attribute("file_size");
1416 const QString hash = elem.attribute("file_hash");
1418 int action = KMessageBox::No;
1419 if (!size.isEmpty() && !hash.isEmpty()) {
1420 if (!m_searchFolder.isEmpty()) newpath = searchFileRecursively(m_searchFolder, size, hash);
1421 else action = (KMessageBox::ButtonCode) KMessageBox::questionYesNoCancel(kapp->activeWindow(), i18n("Clip <b>%1</b><br>is invalid, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search automatically")), KGuiItem(i18n("Keep as placeholder")));
1423 if (elem.attribute("type").toInt() == SLIDESHOW) {
1424 int res = KMessageBox::questionYesNoCancel(kapp->activeWindow(), i18n("Clip <b>%1</b><br>is invalid or missing, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search manually")), KGuiItem(i18n("Keep as placeholder")));
1425 if (res == KMessageBox::Yes)
1426 newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Looking for %1", path));
1427 else if (res == KMessageBox::Cancel) {
1428 // Abort project loading
1429 action = KMessageBox::Cancel;
1432 int res = KMessageBox::questionYesNoCancel(kapp->activeWindow(), i18n("Clip <b>%1</b><br>is invalid or missing, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search manually")), KGuiItem(i18n("Keep as placeholder")));
1433 if (res == KMessageBox::Yes)
1434 newpath = KFileDialog::getOpenFileName(KUrl("kfiledialog:///clipfolder"), QString(), kapp->activeWindow(), i18n("Looking for %1", path));
1435 else if (res == KMessageBox::Cancel) {
1436 // Abort project loading
1437 action = KMessageBox::Cancel;
1441 if (action == KMessageBox::Yes) {
1442 kDebug() << "// ASKED FOR SRCH CLIP: " << clipId;
1443 m_searchFolder = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow());
1444 if (!m_searchFolder.isEmpty()) {
1445 newpath = searchFileRecursively(QDir(m_searchFolder), size, hash);
1447 } else if (action == KMessageBox::Cancel) {
1448 m_abortLoading = true;
1451 if (!newpath.isEmpty()) {
1452 if (elem.attribute("type").toInt() == SLIDESHOW) newpath.append('/' + extension);
1453 elem.setAttribute("resource", newpath);
1454 setNewClipResource(clipId, newpath);
1458 clip = new DocClipBase(m_clipManager, elem, producerId);
1459 m_clipManager->addClip(clip);
1462 if (createClipItem) {
1463 emit addProjectClip(clip);
1464 qApp->processEvents();
1465 m_render->getFileProperties(clip->toXML(), clip->getId());
1470 void KdenliveDoc::setNewClipResource(const QString &id, const QString &path)
1472 QDomNodeList prods = m_document.elementsByTagName("producer");
1473 int maxprod = prods.count();
1474 for (int i = 0; i < maxprod; i++) {
1475 QDomNode m = prods.at(i);
1476 QString prodId = m.toElement().attribute("id");
1477 if (prodId == id || prodId.startsWith(id + '_')) {
1478 QDomNodeList params = m.childNodes();
1479 for (int j = 0; j < params.count(); j++) {
1480 QDomElement e = params.item(j).toElement();
1481 if (e.attribute("name") == "resource") {
1482 e.firstChild().setNodeValue(path);
1490 QString KdenliveDoc::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
1492 QString foundFileName;
1493 QByteArray fileData;
1494 QByteArray fileHash;
1495 QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
1496 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
1497 QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
1498 if (file.open(QIODevice::ReadOnly)) {
1499 if (QString::number(file.size()) == matchSize) {
1501 * 1 MB = 1 second per 450 files (or faster)
1502 * 10 MB = 9 seconds per 450 files (or faster)
1504 if (file.size() > 1000000*2) {
1505 fileData = file.read(1000000);
1506 if (file.seek(file.size() - 1000000))
1507 fileData.append(file.readAll());
1509 fileData = file.readAll();
1511 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
1512 if (QString(fileHash.toHex()) == matchHash)
1513 return file.fileName();
1516 kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
1518 filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
1519 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
1520 foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
1521 if (!foundFileName.isEmpty())
1524 return foundFileName;
1527 void KdenliveDoc::addClipInfo(QDomElement elem, QDomElement orig, QString clipId)
1529 DocClipBase *clip = m_clipManager->getClipById(clipId);
1531 addClip(elem, clipId, false);
1533 QMap <QString, QString> properties;
1534 QDomNamedNodeMap attributes = elem.attributes();
1536 for (unsigned int i = 0; i < attributes.count(); i++) {
1537 attrname = attributes.item(i).nodeName();
1538 if (attrname != "resource")
1539 properties.insert(attrname, attributes.item(i).nodeValue());
1540 kDebug() << attrname << " = " << attributes.item(i).nodeValue();
1542 clip->setProperties(properties);
1543 emit addProjectClip(clip, false);
1545 if (orig != QDomElement()) {
1546 QMap<QString, QString> meta;
1547 QDomNode m = orig.firstChild();
1548 while (!m.isNull()) {
1549 QString name = m.toElement().attribute("name");
1550 if (name.startsWith("meta.attr")) {
1551 meta.insert(name.section('.', 2, 3), m.firstChild().nodeValue());
1553 m = m.nextSibling();
1555 if (!meta.isEmpty()) {
1556 if (clip == NULL) clip = m_clipManager->getClipById(clipId);
1557 if (clip) clip->setMetadata(meta);
1562 void KdenliveDoc::deleteProjectClip(QList <QString> ids)
1564 for (int i = 0; i < ids.size(); ++i) {
1565 emit deleteTimelineClip(ids.at(i));
1566 m_clipManager->slotDeleteClip(ids.at(i));
1571 void KdenliveDoc::deleteClip(const QString &clipId)
1573 emit signalDeleteProjectClip(clipId);
1574 m_clipManager->deleteClip(clipId);
1577 void KdenliveDoc::slotAddClipList(const KUrl::List urls, const QString group, const QString &groupId)
1579 m_clipManager->slotAddClipList(urls, group, groupId);
1580 emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1585 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const QString &groupId)
1587 //kDebug() << "///////// DOCUM, ADD CLP: " << url;
1588 m_clipManager->slotAddClipFile(url, group, groupId);
1589 emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1593 const QString KdenliveDoc::getFreeClipId()
1595 return QString::number(m_clipManager->getFreeClipId());
1598 DocClipBase *KdenliveDoc::getBaseClip(const QString &clipId)
1600 return m_clipManager->getClipById(clipId);
1603 void KdenliveDoc::slotCreateTextClip(QString /*group*/, const QString &/*groupId*/)
1605 QString titlesFolder = projectFolder().path() + "/titles/";
1606 KStandardDirs::makeDir(titlesFolder);
1607 TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, kapp->activeWindow());
1608 if (dia_ui->exec() == QDialog::Accepted) {
1609 QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
1610 QImage pix = dia_ui->renderedPixmap();
1611 pix.save(titleInfo.at(1));
1612 //dia_ui->saveTitle(path + ".kdenlivetitle");
1613 m_clipManager->slotAddTextClipFile(titleInfo.at(0), titleInfo.at(1), dia_ui->xml().toString(), QString(), QString());
1619 int KdenliveDoc::tracksCount() const
1621 return m_tracksList.count();
1624 TrackInfo KdenliveDoc::trackInfoAt(int ix) const
1626 return m_tracksList.at(ix);
1629 void KdenliveDoc::switchTrackAudio(int ix, bool hide)
1631 m_tracksList[ix].isMute = hide; // !m_tracksList.at(ix).isMute;
1634 void KdenliveDoc::switchTrackLock(int ix, bool lock)
1636 m_tracksList[ix].isLocked = lock;
1639 bool KdenliveDoc::isTrackLocked(int ix) const
1641 return m_tracksList.at(ix).isLocked;
1644 void KdenliveDoc::switchTrackVideo(int ix, bool hide)
1646 m_tracksList[ix].isBlind = hide; // !m_tracksList.at(ix).isBlind;
1649 void KdenliveDoc::insertTrack(int ix, TrackInfo type)
1651 if (ix == -1) m_tracksList << type;
1652 else m_tracksList.insert(ix, type);
1655 void KdenliveDoc::deleteTrack(int ix)
1657 m_tracksList.removeAt(ix);
1660 void KdenliveDoc::setTrackType(int ix, TrackInfo type)
1662 m_tracksList[ix].type = type.type;
1663 m_tracksList[ix].isMute = type.isMute;
1664 m_tracksList[ix].isBlind = type.isBlind;
1665 m_tracksList[ix].isLocked = type.isLocked;
1668 const QList <TrackInfo> KdenliveDoc::tracksList() const
1670 return m_tracksList;
1673 QPoint KdenliveDoc::getTracksCount() const
1677 foreach(const TrackInfo &info, m_tracksList) {
1678 if (info.type == VIDEOTRACK) video++;
1681 return QPoint(video, audio);
1684 void KdenliveDoc::cachePixmap(const QString &fileId, const QPixmap &pix) const
1686 pix.save(m_projectFolder.path() + "/thumbs/" + fileId + ".png");
1689 QString KdenliveDoc::getLadspaFile() const
1692 QString counter = QString::number(ct).rightJustified(5, '0', false);
1693 while (QFile::exists(m_projectFolder.path() + "/ladspa/" + counter + ".ladspa")) {
1695 counter = QString::number(ct).rightJustified(5, '0', false);
1697 return m_projectFolder.path() + "/ladspa/" + counter + ".ladspa";
1700 #include "kdenlivedoc.moc"